mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
fix(ai): report the token limit instead of a schema decode error (#60795)
This commit is contained in:
committed by
GitHub
Unverified
parent
66ee1c3ffc
commit
e98d01daf6
@@ -238,17 +238,14 @@ const decodeObject = (output: Tool, response: LLMResponse) => {
|
||||
const call = response.toolCalls.find((event) => event.name === OBJECT_TOOL)
|
||||
const invalid = (message: string, cause?: unknown) =>
|
||||
new AIError({ reason: new InvalidProviderOutputError({ message, cause }), partial: response })
|
||||
// A cut-off call arrives with truncated or empty input, so its decode error would hide the real cause.
|
||||
if (response.finishReason.normalized === "length")
|
||||
return Effect.fail(invalid("output hit the token limit before the forced tool call completed"))
|
||||
if (call)
|
||||
return output
|
||||
._decode(call.input)
|
||||
.pipe(Effect.mapError((error) => invalid(`schema decode failed: ${error.message}`, error)))
|
||||
return Effect.fail(
|
||||
invalid(
|
||||
response.finishReason.normalized === "length"
|
||||
? "output hit the token limit before the forced tool call completed"
|
||||
: `model did not call the forced \`${OBJECT_TOOL}\` tool`,
|
||||
),
|
||||
)
|
||||
return Effect.fail(invalid(`model did not call the forced \`${OBJECT_TOOL}\` tool`))
|
||||
}
|
||||
|
||||
const withObject = (output: Tool, event: Finish, state: LLMResponse.State): Effect.Effect<LLMEvent, AIError> => {
|
||||
|
||||
@@ -207,6 +207,26 @@ describe("schema on ai.stream and ai.loop", () => {
|
||||
execute: ({ value }) => Effect.succeed(value),
|
||||
})
|
||||
|
||||
scripted.effect("reports the token limit instead of a decode error when output is cut off", () =>
|
||||
Effect.gen(function* () {
|
||||
const client = yield* TestLLM.Test
|
||||
yield* client.push(
|
||||
TestLLM.complete(
|
||||
{ reason: { normalized: "length" } },
|
||||
LLMEvent.toolCall({ id: "call-1", name: "generate_object", input: {} }),
|
||||
),
|
||||
)
|
||||
|
||||
const error = yield* ai
|
||||
.generate({ model, prompt: "Pick a city.", schema: City, generation: { maxTokens: 1 } })
|
||||
.pipe(Effect.flip)
|
||||
|
||||
expect(error.message).toContain("output hit the token limit")
|
||||
expect(error.message).not.toContain("schema decode failed")
|
||||
expect(error.partial?.toolCalls[0]?.name).toBe("generate_object")
|
||||
}),
|
||||
)
|
||||
|
||||
scripted.effect("stream puts the decoded object on finish", () =>
|
||||
Effect.gen(function* () {
|
||||
const client = yield* TestLLM.Test
|
||||
|
||||
Reference in New Issue
Block a user