aegiscode 6.7.8 → 6.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -97
- package/bin/aegiscode.js +24 -0
- package/package.json +1 -1
- package/scripts/predist.mjs +11 -16
- package/src/app.js +8 -63
- package/src/commands.js +24 -189
- package/src/config.js +0 -14
- package/src/deps.js +3 -16
- package/src/engine.js +14 -105
- package/src/screens.js +51 -0
- package/src/sharedpaths.js +4 -7
- package/vendor/client/aegis.js +45 -2
- package/vendor/client/env-file.js +182 -0
- package/vendor/desktop/lib/local/engine.js +225 -188
- package/vendor/desktop/lib/local/local.js +433 -0
- package/vendor/desktop/lib/local/tools.js +7 -56
- package/vendor/desktop/lib/settings.js +28 -31
- package/vendor/desktop/renderer/usage.js +4 -4
- package/src/custommodels.js +0 -258
- package/vendor/desktop/lib/local/endpoints.js +0 -228
- package/vendor/desktop/lib/local/providers.js +0 -679
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* local.js — the local-model transport: run a model on hardware you own.
|
|
5
|
+
*
|
|
6
|
+
* This is the third class, alongside `aegis` (pooled, metered) and `byok`
|
|
7
|
+
* (relayed, handling fee). It is also the one class that bills NOTHING, which
|
|
8
|
+
* is exactly why it is fenced the way it is.
|
|
9
|
+
*
|
|
10
|
+
* The fee argument, restated because every rule below follows from it. A
|
|
11
|
+
* transport that dials a user-supplied URL has no pooled margin to take, no
|
|
12
|
+
* handling fee to charge and no account key attached, so a REMOTE call there is
|
|
13
|
+
* an unpaid turn: the relay accepts a fixed catalog of provider ids
|
|
14
|
+
* (aegis1 services/nexus_provider/catalog.py, upstream key from
|
|
15
|
+
* `X-Provider-Key`), so an arbitrary remote URL has nothing to be billed
|
|
16
|
+
* against even if a client wanted to invoice it. That is why remote direct
|
|
17
|
+
* dialing is gone from this codebase and stays gone — there is deliberately no
|
|
18
|
+
* flag or env var that re-opens it, because such a flag would be a billing
|
|
19
|
+
* bypass.
|
|
20
|
+
*
|
|
21
|
+
* A model on YOUR OWN MACHINE is the case that argument never covered. There is
|
|
22
|
+
* no vendor to pay, so there is nothing to bill and nobody being denied a
|
|
23
|
+
* payment: the compute was already bought and is sitting idle. `aegis` and
|
|
24
|
+
* `byok` are lanes to somebody else's GPU; this is the lane to your own.
|
|
25
|
+
*
|
|
26
|
+
* So the fence is the whole feature: `isLocalEndpoint()` is checked before any
|
|
27
|
+
* byte goes out, and it FAILS CLOSED — anything that does not parse, carries no
|
|
28
|
+
* host, or resolves to a public address is remote, i.e. refused. A false
|
|
29
|
+
* "local" is an unpaid turn; a false "remote" is a refusal the user fixes by
|
|
30
|
+
* pointing at a local address. In a tie, refuse.
|
|
31
|
+
*
|
|
32
|
+
* Wire-wise this speaks Ollama's OpenAI-compatible surface
|
|
33
|
+
* (`POST /v1/chat/completions`, keyless) plus its native `GET /api/tags` for
|
|
34
|
+
* the model list. Anything that serves an OpenAI-compatible endpoint on the
|
|
35
|
+
* same box — llama.cpp, LM Studio, vLLM — works by pointing `baseURL` at it;
|
|
36
|
+
* only the model LIST is Ollama-specific, and that failure is recoverable
|
|
37
|
+
* (see listTags) rather than fatal.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/** Where a local daemon listens by default (Ollama's stock port). */
|
|
41
|
+
const DEFAULT_BASE = 'http://localhost:11434';
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* How long a local turn tolerates a silent stream before failing. Local
|
|
45
|
+
* inference is SLOW to first token — a cold 7B model on a laptop CPU can think
|
|
46
|
+
* for a minute before emitting anything, and a warm one still loads weights on
|
|
47
|
+
* the first call after a restart — so this is generous where the cloud client's
|
|
48
|
+
* 60s default would abort a perfectly healthy turn.
|
|
49
|
+
*/
|
|
50
|
+
const SSE_IDLE_TIMEOUT_MS = 300000;
|
|
51
|
+
|
|
52
|
+
/** How long the daemon probe waits. Short: this runs behind a UI spinner, and
|
|
53
|
+
* "no daemon" must read as an answer rather than as a hang. */
|
|
54
|
+
const PROBE_TIMEOUT_MS = 1500;
|
|
55
|
+
|
|
56
|
+
/** Trim trailing slashes so `${base}/v1/...` never doubles one. */
|
|
57
|
+
function baseOf(baseURL) {
|
|
58
|
+
return String(baseURL || DEFAULT_BASE).replace(/\/+$/, '');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Whether a base URL addresses hardware on this machine (or on this private
|
|
63
|
+
* network) — the gate that decides whether the free lane exists at all.
|
|
64
|
+
*
|
|
65
|
+
* True for: loopback (localhost, `*.localhost`, 127.0.0.0/8, `::1`), RFC1918
|
|
66
|
+
* private ranges (10/8, 172.16/12, 192.168/16), IPv6 unique-local fc00::/7,
|
|
67
|
+
* link-local (169.254/16, fe80::/10), the reserved local suffixes `.local`,
|
|
68
|
+
* `.internal`, `.lan`, and a bare dotless hostname (`ollama`, `gpu-box` — it
|
|
69
|
+
* can only resolve through this machine's own resolver).
|
|
70
|
+
*
|
|
71
|
+
* Private ranges are included on purpose: a box on the user's own LAN is still
|
|
72
|
+
* hardware they already own, so the "no vendor to pay" argument holds there
|
|
73
|
+
* too. What is excluded is anything routable to a third party.
|
|
74
|
+
*
|
|
75
|
+
* FAIL CLOSED: an unparseable value, a value with no host, a non-http(s)
|
|
76
|
+
* scheme, or a public address is NOT local. See the file header for why the
|
|
77
|
+
* tie breaks that way.
|
|
78
|
+
*/
|
|
79
|
+
function isLocalEndpoint(url) {
|
|
80
|
+
let host = '';
|
|
81
|
+
let scheme = '';
|
|
82
|
+
try {
|
|
83
|
+
const parsed = new URL(String(url == null ? '' : url));
|
|
84
|
+
host = parsed.hostname.toLowerCase();
|
|
85
|
+
scheme = parsed.protocol.toLowerCase();
|
|
86
|
+
} catch {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
// Only a base URL a transport can actually dial — checked BEFORE the host, so
|
|
90
|
+
// `ftp://box.local` cannot ride the local suffix to a pass.
|
|
91
|
+
if (scheme !== 'http:' && scheme !== 'https:') return false;
|
|
92
|
+
if (!host) return false;
|
|
93
|
+
// WHATWG keeps the brackets on an IPv6 hostname; strip them so one spelling
|
|
94
|
+
// covers both `[::1]` and a bare `::1`.
|
|
95
|
+
if (host.startsWith('[') && host.endsWith(']')) host = host.slice(1, -1);
|
|
96
|
+
if (!host || /[\s/\\@]/.test(host)) return false;
|
|
97
|
+
|
|
98
|
+
// Loopback by name, plus the mDNS / split-DNS names an on-box service
|
|
99
|
+
// answers to. The suffix must END the name: `example.local.evil.com` is
|
|
100
|
+
// somebody else's host.
|
|
101
|
+
if (host === 'localhost' || host.endsWith('.localhost')) return true;
|
|
102
|
+
if (/\.(local|internal|lan)$/.test(host)) return true;
|
|
103
|
+
|
|
104
|
+
// IPv6 literals.
|
|
105
|
+
if (host.includes(':')) {
|
|
106
|
+
if (host === '::1' || host === '0:0:0:0:0:0:0:1') return true; // loopback
|
|
107
|
+
if (/^f[cd][0-9a-f]{2}:/.test(host)) return true; // fc00::/7 unique-local
|
|
108
|
+
if (/^fe[89ab][0-9a-f]:/.test(host)) return true; // fe80::/10 link-local
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// IPv4 literals.
|
|
113
|
+
const quad = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(host);
|
|
114
|
+
if (quad) {
|
|
115
|
+
const o = quad.slice(1).map(Number);
|
|
116
|
+
if (o.some((n) => n > 255)) return false; // not an address, and not local
|
|
117
|
+
const [a, b] = o;
|
|
118
|
+
if (a === 127) return true; // 127.0.0.0/8 loopback
|
|
119
|
+
if (a === 10) return true; // 10.0.0.0/8
|
|
120
|
+
if (a === 172 && b >= 16 && b <= 31) return true; // 172.16.0.0/12
|
|
121
|
+
if (a === 192 && b === 168) return true; // 192.168.0.0/16
|
|
122
|
+
if (a === 169 && b === 254) return true; // 169.254.0.0/16 link-local
|
|
123
|
+
return false; // a real, routable address — remote, and not this lane
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// A dotless name can only come from this machine's own resolver / hosts file.
|
|
127
|
+
if (!host.includes('.')) return true;
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Refuse a base URL that is not local, in the hosts' own voice.
|
|
133
|
+
*
|
|
134
|
+
* Returns null when the URL is usable. The returned error carries `status 400`
|
|
135
|
+
* so every existing caller (the CLI's error painter, the desktop turn guard)
|
|
136
|
+
* treats it as a user-fixable configuration problem rather than a crash.
|
|
137
|
+
*/
|
|
138
|
+
function remoteRefusal(url) {
|
|
139
|
+
if (isLocalEndpoint(url)) return null;
|
|
140
|
+
const err = new Error(
|
|
141
|
+
`local: ${JSON.stringify(String(url || ''))} is not on this machine, so it is not the local ` +
|
|
142
|
+
'class. Running a model you own is free because there is no vendor to pay — a remote URL ' +
|
|
143
|
+
'here would be an unpaid turn instead. Use the aegis or byok class for remote models.'
|
|
144
|
+
);
|
|
145
|
+
err.status = 400;
|
|
146
|
+
return err;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Probe the local daemon. Never throws — a missing daemon is a state, not an
|
|
150
|
+
* error, because the UI has to say "start it" rather than "it failed". */
|
|
151
|
+
async function probe(baseURL = DEFAULT_BASE) {
|
|
152
|
+
const base = baseOf(baseURL);
|
|
153
|
+
try {
|
|
154
|
+
const res = await fetch(`${base}/api/tags`, { signal: AbortSignal.timeout(PROBE_TIMEOUT_MS) });
|
|
155
|
+
return { running: res.ok, baseURL: base };
|
|
156
|
+
} catch {
|
|
157
|
+
return { running: false, baseURL: base };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* List installed model tags (`GET /api/tags` → [{ id, details }]).
|
|
163
|
+
*
|
|
164
|
+
* Ollama-specific: a non-Ollama local server will 404 here. The caller renders
|
|
165
|
+
* that as "no models listed — type one", because the transport itself does not
|
|
166
|
+
* need this call at all. Losing the list must not cost the class its usability.
|
|
167
|
+
*/
|
|
168
|
+
async function listTags(baseURL = DEFAULT_BASE) {
|
|
169
|
+
const res = await fetch(`${baseOf(baseURL)}/api/tags`);
|
|
170
|
+
if (!res.ok) {
|
|
171
|
+
const err = new Error(`local: /api/tags answered ${res.status}`);
|
|
172
|
+
err.status = res.status;
|
|
173
|
+
throw err;
|
|
174
|
+
}
|
|
175
|
+
const data = await res.json();
|
|
176
|
+
const models = (data && data.models) || [];
|
|
177
|
+
return models.map((m) => ({
|
|
178
|
+
id: m && m.name,
|
|
179
|
+
...(m && m.details ? { details: m.details } : {}),
|
|
180
|
+
})).filter((m) => m.id);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Normalize one history row into an OpenAI-compatible message. Tool rows and
|
|
184
|
+
* assistant rows carrying `tool_calls` must survive intact: dropping either
|
|
185
|
+
* leaves the model seeing a tool call it never got a result for. */
|
|
186
|
+
function normalizeRow(m) {
|
|
187
|
+
if (!m || typeof m !== 'object') return null;
|
|
188
|
+
const row = { role: m.role || 'user' };
|
|
189
|
+
if (m.content != null) row.content = m.content;
|
|
190
|
+
if (m.name) row.name = m.name;
|
|
191
|
+
if (m.tool_call_id) row.tool_call_id = m.tool_call_id;
|
|
192
|
+
if (Array.isArray(m.tool_calls) && m.tool_calls.length) row.tool_calls = m.tool_calls;
|
|
193
|
+
return row;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** One SSE event's fields, or null for a keep-alive/comment frame (which
|
|
197
|
+
* carries no payload and so must not count as progress). */
|
|
198
|
+
function parseEvent(raw) {
|
|
199
|
+
const lines = String(raw).split('\n');
|
|
200
|
+
let data = '';
|
|
201
|
+
let sawField = false;
|
|
202
|
+
for (const line of lines) {
|
|
203
|
+
if (!line || line.startsWith(':')) continue; // '' separator, ':' keep-alive
|
|
204
|
+
const i = line.indexOf(':');
|
|
205
|
+
const field = i === -1 ? line : line.slice(0, i);
|
|
206
|
+
const value = i === -1 ? '' : line.slice(i + 1).replace(/^ /, '');
|
|
207
|
+
if (field === 'data') {
|
|
208
|
+
data += value;
|
|
209
|
+
sawField = true;
|
|
210
|
+
} else if (field === 'event' || field === 'id' || field === 'retry') {
|
|
211
|
+
sawField = true;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (!sawField || !data) return null;
|
|
215
|
+
if (data === '[DONE]') return { done: true };
|
|
216
|
+
try {
|
|
217
|
+
return { json: JSON.parse(data) };
|
|
218
|
+
} catch {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Read an SSE body, calling onEvent per parsed frame.
|
|
225
|
+
*
|
|
226
|
+
* The stall watchdog is armed PER PAYLOAD, not per socket read. That
|
|
227
|
+
* distinction is the point: a server that keeps the connection warm with
|
|
228
|
+
* keep-alive comments while the model produces nothing would otherwise reset
|
|
229
|
+
* the deadline forever, and the turn would hang with no error at all. Progress
|
|
230
|
+
* means a parsed payload — text, tool call, or usage — and nothing else.
|
|
231
|
+
*/
|
|
232
|
+
async function readSSE(res, onEvent, { idleTimeoutMs = SSE_IDLE_TIMEOUT_MS } = {}) {
|
|
233
|
+
const reader = res.body.getReader();
|
|
234
|
+
const decoder = new TextDecoder();
|
|
235
|
+
let buf = '';
|
|
236
|
+
let stallTimer = null;
|
|
237
|
+
let stallReject = () => {};
|
|
238
|
+
const stall = new Promise((_, reject) => {
|
|
239
|
+
stallReject = reject;
|
|
240
|
+
});
|
|
241
|
+
const arm = () => {
|
|
242
|
+
if (stallTimer) clearTimeout(stallTimer);
|
|
243
|
+
stallTimer = setTimeout(() => {
|
|
244
|
+
stallReject(
|
|
245
|
+
new Error(`local: the model stopped responding — no output for ${Math.round(idleTimeoutMs / 1000)}s`)
|
|
246
|
+
);
|
|
247
|
+
}, idleTimeoutMs);
|
|
248
|
+
};
|
|
249
|
+
arm();
|
|
250
|
+
try {
|
|
251
|
+
for (;;) {
|
|
252
|
+
const chunk = await Promise.race([reader.read(), stall]);
|
|
253
|
+
if (chunk.done) break;
|
|
254
|
+
buf += decoder.decode(chunk.value, { stream: true });
|
|
255
|
+
// Normalize CRLF so one separator check covers both spellings.
|
|
256
|
+
buf = buf.replace(/\r\n/g, '\n');
|
|
257
|
+
let idx;
|
|
258
|
+
while ((idx = buf.indexOf('\n\n')) >= 0) {
|
|
259
|
+
const raw = buf.slice(0, idx);
|
|
260
|
+
buf = buf.slice(idx + 2);
|
|
261
|
+
const ev = parseEvent(raw);
|
|
262
|
+
if (!ev) continue; // keep-alive — deliberately does NOT re-arm
|
|
263
|
+
if (ev.done) return;
|
|
264
|
+
if (ev.json) {
|
|
265
|
+
arm();
|
|
266
|
+
onEvent(ev.json);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
} finally {
|
|
271
|
+
if (stallTimer) clearTimeout(stallTimer);
|
|
272
|
+
try {
|
|
273
|
+
await reader.cancel();
|
|
274
|
+
} catch {
|
|
275
|
+
/* already closed */
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* One streaming turn against a local model. Keyless by construction — there is
|
|
282
|
+
* no credential to send, which is a second reason a remote URL here could never
|
|
283
|
+
* be billed.
|
|
284
|
+
*
|
|
285
|
+
* Returns the OpenAI shape the engine reads: `choices[0].message.content` and
|
|
286
|
+
* `choices[0].message.tool_calls`.
|
|
287
|
+
*/
|
|
288
|
+
async function chat({
|
|
289
|
+
baseURL = DEFAULT_BASE,
|
|
290
|
+
model,
|
|
291
|
+
messages,
|
|
292
|
+
system,
|
|
293
|
+
prompt,
|
|
294
|
+
maxTokens,
|
|
295
|
+
temperature,
|
|
296
|
+
tools,
|
|
297
|
+
toolChoice,
|
|
298
|
+
signal,
|
|
299
|
+
onDelta,
|
|
300
|
+
} = {}) {
|
|
301
|
+
const base = baseOf(baseURL);
|
|
302
|
+
const refusal = remoteRefusal(base);
|
|
303
|
+
if (refusal) throw refusal;
|
|
304
|
+
if (!model || !String(model).trim()) {
|
|
305
|
+
const err = new Error('local: no model selected — pick one from the model list, or type a tag');
|
|
306
|
+
err.status = 400;
|
|
307
|
+
throw err;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const rows = [];
|
|
311
|
+
if (system) rows.push({ role: 'system', content: system });
|
|
312
|
+
for (const m of Array.isArray(messages) ? messages : []) {
|
|
313
|
+
if (m && m.role === 'system') continue; // never two system rows
|
|
314
|
+
const row = normalizeRow(m);
|
|
315
|
+
if (row) rows.push(row);
|
|
316
|
+
}
|
|
317
|
+
// The engine passes the user's own text as `prompt` alongside the history.
|
|
318
|
+
// Append it only if the history does not already end with it, so a caller
|
|
319
|
+
// that includes it cannot send the same turn twice.
|
|
320
|
+
const p = typeof prompt === 'string' ? prompt : '';
|
|
321
|
+
if (p) {
|
|
322
|
+
const last = rows[rows.length - 1];
|
|
323
|
+
if (!(last && last.role === 'user' && last.content === p)) {
|
|
324
|
+
rows.push({ role: 'user', content: p });
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (!rows.length) {
|
|
328
|
+
const err = new Error('local: nothing to send — no prompt and no messages');
|
|
329
|
+
err.status = 400;
|
|
330
|
+
throw err;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const body = {
|
|
334
|
+
model: String(model),
|
|
335
|
+
messages: rows,
|
|
336
|
+
stream: true,
|
|
337
|
+
// Ask for usage even on a stream: Ollama's shim reports it when asked, and
|
|
338
|
+
// without the ask a local turn's token count is invisible in the UI.
|
|
339
|
+
stream_options: { include_usage: true },
|
|
340
|
+
...(maxTokens ? { max_tokens: maxTokens } : {}),
|
|
341
|
+
...(temperature != null ? { temperature } : {}),
|
|
342
|
+
...(tools && tools.length ? { tools } : {}),
|
|
343
|
+
...(toolChoice ? { tool_choice: toolChoice } : {}),
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
const res = await fetch(`${base}/v1/chat/completions`, {
|
|
347
|
+
method: 'POST',
|
|
348
|
+
headers: { 'content-type': 'application/json' },
|
|
349
|
+
body: JSON.stringify(body),
|
|
350
|
+
signal,
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
if (!res.ok) {
|
|
354
|
+
// Body text, not just the status: a local daemon's 400 says which field it
|
|
355
|
+
// disliked (commonly `tools` on an older build), and the engine retries
|
|
356
|
+
// without tool schemas on exactly that signal.
|
|
357
|
+
let text = '';
|
|
358
|
+
try {
|
|
359
|
+
text = await res.text();
|
|
360
|
+
} catch {
|
|
361
|
+
/* body already gone */
|
|
362
|
+
}
|
|
363
|
+
const err = new Error(`local: chat/completions ${res.status}${text ? ` — ${text.slice(0, 400)}` : ''}`);
|
|
364
|
+
err.status = res.status;
|
|
365
|
+
throw err;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
let content = '';
|
|
369
|
+
let usage = null;
|
|
370
|
+
const toolCalls = [];
|
|
371
|
+
const byIndex = new Map();
|
|
372
|
+
|
|
373
|
+
await readSSE(
|
|
374
|
+
res,
|
|
375
|
+
(json) => {
|
|
376
|
+
if (json && json.usage) usage = json.usage;
|
|
377
|
+
const choice = json && Array.isArray(json.choices) ? json.choices[0] : null;
|
|
378
|
+
if (!choice) return;
|
|
379
|
+
const delta = choice.delta || {};
|
|
380
|
+
if (typeof delta.content === 'string' && delta.content) {
|
|
381
|
+
content += delta.content;
|
|
382
|
+
if (typeof onDelta === 'function') onDelta({ text: delta.content });
|
|
383
|
+
}
|
|
384
|
+
const frags = delta.tool_calls || [];
|
|
385
|
+
for (const f of frags) {
|
|
386
|
+
const i = Number.isInteger(f.index) ? f.index : toolCalls.length;
|
|
387
|
+
let acc = byIndex.get(i);
|
|
388
|
+
if (!acc) {
|
|
389
|
+
acc = { id: '', type: 'function', function: { name: '', arguments: '' } };
|
|
390
|
+
byIndex.set(i, acc);
|
|
391
|
+
}
|
|
392
|
+
if (f.id) acc.id = f.id;
|
|
393
|
+
if (f.type) acc.type = f.type;
|
|
394
|
+
const fn = f.function || {};
|
|
395
|
+
if (fn.name) acc.function.name = fn.name;
|
|
396
|
+
if (fn.arguments) acc.function.arguments += fn.arguments;
|
|
397
|
+
}
|
|
398
|
+
},
|
|
399
|
+
{ idleTimeoutMs: SSE_IDLE_TIMEOUT_MS }
|
|
400
|
+
);
|
|
401
|
+
|
|
402
|
+
for (const [i, acc] of [...byIndex.entries()].sort((a, b) => a[0] - b[0])) {
|
|
403
|
+
if (!acc.function.name && !acc.function.arguments) continue;
|
|
404
|
+
// Ollama does not always mint an id; the engine's tool loop keys results
|
|
405
|
+
// by it, so an absent one is filled rather than left to collapse every
|
|
406
|
+
// call in the turn onto a single empty key.
|
|
407
|
+
if (!acc.id) acc.id = `call_${i}`;
|
|
408
|
+
toolCalls.push(acc);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
const message = { role: 'assistant', content };
|
|
412
|
+
if (toolCalls.length) message.tool_calls = toolCalls;
|
|
413
|
+
|
|
414
|
+
return {
|
|
415
|
+
id: 'local',
|
|
416
|
+
model: String(model),
|
|
417
|
+
choices: [{ index: 0, message, finish_reason: toolCalls.length ? 'tool_calls' : 'stop' }],
|
|
418
|
+
...(usage ? { usage } : {}),
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
module.exports = {
|
|
423
|
+
DEFAULT_BASE,
|
|
424
|
+
SSE_IDLE_TIMEOUT_MS,
|
|
425
|
+
PROBE_TIMEOUT_MS,
|
|
426
|
+
baseOf,
|
|
427
|
+
isLocalEndpoint,
|
|
428
|
+
remoteRefusal,
|
|
429
|
+
probe,
|
|
430
|
+
listTags,
|
|
431
|
+
readSSE,
|
|
432
|
+
chat,
|
|
433
|
+
};
|
|
@@ -218,15 +218,6 @@ function schemaList(includeSubagent) {
|
|
|
218
218
|
return Object.values(SCHEMAS).filter((s) => includeSubagent || s.name !== SUBAGENT_TOOL);
|
|
219
219
|
}
|
|
220
220
|
|
|
221
|
-
/** Anthropic Messages API tool definitions ({name, description, input_schema}). */
|
|
222
|
-
function anthropicTools({ includeSubagent = true } = {}) {
|
|
223
|
-
return schemaList(includeSubagent).map((s) => ({
|
|
224
|
-
name: s.name,
|
|
225
|
-
description: s.description,
|
|
226
|
-
input_schema: s.parameters,
|
|
227
|
-
}));
|
|
228
|
-
}
|
|
229
|
-
|
|
230
221
|
/** OpenAI-compatible /chat/completions tool definitions ({type:'function', function}). */
|
|
231
222
|
function openaiTools({ includeSubagent = true } = {}) {
|
|
232
223
|
return schemaList(includeSubagent).map((s) => ({
|
|
@@ -236,51 +227,14 @@ function openaiTools({ includeSubagent = true } = {}) {
|
|
|
236
227
|
}
|
|
237
228
|
|
|
238
229
|
/**
|
|
239
|
-
* The advertised tool list
|
|
240
|
-
*
|
|
241
|
-
*
|
|
230
|
+
* The advertised tool list. There is exactly ONE wire format now — every turn
|
|
231
|
+
* goes through the AEGIS relay, which speaks the OpenAI shape — so this takes
|
|
232
|
+
* no `wire` argument. The Anthropic-shaped variant went away with the
|
|
233
|
+
* direct-dial transport. `includeSubagent: false` drops the task tool
|
|
234
|
+
* (subagent depth cap).
|
|
242
235
|
*/
|
|
243
|
-
function toolsFor(
|
|
244
|
-
return
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* Convert an OpenAI-format tool list into Anthropic's. Exported because it is
|
|
249
|
-
* the exact transformation the loop depends on (and it is unit-tested as
|
|
250
|
-
* such): an endpoint that speaks Anthropic must never be handed
|
|
251
|
-
* `{type:'function', function:{…}}`.
|
|
252
|
-
*/
|
|
253
|
-
function openaiToAnthropicTools(tools) {
|
|
254
|
-
if (!Array.isArray(tools)) return [];
|
|
255
|
-
return tools
|
|
256
|
-
.map((t) => {
|
|
257
|
-
const fn = (t && t.function) || t || {};
|
|
258
|
-
if (!fn.name) return null;
|
|
259
|
-
return {
|
|
260
|
-
name: fn.name,
|
|
261
|
-
description: fn.description || '',
|
|
262
|
-
input_schema: fn.parameters || { type: 'object', properties: {} },
|
|
263
|
-
};
|
|
264
|
-
})
|
|
265
|
-
.filter(Boolean);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/** Reverse projection (Anthropic → OpenAI), for symmetry/completeness. */
|
|
269
|
-
function anthropicToOpenaiTools(tools) {
|
|
270
|
-
if (!Array.isArray(tools)) return [];
|
|
271
|
-
return tools
|
|
272
|
-
.map((t) => {
|
|
273
|
-
if (!t || !t.name) return null;
|
|
274
|
-
return {
|
|
275
|
-
type: 'function',
|
|
276
|
-
function: {
|
|
277
|
-
name: t.name,
|
|
278
|
-
description: t.description || '',
|
|
279
|
-
parameters: t.input_schema || { type: 'object', properties: {} },
|
|
280
|
-
},
|
|
281
|
-
};
|
|
282
|
-
})
|
|
283
|
-
.filter(Boolean);
|
|
236
|
+
function toolsFor({ includeSubagent = true } = {}) {
|
|
237
|
+
return openaiTools({ includeSubagent });
|
|
284
238
|
}
|
|
285
239
|
|
|
286
240
|
// ── Executors ───────────────────────────────────────────────────────────────
|
|
@@ -840,11 +794,8 @@ module.exports = {
|
|
|
840
794
|
SCHEMAS,
|
|
841
795
|
SUBAGENT_TOOL,
|
|
842
796
|
toolNames,
|
|
843
|
-
anthropicTools,
|
|
844
797
|
openaiTools,
|
|
845
798
|
toolsFor,
|
|
846
|
-
openaiToAnthropicTools,
|
|
847
|
-
anthropicToOpenaiTools,
|
|
848
799
|
// execution
|
|
849
800
|
executeTool,
|
|
850
801
|
isTool,
|
|
@@ -17,12 +17,18 @@
|
|
|
17
17
|
|
|
18
18
|
const fs = require('node:fs');
|
|
19
19
|
const path = require('node:path');
|
|
20
|
-
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
|
|
20
|
+
|
|
21
|
+
// The local-model transport. Required for ONE function: `remoteRefusal`, the
|
|
22
|
+
// fail-closed check that keeps a non-local base URL out of the `local` row. The
|
|
23
|
+
// module owns that policy so this store, the engine and the CLI cannot drift
|
|
24
|
+
// about what "local" means — the failure mode a second implementation would
|
|
25
|
+
// produce is one host refusing an unpaid turn while another served it.
|
|
26
|
+
const localTransport = require('./local/local.js');
|
|
27
|
+
|
|
28
|
+
/** The provider row the `local` class reads its base URL from. Not a reserved
|
|
29
|
+
* namespace: it is a legitimate config row, and `list()` reporting it is
|
|
30
|
+
* harmless because the byok rows are selected by their `byok:` prefix. */
|
|
31
|
+
const LOCAL_NAMESPACE = 'local';
|
|
26
32
|
|
|
27
33
|
const SETTINGS_FILE = 'settings.json';
|
|
28
34
|
|
|
@@ -163,34 +169,25 @@ function createSettingsStore({ dir, safeStorage } = {}) {
|
|
|
163
169
|
/**
|
|
164
170
|
* Write a provider row.
|
|
165
171
|
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
172
|
+
* The endpoint policy lives at this seam for exactly one namespace: `local`,
|
|
173
|
+
* the row holding the local model server's base URL. It is refused HERE so
|
|
174
|
+
* the unusable configuration cannot be created — a row that could never
|
|
175
|
+
* legally be dialed should not be persistable, because it would sit on disk
|
|
176
|
+
* looking like a working setup. The engine re-checks before every call as
|
|
177
|
+
* well (a file hand-edited after it was written cannot be spent either), so
|
|
178
|
+
* there are two gates for two failure modes.
|
|
179
|
+
*
|
|
180
|
+
* Every other row is a `byok:<provider>`, and those stay policy-free: they are
|
|
181
|
+
* remote by definition and reach their provider through AEGIS's relay, and the
|
|
182
|
+
* relay is what bills the turn — so a remote address there is correct rather
|
|
183
|
+
* than a misconfiguration. `baseURL` stays in the record shape for back-compat
|
|
184
|
+
* with rows already on disk.
|
|
177
185
|
*/
|
|
178
186
|
function set(provider, { baseURL, key } = {}) {
|
|
179
187
|
assertNotReserved(provider);
|
|
180
|
-
if (
|
|
181
|
-
const
|
|
182
|
-
if (
|
|
183
|
-
const err = new Error(remoteRefusal(next, {
|
|
184
|
-
subject: `the ${provider} endpoint`,
|
|
185
|
-
hint:
|
|
186
|
-
'This lane is for a model running on this machine. To use a remote provider, ' +
|
|
187
|
-
'save it under Bring-your-own-key (the CLI\'s /class byok), where the AEGIS handling ' +
|
|
188
|
-
'fee bills the turn.',
|
|
189
|
-
}));
|
|
190
|
-
err.status = 400;
|
|
191
|
-
err.code = 'CUSTOM_ENDPOINT_NOT_LOCAL';
|
|
192
|
-
throw err;
|
|
193
|
-
}
|
|
188
|
+
if (provider === LOCAL_NAMESPACE && baseURL) {
|
|
189
|
+
const refusal = localTransport.remoteRefusal(baseURL);
|
|
190
|
+
if (refusal) throw refusal;
|
|
194
191
|
}
|
|
195
192
|
const data = load();
|
|
196
193
|
const cfg = data[provider] || {};
|
|
@@ -131,10 +131,10 @@ function estimatedBuckets(prompt, reply, reasoning) {
|
|
|
131
131
|
// Per-million-token USD rates. Cache-read/write matter for long sessions.
|
|
132
132
|
//
|
|
133
133
|
// These are the PROVIDER's rates, for the fallback path where a turn has no
|
|
134
|
-
// server-settled charge (a
|
|
135
|
-
// pooled turn reports the ledger figure instead — see
|
|
136
|
-
// `costUsd` — because the pool's bill carries a margin and a
|
|
137
|
-
// discount this table cannot see.
|
|
134
|
+
// server-settled charge (a byok relay turn reports the provider rate it
|
|
135
|
+
// relays against). A pooled turn reports the ledger figure instead — see
|
|
136
|
+
// turnAccounting's `costUsd` — because the pool's bill carries a margin and a
|
|
137
|
+
// prompt-cache discount this table cannot see.
|
|
138
138
|
//
|
|
139
139
|
// Keys are matched by exact id first, then by prefix (ratesFor below), so a
|
|
140
140
|
// family row covers every dated variant of it.
|