kovence-cli 0.0.0-stage → 1.0.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/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +101 -2
- package/SECURITY.md +13 -0
- package/nd.js +700 -0
- package/package.json +55 -5
- package/providers.js +530 -0
- package/src/agent/history.js +157 -0
- package/src/agent/project_context.js +136 -0
- package/src/agent/resilience.js +100 -0
- package/src/agent/turn.js +782 -0
- package/src/cli/args.js +171 -0
- package/src/cli/sinks.js +326 -0
- package/src/commands/handlers.js +1280 -0
- package/src/config/memory.js +175 -0
- package/src/config/model_registry.js +123 -0
- package/src/config/paths.js +268 -0
- package/src/config/providers.js +209 -0
- package/src/config/sessions.js +201 -0
- package/src/permissions/engine.js +846 -0
- package/src/permissions/store.js +113 -0
- package/src/settings/schema.js +340 -0
- package/src/settings/store.js +136 -0
- package/src/tools/checkpoints.js +108 -0
- package/src/tools/download.js +513 -0
- package/src/tools/exec.js +155 -0
- package/src/tools/fs_walk.js +165 -0
- package/src/tools/net.js +467 -0
- package/src/tools/patch_git.js +205 -0
- package/src/tools/sudo.js +212 -0
- package/src/tools/tasks.js +127 -0
- package/src/tui/settings_panel.js +223 -0
- package/src/tui/text_width.js +218 -0
- package/src/tui/themes.js +143 -0
- package/store.js +107 -0
- package/tools.js +1267 -0
- package/tui.js +1634 -0
- package/ui.js +394 -0
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "kovence-cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Kovence CLI — Fast zero-dependency multi-step AI coding-agent CLI harness",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://kovence.com",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"cli",
|
|
9
|
+
"ai",
|
|
10
|
+
"coding-agent",
|
|
11
|
+
"llm",
|
|
12
|
+
"tui",
|
|
13
|
+
"openai",
|
|
14
|
+
"anthropic",
|
|
15
|
+
"kovence"
|
|
16
|
+
],
|
|
17
|
+
"main": "nd.js",
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=20.12.0"
|
|
20
|
+
},
|
|
21
|
+
"bin": {
|
|
22
|
+
"kovence-cli": "nd.js",
|
|
23
|
+
"kovence": "nd.js"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "node --test",
|
|
30
|
+
"test:coverage": "node --test --experimental-test-coverage",
|
|
31
|
+
"check": "node scripts/check.js",
|
|
32
|
+
"lint": "eslint .",
|
|
33
|
+
"format:check": "prettier --check .",
|
|
34
|
+
"typecheck": "tsc --noEmit --allowJs --checkJs",
|
|
35
|
+
"prepublishOnly": "npm run check && npm run lint && npm run typecheck && npm run format:check && npm test"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"nd.js",
|
|
39
|
+
"store.js",
|
|
40
|
+
"ui.js",
|
|
41
|
+
"tools.js",
|
|
42
|
+
"providers.js",
|
|
43
|
+
"tui.js",
|
|
44
|
+
"src/",
|
|
45
|
+
"README.md",
|
|
46
|
+
"CHANGELOG.md",
|
|
47
|
+
"LICENSE",
|
|
48
|
+
"SECURITY.md"
|
|
49
|
+
],
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/node": "^26.6.6",
|
|
52
|
+
"eslint": "^10.15.0",
|
|
53
|
+
"prettier": "^3.10.1",
|
|
54
|
+
"typescript": "^7.0.2"
|
|
55
|
+
}
|
|
56
|
+
}
|
package/providers.js
ADDED
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { lookupModelCapabilities } = require("./src/config/model_registry");
|
|
4
|
+
|
|
5
|
+
function buildProviderUrls(provider) {
|
|
6
|
+
const base = String(provider?.baseUrl || "").replace(/\/+$/, "");
|
|
7
|
+
if (!base) throw new Error("Provider baseUrl is not configured.");
|
|
8
|
+
if (provider.type === "anthropic") {
|
|
9
|
+
if (base.endsWith("/messages")) {
|
|
10
|
+
return { chatUrl: base, modelsUrl: base.replace(/\/messages$/, "/models") };
|
|
11
|
+
}
|
|
12
|
+
const v1 = base.endsWith("/v1") ? base : `${base}/v1`;
|
|
13
|
+
return { chatUrl: `${v1}/messages`, modelsUrl: `${v1}/models` };
|
|
14
|
+
}
|
|
15
|
+
if (base.endsWith("/chat/completions")) {
|
|
16
|
+
return { chatUrl: base, modelsUrl: base.replace(/\/chat\/completions$/, "/models") };
|
|
17
|
+
}
|
|
18
|
+
const v1 = base.endsWith("/v1") ? base : `${base}/v1`;
|
|
19
|
+
return { chatUrl: `${v1}/chat/completions`, modelsUrl: `${v1}/models` };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function buildProviderHeaders(provider) {
|
|
23
|
+
if (provider.type === "anthropic") {
|
|
24
|
+
const h = {
|
|
25
|
+
"Content-Type": "application/json",
|
|
26
|
+
"anthropic-version": "2023-06-01",
|
|
27
|
+
};
|
|
28
|
+
if (provider.apiKey) h["x-api-key"] = provider.apiKey;
|
|
29
|
+
return h;
|
|
30
|
+
}
|
|
31
|
+
const h = { "Content-Type": "application/json" };
|
|
32
|
+
if (provider.apiKey) h.Authorization = `Bearer ${provider.apiKey}`;
|
|
33
|
+
return h;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Convert OpenAI-style messages into Anthropic Messages API shape while guaranteeing valid user-first turns.
|
|
38
|
+
*/
|
|
39
|
+
function toAnthropicMessages(messages) {
|
|
40
|
+
const systemParts = [];
|
|
41
|
+
const out = [];
|
|
42
|
+
let pendingToolResults = [];
|
|
43
|
+
|
|
44
|
+
const flushTools = () => {
|
|
45
|
+
if (!pendingToolResults.length) return;
|
|
46
|
+
out.push({ role: "user", content: pendingToolResults });
|
|
47
|
+
pendingToolResults = [];
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
for (const m of messages) {
|
|
51
|
+
if (m.role === "system") {
|
|
52
|
+
if (m.content) systemParts.push(m.content);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (m.role === "tool") {
|
|
56
|
+
pendingToolResults.push({
|
|
57
|
+
type: "tool_result",
|
|
58
|
+
tool_use_id: m.tool_call_id || "tool_0",
|
|
59
|
+
content: String(m.content || ""),
|
|
60
|
+
});
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
flushTools();
|
|
64
|
+
if (m.role === "assistant") {
|
|
65
|
+
const blocks = [];
|
|
66
|
+
if (m.content) blocks.push({ type: "text", text: String(m.content) });
|
|
67
|
+
if (Array.isArray(m.tool_calls)) {
|
|
68
|
+
for (const tc of m.tool_calls) {
|
|
69
|
+
let inputObj = {};
|
|
70
|
+
try {
|
|
71
|
+
inputObj = JSON.parse(tc.function?.arguments || "{}");
|
|
72
|
+
} catch {}
|
|
73
|
+
blocks.push({
|
|
74
|
+
type: "tool_use",
|
|
75
|
+
id: tc.id || `tool_${blocks.length}`,
|
|
76
|
+
name: tc.function?.name || "unknown",
|
|
77
|
+
input: inputObj,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (blocks.length) out.push({ role: "assistant", content: blocks });
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const prev = out[out.length - 1];
|
|
85
|
+
if (prev && prev.role === "user" && typeof prev.content === "string") {
|
|
86
|
+
prev.content = `${prev.content}\n\n${String(m.content || "")}`.trim();
|
|
87
|
+
} else {
|
|
88
|
+
out.push({ role: "user", content: String(m.content || "") });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
flushTools();
|
|
92
|
+
|
|
93
|
+
// Anthropic API requires the first non-system message to have role 'user'.
|
|
94
|
+
while (out.length > 0 && out[0].role !== "user") {
|
|
95
|
+
out.shift();
|
|
96
|
+
}
|
|
97
|
+
if (out.length === 0) {
|
|
98
|
+
out.push({ role: "user", content: "Continue." });
|
|
99
|
+
}
|
|
100
|
+
return { system: systemParts.join("\n\n"), messages: out };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function toAnthropicTools(toolDefinitions) {
|
|
104
|
+
return toolDefinitions.map((t) => ({
|
|
105
|
+
name: t.function.name,
|
|
106
|
+
description: t.function.description,
|
|
107
|
+
input_schema: t.function.parameters || { type: "object", properties: {} },
|
|
108
|
+
}));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Per-(provider, model) capability cache so an error on one model never disables tools globally.
|
|
113
|
+
*/
|
|
114
|
+
function getModelCapabilities(capabilitiesStore, provider, model) {
|
|
115
|
+
if (!capabilitiesStore) {
|
|
116
|
+
return { supportsStreamOptions: true, supportsTools: true };
|
|
117
|
+
}
|
|
118
|
+
if (typeof capabilitiesStore.supportsStreamOptions === "boolean" && !capabilitiesStore._byModel) {
|
|
119
|
+
// Legacy flat capability object compatibility
|
|
120
|
+
return capabilitiesStore;
|
|
121
|
+
}
|
|
122
|
+
if (!capabilitiesStore._byModel) capabilitiesStore._byModel = new Map();
|
|
123
|
+
const key = `${provider?.name || "default"}::${model || provider?.defaultModel || "default"}`;
|
|
124
|
+
if (!capabilitiesStore._byModel.has(key)) {
|
|
125
|
+
capabilitiesStore._byModel.set(key, { supportsStreamOptions: true, supportsTools: true });
|
|
126
|
+
}
|
|
127
|
+
return capabilitiesStore._byModel.get(key);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function isRetryableStatus(status) {
|
|
131
|
+
return status === 429 || status === 502 || status === 503 || status === 504 || status === 529;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function isTransientNetworkError(err) {
|
|
135
|
+
if (!err) return false;
|
|
136
|
+
const msg = `${err.name || ""} ${err.code || ""} ${err.message || ""}`;
|
|
137
|
+
return /ECONNRESET|ETIMEDOUT|ECONNREFUSED|EAI_AGAIN|EPIPE|fetch failed|network/i.test(msg);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function sleepAbortable(ms, signal) {
|
|
141
|
+
return new Promise((resolve, reject) => {
|
|
142
|
+
if (signal?.aborted) {
|
|
143
|
+
reject(signal.reason || new Error("Aborted"));
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const timer = setTimeout(() => {
|
|
147
|
+
signal?.removeEventListener("abort", onAbort);
|
|
148
|
+
resolve();
|
|
149
|
+
}, ms);
|
|
150
|
+
const onAbort = () => {
|
|
151
|
+
clearTimeout(timer);
|
|
152
|
+
reject(signal.reason || new Error("Aborted"));
|
|
153
|
+
};
|
|
154
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function createLinkedController(parentSignal, timeoutMs) {
|
|
159
|
+
const controller = new AbortController();
|
|
160
|
+
let timer = null;
|
|
161
|
+
if (Number.isFinite(timeoutMs) && timeoutMs > 0) {
|
|
162
|
+
timer = setTimeout(() => controller.abort(new Error(`Request timed out after ${timeoutMs}ms`)), timeoutMs);
|
|
163
|
+
}
|
|
164
|
+
const onParentAbort = () => controller.abort(parentSignal.reason || new Error("Aborted"));
|
|
165
|
+
if (parentSignal) {
|
|
166
|
+
if (parentSignal.aborted) {
|
|
167
|
+
controller.abort(parentSignal.reason || new Error("Aborted"));
|
|
168
|
+
} else {
|
|
169
|
+
parentSignal.addEventListener("abort", onParentAbort, { once: true });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
signal: controller.signal,
|
|
174
|
+
cleanup: () => {
|
|
175
|
+
if (timer) clearTimeout(timer);
|
|
176
|
+
parentSignal?.removeEventListener("abort", onParentAbort);
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function listProviderModels(provider, { timeoutMs = 15000, signal = null } = {}) {
|
|
182
|
+
const { modelsUrl } = buildProviderUrls(provider);
|
|
183
|
+
const linked = createLinkedController(signal, timeoutMs);
|
|
184
|
+
try {
|
|
185
|
+
const res = await fetch(modelsUrl, { headers: buildProviderHeaders(provider), signal: linked.signal });
|
|
186
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
|
|
187
|
+
const data = await res.json();
|
|
188
|
+
return (data.data || []).map((m) => m.id).filter(Boolean);
|
|
189
|
+
} finally {
|
|
190
|
+
linked.cleanup();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const STREAM_OPTIONS_UNSUPPORTED_RE =
|
|
195
|
+
/\b(stream_options|include_usage)\b.*(unsupported|unknown|unrecognized|not allowed|invalid|not supported)/i;
|
|
196
|
+
const TOOLS_UNSUPPORTED_RE =
|
|
197
|
+
/(does not support|not support|unsupported|unavailable for|cannot use).{0,40}\b(tools?|function[ _]call|tool_choice)\b|\b(tools?|tool_choice)\b.{0,40}(is|are) not supported/i;
|
|
198
|
+
|
|
199
|
+
async function fetchWithAdapter({
|
|
200
|
+
provider,
|
|
201
|
+
model,
|
|
202
|
+
settings,
|
|
203
|
+
bodyBase,
|
|
204
|
+
signal,
|
|
205
|
+
capabilities,
|
|
206
|
+
toolDefinitions,
|
|
207
|
+
timeoutMs = 120000,
|
|
208
|
+
maxRetries = 3,
|
|
209
|
+
}) {
|
|
210
|
+
const { chatUrl } = buildProviderUrls(provider);
|
|
211
|
+
const headers = buildProviderHeaders(provider);
|
|
212
|
+
const isAnthropic = provider.type === "anthropic";
|
|
213
|
+
const modelCaps = getModelCapabilities(capabilities, provider, model);
|
|
214
|
+
const registryCaps = lookupModelCapabilities(model);
|
|
215
|
+
|
|
216
|
+
let lastRes = null;
|
|
217
|
+
let downgradeAttempts = 0;
|
|
218
|
+
|
|
219
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
220
|
+
if (signal?.aborted) throw signal.reason || new Error("Aborted");
|
|
221
|
+
|
|
222
|
+
let payload;
|
|
223
|
+
if (isAnthropic) {
|
|
224
|
+
const conv = toAnthropicMessages(bodyBase.messages);
|
|
225
|
+
payload = {
|
|
226
|
+
model,
|
|
227
|
+
messages: conv.messages,
|
|
228
|
+
max_tokens: bodyBase.max_tokens || settings?.maxTokens || 4096,
|
|
229
|
+
stream: Boolean(bodyBase.stream),
|
|
230
|
+
};
|
|
231
|
+
if (conv.system) payload.system = conv.system;
|
|
232
|
+
if (!bodyBase._noTools && settings?.toolsEnabled && modelCaps.supportsTools) {
|
|
233
|
+
payload.tools = toAnthropicTools(toolDefinitions);
|
|
234
|
+
}
|
|
235
|
+
if (
|
|
236
|
+
registryCaps.supportsTemperature !== false &&
|
|
237
|
+
Number.isFinite(bodyBase.temperature ?? settings?.temperature)
|
|
238
|
+
) {
|
|
239
|
+
payload.temperature = bodyBase.temperature ?? settings.temperature;
|
|
240
|
+
}
|
|
241
|
+
} else {
|
|
242
|
+
payload = {
|
|
243
|
+
model,
|
|
244
|
+
messages: bodyBase.messages,
|
|
245
|
+
stream: Boolean(bodyBase.stream),
|
|
246
|
+
};
|
|
247
|
+
if (payload.stream && modelCaps.supportsStreamOptions) {
|
|
248
|
+
payload.stream_options = { include_usage: true };
|
|
249
|
+
}
|
|
250
|
+
if (
|
|
251
|
+
!bodyBase._noTools &&
|
|
252
|
+
settings?.toolsEnabled &&
|
|
253
|
+
modelCaps.supportsTools &&
|
|
254
|
+
registryCaps.supportsTools !== false
|
|
255
|
+
) {
|
|
256
|
+
payload.tools = toolDefinitions;
|
|
257
|
+
payload.tool_choice = "auto";
|
|
258
|
+
}
|
|
259
|
+
if (
|
|
260
|
+
registryCaps.supportsTemperature !== false &&
|
|
261
|
+
Number.isFinite(bodyBase.temperature ?? settings?.temperature)
|
|
262
|
+
) {
|
|
263
|
+
payload.temperature = bodyBase.temperature ?? settings.temperature;
|
|
264
|
+
}
|
|
265
|
+
if (Number.isFinite(bodyBase.max_tokens ?? settings?.maxTokens)) {
|
|
266
|
+
payload.max_tokens = bodyBase.max_tokens ?? settings.maxTokens;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const linked = createLinkedController(signal, timeoutMs);
|
|
271
|
+
let res;
|
|
272
|
+
try {
|
|
273
|
+
res = await fetch(chatUrl, {
|
|
274
|
+
method: "POST",
|
|
275
|
+
headers,
|
|
276
|
+
body: JSON.stringify(payload),
|
|
277
|
+
signal: linked.signal,
|
|
278
|
+
});
|
|
279
|
+
} catch (err) {
|
|
280
|
+
linked.cleanup();
|
|
281
|
+
if (signal?.aborted) throw err;
|
|
282
|
+
if (attempt < maxRetries && (isTransientNetworkError(err) || /timed out/i.test(err.message))) {
|
|
283
|
+
const backoff = Math.min(8000, 600 * 2 ** attempt) + Math.floor(Math.random() * 250);
|
|
284
|
+
await sleepAbortable(backoff, signal);
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
throw err;
|
|
288
|
+
}
|
|
289
|
+
linked.cleanup();
|
|
290
|
+
|
|
291
|
+
if (res.ok) return res;
|
|
292
|
+
lastRes = res;
|
|
293
|
+
|
|
294
|
+
// Transient HTTP errors: retry with Retry-After or jittered exponential backoff
|
|
295
|
+
if (isRetryableStatus(res.status) && attempt < maxRetries) {
|
|
296
|
+
await res.text().catch(() => "");
|
|
297
|
+
const retryAfterRaw = Number(res.headers.get("retry-after"));
|
|
298
|
+
const backoff =
|
|
299
|
+
Number.isFinite(retryAfterRaw) && retryAfterRaw > 0 && retryAfterRaw <= 30
|
|
300
|
+
? retryAfterRaw * 1000
|
|
301
|
+
: Math.min(10000, 800 * 2 ** attempt) + Math.floor(Math.random() * 300);
|
|
302
|
+
await sleepAbortable(backoff, signal);
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (res.status !== 400 && res.status !== 422) {
|
|
307
|
+
throw new Error(`HTTP ${res.status}: ${await res.text()}`);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const errText = await res.text();
|
|
311
|
+
let downgraded = false;
|
|
312
|
+
if (downgradeAttempts < 2) {
|
|
313
|
+
if (!isAnthropic && modelCaps.supportsStreamOptions && STREAM_OPTIONS_UNSUPPORTED_RE.test(errText)) {
|
|
314
|
+
modelCaps.supportsStreamOptions = false;
|
|
315
|
+
downgraded = true;
|
|
316
|
+
}
|
|
317
|
+
if (modelCaps.supportsTools && TOOLS_UNSUPPORTED_RE.test(errText)) {
|
|
318
|
+
modelCaps.supportsTools = false;
|
|
319
|
+
downgraded = true;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (!downgraded) throw new Error(`HTTP ${res.status}: ${errText}`);
|
|
323
|
+
downgradeAttempts += 1;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
throw new Error(
|
|
327
|
+
`HTTP ${lastRes?.status || "error"}: ${lastRes ? await lastRes.text().catch(() => "") : "Exhausted retries"}`
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
async function streamProviderTurn({
|
|
332
|
+
provider,
|
|
333
|
+
model,
|
|
334
|
+
settings,
|
|
335
|
+
messages,
|
|
336
|
+
signal,
|
|
337
|
+
sink,
|
|
338
|
+
capabilities,
|
|
339
|
+
toolDefinitions,
|
|
340
|
+
timeoutMs = 120000,
|
|
341
|
+
}) {
|
|
342
|
+
const startedAt = Date.now();
|
|
343
|
+
const streamUi = sink.startThinking("Kovence: ", "Thinking");
|
|
344
|
+
let res;
|
|
345
|
+
try {
|
|
346
|
+
res = await fetchWithAdapter({
|
|
347
|
+
provider,
|
|
348
|
+
model,
|
|
349
|
+
settings,
|
|
350
|
+
bodyBase: { messages, stream: true },
|
|
351
|
+
signal,
|
|
352
|
+
capabilities,
|
|
353
|
+
toolDefinitions,
|
|
354
|
+
timeoutMs,
|
|
355
|
+
});
|
|
356
|
+
if (!res.body) throw new Error("Empty response body from API");
|
|
357
|
+
} catch (err) {
|
|
358
|
+
streamUi.stop(false);
|
|
359
|
+
throw err;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
let firstTokenAt = null;
|
|
363
|
+
const reader = res.body.getReader();
|
|
364
|
+
const decoder = new TextDecoder();
|
|
365
|
+
let buffer = "";
|
|
366
|
+
let fullText = "";
|
|
367
|
+
let printedHeader = false;
|
|
368
|
+
let usage = null;
|
|
369
|
+
let finishReason = null;
|
|
370
|
+
const toolMap = new Map();
|
|
371
|
+
const isAnthropic = provider.type === "anthropic";
|
|
372
|
+
|
|
373
|
+
const markFirstToken = (forText) => {
|
|
374
|
+
if (firstTokenAt === null) firstTokenAt = Date.now();
|
|
375
|
+
if (!printedHeader) {
|
|
376
|
+
streamUi.stop(forText);
|
|
377
|
+
printedHeader = true;
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
const mergeOpenAITools = (deltas) => {
|
|
382
|
+
for (const d of deltas) {
|
|
383
|
+
const idx = d.index ?? 0;
|
|
384
|
+
if (!toolMap.has(idx)) {
|
|
385
|
+
toolMap.set(idx, {
|
|
386
|
+
id: d.id || `call_${idx}`,
|
|
387
|
+
type: "function",
|
|
388
|
+
function: { name: "", arguments: "" },
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
const entry = toolMap.get(idx);
|
|
392
|
+
if (d.id) entry.id = d.id;
|
|
393
|
+
if (d.function?.name) entry.function.name += d.function.name;
|
|
394
|
+
if (d.function?.arguments) entry.function.arguments += d.function.arguments;
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
const handleLine = (rawLine) => {
|
|
399
|
+
const line = rawLine.replace(/\r$/, "");
|
|
400
|
+
if (!line.startsWith("data:")) return;
|
|
401
|
+
const data = line.slice(5).trim();
|
|
402
|
+
if (!data || data === "[DONE]") return;
|
|
403
|
+
let json;
|
|
404
|
+
try {
|
|
405
|
+
json = JSON.parse(data);
|
|
406
|
+
} catch {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (json.error) throw new Error(json.error.message || JSON.stringify(json.error));
|
|
410
|
+
|
|
411
|
+
if (isAnthropic) {
|
|
412
|
+
if (json.type === "message_start" && json.message?.usage) {
|
|
413
|
+
usage = {
|
|
414
|
+
prompt_tokens: json.message.usage.input_tokens || 0,
|
|
415
|
+
completion_tokens: json.message.usage.output_tokens || 0,
|
|
416
|
+
};
|
|
417
|
+
} else if (json.type === "message_delta") {
|
|
418
|
+
if (json.delta?.stop_reason) finishReason = json.delta.stop_reason;
|
|
419
|
+
if (json.usage) {
|
|
420
|
+
usage = {
|
|
421
|
+
prompt_tokens: usage?.prompt_tokens || 0,
|
|
422
|
+
completion_tokens: json.usage.output_tokens ?? usage?.completion_tokens ?? 0,
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
} else if (json.type === "content_block_start" && json.content_block?.type === "tool_use") {
|
|
426
|
+
markFirstToken(false);
|
|
427
|
+
const idx = json.index ?? toolMap.size;
|
|
428
|
+
toolMap.set(idx, {
|
|
429
|
+
id: json.content_block.id || `call_${idx}`,
|
|
430
|
+
type: "function",
|
|
431
|
+
function: { name: json.content_block.name || "", arguments: "" },
|
|
432
|
+
});
|
|
433
|
+
} else if (json.type === "content_block_delta") {
|
|
434
|
+
if (json.delta?.type === "text_delta" && json.delta.text) {
|
|
435
|
+
markFirstToken(true);
|
|
436
|
+
streamUi.write(json.delta.text);
|
|
437
|
+
fullText += json.delta.text;
|
|
438
|
+
} else if (json.delta?.type === "input_json_delta" && json.delta.partial_json) {
|
|
439
|
+
markFirstToken(false);
|
|
440
|
+
const idx = json.index ?? 0;
|
|
441
|
+
const entry = toolMap.get(idx);
|
|
442
|
+
if (entry) entry.function.arguments += json.delta.partial_json;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
if (json.usage) usage = json.usage;
|
|
449
|
+
const choice = json.choices?.[0];
|
|
450
|
+
if (choice?.finish_reason) finishReason = choice.finish_reason;
|
|
451
|
+
const delta = choice?.delta;
|
|
452
|
+
if (delta?.tool_calls?.length) {
|
|
453
|
+
markFirstToken(false);
|
|
454
|
+
mergeOpenAITools(delta.tool_calls);
|
|
455
|
+
}
|
|
456
|
+
const token = delta?.content ?? json.response ?? "";
|
|
457
|
+
if (token) {
|
|
458
|
+
markFirstToken(true);
|
|
459
|
+
streamUi.write(token);
|
|
460
|
+
fullText += token;
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
try {
|
|
465
|
+
while (true) {
|
|
466
|
+
const { done, value } = await reader.read();
|
|
467
|
+
if (done) break;
|
|
468
|
+
buffer += decoder.decode(value, { stream: true });
|
|
469
|
+
const lines = buffer.split("\n");
|
|
470
|
+
buffer = lines.pop();
|
|
471
|
+
for (const line of lines) handleLine(line);
|
|
472
|
+
}
|
|
473
|
+
if (buffer) handleLine(buffer);
|
|
474
|
+
} finally {
|
|
475
|
+
if (!printedHeader) streamUi.stop(false);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
streamUi.end(Boolean(fullText));
|
|
479
|
+
const finishedAt = Date.now();
|
|
480
|
+
const elapsedMs = finishedAt - startedAt;
|
|
481
|
+
const ttftMs = firstTokenAt !== null ? firstTokenAt - startedAt : elapsedMs;
|
|
482
|
+
const outTokens = usage?.completion_tokens ?? (fullText ? Math.max(1, Math.ceil(fullText.length / 4)) : 0);
|
|
483
|
+
const genSec = Math.max(0.001, (finishedAt - (firstTokenAt || startedAt)) / 1000);
|
|
484
|
+
const tokPerSec = outTokens ? Number((outTokens / genSec).toFixed(1)) : 0;
|
|
485
|
+
|
|
486
|
+
const toolCalls = [...toolMap.entries()].sort((a, b) => a[0] - b[0]).map(([, v]) => v);
|
|
487
|
+
return { content: fullText, toolCalls, usage, finishReason, elapsedMs, ttftMs, tokPerSec, outTokens };
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
async function completeProviderFast({
|
|
491
|
+
provider,
|
|
492
|
+
model,
|
|
493
|
+
messages,
|
|
494
|
+
maxTokens = 300,
|
|
495
|
+
temperature = 0.2,
|
|
496
|
+
signal = null,
|
|
497
|
+
capabilities,
|
|
498
|
+
toolDefinitions = [],
|
|
499
|
+
timeoutMs = 30000,
|
|
500
|
+
}) {
|
|
501
|
+
const res = await fetchWithAdapter({
|
|
502
|
+
provider,
|
|
503
|
+
model,
|
|
504
|
+
settings: null,
|
|
505
|
+
bodyBase: { messages, stream: false, max_tokens: maxTokens, temperature, _noTools: true },
|
|
506
|
+
signal,
|
|
507
|
+
capabilities,
|
|
508
|
+
toolDefinitions,
|
|
509
|
+
timeoutMs,
|
|
510
|
+
maxRetries: 1,
|
|
511
|
+
});
|
|
512
|
+
const data = await res.json();
|
|
513
|
+
if (provider.type === "anthropic") {
|
|
514
|
+
const texts = (data.content || []).filter((b) => b.type === "text").map((b) => b.text);
|
|
515
|
+
return texts.join("").trim();
|
|
516
|
+
}
|
|
517
|
+
return data.choices?.[0]?.message?.content?.trim() || "";
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
module.exports = {
|
|
521
|
+
buildProviderUrls,
|
|
522
|
+
buildProviderHeaders,
|
|
523
|
+
toAnthropicMessages,
|
|
524
|
+
getModelCapabilities,
|
|
525
|
+
isRetryableStatus,
|
|
526
|
+
listProviderModels,
|
|
527
|
+
fetchWithAdapter,
|
|
528
|
+
streamProviderTurn,
|
|
529
|
+
completeProviderFast,
|
|
530
|
+
};
|