fascicle 0.9.0 → 0.9.2

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/dist/index.js CHANGED
@@ -1229,6 +1229,27 @@ var provider_error = class extends Error {
1229
1229
  this.cause_kind = metadata.cause_kind;
1230
1230
  }
1231
1231
  };
1232
+ /**
1233
+ * Turn-timeout expiry (D5). Thrown by the engine-owned retry_turn wrapper when
1234
+ * a depth-1 invoke_turn exceeds `turn_timeout_ms` before any chunk streamed.
1235
+ * Its `kind` is the RetryFailureKind wire value `'timeout'` (not the class
1236
+ * name) on purpose: that is the discriminant classify_retryable /
1237
+ * classify_provider_error already key on, so the shared classifier treats an
1238
+ * expiry as retryable without any timeout-specific branch. A mid-stream expiry
1239
+ * never reaches here — retry_turn converts it to a non-retryable stream
1240
+ * interruption first (C4 parity).
1241
+ */
1242
+ var turn_timeout_error = class extends Error {
1243
+ kind = "timeout";
1244
+ timeout_ms;
1245
+ step_index;
1246
+ constructor(timeout_ms, step_index) {
1247
+ super(`turn exceeded turn_timeout_ms budget of ${timeout_ms}ms`);
1248
+ this.name = "turn_timeout_error";
1249
+ this.timeout_ms = timeout_ms;
1250
+ this.step_index = step_index;
1251
+ }
1252
+ };
1232
1253
  var schema_validation_error = class extends Error {
1233
1254
  kind = "schema_validation_error";
1234
1255
  zod_error;
@@ -1352,7 +1373,8 @@ const DEFAULT_RETRY = {
1352
1373
  retry_on: [
1353
1374
  "rate_limit",
1354
1375
  "provider_5xx",
1355
- "network"
1376
+ "network",
1377
+ "timeout"
1356
1378
  ]
1357
1379
  };
1358
1380
  function parse_retry_after(value) {
@@ -1440,7 +1462,7 @@ function read_string(err, key) {
1440
1462
  const value = Reflect.get(err, key);
1441
1463
  return typeof value === "string" ? value : void 0;
1442
1464
  }
1443
- function read_number(err, key) {
1465
+ function read_number$1(err, key) {
1444
1466
  const value = Reflect.get(err, key);
1445
1467
  return typeof value === "number" ? value : void 0;
1446
1468
  }
@@ -1449,9 +1471,9 @@ function classify_retryable(err) {
1449
1471
  const kind = read_string(err, "kind");
1450
1472
  if (kind === "rate_limit") {
1451
1473
  const base = { kind: "rate_limit" };
1452
- const retry_after = read_number(err, "retry_after_ms");
1474
+ const retry_after = read_number$1(err, "retry_after_ms");
1453
1475
  if (retry_after !== void 0) base.retry_after_ms = retry_after;
1454
- const status = read_number(err, "status");
1476
+ const status = read_number$1(err, "status");
1455
1477
  if (status !== void 0) base.status = status;
1456
1478
  const message = read_string(err, "message");
1457
1479
  if (message !== void 0) base.message = message;
@@ -1459,7 +1481,7 @@ function classify_retryable(err) {
1459
1481
  }
1460
1482
  if (kind === "provider_5xx") {
1461
1483
  const base = { kind: "provider_5xx" };
1462
- const status = read_number(err, "status");
1484
+ const status = read_number$1(err, "status");
1463
1485
  if (status !== void 0) base.status = status;
1464
1486
  const body = read_string(err, "body");
1465
1487
  if (body !== void 0) base.body = body;
@@ -1476,6 +1498,17 @@ function classify_retryable(err) {
1476
1498
  }
1477
1499
  //#endregion
1478
1500
  //#region src/engine/providers/types.ts
1501
+ /**
1502
+ * Read the `transport` selector off a provider init. Defaults to 'ai_sdk';
1503
+ * anything but the two known backends throws at construction so a typo
1504
+ * fails loud instead of silently running the default.
1505
+ */
1506
+ function resolve_transport(init, provider) {
1507
+ const raw = init["transport"];
1508
+ if (raw === void 0 || raw === "ai_sdk") return "ai_sdk";
1509
+ if (raw === "native") return "native";
1510
+ throw new engine_config_error(`${provider} provider: transport must be 'ai_sdk' or 'native', got ${JSON.stringify(raw)}`, provider);
1511
+ }
1479
1512
  function default_normalize_usage(raw) {
1480
1513
  if (raw === void 0) return {
1481
1514
  input_tokens: 0,
@@ -1504,7 +1537,89 @@ async function load_optional_peer(specifier) {
1504
1537
  return mod;
1505
1538
  }
1506
1539
  //#endregion
1507
- //#region src/engine/providers/anthropic.ts
1540
+ //#region src/engine/providers/sse_native.ts
1541
+ /**
1542
+ * Incremental SSE decoder shared by the native (raw-HTTP) provider adapters.
1543
+ *
1544
+ * Wire-neutral framing only: push() takes decoded text as it arrives off the
1545
+ * wire (any chunk boundary, including mid-line) and returns the data payloads
1546
+ * of every event completed by that chunk; flush() drains an event left open
1547
+ * when the stream ends without a trailing blank line. Only `data:` fields
1548
+ * matter to the streams fascicle consumes — the Messages API repeats the
1549
+ * event type inside the JSON payload and the Chat Completions stream sends
1550
+ * nothing but data frames — so `event:`/`id:`/`retry:` fields and `:`
1551
+ * comments are dropped. Multi-line data joins with '\n' per the SSE spec.
1552
+ */
1553
+ function create_sse_decoder() {
1554
+ let buffer = "";
1555
+ let data_lines = [];
1556
+ const take_line = (raw, out) => {
1557
+ const line = raw.endsWith("\r") ? raw.slice(0, -1) : raw;
1558
+ if (line.length === 0) {
1559
+ if (data_lines.length > 0) {
1560
+ out.push(data_lines.join("\n"));
1561
+ data_lines = [];
1562
+ }
1563
+ return;
1564
+ }
1565
+ if (line.startsWith(":")) return;
1566
+ if (line.startsWith("data:")) {
1567
+ const value = line.slice(5);
1568
+ data_lines.push(value.startsWith(" ") ? value.slice(1) : value);
1569
+ }
1570
+ };
1571
+ return {
1572
+ push(text) {
1573
+ buffer += text;
1574
+ const out = [];
1575
+ let newline = buffer.indexOf("\n");
1576
+ while (newline >= 0) {
1577
+ take_line(buffer.slice(0, newline), out);
1578
+ buffer = buffer.slice(newline + 1);
1579
+ newline = buffer.indexOf("\n");
1580
+ }
1581
+ return out;
1582
+ },
1583
+ flush() {
1584
+ const out = [];
1585
+ if (buffer.length > 0) take_line(buffer, out);
1586
+ buffer = "";
1587
+ if (data_lines.length > 0) {
1588
+ out.push(data_lines.join("\n"));
1589
+ data_lines = [];
1590
+ }
1591
+ return out;
1592
+ }
1593
+ };
1594
+ }
1595
+ //#endregion
1596
+ //#region src/engine/providers/anthropic_native.ts
1597
+ /**
1598
+ * Native Anthropic provider adapter (depth-1 raw HTTP).
1599
+ *
1600
+ * Talks to the Messages API directly over global fetch with zero `ai` /
1601
+ * `@ai-sdk/*` in its module graph (D4). It owns request/response mapping
1602
+ * only: generate.ts wraps invoke_turn in retry + classification + abort
1603
+ * (D5), so failures are thrown in shapes the shared classify_provider_error
1604
+ * already understands (`status` + `responseHeaders` for HTTP transients,
1605
+ * `kind: 'network'` for transport failures) instead of being re-classified
1606
+ * or retried here.
1607
+ *
1608
+ * Selected via `transport: 'native'` on the anthropic provider init. The
1609
+ * adapter name stays 'anthropic' so DEFAULT_PRICING keys and UsageTotals
1610
+ * fields keep working across transports (C6). Schema requests ride the
1611
+ * engine's prompt + parse + repair loop (D6), so `TurnRequest.schema` is
1612
+ * intentionally unread. Streaming hand-rolls the SSE parse; the aggregator
1613
+ * rebuilds the non-stream payload shape and feeds it through the same
1614
+ * parse_messages_response, so streamed and non-streamed results are equal by
1615
+ * construction (C4) rather than by parallel code paths.
1616
+ */
1617
+ /**
1618
+ * Effort → extended-thinking budget tokens per spec §6.3. Lives here (not in
1619
+ * anthropic.ts) because both transports map effort from this table and the
1620
+ * ai_sdk factory already imports this module for transport dispatch; the
1621
+ * reverse import would be a cycle.
1622
+ */
1508
1623
  const ANTHROPIC_THINKING_BUDGETS = {
1509
1624
  none: 0,
1510
1625
  low: 1024,
@@ -1513,6 +1628,527 @@ const ANTHROPIC_THINKING_BUDGETS = {
1513
1628
  xhigh: 32e3,
1514
1629
  max: 64e3
1515
1630
  };
1631
+ const ANTHROPIC_VERSION = "2023-06-01";
1632
+ /**
1633
+ * Matches the @ai-sdk/anthropic baseURL convention (origin + /v1) so a
1634
+ * base_url configured for the ai_sdk transport keeps pointing at the same
1635
+ * place when the transport flips to native.
1636
+ */
1637
+ const DEFAULT_BASE_URL$2 = "https://api.anthropic.com/v1";
1638
+ /**
1639
+ * The Messages API requires max_tokens on every request. When extended
1640
+ * thinking is enabled the budget counts against max_tokens, so the default
1641
+ * rides on top of the thinking budget rather than being swallowed by it.
1642
+ */
1643
+ const DEFAULT_MAX_TOKENS = 4096;
1644
+ function to_user_blocks(content) {
1645
+ if (typeof content === "string") return content.trim().length > 0 ? [{
1646
+ type: "text",
1647
+ text: content
1648
+ }] : [];
1649
+ const blocks = [];
1650
+ for (const part of content) {
1651
+ if (part.type === "image") throw new provider_capability_error("anthropic", "image_input", "image parts are not mapped on the native transport; use transport: 'ai_sdk'");
1652
+ if (part.text.trim().length > 0) blocks.push({
1653
+ type: "text",
1654
+ text: part.text
1655
+ });
1656
+ }
1657
+ return blocks;
1658
+ }
1659
+ function to_assistant_blocks(content) {
1660
+ if (typeof content === "string") return content.trim().length > 0 ? [{
1661
+ type: "text",
1662
+ text: content
1663
+ }] : [];
1664
+ const blocks = [];
1665
+ for (const part of content) if (part.type === "text") {
1666
+ if (part.text.trim().length > 0) blocks.push({
1667
+ type: "text",
1668
+ text: part.text
1669
+ });
1670
+ } else blocks.push({
1671
+ type: "tool_use",
1672
+ id: part.id,
1673
+ name: part.name,
1674
+ input: part.input
1675
+ });
1676
+ return blocks;
1677
+ }
1678
+ /**
1679
+ * Map fascicle Message[] to Messages-API shape: leading system messages hoist
1680
+ * to the top-level `system` string, tool messages become `tool_result` blocks
1681
+ * on a user turn, and empty text blocks are dropped (the API rejects them).
1682
+ * generate.ts already hoists leading system into TurnRequest.system, so the
1683
+ * hoist here only fires when the adapter is driven directly.
1684
+ */
1685
+ function to_anthropic_messages(messages) {
1686
+ const system_parts = [];
1687
+ const out = [];
1688
+ const push_blocks = (role, blocks) => {
1689
+ if (blocks.length === 0) return;
1690
+ const last = out[out.length - 1];
1691
+ if (last?.role === role) {
1692
+ last.content.push(...blocks);
1693
+ return;
1694
+ }
1695
+ out.push({
1696
+ role,
1697
+ content: blocks
1698
+ });
1699
+ };
1700
+ for (const message of messages) switch (message.role) {
1701
+ case "system":
1702
+ if (out.length > 0) throw new provider_capability_error("anthropic", "mid_conversation_system_messages", "the Messages API accepts system text only before the first user/assistant turn");
1703
+ system_parts.push(message.content);
1704
+ break;
1705
+ case "user":
1706
+ push_blocks("user", to_user_blocks(message.content));
1707
+ break;
1708
+ case "assistant":
1709
+ push_blocks("assistant", to_assistant_blocks(message.content));
1710
+ break;
1711
+ case "tool":
1712
+ push_blocks("user", [{
1713
+ type: "tool_result",
1714
+ tool_use_id: message.tool_call_id,
1715
+ content: message.content
1716
+ }]);
1717
+ break;
1718
+ }
1719
+ return {
1720
+ system: system_parts.length > 0 ? system_parts.join("\n\n") : void 0,
1721
+ messages: out
1722
+ };
1723
+ }
1724
+ function to_anthropic_tools(tools) {
1725
+ return tools.map((tool) => ({
1726
+ name: tool.name,
1727
+ description: tool.description,
1728
+ input_schema: z.toJSONSchema(tool.input_schema)
1729
+ }));
1730
+ }
1731
+ function build_messages_body(req) {
1732
+ const { system: hoisted_system, messages } = to_anthropic_messages(req.messages);
1733
+ const system_parts = [req.system, hoisted_system].filter((s) => typeof s === "string" && s.length > 0);
1734
+ const budget = ANTHROPIC_THINKING_BUDGETS[req.effort];
1735
+ const thinking_enabled = budget > 0;
1736
+ const body = {
1737
+ model: req.model_id,
1738
+ messages,
1739
+ max_tokens: req.max_tokens ?? (thinking_enabled ? budget + DEFAULT_MAX_TOKENS : DEFAULT_MAX_TOKENS)
1740
+ };
1741
+ if (system_parts.length > 0) body["system"] = system_parts.join("\n\n");
1742
+ if (req.tools.length > 0) body["tools"] = to_anthropic_tools(req.tools);
1743
+ if (req.stream) body["stream"] = true;
1744
+ if (thinking_enabled) body["thinking"] = {
1745
+ type: "enabled",
1746
+ budget_tokens: budget
1747
+ };
1748
+ else if (req.temperature !== void 0 || req.top_p !== void 0) {
1749
+ if (req.temperature !== void 0) body["temperature"] = req.temperature;
1750
+ if (req.top_p !== void 0) body["top_p"] = req.top_p;
1751
+ }
1752
+ const passthrough = req.provider_options?.["anthropic"];
1753
+ return passthrough === void 0 ? body : {
1754
+ ...body,
1755
+ ...passthrough
1756
+ };
1757
+ }
1758
+ function map_anthropic_stop_reason(raw) {
1759
+ switch (raw) {
1760
+ case "tool_use": return "tool_calls";
1761
+ case "max_tokens":
1762
+ case "model_context_window_exceeded": return "length";
1763
+ case "refusal": return "content_filter";
1764
+ default: return "stop";
1765
+ }
1766
+ }
1767
+ /**
1768
+ * The API reports input_tokens EXCLUSIVE of cache reads and writes, while
1769
+ * compute_cost subtracts both back out of UsageTotals.input_tokens to price
1770
+ * the fresh remainder. So the inclusive total is the sum of all three; a
1771
+ * straight copy would silently under-count input cost on cached calls.
1772
+ */
1773
+ function map_anthropic_usage(raw) {
1774
+ if (raw === null || typeof raw !== "object") return {
1775
+ input_tokens: 0,
1776
+ output_tokens: 0
1777
+ };
1778
+ const read = (key) => {
1779
+ const value = Reflect.get(raw, key);
1780
+ return typeof value === "number" ? value : void 0;
1781
+ };
1782
+ const cache_read = read("cache_read_input_tokens");
1783
+ const cache_write = read("cache_creation_input_tokens");
1784
+ const totals = {
1785
+ input_tokens: (read("input_tokens") ?? 0) + (cache_read ?? 0) + (cache_write ?? 0),
1786
+ output_tokens: read("output_tokens") ?? 0
1787
+ };
1788
+ if (cache_read !== void 0) totals.cached_input_tokens = cache_read;
1789
+ if (cache_write !== void 0) totals.cache_write_tokens = cache_write;
1790
+ return totals;
1791
+ }
1792
+ function parse_messages_response(payload) {
1793
+ if (payload === null || typeof payload !== "object") throw new provider_error("anthropic native: response payload is not a JSON object");
1794
+ const content = Reflect.get(payload, "content");
1795
+ const text_parts = [];
1796
+ const tool_calls = [];
1797
+ if (Array.isArray(content)) for (const block of content) {
1798
+ if (block === null || typeof block !== "object") continue;
1799
+ const type = Reflect.get(block, "type");
1800
+ if (type === "text") {
1801
+ const text = Reflect.get(block, "text");
1802
+ if (typeof text === "string") text_parts.push(text);
1803
+ } else if (type === "tool_use") {
1804
+ const id = Reflect.get(block, "id");
1805
+ const name = Reflect.get(block, "name");
1806
+ if (typeof id !== "string" || typeof name !== "string") throw new provider_error("anthropic native: malformed tool_use block in response content");
1807
+ tool_calls.push({
1808
+ id,
1809
+ name,
1810
+ input: Reflect.get(block, "input")
1811
+ });
1812
+ }
1813
+ }
1814
+ return {
1815
+ text: text_parts.join(""),
1816
+ tool_calls,
1817
+ finish_reason: map_anthropic_stop_reason(Reflect.get(payload, "stop_reason")),
1818
+ usage: map_anthropic_usage(Reflect.get(payload, "usage"))
1819
+ };
1820
+ }
1821
+ /**
1822
+ * Mid-stream `error` events reuse the HTTP status classification:
1823
+ * overloaded_error is the SSE analog of a 529, api_error of a 500, and
1824
+ * rate_limit_error of a 429, so classify_provider_error sees the transients
1825
+ * it already retries. Anything else is a permanent provider_error. Whether a
1826
+ * retry actually happens stays retry_turn's call — once chunks have streamed
1827
+ * it refuses, exactly as on the ai_sdk path.
1828
+ */
1829
+ function stream_event_error(event) {
1830
+ const error = Reflect.get(event, "error");
1831
+ let error_type = "unknown";
1832
+ let message = "(no message)";
1833
+ if (error !== null && typeof error === "object") {
1834
+ const raw_type = Reflect.get(error, "type");
1835
+ if (typeof raw_type === "string" && raw_type.length > 0) error_type = raw_type;
1836
+ const raw_message = Reflect.get(error, "message");
1837
+ if (typeof raw_message === "string" && raw_message.length > 0) message = raw_message;
1838
+ }
1839
+ const detail = `anthropic stream error (${error_type}): ${message}`;
1840
+ let status;
1841
+ if (error_type === "overloaded_error") status = 529;
1842
+ else if (error_type === "api_error") status = 500;
1843
+ else if (error_type === "rate_limit_error") status = 429;
1844
+ if (status !== void 0) return Object.assign(new Error(detail), { status });
1845
+ return new provider_error(detail);
1846
+ }
1847
+ function read_block_index(event) {
1848
+ const index = Reflect.get(event, "index");
1849
+ if (typeof index !== "number") throw new provider_error("anthropic native: stream event is missing its block index");
1850
+ return index;
1851
+ }
1852
+ /**
1853
+ * Consume parsed Messages-stream events, dispatching StreamChunks as they
1854
+ * arrive and rebuilding the non-stream response payload as it goes: text
1855
+ * deltas accumulate into synthetic text blocks, tool_use input arrives as raw
1856
+ * JSON text (input_json_delta) held per open block and parsed at
1857
+ * content_block_stop. complete() feeds the synthetic payload through
1858
+ * parse_messages_response, which is what makes the streamed TurnResult equal
1859
+ * the non-streamed one by construction (C4). A stream that ends without
1860
+ * message_stop is truncated output, so complete() fails loud instead of
1861
+ * returning a partial turn.
1862
+ */
1863
+ function create_stream_aggregator(step_index, dispatch) {
1864
+ const content = [];
1865
+ const open_text = /* @__PURE__ */ new Map();
1866
+ const open_tools = /* @__PURE__ */ new Map();
1867
+ const usage = {};
1868
+ let stop_reason;
1869
+ let stopped = false;
1870
+ const merge_usage = (raw) => {
1871
+ if (raw === null || typeof raw !== "object") return;
1872
+ for (const [key, value] of Object.entries(raw)) if (typeof value === "number") usage[key] = value;
1873
+ };
1874
+ const on_block_start = async (event) => {
1875
+ const index = read_block_index(event);
1876
+ const block = Reflect.get(event, "content_block");
1877
+ if (block === null || typeof block !== "object") return;
1878
+ const type = Reflect.get(block, "type");
1879
+ if (type === "text") {
1880
+ const initial = Reflect.get(block, "text");
1881
+ const synthetic = {
1882
+ type: "text",
1883
+ text: typeof initial === "string" ? initial : ""
1884
+ };
1885
+ content.push(synthetic);
1886
+ open_text.set(index, synthetic);
1887
+ return;
1888
+ }
1889
+ if (type === "tool_use") {
1890
+ const id = Reflect.get(block, "id");
1891
+ const name = Reflect.get(block, "name");
1892
+ if (typeof id !== "string" || typeof name !== "string") throw new provider_error("anthropic native: malformed tool_use block in stream");
1893
+ const synthetic = {
1894
+ type: "tool_use",
1895
+ id,
1896
+ name,
1897
+ input: Reflect.get(block, "input") ?? {}
1898
+ };
1899
+ content.push(synthetic);
1900
+ open_tools.set(index, {
1901
+ block: synthetic,
1902
+ json: ""
1903
+ });
1904
+ await dispatch({
1905
+ kind: "tool_call_start",
1906
+ id,
1907
+ name,
1908
+ step_index
1909
+ });
1910
+ }
1911
+ };
1912
+ const on_block_delta = async (event) => {
1913
+ const index = read_block_index(event);
1914
+ const delta = Reflect.get(event, "delta");
1915
+ if (delta === null || typeof delta !== "object") return;
1916
+ const type = Reflect.get(delta, "type");
1917
+ if (type === "text_delta") {
1918
+ const text = Reflect.get(delta, "text");
1919
+ const open = open_text.get(index);
1920
+ if (typeof text !== "string" || open === void 0) return;
1921
+ open.text += text;
1922
+ await dispatch({
1923
+ kind: "text",
1924
+ text,
1925
+ step_index
1926
+ });
1927
+ return;
1928
+ }
1929
+ if (type === "thinking_delta") {
1930
+ const thinking = Reflect.get(delta, "thinking");
1931
+ if (typeof thinking === "string") await dispatch({
1932
+ kind: "reasoning",
1933
+ text: thinking,
1934
+ step_index
1935
+ });
1936
+ return;
1937
+ }
1938
+ if (type === "input_json_delta") {
1939
+ const partial = Reflect.get(delta, "partial_json");
1940
+ const open = open_tools.get(index);
1941
+ if (typeof partial !== "string" || open === void 0) return;
1942
+ open.json += partial;
1943
+ await dispatch({
1944
+ kind: "tool_call_input_delta",
1945
+ id: open.block.id,
1946
+ delta: partial,
1947
+ step_index
1948
+ });
1949
+ }
1950
+ };
1951
+ const on_block_stop = async (event) => {
1952
+ const index = read_block_index(event);
1953
+ const open = open_tools.get(index);
1954
+ open_text.delete(index);
1955
+ if (open === void 0) return;
1956
+ open_tools.delete(index);
1957
+ if (open.json.length > 0) try {
1958
+ open.block.input = JSON.parse(open.json);
1959
+ } catch {
1960
+ throw new provider_error(`anthropic native: tool_use input for ${open.block.name} is not valid JSON`);
1961
+ }
1962
+ await dispatch({
1963
+ kind: "tool_call_end",
1964
+ id: open.block.id,
1965
+ input: open.block.input,
1966
+ step_index
1967
+ });
1968
+ };
1969
+ return {
1970
+ async handle_event(data) {
1971
+ let event;
1972
+ try {
1973
+ event = JSON.parse(data);
1974
+ } catch {
1975
+ throw new provider_error("anthropic native: stream event is not valid JSON");
1976
+ }
1977
+ if (event === null || typeof event !== "object") return;
1978
+ switch (Reflect.get(event, "type")) {
1979
+ case "message_start": {
1980
+ const message = Reflect.get(event, "message");
1981
+ if (message !== null && typeof message === "object") merge_usage(Reflect.get(message, "usage"));
1982
+ break;
1983
+ }
1984
+ case "content_block_start":
1985
+ await on_block_start(event);
1986
+ break;
1987
+ case "content_block_delta":
1988
+ await on_block_delta(event);
1989
+ break;
1990
+ case "content_block_stop":
1991
+ await on_block_stop(event);
1992
+ break;
1993
+ case "message_delta": {
1994
+ const delta = Reflect.get(event, "delta");
1995
+ if (delta !== null && typeof delta === "object") {
1996
+ const raw = Reflect.get(delta, "stop_reason");
1997
+ if (typeof raw === "string") stop_reason = raw;
1998
+ }
1999
+ merge_usage(Reflect.get(event, "usage"));
2000
+ break;
2001
+ }
2002
+ case "message_stop":
2003
+ stopped = true;
2004
+ await dispatch({
2005
+ kind: "step_finish",
2006
+ step_index,
2007
+ finish_reason: map_anthropic_stop_reason(stop_reason),
2008
+ usage: map_anthropic_usage(usage)
2009
+ });
2010
+ break;
2011
+ case "error": throw stream_event_error(event);
2012
+ default: break;
2013
+ }
2014
+ },
2015
+ complete() {
2016
+ if (!stopped) throw new provider_error("anthropic native: stream ended before message_stop; the result would be truncated");
2017
+ return parse_messages_response({
2018
+ content,
2019
+ stop_reason,
2020
+ usage
2021
+ });
2022
+ }
2023
+ };
2024
+ }
2025
+ /** Anthropic error bodies are `{ type: 'error', error: { message } }`; fall back to the raw body. */
2026
+ function extract_error_message$2(body) {
2027
+ if (body.length === 0) return "(empty body)";
2028
+ try {
2029
+ const parsed = JSON.parse(body);
2030
+ if (parsed !== null && typeof parsed === "object") {
2031
+ const error = Reflect.get(parsed, "error");
2032
+ if (error !== null && typeof error === "object") {
2033
+ const message = Reflect.get(error, "message");
2034
+ if (typeof message === "string" && message.length > 0) return message;
2035
+ }
2036
+ }
2037
+ } catch {}
2038
+ return body.length > 300 ? `${body.slice(0, 300)}...` : body;
2039
+ }
2040
+ /**
2041
+ * Map a non-2xx response to what the engine's retry stack expects: 401
2042
+ * becomes a typed provider_auth_error, 429/5xx keep `status` +
2043
+ * `responseHeaders` so classify_provider_error marks them retryable
2044
+ * (rate_limit honoring retry-after, provider_5xx), and any other 4xx is a
2045
+ * permanent provider_error that surfaces as-is.
2046
+ */
2047
+ async function response_error$2(response) {
2048
+ let body = "";
2049
+ try {
2050
+ body = await response.text();
2051
+ } catch {}
2052
+ const detail = extract_error_message$2(body);
2053
+ const status = response.status;
2054
+ if (status === 401) return new provider_auth_error("anthropic", `anthropic authentication failed (401): ${detail}`);
2055
+ if (status === 429 || status >= 500) {
2056
+ const retry_after = response.headers.get("retry-after");
2057
+ return Object.assign(/* @__PURE__ */ new Error(`anthropic API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
2058
+ }
2059
+ return new provider_error(`anthropic API error ${status}: ${detail}`, {
2060
+ status,
2061
+ body
2062
+ });
2063
+ }
2064
+ /**
2065
+ * A user abort surfaces as the fetch/reader AbortError; retry_turn converts
2066
+ * it via its own signal check, so it is rethrown untouched. Everything else
2067
+ * is a transport failure wrapped in the `kind: 'network'` shape the shared
2068
+ * classify_provider_error marks retryable.
2069
+ */
2070
+ function rethrow_network_failure$2(err, abort) {
2071
+ if (abort.aborted) throw err;
2072
+ const detail = err instanceof Error ? err.message : String(err);
2073
+ throw Object.assign(/* @__PURE__ */ new Error(`anthropic native: network failure: ${detail}`), { kind: "network" });
2074
+ }
2075
+ /**
2076
+ * Drain a streaming Messages response: decode bytes, reassemble SSE events,
2077
+ * and feed them to the aggregator, which dispatches chunks through
2078
+ * req.dispatch_chunk as they arrive. Transport failures mid-read wrap as
2079
+ * network errors; aggregator throws (malformed events, mid-stream error
2080
+ * events, a rejecting on_chunk) pass through untouched, with the reader
2081
+ * cancelled so the connection is released.
2082
+ */
2083
+ async function consume_sse_response$1(response, req) {
2084
+ const body = response.body;
2085
+ if (body === null) throw new provider_error("anthropic native: streaming response has no body");
2086
+ const dispatch = req.dispatch_chunk ?? (async () => {});
2087
+ const aggregator = create_stream_aggregator(req.step_index, dispatch);
2088
+ const reader = body.getReader();
2089
+ const text_decoder = new TextDecoder();
2090
+ const sse = create_sse_decoder();
2091
+ const next_bytes = async () => {
2092
+ let step;
2093
+ try {
2094
+ step = await reader.read();
2095
+ } catch (err) {
2096
+ rethrow_network_failure$2(err, req.abort);
2097
+ }
2098
+ return step.done ? void 0 : step.value;
2099
+ };
2100
+ try {
2101
+ while (true) {
2102
+ const bytes = await next_bytes();
2103
+ if (bytes === void 0) break;
2104
+ for (const data of sse.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle_event(data);
2105
+ }
2106
+ } finally {
2107
+ reader.cancel().catch(() => {});
2108
+ }
2109
+ const tail = [...sse.push(text_decoder.decode()), ...sse.flush()];
2110
+ for (const data of tail) await aggregator.handle_event(data);
2111
+ return aggregator.complete();
2112
+ }
2113
+ const SUPPORTED$10 = /* @__PURE__ */ new Set([
2114
+ "text",
2115
+ "tools",
2116
+ "schema",
2117
+ "streaming",
2118
+ "reasoning"
2119
+ ]);
2120
+ const create_anthropic_native_adapter = (init) => {
2121
+ const api_key = typeof init.api_key === "string" ? init.api_key : "";
2122
+ if (api_key.length === 0) throw new engine_config_error("anthropic provider requires a non-empty api_key", "anthropic");
2123
+ const base_url = (typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : DEFAULT_BASE_URL$2).replace(/\/+$/, "");
2124
+ return {
2125
+ kind: "native",
2126
+ name: "anthropic",
2127
+ async invoke_turn(req) {
2128
+ let response;
2129
+ try {
2130
+ response = await fetch(`${base_url}/messages`, {
2131
+ method: "POST",
2132
+ headers: {
2133
+ "content-type": "application/json",
2134
+ "x-api-key": api_key,
2135
+ "anthropic-version": ANTHROPIC_VERSION
2136
+ },
2137
+ body: JSON.stringify(build_messages_body(req)),
2138
+ signal: req.abort
2139
+ });
2140
+ } catch (err) {
2141
+ rethrow_network_failure$2(err, req.abort);
2142
+ }
2143
+ if (!response.ok) throw await response_error$2(response);
2144
+ if (req.stream) return consume_sse_response$1(response, req);
2145
+ return parse_messages_response(await response.json());
2146
+ },
2147
+ supports: (capability) => SUPPORTED$10.has(capability)
2148
+ };
2149
+ };
2150
+ //#endregion
2151
+ //#region src/engine/providers/anthropic.ts
1516
2152
  function translate_anthropic_effort(effort) {
1517
2153
  const budget = ANTHROPIC_THINKING_BUDGETS[effort];
1518
2154
  if (effort === "none" || budget === 0) return {
@@ -1530,7 +2166,7 @@ function translate_anthropic_effort(effort) {
1530
2166
  function normalize_anthropic_usage(raw) {
1531
2167
  return default_normalize_usage(raw);
1532
2168
  }
1533
- const SUPPORTED$7 = /* @__PURE__ */ new Set([
2169
+ const SUPPORTED$9 = /* @__PURE__ */ new Set([
1534
2170
  "text",
1535
2171
  "tools",
1536
2172
  "schema",
@@ -1539,6 +2175,10 @@ const SUPPORTED$7 = /* @__PURE__ */ new Set([
1539
2175
  "reasoning"
1540
2176
  ]);
1541
2177
  const create_anthropic_adapter = (init) => {
2178
+ if (resolve_transport(init, "anthropic") === "native") return create_anthropic_native_adapter(init);
2179
+ return create_anthropic_ai_sdk_adapter(init);
2180
+ };
2181
+ const create_anthropic_ai_sdk_adapter = (init) => {
1542
2182
  const api_key = typeof init.api_key === "string" ? init.api_key : "";
1543
2183
  if (api_key.length === 0) throw new engine_config_error("anthropic provider requires a non-empty api_key", "anthropic");
1544
2184
  const base_url = typeof init.base_url === "string" ? init.base_url : void 0;
@@ -1553,7 +2193,516 @@ const create_anthropic_adapter = (init) => {
1553
2193
  },
1554
2194
  translate_effort: translate_anthropic_effort,
1555
2195
  normalize_usage: normalize_anthropic_usage,
1556
- supports: (capability) => SUPPORTED$7.has(capability)
2196
+ supports: (capability) => SUPPORTED$9.has(capability)
2197
+ };
2198
+ };
2199
+ //#endregion
2200
+ //#region src/engine/providers/openai_compatible_native.ts
2201
+ /**
2202
+ * OpenAI-compatible native core (depth-1 raw HTTP).
2203
+ *
2204
+ * One `chat/completions` implementation parameterized by a dialect config
2205
+ * (D1): base_url, auth strategy, extra headers, stream-usage behavior,
2206
+ * token-limit field name, and usage tolerance (D10). The `openai`,
2207
+ * `openrouter`, and `lmstudio` factories each build a dialect and share this
2208
+ * core; pointing base_url at any compat server (including Ollama's /v1)
2209
+ * rides the same path.
2210
+ *
2211
+ * Zero `ai` / `@ai-sdk/*` in the module graph (C3). The adapter owns
2212
+ * request/response mapping only: generate.ts wraps invoke_turn in retry +
2213
+ * classification + abort, so failures are thrown in shapes the shared
2214
+ * classify_provider_error already understands (`status` + `responseHeaders`
2215
+ * for HTTP transients, `kind: 'network'` for transport failures), never
2216
+ * retried here. Schema requests ride the engine's prompt + parse + repair
2217
+ * loop, so `TurnRequest.schema` is intentionally unread. Streaming hand-rolls
2218
+ * the SSE parse (`data:` lines, the literal `[DONE]` terminator, index-keyed
2219
+ * tool_call delta accumulation); the aggregator rebuilds the non-stream
2220
+ * payload shape and feeds it through the same parse_chat_completion, so
2221
+ * streamed and non-streamed results are equal by construction (C4) rather
2222
+ * than by parallel code paths.
2223
+ */
2224
+ /**
2225
+ * Effort map per Appendix A4: the wire enum is `low | medium | high` only, so
2226
+ * `xhigh` and `max` clamp to `high`, the same clamp as the ai_sdk transport's
2227
+ * `reasoningEffort`. `none` omits the field entirely. Non-reasoning models
2228
+ * ignore the field server-side; the adapter does not model-sniff.
2229
+ */
2230
+ const OPENAI_COMPATIBLE_REASONING_EFFORT = {
2231
+ low: "low",
2232
+ medium: "medium",
2233
+ high: "high",
2234
+ xhigh: "high",
2235
+ max: "high"
2236
+ };
2237
+ function to_user_content$1(content, provider) {
2238
+ if (typeof content === "string") return content;
2239
+ const parts = [];
2240
+ for (const part of content) {
2241
+ if (part.type === "image") throw new provider_capability_error(provider, "image_input", "image parts are not mapped on the native transport; use transport: 'ai_sdk'");
2242
+ if (part.text.trim().length > 0) parts.push({
2243
+ type: "text",
2244
+ text: part.text
2245
+ });
2246
+ }
2247
+ return parts.length > 0 ? parts : "";
2248
+ }
2249
+ function to_assistant_message$1(content) {
2250
+ if (typeof content === "string") return {
2251
+ role: "assistant",
2252
+ content
2253
+ };
2254
+ const text_parts = [];
2255
+ const tool_calls = [];
2256
+ for (const part of content) if (part.type === "text") text_parts.push(part.text);
2257
+ else tool_calls.push({
2258
+ id: part.id,
2259
+ type: "function",
2260
+ function: {
2261
+ name: part.name,
2262
+ arguments: JSON.stringify(part.input)
2263
+ }
2264
+ });
2265
+ const text = text_parts.join("");
2266
+ const message = {
2267
+ role: "assistant",
2268
+ content: text.length > 0 || tool_calls.length === 0 ? text : null
2269
+ };
2270
+ if (tool_calls.length > 0) message.tool_calls = tool_calls;
2271
+ return message;
2272
+ }
2273
+ /**
2274
+ * Map fascicle Message[] to Chat Completions shape. Unlike the Messages API
2275
+ * there is no top-level system field or role-alternation constraint: system
2276
+ * messages map in place at any position, and tool results are first-class
2277
+ * `tool` role messages keyed by tool_call_id.
2278
+ */
2279
+ function to_chat_messages(messages, provider) {
2280
+ const out = [];
2281
+ for (const message of messages) switch (message.role) {
2282
+ case "system":
2283
+ out.push({
2284
+ role: "system",
2285
+ content: message.content
2286
+ });
2287
+ break;
2288
+ case "user":
2289
+ out.push({
2290
+ role: "user",
2291
+ content: to_user_content$1(message.content, provider)
2292
+ });
2293
+ break;
2294
+ case "assistant":
2295
+ out.push(to_assistant_message$1(message.content));
2296
+ break;
2297
+ case "tool":
2298
+ out.push({
2299
+ role: "tool",
2300
+ tool_call_id: message.tool_call_id,
2301
+ content: message.content
2302
+ });
2303
+ break;
2304
+ }
2305
+ return out;
2306
+ }
2307
+ function to_chat_tools(tools) {
2308
+ return tools.map((tool) => ({
2309
+ type: "function",
2310
+ function: {
2311
+ name: tool.name,
2312
+ description: tool.description,
2313
+ parameters: z.toJSONSchema(tool.input_schema)
2314
+ }
2315
+ }));
2316
+ }
2317
+ function build_chat_completions_body(req, dialect) {
2318
+ const messages = [];
2319
+ if (req.system !== void 0 && req.system.length > 0) messages.push({
2320
+ role: "system",
2321
+ content: req.system
2322
+ });
2323
+ messages.push(...to_chat_messages(req.messages, dialect.name));
2324
+ const body = {
2325
+ model: req.model_id,
2326
+ messages
2327
+ };
2328
+ if (req.tools.length > 0) body["tools"] = to_chat_tools(req.tools);
2329
+ if (req.max_tokens !== void 0) body[dialect.token_limit_field] = req.max_tokens;
2330
+ if (req.temperature !== void 0) body["temperature"] = req.temperature;
2331
+ if (req.top_p !== void 0) body["top_p"] = req.top_p;
2332
+ if (req.effort !== "none") body["reasoning_effort"] = OPENAI_COMPATIBLE_REASONING_EFFORT[req.effort];
2333
+ if (req.stream) {
2334
+ body["stream"] = true;
2335
+ if (dialect.stream_include_usage) body["stream_options"] = { include_usage: true };
2336
+ }
2337
+ const passthrough = req.provider_options?.[dialect.name];
2338
+ return passthrough === void 0 ? body : {
2339
+ ...body,
2340
+ ...passthrough
2341
+ };
2342
+ }
2343
+ function map_chat_finish_reason(raw) {
2344
+ switch (raw) {
2345
+ case "tool_calls": return "tool_calls";
2346
+ case "length": return "length";
2347
+ case "content_filter": return "content_filter";
2348
+ default: return "stop";
2349
+ }
2350
+ }
2351
+ /**
2352
+ * Usage map per Appendix A3. prompt_tokens is already INCLUSIVE of cached
2353
+ * tokens on this API, unlike Anthropic's exclusive accounting, so a straight
2354
+ * copy is correct; compute_cost subtracts the cached portion back out. There
2355
+ * is no cache-write concept on this wire. A missing usage object throws under
2356
+ * a strict dialect (a hosted API omitting usage is a broken response) and
2357
+ * zeroes under a tolerant one (D10).
2358
+ */
2359
+ function read_number(source, key) {
2360
+ const value = Reflect.get(source, key);
2361
+ return typeof value === "number" ? value : void 0;
2362
+ }
2363
+ function map_chat_usage(raw, dialect) {
2364
+ if (raw === null || typeof raw !== "object") {
2365
+ if (dialect.tolerant_usage) return {
2366
+ input_tokens: 0,
2367
+ output_tokens: 0
2368
+ };
2369
+ throw new provider_error(`${dialect.name} native: response is missing its usage object`);
2370
+ }
2371
+ const totals = {
2372
+ input_tokens: read_number(raw, "prompt_tokens") ?? 0,
2373
+ output_tokens: read_number(raw, "completion_tokens") ?? 0
2374
+ };
2375
+ const prompt_details = Reflect.get(raw, "prompt_tokens_details");
2376
+ if (prompt_details !== null && typeof prompt_details === "object") {
2377
+ const cached = read_number(prompt_details, "cached_tokens");
2378
+ if (cached !== void 0) totals.cached_input_tokens = cached;
2379
+ }
2380
+ const completion_details = Reflect.get(raw, "completion_tokens_details");
2381
+ if (completion_details !== null && typeof completion_details === "object") {
2382
+ const reasoning = read_number(completion_details, "reasoning_tokens");
2383
+ if (reasoning !== void 0) totals.reasoning_tokens = reasoning;
2384
+ }
2385
+ return totals;
2386
+ }
2387
+ function parse_tool_call(raw, provider) {
2388
+ if (raw === null || typeof raw !== "object") throw new provider_error(`${provider} native: malformed tool_calls entry in response`);
2389
+ const id = Reflect.get(raw, "id");
2390
+ const fn = Reflect.get(raw, "function");
2391
+ if (typeof id !== "string" || fn === null || typeof fn !== "object") throw new provider_error(`${provider} native: malformed tool_calls entry in response`);
2392
+ const name = Reflect.get(fn, "name");
2393
+ if (typeof name !== "string") throw new provider_error(`${provider} native: malformed tool_calls entry in response`);
2394
+ const raw_args = Reflect.get(fn, "arguments");
2395
+ let input = {};
2396
+ if (typeof raw_args === "string" && raw_args.length > 0) try {
2397
+ input = JSON.parse(raw_args);
2398
+ } catch {
2399
+ throw new provider_error(`${provider} native: tool_calls arguments for ${name} is not valid JSON`);
2400
+ }
2401
+ return {
2402
+ id,
2403
+ name,
2404
+ input
2405
+ };
2406
+ }
2407
+ /**
2408
+ * The one response parser every path feeds (C4): non-stream parses the
2409
+ * payload directly, and the streaming aggregator (next step) rebuilds this
2410
+ * same payload shape from deltas before calling it.
2411
+ */
2412
+ function parse_chat_completion(payload, dialect) {
2413
+ if (payload === null || typeof payload !== "object") throw new provider_error(`${dialect.name} native: response payload is not a JSON object`);
2414
+ const choices = Reflect.get(payload, "choices");
2415
+ const choice = Array.isArray(choices) ? choices[0] : void 0;
2416
+ if (choice === null || choice === void 0 || typeof choice !== "object") throw new provider_error(`${dialect.name} native: response has no choices`);
2417
+ const message = Reflect.get(choice, "message");
2418
+ if (message === null || typeof message !== "object") throw new provider_error(`${dialect.name} native: response choice has no message`);
2419
+ const content = Reflect.get(message, "content");
2420
+ const raw_tool_calls = Reflect.get(message, "tool_calls");
2421
+ const tool_calls = Array.isArray(raw_tool_calls) ? raw_tool_calls.map((entry) => parse_tool_call(entry, dialect.name)) : [];
2422
+ return {
2423
+ text: typeof content === "string" ? content : "",
2424
+ tool_calls,
2425
+ finish_reason: map_chat_finish_reason(Reflect.get(choice, "finish_reason")),
2426
+ usage: map_chat_usage(Reflect.get(payload, "usage"), dialect)
2427
+ };
2428
+ }
2429
+ /**
2430
+ * Consume Chat Completions stream frames (the JSON payload of each SSE
2431
+ * `data:` line, plus the literal `[DONE]` terminator), dispatching
2432
+ * StreamChunks as they arrive and rebuilding the non-stream payload as it
2433
+ * goes: `delta.content` accumulates into the message text, and
2434
+ * `delta.tool_calls[]` entries accumulate `function.arguments` string deltas
2435
+ * keyed by their `index` field (A5). A tool call closes — parsed input
2436
+ * dispatched — when the choice's finish_reason frame arrives, or at `[DONE]`
2437
+ * for servers that never send one. Usage rides the final pre-DONE frame
2438
+ * (stream_options.include_usage); a stream that ends without `[DONE]` is
2439
+ * truncated output, so complete() fails loud instead of returning a partial
2440
+ * turn. complete() feeds the synthetic payload through parse_chat_completion,
2441
+ * which is what makes the streamed TurnResult equal the non-streamed one by
2442
+ * construction (C4).
2443
+ */
2444
+ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
2445
+ let text = "";
2446
+ const tool_calls = [];
2447
+ const by_index = /* @__PURE__ */ new Map();
2448
+ let finish_reason;
2449
+ let usage;
2450
+ let done = false;
2451
+ const in_index_order = () => tool_calls.toSorted((a, b) => a.index - b.index);
2452
+ const close_tool_calls = async () => {
2453
+ for (const call of in_index_order()) {
2454
+ if (call.closed) continue;
2455
+ call.closed = true;
2456
+ const { input } = parse_tool_call({
2457
+ id: call.id,
2458
+ function: {
2459
+ name: call.name,
2460
+ arguments: call.arguments
2461
+ }
2462
+ }, dialect.name);
2463
+ await dispatch({
2464
+ kind: "tool_call_end",
2465
+ id: call.id,
2466
+ input,
2467
+ step_index
2468
+ });
2469
+ }
2470
+ };
2471
+ const on_tool_call_delta = async (entry) => {
2472
+ if (entry === null || typeof entry !== "object") throw new provider_error(`${dialect.name} native: malformed tool_calls delta in stream`);
2473
+ const index = Reflect.get(entry, "index");
2474
+ if (typeof index !== "number") throw new provider_error(`${dialect.name} native: stream tool_calls delta is missing its index`);
2475
+ const fn = Reflect.get(entry, "function");
2476
+ let call = by_index.get(index);
2477
+ if (call === void 0) {
2478
+ const id = Reflect.get(entry, "id");
2479
+ const name = fn !== null && typeof fn === "object" ? Reflect.get(fn, "name") : void 0;
2480
+ if (typeof id !== "string" || typeof name !== "string") throw new provider_error(`${dialect.name} native: stream tool_calls delta opened without id and name`);
2481
+ call = {
2482
+ index,
2483
+ id,
2484
+ name,
2485
+ arguments: "",
2486
+ closed: false
2487
+ };
2488
+ by_index.set(index, call);
2489
+ tool_calls.push(call);
2490
+ await dispatch({
2491
+ kind: "tool_call_start",
2492
+ id,
2493
+ name,
2494
+ step_index
2495
+ });
2496
+ }
2497
+ if (fn === null || typeof fn !== "object") return;
2498
+ const args = Reflect.get(fn, "arguments");
2499
+ if (typeof args === "string" && args.length > 0) {
2500
+ call.arguments += args;
2501
+ await dispatch({
2502
+ kind: "tool_call_input_delta",
2503
+ id: call.id,
2504
+ delta: args,
2505
+ step_index
2506
+ });
2507
+ }
2508
+ };
2509
+ const on_choice = async (choice) => {
2510
+ const delta = Reflect.get(choice, "delta");
2511
+ if (delta !== null && typeof delta === "object") {
2512
+ const content = Reflect.get(delta, "content");
2513
+ if (typeof content === "string" && content.length > 0) {
2514
+ text += content;
2515
+ await dispatch({
2516
+ kind: "text",
2517
+ text: content,
2518
+ step_index
2519
+ });
2520
+ }
2521
+ const raw_calls = Reflect.get(delta, "tool_calls");
2522
+ if (Array.isArray(raw_calls)) for (const entry of raw_calls) await on_tool_call_delta(entry);
2523
+ }
2524
+ const raw_finish = Reflect.get(choice, "finish_reason");
2525
+ if (typeof raw_finish === "string") {
2526
+ finish_reason = raw_finish;
2527
+ await close_tool_calls();
2528
+ }
2529
+ };
2530
+ return {
2531
+ async handle_data(data) {
2532
+ if (data === "[DONE]") {
2533
+ if (done) return;
2534
+ done = true;
2535
+ await close_tool_calls();
2536
+ await dispatch({
2537
+ kind: "step_finish",
2538
+ step_index,
2539
+ finish_reason: map_chat_finish_reason(finish_reason),
2540
+ usage: map_chat_usage(usage, dialect)
2541
+ });
2542
+ return;
2543
+ }
2544
+ let frame;
2545
+ try {
2546
+ frame = JSON.parse(data);
2547
+ } catch {
2548
+ throw new provider_error(`${dialect.name} native: stream frame is not valid JSON`);
2549
+ }
2550
+ if (frame === null || typeof frame !== "object") return;
2551
+ const raw_usage = Reflect.get(frame, "usage");
2552
+ if (raw_usage !== null && typeof raw_usage === "object") usage = raw_usage;
2553
+ const choices = Reflect.get(frame, "choices");
2554
+ const choice = Array.isArray(choices) ? choices[0] : void 0;
2555
+ if (choice !== null && choice !== void 0 && typeof choice === "object") await on_choice(choice);
2556
+ },
2557
+ complete() {
2558
+ if (!done) throw new provider_error(`${dialect.name} native: stream ended before [DONE]; the result would be truncated`);
2559
+ const ordered = in_index_order();
2560
+ const message = {
2561
+ role: "assistant",
2562
+ content: text.length > 0 || ordered.length === 0 ? text : null
2563
+ };
2564
+ if (ordered.length > 0) message["tool_calls"] = ordered.map((call) => ({
2565
+ id: call.id,
2566
+ type: "function",
2567
+ function: {
2568
+ name: call.name,
2569
+ arguments: call.arguments
2570
+ }
2571
+ }));
2572
+ return parse_chat_completion({
2573
+ choices: [{
2574
+ index: 0,
2575
+ message,
2576
+ finish_reason: finish_reason ?? null
2577
+ }],
2578
+ usage
2579
+ }, dialect);
2580
+ }
2581
+ };
2582
+ }
2583
+ /** OpenAI-style error bodies are `{ error: { message } }`; fall back to the raw body. */
2584
+ function extract_error_message$1(body) {
2585
+ if (body.length === 0) return "(empty body)";
2586
+ try {
2587
+ const parsed = JSON.parse(body);
2588
+ if (parsed !== null && typeof parsed === "object") {
2589
+ const error = Reflect.get(parsed, "error");
2590
+ if (error !== null && typeof error === "object") {
2591
+ const message = Reflect.get(error, "message");
2592
+ if (typeof message === "string" && message.length > 0) return message;
2593
+ }
2594
+ }
2595
+ } catch {}
2596
+ return body.length > 300 ? `${body.slice(0, 300)}...` : body;
2597
+ }
2598
+ /**
2599
+ * Map a non-2xx response to what the engine's retry stack expects: 401
2600
+ * becomes a typed provider_auth_error, 429/5xx keep `status` +
2601
+ * `responseHeaders` so classify_provider_error marks them retryable
2602
+ * (rate_limit honoring retry-after, provider_5xx), and any other 4xx is a
2603
+ * permanent provider_error that surfaces as-is.
2604
+ */
2605
+ async function response_error$1(response, provider) {
2606
+ let body = "";
2607
+ try {
2608
+ body = await response.text();
2609
+ } catch {}
2610
+ const detail = extract_error_message$1(body);
2611
+ const status = response.status;
2612
+ if (status === 401) return new provider_auth_error(provider, `${provider} authentication failed (401): ${detail}`);
2613
+ if (status === 429 || status >= 500) {
2614
+ const retry_after = response.headers.get("retry-after");
2615
+ return Object.assign(/* @__PURE__ */ new Error(`${provider} API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
2616
+ }
2617
+ return new provider_error(`${provider} API error ${status}: ${detail}`, {
2618
+ status,
2619
+ body
2620
+ });
2621
+ }
2622
+ /**
2623
+ * A user abort surfaces as the fetch AbortError; retry_turn converts it via
2624
+ * its own signal check, so it is rethrown untouched. Everything else is a
2625
+ * transport failure wrapped in the `kind: 'network'` shape the shared
2626
+ * classify_provider_error marks retryable.
2627
+ */
2628
+ function rethrow_network_failure$1(err, abort, provider) {
2629
+ if (abort.aborted) throw err;
2630
+ const detail = err instanceof Error ? err.message : String(err);
2631
+ throw Object.assign(/* @__PURE__ */ new Error(`${provider} native: network failure: ${detail}`), { kind: "network" });
2632
+ }
2633
+ /**
2634
+ * Drain a streaming chat/completions response: decode bytes, reassemble SSE
2635
+ * events, and feed each data payload to the aggregator, which dispatches
2636
+ * chunks through req.dispatch_chunk as they arrive. Transport failures
2637
+ * mid-read wrap as network errors; aggregator throws (malformed frames, a
2638
+ * rejecting on_chunk) pass through untouched, with the reader cancelled so
2639
+ * the connection is released.
2640
+ */
2641
+ async function consume_sse_response(response, req, dialect) {
2642
+ const body = response.body;
2643
+ if (body === null) throw new provider_error(`${dialect.name} native: streaming response has no body`);
2644
+ const dispatch = req.dispatch_chunk ?? (async () => {});
2645
+ const aggregator = create_chat_stream_aggregator(dialect, req.step_index, dispatch);
2646
+ const reader = body.getReader();
2647
+ const text_decoder = new TextDecoder();
2648
+ const sse = create_sse_decoder();
2649
+ const next_bytes = async () => {
2650
+ let step;
2651
+ try {
2652
+ step = await reader.read();
2653
+ } catch (err) {
2654
+ rethrow_network_failure$1(err, req.abort, dialect.name);
2655
+ }
2656
+ return step.done ? void 0 : step.value;
2657
+ };
2658
+ try {
2659
+ while (true) {
2660
+ const bytes = await next_bytes();
2661
+ if (bytes === void 0) break;
2662
+ for (const data of sse.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle_data(data);
2663
+ }
2664
+ } finally {
2665
+ reader.cancel().catch(() => {});
2666
+ }
2667
+ const tail = [...sse.push(text_decoder.decode()), ...sse.flush()];
2668
+ for (const data of tail) await aggregator.handle_data(data);
2669
+ return aggregator.complete();
2670
+ }
2671
+ const SUPPORTED$8 = /* @__PURE__ */ new Set([
2672
+ "text",
2673
+ "tools",
2674
+ "schema",
2675
+ "streaming",
2676
+ "reasoning"
2677
+ ]);
2678
+ const create_openai_compatible_adapter = (dialect) => {
2679
+ if (dialect.auth.kind === "bearer" && dialect.auth.api_key.length === 0) throw new engine_config_error(`${dialect.name} provider requires a non-empty api_key`, dialect.name);
2680
+ const base_url = dialect.base_url.replace(/\/+$/, "");
2681
+ const headers = {
2682
+ "content-type": "application/json",
2683
+ ...dialect.extra_headers
2684
+ };
2685
+ if (dialect.auth.kind === "bearer") headers["authorization"] = `Bearer ${dialect.auth.api_key}`;
2686
+ return {
2687
+ kind: "native",
2688
+ name: dialect.name,
2689
+ async invoke_turn(req) {
2690
+ let response;
2691
+ try {
2692
+ response = await fetch(`${base_url}/chat/completions`, {
2693
+ method: "POST",
2694
+ headers,
2695
+ body: JSON.stringify(build_chat_completions_body(req, dialect)),
2696
+ signal: req.abort
2697
+ });
2698
+ } catch (err) {
2699
+ rethrow_network_failure$1(err, req.abort, dialect.name);
2700
+ }
2701
+ if (!response.ok) throw await response_error$1(response, dialect.name);
2702
+ if (req.stream) return consume_sse_response(response, req, dialect);
2703
+ return parse_chat_completion(await response.json(), dialect);
2704
+ },
2705
+ supports: (capability) => SUPPORTED$8.has(capability)
1557
2706
  };
1558
2707
  };
1559
2708
  //#endregion
@@ -1578,7 +2727,7 @@ function translate_openai_effort(effort) {
1578
2727
  function normalize_openai_usage(raw) {
1579
2728
  return default_normalize_usage(raw);
1580
2729
  }
1581
- const SUPPORTED$6 = /* @__PURE__ */ new Set([
2730
+ const SUPPORTED$7 = /* @__PURE__ */ new Set([
1582
2731
  "text",
1583
2732
  "tools",
1584
2733
  "schema",
@@ -1586,7 +2735,42 @@ const SUPPORTED$6 = /* @__PURE__ */ new Set([
1586
2735
  "image_input",
1587
2736
  "reasoning"
1588
2737
  ]);
2738
+ /**
2739
+ * OpenAI's default API origin, matching the @ai-sdk/openai baseURL convention
2740
+ * (origin + /v1) so a base_url configured for the ai_sdk transport keeps
2741
+ * pointing at the same place when the transport flips to native.
2742
+ */
2743
+ const DEFAULT_BASE_URL$1 = "https://api.openai.com/v1";
1589
2744
  const create_openai_adapter = (init) => {
2745
+ if (resolve_transport(init, "openai") === "native") return create_openai_native_adapter(init);
2746
+ return create_openai_ai_sdk_adapter(init);
2747
+ };
2748
+ /**
2749
+ * Build the openai dialect (Appendix A1) and hand it to the shared
2750
+ * OpenAI-compatible core: Bearer auth, an optional OpenAI-Organization header,
2751
+ * the `max_completion_tokens` token-limit field, and strict usage (a hosted
2752
+ * API omitting usage is a broken response, not a local-runtime quirk). The
2753
+ * empty-api_key guard rides on the core, which throws the same
2754
+ * `openai provider requires a non-empty api_key` engine_config_error.
2755
+ */
2756
+ const create_openai_native_adapter = (init) => {
2757
+ const api_key = typeof init.api_key === "string" ? init.api_key : "";
2758
+ const base_url = typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : DEFAULT_BASE_URL$1;
2759
+ const organization = typeof init["organization"] === "string" ? init["organization"] : void 0;
2760
+ return create_openai_compatible_adapter({
2761
+ name: "openai",
2762
+ base_url,
2763
+ auth: {
2764
+ kind: "bearer",
2765
+ api_key
2766
+ },
2767
+ token_limit_field: "max_completion_tokens",
2768
+ stream_include_usage: true,
2769
+ tolerant_usage: false,
2770
+ ...organization !== void 0 ? { extra_headers: { "OpenAI-Organization": organization } } : {}
2771
+ });
2772
+ };
2773
+ const create_openai_ai_sdk_adapter = (init) => {
1590
2774
  const api_key = typeof init.api_key === "string" ? init.api_key : "";
1591
2775
  if (api_key.length === 0) throw new engine_config_error("openai provider requires a non-empty api_key", "openai");
1592
2776
  const base_url = typeof init.base_url === "string" ? init.base_url : void 0;
@@ -1603,54 +2787,467 @@ const create_openai_adapter = (init) => {
1603
2787
  },
1604
2788
  translate_effort: translate_openai_effort,
1605
2789
  normalize_usage: normalize_openai_usage,
2790
+ supports: (capability) => SUPPORTED$7.has(capability)
2791
+ };
2792
+ };
2793
+ //#endregion
2794
+ //#region src/engine/providers/google.ts
2795
+ const GOOGLE_THINKING_BUDGET = {
2796
+ low: 1024,
2797
+ medium: 8192,
2798
+ high: 24576,
2799
+ xhigh: 32768,
2800
+ max: 32768
2801
+ };
2802
+ function translate_google_effort(effort) {
2803
+ if (effort === "none") return {
2804
+ provider_options: {},
2805
+ effort_ignored: false
2806
+ };
2807
+ return {
2808
+ provider_options: { google: { thinkingConfig: { thinkingBudget: GOOGLE_THINKING_BUDGET[effort] } } },
2809
+ effort_ignored: false
2810
+ };
2811
+ }
2812
+ function normalize_google_usage(raw) {
2813
+ return default_normalize_usage(raw);
2814
+ }
2815
+ const SUPPORTED$6 = /* @__PURE__ */ new Set([
2816
+ "text",
2817
+ "tools",
2818
+ "schema",
2819
+ "streaming",
2820
+ "image_input",
2821
+ "reasoning"
2822
+ ]);
2823
+ const create_google_adapter = (init) => {
2824
+ const api_key = typeof init.api_key === "string" ? init.api_key : "";
2825
+ if (api_key.length === 0) throw new engine_config_error("google provider requires a non-empty api_key", "google");
2826
+ const base_url = typeof init.base_url === "string" ? init.base_url : void 0;
2827
+ return {
2828
+ kind: "ai_sdk",
2829
+ name: "google",
2830
+ async build_model(model_id) {
2831
+ const sdk = await load_optional_peer("@ai-sdk/google");
2832
+ const config = { apiKey: api_key };
2833
+ if (base_url !== void 0) config.baseURL = base_url;
2834
+ return sdk.createGoogleGenerativeAI(config)(model_id);
2835
+ },
2836
+ translate_effort: translate_google_effort,
2837
+ normalize_usage: normalize_google_usage,
1606
2838
  supports: (capability) => SUPPORTED$6.has(capability)
1607
2839
  };
1608
- };
1609
- //#endregion
1610
- //#region src/engine/providers/google.ts
1611
- const GOOGLE_THINKING_BUDGET = {
1612
- low: 1024,
1613
- medium: 8192,
1614
- high: 24576,
1615
- xhigh: 32768,
1616
- max: 32768
1617
- };
1618
- function translate_google_effort(effort) {
1619
- if (effort === "none") return {
1620
- provider_options: {},
1621
- effort_ignored: false
2840
+ };
2841
+ //#endregion
2842
+ //#region src/engine/providers/ollama_native.ts
2843
+ function to_user_content(content) {
2844
+ if (typeof content === "string") return content;
2845
+ const parts = [];
2846
+ for (const part of content) {
2847
+ if (part.type === "image") throw new provider_capability_error("ollama", "image_input", "image parts are not mapped on the native transport; use transport: 'ai_sdk'");
2848
+ if (part.text.trim().length > 0) parts.push(part.text);
2849
+ }
2850
+ return parts.join("\n");
2851
+ }
2852
+ function to_assistant_message(content) {
2853
+ if (typeof content === "string") return {
2854
+ role: "assistant",
2855
+ content
2856
+ };
2857
+ const text_parts = [];
2858
+ const tool_calls = [];
2859
+ for (const part of content) if (part.type === "text") text_parts.push(part.text);
2860
+ else tool_calls.push({ function: {
2861
+ name: part.name,
2862
+ arguments: part.input
2863
+ } });
2864
+ const message = {
2865
+ role: "assistant",
2866
+ content: text_parts.join("")
2867
+ };
2868
+ if (tool_calls.length > 0) message.tool_calls = tool_calls;
2869
+ return message;
2870
+ }
2871
+ /**
2872
+ * Map fascicle Message[] to the /api/chat shape. System messages map in place
2873
+ * (no top-level system field); tool results are `tool` role messages keyed by
2874
+ * `tool_name`, since this wire has no tool_call_id to round-trip.
2875
+ */
2876
+ function to_ollama_messages(messages) {
2877
+ const out = [];
2878
+ for (const message of messages) switch (message.role) {
2879
+ case "system":
2880
+ out.push({
2881
+ role: "system",
2882
+ content: message.content
2883
+ });
2884
+ break;
2885
+ case "user":
2886
+ out.push({
2887
+ role: "user",
2888
+ content: to_user_content(message.content)
2889
+ });
2890
+ break;
2891
+ case "assistant":
2892
+ out.push(to_assistant_message(message.content));
2893
+ break;
2894
+ case "tool":
2895
+ out.push({
2896
+ role: "tool",
2897
+ tool_name: message.name,
2898
+ content: message.content
2899
+ });
2900
+ break;
2901
+ }
2902
+ return out;
2903
+ }
2904
+ function build_ollama_chat_body(req) {
2905
+ const messages = [];
2906
+ if (req.system !== void 0 && req.system.length > 0) messages.push({
2907
+ role: "system",
2908
+ content: req.system
2909
+ });
2910
+ messages.push(...to_ollama_messages(req.messages));
2911
+ const body = {
2912
+ model: req.model_id,
2913
+ messages,
2914
+ stream: req.stream
2915
+ };
2916
+ if (req.tools.length > 0) body["tools"] = to_chat_tools(req.tools);
2917
+ const options = {};
2918
+ if (req.max_tokens !== void 0) options["num_predict"] = req.max_tokens;
2919
+ if (req.temperature !== void 0) options["temperature"] = req.temperature;
2920
+ if (req.top_p !== void 0) options["top_p"] = req.top_p;
2921
+ if (Object.keys(options).length > 0) body["options"] = options;
2922
+ const passthrough = req.provider_options?.["ollama"];
2923
+ return passthrough === void 0 ? body : {
2924
+ ...body,
2925
+ ...passthrough
2926
+ };
2927
+ }
2928
+ /**
2929
+ * Appendix A2, Ollama row: the presence of tool calls wins (the wire reports
2930
+ * done_reason 'stop' on a tool-call turn), then `length`; everything else —
2931
+ * 'stop', 'load', absent — means the model stopped on its own.
2932
+ */
2933
+ function map_ollama_finish_reason(raw, has_tool_calls) {
2934
+ if (has_tool_calls) return "tool_calls";
2935
+ return raw === "length" ? "length" : "stop";
2936
+ }
2937
+ /**
2938
+ * Appendix A3, Ollama row: prompt_eval_count → input_tokens, eval_count →
2939
+ * output_tokens; no cache or reasoning fields exist on this wire. Counts are
2940
+ * always tolerant (D10): a local daemon that omits them (or a mid-stream
2941
+ * frame, which never carries them) zeroes the totals, never throws.
2942
+ */
2943
+ function map_ollama_usage(payload) {
2944
+ if (payload === null || typeof payload !== "object") return {
2945
+ input_tokens: 0,
2946
+ output_tokens: 0
2947
+ };
2948
+ const prompt = Reflect.get(payload, "prompt_eval_count");
2949
+ const evaluated = Reflect.get(payload, "eval_count");
2950
+ return {
2951
+ input_tokens: typeof prompt === "number" ? prompt : 0,
2952
+ output_tokens: typeof evaluated === "number" ? evaluated : 0
2953
+ };
2954
+ }
2955
+ /**
2956
+ * This wire sends no tool-call id, so one is synthesized from the step index
2957
+ * and the call's ordinal within the turn — deterministic, so the streamed and
2958
+ * non-streamed parses of the same turn agree (C4), and unique across steps so
2959
+ * transcript tool results stay unambiguous. A wire id is preferred if a
2960
+ * future daemon version starts sending one.
2961
+ */
2962
+ function parse_ollama_tool_call(raw, ordinal, step_index) {
2963
+ if (raw === null || typeof raw !== "object") throw new provider_error("ollama native: malformed tool_calls entry in response");
2964
+ const fn = Reflect.get(raw, "function");
2965
+ if (fn === null || typeof fn !== "object") throw new provider_error("ollama native: malformed tool_calls entry in response");
2966
+ const name = Reflect.get(fn, "name");
2967
+ if (typeof name !== "string") throw new provider_error("ollama native: malformed tool_calls entry in response");
2968
+ const wire_id = Reflect.get(raw, "id");
2969
+ const id = typeof wire_id === "string" && wire_id.length > 0 ? wire_id : `ollama_call_${step_index}_${ordinal}`;
2970
+ const raw_args = Reflect.get(fn, "arguments");
2971
+ let input = {};
2972
+ if (typeof raw_args === "string" && raw_args.length > 0) try {
2973
+ input = JSON.parse(raw_args);
2974
+ } catch {
2975
+ throw new provider_error(`ollama native: tool_calls arguments for ${name} is not valid JSON`);
2976
+ }
2977
+ else if (raw_args !== null && typeof raw_args === "object") input = raw_args;
2978
+ return {
2979
+ id,
2980
+ name,
2981
+ input
2982
+ };
2983
+ }
2984
+ /**
2985
+ * The one response parser every path feeds (C4): non-stream parses the
2986
+ * payload directly, and the NDJSON aggregator rebuilds this same payload
2987
+ * shape from its frames before calling it.
2988
+ */
2989
+ function parse_ollama_chat(payload, step_index) {
2990
+ if (payload === null || typeof payload !== "object") throw new provider_error("ollama native: response payload is not a JSON object");
2991
+ const message = Reflect.get(payload, "message");
2992
+ if (message === null || message === void 0 || typeof message !== "object") throw new provider_error("ollama native: response has no message");
2993
+ const content = Reflect.get(message, "content");
2994
+ const raw_tool_calls = Reflect.get(message, "tool_calls");
2995
+ const tool_calls = Array.isArray(raw_tool_calls) ? raw_tool_calls.map((entry, ordinal) => parse_ollama_tool_call(entry, ordinal, step_index)) : [];
2996
+ return {
2997
+ text: typeof content === "string" ? content : "",
2998
+ tool_calls,
2999
+ finish_reason: map_ollama_finish_reason(Reflect.get(payload, "done_reason"), tool_calls.length > 0),
3000
+ usage: map_ollama_usage(payload)
3001
+ };
3002
+ }
3003
+ /**
3004
+ * Incremental NDJSON framing: push() takes decoded text at any chunk boundary
3005
+ * (including mid-line) and returns the complete lines it finished; flush()
3006
+ * drains a final line left open when the stream ends without a trailing
3007
+ * newline. Blank lines are dropped; JSON parsing is the aggregator's job.
3008
+ */
3009
+ function take_line(raw, out) {
3010
+ const line = raw.endsWith("\r") ? raw.slice(0, -1) : raw;
3011
+ if (line.trim().length > 0) out.push(line);
3012
+ }
3013
+ function create_ndjson_decoder() {
3014
+ let buffer = "";
3015
+ return {
3016
+ push(text) {
3017
+ buffer += text;
3018
+ const out = [];
3019
+ let newline = buffer.indexOf("\n");
3020
+ while (newline >= 0) {
3021
+ take_line(buffer.slice(0, newline), out);
3022
+ buffer = buffer.slice(newline + 1);
3023
+ newline = buffer.indexOf("\n");
3024
+ }
3025
+ return out;
3026
+ },
3027
+ flush() {
3028
+ const out = [];
3029
+ if (buffer.length > 0) take_line(buffer, out);
3030
+ buffer = "";
3031
+ return out;
3032
+ }
3033
+ };
3034
+ }
3035
+ /**
3036
+ * Consume /api/chat NDJSON frames, dispatching StreamChunks as they arrive
3037
+ * and rebuilding the non-stream payload as it goes: `message.content`
3038
+ * accumulates into the turn text, and `message.tool_calls` entries arrive
3039
+ * WHOLE per frame — arguments are complete objects on this wire, so each call
3040
+ * dispatches tool_call_start then tool_call_end with no input deltas between.
3041
+ * The `done: true` frame carries done_reason and the eval counts and closes
3042
+ * the turn (step_finish); frames after it are ignored. A stream that ends
3043
+ * without one is truncated output, so complete() fails loud instead of
3044
+ * returning a partial turn. complete() feeds the synthetic payload through
3045
+ * parse_ollama_chat, which is what makes the streamed TurnResult equal the
3046
+ * non-streamed one by construction (C4).
3047
+ */
3048
+ function create_ollama_stream_aggregator(step_index, dispatch) {
3049
+ let text = "";
3050
+ const tool_calls = [];
3051
+ let done = false;
3052
+ let done_reason;
3053
+ let usage = {
3054
+ input_tokens: 0,
3055
+ output_tokens: 0
3056
+ };
3057
+ const on_message = async (message) => {
3058
+ const content = Reflect.get(message, "content");
3059
+ if (typeof content === "string" && content.length > 0) {
3060
+ text += content;
3061
+ await dispatch({
3062
+ kind: "text",
3063
+ text: content,
3064
+ step_index
3065
+ });
3066
+ }
3067
+ const raw_calls = Reflect.get(message, "tool_calls");
3068
+ if (!Array.isArray(raw_calls)) return;
3069
+ for (const entry of raw_calls) {
3070
+ const call = parse_ollama_tool_call(entry, tool_calls.length, step_index);
3071
+ tool_calls.push(call);
3072
+ await dispatch({
3073
+ kind: "tool_call_start",
3074
+ id: call.id,
3075
+ name: call.name,
3076
+ step_index
3077
+ });
3078
+ await dispatch({
3079
+ kind: "tool_call_end",
3080
+ id: call.id,
3081
+ input: call.input,
3082
+ step_index
3083
+ });
3084
+ }
1622
3085
  };
1623
3086
  return {
1624
- provider_options: { google: { thinkingConfig: { thinkingBudget: GOOGLE_THINKING_BUDGET[effort] } } },
1625
- effort_ignored: false
3087
+ async handle_line(line) {
3088
+ if (done) return;
3089
+ let frame;
3090
+ try {
3091
+ frame = JSON.parse(line);
3092
+ } catch {
3093
+ throw new provider_error("ollama native: stream frame is not valid JSON");
3094
+ }
3095
+ if (frame === null || typeof frame !== "object") return;
3096
+ const message = Reflect.get(frame, "message");
3097
+ if (message !== null && message !== void 0 && typeof message === "object") await on_message(message);
3098
+ if (Reflect.get(frame, "done") === true) {
3099
+ done = true;
3100
+ done_reason = Reflect.get(frame, "done_reason");
3101
+ usage = map_ollama_usage(frame);
3102
+ await dispatch({
3103
+ kind: "step_finish",
3104
+ step_index,
3105
+ finish_reason: map_ollama_finish_reason(done_reason, tool_calls.length > 0),
3106
+ usage
3107
+ });
3108
+ }
3109
+ },
3110
+ complete() {
3111
+ if (!done) throw new provider_error("ollama native: stream ended before its done frame; the result would be truncated");
3112
+ const message = {
3113
+ role: "assistant",
3114
+ content: text
3115
+ };
3116
+ if (tool_calls.length > 0) message["tool_calls"] = tool_calls.map((call) => ({
3117
+ id: call.id,
3118
+ function: {
3119
+ name: call.name,
3120
+ arguments: call.input
3121
+ }
3122
+ }));
3123
+ return parse_ollama_chat({
3124
+ message,
3125
+ done: true,
3126
+ done_reason: done_reason ?? null,
3127
+ prompt_eval_count: usage.input_tokens,
3128
+ eval_count: usage.output_tokens
3129
+ }, step_index);
3130
+ }
1626
3131
  };
1627
3132
  }
1628
- function normalize_google_usage(raw) {
1629
- return default_normalize_usage(raw);
3133
+ /** Ollama error bodies are `{ error: "message" }`; tolerate the nested OpenAI shape too. */
3134
+ function extract_error_message(body) {
3135
+ if (body.length === 0) return "(empty body)";
3136
+ try {
3137
+ const parsed = JSON.parse(body);
3138
+ if (parsed !== null && typeof parsed === "object") {
3139
+ const error = Reflect.get(parsed, "error");
3140
+ if (typeof error === "string" && error.length > 0) return error;
3141
+ if (error !== null && typeof error === "object") {
3142
+ const message = Reflect.get(error, "message");
3143
+ if (typeof message === "string" && message.length > 0) return message;
3144
+ }
3145
+ }
3146
+ } catch {}
3147
+ return body.length > 300 ? `${body.slice(0, 300)}...` : body;
3148
+ }
3149
+ /**
3150
+ * Map a non-2xx response to what the engine's retry stack expects: 401
3151
+ * becomes a typed provider_auth_error (rare locally, but base_url may point
3152
+ * at a fronted daemon), 429/5xx keep `status` + `responseHeaders` so
3153
+ * classify_provider_error marks them retryable, and any other 4xx (a missing
3154
+ * model, a malformed request) is a permanent provider_error.
3155
+ */
3156
+ async function response_error(response) {
3157
+ let body = "";
3158
+ try {
3159
+ body = await response.text();
3160
+ } catch {}
3161
+ const detail = extract_error_message(body);
3162
+ const status = response.status;
3163
+ if (status === 401) return new provider_auth_error("ollama", `ollama authentication failed (401): ${detail}`);
3164
+ if (status === 429 || status >= 500) {
3165
+ const retry_after = response.headers.get("retry-after");
3166
+ return Object.assign(/* @__PURE__ */ new Error(`ollama API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
3167
+ }
3168
+ return new provider_error(`ollama API error ${status}: ${detail}`, {
3169
+ status,
3170
+ body
3171
+ });
3172
+ }
3173
+ /**
3174
+ * A user abort surfaces as the fetch AbortError; retry_turn converts it via
3175
+ * its own signal check, so it is rethrown untouched. Everything else is a
3176
+ * transport failure wrapped in the `kind: 'network'` shape the shared
3177
+ * classify_provider_error marks retryable.
3178
+ */
3179
+ function rethrow_network_failure(err, abort) {
3180
+ if (abort.aborted) throw err;
3181
+ const detail = err instanceof Error ? err.message : String(err);
3182
+ throw Object.assign(/* @__PURE__ */ new Error(`ollama native: network failure: ${detail}`), { kind: "network" });
3183
+ }
3184
+ /**
3185
+ * Drain a streaming /api/chat response: decode bytes, reassemble NDJSON
3186
+ * lines, and feed each to the aggregator, which dispatches chunks through
3187
+ * req.dispatch_chunk as they arrive. Transport failures mid-read wrap as
3188
+ * network errors; aggregator throws (malformed frames, a rejecting on_chunk)
3189
+ * pass through untouched, with the reader cancelled so the connection is
3190
+ * released.
3191
+ */
3192
+ async function consume_ndjson_response(response, req) {
3193
+ const body = response.body;
3194
+ if (body === null) throw new provider_error("ollama native: streaming response has no body");
3195
+ const dispatch = req.dispatch_chunk ?? (async () => {});
3196
+ const aggregator = create_ollama_stream_aggregator(req.step_index, dispatch);
3197
+ const reader = body.getReader();
3198
+ const text_decoder = new TextDecoder();
3199
+ const ndjson = create_ndjson_decoder();
3200
+ const next_bytes = async () => {
3201
+ let step;
3202
+ try {
3203
+ step = await reader.read();
3204
+ } catch (err) {
3205
+ rethrow_network_failure(err, req.abort);
3206
+ }
3207
+ return step.done ? void 0 : step.value;
3208
+ };
3209
+ try {
3210
+ while (true) {
3211
+ const bytes = await next_bytes();
3212
+ if (bytes === void 0) break;
3213
+ for (const line of ndjson.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle_line(line);
3214
+ }
3215
+ } finally {
3216
+ reader.cancel().catch(() => {});
3217
+ }
3218
+ const tail = [...ndjson.push(text_decoder.decode()), ...ndjson.flush()];
3219
+ for (const line of tail) await aggregator.handle_line(line);
3220
+ return aggregator.complete();
1630
3221
  }
1631
3222
  const SUPPORTED$5 = /* @__PURE__ */ new Set([
1632
3223
  "text",
1633
3224
  "tools",
1634
3225
  "schema",
1635
- "streaming",
1636
- "image_input",
1637
- "reasoning"
3226
+ "streaming"
1638
3227
  ]);
1639
- const create_google_adapter = (init) => {
1640
- const api_key = typeof init.api_key === "string" ? init.api_key : "";
1641
- if (api_key.length === 0) throw new engine_config_error("google provider requires a non-empty api_key", "google");
1642
- const base_url = typeof init.base_url === "string" ? init.base_url : void 0;
3228
+ const create_ollama_native_adapter = (init) => {
3229
+ const raw_base_url = typeof init.base_url === "string" ? init.base_url : "";
3230
+ if (raw_base_url.length === 0) throw new engine_config_error("ollama provider requires a non-empty base_url", "ollama");
3231
+ const base_url = raw_base_url.replace(/\/+$/, "");
1643
3232
  return {
1644
- kind: "ai_sdk",
1645
- name: "google",
1646
- async build_model(model_id) {
1647
- const sdk = await load_optional_peer("@ai-sdk/google");
1648
- const config = { apiKey: api_key };
1649
- if (base_url !== void 0) config.baseURL = base_url;
1650
- return sdk.createGoogleGenerativeAI(config)(model_id);
3233
+ kind: "native",
3234
+ name: "ollama",
3235
+ async invoke_turn(req) {
3236
+ let response;
3237
+ try {
3238
+ response = await fetch(`${base_url}/api/chat`, {
3239
+ method: "POST",
3240
+ headers: { "content-type": "application/json" },
3241
+ body: JSON.stringify(build_ollama_chat_body(req)),
3242
+ signal: req.abort
3243
+ });
3244
+ } catch (err) {
3245
+ rethrow_network_failure(err, req.abort);
3246
+ }
3247
+ if (!response.ok) throw await response_error(response);
3248
+ if (req.stream) return consume_ndjson_response(response, req);
3249
+ return parse_ollama_chat(await response.json(), req.step_index);
1651
3250
  },
1652
- translate_effort: translate_google_effort,
1653
- normalize_usage: normalize_google_usage,
1654
3251
  supports: (capability) => SUPPORTED$5.has(capability)
1655
3252
  };
1656
3253
  };
@@ -1677,6 +3274,10 @@ const SUPPORTED$4 = /* @__PURE__ */ new Set([
1677
3274
  "structured_output"
1678
3275
  ]);
1679
3276
  const create_ollama_adapter = (init) => {
3277
+ if (resolve_transport(init, "ollama") === "native") return create_ollama_native_adapter(init);
3278
+ return create_ollama_ai_sdk_adapter(init);
3279
+ };
3280
+ const create_ollama_ai_sdk_adapter = (init) => {
1680
3281
  const base_url = typeof init.base_url === "string" ? init.base_url : "";
1681
3282
  if (base_url.length === 0) throw new engine_config_error("ollama provider requires a non-empty base_url", "ollama");
1682
3283
  return {
@@ -1713,6 +3314,28 @@ const SUPPORTED$3 = /* @__PURE__ */ new Set([
1713
3314
  "structured_output"
1714
3315
  ]);
1715
3316
  const create_lmstudio_adapter = (init) => {
3317
+ if (resolve_transport(init, "lmstudio") === "native") return create_lmstudio_native_adapter(init);
3318
+ return create_lmstudio_ai_sdk_adapter(init);
3319
+ };
3320
+ /**
3321
+ * Build the lmstudio dialect (Appendix A1) and hand it to the shared
3322
+ * OpenAI-compatible core: no auth, the `max_tokens` token-limit field, and
3323
+ * tolerant usage (D10). The base_url guard mirrors the ai_sdk branch; the core
3324
+ * has no api_key to check under `auth: { kind: 'none' }`.
3325
+ */
3326
+ const create_lmstudio_native_adapter = (init) => {
3327
+ const base_url = typeof init.base_url === "string" ? init.base_url : "";
3328
+ if (base_url.length === 0) throw new engine_config_error("lmstudio provider requires a non-empty base_url", "lmstudio");
3329
+ return create_openai_compatible_adapter({
3330
+ name: "lmstudio",
3331
+ base_url,
3332
+ auth: { kind: "none" },
3333
+ token_limit_field: "max_tokens",
3334
+ stream_include_usage: true,
3335
+ tolerant_usage: true
3336
+ });
3337
+ };
3338
+ const create_lmstudio_ai_sdk_adapter = (init) => {
1716
3339
  const base_url = typeof init.base_url === "string" ? init.base_url : "";
1717
3340
  if (base_url.length === 0) throw new engine_config_error("lmstudio provider requires a non-empty base_url", "lmstudio");
1718
3341
  return {
@@ -1753,7 +3376,45 @@ const SUPPORTED$2 = /* @__PURE__ */ new Set([
1753
3376
  "image_input",
1754
3377
  "reasoning"
1755
3378
  ]);
3379
+ /**
3380
+ * OpenRouter's default API origin, matching the @openrouter/ai-sdk-provider
3381
+ * baseURL convention so a base_url configured for the ai_sdk transport keeps
3382
+ * pointing at the same place when the transport flips to native.
3383
+ */
3384
+ const DEFAULT_BASE_URL = "https://openrouter.ai/api/v1";
1756
3385
  const create_openrouter_adapter = (init) => {
3386
+ if (resolve_transport(init, "openrouter") === "native") return create_openrouter_native_adapter(init);
3387
+ return create_openrouter_ai_sdk_adapter(init);
3388
+ };
3389
+ /**
3390
+ * Build the openrouter dialect (Appendix A1) and hand it to the shared
3391
+ * OpenAI-compatible core: Bearer auth, the optional `HTTP-Referer`/`X-Title`
3392
+ * attribution headers, the `max_tokens` token-limit field, and strict usage.
3393
+ * The empty-api_key guard rides on the core, which throws the same
3394
+ * `openrouter provider requires a non-empty api_key` engine_config_error.
3395
+ */
3396
+ const create_openrouter_native_adapter = (init) => {
3397
+ const api_key = typeof init.api_key === "string" ? init.api_key : "";
3398
+ const base_url = typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : DEFAULT_BASE_URL;
3399
+ const http_referer = typeof init["http_referer"] === "string" ? init["http_referer"] : void 0;
3400
+ const x_title = typeof init["x_title"] === "string" ? init["x_title"] : void 0;
3401
+ const extra_headers = {};
3402
+ if (http_referer !== void 0) extra_headers["HTTP-Referer"] = http_referer;
3403
+ if (x_title !== void 0) extra_headers["X-Title"] = x_title;
3404
+ return create_openai_compatible_adapter({
3405
+ name: "openrouter",
3406
+ base_url,
3407
+ auth: {
3408
+ kind: "bearer",
3409
+ api_key
3410
+ },
3411
+ token_limit_field: "max_tokens",
3412
+ stream_include_usage: true,
3413
+ tolerant_usage: false,
3414
+ ...Object.keys(extra_headers).length > 0 ? { extra_headers } : {}
3415
+ });
3416
+ };
3417
+ const create_openrouter_ai_sdk_adapter = (init) => {
1757
3418
  const api_key = typeof init.api_key === "string" ? init.api_key : "";
1758
3419
  if (api_key.length === 0) throw new engine_config_error("openrouter provider requires a non-empty api_key", "openrouter");
1759
3420
  const base_url = typeof init.base_url === "string" ? init.base_url : void 0;
@@ -3158,12 +4819,12 @@ function create_option_ignored_dedup(trajectory) {
3158
4819
  * claude_cli adapter factory (spec §5.5, §6, §8, §13).
3159
4820
  *
3160
4821
  * Exports a `create_claude_cli_adapter(init)` factory that returns a
3161
- * `SubprocessProviderAdapter`. The factory closure-captures a per-instance
4822
+ * `ExternalAgentAdapter`. The factory closure-captures a per-instance
3162
4823
  * `spawn_runtime` (which owns its own `Set<ChildProcess>` live registry), so
3163
4824
  * two factory invocations produce two independent registries (constraints §7
3164
4825
  * invariant 5: no module-level mutable state).
3165
4826
  *
3166
- * The returned adapter exposes exactly `{ kind: 'subprocess', name, generate,
4827
+ * The returned adapter exposes exactly `{ kind: 'external', name, generate,
3167
4828
  * dispose, supports }` — no `build_model`, `translate_effort`, or
3168
4829
  * `normalize_usage` members.
3169
4830
  */
@@ -3282,7 +4943,7 @@ function create_claude_cli_adapter(init) {
3282
4943
  const resolved_startup_timeout = config.startup_timeout_ms ?? 12e4;
3283
4944
  const resolved_stall_timeout = config.stall_timeout_ms ?? 3e5;
3284
4945
  return {
3285
- kind: "subprocess",
4946
+ kind: "external",
3286
4947
  name: PROVIDER_NAME,
3287
4948
  supports: (capability) => SUPPORTED.has(capability),
3288
4949
  async generate(opts, resolved) {
@@ -3453,11 +5114,12 @@ function create_claude_cli_adapter(init) {
3453
5114
  //#endregion
3454
5115
  //#region src/engine/providers/registry.ts
3455
5116
  /**
3456
- * Provider name → adapter factory lookup.
5117
+ * Provider name → adapter factory lookup for the built-in set.
3457
5118
  *
3458
- * Unknown provider names throw provider_not_configured_error. The registry is
3459
- * a frozen Map; runtime provider registration beyond the built-in set is
3460
- * deferred (spec §5.9).
5119
+ * Unknown provider names throw provider_not_configured_error. Custom
5120
+ * providers enter via `EngineConfig.custom_providers` at construction,
5121
+ * resolved custom-first in create_engine; runtime (post-construction)
5122
+ * registration stays deferred.
3461
5123
  */
3462
5124
  const BUILTIN_PROVIDERS = /* @__PURE__ */ new Map([
3463
5125
  ["anthropic", create_anthropic_adapter],
@@ -3469,6 +5131,9 @@ const BUILTIN_PROVIDERS = /* @__PURE__ */ new Map([
3469
5131
  ["bedrock", create_bedrock_adapter],
3470
5132
  ["claude_cli", create_claude_cli_adapter]
3471
5133
  ]);
5134
+ function list_builtin_providers() {
5135
+ return [...BUILTIN_PROVIDERS.keys()];
5136
+ }
3472
5137
  function get_provider_factory(name) {
3473
5138
  const factory = BUILTIN_PROVIDERS.get(name);
3474
5139
  if (factory === void 0) throw new provider_not_configured_error(name);
@@ -3993,6 +5658,30 @@ function validate_tool_input(tool, input) {
3993
5658
  message: `invalid tool input: ${serialize_error(parsed.error)}`
3994
5659
  };
3995
5660
  }
5661
+ /**
5662
+ * Apply the prepare_step hook (D6) for one turn. Returns the messages to send
5663
+ * to the transport: the hook's replacement when it returns `{ messages }`, else
5664
+ * config.messages unchanged. The replacement is ephemeral — it is fed ONLY to
5665
+ * invoke_once for this turn, never pushed onto config.messages, so the loop's
5666
+ * salvage/approval/ends_turn/schema-repair machinery keeps reading the real
5667
+ * transcript. A step_prepared event (recorded inline, as request_sent is) makes
5668
+ * the mid-loop mutation legible with the before/after message counts.
5669
+ */
5670
+ async function apply_prepare_step(config, step_index) {
5671
+ if (config.prepare_step === void 0) return config.messages;
5672
+ const replacement = (await config.prepare_step({
5673
+ step_index,
5674
+ messages: config.messages
5675
+ }))?.messages;
5676
+ if (replacement === void 0) return config.messages;
5677
+ config.trajectory?.record({
5678
+ kind: "step_prepared",
5679
+ step_index,
5680
+ message_count_before: config.messages.length,
5681
+ message_count_after: replacement.length
5682
+ });
5683
+ return replacement;
5684
+ }
3996
5685
  async function run_tool_loop(config) {
3997
5686
  const steps = [];
3998
5687
  const tool_calls_all = [];
@@ -4012,13 +5701,14 @@ async function run_tool_loop(config) {
4012
5701
  const step_span = start_step_span(config.trajectory, step_index);
4013
5702
  let turn;
4014
5703
  try {
5704
+ const request_messages = await apply_prepare_step(config, step_index);
4015
5705
  config.trajectory?.record({
4016
5706
  kind: "request_sent",
4017
5707
  step_index
4018
5708
  });
4019
5709
  turn = await config.invoke_once({
4020
5710
  step_index,
4021
- messages: config.messages,
5711
+ messages: request_messages,
4022
5712
  tools: config.tools,
4023
5713
  abort: config.abort,
4024
5714
  stream: config.stream
@@ -4390,31 +6080,34 @@ async function run_tool_loop(config) {
4390
6080
  };
4391
6081
  }
4392
6082
  //#endregion
4393
- //#region src/engine/generate.ts
6083
+ //#region src/engine/providers/ai_sdk/telemetry.ts
6084
+ async function build_ai_sdk_telemetry(settings) {
6085
+ if (!settings?.enabled) return void 0;
6086
+ const out = {
6087
+ isEnabled: true,
6088
+ integrations: [new (await (load_optional_peer("@ai-sdk/otel"))).OpenTelemetry(settings.tracer !== void 0 ? { tracer: settings.tracer } : {})]
6089
+ };
6090
+ if (settings.function_id !== void 0) out.functionId = settings.function_id;
6091
+ if (settings.record_inputs !== void 0) out.recordInputs = settings.record_inputs;
6092
+ if (settings.record_outputs !== void 0) out.recordOutputs = settings.record_outputs;
6093
+ if (settings.metadata !== void 0) out.metadata = settings.metadata;
6094
+ return out;
6095
+ }
6096
+ //#endregion
6097
+ //#region src/engine/providers/ai_sdk/invoke.ts
4394
6098
  /**
4395
- * generate(opts) — single public entry point for model calls.
6099
+ * The ai_sdk transport: the engine's only Vercel AI SDK call site.
6100
+ *
6101
+ * Invariant 13 (constraints §7, inverted): only this module imports from `ai`
6102
+ * or invokes generateText / streamText. generate.ts drives this transport
6103
+ * through create_ai_sdk_turn and stays SDK-agnostic; run_tool_loop sees the
6104
+ * same neutral TurnResult a native adapter produces. Rule-enforced by
6105
+ * rules/no-ai-import-outside-ai-sdk-provider.yml.
4396
6106
  *
4397
- * This file owns the `ai` SDK boundary for the engine: only generate.ts,
4398
- * tool_loop.ts, and index.ts are permitted to call generateText / streamText
4399
- * directly (constraints §7 invariant 13). tool_loop.ts consumes the InvokeOnce
4400
- * seam built here.
6107
+ * Retry stays out of this module on purpose (D5): generate.ts wraps the turn
6108
+ * returned here in the engine-owned retry_turn, exactly as it wraps a native
6109
+ * adapter's invoke_turn. This module owns request/response mapping only.
4401
6110
  */
4402
- function build_initial_messages(opts) {
4403
- const messages = [];
4404
- if (typeof opts.system === "string" && opts.system.length > 0) messages.push({
4405
- role: "system",
4406
- content: opts.system
4407
- });
4408
- if (typeof opts.prompt === "string") {
4409
- messages.push({
4410
- role: "user",
4411
- content: opts.prompt
4412
- });
4413
- return messages;
4414
- }
4415
- for (const m of opts.prompt) messages.push({ ...m });
4416
- return messages;
4417
- }
4418
6111
  function map_finish_reason(raw) {
4419
6112
  switch (raw) {
4420
6113
  case "stop": return "stop";
@@ -4706,7 +6399,108 @@ function recover_no_object_generated(err, adapter) {
4706
6399
  usage: adapter.normalize_usage(raw_usage)
4707
6400
  };
4708
6401
  }
4709
- function classify_ai_sdk_error(err) {
6402
+ /**
6403
+ * Build the single-attempt SDK turn for one generate call: the
6404
+ * generateText/streamText call body behind the same neutral seam a native
6405
+ * adapter's invoke_turn implements. The built model is memoized across steps
6406
+ * of one generate call. No retry here: generate.ts wraps the returned turn in
6407
+ * the engine-owned retry_turn.
6408
+ */
6409
+ function create_ai_sdk_turn(cfg) {
6410
+ let model_instance;
6411
+ const get_model = async () => {
6412
+ if (model_instance === void 0) model_instance = await cfg.adapter.build_model(cfg.model_id);
6413
+ return model_instance;
6414
+ };
6415
+ let telemetry_options;
6416
+ let telemetry_resolved = false;
6417
+ const get_telemetry = async () => {
6418
+ if (!telemetry_resolved) {
6419
+ telemetry_options = await build_ai_sdk_telemetry(cfg.telemetry);
6420
+ telemetry_resolved = true;
6421
+ }
6422
+ return telemetry_options;
6423
+ };
6424
+ const sdk_tools = to_sdk_tools(cfg.tools);
6425
+ let output_spec;
6426
+ if (cfg.schema !== void 0 && cfg.tools.length === 0 && cfg.adapter.supports("structured_output")) output_spec = Output.object({ schema: cfg.schema });
6427
+ const provider_options = cfg.provider_options;
6428
+ return async (args) => {
6429
+ const model = await get_model();
6430
+ const telemetry = await get_telemetry();
6431
+ const internal_controller = new AbortController();
6432
+ const cancel_on_user_abort = () => {
6433
+ internal_controller.abort(args.abort.reason);
6434
+ };
6435
+ if (args.abort.aborted) internal_controller.abort(args.abort.reason);
6436
+ else args.abort.addEventListener("abort", cancel_on_user_abort, { once: true });
6437
+ const { system: hoisted_system, messages: sdk_messages } = split_leading_system(to_sdk_messages(args.messages));
6438
+ const base_params = {
6439
+ model,
6440
+ messages: sdk_messages,
6441
+ abortSignal: internal_controller.signal,
6442
+ stopWhen: isStepCount(1),
6443
+ maxRetries: 0
6444
+ };
6445
+ if (hoisted_system !== void 0) base_params.instructions = hoisted_system;
6446
+ if (sdk_tools !== void 0) base_params.tools = sdk_tools;
6447
+ if (output_spec !== void 0) base_params.output = output_spec;
6448
+ if (provider_options !== void 0) base_params.providerOptions = provider_options;
6449
+ if (cfg.temperature !== void 0) base_params.temperature = cfg.temperature;
6450
+ if (cfg.max_tokens !== void 0) base_params.maxOutputTokens = cfg.max_tokens;
6451
+ if (cfg.top_p !== void 0) base_params.topP = cfg.top_p;
6452
+ if (telemetry !== void 0) base_params.experimental_telemetry = telemetry;
6453
+ try {
6454
+ if (args.stream) return await collect_stream(base_params, args.step_index, cfg.dispatcher, args.on_first_chunk, internal_controller, cfg.adapter);
6455
+ return await collect_non_stream(base_params, cfg.adapter);
6456
+ } finally {
6457
+ args.abort.removeEventListener("abort", cancel_on_user_abort);
6458
+ }
6459
+ };
6460
+ }
6461
+ //#endregion
6462
+ //#region src/engine/generate.ts
6463
+ function build_initial_messages(opts) {
6464
+ const messages = [];
6465
+ if (typeof opts.system === "string" && opts.system.length > 0) messages.push({
6466
+ role: "system",
6467
+ content: opts.system
6468
+ });
6469
+ if (typeof opts.prompt === "string") {
6470
+ messages.push({
6471
+ role: "user",
6472
+ content: opts.prompt
6473
+ });
6474
+ return messages;
6475
+ }
6476
+ for (const m of opts.prompt) messages.push({ ...m });
6477
+ return messages;
6478
+ }
6479
+ /**
6480
+ * The engine-Message analog of the SDK-side split_leading_system
6481
+ * (providers/ai_sdk/invoke.ts): build_native_invoke hoists the leading system
6482
+ * run into TurnRequest.system so a native adapter maps conversation messages
6483
+ * only. Same guards as the SDK variant: only the leading run is hoisted, and
6484
+ * the original list is returned untouched when hoisting would leave `messages`
6485
+ * empty (provider APIs reject an empty messages array).
6486
+ */
6487
+ function split_leading_system_messages(messages) {
6488
+ let run_end = 0;
6489
+ const system_parts = [];
6490
+ while (run_end < messages.length) {
6491
+ const m = messages[run_end];
6492
+ if (m?.role !== "system") break;
6493
+ system_parts.push(m.content);
6494
+ run_end += 1;
6495
+ }
6496
+ const rest = messages.slice(run_end);
6497
+ if (system_parts.length === 0 || rest.length === 0) return { messages: [...messages] };
6498
+ return {
6499
+ system: system_parts.join("\n\n"),
6500
+ messages: rest
6501
+ };
6502
+ }
6503
+ function classify_provider_error(err) {
4710
6504
  if (err === null || typeof err !== "object") return err;
4711
6505
  const existing_kind = Reflect.get(err, "kind");
4712
6506
  if (typeof existing_kind === "string" && (existing_kind === "rate_limit" || existing_kind === "provider_5xx" || existing_kind === "network" || existing_kind === "timeout")) return err;
@@ -4747,6 +6541,148 @@ function classify_ai_sdk_error(err) {
4747
6541
  }
4748
6542
  return err;
4749
6543
  }
6544
+ /**
6545
+ * Compose the per-attempt turn signal (D5): the user's abort OR'd with a fresh
6546
+ * `turn_timeout_ms` deadline via AbortSignal.any (the src/core/timeout.ts
6547
+ * precedent). `timed_out()` distinguishes an expiry (retryable) from a user
6548
+ * abort (terminal) in the retry_turn ladder; `dispose()` clears the timer so a
6549
+ * settled attempt never leaves one armed. With no budget the user's abort
6550
+ * passes through untouched, so the un-timed path stays byte-for-byte as before.
6551
+ * Armed fresh inside retry_with_policy's callback so each retry gets its own
6552
+ * full budget rather than sharing one deadline across attempts.
6553
+ */
6554
+ function arm_turn_timeout(user_abort, turn_timeout_ms) {
6555
+ if (turn_timeout_ms === void 0) return {
6556
+ signal: user_abort,
6557
+ timed_out: () => false,
6558
+ dispose: () => {}
6559
+ };
6560
+ const local = new AbortController();
6561
+ const composed = AbortSignal.any([user_abort, local.signal]);
6562
+ let timed_out = false;
6563
+ const timer = setTimeout(() => {
6564
+ timed_out = true;
6565
+ local.abort();
6566
+ }, turn_timeout_ms);
6567
+ return {
6568
+ signal: composed,
6569
+ timed_out: () => timed_out,
6570
+ dispose: () => {
6571
+ clearTimeout(timer);
6572
+ }
6573
+ };
6574
+ }
6575
+ /**
6576
+ * Engine-owned wrapper shared verbatim by both depth-1 transports: one turn
6577
+ * attempt inside retry_with_policy. The catch ladder classifies by CAUSE, not
6578
+ * by the shape the transport happened to throw: on_chunk_error passes through,
6579
+ * then a genuine user abort wins, then any error once a chunk has flowed is a
6580
+ * non-retryable stream interruption, then a pre-chunk `turn_timeout_ms` expiry
6581
+ * is a retryable typed timeout, and only a below-loop aborted_error unrelated
6582
+ * to either passes through. Cause-first ordering is what keeps the two
6583
+ * transports in parity: the ai_sdk transport reports a mid-stream abort as an
6584
+ * aborted_error and the native one as a raw AbortError, so both must be read as
6585
+ * the same interruption rather than the ai_sdk timeout masquerading as a user
6586
+ * cancel. call_once receives the composed abort+timeout signal so the deadline
6587
+ * actually cancels the in-flight request. Adapters may swap `classify`, never
6588
+ * the ladder (D5: the engine owns retry; hidden adapter retries are illegible).
6589
+ */
6590
+ function retry_turn(call_once, args, has_streamed, retry_policy, classify, turn_timeout_ms) {
6591
+ return retry_with_policy(async () => {
6592
+ const deadline = arm_turn_timeout(args.abort, turn_timeout_ms);
6593
+ try {
6594
+ return await call_once(deadline.signal);
6595
+ } catch (err) {
6596
+ if (err instanceof on_chunk_error) throw err;
6597
+ if (args.abort.aborted) throw new aborted_error("aborted", {
6598
+ reason: args.abort.reason,
6599
+ step_index: args.step_index
6600
+ });
6601
+ if (has_streamed()) throw new provider_error(`stream interrupted: ${err instanceof Error ? err.message : String(err)}`, { cause_kind: "unknown" });
6602
+ if (deadline.timed_out()) throw new turn_timeout_error(turn_timeout_ms ?? 0, args.step_index);
6603
+ if (err instanceof aborted_error) throw err;
6604
+ throw classify(err);
6605
+ } finally {
6606
+ deadline.dispose();
6607
+ }
6608
+ }, retry_policy, args.abort);
6609
+ }
6610
+ /**
6611
+ * Build the ai_sdk-transport InvokeOnce: the SDK turn built by
6612
+ * providers/ai_sdk/invoke.ts behind the same engine-owned retry_turn wrapper
6613
+ * the native path uses. The SDK call body lives behind the seam; this builder
6614
+ * owns only retry and the streamed-output tracking that makes a mid-stream
6615
+ * failure a non-retryable interruption.
6616
+ */
6617
+ function build_ai_sdk_invoke(cfg) {
6618
+ return async (args) => {
6619
+ let chunks_started = false;
6620
+ const call_once = (turn_abort) => cfg.invoke_turn({
6621
+ step_index: args.step_index,
6622
+ messages: args.messages,
6623
+ abort: turn_abort,
6624
+ stream: args.stream,
6625
+ on_first_chunk: () => {
6626
+ chunks_started = true;
6627
+ }
6628
+ });
6629
+ return await retry_turn(call_once, args, () => chunks_started, cfg.retry_policy, classify_provider_error, cfg.turn_timeout_ms);
6630
+ };
6631
+ }
6632
+ /**
6633
+ * Build the native-transport InvokeOnce: maps the loop's InvokeOnceArgs to a
6634
+ * TurnRequest and calls adapter.invoke_turn inside the same retry_turn
6635
+ * wrapper as the ai_sdk path. The adapter sees a child abort signal so a
6636
+ * throwing on_chunk consumer cancels the in-flight request, and its chunk
6637
+ * emissions flow through the shared dispatcher, which is what lets
6638
+ * run_tool_loop treat both transports identically.
6639
+ */
6640
+ function build_native_invoke(cfg) {
6641
+ const classify = cfg.adapter.classify_error ?? classify_provider_error;
6642
+ return async (args) => {
6643
+ let chunks_started = false;
6644
+ const call_once = async (turn_abort) => {
6645
+ const internal_controller = new AbortController();
6646
+ const cancel_on_turn_abort = () => {
6647
+ internal_controller.abort(turn_abort.reason);
6648
+ };
6649
+ if (turn_abort.aborted) internal_controller.abort(turn_abort.reason);
6650
+ else turn_abort.addEventListener("abort", cancel_on_turn_abort, { once: true });
6651
+ const dispatch_chunk = async (chunk) => {
6652
+ chunks_started = true;
6653
+ try {
6654
+ await cfg.dispatcher.dispatch(chunk);
6655
+ } catch (err) {
6656
+ internal_controller.abort();
6657
+ throw err;
6658
+ }
6659
+ };
6660
+ const { system, messages } = split_leading_system_messages(args.messages);
6661
+ const req = {
6662
+ step_index: args.step_index,
6663
+ messages,
6664
+ tools: args.tools,
6665
+ abort: internal_controller.signal,
6666
+ stream: args.stream,
6667
+ model_id: cfg.model_id,
6668
+ effort: cfg.effort,
6669
+ ...system !== void 0 ? { system } : {},
6670
+ ...cfg.schema !== void 0 ? { schema: cfg.schema } : {},
6671
+ ...cfg.provider_options !== void 0 ? { provider_options: cfg.provider_options } : {},
6672
+ ...cfg.temperature !== void 0 ? { temperature: cfg.temperature } : {},
6673
+ ...cfg.max_tokens !== void 0 ? { max_tokens: cfg.max_tokens } : {},
6674
+ ...cfg.top_p !== void 0 ? { top_p: cfg.top_p } : {},
6675
+ ...args.stream ? { dispatch_chunk } : {}
6676
+ };
6677
+ try {
6678
+ return await cfg.adapter.invoke_turn(req);
6679
+ } finally {
6680
+ turn_abort.removeEventListener("abort", cancel_on_turn_abort);
6681
+ }
6682
+ };
6683
+ return await retry_turn(call_once, args, () => chunks_started, cfg.retry_policy, classify, cfg.turn_timeout_ms);
6684
+ };
6685
+ }
4750
6686
  async function generate(opts_in, engine) {
4751
6687
  if (opts_in.abort?.aborted === true) throw new aborted_error("aborted", { reason: opts_in.abort.reason });
4752
6688
  const resolved_model = opts_in.model ?? engine.default_model;
@@ -4767,7 +6703,7 @@ async function generate(opts_in, engine) {
4767
6703
  };
4768
6704
  const adapter = engine.adapters.get(target.provider);
4769
6705
  if (adapter === void 0) throw new provider_not_configured_error(target.provider);
4770
- if (adapter.kind === "subprocess") return adapter.generate(opts, target);
6706
+ if (adapter.kind === "external") return adapter.generate(opts, target);
4771
6707
  const trajectory = with_timestamps(opts.trajectory);
4772
6708
  const on_chunk_provided = opts.on_chunk !== void 0;
4773
6709
  const tools_list = opts.tools ?? [];
@@ -4775,10 +6711,45 @@ async function generate(opts_in, engine) {
4775
6711
  if (tools_list.length > 0 && !adapter.supports("tools")) throw new provider_capability_error(target.provider, "tools");
4776
6712
  if (on_chunk_provided && !adapter.supports("streaming")) throw new provider_capability_error(target.provider, "streaming");
4777
6713
  const effort = opts.effort ?? engine.default_effort;
4778
- const effort_translation = adapter.translate_effort(effort);
4779
- if (effort !== "none" && effort_translation.effort_ignored) record_effort_ignored(trajectory, target.model_id);
4780
- const combined_provider_options = merge_provider_options(effort_translation.provider_options, opts.provider_options);
4781
- const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
6714
+ const retry_policy = opts.retry ?? engine.default_retry;
6715
+ const turn_timeout_ms = opts.turn_timeout_ms ?? engine.default_turn_timeout_ms;
6716
+ if (turn_timeout_ms !== void 0 && turn_timeout_ms <= 0) throw new engine_config_error("turn_timeout_ms must be > 0");
6717
+ const dispatcher = create_chunk_dispatcher(opts.on_chunk);
6718
+ let invoke_once;
6719
+ if (adapter.kind === "ai_sdk") {
6720
+ const effort_translation = adapter.translate_effort(effort);
6721
+ if (effort !== "none" && effort_translation.effort_ignored) record_effort_ignored(trajectory, target.model_id);
6722
+ const combined_provider_options = merge_provider_options(effort_translation.provider_options, opts.provider_options);
6723
+ const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
6724
+ invoke_once = build_ai_sdk_invoke({
6725
+ invoke_turn: create_ai_sdk_turn({
6726
+ adapter,
6727
+ model_id: target.model_id,
6728
+ dispatcher,
6729
+ tools: tools_list,
6730
+ schema: opts.schema,
6731
+ provider_options,
6732
+ temperature: opts.temperature,
6733
+ max_tokens: opts.max_tokens,
6734
+ top_p: opts.top_p,
6735
+ telemetry: engine.default_ai_sdk_telemetry
6736
+ }),
6737
+ retry_policy,
6738
+ turn_timeout_ms
6739
+ });
6740
+ } else invoke_once = build_native_invoke({
6741
+ adapter,
6742
+ model_id: target.model_id,
6743
+ retry_policy,
6744
+ turn_timeout_ms,
6745
+ dispatcher,
6746
+ effort,
6747
+ schema: opts.schema,
6748
+ provider_options: opts.provider_options,
6749
+ temperature: opts.temperature,
6750
+ max_tokens: opts.max_tokens,
6751
+ top_p: opts.top_p
6752
+ });
4782
6753
  const abort = opts.abort ?? new AbortController().signal;
4783
6754
  const max_steps = opts.max_steps ?? engine.default_max_steps;
4784
6755
  const tool_error_policy = opts.tool_error_policy ?? engine.default_tool_error_policy ?? "feed_back";
@@ -4787,7 +6758,6 @@ async function generate(opts_in, engine) {
4787
6758
  if (tool_call_repair_attempts < 0) throw new engine_config_error("tool_call_repair_attempts must be >= 0");
4788
6759
  const max_tool_calls_per_step = opts.max_tool_calls_per_step ?? engine.default_max_tool_calls_per_step;
4789
6760
  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");
4790
- const retry_policy = opts.retry ?? engine.default_retry;
4791
6761
  const schema_prefix = opts.schema !== void 0 ? "You must respond with a single JSON value that conforms to the expected schema. Return ONLY the JSON value, with no markdown or commentary." : void 0;
4792
6762
  const initial_messages = build_initial_messages(opts);
4793
6763
  if (schema_prefix !== void 0) {
@@ -4812,66 +6782,9 @@ async function generate(opts_in, engine) {
4812
6782
  streaming: on_chunk_provided
4813
6783
  });
4814
6784
  const pricing_dedup = create_pricing_missing_dedup(trajectory);
4815
- const dispatcher = create_chunk_dispatcher(opts.on_chunk);
4816
6785
  const resolve_pricing = () => {
4817
6786
  return engine.pricing[pricing_key(target.provider, target.model_id)];
4818
6787
  };
4819
- let model_instance;
4820
- const get_model = async () => {
4821
- if (model_instance === void 0) model_instance = await adapter.build_model(target.model_id);
4822
- return model_instance;
4823
- };
4824
- const sdk_tools = to_sdk_tools(tools_list);
4825
- const output_spec = opts.schema !== void 0 && tools_list.length === 0 && adapter.supports("structured_output") ? Output.object({ schema: opts.schema }) : void 0;
4826
- const invoke_once = async (args) => {
4827
- let chunks_started = false;
4828
- const call_once = async () => {
4829
- const model = await get_model();
4830
- const internal_controller = new AbortController();
4831
- const cancel_on_user_abort = () => {
4832
- internal_controller.abort(args.abort.reason);
4833
- };
4834
- if (args.abort.aborted) internal_controller.abort(args.abort.reason);
4835
- else args.abort.addEventListener("abort", cancel_on_user_abort, { once: true });
4836
- const { system: hoisted_system, messages: sdk_messages } = split_leading_system(to_sdk_messages(args.messages));
4837
- const base_params = {
4838
- model,
4839
- messages: sdk_messages,
4840
- abortSignal: internal_controller.signal,
4841
- stopWhen: isStepCount(1),
4842
- maxRetries: 0
4843
- };
4844
- if (hoisted_system !== void 0) base_params.instructions = hoisted_system;
4845
- if (sdk_tools !== void 0) base_params.tools = sdk_tools;
4846
- if (output_spec !== void 0) base_params.output = output_spec;
4847
- if (provider_options !== void 0) base_params.providerOptions = provider_options;
4848
- if (opts.temperature !== void 0) base_params.temperature = opts.temperature;
4849
- if (opts.max_tokens !== void 0) base_params.maxOutputTokens = opts.max_tokens;
4850
- if (opts.top_p !== void 0) base_params.topP = opts.top_p;
4851
- try {
4852
- if (args.stream) return await collect_stream(base_params, args.step_index, dispatcher, () => {
4853
- chunks_started = true;
4854
- }, internal_controller, adapter);
4855
- return await collect_non_stream(base_params, adapter);
4856
- } finally {
4857
- args.abort.removeEventListener("abort", cancel_on_user_abort);
4858
- }
4859
- };
4860
- return await retry_with_policy(async () => {
4861
- try {
4862
- return await call_once();
4863
- } catch (err) {
4864
- if (err instanceof on_chunk_error) throw err;
4865
- if (err instanceof aborted_error) throw err;
4866
- if (args.abort.aborted) throw new aborted_error("aborted", {
4867
- reason: args.abort.reason,
4868
- step_index: args.step_index
4869
- });
4870
- if (chunks_started) throw new provider_error(`stream interrupted: ${err instanceof Error ? err.message : String(err)}`, { cause_kind: "unknown" });
4871
- throw classify_ai_sdk_error(err);
4872
- }
4873
- }, retry_policy, args.abort);
4874
- };
4875
6788
  const dispatch_chunk = on_chunk_provided ? async (chunk) => {
4876
6789
  await dispatcher.dispatch(chunk);
4877
6790
  } : void 0;
@@ -4904,7 +6817,8 @@ async function generate(opts_in, engine) {
4904
6817
  resolve_pricing,
4905
6818
  pricing_dedup,
4906
6819
  ...salvage_budget !== void 0 ? { salvage_budget } : {},
4907
- ...max_tool_calls_per_step !== void 0 ? { max_tool_calls_per_step } : {}
6820
+ ...max_tool_calls_per_step !== void 0 ? { max_tool_calls_per_step } : {},
6821
+ ...opts.prepare_step !== void 0 ? { prepare_step: opts.prepare_step } : {}
4908
6822
  });
4909
6823
  for (const s of loop_result.steps) steps_accum.push(s);
4910
6824
  for (const tc of loop_result.tool_calls) tool_calls_accum.push(tc);
@@ -5019,18 +6933,22 @@ function aggregate_cost(steps, provider) {
5019
6933
  }
5020
6934
  //#endregion
5021
6935
  //#region src/engine/create_engine.ts
5022
- function build_provider_adapters(providers) {
6936
+ function build_provider_adapters(providers, custom_providers) {
6937
+ if (custom_providers !== void 0) {
6938
+ const builtins = new Set(list_builtin_providers());
6939
+ for (const name of Object.keys(custom_providers)) if (builtins.has(name)) throw new engine_config_error(`custom_providers must not shadow built-in provider '${name}'`, name);
6940
+ }
5023
6941
  const adapters = /* @__PURE__ */ new Map();
5024
6942
  for (const [name, init] of Object.entries(providers)) {
5025
6943
  if (init === void 0) continue;
5026
- const adapter = get_provider_factory(name)(init);
6944
+ const adapter = ((custom_providers !== void 0 && Object.hasOwn(custom_providers, name) ? custom_providers[name] : void 0) ?? get_provider_factory(name))(init);
5027
6945
  adapters.set(name, adapter);
5028
6946
  }
5029
6947
  return adapters;
5030
6948
  }
5031
6949
  function create_engine(config) {
5032
6950
  if (config.providers === null || typeof config.providers !== "object") throw new engine_config_error("EngineConfig.providers is required");
5033
- const adapters = build_provider_adapters(config.providers);
6951
+ const adapters = build_provider_adapters(config.providers, config.custom_providers);
5034
6952
  const pricing = { ...DEFAULT_PRICING };
5035
6953
  if (config.pricing !== void 0) for (const [key, value] of Object.entries(config.pricing)) pricing[key] = value;
5036
6954
  const defaults = config.defaults;
@@ -5039,6 +6957,7 @@ function create_engine(config) {
5039
6957
  const default_max_steps = defaults?.max_steps ?? config.default_max_steps ?? 10;
5040
6958
  if (defaults?.tool_call_repair_attempts !== void 0 && defaults.tool_call_repair_attempts < 0) throw new engine_config_error("defaults.tool_call_repair_attempts must be >= 0");
5041
6959
  if (defaults?.max_tool_calls_per_step !== void 0 && defaults.max_tool_calls_per_step < 1) throw new engine_config_error("defaults.max_tool_calls_per_step must be >= 1");
6960
+ if (defaults?.turn_timeout_ms !== void 0 && defaults.turn_timeout_ms <= 0) throw new engine_config_error("defaults.turn_timeout_ms must be > 0");
5042
6961
  const get_internals = () => ({
5043
6962
  pricing,
5044
6963
  adapters,
@@ -5052,6 +6971,8 @@ function create_engine(config) {
5052
6971
  ...defaults?.schema_repair_attempts !== void 0 ? { default_schema_repair_attempts: defaults.schema_repair_attempts } : {},
5053
6972
  ...defaults?.tool_call_repair_attempts !== void 0 ? { default_tool_call_repair_attempts: defaults.tool_call_repair_attempts } : {},
5054
6973
  ...defaults?.max_tool_calls_per_step !== void 0 ? { default_max_tool_calls_per_step: defaults.max_tool_calls_per_step } : {},
6974
+ ...defaults?.turn_timeout_ms !== void 0 ? { default_turn_timeout_ms: defaults.turn_timeout_ms } : {},
6975
+ ...defaults?.ai_sdk_telemetry !== void 0 ? { default_ai_sdk_telemetry: defaults.ai_sdk_telemetry } : {},
5055
6976
  ...defaults?.provider_options !== void 0 ? { default_provider_options: defaults.provider_options } : {}
5056
6977
  });
5057
6978
  let disposed = false;
@@ -5070,11 +6991,25 @@ function create_engine(config) {
5070
6991
  list_prices() {
5071
6992
  return { ...pricing };
5072
6993
  },
6994
+ with_providers(providers, custom_providers) {
6995
+ const merged_custom = config.custom_providers !== void 0 || custom_providers !== void 0 ? {
6996
+ ...config.custom_providers,
6997
+ ...custom_providers
6998
+ } : void 0;
6999
+ return create_engine({
7000
+ ...config,
7001
+ providers: {
7002
+ ...config.providers,
7003
+ ...providers
7004
+ },
7005
+ ...merged_custom !== void 0 ? { custom_providers: merged_custom } : {}
7006
+ });
7007
+ },
5073
7008
  dispose() {
5074
7009
  if (dispose_promise !== void 0) return dispose_promise;
5075
7010
  disposed = true;
5076
7011
  const tasks = [];
5077
- for (const adapter of adapters.values()) if (adapter.kind === "subprocess") tasks.push(adapter.dispose());
7012
+ for (const adapter of adapters.values()) if ("dispose" in adapter && adapter.dispose !== void 0) tasks.push(adapter.dispose());
5078
7013
  dispose_promise = Promise.all(tasks).then(() => void 0);
5079
7014
  return dispose_promise;
5080
7015
  }
@@ -5777,6 +7712,6 @@ if (process.argv[1] !== void 0 && process.argv[1] === fileURLToPath(import.meta.
5777
7712
  process.exit(1);
5778
7713
  });
5779
7714
  //#endregion
5780
- export { STEP_KINDS, aborted_error, adversarial, bench, branch, checkpoint, claude_cli_error, compose, consensus, create_engine, custom_event_schema, describe, describe_cycle_error, emit_event_schema, engine_config_error, engine_disposed_error, ensemble, ensemble_step, fallback, flow_schema_default as flow_schema, forward_standard_env, improve, is_step_kind, judge_equals, judge_llm, judge_with, learn, loop, map, model_call, model_required_error, normalize_score, on_chunk_error, parallel, pipe, provider_auth_error, provider_capability_error, provider_error, provider_not_configured_error, rate_limit_error, read_baseline, regression_compare, resume_validation_error, retry, run, run_viewer_cli, schema_validation_error, scope, sequence, span_end_event_schema, span_start_event_schema, start_viewer, stash, step, suspend, suspended_error, timeout, timeout_error, tool_approval_denied_error, tool_error, tournament, trajectory_event_schema, use, write_baseline };
7715
+ export { STEP_KINDS, aborted_error, adversarial, bench, branch, checkpoint, claude_cli_error, compose, consensus, create_engine, custom_event_schema, default_normalize_usage, describe, describe_cycle_error, emit_event_schema, engine_config_error, engine_disposed_error, ensemble, ensemble_step, fallback, flow_schema_default as flow_schema, forward_standard_env, improve, is_step_kind, judge_equals, judge_llm, judge_with, learn, loop, map, model_call, model_required_error, normalize_score, on_chunk_error, parallel, pipe, provider_auth_error, provider_capability_error, provider_error, provider_not_configured_error, rate_limit_error, read_baseline, regression_compare, resume_validation_error, retry, run, run_viewer_cli, schema_validation_error, scope, sequence, span_end_event_schema, span_start_event_schema, start_viewer, stash, step, suspend, suspended_error, timeout, timeout_error, tool_approval_denied_error, tool_error, tournament, trajectory_event_schema, use, write_baseline };
5781
7716
 
5782
7717
  //# sourceMappingURL=index.js.map