Skip to content

fix(server-utils): Map Flue messages and token usage to the gen_ai conventions - #24992

Merged
JPeer264 merged 2 commits into
developfrom
jp/flue-map-messages-conventions
Oct 6, 2026
Merged

JPeer264 merged 2 commits into
developfrom
jp/flue-map-messages-conventions

Conversation

@JPeer264

@JPeer264 JPeer264 commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Flue records the messages of its turns in pi-ai's own shape, with a toolResult role and toolCall parts. Sentry's AI views only render the gen_ai conventions shape, so tool calls and tool results were missing from Flue conversations, and the tool result showed up with an invalid role. This maps the input and output messages to the conventions, the way #24352 did for Google GenAI.

The token counts come from pi-ai too. Its input leaves the cached tokens out, while gen_ai.usage.input_tokens includes them by definition, so input tokens and input cost were too low whenever prompt caching was active (the default for Anthropic models). The finish reason is mapped as well (toolUse becomes tool_call), and the tool span now records the result the model receives (Flue's effectiveResult) instead of the internal { content, details } wrapper.

The mapper lives in ai/pi-ai/ because pi-durable sends the same shapes, and its integration uses it too.

🤖 Generated with Claude Code

@JPeer264
JPeer264 added this pull request to stack #25002 October 2, 2026 13:14
@JPeer264
JPeer264 removed this pull request from stack #25002 October 2, 2026 13:31
@JPeer264
JPeer264 added this pull request to stack #25004 October 2, 2026 13:57
@JPeer264
JPeer264 force-pushed the jp/flue-map-messages-conventions branch from 3a5576b to 203f215 Compare October 3, 2026 16:23
@JPeer264 JPeer264 changed the title fix(server-utils): Map Flue messages to the gen_ai conventions fix(server-utils): Map Flue messages and token usage to the gen_ai conventions Oct 3, 2026
@JPeer264
JPeer264 removed this pull request from stack #25004 October 5, 2026 07:21
@JPeer264
JPeer264 added this pull request to stack #25029 October 5, 2026 07:22
@JPeer264
JPeer264 marked this pull request as ready for review October 5, 2026 08:48
@JPeer264
JPeer264 requested a review from a team as a code owner October 5, 2026 08:48
@JPeer264
JPeer264 requested review from andreiborza and mydea and removed request for a team October 5, 2026 08:48
@JPeer264 JPeer264 self-assigned this Oct 5, 2026
@JPeer264
JPeer264 requested a review from RulaKhaled October 5, 2026 08:49
Comment thread packages/server-utils/src/ai/flue/utils.ts
@JPeer264
JPeer264 force-pushed the jp/flue-map-messages-conventions branch 2 times, most recently from 38396de to 02668a5 Compare October 5, 2026 11:22

@andreiborza andreiborza left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM, just some comments around types.

Comment thread packages/server-utils/src/ai/flue/index.ts Outdated
Comment on lines +3 to +10
/** A message in the gen_ai conventions shape, see https://develop.sentry.dev/sdk/telemetry/traces/modules/ai-agents/. */
export interface GenAiMessage {
role: string;
parts: GenAiMessagePart[];
finish_reason?: string;
}

export type GenAiMessagePart = Record<string, unknown> & { type: string };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: I think we should find a more central place for these, maybe `packages/sever-utils/src/ai/types.ts?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved in 793f315


/** A message in the gen_ai conventions shape, see https://develop.sentry.dev/sdk/telemetry/traces/modules/ai-agents/. */
export interface GenAiMessage {
role: string;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: Can we type this stronger than string?, i.e.

Suggested change
role: string;
role: assistant | system | tool | user;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stronger with 793f315

finish_reason?: string;
}

export type GenAiMessagePart = Record<string, unknown> & { type: string };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: Can we type this stronger? i.e.

Suggested change
export type GenAiMessagePart = Record<string, unknown> & { type: string };
export type GenAiMessagePart = Record<string, unknown> & { type: blob | object | reasoning | text | tool_call | tool_call_response };

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typed in 793f315

JPeer264 and others added 2 commits October 6, 2026 09:03
…nventions

Flue records its turn messages in pi-ai's shape, with a `toolResult` role
and `toolCall` parts, and Sentry renders neither, so tool calls and tool
results were missing from the conversation views. Its token counts come
from pi-ai too, whose `input` leaves the cached tokens out while the
conventions count them in, so input tokens and input cost were too low
whenever prompt caching was active. A shared pi-ai mapper now translates
messages, usage and the finish reason to the conventions, and the tool
span records the result the model receives instead of Flue's internal
result wrapper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Comment thread packages/server-utils/src/ai/pi-ai/messages.ts
@JPeer264
JPeer264 force-pushed the jp/flue-map-messages-conventions branch from 793f315 to e8f0757 Compare October 6, 2026 07:08
@JPeer264
JPeer264 requested a review from andreiborza October 6, 2026 07:11
@JPeer264
JPeer264 merged commit 3dae6a5 into develop Oct 6, 2026
270 checks passed
@JPeer264
JPeer264 deleted the jp/flue-map-messages-conventions branch October 6, 2026 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants