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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export function mapOpenAICompatibleFinishReason(
case "tool_calls":
return "tool-calls"
default:
return "other"
return "unknown" as any // kilocode_change
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV3 {
unified: "other",
raw: undefined,
}
let receivedFinishReason = false // kilocode_change
const usage: {
completionTokens: number | undefined
completionTokensDetails: {
Expand Down Expand Up @@ -453,6 +454,7 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV3 {
const choice = value.choices[0]

if (choice?.finish_reason != null) {
receivedFinishReason = true // kilocode_change
finishReason = {
unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
raw: choice.finish_reason ?? undefined,
Expand Down Expand Up @@ -645,6 +647,12 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV3 {
},

flush(controller) {
// kilocode_change start
if (!receivedFinishReason && !isFirstChunk) {
finishReason = { unified: "unknown" as any, raw: undefined }
}
// kilocode_change end

if (isActiveReasoning) {
controller.enqueue({
type: "reasoning-end",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export function mapOpenAIResponseFinishReason({
case "content_filter":
return "content-filter"
default:
return hasFunctionCall ? "tool-calls" : "other"
return (hasFunctionCall ? "tool-calls" : "unknown") as any // kilocode_change
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,7 @@ export class OpenAIResponsesLanguageModel implements LanguageModelV3 {
unified: "other",
raw: undefined,
}
let receivedFinishChunk = false // kilocode_change
const usage: {
inputTokens: number | undefined
outputTokens: number | undefined
Expand Down Expand Up @@ -1259,6 +1260,7 @@ export class OpenAIResponsesLanguageModel implements LanguageModelV3 {
})
}
} else if (isResponseFinishedChunk(value)) {
receivedFinishChunk = true // kilocode_change
finishReason = {
unified: mapOpenAIResponseFinishReason({
finishReason: value.response.incomplete_details?.reason,
Expand Down Expand Up @@ -1305,6 +1307,12 @@ export class OpenAIResponsesLanguageModel implements LanguageModelV3 {
currentTextId = null
}

// kilocode_change start
if (!receivedFinishChunk && responseId !== null) {
finishReason = { unified: "unknown" as any, raw: undefined }
}
// kilocode_change end

const providerMetadata: SharedV3ProviderMetadata = {
openai: {
responseId,
Expand Down
4 changes: 4 additions & 0 deletions packages/opencode/src/session/message-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,10 @@ export function fromError(
).toObject()
case OutputLengthError.isInstance(e):
return e
// kilocode_change start
case APIError.isInstance(e):
return e.toObject()
// kilocode_change end
case LoadAPIKeyError.isInstance(e):
return new AuthError(
{
Expand Down
14 changes: 14 additions & 0 deletions packages/opencode/src/session/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,20 @@ export const layer: Layer.Layer<
Stream.takeUntil(() => ctx.needsCompaction),
Stream.runDrain,
)

// kilocode_change start
if (ctx.assistantMessage.finish === "unknown") {
const parts = MessageV2.parts(ctx.assistantMessage.id)
if (!parts.some((p) => p.type === "tool" && p.state.status === "completed")) {
yield* Effect.fail(
new MessageV2.APIError({
message: "Stream terminated prematurely without a finish reason",
isRetryable: true,
})
)
}
}
// kilocode_change end
}).pipe(
Effect.onInterrupt(() =>
Effect.gen(function* () {
Expand Down
2 changes: 1 addition & 1 deletion packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
return "break" as const
}

const finished = handle.message.finish && !["tool-calls", "unknown"].includes(handle.message.finish)
const finished = handle.message.finish && !["tool-calls", "unknown"].includes(handle.message.finish) // kilocode_change
if (finished && !handle.message.error) {
if (format.type === "json_schema") {
handle.message.error = new MessageV2.StructuredOutputError({
Expand Down
Loading