From e37f1a0f669d92a6a5fc9bba2f52ca1c3f6f521c Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Wed, 21 Oct 2026 13:23:25 -0500 Subject: [PATCH] refactor(ai): use Schema Json codec in toolResultText and forward Bedrock stream statusCode (#57612) --- packages/ai/src/generation.ts | 2 +- packages/ai/src/protocols/bedrock-converse.ts | 1 + packages/ai/src/schema/errors.ts | 2 +- packages/ai/src/schema/messages.ts | 11 +++---- packages/ai/test/generation.test.ts | 9 ++++++ .../ai/test/provider/bedrock-converse.test.ts | 18 +++++++++-- packages/ai/test/schema.test.ts | 32 +++++++++++++++++++ 7 files changed, 64 insertions(+), 11 deletions(-) diff --git a/packages/ai/src/generation.ts b/packages/ai/src/generation.ts index 6c4d88b0dcd..6cb471ffde5 100644 --- a/packages/ai/src/generation.ts +++ b/packages/ai/src/generation.ts @@ -202,6 +202,6 @@ const timeoutError = (id: string, timeout: Duration.Duration) => new AIError({ reason: new TimeoutError({ message: `Generation ${id} did not finish within ${Duration.format(timeout)}`, - timeoutMs: Duration.toMillis(timeout), + timeoutMs: Math.min(Number.MAX_SAFE_INTEGER, Math.max(0, Math.ceil(Duration.toMillis(timeout)))), }), }) diff --git a/packages/ai/src/protocols/bedrock-converse.ts b/packages/ai/src/protocols/bedrock-converse.ts index dc9deb8759d..0eeba2f57c7 100644 --- a/packages/ai/src/protocols/bedrock-converse.ts +++ b/packages/ai/src/protocols/bedrock-converse.ts @@ -806,6 +806,7 @@ const step = (state: ParserState, event: BedrockEvent) => return yield* new AIError({ reason: classifyProviderFailure({ message, + status: event.exception.details.originalStatusCode, rawBody: body, }), }) diff --git a/packages/ai/src/schema/errors.ts b/packages/ai/src/schema/errors.ts index 9ea4d094aec..4c6ab9d1c44 100644 --- a/packages/ai/src/schema/errors.ts +++ b/packages/ai/src/schema/errors.ts @@ -140,7 +140,7 @@ export class UnknownProviderError extends Schema.TaggedError("AI.Error.Timeout")("Timeout", { ...ReasonFields, - timeoutMs: Schema.optional(Schema.Number), + timeoutMs: Schema.optional(NonNegativeInt), }) {} export const AIErrorReason = Schema.Union([ diff --git a/packages/ai/src/schema/messages.ts b/packages/ai/src/schema/messages.ts index cb27dd00e35..a9359bef1d0 100644 --- a/packages/ai/src/schema/messages.ts +++ b/packages/ai/src/schema/messages.ts @@ -1,4 +1,4 @@ -import { Schema } from "effect" +import { Option, Schema } from "effect" import { withStatics } from "@opencode/schema/schema" import { Tool } from "@opencode/schema/tool" import { @@ -15,6 +15,7 @@ import { } from "./options.js" import { ProviderID } from "./ids.js" import { Media } from "../media.js" +import { Json } from "../utils/json.js" export const MessageRole = Schema.Literals(["system", "user", "assistant", "tool"]) export type MessageRole = Schema.Schema.Type @@ -119,13 +120,11 @@ export const ToolOutput = withStatics( }, ) +const encodeJsonOption = Schema.encodeUnknownOption(Json) + const toolResultText = (value: unknown) => { if (typeof value === "string") return value - try { - return JSON.stringify(value) ?? String(value) - } catch { - return String(value) - } + return Option.getOrElse(encodeJsonOption(value), () => String(value)) } export const ToolCallPart = withStatics( diff --git a/packages/ai/test/generation.test.ts b/packages/ai/test/generation.test.ts index b3ea1be7491..40baff2b41b 100644 --- a/packages/ai/test/generation.test.ts +++ b/packages/ai/test/generation.test.ts @@ -74,8 +74,17 @@ describe("Generation", () => { expect(error).toBeInstanceOf(AIError) expect(error.reason._tag).toBe("Timeout") + if (error.reason._tag === "Timeout") expect(error.reason.timeoutMs).toBe(5000) expect(error.message).toContain("gen_1") expect(yield* Ref.get(scripted.polls)).toBeGreaterThan(1) + + const subMilliFiber = yield* Effect.forkChild( + generation.await({ poll: { interval: "1 second", timeout: "500 micros" } }).pipe(Effect.flip), + ) + yield* TestClock.adjust("1 milli") + const subMilliError = yield* Fiber.join(subMilliFiber) + + expect(subMilliError.reason).toMatchObject({ _tag: "Timeout", timeoutMs: 1 }) }), ) diff --git a/packages/ai/test/provider/bedrock-converse.test.ts b/packages/ai/test/provider/bedrock-converse.test.ts index 25158125494..887c0150ca6 100644 --- a/packages/ai/test/provider/bedrock-converse.test.ts +++ b/packages/ai/test/provider/bedrock-converse.test.ts @@ -1657,9 +1657,9 @@ describe("Bedrock Converse route", () => { }), ) - it.effect("uses originalMessage from model stream exception frames", () => + it.effect("uses originalMessage and originalStatusCode from model stream exception frames", () => Effect.gen(function* () { - const error = yield* LLMClient.generate(baseRequest).pipe( + const serverError = yield* LLMClient.generate(baseRequest).pipe( Effect.provide( fixedBytes( exceptionFrame("modelStreamErrorException", { @@ -1670,8 +1670,20 @@ describe("Bedrock Converse route", () => { ), Effect.flip, ) + const clientError = yield* LLMClient.generate(baseRequest).pipe( + Effect.provide( + fixedBytes( + exceptionFrame("modelStreamErrorException", { + originalMessage: "Upstream rejected request", + originalStatusCode: 400, + }), + ), + ), + Effect.flip, + ) - expect(error).toMatchObject({ reason: { _tag: "ProviderInternal" }, message: "Upstream model failed" }) + expect(serverError).toMatchObject({ reason: { _tag: "ProviderInternal" }, message: "Upstream model failed" }) + expect(clientError).toMatchObject({ reason: { _tag: "InvalidRequest" }, message: "Upstream rejected request" }) }), ) diff --git a/packages/ai/test/schema.test.ts b/packages/ai/test/schema.test.ts index 7c6f78546d1..351c0cde244 100644 --- a/packages/ai/test/schema.test.ts +++ b/packages/ai/test/schema.test.ts @@ -21,6 +21,8 @@ import { QuotaExceededError, RateLimitError, RouteID, + TimeoutError, + ToolOutput, ToolResultValue, TransportError, UnknownProviderError, @@ -115,6 +117,28 @@ describe("ToolResultValue", () => { }), ).toBe(true) }) + + test("fromResultValue encodes text results and falls back to String for non-JSON values", () => { + const circular: Record = {} + circular.self = circular + + expect(ToolOutput.fromResultValue({ type: "text", value: "plain" })).toEqual({ + structured: {}, + content: [{ type: "text", text: "plain" }], + }) + expect(ToolOutput.fromResultValue({ type: "text", value: { ok: true } })).toEqual({ + structured: {}, + content: [{ type: "text", text: '{"ok":true}' }], + }) + expect(ToolOutput.fromResultValue({ type: "text", value: undefined })).toEqual({ + structured: {}, + content: [{ type: "text", text: "undefined" }], + }) + expect(ToolOutput.fromResultValue({ type: "text", value: circular })).toEqual({ + structured: {}, + content: [{ type: "text", text: "[object Object]" }], + }) + }) }) describe("AI.Usage", () => { @@ -326,6 +350,7 @@ test("AI error reasons are tagged Errors with required messages", () => { new TransportError({ message: "Connection failed", transport: "http", operation: "request" }), new InvalidProviderOutputError({ message: "Invalid output" }), new UnknownProviderError({ message: "Unknown failure" }), + new TimeoutError({ message: "Timed out", timeoutMs: 5000 }), ] expect(reasons.map((reason) => reason._tag)).toEqual([ "InvalidRequest", @@ -339,7 +364,14 @@ test("AI error reasons are tagged Errors with required messages", () => { "Transport", "InvalidProviderOutput", "UnknownProvider", + "Timeout", ]) + expect(Schema.decodeUnknownOption(TimeoutError)({ _tag: "Timeout", message: "Timed out", timeoutMs: -1 })._tag).toBe( + "None", + ) + expect(Schema.decodeUnknownOption(TimeoutError)({ _tag: "Timeout", message: "Timed out", timeoutMs: 1.5 })._tag).toBe( + "None", + ) reasons.forEach((reason) => { expect(reason).toBeInstanceOf(Error) const encoded = Schema.encodeSync(AIErrorReason)(reason)