linkque-cli-v2 1.0.5 → 1.0.7
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.
- package/README.md +1 -1
- package/linkquejs.js +23 -2212
- package/package.json +4 -1
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/anthropic-messages-B5T6TFD2.js +7172 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/azure-openai-responses-S7HNQOSE.js +229 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-2NGJGNYB.js +1765 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-3S3TOEBZ.js +608 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-46SEH6ER.js +78 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-6MHDOBWO.js +12 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-6WHNFJT4.js +31278 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-7BQRUS4C.js +671 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-7QWAKKTY.js +7054 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-CT4G24PS.js +8 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-DYEQRE6N.js +343 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-E2P6TIFG.js +31 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-FHNQ6DPF.js +14 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-FJ2BZPIM.js +60308 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-HTG7JBF2.js +38 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-I64V3ICJ.js +11166 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-IAT272TH.js +12121 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-IKZ25GJD.js +497 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-MKS245OU.js +3570 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-NKEOCGYU.js +76 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-OXGCUIKV.js +33 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-RYONFGPP.js +317 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-UEVQXSKG.js +4765 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-UFVAKOTZ.js +17 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-VS2QQHAK.js +46 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-XMVFOHGJ.js +31 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-XUFLSZM4.js +5319 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-Y6UTJIT4.js +23 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-YDKZZ6EH.js +3584 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/dist-QD3CV4KC.js +1224 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/google-generative-ai-IOXNKFEV.js +412 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/google-vertex-U4ZV5IXZ.js +461 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/mistral-conversations-G3K7SSWR.js +30344 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/multipart-parser-2VJ7LVQD.js +170 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/multipart-parser-AJMV6U6H.js +178 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/multipart-parser-PULXGNYO.js +368 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/openai-codex-responses-4JJLPTCS.js +1308 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/openai-completions-PH4WW6E7.js +977 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/openai-responses-KGWAX6HR.js +264 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/openrouter-images-LMBZZQ76.js +138 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/otel-7GSG7QQO.js +6657 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/pi-messages-ZX3KUWSZ.js +309 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/runtime/handler/chat.js +2348 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/runtime/handler/clientCapability.js +16 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/runtime/protocol.js +17 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/src-NGZW4JXI.js +1396 -0
- package/vendor/linkque-agent-appwrite-integrate/package.json +1 -0
- package/worker.js +16 -6
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
import {
|
|
2
|
+
shortHash
|
|
3
|
+
} from "./chunk-UFVAKOTZ.js";
|
|
4
|
+
import {
|
|
5
|
+
transformMessages
|
|
6
|
+
} from "./chunk-RYONFGPP.js";
|
|
7
|
+
import {
|
|
8
|
+
sanitizeSurrogates
|
|
9
|
+
} from "./chunk-CT4G24PS.js";
|
|
10
|
+
import {
|
|
11
|
+
calculateCost
|
|
12
|
+
} from "./chunk-3S3TOEBZ.js";
|
|
13
|
+
import {
|
|
14
|
+
parseStreamingJson
|
|
15
|
+
} from "./chunk-DYEQRE6N.js";
|
|
16
|
+
|
|
17
|
+
// ../../node_modules/@earendil-works/pi-ai/dist/api/openai-responses-shared.js
|
|
18
|
+
function encodeTextSignatureV1(id, phase) {
|
|
19
|
+
const payload = { v: 1, id };
|
|
20
|
+
if (phase)
|
|
21
|
+
payload.phase = phase;
|
|
22
|
+
return JSON.stringify(payload);
|
|
23
|
+
}
|
|
24
|
+
function parseTextSignature(signature) {
|
|
25
|
+
if (!signature)
|
|
26
|
+
return void 0;
|
|
27
|
+
if (signature.startsWith("{")) {
|
|
28
|
+
try {
|
|
29
|
+
const parsed = JSON.parse(signature);
|
|
30
|
+
if (parsed.v === 1 && typeof parsed.id === "string") {
|
|
31
|
+
if (parsed.phase === "commentary" || parsed.phase === "final_answer") {
|
|
32
|
+
return { id: parsed.id, phase: parsed.phase };
|
|
33
|
+
}
|
|
34
|
+
return { id: parsed.id };
|
|
35
|
+
}
|
|
36
|
+
} catch {
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return { id: signature };
|
|
40
|
+
}
|
|
41
|
+
function convertResponsesMessages(model, context, allowedToolCallProviders, options) {
|
|
42
|
+
const messages = [];
|
|
43
|
+
const loadedToolNames = /* @__PURE__ */ new Set();
|
|
44
|
+
const normalizeIdPart = (part) => {
|
|
45
|
+
const sanitized = part.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
46
|
+
const normalized = sanitized.length > 64 ? sanitized.slice(0, 64) : sanitized;
|
|
47
|
+
return normalized.replace(/_+$/, "");
|
|
48
|
+
};
|
|
49
|
+
const buildForeignResponsesItemId = (itemId) => {
|
|
50
|
+
const normalized = `fc_${shortHash(itemId)}`;
|
|
51
|
+
return normalized.length > 64 ? normalized.slice(0, 64) : normalized;
|
|
52
|
+
};
|
|
53
|
+
const normalizeToolCallId = (id, _targetModel, source) => {
|
|
54
|
+
if (!allowedToolCallProviders.has(model.provider))
|
|
55
|
+
return normalizeIdPart(id);
|
|
56
|
+
if (!id.includes("|"))
|
|
57
|
+
return normalizeIdPart(id);
|
|
58
|
+
const [callId, itemId] = id.split("|");
|
|
59
|
+
const normalizedCallId = normalizeIdPart(callId);
|
|
60
|
+
const isForeignToolCall = source.provider !== model.provider || source.api !== model.api;
|
|
61
|
+
let normalizedItemId = isForeignToolCall ? buildForeignResponsesItemId(itemId) : normalizeIdPart(itemId);
|
|
62
|
+
if (!normalizedItemId.startsWith("fc_")) {
|
|
63
|
+
normalizedItemId = normalizeIdPart(`fc_${normalizedItemId}`);
|
|
64
|
+
}
|
|
65
|
+
return `${normalizedCallId}|${normalizedItemId}`;
|
|
66
|
+
};
|
|
67
|
+
const transformedMessages = transformMessages(context.messages, model, normalizeToolCallId);
|
|
68
|
+
const includeSystemPrompt = options?.includeSystemPrompt ?? true;
|
|
69
|
+
if (includeSystemPrompt && context.systemPrompt) {
|
|
70
|
+
const compat = model.compat;
|
|
71
|
+
const role = model.reasoning && compat?.supportsDeveloperRole !== false ? "developer" : "system";
|
|
72
|
+
messages.push({
|
|
73
|
+
role,
|
|
74
|
+
content: sanitizeSurrogates(context.systemPrompt)
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
let msgIndex = 0;
|
|
78
|
+
for (const msg of transformedMessages) {
|
|
79
|
+
if (msg.role === "user") {
|
|
80
|
+
if (typeof msg.content === "string") {
|
|
81
|
+
messages.push({
|
|
82
|
+
role: "user",
|
|
83
|
+
content: [{ type: "input_text", text: sanitizeSurrogates(msg.content) }]
|
|
84
|
+
});
|
|
85
|
+
} else {
|
|
86
|
+
const content = msg.content.map((item) => {
|
|
87
|
+
if (item.type === "text") {
|
|
88
|
+
return {
|
|
89
|
+
type: "input_text",
|
|
90
|
+
text: sanitizeSurrogates(item.text)
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
type: "input_image",
|
|
95
|
+
detail: "auto",
|
|
96
|
+
image_url: `data:${item.mimeType};base64,${item.data}`
|
|
97
|
+
};
|
|
98
|
+
});
|
|
99
|
+
if (content.length === 0)
|
|
100
|
+
continue;
|
|
101
|
+
messages.push({
|
|
102
|
+
role: "user",
|
|
103
|
+
content
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
} else if (msg.role === "assistant") {
|
|
107
|
+
const output = [];
|
|
108
|
+
const assistantMsg = msg;
|
|
109
|
+
const isDifferentModel = assistantMsg.model !== model.id && assistantMsg.provider === model.provider && assistantMsg.api === model.api;
|
|
110
|
+
let textBlockIndex = 0;
|
|
111
|
+
for (const block of msg.content) {
|
|
112
|
+
if (block.type === "thinking") {
|
|
113
|
+
if (block.thinkingSignature) {
|
|
114
|
+
const reasoningItem = JSON.parse(block.thinkingSignature);
|
|
115
|
+
output.push(reasoningItem);
|
|
116
|
+
}
|
|
117
|
+
} else if (block.type === "text") {
|
|
118
|
+
const textBlock = block;
|
|
119
|
+
const parsedSignature = parseTextSignature(textBlock.textSignature);
|
|
120
|
+
const fallbackMessageId = textBlockIndex === 0 ? `msg_pi_${msgIndex}` : `msg_pi_${msgIndex}_${textBlockIndex}`;
|
|
121
|
+
textBlockIndex++;
|
|
122
|
+
let msgId = parsedSignature?.id;
|
|
123
|
+
if (!msgId) {
|
|
124
|
+
msgId = fallbackMessageId;
|
|
125
|
+
} else if (msgId.length > 64) {
|
|
126
|
+
msgId = `msg_${shortHash(msgId)}`;
|
|
127
|
+
}
|
|
128
|
+
output.push({
|
|
129
|
+
type: "message",
|
|
130
|
+
role: "assistant",
|
|
131
|
+
content: [{ type: "output_text", text: sanitizeSurrogates(textBlock.text), annotations: [] }],
|
|
132
|
+
status: "completed",
|
|
133
|
+
id: msgId,
|
|
134
|
+
phase: parsedSignature?.phase
|
|
135
|
+
});
|
|
136
|
+
} else if (block.type === "toolCall") {
|
|
137
|
+
const toolCall = block;
|
|
138
|
+
const [callId, itemIdRaw] = toolCall.id.split("|");
|
|
139
|
+
let itemId = itemIdRaw;
|
|
140
|
+
if (isDifferentModel && itemId?.startsWith("fc_")) {
|
|
141
|
+
itemId = void 0;
|
|
142
|
+
}
|
|
143
|
+
output.push({
|
|
144
|
+
type: "function_call",
|
|
145
|
+
id: itemId,
|
|
146
|
+
call_id: callId,
|
|
147
|
+
name: toolCall.name,
|
|
148
|
+
arguments: JSON.stringify(toolCall.arguments)
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (output.length === 0)
|
|
153
|
+
continue;
|
|
154
|
+
messages.push(...output);
|
|
155
|
+
} else if (msg.role === "toolResult") {
|
|
156
|
+
const textResult = msg.content.filter((c) => c.type === "text").map((c) => c.text).join("\n");
|
|
157
|
+
const hasImages = msg.content.some((c) => c.type === "image");
|
|
158
|
+
const hasText = textResult.length > 0;
|
|
159
|
+
const [callId] = msg.toolCallId.split("|");
|
|
160
|
+
let output;
|
|
161
|
+
if (hasImages && model.input.includes("image")) {
|
|
162
|
+
const contentParts = [];
|
|
163
|
+
if (hasText) {
|
|
164
|
+
contentParts.push({
|
|
165
|
+
type: "input_text",
|
|
166
|
+
text: sanitizeSurrogates(textResult)
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
for (const block of msg.content) {
|
|
170
|
+
if (block.type === "image") {
|
|
171
|
+
contentParts.push({
|
|
172
|
+
type: "input_image",
|
|
173
|
+
detail: "auto",
|
|
174
|
+
image_url: `data:${block.mimeType};base64,${block.data}`
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
output = contentParts;
|
|
179
|
+
} else {
|
|
180
|
+
output = sanitizeSurrogates(hasText ? textResult : hasImages ? "(see attached image)" : "(no tool output)");
|
|
181
|
+
}
|
|
182
|
+
messages.push({
|
|
183
|
+
type: "function_call_output",
|
|
184
|
+
call_id: callId,
|
|
185
|
+
output
|
|
186
|
+
});
|
|
187
|
+
const deferredTools = [];
|
|
188
|
+
for (const name of msg.addedToolNames ?? []) {
|
|
189
|
+
const tool = options?.deferredTools?.get(name);
|
|
190
|
+
if (!tool || loadedToolNames.has(name))
|
|
191
|
+
continue;
|
|
192
|
+
loadedToolNames.add(name);
|
|
193
|
+
deferredTools.push(tool);
|
|
194
|
+
}
|
|
195
|
+
if (deferredTools.length > 0) {
|
|
196
|
+
const names = deferredTools.map((tool) => tool.name);
|
|
197
|
+
const searchCallId = `pi_tool_load_${shortHash(`${msg.toolCallId}:${names.join(",")}`)}`;
|
|
198
|
+
messages.push({
|
|
199
|
+
type: "tool_search_call",
|
|
200
|
+
call_id: searchCallId,
|
|
201
|
+
execution: "client",
|
|
202
|
+
status: "completed",
|
|
203
|
+
arguments: { query: names.join(" "), limit: names.length }
|
|
204
|
+
});
|
|
205
|
+
messages.push({
|
|
206
|
+
type: "tool_search_output",
|
|
207
|
+
call_id: searchCallId,
|
|
208
|
+
execution: "client",
|
|
209
|
+
status: "completed",
|
|
210
|
+
tools: convertResponsesTools(deferredTools, { deferLoading: true })
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
msgIndex++;
|
|
215
|
+
}
|
|
216
|
+
return messages;
|
|
217
|
+
}
|
|
218
|
+
function convertResponsesTools(tools, options) {
|
|
219
|
+
const strict = options?.strict === void 0 ? false : options.strict;
|
|
220
|
+
return tools.map((tool) => ({
|
|
221
|
+
type: "function",
|
|
222
|
+
name: tool.name,
|
|
223
|
+
description: tool.description,
|
|
224
|
+
parameters: tool.parameters,
|
|
225
|
+
// TypeBox already generates JSON Schema
|
|
226
|
+
strict,
|
|
227
|
+
...options?.deferLoading ? { defer_loading: true } : {}
|
|
228
|
+
}));
|
|
229
|
+
}
|
|
230
|
+
async function processResponsesStream(openaiStream, output, stream, model, options) {
|
|
231
|
+
let sawTerminalResponseEvent = false;
|
|
232
|
+
const outputSlots = /* @__PURE__ */ new Map();
|
|
233
|
+
const reasoningBlocksById = /* @__PURE__ */ new Map();
|
|
234
|
+
const getSlot = (outputIndex, type) => {
|
|
235
|
+
const slot = outputSlots.get(outputIndex);
|
|
236
|
+
return slot?.type === type ? slot : void 0;
|
|
237
|
+
};
|
|
238
|
+
const createSlot = (outputIndex, item) => {
|
|
239
|
+
if (item.type === "reasoning") {
|
|
240
|
+
const block = { type: "thinking", thinking: "" };
|
|
241
|
+
output.content.push(block);
|
|
242
|
+
const slot = {
|
|
243
|
+
type: "thinking",
|
|
244
|
+
block,
|
|
245
|
+
contentIndex: output.content.length - 1
|
|
246
|
+
};
|
|
247
|
+
outputSlots.set(outputIndex, slot);
|
|
248
|
+
stream.push({ type: "thinking_start", contentIndex: slot.contentIndex, partial: output });
|
|
249
|
+
return slot;
|
|
250
|
+
}
|
|
251
|
+
if (item.type === "message") {
|
|
252
|
+
const block = { type: "text", text: "" };
|
|
253
|
+
output.content.push(block);
|
|
254
|
+
const slot = { type: "text", block, contentIndex: output.content.length - 1 };
|
|
255
|
+
outputSlots.set(outputIndex, slot);
|
|
256
|
+
stream.push({ type: "text_start", contentIndex: slot.contentIndex, partial: output });
|
|
257
|
+
return slot;
|
|
258
|
+
}
|
|
259
|
+
if (item.type === "function_call") {
|
|
260
|
+
const block = {
|
|
261
|
+
type: "toolCall",
|
|
262
|
+
id: `${item.call_id}|${item.id}`,
|
|
263
|
+
name: item.name,
|
|
264
|
+
arguments: {},
|
|
265
|
+
partialJson: item.arguments || ""
|
|
266
|
+
};
|
|
267
|
+
output.content.push(block);
|
|
268
|
+
const slot = {
|
|
269
|
+
type: "toolCall",
|
|
270
|
+
block,
|
|
271
|
+
contentIndex: output.content.length - 1
|
|
272
|
+
};
|
|
273
|
+
outputSlots.set(outputIndex, slot);
|
|
274
|
+
stream.push({ type: "toolcall_start", contentIndex: slot.contentIndex, partial: output });
|
|
275
|
+
return slot;
|
|
276
|
+
}
|
|
277
|
+
return void 0;
|
|
278
|
+
};
|
|
279
|
+
const getOrCreateSlot = (outputIndex, item) => {
|
|
280
|
+
return outputSlots.get(outputIndex) ?? createSlot(outputIndex, item);
|
|
281
|
+
};
|
|
282
|
+
const backfillReasoningSignatures = (responseOutput) => {
|
|
283
|
+
for (const item of responseOutput) {
|
|
284
|
+
if (item.type !== "reasoning" || !item.encrypted_content)
|
|
285
|
+
continue;
|
|
286
|
+
const block = reasoningBlocksById.get(item.id);
|
|
287
|
+
if (!block?.thinkingSignature)
|
|
288
|
+
continue;
|
|
289
|
+
const storedItem = JSON.parse(block.thinkingSignature);
|
|
290
|
+
if (storedItem.encrypted_content)
|
|
291
|
+
continue;
|
|
292
|
+
block.thinkingSignature = JSON.stringify({
|
|
293
|
+
...storedItem,
|
|
294
|
+
encrypted_content: item.encrypted_content
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
const finalizeResponse = (response) => {
|
|
299
|
+
sawTerminalResponseEvent = true;
|
|
300
|
+
backfillReasoningSignatures(response.output ?? []);
|
|
301
|
+
if (response?.id) {
|
|
302
|
+
output.responseId = response.id;
|
|
303
|
+
}
|
|
304
|
+
if (response?.usage) {
|
|
305
|
+
const inputDetails = response.usage.input_tokens_details;
|
|
306
|
+
const cachedTokens = inputDetails?.cached_tokens || 0;
|
|
307
|
+
const cacheWriteTokens = inputDetails?.cache_write_tokens || 0;
|
|
308
|
+
output.usage = {
|
|
309
|
+
// OpenAI includes cached and cache-write tokens in input_tokens, so subtract both.
|
|
310
|
+
input: Math.max(0, (response.usage.input_tokens || 0) - cachedTokens - cacheWriteTokens),
|
|
311
|
+
output: response.usage.output_tokens || 0,
|
|
312
|
+
cacheRead: cachedTokens,
|
|
313
|
+
cacheWrite: cacheWriteTokens,
|
|
314
|
+
reasoning: response.usage.output_tokens_details?.reasoning_tokens || 0,
|
|
315
|
+
totalTokens: response.usage.total_tokens || 0,
|
|
316
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
calculateCost(model, output.usage);
|
|
320
|
+
if (options?.applyServiceTierPricing) {
|
|
321
|
+
const serviceTier = options.resolveServiceTier ? options.resolveServiceTier(response?.service_tier, options.serviceTier) : response?.service_tier ?? options.serviceTier;
|
|
322
|
+
options.applyServiceTierPricing(output.usage, serviceTier);
|
|
323
|
+
}
|
|
324
|
+
output.stopReason = mapStopReason(response?.status);
|
|
325
|
+
if (output.content.some((b) => b.type === "toolCall") && output.stopReason === "stop") {
|
|
326
|
+
output.stopReason = "toolUse";
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
for await (const event of openaiStream) {
|
|
330
|
+
if (event.type === "response.created") {
|
|
331
|
+
output.responseId = event.response.id;
|
|
332
|
+
} else if (event.type === "response.output_item.added") {
|
|
333
|
+
createSlot(event.output_index, event.item);
|
|
334
|
+
} else if (event.type === "response.reasoning_summary_text.delta") {
|
|
335
|
+
const slot = getSlot(event.output_index, "thinking");
|
|
336
|
+
if (!slot)
|
|
337
|
+
continue;
|
|
338
|
+
slot.block.thinking += event.delta;
|
|
339
|
+
stream.push({
|
|
340
|
+
type: "thinking_delta",
|
|
341
|
+
contentIndex: slot.contentIndex,
|
|
342
|
+
delta: event.delta,
|
|
343
|
+
partial: output
|
|
344
|
+
});
|
|
345
|
+
} else if (event.type === "response.reasoning_summary_part.done") {
|
|
346
|
+
const slot = getSlot(event.output_index, "thinking");
|
|
347
|
+
if (!slot)
|
|
348
|
+
continue;
|
|
349
|
+
slot.block.thinking += "\n\n";
|
|
350
|
+
stream.push({
|
|
351
|
+
type: "thinking_delta",
|
|
352
|
+
contentIndex: slot.contentIndex,
|
|
353
|
+
delta: "\n\n",
|
|
354
|
+
partial: output
|
|
355
|
+
});
|
|
356
|
+
} else if (event.type === "response.reasoning_text.delta") {
|
|
357
|
+
const slot = getSlot(event.output_index, "thinking");
|
|
358
|
+
if (!slot)
|
|
359
|
+
continue;
|
|
360
|
+
slot.block.thinking += event.delta;
|
|
361
|
+
stream.push({
|
|
362
|
+
type: "thinking_delta",
|
|
363
|
+
contentIndex: slot.contentIndex,
|
|
364
|
+
delta: event.delta,
|
|
365
|
+
partial: output
|
|
366
|
+
});
|
|
367
|
+
} else if (event.type === "response.output_text.delta") {
|
|
368
|
+
const slot = getSlot(event.output_index, "text");
|
|
369
|
+
if (!slot)
|
|
370
|
+
continue;
|
|
371
|
+
slot.block.text += event.delta;
|
|
372
|
+
stream.push({
|
|
373
|
+
type: "text_delta",
|
|
374
|
+
contentIndex: slot.contentIndex,
|
|
375
|
+
delta: event.delta,
|
|
376
|
+
partial: output
|
|
377
|
+
});
|
|
378
|
+
} else if (event.type === "response.refusal.delta") {
|
|
379
|
+
const slot = getSlot(event.output_index, "text");
|
|
380
|
+
if (!slot)
|
|
381
|
+
continue;
|
|
382
|
+
slot.block.text += event.delta;
|
|
383
|
+
stream.push({
|
|
384
|
+
type: "text_delta",
|
|
385
|
+
contentIndex: slot.contentIndex,
|
|
386
|
+
delta: event.delta,
|
|
387
|
+
partial: output
|
|
388
|
+
});
|
|
389
|
+
} else if (event.type === "response.function_call_arguments.delta") {
|
|
390
|
+
const slot = getSlot(event.output_index, "toolCall");
|
|
391
|
+
if (!slot)
|
|
392
|
+
continue;
|
|
393
|
+
slot.block.partialJson += event.delta;
|
|
394
|
+
slot.block.arguments = parseStreamingJson(slot.block.partialJson);
|
|
395
|
+
stream.push({
|
|
396
|
+
type: "toolcall_delta",
|
|
397
|
+
contentIndex: slot.contentIndex,
|
|
398
|
+
delta: event.delta,
|
|
399
|
+
partial: output
|
|
400
|
+
});
|
|
401
|
+
} else if (event.type === "response.function_call_arguments.done") {
|
|
402
|
+
const slot = getSlot(event.output_index, "toolCall");
|
|
403
|
+
if (!slot)
|
|
404
|
+
continue;
|
|
405
|
+
const previousPartialJson = slot.block.partialJson;
|
|
406
|
+
slot.block.partialJson = event.arguments;
|
|
407
|
+
slot.block.arguments = parseStreamingJson(slot.block.partialJson);
|
|
408
|
+
if (event.arguments.startsWith(previousPartialJson)) {
|
|
409
|
+
const delta = event.arguments.slice(previousPartialJson.length);
|
|
410
|
+
if (delta.length > 0) {
|
|
411
|
+
stream.push({
|
|
412
|
+
type: "toolcall_delta",
|
|
413
|
+
contentIndex: slot.contentIndex,
|
|
414
|
+
delta,
|
|
415
|
+
partial: output
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
} else if (event.type === "response.output_item.done") {
|
|
420
|
+
const item = event.item;
|
|
421
|
+
const slot = getOrCreateSlot(event.output_index, item);
|
|
422
|
+
if (item.type === "reasoning" && slot?.type === "thinking") {
|
|
423
|
+
const summaryText = item.summary?.map((s) => s.text).join("\n\n") || "";
|
|
424
|
+
const contentText = item.content?.map((c) => c.text).join("\n\n") || "";
|
|
425
|
+
slot.block.thinking = summaryText || contentText || slot.block.thinking;
|
|
426
|
+
slot.block.thinkingSignature = JSON.stringify(item);
|
|
427
|
+
reasoningBlocksById.set(item.id, slot.block);
|
|
428
|
+
stream.push({
|
|
429
|
+
type: "thinking_end",
|
|
430
|
+
contentIndex: slot.contentIndex,
|
|
431
|
+
content: slot.block.thinking,
|
|
432
|
+
partial: output
|
|
433
|
+
});
|
|
434
|
+
outputSlots.delete(event.output_index);
|
|
435
|
+
} else if (item.type === "message" && slot?.type === "text") {
|
|
436
|
+
slot.block.text = item.content?.map((c) => c.type === "output_text" ? c.text : c.refusal).join("") || "";
|
|
437
|
+
slot.block.textSignature = encodeTextSignatureV1(item.id, item.phase ?? void 0);
|
|
438
|
+
stream.push({
|
|
439
|
+
type: "text_end",
|
|
440
|
+
contentIndex: slot.contentIndex,
|
|
441
|
+
content: slot.block.text,
|
|
442
|
+
partial: output
|
|
443
|
+
});
|
|
444
|
+
outputSlots.delete(event.output_index);
|
|
445
|
+
} else if (item.type === "function_call" && slot?.type === "toolCall") {
|
|
446
|
+
slot.block.arguments = parseStreamingJson(item.arguments || slot.block.partialJson || "{}");
|
|
447
|
+
delete slot.block.partialJson;
|
|
448
|
+
stream.push({
|
|
449
|
+
type: "toolcall_end",
|
|
450
|
+
contentIndex: slot.contentIndex,
|
|
451
|
+
toolCall: slot.block,
|
|
452
|
+
partial: output
|
|
453
|
+
});
|
|
454
|
+
outputSlots.delete(event.output_index);
|
|
455
|
+
}
|
|
456
|
+
} else if (event.type === "response.completed" || event.type === "response.incomplete") {
|
|
457
|
+
finalizeResponse(event.response);
|
|
458
|
+
} else if (event.type === "error") {
|
|
459
|
+
throw new Error(`Error Code ${event.code}: ${event.message}` || "Unknown error");
|
|
460
|
+
} else if (event.type === "response.failed") {
|
|
461
|
+
sawTerminalResponseEvent = true;
|
|
462
|
+
const error = event.response?.error;
|
|
463
|
+
const details = event.response?.incomplete_details;
|
|
464
|
+
const msg = error ? `${error.code || "unknown"}: ${error.message || "no message"}` : details?.reason ? `incomplete: ${details.reason}` : "Unknown error (no error details in response)";
|
|
465
|
+
throw new Error(msg);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if (!sawTerminalResponseEvent) {
|
|
469
|
+
throw new Error("OpenAI Responses stream ended before a terminal response event");
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
function mapStopReason(status) {
|
|
473
|
+
if (!status)
|
|
474
|
+
return "stop";
|
|
475
|
+
switch (status) {
|
|
476
|
+
case "completed":
|
|
477
|
+
return "stop";
|
|
478
|
+
case "incomplete":
|
|
479
|
+
return "length";
|
|
480
|
+
case "failed":
|
|
481
|
+
case "cancelled":
|
|
482
|
+
return "error";
|
|
483
|
+
case "in_progress":
|
|
484
|
+
case "queued":
|
|
485
|
+
return "stop";
|
|
486
|
+
default: {
|
|
487
|
+
const _exhaustive = status;
|
|
488
|
+
throw new Error(`Unhandled stop reason: ${_exhaustive}`);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
export {
|
|
494
|
+
convertResponsesMessages,
|
|
495
|
+
convertResponsesTools,
|
|
496
|
+
processResponsesStream
|
|
497
|
+
};
|