fascicle 0.13.1 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +57 -0
- package/README.md +20 -1
- package/dist/acp.d.ts +233 -0
- package/dist/acp.js +1919 -0
- package/dist/acp.js.map +1 -0
- package/dist/adapters.js +1 -2
- package/dist/agents.d.ts +1 -1
- package/dist/agents.js +1 -1
- package/dist/{core-DK7g1Z40.js → core-CDrz0NM1.js} +1 -12
- package/dist/core-CDrz0NM1.js.map +1 -0
- package/dist/diagram_bin.js +1 -1
- package/dist/{engine-BOGuc0X2.js → engine-ha9xmgwE.js} +528 -783
- package/dist/engine-ha9xmgwE.js.map +1 -0
- package/dist/index.d.ts +468 -622
- package/dist/index.js +20 -12
- package/dist/index.js.map +1 -1
- package/dist/{invoke-Beef3R3X.js → invoke-px31fffH.js} +97 -26
- package/dist/invoke-px31fffH.js.map +1 -0
- package/dist/json_schema_to_standard-BTWD0J6p.js +270 -0
- package/dist/json_schema_to_standard-BTWD0J6p.js.map +1 -0
- package/dist/leading_system-DfJLqpGi.js +584 -0
- package/dist/leading_system-DfJLqpGi.js.map +1 -0
- package/dist/mcp.d.ts +20 -19
- package/dist/mcp.js +5 -268
- package/dist/mcp.js.map +1 -1
- package/dist/records-1HU7mWXf.js +22 -0
- package/dist/records-1HU7mWXf.js.map +1 -0
- package/dist/search-CA_OqFeW.js +579 -0
- package/dist/search-CA_OqFeW.js.map +1 -0
- package/dist/search_worker.d.ts +1 -0
- package/dist/search_worker.js +19 -0
- package/dist/search_worker.js.map +1 -0
- package/dist/stdio.js +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +3 -2
- package/dist/testing.js.map +1 -1
- package/dist/{types-CZ83Q5nm.d.ts → types-De-TVQxS.d.ts} +49 -4
- package/dist/viewer-app/assets/index-Fnh5HKSU.js +2 -0
- package/dist/viewer-app/index.html +1 -1
- package/dist/viewer.js +1 -1
- package/package.json +30 -19
- package/dist/core-DK7g1Z40.js.map +0 -1
- package/dist/engine-BOGuc0X2.js.map +0 -1
- package/dist/invoke-Beef3R3X.js.map +0 -1
- package/dist/viewer-app/assets/index-DXsB0byD.js +0 -2
|
@@ -0,0 +1,584 @@
|
|
|
1
|
+
import "./core-CDrz0NM1.js";
|
|
2
|
+
//#region src/engine/errors.ts
|
|
3
|
+
var rate_limit_error = class extends Error {
|
|
4
|
+
kind = "rate_limit_error";
|
|
5
|
+
retry_after_ms;
|
|
6
|
+
attempts;
|
|
7
|
+
status;
|
|
8
|
+
constructor(message, metadata = {}) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "rate_limit_error";
|
|
11
|
+
this.retry_after_ms = metadata.retry_after_ms;
|
|
12
|
+
this.attempts = metadata.attempts ?? 0;
|
|
13
|
+
this.status = metadata.status;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
var provider_error = class extends Error {
|
|
17
|
+
kind = "provider_error";
|
|
18
|
+
status;
|
|
19
|
+
body;
|
|
20
|
+
cause_kind;
|
|
21
|
+
constructor(message, metadata = {}) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.name = "provider_error";
|
|
24
|
+
this.status = metadata.status;
|
|
25
|
+
this.body = metadata.body;
|
|
26
|
+
this.cause_kind = metadata.cause_kind;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Turn-timeout expiry. Thrown by the engine-owned retry_turn wrapper when a
|
|
31
|
+
* single invoke_turn exceeds `turn_timeout_ms` before any chunk streamed.
|
|
32
|
+
* Its `kind` is the RetryFailureKind wire value `'timeout'` (not the class
|
|
33
|
+
* name) on purpose: that is the discriminant classify_retryable /
|
|
34
|
+
* classify_provider_error already key on, so the shared classifier treats an
|
|
35
|
+
* expiry as retryable without any timeout-specific branch. A mid-stream
|
|
36
|
+
* expiry never reaches here; retry_turn converts it to a non-retryable
|
|
37
|
+
* stream interruption first.
|
|
38
|
+
*/
|
|
39
|
+
var turn_timeout_error = class extends Error {
|
|
40
|
+
kind = "timeout";
|
|
41
|
+
timeout_ms;
|
|
42
|
+
step_index;
|
|
43
|
+
constructor(timeout_ms, step_index) {
|
|
44
|
+
super(`turn exceeded turn_timeout_ms budget of ${timeout_ms}ms`);
|
|
45
|
+
this.name = "turn_timeout_error";
|
|
46
|
+
this.timeout_ms = timeout_ms;
|
|
47
|
+
this.step_index = step_index;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var schema_validation_error = class extends Error {
|
|
51
|
+
kind = "schema_validation_error";
|
|
52
|
+
schema_issues;
|
|
53
|
+
raw_text;
|
|
54
|
+
constructor(message, schema_issues, raw_text) {
|
|
55
|
+
super(message);
|
|
56
|
+
this.name = "schema_validation_error";
|
|
57
|
+
this.schema_issues = schema_issues;
|
|
58
|
+
this.raw_text = raw_text;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var tool_error = class extends Error {
|
|
62
|
+
kind = "tool_error";
|
|
63
|
+
tool_name;
|
|
64
|
+
tool_call_id;
|
|
65
|
+
cause;
|
|
66
|
+
constructor(message, metadata) {
|
|
67
|
+
super(message);
|
|
68
|
+
this.name = "tool_error";
|
|
69
|
+
this.tool_name = metadata.tool_name;
|
|
70
|
+
this.tool_call_id = metadata.tool_call_id;
|
|
71
|
+
this.cause = metadata.cause;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var tool_approval_denied_error = class extends Error {
|
|
75
|
+
kind = "tool_approval_denied_error";
|
|
76
|
+
tool_name;
|
|
77
|
+
step_index;
|
|
78
|
+
tool_call_id;
|
|
79
|
+
constructor(message, metadata) {
|
|
80
|
+
super(message);
|
|
81
|
+
this.name = "tool_approval_denied_error";
|
|
82
|
+
this.tool_name = metadata.tool_name;
|
|
83
|
+
this.step_index = metadata.step_index;
|
|
84
|
+
this.tool_call_id = metadata.tool_call_id;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
var model_required_error = class extends Error {
|
|
88
|
+
kind = "model_required_error";
|
|
89
|
+
constructor(message = "no model specified: pass `model` to generate() or set `defaults.model` on the engine") {
|
|
90
|
+
super(message);
|
|
91
|
+
this.name = "model_required_error";
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* A generate call named a provider the engine has no adapter for. Carries the
|
|
96
|
+
* configured names, and the message repeats them, so a typo is visible from
|
|
97
|
+
* the message alone rather than needing the engine config alongside it.
|
|
98
|
+
*/
|
|
99
|
+
var provider_not_configured_error = class extends Error {
|
|
100
|
+
kind = "provider_not_configured_error";
|
|
101
|
+
provider;
|
|
102
|
+
configured;
|
|
103
|
+
constructor(provider, configured = []) {
|
|
104
|
+
const state = configured.length > 0 ? configured.join(", ") : "none";
|
|
105
|
+
super(`provider '${provider}' is not configured on this engine: pass it to create_engine({ providers }) (configured: ${state})`);
|
|
106
|
+
this.name = "provider_not_configured_error";
|
|
107
|
+
this.provider = provider;
|
|
108
|
+
this.configured = configured;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* No provider could be resolved for a generate call: several providers are
|
|
113
|
+
* configured, the call named none, and no `defaults.provider` is set. Carries
|
|
114
|
+
* the configured provider names so the message tells the caller exactly what
|
|
115
|
+
* the engine can route to.
|
|
116
|
+
*/
|
|
117
|
+
var provider_required_error = class extends Error {
|
|
118
|
+
kind = "provider_required_error";
|
|
119
|
+
configured;
|
|
120
|
+
constructor(configured) {
|
|
121
|
+
super(`no provider specified: pass \`provider\` to generate() or set \`defaults.provider\` (configured: ${configured.join(", ")})`);
|
|
122
|
+
this.name = "provider_required_error";
|
|
123
|
+
this.configured = configured;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
var engine_config_error = class extends Error {
|
|
127
|
+
kind = "engine_config_error";
|
|
128
|
+
provider;
|
|
129
|
+
constructor(message, provider) {
|
|
130
|
+
super(message);
|
|
131
|
+
this.name = "engine_config_error";
|
|
132
|
+
this.provider = provider;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
var on_chunk_error = class extends Error {
|
|
136
|
+
kind = "on_chunk_error";
|
|
137
|
+
cause;
|
|
138
|
+
constructor(message, cause) {
|
|
139
|
+
super(message);
|
|
140
|
+
this.name = "on_chunk_error";
|
|
141
|
+
this.cause = cause;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
var provider_capability_error = class extends Error {
|
|
145
|
+
kind = "provider_capability_error";
|
|
146
|
+
provider;
|
|
147
|
+
capability;
|
|
148
|
+
constructor(provider, capability, detail) {
|
|
149
|
+
const suffix = detail !== void 0 ? `: ${detail}` : "";
|
|
150
|
+
super(`provider '${provider}' does not support '${capability}'${suffix}`);
|
|
151
|
+
this.name = "provider_capability_error";
|
|
152
|
+
this.provider = provider;
|
|
153
|
+
this.capability = capability;
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
var engine_disposed_error = class extends Error {
|
|
157
|
+
kind = "engine_disposed_error";
|
|
158
|
+
constructor(message = "engine has been disposed: build a new one with create_engine()") {
|
|
159
|
+
super(message);
|
|
160
|
+
this.name = "engine_disposed_error";
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
var claude_cli_error = class extends Error {
|
|
164
|
+
kind = "claude_cli_error";
|
|
165
|
+
reason;
|
|
166
|
+
status;
|
|
167
|
+
stderr_snippet;
|
|
168
|
+
constructor(reason, message, metadata = {}) {
|
|
169
|
+
super(message);
|
|
170
|
+
this.name = "claude_cli_error";
|
|
171
|
+
this.reason = reason;
|
|
172
|
+
this.status = metadata.status;
|
|
173
|
+
this.stderr_snippet = metadata.stderr_snippet;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
var provider_auth_error = class extends Error {
|
|
177
|
+
kind = "provider_auth_error";
|
|
178
|
+
provider;
|
|
179
|
+
refresh_command;
|
|
180
|
+
constructor(provider, message, metadata = {}) {
|
|
181
|
+
super(message);
|
|
182
|
+
this.name = "provider_auth_error";
|
|
183
|
+
this.provider = provider;
|
|
184
|
+
this.refresh_command = metadata.refresh_command;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* A generate call that set a schema finished for a reason other than 'stop'
|
|
189
|
+
* (content_filter, length, max_steps, tool_calls), so no schema-validated
|
|
190
|
+
* value exists and validation was never attempted. Carries the raw model text
|
|
191
|
+
* and the last provider-reported payload, so a guardrail trace or other
|
|
192
|
+
* block detail stays reachable from the catch. Distinct from
|
|
193
|
+
* schema_validation_error, which covers a response that completed normally
|
|
194
|
+
* and then failed validation after its repair attempts ran out.
|
|
195
|
+
*/
|
|
196
|
+
var incomplete_generation_error = class extends Error {
|
|
197
|
+
kind = "incomplete_generation_error";
|
|
198
|
+
finish_reason;
|
|
199
|
+
raw_text;
|
|
200
|
+
provider_reported;
|
|
201
|
+
constructor(finish_reason, raw_text, provider_reported) {
|
|
202
|
+
super(`generation finished with '${finish_reason}' before a schema-validated response was produced`);
|
|
203
|
+
this.name = "incomplete_generation_error";
|
|
204
|
+
this.finish_reason = finish_reason;
|
|
205
|
+
this.raw_text = raw_text;
|
|
206
|
+
this.provider_reported = provider_reported;
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
//#endregion
|
|
210
|
+
//#region src/engine/providers/types.ts
|
|
211
|
+
/**
|
|
212
|
+
* Read the `transport` selector off a provider init. Defaults to 'ai_sdk';
|
|
213
|
+
* anything but the two known backends throws at construction so a typo
|
|
214
|
+
* fails loud instead of silently running the default.
|
|
215
|
+
*/
|
|
216
|
+
function resolve_transport(init, provider) {
|
|
217
|
+
const raw = init["transport"];
|
|
218
|
+
if (raw === void 0 || raw === "ai_sdk") return "ai_sdk";
|
|
219
|
+
if (raw === "native") return "native";
|
|
220
|
+
throw new engine_config_error(`${provider} provider: transport must be 'ai_sdk' or 'native', got ${JSON.stringify(raw)}`, provider);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Hand effort to the AI SDK's `reasoning` option and build no provider
|
|
224
|
+
* options of its own, so the provider package maps each level per model from
|
|
225
|
+
* the capability table it keeps (adaptive thinking or a budget for Claude,
|
|
226
|
+
* `thinkingLevel` or a budget for Gemini, `reasoningEffort` for OpenAI).
|
|
227
|
+
* `none` sends neither, which is the SDK's provider default. The anthropic,
|
|
228
|
+
* bedrock, google, and openai ai_sdk adapters all translate this way.
|
|
229
|
+
*/
|
|
230
|
+
function translate_effort_to_sdk_reasoning(effort) {
|
|
231
|
+
if (effort === "none") return {
|
|
232
|
+
provider_options: {},
|
|
233
|
+
effort_ignored: false
|
|
234
|
+
};
|
|
235
|
+
return {
|
|
236
|
+
provider_options: {},
|
|
237
|
+
effort_ignored: false,
|
|
238
|
+
reasoning: effort
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Map raw provider usage to UsageTotals, the mapper most adapters share.
|
|
243
|
+
*
|
|
244
|
+
* Missing usage zeroes the totals. Optional fields (reasoning and cache
|
|
245
|
+
* tokens) are set only when the raw payload carries them, reading the
|
|
246
|
+
* flattened field first and the AI SDK's *_details container second.
|
|
247
|
+
*/
|
|
248
|
+
function default_normalize_usage(raw) {
|
|
249
|
+
if (raw === void 0) return {
|
|
250
|
+
input_tokens: 0,
|
|
251
|
+
output_tokens: 0
|
|
252
|
+
};
|
|
253
|
+
const totals = {
|
|
254
|
+
input_tokens: typeof raw.input_tokens === "number" ? raw.input_tokens : 0,
|
|
255
|
+
output_tokens: typeof raw.output_tokens === "number" ? raw.output_tokens : 0
|
|
256
|
+
};
|
|
257
|
+
if (typeof raw.reasoning_tokens === "number") totals.reasoning_tokens = raw.reasoning_tokens;
|
|
258
|
+
else if (typeof raw.output_token_details?.reasoning_tokens === "number") totals.reasoning_tokens = raw.output_token_details.reasoning_tokens;
|
|
259
|
+
if (typeof raw.cached_input_tokens === "number") totals.cached_input_tokens = raw.cached_input_tokens;
|
|
260
|
+
else if (typeof raw.input_token_details?.cached_tokens === "number") totals.cached_input_tokens = raw.input_token_details.cached_tokens;
|
|
261
|
+
if (typeof raw.cache_write_tokens === "number") totals.cache_write_tokens = raw.cache_write_tokens;
|
|
262
|
+
else if (typeof raw.input_token_details?.cache_creation_input_tokens === "number") totals.cache_write_tokens = raw.input_token_details.cache_creation_input_tokens;
|
|
263
|
+
return totals;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Usage mapper for local backends (Ollama, LM Studio) whose servers never
|
|
267
|
+
* report cache or reasoning tokens: default-normalize, then strip those
|
|
268
|
+
* fields so a stray *_details container cannot invent them.
|
|
269
|
+
*/
|
|
270
|
+
function normalize_local_usage(raw) {
|
|
271
|
+
const base = default_normalize_usage(raw);
|
|
272
|
+
delete base.cached_input_tokens;
|
|
273
|
+
delete base.cache_write_tokens;
|
|
274
|
+
delete base.reasoning_tokens;
|
|
275
|
+
return base;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Build the standard missing-peer error: names the package and the install
|
|
279
|
+
* command so a missing peer is diagnosable from the message alone. Shared so
|
|
280
|
+
* every optional-peer failure reads identically, whether the failing
|
|
281
|
+
* `import()` names the peer directly (load_optional_peer below) or a local
|
|
282
|
+
* module that reaches the peer through its own static import (the ai_sdk
|
|
283
|
+
* turn seam in generate.ts, whose dynamic import must stay written in
|
|
284
|
+
* generate.ts for its relative specifier to resolve correctly).
|
|
285
|
+
*/
|
|
286
|
+
function missing_peer_error(peer_name, cause) {
|
|
287
|
+
const message = `missing peer dependency '${peer_name}'. Install it with your package manager, e.g., \`pnpm add ${peer_name}\` or \`npm install ${peer_name}\`. Cause: ${cause instanceof Error ? cause.message : JSON.stringify(cause)}`;
|
|
288
|
+
return new Error(message, { cause });
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Dynamically import an optional peer dependency, rethrowing an import
|
|
292
|
+
* failure via missing_peer_error so a missing peer is diagnosable from the
|
|
293
|
+
* message alone.
|
|
294
|
+
*/
|
|
295
|
+
async function load_optional_peer(specifier) {
|
|
296
|
+
let mod;
|
|
297
|
+
try {
|
|
298
|
+
mod = await import(specifier);
|
|
299
|
+
} catch (err) {
|
|
300
|
+
throw missing_peer_error(specifier, err);
|
|
301
|
+
}
|
|
302
|
+
return mod;
|
|
303
|
+
}
|
|
304
|
+
//#endregion
|
|
305
|
+
//#region src/engine/providers/native_shared.ts
|
|
306
|
+
/**
|
|
307
|
+
* The message under a JSON error envelope's `error` key, or undefined when
|
|
308
|
+
* the shape carries none worth surfacing.
|
|
309
|
+
*/
|
|
310
|
+
function error_field_message(parsed) {
|
|
311
|
+
if (parsed === null || typeof parsed !== "object") return void 0;
|
|
312
|
+
const error = Reflect.get(parsed, "error");
|
|
313
|
+
if (typeof error === "string" && error.length > 0) return error;
|
|
314
|
+
if (error === null || typeof error !== "object") return void 0;
|
|
315
|
+
const message = Reflect.get(error, "message");
|
|
316
|
+
return typeof message === "string" && message.length > 0 ? message : void 0;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Pull a human-readable message out of a non-2xx body. The native wires share
|
|
320
|
+
* one envelope family: Anthropic sends `{ type: 'error', error: { message } }`,
|
|
321
|
+
* OpenAI-style wires send `{ error: { message } }`, and Ollama sends
|
|
322
|
+
* `{ error: "message" }` (with the nested shape appearing behind proxies), so
|
|
323
|
+
* one parser accepts a string or a `{ message }` object under `error` and
|
|
324
|
+
* falls back to the raw body, truncated to 300 chars.
|
|
325
|
+
*/
|
|
326
|
+
function extract_error_message(body) {
|
|
327
|
+
if (body.length === 0) return "(empty body)";
|
|
328
|
+
let message;
|
|
329
|
+
try {
|
|
330
|
+
message = error_field_message(JSON.parse(body));
|
|
331
|
+
} catch {}
|
|
332
|
+
if (message !== void 0) return message;
|
|
333
|
+
return body.length > 300 ? `${body.slice(0, 300)}...` : body;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Map a non-2xx response to what the engine's retry stack expects: 401
|
|
337
|
+
* becomes a typed provider_auth_error, 429/5xx keep `status` +
|
|
338
|
+
* `responseHeaders` so classify_provider_error marks them retryable
|
|
339
|
+
* (rate_limit honoring retry-after, provider_5xx), and any other 4xx is a
|
|
340
|
+
* permanent provider_error that surfaces as-is.
|
|
341
|
+
*/
|
|
342
|
+
async function response_error(response, provider) {
|
|
343
|
+
let body = "";
|
|
344
|
+
try {
|
|
345
|
+
body = await response.text();
|
|
346
|
+
} catch {}
|
|
347
|
+
const detail = extract_error_message(body);
|
|
348
|
+
const status = response.status;
|
|
349
|
+
if (status === 401) return new provider_auth_error(provider, `${provider} authentication failed (401): ${detail}`);
|
|
350
|
+
if (status === 429 || status >= 500) {
|
|
351
|
+
const retry_after = response.headers.get("retry-after");
|
|
352
|
+
return Object.assign(/* @__PURE__ */ new Error(`${provider} API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
|
|
353
|
+
}
|
|
354
|
+
return new provider_error(`${provider} API error ${status}: ${detail}`, {
|
|
355
|
+
status,
|
|
356
|
+
body
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* A user abort surfaces as the fetch/reader AbortError; retry_turn converts
|
|
361
|
+
* it via its own signal check, so it is rethrown untouched. Everything else
|
|
362
|
+
* is a transport failure wrapped in the `kind: 'network'` shape the shared
|
|
363
|
+
* classify_provider_error marks retryable.
|
|
364
|
+
*/
|
|
365
|
+
function rethrow_network_failure(err, abort, provider) {
|
|
366
|
+
if (abort.aborted) throw err;
|
|
367
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
368
|
+
throw Object.assign(/* @__PURE__ */ new Error(`${provider} native: network failure: ${detail}`), { kind: "network" });
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Run one native turn over HTTP: POST the dialect's body, map a non-2xx to
|
|
372
|
+
* the retry stack's shapes, and hand the response to the dialect's stream or
|
|
373
|
+
* JSON consumer. `build_body` is called inside the network try/catch on
|
|
374
|
+
* purpose, so a mapper that throws surfaces as a retryable network failure
|
|
375
|
+
* rather than escaping the turn unclassified.
|
|
376
|
+
*/
|
|
377
|
+
async function invoke_http_turn(req, provider, request, consume) {
|
|
378
|
+
let response;
|
|
379
|
+
try {
|
|
380
|
+
response = await fetch(request.url, {
|
|
381
|
+
method: "POST",
|
|
382
|
+
headers: request.headers,
|
|
383
|
+
body: JSON.stringify(request.build_body()),
|
|
384
|
+
signal: req.abort
|
|
385
|
+
});
|
|
386
|
+
} catch (err) {
|
|
387
|
+
rethrow_network_failure(err, req.abort, provider);
|
|
388
|
+
}
|
|
389
|
+
if (!response.ok) throw await response_error(response, provider);
|
|
390
|
+
if (req.stream) return consume.stream(response);
|
|
391
|
+
const payload = await response.json();
|
|
392
|
+
return consume.parse(payload);
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Drain a streaming response: decode bytes, reassemble framed payloads (SSE
|
|
396
|
+
* events or NDJSON lines), and feed them to the dialect's aggregator, which
|
|
397
|
+
* dispatches chunks through req.dispatch_chunk as they arrive. Transport
|
|
398
|
+
* failures mid-read wrap as network errors; aggregator throws (malformed
|
|
399
|
+
* frames, mid-stream error events, a rejecting on_chunk) pass through
|
|
400
|
+
* untouched, with the reader cancelled so the connection is released.
|
|
401
|
+
*/
|
|
402
|
+
async function consume_framed_stream(response, req, provider, framing, create_aggregator) {
|
|
403
|
+
const body = response.body;
|
|
404
|
+
if (body === null) throw new provider_error(`${provider} native: streaming response has no body`);
|
|
405
|
+
const aggregator = create_aggregator(req.dispatch_chunk ?? (async () => {}));
|
|
406
|
+
const reader = body.getReader();
|
|
407
|
+
const text_decoder = new TextDecoder();
|
|
408
|
+
const next_bytes = async () => {
|
|
409
|
+
let step;
|
|
410
|
+
try {
|
|
411
|
+
step = await reader.read();
|
|
412
|
+
} catch (err) {
|
|
413
|
+
rethrow_network_failure(err, req.abort, provider);
|
|
414
|
+
}
|
|
415
|
+
return step.done ? void 0 : step.value;
|
|
416
|
+
};
|
|
417
|
+
try {
|
|
418
|
+
while (true) {
|
|
419
|
+
const bytes = await next_bytes();
|
|
420
|
+
if (bytes === void 0) break;
|
|
421
|
+
for (const data of framing.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle(data);
|
|
422
|
+
}
|
|
423
|
+
} finally {
|
|
424
|
+
reader.cancel().catch(() => {});
|
|
425
|
+
}
|
|
426
|
+
const tail = [...framing.push(text_decoder.decode()), ...framing.flush()];
|
|
427
|
+
for (const data of tail) await aggregator.handle(data);
|
|
428
|
+
return aggregator.complete();
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Incremental newline framing shared by the SSE and NDJSON decoders: buffer
|
|
432
|
+
* decoded text across arbitrary chunk boundaries, hand each completed line
|
|
433
|
+
* (CR trimmed) to `handle_line`, and on flush hand over an unterminated tail
|
|
434
|
+
* line, then `on_flush` for state a decoder holds across lines (an SSE event
|
|
435
|
+
* never closed by a blank line).
|
|
436
|
+
*/
|
|
437
|
+
function create_line_splitter(handle_line, on_flush) {
|
|
438
|
+
let buffer = "";
|
|
439
|
+
const take_line = (raw, out) => {
|
|
440
|
+
handle_line(raw.endsWith("\r") ? raw.slice(0, -1) : raw, out);
|
|
441
|
+
};
|
|
442
|
+
return {
|
|
443
|
+
push(text) {
|
|
444
|
+
buffer += text;
|
|
445
|
+
const out = [];
|
|
446
|
+
let newline = buffer.indexOf("\n");
|
|
447
|
+
while (newline >= 0) {
|
|
448
|
+
take_line(buffer.slice(0, newline), out);
|
|
449
|
+
buffer = buffer.slice(newline + 1);
|
|
450
|
+
newline = buffer.indexOf("\n");
|
|
451
|
+
}
|
|
452
|
+
return out;
|
|
453
|
+
},
|
|
454
|
+
flush() {
|
|
455
|
+
const out = [];
|
|
456
|
+
if (buffer.length > 0) take_line(buffer, out);
|
|
457
|
+
buffer = "";
|
|
458
|
+
on_flush?.(out);
|
|
459
|
+
return out;
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Decode a tool call's wire `arguments` string into the engine's input value:
|
|
465
|
+
* non-empty JSON parses (bad JSON is a provider_error naming the tool), and
|
|
466
|
+
* an absent or empty value means `{}`, the shape some servers send for
|
|
467
|
+
* no-argument calls. The Ollama wire's structured-object arguments are its
|
|
468
|
+
* caller's business before this runs.
|
|
469
|
+
*/
|
|
470
|
+
function parse_tool_call_arguments(raw_args, provider, tool_name) {
|
|
471
|
+
if (typeof raw_args === "string" && raw_args.length > 0) try {
|
|
472
|
+
return JSON.parse(raw_args);
|
|
473
|
+
} catch {
|
|
474
|
+
throw new provider_error(`${provider} native: tool_calls arguments for ${tool_name} is not valid JSON`);
|
|
475
|
+
}
|
|
476
|
+
return {};
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Fold engine messages into a wire's message array by role. Serves the wires
|
|
480
|
+
* whose messages map 1:1 in place (chat/completions, Ollama's /api/chat); the
|
|
481
|
+
* Messages API's system-hoisting and role-merging rules live in its own
|
|
482
|
+
* mapper instead.
|
|
483
|
+
*/
|
|
484
|
+
function map_messages_by_role(messages, wire) {
|
|
485
|
+
const out = [];
|
|
486
|
+
for (const message of messages) switch (message.role) {
|
|
487
|
+
case "system":
|
|
488
|
+
out.push(wire.system(message.content));
|
|
489
|
+
break;
|
|
490
|
+
case "user":
|
|
491
|
+
out.push(wire.user(message.content));
|
|
492
|
+
break;
|
|
493
|
+
case "assistant":
|
|
494
|
+
out.push(wire.assistant(message.content));
|
|
495
|
+
break;
|
|
496
|
+
case "tool": out.push(wire.tool(message));
|
|
497
|
+
}
|
|
498
|
+
return out;
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Whether a reasoning part is one this provider may send back: only a part
|
|
502
|
+
* carrying data under the provider's own key goes. A part keyed for another
|
|
503
|
+
* vendor stays behind, so a `fallback` never hands one vendor's signature to
|
|
504
|
+
* another, and so does a part with no data, whose text alone is what a
|
|
505
|
+
* signing wire refuses.
|
|
506
|
+
*/
|
|
507
|
+
function is_own_reasoning(part, key) {
|
|
508
|
+
return part.provider_options?.[key] !== void 0;
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Partition assistant content parts into the text runs, tool calls, and
|
|
512
|
+
* reasoning the wire mappers serialize separately. `reasoning` holds only
|
|
513
|
+
* the parts `provider` may send back, in order (see is_own_reasoning).
|
|
514
|
+
*/
|
|
515
|
+
function split_assistant_parts(parts, provider) {
|
|
516
|
+
const texts = [];
|
|
517
|
+
const tool_parts = [];
|
|
518
|
+
const reasoning = [];
|
|
519
|
+
for (const part of parts) if (part.type === "text") texts.push(part.text);
|
|
520
|
+
else if (part.type === "tool_call") tool_parts.push(part);
|
|
521
|
+
else if (is_own_reasoning(part, provider)) reasoning.push(part);
|
|
522
|
+
return {
|
|
523
|
+
texts,
|
|
524
|
+
tool_parts,
|
|
525
|
+
reasoning
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Read the api_key and base_url every OpenAI-compatible dialect starts from:
|
|
530
|
+
* a missing or non-string api_key becomes '' (the core's empty-key guard owns
|
|
531
|
+
* the throw), and an absent base_url falls back to the dialect's default.
|
|
532
|
+
*/
|
|
533
|
+
function read_dialect_init(init, default_base_url) {
|
|
534
|
+
return {
|
|
535
|
+
api_key: typeof init.api_key === "string" ? init.api_key : "",
|
|
536
|
+
base_url: typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : default_base_url
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
//#endregion
|
|
540
|
+
//#region src/engine/leading_system.ts
|
|
541
|
+
/**
|
|
542
|
+
* Collapse a leading run of system messages into a single joined string,
|
|
543
|
+
* returning the remaining messages.
|
|
544
|
+
*
|
|
545
|
+
* Both message paths hoist a leading system run out of their list the same way:
|
|
546
|
+
* the engine-Message side (generate.ts split_leading_system_messages) hands the
|
|
547
|
+
* run to a native adapter's TurnRequest.system, and the AI SDK side
|
|
548
|
+
* (providers/ai_sdk/invoke.ts split_leading_system) hands it to the SDK's
|
|
549
|
+
* top-level `instructions` option. Same guard, same '\n\n' join, and the same
|
|
550
|
+
* "return the list untouched when hoisting would leave `messages` empty" rule,
|
|
551
|
+
* since provider APIs reject an empty messages array. They differ only in the
|
|
552
|
+
* element type and how a system message's text is read, so each caller supplies
|
|
553
|
+
* `system_text`; the traversal and its guards live here once.
|
|
554
|
+
*/
|
|
555
|
+
function split_leading_system_run(messages, system_text) {
|
|
556
|
+
let run_end = 0;
|
|
557
|
+
const system_parts = [];
|
|
558
|
+
while (run_end < messages.length) {
|
|
559
|
+
const m = messages[run_end];
|
|
560
|
+
if (!is_leading_system(m)) break;
|
|
561
|
+
system_parts.push(system_text(m));
|
|
562
|
+
run_end += 1;
|
|
563
|
+
}
|
|
564
|
+
const rest = messages.slice(run_end);
|
|
565
|
+
if (system_parts.length === 0 || rest.length === 0) return { messages: [...messages] };
|
|
566
|
+
return {
|
|
567
|
+
system: system_parts.join("\n\n"),
|
|
568
|
+
messages: rest
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Narrow a possibly undefined message to the system member of its union.
|
|
573
|
+
*
|
|
574
|
+
* A cast-free type guard so `split_leading_system_run` can hand the narrowed
|
|
575
|
+
* message straight to `system_text` without a `T`-eroding assertion; the
|
|
576
|
+
* optional chain covers the out-of-bounds `undefined` the index access yields.
|
|
577
|
+
*/
|
|
578
|
+
function is_leading_system(m) {
|
|
579
|
+
return m?.role === "system";
|
|
580
|
+
}
|
|
581
|
+
//#endregion
|
|
582
|
+
export { tool_error as A, provider_capability_error as C, rate_limit_error as D, provider_required_error as E, schema_validation_error as O, provider_auth_error as S, provider_not_configured_error as T, engine_config_error as _, is_own_reasoning as a, model_required_error as b, read_dialect_init as c, load_optional_peer as d, missing_peer_error as f, claude_cli_error as g, translate_effort_to_sdk_reasoning as h, invoke_http_turn as i, turn_timeout_error as j, tool_approval_denied_error as k, split_assistant_parts as l, resolve_transport as m, consume_framed_stream as n, map_messages_by_role as o, normalize_local_usage as p, create_line_splitter as r, parse_tool_call_arguments as s, split_leading_system_run as t, default_normalize_usage as u, engine_disposed_error as v, provider_error as w, on_chunk_error as x, incomplete_generation_error as y };
|
|
583
|
+
|
|
584
|
+
//# sourceMappingURL=leading_system-DfJLqpGi.js.map
|