llm-kb 0.4.0 → 0.4.2
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 +183 -42
- package/bin/anthropic-5TIU2EED.js +5515 -0
- package/bin/azure-openai-responses-ZVUVMK3G.js +190 -0
- package/bin/chunk-2WV6TQRI.js +4792 -0
- package/bin/chunk-3YMNGUZZ.js +262 -0
- package/bin/chunk-5PYKQQLA.js +14295 -0
- package/bin/chunk-65KFH7OI.js +31 -0
- package/bin/chunk-DHOXVEIR.js +7261 -0
- package/bin/chunk-EAQYK3U2.js +41 -0
- package/bin/chunk-IFS3OKBN.js +428 -0
- package/bin/chunk-LDHOKBJA.js +86 -0
- package/bin/chunk-SLYBG6ZQ.js +32681 -0
- package/bin/chunk-UEODFF7H.js +17 -0
- package/bin/chunk-XCXTZJGO.js +174 -0
- package/bin/chunk-XFV534WU.js +7056 -0
- package/bin/cli.js +30 -4
- package/bin/dist-3YH7P2QF.js +1244 -0
- package/bin/google-JFC43EFJ.js +371 -0
- package/bin/google-gemini-cli-K4XNMYDI.js +712 -0
- package/bin/google-vertex-Y42F254G.js +414 -0
- package/bin/indexer-KSYRIVVN.js +10 -0
- package/bin/mistral-ZU2JS5XZ.js +38406 -0
- package/bin/multipart-parser-CO464TZY.js +371 -0
- package/bin/openai-codex-responses-NW2LELBH.js +712 -0
- package/bin/openai-completions-TW3VKTHO.js +662 -0
- package/bin/openai-responses-VGL522MK.js +198 -0
- package/bin/src-Y22OHE3S.js +1408 -0
- package/package.json +6 -1
- package/PHASE2_SPEC.md +0 -274
- package/PHASE3_SPEC.md +0 -245
- package/PHASE4_SPEC.md +0 -358
- package/SPEC.md +0 -275
- package/plan.md +0 -300
- package/src/auth.ts +0 -55
- package/src/cli.ts +0 -257
- package/src/config.ts +0 -61
- package/src/eval.ts +0 -548
- package/src/indexer.ts +0 -152
- package/src/md-stream.ts +0 -133
- package/src/pdf.ts +0 -119
- package/src/query.ts +0 -408
- package/src/resolve-kb.ts +0 -19
- package/src/scan.ts +0 -59
- package/src/session-store.ts +0 -22
- package/src/session-watcher.ts +0 -89
- package/src/trace-builder.ts +0 -168
- package/src/tui-display.ts +0 -281
- package/src/utils.ts +0 -17
- package/src/watcher.ts +0 -87
- package/src/wiki-updater.ts +0 -136
- package/test/auth.test.ts +0 -65
- package/test/config.test.ts +0 -96
- package/test/md-stream.test.ts +0 -98
- package/test/resolve-kb.test.ts +0 -33
- package/test/scan.test.ts +0 -65
- package/test/trace-builder.test.ts +0 -215
- package/tsconfig.json +0 -14
- package/vitest.config.ts +0 -8
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
8
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
9
|
+
}) : x)(function(x) {
|
|
10
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
11
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
12
|
+
});
|
|
13
|
+
var __esm = (fn, res) => function __init() {
|
|
14
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
15
|
+
};
|
|
16
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
17
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
18
|
+
};
|
|
19
|
+
var __copyProps = (to, from, except, desc) => {
|
|
20
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
+
for (let key of __getOwnPropNames(from))
|
|
22
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
28
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
29
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
30
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
31
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
32
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
33
|
+
mod
|
|
34
|
+
));
|
|
35
|
+
|
|
36
|
+
export {
|
|
37
|
+
__require,
|
|
38
|
+
__esm,
|
|
39
|
+
__commonJS,
|
|
40
|
+
__toESM
|
|
41
|
+
};
|
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
import {
|
|
2
|
+
shortHash
|
|
3
|
+
} from "./chunk-UEODFF7H.js";
|
|
4
|
+
import {
|
|
5
|
+
parseStreamingJson
|
|
6
|
+
} from "./chunk-3YMNGUZZ.js";
|
|
7
|
+
import {
|
|
8
|
+
sanitizeSurrogates,
|
|
9
|
+
transformMessages
|
|
10
|
+
} from "./chunk-XCXTZJGO.js";
|
|
11
|
+
import {
|
|
12
|
+
calculateCost
|
|
13
|
+
} from "./chunk-5PYKQQLA.js";
|
|
14
|
+
|
|
15
|
+
// node_modules/@mariozechner/pi-ai/dist/providers/openai-responses-shared.js
|
|
16
|
+
function encodeTextSignatureV1(id, phase) {
|
|
17
|
+
const payload = { v: 1, id };
|
|
18
|
+
if (phase)
|
|
19
|
+
payload.phase = phase;
|
|
20
|
+
return JSON.stringify(payload);
|
|
21
|
+
}
|
|
22
|
+
function parseTextSignature(signature) {
|
|
23
|
+
if (!signature)
|
|
24
|
+
return void 0;
|
|
25
|
+
if (signature.startsWith("{")) {
|
|
26
|
+
try {
|
|
27
|
+
const parsed = JSON.parse(signature);
|
|
28
|
+
if (parsed.v === 1 && typeof parsed.id === "string") {
|
|
29
|
+
if (parsed.phase === "commentary" || parsed.phase === "final_answer") {
|
|
30
|
+
return { id: parsed.id, phase: parsed.phase };
|
|
31
|
+
}
|
|
32
|
+
return { id: parsed.id };
|
|
33
|
+
}
|
|
34
|
+
} catch {
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return { id: signature };
|
|
38
|
+
}
|
|
39
|
+
function convertResponsesMessages(model, context, allowedToolCallProviders, options) {
|
|
40
|
+
const messages = [];
|
|
41
|
+
const normalizeIdPart = (part) => {
|
|
42
|
+
const sanitized = part.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
43
|
+
const normalized = sanitized.length > 64 ? sanitized.slice(0, 64) : sanitized;
|
|
44
|
+
return normalized.replace(/_+$/, "");
|
|
45
|
+
};
|
|
46
|
+
const buildForeignResponsesItemId = (itemId) => {
|
|
47
|
+
const normalized = `fc_${shortHash(itemId)}`;
|
|
48
|
+
return normalized.length > 64 ? normalized.slice(0, 64) : normalized;
|
|
49
|
+
};
|
|
50
|
+
const normalizeToolCallId = (id, _targetModel, source) => {
|
|
51
|
+
if (!allowedToolCallProviders.has(model.provider))
|
|
52
|
+
return normalizeIdPart(id);
|
|
53
|
+
if (!id.includes("|"))
|
|
54
|
+
return normalizeIdPart(id);
|
|
55
|
+
const [callId, itemId] = id.split("|");
|
|
56
|
+
const normalizedCallId = normalizeIdPart(callId);
|
|
57
|
+
const isForeignToolCall = source.provider !== model.provider || source.api !== model.api;
|
|
58
|
+
let normalizedItemId = isForeignToolCall ? buildForeignResponsesItemId(itemId) : normalizeIdPart(itemId);
|
|
59
|
+
if (!normalizedItemId.startsWith("fc_")) {
|
|
60
|
+
normalizedItemId = normalizeIdPart(`fc_${normalizedItemId}`);
|
|
61
|
+
}
|
|
62
|
+
return `${normalizedCallId}|${normalizedItemId}`;
|
|
63
|
+
};
|
|
64
|
+
const transformedMessages = transformMessages(context.messages, model, normalizeToolCallId);
|
|
65
|
+
const includeSystemPrompt = options?.includeSystemPrompt ?? true;
|
|
66
|
+
if (includeSystemPrompt && context.systemPrompt) {
|
|
67
|
+
const role = model.reasoning ? "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") {
|
|
77
|
+
messages.push({
|
|
78
|
+
role: "user",
|
|
79
|
+
content: [{ type: "input_text", text: sanitizeSurrogates(msg.content) }]
|
|
80
|
+
});
|
|
81
|
+
} else {
|
|
82
|
+
const content = msg.content.map((item) => {
|
|
83
|
+
if (item.type === "text") {
|
|
84
|
+
return {
|
|
85
|
+
type: "input_text",
|
|
86
|
+
text: sanitizeSurrogates(item.text)
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
type: "input_image",
|
|
91
|
+
detail: "auto",
|
|
92
|
+
image_url: `data:${item.mimeType};base64,${item.data}`
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
const filteredContent = !model.input.includes("image") ? content.filter((c) => c.type !== "input_image") : content;
|
|
96
|
+
if (filteredContent.length === 0)
|
|
97
|
+
continue;
|
|
98
|
+
messages.push({
|
|
99
|
+
role: "user",
|
|
100
|
+
content: filteredContent
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
} else if (msg.role === "assistant") {
|
|
104
|
+
const output = [];
|
|
105
|
+
const assistantMsg = msg;
|
|
106
|
+
const isDifferentModel = assistantMsg.model !== model.id && assistantMsg.provider === model.provider && assistantMsg.api === model.api;
|
|
107
|
+
for (const block of msg.content) {
|
|
108
|
+
if (block.type === "thinking") {
|
|
109
|
+
if (block.thinkingSignature) {
|
|
110
|
+
const reasoningItem = JSON.parse(block.thinkingSignature);
|
|
111
|
+
output.push(reasoningItem);
|
|
112
|
+
}
|
|
113
|
+
} else if (block.type === "text") {
|
|
114
|
+
const textBlock = block;
|
|
115
|
+
const parsedSignature = parseTextSignature(textBlock.textSignature);
|
|
116
|
+
let msgId = parsedSignature?.id;
|
|
117
|
+
if (!msgId) {
|
|
118
|
+
msgId = `msg_${msgIndex}`;
|
|
119
|
+
} else if (msgId.length > 64) {
|
|
120
|
+
msgId = `msg_${shortHash(msgId)}`;
|
|
121
|
+
}
|
|
122
|
+
output.push({
|
|
123
|
+
type: "message",
|
|
124
|
+
role: "assistant",
|
|
125
|
+
content: [{ type: "output_text", text: sanitizeSurrogates(textBlock.text), annotations: [] }],
|
|
126
|
+
status: "completed",
|
|
127
|
+
id: msgId,
|
|
128
|
+
phase: parsedSignature?.phase
|
|
129
|
+
});
|
|
130
|
+
} else if (block.type === "toolCall") {
|
|
131
|
+
const toolCall = block;
|
|
132
|
+
const [callId, itemIdRaw] = toolCall.id.split("|");
|
|
133
|
+
let itemId = itemIdRaw;
|
|
134
|
+
if (isDifferentModel && itemId?.startsWith("fc_")) {
|
|
135
|
+
itemId = void 0;
|
|
136
|
+
}
|
|
137
|
+
output.push({
|
|
138
|
+
type: "function_call",
|
|
139
|
+
id: itemId,
|
|
140
|
+
call_id: callId,
|
|
141
|
+
name: toolCall.name,
|
|
142
|
+
arguments: JSON.stringify(toolCall.arguments)
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (output.length === 0)
|
|
147
|
+
continue;
|
|
148
|
+
messages.push(...output);
|
|
149
|
+
} else if (msg.role === "toolResult") {
|
|
150
|
+
const textResult = msg.content.filter((c) => c.type === "text").map((c) => c.text).join("\n");
|
|
151
|
+
const hasImages = msg.content.some((c) => c.type === "image");
|
|
152
|
+
const hasText = textResult.length > 0;
|
|
153
|
+
const [callId] = msg.toolCallId.split("|");
|
|
154
|
+
let output;
|
|
155
|
+
if (hasImages && model.input.includes("image")) {
|
|
156
|
+
const contentParts = [];
|
|
157
|
+
if (hasText) {
|
|
158
|
+
contentParts.push({
|
|
159
|
+
type: "input_text",
|
|
160
|
+
text: sanitizeSurrogates(textResult)
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
for (const block of msg.content) {
|
|
164
|
+
if (block.type === "image") {
|
|
165
|
+
contentParts.push({
|
|
166
|
+
type: "input_image",
|
|
167
|
+
detail: "auto",
|
|
168
|
+
image_url: `data:${block.mimeType};base64,${block.data}`
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
output = contentParts;
|
|
173
|
+
} else {
|
|
174
|
+
output = sanitizeSurrogates(hasText ? textResult : "(see attached image)");
|
|
175
|
+
}
|
|
176
|
+
messages.push({
|
|
177
|
+
type: "function_call_output",
|
|
178
|
+
call_id: callId,
|
|
179
|
+
output
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
msgIndex++;
|
|
183
|
+
}
|
|
184
|
+
return messages;
|
|
185
|
+
}
|
|
186
|
+
function convertResponsesTools(tools, options) {
|
|
187
|
+
const strict = options?.strict === void 0 ? false : options.strict;
|
|
188
|
+
return tools.map((tool) => ({
|
|
189
|
+
type: "function",
|
|
190
|
+
name: tool.name,
|
|
191
|
+
description: tool.description,
|
|
192
|
+
parameters: tool.parameters,
|
|
193
|
+
// TypeBox already generates JSON Schema
|
|
194
|
+
strict
|
|
195
|
+
}));
|
|
196
|
+
}
|
|
197
|
+
async function processResponsesStream(openaiStream, output, stream, model, options) {
|
|
198
|
+
let currentItem = null;
|
|
199
|
+
let currentBlock = null;
|
|
200
|
+
const blocks = output.content;
|
|
201
|
+
const blockIndex = () => blocks.length - 1;
|
|
202
|
+
for await (const event of openaiStream) {
|
|
203
|
+
if (event.type === "response.created") {
|
|
204
|
+
output.responseId = event.response.id;
|
|
205
|
+
} else if (event.type === "response.output_item.added") {
|
|
206
|
+
const item = event.item;
|
|
207
|
+
if (item.type === "reasoning") {
|
|
208
|
+
currentItem = item;
|
|
209
|
+
currentBlock = { type: "thinking", thinking: "" };
|
|
210
|
+
output.content.push(currentBlock);
|
|
211
|
+
stream.push({ type: "thinking_start", contentIndex: blockIndex(), partial: output });
|
|
212
|
+
} else if (item.type === "message") {
|
|
213
|
+
currentItem = item;
|
|
214
|
+
currentBlock = { type: "text", text: "" };
|
|
215
|
+
output.content.push(currentBlock);
|
|
216
|
+
stream.push({ type: "text_start", contentIndex: blockIndex(), partial: output });
|
|
217
|
+
} else if (item.type === "function_call") {
|
|
218
|
+
currentItem = item;
|
|
219
|
+
currentBlock = {
|
|
220
|
+
type: "toolCall",
|
|
221
|
+
id: `${item.call_id}|${item.id}`,
|
|
222
|
+
name: item.name,
|
|
223
|
+
arguments: {},
|
|
224
|
+
partialJson: item.arguments || ""
|
|
225
|
+
};
|
|
226
|
+
output.content.push(currentBlock);
|
|
227
|
+
stream.push({ type: "toolcall_start", contentIndex: blockIndex(), partial: output });
|
|
228
|
+
}
|
|
229
|
+
} else if (event.type === "response.reasoning_summary_part.added") {
|
|
230
|
+
if (currentItem && currentItem.type === "reasoning") {
|
|
231
|
+
currentItem.summary = currentItem.summary || [];
|
|
232
|
+
currentItem.summary.push(event.part);
|
|
233
|
+
}
|
|
234
|
+
} else if (event.type === "response.reasoning_summary_text.delta") {
|
|
235
|
+
if (currentItem?.type === "reasoning" && currentBlock?.type === "thinking") {
|
|
236
|
+
currentItem.summary = currentItem.summary || [];
|
|
237
|
+
const lastPart = currentItem.summary[currentItem.summary.length - 1];
|
|
238
|
+
if (lastPart) {
|
|
239
|
+
currentBlock.thinking += event.delta;
|
|
240
|
+
lastPart.text += event.delta;
|
|
241
|
+
stream.push({
|
|
242
|
+
type: "thinking_delta",
|
|
243
|
+
contentIndex: blockIndex(),
|
|
244
|
+
delta: event.delta,
|
|
245
|
+
partial: output
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
} else if (event.type === "response.reasoning_summary_part.done") {
|
|
250
|
+
if (currentItem?.type === "reasoning" && currentBlock?.type === "thinking") {
|
|
251
|
+
currentItem.summary = currentItem.summary || [];
|
|
252
|
+
const lastPart = currentItem.summary[currentItem.summary.length - 1];
|
|
253
|
+
if (lastPart) {
|
|
254
|
+
currentBlock.thinking += "\n\n";
|
|
255
|
+
lastPart.text += "\n\n";
|
|
256
|
+
stream.push({
|
|
257
|
+
type: "thinking_delta",
|
|
258
|
+
contentIndex: blockIndex(),
|
|
259
|
+
delta: "\n\n",
|
|
260
|
+
partial: output
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
} else if (event.type === "response.content_part.added") {
|
|
265
|
+
if (currentItem?.type === "message") {
|
|
266
|
+
currentItem.content = currentItem.content || [];
|
|
267
|
+
if (event.part.type === "output_text" || event.part.type === "refusal") {
|
|
268
|
+
currentItem.content.push(event.part);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
} else if (event.type === "response.output_text.delta") {
|
|
272
|
+
if (currentItem?.type === "message" && currentBlock?.type === "text") {
|
|
273
|
+
if (!currentItem.content || currentItem.content.length === 0) {
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
const lastPart = currentItem.content[currentItem.content.length - 1];
|
|
277
|
+
if (lastPart?.type === "output_text") {
|
|
278
|
+
currentBlock.text += event.delta;
|
|
279
|
+
lastPart.text += event.delta;
|
|
280
|
+
stream.push({
|
|
281
|
+
type: "text_delta",
|
|
282
|
+
contentIndex: blockIndex(),
|
|
283
|
+
delta: event.delta,
|
|
284
|
+
partial: output
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
} else if (event.type === "response.refusal.delta") {
|
|
289
|
+
if (currentItem?.type === "message" && currentBlock?.type === "text") {
|
|
290
|
+
if (!currentItem.content || currentItem.content.length === 0) {
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
const lastPart = currentItem.content[currentItem.content.length - 1];
|
|
294
|
+
if (lastPart?.type === "refusal") {
|
|
295
|
+
currentBlock.text += event.delta;
|
|
296
|
+
lastPart.refusal += event.delta;
|
|
297
|
+
stream.push({
|
|
298
|
+
type: "text_delta",
|
|
299
|
+
contentIndex: blockIndex(),
|
|
300
|
+
delta: event.delta,
|
|
301
|
+
partial: output
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
} else if (event.type === "response.function_call_arguments.delta") {
|
|
306
|
+
if (currentItem?.type === "function_call" && currentBlock?.type === "toolCall") {
|
|
307
|
+
currentBlock.partialJson += event.delta;
|
|
308
|
+
currentBlock.arguments = parseStreamingJson(currentBlock.partialJson);
|
|
309
|
+
stream.push({
|
|
310
|
+
type: "toolcall_delta",
|
|
311
|
+
contentIndex: blockIndex(),
|
|
312
|
+
delta: event.delta,
|
|
313
|
+
partial: output
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
} else if (event.type === "response.function_call_arguments.done") {
|
|
317
|
+
if (currentItem?.type === "function_call" && currentBlock?.type === "toolCall") {
|
|
318
|
+
const previousPartialJson = currentBlock.partialJson;
|
|
319
|
+
currentBlock.partialJson = event.arguments;
|
|
320
|
+
currentBlock.arguments = parseStreamingJson(currentBlock.partialJson);
|
|
321
|
+
if (event.arguments.startsWith(previousPartialJson)) {
|
|
322
|
+
const delta = event.arguments.slice(previousPartialJson.length);
|
|
323
|
+
if (delta.length > 0) {
|
|
324
|
+
stream.push({
|
|
325
|
+
type: "toolcall_delta",
|
|
326
|
+
contentIndex: blockIndex(),
|
|
327
|
+
delta,
|
|
328
|
+
partial: output
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
} else if (event.type === "response.output_item.done") {
|
|
334
|
+
const item = event.item;
|
|
335
|
+
if (item.type === "reasoning" && currentBlock?.type === "thinking") {
|
|
336
|
+
currentBlock.thinking = item.summary?.map((s) => s.text).join("\n\n") || "";
|
|
337
|
+
currentBlock.thinkingSignature = JSON.stringify(item);
|
|
338
|
+
stream.push({
|
|
339
|
+
type: "thinking_end",
|
|
340
|
+
contentIndex: blockIndex(),
|
|
341
|
+
content: currentBlock.thinking,
|
|
342
|
+
partial: output
|
|
343
|
+
});
|
|
344
|
+
currentBlock = null;
|
|
345
|
+
} else if (item.type === "message" && currentBlock?.type === "text") {
|
|
346
|
+
currentBlock.text = item.content.map((c) => c.type === "output_text" ? c.text : c.refusal).join("");
|
|
347
|
+
currentBlock.textSignature = encodeTextSignatureV1(item.id, item.phase ?? void 0);
|
|
348
|
+
stream.push({
|
|
349
|
+
type: "text_end",
|
|
350
|
+
contentIndex: blockIndex(),
|
|
351
|
+
content: currentBlock.text,
|
|
352
|
+
partial: output
|
|
353
|
+
});
|
|
354
|
+
currentBlock = null;
|
|
355
|
+
} else if (item.type === "function_call") {
|
|
356
|
+
const args = currentBlock?.type === "toolCall" && currentBlock.partialJson ? parseStreamingJson(currentBlock.partialJson) : parseStreamingJson(item.arguments || "{}");
|
|
357
|
+
const toolCall = {
|
|
358
|
+
type: "toolCall",
|
|
359
|
+
id: `${item.call_id}|${item.id}`,
|
|
360
|
+
name: item.name,
|
|
361
|
+
arguments: args
|
|
362
|
+
};
|
|
363
|
+
currentBlock = null;
|
|
364
|
+
stream.push({ type: "toolcall_end", contentIndex: blockIndex(), toolCall, partial: output });
|
|
365
|
+
}
|
|
366
|
+
} else if (event.type === "response.completed") {
|
|
367
|
+
const response = event.response;
|
|
368
|
+
if (response?.id) {
|
|
369
|
+
output.responseId = response.id;
|
|
370
|
+
}
|
|
371
|
+
if (response?.usage) {
|
|
372
|
+
const cachedTokens = response.usage.input_tokens_details?.cached_tokens || 0;
|
|
373
|
+
output.usage = {
|
|
374
|
+
// OpenAI includes cached tokens in input_tokens, so subtract to get non-cached input
|
|
375
|
+
input: (response.usage.input_tokens || 0) - cachedTokens,
|
|
376
|
+
output: response.usage.output_tokens || 0,
|
|
377
|
+
cacheRead: cachedTokens,
|
|
378
|
+
cacheWrite: 0,
|
|
379
|
+
totalTokens: response.usage.total_tokens || 0,
|
|
380
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
calculateCost(model, output.usage);
|
|
384
|
+
if (options?.applyServiceTierPricing) {
|
|
385
|
+
const serviceTier = response?.service_tier ?? options.serviceTier;
|
|
386
|
+
options.applyServiceTierPricing(output.usage, serviceTier);
|
|
387
|
+
}
|
|
388
|
+
output.stopReason = mapStopReason(response?.status);
|
|
389
|
+
if (output.content.some((b) => b.type === "toolCall") && output.stopReason === "stop") {
|
|
390
|
+
output.stopReason = "toolUse";
|
|
391
|
+
}
|
|
392
|
+
} else if (event.type === "error") {
|
|
393
|
+
throw new Error(`Error Code ${event.code}: ${event.message}` || "Unknown error");
|
|
394
|
+
} else if (event.type === "response.failed") {
|
|
395
|
+
const error = event.response?.error;
|
|
396
|
+
const details = event.response?.incomplete_details;
|
|
397
|
+
const msg = error ? `${error.code || "unknown"}: ${error.message || "no message"}` : details?.reason ? `incomplete: ${details.reason}` : "Unknown error (no error details in response)";
|
|
398
|
+
throw new Error(msg);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
function mapStopReason(status) {
|
|
403
|
+
if (!status)
|
|
404
|
+
return "stop";
|
|
405
|
+
switch (status) {
|
|
406
|
+
case "completed":
|
|
407
|
+
return "stop";
|
|
408
|
+
case "incomplete":
|
|
409
|
+
return "length";
|
|
410
|
+
case "failed":
|
|
411
|
+
case "cancelled":
|
|
412
|
+
return "error";
|
|
413
|
+
// These two are wonky ...
|
|
414
|
+
case "in_progress":
|
|
415
|
+
case "queued":
|
|
416
|
+
return "stop";
|
|
417
|
+
default: {
|
|
418
|
+
const _exhaustive = status;
|
|
419
|
+
throw new Error(`Unhandled stop reason: ${_exhaustive}`);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export {
|
|
425
|
+
convertResponsesMessages,
|
|
426
|
+
convertResponsesTools,
|
|
427
|
+
processResponsesStream
|
|
428
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// node_modules/@mariozechner/pi-ai/dist/env-api-keys.js
|
|
2
|
+
var _existsSync = null;
|
|
3
|
+
var _homedir = null;
|
|
4
|
+
var _join = null;
|
|
5
|
+
var dynamicImport = (specifier) => import(specifier);
|
|
6
|
+
var NODE_FS_SPECIFIER = "node:fs";
|
|
7
|
+
var NODE_OS_SPECIFIER = "node:os";
|
|
8
|
+
var NODE_PATH_SPECIFIER = "node:path";
|
|
9
|
+
if (typeof process !== "undefined" && (process.versions?.node || process.versions?.bun)) {
|
|
10
|
+
dynamicImport(NODE_FS_SPECIFIER).then((m) => {
|
|
11
|
+
_existsSync = m.existsSync;
|
|
12
|
+
});
|
|
13
|
+
dynamicImport(NODE_OS_SPECIFIER).then((m) => {
|
|
14
|
+
_homedir = m.homedir;
|
|
15
|
+
});
|
|
16
|
+
dynamicImport(NODE_PATH_SPECIFIER).then((m) => {
|
|
17
|
+
_join = m.join;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
var cachedVertexAdcCredentialsExists = null;
|
|
21
|
+
function hasVertexAdcCredentials() {
|
|
22
|
+
if (cachedVertexAdcCredentialsExists === null) {
|
|
23
|
+
if (!_existsSync || !_homedir || !_join) {
|
|
24
|
+
const isNode = typeof process !== "undefined" && (process.versions?.node || process.versions?.bun);
|
|
25
|
+
if (!isNode) {
|
|
26
|
+
cachedVertexAdcCredentialsExists = false;
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
const gacPath = process.env.GOOGLE_APPLICATION_CREDENTIALS;
|
|
31
|
+
if (gacPath) {
|
|
32
|
+
cachedVertexAdcCredentialsExists = _existsSync(gacPath);
|
|
33
|
+
} else {
|
|
34
|
+
cachedVertexAdcCredentialsExists = _existsSync(_join(_homedir(), ".config", "gcloud", "application_default_credentials.json"));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return cachedVertexAdcCredentialsExists;
|
|
38
|
+
}
|
|
39
|
+
function getEnvApiKey(provider) {
|
|
40
|
+
if (provider === "github-copilot") {
|
|
41
|
+
return process.env.COPILOT_GITHUB_TOKEN || process.env.GH_TOKEN || process.env.GITHUB_TOKEN;
|
|
42
|
+
}
|
|
43
|
+
if (provider === "anthropic") {
|
|
44
|
+
return process.env.ANTHROPIC_OAUTH_TOKEN || process.env.ANTHROPIC_API_KEY;
|
|
45
|
+
}
|
|
46
|
+
if (provider === "google-vertex") {
|
|
47
|
+
if (process.env.GOOGLE_CLOUD_API_KEY) {
|
|
48
|
+
return process.env.GOOGLE_CLOUD_API_KEY;
|
|
49
|
+
}
|
|
50
|
+
const hasCredentials = hasVertexAdcCredentials();
|
|
51
|
+
const hasProject = !!(process.env.GOOGLE_CLOUD_PROJECT || process.env.GCLOUD_PROJECT);
|
|
52
|
+
const hasLocation = !!process.env.GOOGLE_CLOUD_LOCATION;
|
|
53
|
+
if (hasCredentials && hasProject && hasLocation) {
|
|
54
|
+
return "<authenticated>";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (provider === "amazon-bedrock") {
|
|
58
|
+
if (process.env.AWS_PROFILE || process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY || process.env.AWS_BEARER_TOKEN_BEDROCK || process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI || process.env.AWS_CONTAINER_CREDENTIALS_FULL_URI || process.env.AWS_WEB_IDENTITY_TOKEN_FILE) {
|
|
59
|
+
return "<authenticated>";
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const envMap = {
|
|
63
|
+
openai: "OPENAI_API_KEY",
|
|
64
|
+
"azure-openai-responses": "AZURE_OPENAI_API_KEY",
|
|
65
|
+
google: "GEMINI_API_KEY",
|
|
66
|
+
groq: "GROQ_API_KEY",
|
|
67
|
+
cerebras: "CEREBRAS_API_KEY",
|
|
68
|
+
xai: "XAI_API_KEY",
|
|
69
|
+
openrouter: "OPENROUTER_API_KEY",
|
|
70
|
+
"vercel-ai-gateway": "AI_GATEWAY_API_KEY",
|
|
71
|
+
zai: "ZAI_API_KEY",
|
|
72
|
+
mistral: "MISTRAL_API_KEY",
|
|
73
|
+
minimax: "MINIMAX_API_KEY",
|
|
74
|
+
"minimax-cn": "MINIMAX_CN_API_KEY",
|
|
75
|
+
huggingface: "HF_TOKEN",
|
|
76
|
+
opencode: "OPENCODE_API_KEY",
|
|
77
|
+
"opencode-go": "OPENCODE_API_KEY",
|
|
78
|
+
"kimi-coding": "KIMI_API_KEY"
|
|
79
|
+
};
|
|
80
|
+
const envVar = envMap[provider];
|
|
81
|
+
return envVar ? process.env[envVar] : void 0;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export {
|
|
85
|
+
getEnvApiKey
|
|
86
|
+
};
|