refactor(ai): share Responses hosted tool streaming (#61141)

This commit is contained in:
Aiden Cline
2026-11-01 20:22:37 -06:00
committed by GitHub
Unverified
parent 8d18959811
commit c6571b007e
8 changed files with 78 additions and 104 deletions
+19 -29
View File
@@ -38,7 +38,7 @@ const adapter = {
} satisfies OpenResponses.ProviderAdapter
const tools = {
web_search_call: { name: "web_search", input: (item) => item.action ?? {} },
web_search_call: ResponsesHostedTools.common.web_search_call,
code_interpreter_call: { name: "code_interpreter", input: (item) => ({ code: item.code }) },
} satisfies ResponsesHostedTools.Definitions
@@ -63,34 +63,24 @@ export const protocol = Protocol.make({
}
}),
},
stream: {
event: OpenResponses.protocol.stream.event,
initial: (req) => OpenResponses.initial(req, adapter),
step: (state, input) =>
Effect.gen(function* () {
const event = OpenResponses.normalize(state, input)
if (event.type !== "response.output_item.done" || !event.item) return yield* OpenResponses.step(state, event)
if (event.item.type === "web_extractor_call") {
const item = yield* Schema.decodeUnknownEffect(WebExtractorItem)(event.item).pipe(
Effect.mapError((cause) =>
ProviderShared.eventError(
adapter.id,
"Alibaba returned an invalid web extraction item",
ProviderShared.encodeJson(event),
cause,
),
),
)
return yield* ResponsesHostedTools.onDone(state, item, {
web_extractor_call: { name: "web_extractor", input: () => ({ urls: item.urls, goal: item.goal }) },
})
}
if (ResponsesHostedTools.isItem(event.item, tools))
return yield* ResponsesHostedTools.onDone(state, event.item, tools)
return yield* OpenResponses.step(state, event)
}),
terminal: OpenResponses.terminal,
},
stream: ResponsesHostedTools.stream(adapter, tools, (state, event) => {
if (event.type !== "response.output_item.done" || event.item?.type !== "web_extractor_call") return undefined
return Schema.decodeUnknownEffect(WebExtractorItem)(event.item).pipe(
Effect.mapError((cause) =>
ProviderShared.eventError(
adapter.id,
"Alibaba returned an invalid web extraction item",
ProviderShared.encodeJson(event),
cause,
),
),
Effect.flatMap((item) =>
ResponsesHostedTools.onDone(state, item, {
web_extractor_call: { name: "web_extractor", input: () => ({ urls: item.urls, goal: item.goal }) },
}),
),
)
}),
})
export * as AlibabaResponses from "./alibaba-responses.js"
+1 -1
View File
@@ -127,7 +127,7 @@ const isFunctionCallOutput = (
item.type === "function_call_output" || item.type === "custom_tool_call_output"
const HOSTED_TOOLS = {
web_search_call: { name: "web_search", input: (item) => item.action ?? {} },
web_search_call: ResponsesHostedTools.common.web_search_call,
image_generation_call: {
name: "image_generation",
input: () => ({}),
+1 -4
View File
@@ -1290,10 +1290,7 @@ const onOutputItemDone = Effect.fnUntraced(function* (state: ParserState, item:
namespace: item.namespace,
providerMetadata: metadata,
})
const result =
input === undefined
? yield* ToolStream.finish(state.id, tools, item.id)
: yield* ToolStream.finishWithInput(state.id, tools, item.id, input)
const result = yield* ToolStream.finish(state.id, tools, item.id, input)
const events: LLMEvent[] = []
const finished = [...(closed?.events ?? []), ...(result.events ?? [])]
// A done-only call never streamed a start event, so open its lifecycle here.
+7 -27
View File
@@ -315,44 +315,24 @@ const hostedToolResult = Effect.fnUntraced(function* (item: ResponsesHostedTools
})
const HOSTED_TOOLS = {
web_search_call: { name: "web_search", input: (item) => item.action ?? {} },
...ResponsesHostedTools.common,
web_search_preview_call: { name: "web_search_preview", input: (item) => item.action ?? {} },
file_search_call: { name: "file_search", input: (item) => ({ queries: item.queries ?? [] }) },
code_interpreter_call: {
name: "code_interpreter",
input: (item) => ({ code: item.code, container_id: item.container_id }),
},
computer_call: { name: "computer_use", input: (item) => item.action ?? {} },
image_generation_call: { name: "image_generation", input: () => ({}), result: hostedToolResult },
mcp_call: {
name: "mcp",
input: (item) => ({ server_label: item.server_label, name: item.name, arguments: item.arguments }),
},
} as const satisfies ResponsesHostedTools.Definitions
const step = (state: OpenResponses.ParserState, input: OpenResponses.Event) => {
const event = OpenResponses.normalize(state, input)
if (event.type === "response.reasoning_text.delta")
return event.item_id !== undefined
? Effect.succeed(OpenResponses.onReasoningDelta(state, event, event.item_id))
: ProviderShared.eventError(ADAPTER, `${event.type} is missing item_id`)
if (event.type === "response.output_item.done" && event.item && ResponsesHostedTools.isItem(event.item, HOSTED_TOOLS))
return ResponsesHostedTools.onDone(state, event.item, HOSTED_TOOLS)
return OpenResponses.step(state, event)
}
export const protocol = Protocol.make({
id: ADAPTER,
body: {
schema: OpenAIResponsesBody,
from: fromRequest,
},
stream: {
event: OpenResponses.protocol.stream.event,
initial: (request) => OpenResponses.initial(request, adapter),
step,
terminal: OpenResponses.terminal,
},
stream: ResponsesHostedTools.stream(adapter, HOSTED_TOOLS, (state, event) => {
if (event.type !== "response.reasoning_text.delta") return undefined
return event.item_id !== undefined
? Effect.succeed(OpenResponses.onReasoningDelta(state, event, event.item_id))
: ProviderShared.eventError(ADAPTER, `${event.type} is missing item_id`)
}),
supportsEffortUpdates,
})
@@ -1,5 +1,5 @@
import { Effect } from "effect"
import { LLMEvent, type AIError, type ToolResultPart } from "../../schema/index.js"
import { LLMEvent, type AIError, type LLMRequest, type ToolResultPart } from "../../schema/index.js"
import { OpenResponses } from "../open-responses.js"
import { Lifecycle } from "./lifecycle.js"
@@ -26,9 +26,48 @@ export interface Definition {
export type Definitions = Readonly<Record<string, Definition>>
/** Hosted tools that several Responses providers report with the same item shape. */
export const common = {
web_search_call: { name: "web_search", input: (item) => item.action ?? {} },
file_search_call: { name: "file_search", input: (item) => ({ queries: item.queries ?? [] }) },
code_interpreter_call: {
name: "code_interpreter",
input: (item) => ({ code: item.code, container_id: item.container_id }),
},
mcp_call: {
name: "mcp",
input: (item) => ({ server_label: item.server_label, name: item.name, arguments: item.arguments }),
},
} as const satisfies Definitions
export const isItem = <Tools extends Definitions>(item: OpenResponses.OutputItem, tools: Tools): item is Item =>
item.type in tools
/**
* Responses stream that turns completed hosted tool items into a provider-executed
* tool call and result. `override` sees each normalized event first and may handle it.
*/
export const stream = (
adapter: OpenResponses.ProviderAdapter,
tools: Definitions,
override?: (
state: OpenResponses.ParserState,
event: OpenResponses.NormalizedEvent,
) => Effect.Effect<OpenResponses.StepResult, AIError> | undefined,
) => ({
event: OpenResponses.protocol.stream.event,
initial: (request: LLMRequest) => OpenResponses.initial(request, adapter),
step: (state: OpenResponses.ParserState, input: OpenResponses.Event) => {
const event = OpenResponses.normalize(state, input)
const handled = override?.(state, event)
if (handled) return handled
if (event.type === "response.output_item.done" && event.item && isItem(event.item, tools))
return onDone(state, event.item, tools)
return OpenResponses.step(state, event)
},
terminal: OpenResponses.terminal,
})
export const onDone: (
state: OpenResponses.ParserState,
item: Item,
+3 -14
View File
@@ -204,23 +204,12 @@ export const appendExisting = <K extends StreamKey>(
* from state, and reject malformed local arguments without repairing them.
* Missing keys are a no-op because some providers emit stop events for
* non-tool content blocks.
*/
export const finish = <K extends StreamKey>(route: string, tools: State<K>, key: K) =>
Effect.gen(function* () {
const tool = tools[key]
if (!tool) return { tools }
return {
tools: withoutTool(tools, key),
events: finishEvents(tool, yield* toolCall(route, tool)),
}
})
/**
* Finalize one pending tool call with an authoritative final input string.
*
* Pass `input` when the provider sends an authoritative final input string.
* OpenAI Responses can send accumulated deltas and then repeat the completed
* arguments on `response.output_item.done`; the final value wins.
*/
export const finishWithInput = <K extends StreamKey>(route: string, tools: State<K>, key: K, input: string) =>
export const finish = <K extends StreamKey>(route: string, tools: State<K>, key: K, input?: string) =>
Effect.gen(function* () {
const tool = tools[key]
if (!tool) return { tools }
+6 -27
View File
@@ -76,29 +76,13 @@ const fromRequest = Effect.fn("XAIResponses.fromRequest")(function* (request: LL
}
})
const HOSTED_TOOLS = {
web_search_call: { name: "web_search", input: (item) => item.action ?? {} },
x_search_call: { name: "x_search", input: (item) => item.action ?? {} },
file_search_call: { name: "file_search", input: (item) => ({ queries: item.queries ?? [] }) },
code_interpreter_call: {
name: "code_interpreter",
input: (item) => ({ code: item.code, container_id: item.container_id }),
},
image_generation_call: { name: "image_generation", input: () => ({}) },
mcp_call: {
name: "mcp",
input: (item) => ({ server_label: item.server_label, name: item.name, arguments: item.arguments }),
},
} as const satisfies ResponsesHostedTools.Definitions
// Grok speaks the standard Responses reasoning dialect (`reasoning_summary_text.*`,
// handled by the baseline); only its hosted tool vocabulary differs.
const step = (state: OpenResponses.ParserState, input: OpenResponses.Event) => {
const event = OpenResponses.normalize(state, input)
if (event.type === "response.output_item.done" && event.item && ResponsesHostedTools.isItem(event.item, HOSTED_TOOLS))
return ResponsesHostedTools.onDone(state, event.item, HOSTED_TOOLS)
return OpenResponses.step(state, event)
}
const HOSTED_TOOLS = {
...ResponsesHostedTools.common,
x_search_call: { name: "x_search", input: (item) => item.action ?? {} },
image_generation_call: { name: "image_generation", input: () => ({}) },
} as const satisfies ResponsesHostedTools.Definitions
export const protocol = Protocol.make({
id: ADAPTER,
@@ -106,12 +90,7 @@ export const protocol = Protocol.make({
schema: Body,
from: fromRequest,
},
stream: {
event: OpenResponses.protocol.stream.event,
initial: (request) => OpenResponses.initial(request, adapter),
step,
terminal: OpenResponses.terminal,
},
stream: ResponsesHostedTools.stream(adapter, HOSTED_TOOLS),
})
export const compact = ResponsesCompaction.make(adapter, (request) =>
+1 -1
View File
@@ -114,7 +114,7 @@ describe("ToolStream", () => {
name: "lookup",
input: '{"query":"partial"}',
})
const finished = yield* ToolStream.finishWithInput(ADAPTER, tools, "item_1", '{"query":"final"}')
const finished = yield* ToolStream.finish(ADAPTER, tools, "item_1", '{"query":"final"}')
expect(finished).toEqual({
tools: {},