@scuffi/gardener 0.1.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.
Files changed (21) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +17 -0
  3. package/assets/apps/gardener/dist/gardener_runtime/assets/anthropic-messages-CpAYOexF.js +6431 -0
  4. package/assets/apps/gardener/dist/gardener_runtime/assets/deferred-tools-BwNJSp7E.js +31 -0
  5. package/assets/apps/gardener/dist/gardener_runtime/assets/error-body-D8mmocwO.js +6744 -0
  6. package/assets/apps/gardener/dist/gardener_runtime/assets/hash-DAk4mnwB.js +18 -0
  7. package/assets/apps/gardener/dist/gardener_runtime/assets/json-parse-DCA_fvRV.js +1094 -0
  8. package/assets/apps/gardener/dist/gardener_runtime/assets/mimetext.node.es-XQfmuX2L.js +8403 -0
  9. package/assets/apps/gardener/dist/gardener_runtime/assets/openai-completions-Dg9BYhy-.js +907 -0
  10. package/assets/apps/gardener/dist/gardener_runtime/assets/openai-prompt-cache-IAZGhS1U.js +12 -0
  11. package/assets/apps/gardener/dist/gardener_runtime/assets/openai-responses-shared-BptkVdeL.js +560 -0
  12. package/assets/apps/gardener/dist/gardener_runtime/assets/openai-responses-vjbR2NOB.js +240 -0
  13. package/assets/apps/gardener/dist/gardener_runtime/assets/rolldown-runtime-7_rZTKki.js +42 -0
  14. package/assets/apps/gardener/dist/gardener_runtime/assets/simple-options-tFhuCUzS.js +300 -0
  15. package/assets/apps/gardener/dist/gardener_runtime/assets/transform-messages-B41ICucH.js +246 -0
  16. package/assets/apps/gardener/dist/gardener_runtime/index.js +80307 -0
  17. package/assets/apps/gardener/migrations/0001_actions_baseline.sql +116 -0
  18. package/assets/gardener-distribution.json +5 -0
  19. package/dist/cli.js +24719 -0
  20. package/dist/cli.js.map +7 -0
  21. package/package.json +45 -0
@@ -0,0 +1,12 @@
1
+ //#region ../../node_modules/.pnpm/@earendil-works+pi-ai@0.83.0_@modelcontextprotocol+sdk@1.30.0_@cfworker+json-schema@4.1_c038161535a4cc121d37390bb491fb78/node_modules/@earendil-works/pi-ai/dist/api/openai-prompt-cache.js
2
+ var OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH = 64;
3
+ function clampOpenAIPromptCacheKey(key) {
4
+ if (key === void 0) return void 0;
5
+ const chars = Array.from(key);
6
+ if (chars.length <= 64) return key;
7
+ return chars.slice(0, 64).join("");
8
+ }
9
+ //#endregion
10
+ export { OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH, clampOpenAIPromptCacheKey };
11
+
12
+ //# sourceMappingURL=openai-prompt-cache-IAZGhS1U.js.map
@@ -0,0 +1,560 @@
1
+ import { n as parseStreamingJson, r as calculateCost } from "./json-parse-DCA_fvRV.js";
2
+ import { a as resolveGrammarConstrainedSampling, i as getGrammarToolInput, n as appendGrammarToolInputJsonDelta, o as resolveJsonSchemaStrictSampling, s as sanitizeSurrogates, t as transformMessages } from "./transform-messages-B41ICucH.js";
3
+ import { t as shortHash } from "./hash-DAk4mnwB.js";
4
+ //#region ../../node_modules/.pnpm/@earendil-works+pi-ai@0.83.0_@modelcontextprotocol+sdk@1.30.0_@cfworker+json-schema@4.1_c038161535a4cc121d37390bb491fb78/node_modules/@earendil-works/pi-ai/dist/api/openai-responses-shared.js
5
+ function encodeTextSignatureV1(id, phase) {
6
+ const payload = {
7
+ v: 1,
8
+ id
9
+ };
10
+ if (phase) payload.phase = phase;
11
+ return JSON.stringify(payload);
12
+ }
13
+ function parseTextSignature(signature) {
14
+ if (!signature) return void 0;
15
+ if (signature.startsWith("{")) try {
16
+ const parsed = JSON.parse(signature);
17
+ if (parsed.v === 1 && typeof parsed.id === "string") {
18
+ if (parsed.phase === "commentary" || parsed.phase === "final_answer") return {
19
+ id: parsed.id,
20
+ phase: parsed.phase
21
+ };
22
+ return { id: parsed.id };
23
+ }
24
+ } catch {}
25
+ return { id: signature };
26
+ }
27
+ function convertToolResultOutput(model, content) {
28
+ const textResult = content.filter((c) => c.type === "text").map((c) => c.text).join("\n");
29
+ const images = content.filter((c) => c.type === "image");
30
+ const hasText = textResult.length > 0;
31
+ if (images.length === 0 || !model.input.includes("image")) return sanitizeSurrogates(hasText ? textResult : images.length > 0 ? "(see attached image)" : "(no tool output)");
32
+ const output = [];
33
+ if (hasText) output.push({
34
+ type: "input_text",
35
+ text: sanitizeSurrogates(textResult)
36
+ });
37
+ for (const image of images) output.push({
38
+ type: "input_image",
39
+ detail: "auto",
40
+ image_url: `data:${image.mimeType};base64,${image.data}`
41
+ });
42
+ return output;
43
+ }
44
+ function convertResponsesMessages(model, context, allowedToolCallProviders, options) {
45
+ const messages = [];
46
+ const loadedToolNames = /* @__PURE__ */ new Set();
47
+ const normalizeIdPart = (part) => {
48
+ const sanitized = part.replace(/[^a-zA-Z0-9_-]/g, "_");
49
+ return (sanitized.length > 64 ? sanitized.slice(0, 64) : sanitized).replace(/_+$/, "");
50
+ };
51
+ const buildForeignResponsesItemId = (itemId) => {
52
+ const normalized = `fc_${shortHash(itemId)}`;
53
+ return normalized.length > 64 ? normalized.slice(0, 64) : normalized;
54
+ };
55
+ const normalizeToolCallId = (id, _targetModel, source) => {
56
+ if (!allowedToolCallProviders.has(model.provider)) return normalizeIdPart(id);
57
+ if (!id.includes("|")) return normalizeIdPart(id);
58
+ const [callId, itemId] = id.split("|");
59
+ const normalizedCallId = normalizeIdPart(callId);
60
+ let normalizedItemId = source.provider !== model.provider || source.api !== model.api ? buildForeignResponsesItemId(itemId) : normalizeIdPart(itemId);
61
+ if (!normalizedItemId.startsWith("fc_")) normalizedItemId = normalizeIdPart(`fc_${normalizedItemId}`);
62
+ return `${normalizedCallId}|${normalizedItemId}`;
63
+ };
64
+ const transformedMessages = transformMessages(context.messages, model, normalizeToolCallId);
65
+ if ((options?.includeSystemPrompt ?? true) && context.systemPrompt) {
66
+ const compat = model.compat;
67
+ const role = model.reasoning && compat?.supportsDeveloperRole !== false ? "developer" : "system";
68
+ messages.push({
69
+ role,
70
+ content: sanitizeSurrogates(context.systemPrompt)
71
+ });
72
+ }
73
+ let msgIndex = 0;
74
+ for (const msg of transformedMessages) {
75
+ if (msg.role === "user") {
76
+ if (typeof msg.content === "string") messages.push({
77
+ role: "user",
78
+ content: [{
79
+ type: "input_text",
80
+ text: sanitizeSurrogates(msg.content)
81
+ }]
82
+ });
83
+ else {
84
+ const content = msg.content.map((item) => {
85
+ if (item.type === "text") return {
86
+ type: "input_text",
87
+ text: sanitizeSurrogates(item.text)
88
+ };
89
+ return {
90
+ type: "input_image",
91
+ detail: "auto",
92
+ image_url: `data:${item.mimeType};base64,${item.data}`
93
+ };
94
+ });
95
+ if (content.length === 0) continue;
96
+ messages.push({
97
+ role: "user",
98
+ content
99
+ });
100
+ }
101
+ } else if (msg.role === "assistant") {
102
+ const output = [];
103
+ const assistantMsg = msg;
104
+ const isDifferentModel = assistantMsg.model !== model.id && assistantMsg.provider === model.provider && assistantMsg.api === model.api;
105
+ let textBlockIndex = 0;
106
+ for (const block of msg.content) if (block.type === "thinking") {
107
+ if (block.thinkingSignature) {
108
+ const reasoningItem = JSON.parse(block.thinkingSignature);
109
+ output.push(reasoningItem);
110
+ }
111
+ } else if (block.type === "text") {
112
+ const textBlock = block;
113
+ const parsedSignature = parseTextSignature(textBlock.textSignature);
114
+ const fallbackMessageId = textBlockIndex === 0 ? `msg_pi_${msgIndex}` : `msg_pi_${msgIndex}_${textBlockIndex}`;
115
+ textBlockIndex++;
116
+ let msgId = parsedSignature?.id;
117
+ if (!msgId) msgId = fallbackMessageId;
118
+ else if (msgId.length > 64) msgId = `msg_${shortHash(msgId)}`;
119
+ output.push({
120
+ type: "message",
121
+ role: "assistant",
122
+ content: [{
123
+ type: "output_text",
124
+ text: sanitizeSurrogates(textBlock.text),
125
+ annotations: []
126
+ }],
127
+ status: "completed",
128
+ id: msgId,
129
+ phase: parsedSignature?.phase
130
+ });
131
+ } else if (block.type === "toolCall") {
132
+ const toolCall = block;
133
+ const [callId, itemIdRaw] = toolCall.id.split("|");
134
+ const customInputProperty = options?.grammarToolInputProperties?.get(toolCall.name);
135
+ let itemId = itemIdRaw;
136
+ if (isDifferentModel && itemId?.startsWith("fc_") || customInputProperty === void 0 && !itemId?.startsWith("fc_")) itemId = void 0;
137
+ if (customInputProperty !== void 0) output.push({
138
+ type: "custom_tool_call",
139
+ id: itemId,
140
+ call_id: callId,
141
+ name: toolCall.name,
142
+ input: sanitizeSurrogates(getGrammarToolInput(toolCall.name, toolCall.arguments, customInputProperty))
143
+ });
144
+ else output.push({
145
+ type: "function_call",
146
+ id: itemId,
147
+ call_id: callId,
148
+ name: toolCall.name,
149
+ arguments: JSON.stringify(toolCall.arguments)
150
+ });
151
+ }
152
+ if (output.length === 0) continue;
153
+ messages.push(...output);
154
+ } else if (msg.role === "toolResult") {
155
+ const [callId] = msg.toolCallId.split("|");
156
+ const output = convertToolResultOutput(model, msg.content);
157
+ if (options?.grammarToolInputProperties?.has(msg.toolName)) messages.push({
158
+ type: "custom_tool_call_output",
159
+ call_id: callId,
160
+ output
161
+ });
162
+ else messages.push({
163
+ type: "function_call_output",
164
+ call_id: callId,
165
+ output
166
+ });
167
+ const deferredTools = [];
168
+ for (const name of msg.addedToolNames ?? []) {
169
+ const tool = options?.deferredTools?.get(name);
170
+ if (!tool || loadedToolNames.has(name)) continue;
171
+ loadedToolNames.add(name);
172
+ deferredTools.push(tool);
173
+ }
174
+ if (deferredTools.length > 0) {
175
+ const names = deferredTools.map((tool) => tool.name);
176
+ const searchCallId = `pi_tool_load_${shortHash(`${msg.toolCallId}:${names.join(",")}`)}`;
177
+ messages.push({
178
+ type: "tool_search_call",
179
+ call_id: searchCallId,
180
+ execution: "client",
181
+ status: "completed",
182
+ arguments: {
183
+ query: names.join(" "),
184
+ limit: names.length
185
+ }
186
+ });
187
+ messages.push({
188
+ type: "tool_search_output",
189
+ call_id: searchCallId,
190
+ execution: "client",
191
+ status: "completed",
192
+ tools: convertResponsesTools(deferredTools, {
193
+ ...options?.toolOptions,
194
+ deferLoading: true
195
+ })
196
+ });
197
+ }
198
+ }
199
+ msgIndex++;
200
+ }
201
+ return messages;
202
+ }
203
+ function convertResponsesTools(tools, options) {
204
+ const defaultStrict = options?.strict === void 0 ? false : options.strict;
205
+ const supportsStrictMode = options?.supportsStrictMode ?? true;
206
+ const supportsOpenAIGrammarTools = options?.supportsOpenAIGrammarTools ?? false;
207
+ return tools.map((tool) => {
208
+ const grammar = resolveGrammarConstrainedSampling(tool, supportsOpenAIGrammarTools);
209
+ if (grammar) return {
210
+ type: "custom",
211
+ name: tool.name,
212
+ description: tool.description,
213
+ format: {
214
+ type: "grammar",
215
+ syntax: grammar.format,
216
+ definition: grammar.definition
217
+ },
218
+ ...options?.deferLoading ? { defer_loading: true } : {}
219
+ };
220
+ const constrainedStrict = resolveJsonSchemaStrictSampling(tool, supportsStrictMode);
221
+ const functionTool = {
222
+ type: "function",
223
+ name: tool.name,
224
+ description: tool.description,
225
+ parameters: tool.parameters,
226
+ ...options?.deferLoading ? { defer_loading: true } : {}
227
+ };
228
+ if (supportsStrictMode) functionTool.strict = constrainedStrict ?? defaultStrict;
229
+ return functionTool;
230
+ });
231
+ }
232
+ function getCustomToolCallInput(block) {
233
+ const property = block.customInput?.property;
234
+ if (property === void 0) return "";
235
+ const value = block.arguments[property];
236
+ return typeof value === "string" ? value : "";
237
+ }
238
+ function appendCustomToolCallInput(block, nextInput, close) {
239
+ const customInput = block.customInput;
240
+ if (!customInput) return void 0;
241
+ const delta = appendGrammarToolInputJsonDelta(customInput.jsonBuffer, customInput.property, nextInput, close);
242
+ block.arguments = { [customInput.property]: nextInput };
243
+ return delta;
244
+ }
245
+ async function processResponsesStream(openaiStream, output, stream, model, options) {
246
+ let sawTerminalResponseEvent = false;
247
+ const outputSlots = /* @__PURE__ */ new Map();
248
+ const reasoningBlocksById = /* @__PURE__ */ new Map();
249
+ const applyMessagePhaseStopReason = (item) => {
250
+ if (item.type === "message" && item.phase === "final_answer") output.stopReason = "stop";
251
+ };
252
+ const getSlot = (outputIndex, type) => {
253
+ const slot = outputSlots.get(outputIndex);
254
+ return slot?.type === type ? slot : void 0;
255
+ };
256
+ const pushToolCallDelta = (slot, delta) => {
257
+ if (delta === void 0) return;
258
+ stream.push({
259
+ type: "toolcall_delta",
260
+ contentIndex: slot.contentIndex,
261
+ delta,
262
+ partial: output
263
+ });
264
+ };
265
+ const createSlot = (outputIndex, item) => {
266
+ if (item.type === "reasoning") {
267
+ const block = {
268
+ type: "thinking",
269
+ thinking: ""
270
+ };
271
+ output.content.push(block);
272
+ const slot = {
273
+ type: "thinking",
274
+ block,
275
+ contentIndex: output.content.length - 1
276
+ };
277
+ outputSlots.set(outputIndex, slot);
278
+ stream.push({
279
+ type: "thinking_start",
280
+ contentIndex: slot.contentIndex,
281
+ partial: output
282
+ });
283
+ return slot;
284
+ }
285
+ if (item.type === "message") {
286
+ applyMessagePhaseStopReason(item);
287
+ const block = {
288
+ type: "text",
289
+ text: ""
290
+ };
291
+ output.content.push(block);
292
+ const slot = {
293
+ type: "text",
294
+ block,
295
+ contentIndex: output.content.length - 1
296
+ };
297
+ outputSlots.set(outputIndex, slot);
298
+ stream.push({
299
+ type: "text_start",
300
+ contentIndex: slot.contentIndex,
301
+ partial: output
302
+ });
303
+ return slot;
304
+ }
305
+ if (item.type === "function_call") {
306
+ const block = {
307
+ type: "toolCall",
308
+ id: `${item.call_id}|${item.id}`,
309
+ name: item.name,
310
+ arguments: {},
311
+ partialJson: item.arguments || ""
312
+ };
313
+ output.content.push(block);
314
+ const slot = {
315
+ type: "toolCall",
316
+ block,
317
+ contentIndex: output.content.length - 1
318
+ };
319
+ outputSlots.set(outputIndex, slot);
320
+ stream.push({
321
+ type: "toolcall_start",
322
+ contentIndex: slot.contentIndex,
323
+ partial: output
324
+ });
325
+ return slot;
326
+ }
327
+ if (item.type === "custom_tool_call") {
328
+ const inputProperty = options?.grammarToolInputProperties?.get(item.name) ?? "input";
329
+ const input = item.input || "";
330
+ const block = {
331
+ type: "toolCall",
332
+ id: `${item.call_id}|${item.id}`,
333
+ name: item.name,
334
+ arguments: { [inputProperty]: input },
335
+ customInput: {
336
+ property: inputProperty,
337
+ jsonBuffer: {
338
+ input: "",
339
+ started: false,
340
+ closed: false
341
+ }
342
+ }
343
+ };
344
+ output.content.push(block);
345
+ const slot = {
346
+ type: "toolCall",
347
+ block,
348
+ contentIndex: output.content.length - 1
349
+ };
350
+ outputSlots.set(outputIndex, slot);
351
+ stream.push({
352
+ type: "toolcall_start",
353
+ contentIndex: slot.contentIndex,
354
+ partial: output
355
+ });
356
+ return slot;
357
+ }
358
+ };
359
+ const getOrCreateSlot = (outputIndex, item) => {
360
+ return outputSlots.get(outputIndex) ?? createSlot(outputIndex, item);
361
+ };
362
+ const backfillReasoningSignatures = (responseOutput) => {
363
+ for (const item of responseOutput) {
364
+ if (item.type !== "reasoning" || !item.encrypted_content) continue;
365
+ const block = reasoningBlocksById.get(item.id);
366
+ if (!block?.thinkingSignature) continue;
367
+ const storedItem = JSON.parse(block.thinkingSignature);
368
+ if (storedItem.encrypted_content) continue;
369
+ block.thinkingSignature = JSON.stringify({
370
+ ...storedItem,
371
+ encrypted_content: item.encrypted_content
372
+ });
373
+ }
374
+ };
375
+ const finalizeResponse = (response) => {
376
+ sawTerminalResponseEvent = true;
377
+ backfillReasoningSignatures(response.output ?? []);
378
+ if (response?.id) output.responseId = response.id;
379
+ if (response?.usage) {
380
+ const inputDetails = response.usage.input_tokens_details;
381
+ const cachedTokens = inputDetails?.cached_tokens || 0;
382
+ const cacheWriteTokens = inputDetails?.cache_write_tokens || 0;
383
+ output.usage = {
384
+ input: Math.max(0, (response.usage.input_tokens || 0) - cachedTokens - cacheWriteTokens),
385
+ output: response.usage.output_tokens || 0,
386
+ cacheRead: cachedTokens,
387
+ cacheWrite: cacheWriteTokens,
388
+ reasoning: response.usage.output_tokens_details?.reasoning_tokens || 0,
389
+ totalTokens: response.usage.total_tokens || 0,
390
+ cost: {
391
+ input: 0,
392
+ output: 0,
393
+ cacheRead: 0,
394
+ cacheWrite: 0,
395
+ total: 0
396
+ }
397
+ };
398
+ }
399
+ calculateCost(model, output.usage);
400
+ if (options?.applyServiceTierPricing) {
401
+ const serviceTier = options.resolveServiceTier ? options.resolveServiceTier(response?.service_tier, options.serviceTier) : response?.service_tier ?? options.serviceTier;
402
+ options.applyServiceTierPricing(output.usage, serviceTier);
403
+ }
404
+ const status = response?.status;
405
+ output.rawStopReason = status;
406
+ output.stopReason = mapStopReason(status);
407
+ if (output.content.some((b) => b.type === "toolCall") && output.stopReason === "stop") output.stopReason = "toolUse";
408
+ };
409
+ for await (const event of openaiStream) if (event.type === "response.created") output.responseId = event.response.id;
410
+ else if (event.type === "response.output_item.added") createSlot(event.output_index, event.item);
411
+ else if (event.type === "response.reasoning_summary_text.delta") {
412
+ const slot = getSlot(event.output_index, "thinking");
413
+ if (!slot) continue;
414
+ slot.block.thinking += event.delta;
415
+ stream.push({
416
+ type: "thinking_delta",
417
+ contentIndex: slot.contentIndex,
418
+ delta: event.delta,
419
+ partial: output
420
+ });
421
+ } else if (event.type === "response.reasoning_summary_part.done") {
422
+ const slot = getSlot(event.output_index, "thinking");
423
+ if (!slot) continue;
424
+ slot.block.thinking += "\n\n";
425
+ stream.push({
426
+ type: "thinking_delta",
427
+ contentIndex: slot.contentIndex,
428
+ delta: "\n\n",
429
+ partial: output
430
+ });
431
+ } else if (event.type === "response.reasoning_text.delta") {
432
+ const slot = getSlot(event.output_index, "thinking");
433
+ if (!slot) continue;
434
+ slot.block.thinking += event.delta;
435
+ stream.push({
436
+ type: "thinking_delta",
437
+ contentIndex: slot.contentIndex,
438
+ delta: event.delta,
439
+ partial: output
440
+ });
441
+ } else if (event.type === "response.output_text.delta") {
442
+ const slot = getSlot(event.output_index, "text");
443
+ if (!slot) continue;
444
+ slot.block.text += event.delta;
445
+ stream.push({
446
+ type: "text_delta",
447
+ contentIndex: slot.contentIndex,
448
+ delta: event.delta,
449
+ partial: output
450
+ });
451
+ } else if (event.type === "response.refusal.delta") {
452
+ const slot = getSlot(event.output_index, "text");
453
+ if (!slot) continue;
454
+ slot.block.text += event.delta;
455
+ stream.push({
456
+ type: "text_delta",
457
+ contentIndex: slot.contentIndex,
458
+ delta: event.delta,
459
+ partial: output
460
+ });
461
+ } else if (event.type === "response.function_call_arguments.delta") {
462
+ const slot = getSlot(event.output_index, "toolCall");
463
+ if (!slot || slot.block.partialJson === void 0) continue;
464
+ slot.block.partialJson += event.delta;
465
+ slot.block.arguments = parseStreamingJson(slot.block.partialJson);
466
+ pushToolCallDelta(slot, event.delta);
467
+ } else if (event.type === "response.function_call_arguments.done") {
468
+ const slot = getSlot(event.output_index, "toolCall");
469
+ if (!slot || slot.block.partialJson === void 0) continue;
470
+ const previousPartialJson = slot.block.partialJson;
471
+ slot.block.partialJson = event.arguments;
472
+ slot.block.arguments = parseStreamingJson(slot.block.partialJson);
473
+ if (event.arguments.startsWith(previousPartialJson)) {
474
+ const delta = event.arguments.slice(previousPartialJson.length);
475
+ if (delta.length > 0) pushToolCallDelta(slot, delta);
476
+ }
477
+ } else if (event.type === "response.custom_tool_call_input.delta") {
478
+ const slot = getSlot(event.output_index, "toolCall");
479
+ if (!slot || !slot.block.customInput) continue;
480
+ pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, getCustomToolCallInput(slot.block) + event.delta, false));
481
+ } else if (event.type === "response.custom_tool_call_input.done") {
482
+ const slot = getSlot(event.output_index, "toolCall");
483
+ if (!slot || !slot.block.customInput) continue;
484
+ pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, event.input, true));
485
+ } else if (event.type === "response.output_item.done") {
486
+ const item = event.item;
487
+ applyMessagePhaseStopReason(item);
488
+ const slot = getOrCreateSlot(event.output_index, item);
489
+ if (item.type === "reasoning" && slot?.type === "thinking") {
490
+ const summaryText = item.summary?.map((s) => s.text).join("\n\n") || "";
491
+ const contentText = item.content?.map((c) => c.text).join("\n\n") || "";
492
+ slot.block.thinking = summaryText || contentText || slot.block.thinking;
493
+ slot.block.thinkingSignature = JSON.stringify(item);
494
+ reasoningBlocksById.set(item.id, slot.block);
495
+ stream.push({
496
+ type: "thinking_end",
497
+ contentIndex: slot.contentIndex,
498
+ content: slot.block.thinking,
499
+ partial: output
500
+ });
501
+ outputSlots.delete(event.output_index);
502
+ } else if (item.type === "message" && slot?.type === "text") {
503
+ slot.block.text = item.content?.map((c) => c.type === "output_text" ? c.text : c.refusal).join("") || "";
504
+ slot.block.textSignature = encodeTextSignatureV1(item.id, item.phase ?? void 0);
505
+ stream.push({
506
+ type: "text_end",
507
+ contentIndex: slot.contentIndex,
508
+ content: slot.block.text,
509
+ partial: output
510
+ });
511
+ outputSlots.delete(event.output_index);
512
+ } else if (item.type === "function_call" && slot?.type === "toolCall" && slot.block.partialJson !== void 0) {
513
+ slot.block.arguments = parseStreamingJson(item.arguments || slot.block.partialJson || "{}");
514
+ delete slot.block.partialJson;
515
+ stream.push({
516
+ type: "toolcall_end",
517
+ contentIndex: slot.contentIndex,
518
+ toolCall: slot.block,
519
+ partial: output
520
+ });
521
+ outputSlots.delete(event.output_index);
522
+ } else if (item.type === "custom_tool_call" && slot?.type === "toolCall" && slot.block.customInput) {
523
+ pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, item.input ?? getCustomToolCallInput(slot.block), true));
524
+ delete slot.block.customInput;
525
+ stream.push({
526
+ type: "toolcall_end",
527
+ contentIndex: slot.contentIndex,
528
+ toolCall: slot.block,
529
+ partial: output
530
+ });
531
+ outputSlots.delete(event.output_index);
532
+ }
533
+ } else if (event.type === "response.completed" || event.type === "response.incomplete") finalizeResponse(event.response);
534
+ else if (event.type === "error") throw new Error(`Error Code ${event.code}: ${event.message}` || "Unknown error");
535
+ else if (event.type === "response.failed") {
536
+ sawTerminalResponseEvent = true;
537
+ output.rawStopReason = event.response?.status;
538
+ const error = event.response?.error;
539
+ const details = event.response?.incomplete_details;
540
+ const msg = error ? `${error.code || "unknown"}: ${error.message || "no message"}` : details?.reason ? `incomplete: ${details.reason}` : "Unknown error (no error details in response)";
541
+ throw new Error(msg);
542
+ }
543
+ if (!sawTerminalResponseEvent) throw new Error("OpenAI Responses stream ended before a terminal response event");
544
+ }
545
+ function mapStopReason(status) {
546
+ if (!status) return "stop";
547
+ switch (status) {
548
+ case "completed": return "stop";
549
+ case "incomplete": return "length";
550
+ case "failed":
551
+ case "cancelled": return "error";
552
+ case "in_progress":
553
+ case "queued": return "stop";
554
+ default: throw new Error(`Unhandled stop reason: ${status}`);
555
+ }
556
+ }
557
+ //#endregion
558
+ export { convertResponsesMessages, convertResponsesTools, processResponsesStream };
559
+
560
+ //# sourceMappingURL=openai-responses-shared-BptkVdeL.js.map