aegiscode 7.0.14 → 7.0.16

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.
Files changed (109) hide show
  1. package/SOURCE-OFFER.txt +9 -0
  2. package/package.json +4 -7
  3. package/scripts/check.mjs +1 -52
  4. package/scripts/demo.mjs +1 -94
  5. package/scripts/predist.mjs +1 -75
  6. package/scripts/runtests.mjs +1 -56
  7. package/scripts/seal-gate.mjs +1 -74
  8. package/src/agents.js +1 -139
  9. package/src/app.js +1 -2932
  10. package/src/art.js +1 -289
  11. package/src/chatflow.js +1 -3020
  12. package/src/checkpoint.js +1 -90
  13. package/src/clipboard.js +1 -424
  14. package/src/cloudsync.js +1 -528
  15. package/src/commands.js +1 -4003
  16. package/src/config.js +1 -581
  17. package/src/connect.js +1 -349
  18. package/src/credentials.js +1 -33
  19. package/src/deps.js +1 -232
  20. package/src/devrun.js +1 -187
  21. package/src/diff.js +1 -333
  22. package/src/diffview.js +1 -296
  23. package/src/engine.js +1 -297
  24. package/src/entitlement.js +1 -355
  25. package/src/events.js +1 -707
  26. package/src/export.js +1 -67
  27. package/src/fingerprint.js +1 -592
  28. package/src/format.js +1 -259
  29. package/src/fuzzy.js +1 -119
  30. package/src/history.js +1 -401
  31. package/src/init.js +1 -175
  32. package/src/input.js +1 -139
  33. package/src/keys.js +1 -183
  34. package/src/markdown.js +1 -306
  35. package/src/models.js +1 -487
  36. package/src/naming.js +1 -229
  37. package/src/overlays.js +1 -461
  38. package/src/panels.js +1 -1418
  39. package/src/ping.js +1 -226
  40. package/src/presence.js +1 -238
  41. package/src/render.js +1 -515
  42. package/src/screen.js +1 -518
  43. package/src/screens.js +1 -896
  44. package/src/secret.js +1 -59
  45. package/src/securefs.js +1 -359
  46. package/src/settings.js +1 -155
  47. package/src/shared.js +1 -52
  48. package/src/sharedpaths.js +1 -53
  49. package/src/signup.js +1 -156
  50. package/src/summarize.js +1 -95
  51. package/src/sysdiag.js +1 -585
  52. package/src/system.js +1 -40
  53. package/src/theme.js +1 -570
  54. package/src/title.js +1 -220
  55. package/src/todos.js +1 -115
  56. package/src/tokens.js +1 -263
  57. package/src/topup.js +1 -210
  58. package/src/update.js +1 -181
  59. package/vendor/client/aegis.js +1 -2179
  60. package/vendor/client/attest.js +1 -442
  61. package/vendor/client/authorship-key.js +1 -410
  62. package/vendor/client/auto-provision.js +1 -134
  63. package/vendor/client/brain-catalog.js +1 -268
  64. package/vendor/client/byok-catalog.js +1 -230
  65. package/vendor/client/credentials.js +1 -494
  66. package/vendor/client/device-fingerprint.js +1 -141
  67. package/vendor/client/drive-connect.js +1 -110
  68. package/vendor/client/drive-upload.js +1 -174
  69. package/vendor/client/env-file.js +1 -1273
  70. package/vendor/client/fingerprint.js +1 -1064
  71. package/vendor/client/foreign-memory.js +1 -669
  72. package/vendor/client/google-signin.js +1 -133
  73. package/vendor/client/hooks.js +1 -430
  74. package/vendor/client/model-discovery.js +1 -373
  75. package/vendor/client/platform.js +1 -937
  76. package/vendor/client/prefs.js +1 -351
  77. package/vendor/client/presence-constants.js +1 -107
  78. package/vendor/client/presence.js +1 -438
  79. package/vendor/client/record-vault.js +3 -1055
  80. package/vendor/client/session-store.js +1 -548
  81. package/vendor/client/topup.js +1 -297
  82. package/vendor/client/touch.js +1 -458
  83. package/vendor/client/update.js +1 -704
  84. package/vendor/client/user-models.js +1 -381
  85. package/vendor/desktop/lib/local/agents.js +1 -105
  86. package/vendor/desktop/lib/local/autonomous.js +1 -1045
  87. package/vendor/desktop/lib/local/claude-pro.js +1 -993
  88. package/vendor/desktop/lib/local/context.js +1 -95
  89. package/vendor/desktop/lib/local/engine.js +1 -3301
  90. package/vendor/desktop/lib/local/entitlement.js +1 -660
  91. package/vendor/desktop/lib/local/git-scope.js +1 -228
  92. package/vendor/desktop/lib/local/local.js +1 -2135
  93. package/vendor/desktop/lib/local/prompt.js +1 -275
  94. package/vendor/desktop/lib/local/queue.js +1 -560
  95. package/vendor/desktop/lib/local/session-rounds.js +1 -208
  96. package/vendor/desktop/lib/local/shell.js +1 -680
  97. package/vendor/desktop/lib/local/tools.js +1 -1238
  98. package/vendor/desktop/lib/local/turn-guard.js +1 -300
  99. package/vendor/desktop/lib/local/worktree-lock.js +1 -235
  100. package/vendor/desktop/lib/settings.js +1 -890
  101. package/vendor/desktop/renderer/usage.js +1 -1239
  102. package/vendor/hosts/install.js +1 -638
  103. package/vendor/hosts/openai-shim.js +1 -515
  104. package/vendor/hosts/spec.js +1 -164
  105. package/vendor/hosts/targets.js +1 -346
  106. package/vendor/mcp/server.js +1 -221
  107. package/vendor/mcp/tools.js +1 -408
  108. package/vendor/one/src/child.js +1 -285
  109. package/vendor/one/src/permissions.js +1 -369
@@ -1,2137 +1,3 @@
1
1
  // SPDX-License-Identifier: AGPL-3.0-or-later
2
2
  // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
- // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
- // Conveying a modified copy, or running one as a network service, owes the
5
- // corresponding source under this licence (§5/§13) — see LICENSING.md.
6
- 'use strict';
7
-
8
- /**
9
- * local.js — the local-model transport: run a model on hardware you own.
10
- *
11
- * This is the third class, alongside `aegis` (pooled, metered) and `byok`
12
- * (relayed, handling fee). It is also the one class that bills NOTHING, which
13
- * is exactly why it is fenced the way it is.
14
- *
15
- * The fee argument, restated because every rule below follows from it. A
16
- * transport that dials a user-supplied URL has no pooled margin to take, no
17
- * handling fee to charge and no account key attached, so a REMOTE call there is
18
- * an unpaid turn: the relay accepts a fixed catalog of provider ids
19
- * (aegis1 services/nexus_provider/catalog.py, upstream key from
20
- * `X-Provider-Key`), so an arbitrary remote URL has nothing to be billed
21
- * against even if a client wanted to invoice it. That is why remote direct
22
- * dialing is gone from this codebase and stays gone — there is deliberately no
23
- * flag or env var that re-opens it, because such a flag would be a billing
24
- * bypass.
25
- *
26
- * A model on YOUR OWN MACHINE is the case that argument never covered. There is
27
- * no vendor to pay, so there is nothing to bill and nobody being denied a
28
- * payment: the compute was already bought and is sitting idle. `aegis` and
29
- * `byok` are lanes to somebody else's GPU; this is the lane to your own.
30
- *
31
- * So the fence is the whole feature: `isLocalEndpoint()` is checked before any
32
- * byte goes out, and it FAILS CLOSED — anything that does not parse, carries no
33
- * host, or resolves to a public address is remote, i.e. refused. A false
34
- * "local" is an unpaid turn; a false "remote" is a refusal the user fixes by
35
- * pointing at a local address. In a tie, refuse.
36
- *
37
- * Wire-wise this speaks Ollama's OpenAI-compatible surface
38
- * (`POST /v1/chat/completions`, keyless) plus its native `GET /api/tags` for
39
- * the model list. Anything that serves an OpenAI-compatible endpoint on the
40
- * same box — llama.cpp, LM Studio, vLLM — works by pointing `baseURL` at it;
41
- * only the model LIST is Ollama-specific, and that failure is recoverable
42
- * (see listTags) rather than fatal.
43
- */
44
-
45
- // The standalone token-budget trimmer (plan P1 §5.1), used here for the one
46
- // thing it was written for and never given a caller: keeping a turn's wire
47
- // payload inside the window the daemon actually granted. See `fitToWindowOf`
48
- // below for why the transport and not the caller owns this.
49
- const { estimateTokens, trimContext } = require('./context.js');
50
-
51
- /** Where a local daemon listens by default (Ollama's stock port). */
52
- const DEFAULT_BASE = 'http://localhost:11434';
53
-
54
- /**
55
- * How long a local turn tolerates a silent stream before failing. Local
56
- * inference is SLOW to first token — a cold 7B model on a laptop CPU can think
57
- * for a minute before emitting anything, and a warm one still loads weights on
58
- * the first call after a restart — so this is generous where the cloud client's
59
- * 60s default would abort a perfectly healthy turn.
60
- */
61
- const SSE_IDLE_TIMEOUT_MS = 300000;
62
-
63
- /**
64
- * How long the daemon may take to answer with response headers at all — the
65
- * window BEFORE the first byte, which `SSE_IDLE_TIMEOUT_MS` above cannot cover:
66
- * that timer is armed per parsed payload, and there is no payload, and not even
67
- * a body to read, until this fetch resolves. The cloud transport bounds exactly
68
- * this window in `boundedFetch` (client/aegis.js: "a server that accepts the
69
- * socket and never sends response headers ... nothing is watching at all"); the
70
- * local lane had no bound at all, so a daemon that accepted the socket and then
71
- * went quiet left the turn as a spinner with no error, no timeout and no way
72
- * out but Stop.
73
- *
74
- * Deliberately the SAME budget as the stall timer rather than a tighter one,
75
- * because this wait is a legitimate part of a local turn on the hardware this
76
- * class exists for: measured on the machine this was written (Ollama 0.12,
77
- * `omega-sonnet5-lite` — a 1.5B model, daemon already warm), the first byte
78
- * arrived **107.8s** after the request, all of it load + prompt eval with
79
- * nothing to show. A bound tight enough to catch a wedged daemon fast would
80
- * abort healthy turns; five minutes catches only the genuinely dead ones, and
81
- * the error says which it is.
82
- */
83
- const FIRST_BYTE_TIMEOUT_MS = SSE_IDLE_TIMEOUT_MS;
84
-
85
- /**
86
- * The response-header deadline the RUNTIME imposes, independent of the one
87
- * above — and the reason a second clock has to be handled here at all.
88
- *
89
- * Measured on this host: a live `/v1/chat/completions` turn whose prompt prefill
90
- * outlasted it (a 2521-token payload on a contended CPU) failed with
91
- * `TypeError: fetch failed`, `cause: HeadersTimeoutError`,
92
- * `code: 'UND_ERR_HEADERS_TIMEOUT'` at ~300s — undici's default `headersTimeout`,
93
- * not this transport's `FIRST_BYTE_TIMEOUT_MS`. Any caller that states a longer
94
- * bound (`firstByteTimeoutMs: 500000`) therefore does not get a longer wait: the
95
- * runtime cuts the turn first, and it cut it as a raw fetch failure rather than
96
- * in the timeout shape every other "waited too long" on this lane uses, so the
97
- * host rendered an opaque error for a plain slow prefill. A local prefill IS
98
- * routinely slower than this: the daemon's own log shows 236.7s for 2048 tokens
99
- * while another model was loaded.
100
- *
101
- * Because the engine's contract is that a timeout arrives as `status: 0` +
102
- * `code: 'timeout'` (the cloud client's shape, so "waited too long" stays one
103
- * branch in the renderer), the second clock is translated into that shape rather
104
- * than passed through. The number is stated here as a measured constant, not read
105
- * from the runtime, which does not expose it.
106
- */
107
- const HEADERS_DEADLINE_MS = 300000;
108
-
109
- /**
110
- * Is this rejection the runtime's own response-header deadline?
111
- *
112
- * Matched on the undici cause rather than on the message text, and tolerant of
113
- * the several shapes it wears (`HeadersTimeoutError` name, `UND_ERR_HEADERS_TIMEOUT`
114
- * code) — a socket that dies for another reason must NOT be reported as a
115
- * timeout, which is why this is a predicate rather than a catch-all.
116
- */
117
- function isHeadersDeadline(err) {
118
- if (!err) return false;
119
- const cause = err.cause || err;
120
- if (!cause) return false;
121
- const code = String(cause.code || '');
122
- const name = String(cause.name || '');
123
- return code === 'UND_ERR_HEADERS_TIMEOUT' || name === 'HeadersTimeoutError';
124
- }
125
-
126
- /** How long the daemon probe waits. Short: this runs behind a UI spinner, and
127
- * "no daemon" must read as an answer rather than as a hang. */
128
- const PROBE_TIMEOUT_MS = 1500;
129
-
130
- /**
131
- * The one spelling of a base URL this module dials.
132
- *
133
- * A missing scheme is the COMMON case, not an edge one: `localhost:11434` and
134
- * `127.0.0.1:11434` are what a user types into the Settings row, and both were
135
- * previously read by `new URL()` as scheme `localhost:`/`127.0.0.1:` with an
136
- * empty path — not http/https, so `isLocalEndpoint()` said "remote" and the
137
- * fence REFUSED the user's own machine. The settings store refuses to write such
138
- * a row, and a row already on disk from a hand edit gets a turn refused with
139
- * "… is not on this machine, so it is not the local class" while Ollama is up
140
- * and answering. That is the whole reported symptom: a healthy local daemon
141
- * reported as a remote address.
142
- *
143
- * `http://` is prepended only when the string carries NO scheme, and the result
144
- * is still parsed by `new URL()` — so this normalizes the dialing spelling
145
- * without widening the fence by one host: `example.com:11434` becomes
146
- * `http://example.com:11434`, still a dotted public name, still refused.
147
- * `https://` is left exactly as given (a local daemon behind TLS is legitimate,
148
- * and rewriting a stated scheme would send the turn to the wrong port).
149
- */
150
- function normalizeBase(url) {
151
- const raw = String(url == null ? '' : url).trim();
152
- if (!raw) return DEFAULT_BASE;
153
- // Any `scheme://` is taken as stated. A colon WITHOUT `//` is a host:port
154
- // (`localhost:11434`), never a scheme — which is exactly the ambiguity
155
- // WHATWG's parser resolves the other way, and the bug above.
156
- return /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(raw) ? raw : `http://${raw}`;
157
- }
158
-
159
- /** Trim trailing slashes so `${base}/v1/...` never doubles one. */
160
- function baseOf(baseURL) {
161
- return normalizeBase(baseURL).replace(/\/+$/, '');
162
- }
163
-
164
- /**
165
- * DeepSeek (also the chat template a locally-pulled `deepseek-*` Ollama model
166
- * uses) emits its tool calls as DSML — an XML-ish dialect that the
167
- * OpenAI-compatible wire format does NOT structure for us:
168
- *
169
- * <|DSML|tool_calls>
170
- * <|DSML|invoke name="write">
171
- * <|DSML|parameter name="path" string="true">src/app.js</|DSML|parameter>
172
- * <|DSML|parameter name="content" string="true">…the file body…</|DSML|parameter>
173
- * </|DSML|invoke>
174
- * </|DSML|tool_calls>
175
- *
176
- * It arrives as plain `delta.content` text, and that produces two failures:
177
- * the markup rendering verbatim (reported as the pasted "||DSML||" envelope),
178
- * and the call itself never running because text is not a tool call. Ported
179
- * from aegiscodex-dev's src/backend.js (also client/aegis.js, the other
180
- * transport this engine shares), which carries the full history of this
181
- * grammar's quirks (provider/template/model revision disagree about the exact
182
- * identifier, angle brackets sometimes absent, tag names drift under
183
- * sampling) — every part of the pattern below is deliberately optional and
184
- * spacing-insensitive for that reason. `string="false"` marks a JSON
185
- * parameter; `string="true"` (or an absent attribute) is the literal value,
186
- * kept verbatim so a multi-line `content` is never mangled by a JSON
187
- * round-trip.
188
- */
189
- // `\b` after each tag-name alternative matters: without it, "call" matches as
190
- // a bare PREFIX of a drifted wrapper tag like "calls" (observed verbatim —
191
- // `<|| calls>`, no "tool_"/"function_" prefix at all), swallowing everything
192
- // up to the next real `</invoke>` as that spurious match's unnamed body and
193
- // silently discarding a whole tool call with it. "call" and "calls" are both
194
- // word characters, so plain alternation order cannot tell them apart.
195
- const DSML_RE_INVOKE = /<\s*|\s*(?:DSML\s*)?|\s*(?:invoke|call|tool_call)\b\s*([^>]*?)\s*>([\s\S]*?)<\s*\/\s*|\s*(?:DSML\s*)?|\s*(?:invoke|call|tool_call)\b\s*>/g;
196
- const DSML_RE_PARAM = /<\s*|\s*(?:DSML\s*)?|\s*(?:parameter|param|arg)\b\s*([^>]*?)\s*>([\s\S]*?)<\s*\/\s*|\s*(?:DSML\s*)?|\s*(?:parameter|param|arg)\b\s*>/g;
197
- const DSML_RE_LEADER = /<\s*\/?\s*|\s*(?:DSML\s*)?|\s*(?:(?:tool|function)[_▁ ]*calls?|reasoning)[_▁ ]*(?:begin|end)?\s*>/gi;
198
- const DSML_RE_ANY_TAG = /<\s*\/?\s*|\s*(?:DSML\s*)?|\s*[A-Za-z_][A-Za-z0-9_]*[^>]{0,120}>/g;
199
- const DSML_RE_SKELETON = /<\s*\/?\s*(?:tool_calls|function_calls|invoke|parameter|param|reasoning)\b[^>]{0,120}>/gi;
200
- const DSML_RE_JSON_CALLS = /|[ \t]*(?:tool|function)[_▁ ]*calls?[_▁ ]*begin[ \t]*|([\s\S]*?)|[ \t]*(?:tool|function)[_▁ ]*calls?[_▁ ]*end[ \t]*|/gi;
201
- // The leader/closer halves of DSML_RE_JSON_CALLS above, split out so the hold
202
- // logic can detect "the leader arrived but its closer has not" — an arbitrary
203
- // JSON blob sits between them with no '<' or recognizable structure of its
204
- // own, so nothing else below would otherwise hold it back.
205
- const DSML_RE_JSON_CALLS_OPEN = /|[ \t]*(?:tool|function)[_▁ ]*calls?[_▁ ]*begin[ \t]*|/gi;
206
- const DSML_RE_JSON_CALLS_CLOSE = /|[ \t]*(?:tool|function)[_▁ ]*calls?[_▁ ]*end[ \t]*|/gi;
207
- const DSML_RE_ENVELOPE_TOKEN = /|[ \t]?[A-Za-z0-9_▁]*[ \t]?|/g;
208
- const DSML_RE_DRIFT_INVOKE = /<\s*|\s*(?:DSML\s*)?|\s*(?:invoke|call|tool_call)\b\s*([^>]*?)\s*>/gi;
209
- const DSML_RE_DRIFT_PARAM = /<\s*|\s*(?:DSML\s*)?|\s*(?:parameter|param|arg)\b\s*([^>]*?)\s*>/gi;
210
- const DSML_RE_PARAM_CLOSE = /<\s*\/\s*|\s*(?:DSML\s*)?|\s*(?:parameter|param|arg)\b\s*>/gi;
211
- const DSML_RE_ATTR = /([A-Za-z_][A-Za-z0-9_.-]*)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
212
- const DSML_RE_STRUCT_TAG = /<\s*\/?\s*|\s*(?:DSML\s*)?|\s*(?:invoke|call|tool_call|parameter|param|arg|reasoning|(?:tool|function)[_▁ ]*calls?)\b/gi;
213
- const DSML_RE_STRUCT_CLOSER = /<\s*\/\s*|\s*(?:DSML\s*)?|\s*(?:invoke|call|tool_call|parameter|param|arg|reasoning|(?:tool|function)[_▁ ]*calls?)\b/gi;
214
- /**
215
- * A COMPLETE (its own '>' has arrived) wrapper/leader tag that carries no
216
- * call of its own — observed verbatim: a bare `<||calls>`, with neither a
217
- * "tool_"/"function_" prefix (so DSML_RE_LEADER above misses it) nor a JSON
218
- * envelope pair. Safe to drop the moment it closes, UNLIKE invoke/parameter —
219
- * excluded here via negative lookahead so a *partial* invoke/parameter head is
220
- * never erased before its own close has a chance to pair it up with
221
- * DSML_RE_INVOKE/DSML_RE_PARAM on a later call once more of it has arrived.
222
- */
223
- const DSML_RE_WRAPPER_TAG = /<\s*\/?\s*|\s*(?:DSML\s*)?|\s*(?!(?:invoke|call|tool_call|parameter|param|arg)\b)[A-Za-z_][A-Za-z0-9_]*[^>]{0,120}>/g;
224
- /** A '<' (or '</') at the tail: the head of a tag whose pipe is still in flight. */
225
- const DSML_RE_TRAILING_LT = /<\s*\/?\s*$/;
226
- /** An in-flight identifier token: a pipe whose partner has not arrived yet. */
227
- const DSML_RE_OPEN_TOKEN = /^|[ \t]?[A-Za-z0-9_▁]*[ \t]?$/;
228
- /** Past this much buffered markup, a held tail is released as-is. */
229
- const DSML_HOLD_MAX = 4 * 1024 * 1024;
230
-
231
- /** The attributes of one tag head, lower-cased: name="write" → { name: 'write' }. */
232
- function dsmlAttrs(text) {
233
- const out = {};
234
- if (!text) return out;
235
- DSML_RE_ATTR.lastIndex = 0;
236
- let m;
237
- while ((m = DSML_RE_ATTR.exec(text))) out[m[1].toLowerCase()] = m[2] != null ? m[2] : m[3];
238
- return out;
239
- }
240
-
241
- /** A JSON parameter — or, when it is not JSON after all, the text it came as. */
242
- function dsmlJson(raw) {
243
- const s = String(raw == null ? '' : raw).trim();
244
- try {
245
- const v = JSON.parse(s);
246
- return v === undefined ? s : v;
247
- } catch {
248
- return s;
249
- }
250
- }
251
-
252
- /** The `parameter` tags of one `invoke` body, as a tool call's args object. */
253
- function dsmlArgs(body) {
254
- const args = {};
255
- DSML_RE_PARAM.lastIndex = 0;
256
- let p;
257
- while ((p = DSML_RE_PARAM.exec(body))) {
258
- const a = dsmlAttrs(p[1]);
259
- const key = a.name || a.key || `arg${Object.keys(args).length}`;
260
- const isJson = String(a.string || '').toLowerCase() === 'false' || String(a.json || '').toLowerCase() === 'true';
261
- args[key] = isJson ? dsmlJson(p[2]) : p[2];
262
- }
263
- return args;
264
- }
265
-
266
- /**
267
- * Decode every DSML tool call in `text` and return the two things a caller
268
- * needs: what may be shown, and what must be executed — `{ text, calls }`.
269
- * The generic `|…|` tokens are deliberately left in place here: an `invoke`
270
- * block still arriving is identified by its delimiters on the next pass, and
271
- * eating them early would lose the call's name and arguments. They are
272
- * removed from the *released* text instead (see dsmlResidue).
273
- */
274
- function decodeDeepSeekDSML(input) {
275
- if (typeof input !== 'string' || !input || input.indexOf('|') === -1) {
276
- return { text: input || '', calls: [] };
277
- }
278
- const calls = [];
279
- let text = input;
280
-
281
- // 1. One `invoke` block per call, the well-formed spelling.
282
- DSML_RE_INVOKE.lastIndex = 0;
283
- const spans = [];
284
- let m;
285
- while ((m = DSML_RE_INVOKE.exec(text))) {
286
- const a = dsmlAttrs(m[1]);
287
- const name = a.name || a.function || a.tool || '';
288
- if (name) calls.push({ name, args: dsmlArgs(m[2]) });
289
- spans.push([m.index, m.index + m[0].length]);
290
- }
291
- for (let i = spans.length - 1; i >= 0; i--) {
292
- text = text.slice(0, spans[i][0]) + text.slice(spans[i][1]);
293
- }
294
-
295
- // 2. The older spelling: a JSON array wrapped in envelope tokens.
296
- text = text.replace(DSML_RE_JSON_CALLS, (full, body) => {
297
- const parsed = dsmlJson(body);
298
- const list = Array.isArray(parsed) ? parsed : (parsed && Array.isArray(parsed.tool_calls) ? parsed.tool_calls : []);
299
- for (const c of list) {
300
- const fn = (c && c.function) || c || {};
301
- const name = fn.name || (c && c.name) || '';
302
- if (!name) continue;
303
- let args = fn.arguments != null ? fn.arguments : (c && c.arguments);
304
- if (typeof args === 'string') args = dsmlJson(args);
305
- calls.push({ name, args: args && typeof args === 'object' ? args : {} });
306
- }
307
- return '';
308
- });
309
-
310
- // 3. The wrapper tags carry no call of their own.
311
- text = text.replace(DSML_RE_LEADER, '');
312
- return { text, calls };
313
- }
314
-
315
- /**
316
- * Last-resort decode for markup that drifted out of well-formedness: a missing
317
- * closing tag, or a stream cut mid-call. The call still names a tool and its
318
- * parameters are still readable, so it is decoded rather than deleted — a call
319
- * that reached the wire must not vanish because its closing tag did.
320
- */
321
- function decodeDeepSeekDSMLDrift(input) {
322
- if (typeof input !== 'string' || !input || input.indexOf('|') === -1) return { text: input || '', calls: [] };
323
- const opens = [];
324
- DSML_RE_DRIFT_INVOKE.lastIndex = 0;
325
- let m;
326
- while ((m = DSML_RE_DRIFT_INVOKE.exec(input))) {
327
- opens.push({ at: m.index, attrs: m[1], body: DSML_RE_DRIFT_INVOKE.lastIndex });
328
- }
329
- if (!opens.length) return { text: input, calls: [] };
330
- const calls = [];
331
- let text = '';
332
- let cursor = 0;
333
- for (let i = 0; i < opens.length; i++) {
334
- // One call runs to the next `invoke` head, or to the end of the text.
335
- const end = i + 1 < opens.length ? opens[i + 1].at : input.length;
336
- const body = input.slice(opens[i].body, end);
337
- const a = dsmlAttrs(opens[i].attrs);
338
- const name = a.name || a.function || a.tool || '';
339
- const args = {};
340
- const params = [];
341
- DSML_RE_DRIFT_PARAM.lastIndex = 0;
342
- let pm;
343
- while ((pm = DSML_RE_DRIFT_PARAM.exec(body))) {
344
- params.push({ at: pm.index, attrs: pm[1], from: DSML_RE_DRIFT_PARAM.lastIndex });
345
- }
346
- for (let k = 0; k < params.length; k++) {
347
- // A value runs to the next parameter tag — its closing tag may be the
348
- // thing that never arrived.
349
- const stop = k + 1 < params.length ? params[k + 1].at : body.length;
350
- const raw = body.slice(params[k].from, stop).replace(DSML_RE_PARAM_CLOSE, '');
351
- const pa = dsmlAttrs(params[k].attrs);
352
- const key = pa.name || pa.key || `arg${Object.keys(args).length}`;
353
- const isJson = String(pa.string || '').toLowerCase() === 'false';
354
- args[key] = isJson ? dsmlJson(raw) : raw;
355
- }
356
- text += input.slice(cursor, opens[i].at);
357
- cursor = end;
358
- if (name) calls.push({ name, args });
359
- }
360
- text += input.slice(cursor);
361
- return { text, calls };
362
- }
363
-
364
- /**
365
- * Residue removal for text that is about to be shown. Two spellings have to go:
366
- * a leftover envelope token (`|DSML|`, `|tool▁calls▁begin|`, `|Assistant|`),
367
- * and the skeleton an earlier release left behind by stripping the delimiters
368
- * out of a DSML block — `<tool_calls> <invoke name="write"> <parameter …>`,
369
- * which is exactly what a user pasted back at us.
370
- */
371
- function dsmlResidue(text) {
372
- if (!text) return text;
373
- if (text.indexOf('|') === -1) return text.replace(DSML_RE_SKELETON, '');
374
- return text.replace(DSML_RE_ANY_TAG, '').replace(DSML_RE_SKELETON, '').replace(DSML_RE_ENVELOPE_TOKEN, '');
375
- }
376
-
377
- /**
378
- * Is the buffer, read from the first `|` in it, a construct that is still
379
- * arriving? Only two shapes may be held back: a pipe whose partner has not
380
- * arrived, and a DSML opener whose matching close has not landed. Everything
381
- * else is prose — a `|` in an answer is a character, not a promise of markup,
382
- * and holding the rest of the answer behind it would swallow the reply.
383
- */
384
- function dsmlUnterminated(s) {
385
- if (DSML_RE_OPEN_TOKEN.test(s)) return true;
386
- const openers = (s.match(DSML_RE_STRUCT_TAG) || []).length;
387
- const closers = (s.match(DSML_RE_STRUCT_CLOSER) || []).length;
388
- return openers > closers;
389
- }
390
-
391
- /**
392
- * Where, if anywhere, an in-flight DSML construct still needs the rest of the
393
- * buffer held back from view. By the time this runs, decodeDeepSeekDSML() has
394
- * already removed every COMPLETE invoke/parameter span and the caller has
395
- * already removed every complete non-invoke wrapper tag (DSML_RE_WRAPPER_TAG),
396
- * so whatever is left can only be: clean prose, or the start of exactly ONE
397
- * incomplete construct running to the end of the buffer — an opener whose own
398
- * '|' has arrived but not its '>' (`<\s*\/?\s*|`, found wherever it starts,
399
- * not just at the tail: a Write's `content` can be arbitrarily long, so the
400
- * unresolved opener is usually far behind the cursor, not at it), a '<' that
401
- * arrived with no '|' yet (the chunk boundary landed first), or a bare pipe
402
- * token still accumulating its identifier (the plain envelope-leak case, e.g.
403
- * '|tool▁cal'). Returns -1 when none apply — safe to release in full.
404
- */
405
- function dsmlHoldFrom(buf) {
406
- const tagStart = buf.search(/<\s*\/?\s*|/);
407
- if (tagStart !== -1) return tagStart;
408
- // The older JSON-array envelope (DSML_RE_JSON_CALLS): once its leader token
409
- // has arrived, hold everything until the matching closer token lands. An
410
- // arbitrary JSON blob sits between them with no '<' and no recognizable
411
- // per-character structure, so without this check it streams straight
412
- // through as visible prose the instant the leader's own closing '|'
413
- // arrives — the hold rule below only looks at ONE pipe token at a time and
414
- // has no notion of "waiting for a second, different token later on".
415
- const jsonOpenIdx = buf.search(DSML_RE_JSON_CALLS_OPEN);
416
- if (jsonOpenIdx !== -1 && buf.slice(jsonOpenIdx).search(DSML_RE_JSON_CALLS_CLOSE) === -1) {
417
- return jsonOpenIdx;
418
- }
419
- const lt = DSML_RE_TRAILING_LT.exec(buf);
420
- if (lt) return lt.index;
421
- const openIdx = buf.indexOf('|');
422
- if (openIdx !== -1 && dsmlUnterminated(buf.slice(openIdx))) return openIdx;
423
- return -1;
424
- }
425
-
426
- /**
427
- * Pull COMPLETE plain `<tool_call>` blocks out of `text` (mutating `calls`).
428
- *
429
- * This runs inside the DSML stripper, ahead of its skeleton cleanup, for two
430
- * reasons: that cleanup strips bare `<tool_calls>` tags and would otherwise
431
- * leave the JSON body behind as prose, and running here means a DSML parameter
432
- * body — a Write's `content`, a whole file — is never scanned, because those
433
- * bodies are consumed by the DSML decoder and never reach this text path.
434
- */
435
- function decodePlainToolCallTags(text, calls) {
436
- if (!text || text.indexOf('<') === -1) return text;
437
- let out = '';
438
- let cursor = 0;
439
- for (;;) {
440
- const head = TEXT_TOOL_TAG_OPEN_RE.exec(text.slice(cursor));
441
- if (!head) break;
442
- const at = cursor + head.index;
443
- const headText = head[0];
444
- if (headText.includes(DSML_TAG_MARK)) { // belongs to the DSML dialect
445
- out += text.slice(cursor, at + headText.length);
446
- cursor = at + headText.length;
447
- continue;
448
- }
449
- const rest = text.slice(at + headText.length);
450
- const close = TEXT_TOOL_TAG_CLOSE_RE.exec(rest);
451
- if (!close) break; // never closed: leave for the hold logic
452
- const decoded = textCallsIn(rest.slice(0, close.index));
453
- out += text.slice(cursor, at);
454
- cursor = at + headText.length + close.index + close[0].length;
455
- if (decoded.length) {
456
- for (const c of decoded) calls.push(c);
457
- } else {
458
- out += text.slice(at, cursor); // a tag that is not a call: unchanged
459
- }
460
- }
461
- return out + text.slice(cursor);
462
- }
463
-
464
- /** The position of a plain `<tool_call>` head whose closer has not arrived, or
465
- * -1 — the second hold rule, alongside dsmlHoldFrom(). */
466
- function plainToolTagHoldAt(text) {
467
- if (!text || text.indexOf('<') === -1) return -1;
468
- let cursor = 0;
469
- for (;;) {
470
- const head = TEXT_TOOL_TAG_OPEN_RE.exec(text.slice(cursor));
471
- if (!head) return -1;
472
- const at = cursor + head.index;
473
- if (head[0].includes(DSML_TAG_MARK)) { cursor = at + head[0].length; continue; }
474
- const rest = text.slice(at + head[0].length);
475
- return TEXT_TOOL_TAG_CLOSE_RE.test(rest) ? -1 : at;
476
- }
477
- }
478
-
479
- /**
480
- * The per-turn stripper this transport uses. Feed it each `delta.content`
481
- * chunk: it returns the text that is safe to show, holds back whatever is
482
- * still arriving (a Write's `content` parameter is a whole file — it belongs
483
- * to the call, not to the screen), and collects the tool calls the model made
484
- * in text form. `flush()` releases the tail once the stream is drained, and
485
- * `calls()` returns everything decoded so far, which the caller merges into
486
- * the turn's tool calls so a Write that arrived as DSML is executed like any
487
- * other.
488
- */
489
- function makeDeepSeekEnvelopeStripper() {
490
- let buf = '';
491
- const calls = [];
492
- const release = (text) => dsmlResidue(decodePlainToolCallTags(text, calls));
493
- const strip = (chunk) => {
494
- buf += typeof chunk === 'string' ? chunk : '';
495
- const dec = decodeDeepSeekDSML(buf);
496
- buf = dec.text;
497
- for (const c of dec.calls) calls.push(c);
498
- buf = buf.replace(DSML_RE_WRAPPER_TAG, '');
499
- const hold = dsmlHoldFrom(buf);
500
- let out;
501
- if (hold !== -1 && buf.length - hold <= DSML_HOLD_MAX) {
502
- out = buf.slice(0, hold);
503
- buf = buf.slice(hold);
504
- } else {
505
- out = buf;
506
- buf = '';
507
- }
508
- // A plain `<tool_call>` head whose closer has not landed yet is held the
509
- // same way the DSML envelope is: the call is one whole thing, and showing
510
- // half of it would mean showing a tag the decoder is about to consume.
511
- const plainAt = plainToolTagHoldAt(out);
512
- if (plainAt !== -1) {
513
- buf = out.slice(plainAt) + buf;
514
- out = out.slice(0, plainAt);
515
- }
516
- return release(out);
517
- };
518
- strip.flush = () => {
519
- const dec = decodeDeepSeekDSML(buf);
520
- buf = '';
521
- for (const c of dec.calls) calls.push(c);
522
- const drift = decodeDeepSeekDSMLDrift(dec.text);
523
- for (const c of drift.calls) calls.push(c);
524
- return release(drift.text);
525
- };
526
- /** Every tool call decoded from text so far, as `{ name, args }`. */
527
- strip.calls = () => calls.slice();
528
- return strip;
529
- }
530
-
531
- /**
532
- * Tool calls a local model wrote as TEXT, rather than in the structured field.
533
- *
534
- * Measured against a live daemon (ollama 0.32.1, 2026-10-05), on the same
535
- * prompt this file's DSML decoder exists for:
536
- *
537
- * qwen2.5-coder:1.5b-instruct-q4_K_M, `tools: [read]`, "read /etc/hostname
538
- * with the read tool" →
539
- * `content: "```json\n{\"name\": \"read\", \"arguments\": {\"path\": …}```"`,
540
- * `finish_reason: "stop"`, and NO `delta.tool_calls` at all.
541
- *
542
- * That is not a quirk of one tag: Ollama's OpenAI shim only fills
543
- * `delta.tool_calls` for templates it knows carry tool tokens (`tools` in the
544
- * model's own `capabilities`, listed by `/api/tags`). Every other local model
545
- * — an older qwen, a Llama, a gguf picked up from the Hub — is handed the
546
- * schemas, answers in the dialect its chat template taught it, and emits it as
547
- * plain content. The engine's loop then sees a model that "said" a JSON blob
548
- * and ran nothing, which is what the user sees as the local class being unable
549
- * to use tools at all.
550
- *
551
- * So the same treatment the DSML dialect gets: decode the envelope out of the
552
- * text, keep the prose, and hand the calls to the tool loop. Two shapes are
553
- * recognised, because they are the two the standard templates emit:
554
- *
555
- * <tool_call>{"name": "read", "arguments": {"path": "/etc/hostname"}}</tool_call>
556
- * ```json
557
- * {"name": "read", "arguments": {"path": "/etc/hostname"}}
558
- * ```
559
- *
560
- * Deliberately NOT recognised: a bare `{…}` with no fence and no tag. Prose
561
- * questions about JSON ("what does {"name": "read"} mean?") are common and a
562
- * decoder that guessed there would turn a user's own example into a real file
563
- * read — the conservative half of the trade this file already makes for the
564
- * endpoint gate. A fenced block whose JSON does not carry a `name` + arguments
565
- * pair is left exactly as it arrived: it is a code block, not a call.
566
- */
567
- const TEXT_TOOL_TAG_OPEN_RE = /<\s*tool_calls?\b[^>]{0,80}>/i;
568
- const TEXT_TOOL_TAG_CLOSE_RE = /<\s*\/\s*tool_calls?\s*>/i;
569
- const TEXT_FENCE_RE = /```[ \t]*[A-Za-z0-9_+-]*[ \t]*\n?([\s\S]*?)```/;
570
- // The OPENing half alone, for the case where the closer has not arrived yet:
571
- // TEXT_FENCE_RE cannot match there (its closer is part of the match, NOT part
572
- // of the captured body — the first version of this code sliced with
573
- // `fence[0].length` and so held every fence forever), which is exactly why the
574
- // hold test needs a separate pattern.
575
- const TEXT_FENCE_OPEN_RE = /```[ \t]*[A-Za-z0-9_+-]*[ \t]*\n?/;
576
- // A DSML tag always carries the fullwidth pipe; a PLAIN `<tool_call>` never
577
- // does. That single character is what tells the two dialects apart, and it is
578
- // why this pass can run ahead of the DSML machinery without ever claiming a
579
- // tag that belongs to it.
580
- const DSML_TAG_MARK = '|';
581
-
582
- /** Coerce one decoded envelope body into `{ name, args }`, or null when it is
583
- * not a call. Accepts the argument spellings the templates actually produce:
584
- * `arguments` (openai-ish, object or JSON string), `parameters`, `args`, and
585
- * the `{function:{name,arguments}}` wrapper a model sometimes copies from the
586
- * schema it was handed. */
587
- function asTextToolCall(value) {
588
- if (!value || typeof value !== 'object') return null;
589
- let node = value;
590
- if (node.function && typeof node.function === 'object') {
591
- node = { ...node.function, name: node.function.name || node.name };
592
- }
593
- const rawName = node.name || node.tool || node.tool_name || node.function_name;
594
- const name = typeof rawName === 'string' ? rawName.trim() : '';
595
- if (!name) return null;
596
- let args = node.arguments != null ? node.arguments
597
- : node.parameters != null ? node.parameters
598
- : node.args != null ? node.args
599
- : node.tool_input != null ? node.tool_input
600
- : {};
601
- if (typeof args === 'string') {
602
- const parsed = args.trim() ? safeJson(args) : {};
603
- args = parsed && typeof parsed === 'object' ? parsed : {};
604
- }
605
- if (args == null || typeof args !== 'object') args = {};
606
- return { name, args };
607
- }
608
-
609
- /** JSON.parse that returns null instead of throwing. */
610
- function safeJson(text) {
611
- try {
612
- return JSON.parse(text);
613
- } catch {
614
- return null;
615
- }
616
- }
617
-
618
- /** Decode one envelope BODY (tag inner text or fence inner text). Returns []
619
- * when it is not a call, so the caller can hand the text back unchanged. */
620
- function textCallsIn(body) {
621
- const text = String(body || '').trim().replace(/^json\s*/i, '').trim();
622
- if (!text) return [];
623
- const parsed = safeJson(text);
624
- if (!parsed) return [];
625
- const list = Array.isArray(parsed) ? parsed : [parsed];
626
- const calls = [];
627
- for (const item of list) {
628
- const call = asTextToolCall(item);
629
- if (call) calls.push(call);
630
- }
631
- return calls;
632
- }
633
-
634
- /** The next envelope in `buf`: a ``` fence carrying a call, or the position of
635
- * a plain `<tool_call>` head the caller must hold. `end` is where it finishes
636
- * in `buf`; `complete` is false while its closer is still on the wire, in
637
- * which case the caller must HOLD the tail, not show it. */
638
- function nextTextToolEnvelope(buf) {
639
- const fence = TEXT_FENCE_RE.exec(buf);
640
- if (fence) {
641
- // `fence[1]` is the body; the closing fence is part of the MATCH, so the
642
- // body must come from the capture group and the end from `fence[0]`.
643
- return {
644
- start: fence.index,
645
- end: fence.index + fence[0].length,
646
- complete: true,
647
- raw: fence[0],
648
- body: fence[1],
649
- };
650
- }
651
- const open = TEXT_FENCE_OPEN_RE.exec(buf);
652
- if (!open) return null;
653
- return {
654
- start: open.index,
655
- end: buf.length,
656
- complete: false,
657
- body: buf.slice(open.index + open[0].length),
658
- };
659
- }
660
-
661
- /** Where the hold must start in `buf`, or -1 when nothing is pending: the
662
- * index of a recognized-but-unclosed head, or of the first characters of one
663
- * that is still arriving (a '`' that may become a fence, a '<' that may become
664
- * a tag). Everything before that index is released as prose. */
665
- function trailingHeadFrom(buf, allowBare = false) {
666
- if (!buf) return -1;
667
- const marker = buf.search(TEXT_TOOL_HEAD_RE);
668
- if (marker !== -1) return marker; // head started, closer has not landed
669
- // A fence opener still arriving: one or two backticks at the very end. It
670
- // cannot be anything else — a closed inline code span would carry its closer.
671
- const ticks = /`{1,2}$/.exec(buf);
672
- if (ticks) return ticks.index;
673
- // A tag still arriving: the last '<' has only a prefix of "tool_call" after
674
- // it. Prose that merely contains '<' resolves on the very next chunk.
675
- const lt = buf.lastIndexOf('<');
676
- if (lt !== -1 && couldBeTextToolHead(buf.slice(lt))) return lt;
677
- // A bare JSON object still arriving — the same dialect rule as above, and it
678
- // has to be held for the same reason: a chunk landing on the first `{` would
679
- // otherwise show that brace as prose and the object could never be recognised
680
- // afterwards. Only JSON-SHAPED tails are held; an ordinary brace in prose
681
- // ('use a { in your code') is released and streams normally.
682
- if (allowBare) {
683
- const open = buf.lastIndexOf('{');
684
- if (open !== -1 && /^\{\s*(?:"[^"]*"\s*:[\s\S]*|"[^"]*"?\s*|)$/.test(buf.slice(open))) return open;
685
- }
686
- return -1;
687
- }
688
-
689
- /** Still a POSSIBLE head — a fence just starting, or a `<tool_c…` prefix that
690
- * has not finished arriving. A recognized (already-matching) head is held by
691
- * TEXT_TOOL_HEAD_RE, not here; this is only for the partial case, so that a
692
- * chunk boundary landing mid-`<tool_call` cannot show two characters of a tag
693
- * and then retract them. */
694
- function couldBeTextToolHead(buf) {
695
- const lead = buf.replace(/^[\s\uFEFF]+/, '');
696
- if (!lead) return true; // nothing buffered yet
697
- if (lead.length <= 3 && '```'.startsWith(lead)) return true;
698
- if (lead.startsWith('<') && !TEXT_TOOL_HEAD_RE.test(lead)) {
699
- const name = lead.slice(1).replace(/^\/?\s*/, '').toLowerCase();
700
- if (name.length <= 10 && /^[a-z_]*$/.test(name)) {
701
- return 'tool_call'.startsWith(name) || 'tool_calls'.startsWith(name);
702
- }
703
- }
704
- return false;
705
- }
706
-
707
- /**
708
- * Where a JSON object starting at `start` ends, or -1 while it is still
709
- * arriving. String- and escape-aware, because the arguments of a real call
710
- * routinely carry quoted braces (`{"content": "function f() { … }"}`) and a
711
- * naive depth count ends the object inside one of them.
712
- */
713
- function balancedJsonEnd(text, start) {
714
- let depth = 0;
715
- let inStr = false;
716
- let esc = false;
717
- for (let i = start; i < text.length; i++) {
718
- const ch = text[i];
719
- if (inStr) {
720
- if (esc) esc = false;
721
- else if (ch === '\\') esc = true;
722
- else if (ch === '"') inStr = false;
723
- continue;
724
- }
725
- if (ch === '"') inStr = true;
726
- else if (ch === '{' || ch === '[') depth++;
727
- else if (ch === '}' || ch === ']') {
728
- depth--;
729
- if (depth <= 0) return i + 1;
730
- }
731
- }
732
- return -1;
733
- }
734
-
735
- /**
736
- * Decode tool calls out of a local model's TEXT — the fenced and `<tool_call>`
737
- * dialects, and the bare JSON object a small model emits when it ignores both.
738
- *
739
- * `toolNames` is the set of tools THIS request offered. It is the guard that
740
- * makes the bare-JSON rule honest rather than a guess: a JSON object is only
741
- * ever read as a call when the name it carries is one of the tools the model
742
- * was actually given (and, for an array, every one of them). A user asking
743
- * "what does {"name": "read"} mean?" in a turn with no tools, or naming a tool
744
- * that was never offered, gets prose back untouched. That is measured against
745
- * the real failure mode: with the fence rule alone, qwen2.5-coder answered the
746
- * tool prompt with a bare object about as often as with a fenced one, and the
747
- * turn ran nothing either time.
748
- *
749
- * Incomplete envelopes are held (never shown and retracted): the caller feeds
750
- * chunks, and a fence/tag/object whose closer has not arrived stays in the
751
- * buffer until it has, or until flush() decides.
752
- */
753
- function makeTextToolCallStripper({ toolNames = null } = {}) {
754
- const names = toolNames ? new Set([...toolNames].map((n) => String(n))) : null;
755
- let buf = '';
756
- const calls = [];
757
- /** A decoded batch counts only if every call names an offered tool. */
758
- const accept = (decoded) =>
759
- Boolean(decoded.length) && (!names || decoded.every((c) => names.has(c.name)));
760
- /** Show a complete envelope's TEXT when it is not a call, or swallow it and
761
- * record the call when it is. */
762
- const digest = (raw, body) => {
763
- const decoded = textCallsIn(body);
764
- if (!accept(decoded)) return raw; // prose fence / tag: shown exactly as it came
765
- for (const c of decoded) calls.push(c);
766
- return '';
767
- };
768
- const strip = (chunk) => {
769
- buf += typeof chunk === 'string' ? chunk : '';
770
- let out = '';
771
- // Set whenever the loop stops on an envelope that is still arriving: the
772
- // buffer then holds a decided-pending head and the tail release below must
773
- // not undo the hold it just took.
774
- let held = false;
775
- for (;;) {
776
- const fence = nextTextToolEnvelope(buf);
777
- if (fence) {
778
- out += buf.slice(0, fence.start); // prose before the envelope is never held
779
- buf = buf.slice(fence.start);
780
- if (!fence.complete) { held = true; break; }
781
- out += digest(fence.raw, fence.body);
782
- buf = buf.slice(fence.raw.length);
783
- continue;
784
- }
785
- const tag = TEXT_TOOL_TAG_OPEN_RE.exec(buf);
786
- if (tag) {
787
- out += buf.slice(0, tag.index);
788
- buf = buf.slice(tag.index);
789
- const rest = buf.slice(tag[0].length);
790
- const close = TEXT_TOOL_TAG_CLOSE_RE.exec(rest);
791
- if (!close) { held = true; break; } // closer still on the wire: hold it
792
- const raw = buf.slice(0, tag[0].length + close.index + close[0].length);
793
- out += digest(raw, rest.slice(0, close.index));
794
- buf = buf.slice(raw.length);
795
- continue;
796
- }
797
- // Bare JSON — admitted only against the tools this turn offered, so a
798
- // JSON-shaped QUESTION can never become a file read. `{"` rather than a
799
- // lone `{`: the templates that emit this write an object with quoted
800
- // keys, and it keeps prose braces out of the scan entirely.
801
- const bare = names ? /\{\s*"/.exec(buf) : null;
802
- if (!bare) break;
803
- const end = balancedJsonEnd(buf, bare.index);
804
- const before = buf.slice(0, bare.index);
805
- if (end === -1) { out += before; buf = buf.slice(bare.index); held = true; break; } // hold
806
- const decoded = textCallsIn(buf.slice(bare.index, end));
807
- out += before;
808
- if (accept(decoded)) {
809
- for (const c of decoded) calls.push(c);
810
- } else {
811
- out += buf.slice(bare.index, end); // prose: shown, and not rescanned
812
- }
813
- buf = buf.slice(end);
814
- }
815
- // Nothing further can turn into an envelope: release the tail rather than
816
- // delay prose behind a guard that will never fire. A recognized-but-unclosed
817
- // head (or the first characters of one, e.g. the '`' a fence opener starts
818
- // with) stays held — its closer is still coming, and showing it now would
819
- // mean retracting it a moment later. The head can be anywhere in the
820
- // buffer, not only at its start: prose streams ahead of the envelope it
821
- // introduces, and the first version of this code released the whole buffer
822
- // whenever the buffer did not BEGIN with a head, which let every
823
- // prose-then-call turn through as text.
824
- const holdAt = held ? 0 : trailingHeadFrom(buf, Boolean(names));
825
- if (holdAt === -1) {
826
- out += buf;
827
- buf = '';
828
- } else if (holdAt > 0) {
829
- out += buf.slice(0, holdAt);
830
- buf = buf.slice(holdAt);
831
- }
832
- if (buf.length > TEXT_TOOL_HOLD_MAX) {
833
- // A fence or tag left open forever must not swallow the rest of the
834
- // answer; past this much held text it is prose, and it is shown as such.
835
- out += buf;
836
- buf = '';
837
- }
838
- return out;
839
- };
840
- strip.flush = () => {
841
- const rest = buf;
842
- buf = '';
843
- if (!rest) return '';
844
- const env = nextTextToolEnvelope(rest);
845
- // A fence or tag whose closer never arrived: decide NOW rather than hold
846
- // forever. If its body decodes it was a call cut off mid-stream (still
847
- // worth running — the arguments it did send are the caller's to validate);
848
- // if it does not decode it was prose all along and is released verbatim.
849
- if (env) return rest.slice(0, env.start) + digest(rest.slice(env.start), env.body);
850
- const tag = TEXT_TOOL_TAG_OPEN_RE.exec(rest);
851
- if (!tag) {
852
- // A bare object the stream stopped inside: decide it here, so a call cut
853
- // off mid-argument still runs instead of being dropped as text.
854
- const bare = names ? /\{\s*"/.exec(rest) : null;
855
- if (!bare) return rest;
856
- const end = balancedJsonEnd(rest, bare.index);
857
- return end === -1
858
- ? rest
859
- : rest.slice(0, bare.index) + digest(rest.slice(bare.index, end), rest.slice(bare.index, end)) + rest.slice(end);
860
- }
861
- const body = rest.slice(tag.index + tag[0].length).replace(TEXT_TOOL_TAG_CLOSE_RE, '');
862
- return rest.slice(0, tag.index) + digest(rest.slice(tag.index), body);
863
- };
864
- strip.calls = () => calls.slice();
865
- return strip;
866
- }
867
-
868
- /** Where a text-form envelope could still begin — the guard that keeps a
869
- * partial ` ` ` or `<tool_c` from being shown and then retracted. */
870
- const TEXT_TOOL_HEAD_RE = /```|<\s*tool_calls?\b/i;
871
-
872
- /** A fence left open forever (a model that never closed it) must not hide the
873
- * rest of the answer — past this much held text, release it as prose. */
874
- const TEXT_TOOL_HOLD_MAX = 64 * 1024;
875
-
876
- /**
877
- * Whether a base URL addresses hardware on this machine (or on this private
878
- * network) — the gate that decides whether the free lane exists at all.
879
- *
880
- * True for: loopback (localhost, `*.localhost`, 127.0.0.0/8, `::1`), RFC1918
881
- * private ranges (10/8, 172.16/12, 192.168/16), IPv6 unique-local fc00::/7,
882
- * link-local (169.254/16, fe80::/10), the reserved local suffixes `.local`,
883
- * `.internal`, `.lan`, and a bare dotless hostname (`ollama`, `gpu-box` — it
884
- * can only resolve through this machine's own resolver).
885
- *
886
- * Private ranges are included on purpose: a box on the user's own LAN is still
887
- * hardware they already own, so the "no vendor to pay" argument holds there
888
- * too. What is excluded is anything routable to a third party.
889
- *
890
- * FAIL CLOSED: an unparseable value, a value with no host, a non-http(s)
891
- * scheme, or a public address is NOT local. See the file header for why the
892
- * tie breaks that way.
893
- */
894
- function isLocalEndpoint(url) {
895
- let host = '';
896
- let scheme = '';
897
- // No host stated is not evidence of this machine: '' / whitespace / null stay
898
- // refused, exactly as before (the sealed list in test/local-endpoint.test.mjs
899
- // pins those). Only a NON-EMPTY string gets the scheme defaulted, which is the
900
- // `localhost:11434` case above — a host the user named, in the spelling they
901
- // named it in.
902
- if (String(url == null ? '' : url).trim() === '') return false;
903
- try {
904
- const parsed = new URL(normalizeBase(url));
905
- host = parsed.hostname.toLowerCase();
906
- scheme = parsed.protocol.toLowerCase();
907
- } catch {
908
- return false;
909
- }
910
- // Only a base URL a transport can actually dial — checked BEFORE the host, so
911
- // `ftp://box.local` cannot ride the local suffix to a pass.
912
- if (scheme !== 'http:' && scheme !== 'https:') return false;
913
- if (!host) return false;
914
- // WHATWG keeps the brackets on an IPv6 hostname; strip them so one spelling
915
- // covers both `[::1]` and a bare `::1`.
916
- if (host.startsWith('[') && host.endsWith(']')) host = host.slice(1, -1);
917
- if (!host || /[\s/\\@]/.test(host)) return false;
918
-
919
- // Loopback by name, plus the mDNS / split-DNS names an on-box service
920
- // answers to. The suffix must END the name: `example.local.evil.com` is
921
- // somebody else's host.
922
- if (host === 'localhost' || host.endsWith('.localhost')) return true;
923
- if (/\.(local|internal|lan)$/.test(host)) return true;
924
-
925
- // IPv6 literals.
926
- if (host.includes(':')) {
927
- if (host === '::1' || host === '0:0:0:0:0:0:0:1') return true; // loopback
928
- if (/^f[cd][0-9a-f]{2}:/.test(host)) return true; // fc00::/7 unique-local
929
- if (/^fe[89ab][0-9a-f]:/.test(host)) return true; // fe80::/10 link-local
930
- return false;
931
- }
932
-
933
- // IPv4 literals.
934
- const quad = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(host);
935
- if (quad) {
936
- const o = quad.slice(1).map(Number);
937
- if (o.some((n) => n > 255)) return false; // not an address, and not local
938
- const [a, b] = o;
939
- if (a === 127) return true; // 127.0.0.0/8 loopback
940
- if (a === 10) return true; // 10.0.0.0/8
941
- if (a === 172 && b >= 16 && b <= 31) return true; // 172.16.0.0/12
942
- if (a === 192 && b === 168) return true; // 192.168.0.0/16
943
- if (a === 169 && b === 254) return true; // 169.254.0.0/16 link-local
944
- return false; // a real, routable address — remote, and not this lane
945
- }
946
-
947
- // A dotless name can only come from this machine's own resolver / hosts file.
948
- if (!host.includes('.')) return true;
949
- return false;
950
- }
951
-
952
- /**
953
- * Refuse a base URL that is not local, in the hosts' own voice.
954
- *
955
- * Returns null when the URL is usable. The returned error carries `status 400`
956
- * so every existing caller (the CLI's error painter, the desktop turn guard)
957
- * treats it as a user-fixable configuration problem rather than a crash.
958
- */
959
- function remoteRefusal(url) {
960
- if (isLocalEndpoint(url)) return null;
961
- const err = new Error(
962
- `local: ${JSON.stringify(String(url || ''))} is not on this machine, so it is not the local ` +
963
- 'class. Running a model you own is free because there is no vendor to pay — a remote URL ' +
964
- 'here would be an unpaid turn instead. Use the aegis or byok class for remote models.'
965
- );
966
- err.status = 400;
967
- return err;
968
- }
969
-
970
- /** Probe the local daemon. Never throws — a missing daemon is a state, not an
971
- * error, because the UI has to say "start it" rather than "it failed". */
972
- async function probe(baseURL = DEFAULT_BASE) {
973
- const base = baseOf(baseURL);
974
- try {
975
- const res = await fetch(`${base}/api/tags`, { signal: AbortSignal.timeout(PROBE_TIMEOUT_MS) });
976
- return { running: res.ok, baseURL: base };
977
- } catch {
978
- return { running: false, baseURL: base };
979
- }
980
- }
981
-
982
- /**
983
- * List installed model tags (`GET /api/tags` → [{ id, details }]).
984
- *
985
- * Ollama-specific: a non-Ollama local server will 404 here. The caller renders
986
- * that as "no models listed — type one", because the transport itself does not
987
- * need this call at all. Losing the list must not cost the class its usability.
988
- */
989
- async function listTags(baseURL = DEFAULT_BASE) {
990
- const res = await fetch(`${baseOf(baseURL)}/api/tags`);
991
- if (!res.ok) {
992
- const err = new Error(`local: /api/tags answered ${res.status}`);
993
- err.status = res.status;
994
- throw err;
995
- }
996
- const data = await res.json();
997
- const models = (data && data.models) || [];
998
- return models.map((m) => ({
999
- id: m && m.name,
1000
- ...(m && m.details ? { details: m.details } : {}),
1001
- })).filter((m) => m.id);
1002
- }
1003
-
1004
- /** How long an unload (`keep_alive: 0`) request waits before giving up.
1005
- * Generous — Ollama still has to touch the scheduler for a model that may be
1006
- * mid-generation — but bounded, because this runs on the app's quit path and
1007
- * must never be the reason closing the window hangs. */
1008
- const UNLOAD_TIMEOUT_MS = 4000;
1009
-
1010
- /** How long the warm-up request below may take before it is abandoned.
1011
- *
1012
- * It is the TURN's own first-byte budget (`FIRST_BYTE_TIMEOUT_MS`), and that
1013
- * is the point: the pin exists to absorb a load the turn would otherwise wait
1014
- * out, so it may legitimately take as long as the turn would have — but it may
1015
- * never outlive the caller's own patience with the daemon. Measured 107.8s for
1016
- * a warm 1.5B model's first byte on this class of hardware, so a bound tight
1017
- * enough to abort a wedged daemon fast would abort healthy cold loads; the
1018
- * turn's five-minute bound is the measured right answer, applied twice at most
1019
- * (pin, then turn) rather than once.
1020
- *
1021
- * A caller that STATES its own window (`firstByteTimeoutMs`) is never widened:
1022
- * the pin gets that number, so the turn still fails on the caller's clock
1023
- * instead of the pin's. That is the bug this bound replaced — against a daemon
1024
- * that accepts the socket and never answers, a pin on a budget of its own held
1025
- * the turn for minutes before the turn's own 1s deadline was even armed.
1026
- *
1027
- * The timer is `unref`'d (see `ensureResident`), the same rule
1028
- * `FIRST_BYTE_TIMEOUT_MS`'s timer follows: a short-lived CLI process
1029
- * (`aegiscode -p "…"`) must never be held open by a pending warm-up. */
1030
- const RESIDENT_TIMEOUT_MS = FIRST_BYTE_TIMEOUT_MS;
1031
-
1032
- /** How long a pin asks the daemon to hold the model warm, when nothing states
1033
- * otherwise — the whole point of the pin. Ollama's stock window is 5 MINUTES,
1034
- * and that is the number this lane was silently running on: idle past it and
1035
- * the daemon evicts, so the next turn re-pays the entire cold load. A user
1036
- * who steps away from the desktop for coffee should come back to a warm model
1037
- * (they paid for the load once already), and the model is released anyway on
1038
- * quit (`unloadAll`) and on a model switch (`swapWarmModel`), so 30 minutes
1039
- * costs nothing that is not already accounted for.
1040
- *
1041
- * Tunable per call (`opts.warmSecs`) and by env (`AEGIS_LOCAL_WARM_SECS`), and
1042
- * `0` is meaningful: it turns the pin off and returns the lane to the daemon's
1043
- * own lifecycle. */
1044
- const DEFAULT_WARM_SECS = 1800;
1045
-
1046
- /**
1047
- * Evict `model` from the daemon's memory right now, instead of waiting out
1048
- * its keep-alive. Ollama's documented mechanism: a chat request with
1049
- * `keep_alive: 0` and nothing to answer unloads the model as its only
1050
- * effect (https://github.com/ollama/ollama/blob/main/docs/faq.md). Best-effort
1051
- * and NEVER throws — called from the app's quit path and from the warm-model
1052
- * swap below, neither of which may fail a turn or block a shutdown over a
1053
- * daemon that is merely slow or already gone.
1054
- */
1055
- async function unload(baseURL, model) {
1056
- if (!model) return { ok: false };
1057
- const base = baseOf(baseURL);
1058
- if (!isLocalEndpoint(base)) return { ok: false }; // never dial out, even to "clean up"
1059
- try {
1060
- const res = await fetch(`${base}/api/chat`, {
1061
- method: 'POST',
1062
- headers: { 'content-type': 'application/json' },
1063
- body: JSON.stringify({ model: String(model), messages: [], stream: false, keep_alive: 0 }),
1064
- signal: AbortSignal.timeout(UNLOAD_TIMEOUT_MS),
1065
- });
1066
- return { ok: res.ok };
1067
- } catch {
1068
- return { ok: false };
1069
- }
1070
- }
1071
-
1072
- /** Normalize one history row into an OpenAI-compatible message. Tool rows and
1073
- * assistant rows carrying `tool_calls` must survive intact: dropping either
1074
- * leaves the model seeing a tool call it never got a result for. */
1075
- function normalizeRow(m) {
1076
- if (!m || typeof m !== 'object') return null;
1077
- const row = { role: m.role || 'user' };
1078
- if (m.content != null) row.content = m.content;
1079
- if (m.name) row.name = m.name;
1080
- if (m.tool_call_id) row.tool_call_id = m.tool_call_id;
1081
- if (Array.isArray(m.tool_calls) && m.tool_calls.length) row.tool_calls = m.tool_calls;
1082
- return row;
1083
- }
1084
-
1085
- /** One SSE event's fields, or null for a keep-alive/comment frame (which
1086
- * carries no payload and so must not count as progress). */
1087
- function parseEvent(raw) {
1088
- const lines = String(raw).split('\n');
1089
- let data = '';
1090
- let sawField = false;
1091
- for (const line of lines) {
1092
- if (!line || line.startsWith(':')) continue; // '' separator, ':' keep-alive
1093
- const i = line.indexOf(':');
1094
- const field = i === -1 ? line : line.slice(0, i);
1095
- const value = i === -1 ? '' : line.slice(i + 1).replace(/^ /, '');
1096
- if (field === 'data') {
1097
- data += value;
1098
- sawField = true;
1099
- } else if (field === 'event' || field === 'id' || field === 'retry') {
1100
- sawField = true;
1101
- }
1102
- }
1103
- if (!sawField || !data) return null;
1104
- if (data === '[DONE]') return { done: true };
1105
- try {
1106
- return { json: JSON.parse(data) };
1107
- } catch {
1108
- return null;
1109
- }
1110
- }
1111
-
1112
- /**
1113
- * Read an SSE body, calling onEvent per parsed frame.
1114
- *
1115
- * The stall watchdog is armed PER PAYLOAD, not per socket read. That
1116
- * distinction is the point: a server that keeps the connection warm with
1117
- * keep-alive comments while the model produces nothing would otherwise reset
1118
- * the deadline forever, and the turn would hang with no error at all. Progress
1119
- * means a parsed payload — text, tool call, or usage — and nothing else.
1120
- */
1121
- async function readSSE(res, onEvent, { idleTimeoutMs = SSE_IDLE_TIMEOUT_MS } = {}) {
1122
- const reader = res.body.getReader();
1123
- const decoder = new TextDecoder();
1124
- let buf = '';
1125
- let stallTimer = null;
1126
- let stallReject = () => {};
1127
- const stall = new Promise((_, reject) => {
1128
- stallReject = reject;
1129
- });
1130
- const arm = () => {
1131
- if (stallTimer) clearTimeout(stallTimer);
1132
- stallTimer = setTimeout(() => {
1133
- stallReject(
1134
- new Error(`local: the model stopped responding — no output for ${Math.round(idleTimeoutMs / 1000)}s`)
1135
- );
1136
- }, idleTimeoutMs);
1137
- };
1138
- arm();
1139
- try {
1140
- for (;;) {
1141
- const chunk = await Promise.race([reader.read(), stall]);
1142
- if (chunk.done) break;
1143
- buf += decoder.decode(chunk.value, { stream: true });
1144
- // Normalize CRLF so one separator check covers both spellings.
1145
- buf = buf.replace(/\r\n/g, '\n');
1146
- let idx;
1147
- while ((idx = buf.indexOf('\n\n')) >= 0) {
1148
- const raw = buf.slice(0, idx);
1149
- buf = buf.slice(idx + 2);
1150
- const ev = parseEvent(raw);
1151
- if (!ev) continue; // keep-alive — deliberately does NOT re-arm
1152
- if (ev.done) return;
1153
- if (ev.json) {
1154
- arm();
1155
- onEvent(ev.json);
1156
- }
1157
- }
1158
- }
1159
- } finally {
1160
- if (stallTimer) clearTimeout(stallTimer);
1161
- try {
1162
- await reader.cancel();
1163
- } catch {
1164
- /* already closed */
1165
- }
1166
- }
1167
- }
1168
-
1169
- /**
1170
- * Serializes calls that would land on the SAME daemon+model pair.
1171
- *
1172
- * Ollama loads each model into its own `llama-server` with exactly one
1173
- * request slot (`-np 1`). That slot does not queue a second request that
1174
- * arrives while it is still generating — it CANCELS the in-flight task
1175
- * server-side and answers the first caller with an HTTP 500, after however
1176
- * long that caller had already been waiting (reproduced live: a stuck or
1177
- * merely slow turn on this machine's CPU-only inference, then a second turn
1178
- * — another chat window, a subagent, a retry — lands on the same model and
1179
- * the first caller's request dies with a 500 it cannot act on). Client-side
1180
- * serialization is the only place this can be prevented at all; the daemon
1181
- * itself does not offer a queued mode. Keyed on (base, model) rather than
1182
- * model alone: two different daemons (this box and a LAN box, say) each have
1183
- * their own slot and may run fully in parallel.
1184
- */
1185
- const localCallQueues = new Map(); // "<base>|<model>" -> tail promise (never rejects)
1186
-
1187
- function runSerialized(key, fn) {
1188
- const prev = localCallQueues.get(key) || Promise.resolve();
1189
- // Run `fn` once the previous call for this key has SETTLED, success or
1190
- // failure — a failed turn must not wedge every later turn behind it.
1191
- const settled = prev.then(fn, fn);
1192
- // The map holds a swallowed copy so the next call's `.then` always fires;
1193
- // the caller still gets `settled`, which carries the real result or error.
1194
- localCallQueues.set(
1195
- key,
1196
- settled.then(
1197
- () => {},
1198
- () => {}
1199
- )
1200
- );
1201
- return settled;
1202
- }
1203
-
1204
- // How many calls are CURRENTLY in flight for a given "<base>|<model>" key —
1205
- // not the queue above (which only orders them), but a live count, so the
1206
- // warm-model swap below can tell "idle" from "still serving someone else".
1207
- const activeCountByKey = new Map();
1208
-
1209
- // The one model THIS transport last warmed per daemon — not every model
1210
- // Ollama happens to have loaded (another process could have warmed others),
1211
- // only the ones this app is responsible for cleaning up after. Keyed on the
1212
- // normalized base, same spelling as the call queue above.
1213
- const warmModelByBase = new Map(); // base -> model
1214
-
1215
- /**
1216
- * Swap the daemon's "warm" model for this app from `prevModel` to `model`,
1217
- * evicting the outgoing one — UNLESS something else is still actively using
1218
- * it right now. That guard matters: a second chat window or a subagent still
1219
- * mid-turn on the old model must not be cut out from under itself just
1220
- * because a DIFFERENT turn moved on to a new one. Fired after a turn
1221
- * SUCCEEDS, never before — a model that was never actually reached (a 400, a
1222
- * refusal) must not be recorded as warm.
1223
- *
1224
- * Without this, every model switch in a session left the previous one loaded
1225
- * too: a few turns in, Ollama was running two or three `llama-server`
1226
- * processes at once (one per model, each pinned at ~100%+ CPU on this
1227
- * class of hardware — reproduced live), each fighting the others for the
1228
- * same handful of cores. Capping the daemon at one warm model per app is
1229
- * what keeps that from compounding turn after turn.
1230
- */
1231
- function swapWarmModel(base, model) {
1232
- if (!model) return;
1233
- const prevModel = warmModelByBase.get(base);
1234
- warmModelByBase.set(base, model);
1235
- if (!prevModel || prevModel === model) return;
1236
- if (activeCountByKey.get(`${base}|${prevModel}`)) return; // still in use elsewhere
1237
- unload(base, prevModel).catch(() => {});
1238
- }
1239
-
1240
- /**
1241
- * Unload every model this transport warmed, across every daemon it talked
1242
- * to. Called once, from the app's quit path: "closing the window closes the
1243
- * model" is the point — a local model left loaded after the app exits is
1244
- * pure waste, the one thing the free/local lane was supposed to never cost.
1245
- * Best-effort and bounded, like `unload` itself, so this resolves quickly
1246
- * even against a daemon that is slow or already gone.
1247
- */
1248
- async function unloadAll() {
1249
- const entries = [...warmModelByBase.entries()];
1250
- warmModelByBase.clear();
1251
- await Promise.all(entries.map(([base, model]) => unload(base, model)));
1252
- }
1253
-
1254
- /** The window a pin should ask for: the caller's number, else the operator's,
1255
- * else the default above. `0` is a real answer (pin nothing) and is passed
1256
- * through rather than treated as "unstated". */
1257
- function warmSecsFor({ warmSecs } = {}) {
1258
- const stated = Number(warmSecs);
1259
- if (Number.isFinite(stated) && stated >= 0) return Math.floor(stated);
1260
- const env = Number(process.env && process.env.AEGIS_LOCAL_WARM_SECS);
1261
- if (Number.isFinite(env) && env >= 0) return Math.floor(env);
1262
- return DEFAULT_WARM_SECS;
1263
- }
1264
-
1265
- /**
1266
- * Live-pin memo: which `<base>|<model>|<prefix>` is known warm until when.
1267
- *
1268
- * The pin's cost is a PREFILL, not a turn, so the steady-state win is not
1269
- * making the pin cheaper — it is not issuing one. Residency IS still checked
1270
- * against the daemon (`/api/ps`), because the daemon is the authority on what
1271
- * it holds: another app, an `ollama stop`, or a reboot can all clear a window
1272
- * this process set. But a window this process set, with seconds still left on
1273
- * it, cannot have expired underneath us — so the memo answers without the
1274
- * round trip, and is *dropped* the moment its deadline passes, so it can never
1275
- * claim residency it cannot prove.
1276
- *
1277
- * Keyed by the PREFIX as well as the model, deliberately: a pin only buys
1278
- * anything for the exact text it warmed (llama.cpp reuses the shared prefix and
1279
- * nothing else), so a warm-up for one system prompt must never suppress the pin
1280
- * for a turn sending a different one — that turn would pay the cold prefill the
1281
- * memo claimed to have paid for it.
1282
- *
1283
- * `dialer` is the `fetch` that performed the pin. A different one (a proxy, a
1284
- * test's stub, a re-`require`d daemon handle) is a different daemon and cannot
1285
- * be vouched for by this map.
1286
- */
1287
- const residentMemo = new Map(); // `${base}\0${model}\0${prefix}` -> { until, dialer }
1288
-
1289
- function residentKey(base, model, prefix) {
1290
- return `${base}\u0000${model}\u0000${prefix}`;
1291
- }
1292
-
1293
- /** Seconds of pinned window left for this exact prefix, or 0 when unproven. */
1294
- function residentSecsLeft(base, model, prefix, { dialer = fetch, now = Date.now() } = {}) {
1295
- const hit = residentMemo.get(residentKey(base, model, prefix));
1296
- if (!hit || hit.dialer !== dialer) return 0;
1297
- const left = Math.round((hit.until - now) / 1000);
1298
- if (left <= 0) {
1299
- residentMemo.delete(residentKey(base, model, prefix));
1300
- return 0;
1301
- }
1302
- return left;
1303
- }
1304
-
1305
- /**
1306
- * The live memo entry itself, so a free (no-daemon-contact) memo hit can still
1307
- * report the window the pin observed. The window is stored with the pin rather
1308
- * than re-read because a memo hit is answered with NO daemon contact by design —
1309
- * that is what makes a burst of turns free.
1310
- */
1311
- function residentEntry(base, model, prefix, { dialer = fetch, now = Date.now() } = {}) {
1312
- const hit = residentMemo.get(residentKey(base, model, prefix));
1313
- if (!hit || hit.dialer !== dialer) return null;
1314
- return hit.until - now > 0 ? hit : null;
1315
- }
1316
-
1317
- /**
1318
- * Does a `/api/ps` row name the model the user picked?
1319
- *
1320
- * Matched by prefix because the daemon reports the RESOLVED identity while the
1321
- * row carries whatever the user chose: `llama3.2:1b` for a plain pull, and
1322
- * `name:tag@sha256:…` for a digest-pinned or remotely-pulled one. The digest
1323
- * form is why `@` is here and not only `:` — the rule this replaced claimed to
1324
- * handle it in a comment while matching only the colon.
1325
- *
1326
- * One predicate rather than two copies: it was already duplicated between the
1327
- * residency read and the window read, and a rule stated twice drifts.
1328
- */
1329
- function sameModel(reported, picked) {
1330
- const name = String(reported || '');
1331
- const want = String(picked || '');
1332
- if (!name || !want) return false;
1333
- return name === want || name.startsWith(`${want}:`) || name.startsWith(`${want}@`);
1334
- }
1335
-
1336
- /**
1337
- * How long the standalone window read (`windowOf`) waits. Short on purpose: it is
1338
- * a loopback metadata call (measured at 115µs on this host) that runs BEFORE the
1339
- * turn, so it must never become a place a turn can park. The probe's budget, not
1340
- * the generation's — see `PROBE_TIMEOUT_MS` above for the same reasoning.
1341
- */
1342
- const WINDOW_TIMEOUT_MS = 3000;
1343
-
1344
- /**
1345
- * The window of the LOADED runner for `model`, straight from `/api/ps`, or null.
1346
- *
1347
- * Never throws and never guesses — the same rule `environmentPreamble` follows
1348
- * for a missing field. `/api/ps` is the only honest source: `/api/show` reports
1349
- * the model's TRAINING capacity (`llama.context_length` 131072 for a model this
1350
- * daemon runs at 4096), and fitting a turn to that would not fit it at all.
1351
- *
1352
- * This exists as a fallback rather than as the only read because the pin can fail
1353
- * for reasons that say nothing about the window — a Stop, its own clock, a daemon
1354
- * that will not enumerate models — and a turn that then sent its payload blind
1355
- * would overrun the window and have its SYSTEM row shifted away by llama.cpp's
1356
- * `--context-shift`. The answer to "how big is this window" should not depend on
1357
- * a warm-up succeeding.
1358
- */
1359
- async function windowOf(base, model, { signal } = {}) {
1360
- // Bounded, because this runs BEFORE the turn and unwatched it is precisely the
1361
- // hang this transport already had to fix once: a daemon that accepts the socket
1362
- // and never answers would otherwise park the turn here, before `chatOne`'s own
1363
- // first-byte deadline is even armed, and a request nothing is timing out is a
1364
- // request nothing can fail. The budget is the probe's, not the turn's — this is
1365
- // a loopback metadata call (measured at 115µs on this host) and it must not eat
1366
- // the generation's clock.
1367
- const ctl = new AbortController();
1368
- const onCallerAbort = () => ctl.abort();
1369
- if (signal) {
1370
- if (signal.aborted) return null;
1371
- if (typeof signal.addEventListener === 'function') signal.addEventListener('abort', onCallerAbort, { once: true });
1372
- }
1373
- const timer = setTimeout(() => ctl.abort(), WINDOW_TIMEOUT_MS);
1374
- if (timer && typeof timer.unref === 'function') timer.unref();
1375
- try {
1376
- const res = await fetch(`${base}/api/ps`, { method: 'GET', signal: ctl.signal });
1377
- if (!res || !res.ok || typeof res.json !== 'function') return null;
1378
- const body = await res.json();
1379
- const models = (body && body.models) || [];
1380
- const hit = models.find((m) => sameModel(m && (m.name || m.model), model));
1381
- const win = hit && Number(hit.context_length);
1382
- return Number.isFinite(win) && win > 0 ? win : null;
1383
- } catch {
1384
- return null;
1385
- } finally {
1386
- clearTimeout(timer);
1387
- if (signal && typeof signal.removeEventListener === 'function') {
1388
- signal.removeEventListener('abort', onCallerAbort);
1389
- }
1390
- }
1391
- }
1392
-
1393
- /**
1394
- * Make `model` resident on the local daemon and PIN its window — the extend
1395
- * call this lane was missing.
1396
- *
1397
- * Why this exists, and why the arguments are what they are. The cost of a local
1398
- * turn is a LOAD cost, not a turn cost: a cold first call pays for reading the
1399
- * weights off disk, and every call after it is fast because llama.cpp reuses the
1400
- * KV of the shared prefix. Without a pin this lane ran on the daemon's stock
1401
- * 5-minute window and had no way to extend it: `keep_alive` on
1402
- * `/v1/chat/completions` is accepted and IGNORED (measured, engine-side:
1403
- * 0/120/1800 all yield the same stored window), so the ONLY endpoint that can
1404
- * extend residency is Ollama's native `/api/chat`. Step away for six minutes and
1405
- * the next turn re-bought the entire cold load.
1406
- *
1407
- * `system` is the prefix this pin warms, and it MUST be the turn's own system
1408
- * text — the caller passes `opts.system` straight through from the engine's
1409
- * dispatch, which is the exact string the turn is about to send. Warming
1410
- * anything else is worse than warming nothing: the daemon uses the REQUEST's
1411
- * system message in place of the model's baked Modelfile default, so a pin
1412
- * carrying different text diverges from the turn at the first token, shares no
1413
- * prefix with it, and EVICTS the very cache it was meant to warm. This is not a
1414
- * theory — it is the measured failure mode that made the engine-side pin
1415
- * useless (a mismatched pin cost 1.30s and left the following real turn at
1416
- * 2.26s, where a matching pin cost 0.53s and left it at 0.58s). So: no
1417
- * fabricated "you are a local model" line, no Modelfile guess. Whatever the
1418
- * caller says, or nothing.
1419
- *
1420
- * Three measured constraints are honoured by the order of operations below:
1421
- * * a memo hit is answered with NO daemon contact and no slot (it is free);
1422
- * * a daemon that already holds an adequate window needs no pin (one GET);
1423
- * * a pin that is not needed is not issued, because its prefill is the cost.
1424
- *
1425
- * Never throws and never rejects: the caller is a live turn, and "could not
1426
- * warm" is information, not a failure. A pin that fails is simply not memoized,
1427
- * so the next turn retries instead of trusting a lie.
1428
- */
1429
- async function ensureResident({ baseURL = DEFAULT_BASE, model, system = '', signal, warmSecs, firstByteTimeoutMs } = {}) {
1430
- const base = baseOf(baseURL);
1431
- // The fence covers the warm-up too. `unload` states the same rule ("never
1432
- // dial out, even to clean up"); a helper that dials where the turn may not
1433
- // would be the hole in it. The turn itself (`chatOne`) still does the
1434
- // refusing and erroring — this just does not add a second caller.
1435
- if (!isLocalEndpoint(base)) return { ok: false, skipped: 'remote' };
1436
- if (!model || !String(model).trim()) return { ok: false, skipped: 'no-model' };
1437
-
1438
- const want = warmSecsFor({ warmSecs });
1439
- const prefix = typeof system === 'string' ? system : '';
1440
- const prefixSource = prefix ? 'turn-system' : 'none';
1441
- const shape = { warmSecs: want, prefixSource, prefixChars: prefix.length };
1442
- if (want <= 0) return { ok: false, skipped: 'warm-disabled', ...shape };
1443
-
1444
- const dialer = fetch;
1445
- // Free answer first: a window this process set, still carrying seconds, for
1446
- // THIS prefix. Consulted before any request, so a burst of turns costs
1447
- // nothing at all rather than a loopback GET each.
1448
- const memoLeft = residentSecsLeft(base, model, prefix, { dialer });
1449
- if (memoLeft >= Math.min(want, 60)) {
1450
- const entry = residentEntry(base, model, prefix, { dialer });
1451
- return {
1452
- ok: true,
1453
- already: true,
1454
- memo: true,
1455
- remainingSecs: memoLeft,
1456
- contextLength: (entry && entry.contextLength) || null,
1457
- ...shape,
1458
- };
1459
- }
1460
-
1461
- // The caller's Stop must reach the warm-up too: a user who cancels should not
1462
- // leave a 300s request running behind them.
1463
- const ctl = new AbortController();
1464
- const onCallerAbort = () => ctl.abort();
1465
- if (signal) {
1466
- if (signal.aborted) ctl.abort();
1467
- else if (typeof signal.addEventListener === 'function') {
1468
- signal.addEventListener('abort', onCallerAbort, { once: true });
1469
- }
1470
- }
1471
- // The pin's own clock — ONE timer for the whole pin (the residency GET and
1472
- // the warm-up POST share it), so a daemon that answers one and stalls the
1473
- // other cannot spend the budget twice. A caller that STATED a window is
1474
- // honoured exactly, never widened: the turn must fail on the caller's clock,
1475
- // not on the pin's.
1476
- const stated = Number(firstByteTimeoutMs);
1477
- const budgetMs = Number.isFinite(stated) && stated > 0 ? Math.min(stated, RESIDENT_TIMEOUT_MS) : RESIDENT_TIMEOUT_MS;
1478
- let pinTimedOut = false;
1479
- // The window of the LOADED runner, read from `/api/ps` below and handed back
1480
- // to the caller so the turn it warms can be fitted to it. `/api/ps` is the
1481
- // only honest source: `/api/show` reports the model's training capacity
1482
- // (`llama.context_length` 131072 on a model running at 4096), and a window
1483
- // guessed from that is worse than no window at all.
1484
- let daemonContext = null;
1485
- const timer = setTimeout(() => {
1486
- pinTimedOut = true;
1487
- ctl.abort();
1488
- }, budgetMs);
1489
- if (timer && typeof timer.unref === 'function') timer.unref();
1490
-
1491
- try {
1492
- // Is the daemon already holding this model long enough? One GET, and a
1493
- // "yes" is worth the whole pin: the prefill is the cost we are avoiding.
1494
- try {
1495
- const ps = await fetch(`${base}/api/ps`, { method: 'GET', signal: ctl.signal });
1496
- if (ps && ps.ok && typeof ps.json === 'function') {
1497
- const body = await ps.json();
1498
- const models = (body && body.models) || [];
1499
- // Matched by the shared predicate: /api/ps reports the RESOLVED
1500
- // identity ("llama3.2:1b", or "name:tag@sha256:…" for a digest-pinned
1501
- // pull) while the row carries whatever the user picked.
1502
- const hit = models.find((m) => sameModel(m && (m.name || m.model), model));
1503
- // The loaded runner's window, when the daemon states one.
1504
- if (hit && Number.isFinite(hit.context_length) && hit.context_length > 0) {
1505
- daemonContext = hit.context_length;
1506
- }
1507
- if (hit && hit.expires_at) {
1508
- const left = Math.round((new Date(hit.expires_at).getTime() - Date.now()) / 1000);
1509
- if (Number.isFinite(left) && left >= Math.min(want, 60)) {
1510
- // A proven window is worth hinting to the memo — but only for the
1511
- // prefix the caller is about to send, which is the one key this
1512
- // hint is filed under.
1513
- residentMemo.set(residentKey(base, model, prefix), { until: Date.now() + left * 1000, dialer, contextLength: daemonContext });
1514
- return { ok: true, already: true, remainingSecs: left, contextLength: daemonContext, ...shape };
1515
- }
1516
- }
1517
- }
1518
- } catch {
1519
- /* a daemon that will not enumerate is pinned blind, below */
1520
- }
1521
- if (ctl.signal.aborted) {
1522
- // The caller's Stop, or the pin's own clock. Neither is a turn failure:
1523
- // the turn below runs and reports its OWN outcome, on the caller's terms.
1524
- return { ok: false, aborted: true, timeout: pinTimedOut, contextLength: daemonContext, ...shape };
1525
- }
1526
-
1527
- const messages = prefix
1528
- ? [{ role: 'system', content: prefix }, { role: 'user', content: 'PONG' }]
1529
- : [{ role: 'user', content: 'PONG' }];
1530
- const res = await fetch(`${base}/api/chat`, {
1531
- method: 'POST',
1532
- headers: { 'content-type': 'application/json' },
1533
- body: JSON.stringify({
1534
- model: String(model),
1535
- // The native field that actually works — see this function's note.
1536
- keep_alive: want,
1537
- stream: false,
1538
- messages,
1539
- // One token: enough to force the full prefill of the prefix, cheap
1540
- // enough that the generation half of the call is free.
1541
- options: { num_predict: 1 },
1542
- }),
1543
- signal: ctl.signal,
1544
- });
1545
-
1546
- // Read the body AND keep the daemon's own accounting from it. Ollama does
1547
- // not bill a pin, but it does REPORT one, and `prompt_eval_count` is the
1548
- // only honest measure of what the extend cost — throwing it away makes "is
1549
- // the pin expensive?" a matter of opinion rather than a number.
1550
- let promptTokens = null;
1551
- let outputTokens = null;
1552
- if (res && typeof res.text === 'function') {
1553
- const raw = await res.text().catch(() => '');
1554
- try {
1555
- const parsed = JSON.parse(raw);
1556
- if (Number.isFinite(parsed.prompt_eval_count)) promptTokens = parsed.prompt_eval_count;
1557
- if (Number.isFinite(parsed.eval_count)) outputTokens = parsed.eval_count;
1558
- } catch {
1559
- /* a non-JSON body is still a successful pin */
1560
- }
1561
- }
1562
-
1563
- if (res && res.ok) {
1564
- // A COLD load is why this re-read exists. The window is read from
1565
- // `/api/ps` at the top of this function, and on a cold load the daemon has
1566
- // no runner to report — the pin's own request is what loads it, so the
1567
- // window was unknown at the moment it was asked for and the turn that
1568
- // follows would have gone out unfitted. Measured live: nothing loaded ->
1569
- // the pin reported `contextLength: null` while returning `ok: true`, and
1570
- // the turn sent 4516 estimated tokens at a window of 4096. One GET after
1571
- // the load closes it, on the cold path only.
1572
- if (daemonContext == null) {
1573
- daemonContext = (await windowOf(base, model, { signal: ctl.signal })) || null;
1574
- }
1575
- residentMemo.set(residentKey(base, model, prefix), { until: Date.now() + want * 1000, dialer, contextLength: daemonContext });
1576
- // Record it as this app's warm model the moment the daemon ACCEPTS it,
1577
- // not only after a successful turn. Otherwise a turn that fails right
1578
- // after its pin (a 400, a Stop, a full context window) leaves a loaded
1579
- // model that `unloadAll` never learned about — exactly the leak
1580
- // `unloadAll` exists to close. `swapWarmModel` is idempotent, so the
1581
- // post-turn call is still the one that keeps the "only a successful turn
1582
- // moves the warm model" contract for the SWITCH this makes.
1583
- swapWarmModel(base, model);
1584
- }
1585
- return {
1586
- ok: !!(res && res.ok),
1587
- status: res && res.status,
1588
- pinned: true,
1589
- promptTokens,
1590
- outputTokens,
1591
- // The window the turn that follows will actually be given, so it can be
1592
- // fitted to it. Null when the daemon did not state one.
1593
- contextLength: daemonContext,
1594
- ...shape,
1595
- };
1596
- } catch (e) {
1597
- // Never fatal. A turn is better than a pin, always — the caller runs the
1598
- // turn regardless of what this returns.
1599
- return { ok: false, error: String((e && e.message) || e), timeout: pinTimedOut, contextLength: daemonContext, ...shape };
1600
- } finally {
1601
- clearTimeout(timer);
1602
- if (signal && typeof signal.removeEventListener === 'function') {
1603
- signal.removeEventListener('abort', onCallerAbort);
1604
- }
1605
- }
1606
- }
1607
-
1608
- /**
1609
- * The result a turn the user STOPPED resolves to. Every transport the engine
1610
- * dispatches to resolves an aborted request with an empty, well-formed answer
1611
- * (see the engine's own guard: "an aborted request resolves with exactly that —
1612
- * no text"), and this makes the local lane do the same instead of rejecting
1613
- * with the transport's raw AbortError.
1614
- *
1615
- * `content` carries whatever had already streamed, because every other
1616
- * transport returns the partial it collected and the renderer has already
1617
- * painted those deltas — dropping them here would make the transcript
1618
- * disagree with the bubble. Tool calls are deliberately NOT replayed: a turn
1619
- * that was stopped is over, and the engine drops tools for an aborted turn
1620
- * anyway, so listing them would only put an unexecuted call in the transcript.
1621
- */
1622
- function stoppedTurn(model, { content = '', usage = null } = {}) {
1623
- const result = {
1624
- id: 'local',
1625
- model: String(model),
1626
- choices: [{ index: 0, message: { role: 'assistant', content }, finish_reason: 'stop' }],
1627
- };
1628
- if (usage) result.usage = usage;
1629
- return result;
1630
- }
1631
-
1632
- /**
1633
- * Keep a turn's wire payload inside the window the daemon actually granted.
1634
- *
1635
- * Ollama's OpenAI shim is the endpoint this transport talks to, and it can
1636
- * ACCEPT `num_ctx` without honouring it. Measured on this host: `options.num_ctx`
1637
- * AND a top-level `num_ctx` on `/v1/chat/completions` both returned 200 and left
1638
- * the loaded window untouched at 4096, while native `/api/chat` moved it
1639
- * 4096 → 8192 — by RELOADING the runner. Asking this lane for a bigger window
1640
- * therefore does not work, and asking for one that the next `/v1` turn
1641
- * contradicts is worse than not asking: `/api/ps` shows the runner respawn
1642
- * (pid 200467 at n_ctx 8192 → pid 200484 at 4096), and a respawn discards the
1643
- * very prefix the pin just warmed.
1644
- *
1645
- * What remains is the other half of the same problem — not OVERRUNNING the
1646
- * window we are given. llama-server runs with `--context-shift --keep 4` (its own
1647
- * command line, read from the daemon log), which does not fail on an oversized
1648
- * prompt: it silently discards the OLDEST tokens to make room. The oldest tokens
1649
- * are the system row — the persona, the work rules and the tool schemas — so an
1650
- * overrun costs the model its instructions mid-turn and it answers as though it
1651
- * had never been given any. That is what "ollama is broken" looked like.
1652
- *
1653
- * The window is READ, never guessed. `/api/show` is not a source for it: it
1654
- * reports the model's TRAINING capacity (`llama.context_length` 131072 on a
1655
- * model this daemon runs at 4096). The only honest source is `/api/ps`, which
1656
- * reports the window of the LOADED runner — and `ensureResident` already makes
1657
- * that request. No reported window means no trimming and no guessing, which is
1658
- * today's behaviour.
1659
- *
1660
- * Returns null when there is no window to fit against, so the caller can leave
1661
- * the payload exactly as it was.
1662
- */
1663
- function fitToWindowOf({ rows, system, tools, contextLength, maxTokens }) {
1664
- const win = Number(contextLength);
1665
- if (!Number.isFinite(win) || win <= 0) return null;
1666
- const history = (Array.isArray(rows) ? rows : []).filter((r) => r && r.role !== 'system');
1667
- // Tool schemas are wire material the model has to see, so they are charged
1668
- // before any history is kept. Otherwise the trimmer believes a 4096-token
1669
- // window has 4096 tokens free when ~1971 of them are already spoken for
1670
- // (measured: 10 schemas, 7882 chars ≈ 1971 tokens — almost half the window,
1671
- // spent before the user's first word).
1672
- const fixedTokens = Array.isArray(tools) && tools.length ? estimateTokens(JSON.stringify(tools)) : 0;
1673
- // The reply needs room to exist. A stated cap is honoured exactly; with none,
1674
- // a quarter of the window — a turn that fills the window exactly leaves
1675
- // nowhere to emit into, and the daemon starts shifting the prompt to write.
1676
- const stated = Number(maxTokens);
1677
- const reserveTokens = Number.isFinite(stated) && stated > 0 ? stated : Math.round(win / 4);
1678
- const trimmed = trimContext({
1679
- messages: history,
1680
- system: typeof system === 'string' ? system : '',
1681
- budgetTokens: win,
1682
- reserveTokens,
1683
- fixedTokens,
1684
- });
1685
- return {
1686
- window: win,
1687
- reserveTokens,
1688
- toolTokens: fixedTokens,
1689
- dropped: trimmed.dropped,
1690
- messages: trimmed.messages,
1691
- // Still over after dropping every turn it could: the newest turn alone does
1692
- // not fit. Reported rather than hidden — the daemon will shift the prompt
1693
- // instead, and which turn did it is worth knowing.
1694
- over: trimmed.estimatedTokens > Math.max(0, win - reserveTokens),
1695
- estimatedTokens: trimmed.estimatedTokens,
1696
- };
1697
- }
1698
-
1699
- /**
1700
- * One streaming turn against a local model. Keyless by construction — there is
1701
- * no credential to send, which is a second reason a remote URL here could never
1702
- * be billed.
1703
- *
1704
- * Returns the OpenAI shape the engine reads: `choices[0].message.content` and
1705
- * `choices[0].message.tool_calls`.
1706
- */
1707
- async function chatOne({
1708
- baseURL = DEFAULT_BASE,
1709
- model,
1710
- messages,
1711
- system,
1712
- prompt,
1713
- maxTokens,
1714
- temperature,
1715
- tools,
1716
- toolChoice,
1717
- signal,
1718
- onDelta,
1719
- firstByteTimeoutMs = FIRST_BYTE_TIMEOUT_MS,
1720
- // The window the daemon actually granted, read from `/api/ps` by the pin
1721
- // (`ensureResident`) and threaded through here. Absent when the daemon did not
1722
- // report one — in which case nothing is trimmed and nothing is guessed.
1723
- contextLength,
1724
- } = {}) {
1725
- const base = baseOf(baseURL);
1726
- const refusal = remoteRefusal(base);
1727
- if (refusal) throw refusal;
1728
- if (!model || !String(model).trim()) {
1729
- const err = new Error('local: no model selected — pick one from the model list, or type a tag');
1730
- err.status = 400;
1731
- throw err;
1732
- }
1733
-
1734
- const rows = [];
1735
- if (system) rows.push({ role: 'system', content: system });
1736
- for (const m of Array.isArray(messages) ? messages : []) {
1737
- if (m && m.role === 'system') continue; // never two system rows
1738
- const row = normalizeRow(m);
1739
- if (row) rows.push(row);
1740
- }
1741
- // The engine passes the user's own text as `prompt` alongside the history.
1742
- // Append it only if the history does not already end with it, so a caller
1743
- // that includes it cannot send the same turn twice.
1744
- const p = typeof prompt === 'string' ? prompt : '';
1745
- if (p) {
1746
- const last = rows[rows.length - 1];
1747
- if (!(last && last.role === 'user' && last.content === p)) {
1748
- rows.push({ role: 'user', content: p });
1749
- }
1750
- }
1751
- if (!rows.length) {
1752
- const err = new Error('local: nothing to send — no prompt and no messages');
1753
- err.status = 400;
1754
- throw err;
1755
- }
1756
-
1757
- // Fit the turn to the window the daemon actually granted. See
1758
- // `fitToWindowOf` for why this is the transport's job rather than the
1759
- // caller's, and why the window is read rather than requested.
1760
- let fit = null;
1761
- if (contextLength != null) {
1762
- const sysRow = rows[0] && rows[0].role === 'system' ? rows[0] : null;
1763
- fit = fitToWindowOf({ rows, system: (sysRow && sysRow.content) || system || '', tools, contextLength, maxTokens });
1764
- if (fit) {
1765
- const kept = fit.messages;
1766
- rows.length = 0;
1767
- if (sysRow) rows.push(sysRow);
1768
- rows.push(...kept);
1769
- }
1770
- }
1771
-
1772
- const body = {
1773
- model: String(model),
1774
- messages: rows,
1775
- stream: true,
1776
- // Ask for usage even on a stream: Ollama's shim reports it when asked, and
1777
- // without the ask a local turn's token count is invisible in the UI.
1778
- stream_options: { include_usage: true },
1779
- ...(maxTokens ? { max_tokens: maxTokens } : {}),
1780
- ...(temperature != null ? { temperature } : {}),
1781
- ...(tools && tools.length ? { tools } : {}),
1782
- ...(toolChoice ? { tool_choice: toolChoice } : {}),
1783
- };
1784
-
1785
- // The caller's abort is FORWARDED to our own controller rather than handed
1786
- // straight to `fetch`, so one request can carry both the caller's intent and
1787
- // the first-byte deadline below. It stays attached for the whole turn, not
1788
- // just the header wait: aborting after headers is what tears down the body
1789
- // mid-stream, which is how Stop reaches a generation already in flight.
1790
- const ctl = new AbortController();
1791
- let firstByteTimedOut = false;
1792
- const firstByteTimer = setTimeout(() => {
1793
- firstByteTimedOut = true;
1794
- ctl.abort();
1795
- }, firstByteTimeoutMs);
1796
- // Never the reason a short-lived CLI process (`aegiscode -p "…"`) refuses to
1797
- // exit, same rule the cloud client's boundedFetch follows.
1798
- if (firstByteTimer && typeof firstByteTimer.unref === 'function') firstByteTimer.unref();
1799
- const onCallerAbort = () => ctl.abort();
1800
- if (signal) {
1801
- if (signal.aborted) ctl.abort();
1802
- else if (typeof signal.addEventListener === 'function') {
1803
- signal.addEventListener('abort', onCallerAbort, { once: true });
1804
- }
1805
- }
1806
-
1807
- let res;
1808
- try {
1809
- res = await fetch(`${base}/v1/chat/completions`, {
1810
- method: 'POST',
1811
- headers: { 'content-type': 'application/json' },
1812
- body: JSON.stringify(body),
1813
- signal: ctl.signal,
1814
- });
1815
- } catch (err) {
1816
- if (firstByteTimedOut || isHeadersDeadline(err)) {
1817
- // Two different clocks can end this wait, and the user should be told
1818
- // which one did — see `isHeadersDeadline` for the second, which is not
1819
- // ours and was reaching the host as an opaque `TypeError: fetch failed`.
1820
- const whose = firstByteTimedOut
1821
- ? `sent nothing in ${Math.round(firstByteTimeoutMs / 1000)}s`
1822
- : `sent no response headers before this runtime's own ${Math.round(HEADERS_DEADLINE_MS / 1000)}s deadline`;
1823
- const e = new Error(
1824
- `local: ${model} ${whose} — the daemon is still loading the model, or its prompt ` +
1825
- 'prefill is longer than that. `ollama ps` shows which; a smaller prompt or a smaller ' +
1826
- 'model is the way through.'
1827
- );
1828
- // The shape the cloud client gives its own timeout (status 0 + a code),
1829
- // so "waited too long" stays one branch in the renderer for every class.
1830
- e.status = 0;
1831
- e.code = 'timeout';
1832
- throw e;
1833
- }
1834
- // A STOPPED turn is not a failed one, and it must not look like one. The
1835
- // engine states this contract for every transport it dispatches to
1836
- // (engine.js: "an aborted request resolves with exactly that — no text",
1837
- // the guard that stops an interrupted turn from billing a second call).
1838
- // The cloud client satisfies it; this transport rejected with the raw
1839
- // AbortError instead, so pressing Stop on a local turn surfaced in the host
1840
- // as `Error occurred in handler for 'model:chat': DOMException [AbortError]`
1841
- // — an error dialog and a stack for the one action the user chose. Nothing
1842
- // was produced to preserve here: no byte of the response was ever read.
1843
- if ((signal && signal.aborted) || ctl.signal.aborted) return stoppedTurn(model);
1844
- throw err;
1845
- } finally {
1846
- // Time-to-headers only, the same contract boundedFetch documents: once the
1847
- // headers are here the per-payload stall timer takes over, so a
1848
- // legitimately long generation is never cut off by this one.
1849
- clearTimeout(firstByteTimer);
1850
- }
1851
-
1852
- if (!res.ok) {
1853
- // Body text, not just the status: a local daemon's 400 says which field it
1854
- // disliked (commonly `tools` on an older build), and the engine retries
1855
- // without tool schemas on exactly that signal.
1856
- let text = '';
1857
- try {
1858
- text = await res.text();
1859
- } catch {
1860
- /* body already gone */
1861
- }
1862
- const err = new Error(`local: chat/completions ${res.status}${text ? ` — ${text.slice(0, 400)}` : ''}`);
1863
- err.status = res.status;
1864
- throw err;
1865
- }
1866
-
1867
- let content = '';
1868
- let usage = null;
1869
- const toolCalls = [];
1870
- const byIndex = new Map();
1871
- const stripEnvelope = makeDeepSeekEnvelopeStripper();
1872
- // Text-form calls the templates emit that Ollama's shim does not structure
1873
- // for us (see makeTextToolCallStripper). Chained AFTER the DSML pass: DSML
1874
- // parameter bodies are swallowed by that pass, so a file being written can
1875
- // never be scanned — and misread — as a call. The offered tool names are
1876
- // handed over as the guard on the bare-JSON rule: only a name this turn
1877
- // actually offered is allowed to turn text into a call.
1878
- const stripTextCalls = makeTextToolCallStripper({
1879
- toolNames: (Array.isArray(tools) ? tools : [])
1880
- .map((t) => (t && t.function && t.function.name) || (t && t.name) || '')
1881
- .filter(Boolean),
1882
- });
1883
-
1884
- try {
1885
- await readSSE(
1886
- res,
1887
- (json) => {
1888
- if (json && json.usage) usage = json.usage;
1889
- const choice = json && Array.isArray(json.choices) ? json.choices[0] : null;
1890
- if (!choice) return;
1891
- const delta = choice.delta || {};
1892
- if (typeof delta.content === 'string' && delta.content) {
1893
- const clean = stripTextCalls(stripEnvelope(delta.content));
1894
- if (clean) {
1895
- content += clean;
1896
- if (typeof onDelta === 'function') onDelta({ text: clean });
1897
- }
1898
- }
1899
- const frags = delta.tool_calls || [];
1900
- for (const f of frags) {
1901
- const i = Number.isInteger(f.index) ? f.index : toolCalls.length;
1902
- let acc = byIndex.get(i);
1903
- if (!acc) {
1904
- acc = { id: '', type: 'function', function: { name: '', arguments: '' } };
1905
- byIndex.set(i, acc);
1906
- }
1907
- if (f.id) acc.id = f.id;
1908
- if (f.type) acc.type = f.type;
1909
- const fn = f.function || {};
1910
- if (fn.name) acc.function.name = fn.name;
1911
- if (fn.arguments) acc.function.arguments += fn.arguments;
1912
- }
1913
- },
1914
- { idleTimeoutMs: SSE_IDLE_TIMEOUT_MS }
1915
- );
1916
- } catch (err) {
1917
- // Stopped mid-stream. The forwarded abort tears the body down, so the read
1918
- // loop rejects with AbortError — resolve with what already streamed
1919
- // instead of turning the user's own Stop into a host-side error, and let
1920
- // the tail below stay unreached (an aborted stream has no unfinished
1921
- // envelope worth flushing into an answer the user cancelled).
1922
- if (signal && signal.aborted) return stoppedTurn(model, { content, usage });
1923
- throw err;
1924
- } finally {
1925
- if (signal && typeof signal.removeEventListener === 'function') {
1926
- signal.removeEventListener('abort', onCallerAbort);
1927
- }
1928
- }
1929
-
1930
- // A trailing '|…' that never closed (stream ended right after it, or it
1931
- // was real prose all along) is real content, not an unfinished envelope
1932
- // token — release it rather than dropping it silently.
1933
- const strayTail = stripTextCalls(stripEnvelope.flush());
1934
- const textTail = stripTextCalls.flush();
1935
- const tail = `${strayTail || ''}${textTail || ''}`;
1936
- if (tail) {
1937
- content += tail;
1938
- if (typeof onDelta === 'function') onDelta({ text: tail });
1939
- }
1940
-
1941
- for (const [i, acc] of [...byIndex.entries()].sort((a, b) => a[0] - b[0])) {
1942
- if (!acc.function.name && !acc.function.arguments) continue;
1943
- // Ollama does not always mint an id; the engine's tool loop keys results
1944
- // by it, so an absent one is filled rather than left to collapse every
1945
- // call in the turn onto a single empty key.
1946
- if (!acc.id) acc.id = `call_${i}`;
1947
- toolCalls.push(acc);
1948
- }
1949
-
1950
- const message = { role: 'assistant', content };
1951
- if (toolCalls.length) message.tool_calls = toolCalls;
1952
-
1953
- // DSML calls arrive as plain text, decoded by stripEnvelope rather than
1954
- // accumulated by index like structured delta.tool_calls — merged into one
1955
- // normalised `{ id, name, args }` list so a call that arrived as DSML (a
1956
- // locally-pulled deepseek-* model with no provider-native delta.tool_calls
1957
- // at all) is executed like any other. engine.js's extractToolCalls() reads
1958
- // this field first, ahead of message.tool_calls.
1959
- const dsmlCalls = stripEnvelope.calls();
1960
- // Calls the model wrote as text rather than in the structured field — the
1961
- // same merge, because from here on they ARE tool calls: the engine's
1962
- // extractToolCalls() reads this field first, ahead of message.tool_calls.
1963
- const textCalls = stripTextCalls.calls();
1964
- const result = {
1965
- id: 'local',
1966
- model: String(model),
1967
- choices: [{ index: 0, message, finish_reason: toolCalls.length ? 'tool_calls' : 'stop' }],
1968
- ...(usage ? { usage } : {}),
1969
- };
1970
- if (toolCalls.length || dsmlCalls.length || textCalls.length) {
1971
- result.toolCalls = [
1972
- ...toolCalls.map((c) => ({ id: c.id, name: c.function.name, args: c.function.arguments })),
1973
- ...dsmlCalls.map((c) => ({ id: '', name: c.name, args: c.args || {} })),
1974
- ...textCalls.map((c) => ({ id: '', name: c.name, args: c.args || {} })),
1975
- ];
1976
- if ((dsmlCalls.length || textCalls.length) && result.choices[0].finish_reason !== 'tool_calls') {
1977
- result.choices[0].finish_reason = 'tool_calls';
1978
- }
1979
- }
1980
- // What the fit cost, reported rather than hidden: the window the daemon
1981
- // granted, the fixed wire cost the tool schemas took out of it, and how much
1982
- // history had to be dropped to stay inside. `over` is the honest failure — the
1983
- // newest turn alone does not fit, so the daemon will shift the prompt instead.
1984
- if (fit) {
1985
- result.window = {
1986
- contextLength: fit.window,
1987
- dropped: fit.dropped,
1988
- toolTokens: fit.toolTokens,
1989
- estimatedTokens: fit.estimatedTokens,
1990
- reserveTokens: fit.reserveTokens,
1991
- over: fit.over,
1992
- };
1993
- }
1994
- return result;
1995
- }
1996
-
1997
- /**
1998
- * Public entry point: `chatOne` serialized against every other call that
1999
- * would land on the same daemon+model slot (see `runSerialized` above). A
2000
- * caller that wants true concurrency points at a different model or a
2001
- * different daemon — both get their own key and run in parallel.
2002
- *
2003
- * Also tracks how many calls are live for this key (`activeCountByKey`) and,
2004
- * once this one succeeds, swaps the daemon's warm model over to it — see
2005
- * `swapWarmModel`. The count is kept here rather than inside `runSerialized`
2006
- * because it has to span the whole call (queueing included): a model is
2007
- * still "in use" while queued behind another turn on the same slot, not only
2008
- * while its own fetch is running.
2009
- */
2010
- async function chat(opts = {}) {
2011
- const base = baseOf(opts && opts.baseURL);
2012
- const model = opts && opts.model;
2013
- const key = `${base}|${model}`;
2014
- activeCountByKey.set(key, (activeCountByKey.get(key) || 0) + 1);
2015
- try {
2016
- // The pin runs INSIDE the same slot as the turn it warms, deliberately.
2017
- // Two reasons, both load-bearing:
2018
- // * the daemon's single request slot is per model (see `runSerialized`),
2019
- // so a pin issued beside the turn rather than before it would be the
2020
- // second overlapping request that hangs the loser;
2021
- // * a pin that warmed the prefix and then released the slot, letting
2022
- // another call's model in, would warm a cache that is gone by the time
2023
- // this turn sends its first byte.
2024
- // `pin` is held in the closure so the turn's own error path is untouched:
2025
- // a failed pin never becomes a failed turn.
2026
- let pin = null;
2027
- const result = await runSerialized(key, async () => {
2028
- pin = await ensureResident({
2029
- baseURL: opts && opts.baseURL,
2030
- model,
2031
- // The turn's OWN system text — the engine hands it down as `system`
2032
- // and `chatOne` sends it as the first row, so warming it is warming
2033
- // the exact prefix this turn will re-send.
2034
- system: opts && opts.system,
2035
- signal: opts && opts.signal,
2036
- warmSecs: opts && opts.warmSecs,
2037
- // The turn's OWN first-byte bound, so a daemon that accepts the socket
2038
- // and never answers fails the turn on the CALLER's clock. Without it
2039
- // the pin sat on a budget of its own and the turn's 1s deadline was not
2040
- // even armed yet — the pin silently re-armed the hang it exists to
2041
- // prevent.
2042
- firstByteTimeoutMs: opts && opts.firstByteTimeoutMs,
2043
- });
2044
- // The turn is fitted to the window the pin READ, never to one requested —
2045
- // see `fitToWindowOf` for the measurement that closed that door.
2046
- //
2047
- // Read directly ONLY when the pin did not answer the residency question:
2048
- // * a pin that SUCCEEDED is the answer, and a memo hit especially so —
2049
- // that path's whole contract is "no daemon contact and no slot", so
2050
- // re-reading here would turn a free turn into a dialing one;
2051
- // * a remote URL is never dialed from this lane at all (fail-closed, the
2052
- // rule `remoteRefusal` enforces), and a helper that dialed where the
2053
- // turn may not would be the hole in it. `ensureResident` states the
2054
- // same rule for the same reason.
2055
- // Everything else gets one cheap local GET, because the size of the window
2056
- // is a fact about the daemon rather than about the warm-up having worked:
2057
- // a pin that was ATTEMPTED and came back empty-handed (its own clock, an
2058
- // error, a daemon that would not enumerate) still wants the turn fitted, and
2059
- // it already dialed, so one more read costs nothing it has not already
2060
- // spent.
2061
- //
2062
- // Two things deliberately do NOT qualify, and both are existing contracts
2063
- // this lane must not quietly widen:
2064
- // * a pin that was SKIPPED — `warm-disabled` especially, where "nothing to
2065
- // check residency for, so not even the GET is issued" is asserted in
2066
- // test/local-resident.test.mjs. Trading that for a fitted turn would be
2067
- // a real change to a documented behaviour for the benefit of an opt-out
2068
- // configuration, so the skip stands and a warm-disabled turn is sent
2069
- // unfitted. The default is warm ON (DEFAULT_WARM_SECS), which is where
2070
- // the fitting happens.
2071
- // * a turn that will not be sent at all. The refusals `chatOne` raises
2072
- // BEFORE dialing (an empty model, a remote URL) must not be preceded by
2073
- // a request of ours, or "refused before dialing" stops being true.
2074
- let contextLength = (pin && pin.contextLength) || null;
2075
- const willSend = isLocalEndpoint(base) && String(model || '').trim() !== '';
2076
- if (!contextLength && willSend && pin && pin.ok !== true && !pin.skipped) {
2077
- contextLength = (await windowOf(base, model, { signal: opts && opts.signal })) || null;
2078
- }
2079
- return chatOne({ ...opts, contextLength });
2080
- });
2081
- swapWarmModel(base, model);
2082
- // What the extend cost, reported rather than discarded — the daemon's own
2083
- // `prompt_eval_count` for the prefill it did. Deliberately NOT folded into
2084
- // `usage`: the pin is not part of the turn's token bill, and adding it
2085
- // would report tokens the user's turn never spent.
2086
- if (pin && typeof result === 'object' && result) result.residency = pin;
2087
- return result;
2088
- } finally {
2089
- const remaining = (activeCountByKey.get(key) || 1) - 1;
2090
- if (remaining <= 0) activeCountByKey.delete(key);
2091
- else activeCountByKey.set(key, remaining);
2092
- }
2093
- }
2094
-
2095
- module.exports = {
2096
- DEFAULT_BASE,
2097
- DEFAULT_WARM_SECS,
2098
- RESIDENT_TIMEOUT_MS,
2099
- SSE_IDLE_TIMEOUT_MS,
2100
- FIRST_BYTE_TIMEOUT_MS,
2101
- PROBE_TIMEOUT_MS,
2102
- baseOf,
2103
- normalizeBase,
2104
- isLocalEndpoint,
2105
- remoteRefusal,
2106
- probe,
2107
- listTags,
2108
- readSSE,
2109
- chat,
2110
- ensureResident,
2111
- residentSecsLeft,
2112
- residentEntry,
2113
- unload,
2114
- unloadAll,
2115
- // Exported for this transport's own tests. The text-form tool-call decoders
2116
- // are pure, and the only honest way to test a streaming decoder is chunk by
2117
- // chunk — see test/local-tool-text.test.mjs.
2118
- makeTextToolCallStripper,
2119
- makeDeepSeekEnvelopeStripper,
2120
- textCallsIn,
2121
- // The window fit, exported for the same reason: it decides what leaves this
2122
- // machine, and its arithmetic (fixed tool cost charged before history, reserve
2123
- // for the reply, never a guessed window) is worth asserting rather than
2124
- // eyeballing.
2125
- fitToWindowOf,
2126
- // The runtime's own response-header deadline, translated into this lane's
2127
- // timeout shape. Exported because the shape it produces is the engine's
2128
- // contract, and the real error it recognises is a `fetch failed` wrapper —
2129
- // worth asserting on the wrapper rather than on a string.
2130
- isHeadersDeadline,
2131
- HEADERS_DEADLINE_MS,
2132
- windowOf,
2133
- // Exported so a test can assert the STOP path's contract (a stopped turn
2134
- // resolves; it never rejects) against the real helper rather than against a
2135
- // copy of its shape.
2136
- stoppedTurn,
2137
- };
3
+ "use strict";const{estimateTokens:estimateTokens,trimContext:trimContext}=require("./context.js"),DEFAULT_BASE="http://localhost:11434",SSE_IDLE_TIMEOUT_MS=3e5,FIRST_BYTE_TIMEOUT_MS=SSE_IDLE_TIMEOUT_MS,HEADERS_DEADLINE_MS=3e5;function isHeadersDeadline(e){if(!e)return!1;const t=e.cause||e;if(!t)return!1;const n=String(t.code||""),s=String(t.name||"");return"UND_ERR_HEADERS_TIMEOUT"===n||"HeadersTimeoutError"===s}const PROBE_TIMEOUT_MS=1500;function normalizeBase(e){const t=String(null==e?"":e).trim();return t?/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(t)?t:`http://${t}`:DEFAULT_BASE}function baseOf(e){return normalizeBase(e).replace(/\/+$/,"")}const DSML_RE_INVOKE=/<\s*|\s*(?:DSML\s*)?|\s*(?:invoke|call|tool_call)\b\s*([^>]*?)\s*>([\s\S]*?)<\s*\/\s*|\s*(?:DSML\s*)?|\s*(?:invoke|call|tool_call)\b\s*>/g,DSML_RE_PARAM=/<\s*|\s*(?:DSML\s*)?|\s*(?:parameter|param|arg)\b\s*([^>]*?)\s*>([\s\S]*?)<\s*\/\s*|\s*(?:DSML\s*)?|\s*(?:parameter|param|arg)\b\s*>/g,DSML_RE_LEADER=/<\s*\/?\s*|\s*(?:DSML\s*)?|\s*(?:(?:tool|function)[_▁ ]*calls?|reasoning)[_▁ ]*(?:begin|end)?\s*>/gi,DSML_RE_ANY_TAG=/<\s*\/?\s*|\s*(?:DSML\s*)?|\s*[A-Za-z_][A-Za-z0-9_]*[^>]{0,120}>/g,DSML_RE_SKELETON=/<\s*\/?\s*(?:tool_calls|function_calls|invoke|parameter|param|reasoning)\b[^>]{0,120}>/gi,DSML_RE_JSON_CALLS=/|[ \t]*(?:tool|function)[_▁ ]*calls?[_▁ ]*begin[ \t]*|([\s\S]*?)|[ \t]*(?:tool|function)[_▁ ]*calls?[_▁ ]*end[ \t]*|/gi,DSML_RE_JSON_CALLS_OPEN=/|[ \t]*(?:tool|function)[_▁ ]*calls?[_▁ ]*begin[ \t]*|/gi,DSML_RE_JSON_CALLS_CLOSE=/|[ \t]*(?:tool|function)[_▁ ]*calls?[_▁ ]*end[ \t]*|/gi,DSML_RE_ENVELOPE_TOKEN=/|[ \t]?[A-Za-z0-9_▁]*[ \t]?|/g,DSML_RE_DRIFT_INVOKE=/<\s*|\s*(?:DSML\s*)?|\s*(?:invoke|call|tool_call)\b\s*([^>]*?)\s*>/gi,DSML_RE_DRIFT_PARAM=/<\s*|\s*(?:DSML\s*)?|\s*(?:parameter|param|arg)\b\s*([^>]*?)\s*>/gi,DSML_RE_PARAM_CLOSE=/<\s*\/\s*|\s*(?:DSML\s*)?|\s*(?:parameter|param|arg)\b\s*>/gi,DSML_RE_ATTR=/([A-Za-z_][A-Za-z0-9_.-]*)\s*=\s*(?:"([^"]*)"|'([^']*)')/g,DSML_RE_STRUCT_TAG=/<\s*\/?\s*|\s*(?:DSML\s*)?|\s*(?:invoke|call|tool_call|parameter|param|arg|reasoning|(?:tool|function)[_▁ ]*calls?)\b/gi,DSML_RE_STRUCT_CLOSER=/<\s*\/\s*|\s*(?:DSML\s*)?|\s*(?:invoke|call|tool_call|parameter|param|arg|reasoning|(?:tool|function)[_▁ ]*calls?)\b/gi,DSML_RE_WRAPPER_TAG=/<\s*\/?\s*|\s*(?:DSML\s*)?|\s*(?!(?:invoke|call|tool_call|parameter|param|arg)\b)[A-Za-z_][A-Za-z0-9_]*[^>]{0,120}>/g,DSML_RE_TRAILING_LT=/<\s*\/?\s*$/,DSML_RE_OPEN_TOKEN=/^|[ \t]?[A-Za-z0-9_▁]*[ \t]?$/,DSML_HOLD_MAX=4194304;function dsmlAttrs(e){const t={};if(!e)return t;let n;for(DSML_RE_ATTR.lastIndex=0;n=DSML_RE_ATTR.exec(e);)t[n[1].toLowerCase()]=null!=n[2]?n[2]:n[3];return t}function dsmlJson(e){const t=String(null==e?"":e).trim();try{const e=JSON.parse(t);return void 0===e?t:e}catch{return t}}function dsmlArgs(e){const t={};let n;for(DSML_RE_PARAM.lastIndex=0;n=DSML_RE_PARAM.exec(e);){const e=dsmlAttrs(n[1]),s=e.name||e.key||`arg${Object.keys(t).length}`,o="false"===String(e.string||"").toLowerCase()||"true"===String(e.json||"").toLowerCase();t[s]=o?dsmlJson(n[2]):n[2]}return t}function decodeDeepSeekDSML(e){if("string"!=typeof e||!e||-1===e.indexOf("|"))return{text:e||"",calls:[]};const t=[];let n=e;DSML_RE_INVOKE.lastIndex=0;const s=[];let o;for(;o=DSML_RE_INVOKE.exec(n);){const e=dsmlAttrs(o[1]),n=e.name||e.function||e.tool||"";n&&t.push({name:n,args:dsmlArgs(o[2])}),s.push([o.index,o.index+o[0].length])}for(let e=s.length-1;e>=0;e--)n=n.slice(0,s[e][0])+n.slice(s[e][1]);return n=n.replace(DSML_RE_JSON_CALLS,((e,n)=>{const s=dsmlJson(n),o=Array.isArray(s)?s:s&&Array.isArray(s.tool_calls)?s.tool_calls:[];for(const e of o){const n=e&&e.function||e||{},s=n.name||e&&e.name||"";if(!s)continue;let o=null!=n.arguments?n.arguments:e&&e.arguments;"string"==typeof o&&(o=dsmlJson(o)),t.push({name:s,args:o&&"object"==typeof o?o:{}})}return""})),n=n.replace(DSML_RE_LEADER,""),{text:n,calls:t}}function decodeDeepSeekDSMLDrift(e){if("string"!=typeof e||!e||-1===e.indexOf("|"))return{text:e||"",calls:[]};const t=[];let n;for(DSML_RE_DRIFT_INVOKE.lastIndex=0;n=DSML_RE_DRIFT_INVOKE.exec(e);)t.push({at:n.index,attrs:n[1],body:DSML_RE_DRIFT_INVOKE.lastIndex});if(!t.length)return{text:e,calls:[]};const s=[];let o="",r=0;for(let n=0;n<t.length;n++){const i=n+1<t.length?t[n+1].at:e.length,l=e.slice(t[n].body,i),a=dsmlAttrs(t[n].attrs),c=a.name||a.function||a.tool||"",u={},_=[];let f;for(DSML_RE_DRIFT_PARAM.lastIndex=0;f=DSML_RE_DRIFT_PARAM.exec(l);)_.push({at:f.index,attrs:f[1],from:DSML_RE_DRIFT_PARAM.lastIndex});for(let e=0;e<_.length;e++){const t=e+1<_.length?_[e+1].at:l.length,n=l.slice(_[e].from,t).replace(DSML_RE_PARAM_CLOSE,""),s=dsmlAttrs(_[e].attrs),o=s.name||s.key||`arg${Object.keys(u).length}`,r="false"===String(s.string||"").toLowerCase();u[o]=r?dsmlJson(n):n}o+=e.slice(r,t[n].at),r=i,c&&s.push({name:c,args:u})}return o+=e.slice(r),{text:o,calls:s}}function dsmlResidue(e){return e?-1===e.indexOf("|")?e.replace(DSML_RE_SKELETON,""):e.replace(DSML_RE_ANY_TAG,"").replace(DSML_RE_SKELETON,"").replace(DSML_RE_ENVELOPE_TOKEN,""):e}function dsmlUnterminated(e){return!!DSML_RE_OPEN_TOKEN.test(e)||(e.match(DSML_RE_STRUCT_TAG)||[]).length>(e.match(DSML_RE_STRUCT_CLOSER)||[]).length}function dsmlHoldFrom(e){const t=e.search(/<\s*\/?\s*|/);if(-1!==t)return t;const n=e.search(DSML_RE_JSON_CALLS_OPEN);if(-1!==n&&-1===e.slice(n).search(DSML_RE_JSON_CALLS_CLOSE))return n;const s=DSML_RE_TRAILING_LT.exec(e);if(s)return s.index;const o=e.indexOf("|");return-1!==o&&dsmlUnterminated(e.slice(o))?o:-1}function decodePlainToolCallTags(e,t){if(!e||-1===e.indexOf("<"))return e;let n="",s=0;for(;;){const o=TEXT_TOOL_TAG_OPEN_RE.exec(e.slice(s));if(!o)break;const r=s+o.index,i=o[0];if(i.includes(DSML_TAG_MARK)){n+=e.slice(s,r+i.length),s=r+i.length;continue}const l=e.slice(r+i.length),a=TEXT_TOOL_TAG_CLOSE_RE.exec(l);if(!a)break;const c=textCallsIn(l.slice(0,a.index));if(n+=e.slice(s,r),s=r+i.length+a.index+a[0].length,c.length)for(const e of c)t.push(e);else n+=e.slice(r,s)}return n+e.slice(s)}function plainToolTagHoldAt(e){if(!e||-1===e.indexOf("<"))return-1;let t=0;for(;;){const n=TEXT_TOOL_TAG_OPEN_RE.exec(e.slice(t));if(!n)return-1;const s=t+n.index;if(n[0].includes(DSML_TAG_MARK)){t=s+n[0].length;continue}const o=e.slice(s+n[0].length);return TEXT_TOOL_TAG_CLOSE_RE.test(o)?-1:s}}function makeDeepSeekEnvelopeStripper(){let e="";const t=[],n=e=>dsmlResidue(decodePlainToolCallTags(e,t)),s=s=>{e+="string"==typeof s?s:"";const o=decodeDeepSeekDSML(e);e=o.text;for(const e of o.calls)t.push(e);e=e.replace(DSML_RE_WRAPPER_TAG,"");const r=dsmlHoldFrom(e);let i;-1!==r&&e.length-r<=DSML_HOLD_MAX?(i=e.slice(0,r),e=e.slice(r)):(i=e,e="");const l=plainToolTagHoldAt(i);return-1!==l&&(e=i.slice(l)+e,i=i.slice(0,l)),n(i)};return s.flush=()=>{const s=decodeDeepSeekDSML(e);e="";for(const e of s.calls)t.push(e);const o=decodeDeepSeekDSMLDrift(s.text);for(const e of o.calls)t.push(e);return n(o.text)},s.calls=()=>t.slice(),s}const TEXT_TOOL_TAG_OPEN_RE=/<\s*tool_calls?\b[^>]{0,80}>/i,TEXT_TOOL_TAG_CLOSE_RE=/<\s*\/\s*tool_calls?\s*>/i,TEXT_FENCE_RE=/```[ \t]*[A-Za-z0-9_+-]*[ \t]*\n?([\s\S]*?)```/,TEXT_FENCE_OPEN_RE=/```[ \t]*[A-Za-z0-9_+-]*[ \t]*\n?/,DSML_TAG_MARK="|";function asTextToolCall(e){if(!e||"object"!=typeof e)return null;let t=e;t.function&&"object"==typeof t.function&&(t={...t.function,name:t.function.name||t.name});const n=t.name||t.tool||t.tool_name||t.function_name,s="string"==typeof n?n.trim():"";if(!s)return null;let o=null!=t.arguments?t.arguments:null!=t.parameters?t.parameters:null!=t.args?t.args:null!=t.tool_input?t.tool_input:{};if("string"==typeof o){const e=o.trim()?safeJson(o):{};o=e&&"object"==typeof e?e:{}}return null!=o&&"object"==typeof o||(o={}),{name:s,args:o}}function safeJson(e){try{return JSON.parse(e)}catch{return null}}function textCallsIn(e){const t=String(e||"").trim().replace(/^json\s*/i,"").trim();if(!t)return[];const n=safeJson(t);if(!n)return[];const s=Array.isArray(n)?n:[n],o=[];for(const e of s){const t=asTextToolCall(e);t&&o.push(t)}return o}function nextTextToolEnvelope(e){const t=TEXT_FENCE_RE.exec(e);if(t)return{start:t.index,end:t.index+t[0].length,complete:!0,raw:t[0],body:t[1]};const n=TEXT_FENCE_OPEN_RE.exec(e);return n?{start:n.index,end:e.length,complete:!1,body:e.slice(n.index+n[0].length)}:null}function trailingHeadFrom(e,t=!1){if(!e)return-1;const n=e.search(TEXT_TOOL_HEAD_RE);if(-1!==n)return n;const s=/`{1,2}$/.exec(e);if(s)return s.index;const o=e.lastIndexOf("<");if(-1!==o&&couldBeTextToolHead(e.slice(o)))return o;if(t){const t=e.lastIndexOf("{");if(-1!==t&&/^\{\s*(?:"[^"]*"\s*:[\s\S]*|"[^"]*"?\s*|)$/.test(e.slice(t)))return t}return-1}function couldBeTextToolHead(e){const t=e.replace(/^[\s\uFEFF]+/,"");if(!t)return!0;if(t.length<=3&&"```".startsWith(t))return!0;if(t.startsWith("<")&&!TEXT_TOOL_HEAD_RE.test(t)){const e=t.slice(1).replace(/^\/?\s*/,"").toLowerCase();if(e.length<=10&&/^[a-z_]*$/.test(e))return"tool_call".startsWith(e)||"tool_calls".startsWith(e)}return!1}function balancedJsonEnd(e,t){let n=0,s=!1,o=!1;for(let r=t;r<e.length;r++){const t=e[r];if(s)o?o=!1:"\\"===t?o=!0:'"'===t&&(s=!1);else if('"'===t)s=!0;else if("{"===t||"["===t)n++;else if(("}"===t||"]"===t)&&(n--,n<=0))return r+1}return-1}function makeTextToolCallStripper({toolNames:e=null}={}){const t=e?new Set([...e].map((e=>String(e)))):null;let n="";const s=[],o=e=>Boolean(e.length)&&(!t||e.every((e=>t.has(e.name)))),r=(e,t)=>{const n=textCallsIn(t);if(!o(n))return e;for(const e of n)s.push(e);return""},i=e=>{n+="string"==typeof e?e:"";let i="",l=!1;for(;;){const e=nextTextToolEnvelope(n);if(e){if(i+=n.slice(0,e.start),n=n.slice(e.start),!e.complete){l=!0;break}i+=r(e.raw,e.body),n=n.slice(e.raw.length);continue}const a=TEXT_TOOL_TAG_OPEN_RE.exec(n);if(a){i+=n.slice(0,a.index),n=n.slice(a.index);const e=n.slice(a[0].length),t=TEXT_TOOL_TAG_CLOSE_RE.exec(e);if(!t){l=!0;break}const s=n.slice(0,a[0].length+t.index+t[0].length);i+=r(s,e.slice(0,t.index)),n=n.slice(s.length);continue}const c=t?/\{\s*"/.exec(n):null;if(!c)break;const u=balancedJsonEnd(n,c.index),_=n.slice(0,c.index);if(-1===u){i+=_,n=n.slice(c.index),l=!0;break}const f=textCallsIn(n.slice(c.index,u));if(i+=_,o(f))for(const e of f)s.push(e);else i+=n.slice(c.index,u);n=n.slice(u)}const a=l?0:trailingHeadFrom(n,Boolean(t));return-1===a?(i+=n,n=""):a>0&&(i+=n.slice(0,a),n=n.slice(a)),n.length>TEXT_TOOL_HOLD_MAX&&(i+=n,n=""),i};return i.flush=()=>{const e=n;if(n="",!e)return"";const s=nextTextToolEnvelope(e);if(s)return e.slice(0,s.start)+r(e.slice(s.start),s.body);const o=TEXT_TOOL_TAG_OPEN_RE.exec(e);if(!o){const n=t?/\{\s*"/.exec(e):null;if(!n)return e;const s=balancedJsonEnd(e,n.index);return-1===s?e:e.slice(0,n.index)+r(e.slice(n.index,s),e.slice(n.index,s))+e.slice(s)}const i=e.slice(o.index+o[0].length).replace(TEXT_TOOL_TAG_CLOSE_RE,"");return e.slice(0,o.index)+r(e.slice(o.index),i)},i.calls=()=>s.slice(),i}const TEXT_TOOL_HEAD_RE=/```|<\s*tool_calls?\b/i,TEXT_TOOL_HOLD_MAX=65536;function isLocalEndpoint(e){let t="",n="";if(""===String(null==e?"":e).trim())return!1;try{const s=new URL(normalizeBase(e));t=s.hostname.toLowerCase(),n=s.protocol.toLowerCase()}catch{return!1}if("http:"!==n&&"https:"!==n)return!1;if(!t)return!1;if(t.startsWith("[")&&t.endsWith("]")&&(t=t.slice(1,-1)),!t||/[\s/\\@]/.test(t))return!1;if("localhost"===t||t.endsWith(".localhost"))return!0;if(/\.(local|internal|lan)$/.test(t))return!0;if(t.includes(":"))return"::1"===t||"0:0:0:0:0:0:0:1"===t||!!/^f[cd][0-9a-f]{2}:/.test(t)||!!/^fe[89ab][0-9a-f]:/.test(t);const s=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(t);if(s){const e=s.slice(1).map(Number);if(e.some((e=>e>255)))return!1;const[t,n]=e;return 127===t||10===t||172===t&&n>=16&&n<=31||192===t&&168===n||169===t&&254===n}return!t.includes(".")}function remoteRefusal(e){if(isLocalEndpoint(e))return null;const t=new Error(`local: ${JSON.stringify(String(e||""))} is not on this machine, so it is not the local class. Running a model you own is free because there is no vendor to pay — a remote URL here would be an unpaid turn instead. Use the aegis or byok class for remote models.`);return t.status=400,t}async function probe(e=DEFAULT_BASE){const t=baseOf(e);try{return{running:(await fetch(`${t}/api/tags`,{signal:AbortSignal.timeout(1500)})).ok,baseURL:t}}catch{return{running:!1,baseURL:t}}}async function listTags(e=DEFAULT_BASE){const t=await fetch(`${baseOf(e)}/api/tags`);if(!t.ok){const e=new Error(`local: /api/tags answered ${t.status}`);throw e.status=t.status,e}const n=await t.json();return(n&&n.models||[]).map((e=>({id:e&&e.name,...e&&e.details?{details:e.details}:{}}))).filter((e=>e.id))}const UNLOAD_TIMEOUT_MS=4e3,RESIDENT_TIMEOUT_MS=FIRST_BYTE_TIMEOUT_MS,DEFAULT_WARM_SECS=1800;async function unload(e,t){if(!t)return{ok:!1};const n=baseOf(e);if(!isLocalEndpoint(n))return{ok:!1};try{return{ok:(await fetch(`${n}/api/chat`,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({model:String(t),messages:[],stream:!1,keep_alive:0}),signal:AbortSignal.timeout(UNLOAD_TIMEOUT_MS)})).ok}}catch{return{ok:!1}}}function normalizeRow(e){if(!e||"object"!=typeof e)return null;const t={role:e.role||"user"};return null!=e.content&&(t.content=e.content),e.name&&(t.name=e.name),e.tool_call_id&&(t.tool_call_id=e.tool_call_id),Array.isArray(e.tool_calls)&&e.tool_calls.length&&(t.tool_calls=e.tool_calls),t}function parseEvent(e){const t=String(e).split("\n");let n="",s=!1;for(const e of t){if(!e||e.startsWith(":"))continue;const t=e.indexOf(":"),o=-1===t?e:e.slice(0,t),r=-1===t?"":e.slice(t+1).replace(/^ /,"");"data"===o?(n+=r,s=!0):"event"!==o&&"id"!==o&&"retry"!==o||(s=!0)}if(!s||!n)return null;if("[DONE]"===n)return{done:!0};try{return{json:JSON.parse(n)}}catch{return null}}async function readSSE(e,t,{idleTimeoutMs:n=SSE_IDLE_TIMEOUT_MS}={}){const s=e.body.getReader(),o=new TextDecoder;let r="",i=null,l=()=>{};const a=new Promise(((e,t)=>{l=t})),c=()=>{i&&clearTimeout(i),i=setTimeout((()=>{l(new Error(`local: the model stopped responding — no output for ${Math.round(n/1e3)}s`))}),n)};c();try{for(;;){const e=await Promise.race([s.read(),a]);if(e.done)break;let n;for(r+=o.decode(e.value,{stream:!0}),r=r.replace(/\r\n/g,"\n");(n=r.indexOf("\n\n"))>=0;){const e=r.slice(0,n);r=r.slice(n+2);const s=parseEvent(e);if(s){if(s.done)return;s.json&&(c(),t(s.json))}}}}finally{i&&clearTimeout(i);try{await s.cancel()}catch{}}}const localCallQueues=new Map;function runSerialized(e,t){const n=(localCallQueues.get(e)||Promise.resolve()).then(t,t);return localCallQueues.set(e,n.then((()=>{}),(()=>{}))),n}const activeCountByKey=new Map,warmModelByBase=new Map;function swapWarmModel(e,t){if(!t)return;const n=warmModelByBase.get(e);warmModelByBase.set(e,t),n&&n!==t&&(activeCountByKey.get(`${e}|${n}`)||unload(e,n).catch((()=>{})))}async function unloadAll(){const e=[...warmModelByBase.entries()];warmModelByBase.clear(),await Promise.all(e.map((([e,t])=>unload(e,t))))}function warmSecsFor({warmSecs:e}={}){const t=Number(e);if(Number.isFinite(t)&&t>=0)return Math.floor(t);const n=Number(process.env&&process.env.AEGIS_LOCAL_WARM_SECS);return Number.isFinite(n)&&n>=0?Math.floor(n):DEFAULT_WARM_SECS}const residentMemo=new Map;function residentKey(e,t,n){return`${e}\0${t}\0${n}`}function residentSecsLeft(e,t,n,{dialer:s=fetch,now:o=Date.now()}={}){const r=residentMemo.get(residentKey(e,t,n));if(!r||r.dialer!==s)return 0;const i=Math.round((r.until-o)/1e3);return i<=0?(residentMemo.delete(residentKey(e,t,n)),0):i}function residentEntry(e,t,n,{dialer:s=fetch,now:o=Date.now()}={}){const r=residentMemo.get(residentKey(e,t,n));return r&&r.dialer===s&&r.until-o>0?r:null}function sameModel(e,t){const n=String(e||""),s=String(t||"");return!(!n||!s)&&(n===s||n.startsWith(`${s}:`)||n.startsWith(`${s}@`))}const WINDOW_TIMEOUT_MS=3e3;async function windowOf(e,t,{signal:n}={}){const s=new AbortController,o=()=>s.abort();if(n){if(n.aborted)return null;"function"==typeof n.addEventListener&&n.addEventListener("abort",o,{once:!0})}const r=setTimeout((()=>s.abort()),WINDOW_TIMEOUT_MS);r&&"function"==typeof r.unref&&r.unref();try{const n=await fetch(`${e}/api/ps`,{method:"GET",signal:s.signal});if(!n||!n.ok||"function"!=typeof n.json)return null;const o=await n.json(),r=(o&&o.models||[]).find((e=>sameModel(e&&(e.name||e.model),t))),i=r&&Number(r.context_length);return Number.isFinite(i)&&i>0?i:null}catch{return null}finally{clearTimeout(r),n&&"function"==typeof n.removeEventListener&&n.removeEventListener("abort",o)}}async function ensureResident({baseURL:e=DEFAULT_BASE,model:t,system:n="",signal:s,warmSecs:o,firstByteTimeoutMs:r}={}){const i=baseOf(e);if(!isLocalEndpoint(i))return{ok:!1,skipped:"remote"};if(!t||!String(t).trim())return{ok:!1,skipped:"no-model"};const l=warmSecsFor({warmSecs:o}),a="string"==typeof n?n:"",c={warmSecs:l,prefixSource:a?"turn-system":"none",prefixChars:a.length};if(l<=0)return{ok:!1,skipped:"warm-disabled",...c};const u=fetch,_=residentSecsLeft(i,t,a,{dialer:u});if(_>=Math.min(l,60)){const e=residentEntry(i,t,a,{dialer:u});return{ok:!0,already:!0,memo:!0,remainingSecs:_,contextLength:e&&e.contextLength||null,...c}}const f=new AbortController,d=()=>f.abort();s&&(s.aborted?f.abort():"function"==typeof s.addEventListener&&s.addEventListener("abort",d,{once:!0}));const m=Number(r),E=Number.isFinite(m)&&m>0?Math.min(m,RESIDENT_TIMEOUT_MS):RESIDENT_TIMEOUT_MS;let T=!1,p=null;const S=setTimeout((()=>{T=!0,f.abort()}),E);S&&"function"==typeof S.unref&&S.unref();try{try{const e=await fetch(`${i}/api/ps`,{method:"GET",signal:f.signal});if(e&&e.ok&&"function"==typeof e.json){const n=await e.json(),s=(n&&n.models||[]).find((e=>sameModel(e&&(e.name||e.model),t)));if(s&&Number.isFinite(s.context_length)&&s.context_length>0&&(p=s.context_length),s&&s.expires_at){const e=Math.round((new Date(s.expires_at).getTime()-Date.now())/1e3);if(Number.isFinite(e)&&e>=Math.min(l,60))return residentMemo.set(residentKey(i,t,a),{until:Date.now()+1e3*e,dialer:u,contextLength:p}),{ok:!0,already:!0,remainingSecs:e,contextLength:p,...c}}}}catch{}if(f.signal.aborted)return{ok:!1,aborted:!0,timeout:T,contextLength:p,...c};const e=a?[{role:"system",content:a},{role:"user",content:"PONG"}]:[{role:"user",content:"PONG"}],n=await fetch(`${i}/api/chat`,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({model:String(t),keep_alive:l,stream:!1,messages:e,options:{num_predict:1}}),signal:f.signal});let s=null,o=null;if(n&&"function"==typeof n.text){const e=await n.text().catch((()=>""));try{const t=JSON.parse(e);Number.isFinite(t.prompt_eval_count)&&(s=t.prompt_eval_count),Number.isFinite(t.eval_count)&&(o=t.eval_count)}catch{}}return n&&n.ok&&(null==p&&(p=await windowOf(i,t,{signal:f.signal})||null),residentMemo.set(residentKey(i,t,a),{until:Date.now()+1e3*l,dialer:u,contextLength:p}),swapWarmModel(i,t)),{ok:!(!n||!n.ok),status:n&&n.status,pinned:!0,promptTokens:s,outputTokens:o,contextLength:p,...c}}catch(e){return{ok:!1,error:String(e&&e.message||e),timeout:T,contextLength:p,...c}}finally{clearTimeout(S),s&&"function"==typeof s.removeEventListener&&s.removeEventListener("abort",d)}}function stoppedTurn(e,{content:t="",usage:n=null}={}){const s={id:"local",model:String(e),choices:[{index:0,message:{role:"assistant",content:t},finish_reason:"stop"}]};return n&&(s.usage=n),s}function fitToWindowOf({rows:e,system:t,tools:n,contextLength:s,maxTokens:o}){const r=Number(s);if(!Number.isFinite(r)||r<=0)return null;const i=(Array.isArray(e)?e:[]).filter((e=>e&&"system"!==e.role)),l=Array.isArray(n)&&n.length?estimateTokens(JSON.stringify(n)):0,a=Number(o),c=Number.isFinite(a)&&a>0?a:Math.round(r/4),u=trimContext({messages:i,system:"string"==typeof t?t:"",budgetTokens:r,reserveTokens:c,fixedTokens:l});return{window:r,reserveTokens:c,toolTokens:l,dropped:u.dropped,messages:u.messages,over:u.estimatedTokens>Math.max(0,r-c),estimatedTokens:u.estimatedTokens}}async function chatOne({baseURL:e=DEFAULT_BASE,model:t,messages:n,system:s,prompt:o,maxTokens:r,temperature:i,tools:l,toolChoice:a,signal:c,onDelta:u,firstByteTimeoutMs:_=FIRST_BYTE_TIMEOUT_MS,contextLength:f}={}){const d=baseOf(e),m=remoteRefusal(d);if(m)throw m;if(!t||!String(t).trim()){const e=new Error("local: no model selected — pick one from the model list, or type a tag");throw e.status=400,e}const E=[];s&&E.push({role:"system",content:s});for(const e of Array.isArray(n)?n:[]){if(e&&"system"===e.role)continue;const t=normalizeRow(e);t&&E.push(t)}const T="string"==typeof o?o:"";if(T){const e=E[E.length-1];e&&"user"===e.role&&e.content===T||E.push({role:"user",content:T})}if(!E.length){const e=new Error("local: nothing to send — no prompt and no messages");throw e.status=400,e}let p=null;if(null!=f){const e=E[0]&&"system"===E[0].role?E[0]:null;if(p=fitToWindowOf({rows:E,system:e&&e.content||s||"",tools:l,contextLength:f,maxTokens:r}),p){const t=p.messages;E.length=0,e&&E.push(e),E.push(...t)}}const S={model:String(t),messages:E,stream:!0,stream_options:{include_usage:!0},...r?{max_tokens:r}:{},...null!=i?{temperature:i}:{},...l&&l.length?{tools:l}:{},...a?{tool_choice:a}:{}},g=new AbortController;let h=!1;const L=setTimeout((()=>{h=!0,g.abort()}),_);L&&"function"==typeof L.unref&&L.unref();const M=()=>g.abort();let y;c&&(c.aborted?g.abort():"function"==typeof c.addEventListener&&c.addEventListener("abort",M,{once:!0}));try{y=await fetch(`${d}/v1/chat/completions`,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(S),signal:g.signal})}catch(e){if(h||isHeadersDeadline(e)){const e=h?`sent nothing in ${Math.round(_/1e3)}s`:`sent no response headers before this runtime's own ${Math.round(HEADERS_DEADLINE_MS/1e3)}s deadline`,n=new Error(`local: ${t} ${e} — the daemon is still loading the model, or its prompt prefill is longer than that. \`ollama ps\` shows which; a smaller prompt or a smaller model is the way through.`);throw n.status=0,n.code="timeout",n}if(c&&c.aborted||g.signal.aborted)return stoppedTurn(t);throw e}finally{clearTimeout(L)}if(!y.ok){let e="";try{e=await y.text()}catch{}const t=new Error(`local: chat/completions ${y.status}${e?` — ${e.slice(0,400)}`:""}`);throw t.status=y.status,t}let O="",A=null;const D=[],R=new Map,x=makeDeepSeekEnvelopeStripper(),b=makeTextToolCallStripper({toolNames:(Array.isArray(l)?l:[]).map((e=>e&&e.function&&e.function.name||e&&e.name||"")).filter(Boolean)});try{await readSSE(y,(e=>{e&&e.usage&&(A=e.usage);const t=e&&Array.isArray(e.choices)?e.choices[0]:null;if(!t)return;const n=t.delta||{};if("string"==typeof n.content&&n.content){const e=b(x(n.content));e&&(O+=e,"function"==typeof u&&u({text:e}))}const s=n.tool_calls||[];for(const e of s){const t=Number.isInteger(e.index)?e.index:D.length;let n=R.get(t);n||(n={id:"",type:"function",function:{name:"",arguments:""}},R.set(t,n)),e.id&&(n.id=e.id),e.type&&(n.type=e.type);const s=e.function||{};s.name&&(n.function.name=s.name),s.arguments&&(n.function.arguments+=s.arguments)}}),{idleTimeoutMs:SSE_IDLE_TIMEOUT_MS})}catch(e){if(c&&c.aborted)return stoppedTurn(t,{content:O,usage:A});throw e}finally{c&&"function"==typeof c.removeEventListener&&c.removeEventListener("abort",M)}const w=`${b(x.flush())||""}${b.flush()||""}`;w&&(O+=w,"function"==typeof u&&u({text:w}));for(const[e,t]of[...R.entries()].sort(((e,t)=>e[0]-t[0])))(t.function.name||t.function.arguments)&&(t.id||(t.id=`call_${e}`),D.push(t));const k={role:"assistant",content:O};D.length&&(k.tool_calls=D);const N=x.calls(),I=b.calls(),C={id:"local",model:String(t),choices:[{index:0,message:k,finish_reason:D.length?"tool_calls":"stop"}],...A?{usage:A}:{}};return(D.length||N.length||I.length)&&(C.toolCalls=[...D.map((e=>({id:e.id,name:e.function.name,args:e.function.arguments}))),...N.map((e=>({id:"",name:e.name,args:e.args||{}}))),...I.map((e=>({id:"",name:e.name,args:e.args||{}})))],(N.length||I.length)&&"tool_calls"!==C.choices[0].finish_reason&&(C.choices[0].finish_reason="tool_calls")),p&&(C.window={contextLength:p.window,dropped:p.dropped,toolTokens:p.toolTokens,estimatedTokens:p.estimatedTokens,reserveTokens:p.reserveTokens,over:p.over}),C}async function chat(e={}){const t=baseOf(e&&e.baseURL),n=e&&e.model,s=`${t}|${n}`;activeCountByKey.set(s,(activeCountByKey.get(s)||0)+1);try{let o=null;const r=await runSerialized(s,(async()=>{o=await ensureResident({baseURL:e&&e.baseURL,model:n,system:e&&e.system,signal:e&&e.signal,warmSecs:e&&e.warmSecs,firstByteTimeoutMs:e&&e.firstByteTimeoutMs});let s=o&&o.contextLength||null;const r=isLocalEndpoint(t)&&""!==String(n||"").trim();return!s&&r&&o&&!0!==o.ok&&!o.skipped&&(s=await windowOf(t,n,{signal:e&&e.signal})||null),chatOne({...e,contextLength:s})}));return swapWarmModel(t,n),o&&"object"==typeof r&&r&&(r.residency=o),r}finally{const e=(activeCountByKey.get(s)||1)-1;e<=0?activeCountByKey.delete(s):activeCountByKey.set(s,e)}}module.exports={DEFAULT_BASE:DEFAULT_BASE,DEFAULT_WARM_SECS:DEFAULT_WARM_SECS,RESIDENT_TIMEOUT_MS:RESIDENT_TIMEOUT_MS,SSE_IDLE_TIMEOUT_MS:SSE_IDLE_TIMEOUT_MS,FIRST_BYTE_TIMEOUT_MS:FIRST_BYTE_TIMEOUT_MS,PROBE_TIMEOUT_MS:1500,baseOf:baseOf,normalizeBase:normalizeBase,isLocalEndpoint:isLocalEndpoint,remoteRefusal:remoteRefusal,probe:probe,listTags:listTags,readSSE:readSSE,chat:chat,ensureResident:ensureResident,residentSecsLeft:residentSecsLeft,residentEntry:residentEntry,unload:unload,unloadAll:unloadAll,makeTextToolCallStripper:makeTextToolCallStripper,makeDeepSeekEnvelopeStripper:makeDeepSeekEnvelopeStripper,textCallsIn:textCallsIn,fitToWindowOf:fitToWindowOf,isHeadersDeadline:isHeadersDeadline,HEADERS_DEADLINE_MS:HEADERS_DEADLINE_MS,windowOf:windowOf,stoppedTurn:stoppedTurn};