@agentpro/scroll 0.0.0-stage → 2.5.0
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/AGENTS.md +55 -0
- package/LICENSE +21 -0
- package/README.md +293 -2
- package/SPEC.md +1079 -0
- package/bin/scroll.js +8 -0
- package/docs/prompt-caching.md +128 -0
- package/index.js +33 -0
- package/lib/audit.js +358 -0
- package/lib/auth.js +849 -0
- package/lib/chat-sdk/admin-controller.js +289 -0
- package/lib/chat-sdk/debouncer.js +94 -0
- package/lib/chat-sdk/group-grants.js +150 -0
- package/lib/chat-sdk/index.js +222 -0
- package/lib/chat-sdk/long-task-runner.js +413 -0
- package/lib/chat-session.js +1788 -0
- package/lib/chat-telemetry.js +557 -0
- package/lib/codex-app-server.js +851 -0
- package/lib/digest.js +112 -0
- package/lib/effects.js +157 -0
- package/lib/eval.js +408 -0
- package/lib/grounding.js +356 -0
- package/lib/installers.js +176 -0
- package/lib/knowledge.js +335 -0
- package/lib/ledger.js +38 -0
- package/lib/loop.js +197 -0
- package/lib/mcp-client.js +926 -0
- package/lib/memory-cli.js +66 -0
- package/lib/memory-tracker.js +1472 -0
- package/lib/memory.js +1279 -0
- package/lib/people-cli.js +81 -0
- package/lib/people.js +534 -0
- package/lib/permissions.js +272 -0
- package/lib/providers.js +1572 -0
- package/lib/runtime.js +928 -0
- package/lib/sandbox.js +744 -0
- package/lib/scheduler.js +402 -0
- package/lib/scroll.js +1225 -0
- package/lib/skill-cli.js +120 -0
- package/lib/skill-eval.js +156 -0
- package/lib/skills.js +860 -0
- package/lib/style.js +126 -0
- package/lib/tool-retriever.js +489 -0
- package/lib/tools.js +1833 -0
- package/lib/user.js +711 -0
- package/lib/vault.js +151 -0
- package/package.json +88 -4
- package/templates/agent/IDENTITY.md +56 -0
- package/templates/agent/SOUL.md +23 -0
- package/templates/agent/TOOLS.md +25 -0
- package/templates/agent/evals/case-02-citation-discipline.md +29 -0
- package/templates/agent/evals/case-03-boundary.md +27 -0
- package/templates/agent/evals/case-example.md +43 -0
- package/templates/agent/hard-rules.md +11 -0
- package/templates/agent/memory/lessons.md +5 -0
- package/templates/agent/memory/log/.gitkeep +0 -0
- package/templates/external-safety-contract.md +5 -0
- package/templates/long-task-planner.md +17 -0
- package/templates/work/LOOP.md +37 -0
- package/templates/work/WORK.md +50 -0
- package/types/chat.d.ts +61 -0
- package/types/index.d.ts +112 -0
package/lib/providers.js
ADDED
|
@@ -0,0 +1,1572 @@
|
|
|
1
|
+
// SCROLL 2.5 — provider adapters (vendor-neutral 4-provider model layer for `scroll run` & `scroll chat`).
|
|
2
|
+
// Zero-dep: Node built-ins + global fetch (Node >= 20).
|
|
3
|
+
//
|
|
4
|
+
// A provider exposes: { name, model, ready, supportsTools, missing, generate(prompt, opts), close?() }
|
|
5
|
+
// generate(prompt, { model, maxTokens, cwd, system, cachePrefix, messages, tools, images, onDelta, signal, authStore, effort, threadId })
|
|
6
|
+
// -> { text, toolCalls?, thinkingBlocks?, rawParts?, usage:{ uncachedInputTokens, cachedInputTokens, outputTokens, providerTotalTokens }, durationMs, responseId, threadId? }
|
|
7
|
+
//
|
|
8
|
+
// The runtime is what drives the loop; providers only turn a prompt/messages into text+toolCalls+usage.
|
|
9
|
+
import fs from 'node:fs';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { spawnSync } from 'node:child_process';
|
|
12
|
+
import { createCodexAppServerClient, findCodexBinary } from './codex-app-server.js';
|
|
13
|
+
|
|
14
|
+
const approxTokens = (s) => Math.max(1, Math.ceil((s || '').length / 4));
|
|
15
|
+
const RETRYABLE_HTTP_STATUSES = new Set([429, 500, 502, 503, 504, 529]);
|
|
16
|
+
|
|
17
|
+
function sleepAbortable(ms, signal) {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
if (signal?.aborted) { reject(new Error('aborted')); return; }
|
|
20
|
+
const timer = setTimeout(() => {
|
|
21
|
+
if (signal) signal.removeEventListener('abort', onAbort);
|
|
22
|
+
resolve();
|
|
23
|
+
}, Math.max(0, ms));
|
|
24
|
+
const onAbort = () => {
|
|
25
|
+
clearTimeout(timer);
|
|
26
|
+
reject(new Error('aborted'));
|
|
27
|
+
};
|
|
28
|
+
if (signal) signal.addEventListener('abort', onAbort, { once: true });
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function fetchWithRetry(fetchImpl, url, opts = {}, { maxRetries = 2 } = {}) {
|
|
33
|
+
let attempt = 0;
|
|
34
|
+
while (true) {
|
|
35
|
+
const res = await fetchImpl(url, opts);
|
|
36
|
+
if (res.ok || !RETRYABLE_HTTP_STATUSES.has(res.status) || attempt >= maxRetries || opts?.signal?.aborted) {
|
|
37
|
+
return res;
|
|
38
|
+
}
|
|
39
|
+
const retryAfterMs = parseRetryAfterMs(res.headers);
|
|
40
|
+
const backoff = Math.min(10_000, retryAfterMs != null ? retryAfterMs : (250 * (2 ** attempt) + Math.floor(Math.random() * 150)));
|
|
41
|
+
try { await res.text(); } catch { /* drain body before retry */ }
|
|
42
|
+
attempt++;
|
|
43
|
+
await sleepAbortable(backoff, opts?.signal);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Proper SSE event-block consumer (`\n\n` boundaries, multi-line `data:` join, TextDecoder EOF flush,
|
|
48
|
+
// and trailing event without final newline).
|
|
49
|
+
async function consumeSseStream(body, onDataPayload) {
|
|
50
|
+
const reader = body.getReader();
|
|
51
|
+
const decoder = new TextDecoder();
|
|
52
|
+
let buf = '';
|
|
53
|
+
|
|
54
|
+
function dispatchBlock(block) {
|
|
55
|
+
if (!block) return;
|
|
56
|
+
const dataLines = [];
|
|
57
|
+
for (const rawLine of block.split('\n')) {
|
|
58
|
+
if (rawLine.startsWith('data:')) {
|
|
59
|
+
dataLines.push(rawLine.slice(5).replace(/^ /, ''));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (!dataLines.length) return;
|
|
63
|
+
const payload = dataLines.join('\n').trim();
|
|
64
|
+
if (!payload || payload === '[DONE]') return;
|
|
65
|
+
onDataPayload(payload);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
while (true) {
|
|
69
|
+
const { done, value } = await reader.read();
|
|
70
|
+
if (done) break;
|
|
71
|
+
buf += decoder.decode(value, { stream: true }).replace(/\r\n/g, '\n');
|
|
72
|
+
let sep;
|
|
73
|
+
while ((sep = buf.indexOf('\n\n')) >= 0) {
|
|
74
|
+
const block = buf.slice(0, sep);
|
|
75
|
+
buf = buf.slice(sep + 2);
|
|
76
|
+
dispatchBlock(block);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
buf += decoder.decode().replace(/\r\n/g, '\n');
|
|
80
|
+
if (buf.trim()) {
|
|
81
|
+
for (const block of buf.split('\n\n')) {
|
|
82
|
+
dispatchBlock(block);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function parseRetryAfterMs(headers) {
|
|
88
|
+
if (!headers || typeof headers.get !== 'function') return null;
|
|
89
|
+
const ra = headers.get('retry-after');
|
|
90
|
+
if (!ra) return null;
|
|
91
|
+
const asNum = Number(ra);
|
|
92
|
+
if (Number.isFinite(asNum) && asNum >= 0) return Math.round(asNum * 1000);
|
|
93
|
+
const ts = Date.parse(ra);
|
|
94
|
+
if (Number.isFinite(ts)) return Math.max(0, ts - Date.now());
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function buildHttpError(status, message, headers) {
|
|
99
|
+
const e = new Error(message || `http_error:${status}`);
|
|
100
|
+
e.status = status;
|
|
101
|
+
const retryMs = parseRetryAfterMs(headers);
|
|
102
|
+
if (retryMs != null) e.retryAfterMs = retryMs;
|
|
103
|
+
if ((status === 400 || status === 413) && /context.?length|maximum.?context|prompt.?is.?too.?long|too many tokens|exceeds?.{0,25}token|context_window/i.test(String(message || ''))) {
|
|
104
|
+
e.code = 'CONTEXT_LENGTH_EXCEEDED';
|
|
105
|
+
}
|
|
106
|
+
return e;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function leanFlags(toolPolicy) {
|
|
110
|
+
if (toolPolicy !== 'none') return [];
|
|
111
|
+
const raw = process.env.SCROLL_LEAN_CLI_FLAGS;
|
|
112
|
+
if (!raw) return [];
|
|
113
|
+
const trimmed = raw.trim();
|
|
114
|
+
if (trimmed.startsWith('[')) {
|
|
115
|
+
try {
|
|
116
|
+
const parsed = JSON.parse(trimmed);
|
|
117
|
+
if (Array.isArray(parsed)) return parsed.map(String);
|
|
118
|
+
} catch {
|
|
119
|
+
// Fall through to simple whitespace splitting below.
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return trimmed.split(/\s+/).filter(Boolean);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ── command helpers (pure PATH scan, zero shell expansion) ───────────────────
|
|
126
|
+
function commandExists(command) {
|
|
127
|
+
const cmd = String(command || '').trim();
|
|
128
|
+
if (!cmd) return false;
|
|
129
|
+
if (cmd.includes('/')) {
|
|
130
|
+
try { fs.accessSync(cmd, fs.constants.X_OK); return cmd; } catch { return false; }
|
|
131
|
+
}
|
|
132
|
+
const pathEnv = process.env.PATH || '';
|
|
133
|
+
for (const dir of pathEnv.split(path.delimiter)) {
|
|
134
|
+
if (!dir) continue;
|
|
135
|
+
const candidate = path.join(dir, cmd);
|
|
136
|
+
try {
|
|
137
|
+
const st = fs.statSync(candidate);
|
|
138
|
+
if (st.isFile()) {
|
|
139
|
+
fs.accessSync(candidate, fs.constants.X_OK);
|
|
140
|
+
return candidate;
|
|
141
|
+
}
|
|
142
|
+
} catch { /* continue */ }
|
|
143
|
+
}
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function runCommand(command, args, { cwd, input = '', timeoutMs = 300000 } = {}) {
|
|
148
|
+
const started = Date.now();
|
|
149
|
+
const child = spawnSync(command, args, {
|
|
150
|
+
cwd, input, encoding: 'utf8', timeout: timeoutMs, maxBuffer: 64 * 1024 * 1024,
|
|
151
|
+
});
|
|
152
|
+
const durationMs = Date.now() - started;
|
|
153
|
+
if (child.error) throw child.error;
|
|
154
|
+
if (child.status !== 0) {
|
|
155
|
+
throw new Error([`${command} exited ${child.status}`, child.stderr, child.stdout].filter(Boolean).join('\n').slice(0, 4000));
|
|
156
|
+
}
|
|
157
|
+
return { stdout: child.stdout || '', durationMs };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function parseJsonLines(stdout) {
|
|
161
|
+
return stdout.split('\n').map((l) => l.trim()).filter((l) => l.startsWith('{') && l.endsWith('}'))
|
|
162
|
+
.map((l) => { try { return JSON.parse(l); } catch { return null; } }).filter(Boolean);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function formatMessagesForPrompt(prompt, messages, system) {
|
|
166
|
+
if (Array.isArray(messages) && messages.length) {
|
|
167
|
+
const parts = [];
|
|
168
|
+
if (system) parts.push(`[System]\n${system}`);
|
|
169
|
+
for (const m of messages) {
|
|
170
|
+
const role = m.role === 'assistant' ? 'Assistant' : (m.role === 'tool' ? `Tool(${m.name || 'tool'})` : 'User');
|
|
171
|
+
parts.push(`[${role}]\n${typeof m.content === 'string' ? m.content : JSON.stringify(m.content)}`);
|
|
172
|
+
}
|
|
173
|
+
return parts.join('\n\n');
|
|
174
|
+
}
|
|
175
|
+
return system && !String(prompt || '').startsWith(system) ? `${system}\n\n${prompt || ''}` : String(prompt || '');
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function toOpenAiTools(tools) {
|
|
179
|
+
if (!Array.isArray(tools) || !tools.length) return undefined;
|
|
180
|
+
return tools.map((t) => ({
|
|
181
|
+
type: 'function',
|
|
182
|
+
function: {
|
|
183
|
+
name: t.name,
|
|
184
|
+
description: t.description || '',
|
|
185
|
+
parameters: t.parameters || { type: 'object', properties: {} },
|
|
186
|
+
},
|
|
187
|
+
}));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function readImageBase64(img) {
|
|
191
|
+
if (!img) return null;
|
|
192
|
+
if (img.base64) {
|
|
193
|
+
return {
|
|
194
|
+
mimeType: img.mimeType || 'image/jpeg',
|
|
195
|
+
data: String(img.base64).replace(/^data:[^;]+;base64,/i, '').trim(),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
if (img.path && fs.existsSync(img.path)) {
|
|
199
|
+
const mimeType = img.mimeType || (img.path.endsWith('.png') ? 'image/png' : (img.path.endsWith('.webp') ? 'image/webp' : 'image/jpeg'));
|
|
200
|
+
return { mimeType, data: fs.readFileSync(img.path).toString('base64') };
|
|
201
|
+
}
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function attachImagesToOpenAiContent(text, images) {
|
|
206
|
+
if (!Array.isArray(images) || !images.length) return text;
|
|
207
|
+
const parts = [{ type: 'text', text: String(text || '') }];
|
|
208
|
+
for (const img of images) {
|
|
209
|
+
if (!img) continue;
|
|
210
|
+
if (img.url) {
|
|
211
|
+
parts.push({ type: 'image_url', image_url: { url: img.url } });
|
|
212
|
+
} else {
|
|
213
|
+
const b64 = readImageBase64(img);
|
|
214
|
+
if (b64) {
|
|
215
|
+
parts.push({ type: 'image_url', image_url: { url: `data:${b64.mimeType};base64,${b64.data}` } });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return parts;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// ── MOCK (deterministic — proves runtime mechanics without spending tokens) ────
|
|
223
|
+
function makeMockProvider({ scriptedTurns = null } = {}) {
|
|
224
|
+
const seenPrefixes = new Set();
|
|
225
|
+
let turnIdx = 0;
|
|
226
|
+
return {
|
|
227
|
+
name: 'mock', model: 'mock-1', ready: true, supportsTools: true, missing: null,
|
|
228
|
+
async generate(prompt, { cachePrefix = '', system = '', messages = null, tools = null, onDelta = null, scriptedTurns: callScript = null } = {}) {
|
|
229
|
+
const t0 = Date.now();
|
|
230
|
+
const effectivePrompt = Array.isArray(messages) && messages.length
|
|
231
|
+
? formatMessagesForPrompt(prompt, messages, '')
|
|
232
|
+
: String(prompt ?? '');
|
|
233
|
+
const prefix = cachePrefix || system || '';
|
|
234
|
+
const prefixTok = approxTokens(prefix);
|
|
235
|
+
const bodyTok = approxTokens(effectivePrompt) - (cachePrefix && effectivePrompt.includes(cachePrefix) ? prefixTok : 0);
|
|
236
|
+
const cached = prefix && seenPrefixes.has(prefix);
|
|
237
|
+
if (prefix) seenPrefixes.add(prefix);
|
|
238
|
+
|
|
239
|
+
const script = callScript || scriptedTurns;
|
|
240
|
+
let text;
|
|
241
|
+
let toolCalls = [];
|
|
242
|
+
if (Array.isArray(script) && turnIdx < script.length) {
|
|
243
|
+
const entry = script[turnIdx++];
|
|
244
|
+
if (typeof entry === 'function') {
|
|
245
|
+
const out = await entry({ prompt: effectivePrompt, system, messages, tools });
|
|
246
|
+
text = out?.text ?? '';
|
|
247
|
+
toolCalls = Array.isArray(out?.toolCalls) ? out.toolCalls : [];
|
|
248
|
+
} else if (typeof entry === 'string') {
|
|
249
|
+
text = entry;
|
|
250
|
+
} else {
|
|
251
|
+
text = entry?.text ?? '';
|
|
252
|
+
toolCalls = Array.isArray(entry?.toolCalls) ? entry.toolCalls : [];
|
|
253
|
+
}
|
|
254
|
+
} else {
|
|
255
|
+
text = mockAnswer(effectivePrompt);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (typeof onDelta === 'function' && text) {
|
|
259
|
+
onDelta(text);
|
|
260
|
+
}
|
|
261
|
+
return {
|
|
262
|
+
text,
|
|
263
|
+
toolCalls,
|
|
264
|
+
usage: {
|
|
265
|
+
uncachedInputTokens: cached ? Math.max(1, bodyTok) : approxTokens(effectivePrompt),
|
|
266
|
+
cachedInputTokens: cached ? prefixTok : 0,
|
|
267
|
+
outputTokens: approxTokens(text),
|
|
268
|
+
providerTotalTokens: approxTokens(effectivePrompt) + approxTokens(text),
|
|
269
|
+
},
|
|
270
|
+
durationMs: Date.now() - t0 + 5,
|
|
271
|
+
responseId: 'mock-' + Math.random().toString(36).slice(2, 10),
|
|
272
|
+
};
|
|
273
|
+
},
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function mockAnswer(prompt) {
|
|
278
|
+
if (prompt.includes('SCROLL_JUDGE_JSON')) return '{"scores": {"faithfulness": 5, "clarity": 4, "honesty": 4.5}, "average": 4.5}';
|
|
279
|
+
const sources = [...new Set((prompt.match(/\bS\d\b/g) || []))];
|
|
280
|
+
const c = (i) => (sources[i] ? `[${sources[i]}]` : '');
|
|
281
|
+
const objMatch = prompt.match(/objective:\s*(.+)/i);
|
|
282
|
+
const objective = objMatch ? objMatch[1].trim().slice(0, 120) : 'the requested task';
|
|
283
|
+
return [
|
|
284
|
+
'## Verdict',
|
|
285
|
+
`Conditional go — treat as a measured pilot, not a finished product ${c(0)}.`,
|
|
286
|
+
'',
|
|
287
|
+
'## Evidence',
|
|
288
|
+
`- The core problem is drift/fragmentation across runtimes ${c(0)}.`,
|
|
289
|
+
`- The proposed approach is a single source of truth that renders to each runtime ${c(1)}.`,
|
|
290
|
+
'',
|
|
291
|
+
'## Assumptions',
|
|
292
|
+
`- Production-grade runtime claims stay unmeasured until a real runner exists ${c(2)}.`,
|
|
293
|
+
`- Objective addressed: ${objective}.`,
|
|
294
|
+
'',
|
|
295
|
+
'## Next actions',
|
|
296
|
+
'1. Build the minimal runtime and measure on a real multi-step task.',
|
|
297
|
+
'2. Re-run the eval with >=3 repeats through an API provider.',
|
|
298
|
+
].join('\n');
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// ── Codex App-Server (v2.0 §46 persistent JSON-RPC stdio client) ──────────────
|
|
302
|
+
function makeCodexAppServerProvider({
|
|
303
|
+
model = null,
|
|
304
|
+
command = null,
|
|
305
|
+
args = null,
|
|
306
|
+
searchPaths = [],
|
|
307
|
+
cwd = process.cwd(),
|
|
308
|
+
env = process.env,
|
|
309
|
+
timeoutMs = 120_000,
|
|
310
|
+
effort = null,
|
|
311
|
+
client = null,
|
|
312
|
+
} = {}) {
|
|
313
|
+
const rpc = client || createCodexAppServerClient({
|
|
314
|
+
command,
|
|
315
|
+
args,
|
|
316
|
+
searchPaths,
|
|
317
|
+
cwd,
|
|
318
|
+
env,
|
|
319
|
+
timeoutMs,
|
|
320
|
+
defaultModel: model || process.env.CODEX_MODEL || null,
|
|
321
|
+
});
|
|
322
|
+
return {
|
|
323
|
+
name: 'codex-app-server',
|
|
324
|
+
model: model || process.env.CODEX_MODEL || 'auto',
|
|
325
|
+
supportsTools: true,
|
|
326
|
+
get ready() { return client ? true : Boolean(findCodexBinary({ command, searchPaths, env })); },
|
|
327
|
+
missing: 'codex binary not found (set CODEX_APP_SERVER_BIN or install codex CLI)',
|
|
328
|
+
client: rpc,
|
|
329
|
+
async generate(prompt, {
|
|
330
|
+
model: m,
|
|
331
|
+
system,
|
|
332
|
+
messages,
|
|
333
|
+
tools,
|
|
334
|
+
onToolCall,
|
|
335
|
+
images,
|
|
336
|
+
effort: e,
|
|
337
|
+
threadId,
|
|
338
|
+
ephemeral = true,
|
|
339
|
+
onDelta,
|
|
340
|
+
signal,
|
|
341
|
+
timeoutMs: tMs,
|
|
342
|
+
} = {}) {
|
|
343
|
+
const res = await rpc.generate(prompt, {
|
|
344
|
+
model: m || (this.model !== 'auto' ? this.model : null),
|
|
345
|
+
system,
|
|
346
|
+
messages,
|
|
347
|
+
tools,
|
|
348
|
+
onToolCall,
|
|
349
|
+
images,
|
|
350
|
+
effort: e || effort,
|
|
351
|
+
threadId,
|
|
352
|
+
ephemeral,
|
|
353
|
+
onDelta,
|
|
354
|
+
signal,
|
|
355
|
+
timeoutMs: tMs || timeoutMs,
|
|
356
|
+
});
|
|
357
|
+
return {
|
|
358
|
+
text: res.text,
|
|
359
|
+
toolCalls: [],
|
|
360
|
+
usage: res.usage,
|
|
361
|
+
durationMs: res.durationMs,
|
|
362
|
+
responseId: res.responseId,
|
|
363
|
+
threadId: res.threadId,
|
|
364
|
+
model: res.model,
|
|
365
|
+
};
|
|
366
|
+
},
|
|
367
|
+
async close() {
|
|
368
|
+
await rpc.close();
|
|
369
|
+
},
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// ── Codex CLI ─────────────────────────────────────────────────────────────────
|
|
374
|
+
export function codexArgs({ model, toolPolicy, sandbox, effort } = {}) {
|
|
375
|
+
const args = ['exec', '--json', '--skip-git-repo-check', '--color', 'never', '--ephemeral'];
|
|
376
|
+
args.push('--sandbox', sandbox === 'write' ? 'workspace-write' : 'read-only');
|
|
377
|
+
if (effort) args.push('-c', `model_reasoning_effort="${effort}"`);
|
|
378
|
+
if (model) args.push('--model', model);
|
|
379
|
+
args.push(...leanFlags(toolPolicy));
|
|
380
|
+
args.push('-');
|
|
381
|
+
return args;
|
|
382
|
+
}
|
|
383
|
+
function makeCodexProvider({ model } = {}) {
|
|
384
|
+
return {
|
|
385
|
+
name: 'codex-cli', model: model || process.env.CODEX_MODEL || 'codex-default',
|
|
386
|
+
supportsTools: false,
|
|
387
|
+
ready: Boolean(commandExists('codex')), missing: 'codex CLI not on PATH',
|
|
388
|
+
async generate(prompt, { cwd, model: m, toolPolicy, sandbox, effort, timeoutMs, system, messages } = {}) {
|
|
389
|
+
const input = Array.isArray(messages) && messages.length ? formatMessagesForPrompt(prompt, messages, system) : prompt;
|
|
390
|
+
const args = codexArgs({ model: (process.env.CODEX_MODEL || m) ? (m || this.model) : null, toolPolicy, sandbox, effort });
|
|
391
|
+
const { stdout, durationMs } = runCommand('codex', args, { cwd, input, timeoutMs: timeoutMs || (sandbox === 'write' ? 900000 : 300000) });
|
|
392
|
+
const events = parseJsonLines(stdout);
|
|
393
|
+
const msg = events.filter((e) => e.type === 'item.completed' && e.item?.type === 'agent_message').map((e) => e.item.text || '');
|
|
394
|
+
const done = [...events].reverse().find((e) => e.type === 'turn.completed');
|
|
395
|
+
const u = done?.usage || {};
|
|
396
|
+
const inputTok = Number(u.input_tokens || 0), cached = Number(u.cached_input_tokens || 0);
|
|
397
|
+
return {
|
|
398
|
+
text: msg.at(-1) || '',
|
|
399
|
+
toolCalls: [],
|
|
400
|
+
usage: { uncachedInputTokens: Math.max(0, inputTok - cached), cachedInputTokens: cached, outputTokens: Number(u.output_tokens || 0), providerTotalTokens: inputTok + Number(u.output_tokens || 0) },
|
|
401
|
+
durationMs, responseId: events.find((e) => e.type === 'thread.started')?.thread_id || null,
|
|
402
|
+
};
|
|
403
|
+
},
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// ── Claude CLI ──────────────────────────────────────────────────────────────
|
|
408
|
+
function makeClaudeProvider({ model } = {}) {
|
|
409
|
+
const bin = process.env.CLAUDE_CLI || 'claude';
|
|
410
|
+
return {
|
|
411
|
+
name: 'claude-cli', model: model || process.env.CLAUDE_MODEL || 'claude-default',
|
|
412
|
+
supportsTools: false,
|
|
413
|
+
ready: Boolean(commandExists(bin)), missing: `Claude CLI not found (${bin})`,
|
|
414
|
+
async generate(prompt, { cwd, model: m, toolPolicy, system, messages } = {}) {
|
|
415
|
+
const fullPrompt = Array.isArray(messages) && messages.length ? formatMessagesForPrompt(prompt, messages, system) : prompt;
|
|
416
|
+
const args = ['-p', '--output-format', 'json', '--permission-mode', 'dontAsk'];
|
|
417
|
+
if (process.env.CLAUDE_MODEL || m) args.push('--model', m || this.model);
|
|
418
|
+
args.push(...leanFlags(toolPolicy));
|
|
419
|
+
args.push(fullPrompt);
|
|
420
|
+
const { stdout, durationMs } = runCommand(bin, args, { cwd });
|
|
421
|
+
const j = JSON.parse(stdout);
|
|
422
|
+
const u = j.usage || {};
|
|
423
|
+
const input = Number(u.input_tokens || 0), cc = Number(u.cache_creation_input_tokens || 0), cr = Number(u.cache_read_input_tokens || 0);
|
|
424
|
+
return {
|
|
425
|
+
text: j.result || '',
|
|
426
|
+
toolCalls: [],
|
|
427
|
+
usage: { uncachedInputTokens: input + cc, cachedInputTokens: cr, outputTokens: Number(u.output_tokens || 0), providerTotalTokens: input + cc + cr + Number(u.output_tokens || 0) },
|
|
428
|
+
durationMs: Number(j.duration_ms || durationMs), responseId: j.session_id || null,
|
|
429
|
+
};
|
|
430
|
+
},
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// ── API HTTP + SSE helpers ────────────────────────────────────────────────────
|
|
435
|
+
async function fetchJson(url, opts, fetchImpl = globalThis.fetch) {
|
|
436
|
+
const started = Date.now();
|
|
437
|
+
const res = await fetchWithRetry(fetchImpl, url, opts);
|
|
438
|
+
const text = await res.text();
|
|
439
|
+
let json; try { json = JSON.parse(text); } catch { json = { raw: text }; }
|
|
440
|
+
if (!res.ok) {
|
|
441
|
+
throw buildHttpError(res.status, json?.error?.message || text.slice(0, 300), res.headers);
|
|
442
|
+
}
|
|
443
|
+
return { json, durationMs: Date.now() - started };
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
async function fetchOpenAiSse(url, opts, { onDelta, fetchImpl = globalThis.fetch } = {}) {
|
|
447
|
+
const started = Date.now();
|
|
448
|
+
const res = await fetchWithRetry(fetchImpl, url, opts);
|
|
449
|
+
if (!res.ok) {
|
|
450
|
+
const text = await res.text();
|
|
451
|
+
let json; try { json = JSON.parse(text); } catch { json = { raw: text }; }
|
|
452
|
+
throw buildHttpError(res.status, json?.error?.message || text.slice(0, 300), res.headers);
|
|
453
|
+
}
|
|
454
|
+
const ct = res.headers && typeof res.headers.get === 'function' ? (res.headers.get('content-type') || '') : '';
|
|
455
|
+
if (!ct.includes('text/event-stream') || !res.body) {
|
|
456
|
+
const json = await res.json();
|
|
457
|
+
return { json, durationMs: Date.now() - started, streamed: false };
|
|
458
|
+
}
|
|
459
|
+
let text = '';
|
|
460
|
+
let reasoningContent = '';
|
|
461
|
+
let responseId = null;
|
|
462
|
+
let usage = null;
|
|
463
|
+
let stopReason = null;
|
|
464
|
+
const toolAcc = new Map();
|
|
465
|
+
await consumeSseStream(res.body, (payload) => {
|
|
466
|
+
let chunk;
|
|
467
|
+
try { chunk = JSON.parse(payload); } catch { return; }
|
|
468
|
+
if (chunk.id) responseId = chunk.id;
|
|
469
|
+
if (chunk.usage) usage = chunk.usage;
|
|
470
|
+
const choice = chunk.choices?.[0];
|
|
471
|
+
if (choice?.finish_reason) stopReason = choice.finish_reason;
|
|
472
|
+
const delta = choice?.delta;
|
|
473
|
+
if (!delta) return;
|
|
474
|
+
// DeepSeek-R1/V3.2/V4 & reasoning models interleaved thinking stream
|
|
475
|
+
if (typeof delta.reasoning_content === 'string' && delta.reasoning_content) {
|
|
476
|
+
reasoningContent += delta.reasoning_content;
|
|
477
|
+
}
|
|
478
|
+
if (typeof delta.content === 'string' && delta.content) {
|
|
479
|
+
text += delta.content;
|
|
480
|
+
if (typeof onDelta === 'function') onDelta(delta.content);
|
|
481
|
+
}
|
|
482
|
+
if (Array.isArray(delta.tool_calls)) {
|
|
483
|
+
for (const tc of delta.tool_calls) {
|
|
484
|
+
const k = tc.index ?? 0;
|
|
485
|
+
const prev = toolAcc.get(k) || { id: tc.id || `call_${k}`, name: '', argumentsRaw: '' };
|
|
486
|
+
if (tc.id) prev.id = tc.id;
|
|
487
|
+
if (tc.function?.name) prev.name += tc.function.name;
|
|
488
|
+
if (tc.function?.arguments) prev.argumentsRaw += tc.function.arguments;
|
|
489
|
+
toolAcc.set(k, prev);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
const truncated = stopReason === 'length' || stopReason === 'max_tokens';
|
|
494
|
+
const toolCalls = [...toolAcc.values()].map((t) => {
|
|
495
|
+
let args = {};
|
|
496
|
+
let malformed = false;
|
|
497
|
+
try { args = JSON.parse(t.argumentsRaw || '{}'); } catch { args = { raw: t.argumentsRaw }; malformed = true; }
|
|
498
|
+
// Even if `argumentsRaw` is empty and parses as `{}`, a `length`/`max_tokens` cut NEVER safely completed arguments!
|
|
499
|
+
if (truncated) malformed = true;
|
|
500
|
+
return { id: t.id, name: t.name, arguments: args, ...(malformed ? { malformed: true, truncated } : {}) };
|
|
501
|
+
});
|
|
502
|
+
return {
|
|
503
|
+
streamed: true,
|
|
504
|
+
text,
|
|
505
|
+
...(reasoningContent ? { reasoningContent } : {}),
|
|
506
|
+
toolCalls,
|
|
507
|
+
usage,
|
|
508
|
+
stopReason,
|
|
509
|
+
responseId,
|
|
510
|
+
durationMs: Date.now() - started,
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function buildOpenAiChatMessages(prompt, { system, messages, images } = {}) {
|
|
515
|
+
const out = [];
|
|
516
|
+
if (system) out.push({ role: 'system', content: system });
|
|
517
|
+
if (Array.isArray(messages) && messages.length) {
|
|
518
|
+
for (let i = 0; i < messages.length; i++) {
|
|
519
|
+
const m = messages[i];
|
|
520
|
+
const isLastUser = i === messages.length - 1 && m.role === 'user';
|
|
521
|
+
if (m.role === 'tool') {
|
|
522
|
+
out.push({ role: 'tool', tool_call_id: m.toolCallId || m.id || 'call_0', name: m.name, content: typeof m.content === 'string' ? m.content : JSON.stringify(m.content) });
|
|
523
|
+
} else if (m.role === 'assistant' && Array.isArray(m.toolCalls) && m.toolCalls.length) {
|
|
524
|
+
out.push({
|
|
525
|
+
role: 'assistant',
|
|
526
|
+
content: m.content || null,
|
|
527
|
+
...(typeof m.reasoningContent === 'string' && m.reasoningContent ? { reasoning_content: m.reasoningContent } : {}),
|
|
528
|
+
tool_calls: m.toolCalls.map((tc) => ({
|
|
529
|
+
id: tc.id,
|
|
530
|
+
type: 'function',
|
|
531
|
+
function: { name: tc.name, arguments: typeof tc.arguments === 'string' ? tc.arguments : JSON.stringify(tc.arguments || {}) },
|
|
532
|
+
})),
|
|
533
|
+
});
|
|
534
|
+
} else {
|
|
535
|
+
const content = isLastUser && Array.isArray(images) && images.length
|
|
536
|
+
? attachImagesToOpenAiContent(m.content, images)
|
|
537
|
+
: m.content;
|
|
538
|
+
out.push({
|
|
539
|
+
role: m.role || 'user',
|
|
540
|
+
content,
|
|
541
|
+
...(m.role === 'assistant' && typeof m.reasoningContent === 'string' && m.reasoningContent ? { reasoning_content: m.reasoningContent } : {}),
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
} else {
|
|
546
|
+
out.push({ role: 'user', content: attachImagesToOpenAiContent(prompt, images) });
|
|
547
|
+
}
|
|
548
|
+
return out;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
function parseOpenAiChatResponse(json, durationMs, onDelta) {
|
|
552
|
+
const choice = json.choices?.[0] || {};
|
|
553
|
+
const msg = choice.message || {};
|
|
554
|
+
const stopReason = choice.finish_reason || null;
|
|
555
|
+
const truncated = stopReason === 'length' || stopReason === 'max_tokens';
|
|
556
|
+
const text = typeof msg.content === 'string' ? msg.content.trim() : '';
|
|
557
|
+
const reasoningContent = typeof msg.reasoning_content === 'string' && msg.reasoning_content ? msg.reasoning_content : null;
|
|
558
|
+
if (typeof onDelta === 'function' && text) onDelta(text);
|
|
559
|
+
const toolCalls = (msg.tool_calls || []).map((tc, idx) => {
|
|
560
|
+
let args = {};
|
|
561
|
+
let malformed = false;
|
|
562
|
+
try { args = JSON.parse(tc.function?.arguments || '{}'); } catch { args = { raw: tc.function?.arguments }; malformed = true; }
|
|
563
|
+
// Parity with SSE: if finish_reason is length/max_tokens, mark tool call malformed+truncated even if args parsed as {}
|
|
564
|
+
if (truncated) malformed = true;
|
|
565
|
+
return {
|
|
566
|
+
id: tc.id || `call_${idx}`,
|
|
567
|
+
name: tc.function?.name || '',
|
|
568
|
+
arguments: args,
|
|
569
|
+
...(malformed ? { malformed: true, truncated } : {}),
|
|
570
|
+
};
|
|
571
|
+
});
|
|
572
|
+
const u = json.usage || {};
|
|
573
|
+
const cached = Number(u.prompt_tokens_details?.cached_tokens || 0);
|
|
574
|
+
const input = Number(u.prompt_tokens || 0);
|
|
575
|
+
const output = Number(u.completion_tokens || 0);
|
|
576
|
+
return {
|
|
577
|
+
text,
|
|
578
|
+
...(reasoningContent ? { reasoningContent } : {}),
|
|
579
|
+
toolCalls,
|
|
580
|
+
stopReason,
|
|
581
|
+
usage: {
|
|
582
|
+
uncachedInputTokens: Math.max(0, input - cached),
|
|
583
|
+
cachedInputTokens: cached,
|
|
584
|
+
outputTokens: output,
|
|
585
|
+
providerTotalTokens: Number(u.total_tokens || (input + output)),
|
|
586
|
+
},
|
|
587
|
+
durationMs,
|
|
588
|
+
responseId: json.id || null,
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function usesMaxCompletionTokens(modelId) {
|
|
593
|
+
const base = String(modelId || '').trim().split('/').pop() || '';
|
|
594
|
+
return /^(?:o[1-9](?:$|[-._])|gpt-5(?:$|[-._])|codex-)/i.test(base);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// ── OpenAI-compatible HTTP provider ───────────────────────────────────────────
|
|
598
|
+
function makeOpenAICompatibleProvider({
|
|
599
|
+
model = null,
|
|
600
|
+
baseUrl = null,
|
|
601
|
+
apiKey = null,
|
|
602
|
+
authStore = null,
|
|
603
|
+
providerId = 'openai',
|
|
604
|
+
headers = {},
|
|
605
|
+
cacheKey: defaultCacheKey = null,
|
|
606
|
+
cacheTtl: defaultCacheTtl = null,
|
|
607
|
+
includeStreamUsage = true,
|
|
608
|
+
fetchImpl = globalThis.fetch,
|
|
609
|
+
} = {}) {
|
|
610
|
+
const resolvedBase = (baseUrl || process.env.OPENAI_BASE_URL || 'https://api.openai.com/v1').replace(/\/+$/, '');
|
|
611
|
+
const resolvedKey = apiKey || process.env.OPENAI_API_KEY || null;
|
|
612
|
+
return {
|
|
613
|
+
name: 'openai-compatible',
|
|
614
|
+
model: model || process.env.OPENAI_MODEL || 'gpt-4o-mini',
|
|
615
|
+
supportsTools: true,
|
|
616
|
+
get ready() {
|
|
617
|
+
if (resolvedKey) return true;
|
|
618
|
+
if (authStore && authStore.listProfiles({ provider: providerId }).length > 0) return true;
|
|
619
|
+
return Boolean(baseUrl);
|
|
620
|
+
},
|
|
621
|
+
missing: 'apiKey, authStore profile, or baseUrl not configured',
|
|
622
|
+
async generate(prompt, {
|
|
623
|
+
maxTokens = 1200,
|
|
624
|
+
model: m,
|
|
625
|
+
system,
|
|
626
|
+
messages,
|
|
627
|
+
tools,
|
|
628
|
+
images,
|
|
629
|
+
onDelta,
|
|
630
|
+
signal,
|
|
631
|
+
authStore: callAuthStore,
|
|
632
|
+
cacheKey = null,
|
|
633
|
+
promptCacheKey = null,
|
|
634
|
+
cacheTtl = null,
|
|
635
|
+
stream = Boolean(onDelta),
|
|
636
|
+
} = {}) {
|
|
637
|
+
const store = callAuthStore || authStore;
|
|
638
|
+
const targetModel = m || this.model;
|
|
639
|
+
const bodyObj = {
|
|
640
|
+
model: targetModel,
|
|
641
|
+
messages: buildOpenAiChatMessages(prompt, { system, messages, images }),
|
|
642
|
+
...(usesMaxCompletionTokens(targetModel) ? { max_completion_tokens: maxTokens } : { max_tokens: maxTokens }),
|
|
643
|
+
};
|
|
644
|
+
const oaTools = toOpenAiTools(tools);
|
|
645
|
+
if (oaTools) bodyObj.tools = oaTools;
|
|
646
|
+
if (stream) {
|
|
647
|
+
bodyObj.stream = true;
|
|
648
|
+
if (includeStreamUsage !== false) {
|
|
649
|
+
bodyObj.stream_options = { include_usage: true };
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
const effectiveCacheKey = promptCacheKey || cacheKey || defaultCacheKey || null;
|
|
654
|
+
const effectiveTtl = cacheTtl || defaultCacheTtl || null;
|
|
655
|
+
|
|
656
|
+
const execWithCred = async (cred) => {
|
|
657
|
+
const targetBase = (cred?.baseUrl || resolvedBase).replace(/\/+$/, '');
|
|
658
|
+
const url = `${targetBase}/chat/completions`;
|
|
659
|
+
const payload = { ...bodyObj };
|
|
660
|
+
if (promptCacheKey || (effectiveCacheKey && targetBase.includes('api.openai.com'))) {
|
|
661
|
+
payload.prompt_cache_key = String(promptCacheKey || effectiveCacheKey).slice(0, 64);
|
|
662
|
+
if (effectiveTtl === '24h') payload.prompt_cache_retention = '24h';
|
|
663
|
+
}
|
|
664
|
+
const reqHeaders = {
|
|
665
|
+
'Content-Type': 'application/json',
|
|
666
|
+
...headers,
|
|
667
|
+
...(cred?.headers || {}),
|
|
668
|
+
};
|
|
669
|
+
if (!reqHeaders.Authorization && (cred?.token || resolvedKey)) {
|
|
670
|
+
reqHeaders.Authorization = `Bearer ${cred?.token || resolvedKey}`;
|
|
671
|
+
}
|
|
672
|
+
const fetchOpts = { method: 'POST', headers: reqHeaders, body: JSON.stringify(payload), signal };
|
|
673
|
+
if (stream) {
|
|
674
|
+
const r = await fetchOpenAiSse(url, fetchOpts, { onDelta, fetchImpl });
|
|
675
|
+
if (r.streamed) {
|
|
676
|
+
const u = r.usage || {};
|
|
677
|
+
const input = Number(u.prompt_tokens || 0);
|
|
678
|
+
const cached = Number(u.prompt_tokens_details?.cached_tokens || 0);
|
|
679
|
+
const output = Number(u.completion_tokens || approxTokens(r.text));
|
|
680
|
+
return {
|
|
681
|
+
text: r.text.trim(),
|
|
682
|
+
...(r.reasoningContent ? { reasoningContent: r.reasoningContent } : {}),
|
|
683
|
+
toolCalls: r.toolCalls,
|
|
684
|
+
stopReason: r.stopReason || null,
|
|
685
|
+
usage: { uncachedInputTokens: Math.max(0, input - cached), cachedInputTokens: cached, outputTokens: output, providerTotalTokens: Number(u.total_tokens || (input + output)) },
|
|
686
|
+
durationMs: r.durationMs,
|
|
687
|
+
responseId: r.responseId,
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
return parseOpenAiChatResponse(r.json, r.durationMs, onDelta);
|
|
691
|
+
}
|
|
692
|
+
const { json, durationMs } = await fetchJson(url, fetchOpts, fetchImpl);
|
|
693
|
+
return parseOpenAiChatResponse(json, durationMs, onDelta);
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
if (store && store.listProfiles({ provider: providerId }).length > 0) {
|
|
697
|
+
const { result, profileId } = await store.withFailover(providerId, execWithCred);
|
|
698
|
+
return { ...result, authProfileId: profileId };
|
|
699
|
+
}
|
|
700
|
+
return execWithCred(null);
|
|
701
|
+
},
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
function makeOpenAIProvider({ model, authStore = null, baseUrl = null, apiKey = null, cacheKey: defaultCacheKey = null, cacheTtl: defaultCacheTtl = null, fetchImpl = globalThis.fetch } = {}) {
|
|
706
|
+
const key = apiKey || process.env.OPENAI_API_KEY;
|
|
707
|
+
return {
|
|
708
|
+
name: 'openai', model: model || process.env.OPENAI_MODEL || 'gpt-4o-mini',
|
|
709
|
+
supportsTools: true,
|
|
710
|
+
get ready() { return Boolean(key || (authStore && authStore.listProfiles({ provider: 'openai' }).length > 0)); },
|
|
711
|
+
missing: 'OPENAI_API_KEY not set',
|
|
712
|
+
async generate(prompt, opts = {}) {
|
|
713
|
+
const { maxTokens = 800, model: m, messages, tools, images, system, onDelta, signal, authStore: callStore, cacheKey = null, promptCacheKey = null, cacheTtl = null } = opts;
|
|
714
|
+
const store = callStore || authStore;
|
|
715
|
+
if (messages || tools || images || system || onDelta || store || baseUrl) {
|
|
716
|
+
const compat = makeOpenAICompatibleProvider({
|
|
717
|
+
model: m || this.model,
|
|
718
|
+
baseUrl: baseUrl || 'https://api.openai.com/v1',
|
|
719
|
+
apiKey: key,
|
|
720
|
+
authStore: store,
|
|
721
|
+
providerId: 'openai',
|
|
722
|
+
cacheKey: defaultCacheKey,
|
|
723
|
+
cacheTtl: defaultCacheTtl,
|
|
724
|
+
fetchImpl,
|
|
725
|
+
});
|
|
726
|
+
return compat.generate(prompt, { ...opts, model: m || this.model, maxTokens });
|
|
727
|
+
}
|
|
728
|
+
const reqBody = { model: m || this.model, input: prompt, max_output_tokens: maxTokens, store: false };
|
|
729
|
+
const effectiveCacheKey = promptCacheKey || cacheKey || defaultCacheKey || null;
|
|
730
|
+
const effectiveTtl = cacheTtl || defaultCacheTtl || null;
|
|
731
|
+
if (effectiveCacheKey) reqBody.prompt_cache_key = String(effectiveCacheKey).slice(0, 64);
|
|
732
|
+
if (effectiveTtl === '24h') reqBody.prompt_cache_retention = '24h';
|
|
733
|
+
const { json, durationMs } = await fetchJson('https://api.openai.com/v1/responses', {
|
|
734
|
+
method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${key}` },
|
|
735
|
+
body: JSON.stringify(reqBody),
|
|
736
|
+
signal,
|
|
737
|
+
}, fetchImpl);
|
|
738
|
+
const u = json.usage || {}; const cached = Number(u.input_tokens_details?.cached_tokens || 0); const input = Number(u.input_tokens || 0);
|
|
739
|
+
const text = json.output_text || (json.output || []).flatMap((i) => i.content || []).map((p) => p.text || '').join('\n').trim();
|
|
740
|
+
return { text, toolCalls: [], usage: { uncachedInputTokens: Math.max(0, input - cached), cachedInputTokens: cached, outputTokens: Number(u.output_tokens || 0), providerTotalTokens: Number(u.total_tokens || 0) }, durationMs, responseId: json.id };
|
|
741
|
+
},
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
// ── Anthropic Messages API (2.5 Full Parity: Real SSE + Parallel Tool Coalescing + Thinking + Images + AuthStore) ──
|
|
746
|
+
function attachImagesToAnthropicBlocks(text, images) {
|
|
747
|
+
const blocks = [];
|
|
748
|
+
if (text) blocks.push({ type: 'text', text: String(text) });
|
|
749
|
+
for (const img of images || []) {
|
|
750
|
+
if (!img) continue;
|
|
751
|
+
if (img.url) {
|
|
752
|
+
blocks.push({ type: 'image', source: { type: 'url', url: img.url } });
|
|
753
|
+
} else {
|
|
754
|
+
const b64 = readImageBase64(img);
|
|
755
|
+
if (b64) {
|
|
756
|
+
blocks.push({ type: 'image', source: { type: 'base64', media_type: b64.mimeType, data: b64.data } });
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
return blocks.length ? blocks : [{ type: 'text', text: '' }];
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
function buildAnthropicMessages(prompt, { cachePrefix = '', messages = null, images = null } = {}) {
|
|
764
|
+
const userText = cachePrefix && String(prompt || '').startsWith(cachePrefix)
|
|
765
|
+
? String(prompt).slice(cachePrefix.length)
|
|
766
|
+
: prompt;
|
|
767
|
+
if (!Array.isArray(messages) || !messages.length) {
|
|
768
|
+
const content = Array.isArray(images) && images.length
|
|
769
|
+
? attachImagesToAnthropicBlocks(userText || prompt, images)
|
|
770
|
+
: (userText || prompt);
|
|
771
|
+
return [{ role: 'user', content }];
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
const out = [];
|
|
775
|
+
for (let i = 0; i < messages.length; i++) {
|
|
776
|
+
const msg = messages[i];
|
|
777
|
+
if (msg.role === 'tool') {
|
|
778
|
+
const block = {
|
|
779
|
+
type: 'tool_result',
|
|
780
|
+
tool_use_id: msg.toolCallId || msg.id || 'tool_0',
|
|
781
|
+
content: typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content ?? ''),
|
|
782
|
+
};
|
|
783
|
+
// Coalesce consecutive `role: 'tool'` results into ONE `user` turn (prevents Anthropic HTTP 400!)
|
|
784
|
+
const prev = out[out.length - 1];
|
|
785
|
+
if (prev && prev.role === 'user' && Array.isArray(prev.content) && prev.content[0]?.type === 'tool_result') {
|
|
786
|
+
prev.content.push(block);
|
|
787
|
+
} else if (prev && prev.role === 'user') {
|
|
788
|
+
prev.content = [
|
|
789
|
+
...(typeof prev.content === 'string' ? [{ type: 'text', text: prev.content }] : prev.content),
|
|
790
|
+
block,
|
|
791
|
+
];
|
|
792
|
+
} else {
|
|
793
|
+
out.push({ role: 'user', content: [block] });
|
|
794
|
+
}
|
|
795
|
+
continue;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
if (msg.role === 'assistant') {
|
|
799
|
+
const blocks = [];
|
|
800
|
+
if (Array.isArray(msg.thinkingBlocks)) {
|
|
801
|
+
for (const tb of msg.thinkingBlocks) blocks.push(tb);
|
|
802
|
+
}
|
|
803
|
+
if (msg.content) blocks.push({ type: 'text', text: String(msg.content) });
|
|
804
|
+
if (Array.isArray(msg.toolCalls) && msg.toolCalls.length) {
|
|
805
|
+
for (const tc of msg.toolCalls) {
|
|
806
|
+
blocks.push({ type: 'tool_use', id: tc.id, name: tc.name, input: tc.arguments || {} });
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
out.push({
|
|
810
|
+
role: 'assistant',
|
|
811
|
+
content: blocks.length ? (blocks.length === 1 && blocks[0].type === 'text' ? blocks[0].text : blocks) : '',
|
|
812
|
+
});
|
|
813
|
+
continue;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
// `user` message (defensively merge consecutive user messages to guarantee strict alternation)
|
|
817
|
+
const isLastUser = i === messages.length - 1;
|
|
818
|
+
const textVal = typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content ?? '');
|
|
819
|
+
const content = isLastUser && Array.isArray(images) && images.length
|
|
820
|
+
? attachImagesToAnthropicBlocks(textVal, images)
|
|
821
|
+
: textVal;
|
|
822
|
+
const prev = out[out.length - 1];
|
|
823
|
+
if (prev && prev.role === 'user') {
|
|
824
|
+
const prevBlocks = typeof prev.content === 'string' ? [{ type: 'text', text: prev.content }] : [...prev.content];
|
|
825
|
+
const nextBlocks = typeof content === 'string' ? [{ type: 'text', text: content }] : content;
|
|
826
|
+
prev.content = [...prevBlocks, ...nextBlocks];
|
|
827
|
+
} else {
|
|
828
|
+
out.push({ role: 'user', content });
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
return out;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
async function fetchAnthropicSse(url, opts, { onDelta, fetchImpl = globalThis.fetch } = {}) {
|
|
835
|
+
const started = Date.now();
|
|
836
|
+
const res = await fetchWithRetry(fetchImpl, url, opts);
|
|
837
|
+
if (!res.ok) {
|
|
838
|
+
const text = await res.text();
|
|
839
|
+
let json; try { json = JSON.parse(text); } catch { json = { raw: text }; }
|
|
840
|
+
throw buildHttpError(res.status, json?.error?.message || text.slice(0, 300), res.headers);
|
|
841
|
+
}
|
|
842
|
+
const ct = res.headers && typeof res.headers.get === 'function' ? (res.headers.get('content-type') || '') : '';
|
|
843
|
+
if (!ct.includes('text/event-stream') || !res.body) {
|
|
844
|
+
const json = await res.json();
|
|
845
|
+
return { streamed: false, json, durationMs: Date.now() - started };
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
let responseId = null;
|
|
849
|
+
let text = '';
|
|
850
|
+
let stopReason = null;
|
|
851
|
+
const blocks = new Map(); // index -> { type, id, name, text, jsonRaw, thinking, signature, data }
|
|
852
|
+
const usage = { input_tokens: 0, cache_creation_input_tokens: 0, cache_read_input_tokens: 0, output_tokens: 0 };
|
|
853
|
+
|
|
854
|
+
await consumeSseStream(res.body, (payload) => {
|
|
855
|
+
let ev;
|
|
856
|
+
try { ev = JSON.parse(payload); } catch { return; }
|
|
857
|
+
if (ev.type === 'message_start') {
|
|
858
|
+
if (ev.message?.id) responseId = ev.message.id;
|
|
859
|
+
const u = ev.message?.usage || {};
|
|
860
|
+
if (u.input_tokens != null) usage.input_tokens = Number(u.input_tokens);
|
|
861
|
+
if (u.cache_creation_input_tokens != null) usage.cache_creation_input_tokens = Number(u.cache_creation_input_tokens);
|
|
862
|
+
if (u.cache_read_input_tokens != null) usage.cache_read_input_tokens = Number(u.cache_read_input_tokens);
|
|
863
|
+
if (u.output_tokens != null) usage.output_tokens = Number(u.output_tokens);
|
|
864
|
+
} else if (ev.type === 'content_block_start') {
|
|
865
|
+
const cb = ev.content_block || {};
|
|
866
|
+
blocks.set(ev.index ?? blocks.size, {
|
|
867
|
+
type: cb.type || 'text',
|
|
868
|
+
id: cb.id || null,
|
|
869
|
+
name: cb.name || null,
|
|
870
|
+
text: cb.text || '',
|
|
871
|
+
jsonRaw: '',
|
|
872
|
+
thinking: cb.thinking || '',
|
|
873
|
+
signature: cb.signature || '',
|
|
874
|
+
data: cb.data || null,
|
|
875
|
+
});
|
|
876
|
+
} else if (ev.type === 'content_block_delta') {
|
|
877
|
+
const entry = blocks.get(ev.index ?? 0);
|
|
878
|
+
const d = ev.delta || {};
|
|
879
|
+
if (d.type === 'text_delta' && typeof d.text === 'string') {
|
|
880
|
+
text += d.text;
|
|
881
|
+
if (entry) entry.text += d.text;
|
|
882
|
+
if (typeof onDelta === 'function' && d.text) onDelta(d.text);
|
|
883
|
+
} else if (d.type === 'input_json_delta' && typeof d.partial_json === 'string') {
|
|
884
|
+
if (entry) entry.jsonRaw += d.partial_json;
|
|
885
|
+
} else if (d.type === 'thinking_delta' && typeof d.thinking === 'string') {
|
|
886
|
+
if (entry) entry.thinking += d.thinking;
|
|
887
|
+
} else if (d.type === 'signature_delta' && typeof d.signature === 'string') {
|
|
888
|
+
if (entry) entry.signature += d.signature;
|
|
889
|
+
}
|
|
890
|
+
} else if (ev.type === 'message_delta') {
|
|
891
|
+
if (ev.delta?.stop_reason) stopReason = ev.delta.stop_reason;
|
|
892
|
+
const u = ev.usage || {};
|
|
893
|
+
if (u.output_tokens != null) usage.output_tokens = Number(u.output_tokens);
|
|
894
|
+
if (u.input_tokens != null) usage.input_tokens = Number(u.input_tokens);
|
|
895
|
+
if (u.cache_read_input_tokens != null) usage.cache_read_input_tokens = Number(u.cache_read_input_tokens);
|
|
896
|
+
if (u.cache_creation_input_tokens != null) usage.cache_creation_input_tokens = Number(u.cache_creation_input_tokens);
|
|
897
|
+
} else if (ev.type === 'error') {
|
|
898
|
+
const status = ev.error?.type === 'overloaded_error' ? 529 : (ev.error?.type === 'rate_limit_error' ? 429 : 500);
|
|
899
|
+
const err = buildHttpError(status, ev.error?.message || 'anthropic_stream_error', res.headers);
|
|
900
|
+
err.errorType = ev.error?.type || 'stream_error';
|
|
901
|
+
throw err;
|
|
902
|
+
}
|
|
903
|
+
});
|
|
904
|
+
|
|
905
|
+
const truncated = stopReason === 'max_tokens';
|
|
906
|
+
const ordered = [...blocks.entries()].sort((a, b) => a[0] - b[0]).map(([, v]) => v);
|
|
907
|
+
const toolCalls = [];
|
|
908
|
+
const thinkingBlocks = [];
|
|
909
|
+
for (const b of ordered) {
|
|
910
|
+
if (b.type === 'tool_use') {
|
|
911
|
+
let args = {};
|
|
912
|
+
let malformed = false;
|
|
913
|
+
try { args = JSON.parse(b.jsonRaw || '{}'); } catch { args = { raw: b.jsonRaw }; malformed = true; }
|
|
914
|
+
if (truncated) malformed = true;
|
|
915
|
+
toolCalls.push({
|
|
916
|
+
id: b.id || `toolu_${toolCalls.length}`,
|
|
917
|
+
name: b.name || '',
|
|
918
|
+
arguments: args,
|
|
919
|
+
...(malformed ? { malformed: true, truncated } : {}),
|
|
920
|
+
});
|
|
921
|
+
} else if (b.type === 'thinking') {
|
|
922
|
+
thinkingBlocks.push({ type: 'thinking', thinking: b.thinking, ...(b.signature ? { signature: b.signature } : {}) });
|
|
923
|
+
} else if (b.type === 'redacted_thinking') {
|
|
924
|
+
thinkingBlocks.push({ type: 'redacted_thinking', data: b.data });
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
const cr = Number(usage.cache_read_input_tokens || 0);
|
|
928
|
+
const ccTok = Number(usage.cache_creation_input_tokens || 0);
|
|
929
|
+
const input = Number(usage.input_tokens || 0);
|
|
930
|
+
const out = Number(usage.output_tokens || 0);
|
|
931
|
+
return {
|
|
932
|
+
streamed: true,
|
|
933
|
+
text: text.trim(),
|
|
934
|
+
toolCalls,
|
|
935
|
+
thinkingBlocks,
|
|
936
|
+
stopReason,
|
|
937
|
+
usage: {
|
|
938
|
+
uncachedInputTokens: input + ccTok,
|
|
939
|
+
cachedInputTokens: cr,
|
|
940
|
+
outputTokens: out,
|
|
941
|
+
providerTotalTokens: input + ccTok + cr + out,
|
|
942
|
+
},
|
|
943
|
+
durationMs: Date.now() - started,
|
|
944
|
+
responseId,
|
|
945
|
+
};
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
function makeAnthropicProvider({
|
|
949
|
+
model,
|
|
950
|
+
apiKey = null,
|
|
951
|
+
baseUrl = null,
|
|
952
|
+
authStore = null,
|
|
953
|
+
providerId = 'anthropic',
|
|
954
|
+
cacheTtl: defaultCacheTtl = null,
|
|
955
|
+
fetchImpl = globalThis.fetch,
|
|
956
|
+
} = {}) {
|
|
957
|
+
const key = apiKey || process.env.ANTHROPIC_API_KEY || null;
|
|
958
|
+
const resolvedBase = (baseUrl || process.env.ANTHROPIC_BASE_URL || 'https://api.anthropic.com').replace(/\/+$/, '');
|
|
959
|
+
return {
|
|
960
|
+
name: 'anthropic',
|
|
961
|
+
model: model || process.env.ANTHROPIC_MODEL || 'claude-3-5-haiku-latest',
|
|
962
|
+
supportsTools: true,
|
|
963
|
+
get ready() {
|
|
964
|
+
if (key) return true;
|
|
965
|
+
if (authStore && authStore.listProfiles({ provider: providerId }).length > 0) return true;
|
|
966
|
+
return false;
|
|
967
|
+
},
|
|
968
|
+
missing: 'ANTHROPIC_API_KEY not set (or no anthropic profile in authStore)',
|
|
969
|
+
async generate(prompt, {
|
|
970
|
+
maxTokens = 800,
|
|
971
|
+
model: m,
|
|
972
|
+
cachePrefix = '',
|
|
973
|
+
system: sysProp = '',
|
|
974
|
+
messages = null,
|
|
975
|
+
tools = null,
|
|
976
|
+
images = null,
|
|
977
|
+
cacheTtl = null,
|
|
978
|
+
onDelta = null,
|
|
979
|
+
stream = Boolean(onDelta),
|
|
980
|
+
signal = null,
|
|
981
|
+
authStore: callAuthStore = null,
|
|
982
|
+
} = {}) {
|
|
983
|
+
const store = callAuthStore || authStore;
|
|
984
|
+
const effectiveTtl = cacheTtl || defaultCacheTtl || null;
|
|
985
|
+
const cc = effectiveTtl === '1h' ? { type: 'ephemeral', ttl: '1h' } : { type: 'ephemeral' };
|
|
986
|
+
|
|
987
|
+
const sysText = sysProp || cachePrefix;
|
|
988
|
+
const system = sysText
|
|
989
|
+
? [{ type: 'text', text: sysText, cache_control: { ...cc } }]
|
|
990
|
+
: undefined;
|
|
991
|
+
const apiMessages = buildAnthropicMessages(prompt, { cachePrefix, messages, images });
|
|
992
|
+
|
|
993
|
+
// §13 Breakpoint 3: penultimate message caches entire multi-turn history prefix
|
|
994
|
+
if (apiMessages.length >= 2) {
|
|
995
|
+
const prev = apiMessages[apiMessages.length - 2];
|
|
996
|
+
if (typeof prev.content === 'string') {
|
|
997
|
+
prev.content = [{ type: 'text', text: prev.content, cache_control: { ...cc } }];
|
|
998
|
+
} else if (Array.isArray(prev.content) && prev.content.length > 0) {
|
|
999
|
+
prev.content = prev.content.map((b, idx) => (idx === prev.content.length - 1 ? { ...b, cache_control: { ...cc } } : b));
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
const body = {
|
|
1004
|
+
model: m || this.model,
|
|
1005
|
+
max_tokens: maxTokens,
|
|
1006
|
+
...(system ? { system } : {}),
|
|
1007
|
+
messages: apiMessages,
|
|
1008
|
+
...(stream ? { stream: true } : {}),
|
|
1009
|
+
};
|
|
1010
|
+
if (Array.isArray(tools) && tools.length) {
|
|
1011
|
+
body.tools = tools.map((t) => ({ name: t.name, description: t.description || '', input_schema: t.parameters || { type: 'object', properties: {} } }));
|
|
1012
|
+
body.tools[body.tools.length - 1].cache_control = { ...cc };
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
const execWithCred = async (cred) => {
|
|
1016
|
+
const targetBase = (cred?.baseUrl || resolvedBase).replace(/\/+$/, '');
|
|
1017
|
+
const url = `${targetBase}/v1/messages`;
|
|
1018
|
+
const reqHeaders = {
|
|
1019
|
+
'Content-Type': 'application/json',
|
|
1020
|
+
'anthropic-version': '2023-06-01',
|
|
1021
|
+
...(cred?.headers || {}),
|
|
1022
|
+
};
|
|
1023
|
+
if (effectiveTtl === '1h') reqHeaders['anthropic-beta'] = 'extended-cache-ttl-2025-04-11';
|
|
1024
|
+
if (cred?.type === 'oauth' && cred.token) {
|
|
1025
|
+
reqHeaders.Authorization = `Bearer ${cred.token}`;
|
|
1026
|
+
} else {
|
|
1027
|
+
const apiK = cred?.token || key;
|
|
1028
|
+
if (apiK) reqHeaders['x-api-key'] = apiK;
|
|
1029
|
+
}
|
|
1030
|
+
const fetchOpts = { method: 'POST', headers: reqHeaders, body: JSON.stringify(body), signal };
|
|
1031
|
+
|
|
1032
|
+
const parseAnthropicJson = (json, durationMs) => {
|
|
1033
|
+
const blocks = json.content || [];
|
|
1034
|
+
const stopReason = json.stop_reason || null;
|
|
1035
|
+
const truncated = stopReason === 'max_tokens';
|
|
1036
|
+
const text = blocks.filter((p) => p.type === 'text' || p.text).map((p) => p.text || '').join('\n').trim();
|
|
1037
|
+
if (typeof onDelta === 'function' && text) onDelta(text);
|
|
1038
|
+
const toolCalls = blocks.filter((p) => p.type === 'tool_use').map((p) => ({
|
|
1039
|
+
id: p.id,
|
|
1040
|
+
name: p.name,
|
|
1041
|
+
arguments: p.input || {},
|
|
1042
|
+
...(truncated ? { malformed: true, truncated: true } : {}),
|
|
1043
|
+
}));
|
|
1044
|
+
const thinkingBlocks = blocks.filter((p) => p.type === 'thinking' || p.type === 'redacted_thinking');
|
|
1045
|
+
const u = json.usage || {};
|
|
1046
|
+
const cr = Number(u.cache_read_input_tokens || 0);
|
|
1047
|
+
const ccTok = Number(u.cache_creation_input_tokens || 0);
|
|
1048
|
+
const input = Number(u.input_tokens || 0);
|
|
1049
|
+
return {
|
|
1050
|
+
text,
|
|
1051
|
+
toolCalls,
|
|
1052
|
+
thinkingBlocks,
|
|
1053
|
+
stopReason,
|
|
1054
|
+
usage: { uncachedInputTokens: input + ccTok, cachedInputTokens: cr, outputTokens: Number(u.output_tokens || 0), providerTotalTokens: input + ccTok + cr + Number(u.output_tokens || 0) },
|
|
1055
|
+
durationMs,
|
|
1056
|
+
responseId: json.id,
|
|
1057
|
+
};
|
|
1058
|
+
};
|
|
1059
|
+
|
|
1060
|
+
if (stream) {
|
|
1061
|
+
const r = await fetchAnthropicSse(url, fetchOpts, { onDelta, fetchImpl });
|
|
1062
|
+
if (r.streamed) return r;
|
|
1063
|
+
return parseAnthropicJson(r.json, r.durationMs);
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
const { json, durationMs } = await fetchJson(url, fetchOpts, fetchImpl);
|
|
1067
|
+
return parseAnthropicJson(json, durationMs);
|
|
1068
|
+
};
|
|
1069
|
+
|
|
1070
|
+
if (store && store.listProfiles({ provider: providerId }).length > 0) {
|
|
1071
|
+
const { result, profileId } = await store.withFailover(providerId, execWithCred);
|
|
1072
|
+
return { ...result, authProfileId: profileId };
|
|
1073
|
+
}
|
|
1074
|
+
return execWithCred(null);
|
|
1075
|
+
},
|
|
1076
|
+
};
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
// ── Google Gemini API (2.5 Full Parity: Real SSE + Parallel Function Calling + thoughtSignature + Images + AuthStore) ──
|
|
1080
|
+
const GEMINI_UNSUPPORTED_SCHEMA_KEYS = new Set([
|
|
1081
|
+
'additionalProperties',
|
|
1082
|
+
'unevaluatedProperties',
|
|
1083
|
+
'patternProperties',
|
|
1084
|
+
'$schema',
|
|
1085
|
+
'$id',
|
|
1086
|
+
'$ref',
|
|
1087
|
+
'$defs',
|
|
1088
|
+
'definitions',
|
|
1089
|
+
'$comment',
|
|
1090
|
+
'default',
|
|
1091
|
+
'examples',
|
|
1092
|
+
'oneOf',
|
|
1093
|
+
'anyOf',
|
|
1094
|
+
'allOf',
|
|
1095
|
+
'not',
|
|
1096
|
+
]);
|
|
1097
|
+
|
|
1098
|
+
function sanitizeGeminiNode(node, { isPropertiesMap = false } = {}) {
|
|
1099
|
+
// Primitives (strings inside `required` / `enum`, numbers, booleans, null) MUST pass through untouched!
|
|
1100
|
+
if (node === null || typeof node !== 'object') return node;
|
|
1101
|
+
if (Array.isArray(node)) {
|
|
1102
|
+
return node.map((item) => sanitizeGeminiNode(item, { isPropertiesMap: false }));
|
|
1103
|
+
}
|
|
1104
|
+
// Handle JSON-Schema unions (`anyOf` / `oneOf`) without dropping branch constraints or breaking `type: 'object'` schemas:
|
|
1105
|
+
// - Object schemas (`type: 'object'` or explicit `properties`) keep `type: 'object'` intact (Gemini requires OBJECT root/containers)
|
|
1106
|
+
// - Each branch is sanitized BEFORE inheriting scalar parent `type` so `$ref`-only branches strip to `{}` and are pruned
|
|
1107
|
+
// - Single non-null branch (e.g. Pydantic v2 `Optional[T]`): unwrap to `{ ...branch, nullable: true }`
|
|
1108
|
+
// - Single nested-union branch (`Optional[Union[A, B]]`) or multiple non-null branches: preserve all branches in `anyOf`
|
|
1109
|
+
let source = node;
|
|
1110
|
+
let preservedAnyOf = null;
|
|
1111
|
+
let preservedNullable = false;
|
|
1112
|
+
const isObjectContainer = node.type === 'object' || (node.properties && typeof node.properties === 'object' && !Array.isArray(node.properties));
|
|
1113
|
+
if (!isPropertiesMap && !isObjectContainer && (Array.isArray(node.anyOf) || Array.isArray(node.oneOf))) {
|
|
1114
|
+
const branches = Array.isArray(node.anyOf) ? node.anyOf : node.oneOf;
|
|
1115
|
+
const nonNullNodeTypes = Array.isArray(node.type)
|
|
1116
|
+
? node.type.filter((t) => typeof t === 'string' && t !== 'null')
|
|
1117
|
+
: (typeof node.type === 'string' && node.type !== 'null' ? [node.type] : []);
|
|
1118
|
+
const scalarParentType = nonNullNodeTypes.length === 1 ? nonNullNodeTypes[0] : null;
|
|
1119
|
+
const typeArrayHasNull = Array.isArray(node.type) && node.type.includes('null');
|
|
1120
|
+
const hasNull = typeArrayHasNull || branches.some((b) => b && typeof b === 'object' && b.type === 'null');
|
|
1121
|
+
const rawNonNull = branches.filter((b) => b && typeof b === 'object' && b.type !== 'null');
|
|
1122
|
+
const sanitizedBranches = rawNonNull
|
|
1123
|
+
.map((b) => sanitizeGeminiNode(b, { isPropertiesMap: false }))
|
|
1124
|
+
.filter((b) => b && typeof b === 'object' && Object.keys(b).length > 0)
|
|
1125
|
+
.map((b) => (scalarParentType && !b.type && !b.anyOf ? { type: scalarParentType, ...b } : b));
|
|
1126
|
+
if (sanitizedBranches.length === 1 && Array.isArray(sanitizedBranches[0].anyOf)) {
|
|
1127
|
+
preservedAnyOf = sanitizedBranches[0].anyOf;
|
|
1128
|
+
preservedNullable = hasNull || Boolean(sanitizedBranches[0].nullable);
|
|
1129
|
+
} else if (sanitizedBranches.length === 1 && sanitizedBranches[0].type) {
|
|
1130
|
+
source = { ...sanitizedBranches[0], ...node, type: sanitizedBranches[0].type, ...(hasNull ? { nullable: true } : {}) };
|
|
1131
|
+
} else if (sanitizedBranches.length > 1) {
|
|
1132
|
+
preservedAnyOf = sanitizedBranches;
|
|
1133
|
+
preservedNullable = hasNull;
|
|
1134
|
+
} else if (sanitizedBranches.length === 1) {
|
|
1135
|
+
source = { ...sanitizedBranches[0], ...node, ...(hasNull ? { nullable: true } : {}) };
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
const out = {};
|
|
1139
|
+
for (const [k, v] of Object.entries(source)) {
|
|
1140
|
+
// Only filter/transform JSON-Schema keywords on schema nodes, NEVER when `k` is a tool parameter name inside `properties`!
|
|
1141
|
+
if (!isPropertiesMap) {
|
|
1142
|
+
if (GEMINI_UNSUPPORTED_SCHEMA_KEYS.has(k)) continue;
|
|
1143
|
+
if (k === 'type' && preservedAnyOf) {
|
|
1144
|
+
// Explicit anyOf/oneOf branches already carry their types and constraints; preserve nullability only!
|
|
1145
|
+
if (Array.isArray(v) && v.includes('null')) out.nullable = true;
|
|
1146
|
+
continue;
|
|
1147
|
+
}
|
|
1148
|
+
if (k === 'const') {
|
|
1149
|
+
if (v !== undefined && v !== null) out.enum = [String(v)];
|
|
1150
|
+
continue;
|
|
1151
|
+
}
|
|
1152
|
+
if (k === 'type' && Array.isArray(v)) {
|
|
1153
|
+
const nonNull = v.filter((t) => typeof t === 'string' && t !== 'null');
|
|
1154
|
+
if (nonNull.length === 1) {
|
|
1155
|
+
out.type = nonNull[0];
|
|
1156
|
+
} else if (nonNull.length > 1) {
|
|
1157
|
+
out.anyOf = nonNull.map((t) => ({ type: t }));
|
|
1158
|
+
}
|
|
1159
|
+
if (v.includes('null')) out.nullable = true;
|
|
1160
|
+
continue;
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
if (k === 'properties' && !isPropertiesMap && v && typeof v === 'object' && !Array.isArray(v)) {
|
|
1164
|
+
out[k] = sanitizeGeminiNode(v, { isPropertiesMap: true });
|
|
1165
|
+
} else if (v && typeof v === 'object') {
|
|
1166
|
+
out[k] = sanitizeGeminiNode(v, { isPropertiesMap: false });
|
|
1167
|
+
} else {
|
|
1168
|
+
out[k] = v;
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
if (preservedAnyOf) {
|
|
1172
|
+
out.anyOf = preservedAnyOf;
|
|
1173
|
+
if (preservedNullable) out.nullable = true;
|
|
1174
|
+
}
|
|
1175
|
+
if (!isPropertiesMap && isObjectContainer) {
|
|
1176
|
+
if (!out.type) out.type = 'object';
|
|
1177
|
+
if (Array.isArray(node.anyOf) || Array.isArray(node.oneOf)) {
|
|
1178
|
+
const parentPropKeys = new Set(out.properties && typeof out.properties === 'object' ? Object.keys(out.properties) : []);
|
|
1179
|
+
const isNonEmptySchema = (obj) => Boolean(obj && typeof obj === 'object' && Object.keys(obj).length > 0);
|
|
1180
|
+
const isPureAnyOfWrapper = (obj) => Boolean(
|
|
1181
|
+
obj && typeof obj === 'object' && Array.isArray(obj.anyOf) && !obj.type
|
|
1182
|
+
&& Object.keys(obj).every((k) => k === 'anyOf' || k === 'nullable')
|
|
1183
|
+
);
|
|
1184
|
+
const unionBranches = Array.isArray(node.anyOf) ? node.anyOf : node.oneOf;
|
|
1185
|
+
for (const b of unionBranches) {
|
|
1186
|
+
if (b && typeof b === 'object' && b.properties && typeof b.properties === 'object' && !Array.isArray(b.properties)) {
|
|
1187
|
+
if (!out.properties) out.properties = {};
|
|
1188
|
+
const cleanProps = sanitizeGeminiNode(b.properties, { isPropertiesMap: true });
|
|
1189
|
+
for (const [pk, pv] of Object.entries(cleanProps)) {
|
|
1190
|
+
if (parentPropKeys.has(pk) || !isNonEmptySchema(pv)) continue;
|
|
1191
|
+
if (!Object.prototype.hasOwnProperty.call(out.properties, pk)) {
|
|
1192
|
+
out.properties[pk] = pv;
|
|
1193
|
+
} else {
|
|
1194
|
+
// Flatten ONLY pure `{ anyOf, nullable? }` wrappers so sibling constraints (`enum`, `description`, etc.) are NEVER dropped!
|
|
1195
|
+
const existing = out.properties[pk];
|
|
1196
|
+
const existingList = isPureAnyOfWrapper(existing) ? existing.anyOf : [existing];
|
|
1197
|
+
const incomingList = isPureAnyOfWrapper(pv) ? pv.anyOf : [pv];
|
|
1198
|
+
const merged = [];
|
|
1199
|
+
const seen = new Set();
|
|
1200
|
+
for (const item of [...existingList, ...incomingList]) {
|
|
1201
|
+
if (!isNonEmptySchema(item)) continue;
|
|
1202
|
+
const key = JSON.stringify(item);
|
|
1203
|
+
if (!seen.has(key)) {
|
|
1204
|
+
seen.add(key);
|
|
1205
|
+
merged.push(item);
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
if (merged.length > 1) {
|
|
1209
|
+
const nullable = Boolean(existing?.nullable || pv?.nullable);
|
|
1210
|
+
out.properties[pk] = nullable ? { anyOf: merged, nullable: true } : { anyOf: merged };
|
|
1211
|
+
} else if (merged.length === 1 && !existing?.nullable && pv?.nullable) {
|
|
1212
|
+
out.properties[pk] = { ...merged[0], nullable: true };
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
// Gemini rejects `required` keys that do not exist in `properties`
|
|
1221
|
+
if (!isPropertiesMap && Array.isArray(out.required)) {
|
|
1222
|
+
const propKeys = out.properties && typeof out.properties === 'object' ? Object.keys(out.properties) : [];
|
|
1223
|
+
const propSet = new Set(propKeys);
|
|
1224
|
+
const filteredReq = out.required.filter((r) => typeof r === 'string' && propSet.has(r));
|
|
1225
|
+
if (filteredReq.length) out.required = filteredReq;
|
|
1226
|
+
else delete out.required;
|
|
1227
|
+
}
|
|
1228
|
+
return out;
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
export function sanitizeGeminiSchema(schema) {
|
|
1232
|
+
if (!schema || typeof schema !== 'object' || Array.isArray(schema)) {
|
|
1233
|
+
return { type: 'object', properties: {} };
|
|
1234
|
+
}
|
|
1235
|
+
return sanitizeGeminiNode(schema, { isPropertiesMap: false });
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
function buildGeminiContents(prompt, { messages = null, images = null } = {}) {
|
|
1239
|
+
const contents = [];
|
|
1240
|
+
if (Array.isArray(messages) && messages.length) {
|
|
1241
|
+
for (const msg of messages) {
|
|
1242
|
+
if (msg.role === 'tool') {
|
|
1243
|
+
let parsed = null;
|
|
1244
|
+
try { parsed = typeof msg.content === 'string' ? JSON.parse(msg.content) : msg.content; } catch { parsed = null; }
|
|
1245
|
+
const responseObj = (parsed && typeof parsed === 'object' && !Array.isArray(parsed))
|
|
1246
|
+
? parsed
|
|
1247
|
+
: { result: typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content ?? '') };
|
|
1248
|
+
const part = { functionResponse: { name: msg.name || 'tool', response: responseObj } };
|
|
1249
|
+
const prev = contents[contents.length - 1];
|
|
1250
|
+
if (prev && prev.role === 'user' && prev.parts.some((p) => p.functionResponse)) {
|
|
1251
|
+
prev.parts.push(part);
|
|
1252
|
+
} else {
|
|
1253
|
+
contents.push({ role: 'user', parts: [part] });
|
|
1254
|
+
}
|
|
1255
|
+
continue;
|
|
1256
|
+
}
|
|
1257
|
+
if (msg.role === 'assistant') {
|
|
1258
|
+
if (Array.isArray(msg.rawParts) && msg.rawParts.length) {
|
|
1259
|
+
contents.push({ role: 'model', parts: msg.rawParts });
|
|
1260
|
+
continue;
|
|
1261
|
+
}
|
|
1262
|
+
const parts = [];
|
|
1263
|
+
if (msg.content) parts.push({ text: String(msg.content) });
|
|
1264
|
+
if (Array.isArray(msg.toolCalls) && msg.toolCalls.length) {
|
|
1265
|
+
for (const tc of msg.toolCalls) {
|
|
1266
|
+
const p = { functionCall: { name: tc.name, args: tc.arguments || {} } };
|
|
1267
|
+
if (tc.thoughtSignature) p.thoughtSignature = tc.thoughtSignature;
|
|
1268
|
+
parts.push(p);
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
contents.push({ role: 'model', parts: parts.length ? parts : [{ text: '' }] });
|
|
1272
|
+
continue;
|
|
1273
|
+
}
|
|
1274
|
+
const txt = typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content ?? '');
|
|
1275
|
+
contents.push({ role: 'user', parts: [{ text: txt }] });
|
|
1276
|
+
}
|
|
1277
|
+
} else {
|
|
1278
|
+
contents.push({ role: 'user', parts: [{ text: String(prompt ?? '') }] });
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
if (Array.isArray(images) && images.length && contents.length) {
|
|
1282
|
+
const last = contents[contents.length - 1];
|
|
1283
|
+
for (const img of images) {
|
|
1284
|
+
if (!img) continue;
|
|
1285
|
+
if (img.url && !img.base64 && !img.path) {
|
|
1286
|
+
last.parts.push({ fileData: { mimeType: img.mimeType || 'image/jpeg', fileUri: img.url } });
|
|
1287
|
+
} else {
|
|
1288
|
+
const b64 = readImageBase64(img);
|
|
1289
|
+
if (b64) {
|
|
1290
|
+
last.parts.push({ inlineData: { mimeType: b64.mimeType, data: b64.data } });
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
return contents;
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
function parseGeminiParts(parts, toolCounter = { n: 0 }) {
|
|
1299
|
+
const visibleTexts = [];
|
|
1300
|
+
const toolCalls = [];
|
|
1301
|
+
const rawParts = [];
|
|
1302
|
+
for (const p of parts || []) {
|
|
1303
|
+
if (!p || typeof p !== 'object') continue;
|
|
1304
|
+
rawParts.push(p);
|
|
1305
|
+
if (p.functionCall) {
|
|
1306
|
+
toolCounter.n++;
|
|
1307
|
+
const tc = {
|
|
1308
|
+
id: p.functionCall.id || `gem_call_${toolCounter.n}_${p.functionCall.name || 'tool'}`,
|
|
1309
|
+
name: p.functionCall.name || '',
|
|
1310
|
+
arguments: p.functionCall.args || {},
|
|
1311
|
+
};
|
|
1312
|
+
if (p.thoughtSignature) tc.thoughtSignature = p.thoughtSignature;
|
|
1313
|
+
toolCalls.push(tc);
|
|
1314
|
+
} else if (typeof p.text === 'string' && p.text && p.thought !== true) {
|
|
1315
|
+
visibleTexts.push(p.text);
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
return { text: visibleTexts.join(''), toolCalls, rawParts };
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
async function fetchGeminiSse(url, opts, { onDelta, fetchImpl = globalThis.fetch } = {}) {
|
|
1322
|
+
const started = Date.now();
|
|
1323
|
+
const res = await fetchWithRetry(fetchImpl, url, opts);
|
|
1324
|
+
if (!res.ok) {
|
|
1325
|
+
const text = await res.text();
|
|
1326
|
+
let json; try { json = JSON.parse(text); } catch { json = { raw: text }; }
|
|
1327
|
+
throw buildHttpError(res.status, json?.error?.message || text.slice(0, 300), res.headers);
|
|
1328
|
+
}
|
|
1329
|
+
const ct = res.headers && typeof res.headers.get === 'function' ? (res.headers.get('content-type') || '') : '';
|
|
1330
|
+
if (!ct.includes('text/event-stream') || !res.body) {
|
|
1331
|
+
const json = await res.json();
|
|
1332
|
+
return { streamed: false, json, durationMs: Date.now() - started };
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
let text = '';
|
|
1336
|
+
const allToolCalls = [];
|
|
1337
|
+
const allRawParts = [];
|
|
1338
|
+
const toolCounter = { n: 0 };
|
|
1339
|
+
let usageMeta = {};
|
|
1340
|
+
let responseId = null;
|
|
1341
|
+
let stopReason = null;
|
|
1342
|
+
|
|
1343
|
+
await consumeSseStream(res.body, (payload) => {
|
|
1344
|
+
let chunk;
|
|
1345
|
+
try { chunk = JSON.parse(payload); } catch { return; }
|
|
1346
|
+
if (chunk.responseId) responseId = chunk.responseId;
|
|
1347
|
+
if (chunk.usageMetadata) usageMeta = chunk.usageMetadata;
|
|
1348
|
+
const cand0 = chunk.candidates?.[0];
|
|
1349
|
+
if (cand0?.finishReason) stopReason = cand0.finishReason;
|
|
1350
|
+
const parts = (chunk.candidates || []).flatMap((c) => c.content?.parts || []);
|
|
1351
|
+
if (!parts.length) return;
|
|
1352
|
+
const parsed = parseGeminiParts(parts, toolCounter);
|
|
1353
|
+
if (parsed.text) {
|
|
1354
|
+
text += parsed.text;
|
|
1355
|
+
if (typeof onDelta === 'function') onDelta(parsed.text);
|
|
1356
|
+
}
|
|
1357
|
+
if (parsed.toolCalls.length) allToolCalls.push(...parsed.toolCalls);
|
|
1358
|
+
if (parsed.rawParts.length) allRawParts.push(...parsed.rawParts);
|
|
1359
|
+
});
|
|
1360
|
+
|
|
1361
|
+
const cached = Number(usageMeta.cachedContentTokenCount || 0);
|
|
1362
|
+
const input = Number(usageMeta.promptTokenCount || 0);
|
|
1363
|
+
const out = Number(usageMeta.candidatesTokenCount || 0);
|
|
1364
|
+
return {
|
|
1365
|
+
streamed: true,
|
|
1366
|
+
text: text.trim(),
|
|
1367
|
+
toolCalls: allToolCalls,
|
|
1368
|
+
rawParts: allRawParts,
|
|
1369
|
+
stopReason,
|
|
1370
|
+
usage: {
|
|
1371
|
+
uncachedInputTokens: Math.max(0, input - cached),
|
|
1372
|
+
cachedInputTokens: cached,
|
|
1373
|
+
outputTokens: out,
|
|
1374
|
+
providerTotalTokens: Number(usageMeta.totalTokenCount || (input + out)),
|
|
1375
|
+
},
|
|
1376
|
+
durationMs: Date.now() - started,
|
|
1377
|
+
responseId,
|
|
1378
|
+
};
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
function makeGeminiProvider({
|
|
1382
|
+
model,
|
|
1383
|
+
apiKey = null,
|
|
1384
|
+
baseUrl = null,
|
|
1385
|
+
authStore = null,
|
|
1386
|
+
providerId = 'google',
|
|
1387
|
+
cachedContent: defaultCachedContent = null,
|
|
1388
|
+
cachedContentIncludesTools = false,
|
|
1389
|
+
fetchImpl = globalThis.fetch,
|
|
1390
|
+
} = {}) {
|
|
1391
|
+
const key = apiKey || process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY || null;
|
|
1392
|
+
const resolvedBase = (baseUrl || process.env.GEMINI_BASE_URL || 'https://generativelanguage.googleapis.com/v1beta').replace(/\/+$/, '');
|
|
1393
|
+
return {
|
|
1394
|
+
name: 'gemini',
|
|
1395
|
+
model: model || process.env.GEMINI_MODEL || 'gemini-2.5-flash',
|
|
1396
|
+
supportsTools: true,
|
|
1397
|
+
get ready() {
|
|
1398
|
+
if (key) return true;
|
|
1399
|
+
if (authStore && authStore.listProfiles({ provider: providerId }).length > 0) return true;
|
|
1400
|
+
return false;
|
|
1401
|
+
},
|
|
1402
|
+
missing: 'GEMINI_API_KEY/GOOGLE_API_KEY not set (or no google profile in authStore)',
|
|
1403
|
+
async generate(prompt, {
|
|
1404
|
+
maxTokens = 800,
|
|
1405
|
+
model: m,
|
|
1406
|
+
system = '',
|
|
1407
|
+
messages = null,
|
|
1408
|
+
tools = null,
|
|
1409
|
+
images = null,
|
|
1410
|
+
cachedContent = null,
|
|
1411
|
+
onDelta = null,
|
|
1412
|
+
stream = Boolean(onDelta),
|
|
1413
|
+
signal = null,
|
|
1414
|
+
authStore: callAuthStore = null,
|
|
1415
|
+
} = {}) {
|
|
1416
|
+
const store = callAuthStore || authStore;
|
|
1417
|
+
const mp = (m || this.model).startsWith('models/') ? (m || this.model) : `models/${m || this.model}`;
|
|
1418
|
+
const contents = buildGeminiContents(prompt, { messages, images });
|
|
1419
|
+
const hasTools = Array.isArray(tools) && tools.length > 0;
|
|
1420
|
+
const body = { contents, generationConfig: { maxOutputTokens: maxTokens } };
|
|
1421
|
+
|
|
1422
|
+
const effectiveCachedContent = cachedContent || defaultCachedContent || null;
|
|
1423
|
+
// Gemini rejects sending `cachedContent` together with `systemInstruction` or `tools`.
|
|
1424
|
+
// If dynamic `tools` are supplied and the cache handle wasn't built with tools baked in,
|
|
1425
|
+
// gracefully fall back to Gemini's automatic Implicit Caching (`systemInstruction` + `tools`).
|
|
1426
|
+
if (effectiveCachedContent && (!hasTools || cachedContentIncludesTools)) {
|
|
1427
|
+
body.cachedContent = String(effectiveCachedContent);
|
|
1428
|
+
} else {
|
|
1429
|
+
if (system) body.systemInstruction = { parts: [{ text: system }] };
|
|
1430
|
+
if (hasTools) {
|
|
1431
|
+
body.tools = [{
|
|
1432
|
+
functionDeclarations: tools.map((t) => ({
|
|
1433
|
+
name: t.name,
|
|
1434
|
+
description: t.description || '',
|
|
1435
|
+
parameters: sanitizeGeminiSchema(t.parameters),
|
|
1436
|
+
})),
|
|
1437
|
+
}];
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
const execWithCred = async (cred) => {
|
|
1442
|
+
const targetBase = (cred?.baseUrl || resolvedBase).replace(/\/+$/, '');
|
|
1443
|
+
const reqHeaders = { 'Content-Type': 'application/json', ...(cred?.headers || {}) };
|
|
1444
|
+
let url;
|
|
1445
|
+
if (cred?.type === 'oauth' && cred.token) {
|
|
1446
|
+
reqHeaders.Authorization = `Bearer ${cred.token}`;
|
|
1447
|
+
url = stream
|
|
1448
|
+
? `${targetBase}/${mp}:streamGenerateContent?alt=sse`
|
|
1449
|
+
: `${targetBase}/${mp}:generateContent`;
|
|
1450
|
+
} else {
|
|
1451
|
+
const apiK = cred?.token || key;
|
|
1452
|
+
const qs = apiK ? `key=${encodeURIComponent(apiK)}` : '';
|
|
1453
|
+
url = stream
|
|
1454
|
+
? `${targetBase}/${mp}:streamGenerateContent?alt=sse${qs ? `&${qs}` : ''}`
|
|
1455
|
+
: `${targetBase}/${mp}:generateContent${qs ? `?${qs}` : ''}`;
|
|
1456
|
+
}
|
|
1457
|
+
const fetchOpts = { method: 'POST', headers: reqHeaders, body: JSON.stringify(body), signal };
|
|
1458
|
+
|
|
1459
|
+
if (stream) {
|
|
1460
|
+
const r = await fetchGeminiSse(url, fetchOpts, { onDelta, fetchImpl });
|
|
1461
|
+
if (r.streamed) return r;
|
|
1462
|
+
const json = r.json;
|
|
1463
|
+
const u = json.usageMetadata || {};
|
|
1464
|
+
const cached = Number(u.cachedContentTokenCount || 0);
|
|
1465
|
+
const input = Number(u.promptTokenCount || 0);
|
|
1466
|
+
const parts = (json.candidates || []).flatMap((c) => c.content?.parts || []);
|
|
1467
|
+
const parsed = parseGeminiParts(parts);
|
|
1468
|
+
const text = parsed.text.trim();
|
|
1469
|
+
if (typeof onDelta === 'function' && text) onDelta(text);
|
|
1470
|
+
return {
|
|
1471
|
+
text,
|
|
1472
|
+
toolCalls: parsed.toolCalls,
|
|
1473
|
+
rawParts: parsed.rawParts,
|
|
1474
|
+
usage: { uncachedInputTokens: Math.max(0, input - cached), cachedInputTokens: cached, outputTokens: Number(u.candidatesTokenCount || 0), providerTotalTokens: Number(u.totalTokenCount || 0) },
|
|
1475
|
+
durationMs: r.durationMs,
|
|
1476
|
+
responseId: json.responseId || null,
|
|
1477
|
+
};
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
const { json, durationMs } = await fetchJson(url, fetchOpts, fetchImpl);
|
|
1481
|
+
const u = json.usageMetadata || {};
|
|
1482
|
+
const cached = Number(u.cachedContentTokenCount || 0);
|
|
1483
|
+
const input = Number(u.promptTokenCount || 0);
|
|
1484
|
+
const parts = (json.candidates || []).flatMap((c) => c.content?.parts || []);
|
|
1485
|
+
const parsed = parseGeminiParts(parts);
|
|
1486
|
+
const text = parsed.text.trim();
|
|
1487
|
+
if (typeof onDelta === 'function' && text) onDelta(text);
|
|
1488
|
+
return {
|
|
1489
|
+
text,
|
|
1490
|
+
toolCalls: parsed.toolCalls,
|
|
1491
|
+
rawParts: parsed.rawParts,
|
|
1492
|
+
usage: { uncachedInputTokens: Math.max(0, input - cached), cachedInputTokens: cached, outputTokens: Number(u.candidatesTokenCount || 0), providerTotalTokens: Number(u.totalTokenCount || 0) },
|
|
1493
|
+
durationMs,
|
|
1494
|
+
responseId: json.responseId || null,
|
|
1495
|
+
};
|
|
1496
|
+
};
|
|
1497
|
+
|
|
1498
|
+
if (store && store.listProfiles({ provider: providerId }).length > 0) {
|
|
1499
|
+
const { result, profileId } = await store.withFailover(providerId, execWithCred);
|
|
1500
|
+
return { ...result, authProfileId: profileId };
|
|
1501
|
+
}
|
|
1502
|
+
return execWithCred(null);
|
|
1503
|
+
},
|
|
1504
|
+
};
|
|
1505
|
+
}
|
|
1506
|
+
|
|
1507
|
+
// ── Grok CLI (Grok Build) — v1.6 ─────────────────────────────────────────────
|
|
1508
|
+
export function grokArgs({ model, effort, sandbox, maxTurns } = {}) {
|
|
1509
|
+
const args = ['--no-auto-update', '--output-format', 'json', '--max-turns', String(maxTurns || 30), '--sandbox', 'workspace'];
|
|
1510
|
+
if (effort) args.push('--effort', String(effort));
|
|
1511
|
+
if (model) args.push('-m', model);
|
|
1512
|
+
if (sandbox === 'write') {
|
|
1513
|
+
args.push('--permission-mode', 'acceptEdits');
|
|
1514
|
+
} else {
|
|
1515
|
+
args.push('--permission-mode', 'dontAsk');
|
|
1516
|
+
for (const a of ['Read', 'Grep', 'Glob', 'Bash']) args.push('--allow', a);
|
|
1517
|
+
for (const d of ['Write', 'Edit', 'Bash(rm *)', 'Bash(git push*)', 'Bash(sudo *)']) args.push('--deny', d);
|
|
1518
|
+
}
|
|
1519
|
+
return args;
|
|
1520
|
+
}
|
|
1521
|
+
function makeGrokProvider({ model } = {}) {
|
|
1522
|
+
const bin = process.env.GROK_CLI || 'grok';
|
|
1523
|
+
return {
|
|
1524
|
+
name: 'grok-cli', model: model || process.env.GROK_MODEL || 'grok-4.5',
|
|
1525
|
+
supportsTools: false,
|
|
1526
|
+
ready: Boolean(commandExists(bin)), missing: `grok CLI not found (${bin})`,
|
|
1527
|
+
async generate(prompt, { cwd, model: m, effort, sandbox, maxTurns, timeoutMs } = {}) {
|
|
1528
|
+
const args = [...grokArgs({ model: m || this.model, effort, sandbox, maxTurns }), '-p', prompt];
|
|
1529
|
+
const { stdout, durationMs } = runCommand(bin, args, { cwd, timeoutMs: timeoutMs || 900000 });
|
|
1530
|
+
let j = null;
|
|
1531
|
+
try { j = JSON.parse(stdout); } catch { j = parseJsonLines(stdout).at(-1) || null; }
|
|
1532
|
+
const cand = j && (j.result ?? j.text ?? j.response ?? j.output ?? j.final ?? (typeof j.message === 'string' ? j.message : null));
|
|
1533
|
+
const text = typeof cand === 'string' && cand ? cand : stdout.trim();
|
|
1534
|
+
const u = (j && j.usage) || {};
|
|
1535
|
+
const input = Number(u.input_tokens || u.prompt_tokens || 0), cached = Number(u.cached_input_tokens || u.cache_read_input_tokens || 0), out = Number(u.output_tokens || u.completion_tokens || 0);
|
|
1536
|
+
return {
|
|
1537
|
+
text,
|
|
1538
|
+
toolCalls: [],
|
|
1539
|
+
usage: (input || out)
|
|
1540
|
+
? { uncachedInputTokens: Math.max(0, input - cached), cachedInputTokens: cached, outputTokens: out, providerTotalTokens: input + out }
|
|
1541
|
+
: { uncachedInputTokens: approxTokens(prompt), cachedInputTokens: 0, outputTokens: approxTokens(text), providerTotalTokens: approxTokens(prompt) + approxTokens(text) },
|
|
1542
|
+
durationMs, responseId: (j && (j.sessionId || j.session_id || j.id)) || null,
|
|
1543
|
+
};
|
|
1544
|
+
},
|
|
1545
|
+
};
|
|
1546
|
+
}
|
|
1547
|
+
|
|
1548
|
+
const FACTORIES = {
|
|
1549
|
+
mock: makeMockProvider,
|
|
1550
|
+
'codex-app-server': makeCodexAppServerProvider,
|
|
1551
|
+
'openai-compatible': makeOpenAICompatibleProvider,
|
|
1552
|
+
codex: makeCodexProvider,
|
|
1553
|
+
claude: makeClaudeProvider,
|
|
1554
|
+
openai: makeOpenAIProvider,
|
|
1555
|
+
anthropic: makeAnthropicProvider,
|
|
1556
|
+
gemini: makeGeminiProvider,
|
|
1557
|
+
grok: makeGrokProvider,
|
|
1558
|
+
};
|
|
1559
|
+
|
|
1560
|
+
export function pickProvider(name = 'auto', opts = {}) {
|
|
1561
|
+
if (name && name !== 'auto') {
|
|
1562
|
+
const f = FACTORIES[name];
|
|
1563
|
+
if (!f) throw new Error(`unknown provider "${name}" (have: ${Object.keys(FACTORIES).join(', ')})`);
|
|
1564
|
+
return f(opts);
|
|
1565
|
+
}
|
|
1566
|
+
const order = ['openai', 'anthropic', 'gemini', 'codex-app-server', 'codex', 'claude'];
|
|
1567
|
+
for (const n of order) { const p = FACTORIES[n](opts); if (p.ready) return p; }
|
|
1568
|
+
const codex = FACTORIES.codex(opts);
|
|
1569
|
+
return codex;
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
export { approxTokens };
|