Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/llm/src/schema/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Schema } from "effect"
import { ContentBlockID, FinishReason, ProtocolID, ProviderMetadata, RouteID, ToolCallID } from "./ids"
import { ContentBlockID, FinishReason, ModelID, ProtocolID, ProviderMetadata, RouteID, ToolCallID } from "./ids"
import { ModelSchema } from "./options"
import { Message, ToolCallPart, ToolOutput, ToolResultPart, ToolResultValue, type ContentPart } from "./messages"
import { ProviderFailureClassification } from "./errors"
Expand Down Expand Up @@ -184,6 +184,7 @@ export const StepFinish = Schema.Struct({
type: Schema.tag("step-finish"),
index: Schema.Number,
reason: FinishReason,
modelID: Schema.optional(ModelID),
usage: Schema.optional(Usage),
providerMetadata: Schema.optional(ProviderMetadata),
}).annotate({ identifier: "LLM.Event.StepFinish" })
Expand Down
3 changes: 2 additions & 1 deletion packages/opencode/src/session/llm/ai-sdk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FinishReason, LLMEvent, ProviderMetadata, ToolResultValue } from "@opencode-ai/llm"
import { FinishReason, LLMEvent, ModelID, ProviderMetadata, ToolResultValue } from "@opencode-ai/llm"
import { Effect, Schema } from "effect"
import { type streamText } from "ai"
import { errorMessage } from "@/util/error"
Expand Down Expand Up @@ -105,6 +105,7 @@ export function toLLMEvents(
LLMEvent.stepFinish({
index: state.step++,
reason: finishReason(event.finishReason),
modelID: ModelID.make(event.response.modelId),
usage: usage(event.usage),
providerMetadata: metadata,
}),
Expand Down
2 changes: 2 additions & 0 deletions packages/opencode/src/session/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { isRecord } from "@/util/record"
import { EventV2Bridge } from "@/event-v2-bridge"
import { Database } from "@opencode-ai/core/database/database"
import { Usage, type LLMEvent } from "@opencode-ai/llm"
import { ModelV2 } from "@opencode-ai/core/model"

const DOOM_LOOP_THRESHOLD = 3
export type Result = "compact" | "stop" | "continue"
Expand Down Expand Up @@ -464,6 +465,7 @@ const layer = Layer.effect(
messageID: ctx.assistantMessage.id,
sessionID: ctx.assistantMessage.sessionID,
type: "step-finish",
modelID: value.modelID ? ModelV2.ID.make(value.modelID) : undefined,
tokens: usage.tokens,
cost: usage.cost,
})
Expand Down
9 changes: 8 additions & 1 deletion packages/opencode/test/session/llm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,12 @@ describe("session.llm.ai-sdk adapter", () => {
},
{
type: "finish-step",
response: { id: "response-1", timestamp: new Date(0), modelId: "gpt-test" },
response: {
id: "response-1",
timestamp: new Date(0),
modelId: "gpt-test",
headers: { authorization: "must-not-be-persisted" },
},
finishReason: "other",
rawFinishReason: "other",
usage: {
Expand Down Expand Up @@ -281,6 +286,7 @@ describe("session.llm.ai-sdk adapter", () => {
type: "step-finish",
index: 0,
reason: "unknown",
modelID: "gpt-test",
usage: {
inputTokens: 10,
outputTokens: 5,
Expand All @@ -303,6 +309,7 @@ describe("session.llm.ai-sdk adapter", () => {
},
},
])
expect(events.find((event) => event.type === "step-finish")).not.toHaveProperty("responseHeaders")
})

test("creates stable block ids when AI SDK omits them", async () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/opencode/test/session/processor-effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ it.live("session.processor effect tests preserve text start time", () =>
{
id: "chatcmpl-test",
object: "chat.completion.chunk",
model: "actual-model",
choices: [{ delta: { role: "assistant" } }],
},
{
Expand Down Expand Up @@ -358,10 +359,13 @@ it.live("session.processor effect tests preserve text start time", () =>
gate.resolve()

const exit = yield* Fiber.await(run)
const text = (yield* MessageV2.parts(msg.id)).find((part): part is SessionV1.TextPart => part.type === "text")
const parts = yield* MessageV2.parts(msg.id)
const text = parts.find((part): part is SessionV1.TextPart => part.type === "text")
const finish = parts.find((part): part is SessionV1.StepFinishPart => part.type === "step-finish")

expect(Exit.isSuccess(exit)).toBe(true)
expect(text?.text).toBe("hello")
expect(finish?.modelID).toBe(ModelV2.ID.make("actual-model"))
expect(text?.time?.start).toBeDefined()
expect(text?.time?.end).toBeDefined()
if (!text?.time?.start || !text.time.end) return
Expand Down
1 change: 1 addition & 0 deletions packages/schema/src/v1/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ export const StepFinishPart = Schema.Struct({
...partBase,
type: Schema.Literal("step-finish"),
reason: Schema.String,
modelID: optional(Model.ID),
snapshot: Schema.optional(Schema.String),
cost: Schema.Finite,
tokens: Schema.Struct({
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/js/src/v2/gen/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ export type StepFinishPart = {
messageID: string
type: "step-finish"
reason: string
modelID?: string
snapshot?: string
cost: number
tokens: {
Expand Down
Loading