|
1 | 1 | import { randomUUID } from "node:crypto"; |
2 | 2 | import type { Model } from "@openclaw/llm-core"; |
3 | 3 | import { isRecord } from "@openclaw/normalization-core/record-coerce"; |
4 | | -import type { ResponseCreateParamsStreaming } from "openai/resources/responses/responses.js"; |
| 4 | +import type { |
| 5 | + ResponseCreateParamsStreaming, |
| 6 | + ResponseOutputMessage, |
| 7 | + ResponseStreamEvent, |
| 8 | +} from "openai/resources/responses/responses.js"; |
5 | 9 | import { |
6 | 10 | createResponsesToolCallTracker, |
7 | 11 | isResponsesTextContentPartType, |
@@ -32,6 +36,66 @@ import { |
32 | 36 | type MutableAssistantOutput, |
33 | 37 | } from "./openai-transport-shared.js"; |
34 | 38 |
|
| 39 | +type ResponsesConsumedEventType = |
| 40 | + | "error" |
| 41 | + | "response.completed" |
| 42 | + | "response.content_part.added" |
| 43 | + | "response.created" |
| 44 | + | "response.failed" |
| 45 | + | "response.function_call_arguments.delta" |
| 46 | + | "response.function_call_arguments.done" |
| 47 | + | "response.incomplete" |
| 48 | + | "response.output_item.added" |
| 49 | + | "response.output_item.done" |
| 50 | + | "response.output_text.delta" |
| 51 | + | "response.reasoning_summary_part.added" |
| 52 | + | "response.reasoning_summary_part.done" |
| 53 | + | "response.reasoning_summary_text.delta" |
| 54 | + | "response.reasoning_text.delta" |
| 55 | + | "response.refusal.delta"; |
| 56 | + |
| 57 | +type OpenAIResponsesConsumedEvent = Extract< |
| 58 | + ResponseStreamEvent, |
| 59 | + { type: ResponsesConsumedEventType } |
| 60 | +>; |
| 61 | +type OpenAIResponsesIgnoredSdkEvent = Exclude<ResponseStreamEvent, OpenAIResponsesConsumedEvent>; |
| 62 | +type ResponsesTextContentPart = |
| 63 | + | ResponseOutputMessage["content"][number] |
| 64 | + | { |
| 65 | + type: "text"; |
| 66 | + text: string; |
| 67 | + }; |
| 68 | +type ResponsesStreamOutputMessage = Omit<ResponseOutputMessage, "content"> & { |
| 69 | + content: ResponsesTextContentPart[] | null; |
| 70 | +}; |
| 71 | +type ResponsesContentPartAddedEvent = Extract< |
| 72 | + ResponseStreamEvent, |
| 73 | + { type: "response.content_part.added" } |
| 74 | +>; |
| 75 | +type ResponsesOutputItemDoneEvent = Extract< |
| 76 | + ResponseStreamEvent, |
| 77 | + { type: "response.output_item.done" } |
| 78 | +>; |
| 79 | + |
| 80 | +/** Private structural event contract shared by Responses stream callers. */ |
| 81 | +export type OpenAIResponsesStreamEvent = |
| 82 | + | OpenAIResponsesConsumedEvent |
| 83 | + | OpenAIResponsesIgnoredSdkEvent |
| 84 | + | (Omit<ResponsesContentPartAddedEvent, "part"> & { |
| 85 | + part: Extract<ResponsesTextContentPart, { type: "text" }>; |
| 86 | + }) |
| 87 | + | (Omit<ResponsesOutputItemDoneEvent, "item"> & { |
| 88 | + item: ResponsesStreamOutputMessage; |
| 89 | + }) |
| 90 | + | { |
| 91 | + type: "response.text.delta"; |
| 92 | + delta: string; |
| 93 | + output_index?: number; |
| 94 | + item_id?: string; |
| 95 | + content_index?: number; |
| 96 | + sequence_number?: number; |
| 97 | + }; |
| 98 | + |
35 | 99 | export async function processResponsesStream( |
36 | 100 | openaiStream: AsyncIterable<unknown>, |
37 | 101 | output: MutableAssistantOutput, |
@@ -235,7 +299,7 @@ export async function processResponsesStream( |
235 | 299 | const cooperativeScheduler = createModelStreamCooperativeScheduler(options?.signal); |
236 | 300 | for await (const rawEvent of guardedStream) { |
237 | 301 | throwIfModelStreamAborted(options?.signal); |
238 | | - const event = rawEvent as Record<string, unknown>; |
| 302 | + const event = rawEvent as unknown as Record<string, unknown>; |
239 | 303 | const type = stringifyUnknown(event.type); |
240 | 304 | eventCount += 1; |
241 | 305 | eventTypes.set(type, (eventTypes.get(type) ?? 0) + 1); |
|
0 commit comments