Forward live usage stream events
The wire schema already allowed completed-turn usage, but rejected active usage updates before clients could see them. Accept usage_updated payloads and preserve turn ids on streamed turn events.
This commit is contained in:
@@ -160,6 +160,45 @@ describe("shared messages stream parsing", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("parses agent_stream usage_updated events", () => {
|
||||||
|
const fixture = {
|
||||||
|
type: "agent_stream",
|
||||||
|
payload: {
|
||||||
|
agentId: "agent_live",
|
||||||
|
timestamp: "2026-02-08T20:10:00.000Z",
|
||||||
|
event: {
|
||||||
|
type: "usage_updated",
|
||||||
|
provider: "claude",
|
||||||
|
turnId: "foreground-turn-1",
|
||||||
|
usage: {
|
||||||
|
contextWindowUsedTokens: 24_074,
|
||||||
|
contextWindowMaxTokens: 200_000,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const parsed = AgentStreamMessageSchema.parse(fixture);
|
||||||
|
expect(parsed.payload.event).toEqual({
|
||||||
|
type: "usage_updated",
|
||||||
|
provider: "claude",
|
||||||
|
turnId: "foreground-turn-1",
|
||||||
|
usage: {
|
||||||
|
contextWindowUsedTokens: 24_074,
|
||||||
|
contextWindowMaxTokens: 200_000,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const wrapped = WSOutboundMessageSchema.parse({
|
||||||
|
type: "session",
|
||||||
|
message: fixture,
|
||||||
|
});
|
||||||
|
expect(wrapped.message.type).toBe("agent_stream");
|
||||||
|
if (wrapped.message.type === "agent_stream") {
|
||||||
|
expect(wrapped.message.payload.event.type).toBe("usage_updated");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it("parses representative sub_agent tool_call event", () => {
|
it("parses representative sub_agent tool_call event", () => {
|
||||||
const parsed = AgentStreamMessageSchema.parse({
|
const parsed = AgentStreamMessageSchema.parse({
|
||||||
type: "agent_stream",
|
type: "agent_stream",
|
||||||
|
|||||||
@@ -594,11 +594,19 @@ export const AgentStreamEventPayloadSchema = z.discriminatedUnion("type", [
|
|||||||
z.object({
|
z.object({
|
||||||
type: z.literal("turn_started"),
|
type: z.literal("turn_started"),
|
||||||
provider: AgentProviderSchema,
|
provider: AgentProviderSchema,
|
||||||
|
turnId: z.string().optional(),
|
||||||
}),
|
}),
|
||||||
z.object({
|
z.object({
|
||||||
type: z.literal("turn_completed"),
|
type: z.literal("turn_completed"),
|
||||||
provider: AgentProviderSchema,
|
provider: AgentProviderSchema,
|
||||||
usage: AgentUsageSchema.optional(),
|
usage: AgentUsageSchema.optional(),
|
||||||
|
turnId: z.string().optional(),
|
||||||
|
}),
|
||||||
|
z.object({
|
||||||
|
type: z.literal("usage_updated"),
|
||||||
|
provider: AgentProviderSchema,
|
||||||
|
usage: AgentUsageSchema,
|
||||||
|
turnId: z.string().optional(),
|
||||||
}),
|
}),
|
||||||
z.object({
|
z.object({
|
||||||
type: z.literal("turn_failed"),
|
type: z.literal("turn_failed"),
|
||||||
@@ -606,27 +614,32 @@ export const AgentStreamEventPayloadSchema = z.discriminatedUnion("type", [
|
|||||||
error: z.string(),
|
error: z.string(),
|
||||||
code: z.string().optional(),
|
code: z.string().optional(),
|
||||||
diagnostic: z.string().optional(),
|
diagnostic: z.string().optional(),
|
||||||
|
turnId: z.string().optional(),
|
||||||
}),
|
}),
|
||||||
z.object({
|
z.object({
|
||||||
type: z.literal("turn_canceled"),
|
type: z.literal("turn_canceled"),
|
||||||
provider: AgentProviderSchema,
|
provider: AgentProviderSchema,
|
||||||
reason: z.string(),
|
reason: z.string(),
|
||||||
|
turnId: z.string().optional(),
|
||||||
}),
|
}),
|
||||||
z.object({
|
z.object({
|
||||||
type: z.literal("timeline"),
|
type: z.literal("timeline"),
|
||||||
provider: AgentProviderSchema,
|
provider: AgentProviderSchema,
|
||||||
item: AgentTimelineItemPayloadSchema,
|
item: AgentTimelineItemPayloadSchema,
|
||||||
|
turnId: z.string().optional(),
|
||||||
}),
|
}),
|
||||||
z.object({
|
z.object({
|
||||||
type: z.literal("permission_requested"),
|
type: z.literal("permission_requested"),
|
||||||
provider: AgentProviderSchema,
|
provider: AgentProviderSchema,
|
||||||
request: AgentPermissionRequestPayloadSchema,
|
request: AgentPermissionRequestPayloadSchema,
|
||||||
|
turnId: z.string().optional(),
|
||||||
}),
|
}),
|
||||||
z.object({
|
z.object({
|
||||||
type: z.literal("permission_resolved"),
|
type: z.literal("permission_resolved"),
|
||||||
provider: AgentProviderSchema,
|
provider: AgentProviderSchema,
|
||||||
requestId: z.string(),
|
requestId: z.string(),
|
||||||
resolution: AgentPermissionResponseSchema,
|
resolution: AgentPermissionResponseSchema,
|
||||||
|
turnId: z.string().optional(),
|
||||||
}),
|
}),
|
||||||
z.object({
|
z.object({
|
||||||
type: z.literal("attention_required"),
|
type: z.literal("attention_required"),
|
||||||
|
|||||||
Reference in New Issue
Block a user