fascicle 0.12.1 → 0.12.5
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 +113 -38
- package/README.md +16 -15
- package/dist/{adapters-hmnJOOgf.js → adapters-CutexJd3.js} +12 -12
- package/dist/adapters-CutexJd3.js.map +1 -0
- package/dist/adapters.d.ts +10 -10
- package/dist/adapters.js +1 -1
- package/dist/agents.d.ts +13 -3
- package/dist/agents.js +12 -5
- package/dist/agents.js.map +1 -1
- package/dist/{core-DDRhHu5P.js → core-DdJEhyQc.js} +122 -488
- package/dist/core-DdJEhyQc.js.map +1 -0
- package/dist/{engine-b4ZH8esY.js → engine-CdXegeqC.js} +201 -380
- package/dist/engine-CdXegeqC.js.map +1 -0
- package/dist/index.d.ts +111 -25
- package/dist/index.js +446 -48
- package/dist/index.js.map +1 -1
- package/dist/{invoke-CyKNhvBw.js → invoke-CH_eJZeS.js} +8 -8
- package/dist/{invoke-CyKNhvBw.js.map → invoke-CH_eJZeS.js.map} +1 -1
- package/dist/leading_system-BUCT-PwD.js +330 -0
- package/dist/leading_system-BUCT-PwD.js.map +1 -0
- package/dist/mcp.d.ts +5 -5
- package/dist/mcp.js +10 -9
- package/dist/mcp.js.map +1 -1
- package/dist/otel.d.ts +21 -3
- package/dist/otel.js +232 -12
- package/dist/otel.js.map +1 -1
- package/dist/stdio.d.ts +2 -2
- package/dist/stdio.js +2 -2
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +3 -2
- package/dist/testing.js.map +1 -1
- package/dist/{trajectory-cSLNlHzh.d.ts → trajectory-ByoOQZ6T.d.ts} +10 -10
- package/dist/trajectory-D1eq9eue.js +81 -0
- package/dist/trajectory-D1eq9eue.js.map +1 -0
- package/dist/{types-BCtKsxrX.d.ts → types-BK0UW28o.d.ts} +3 -3
- package/dist/{types-CXBB4aIM.d.ts → types-CLigYA62.d.ts} +30 -4
- package/dist/{types-BqUY6nRX.d.ts → types-DBpU27sQ.d.ts} +7 -2
- package/dist/ui.d.ts +1 -1
- package/dist/ui.js +1 -1
- package/dist/ui.js.map +1 -1
- package/dist/viewer.d.ts +8 -8
- package/dist/viewer.js +16 -15
- package/dist/viewer.js.map +1 -1
- package/package.json +10 -5
- package/dist/adapters-hmnJOOgf.js.map +0 -1
- package/dist/core-DDRhHu5P.js.map +0 -1
- package/dist/engine-b4ZH8esY.js.map +0 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { O as aborted_error, h as wait_with_abort, m as compute_backoff, o as format_schema_issues, s as validate_schema } from "./core-DdJEhyQc.js";
|
|
2
|
+
import { S as turn_timeout_error, _ as provider_required_error, a as normalize_local_usage, b as tool_approval_denied_error, c as engine_config_error, d as model_required_error, f as on_chunk_error, g as provider_not_configured_error, h as provider_error, i as missing_peer_error, l as engine_disposed_error, m as provider_capability_error, n as default_normalize_usage, o as resolve_transport, p as provider_auth_error, r as load_optional_peer, s as claude_cli_error, t as split_leading_system_run, u as incomplete_generation_error, v as rate_limit_error, x as tool_error, y as schema_validation_error } from "./leading_system-BUCT-PwD.js";
|
|
2
3
|
import { join } from "node:path";
|
|
3
4
|
import { mkdtempSync, writeFileSync } from "node:fs";
|
|
4
5
|
import { tmpdir } from "node:os";
|
|
@@ -25,7 +26,7 @@ var schema_not_convertible_error = class extends Error {
|
|
|
25
26
|
* JSON Schema emission for schemas that reach a model.
|
|
26
27
|
*
|
|
27
28
|
* Standard JSON Schema exposes two directions, `input` and `output`. A tool
|
|
28
|
-
* definition describes what the model should *produce*, so
|
|
29
|
+
* definition describes what the model should *produce*, so Fascicle emits the
|
|
29
30
|
* input direction: a field with a default is one the model may omit, and
|
|
30
31
|
* saying otherwise asks it to supply a value the schema already supplies.
|
|
31
32
|
*
|
|
@@ -35,7 +36,7 @@ var schema_not_convertible_error = class extends Error {
|
|
|
35
36
|
* schema stops a model inventing one. `close_objects` supplies that guard,
|
|
36
37
|
* which is also what the strict and constrained-decode modes require. The AI
|
|
37
38
|
* SDK composes the same two halves for every `kind: 'ai_sdk'` provider, so
|
|
38
|
-
* this is what the majority of
|
|
39
|
+
* this is what the majority of Fascicle's transports already send.
|
|
39
40
|
*/
|
|
40
41
|
const DEFAULT_TARGET = "draft-2020-12";
|
|
41
42
|
/**
|
|
@@ -287,206 +288,6 @@ function compute_cost(usage, pricing, provider) {
|
|
|
287
288
|
return breakdown;
|
|
288
289
|
}
|
|
289
290
|
//#endregion
|
|
290
|
-
//#region src/engine/errors.ts
|
|
291
|
-
var rate_limit_error = class extends Error {
|
|
292
|
-
kind = "rate_limit_error";
|
|
293
|
-
retry_after_ms;
|
|
294
|
-
attempts;
|
|
295
|
-
status;
|
|
296
|
-
constructor(message, metadata = {}) {
|
|
297
|
-
super(message);
|
|
298
|
-
this.name = "rate_limit_error";
|
|
299
|
-
this.retry_after_ms = metadata.retry_after_ms;
|
|
300
|
-
this.attempts = metadata.attempts ?? 0;
|
|
301
|
-
this.status = metadata.status;
|
|
302
|
-
}
|
|
303
|
-
};
|
|
304
|
-
var provider_error = class extends Error {
|
|
305
|
-
kind = "provider_error";
|
|
306
|
-
status;
|
|
307
|
-
body;
|
|
308
|
-
cause_kind;
|
|
309
|
-
constructor(message, metadata = {}) {
|
|
310
|
-
super(message);
|
|
311
|
-
this.name = "provider_error";
|
|
312
|
-
this.status = metadata.status;
|
|
313
|
-
this.body = metadata.body;
|
|
314
|
-
this.cause_kind = metadata.cause_kind;
|
|
315
|
-
}
|
|
316
|
-
};
|
|
317
|
-
/**
|
|
318
|
-
* Turn-timeout expiry. Thrown by the engine-owned retry_turn wrapper when a
|
|
319
|
-
* single invoke_turn exceeds `turn_timeout_ms` before any chunk streamed.
|
|
320
|
-
* Its `kind` is the RetryFailureKind wire value `'timeout'` (not the class
|
|
321
|
-
* name) on purpose: that is the discriminant classify_retryable /
|
|
322
|
-
* classify_provider_error already key on, so the shared classifier treats an
|
|
323
|
-
* expiry as retryable without any timeout-specific branch. A mid-stream
|
|
324
|
-
* expiry never reaches here; retry_turn converts it to a non-retryable
|
|
325
|
-
* stream interruption first.
|
|
326
|
-
*/
|
|
327
|
-
var turn_timeout_error = class extends Error {
|
|
328
|
-
kind = "timeout";
|
|
329
|
-
timeout_ms;
|
|
330
|
-
step_index;
|
|
331
|
-
constructor(timeout_ms, step_index) {
|
|
332
|
-
super(`turn exceeded turn_timeout_ms budget of ${timeout_ms}ms`);
|
|
333
|
-
this.name = "turn_timeout_error";
|
|
334
|
-
this.timeout_ms = timeout_ms;
|
|
335
|
-
this.step_index = step_index;
|
|
336
|
-
}
|
|
337
|
-
};
|
|
338
|
-
var schema_validation_error = class extends Error {
|
|
339
|
-
kind = "schema_validation_error";
|
|
340
|
-
schema_issues;
|
|
341
|
-
raw_text;
|
|
342
|
-
constructor(message, schema_issues, raw_text) {
|
|
343
|
-
super(message);
|
|
344
|
-
this.name = "schema_validation_error";
|
|
345
|
-
this.schema_issues = schema_issues;
|
|
346
|
-
this.raw_text = raw_text;
|
|
347
|
-
}
|
|
348
|
-
};
|
|
349
|
-
var tool_error = class extends Error {
|
|
350
|
-
kind = "tool_error";
|
|
351
|
-
tool_name;
|
|
352
|
-
tool_call_id;
|
|
353
|
-
cause;
|
|
354
|
-
constructor(message, metadata) {
|
|
355
|
-
super(message);
|
|
356
|
-
this.name = "tool_error";
|
|
357
|
-
this.tool_name = metadata.tool_name;
|
|
358
|
-
this.tool_call_id = metadata.tool_call_id;
|
|
359
|
-
this.cause = metadata.cause;
|
|
360
|
-
}
|
|
361
|
-
};
|
|
362
|
-
var tool_approval_denied_error = class extends Error {
|
|
363
|
-
kind = "tool_approval_denied_error";
|
|
364
|
-
tool_name;
|
|
365
|
-
step_index;
|
|
366
|
-
tool_call_id;
|
|
367
|
-
constructor(message, metadata) {
|
|
368
|
-
super(message);
|
|
369
|
-
this.name = "tool_approval_denied_error";
|
|
370
|
-
this.tool_name = metadata.tool_name;
|
|
371
|
-
this.step_index = metadata.step_index;
|
|
372
|
-
this.tool_call_id = metadata.tool_call_id;
|
|
373
|
-
}
|
|
374
|
-
};
|
|
375
|
-
var model_required_error = class extends Error {
|
|
376
|
-
kind = "model_required_error";
|
|
377
|
-
constructor(message = "no model specified: pass `model` to generate() or set `defaults.model` on the engine") {
|
|
378
|
-
super(message);
|
|
379
|
-
this.name = "model_required_error";
|
|
380
|
-
}
|
|
381
|
-
};
|
|
382
|
-
var provider_not_configured_error = class extends Error {
|
|
383
|
-
kind = "provider_not_configured_error";
|
|
384
|
-
provider;
|
|
385
|
-
constructor(provider) {
|
|
386
|
-
super(`provider '${provider}' is not configured on this engine`);
|
|
387
|
-
this.name = "provider_not_configured_error";
|
|
388
|
-
this.provider = provider;
|
|
389
|
-
}
|
|
390
|
-
};
|
|
391
|
-
/**
|
|
392
|
-
* No provider could be resolved for a generate call: several providers are
|
|
393
|
-
* configured, the call named none, and no `defaults.provider` is set. Carries
|
|
394
|
-
* the configured provider names so the message tells the caller exactly what
|
|
395
|
-
* the engine can route to.
|
|
396
|
-
*/
|
|
397
|
-
var provider_required_error = class extends Error {
|
|
398
|
-
kind = "provider_required_error";
|
|
399
|
-
configured;
|
|
400
|
-
constructor(configured) {
|
|
401
|
-
super(`no provider specified: pass \`provider\` to generate() or set \`defaults.provider\` (configured: ${configured.join(", ")})`);
|
|
402
|
-
this.name = "provider_required_error";
|
|
403
|
-
this.configured = configured;
|
|
404
|
-
}
|
|
405
|
-
};
|
|
406
|
-
var engine_config_error = class extends Error {
|
|
407
|
-
kind = "engine_config_error";
|
|
408
|
-
provider;
|
|
409
|
-
constructor(message, provider) {
|
|
410
|
-
super(message);
|
|
411
|
-
this.name = "engine_config_error";
|
|
412
|
-
this.provider = provider;
|
|
413
|
-
}
|
|
414
|
-
};
|
|
415
|
-
var on_chunk_error = class extends Error {
|
|
416
|
-
kind = "on_chunk_error";
|
|
417
|
-
cause;
|
|
418
|
-
constructor(message, cause) {
|
|
419
|
-
super(message);
|
|
420
|
-
this.name = "on_chunk_error";
|
|
421
|
-
this.cause = cause;
|
|
422
|
-
}
|
|
423
|
-
};
|
|
424
|
-
var provider_capability_error = class extends Error {
|
|
425
|
-
kind = "provider_capability_error";
|
|
426
|
-
provider;
|
|
427
|
-
capability;
|
|
428
|
-
constructor(provider, capability, detail) {
|
|
429
|
-
const suffix = detail !== void 0 ? `: ${detail}` : "";
|
|
430
|
-
super(`provider '${provider}' does not support '${capability}'${suffix}`);
|
|
431
|
-
this.name = "provider_capability_error";
|
|
432
|
-
this.provider = provider;
|
|
433
|
-
this.capability = capability;
|
|
434
|
-
}
|
|
435
|
-
};
|
|
436
|
-
var engine_disposed_error = class extends Error {
|
|
437
|
-
kind = "engine_disposed_error";
|
|
438
|
-
constructor(message = "engine has been disposed; further calls are not permitted") {
|
|
439
|
-
super(message);
|
|
440
|
-
this.name = "engine_disposed_error";
|
|
441
|
-
}
|
|
442
|
-
};
|
|
443
|
-
var claude_cli_error = class extends Error {
|
|
444
|
-
kind = "claude_cli_error";
|
|
445
|
-
reason;
|
|
446
|
-
status;
|
|
447
|
-
stderr_snippet;
|
|
448
|
-
constructor(reason, message, metadata = {}) {
|
|
449
|
-
super(message);
|
|
450
|
-
this.name = "claude_cli_error";
|
|
451
|
-
this.reason = reason;
|
|
452
|
-
this.status = metadata.status;
|
|
453
|
-
this.stderr_snippet = metadata.stderr_snippet;
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
var provider_auth_error = class extends Error {
|
|
457
|
-
kind = "provider_auth_error";
|
|
458
|
-
provider;
|
|
459
|
-
refresh_command;
|
|
460
|
-
constructor(provider, message, metadata = {}) {
|
|
461
|
-
super(message);
|
|
462
|
-
this.name = "provider_auth_error";
|
|
463
|
-
this.provider = provider;
|
|
464
|
-
this.refresh_command = metadata.refresh_command;
|
|
465
|
-
}
|
|
466
|
-
};
|
|
467
|
-
/**
|
|
468
|
-
* A generate call that set a schema finished for a reason other than 'stop'
|
|
469
|
-
* (content_filter, length, max_steps, tool_calls), so no schema-validated
|
|
470
|
-
* value exists and validation was never attempted. Carries the raw model text
|
|
471
|
-
* and the last provider-reported payload, so a guardrail trace or other
|
|
472
|
-
* block detail stays reachable from the catch. Distinct from
|
|
473
|
-
* schema_validation_error, which covers a response that completed normally
|
|
474
|
-
* and then failed validation after its repair attempts ran out.
|
|
475
|
-
*/
|
|
476
|
-
var incomplete_generation_error = class extends Error {
|
|
477
|
-
kind = "incomplete_generation_error";
|
|
478
|
-
finish_reason;
|
|
479
|
-
raw_text;
|
|
480
|
-
provider_reported;
|
|
481
|
-
constructor(finish_reason, raw_text, provider_reported) {
|
|
482
|
-
super(`generation finished with '${finish_reason}' before a schema-validated response was produced`);
|
|
483
|
-
this.name = "incomplete_generation_error";
|
|
484
|
-
this.finish_reason = finish_reason;
|
|
485
|
-
this.raw_text = raw_text;
|
|
486
|
-
this.provider_reported = provider_reported;
|
|
487
|
-
}
|
|
488
|
-
};
|
|
489
|
-
//#endregion
|
|
490
291
|
//#region src/engine/retry.ts
|
|
491
292
|
const DEFAULT_RETRY = {
|
|
492
293
|
max_attempts: 3,
|
|
@@ -529,7 +330,7 @@ function is_retryable(kind, policy) {
|
|
|
529
330
|
* The engine's convention, and the opposite of core's propagate-verbatim rule.
|
|
530
331
|
* Two things depend on the wrap. A bare `controller.abort()` sets
|
|
531
332
|
* `signal.reason` to a `DOMException`, which is an `Error` on Node, so
|
|
532
|
-
* propagating would leak a non-
|
|
333
|
+
* propagating would leak a non-Fascicle error out of a boundary documented as
|
|
533
334
|
* throwing `aborted_error`. And the engine's other abort sites (`generate`,
|
|
534
335
|
* `tool_loop`, the adapters) wrap in order to attach `step_index` and
|
|
535
336
|
* `tool_call_in_flight`, which only the construction site knows; propagating
|
|
@@ -586,6 +387,22 @@ function next_backoff(retryable, backoff_policy, attempt) {
|
|
|
586
387
|
return { delay };
|
|
587
388
|
}
|
|
588
389
|
/**
|
|
390
|
+
* Assemble the `on_retry` payload from the classified failure and the backoff
|
|
391
|
+
* the retry will actually wait.
|
|
392
|
+
*/
|
|
393
|
+
function build_attempt_info(retryable, attempt, delay_ms) {
|
|
394
|
+
const info = {
|
|
395
|
+
attempt,
|
|
396
|
+
failure_kind: retryable.kind,
|
|
397
|
+
delay_ms
|
|
398
|
+
};
|
|
399
|
+
if (retryable.kind === "rate_limit" || retryable.kind === "provider_5xx") {
|
|
400
|
+
if (retryable.status !== void 0) info.status = retryable.status;
|
|
401
|
+
}
|
|
402
|
+
if (retryable.kind === "rate_limit" && retryable.retry_after_ms !== void 0) info.retry_after_ms = retryable.retry_after_ms;
|
|
403
|
+
return info;
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
589
406
|
* Retry `fn` under `policy`. `fn` must throw a RetryableError-shaped object to
|
|
590
407
|
* trigger retry; any other thrown value short-circuits as a permanent failure.
|
|
591
408
|
*
|
|
@@ -593,8 +410,13 @@ function next_backoff(retryable, backoff_policy, attempt) {
|
|
|
593
410
|
* `aborted_error`. Returns the value from the last successful `fn()` call. On
|
|
594
411
|
* exhaustion, throws `rate_limit_error` (for 429s) or `provider_error` (for
|
|
595
412
|
* 5xx/network/timeout).
|
|
413
|
+
*
|
|
414
|
+
* `on_retry` fires once per retried failure, just before its backoff wait.
|
|
415
|
+
* It does NOT fire for a first attempt that succeeds, a non-retryable
|
|
416
|
+
* failure, or the final failure that exhausts the policy (those two throw
|
|
417
|
+
* instead), so its call count is exactly the number of extra attempts made.
|
|
596
418
|
*/
|
|
597
|
-
async function retry_with_policy(fn, policy = DEFAULT_RETRY, abort) {
|
|
419
|
+
async function retry_with_policy(fn, policy = DEFAULT_RETRY, abort, on_retry) {
|
|
598
420
|
const backoff_policy = {
|
|
599
421
|
initial_delay_ms: policy.initial_delay_ms,
|
|
600
422
|
max_delay_ms: policy.max_delay_ms,
|
|
@@ -614,6 +436,7 @@ async function retry_with_policy(fn, policy = DEFAULT_RETRY, abort) {
|
|
|
614
436
|
if (attempt >= policy.max_attempts) throw build_exhaustion_error(retryable, attempt, last_rate_limit_after);
|
|
615
437
|
const backoff = next_backoff(retryable, backoff_policy, attempt);
|
|
616
438
|
last_rate_limit_after = backoff.retry_after_ms ?? last_rate_limit_after;
|
|
439
|
+
on_retry?.(build_attempt_info(retryable, attempt, backoff.delay));
|
|
617
440
|
await wait_with_abort(backoff.delay, signal, to_abort_error);
|
|
618
441
|
}
|
|
619
442
|
}
|
|
@@ -686,82 +509,6 @@ function classify_retryable(err) {
|
|
|
686
509
|
if (kind === "network" || kind === "timeout") return build_retryable_signal(err, kind);
|
|
687
510
|
}
|
|
688
511
|
//#endregion
|
|
689
|
-
//#region src/engine/providers/types.ts
|
|
690
|
-
/**
|
|
691
|
-
* Read the `transport` selector off a provider init. Defaults to 'ai_sdk';
|
|
692
|
-
* anything but the two known backends throws at construction so a typo
|
|
693
|
-
* fails loud instead of silently running the default.
|
|
694
|
-
*/
|
|
695
|
-
function resolve_transport(init, provider) {
|
|
696
|
-
const raw = init["transport"];
|
|
697
|
-
if (raw === void 0 || raw === "ai_sdk") return "ai_sdk";
|
|
698
|
-
if (raw === "native") return "native";
|
|
699
|
-
throw new engine_config_error(`${provider} provider: transport must be 'ai_sdk' or 'native', got ${JSON.stringify(raw)}`, provider);
|
|
700
|
-
}
|
|
701
|
-
/**
|
|
702
|
-
* Map raw provider usage to UsageTotals, the mapper most adapters share.
|
|
703
|
-
*
|
|
704
|
-
* Missing usage zeroes the totals. Optional fields (reasoning and cache
|
|
705
|
-
* tokens) are set only when the raw payload carries them, reading the
|
|
706
|
-
* flattened field first and the AI SDK's *_details container second.
|
|
707
|
-
*/
|
|
708
|
-
function default_normalize_usage(raw) {
|
|
709
|
-
if (raw === void 0) return {
|
|
710
|
-
input_tokens: 0,
|
|
711
|
-
output_tokens: 0
|
|
712
|
-
};
|
|
713
|
-
const totals = {
|
|
714
|
-
input_tokens: typeof raw.input_tokens === "number" ? raw.input_tokens : 0,
|
|
715
|
-
output_tokens: typeof raw.output_tokens === "number" ? raw.output_tokens : 0
|
|
716
|
-
};
|
|
717
|
-
if (typeof raw.reasoning_tokens === "number") totals.reasoning_tokens = raw.reasoning_tokens;
|
|
718
|
-
else if (typeof raw.output_token_details?.reasoning_tokens === "number") totals.reasoning_tokens = raw.output_token_details.reasoning_tokens;
|
|
719
|
-
if (typeof raw.cached_input_tokens === "number") totals.cached_input_tokens = raw.cached_input_tokens;
|
|
720
|
-
else if (typeof raw.input_token_details?.cached_tokens === "number") totals.cached_input_tokens = raw.input_token_details.cached_tokens;
|
|
721
|
-
if (typeof raw.cache_write_tokens === "number") totals.cache_write_tokens = raw.cache_write_tokens;
|
|
722
|
-
else if (typeof raw.input_token_details?.cache_creation_input_tokens === "number") totals.cache_write_tokens = raw.input_token_details.cache_creation_input_tokens;
|
|
723
|
-
return totals;
|
|
724
|
-
}
|
|
725
|
-
/**
|
|
726
|
-
* Usage mapper for local backends (Ollama, LM Studio) whose servers never
|
|
727
|
-
* report cache or reasoning tokens: default-normalize, then strip those
|
|
728
|
-
* fields so a stray *_details container cannot invent them.
|
|
729
|
-
*/
|
|
730
|
-
function normalize_local_usage(raw) {
|
|
731
|
-
const base = default_normalize_usage(raw);
|
|
732
|
-
delete base.cached_input_tokens;
|
|
733
|
-
delete base.cache_write_tokens;
|
|
734
|
-
delete base.reasoning_tokens;
|
|
735
|
-
return base;
|
|
736
|
-
}
|
|
737
|
-
/**
|
|
738
|
-
* Build the standard missing-peer error: names the package and the install
|
|
739
|
-
* command so a missing peer is diagnosable from the message alone. Shared so
|
|
740
|
-
* every optional-peer failure reads identically, whether the failing
|
|
741
|
-
* `import()` names the peer directly (load_optional_peer below) or a local
|
|
742
|
-
* module that reaches the peer through its own static import (the ai_sdk
|
|
743
|
-
* turn seam in generate.ts, whose dynamic import must stay written in
|
|
744
|
-
* generate.ts for its relative specifier to resolve correctly).
|
|
745
|
-
*/
|
|
746
|
-
function missing_peer_error(peer_name, cause) {
|
|
747
|
-
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)}`;
|
|
748
|
-
return new Error(message, { cause });
|
|
749
|
-
}
|
|
750
|
-
/**
|
|
751
|
-
* Dynamically import an optional peer dependency, rethrowing an import
|
|
752
|
-
* failure via missing_peer_error so a missing peer is diagnosable from the
|
|
753
|
-
* message alone.
|
|
754
|
-
*/
|
|
755
|
-
async function load_optional_peer(specifier) {
|
|
756
|
-
let mod;
|
|
757
|
-
try {
|
|
758
|
-
mod = await import(specifier);
|
|
759
|
-
} catch (err) {
|
|
760
|
-
throw missing_peer_error(specifier, err);
|
|
761
|
-
}
|
|
762
|
-
return mod;
|
|
763
|
-
}
|
|
764
|
-
//#endregion
|
|
765
512
|
//#region src/engine/providers/native_shared.ts
|
|
766
513
|
/**
|
|
767
514
|
* The message under a JSON error envelope's `error` key, or undefined when
|
|
@@ -831,8 +578,8 @@ function rethrow_network_failure(err, abort, provider) {
|
|
|
831
578
|
* Run one native turn over HTTP: POST the dialect's body, map a non-2xx to
|
|
832
579
|
* the retry stack's shapes, and hand the response to the dialect's stream or
|
|
833
580
|
* JSON consumer. `build_body` is called inside the network try/catch on
|
|
834
|
-
* purpose
|
|
835
|
-
*
|
|
581
|
+
* purpose, so a mapper that throws surfaces as a retryable network failure
|
|
582
|
+
* rather than escaping the turn unclassified.
|
|
836
583
|
*/
|
|
837
584
|
async function invoke_http_turn(req, provider, request, consume) {
|
|
838
585
|
let response;
|
|
@@ -994,7 +741,7 @@ function read_dialect_init(init, default_base_url) {
|
|
|
994
741
|
* wire (any chunk boundary, including mid-line) and returns the data payloads
|
|
995
742
|
* of every event completed by that chunk; flush() drains an event left open
|
|
996
743
|
* when the stream ends without a trailing blank line. Only `data:` fields
|
|
997
|
-
* matter to the streams
|
|
744
|
+
* matter to the streams Fascicle consumes (the Messages API repeats the
|
|
998
745
|
* event type inside the JSON payload and the Chat Completions stream sends
|
|
999
746
|
* nothing but data frames), so `event:`/`id:`/`retry:` fields and `:`
|
|
1000
747
|
* comments are dropped. Multi-line data joins with '\n' per the SSE spec.
|
|
@@ -1109,7 +856,7 @@ function to_assistant_blocks(content) {
|
|
|
1109
856
|
return blocks;
|
|
1110
857
|
}
|
|
1111
858
|
/**
|
|
1112
|
-
* Map
|
|
859
|
+
* Map Fascicle Message[] to Messages-API shape: leading system messages hoist
|
|
1113
860
|
* to the top-level `system` string, tool messages become `tool_result` blocks
|
|
1114
861
|
* on a user turn, and empty text blocks are dropped (the API rejects them).
|
|
1115
862
|
* generate.ts already hoists leading system into TurnRequest.system, so the
|
|
@@ -1155,7 +902,7 @@ function to_anthropic_messages(messages) {
|
|
|
1155
902
|
};
|
|
1156
903
|
}
|
|
1157
904
|
/**
|
|
1158
|
-
* Map
|
|
905
|
+
* Map Fascicle tool definitions to the Messages-API tool shape, converting
|
|
1159
906
|
* each input schema to JSON Schema.
|
|
1160
907
|
*/
|
|
1161
908
|
function to_anthropic_tools(tools) {
|
|
@@ -1703,7 +1450,7 @@ function to_assistant_message$1(content) {
|
|
|
1703
1450
|
return message;
|
|
1704
1451
|
}
|
|
1705
1452
|
/**
|
|
1706
|
-
* Map
|
|
1453
|
+
* Map Fascicle Message[] to Chat Completions shape. Unlike the Messages API
|
|
1707
1454
|
* there is no top-level system field or role-alternation constraint: system
|
|
1708
1455
|
* messages map in place at any position, and tool results are first-class
|
|
1709
1456
|
* `tool` role messages keyed by tool_call_id.
|
|
@@ -1727,7 +1474,7 @@ function to_chat_messages(messages, provider) {
|
|
|
1727
1474
|
});
|
|
1728
1475
|
}
|
|
1729
1476
|
/**
|
|
1730
|
-
* Map
|
|
1477
|
+
* Map Fascicle Tool[] to the Chat Completions function-tool shape,
|
|
1731
1478
|
* converting each input schema to JSON Schema.
|
|
1732
1479
|
*/
|
|
1733
1480
|
function to_chat_tools(tools) {
|
|
@@ -1795,7 +1542,7 @@ function read_number(source, key) {
|
|
|
1795
1542
|
* Map the Chat Completions usage object to UsageTotals. prompt_tokens is
|
|
1796
1543
|
* already INCLUSIVE of cached tokens on this API, unlike Anthropic's
|
|
1797
1544
|
* exclusive accounting, so a straight copy is correct; compute_cost
|
|
1798
|
-
* subtracts the cached portion back out.
|
|
1545
|
+
* subtracts the cached portion back out. No cache-write concept exists on
|
|
1799
1546
|
* this wire. A missing usage object throws under a strict dialect (a hosted
|
|
1800
1547
|
* API omitting usage is a broken response) and zeroes under a tolerant one.
|
|
1801
1548
|
*/
|
|
@@ -2265,7 +2012,7 @@ function to_assistant_message(content) {
|
|
|
2265
2012
|
return message;
|
|
2266
2013
|
}
|
|
2267
2014
|
/**
|
|
2268
|
-
* Map
|
|
2015
|
+
* Map Fascicle Message[] to the /api/chat shape. System messages map in place
|
|
2269
2016
|
* (no top-level system field); tool results are `tool` role messages keyed by
|
|
2270
2017
|
* `tool_name`, since this wire has no tool_call_id to round-trip.
|
|
2271
2018
|
*/
|
|
@@ -2346,6 +2093,28 @@ function map_ollama_usage(payload) {
|
|
|
2346
2093
|
output_tokens: typeof evaluated === "number" ? evaluated : 0
|
|
2347
2094
|
};
|
|
2348
2095
|
}
|
|
2096
|
+
const REPORTED_DURATION_FIELDS = [
|
|
2097
|
+
"total_duration",
|
|
2098
|
+
"load_duration",
|
|
2099
|
+
"prompt_eval_duration",
|
|
2100
|
+
"eval_duration"
|
|
2101
|
+
];
|
|
2102
|
+
/**
|
|
2103
|
+
* Collect the daemon's nanosecond duration fields for verbatim passthrough
|
|
2104
|
+
* under `provider_reported.ollama`: the daemon's own decode/prefill timing,
|
|
2105
|
+
* which is what an honest local tokens-per-second number derives from
|
|
2106
|
+
* (eval_count over eval_duration). Units and names stay the wire's own, per
|
|
2107
|
+
* the provider_reported contract; returns undefined when none are present
|
|
2108
|
+
* (mid-proxy shapes that strip them), never an empty object.
|
|
2109
|
+
*/
|
|
2110
|
+
function map_ollama_reported(payload) {
|
|
2111
|
+
const out = {};
|
|
2112
|
+
for (const key of REPORTED_DURATION_FIELDS) {
|
|
2113
|
+
const value = Reflect.get(payload, key);
|
|
2114
|
+
if (typeof value === "number") out[key] = value;
|
|
2115
|
+
}
|
|
2116
|
+
return Object.keys(out).length > 0 ? out : void 0;
|
|
2117
|
+
}
|
|
2349
2118
|
/**
|
|
2350
2119
|
* This wire sends no tool-call id, so one is synthesized from the step index
|
|
2351
2120
|
* and the call's ordinal within the turn: deterministic, so the streamed and
|
|
@@ -2380,11 +2149,13 @@ function parse_ollama_chat(payload, step_index) {
|
|
|
2380
2149
|
const content = Reflect.get(message, "content");
|
|
2381
2150
|
const raw_tool_calls = Reflect.get(message, "tool_calls");
|
|
2382
2151
|
const tool_calls = Array.isArray(raw_tool_calls) ? raw_tool_calls.map((entry, ordinal) => parse_ollama_tool_call(entry, ordinal, step_index)) : [];
|
|
2152
|
+
const reported = map_ollama_reported(payload);
|
|
2383
2153
|
return {
|
|
2384
2154
|
text: typeof content === "string" ? content : "",
|
|
2385
2155
|
tool_calls,
|
|
2386
2156
|
finish_reason: map_ollama_finish_reason(Reflect.get(payload, "done_reason"), tool_calls.length > 0),
|
|
2387
|
-
usage: map_ollama_usage(payload)
|
|
2157
|
+
usage: map_ollama_usage(payload),
|
|
2158
|
+
...reported !== void 0 ? { provider_reported: { ollama: reported } } : {}
|
|
2388
2159
|
};
|
|
2389
2160
|
}
|
|
2390
2161
|
/**
|
|
@@ -2420,6 +2191,7 @@ function create_ollama_stream_aggregator(step_index, dispatch) {
|
|
|
2420
2191
|
input_tokens: 0,
|
|
2421
2192
|
output_tokens: 0
|
|
2422
2193
|
};
|
|
2194
|
+
let reported;
|
|
2423
2195
|
const on_message = async (message) => {
|
|
2424
2196
|
const content = Reflect.get(message, "content");
|
|
2425
2197
|
if (typeof content === "string" && content.length > 0) {
|
|
@@ -2465,6 +2237,7 @@ function create_ollama_stream_aggregator(step_index, dispatch) {
|
|
|
2465
2237
|
done = true;
|
|
2466
2238
|
done_reason = Reflect.get(frame, "done_reason");
|
|
2467
2239
|
usage = map_ollama_usage(frame);
|
|
2240
|
+
reported = map_ollama_reported(frame);
|
|
2468
2241
|
await dispatch({
|
|
2469
2242
|
kind: "step_finish",
|
|
2470
2243
|
step_index,
|
|
@@ -2491,7 +2264,8 @@ function create_ollama_stream_aggregator(step_index, dispatch) {
|
|
|
2491
2264
|
done: true,
|
|
2492
2265
|
done_reason: done_reason ?? null,
|
|
2493
2266
|
prompt_eval_count: usage.input_tokens,
|
|
2494
|
-
eval_count: usage.output_tokens
|
|
2267
|
+
eval_count: usage.output_tokens,
|
|
2268
|
+
...reported
|
|
2495
2269
|
}, step_index);
|
|
2496
2270
|
}
|
|
2497
2271
|
};
|
|
@@ -2925,7 +2699,7 @@ const DEFAULT_SETTING_SOURCES = Object.freeze(["project", "local"]);
|
|
|
2925
2699
|
* (greywall/bwrap on PATH, then `claude`) can be resolved and run.
|
|
2926
2700
|
*
|
|
2927
2701
|
* Mirrors `STANDARD_KEYS` in `src/forward_standard_env.ts`. Keep the two
|
|
2928
|
-
* lists in sync; engine cannot import from
|
|
2702
|
+
* lists in sync; engine cannot import from Fascicle (the umbrella depends
|
|
2929
2703
|
* on engine, not the other way).
|
|
2930
2704
|
*/
|
|
2931
2705
|
const STANDARD_ENV_KEYS = [
|
|
@@ -3663,7 +3437,7 @@ function create_parser_state() {
|
|
|
3663
3437
|
};
|
|
3664
3438
|
}
|
|
3665
3439
|
/**
|
|
3666
|
-
* Convert the CLI's raw usage fields to
|
|
3440
|
+
* Convert the CLI's raw usage fields to Fascicle's `UsageTotals` shape.
|
|
3667
3441
|
*
|
|
3668
3442
|
* `cache_read_input_tokens` maps to `cached_input_tokens` and
|
|
3669
3443
|
* `cache_creation_input_tokens` maps to `cache_write_tokens`; both are
|
|
@@ -4231,7 +4005,7 @@ function build_generate_result(input) {
|
|
|
4231
4005
|
* When every candidate fails, we prefer the schema-validation issues from the
|
|
4232
4006
|
* FIRST candidate that parsed as JSON: those issues reflect the model's
|
|
4233
4007
|
* primary output and are what a repair prompt should feed back. Later
|
|
4234
|
-
* candidates (
|
|
4008
|
+
* candidates (for example, the bracket-slice fallback) often produce noisy issues
|
|
4235
4009
|
* like "expected object, received array" that misdirect the model. Only when
|
|
4236
4010
|
* NO candidate parses as JSON do we surface the JSON parse error.
|
|
4237
4011
|
*
|
|
@@ -4336,7 +4110,10 @@ function start_generate_span(trajectory, meta) {
|
|
|
4336
4110
|
}
|
|
4337
4111
|
/**
|
|
4338
4112
|
* Close the `engine.generate` span with usage, finish reason, resolved model,
|
|
4339
|
-
* or the error message on failure.
|
|
4113
|
+
* or the error message on failure. `step_count` and `tool_call_count` are the
|
|
4114
|
+
* call-level totals (model turns and tool executions in the call), the
|
|
4115
|
+
* span-attribute analog of the GenAI semconv agent metrics
|
|
4116
|
+
* `inference_calls` / `tool_calls`.
|
|
4340
4117
|
*/
|
|
4341
4118
|
function end_generate_span(trajectory, span_id, meta) {
|
|
4342
4119
|
if (trajectory === void 0 || span_id === void 0) return;
|
|
@@ -4357,6 +4134,64 @@ function end_step_span(trajectory, span_id, meta) {
|
|
|
4357
4134
|
trajectory.end_span(span_id, { ...meta });
|
|
4358
4135
|
}
|
|
4359
4136
|
/**
|
|
4137
|
+
* Record a provider response: finish reason, flat token counts, and the
|
|
4138
|
+
* engine-measured turn timing when the turn carried one.
|
|
4139
|
+
*
|
|
4140
|
+
* Every field is a flat number or string on purpose. An OTel span event or a
|
|
4141
|
+
* log pipeline can aggregate `input_tokens` / `duration_ms` /
|
|
4142
|
+
* `first_chunk_ms` directly, where a nested usage object would arrive as an
|
|
4143
|
+
* opaque JSON string. This is the one per-turn event a metrics dashboard
|
|
4144
|
+
* needs: tokens by direction, the cache and reasoning splits when the
|
|
4145
|
+
* provider reports them, and the model-only latency window.
|
|
4146
|
+
*/
|
|
4147
|
+
function record_response_received(trajectory, step_index, turn) {
|
|
4148
|
+
if (trajectory === void 0) return;
|
|
4149
|
+
const { usage, timing } = turn;
|
|
4150
|
+
const event = {
|
|
4151
|
+
kind: "response_received",
|
|
4152
|
+
step_index,
|
|
4153
|
+
input_tokens: usage.input_tokens,
|
|
4154
|
+
output_tokens: usage.output_tokens,
|
|
4155
|
+
finish_reason: turn.finish_reason
|
|
4156
|
+
};
|
|
4157
|
+
if (usage.reasoning_tokens !== void 0) event["reasoning_tokens"] = usage.reasoning_tokens;
|
|
4158
|
+
if (usage.cached_input_tokens !== void 0) event["cached_input_tokens"] = usage.cached_input_tokens;
|
|
4159
|
+
if (usage.cache_write_tokens !== void 0) event["cache_write_tokens"] = usage.cache_write_tokens;
|
|
4160
|
+
if (timing !== void 0) {
|
|
4161
|
+
event["started_at"] = timing.started_at;
|
|
4162
|
+
event["duration_ms"] = timing.duration_ms;
|
|
4163
|
+
if (timing.first_chunk_ms !== void 0) event["first_chunk_ms"] = timing.first_chunk_ms;
|
|
4164
|
+
}
|
|
4165
|
+
trajectory.record({
|
|
4166
|
+
kind: "response_received",
|
|
4167
|
+
...event
|
|
4168
|
+
});
|
|
4169
|
+
}
|
|
4170
|
+
/**
|
|
4171
|
+
* Record one failed-then-retried provider attempt as a `turn_retry` event:
|
|
4172
|
+
* the 1-based attempt count, the classified failure kind, the backoff this
|
|
4173
|
+
* retry waits, and the HTTP status / `Retry-After` when the failure carried
|
|
4174
|
+
* them. This is the event a rate-limit or error-budget dashboard counts;
|
|
4175
|
+
* without it, only the final exhaustion is visible (as a span error), and
|
|
4176
|
+
* every absorbed 429 or 5xx goes unrecorded.
|
|
4177
|
+
*/
|
|
4178
|
+
function record_turn_retry(trajectory, step_index, info) {
|
|
4179
|
+
if (trajectory === void 0) return;
|
|
4180
|
+
const event = {
|
|
4181
|
+
kind: "turn_retry",
|
|
4182
|
+
step_index,
|
|
4183
|
+
attempt: info.attempt,
|
|
4184
|
+
failure_kind: info.failure_kind,
|
|
4185
|
+
delay_ms: info.delay_ms
|
|
4186
|
+
};
|
|
4187
|
+
if (info.status !== void 0) event["status"] = info.status;
|
|
4188
|
+
if (info.retry_after_ms !== void 0) event["retry_after_ms"] = info.retry_after_ms;
|
|
4189
|
+
trajectory.record({
|
|
4190
|
+
kind: "turn_retry",
|
|
4191
|
+
...event
|
|
4192
|
+
});
|
|
4193
|
+
}
|
|
4194
|
+
/**
|
|
4360
4195
|
* Record a tool invocation request: name, call id, input, and duration.
|
|
4361
4196
|
*/
|
|
4362
4197
|
function record_tool_call(trajectory, meta) {
|
|
@@ -4749,7 +4584,7 @@ function emit_ignored_options(opts, trajectory) {
|
|
|
4749
4584
|
function validate_request(prompt, tools_list, tool_bridge) {
|
|
4750
4585
|
if (count_user_messages(prompt) >= 2) throw new provider_capability_error(PROVIDER_NAME, "multi_turn_history", "use provider_options.claude_cli.session_id instead");
|
|
4751
4586
|
if (tools_list.length > 0 && tool_bridge === "forbid") {
|
|
4752
|
-
if (tools_list.some((t) => typeof t.execute === "function")) throw new provider_capability_error(PROVIDER_NAME, "tool_execute", "tool_bridge is forbid
|
|
4587
|
+
if (tools_list.some((t) => typeof t.execute === "function")) throw new provider_capability_error(PROVIDER_NAME, "tool_execute", "tool_bridge is 'forbid' and this tool has an execute closure — set tool_bridge: 'allowlist_only' or drop the closure");
|
|
4753
4588
|
}
|
|
4754
4589
|
}
|
|
4755
4590
|
/**
|
|
@@ -4904,7 +4739,7 @@ function record_provider_costs(trajectory, steps) {
|
|
|
4904
4739
|
for (const step of steps) if (step.cost !== void 0) record_cost(trajectory, step.index, step.cost, "provider_reported");
|
|
4905
4740
|
}
|
|
4906
4741
|
/**
|
|
4907
|
-
* Close the eagerly
|
|
4742
|
+
* Close the eagerly opened first step span against the steps the CLI
|
|
4908
4743
|
* reported, then emit paired open/close spans for steps 1..n; those arrive
|
|
4909
4744
|
* all at once on process close, so their spans carry usage, not timing.
|
|
4910
4745
|
* Clears `spans.first_step` so the error path cannot close it twice.
|
|
@@ -4944,7 +4779,7 @@ async function dispatch_finish(dispatch_chunk, result) {
|
|
|
4944
4779
|
}
|
|
4945
4780
|
/**
|
|
4946
4781
|
* Close whatever spans are still open with the failure message. A failure
|
|
4947
|
-
* after the step spans already closed (
|
|
4782
|
+
* after the step spans already closed (for example, in the finish-chunk dispatch)
|
|
4948
4783
|
* closes only the generate span.
|
|
4949
4784
|
*/
|
|
4950
4785
|
function close_spans_on_error(trajectory, spans, err) {
|
|
@@ -5078,48 +4913,6 @@ function get_provider_factory(name) {
|
|
|
5078
4913
|
return factory;
|
|
5079
4914
|
}
|
|
5080
4915
|
//#endregion
|
|
5081
|
-
//#region src/engine/leading_system.ts
|
|
5082
|
-
/**
|
|
5083
|
-
* Collapse a leading run of system messages into a single joined string,
|
|
5084
|
-
* returning the remaining messages.
|
|
5085
|
-
*
|
|
5086
|
-
* Both message paths hoist a leading system run out of their list the same way:
|
|
5087
|
-
* the engine-Message side (generate.ts split_leading_system_messages) hands the
|
|
5088
|
-
* run to a native adapter's TurnRequest.system, and the AI SDK side
|
|
5089
|
-
* (providers/ai_sdk/invoke.ts split_leading_system) hands it to the SDK's
|
|
5090
|
-
* top-level `instructions` option. Same guard, same '\n\n' join, and the same
|
|
5091
|
-
* "return the list untouched when hoisting would leave `messages` empty" rule,
|
|
5092
|
-
* since provider APIs reject an empty messages array. They differ only in the
|
|
5093
|
-
* element type and how a system message's text is read, so each caller supplies
|
|
5094
|
-
* `system_text`; the traversal and its guards live here once.
|
|
5095
|
-
*/
|
|
5096
|
-
function split_leading_system_run(messages, system_text) {
|
|
5097
|
-
let run_end = 0;
|
|
5098
|
-
const system_parts = [];
|
|
5099
|
-
while (run_end < messages.length) {
|
|
5100
|
-
const m = messages[run_end];
|
|
5101
|
-
if (!is_leading_system(m)) break;
|
|
5102
|
-
system_parts.push(system_text(m));
|
|
5103
|
-
run_end += 1;
|
|
5104
|
-
}
|
|
5105
|
-
const rest = messages.slice(run_end);
|
|
5106
|
-
if (system_parts.length === 0 || rest.length === 0) return { messages: [...messages] };
|
|
5107
|
-
return {
|
|
5108
|
-
system: system_parts.join("\n\n"),
|
|
5109
|
-
messages: rest
|
|
5110
|
-
};
|
|
5111
|
-
}
|
|
5112
|
-
/**
|
|
5113
|
-
* Narrow a possibly-undefined message to the system member of its union.
|
|
5114
|
-
*
|
|
5115
|
-
* A cast-free type guard so `split_leading_system_run` can hand the narrowed
|
|
5116
|
-
* message straight to `system_text` without a `T`-eroding assertion; the
|
|
5117
|
-
* optional chain covers the out-of-bounds `undefined` the index access yields.
|
|
5118
|
-
*/
|
|
5119
|
-
function is_leading_system(m) {
|
|
5120
|
-
return m?.role === "system";
|
|
5121
|
-
}
|
|
5122
|
-
//#endregion
|
|
5123
4916
|
//#region src/engine/merge_defaults.ts
|
|
5124
4917
|
/**
|
|
5125
4918
|
* Default-merging helpers for engine-level `EngineDefaults`.
|
|
@@ -5131,9 +4924,9 @@ function is_leading_system(m) {
|
|
|
5131
4924
|
* `provider_options` needs a two-level merge: outer keys are provider names
|
|
5132
4925
|
* (`claude_cli`, `anthropic`, ...) and inner records hold per-provider
|
|
5133
4926
|
* settings. Per-call inner keys override the matching default's inner keys;
|
|
5134
|
-
* provider keys unique to either side fall through unchanged.
|
|
5135
|
-
*
|
|
5136
|
-
*
|
|
4927
|
+
* provider keys unique to either side fall through unchanged. Recursion stops
|
|
4928
|
+
* at that second level, so deeper structures replace wholesale rather than
|
|
4929
|
+
* merging further.
|
|
5137
4930
|
*/
|
|
5138
4931
|
function merge_provider_options(defaults, call) {
|
|
5139
4932
|
if (defaults === void 0 && call === void 0) return void 0;
|
|
@@ -5724,7 +5517,7 @@ async function request_approval(tool, input, step_index, tool_call_id, abort, on
|
|
|
5724
5517
|
step_index,
|
|
5725
5518
|
tool_call_id
|
|
5726
5519
|
});
|
|
5727
|
-
throw new tool_approval_denied_error(`tool approval required for '${tool.name}'
|
|
5520
|
+
throw new tool_approval_denied_error(`tool approval required for '${tool.name}': pass an on_tool_approval handler to generate(), or clear needs_approval on the tool`, {
|
|
5728
5521
|
tool_name: tool.name,
|
|
5729
5522
|
step_index,
|
|
5730
5523
|
tool_call_id
|
|
@@ -5914,12 +5707,7 @@ async function invoke_turn(config, step_index, step_span) {
|
|
|
5914
5707
|
abort: config.abort,
|
|
5915
5708
|
stream: config.stream
|
|
5916
5709
|
});
|
|
5917
|
-
config.trajectory
|
|
5918
|
-
kind: "response_received",
|
|
5919
|
-
step_index,
|
|
5920
|
-
output_tokens: turn.usage.output_tokens,
|
|
5921
|
-
finish_reason: turn.finish_reason
|
|
5922
|
-
});
|
|
5710
|
+
record_response_received(config.trajectory, step_index, turn);
|
|
5923
5711
|
return turn;
|
|
5924
5712
|
} catch (err) {
|
|
5925
5713
|
const message = err instanceof Error ? err.message : String(err);
|
|
@@ -6237,8 +6025,8 @@ async function record_execute_failure(ctx, tool, raw_call, input, failure) {
|
|
|
6237
6025
|
});
|
|
6238
6026
|
}
|
|
6239
6027
|
/**
|
|
6240
|
-
* Run one raw tool call through the guard chain
|
|
6241
|
-
* lookup, input validation, approval
|
|
6028
|
+
* Run one raw tool call through the guard chain (final-turn skip, tool
|
|
6029
|
+
* lookup, input validation, approval), then execute it. Each guard either
|
|
6242
6030
|
* records the failure (feed-back policy) and stops, or throws (throw
|
|
6243
6031
|
* policy / abort).
|
|
6244
6032
|
*/
|
|
@@ -6301,8 +6089,9 @@ function resolve_turn_finish_reason(ctx, salvaged, turn_finish) {
|
|
|
6301
6089
|
return turn_finish;
|
|
6302
6090
|
}
|
|
6303
6091
|
/**
|
|
6304
|
-
* Build one StepRecord
|
|
6305
|
-
* passed through
|
|
6092
|
+
* Build one StepRecord (cost derived and recorded, timing and
|
|
6093
|
+
* provider_reported passed through), push it, notify on_finish_step, and
|
|
6094
|
+
* close the step span.
|
|
6306
6095
|
*/
|
|
6307
6096
|
function push_step_record(config, acc, step_index, turn, tool_calls, finish_reason, step_span) {
|
|
6308
6097
|
const record = {
|
|
@@ -6314,6 +6103,7 @@ function push_step_record(config, acc, step_index, turn, tool_calls, finish_reas
|
|
|
6314
6103
|
};
|
|
6315
6104
|
const breakdown = compute_and_record_cost(config, step_index, turn.usage);
|
|
6316
6105
|
if (breakdown !== void 0) record.cost = breakdown;
|
|
6106
|
+
if (turn.timing !== void 0) record.timing = turn.timing;
|
|
6317
6107
|
if (turn.provider_reported !== void 0) record.provider_reported = turn.provider_reported;
|
|
6318
6108
|
acc.steps.push(record);
|
|
6319
6109
|
if (config.on_finish_step !== void 0) config.on_finish_step(record);
|
|
@@ -6558,12 +6348,24 @@ function arm_turn_timeout(user_abort, turn_timeout_ms) {
|
|
|
6558
6348
|
* actually cancels the in-flight request. Adapters may swap `classify`, but
|
|
6559
6349
|
* never the ladder itself: the engine owns retry, so a hidden adapter-level
|
|
6560
6350
|
* retry would be invisible to anything relying on this ladder.
|
|
6561
|
-
|
|
6562
|
-
|
|
6351
|
+
*
|
|
6352
|
+
* The wrapper also stamps StepTiming here, and only here, because this is the
|
|
6353
|
+
* one place that can see attempt boundaries: started_at re-stamps at every
|
|
6354
|
+
* attempt entry, so failed attempts and backoff waits never inflate
|
|
6355
|
+
* duration_ms. `first_chunk_at` (the builder's Date.now() from its first
|
|
6356
|
+
* dispatched chunk) can only belong to the returning attempt, since any
|
|
6357
|
+
* failure after a chunk has flowed is non-retryable by the ladder below.
|
|
6358
|
+
*/
|
|
6359
|
+
function retry_turn(call_once, args, first_chunk_at, retry_policy, classify, turn_timeout_ms, trajectory) {
|
|
6360
|
+
const has_streamed = () => first_chunk_at() !== void 0;
|
|
6563
6361
|
return retry_with_policy(async () => {
|
|
6362
|
+
const started_at = Date.now();
|
|
6564
6363
|
const deadline = arm_turn_timeout(args.abort, turn_timeout_ms);
|
|
6565
6364
|
try {
|
|
6566
|
-
return
|
|
6365
|
+
return {
|
|
6366
|
+
...await call_once(deadline.signal),
|
|
6367
|
+
timing: build_step_timing(started_at, first_chunk_at())
|
|
6368
|
+
};
|
|
6567
6369
|
} catch (err) {
|
|
6568
6370
|
if (err instanceof on_chunk_error) throw err;
|
|
6569
6371
|
if (args.abort.aborted) throw new aborted_error("aborted", {
|
|
@@ -6577,28 +6379,43 @@ function retry_turn(call_once, args, has_streamed, retry_policy, classify, turn_
|
|
|
6577
6379
|
} finally {
|
|
6578
6380
|
deadline.dispose();
|
|
6579
6381
|
}
|
|
6580
|
-
}, retry_policy, args.abort)
|
|
6382
|
+
}, retry_policy, args.abort, (info) => {
|
|
6383
|
+
record_turn_retry(trajectory, args.step_index, info);
|
|
6384
|
+
});
|
|
6385
|
+
}
|
|
6386
|
+
/**
|
|
6387
|
+
* Assemble one turn's StepTiming from the attempt's start stamp and the
|
|
6388
|
+
* builder's first-chunk stamp (undefined on non-streamed turns).
|
|
6389
|
+
*/
|
|
6390
|
+
function build_step_timing(started_at, first_chunk) {
|
|
6391
|
+
const timing = {
|
|
6392
|
+
started_at,
|
|
6393
|
+
duration_ms: Date.now() - started_at
|
|
6394
|
+
};
|
|
6395
|
+
if (first_chunk !== void 0) timing.first_chunk_ms = first_chunk - started_at;
|
|
6396
|
+
return timing;
|
|
6581
6397
|
}
|
|
6582
6398
|
/**
|
|
6583
6399
|
* Build the ai_sdk-transport InvokeOnce: the SDK turn built by
|
|
6584
6400
|
* providers/ai_sdk/invoke.ts behind the same engine-owned retry_turn wrapper
|
|
6585
6401
|
* the native path uses. The SDK call body lives behind the seam; this builder
|
|
6586
6402
|
* owns only retry and the streamed-output tracking that makes a mid-stream
|
|
6587
|
-
* failure a non-retryable interruption
|
|
6403
|
+
* failure a non-retryable interruption (the same first-chunk stamp doubles as
|
|
6404
|
+
* StepTiming.first_chunk_ms).
|
|
6588
6405
|
*/
|
|
6589
6406
|
function build_ai_sdk_invoke(cfg) {
|
|
6590
6407
|
return async (args) => {
|
|
6591
|
-
let
|
|
6408
|
+
let first_chunk_at;
|
|
6592
6409
|
const call_once = (turn_abort) => cfg.invoke_turn({
|
|
6593
6410
|
step_index: args.step_index,
|
|
6594
6411
|
messages: args.messages,
|
|
6595
6412
|
abort: turn_abort,
|
|
6596
6413
|
stream: args.stream,
|
|
6597
6414
|
on_first_chunk: () => {
|
|
6598
|
-
|
|
6415
|
+
first_chunk_at ??= Date.now();
|
|
6599
6416
|
}
|
|
6600
6417
|
});
|
|
6601
|
-
return await retry_turn(call_once, args, () =>
|
|
6418
|
+
return await retry_turn(call_once, args, () => first_chunk_at, cfg.retry_policy, classify_provider_error, cfg.turn_timeout_ms, cfg.trajectory);
|
|
6602
6419
|
};
|
|
6603
6420
|
}
|
|
6604
6421
|
/**
|
|
@@ -6612,7 +6429,7 @@ function build_ai_sdk_invoke(cfg) {
|
|
|
6612
6429
|
function build_native_invoke(cfg) {
|
|
6613
6430
|
const classify = cfg.adapter.classify_error ?? classify_provider_error;
|
|
6614
6431
|
return async (args) => {
|
|
6615
|
-
let
|
|
6432
|
+
let first_chunk_at;
|
|
6616
6433
|
const call_once = async (turn_abort) => {
|
|
6617
6434
|
const internal_controller = new AbortController();
|
|
6618
6435
|
const cancel_on_turn_abort = () => {
|
|
@@ -6621,7 +6438,7 @@ function build_native_invoke(cfg) {
|
|
|
6621
6438
|
if (turn_abort.aborted) internal_controller.abort(turn_abort.reason);
|
|
6622
6439
|
else turn_abort.addEventListener("abort", cancel_on_turn_abort, { once: true });
|
|
6623
6440
|
const dispatch_chunk = async (chunk) => {
|
|
6624
|
-
|
|
6441
|
+
first_chunk_at ??= Date.now();
|
|
6625
6442
|
try {
|
|
6626
6443
|
await cfg.dispatcher.dispatch(chunk);
|
|
6627
6444
|
} catch (err) {
|
|
@@ -6652,7 +6469,7 @@ function build_native_invoke(cfg) {
|
|
|
6652
6469
|
turn_abort.removeEventListener("abort", cancel_on_turn_abort);
|
|
6653
6470
|
}
|
|
6654
6471
|
};
|
|
6655
|
-
return await retry_turn(call_once, args, () =>
|
|
6472
|
+
return await retry_turn(call_once, args, () => first_chunk_at, cfg.retry_policy, classify, cfg.turn_timeout_ms, cfg.trajectory);
|
|
6656
6473
|
};
|
|
6657
6474
|
}
|
|
6658
6475
|
/**
|
|
@@ -6719,7 +6536,7 @@ function resolve_turn_config(opts, engine) {
|
|
|
6719
6536
|
const effort = opts.effort ?? engine.default_effort;
|
|
6720
6537
|
const retry_policy = opts.retry ?? engine.default_retry;
|
|
6721
6538
|
const turn_timeout_ms = opts.turn_timeout_ms ?? engine.default_turn_timeout_ms;
|
|
6722
|
-
if (turn_timeout_ms !== void 0 && turn_timeout_ms <= 0) throw new engine_config_error(
|
|
6539
|
+
if (turn_timeout_ms !== void 0 && turn_timeout_ms <= 0) throw new engine_config_error(`turn_timeout_ms must be > 0, got ${String(turn_timeout_ms)}`);
|
|
6723
6540
|
return {
|
|
6724
6541
|
effort,
|
|
6725
6542
|
retry_policy,
|
|
@@ -6733,7 +6550,7 @@ function resolve_turn_config(opts, engine) {
|
|
|
6733
6550
|
*/
|
|
6734
6551
|
function resolve_salvage_budget(opts, engine) {
|
|
6735
6552
|
const tool_call_repair_attempts = opts.tool_call_repair_attempts ?? engine.default_tool_call_repair_attempts ?? 0;
|
|
6736
|
-
if (tool_call_repair_attempts < 0) throw new engine_config_error(
|
|
6553
|
+
if (tool_call_repair_attempts < 0) throw new engine_config_error(`tool_call_repair_attempts must be >= 0, got ${String(tool_call_repair_attempts)}`);
|
|
6737
6554
|
return tool_call_repair_attempts > 0 ? { remaining: tool_call_repair_attempts } : void 0;
|
|
6738
6555
|
}
|
|
6739
6556
|
/**
|
|
@@ -6746,7 +6563,7 @@ function resolve_loop_limits(opts, engine) {
|
|
|
6746
6563
|
const schema_repair_attempts = opts.schema_repair_attempts ?? engine.default_schema_repair_attempts ?? 1;
|
|
6747
6564
|
const salvage_budget = resolve_salvage_budget(opts, engine);
|
|
6748
6565
|
const max_tool_calls_per_step = opts.max_tool_calls_per_step ?? engine.default_max_tool_calls_per_step;
|
|
6749
|
-
if (max_tool_calls_per_step !== void 0 && max_tool_calls_per_step < 1) throw new engine_config_error(
|
|
6566
|
+
if (max_tool_calls_per_step !== void 0 && max_tool_calls_per_step < 1) throw new engine_config_error(`max_tool_calls_per_step must be >= 1, got ${String(max_tool_calls_per_step)}`);
|
|
6750
6567
|
return {
|
|
6751
6568
|
max_steps,
|
|
6752
6569
|
tool_error_policy,
|
|
@@ -6768,7 +6585,7 @@ async function build_ai_sdk_transport(cfg) {
|
|
|
6768
6585
|
const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
|
|
6769
6586
|
let ai_sdk_invoke_mod;
|
|
6770
6587
|
try {
|
|
6771
|
-
ai_sdk_invoke_mod = await import("./invoke-
|
|
6588
|
+
ai_sdk_invoke_mod = await import("./invoke-CH_eJZeS.js");
|
|
6772
6589
|
} catch (err) {
|
|
6773
6590
|
throw missing_peer_error("ai", err);
|
|
6774
6591
|
}
|
|
@@ -6787,7 +6604,8 @@ async function build_ai_sdk_transport(cfg) {
|
|
|
6787
6604
|
telemetry: cfg.telemetry
|
|
6788
6605
|
}),
|
|
6789
6606
|
retry_policy: cfg.retry_policy,
|
|
6790
|
-
turn_timeout_ms: cfg.turn_timeout_ms
|
|
6607
|
+
turn_timeout_ms: cfg.turn_timeout_ms,
|
|
6608
|
+
trajectory: cfg.trajectory
|
|
6791
6609
|
});
|
|
6792
6610
|
}
|
|
6793
6611
|
/**
|
|
@@ -6975,7 +6793,7 @@ async function generate(opts_in, engine) {
|
|
|
6975
6793
|
if (opts_in.abort?.aborted === true) throw new aborted_error("aborted", { reason: opts_in.abort.reason });
|
|
6976
6794
|
const { opts, target } = resolve_target(opts_in, engine);
|
|
6977
6795
|
const adapter = engine.adapters.get(target.provider);
|
|
6978
|
-
if (adapter === void 0) throw new provider_not_configured_error(target.provider);
|
|
6796
|
+
if (adapter === void 0) throw new provider_not_configured_error(target.provider, [...engine.adapters.keys()]);
|
|
6979
6797
|
if (adapter.kind === "external") return adapter.generate(opts, target);
|
|
6980
6798
|
const trajectory = with_timestamps(opts.trajectory);
|
|
6981
6799
|
const on_chunk_provided = opts.on_chunk !== void 0;
|
|
@@ -7001,6 +6819,7 @@ async function generate(opts_in, engine) {
|
|
|
7001
6819
|
model_id: target.model_id,
|
|
7002
6820
|
retry_policy: turn.retry_policy,
|
|
7003
6821
|
turn_timeout_ms: turn.turn_timeout_ms,
|
|
6822
|
+
trajectory,
|
|
7004
6823
|
dispatcher,
|
|
7005
6824
|
effort: turn.effort,
|
|
7006
6825
|
schema: opts.schema,
|
|
@@ -7046,7 +6865,9 @@ async function generate(opts_in, engine) {
|
|
|
7046
6865
|
end_generate_span(trajectory, generate_span, {
|
|
7047
6866
|
usage: result.usage,
|
|
7048
6867
|
finish_reason: outcome.finish_reason,
|
|
7049
|
-
model_resolved: result.model_resolved
|
|
6868
|
+
model_resolved: result.model_resolved,
|
|
6869
|
+
step_count: result.steps.length,
|
|
6870
|
+
tool_call_count: result.tool_calls.length
|
|
7050
6871
|
});
|
|
7051
6872
|
return result;
|
|
7052
6873
|
} catch (err) {
|
|
@@ -7207,9 +7028,9 @@ function resolve_default_scalars(config) {
|
|
|
7207
7028
|
function validate_numeric_defaults(defaults) {
|
|
7208
7029
|
if (defaults === void 0) return;
|
|
7209
7030
|
const { tool_call_repair_attempts, max_tool_calls_per_step, turn_timeout_ms } = defaults;
|
|
7210
|
-
if (tool_call_repair_attempts !== void 0 && tool_call_repair_attempts < 0) throw new engine_config_error(
|
|
7211
|
-
if (max_tool_calls_per_step !== void 0 && max_tool_calls_per_step < 1) throw new engine_config_error(
|
|
7212
|
-
if (turn_timeout_ms !== void 0 && turn_timeout_ms <= 0) throw new engine_config_error(
|
|
7031
|
+
if (tool_call_repair_attempts !== void 0 && tool_call_repair_attempts < 0) throw new engine_config_error(`defaults.tool_call_repair_attempts must be >= 0, got ${String(tool_call_repair_attempts)}`);
|
|
7032
|
+
if (max_tool_calls_per_step !== void 0 && max_tool_calls_per_step < 1) throw new engine_config_error(`defaults.max_tool_calls_per_step must be >= 1, got ${String(max_tool_calls_per_step)}`);
|
|
7033
|
+
if (turn_timeout_ms !== void 0 && turn_timeout_ms <= 0) throw new engine_config_error(`defaults.turn_timeout_ms must be > 0, got ${String(turn_timeout_ms)}`);
|
|
7213
7034
|
}
|
|
7214
7035
|
/**
|
|
7215
7036
|
* Copy `value` onto `target[key]` only when it is defined, so an absent optional
|
|
@@ -7223,8 +7044,8 @@ function assign_if_present(target, key, value) {
|
|
|
7223
7044
|
* plus whichever optional `defaults.*` knobs were supplied.
|
|
7224
7045
|
*
|
|
7225
7046
|
* The optional defaults form a fixed source-to-target table (`defaults.model` ->
|
|
7226
|
-
* `default_model`, and so on)
|
|
7227
|
-
*
|
|
7047
|
+
* `default_model`, and so on), and each is folded in only when present, so an
|
|
7048
|
+
* omitted knob leaves its key off the object rather than present-as-undefined.
|
|
7228
7049
|
*/
|
|
7229
7050
|
function build_internals(base, defaults) {
|
|
7230
7051
|
const internals = {
|
|
@@ -7312,6 +7133,6 @@ function create_engine(config) {
|
|
|
7312
7133
|
};
|
|
7313
7134
|
}
|
|
7314
7135
|
//#endregion
|
|
7315
|
-
export {
|
|
7136
|
+
export { create_engine as t };
|
|
7316
7137
|
|
|
7317
|
-
//# sourceMappingURL=engine-
|
|
7138
|
+
//# sourceMappingURL=engine-CdXegeqC.js.map
|