refactor(ai): use Schema Json codec in toolResultText and forward Bedrock stream statusCode (#57612)

This commit is contained in:
Aiden Cline
2026-10-21 13:23:25 -05:00
committed by GitHub
Unverified
parent bddf5346c8
commit e37f1a0f66
7 changed files with 64 additions and 11 deletions
+1 -1
View File
@@ -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)))),
}),
})
@@ -806,6 +806,7 @@ const step = (state: ParserState, event: BedrockEvent) =>
return yield* new AIError({
reason: classifyProviderFailure({
message,
status: event.exception.details.originalStatusCode,
rawBody: body,
}),
})
+1 -1
View File
@@ -140,7 +140,7 @@ export class UnknownProviderError extends Schema.TaggedError<UnknownProviderErro
/** A caller-supplied deadline elapsed, such as `Generation.await` polling past its `Poll.timeout`. */
export class TimeoutError extends Schema.TaggedError<TimeoutError>("AI.Error.Timeout")("Timeout", {
...ReasonFields,
timeoutMs: Schema.optional(Schema.Number),
timeoutMs: Schema.optional(NonNegativeInt),
}) {}
export const AIErrorReason = Schema.Union([
+5 -6
View File
@@ -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<typeof MessageRole>
@@ -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(
+9
View File
@@ -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 })
}),
)
@@ -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" })
}),
)
+32
View File
@@ -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<string, unknown> = {}
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)