magpie-aws-quick-bridge 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,192 @@
1
+ // Anthropic Messages (POST /v1/messages), which Claude Code speaks, on top
2
+ // of the plugin's chat completions: the request is rewritten as one, and
3
+ // its answer is rewritten back, streamed or whole.
4
+
5
+ import { randomUUID } from "node:crypto"
6
+ import { eventStream, failure, textOf, turnEvents, turnResult } from "./sse.mjs"
7
+
8
+ const TYPES = { 400: "invalid_request_error", 401: "authentication_error", 403: "permission_error", 404: "not_found_error", 413: "request_too_large", 429: "rate_limit_error", 529: "overloaded_error" }
9
+
10
+ export const error = (status, message) => Response.json({ type: "error", error: { type: TYPES[status] ?? "api_error", message } }, { status })
11
+
12
+ // Claude Code opens its system prompt with a billing line meant for
13
+ // Anthropic; it changes with every request and says nothing to the model.
14
+ const system = (value) =>
15
+ textOf(typeof value === "string" ? [{ type: "text", text: value }] : value)
16
+ .split("\n")
17
+ .filter((line) => !line.startsWith("x-anthropic-billing-header:"))
18
+ .join("\n")
19
+ .trim()
20
+
21
+ function image(block) {
22
+ const s = block?.source
23
+ if (s?.type === "base64" && s.data) return { type: "image_url", image_url: { url: `data:${s.media_type || "image/png"};base64,${s.data}` } }
24
+ if (s?.type === "url" && s.url) return { type: "image_url", image_url: { url: s.url } }
25
+ return null
26
+ }
27
+
28
+ function resultText(block) {
29
+ const parts = typeof block.content === "string" ? [block.content] : []
30
+ for (const p of Array.isArray(block.content) ? block.content : []) {
31
+ if (p?.type === "text" && typeof p.text === "string") parts.push(p.text)
32
+ else if (p?.type === "image") parts.push("[image]")
33
+ else if (p?.type === "tool_reference" && p.tool_name) parts.push(`Tool loaded: ${p.tool_name}`)
34
+ }
35
+ const text = parts.join("\n")
36
+ return block.is_error && !/^error/i.test(text) ? `Error: ${text}`.trim() : text
37
+ }
38
+
39
+ function messages(list) {
40
+ const out = []
41
+ for (const m of Array.isArray(list) ? list : []) {
42
+ const blocks = typeof m?.content === "string" ? [{ type: "text", text: m.content }] : Array.isArray(m?.content) ? m.content : []
43
+ if (m?.role === "assistant") {
44
+ const text = textOf(blocks)
45
+ const calls = blocks
46
+ .filter((b) => b?.type === "tool_use")
47
+ .map((b) => ({ id: String(b.id ?? ""), type: "function", function: { name: String(b.name ?? ""), arguments: JSON.stringify(b.input ?? {}) } }))
48
+ if (!text && !calls.length) continue
49
+ out.push({ role: "assistant", content: text || null, ...(calls.length ? { tool_calls: calls } : {}) })
50
+ continue
51
+ }
52
+ // Claude Code tells the model of changes mid-conversation as "system"
53
+ if (m?.role === "system") {
54
+ const text = textOf(blocks).trim()
55
+ if (text) out.push({ role: "system", content: text })
56
+ continue
57
+ }
58
+ // a turn's tool results come before what the user wrote after them
59
+ for (const b of blocks) if (b?.type === "tool_result") out.push({ role: "tool", tool_call_id: String(b.tool_use_id ?? ""), content: resultText(b) })
60
+ const parts = []
61
+ for (const b of blocks) {
62
+ if (b?.type === "text" && typeof b.text === "string") parts.push({ type: "text", text: b.text })
63
+ else if (b?.type === "image") parts.push(image(b) ?? { type: "text", text: "[image]" })
64
+ else if (b?.type === "document") parts.push({ type: "text", text: `[document${b.title ? ` ${b.title}` : ""}]` })
65
+ }
66
+ if (parts.length) out.push({ role: "user", content: parts.every((p) => p.type === "text") ? parts.map((p) => p.text).join("\n") : parts })
67
+ }
68
+ return out
69
+ }
70
+
71
+ function effort(body) {
72
+ const asked = String(body.output_config?.effort ?? "").toLowerCase()
73
+ if (asked) return asked === "max" || asked === "xhigh" ? "high" : asked
74
+ const budget = Number(body.thinking?.budget_tokens)
75
+ if (body.thinking?.type !== "enabled" || !(budget > 0)) return undefined
76
+ return budget < 4_000 ? "low" : budget < 16_000 ? "medium" : "high"
77
+ }
78
+
79
+ function toolChoice(choice) {
80
+ if (choice?.type === "any") return "required"
81
+ if (choice?.type === "none") return "none"
82
+ if (choice?.type === "tool" && choice.name) return { type: "function", function: { name: choice.name } }
83
+ return undefined
84
+ }
85
+
86
+ // The request as a chat completions body, and what the answer needs of it.
87
+ export function toChat(body) {
88
+ const sys = system(body.system)
89
+ const chat = {
90
+ model: body.model,
91
+ messages: [...(sys ? [{ role: "system", content: sys }] : []), ...messages(body.messages)],
92
+ stream: body.stream === true,
93
+ stream_options: { include_usage: true },
94
+ }
95
+ // tools the client runs: Anthropic's own (web search, …) have no schema,
96
+ // and a deferred one is only a name until the client sends it in full
97
+ const tools = (Array.isArray(body.tools) ? body.tools : []).filter((t) => t?.name && t.input_schema && t.defer_loading !== true)
98
+ if (tools.length) chat.tools = tools.map((t) => ({ type: "function", function: { name: t.name, description: t.description ?? "", parameters: t.input_schema } }))
99
+ const choice = toolChoice(body.tool_choice)
100
+ if (choice) chat.tool_choice = choice
101
+ if (body.tool_choice?.disable_parallel_tool_use === true) chat.parallel_tool_calls = false
102
+ const asked = effort(body)
103
+ if (asked) chat.reasoning_effort = asked
104
+ return {
105
+ chat,
106
+ // Claude Code names its session in metadata.user_id
107
+ conversation: typeof body.metadata?.user_id === "string" ? body.metadata.user_id : "",
108
+ model: String(body.model ?? ""),
109
+ thinking: ["enabled", "adaptive"].includes(body.thinking?.type) && body.thinking.display !== "omitted",
110
+ }
111
+ }
112
+
113
+ const usage = (u) => ({ input_tokens: u.input, output_tokens: u.output, cache_creation_input_tokens: 0, cache_read_input_tokens: u.cached })
114
+
115
+ function parsed(args) {
116
+ try {
117
+ const value = JSON.parse(args)
118
+ return value && typeof value === "object" ? value : {}
119
+ } catch {
120
+ return {}
121
+ }
122
+ }
123
+
124
+ // The chat completions answer as a Messages one.
125
+ export async function fromChat(response, req) {
126
+ if (!response.ok) {
127
+ const f = await failure(response)
128
+ return error(f.status, f.message)
129
+ }
130
+ const id = "msg_" + randomUUID().replaceAll("-", "")
131
+ const head = { id, type: "message", role: "assistant", model: req.model }
132
+
133
+ if (!req.chat.stream) {
134
+ const res = turnResult(await response.json())
135
+ const content = []
136
+ if (req.thinking && res.reasoning) content.push({ type: "thinking", thinking: res.reasoning, signature: "" })
137
+ if (res.text) content.push({ type: "text", text: res.text })
138
+ for (const c of res.calls) content.push({ type: "tool_use", id: c.id, name: c.name, input: parsed(c.arguments) })
139
+ return Response.json({ ...head, content, stop_reason: res.calls.length ? "tool_use" : "end_turn", stop_sequence: null, usage: usage(res.usage) })
140
+ }
141
+
142
+ return eventStream(async (send) => {
143
+ send("message_start", {
144
+ type: "message_start",
145
+ message: { ...head, content: [], stop_reason: null, stop_sequence: null, usage: usage({ input: 0, output: 0, cached: 0 }) },
146
+ })
147
+ send("ping", { type: "ping" })
148
+ let index = -1
149
+ let open = ""
150
+ let calls = 0
151
+ const close = () => {
152
+ if (!open) return
153
+ if (open === "thinking") send("content_block_delta", { type: "content_block_delta", index, delta: { type: "signature_delta", signature: "" } })
154
+ send("content_block_stop", { type: "content_block_stop", index })
155
+ open = ""
156
+ }
157
+ const block = (kind, content_block) => {
158
+ if (open === kind && kind !== "tool_use") return
159
+ close()
160
+ open = kind
161
+ send("content_block_start", { type: "content_block_start", index: ++index, content_block })
162
+ }
163
+ for await (const event of turnEvents(response)) {
164
+ if (event.type === "reasoning") {
165
+ if (!req.thinking) continue
166
+ block("thinking", { type: "thinking", thinking: "", signature: "" })
167
+ send("content_block_delta", { type: "content_block_delta", index, delta: { type: "thinking_delta", thinking: event.text } })
168
+ } else if (event.type === "text") {
169
+ block("text", { type: "text", text: "" })
170
+ send("content_block_delta", { type: "content_block_delta", index, delta: { type: "text_delta", text: event.text } })
171
+ } else if (event.type === "tool") {
172
+ calls++
173
+ block("tool_use", { type: "tool_use", id: event.call.id, name: event.call.name, input: {} })
174
+ send("content_block_delta", { type: "content_block_delta", index, delta: { type: "input_json_delta", partial_json: JSON.stringify(parsed(event.call.arguments)) } })
175
+ } else if (event.type === "error") {
176
+ close()
177
+ return send("error", { type: "error", error: { type: "api_error", message: event.message } })
178
+ } else if (event.type === "done") {
179
+ close()
180
+ send("message_delta", { type: "message_delta", delta: { stop_reason: calls ? "tool_use" : "end_turn", stop_sequence: null }, usage: usage(event.usage) })
181
+ send("message_stop", { type: "message_stop" })
182
+ }
183
+ }
184
+ })
185
+ }
186
+
187
+ // POST /v1/messages/count_tokens. Amazon Quick counts nothing ahead of a
188
+ // turn, so this is an estimate: about four characters a token.
189
+ export function countTokens(body) {
190
+ const chars = JSON.stringify([body?.system ?? "", body?.messages ?? [], body?.tools ?? []]).length
191
+ return Response.json({ input_tokens: Math.max(1, Math.ceil(chars / 4)) })
192
+ }
@@ -0,0 +1,38 @@
1
+ // CC Switch (https://github.com/farion1231/cc-switch) import links: opening
2
+ // one adds "Amazon Quick" to CC Switch as a provider for one of its apps,
3
+ // pointing at the gateway. Each app is given the API it speaks natively, so
4
+ // CC Switch's own local routing is not needed.
5
+
6
+ const HOMEPAGE = "https://github.com/DaBinBinah/magpie-AWS-Quick-bridge"
7
+ const ICON = "https://raw.githubusercontent.com/DaBinBinah/magpie-AWS-Quick-bridge/main/assets/icon-128.png"
8
+
9
+ export const APPS = {
10
+ claude: { label: "Claude Code", apiFormat: "anthropic", v1: false, models: { haikuModel: "fast", sonnetModel: "smart", opusModel: "smart" } },
11
+ codex: { label: "Codex", apiFormat: "openai_responses", v1: true },
12
+ opencode: { label: "OpenCode", apiFormat: "openai_chat", v1: true },
13
+ }
14
+
15
+ // The address an app is given: Claude Code adds /v1 itself, the others are
16
+ // given it.
17
+ export const endpoint = (app, base) => base.replace(/\/+$/, "") + (APPS[app]?.v1 ? "/v1" : "")
18
+
19
+ export function importLink(app, { base, apiKey, model = "smart", name = "Amazon Quick" }) {
20
+ const spec = APPS[app]
21
+ if (!spec) throw new Error(`unknown app ${JSON.stringify(app)}; one of: ${Object.keys(APPS).join(", ")}`)
22
+ const params = new URLSearchParams({
23
+ resource: "provider",
24
+ app,
25
+ name,
26
+ endpoint: endpoint(app, base),
27
+ apiKey,
28
+ model,
29
+ ...(spec.models ?? {}),
30
+ apiFormat: spec.apiFormat,
31
+ homepage: HOMEPAGE,
32
+ icon: "aws",
33
+ iconUrl: ICON,
34
+ notes: "Amazon Quick through magpie-aws-quick-bridge on this computer; keep `magpie-aws-quick-bridge serve` running.",
35
+ })
36
+ // CC Switch reads "+" as a plus sign, not a space
37
+ return "ccswitch://v1/import?" + params.toString().replaceAll("+", "%20")
38
+ }
@@ -0,0 +1,254 @@
1
+ // OpenAI Responses (POST /v1/responses), which Codex speaks, on top of the
2
+ // plugin's chat completions: the request is rewritten as one, and its answer
3
+ // is rewritten back, streamed or whole. Nothing is stored between requests,
4
+ // so previous_response_id is refused: Codex sends the conversation itself.
5
+
6
+ import { randomUUID } from "node:crypto"
7
+ import { eventStream, failure, textOf, turnEvents, turnResult } from "./sse.mjs"
8
+
9
+ export const error = (status, message, code = "") =>
10
+ Response.json({ error: { message, type: status === 401 ? "authentication_error" : status < 500 ? "invalid_request_error" : "server_error", code: code || null } }, { status })
11
+
12
+ const id = (prefix) => prefix + randomUUID().replaceAll("-", "")
13
+ const TEXT = ["input_text", "output_text", "text", "summary_text", "reasoning_text"]
14
+
15
+ function content(value) {
16
+ if (typeof value === "string") return value
17
+ const parts = []
18
+ for (const p of Array.isArray(value) ? value : []) {
19
+ if (TEXT.includes(p?.type) && typeof p.text === "string") parts.push({ type: "text", text: p.text })
20
+ else if (p?.type === "input_image") {
21
+ const url = typeof p.image_url === "string" ? p.image_url : p.image_url?.url
22
+ parts.push(url ? { type: "image_url", image_url: { url } } : { type: "text", text: "[image]" })
23
+ } else if (p?.type === "input_file") parts.push({ type: "text", text: `[file${p.filename ? ` ${p.filename}` : ""}]` })
24
+ }
25
+ return parts.every((p) => p.type === "text") ? parts.map((p) => p.text).join("\n") : parts
26
+ }
27
+
28
+ // A tool inside a namespace (Codex groups MCP and sub-agent tools this way)
29
+ // under one name, as chat tools have no namespaces.
30
+ const flat = (namespace, name) => (namespace ? `${namespace}${namespace.endsWith("__") ? "" : "__"}${name}` : String(name ?? ""))
31
+
32
+ const output = (value) => (typeof value === "string" ? value : Array.isArray(value) ? textOf(value, TEXT) || JSON.stringify(value) : value == null ? "" : JSON.stringify(value))
33
+
34
+ // The `input` items as chat messages. A model's tool calls are items of
35
+ // their own here; in chat they hang off the assistant message before them.
36
+ function messages(input) {
37
+ if (typeof input === "string") return input ? [{ role: "user", content: input }] : []
38
+ const out = []
39
+ const call = (callId, name, args) => {
40
+ const entry = { id: String(callId ?? ""), type: "function", function: { name: String(name ?? ""), arguments: args } }
41
+ const last = out.at(-1)
42
+ if (last?.role === "assistant") (last.tool_calls ??= []).push(entry)
43
+ else out.push({ role: "assistant", content: null, tool_calls: [entry] })
44
+ }
45
+ for (const item of Array.isArray(input) ? input : []) {
46
+ if (!item || typeof item !== "object") continue
47
+ const type = item.type ?? (item.role ? "message" : "")
48
+ if (type === "message") {
49
+ const role = item.role === "developer" ? "system" : item.role
50
+ const body = content(item.content)
51
+ if (!["user", "assistant", "system"].includes(role) || !body?.length) continue
52
+ out.push({ role, content: body })
53
+ } else if (type === "function_call") call(item.call_id ?? item.id, flat(item.namespace, item.name), typeof item.arguments === "string" ? item.arguments : JSON.stringify(item.arguments ?? {}))
54
+ else if (type === "custom_tool_call") call(item.call_id ?? item.id, item.name, JSON.stringify({ input: item.input ?? "" }))
55
+ else if (type === "local_shell_call") call(item.call_id ?? item.id, "local_shell", JSON.stringify(item.action ?? {}))
56
+ else if (type === "function_call_output" || type === "custom_tool_call_output" || type === "local_shell_call_output")
57
+ out.push({ role: "tool", tool_call_id: String(item.call_id ?? ""), content: output(item.output) })
58
+ // reasoning items, web search calls and the like have nothing to resend
59
+ }
60
+ return out
61
+ }
62
+
63
+ const INPUT = { type: "object", properties: { input: { type: "string", description: "The tool's whole input, as plain text" } }, required: ["input"] }
64
+ const SHELL = {
65
+ type: "object",
66
+ properties: { command: { type: "array", items: { type: "string" } }, workdir: { type: "string" }, timeout_ms: { type: "integer" } },
67
+ required: ["command"],
68
+ }
69
+
70
+ // The tools the client runs, as chat tools. A "custom" tool takes free text
71
+ // instead of JSON arguments; it is offered with one string argument, and
72
+ // its calls are turned back in the answer (`custom` names them, `spaces`
73
+ // the ones that came in a namespace).
74
+ function tools(list) {
75
+ const out = []
76
+ const custom = new Set()
77
+ const spaces = new Map()
78
+ const fn = (t, name = t.name) => out.push({ type: "function", function: { name, description: t.description ?? "", parameters: t.parameters ?? { type: "object", properties: {} } } })
79
+ for (const t of Array.isArray(list) ? list : []) {
80
+ if (t?.type === "function" && t.name) fn(t)
81
+ else if (t?.type === "namespace" && t.name) {
82
+ for (const inner of Array.isArray(t.tools) ? t.tools : []) {
83
+ if (inner?.type !== "function" || !inner.name) continue
84
+ spaces.set(flat(t.name, inner.name), { namespace: t.name, name: inner.name })
85
+ fn(inner, flat(t.name, inner.name))
86
+ }
87
+ } else if (t?.type === "custom" && t.name) {
88
+ custom.add(t.name)
89
+ const grammar = typeof t.format?.definition === "string" ? `\n\nThe input follows this ${t.format.syntax ?? ""} grammar:\n${t.format.definition.slice(0, 3000)}` : ""
90
+ out.push({ type: "function", function: { name: t.name, description: (t.description ?? "") + grammar, parameters: INPUT } })
91
+ } else if (t?.type === "local_shell") out.push({ type: "function", function: { name: "local_shell", description: "Runs a command on the user's computer and returns its output.", parameters: SHELL } })
92
+ // web_search and other tools OpenAI runs itself are not offered
93
+ }
94
+ return { tools: out, custom, spaces }
95
+ }
96
+
97
+ function toolChoice(choice) {
98
+ if (choice === "none" || choice === "required") return choice
99
+ if (choice && typeof choice === "object" && choice.name) return { type: "function", function: { name: choice.name } }
100
+ return undefined
101
+ }
102
+
103
+ // The request as a chat completions body, and what the answer needs of it.
104
+ export function toChat(body, headers) {
105
+ const offered = tools(body.tools)
106
+ const chat = {
107
+ model: body.model,
108
+ messages: [...(typeof body.instructions === "string" && body.instructions.trim() ? [{ role: "system", content: body.instructions }] : []), ...messages(body.input)],
109
+ stream: body.stream === true,
110
+ stream_options: { include_usage: true },
111
+ }
112
+ if (offered.tools.length) chat.tools = offered.tools
113
+ const choice = toolChoice(body.tool_choice)
114
+ if (choice) chat.tool_choice = choice
115
+ if (body.parallel_tool_calls === false) chat.parallel_tool_calls = false
116
+ if (body.reasoning?.effort) chat.reasoning_effort = body.reasoning.effort
117
+ return {
118
+ chat,
119
+ custom: offered.custom,
120
+ spaces: offered.spaces,
121
+ model: String(body.model ?? ""),
122
+ // Codex names its session in these
123
+ conversation: String(headers?.get("session-id") ?? headers?.get("session_id") ?? headers?.get("conversation_id") ?? body.prompt_cache_key ?? ""),
124
+ }
125
+ }
126
+
127
+ const usage = (u) => ({
128
+ input_tokens: u.input + u.cached,
129
+ input_tokens_details: { cached_tokens: u.cached },
130
+ output_tokens: u.output,
131
+ output_tokens_details: { reasoning_tokens: 0 },
132
+ total_tokens: u.total || u.input + u.cached + u.output,
133
+ })
134
+
135
+ function callItem(call, { custom, spaces }) {
136
+ if (custom.has(call.name)) {
137
+ let input = call.arguments
138
+ try {
139
+ const args = JSON.parse(call.arguments)
140
+ input = typeof args?.input === "string" ? args.input : typeof args === "string" ? args : call.arguments
141
+ } catch {}
142
+ return { id: id("ctc_"), type: "custom_tool_call", status: "completed", call_id: call.id, name: call.name, input }
143
+ }
144
+ if (call.name === "local_shell") {
145
+ let action = {}
146
+ try {
147
+ action = JSON.parse(call.arguments)
148
+ } catch {}
149
+ return { id: id("lsh_"), type: "local_shell_call", status: "completed", call_id: call.id, action: { type: "exec", ...action } }
150
+ }
151
+ return { id: id("fc_"), type: "function_call", status: "completed", call_id: call.id, ...(spaces.get(call.name) ?? { name: call.name }), arguments: call.arguments }
152
+ }
153
+
154
+ const reasoningItem = (text) => ({ id: id("rs_"), type: "reasoning", summary: [{ type: "summary_text", text }] })
155
+ const messageItem = (text, itemId = id("msg_")) => ({ id: itemId, type: "message", status: "completed", role: "assistant", content: [{ type: "output_text", text, annotations: [] }] })
156
+
157
+ // The chat completions answer as a Responses one.
158
+ export async function fromChat(response, req) {
159
+ if (!response.ok) {
160
+ const f = await failure(response)
161
+ return error(f.status, f.message, f.code)
162
+ }
163
+ const base = {
164
+ id: id("resp_"),
165
+ object: "response",
166
+ created_at: Math.floor(Date.now() / 1000),
167
+ model: req.model,
168
+ error: null,
169
+ incomplete_details: null,
170
+ parallel_tool_calls: req.chat.parallel_tool_calls !== false,
171
+ tool_choice: "auto",
172
+ store: false,
173
+ }
174
+
175
+ if (!req.chat.stream) {
176
+ const res = turnResult(await response.json())
177
+ const items = []
178
+ if (res.reasoning) items.push(reasoningItem(res.reasoning))
179
+ if (res.text) items.push(messageItem(res.text))
180
+ for (const c of res.calls) items.push(callItem(c, req))
181
+ return Response.json({ ...base, status: "completed", output: items, output_text: res.text, usage: usage(res.usage) })
182
+ }
183
+
184
+ return eventStream(async (emit) => {
185
+ let sequence = 0
186
+ const send = (type, data) => emit(type, { type, sequence_number: sequence++, ...data })
187
+ const items = []
188
+ send("response.created", { response: { ...base, status: "in_progress", output: [], usage: null } })
189
+ send("response.in_progress", { response: { ...base, status: "in_progress", output: [], usage: null } })
190
+
191
+ // the item being written: { kind: "reasoning" | "message", id, text }
192
+ let open = null
193
+ const close = () => {
194
+ if (!open) return
195
+ const output_index = items.length
196
+ const { kind, id: item_id, text } = open
197
+ open = null
198
+ if (kind === "reasoning") {
199
+ send("response.reasoning_summary_text.done", { item_id, output_index, summary_index: 0, text })
200
+ send("response.reasoning_summary_part.done", { item_id, output_index, summary_index: 0, part: { type: "summary_text", text } })
201
+ items.push({ ...reasoningItem(text), id: item_id })
202
+ } else {
203
+ send("response.output_text.done", { item_id, output_index, content_index: 0, text, logprobs: [] })
204
+ send("response.content_part.done", { item_id, output_index, content_index: 0, part: { type: "output_text", text, annotations: [] } })
205
+ items.push(messageItem(text, item_id))
206
+ }
207
+ send("response.output_item.done", { output_index, item: items.at(-1) })
208
+ }
209
+ const write = (kind, text) => {
210
+ if (open?.kind !== kind) {
211
+ close()
212
+ const output_index = items.length
213
+ open = { kind, id: id(kind === "reasoning" ? "rs_" : "msg_"), text: "" }
214
+ if (kind === "reasoning") {
215
+ send("response.output_item.added", { output_index, item: { id: open.id, type: "reasoning", summary: [] } })
216
+ send("response.reasoning_summary_part.added", { item_id: open.id, output_index, summary_index: 0, part: { type: "summary_text", text: "" } })
217
+ } else {
218
+ send("response.output_item.added", { output_index, item: { id: open.id, type: "message", status: "in_progress", role: "assistant", content: [] } })
219
+ send("response.content_part.added", { item_id: open.id, output_index, content_index: 0, part: { type: "output_text", text: "", annotations: [] } })
220
+ }
221
+ }
222
+ open.text += text
223
+ const output_index = items.length
224
+ if (kind === "reasoning") send("response.reasoning_summary_text.delta", { item_id: open.id, output_index, summary_index: 0, delta: text })
225
+ else send("response.output_text.delta", { item_id: open.id, output_index, content_index: 0, delta: text, logprobs: [] })
226
+ }
227
+
228
+ for await (const event of turnEvents(response)) {
229
+ if (event.type === "reasoning" || event.type === "text") write(event.type === "text" ? "message" : "reasoning", event.text)
230
+ else if (event.type === "tool") {
231
+ close()
232
+ const output_index = items.length
233
+ const item = callItem(event.call, req)
234
+ const blank = item.type === "function_call" ? { arguments: "" } : item.type === "custom_tool_call" ? { input: "" } : {}
235
+ send("response.output_item.added", { output_index, item: { ...item, ...blank, status: "in_progress" } })
236
+ if (item.type === "function_call") {
237
+ send("response.function_call_arguments.delta", { item_id: item.id, output_index, delta: item.arguments })
238
+ send("response.function_call_arguments.done", { item_id: item.id, output_index, arguments: item.arguments })
239
+ } else if (item.type === "custom_tool_call") {
240
+ send("response.custom_tool_call_input.delta", { item_id: item.id, output_index, delta: item.input })
241
+ send("response.custom_tool_call_input.done", { item_id: item.id, output_index, input: item.input })
242
+ }
243
+ items.push(item)
244
+ send("response.output_item.done", { output_index, item })
245
+ } else if (event.type === "error") {
246
+ close()
247
+ send("response.failed", { response: { ...base, status: "failed", output: items, usage: null, error: { code: "server_error", message: event.message } } })
248
+ } else if (event.type === "done") {
249
+ close()
250
+ send("response.completed", { response: { ...base, status: "completed", output: items, usage: usage(event.usage) } })
251
+ }
252
+ }
253
+ })
254
+ }