fascicle 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import { h as wait_with_abort, k as aborted_error, m as compute_backoff, o as format_schema_issues, s as validate_schema } from "./core-CDrz0NM1.js";
2
- import { S as turn_timeout_error, _ as provider_required_error, a as normalize_local_usage, b as tool_approval_denied_error, c as engine_config_error, d as model_required_error, f as on_chunk_error, g as provider_not_configured_error, h as provider_error, i as missing_peer_error, l as engine_disposed_error, m as provider_capability_error, n as default_normalize_usage, o as resolve_transport, p as provider_auth_error, r as load_optional_peer, s as claude_cli_error, t as split_leading_system_run, u as incomplete_generation_error, v as rate_limit_error, x as tool_error, y as schema_validation_error } from "./leading_system-BQe_405V.js";
2
+ import { n as is_record$1 } from "./records-1HU7mWXf.js";
3
+ import { A as tool_error, C as provider_capability_error, D as rate_limit_error, E as provider_required_error, O as schema_validation_error, S as provider_auth_error, T as provider_not_configured_error, _ as engine_config_error, b as model_required_error, c as read_dialect_init, d as load_optional_peer, f as missing_peer_error, g as claude_cli_error, h as translate_effort_to_sdk_reasoning, i as invoke_http_turn, j as turn_timeout_error, k as tool_approval_denied_error, l as split_assistant_parts, m as resolve_transport, n as consume_framed_stream, o as map_messages_by_role, p as normalize_local_usage, r as create_line_splitter, s as parse_tool_call_arguments, t as split_leading_system_run, u as default_normalize_usage, v as engine_disposed_error, w as provider_error, x as on_chunk_error, y as incomplete_generation_error } from "./leading_system-DfJLqpGi.js";
3
4
  import { join } from "node:path";
4
5
  import { mkdtempSync, writeFileSync } from "node:fs";
5
6
  import { tmpdir } from "node:os";
@@ -143,6 +144,8 @@ function strip_meta_keys(json) {
143
144
  //#endregion
144
145
  //#region src/engine/pricing.ts
145
146
  const FREE_PROVIDERS = /* @__PURE__ */ new Set(["ollama", "lmstudio"]);
147
+ const CACHE_READ_MULTIPLIER = .1;
148
+ const CACHE_WRITE_MULTIPLIER = 1.25;
146
149
  const DEFAULT_PRICING = Object.freeze({
147
150
  "anthropic:claude-opus-4-8": {
148
151
  input_per_million: 5,
@@ -287,6 +290,50 @@ function compute_cost(usage, pricing, provider) {
287
290
  if (surface_reasoning_usd !== void 0) breakdown.reasoning_usd = surface_reasoning_usd;
288
291
  return breakdown;
289
292
  }
293
+ /**
294
+ * Split a reported total cost into an input/output cost breakdown, weighted
295
+ * by token counts.
296
+ *
297
+ * Cache-read and cache-write tokens are cheaper and pricier respectively
298
+ * than base input tokens, so they're weighted by `CACHE_READ_MULTIPLIER`
299
+ * and `CACHE_WRITE_MULTIPLIER` before the total is distributed
300
+ * proportionally across all weighted components. The total is what the
301
+ * provider charged and the split is estimated, so the breakdown keeps
302
+ * `is_estimate: true`. Returns an all-zero estimate when there's no usage or
303
+ * no cost to distribute.
304
+ */
305
+ function decompose_total_cost(total_cost_usd, usage) {
306
+ const input = Math.max(0, usage.input_tokens);
307
+ const output = Math.max(0, usage.output_tokens);
308
+ const cached = Math.max(0, usage.cached_input_tokens ?? 0);
309
+ const cache_write = Math.max(0, usage.cache_write_tokens ?? 0);
310
+ const input_weight = Math.max(0, input - cached - cache_write);
311
+ const output_weight = output;
312
+ const cached_weight = cached * CACHE_READ_MULTIPLIER;
313
+ const cache_write_weight = cache_write * CACHE_WRITE_MULTIPLIER;
314
+ const total_weight = input_weight + output_weight + cached_weight + cache_write_weight;
315
+ if (total_weight === 0 || total_cost_usd === 0) return {
316
+ total_usd: total_cost_usd,
317
+ input_usd: 0,
318
+ output_usd: 0,
319
+ currency: "USD",
320
+ is_estimate: true
321
+ };
322
+ const input_usd = total_cost_usd * input_weight / total_weight;
323
+ const output_usd = total_cost_usd * output_weight / total_weight;
324
+ const cached_usd_raw = total_cost_usd * cached_weight / total_weight;
325
+ const cache_write_usd_raw = total_cost_usd * cache_write_weight / total_weight;
326
+ const breakdown = {
327
+ total_usd: total_cost_usd,
328
+ input_usd,
329
+ output_usd,
330
+ currency: "USD",
331
+ is_estimate: true
332
+ };
333
+ if (cached > 0) breakdown.cached_input_usd = cached_usd_raw;
334
+ if (cache_write > 0) breakdown.cache_write_usd = cache_write_usd_raw;
335
+ return breakdown;
336
+ }
290
337
  //#endregion
291
338
  //#region src/engine/retry.ts
292
339
  const DEFAULT_RETRY = {
@@ -509,227 +556,6 @@ function classify_retryable(err) {
509
556
  if (kind === "network" || kind === "timeout") return build_retryable_signal(err, kind);
510
557
  }
511
558
  //#endregion
512
- //#region src/engine/providers/native_shared.ts
513
- /**
514
- * The message under a JSON error envelope's `error` key, or undefined when
515
- * the shape carries none worth surfacing.
516
- */
517
- function error_field_message(parsed) {
518
- if (parsed === null || typeof parsed !== "object") return void 0;
519
- const error = Reflect.get(parsed, "error");
520
- if (typeof error === "string" && error.length > 0) return error;
521
- if (error === null || typeof error !== "object") return void 0;
522
- const message = Reflect.get(error, "message");
523
- return typeof message === "string" && message.length > 0 ? message : void 0;
524
- }
525
- /**
526
- * Pull a human-readable message out of a non-2xx body. The native wires share
527
- * one envelope family: Anthropic sends `{ type: 'error', error: { message } }`,
528
- * OpenAI-style wires send `{ error: { message } }`, and Ollama sends
529
- * `{ error: "message" }` (with the nested shape appearing behind proxies), so
530
- * one parser accepts a string or a `{ message }` object under `error` and
531
- * falls back to the raw body, truncated to 300 chars.
532
- */
533
- function extract_error_message(body) {
534
- if (body.length === 0) return "(empty body)";
535
- let message;
536
- try {
537
- message = error_field_message(JSON.parse(body));
538
- } catch {}
539
- if (message !== void 0) return message;
540
- return body.length > 300 ? `${body.slice(0, 300)}...` : body;
541
- }
542
- /**
543
- * Map a non-2xx response to what the engine's retry stack expects: 401
544
- * becomes a typed provider_auth_error, 429/5xx keep `status` +
545
- * `responseHeaders` so classify_provider_error marks them retryable
546
- * (rate_limit honoring retry-after, provider_5xx), and any other 4xx is a
547
- * permanent provider_error that surfaces as-is.
548
- */
549
- async function response_error(response, provider) {
550
- let body = "";
551
- try {
552
- body = await response.text();
553
- } catch {}
554
- const detail = extract_error_message(body);
555
- const status = response.status;
556
- if (status === 401) return new provider_auth_error(provider, `${provider} authentication failed (401): ${detail}`);
557
- if (status === 429 || status >= 500) {
558
- const retry_after = response.headers.get("retry-after");
559
- return Object.assign(/* @__PURE__ */ new Error(`${provider} API error ${status}: ${detail}`), { status }, retry_after !== null ? { responseHeaders: { "retry-after": retry_after } } : {});
560
- }
561
- return new provider_error(`${provider} API error ${status}: ${detail}`, {
562
- status,
563
- body
564
- });
565
- }
566
- /**
567
- * A user abort surfaces as the fetch/reader AbortError; retry_turn converts
568
- * it via its own signal check, so it is rethrown untouched. Everything else
569
- * is a transport failure wrapped in the `kind: 'network'` shape the shared
570
- * classify_provider_error marks retryable.
571
- */
572
- function rethrow_network_failure(err, abort, provider) {
573
- if (abort.aborted) throw err;
574
- const detail = err instanceof Error ? err.message : String(err);
575
- throw Object.assign(/* @__PURE__ */ new Error(`${provider} native: network failure: ${detail}`), { kind: "network" });
576
- }
577
- /**
578
- * Run one native turn over HTTP: POST the dialect's body, map a non-2xx to
579
- * the retry stack's shapes, and hand the response to the dialect's stream or
580
- * JSON consumer. `build_body` is called inside the network try/catch on
581
- * purpose, so a mapper that throws surfaces as a retryable network failure
582
- * rather than escaping the turn unclassified.
583
- */
584
- async function invoke_http_turn(req, provider, request, consume) {
585
- let response;
586
- try {
587
- response = await fetch(request.url, {
588
- method: "POST",
589
- headers: request.headers,
590
- body: JSON.stringify(request.build_body()),
591
- signal: req.abort
592
- });
593
- } catch (err) {
594
- rethrow_network_failure(err, req.abort, provider);
595
- }
596
- if (!response.ok) throw await response_error(response, provider);
597
- if (req.stream) return consume.stream(response);
598
- const payload = await response.json();
599
- return consume.parse(payload);
600
- }
601
- /**
602
- * Drain a streaming response: decode bytes, reassemble framed payloads (SSE
603
- * events or NDJSON lines), and feed them to the dialect's aggregator, which
604
- * dispatches chunks through req.dispatch_chunk as they arrive. Transport
605
- * failures mid-read wrap as network errors; aggregator throws (malformed
606
- * frames, mid-stream error events, a rejecting on_chunk) pass through
607
- * untouched, with the reader cancelled so the connection is released.
608
- */
609
- async function consume_framed_stream(response, req, provider, framing, create_aggregator) {
610
- const body = response.body;
611
- if (body === null) throw new provider_error(`${provider} native: streaming response has no body`);
612
- const aggregator = create_aggregator(req.dispatch_chunk ?? (async () => {}));
613
- const reader = body.getReader();
614
- const text_decoder = new TextDecoder();
615
- const next_bytes = async () => {
616
- let step;
617
- try {
618
- step = await reader.read();
619
- } catch (err) {
620
- rethrow_network_failure(err, req.abort, provider);
621
- }
622
- return step.done ? void 0 : step.value;
623
- };
624
- try {
625
- while (true) {
626
- const bytes = await next_bytes();
627
- if (bytes === void 0) break;
628
- for (const data of framing.push(text_decoder.decode(bytes, { stream: true }))) await aggregator.handle(data);
629
- }
630
- } finally {
631
- reader.cancel().catch(() => {});
632
- }
633
- const tail = [...framing.push(text_decoder.decode()), ...framing.flush()];
634
- for (const data of tail) await aggregator.handle(data);
635
- return aggregator.complete();
636
- }
637
- /**
638
- * Incremental newline framing shared by the SSE and NDJSON decoders: buffer
639
- * decoded text across arbitrary chunk boundaries, hand each completed line
640
- * (CR trimmed) to `handle_line`, and on flush hand over an unterminated tail
641
- * line, then `on_flush` for state a decoder holds across lines (an SSE event
642
- * never closed by a blank line).
643
- */
644
- function create_line_splitter(handle_line, on_flush) {
645
- let buffer = "";
646
- const take_line = (raw, out) => {
647
- handle_line(raw.endsWith("\r") ? raw.slice(0, -1) : raw, out);
648
- };
649
- return {
650
- push(text) {
651
- buffer += text;
652
- const out = [];
653
- let newline = buffer.indexOf("\n");
654
- while (newline >= 0) {
655
- take_line(buffer.slice(0, newline), out);
656
- buffer = buffer.slice(newline + 1);
657
- newline = buffer.indexOf("\n");
658
- }
659
- return out;
660
- },
661
- flush() {
662
- const out = [];
663
- if (buffer.length > 0) take_line(buffer, out);
664
- buffer = "";
665
- on_flush?.(out);
666
- return out;
667
- }
668
- };
669
- }
670
- /**
671
- * Decode a tool call's wire `arguments` string into the engine's input value:
672
- * non-empty JSON parses (bad JSON is a provider_error naming the tool), and
673
- * an absent or empty value means `{}`, the shape some servers send for
674
- * no-argument calls. The Ollama wire's structured-object arguments are its
675
- * caller's business before this runs.
676
- */
677
- function parse_tool_call_arguments(raw_args, provider, tool_name) {
678
- if (typeof raw_args === "string" && raw_args.length > 0) try {
679
- return JSON.parse(raw_args);
680
- } catch {
681
- throw new provider_error(`${provider} native: tool_calls arguments for ${tool_name} is not valid JSON`);
682
- }
683
- return {};
684
- }
685
- /**
686
- * Fold engine messages into a wire's message array by role. Serves the wires
687
- * whose messages map 1:1 in place (chat/completions, Ollama's /api/chat); the
688
- * Messages API's system-hoisting and role-merging rules live in its own
689
- * mapper instead.
690
- */
691
- function map_messages_by_role(messages, wire) {
692
- const out = [];
693
- for (const message of messages) switch (message.role) {
694
- case "system":
695
- out.push(wire.system(message.content));
696
- break;
697
- case "user":
698
- out.push(wire.user(message.content));
699
- break;
700
- case "assistant":
701
- out.push(wire.assistant(message.content));
702
- break;
703
- case "tool": out.push(wire.tool(message));
704
- }
705
- return out;
706
- }
707
- /**
708
- * Partition assistant content parts into the text runs and tool calls the
709
- * wire mappers serialize separately.
710
- */
711
- function split_assistant_parts(parts) {
712
- const texts = [];
713
- const tool_parts = [];
714
- for (const part of parts) if (part.type === "text") texts.push(part.text);
715
- else tool_parts.push(part);
716
- return {
717
- texts,
718
- tool_parts
719
- };
720
- }
721
- /**
722
- * Read the api_key and base_url every OpenAI-compatible dialect starts from:
723
- * a missing or non-string api_key becomes '' (the core's empty-key guard owns
724
- * the throw), and an absent base_url falls back to the dialect's default.
725
- */
726
- function read_dialect_init(init, default_base_url) {
727
- return {
728
- api_key: typeof init.api_key === "string" ? init.api_key : "",
729
- base_url: typeof init.base_url === "string" && init.base_url.length > 0 ? init.base_url : default_base_url
730
- };
731
- }
732
- //#endregion
733
559
  //#region src/engine/providers/sse_native.ts
734
560
  /**
735
561
  * Incremental SSE decoder shared by the native (raw-HTTP) provider adapters.
@@ -787,10 +613,10 @@ function create_sse_decoder() {
787
613
  * construction rather than by parallel code paths.
788
614
  */
789
615
  /**
790
- * Effort level to extended-thinking budget tokens. Lives here (not in
791
- * anthropic.ts) because both transports map effort from this table and the
792
- * ai_sdk factory already imports this module for transport dispatch; the
793
- * reverse import would be a cycle.
616
+ * Effort level to the room native adds to its max_tokens default for
617
+ * adaptive thinking. No transport sends these as a budget any more: native
618
+ * sends `output_config.effort`, and the ai_sdk transport hands the level to
619
+ * the SDK's `reasoning` option.
794
620
  */
795
621
  const ANTHROPIC_THINKING_BUDGETS = {
796
622
  none: 0,
@@ -808,9 +634,9 @@ const ANTHROPIC_VERSION = "2023-06-01";
808
634
  */
809
635
  const DEFAULT_BASE_URL$2 = "https://api.anthropic.com/v1";
810
636
  /**
811
- * The Messages API requires max_tokens on every request. When extended
812
- * thinking is enabled the budget counts against max_tokens, so the default
813
- * rides on top of the thinking budget rather than being swallowed by it.
637
+ * The Messages API requires max_tokens on every request. Thinking spends
638
+ * from max_tokens, so with effort set the default rides on top of that
639
+ * effort's budget-table value rather than being swallowed by it.
814
640
  */
815
641
  const DEFAULT_MAX_TOKENS = 4096;
816
642
  /**
@@ -838,19 +664,44 @@ function to_user_blocks(content) {
838
664
  return blocks;
839
665
  }
840
666
  /**
841
- * Map assistant message content to Messages-API blocks: text parts become
842
- * text blocks (empties dropped), tool-call parts become tool_use blocks.
667
+ * Map a reasoning part back to the block it came from: a `signature` under
668
+ * `anthropic` is a thinking block, its text as it was (empty included), and
669
+ * `redactedData` is a redacted_thinking block. A part with neither (keyed for
670
+ * another provider, or carrying no data) maps to no block, since Anthropic
671
+ * refuses thinking it didn't sign.
672
+ */
673
+ function reasoning_blocks(part) {
674
+ const own = part.provider_options?.["anthropic"];
675
+ const signature = own?.["signature"];
676
+ if (typeof signature === "string") return [{
677
+ type: "thinking",
678
+ thinking: part.text,
679
+ signature
680
+ }];
681
+ const data = own?.["redactedData"];
682
+ return typeof data === "string" ? [{
683
+ type: "redacted_thinking",
684
+ data
685
+ }] : [];
686
+ }
687
+ /**
688
+ * Map assistant message content to Messages-API blocks in part order: text
689
+ * parts become text blocks (empties dropped), tool-call parts become tool_use
690
+ * blocks, and Anthropic's own reasoning parts become the thinking and
691
+ * redacted_thinking blocks they came from. Order is kept because Anthropic
692
+ * refuses a tool-use turn whose thinking blocks moved.
843
693
  */
844
694
  function to_assistant_blocks(content) {
845
695
  if (typeof content === "string") return text_blocks(content);
846
696
  const blocks = [];
847
697
  for (const part of content) if (part.type === "text") blocks.push(...text_blocks(part.text));
848
- else blocks.push({
698
+ else if (part.type === "tool_call") blocks.push({
849
699
  type: "tool_use",
850
700
  id: part.id,
851
701
  name: part.name,
852
702
  input: part.input
853
703
  });
704
+ else blocks.push(...reasoning_blocks(part));
854
705
  return blocks;
855
706
  }
856
707
  /**
@@ -910,29 +761,30 @@ function to_anthropic_tools(tools) {
910
761
  }
911
762
  /**
912
763
  * Build the Messages-API request body for one turn: mapped messages and
913
- * tools, the effort-derived thinking block, the required max_tokens default,
914
- * and the `provider_options.anthropic` wire passthrough merged last.
764
+ * tools, effort as adaptive thinking plus `output_config.effort`, the
765
+ * required max_tokens default, and the `provider_options.anthropic` wire
766
+ * passthrough merged last.
915
767
  */
916
768
  function build_messages_body(req) {
917
769
  const { system: hoisted_system, messages } = to_anthropic_messages(req.messages);
918
770
  const system_parts = [req.system, hoisted_system].filter((s) => typeof s === "string" && s.length > 0);
919
- const budget = ANTHROPIC_THINKING_BUDGETS[req.effort];
920
- const thinking_enabled = budget > 0;
921
771
  const body = {
922
772
  model: req.model_id,
923
773
  messages,
924
- max_tokens: req.max_tokens ?? (thinking_enabled ? budget + DEFAULT_MAX_TOKENS : DEFAULT_MAX_TOKENS)
774
+ max_tokens: req.max_tokens ?? ANTHROPIC_THINKING_BUDGETS[req.effort] + DEFAULT_MAX_TOKENS
925
775
  };
926
776
  if (system_parts.length > 0) body["system"] = system_parts.join("\n\n");
927
777
  if (req.tools.length > 0) body["tools"] = to_anthropic_tools(req.tools);
928
778
  if (req.stream) body["stream"] = true;
929
- if (thinking_enabled) body["thinking"] = {
930
- type: "enabled",
931
- budget_tokens: budget
932
- };
933
- else {
779
+ if (req.effort === "none") {
934
780
  if (req.temperature !== void 0) body["temperature"] = req.temperature;
935
781
  if (req.top_p !== void 0) body["top_p"] = req.top_p;
782
+ } else {
783
+ body["thinking"] = {
784
+ type: "adaptive",
785
+ display: "summarized"
786
+ };
787
+ body["output_config"] = { effort: req.effort };
936
788
  }
937
789
  const passthrough = req.provider_options?.["anthropic"];
938
790
  return passthrough === void 0 ? body : {
@@ -993,56 +845,103 @@ function read_tool_use_identity(block, where) {
993
845
  };
994
846
  }
995
847
  /**
996
- * Fold one response `content` block into the text/tool_call accumulators.
997
- * Text blocks append their string; tool_use blocks contribute a call whose id
998
- * and name are validated by read_tool_use_identity; thinking /
999
- * redacted_thinking blocks have no TurnResult field and are skipped.
848
+ * Read a thinking or redacted_thinking block as a reasoning part under
849
+ * `@ai-sdk/anthropic`'s own keys, so a transcript moves between the two
850
+ * transports: a thinking block keeps its text (empty when the display was
851
+ * omitted) and its `signature`, and a redacted one keeps its `data` as
852
+ * `redactedData`. A block missing its signature or data becomes a part with
853
+ * no data, which no adapter sends back. Any other block type is undefined.
854
+ */
855
+ function read_reasoning_block(block, type) {
856
+ if (type === "thinking") {
857
+ const thinking = Reflect.get(block, "thinking");
858
+ const signature = Reflect.get(block, "signature");
859
+ return {
860
+ type: "reasoning",
861
+ text: typeof thinking === "string" ? thinking : "",
862
+ ...typeof signature === "string" ? { provider_options: { anthropic: { signature } } } : {}
863
+ };
864
+ }
865
+ if (type !== "redacted_thinking") return void 0;
866
+ const data = Reflect.get(block, "data");
867
+ return {
868
+ type: "reasoning",
869
+ text: "",
870
+ ...typeof data === "string" ? { provider_options: { anthropic: { redactedData: data } } } : {}
871
+ };
872
+ }
873
+ /**
874
+ * Fold one response `content` block into the accumulators. Text blocks append
875
+ * their string; tool_use blocks contribute a call whose id and name are
876
+ * validated by read_tool_use_identity; thinking and redacted_thinking blocks
877
+ * become reasoning parts. Every kept block also lands in `parts`, in block
878
+ * order, for TurnResult.content.
1000
879
  */
1001
- function push_content_block(block, text_parts, tool_calls) {
880
+ function push_content_block(block, acc) {
1002
881
  if (block === null || typeof block !== "object") return;
1003
882
  const type = Reflect.get(block, "type");
1004
883
  if (type === "text") {
1005
884
  const text = Reflect.get(block, "text");
1006
- if (typeof text === "string") text_parts.push(text);
885
+ if (typeof text === "string") {
886
+ acc.text_parts.push(text);
887
+ acc.parts.push({
888
+ type: "text",
889
+ text
890
+ });
891
+ }
1007
892
  return;
1008
893
  }
1009
894
  if (type === "tool_use") {
1010
895
  const { id, name } = read_tool_use_identity(block, "response content");
1011
- tool_calls.push({
896
+ const call = {
1012
897
  id,
1013
898
  name,
1014
899
  input: Reflect.get(block, "input")
900
+ };
901
+ acc.tool_calls.push(call);
902
+ acc.parts.push({
903
+ type: "tool_call",
904
+ ...call
1015
905
  });
906
+ return;
1016
907
  }
908
+ const reasoning = read_reasoning_block(block, type);
909
+ if (reasoning !== void 0) acc.parts.push(reasoning);
1017
910
  }
1018
911
  /**
1019
912
  * Walk a response payload's `content` array into the flat text and tool_call
1020
- * lists a TurnResult carries. A non-array `content` yields empty lists,
1021
- * matching a payload that carried no content blocks.
913
+ * lists a TurnResult carries, plus every block as a part in order. A
914
+ * non-array `content` yields empty lists, matching a payload that carried no
915
+ * content blocks.
1022
916
  */
1023
917
  function collect_content_blocks(content) {
1024
- const text_parts = [];
1025
- const tool_calls = [];
1026
- if (Array.isArray(content)) for (const block of content) push_content_block(block, text_parts, tool_calls);
1027
- return {
1028
- text_parts,
1029
- tool_calls
918
+ const acc = {
919
+ text_parts: [],
920
+ tool_calls: [],
921
+ parts: []
1030
922
  };
923
+ if (Array.isArray(content)) for (const block of content) push_content_block(block, acc);
924
+ return acc;
1031
925
  }
1032
926
  /**
1033
927
  * Parse a non-stream Messages-API response payload into a TurnResult. The
1034
928
  * stream aggregator rebuilds this same payload shape and feeds it here too,
1035
- * so both paths share one parser.
929
+ * so both paths share one parser. A turn with a thinking block gets
930
+ * `content`, every block in order, and any other turn stays as it was.
1036
931
  */
1037
932
  function parse_messages_response(payload) {
1038
933
  if (payload === null || typeof payload !== "object") throw new provider_error("anthropic native: response payload is not a JSON object");
1039
- const { text_parts, tool_calls } = collect_content_blocks(Reflect.get(payload, "content"));
1040
- return {
934
+ const { text_parts, tool_calls, parts } = collect_content_blocks(Reflect.get(payload, "content"));
935
+ const result = {
1041
936
  text: text_parts.join(""),
1042
937
  tool_calls,
1043
938
  finish_reason: map_anthropic_stop_reason(Reflect.get(payload, "stop_reason")),
1044
939
  usage: map_anthropic_usage(Reflect.get(payload, "usage"))
1045
940
  };
941
+ return parts.some((part) => part.type === "reasoning") ? {
942
+ ...result,
943
+ content: parts
944
+ } : result;
1046
945
  }
1047
946
  /**
1048
947
  * Mid-stream `error` events reuse the HTTP status classification:
@@ -1080,6 +979,21 @@ function read_block_index(event) {
1080
979
  return index;
1081
980
  }
1082
981
  /**
982
+ * Parse one Messages-stream data line. OpenRouter's Anthropic-compatible
983
+ * endpoint ends every stream with a chat-completions `[DONE]` line after
984
+ * message_stop; it carries no event, so it reads as null, which the
985
+ * aggregator drops like a JSON null. Any other data that isn't JSON is a
986
+ * broken stream.
987
+ */
988
+ function parse_stream_event(data) {
989
+ if (data === "[DONE]") return null;
990
+ try {
991
+ return JSON.parse(data);
992
+ } catch {
993
+ throw new provider_error("anthropic native: stream event is not valid JSON");
994
+ }
995
+ }
996
+ /**
1083
997
  * Consume parsed Messages-stream events, dispatching StreamChunks as they
1084
998
  * arrive and rebuilding the non-stream response payload as it goes: text
1085
999
  * deltas accumulate into synthetic text blocks, tool_use input arrives as raw
@@ -1093,6 +1007,7 @@ function read_block_index(event) {
1093
1007
  function create_stream_aggregator(step_index, dispatch) {
1094
1008
  const content = [];
1095
1009
  const open_text = /* @__PURE__ */ new Map();
1010
+ const open_thinking = /* @__PURE__ */ new Map();
1096
1011
  const open_tools = /* @__PURE__ */ new Map();
1097
1012
  const usage = {};
1098
1013
  let stop_reason;
@@ -1101,6 +1016,21 @@ function create_stream_aggregator(step_index, dispatch) {
1101
1016
  if (raw === null || typeof raw !== "object") return;
1102
1017
  for (const [key, value] of Object.entries(raw)) if (typeof value === "number") usage[key] = value;
1103
1018
  };
1019
+ const start_reasoning_block = (index, block, type) => {
1020
+ if (type === "thinking") {
1021
+ const initial = Reflect.get(block, "thinking");
1022
+ const synthetic = {
1023
+ type: "thinking",
1024
+ thinking: typeof initial === "string" ? initial : "",
1025
+ signature: Reflect.get(block, "signature")
1026
+ };
1027
+ content.push(synthetic);
1028
+ open_thinking.set(index, synthetic);
1029
+ } else if (type === "redacted_thinking") content.push({
1030
+ type: "redacted_thinking",
1031
+ data: Reflect.get(block, "data")
1032
+ });
1033
+ };
1104
1034
  const on_block_start = async (event) => {
1105
1035
  const index = read_block_index(event);
1106
1036
  const block = Reflect.get(event, "content_block");
@@ -1135,7 +1065,25 @@ function create_stream_aggregator(step_index, dispatch) {
1135
1065
  name,
1136
1066
  step_index
1137
1067
  });
1068
+ return;
1069
+ }
1070
+ start_reasoning_block(index, block, type);
1071
+ };
1072
+ const on_thinking_delta = async (index, delta, type) => {
1073
+ const open = open_thinking.get(index);
1074
+ if (type === "signature_delta") {
1075
+ const signature = Reflect.get(delta, "signature");
1076
+ if (typeof signature === "string" && open !== void 0) open.signature = signature;
1077
+ return;
1138
1078
  }
1079
+ const thinking = Reflect.get(delta, "thinking");
1080
+ if (typeof thinking !== "string") return;
1081
+ if (open !== void 0) open.thinking += thinking;
1082
+ await dispatch({
1083
+ kind: "reasoning",
1084
+ text: thinking,
1085
+ step_index
1086
+ });
1139
1087
  };
1140
1088
  const on_block_delta = async (event) => {
1141
1089
  const index = read_block_index(event);
@@ -1154,13 +1102,8 @@ function create_stream_aggregator(step_index, dispatch) {
1154
1102
  });
1155
1103
  return;
1156
1104
  }
1157
- if (type === "thinking_delta") {
1158
- const thinking = Reflect.get(delta, "thinking");
1159
- if (typeof thinking === "string") await dispatch({
1160
- kind: "reasoning",
1161
- text: thinking,
1162
- step_index
1163
- });
1105
+ if (type === "thinking_delta" || type === "signature_delta") {
1106
+ await on_thinking_delta(index, delta, type);
1164
1107
  return;
1165
1108
  }
1166
1109
  if (type === "input_json_delta") {
@@ -1180,6 +1123,7 @@ function create_stream_aggregator(step_index, dispatch) {
1180
1123
  const index = read_block_index(event);
1181
1124
  const open = open_tools.get(index);
1182
1125
  open_text.delete(index);
1126
+ open_thinking.delete(index);
1183
1127
  if (open === void 0) return;
1184
1128
  open_tools.delete(index);
1185
1129
  if (open.json.length > 0) try {
@@ -1196,12 +1140,7 @@ function create_stream_aggregator(step_index, dispatch) {
1196
1140
  };
1197
1141
  return {
1198
1142
  async handle(data) {
1199
- let event;
1200
- try {
1201
- event = JSON.parse(data);
1202
- } catch {
1203
- throw new provider_error("anthropic native: stream event is not valid JSON");
1204
- }
1143
+ const event = parse_stream_event(data);
1205
1144
  if (event === null || typeof event !== "object") return;
1206
1145
  switch (Reflect.get(event, "type")) {
1207
1146
  case "message_start": {
@@ -1296,27 +1235,6 @@ const create_anthropic_native_adapter = (init) => {
1296
1235
  //#endregion
1297
1236
  //#region src/engine/providers/anthropic.ts
1298
1237
  /**
1299
- * Map an effort level to Anthropic's extended-thinking provider options.
1300
- *
1301
- * A zero budget (which is what `none` maps to) sends no thinking config at all
1302
- * rather than an explicit "disabled" value, so non-reasoning models see a plain
1303
- * request.
1304
- */
1305
- function translate_anthropic_effort(effort) {
1306
- const budget = ANTHROPIC_THINKING_BUDGETS[effort];
1307
- if (budget === 0) return {
1308
- provider_options: {},
1309
- effort_ignored: false
1310
- };
1311
- return {
1312
- provider_options: { anthropic: { thinking: {
1313
- type: "enabled",
1314
- budgetTokens: budget
1315
- } } },
1316
- effort_ignored: false
1317
- };
1318
- }
1319
- /**
1320
1238
  * Normalize Anthropic usage via the shared default mapper; the AI SDK already
1321
1239
  * reports cache and reasoning tokens in the fields the default reads.
1322
1240
  */
@@ -1357,7 +1275,7 @@ const create_anthropic_ai_sdk_adapter = (init) => {
1357
1275
  if (base_url !== void 0) config.baseURL = base_url;
1358
1276
  return sdk.createAnthropic(config)(model_id);
1359
1277
  },
1360
- translate_effort: translate_anthropic_effort,
1278
+ translate_effort: translate_effort_to_sdk_reasoning,
1361
1279
  normalize_usage: normalize_anthropic_usage,
1362
1280
  supports: (capability) => SUPPORTED$9.has(capability)
1363
1281
  };
@@ -1388,10 +1306,10 @@ const create_anthropic_ai_sdk_adapter = (init) => {
1388
1306
  * parallel code paths.
1389
1307
  */
1390
1308
  /**
1391
- * The wire enum is `low | medium | high` only, so `xhigh` and `max` clamp to
1392
- * `high`, the same clamp as the ai_sdk transport's `reasoningEffort`. `none`
1393
- * omits the field entirely. Non-reasoning models ignore the field
1394
- * server-side; the adapter does not model-sniff.
1309
+ * The flat field's wire enum is `low | medium | high` only, so `xhigh` and
1310
+ * `max` clamp to `high`. The nested shape takes every level, so it skips this
1311
+ * table. Non-reasoning models ignore either field server-side; the adapter
1312
+ * does not model-sniff.
1395
1313
  */
1396
1314
  const OPENAI_COMPATIBLE_REASONING_EFFORT = {
1397
1315
  low: "low",
@@ -1401,6 +1319,21 @@ const OPENAI_COMPATIBLE_REASONING_EFFORT = {
1401
1319
  max: "high"
1402
1320
  };
1403
1321
  /**
1322
+ * The field each joinable reasoning_details entry type carries its visible
1323
+ * text in. A Map rather than an object literal, since the lookup key is a
1324
+ * `type` read off the wire.
1325
+ */
1326
+ const REASONING_TEXT_FIELD = /* @__PURE__ */ new Map([["reasoning.text", "text"], ["reasoning.summary", "summary"]]);
1327
+ /**
1328
+ * Read a reasoning_details entry's visible text: a text entry's `text`, a
1329
+ * summary's `summary`, and '' for any other entry or a missing value.
1330
+ */
1331
+ function reasoning_detail_text(entry) {
1332
+ const field = REASONING_TEXT_FIELD.get(entry["type"]);
1333
+ const value = field === void 0 ? void 0 : entry[field];
1334
+ return typeof value === "string" ? value : "";
1335
+ }
1336
+ /**
1404
1337
  * Map user message content to this wire's shape (a string, or an array of
1405
1338
  * text parts). Image parts throw a capability error: they are not mapped on
1406
1339
  * the native transport.
@@ -1419,15 +1352,16 @@ function to_user_content$1(content, provider) {
1419
1352
  }
1420
1353
  /**
1421
1354
  * Map assistant message content to Chat Completions shape: text parts join
1422
- * into `content`, and tool-call parts become `tool_calls` entries with
1423
- * JSON-stringified arguments.
1355
+ * into `content`, tool-call parts become `tool_calls` entries with
1356
+ * JSON-stringified arguments, and the provider's own reasoning parts give
1357
+ * their entries back as `reasoning_details`.
1424
1358
  */
1425
- function to_assistant_message$1(content) {
1359
+ function to_assistant_message$1(content, provider) {
1426
1360
  if (typeof content === "string") return {
1427
1361
  role: "assistant",
1428
1362
  content
1429
1363
  };
1430
- const { texts, tool_parts } = split_assistant_parts(content);
1364
+ const { texts, tool_parts, reasoning } = split_assistant_parts(content, provider);
1431
1365
  const tool_calls = tool_parts.map((part) => ({
1432
1366
  id: part.id,
1433
1367
  type: "function",
@@ -1442,9 +1376,20 @@ function to_assistant_message$1(content) {
1442
1376
  content: text.length > 0 || tool_calls.length === 0 ? text : null
1443
1377
  };
1444
1378
  if (tool_calls.length > 0) message.tool_calls = tool_calls;
1379
+ const reasoning_details = reasoning.flatMap((part) => own_reasoning_detail(part, provider));
1380
+ if (reasoning_details.length > 0) message.reasoning_details = reasoning_details;
1445
1381
  return message;
1446
1382
  }
1447
1383
  /**
1384
+ * The reasoning_details entry a reasoning part keeps for `provider`, as a
1385
+ * one-element list, or an empty one when the part carries no entry (a part
1386
+ * another transport keyed for this provider, say).
1387
+ */
1388
+ function own_reasoning_detail(part, provider) {
1389
+ const detail = part.provider_options?.[provider]?.["detail"];
1390
+ return is_record$1(detail) ? [detail] : [];
1391
+ }
1392
+ /**
1448
1393
  * Map Fascicle Message[] to Chat Completions shape. Unlike the Messages API
1449
1394
  * there is no top-level system field or role-alternation constraint: system
1450
1395
  * messages map in place at any position, and tool results are first-class
@@ -1460,7 +1405,7 @@ function to_chat_messages(messages, provider) {
1460
1405
  role: "user",
1461
1406
  content: to_user_content$1(content, provider)
1462
1407
  }),
1463
- assistant: to_assistant_message$1,
1408
+ assistant: (content) => to_assistant_message$1(content, provider),
1464
1409
  tool: (message) => ({
1465
1410
  role: "tool",
1466
1411
  tool_call_id: message.tool_call_id,
@@ -1483,10 +1428,19 @@ function to_chat_tools(tools) {
1483
1428
  }));
1484
1429
  }
1485
1430
  /**
1431
+ * Write effort into the body in the dialect's shape. `none` writes nothing,
1432
+ * on either shape.
1433
+ */
1434
+ function apply_effort(body, effort, dialect) {
1435
+ if (effort === "none") return;
1436
+ if (dialect.effort_shape === "nested") body["reasoning"] = { effort };
1437
+ else body["reasoning_effort"] = OPENAI_COMPATIBLE_REASONING_EFFORT[effort];
1438
+ }
1439
+ /**
1486
1440
  * Build the Chat Completions request body for one turn: mapped messages and
1487
- * tools, the token-limit field named per dialect, sampling params, the
1488
- * effort-derived reasoning_effort, and the dialect's
1489
- * `provider_options.<name>` wire passthrough merged last.
1441
+ * tools, the token-limit field named per dialect, sampling params, effort in
1442
+ * the dialect's shape, and the dialect's `provider_options.<name>` wire
1443
+ * passthrough merged last.
1490
1444
  */
1491
1445
  function build_chat_completions_body(req, dialect) {
1492
1446
  const messages = [];
@@ -1503,7 +1457,7 @@ function build_chat_completions_body(req, dialect) {
1503
1457
  if (req.max_tokens !== void 0) body[dialect.token_limit_field] = req.max_tokens;
1504
1458
  if (req.temperature !== void 0) body["temperature"] = req.temperature;
1505
1459
  if (req.top_p !== void 0) body["top_p"] = req.top_p;
1506
- if (req.effort !== "none") body["reasoning_effort"] = OPENAI_COMPATIBLE_REASONING_EFFORT[req.effort];
1460
+ apply_effort(body, req.effort, dialect);
1507
1461
  if (req.stream) {
1508
1462
  body["stream"] = true;
1509
1463
  if (dialect.stream_include_usage) body["stream_options"] = { include_usage: true };
@@ -1566,6 +1520,32 @@ function map_chat_usage(raw, dialect) {
1566
1520
  return totals;
1567
1521
  }
1568
1522
  /**
1523
+ * Give a turn the charge a cost-reporting dialect puts on its usage object,
1524
+ * and leave every other turn as it was. `usage.cost` is the request's cost in
1525
+ * US dollars (OpenRouter's credits are dollars). On a bring-your-own-key
1526
+ * request it is only OpenRouter's fee, so the upstream provider's own charge
1527
+ * (`cost_details.upstream_inference_cost`) is added, a null one counting as
1528
+ * nothing. The raw fields ride `provider_reported` under the dialect's name
1529
+ * as sent. A usage object without a numeric `cost` reports nothing, so the
1530
+ * loop falls back to pricing.
1531
+ */
1532
+ function with_reported_cost(result, raw_usage, dialect) {
1533
+ if (!dialect.reports_cost || !is_record$1(raw_usage)) return result;
1534
+ const cost = raw_usage["cost"];
1535
+ if (typeof cost !== "number") return result;
1536
+ const details = raw_usage["cost_details"];
1537
+ const upstream = is_record$1(details) ? details["upstream_inference_cost"] : void 0;
1538
+ const byok_upstream = raw_usage["is_byok"] === true && typeof upstream === "number" ? upstream : 0;
1539
+ const reported = { cost };
1540
+ if (raw_usage["is_byok"] !== void 0) reported["is_byok"] = raw_usage["is_byok"];
1541
+ if (details !== void 0) reported["cost_details"] = details;
1542
+ return {
1543
+ ...result,
1544
+ reported_cost_usd: cost + byok_upstream,
1545
+ provider_reported: { [dialect.name]: reported }
1546
+ };
1547
+ }
1548
+ /**
1569
1549
  * Parse one Chat Completions tool_calls entry into the engine's tool-call
1570
1550
  * shape, throwing a provider_error if the entry is missing id, function
1571
1551
  * name, or valid JSON arguments.
@@ -1598,23 +1578,123 @@ function parse_chat_completion(payload, dialect) {
1598
1578
  const content = Reflect.get(message, "content");
1599
1579
  const raw_tool_calls = Reflect.get(message, "tool_calls");
1600
1580
  const tool_calls = Array.isArray(raw_tool_calls) ? raw_tool_calls.map((entry) => parse_tool_call(entry, dialect.name)) : [];
1601
- return {
1581
+ const raw_usage = Reflect.get(payload, "usage");
1582
+ return with_reported_cost(with_reasoning_content({
1602
1583
  text: typeof content === "string" ? content : "",
1603
1584
  tool_calls,
1604
1585
  finish_reason: map_chat_finish_reason(Reflect.get(choice, "finish_reason")),
1605
- usage: map_chat_usage(Reflect.get(payload, "usage"), dialect)
1586
+ usage: map_chat_usage(raw_usage, dialect)
1587
+ }, read_reasoning_parts(message, dialect)), raw_usage, dialect);
1588
+ }
1589
+ /**
1590
+ * Read a response message's reasoning_details into reasoning parts, one per
1591
+ * entry, in order, each keeping its entry under
1592
+ * `provider_options.<name>.detail` for the trip back. Empty unless the
1593
+ * dialect reads reasoning_details.
1594
+ */
1595
+ function read_reasoning_parts(message, dialect) {
1596
+ if (dialect.reasoning !== "reasoning_details") return [];
1597
+ const entries = Reflect.get(message, "reasoning_details");
1598
+ if (!Array.isArray(entries)) return [];
1599
+ return entries.filter(is_record$1).map((detail) => ({
1600
+ type: "reasoning",
1601
+ text: reasoning_detail_text(detail),
1602
+ provider_options: { [dialect.name]: { detail } }
1603
+ }));
1604
+ }
1605
+ /**
1606
+ * Give a turn its `content` when it carries reasoning, and leave every other
1607
+ * turn exactly as it was. This wire keeps reasoning, text, and tool calls in
1608
+ * separate fields, so the parts go in that order.
1609
+ */
1610
+ function with_reasoning_content(result, reasoning) {
1611
+ if (reasoning.length === 0) return result;
1612
+ const content = [...reasoning];
1613
+ if (result.text.length > 0) content.push({
1614
+ type: "text",
1615
+ text: result.text
1616
+ });
1617
+ for (const call of result.tool_calls) content.push({
1618
+ type: "tool_call",
1619
+ ...call
1620
+ });
1621
+ return {
1622
+ ...result,
1623
+ content
1624
+ };
1625
+ }
1626
+ /**
1627
+ * The error a compatible server reports after its stream began under HTTP
1628
+ * 200: a frame whose top-level `error` holds the server's message and, on
1629
+ * OpenRouter, the HTTP status the failure would have had as a numeric
1630
+ * `code`. Carrying that code as `status` lets the engine's retry ladder treat
1631
+ * it as it treats the same error at the start of a response, so a 429 or a
1632
+ * 5xx before any chunk retries, and any error after a chunk fails the turn as
1633
+ * a stream interruption that names the message.
1634
+ */
1635
+ function stream_error_frame(error, provider) {
1636
+ const message = error["message"];
1637
+ const code = error["code"];
1638
+ const detail = typeof message === "string" && message.length > 0 ? message : "(no message)";
1639
+ const label = code === void 0 ? "" : ` (code ${JSON.stringify(code)})`;
1640
+ return new provider_error(`${provider} native: the server reported an error mid-stream${label}: ${detail}`, typeof code === "number" ? { status: code } : {});
1641
+ }
1642
+ /**
1643
+ * Join streamed reasoning_details fragments into whole entries, the way
1644
+ * OpenRouter's own AI SDK provider does. Consecutive `reasoning.text` or
1645
+ * `reasoning.summary` fragments of one type grow one entry until the type
1646
+ * changes or a signature closes a text entry, and any other entry (encrypted,
1647
+ * a server tool call) stands alone. `index` is ignored, since providers
1648
+ * repeat it. A later fragment fills only the fields its entry still lacks, so
1649
+ * the late signature lands and the first format stays. add() returns the
1650
+ * fragment's visible text, '' when it has none.
1651
+ */
1652
+ function create_reasoning_joiner() {
1653
+ const entries = [];
1654
+ let open;
1655
+ return {
1656
+ entries,
1657
+ add(fragment) {
1658
+ if (!is_record$1(fragment)) return "";
1659
+ const field = REASONING_TEXT_FIELD.get(fragment["type"]);
1660
+ if (field === void 0) {
1661
+ entries.push(fragment);
1662
+ open = void 0;
1663
+ return "";
1664
+ }
1665
+ const text = reasoning_detail_text(fragment);
1666
+ const joinable = open?.["type"] === fragment["type"] ? open : void 0;
1667
+ let entry;
1668
+ if (joinable === void 0) {
1669
+ entry = { ...fragment };
1670
+ entries.push(entry);
1671
+ } else {
1672
+ entry = joinable;
1673
+ const joined = reasoning_detail_text(entry) + text;
1674
+ for (const [key, value] of Object.entries(fragment)) entry[key] ??= value;
1675
+ entry[field] = joined;
1676
+ }
1677
+ const signature = entry["signature"];
1678
+ open = typeof signature === "string" && signature.length > 0 ? void 0 : entry;
1679
+ return text;
1680
+ }
1606
1681
  };
1607
1682
  }
1608
1683
  /**
1609
1684
  * Consume Chat Completions stream frames (the JSON payload of each SSE
1610
1685
  * `data:` line, plus the literal `[DONE]` terminator), dispatching
1611
1686
  * StreamChunks as they arrive and rebuilding the non-stream payload as it
1612
- * goes: `delta.content` accumulates into the message text, and
1687
+ * goes: `delta.content` accumulates into the message text,
1613
1688
  * `delta.tool_calls[]` entries accumulate `function.arguments` string deltas
1614
- * keyed by their `index` field. A tool call closes (parsed input dispatched)
1689
+ * keyed by their `index` field, and under a reasoning_details dialect each
1690
+ * `delta.reasoning_details` fragment dispatches a reasoning chunk and joins
1691
+ * into the message's entries. A tool call closes (parsed input dispatched)
1615
1692
  * when the choice's finish_reason frame arrives, or at `[DONE]` for servers
1616
- * that never send one. Usage rides the final pre-DONE frame
1617
- * (stream_options.include_usage); a stream that ends without `[DONE]` is
1693
+ * that never send one. A frame with a top-level `error` throws before its
1694
+ * choice is read, on any dialect. Usage rides the final pre-DONE frame
1695
+ * (stream_options.include_usage), and OpenRouter's reported cost rides
1696
+ * inside it, so the streamed turn reads the same charge as the whole
1697
+ * response; a stream that ends without `[DONE]` is
1618
1698
  * truncated output, so complete() fails loud instead of returning a partial
1619
1699
  * turn. complete() feeds the synthetic payload through parse_chat_completion,
1620
1700
  * which is what makes the streamed TurnResult equal the non-streamed one by
@@ -1624,6 +1704,7 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
1624
1704
  let text = "";
1625
1705
  const tool_calls = [];
1626
1706
  const by_index = /* @__PURE__ */ new Map();
1707
+ const reasoning = create_reasoning_joiner();
1627
1708
  let finish_reason;
1628
1709
  let usage;
1629
1710
  let done = false;
@@ -1685,9 +1766,31 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
1685
1766
  });
1686
1767
  }
1687
1768
  };
1769
+ const on_reasoning_delta = async (delta) => {
1770
+ if (dialect.reasoning !== "reasoning_details") return;
1771
+ const fragments = Reflect.get(delta, "reasoning_details");
1772
+ if (Array.isArray(fragments) && fragments.length > 0) {
1773
+ for (const fragment of fragments) {
1774
+ const shown = reasoning.add(fragment);
1775
+ if (shown.length > 0) await dispatch({
1776
+ kind: "reasoning",
1777
+ text: shown,
1778
+ step_index
1779
+ });
1780
+ }
1781
+ return;
1782
+ }
1783
+ const bare = Reflect.get(delta, "reasoning");
1784
+ if (typeof bare === "string" && bare.length > 0) await dispatch({
1785
+ kind: "reasoning",
1786
+ text: bare,
1787
+ step_index
1788
+ });
1789
+ };
1688
1790
  const on_choice = async (choice) => {
1689
1791
  const delta = Reflect.get(choice, "delta");
1690
1792
  if (delta !== null && typeof delta === "object") {
1793
+ await on_reasoning_delta(delta);
1691
1794
  const content = Reflect.get(delta, "content");
1692
1795
  if (typeof content === "string" && content.length > 0) {
1693
1796
  text += content;
@@ -1727,6 +1830,8 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
1727
1830
  throw new provider_error(`${dialect.name} native: stream frame is not valid JSON`);
1728
1831
  }
1729
1832
  if (frame === null || typeof frame !== "object") return;
1833
+ const error = Reflect.get(frame, "error");
1834
+ if (is_record$1(error)) throw stream_error_frame(error, dialect.name);
1730
1835
  const raw_usage = Reflect.get(frame, "usage");
1731
1836
  if (raw_usage !== null && typeof raw_usage === "object") usage = raw_usage;
1732
1837
  const choices = Reflect.get(frame, "choices");
@@ -1738,7 +1843,8 @@ function create_chat_stream_aggregator(dialect, step_index, dispatch) {
1738
1843
  const ordered = in_index_order();
1739
1844
  const message = {
1740
1845
  role: "assistant",
1741
- content: text.length > 0 || ordered.length === 0 ? text : null
1846
+ content: text.length > 0 || ordered.length === 0 ? text : null,
1847
+ reasoning_details: reasoning.entries
1742
1848
  };
1743
1849
  if (ordered.length > 0) message["tool_calls"] = ordered.map((call) => ({
1744
1850
  id: call.id,
@@ -1814,26 +1920,6 @@ const create_openai_compatible_adapter = (dialect) => {
1814
1920
  };
1815
1921
  //#endregion
1816
1922
  //#region src/engine/providers/openai.ts
1817
- const OPENAI_REASONING_EFFORT = {
1818
- low: "low",
1819
- medium: "medium",
1820
- high: "high",
1821
- xhigh: "high",
1822
- max: "high"
1823
- };
1824
- /**
1825
- * Map an EffortLevel to OpenAI's `reasoningEffort` provider option.
1826
- */
1827
- function translate_openai_effort(effort) {
1828
- if (effort === "none") return {
1829
- provider_options: {},
1830
- effort_ignored: false
1831
- };
1832
- return {
1833
- provider_options: { openai: { reasoningEffort: OPENAI_REASONING_EFFORT[effort] } },
1834
- effort_ignored: false
1835
- };
1836
- }
1837
1923
  /**
1838
1924
  * Normalize OpenAI's raw usage payload into UsageTotals.
1839
1925
  */
@@ -1883,6 +1969,7 @@ const create_openai_native_adapter = (init) => {
1883
1969
  token_limit_field: "max_completion_tokens",
1884
1970
  stream_include_usage: true,
1885
1971
  tolerant_usage: false,
1972
+ reports_cost: false,
1886
1973
  ...organization !== void 0 ? { extra_headers: { "OpenAI-Organization": organization } } : {}
1887
1974
  };
1888
1975
  return create_openai_compatible_adapter(dialect);
@@ -1906,34 +1993,13 @@ const create_openai_ai_sdk_adapter = (init) => {
1906
1993
  if (organization !== void 0) config.organization = organization;
1907
1994
  return sdk.createOpenAI(config)(model_id);
1908
1995
  },
1909
- translate_effort: translate_openai_effort,
1996
+ translate_effort: translate_effort_to_sdk_reasoning,
1910
1997
  normalize_usage: normalize_openai_usage,
1911
1998
  supports: (capability) => SUPPORTED$7.has(capability)
1912
1999
  };
1913
2000
  };
1914
2001
  //#endregion
1915
2002
  //#region src/engine/providers/google.ts
1916
- const GOOGLE_THINKING_BUDGET = {
1917
- low: 1024,
1918
- medium: 8192,
1919
- high: 24576,
1920
- xhigh: 32768,
1921
- max: 32768
1922
- };
1923
- /**
1924
- * Map an EffortLevel to Google's `thinkingConfig.thinkingBudget` provider
1925
- * option.
1926
- */
1927
- function translate_google_effort(effort) {
1928
- if (effort === "none") return {
1929
- provider_options: {},
1930
- effort_ignored: false
1931
- };
1932
- return {
1933
- provider_options: { google: { thinkingConfig: { thinkingBudget: GOOGLE_THINKING_BUDGET[effort] } } },
1934
- effort_ignored: false
1935
- };
1936
- }
1937
2003
  /**
1938
2004
  * Normalize Google's raw usage payload into UsageTotals.
1939
2005
  */
@@ -1965,7 +2031,7 @@ const create_google_adapter = (init) => {
1965
2031
  if (base_url !== void 0) config.baseURL = base_url;
1966
2032
  return sdk.createGoogleGenerativeAI(config)(model_id);
1967
2033
  },
1968
- translate_effort: translate_google_effort,
2034
+ translate_effort: translate_effort_to_sdk_reasoning,
1969
2035
  normalize_usage: normalize_google_usage,
1970
2036
  supports: (capability) => SUPPORTED$6.has(capability)
1971
2037
  };
@@ -1989,14 +2055,15 @@ function to_user_content(content) {
1989
2055
  /**
1990
2056
  * Map assistant message content to this wire's message shape: text parts
1991
2057
  * join into `content`, and tool-call parts become `tool_calls` entries with
1992
- * structured (not JSON-string) arguments.
2058
+ * structured (not JSON-string) arguments. Reasoning parts are never sent:
2059
+ * this adapter maps no `thinking` field in either direction.
1993
2060
  */
1994
2061
  function to_assistant_message(content) {
1995
2062
  if (typeof content === "string") return {
1996
2063
  role: "assistant",
1997
2064
  content
1998
2065
  };
1999
- const { texts, tool_parts } = split_assistant_parts(content);
2066
+ const { texts, tool_parts } = split_assistant_parts(content, "ollama");
2000
2067
  const message = {
2001
2068
  role: "assistant",
2002
2069
  content: texts.join("")
@@ -2404,7 +2471,8 @@ const create_lmstudio_native_adapter = (init) => {
2404
2471
  auth: { kind: "none" },
2405
2472
  token_limit_field: "max_tokens",
2406
2473
  stream_include_usage: true,
2407
- tolerant_usage: true
2474
+ tolerant_usage: true,
2475
+ reports_cost: false
2408
2476
  });
2409
2477
  };
2410
2478
  /**
@@ -2480,8 +2548,10 @@ const create_openrouter_adapter = (init) => {
2480
2548
  /**
2481
2549
  * Build the openrouter dialect and hand it to the shared OpenAI-compatible
2482
2550
  * core: Bearer auth, the optional `HTTP-Referer`/`X-Title` attribution
2483
- * headers, the `max_tokens` token-limit field, and strict usage. The
2484
- * empty-api_key guard rides on the core, which throws the same
2551
+ * headers, the `max_tokens` token-limit field, the nested effort shape,
2552
+ * reasoning read from and sent back as `reasoning_details`, strict usage,
2553
+ * and the cost OpenRouter reports on every response. The empty-api_key guard
2554
+ * rides on the core, which throws the same
2485
2555
  * `openrouter provider requires a non-empty api_key` engine_config_error.
2486
2556
  */
2487
2557
  const create_openrouter_native_adapter = (init) => {
@@ -2501,6 +2571,9 @@ const create_openrouter_native_adapter = (init) => {
2501
2571
  token_limit_field: "max_tokens",
2502
2572
  stream_include_usage: true,
2503
2573
  tolerant_usage: false,
2574
+ reports_cost: true,
2575
+ effort_shape: "nested",
2576
+ reasoning: "reasoning_details",
2504
2577
  ...Object.keys(extra_headers).length > 0 ? { extra_headers } : {}
2505
2578
  };
2506
2579
  return create_openai_compatible_adapter(dialect);
@@ -2535,31 +2608,6 @@ const create_openrouter_ai_sdk_adapter = (init) => {
2535
2608
  };
2536
2609
  //#endregion
2537
2610
  //#region src/engine/providers/bedrock.ts
2538
- const BEDROCK_THINKING_BUDGETS = {
2539
- none: 0,
2540
- low: 1024,
2541
- medium: 5e3,
2542
- high: 2e4,
2543
- xhigh: 32e3,
2544
- max: 64e3
2545
- };
2546
- /**
2547
- * Map an EffortLevel to the Bedrock `reasoningConfig` provider option.
2548
- */
2549
- function translate_bedrock_effort(effort) {
2550
- const budget = BEDROCK_THINKING_BUDGETS[effort];
2551
- if (budget === 0) return {
2552
- provider_options: {},
2553
- effort_ignored: false
2554
- };
2555
- return {
2556
- provider_options: { bedrock: { reasoningConfig: {
2557
- type: "enabled",
2558
- budgetTokens: budget
2559
- } } },
2560
- effort_ignored: false
2561
- };
2562
- }
2563
2611
  /**
2564
2612
  * Normalize Bedrock's raw usage payload into UsageTotals.
2565
2613
  */
@@ -2654,7 +2702,7 @@ const create_bedrock_adapter = (init) => {
2654
2702
  async build_model(model_id) {
2655
2703
  return (await load_optional_peer("@ai-sdk/amazon-bedrock")).createAmazonBedrock(to_sdk_config(statics, await resolve_credential_provider()))(model_id);
2656
2704
  },
2657
- translate_effort: translate_bedrock_effort,
2705
+ translate_effort: translate_effort_to_sdk_reasoning,
2658
2706
  normalize_usage: normalize_bedrock_usage,
2659
2707
  supports: (capability) => SUPPORTED$1.has(capability)
2660
2708
  };
@@ -2663,16 +2711,13 @@ const create_bedrock_adapter = (init) => {
2663
2711
  //#region src/engine/providers/claude_cli/constants.ts
2664
2712
  /**
2665
2713
  * Frozen constants for the claude_cli provider: startup and idle defaults,
2666
- * the escalation window, cost decomposition multipliers, and the auth
2667
- * failure pattern list.
2714
+ * the escalation window, and the auth failure pattern list.
2668
2715
  *
2669
2716
  * Arrays are Object.freeze-d at module load so downstream code cannot mutate
2670
2717
  * them.
2671
2718
  */
2672
2719
  const CLI_BINARY_DEFAULT = "claude";
2673
2720
  const SIGKILL_ESCALATION_MS = 2e3;
2674
- const CACHE_READ_MULTIPLIER = .1;
2675
- const CACHE_WRITE_MULTIPLIER = 1.25;
2676
2721
  const CLI_AUTH_ERROR_PATTERNS = Object.freeze([
2677
2722
  "authentication",
2678
2723
  "unauthorized",
@@ -3744,47 +3789,15 @@ function snapshot(state) {
3744
3789
  //#endregion
3745
3790
  //#region src/engine/providers/claude_cli/cost.ts
3746
3791
  /**
3747
- * Split a single `total_cost_usd` figure into an input/output cost
3748
- * breakdown, weighted by token counts.
3792
+ * Cost allocation from the CLI's reported `total_cost_usd`.
3749
3793
  *
3750
- * Cache-read and cache-write tokens are cheaper and pricier respectively
3751
- * than base input tokens, so they're weighted by `CACHE_READ_MULTIPLIER`
3752
- * and `CACHE_WRITE_MULTIPLIER` before the total is distributed
3753
- * proportionally across all weighted components. Returns an all-zero
3754
- * estimate when there's no usage or no cost to distribute.
3794
+ * The CLI reports a single total_cost_usd and per-turn usage; it does not
3795
+ * break cost down by component or by turn. This module splits the total
3796
+ * across turns proportional to each turn's output tokens, then hands each
3797
+ * turn's share to the engine's `decompose_total_cost` for the component
3798
+ * split. Sum equality is preserved exactly by giving any floating-point
3799
+ * rounding remainder to the last turn.
3755
3800
  */
3756
- function decompose_total_cost(total_cost_usd, usage) {
3757
- const input = Math.max(0, usage.input_tokens);
3758
- const output = Math.max(0, usage.output_tokens);
3759
- const cached = Math.max(0, usage.cached_input_tokens ?? 0);
3760
- const cache_write = Math.max(0, usage.cache_write_tokens ?? 0);
3761
- const input_weight = Math.max(0, input - cached - cache_write);
3762
- const output_weight = output;
3763
- const cached_weight = cached * CACHE_READ_MULTIPLIER;
3764
- const cache_write_weight = cache_write * CACHE_WRITE_MULTIPLIER;
3765
- const total_weight = input_weight + output_weight + cached_weight + cache_write_weight;
3766
- if (total_weight === 0 || total_cost_usd === 0) return {
3767
- total_usd: total_cost_usd,
3768
- input_usd: 0,
3769
- output_usd: 0,
3770
- currency: "USD",
3771
- is_estimate: true
3772
- };
3773
- const input_usd = total_cost_usd * input_weight / total_weight;
3774
- const output_usd = total_cost_usd * output_weight / total_weight;
3775
- const cached_usd_raw = total_cost_usd * cached_weight / total_weight;
3776
- const cache_write_usd_raw = total_cost_usd * cache_write_weight / total_weight;
3777
- const breakdown = {
3778
- total_usd: total_cost_usd,
3779
- input_usd,
3780
- output_usd,
3781
- currency: "USD",
3782
- is_estimate: true
3783
- };
3784
- if (cached > 0) breakdown.cached_input_usd = cached_usd_raw;
3785
- if (cache_write > 0) breakdown.cache_write_usd = cache_write_usd_raw;
3786
- return breakdown;
3787
- }
3788
3801
  /**
3789
3802
  * Split a run-level `total` into a per-turn share for each turn, proportional
3790
3803
  * to its output tokens, falling back to an equal split when every turn reports
@@ -5651,43 +5664,95 @@ function safe_json_stringify(value) {
5651
5664
  }
5652
5665
  }
5653
5666
  /**
5654
- * Build the assistant history message for a turn: plain text when there are
5655
- * no tool calls, otherwise structured text plus tool_call parts.
5667
+ * Build the assistant history message for a turn. A turn with `content`
5668
+ * keeps the provider's parts in its order, data included, so reasoning goes
5669
+ * back beside the text and calls it came with. Any other turn is plain text
5670
+ * when there are no tool calls, otherwise text plus tool_call parts.
5656
5671
  */
5657
- function build_assistant_message(text, tool_calls) {
5658
- if (tool_calls.length === 0) return {
5672
+ function build_assistant_message(turn, view, kept) {
5673
+ if (turn.content !== void 0) return {
5659
5674
  role: "assistant",
5660
- content: text
5675
+ content: history_parts(turn.content, view, kept)
5676
+ };
5677
+ if (kept.length === 0) return {
5678
+ role: "assistant",
5679
+ content: view.history_text
5661
5680
  };
5662
5681
  const parts = [];
5663
- if (text.length > 0) parts.push({
5682
+ if (view.history_text.length > 0) parts.push({
5664
5683
  type: "text",
5665
- text
5684
+ text: view.history_text
5666
5685
  });
5667
- for (const tc of tool_calls) parts.push({
5686
+ for (const tc of kept) parts.push(tool_call_part(tc));
5687
+ return {
5688
+ role: "assistant",
5689
+ content: parts
5690
+ };
5691
+ }
5692
+ /**
5693
+ * A raw call as a history part, carrying no provider data.
5694
+ */
5695
+ function tool_call_part(tc) {
5696
+ return {
5668
5697
  type: "tool_call",
5669
5698
  id: tc.id,
5670
5699
  name: tc.name,
5671
5700
  input: tc.input
5672
- });
5673
- return {
5674
- role: "assistant",
5675
- content: parts
5676
5701
  };
5677
5702
  }
5678
5703
  /**
5679
- * Derive the step's cost from pricing tables and record it.
5680
- *
5681
- * Missing pricing for a paid provider emits `pricing_missing` (deduped per
5682
- * call) and yields no breakdown; free providers skip the emit entirely.
5704
+ * A turn's own parts for history. A call the per-step clamp dropped leaves,
5705
+ * as it leaves the plain message, while everything around it stays.
5706
+ */
5707
+ function history_parts(content, view, kept) {
5708
+ if (view.outcome !== void 0) return salvaged_parts(content, view.history_text, kept);
5709
+ const kept_ids = new Set(kept.map((tc) => tc.id));
5710
+ return content.filter((part) => part.type !== "tool_call" || kept_ids.has(part.id));
5711
+ }
5712
+ /**
5713
+ * A salvaged turn's parts for history: the stripped text replaces the text
5714
+ * parts at the first one's place, and the salvaged calls follow the last
5715
+ * part. Salvage runs only on a turn with no native calls, so the provider
5716
+ * gave its calls no place of their own. The replacement carries no provider
5717
+ * data, since whatever the provider attached belonged to the text salvage
5718
+ * rewrote.
5683
5719
  */
5684
- function compute_and_record_cost(config, step_index, usage, step_span) {
5720
+ function salvaged_parts(content, stripped_text, kept) {
5721
+ const parts = [];
5722
+ let text_placed = false;
5723
+ for (const part of content) if (part.type !== "text") parts.push(part);
5724
+ else if (!text_placed) {
5725
+ text_placed = true;
5726
+ if (stripped_text.length > 0) parts.push({
5727
+ type: "text",
5728
+ text: stripped_text
5729
+ });
5730
+ }
5731
+ for (const tc of kept) parts.push(tool_call_part(tc));
5732
+ return parts;
5733
+ }
5734
+ /**
5735
+ * Derive the step's cost and record it.
5736
+ *
5737
+ * A cost the provider reported wins over any pricing row, since it is what
5738
+ * was charged: it is split by token weight and recorded as
5739
+ * `provider_reported`, and the pricing lookup never runs, so no
5740
+ * `pricing_missing` either. Otherwise the cost comes from the pricing tables,
5741
+ * where missing pricing for a paid provider emits `pricing_missing` (deduped
5742
+ * per call) and yields no breakdown; free providers skip the emit entirely.
5743
+ */
5744
+ function compute_and_record_cost(config, step_index, turn, step_span) {
5745
+ if (turn.reported_cost_usd !== void 0) {
5746
+ const reported = decompose_total_cost(turn.reported_cost_usd, turn.usage);
5747
+ record_cost(config.trajectory, step_index, reported, "provider_reported", step_span);
5748
+ return reported;
5749
+ }
5685
5750
  const pricing = config.resolve_pricing();
5686
5751
  if (pricing === void 0 && !FREE_PROVIDERS.has(config.provider)) {
5687
5752
  config.pricing_dedup.emit(config.provider, config.model_id);
5688
5753
  return;
5689
5754
  }
5690
- const breakdown = compute_cost(usage, pricing, config.provider);
5755
+ const breakdown = compute_cost(turn.usage, pricing, config.provider);
5691
5756
  if (breakdown !== void 0) record_cost(config.trajectory, step_index, breakdown, "engine_derived", step_span);
5692
5757
  return breakdown;
5693
5758
  }
@@ -6156,7 +6221,7 @@ function push_step_record(config, acc, step_index, turn, tool_calls, finish_reas
6156
6221
  usage: turn.usage,
6157
6222
  finish_reason
6158
6223
  };
6159
- const breakdown = compute_and_record_cost(config, step_index, turn.usage, step_span);
6224
+ const breakdown = compute_and_record_cost(config, step_index, turn, step_span);
6160
6225
  if (breakdown !== void 0) record.cost = breakdown;
6161
6226
  if (turn.timing !== void 0) record.timing = turn.timing;
6162
6227
  if (turn.provider_reported !== void 0) record.provider_reported = turn.provider_reported;
@@ -6178,7 +6243,7 @@ async function run_loop_step(config, tool_map, acc, step_index) {
6178
6243
  const turn = await invoke_turn(config, step_index, step_span);
6179
6244
  const view = await salvage_turn(config, turn, tool_map, step_index);
6180
6245
  const { kept, dropped } = clamp_calls(config, view.calls, step_index);
6181
- config.messages.push(build_assistant_message(view.history_text, kept));
6246
+ config.messages.push(build_assistant_message(turn, view, kept));
6182
6247
  if (kept.length === 0) {
6183
6248
  push_step_record(config, acc, step_index, turn, [], turn.finish_reason, step_span);
6184
6249
  return loop_result(acc, turn.text, turn.finish_reason, false);
@@ -6643,7 +6708,7 @@ async function build_ai_sdk_transport(cfg) {
6643
6708
  const provider_options = combined_provider_options !== void 0 && Object.keys(combined_provider_options).length > 0 ? combined_provider_options : void 0;
6644
6709
  let ai_sdk_invoke_mod;
6645
6710
  try {
6646
- ai_sdk_invoke_mod = await import("./invoke-tjTjZLHj.js");
6711
+ ai_sdk_invoke_mod = await import("./invoke-px31fffH.js");
6647
6712
  } catch (err) {
6648
6713
  throw missing_peer_error("ai", err);
6649
6714
  }
@@ -6659,6 +6724,7 @@ async function build_ai_sdk_transport(cfg) {
6659
6724
  temperature: opts.temperature,
6660
6725
  max_tokens: opts.max_tokens,
6661
6726
  top_p: opts.top_p,
6727
+ reasoning: effort_translation.reasoning,
6662
6728
  telemetry: cfg.telemetry
6663
6729
  }),
6664
6730
  retry_policy: cfg.retry_policy,
@@ -7209,4 +7275,4 @@ function create_engine(config) {
7209
7275
  //#endregion
7210
7276
  export { create_engine as t };
7211
7277
 
7212
- //# sourceMappingURL=engine-BDWB2jdn.js.map
7278
+ //# sourceMappingURL=engine-ha9xmgwE.js.map