fascicle 0.17.2 → 0.17.4
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 +20 -0
- package/dist/AGENTS.md +9 -8
- package/dist/acp.d.ts +1 -1
- package/dist/acp.js +1 -1
- package/dist/agents.d.ts +1 -1
- package/dist/agents.js +1 -1
- package/dist/{core-Dwir3pkb.js → core-Bfzt55Fl.js} +68 -43
- package/dist/core-Bfzt55Fl.js.map +1 -0
- package/dist/diagram_bin.js +34 -6
- package/dist/diagram_bin.js.map +1 -1
- package/dist/{engine-DPPqAVdZ.js → engine-CbVdzplI.js} +131 -50
- package/dist/engine-CbVdzplI.js.map +1 -0
- package/dist/index.d.ts +7 -1
- package/dist/index.js +3 -3
- package/dist/{invoke-DF7YLovL.js → invoke-x5SNh9DV.js} +3 -3
- package/dist/{invoke-DF7YLovL.js.map → invoke-x5SNh9DV.js.map} +1 -1
- package/dist/{leading_system-DJvuhESU.js → leading_system-C23TJ9OQ.js} +52 -22
- package/dist/leading_system-C23TJ9OQ.js.map +1 -0
- package/dist/mcp.d.ts +1 -1
- package/dist/mcp.js +1 -1
- package/dist/stdio.js +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +3 -3
- package/dist/{types-C7pO0jLq.d.ts → types-C23S11Kk.d.ts} +26 -1
- package/dist/viewer.js +1 -1
- package/package.json +1 -1
- package/dist/core-Dwir3pkb.js.map +0 -1
- package/dist/engine-DPPqAVdZ.js.map +0 -1
- package/dist/leading_system-DJvuhESU.js.map +0 -1
package/dist/diagram_bin.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { A as is_step, k as describe } from "./core-Bfzt55Fl.js";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
4
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
@@ -10,13 +10,15 @@ import { parseArgs } from "node:util";
|
|
|
10
10
|
* fascicle-diagram <module> print the diagram of the module's `flow`
|
|
11
11
|
* fascicle-diagram <module> --export <name> read another export instead
|
|
12
12
|
*
|
|
13
|
-
* Flags: --export <name> --width <n> --prefix <text> --replays --internals --help
|
|
13
|
+
* Flags: --export <name> --width <n> --prefix <text> --replays --internals --outline --help
|
|
14
14
|
*
|
|
15
15
|
* The export is a Step, or a function that builds one with no arguments,
|
|
16
16
|
* which is where an app wires its flow to stub dependencies so that drawing
|
|
17
17
|
* it needs no credentials. The diagram is `describe.diagram` of that Step, so
|
|
18
18
|
* it is what the code builds today and can be compared with the header in
|
|
19
|
-
* `flow.ts` at a glance.
|
|
19
|
+
* `flow.ts` at a glance. `--outline` prints the plain `describe` outline
|
|
20
|
+
* instead, with each node's kind, id, and config, which is the view to read
|
|
21
|
+
* when the diagram hides something.
|
|
20
22
|
*
|
|
21
23
|
* This file holds every decision the command makes and touches nothing of
|
|
22
24
|
* the process: output and module loading arrive through `DiagramCliIo`.
|
|
@@ -38,8 +40,15 @@ Options:
|
|
|
38
40
|
--prefix <text> start every line with text, for example ' * '
|
|
39
41
|
--replays mark the steps a resumed run would pay for again
|
|
40
42
|
--internals draw a named composite's internal steps, not only its parts
|
|
43
|
+
--outline print describe()'s plain outline, with ids and config,
|
|
44
|
+
instead of the diagram (no --width, --replays, or --internals)
|
|
41
45
|
--help show this message
|
|
42
46
|
`;
|
|
47
|
+
const DIAGRAM_ONLY = [
|
|
48
|
+
"width",
|
|
49
|
+
"replays",
|
|
50
|
+
"internals"
|
|
51
|
+
];
|
|
43
52
|
/**
|
|
44
53
|
* Run the CLI over `argv` and resolve to its exit code: 0 once the diagram is
|
|
45
54
|
* printed, 1 when the module cannot be loaded or holds no Step, and 2 for a
|
|
@@ -56,7 +65,7 @@ async function run_diagram_cli(argv, io) {
|
|
|
56
65
|
io.err(`fascicle-diagram: ${parsed.message}\n\n${HELP}`);
|
|
57
66
|
return 2;
|
|
58
67
|
}
|
|
59
|
-
const { module, export_name, options } = parsed.args;
|
|
68
|
+
const { module, export_name, outline, options } = parsed.args;
|
|
60
69
|
const path = resolve(module);
|
|
61
70
|
let exports;
|
|
62
71
|
try {
|
|
@@ -82,13 +91,22 @@ async function run_diagram_cli(argv, io) {
|
|
|
82
91
|
io.err(`fascicle-diagram: ${export_name} in ${module} is not a Step: export the flow itself, or a function that builds it with no arguments\n`);
|
|
83
92
|
return 1;
|
|
84
93
|
}
|
|
85
|
-
|
|
94
|
+
const text = outline ? outline_of(flow, options.prefix) : describe.diagram(flow, options);
|
|
95
|
+
io.out(`${text}\n`);
|
|
86
96
|
return 0;
|
|
87
97
|
}
|
|
88
98
|
/**
|
|
99
|
+
* Render `describe`'s plain outline of `flow`, starting every line with
|
|
100
|
+
* `prefix` the way the diagram does.
|
|
101
|
+
*/
|
|
102
|
+
function outline_of(flow, prefix = "") {
|
|
103
|
+
return describe(flow).split("\n").map((line) => `${prefix}${line}`).join("\n");
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
89
106
|
* Parse argv into a run request, a help request, or a usage error. Unknown
|
|
90
107
|
* flags and a missing module are usage errors, and so is a width that is not
|
|
91
|
-
* a positive integer.
|
|
108
|
+
* a positive integer. `--outline` with an option that only the diagram reads
|
|
109
|
+
* is one too, since the outline would drop it without a word.
|
|
92
110
|
*/
|
|
93
111
|
function parse(argv) {
|
|
94
112
|
let parsed;
|
|
@@ -111,6 +129,14 @@ function parse(argv) {
|
|
|
111
129
|
kind: "usage",
|
|
112
130
|
message: `expected one <module>, got ${positionals.join(" ")}`
|
|
113
131
|
};
|
|
132
|
+
const outline = values.outline === true;
|
|
133
|
+
if (outline) {
|
|
134
|
+
const dropped = DIAGRAM_ONLY.filter((flag) => values[flag] !== void 0);
|
|
135
|
+
if (dropped.length > 0) return {
|
|
136
|
+
kind: "usage",
|
|
137
|
+
message: `--outline can't take ${dropped.map((flag) => `--${flag}`).join(", ")}, which only the diagram reads`
|
|
138
|
+
};
|
|
139
|
+
}
|
|
114
140
|
const options = {};
|
|
115
141
|
if (values.width !== void 0) {
|
|
116
142
|
const width = Number(values.width);
|
|
@@ -128,6 +154,7 @@ function parse(argv) {
|
|
|
128
154
|
args: {
|
|
129
155
|
module,
|
|
130
156
|
export_name: values.export ?? "flow",
|
|
157
|
+
outline,
|
|
131
158
|
options
|
|
132
159
|
}
|
|
133
160
|
};
|
|
@@ -146,6 +173,7 @@ function parse_flags(argv) {
|
|
|
146
173
|
prefix: { type: "string" },
|
|
147
174
|
replays: { type: "boolean" },
|
|
148
175
|
internals: { type: "boolean" },
|
|
176
|
+
outline: { type: "boolean" },
|
|
149
177
|
help: { type: "boolean" }
|
|
150
178
|
}
|
|
151
179
|
});
|
package/dist/diagram_bin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"diagram_bin.js","names":[],"sources":["../src/diagram_cli.ts","../src/diagram_bin.ts"],"sourcesContent":["/**\n * fascicle-diagram CLI.\n *\n * fascicle-diagram <module> print the diagram of the module's `flow`\n * fascicle-diagram <module> --export <name> read another export instead\n *\n * Flags: --export <name> --width <n> --prefix <text> --replays --internals --help\n *\n * The export is a Step, or a function that builds one with no arguments,\n * which is where an app wires its flow to stub dependencies so that drawing\n * it needs no credentials. The diagram is `describe.diagram` of that Step, so\n * it is what the code builds today and can be compared with the header in\n * `flow.ts` at a glance.\n *\n * This file holds every decision the command makes and touches nothing of\n * the process: output and module loading arrive through `DiagramCliIo`.\n * diagram_bin.ts binds that io to the real streams and to the project's\n * `tsx`, and is the entry that the package's bin shim runs.\n */\n\nimport { resolve } from 'node:path'\nimport { parseArgs } from 'node:util'\nimport { describe, is_step } from '#core'\nimport type { DiagramOptions } from '#core'\n\nexport type DiagramCliIo = {\n readonly out: (text: string) => void\n readonly err: (text: string) => void\n /** Import the module at an absolute path and return its exports. */\n readonly load: (path: string) => Promise<Readonly<Record<string, unknown>>>\n}\n\ntype CliArgs = {\n readonly module: string\n readonly export_name: string\n readonly options: DiagramOptions\n}\n\ntype Parsed =\n | { readonly kind: 'help' }\n | { readonly kind: 'usage'; readonly message: string }\n | { readonly kind: 'run'; readonly args: CliArgs }\n\nconst HELP = `\\\nfascicle-diagram prints a flow as an annotated box-drawing tree.\n\nUsage:\n fascicle-diagram <module> draw the module's \\`flow\\` export\n fascicle-diagram <module> --export <name> draw another export instead\n\nThe export is a Step, or a function that builds one with no arguments.\n\nOptions:\n --export <name> the export to draw (default flow)\n --width <n> wrap descriptions so no line runs wider than n\n --prefix <text> start every line with text, for example ' * '\n --replays mark the steps a resumed run would pay for again\n --internals draw a named composite's internal steps, not only its parts\n --help show this message\n`\n\n\n/**\n * Run the CLI over `argv` and resolve to its exit code: 0 once the diagram is\n * printed, 1 when the module cannot be loaded or holds no Step, and 2 for a\n * usage error. Output and loading go through `io`, so the caller owns the\n * process.\n */\nexport async function run_diagram_cli(argv: readonly string[], io: DiagramCliIo): Promise<number> {\n const parsed = parse(argv)\n if (parsed.kind === 'help') {\n io.out(HELP)\n return 0\n }\n if (parsed.kind === 'usage') {\n io.err(`fascicle-diagram: ${parsed.message}\\n\\n${HELP}`)\n return 2\n }\n const { module, export_name, options } = parsed.args\n const path = resolve(module)\n let exports: Readonly<Record<string, unknown>>\n try {\n exports = await io.load(path)\n } catch (err) {\n io.err(`fascicle-diagram: could not load ${module}: ${message_of(err)}\\n`)\n return 1\n }\n if (!Object.hasOwn(exports, export_name)) {\n const names = Object.keys(exports).join(', ') || 'nothing'\n io.err(\n `fascicle-diagram: ${module} has no export named ${export_name}: pass --export <name> (it exports ${names})\\n`,\n )\n return 1\n }\n let flow: unknown\n try {\n flow = await build(exports[export_name])\n } catch (err) {\n io.err(\n `fascicle-diagram: calling ${export_name}() in ${module} threw: ${message_of(err)}\\n` +\n 'Export a function that builds the flow with no arguments, wired to stub dependencies.\\n',\n )\n return 1\n }\n if (!is_step(flow)) {\n io.err(\n `fascicle-diagram: ${export_name} in ${module} is not a Step: export the flow itself, or a function that builds it with no arguments\\n`,\n )\n return 1\n }\n io.out(`${describe.diagram(flow, options)}\\n`)\n return 0\n}\n\n/**\n * Parse argv into a run request, a help request, or a usage error. Unknown\n * flags and a missing module are usage errors, and so is a width that is not\n * a positive integer.\n */\nfunction parse(argv: readonly string[]): Parsed {\n let parsed: ReturnType<typeof parse_flags>\n try {\n parsed = parse_flags(argv)\n } catch (err) {\n return { kind: 'usage', message: message_of(err) }\n }\n const { values, positionals } = parsed\n if (values.help === true) return { kind: 'help' }\n const [module, ...extra] = positionals\n if (module === undefined) return { kind: 'usage', message: 'a <module> to draw is required' }\n if (extra.length > 0) return { kind: 'usage', message: `expected one <module>, got ${positionals.join(' ')}` }\n const options: { width?: number; prefix?: string; replays?: boolean; internals?: boolean } = {}\n if (values.width !== undefined) {\n const width = Number(values.width)\n if (!Number.isInteger(width) || width <= 0) {\n return { kind: 'usage', message: `invalid --width '${values.width}': expected a positive integer` }\n }\n options.width = width\n }\n if (values.prefix !== undefined) options.prefix = values.prefix\n if (values.replays === true) options.replays = true\n if (values.internals === true) options.internals = true\n return { kind: 'run', args: { module, export_name: values.export ?? 'flow', options } }\n}\n\n/**\n * Read the CLI's flags with `node:util`'s strict parser, which throws on a\n * flag it does not know.\n */\nfunction parse_flags(argv: readonly string[]) {\n return parseArgs({\n args: [...argv],\n allowPositionals: true,\n options: {\n export: { type: 'string' },\n width: { type: 'string' },\n prefix: { type: 'string' },\n replays: { type: 'boolean' },\n internals: { type: 'boolean' },\n help: { type: 'boolean' },\n },\n })\n}\n\n/**\n * The Step an export stands for: the export itself, or what calling it with\n * no arguments returns (awaited, so an async builder works too).\n */\nasync function build(value: unknown): Promise<unknown> {\n return typeof value === 'function' ? await value() : value\n}\n\n/**\n * Render a thrown value as its message.\n */\nfunction message_of(err: unknown): string {\n return err instanceof Error ? err.message : String(err)\n}\n","/**\n * The process side of the fascicle-diagram bin: the real streams, and module\n * loading through the project's own `tsx`.\n *\n * Every decision the command makes lives in diagram_cli.ts, which is unit\n * tested through an injected io. This file only binds that io to the process,\n * so it is exercised by spawning it (__tests__/diagram_bin.test.ts) and kept\n * out of mutation testing, which cannot see a spawned child's coverage.\n *\n * A TypeScript module loads through `tsx` because Node's built-in type\n * stripping cannot follow the `./flow.js` specifiers that point at `.ts`\n * files under NodeNext resolution. Anything else, or a project without\n * `tsx`, goes through a plain `import()`.\n */\n\nimport { createRequire } from 'node:module'\nimport { resolve } from 'node:path'\nimport { fileURLToPath, pathToFileURL } from 'node:url'\nimport { run_diagram_cli } from './diagram_cli.js'\n\nconst TS_MODULE = /\\.[cm]?tsx?$/\n\n/**\n * Run the fascicle-diagram command over `argv` against the real process, and\n * leave its exit code on `process.exitCode`. A reader that closes the pipe\n * early (`| head`) ends the output quietly instead of crashing the command on\n * an unhandled EPIPE.\n */\nexport async function run_diagram_bin(argv: readonly string[]): Promise<void> {\n process.stdout.once('error', (err: NodeJS.ErrnoException) => {\n if (err.code !== 'EPIPE') throw err\n })\n process.exitCode = await run_diagram_cli(argv, {\n out: (text) => process.stdout.write(text),\n err: (text) => process.stderr.write(text),\n load: load_module,\n })\n}\n\n/**\n * Import the module at `path`. A TypeScript module goes through the `tsx`\n * that the project installs, looked up from the module's own directory and\n * then from the working directory. Everything else is a plain dynamic import.\n */\nasync function load_module(path: string): Promise<Readonly<Record<string, unknown>>> {\n const url = pathToFileURL(path).href\n if (!TS_MODULE.test(path)) return as_record(await import(url))\n const tsx = find_tsx([path, resolve('package.json')])\n if (tsx === undefined) return import_without_tsx(url)\n const api: unknown = await import(pathToFileURL(tsx).href)\n const ts_import = as_record(api)['tsImport']\n if (typeof ts_import !== 'function') return as_record(await import(url))\n return as_record(await ts_import(url, import.meta.url))\n}\n\n/**\n * Import a TypeScript module with Node's own type stripping, which handles\n * only erasable syntax and specifiers that name real files. A failure says\n * that tsx is the fix, since this path is taken only when none was found.\n */\nasync function import_without_tsx(url: string): Promise<Readonly<Record<string, unknown>>> {\n try {\n return as_record(await import(url))\n } catch (err) {\n const reason = err instanceof Error ? err.message : String(err)\n throw new Error(`${reason} (no tsx was found to load TypeScript: install it with pnpm add -D tsx)`, {\n cause: err,\n })\n }\n}\n\n/**\n * Resolve tsx's programmatic API from the first of `origins` that can see it,\n * or `undefined` when none can.\n */\nfunction find_tsx(origins: readonly string[]): string | undefined {\n for (const origin of origins) {\n try {\n return createRequire(origin).resolve('tsx/esm/api')\n } catch {\n // Not installed where this origin looks; try the next one.\n }\n }\n return undefined\n}\n\n/**\n * View a loaded module namespace as a record of its exports.\n */\nfunction as_record(value: unknown): Readonly<Record<string, unknown>> {\n return typeof value === 'object' && value !== null ? { ...value } : {}\n}\n\n// Auto-run only when this file is the entry script (`tsx src/diagram_bin.ts`),\n// never when the bundle that contains it is imported. The path comparison is\n// the same guard the viewer CLI uses.\nconst invoked_directly =\n process.argv[1] !== undefined && process.argv[1] === fileURLToPath(import.meta.url)\n\nif (invoked_directly) void run_diagram_bin(process.argv.slice(2))\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,MAAM,OAAO;;;;;;;;;;;;;;;;;;;;;;;AAyBb,eAAsB,gBAAgB,MAAyB,IAAmC;CAChG,MAAM,SAAS,MAAM,IAAI;CACzB,IAAI,OAAO,SAAS,QAAQ;EAC1B,GAAG,IAAI,IAAI;EACX,OAAO;CACT;CACA,IAAI,OAAO,SAAS,SAAS;EAC3B,GAAG,IAAI,qBAAqB,OAAO,QAAQ,MAAM,MAAM;EACvD,OAAO;CACT;CACA,MAAM,EAAE,QAAQ,aAAa,YAAY,OAAO;CAChD,MAAM,OAAO,QAAQ,MAAM;CAC3B,IAAI;CACJ,IAAI;EACF,UAAU,MAAM,GAAG,KAAK,IAAI;CAC9B,SAAS,KAAK;EACZ,GAAG,IAAI,oCAAoC,OAAO,IAAI,WAAW,GAAG,EAAE,GAAG;EACzE,OAAO;CACT;CACA,IAAI,CAAC,OAAO,OAAO,SAAS,WAAW,GAAG;EACxC,MAAM,QAAQ,OAAO,KAAK,OAAO,CAAC,CAAC,KAAK,IAAI,KAAK;EACjD,GAAG,IACD,qBAAqB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C;CACjC,IAAI,OAAO,cAAc,YAAY,OAAO,UAAU,MAAM,OAAO,IAAI;CACvE,OAAO,UAAU,MAAM,UAAU,KAAK,YAAY,GAAG,CAAC;AACxD;;;;;;AAOA,eAAe,mBAAmB,KAAyD;CACzF,IAAI;EACF,OAAO,UAAU,MAAM,OAAO,IAAI;CACpC,SAAS,KAAK;EACZ,MAAM,SAAS,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;EAC9D,MAAM,IAAI,MAAM,GAAG,OAAO,0EAA0E,EAClG,OAAO,IACT,CAAC;CACH;AACF;;;;;AAMA,SAAS,SAAS,SAAgD;CAChE,KAAK,MAAM,UAAU,SACnB,IAAI;EACF,OAAO,cAAc,MAAM,CAAC,CAAC,QAAQ,aAAa;CACpD,QAAQ,CAER;AAGJ;;;;AAKA,SAAS,UAAU,OAAmD;CACpE,OAAO,OAAO,UAAU,YAAY,UAAU,OAAO,EAAE,GAAG,MAAM,IAAI,CAAC;AACvE;AAQA,IAFE,QAAQ,KAAK,OAAO,KAAA,KAAa,QAAQ,KAAK,OAAO,cAAc,YAAY,GAAG,GAE9D,gBAAqB,QAAQ,KAAK,MAAM,CAAC,CAAC"}
|
|
1
|
+
{"version":3,"file":"diagram_bin.js","names":[],"sources":["../src/diagram_cli.ts","../src/diagram_bin.ts"],"sourcesContent":["/**\n * fascicle-diagram CLI.\n *\n * fascicle-diagram <module> print the diagram of the module's `flow`\n * fascicle-diagram <module> --export <name> read another export instead\n *\n * Flags: --export <name> --width <n> --prefix <text> --replays --internals --outline --help\n *\n * The export is a Step, or a function that builds one with no arguments,\n * which is where an app wires its flow to stub dependencies so that drawing\n * it needs no credentials. The diagram is `describe.diagram` of that Step, so\n * it is what the code builds today and can be compared with the header in\n * `flow.ts` at a glance. `--outline` prints the plain `describe` outline\n * instead, with each node's kind, id, and config, which is the view to read\n * when the diagram hides something.\n *\n * This file holds every decision the command makes and touches nothing of\n * the process: output and module loading arrive through `DiagramCliIo`.\n * diagram_bin.ts binds that io to the real streams and to the project's\n * `tsx`, and is the entry that the package's bin shim runs.\n */\n\nimport { resolve } from 'node:path'\nimport { parseArgs } from 'node:util'\nimport { describe, is_step } from '#core'\nimport type { AnyStep, DiagramOptions } from '#core'\n\nexport type DiagramCliIo = {\n readonly out: (text: string) => void\n readonly err: (text: string) => void\n /** Import the module at an absolute path and return its exports. */\n readonly load: (path: string) => Promise<Readonly<Record<string, unknown>>>\n}\n\ntype CliArgs = {\n readonly module: string\n readonly export_name: string\n readonly outline: boolean\n readonly options: DiagramOptions\n}\n\ntype Parsed =\n | { readonly kind: 'help' }\n | { readonly kind: 'usage'; readonly message: string }\n | { readonly kind: 'run'; readonly args: CliArgs }\n\nconst HELP = `\\\nfascicle-diagram prints a flow as an annotated box-drawing tree.\n\nUsage:\n fascicle-diagram <module> draw the module's \\`flow\\` export\n fascicle-diagram <module> --export <name> draw another export instead\n\nThe export is a Step, or a function that builds one with no arguments.\n\nOptions:\n --export <name> the export to draw (default flow)\n --width <n> wrap descriptions so no line runs wider than n\n --prefix <text> start every line with text, for example ' * '\n --replays mark the steps a resumed run would pay for again\n --internals draw a named composite's internal steps, not only its parts\n --outline print describe()'s plain outline, with ids and config,\n instead of the diagram (no --width, --replays, or --internals)\n --help show this message\n`\n\n// The flags that only the diagram reads, which `--outline` refuses.\nconst DIAGRAM_ONLY = ['width', 'replays', 'internals'] as const\n\n/**\n * Run the CLI over `argv` and resolve to its exit code: 0 once the diagram is\n * printed, 1 when the module cannot be loaded or holds no Step, and 2 for a\n * usage error. Output and loading go through `io`, so the caller owns the\n * process.\n */\nexport async function run_diagram_cli(argv: readonly string[], io: DiagramCliIo): Promise<number> {\n const parsed = parse(argv)\n if (parsed.kind === 'help') {\n io.out(HELP)\n return 0\n }\n if (parsed.kind === 'usage') {\n io.err(`fascicle-diagram: ${parsed.message}\\n\\n${HELP}`)\n return 2\n }\n const { module, export_name, outline, options } = parsed.args\n const path = resolve(module)\n let exports: Readonly<Record<string, unknown>>\n try {\n exports = await io.load(path)\n } catch (err) {\n io.err(`fascicle-diagram: could not load ${module}: ${message_of(err)}\\n`)\n return 1\n }\n if (!Object.hasOwn(exports, export_name)) {\n const names = Object.keys(exports).join(', ') || 'nothing'\n io.err(\n `fascicle-diagram: ${module} has no export named ${export_name}: pass --export <name> (it exports ${names})\\n`,\n )\n return 1\n }\n let flow: unknown\n try {\n flow = await build(exports[export_name])\n } catch (err) {\n io.err(\n `fascicle-diagram: calling ${export_name}() in ${module} threw: ${message_of(err)}\\n` +\n 'Export a function that builds the flow with no arguments, wired to stub dependencies.\\n',\n )\n return 1\n }\n if (!is_step(flow)) {\n io.err(\n `fascicle-diagram: ${export_name} in ${module} is not a Step: export the flow itself, or a function that builds it with no arguments\\n`,\n )\n return 1\n }\n const text = outline ? outline_of(flow, options.prefix) : describe.diagram(flow, options)\n io.out(`${text}\\n`)\n return 0\n}\n\n/**\n * Render `describe`'s plain outline of `flow`, starting every line with\n * `prefix` the way the diagram does.\n */\nfunction outline_of(flow: AnyStep, prefix = ''): string {\n return describe(flow)\n .split('\\n')\n .map((line) => `${prefix}${line}`)\n .join('\\n')\n}\n\n/**\n * Parse argv into a run request, a help request, or a usage error. Unknown\n * flags and a missing module are usage errors, and so is a width that is not\n * a positive integer. `--outline` with an option that only the diagram reads\n * is one too, since the outline would drop it without a word.\n */\nfunction parse(argv: readonly string[]): Parsed {\n let parsed: ReturnType<typeof parse_flags>\n try {\n parsed = parse_flags(argv)\n } catch (err) {\n return { kind: 'usage', message: message_of(err) }\n }\n const { values, positionals } = parsed\n if (values.help === true) return { kind: 'help' }\n const [module, ...extra] = positionals\n if (module === undefined) return { kind: 'usage', message: 'a <module> to draw is required' }\n if (extra.length > 0) return { kind: 'usage', message: `expected one <module>, got ${positionals.join(' ')}` }\n const outline = values.outline === true\n if (outline) {\n const dropped = DIAGRAM_ONLY.filter((flag) => values[flag] !== undefined)\n if (dropped.length > 0) {\n const flags = dropped.map((flag) => `--${flag}`).join(', ')\n return { kind: 'usage', message: `--outline can't take ${flags}, which only the diagram reads` }\n }\n }\n const options: { width?: number; prefix?: string; replays?: boolean; internals?: boolean } = {}\n if (values.width !== undefined) {\n const width = Number(values.width)\n if (!Number.isInteger(width) || width <= 0) {\n return { kind: 'usage', message: `invalid --width '${values.width}': expected a positive integer` }\n }\n options.width = width\n }\n if (values.prefix !== undefined) options.prefix = values.prefix\n if (values.replays === true) options.replays = true\n if (values.internals === true) options.internals = true\n return { kind: 'run', args: { module, export_name: values.export ?? 'flow', outline, options } }\n}\n\n/**\n * Read the CLI's flags with `node:util`'s strict parser, which throws on a\n * flag it does not know.\n */\nfunction parse_flags(argv: readonly string[]) {\n return parseArgs({\n args: [...argv],\n allowPositionals: true,\n options: {\n export: { type: 'string' },\n width: { type: 'string' },\n prefix: { type: 'string' },\n replays: { type: 'boolean' },\n internals: { type: 'boolean' },\n outline: { type: 'boolean' },\n help: { type: 'boolean' },\n },\n })\n}\n\n/**\n * The Step an export stands for: the export itself, or what calling it with\n * no arguments returns (awaited, so an async builder works too).\n */\nasync function build(value: unknown): Promise<unknown> {\n return typeof value === 'function' ? await value() : value\n}\n\n/**\n * Render a thrown value as its message.\n */\nfunction message_of(err: unknown): string {\n return err instanceof Error ? err.message : String(err)\n}\n","/**\n * The process side of the fascicle-diagram bin: the real streams, and module\n * loading through the project's own `tsx`.\n *\n * Every decision the command makes lives in diagram_cli.ts, which is unit\n * tested through an injected io. This file only binds that io to the process,\n * so it is exercised by spawning it (__tests__/diagram_bin.test.ts) and kept\n * out of mutation testing, which cannot see a spawned child's coverage.\n *\n * A TypeScript module loads through `tsx` because Node's built-in type\n * stripping cannot follow the `./flow.js` specifiers that point at `.ts`\n * files under NodeNext resolution. Anything else, or a project without\n * `tsx`, goes through a plain `import()`.\n */\n\nimport { createRequire } from 'node:module'\nimport { resolve } from 'node:path'\nimport { fileURLToPath, pathToFileURL } from 'node:url'\nimport { run_diagram_cli } from './diagram_cli.js'\n\nconst TS_MODULE = /\\.[cm]?tsx?$/\n\n/**\n * Run the fascicle-diagram command over `argv` against the real process, and\n * leave its exit code on `process.exitCode`. A reader that closes the pipe\n * early (`| head`) ends the output quietly instead of crashing the command on\n * an unhandled EPIPE.\n */\nexport async function run_diagram_bin(argv: readonly string[]): Promise<void> {\n process.stdout.once('error', (err: NodeJS.ErrnoException) => {\n if (err.code !== 'EPIPE') throw err\n })\n process.exitCode = await run_diagram_cli(argv, {\n out: (text) => process.stdout.write(text),\n err: (text) => process.stderr.write(text),\n load: load_module,\n })\n}\n\n/**\n * Import the module at `path`. A TypeScript module goes through the `tsx`\n * that the project installs, looked up from the module's own directory and\n * then from the working directory. Everything else is a plain dynamic import.\n */\nasync function load_module(path: string): Promise<Readonly<Record<string, unknown>>> {\n const url = pathToFileURL(path).href\n if (!TS_MODULE.test(path)) return as_record(await import(url))\n const tsx = find_tsx([path, resolve('package.json')])\n if (tsx === undefined) return import_without_tsx(url)\n const api: unknown = await import(pathToFileURL(tsx).href)\n const ts_import = as_record(api)['tsImport']\n if (typeof ts_import !== 'function') return as_record(await import(url))\n return as_record(await ts_import(url, import.meta.url))\n}\n\n/**\n * Import a TypeScript module with Node's own type stripping, which handles\n * only erasable syntax and specifiers that name real files. A failure says\n * that tsx is the fix, since this path is taken only when none was found.\n */\nasync function import_without_tsx(url: string): Promise<Readonly<Record<string, unknown>>> {\n try {\n return as_record(await import(url))\n } catch (err) {\n const reason = err instanceof Error ? err.message : String(err)\n throw new Error(`${reason} (no tsx was found to load TypeScript: install it with pnpm add -D tsx)`, {\n cause: err,\n })\n }\n}\n\n/**\n * Resolve tsx's programmatic API from the first of `origins` that can see it,\n * or `undefined` when none can.\n */\nfunction find_tsx(origins: readonly string[]): string | undefined {\n for (const origin of origins) {\n try {\n return createRequire(origin).resolve('tsx/esm/api')\n } catch {\n // Not installed where this origin looks; try the next one.\n }\n }\n return undefined\n}\n\n/**\n * View a loaded module namespace as a record of its exports.\n */\nfunction as_record(value: unknown): Readonly<Record<string, unknown>> {\n return typeof value === 'object' && value !== null ? { ...value } : {}\n}\n\n// Auto-run only when this file is the entry script (`tsx src/diagram_bin.ts`),\n// never when the bundle that contains it is imported. The path comparison is\n// the same guard the viewer CLI uses.\nconst invoked_directly =\n process.argv[1] !== undefined && process.argv[1] === fileURLToPath(import.meta.url)\n\nif (invoked_directly) void run_diagram_bin(process.argv.slice(2))\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CA,MAAM,OAAO;;;;;;;;;;;;;;;;;;;AAqBb,MAAM,eAAe;CAAC;CAAS;CAAW;AAAW;;;;;;;AAQrD,eAAsB,gBAAgB,MAAyB,IAAmC;CAChG,MAAM,SAAS,MAAM,IAAI;CACzB,IAAI,OAAO,SAAS,QAAQ;EAC1B,GAAG,IAAI,IAAI;EACX,OAAO;CACT;CACA,IAAI,OAAO,SAAS,SAAS;EAC3B,GAAG,IAAI,qBAAqB,OAAO,QAAQ,MAAM,MAAM;EACvD,OAAO;CACT;CACA,MAAM,EAAE,QAAQ,aAAa,SAAS,YAAY,OAAO;CACzD,MAAM,OAAO,QAAQ,MAAM;CAC3B,IAAI;CACJ,IAAI;EACF,UAAU,MAAM,GAAG,KAAK,IAAI;CAC9B,SAAS,KAAK;EACZ,GAAG,IAAI,oCAAoC,OAAO,IAAI,WAAW,GAAG,EAAE,GAAG;EACzE,OAAO;CACT;CACA,IAAI,CAAC,OAAO,OAAO,SAAS,WAAW,GAAG;EACxC,MAAM,QAAQ,OAAO,KAAK,OAAO,CAAC,CAAC,KAAK,IAAI,KAAK;EACjD,GAAG,IACD,qBAAqB,OAAO,uBAAuB,YAAY,qCAAqC,MAAM,IAC5G;EACA,OAAO;CACT;CACA,IAAI;CACJ,IAAI;EACF,OAAO,MAAM,MAAM,QAAQ,YAAY;CACzC,SAAS,KAAK;EACZ,GAAG,IACD,6BAA6B,YAAY,QAAQ,OAAO,UAAU,WAAW,GAAG,EAAE;CAEpF;EACA,OAAO;CACT;CACA,IAAI,CAAC,QAAQ,IAAI,GAAG;EAClB,GAAG,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,GAAG,OAAO,UAAU,MAAM,OAAO,IAAI;CAC7D,MAAM,MAAM,SAAS,CAAC,MAAM,QAAQ,cAAc,CAAC,CAAC;CACpD,IAAI,QAAQ,KAAA,GAAW,OAAO,mBAAmB,GAAG;CAEpD,MAAM,YAAY,UAAU,MADD,OAAO,cAAc,GAAG,CAAC,CAAC,KACtB,CAAC,CAAC;CACjC,IAAI,OAAO,cAAc,YAAY,OAAO,UAAU,MAAM,OAAO,IAAI;CACvE,OAAO,UAAU,MAAM,UAAU,KAAK,YAAY,GAAG,CAAC;AACxD;;;;;;AAOA,eAAe,mBAAmB,KAAyD;CACzF,IAAI;EACF,OAAO,UAAU,MAAM,OAAO,IAAI;CACpC,SAAS,KAAK;EACZ,MAAM,SAAS,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;EAC9D,MAAM,IAAI,MAAM,GAAG,OAAO,0EAA0E,EAClG,OAAO,IACT,CAAC;CACH;AACF;;;;;AAMA,SAAS,SAAS,SAAgD;CAChE,KAAK,MAAM,UAAU,SACnB,IAAI;EACF,OAAO,cAAc,MAAM,CAAC,CAAC,QAAQ,aAAa;CACpD,QAAQ,CAER;AAGJ;;;;AAKA,SAAS,UAAU,OAAmD;CACpE,OAAO,OAAO,UAAU,YAAY,UAAU,OAAO,EAAE,GAAG,MAAM,IAAI,CAAC;AACvE;AAQA,IAFE,QAAQ,KAAK,OAAO,KAAA,KAAa,QAAQ,KAAK,OAAO,cAAc,YAAY,GAAG,GAE9D,gBAAqB,QAAQ,KAAK,MAAM,CAAC,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { C as validate_schema, D as wait_with_abort, E as compute_backoff, S as format_schema_issues, T as failure_meta, j as aborted_error } from "./core-Bfzt55Fl.js";
|
|
2
2
|
import { n as is_record$1 } from "./records-1HU7mWXf.js";
|
|
3
|
-
import { A as
|
|
3
|
+
import { A as schema_validation_error, C as on_chunk_error, D as provider_not_configured_error, E as provider_error, M as tool_error, N as turn_timeout_error, O as provider_required_error, S as model_required_error, T as provider_capability_error, _ as translate_effort_to_sdk_reasoning, a as invoke_http_turn, b as engine_disposed_error, c as network_failure, d as split_assistant_parts, f as default_normalize_usage, g as resolve_transport, h as normalize_local_usage, i as error_body_message, j as tool_approval_denied_error, k as rate_limit_error, l as parse_tool_call_arguments, m as missing_peer_error, n as consume_framed_stream, p as load_optional_peer, r as create_line_splitter, s as map_messages_by_role, t as split_leading_system_run, u as read_dialect_init, v as claude_cli_error, w as provider_auth_error, x as incomplete_generation_error, y as engine_config_error } from "./leading_system-C23TJ9OQ.js";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { mkdtempSync, writeFileSync } from "node:fs";
|
|
6
6
|
import { tmpdir } from "node:os";
|
|
@@ -414,8 +414,8 @@ function decompose_total_cost(total_cost_usd, usage) {
|
|
|
414
414
|
//#endregion
|
|
415
415
|
//#region src/engine/retry.ts
|
|
416
416
|
const DEFAULT_RETRY = {
|
|
417
|
-
max_attempts:
|
|
418
|
-
initial_delay_ms:
|
|
417
|
+
max_attempts: 4,
|
|
418
|
+
initial_delay_ms: 1e3,
|
|
419
419
|
max_delay_ms: 3e4,
|
|
420
420
|
retry_on: [
|
|
421
421
|
"rate_limit",
|
|
@@ -465,40 +465,60 @@ function to_abort_error(reason) {
|
|
|
465
465
|
return new aborted_error("aborted", { reason });
|
|
466
466
|
}
|
|
467
467
|
/**
|
|
468
|
+
* The message a typed error carries: `lead`, which says what this layer did,
|
|
469
|
+
* then the status when the failure had one, then the provider's own message
|
|
470
|
+
* when it had one, as in `provider_5xx after 4 attempts (status 503): Service
|
|
471
|
+
* Unavailable`. The lead comes first so the message reads the same whether
|
|
472
|
+
* or not the provider said anything.
|
|
473
|
+
*/
|
|
474
|
+
function compose_message(lead, status, message) {
|
|
475
|
+
return `${lead}${status === void 0 ? "" : ` (status ${status})`}${message === void 0 ? "" : `: ${message}`}`;
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
468
478
|
* Build the `rate_limit_error` for a 429 that this layer won't retry again.
|
|
469
479
|
* Prefers the failing attempt's own `Retry-After`, falling back to the most
|
|
470
|
-
* recent one a prior attempt carried (`last_rate_limit_after`), and
|
|
471
|
-
*
|
|
480
|
+
* recent one a prior attempt carried (`last_rate_limit_after`), and keeps the
|
|
481
|
+
* failure's original error as its `cause`.
|
|
472
482
|
*/
|
|
473
|
-
function build_rate_limit_error(retryable, attempt, last_rate_limit_after,
|
|
474
|
-
const metadata = {
|
|
483
|
+
function build_rate_limit_error(retryable, attempt, last_rate_limit_after, lead) {
|
|
484
|
+
const metadata = {
|
|
485
|
+
attempts: attempt,
|
|
486
|
+
cause: retryable.cause
|
|
487
|
+
};
|
|
475
488
|
if (retryable.retry_after_ms !== void 0) metadata.retry_after_ms = retryable.retry_after_ms;
|
|
476
489
|
else if (last_rate_limit_after !== void 0) metadata.retry_after_ms = last_rate_limit_after;
|
|
477
490
|
if (retryable.status !== void 0) metadata.status = retryable.status;
|
|
478
|
-
return new rate_limit_error(retryable.message
|
|
491
|
+
return new rate_limit_error(compose_message(lead, retryable.status, retryable.message), metadata);
|
|
479
492
|
}
|
|
480
493
|
/**
|
|
481
494
|
* Build the `provider_error` for a 5xx, network, or timeout failure that this
|
|
482
|
-
* layer won't retry again
|
|
483
|
-
* 5xx carries status/body. The failure's
|
|
495
|
+
* layer won't retry again, after `attempt` failures in all. Timeout collapses
|
|
496
|
+
* onto the `network` cause; only 5xx carries status/body. The failure's
|
|
497
|
+
* original error is its `cause`.
|
|
484
498
|
*/
|
|
485
|
-
function build_provider_error(retryable,
|
|
486
|
-
const metadata = {
|
|
499
|
+
function build_provider_error(retryable, attempt, lead) {
|
|
500
|
+
const metadata = {
|
|
501
|
+
cause_kind: retryable.kind === "provider_5xx" ? "provider_5xx" : "network",
|
|
502
|
+
attempts: attempt,
|
|
503
|
+
cause: retryable.cause
|
|
504
|
+
};
|
|
505
|
+
let status;
|
|
487
506
|
if (retryable.kind === "provider_5xx") {
|
|
488
|
-
|
|
507
|
+
status = retryable.status;
|
|
508
|
+
if (status !== void 0) metadata.status = status;
|
|
489
509
|
if (retryable.body !== void 0) metadata.body = retryable.body;
|
|
490
510
|
}
|
|
491
|
-
return new provider_error(retryable.message
|
|
511
|
+
return new provider_error(compose_message(lead, status, retryable.message), metadata);
|
|
492
512
|
}
|
|
493
513
|
/**
|
|
494
514
|
* Map a classified failure that this layer won't retry again onto the error
|
|
495
515
|
* it rejects with: `rate_limit_error` for 429s, `provider_error` for
|
|
496
|
-
* everything else, after `attempt` failures in all. `
|
|
497
|
-
*
|
|
516
|
+
* everything else, after `attempt` failures in all. `lead` opens the message
|
|
517
|
+
* and says why this layer stopped.
|
|
498
518
|
*/
|
|
499
|
-
function build_typed_error(retryable, attempt, last_rate_limit_after,
|
|
500
|
-
if (retryable.kind === "rate_limit") return build_rate_limit_error(retryable, attempt, last_rate_limit_after,
|
|
501
|
-
return build_provider_error(retryable,
|
|
519
|
+
function build_typed_error(retryable, attempt, last_rate_limit_after, lead) {
|
|
520
|
+
if (retryable.kind === "rate_limit") return build_rate_limit_error(retryable, attempt, last_rate_limit_after, lead);
|
|
521
|
+
return build_provider_error(retryable, attempt, lead);
|
|
502
522
|
}
|
|
503
523
|
/**
|
|
504
524
|
* The error for a failure whose kind `retry_on` leaves out. One that's
|
|
@@ -510,13 +530,14 @@ function build_typed_error(retryable, attempt, last_rate_limit_after, fallback)
|
|
|
510
530
|
*/
|
|
511
531
|
function build_left_out_error(err, retryable, attempt, last_rate_limit_after) {
|
|
512
532
|
if (err instanceof Error) return err;
|
|
513
|
-
return build_typed_error(retryable, attempt, last_rate_limit_after, `${retryable.kind}
|
|
533
|
+
return build_typed_error(retryable, attempt, last_rate_limit_after, `${retryable.kind} left out of retry_on`);
|
|
514
534
|
}
|
|
515
535
|
/**
|
|
516
|
-
* The
|
|
536
|
+
* The lead of an exhausted failure's message, which counts its attempts.
|
|
517
537
|
*/
|
|
518
|
-
function
|
|
519
|
-
|
|
538
|
+
function exhaustion_lead(retryable, attempt) {
|
|
539
|
+
const attempts = attempt === 1 ? "1 attempt" : `${attempt} attempts`;
|
|
540
|
+
return retryable.kind === "rate_limit" ? `rate limited after ${attempts}` : `${retryable.kind} after ${attempts}`;
|
|
520
541
|
}
|
|
521
542
|
/**
|
|
522
543
|
* The server's `Retry-After`, in milliseconds, that a 429 or a 5xx carried,
|
|
@@ -572,13 +593,15 @@ function build_attempt_info(retryable, attempt, delay_ms) {
|
|
|
572
593
|
* An aborted `abort` signal interrupts a pending backoff wait and throws
|
|
573
594
|
* `aborted_error`. Returns the value from the last successful `fn()` call. On
|
|
574
595
|
* exhaustion, throws `rate_limit_error` (for 429s) or `provider_error` (for
|
|
575
|
-
* 5xx/network/timeout)
|
|
596
|
+
* 5xx/network/timeout), counting the attempts, with the last failure's
|
|
597
|
+
* original error as its `cause` and a message that says how many attempts it
|
|
598
|
+
* took before the status and the provider's own message.
|
|
576
599
|
*
|
|
577
600
|
* A failure whose kind `retry_on` leaves out isn't retried. It's rethrown as
|
|
578
601
|
* it is when it's already an `Error`, and a plain record becomes the
|
|
579
602
|
* `rate_limit_error` or `provider_error` that exhaustion would throw for its
|
|
580
|
-
* kind, counting every failure so far in `attempts`, with
|
|
581
|
-
*
|
|
603
|
+
* kind, counting every failure so far in `attempts`, with a message that
|
|
604
|
+
* says `retry_on` leaves its kind out.
|
|
582
605
|
*
|
|
583
606
|
* `on_retry` fires once per retried failure, just before its backoff wait.
|
|
584
607
|
* It does NOT fire for a first attempt that succeeds, a non-retryable
|
|
@@ -603,7 +626,7 @@ async function retry_with_policy(fn, policy = DEFAULT_RETRY, abort, on_retry) {
|
|
|
603
626
|
if (retryable === void 0) throw err;
|
|
604
627
|
attempt += 1;
|
|
605
628
|
if (!is_retryable(retryable.kind, policy)) throw build_left_out_error(err, retryable, attempt, last_rate_limit_after);
|
|
606
|
-
if (attempt >= policy.max_attempts) throw build_typed_error(retryable, attempt, last_rate_limit_after,
|
|
629
|
+
if (attempt >= policy.max_attempts) throw build_typed_error(retryable, attempt, last_rate_limit_after, exhaustion_lead(retryable, attempt));
|
|
607
630
|
const backoff = next_backoff(retryable, backoff_policy, attempt);
|
|
608
631
|
last_rate_limit_after = carry_rate_limit_after(retryable, backoff, last_rate_limit_after);
|
|
609
632
|
on_retry?.(build_attempt_info(retryable, attempt, backoff.delay));
|
|
@@ -674,19 +697,32 @@ function build_retryable_signal(err, kind) {
|
|
|
674
697
|
return base;
|
|
675
698
|
}
|
|
676
699
|
/**
|
|
700
|
+
* Read the retryable shape for a recognized `kind`, or `undefined` for any
|
|
701
|
+
* other.
|
|
702
|
+
*/
|
|
703
|
+
function read_retryable(err) {
|
|
704
|
+
const kind = read_string$1(err, "kind");
|
|
705
|
+
if (kind === "rate_limit") return build_retryable_rate_limit(err);
|
|
706
|
+
if (kind === "provider_5xx") return build_retryable_5xx(err);
|
|
707
|
+
if (kind === "network" || kind === "timeout") return build_retryable_signal(err, kind);
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
677
710
|
* Classify a thrown value into a `RetryableError`, or `undefined` if it
|
|
678
711
|
* does not carry one of the recognized retryable `kind`s.
|
|
679
712
|
*
|
|
680
713
|
* Providers throw plain objects tagged with `kind` (set by
|
|
681
714
|
* `classify_provider_error` in generate.ts); this reads that shape back out
|
|
682
|
-
* field by field rather than trusting it wholesale.
|
|
715
|
+
* field by field rather than trusting it wholesale. The failure's `cause` is
|
|
716
|
+
* what a typed error hands back as its own: the original error that a plain
|
|
717
|
+
* record names as its `cause`, or the thrown value itself when it is already
|
|
718
|
+
* an `Error`, such as `turn_timeout_error`.
|
|
683
719
|
*/
|
|
684
720
|
function classify_retryable(err) {
|
|
685
721
|
if (err === null || typeof err !== "object") return void 0;
|
|
686
|
-
const
|
|
687
|
-
if (
|
|
688
|
-
|
|
689
|
-
|
|
722
|
+
const retryable = read_retryable(err);
|
|
723
|
+
if (retryable === void 0) return void 0;
|
|
724
|
+
retryable.cause = err instanceof Error ? err : Reflect.get(err, "cause");
|
|
725
|
+
return retryable;
|
|
690
726
|
}
|
|
691
727
|
//#endregion
|
|
692
728
|
//#region src/engine/providers/sse_native.ts
|
|
@@ -4497,8 +4533,19 @@ function start_generate_span(trajectory, meta) {
|
|
|
4497
4533
|
return trajectory.start_span("engine.generate", { ...meta });
|
|
4498
4534
|
}
|
|
4499
4535
|
/**
|
|
4536
|
+
* The meta an engine span ends with when its call throws: the message as
|
|
4537
|
+
* `error`, and the status, attempts, cause kind, and `Retry-After` that a
|
|
4538
|
+
* typed provider error carries (see `failure_meta`).
|
|
4539
|
+
*/
|
|
4540
|
+
function error_span_meta(err) {
|
|
4541
|
+
return {
|
|
4542
|
+
error: err instanceof Error ? err.message : String(err),
|
|
4543
|
+
...failure_meta(err)
|
|
4544
|
+
};
|
|
4545
|
+
}
|
|
4546
|
+
/**
|
|
4500
4547
|
* Close the `engine.generate` span with usage, finish reason, resolved model,
|
|
4501
|
-
* or the error
|
|
4548
|
+
* or the error meta on failure. `step_count` and `tool_call_count` are the
|
|
4502
4549
|
* call-level totals (model turns and tool executions in the call), the
|
|
4503
4550
|
* span-attribute analog of the GenAI semconv agent metrics
|
|
4504
4551
|
* `inference_calls` / `tool_calls`.
|
|
@@ -6648,8 +6695,7 @@ async function invoke_turn(config, memory, step_index, step_span) {
|
|
|
6648
6695
|
});
|
|
6649
6696
|
return turn;
|
|
6650
6697
|
} catch (err) {
|
|
6651
|
-
|
|
6652
|
-
end_step_span(config.trajectory, step_span, { error: message });
|
|
6698
|
+
end_step_span(config.trajectory, step_span, error_span_meta(err));
|
|
6653
6699
|
throw err;
|
|
6654
6700
|
}
|
|
6655
6701
|
}
|
|
@@ -7252,15 +7298,43 @@ function resolve_error_status(err) {
|
|
|
7252
7298
|
return typeof status_alt === "number" ? status_alt : void 0;
|
|
7253
7299
|
}
|
|
7254
7300
|
/**
|
|
7255
|
-
*
|
|
7256
|
-
*
|
|
7301
|
+
* The provider's own message: the one a JSON `responseBody` carries, when the
|
|
7302
|
+
* AI SDK attached one, over the error's `message`. A provider package renders
|
|
7303
|
+
* that message from the body, and `@ai-sdk/amazon-bedrock` renders a Bedrock
|
|
7304
|
+
* 503's `{ message }` as `undefined: <message>`, since the body has no
|
|
7305
|
+
* `type`. A native adapter's message already names the provider and the
|
|
7306
|
+
* status it read, so it stays as it is.
|
|
7257
7307
|
*/
|
|
7258
|
-
function
|
|
7308
|
+
function provider_message(err) {
|
|
7309
|
+
const body = Reflect.get(err, "responseBody");
|
|
7310
|
+
const from_body = typeof body === "string" ? error_body_message(body) : void 0;
|
|
7311
|
+
if (from_body !== void 0) return from_body;
|
|
7259
7312
|
const message = Reflect.get(err, "message");
|
|
7260
|
-
|
|
7313
|
+
return typeof message === "string" ? message : void 0;
|
|
7314
|
+
}
|
|
7315
|
+
/**
|
|
7316
|
+
* Copy the provider's message onto a classification record when there is one,
|
|
7317
|
+
* and the thrown error itself as its `cause`, so the typed error that
|
|
7318
|
+
* retry_with_policy throws in the end still holds it. Returns the same record
|
|
7319
|
+
* for chaining.
|
|
7320
|
+
*/
|
|
7321
|
+
function with_error_source(err, out) {
|
|
7322
|
+
const message = provider_message(err);
|
|
7323
|
+
if (message !== void 0) out["message"] = message;
|
|
7324
|
+
out["cause"] = err;
|
|
7261
7325
|
return out;
|
|
7262
7326
|
}
|
|
7263
7327
|
/**
|
|
7328
|
+
* The body of the failed response: the AI SDK's `responseBody`, or else the
|
|
7329
|
+
* `body` a native adapter attaches, when either is a string.
|
|
7330
|
+
*/
|
|
7331
|
+
function response_body(err) {
|
|
7332
|
+
const sdk_body = Reflect.get(err, "responseBody");
|
|
7333
|
+
if (typeof sdk_body === "string") return sdk_body;
|
|
7334
|
+
const body = Reflect.get(err, "body");
|
|
7335
|
+
return typeof body === "string" ? body : void 0;
|
|
7336
|
+
}
|
|
7337
|
+
/**
|
|
7264
7338
|
* Parse a `Retry-After` value off the error's `responseHeaders`, if present.
|
|
7265
7339
|
*/
|
|
7266
7340
|
function retry_after_from_error(err) {
|
|
@@ -7271,15 +7345,18 @@ function retry_after_from_error(err) {
|
|
|
7271
7345
|
}
|
|
7272
7346
|
/**
|
|
7273
7347
|
* Build the classification for a retryable status (`rate_limit` for a 429,
|
|
7274
|
-
* `provider_5xx` for a 5xx), attaching the message and a
|
|
7275
|
-
* when
|
|
7276
|
-
* so a server's wait
|
|
7348
|
+
* `provider_5xx` for a 5xx), attaching the message, the response body, and a
|
|
7349
|
+
* parsed `Retry-After` when each is present, and the error as its `cause`.
|
|
7350
|
+
* HTTP defines the header for a 503 as well as a 429, so a server's wait
|
|
7351
|
+
* counts on both.
|
|
7277
7352
|
*/
|
|
7278
7353
|
function classify_status(err, kind, status) {
|
|
7279
|
-
const out =
|
|
7354
|
+
const out = with_error_source(err, {
|
|
7280
7355
|
kind,
|
|
7281
7356
|
status
|
|
7282
7357
|
});
|
|
7358
|
+
const body = response_body(err);
|
|
7359
|
+
if (body !== void 0) out["body"] = body;
|
|
7283
7360
|
const retry_after_ms = retry_after_from_error(err);
|
|
7284
7361
|
if (retry_after_ms !== void 0) out["retry_after_ms"] = retry_after_ms;
|
|
7285
7362
|
return out;
|
|
@@ -7299,7 +7376,7 @@ function has_network_code(value) {
|
|
|
7299
7376
|
*/
|
|
7300
7377
|
function classify_network_error(err) {
|
|
7301
7378
|
if (!has_network_code(err) && !has_network_code(Reflect.get(err, "cause"))) return void 0;
|
|
7302
|
-
return
|
|
7379
|
+
return with_error_source(err, { kind: "network" });
|
|
7303
7380
|
}
|
|
7304
7381
|
/**
|
|
7305
7382
|
* Classify a thrown provider error into the shape `retry_with_policy`
|
|
@@ -7308,7 +7385,8 @@ function classify_network_error(err) {
|
|
|
7308
7385
|
* Passes through anything that already carries a recognized `kind`.
|
|
7309
7386
|
* Otherwise inspects `statusCode`/`status`: 429 becomes `rate_limit` and 5xx
|
|
7310
7387
|
* becomes `provider_5xx`, each reading `Retry-After` off `responseHeaders`
|
|
7311
|
-
* when present, and a known network
|
|
7388
|
+
* and the body off `responseBody` or `body` when present, and a known network
|
|
7389
|
+
* error code (`ECONNRESET`, `ETIMEDOUT`,
|
|
7312
7390
|
* `ENOTFOUND`, `ECONNREFUSED`, or undici's `UND_ERR_HEADERS_TIMEOUT`,
|
|
7313
7391
|
* `UND_ERR_BODY_TIMEOUT`, and `UND_ERR_SOCKET`), on the error or on its
|
|
7314
7392
|
* `cause`, becomes `network`. Anything else passes through unclassified,
|
|
@@ -7396,7 +7474,10 @@ function retry_turn(call_once, args, stamp, retry_policy, classify, turn_timeout
|
|
|
7396
7474
|
});
|
|
7397
7475
|
if (has_streamed()) {
|
|
7398
7476
|
const message = err instanceof Error ? err.message : String(err);
|
|
7399
|
-
throw new provider_error(`stream interrupted: ${message}`, {
|
|
7477
|
+
throw new provider_error(`stream interrupted: ${message}`, {
|
|
7478
|
+
cause_kind: "unknown",
|
|
7479
|
+
cause: err
|
|
7480
|
+
});
|
|
7400
7481
|
}
|
|
7401
7482
|
if (deadline.timed_out()) throw new turn_timeout_error(turn_timeout_ms ?? 0, args.step_index);
|
|
7402
7483
|
if (err instanceof aborted_error) throw err;
|
|
@@ -7739,7 +7820,7 @@ async function build_ai_sdk_transport(cfg) {
|
|
|
7739
7820
|
const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
|
|
7740
7821
|
let ai_sdk_invoke_mod;
|
|
7741
7822
|
try {
|
|
7742
|
-
ai_sdk_invoke_mod = await import("./invoke-
|
|
7823
|
+
ai_sdk_invoke_mod = await import("./invoke-x5SNh9DV.js");
|
|
7743
7824
|
} catch (err) {
|
|
7744
7825
|
throw missing_peer_error("ai", err);
|
|
7745
7826
|
}
|
|
@@ -8100,7 +8181,7 @@ async function generate(opts_in, engine) {
|
|
|
8100
8181
|
result.timing = call_timing(started_at);
|
|
8101
8182
|
return result;
|
|
8102
8183
|
} catch (err) {
|
|
8103
|
-
end_generate_span(trajectory, generate_span,
|
|
8184
|
+
end_generate_span(trajectory, generate_span, error_span_meta(err));
|
|
8104
8185
|
throw err;
|
|
8105
8186
|
}
|
|
8106
8187
|
}
|
|
@@ -8376,4 +8457,4 @@ function create_engine(config) {
|
|
|
8376
8457
|
//#endregion
|
|
8377
8458
|
export { to_json_schema as n, create_engine as t };
|
|
8378
8459
|
|
|
8379
|
-
//# sourceMappingURL=engine-
|
|
8460
|
+
//# sourceMappingURL=engine-CbVdzplI.js.map
|