Skip to content

Commit 2b1a6bf

Browse files
committed
fix(session): align OpenAI response stream fixtures
1 parent 00656ed commit 2b1a6bf

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

packages/llm/src/schema/events.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ const llmEventTagged = Schema.Union([
222222
]).pipe(Schema.toTaggedUnion("type"))
223223

224224
type WithID<Event extends { readonly id: unknown }, ID> = Omit<Event, "type" | "id"> & { readonly id: ID | string }
225+
type WithUsage<Event extends { readonly usage?: Usage }> = Omit<Event, "type" | "usage"> & {
226+
readonly usage?: Usage | ConstructorParameters<typeof Usage>[0]
227+
}
225228

226229
const responseID = (value: ResponseID | string) => ResponseID.make(value)
227230
const contentBlockID = (value: ContentBlockID | string) => ContentBlockID.make(value)
@@ -252,8 +255,10 @@ export const LLMEvent = Object.assign(llmEventTagged, {
252255
toolCall: (input: WithID<ToolCall, ToolCallID>) => ToolCall.make({ ...input, id: toolCallID(input.id) }),
253256
toolResult: (input: WithID<ToolResult, ToolCallID>) => ToolResult.make({ ...input, id: toolCallID(input.id) }),
254257
toolError: (input: WithID<ToolError, ToolCallID>) => ToolError.make({ ...input, id: toolCallID(input.id) }),
255-
stepFinish: StepFinish.make,
256-
requestFinish: RequestFinish.make,
258+
stepFinish: (input: WithUsage<StepFinish>) =>
259+
StepFinish.make({ ...input, usage: input.usage instanceof Usage ? input.usage : new Usage(input.usage ?? {}) }),
260+
requestFinish: (input: WithUsage<RequestFinish>) =>
261+
RequestFinish.make({ ...input, usage: input.usage instanceof Usage ? input.usage : new Usage(input.usage ?? {}) }),
257262
providerError: ProviderErrorEvent.make,
258263
is: {
259264
requestStart: llmEventTagged.guards["request-start"],

packages/opencode/test/session/llm.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,18 @@ describe("session.llm.stream", () => {
581581
service_tier: null,
582582
},
583583
},
584+
{
585+
type: "response.output_item.added",
586+
output_index: 0,
587+
item: { type: "message", id: "item-1", status: "in_progress", role: "assistant", content: [] },
588+
},
589+
{
590+
type: "response.content_part.added",
591+
item_id: "item-1",
592+
output_index: 0,
593+
content_index: 0,
594+
part: { type: "output_text", text: "", annotations: [] },
595+
},
584596
{
585597
type: "response.output_text.delta",
586598
item_id: "item-1",
@@ -694,6 +706,18 @@ describe("session.llm.stream", () => {
694706
service_tier: null,
695707
},
696708
},
709+
{
710+
type: "response.output_item.added",
711+
output_index: 0,
712+
item: { type: "message", id: "item-data-url", status: "in_progress", role: "assistant", content: [] },
713+
},
714+
{
715+
type: "response.content_part.added",
716+
item_id: "item-data-url",
717+
output_index: 0,
718+
content_index: 0,
719+
part: { type: "output_text", text: "", annotations: [] },
720+
},
697721
{
698722
type: "response.output_text.delta",
699723
item_id: "item-data-url",

0 commit comments

Comments
 (0)