mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 08:39:02 +03:00
refactor(ai): type Bedrock event-stream frames without casts or runtime checks (#57609)
This commit is contained in:
committed by
GitHub
Unverified
parent
e37f1a0f66
commit
863ea41364
@@ -860,7 +860,7 @@ export const protocol = Protocol.make({
|
||||
from: fromRequest,
|
||||
},
|
||||
stream: {
|
||||
event: BedrockEvent,
|
||||
event: BedrockEventStream.Frame.pipe(Schema.decodeTo(BedrockEvent)),
|
||||
initial: (request) => ({
|
||||
providerMetadataKey: request.model.route.providerMetadataKey ?? String(request.model.provider),
|
||||
tools: ToolStream.empty<number>(),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { EventStreamCodec } from "@smithy/eventstream-codec"
|
||||
import { fromUtf8, toUtf8 } from "@smithy/util-utf8"
|
||||
import { Effect, Stream } from "effect"
|
||||
import { Effect, Schema, Stream } from "effect"
|
||||
import { Base64 } from "effect/encoding"
|
||||
import { AIError, AIErrorReason, InvalidProviderOutputError } from "../schema/index.js"
|
||||
import { Framing } from "../route/framing.js"
|
||||
@@ -23,6 +23,9 @@ interface FrameBufferState {
|
||||
|
||||
const initialFrameBuffer: FrameBufferState = { buffer: new Uint8Array(0), offset: 0 }
|
||||
|
||||
export const Frame = Schema.StructWithRest(Schema.Struct({ rawBody: Schema.String }), [ProviderShared.JsonObject])
|
||||
export type Frame = typeof Frame.Type
|
||||
|
||||
type FrameInput = { readonly _tag: "Chunk"; readonly bytes: Uint8Array } | { readonly _tag: "End" }
|
||||
|
||||
const endOfStream: FrameInput = { _tag: "End" }
|
||||
@@ -55,7 +58,7 @@ const consumeFrames = (route: string) => (state: FrameBufferState, input: FrameI
|
||||
}
|
||||
|
||||
let cursor = appendChunk(state, input.bytes)
|
||||
const out: object[] = []
|
||||
const out: Frame[] = []
|
||||
while (cursor.buffer.length - cursor.offset >= 4) {
|
||||
const view = cursor.buffer.subarray(cursor.offset)
|
||||
const totalLength = new DataView(view.buffer, view.byteOffset, view.byteLength).getUint32(0, false)
|
||||
@@ -100,7 +103,7 @@ const consumeFrames = (route: string) => (state: FrameBufferState, input: FrameI
|
||||
// before handing the object to the chunk schema. JSON decode goes
|
||||
// through the shared Schema-driven codec to satisfy the package rule
|
||||
// against ad-hoc `JSON.parse` calls.
|
||||
const parsed = (yield* ProviderShared.parseJson(
|
||||
const parsed = yield* ProviderShared.parseJson(
|
||||
route,
|
||||
payload,
|
||||
"Failed to parse Bedrock Converse event-stream payload",
|
||||
@@ -111,12 +114,12 @@ const consumeFrames = (route: string) => (state: FrameBufferState, input: FrameI
|
||||
reason: AIErrorReason.make({ ...error.reason, message: error.message, cause: error.reason.cause, body }),
|
||||
}),
|
||||
),
|
||||
)) as Record<string, unknown>
|
||||
delete parsed.p
|
||||
)
|
||||
if (!ProviderShared.isRecord(parsed))
|
||||
return yield* ProviderShared.eventError(route, "Failed to parse Bedrock Converse event-stream payload", body)
|
||||
const { p: _, ...event } = parsed
|
||||
out.push({
|
||||
...(messageType === "exception"
|
||||
? { exception: { type: eventType, details: parsed } }
|
||||
: { [eventType]: parsed }),
|
||||
...(messageType === "exception" ? { exception: { type: eventType, details: event } } : { [eventType]: event }),
|
||||
rawBody: body,
|
||||
})
|
||||
}
|
||||
@@ -129,9 +132,9 @@ const consumeFrames = (route: string) => (state: FrameBufferState, input: FrameI
|
||||
* under its `:event-type` header so the chunk schema can match the JSON
|
||||
* payload directly.
|
||||
*/
|
||||
export const framing = (route: string): Framing.Definition<object> => ({
|
||||
export const framing = (route: string): Framing.Definition<Frame> => ({
|
||||
id: "aws-event-stream",
|
||||
body: (frame) => ("rawBody" in frame && typeof frame.rawBody === "string" ? frame.rawBody : undefined),
|
||||
body: (frame) => frame.rawBody,
|
||||
frame: (bytes) =>
|
||||
bytes.pipe(
|
||||
Stream.map((bytes): FrameInput => ({ _tag: "Chunk", bytes })),
|
||||
|
||||
Reference in New Issue
Block a user