@cldmv/llm-translator 0.5.0 → 0.7.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.
Files changed (50) hide show
  1. package/README.md +7 -7
  2. package/dist/api/convert/anthropic/messages/to/openai/chat/request.mjs +11 -11
  3. package/dist/api/convert/anthropic/messages/to/openai/chat/response.mjs +1 -1
  4. package/dist/api/convert/anthropic/messages/to/openai/chat/stream.mjs +1 -1
  5. package/dist/api/convert/openai/chat/to/anthropic/messages/request.mjs +4 -4
  6. package/dist/api/convert/openai/chat/to/anthropic/messages/response.mjs +1 -1
  7. package/dist/api/convert/openai/chat/to/anthropic/messages/stream.mjs +1 -1
  8. package/dist/api/detect.mjs +1 -1
  9. package/dist/api/driver/anthropic/detect.mjs +1 -1
  10. package/dist/api/driver/anthropic/info.mjs +1 -1
  11. package/dist/api/driver/compatible/info.mjs +1 -1
  12. package/dist/api/driver/deepseek/info.mjs +1 -1
  13. package/dist/api/driver/llamacpp/info.mjs +1 -1
  14. package/dist/api/driver/ollama/info.mjs +1 -1
  15. package/dist/api/driver/openai/info.mjs +1 -1
  16. package/dist/api/driver/sglang/info.mjs +1 -1
  17. package/dist/api/driver/vllm/info.mjs +1 -1
  18. package/dist/api/embeddings/send.mjs +2 -0
  19. package/dist/api/request.mjs +2 -2
  20. package/dist/api/send.mjs +1 -1
  21. package/dist/api/server/plugin.mjs +1 -1
  22. package/dist/lib/config.mjs +1 -1
  23. package/dist/lib/http.mjs +1 -1
  24. package/dist/lib/ids.mjs +1 -1
  25. package/dist/lib/record.mjs +3 -0
  26. package/dist/lib/routes.mjs +1 -1
  27. package/dist/lib/sampling.mjs +2 -0
  28. package/dist/lib/settings.mjs +1 -1
  29. package/dist/lib/strict.mjs +2 -0
  30. package/package.json +4 -3
  31. package/types/src/api/convert/anthropic/messages/to/openai/chat/request.d.mts +6 -1
  32. package/types/src/api/convert/openai/chat/to/anthropic/messages/request.d.mts +8 -1
  33. package/types/src/api/driver/anthropic/detect.d.mts +10 -23
  34. package/types/src/api/driver/anthropic/info.d.mts +4 -1
  35. package/types/src/api/driver/compatible/info.d.mts +2 -1
  36. package/types/src/api/driver/deepseek/info.d.mts +2 -1
  37. package/types/src/api/driver/llamacpp/info.d.mts +2 -1
  38. package/types/src/api/driver/ollama/info.d.mts +2 -1
  39. package/types/src/api/driver/openai/info.d.mts +2 -1
  40. package/types/src/api/driver/sglang/info.d.mts +2 -1
  41. package/types/src/api/driver/vllm/info.d.mts +2 -1
  42. package/types/src/api/embeddings/send.d.mts +23 -0
  43. package/types/src/api/server/plugin.d.mts +1 -1
  44. package/types/src/index.d.mts +5 -0
  45. package/types/src/lib/http.d.mts +8 -2
  46. package/types/src/lib/ids.d.mts +40 -2
  47. package/types/src/lib/record.d.mts +41 -0
  48. package/types/src/lib/sampling.d.mts +13 -0
  49. package/types/src/lib/settings.d.mts +5 -0
  50. package/types/src/lib/strict.d.mts +26 -0
package/README.md CHANGED
@@ -17,18 +17,18 @@ Use it as a library when you want the conversion without any I/O (`request`, `re
17
17
 
18
18
  ## ✨ What's New
19
19
 
20
- ### Latest: v0.5.0 (November 2026)
20
+ ### Latest: v0.7.0 (November 2026)
21
21
 
22
- - **Events and opt-in logging** — everything the library logs is now an event: `translator.events.on(name, fn)`, `"*"` and an `onEvent` config callback, with general `log`/`warning`/`error` events alongside specific ones such as `fallback` and `usage`. Logging is off by default as a library; `logs: true` prints to the console and `logs: "<path>"` writes JSON lines.
23
- - **Also:** `response_format: json_object` reaches Anthropic upstreams as a schema, falling back to a system instruction when an upstream refuses it; refusals carry `stop_details`.
24
- - [View full v0.5.0 Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.5.0.md)
22
+ - **Anthropic-protocol servers detected by probe** — an Anthropic-compatible server on any host is now recognised from its `GET /v1/models` answer, matched strictly on the Anthropic model-list shape, so it no longer needs `backend: "anthropic"`. api.anthropic.com is still matched by host name.
23
+ - **Tool-call ids, embeddings usage and capture** — a driver can declare a tool-call id format with `info().toolCallIds`, and ids are rewritten into it and mapped back statelessly. Embeddings usage now reaches `onUsage`. `capture` records every failed-over attempt, linked by `exchange`, and captures embeddings too.
24
+ - [View full v0.7.0 Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.7.0.md)
25
25
 
26
26
  ### Recent Releases
27
27
 
28
+ - **v0.6.0** (November 2026) — `POST /v1/embeddings` pass-through, sampling extensions per upstream and per request, `strict` tool use end to end with a schema check, and the `capture` option for debugging ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.6.0.md))
29
+ - **v0.5.0** (November 2026) — everything the library logs is available as events, logging is opt-in through `logs`, `json_object` reaches Anthropic upstreams as a schema with a fallback, and refusals carry `stop_details` ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.5.0.md))
28
30
  - **v0.4.0** (November 2026) — per-model effort levels and aliases that pin effort and thinking, non-standard finish reasons, a configurable tool-error marker, per-upstream `pingInterval` and a Node.js 22.15 floor ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.4.0.md))
29
31
  - **v0.3.1** (November 2026) — patched the dev-only `minimatch` and `katex` lint dependencies and corrected the v0.3.0 issue links; no runtime changes ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.3.1.md))
30
- - **v0.3.0** (November 2026) — usage reporting from `send()`, the `route()` leaf and the server `authorize` hook; drivers for llama.cpp, Ollama, SGLang and the DeepSeek API, endpoint pools, `reasoningRoundTrip` and `toolSearch`; requires Node.js 24 ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.3.0.md))
31
- - **v0.2.0** (October 2026) — the first working translator: six Anthropic Messages and OpenAI Chat Completions converters, backend detection (vLLM, OpenAI, Anthropic, generic), model aliases, the optional Fastify server and the `llm-translator` CLI for Claude Code ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.2.0.md))
32
32
 
33
33
  📚 **For complete version history and detailed release notes, see the [docs/changelog/](https://github.com/CLDMV/llm-translator/tree/master/docs/changelog/) folder.**
34
34
 
@@ -127,7 +127,7 @@ Then point Claude Code at it with `ANTHROPIC_BASE_URL=http://127.0.0.1:8787`. Th
127
127
 
128
128
  ## 📚 Configuration
129
129
 
130
- `createTranslator(config)` takes `front`, `upstreams` (URLs or objects with `url`, `apiKey`, `backend`, `models`, `default` and `settings`), `aliases` (a model name, or `{ model, effort, thinking }` to pin settings), `efforts` (the reasoning levels each model supports, see [docs/settings.md](https://github.com/CLDMV/llm-translator/blob/master/docs/settings.md#effort-levels-and-pinned-aliases-not-converter-settings)), global `settings`, `pool` (failover and load balancing between upstreams that serve the same model, see [docs/server.md](https://github.com/CLDMV/llm-translator/blob/master/docs/server.md#endpoint-pools-failover-and-load-balancing)), `detect`, `logs` (off by default; `true` prints warnings, a path appends JSON lines), `events` and `onEvent` (see [docs/events.md](https://github.com/CLDMV/llm-translator/blob/master/docs/events.md)) and a custom `transport`. `silent: true` is a deprecated alias for `logs: false`. The CLI reads the same keys from flags, `LLM_TRANSLATOR_*` environment variables and a JSON file, in that order of precedence.
130
+ `createTranslator(config)` takes `front`, `upstreams` (URLs or objects with `url`, `apiKey`, `backend`, `models`, `default` and `settings`), `aliases` (a model name, or `{ model, effort, thinking }` to pin settings), `efforts` (the reasoning levels each model supports, see [docs/settings.md](https://github.com/CLDMV/llm-translator/blob/master/docs/settings.md#effort-levels-and-pinned-aliases-not-converter-settings)), global `settings`, `pool` (failover and load balancing between upstreams that serve the same model, see [docs/server.md](https://github.com/CLDMV/llm-translator/blob/master/docs/server.md#endpoint-pools-failover-and-load-balancing)), `detect`, `logs` (off by default; `true` prints warnings, a path appends JSON lines), `events` and `onEvent` (see [docs/events.md](https://github.com/CLDMV/llm-translator/blob/master/docs/events.md)), `capture` (off by default; a directory or a callback receives redacted request and response records for debugging, see [docs/events.md](https://github.com/CLDMV/llm-translator/blob/master/docs/events.md#capture-for-debugging)) and a custom `transport`. `silent: true` is a deprecated alias for `logs: false`. The CLI reads the same keys from flags, `LLM_TRANSLATOR_*` environment variables and a JSON file, in that order of precedence.
131
131
 
132
132
  Settings tune how the converters map one protocol onto the other (`inlineSystem`, `images`, `imageFallback`, `toolStreaming`, `pingInterval` and more); they are resolved per upstream from defaults, probed values, driver defaults and your overrides. The full table is in [docs/settings.md](https://github.com/CLDMV/llm-translator/blob/master/docs/settings.md).
133
133
 
@@ -1,17 +1,17 @@
1
- var N=Object.defineProperty;var m=(e,t)=>N(e,"name",{value:t,configurable:!0});import{resolve as q}from"../../../../../../../lib/settings.mjs";import{TranslatorError as T}from"../../../../../../../lib/errors.mjs";import{createNameFitter as O}from"../../../../../../../lib/ids.mjs";import{sanitize as B}from"../../../../../../../lib/schema.mjs";import{NOTE as I}from"../../../../../../../lib/images.mjs";import{expand as A}from"../../../../../../../lib/documents.mjs";import{createBudget as L,unsign as z}from"../../../../../../../lib/thinking.mjs";import{plan as C,scan as D}from"../../../../../../../lib/toolsearch.mjs";const P=new Set(["model","messages","system","max_tokens","stream","temperature","top_p","top_k","stop_sequences","tools","tool_choice","thinking","output_config","output_format","context_management"]),W=new Set(["metadata","cache_control","service_tier"]),H=new Set(["mcp_servers","container","inference_geo"]),K="x-anthropic-billing-header:";async function ue(e,t,o){const n=q(t);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new T("messages: Field required",{code:"invalid_request",param:"messages"});const i={front:"anthropic",model:e.model,stream:e.stream===!0,thinking:e.thinking?.type??null,reasoningField:n.reasoningField,toolNames:{},dropped:[]};o?.producer!==void 0&&(i.producer=o.producer);const r=O({max:64,map:i.toolNames});for(const c of e.tools??[])(c.type===void 0||c.type==="custom")&&r(c.name);const d=D(e.messages,new Set((e.tools??[]).map(c=>c.name))),l=C(e.tools??[],d,n,e.tool_choice);i.toolSearch={mode:n.toolSearch,loaded:l.loaded,hidden:l.hidden,stubbed:l.stubbed,capped:d.capped};for(const c of Object.keys(e))if(!(P.has(c)||W.has(c)))if(H.has(c))g(i,c);else{if(n.unknownFields==="error")throw new T(`${c}: Extra inputs are not permitted`,{code:"unsupported_field",param:c});g(i,c)}const s={model:e.model},h=F(e.system,n),k=await R(e.messages,n,i,r,h,U(e.context_management,i),o?.budget??L());s.messages=k.messages,k.prefill&&(s.continue_final_message=!0,s.add_generation_prompt=!1),e.max_tokens!==void 0&&(s[n.maxTokensField]=e.max_tokens),e.temperature!==void 0&&(s.temperature=e.temperature),e.top_p!==void 0&&(s.top_p=e.top_p),e.top_k!==void 0&&(n.topK?s.top_k=e.top_k:g(i,"top_k")),e.stop_sequences?.length>0&&(s.stop=e.stop_sequences),i.stream&&(s.stream=!0,n.streamUsage&&(s.stream_options={include_usage:!0},n.continuousUsage&&(s.stream_options.continuous_usage_stats=!0)));const f=X(l.offer,n,i,r);if(f.length>0){s.tools=f;const c=Y(e.tool_choice,n,i,r);c!==void 0&&(s.tool_choice=c),e.tool_choice?.disable_parallel_tool_use===!0&&n.parallelToolCalls&&(s.parallel_tool_calls=!1)}return Z(e,n,s,o?.listed===!0),b(e,n,i,s),{body:s,state:i}}m(ue,"request");function g(e,t){e.dropped.includes(t)||e.dropped.push(t)}m(g,"drop");function v(e,t,o){if(t.unknownFields==="error")throw new T(`${o}: is not supported by this translator`,{code:"unsupported_content",param:o});g(e,o)}m(v,"unsupported");function F(e,t){return e==null?"":(typeof e=="string"?[{type:"text",text:e}]:e).filter(n=>n.type==="text"&&!(t.stripBillingHeader&&n.text.trimStart().startsWith(K))).map(n=>n.text).join(`
1
+ var N=Object.defineProperty;var _=(e,t)=>N(e,"name",{value:t,configurable:!0});import{resolve as O}from"../../../../../../../lib/settings.mjs";import{TranslatorError as j}from"../../../../../../../lib/errors.mjs";import{createIdFitter as q,createNameFitter as B}from"../../../../../../../lib/ids.mjs";import{sanitize as C}from"../../../../../../../lib/schema.mjs";import{NOTE as A}from"../../../../../../../lib/images.mjs";import{expand as L}from"../../../../../../../lib/documents.mjs";import{createBudget as z,unsign as D}from"../../../../../../../lib/thinking.mjs";import{plan as P,scan as W}from"../../../../../../../lib/toolsearch.mjs";const H=new Set(["model","messages","system","max_tokens","stream","temperature","top_p","top_k","stop_sequences","tools","tool_choice","thinking","output_config","output_format","context_management"]),K=new Set(["metadata","cache_control","service_tier"]),R=new Set(["mcp_servers","container","inference_geo"]),U="x-anthropic-billing-header:";async function pe(e,t,o){const n=O(t);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new j("messages: Field required",{code:"invalid_request",param:"messages"});const s={front:"anthropic",model:e.model,stream:e.stream===!0,thinking:e.thinking?.type??null,reasoningField:n.reasoningField,toolNames:{},toolIds:{},dropped:[]};o?.producer!==void 0&&(s.producer=o.producer);const r=B({max:64,map:s.toolNames}),d=o?.toolCallIds?q({policy:o.toolCallIds,ids:G(e.messages),map:s.toolIds}):c=>c;for(const c of e.tools??[])(c.type===void 0||c.type==="custom")&&r(c.name);const m=W(e.messages,new Set((e.tools??[]).map(c=>c.name))),a=P(e.tools??[],m,n,e.tool_choice);s.toolSearch={mode:n.toolSearch,loaded:a.loaded,hidden:a.hidden,stubbed:a.stubbed,capped:m.capped};for(const c of Object.keys(e))if(!(H.has(c)||K.has(c)))if(R.has(c))x(s,c);else{if(n.unknownFields==="error")throw new j(`${c}: Extra inputs are not permitted`,{code:"unsupported_field",param:c});x(s,c)}const u={model:e.model},$=I(e.system,n),f=await J(e.messages,n,s,r,d,$,Q(e.context_management,s),o?.budget??z());u.messages=f.messages,f.prefill&&(u.continue_final_message=!0,u.add_generation_prompt=!1),e.max_tokens!==void 0&&(u[n.maxTokensField]=e.max_tokens),e.temperature!==void 0&&(u.temperature=e.temperature),e.top_p!==void 0&&(u.top_p=e.top_p),e.top_k!==void 0&&(n.topK?u.top_k=e.top_k:x(s,"top_k")),e.stop_sequences?.length>0&&(u.stop=e.stop_sequences),s.stream&&(u.stream=!0,n.streamUsage&&(u.stream_options={include_usage:!0},n.continuousUsage&&(u.stream_options.continuous_usage_stats=!0)));const h=b(a.offer,n,s,r);if(h.length>0){u.tools=h;const c=ee(e.tool_choice,n,s,r);c!==void 0&&(u.tool_choice=c),e.tool_choice?.disable_parallel_tool_use===!0&&n.parallelToolCalls&&(u.parallel_tool_calls=!1)}return te(e,n,u,o?.listed===!0),ne(e,n,s,u),{body:u,state:s}}_(pe,"request");function x(e,t){e.dropped.includes(t)||e.dropped.push(t)}_(x,"drop");function F(e,t,o){if(t.unknownFields==="error")throw new j(`${o}: is not supported by this translator`,{code:"unsupported_content",param:o});x(e,o)}_(F,"unsupported");function I(e,t){return e==null?"":(typeof e=="string"?[{type:"text",text:e}]:e).filter(n=>n.type==="text"&&!(t.stripBillingHeader&&n.text.trimStart().startsWith(U))).map(n=>n.text).join(`
2
2
 
3
- `)}m(F,"textOf");function j(e){return typeof e=="string"?[{type:"text",text:e}]:e??[]}m(j,"asBlocks");async function R(e,t,o,n,i,r,d){const l=[],s=[],h=new Map,k=t.inlineSystem==="auto"?"user":t.inlineSystem,f=new Map;if(t.reasoningRoundTrip){for(let a=e.length-1;a>=0;a--)if(e[a].role==="assistant")for(const u of[...j(e[a].content)].reverse())u.type==="thinking"&&f.set(u,await z(u.signature,{producer:o.producer,budget:d}))}const c=m(a=>{const u=l.at(-1);u?.role==="user"?u.parts.push(...a):l.push({role:"user",parts:a})},"pushUser"),w=m(async(a,u)=>{switch(a.type){case"text":return a.text===""?[]:[{type:"text",text:a.text}];case"image":return V(a,t,o);case"document":{const _=[];for(const p of await A(a,{settings:t,toolInputs:h,toolUseId:u}))_.push(...await w(p,u));return _}case"search_result":{const _=(a.content??[]).map(p=>p.text).join(`
4
- `);return[{type:"text",text:`[Search result: ${a.title??""} (${a.source??""})]
5
- ${_}`}]}case"tool_reference":return t.toolSearch==="native"&&t.deferredTools!=="drop"?[{type:"tool_reference",tool_name:a.tool_name}]:[{type:"text",text:o.toolSearch.loaded.includes(a.tool_name)?`[Tool loaded: ${a.tool_name}]`:`[Tool reference: ${a.tool_name}]`}];default:return v(o,t,`block:${a.type}`),[]}},"toParts");for(const a of e)if(a.role==="system"){const u=F(a.content,t);if(u===""||k==="drop")continue;if(k==="keep")l.push({role:"system",content:u});else if(k==="hoist")s.push(u);else{const _=u.trimStart().startsWith("<system-reminder>")?u:`<system-reminder>
6
- ${u}
7
- </system-reminder>`;c([{type:"text",text:_}])}}else if(a.role==="user"){const u=[],_=[];for(const y of j(a.content)){if(y.type!=="tool_result"){u.push(...await w(y,null));continue}const S=[];for(const x of j(y.content))S.push(...await w(x,y.tool_use_id));const M=S.filter(x=>x.type==="text").map(x=>x.text),E=S.filter(x=>x.type==="image_url");let $=S.some(x=>x.type==="tool_reference")?S.filter(x=>x.type!=="image_url"):M.join(`
8
- `);E.length>0&&(_.push({type:"text",text:`Image output of tool call ${y.tool_use_id}:`},...E),$===""&&($="(image output attached in the next message)")),y.is_error===!0&&t.toolErrors&&($=typeof $=="string"?`${t.toolErrorMarker}${$}`:[{type:"text",text:t.toolErrorMarker.trimEnd()},...$]),l.push({role:"tool",tool_call_id:y.tool_use_id,content:$})}const p=[..._,...u];p.length>0&&c(p)}else if(a.role==="assistant"){const u={role:"assistant",texts:[],parts:[],tool_calls:[]};for(const p of j(a.content))if(p.type==="text")p.text!==""&&u.texts.push(p.text);else if(p.type==="thinking"){const y=f.get(p)??null;y?u.parts.push({restored:y}):p.thinking&&u.parts.push({text:p.thinking})}else p.type==="redacted_thinking"?g(o,"redacted_thinking"):p.type==="tool_use"?(h.set(p.id,p.input),u.tool_calls.push({id:p.id,type:"function",function:{name:n(p.name),arguments:JSON.stringify(p.input??{})}})):v(o,t,`block:${p.type}`);const _=l.at(-1);_?.role==="assistant"?(_.texts.push(...u.texts),_.parts.push(...u.parts),_.tool_calls.push(...u.tool_calls)):l.push(u)}else throw new T(`messages: unsupported role "${a.role}"`,{code:"invalid_request",param:"messages"});return Q(l,t,i,s,r,o)}m(R,"convertMessages");function U(e,t){let o=1/0;for(const n of e?.edits??[])typeof n.type=="string"&&n.type.startsWith("clear_thinking")?n.keep==="all"?o=1/0:o=n.keep?.value??1:g(t,`context_management.${n.type}`);return o}m(U,"thinkingTurnsToKeep");function G(e,t){const o=e.find(s=>s.restored&&(s.restored.reasoning_content!==void 0||s.restored.reasoning!==void 0)),n=o?o.restored.reasoning_content!==void 0?"reasoning_content":"reasoning":t;let i="",r,d=null;for(const s of e){const h=s.restored?s.restored.reasoning_content??s.restored.reasoning:s.text;s.restored?.reasoning_details&&(r=[...r??[],...s.restored.reasoning_details]),h!==void 0&&(i+=(d!==null&&!(d.restored&&s.restored)?`
3
+ `)}_(I,"textOf");function T(e){return typeof e=="string"?[{type:"text",text:e}]:e??[]}_(T,"asBlocks");function G(e){const t=[];for(const o of e)for(const n of T(o.content))n.type==="tool_use"?t.push(n.id):n.type==="tool_result"&&t.push(n.tool_use_id);return t}_(G,"idsOf");async function J(e,t,o,n,s,r,d,m){const a=[],u=[],$=new Map,f=t.inlineSystem==="auto"?"user":t.inlineSystem,h=new Map;if(t.reasoningRoundTrip){for(let i=e.length-1;i>=0;i--)if(e[i].role==="assistant")for(const l of[...T(e[i].content)].reverse())l.type==="thinking"&&h.set(l,await D(l.signature,{producer:o.producer,budget:m}))}const c=_(i=>{const l=a.at(-1);l?.role==="user"?l.parts.push(...i):a.push({role:"user",parts:i})},"pushUser"),v=_(async(i,l)=>{switch(i.type){case"text":return i.text===""?[]:[{type:"text",text:i.text}];case"image":return Z(i,t,o);case"document":{const g=[];for(const p of await L(i,{settings:t,toolInputs:$,toolUseId:l}))g.push(...await v(p,l));return g}case"search_result":{const g=(i.content??[]).map(p=>p.text).join(`
4
+ `);return[{type:"text",text:`[Search result: ${i.title??""} (${i.source??""})]
5
+ ${g}`}]}case"tool_reference":return t.toolSearch==="native"&&t.deferredTools!=="drop"?[{type:"tool_reference",tool_name:i.tool_name}]:[{type:"text",text:o.toolSearch.loaded.includes(i.tool_name)?`[Tool loaded: ${i.tool_name}]`:`[Tool reference: ${i.tool_name}]`}];default:return F(o,t,`block:${i.type}`),[]}},"toParts");for(const i of e)if(i.role==="system"){const l=I(i.content,t);if(l===""||f==="drop")continue;if(f==="keep")a.push({role:"system",content:l});else if(f==="hoist")u.push(l);else{const g=l.trimStart().startsWith("<system-reminder>")?l:`<system-reminder>
6
+ ${l}
7
+ </system-reminder>`;c([{type:"text",text:g}])}}else if(i.role==="user"){const l=[],g=[];for(const k of T(i.content)){if(k.type!=="tool_result"){l.push(...await v(k,null));continue}const S=[];for(const y of T(k.content))S.push(...await v(y,k.tool_use_id));const M=S.filter(y=>y.type==="text").map(y=>y.text),E=S.filter(y=>y.type==="image_url");let w=S.some(y=>y.type==="tool_reference")?S.filter(y=>y.type!=="image_url"):M.join(`
8
+ `);E.length>0&&(g.push({type:"text",text:`Image output of tool call ${s(k.tool_use_id)}:`},...E),w===""&&(w="(image output attached in the next message)")),k.is_error===!0&&t.toolErrors&&(w=typeof w=="string"?`${t.toolErrorMarker}${w}`:[{type:"text",text:t.toolErrorMarker.trimEnd()},...w]),a.push({role:"tool",tool_call_id:s(k.tool_use_id),content:w})}const p=[...g,...l];p.length>0&&c(p)}else if(i.role==="assistant"){const l={role:"assistant",texts:[],parts:[],tool_calls:[]};for(const p of T(i.content))if(p.type==="text")p.text!==""&&l.texts.push(p.text);else if(p.type==="thinking"){const k=h.get(p)??null;k?l.parts.push({restored:k}):p.thinking&&l.parts.push({text:p.thinking})}else p.type==="redacted_thinking"?x(o,"redacted_thinking"):p.type==="tool_use"?($.set(p.id,p.input),l.tool_calls.push({id:s(p.id),type:"function",function:{name:n(p.name),arguments:JSON.stringify(p.input??{})}})):F(o,t,`block:${p.type}`);const g=a.at(-1);g?.role==="assistant"?(g.texts.push(...l.texts),g.parts.push(...l.parts),g.tool_calls.push(...l.tool_calls)):a.push(l)}else throw new j(`messages: unsupported role "${i.role}"`,{code:"invalid_request",param:"messages"});return Y(a,t,r,u,d,o)}_(J,"convertMessages");function Q(e,t){let o=1/0;for(const n of e?.edits??[])typeof n.type=="string"&&n.type.startsWith("clear_thinking")?n.keep==="all"?o=1/0:o=n.keep?.value??1:x(t,`context_management.${n.type}`);return o}_(Q,"thinkingTurnsToKeep");function V(e,t){const o=e.find(a=>a.restored&&(a.restored.reasoning_content!==void 0||a.restored.reasoning!==void 0)),n=o?o.restored.reasoning_content!==void 0?"reasoning_content":"reasoning":t;let s="",r,d=null;for(const a of e){const u=a.restored?a.restored.reasoning_content??a.restored.reasoning:a.text;a.restored?.reasoning_details&&(r=[...r??[],...a.restored.reasoning_details]),u!==void 0&&(s+=(d!==null&&!(d.restored&&a.restored)?`
9
9
 
10
- `:"")+h,d=s)}const l={};return i!==""&&(l[n]=i),r&&(l.reasoning_details=r),l}m(G,"reasoningFields");function J(e){return!!(e.reasoning_content||e.reasoning||e.reasoning_details)}m(J,"reasoningSent");function Q(e,t,o,n,i,r){let d=i;for(let f=e.length-1;f>=0;f--)e[f].role!=="assistant"||e[f].parts.length===0||(d>0?d--:e[f].parts=[]);const l=[],s=[o,...n].filter(f=>f!=="").join(`
10
+ `:"")+u,d=a)}const m={};return s!==""&&(m[n]=s),r&&(m.reasoning_details=r),m}_(V,"reasoningFields");function X(e){return!!(e.reasoning_content||e.reasoning||e.reasoning_details)}_(X,"reasoningSent");function Y(e,t,o,n,s,r){let d=s;for(let f=e.length-1;f>=0;f--)e[f].role!=="assistant"||e[f].parts.length===0||(d>0?d--:e[f].parts=[]);const m=[],a=[o,...n].filter(f=>f!=="").join(`
11
11
 
12
- `);s!==""&&l.push({role:"system",content:s});for(const f of e)if(f.role==="user"){const c=f.parts.every(w=>w.type==="text");l.push({role:"user",content:c?f.parts.map(w=>w.text).join(`
12
+ `);a!==""&&m.push({role:"system",content:a});for(const f of e)if(f.role==="user"){const h=f.parts.every(c=>c.type==="text");m.push({role:"user",content:h?f.parts.map(c=>c.text).join(`
13
13
 
14
- `):f.parts})}else if(f.role==="assistant"){const c={role:"assistant",content:f.texts.join(`
14
+ `):f.parts})}else if(f.role==="assistant"){const h={role:"assistant",content:f.texts.join(`
15
15
 
16
- `)};f.parts.length>0&&Object.assign(c,G(f.parts,t.reasoningField)),t.reasoningRoundTrip&&f.tool_calls.length>0&&!J(c)&&(r.reasoningMissing=(r.reasoningMissing??0)+1),f.tool_calls.length>0&&(c.tool_calls=f.tool_calls),l.push(c)}else l.push(f);const h=l.at(-1),k=t.prefill==="continue_final_message"&&h?.role==="assistant"&&!h.tool_calls&&h.content!=="";return{messages:l,prefill:k}}m(Q,"finalize");function V(e,t,o){if(!t.images)return g(o,"image"),[{type:"text",text:I}];const n=e.source;return n.type==="base64"?[{type:"image_url",image_url:{url:`data:${n.media_type};base64,${n.data}`}}]:n.type==="url"?[{type:"image_url",image_url:{url:n.url}}]:(g(o,"image.file"),[{type:"text",text:"[An image supplied as an uploaded file could not be forwarded.]"}])}m(V,"imageParts");function X(e,t,o,n){const i=[];for(const r of e){if(r.type!==void 0&&r.type!=="custom"){g(o,`tool:${r.name}`);continue}if(r.defer_loading===!0&&t.deferredTools==="drop"){g(o,"deferred_tools");continue}const d=r.input_schema??{type:"object",properties:{}},l={name:n(r.name),parameters:t.toolSchema==="sanitize"?B(d):d};r.description!==void 0&&(l.description=r.description),r.strict!==void 0&&(l.strict=r.strict),i.push(r.defer_loading===!0&&t.toolSearch==="native"?{type:"function",function:l,defer_loading:!0}:{type:"function",function:l})}return i}m(X,"convertTools");function Y(e,t,o,n){switch(e?.type){case void 0:return;case"auto":return"auto";case"any":return"required";case"none":return"none";case"tool":return{type:"function",function:{name:n(e.name)}};default:v(o,t,`tool_choice:${e.type}`);return}}m(Y,"convertToolChoice");function Z(e,t,o,n){const i=e.thinking,r=i?.type;let d;r==="disabled"?(t.thinking==="reasoning_effort"&&(d="none"),t.thinking==="chat_template_kwargs"&&(o.chat_template_kwargs={enable_thinking:!1})):(t.effort&&e.output_config?.effort!==void 0&&(d=e.output_config.effort),r==="enabled"&&(t.thinking==="chat_template_kwargs"&&(o.chat_template_kwargs={enable_thinking:!0},t.thinkingBudget&&i.budget_tokens!==void 0&&e.max_tokens!==void 0&&(o.thinking_token_budget=Math.max(0,Math.min(i.budget_tokens,e.max_tokens-1)))),t.thinking==="reasoning_effort"&&d===void 0&&!n&&(d="medium"))),d!==void 0&&(o.reasoning_effort=d)}m(Z,"applyThinking");function b(e,t,o,n){const i=e.output_config?.format??e.output_format;i!==void 0&&(t.responseFormat&&i.type==="json_schema"?n.response_format={type:"json_schema",json_schema:{name:"output",schema:i.schema}}:g(o,"output_config.format"));for(const r of Object.keys(e.output_config??{}))r!=="effort"&&r!=="format"&&g(o,`output_config.${r}`)}m(b,"applyOutputConfig");export{ue as request};
16
+ `)};f.parts.length>0&&Object.assign(h,V(f.parts,t.reasoningField)),t.reasoningRoundTrip&&f.tool_calls.length>0&&!X(h)&&(r.reasoningMissing=(r.reasoningMissing??0)+1),f.tool_calls.length>0&&(h.tool_calls=f.tool_calls),m.push(h)}else m.push(f);const u=m.at(-1),$=t.prefill==="continue_final_message"&&u?.role==="assistant"&&!u.tool_calls&&u.content!=="";return{messages:m,prefill:$}}_(Y,"finalize");function Z(e,t,o){if(!t.images)return x(o,"image"),[{type:"text",text:A}];const n=e.source;return n.type==="base64"?[{type:"image_url",image_url:{url:`data:${n.media_type};base64,${n.data}`}}]:n.type==="url"?[{type:"image_url",image_url:{url:n.url}}]:(x(o,"image.file"),[{type:"text",text:"[An image supplied as an uploaded file could not be forwarded.]"}])}_(Z,"imageParts");function b(e,t,o,n){const s=[];for(const r of e){if(r.type!==void 0&&r.type!=="custom"){x(o,`tool:${r.name}`);continue}if(r.defer_loading===!0&&t.deferredTools==="drop"){x(o,"deferred_tools");continue}const d=r.input_schema??{type:"object",properties:{}},m={name:n(r.name),parameters:t.toolSchema==="sanitize"?C(d):d};r.description!==void 0&&(m.description=r.description),r.strict!==void 0&&(m.strict=r.strict),s.push(r.defer_loading===!0&&t.toolSearch==="native"?{type:"function",function:m,defer_loading:!0}:{type:"function",function:m})}return s}_(b,"convertTools");function ee(e,t,o,n){switch(e?.type){case void 0:return;case"auto":return"auto";case"any":return"required";case"none":return"none";case"tool":return{type:"function",function:{name:n(e.name)}};default:F(o,t,`tool_choice:${e.type}`);return}}_(ee,"convertToolChoice");function te(e,t,o,n){const s=e.thinking,r=s?.type;let d;r==="disabled"?(t.thinking==="reasoning_effort"&&(d="none"),t.thinking==="chat_template_kwargs"&&(o.chat_template_kwargs={enable_thinking:!1})):(t.effort&&e.output_config?.effort!==void 0&&(d=e.output_config.effort),r==="enabled"&&(t.thinking==="chat_template_kwargs"&&(o.chat_template_kwargs={enable_thinking:!0},t.thinkingBudget&&s.budget_tokens!==void 0&&e.max_tokens!==void 0&&(o.thinking_token_budget=Math.max(0,Math.min(s.budget_tokens,e.max_tokens-1)))),t.thinking==="reasoning_effort"&&d===void 0&&!n&&(d="medium"))),d!==void 0&&(o.reasoning_effort=d)}_(te,"applyThinking");function ne(e,t,o,n){const s=e.output_config?.format??e.output_format;s!==void 0&&(t.responseFormat&&s.type==="json_schema"?n.response_format={type:"json_schema",json_schema:{name:"output",schema:s.schema}}:x(o,"output_config.format"));for(const r of Object.keys(e.output_config??{}))r!=="effort"&&r!=="format"&&x(o,`output_config.${r}`)}_(ne,"applyOutputConfig");export{pe as request};
17
17
  //# sourceMappingURL=request.mjs.map
@@ -1,2 +1,2 @@
1
- var m=Object.defineProperty;var h=(o,e)=>m(o,"name",{value:e,configurable:!0});import{resolve as _}from"../../../../../../../lib/settings.mjs";import{restoreName as x}from"../../../../../../../lib/ids.mjs";import{detailOf as y}from"../../../../../../../lib/thinking.mjs";import{toOpenAIFinish as N,toOpenAIUsage as O}from"../../../../../../../lib/finish.mjs";function A(o,e,d){const c=_(d),g=e?.toolNames??{},s=[],l=[],n=[],r=[];let p=0,f=!1;for(const t of o.content??[]){if(c.reasoningRoundTrip){const u=y(t,p+(f?1:0));u&&r.push(u)}t.type==="text"&&t.text!==""&&(f=!0),t.type==="tool_use"&&p++,t.type==="text"?s.push(t.text):t.type==="thinking"?l.push(t.thinking):t.type==="tool_use"&&n.push({id:t.id,type:"function",function:{name:x(g,t.name),arguments:JSON.stringify(t.input??{})}})}const i={role:"assistant",content:s.length===0&&n.length>0?null:s.join("")};l.length>0&&(i[c.reasoningField]=l.join("")),r.length>0&&(i.reasoning_details=r),n.length>0&&(i.tool_calls=n);const a={index:0,message:i,finish_reason:N(o.stop_reason)};return typeof o.stop_sequence=="string"&&(a.stop_reason=o.stop_sequence),e&&(e.upstreamUsage=o.usage),{id:`chatcmpl-${String(o.id).replace(/^msg_/,"")}`,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:e?.model??o.model,choices:[a],usage:O(o.usage)}}h(A,"response");export{A as response};
1
+ var _=Object.defineProperty;var d=(o,e)=>_(o,"name",{value:e,configurable:!0});import{resolve as x}from"../../../../../../../lib/settings.mjs";import{restoreId as y,restoreName as I}from"../../../../../../../lib/ids.mjs";import{detailOf as N}from"../../../../../../../lib/thinking.mjs";import{toOpenAIFinish as O,toOpenAIUsage as j}from"../../../../../../../lib/finish.mjs";function F(o,e,h){const c=x(h),g=e?.toolNames??{},m=e?.toolIds??{},s=[],l=[],n=[],r=[];let p=0,f=!1;for(const t of o.content??[]){if(c.reasoningRoundTrip){const u=N(t,p+(f?1:0));u&&r.push(u)}t.type==="text"&&t.text!==""&&(f=!0),t.type==="tool_use"&&p++,t.type==="text"?s.push(t.text):t.type==="thinking"?l.push(t.thinking):t.type==="tool_use"&&n.push({id:y(m,t.id),type:"function",function:{name:I(g,t.name),arguments:JSON.stringify(t.input??{})}})}const i={role:"assistant",content:s.length===0&&n.length>0?null:s.join("")};l.length>0&&(i[c.reasoningField]=l.join("")),r.length>0&&(i.reasoning_details=r),n.length>0&&(i.tool_calls=n);const a={index:0,message:i,finish_reason:O(o.stop_reason)};return typeof o.stop_sequence=="string"&&(a.stop_reason=o.stop_sequence),e&&(e.upstreamUsage=o.usage),{id:`chatcmpl-${String(o.id).replace(/^msg_/,"")}`,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:e?.model??o.model,choices:[a],usage:j(o.usage)}}d(F,"response");export{F as response};
2
2
  //# sourceMappingURL=response.mjs.map
@@ -1,2 +1,2 @@
1
- var O=Object.defineProperty;var r=(d,o)=>O(d,"name",{value:o,configurable:!0});import{resolve as N}from"../../../../../../../lib/settings.mjs";import{anthropicToOpenAIType as U,anthropicTypeToStatus as j}from"../../../../../../../lib/errors.mjs";import{completionId as D,restoreName as T}from"../../../../../../../lib/ids.mjs";import{toOpenAIFinish as E,toOpenAIUsage as I}from"../../../../../../../lib/finish.mjs";import{messages as w}from"../../../../../../../lib/sse.mjs";import{detailOf as A}from"../../../../../../../lib/thinking.mjs";async function*R(d,o,k){const u=N(k),x=o?.toolNames??{},b=o?.includeUsage??!0,y=Math.floor(Date.now()/1e3),s=new Map;let c=D(),p=o?.model,a={},g=!1,n=null,_=!1,h=0;const i=r((e,t=null,l={})=>({data:{id:c,object:"chat.completion.chunk",created:y,model:p,choices:[{index:0,delta:e,finish_reason:t,...l}]}}),"chunk"),f=r((e,t)=>({data:{error:{message:t,type:U(e),param:null,code:j(e)}}}),"failure");try{for await(const{data:e}of w(d))if(e.type==="message_start")c=`chatcmpl-${String(e.message.id).replace(/^msg_/,"")}`,p=o?.model??e.message.model,a={...e.message.usage},o&&(o.upstreamUsage=a),yield i({role:"assistant",content:""});else if(e.type==="content_block_start"){const t=e.content_block;if((t.type==="thinking"||t.type==="redacted_thinking")&&(n={type:t.type,data:t.data,thinking:"",signature:"",slot:h+(_?1:0)}),t.type==="tool_use"&&h++,t.type==="tool_use"){s.set(e.index,s.size);const l=T(x,t.name);yield i({tool_calls:[{index:s.get(e.index),id:t.id,type:"function",function:{name:l,arguments:""}}]})}}else if(e.type==="content_block_delta"){const t=e.delta;t.type==="text_delta"?(t.text!==""&&(_=!0),yield i({content:t.text})):t.type==="thinking_delta"?(n&&(n.thinking+=t.thinking),yield i({[u.reasoningField]:t.thinking})):t.type==="signature_delta"?n&&(n.signature+=t.signature):t.type==="input_json_delta"&&s.has(e.index)&&(yield i({tool_calls:[{index:s.get(e.index),function:{arguments:t.partial_json}}]}))}else if(e.type==="content_block_stop"){const t=u.reasoningRoundTrip&&n?A(n,n.slot):null;t&&(yield i({reasoning_details:[t]})),n=null}else if(e.type==="message_delta"){for(const[l,m]of Object.entries(e.usage??{}))m!=null&&(a[l]=m);o&&(o.upstreamUsage=a);const t=typeof e.delta?.stop_sequence=="string"?{stop_reason:e.delta.stop_sequence}:{};yield i({},E(e.delta?.stop_reason),t)}else if(e.type==="message_stop"){g=!0;break}else if(e.type==="error"){yield f(e.error.type,e.error.message),yield{data:"[DONE]"};return}}catch(e){yield f(e.type??"api_error",e.message),yield{data:"[DONE]"};return}if(!g){yield f("api_error","Upstream stream ended before completion"),yield{data:"[DONE]"};return}b&&(yield{data:{id:c,object:"chat.completion.chunk",created:y,model:p,choices:[],usage:I(a)}}),yield{data:"[DONE]"}}r(R,"stream");export{R as stream};
1
+ var I=Object.defineProperty;var r=(d,o)=>I(d,"name",{value:o,configurable:!0});import{resolve as N}from"../../../../../../../lib/settings.mjs";import{anthropicToOpenAIType as U,anthropicTypeToStatus as j}from"../../../../../../../lib/errors.mjs";import{completionId as D,restoreId as T,restoreName as E}from"../../../../../../../lib/ids.mjs";import{toOpenAIFinish as w,toOpenAIUsage as A}from"../../../../../../../lib/finish.mjs";import{messages as B}from"../../../../../../../lib/sse.mjs";import{detailOf as q}from"../../../../../../../lib/thinking.mjs";async function*C(d,o,k){const u=N(k),x=o?.toolNames??{},b=o?.toolIds??{},O=o?.includeUsage??!0,y=Math.floor(Date.now()/1e3),s=new Map;let c=D(),p=o?.model,a={},g=!1,n=null,_=!1,h=0;const i=r((e,t=null,l={})=>({data:{id:c,object:"chat.completion.chunk",created:y,model:p,choices:[{index:0,delta:e,finish_reason:t,...l}]}}),"chunk"),f=r((e,t)=>({data:{error:{message:t,type:U(e),param:null,code:j(e)}}}),"failure");try{for await(const{data:e}of B(d))if(e.type==="message_start")c=`chatcmpl-${String(e.message.id).replace(/^msg_/,"")}`,p=o?.model??e.message.model,a={...e.message.usage},o&&(o.upstreamUsage=a),yield i({role:"assistant",content:""});else if(e.type==="content_block_start"){const t=e.content_block;if((t.type==="thinking"||t.type==="redacted_thinking")&&(n={type:t.type,data:t.data,thinking:"",signature:"",slot:h+(_?1:0)}),t.type==="tool_use"&&h++,t.type==="tool_use"){s.set(e.index,s.size);const l=E(x,t.name);yield i({tool_calls:[{index:s.get(e.index),id:T(b,t.id),type:"function",function:{name:l,arguments:""}}]})}}else if(e.type==="content_block_delta"){const t=e.delta;t.type==="text_delta"?(t.text!==""&&(_=!0),yield i({content:t.text})):t.type==="thinking_delta"?(n&&(n.thinking+=t.thinking),yield i({[u.reasoningField]:t.thinking})):t.type==="signature_delta"?n&&(n.signature+=t.signature):t.type==="input_json_delta"&&s.has(e.index)&&(yield i({tool_calls:[{index:s.get(e.index),function:{arguments:t.partial_json}}]}))}else if(e.type==="content_block_stop"){const t=u.reasoningRoundTrip&&n?q(n,n.slot):null;t&&(yield i({reasoning_details:[t]})),n=null}else if(e.type==="message_delta"){for(const[l,m]of Object.entries(e.usage??{}))m!=null&&(a[l]=m);o&&(o.upstreamUsage=a);const t=typeof e.delta?.stop_sequence=="string"?{stop_reason:e.delta.stop_sequence}:{};yield i({},w(e.delta?.stop_reason),t)}else if(e.type==="message_stop"){g=!0;break}else if(e.type==="error"){yield f(e.error.type,e.error.message),yield{data:"[DONE]"};return}}catch(e){yield f(e.type??"api_error",e.message),yield{data:"[DONE]"};return}if(!g){yield f("api_error","Upstream stream ended before completion"),yield{data:"[DONE]"};return}O&&(yield{data:{id:c,object:"chat.completion.chunk",created:y,model:p,choices:[],usage:A(a)}}),yield{data:"[DONE]"}}r(C,"stream");export{C as stream};
2
2
  //# sourceMappingURL=stream.mjs.map
@@ -1,8 +1,8 @@
1
- var v=Object.defineProperty;var f=(e,r)=>v(e,"name",{value:r,configurable:!0});import{resolve as N}from"../../../../../../../lib/settings.mjs";import{TranslatorError as b}from"../../../../../../../lib/errors.mjs";import{anthropicToolId as w,createNameFitter as O}from"../../../../../../../lib/ids.mjs";import{parseArguments as S}from"../../../../../../../lib/arguments.mjs";import{sanitize as q}from"../../../../../../../lib/schema.mjs";import{blocksOf as E}from"../../../../../../../lib/thinking.mjs";const I=new Set(["model","messages","max_tokens","max_completion_tokens","temperature","top_p","top_k","stop","stream","stream_options","tools","tool_choice","parallel_tool_calls","reasoning_effort","response_format","user","n"]),$=new Set(["metadata","store","service_tier"]),x=/^data:([^;,]+);base64,(.*)$/s;async function P(e,r){const i=N(r);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new b("messages is a required property",{code:"invalid_request",param:"messages"});if(e.n!==void 0&&e.n!==1)throw new b("n: only a single choice (n = 1) is supported",{code:"unsupported_field",param:"n"});const o={front:"openai",model:e.model,stream:e.stream===!0,includeUsage:e.stream_options?.include_usage===!0,reasoningField:i.reasoningField,toolNames:{},dropped:[]},a=O({max:128,map:o.toolNames});for(const t of Object.keys(e))if(!(I.has(t)||$.has(t))){if(i.unknownFields==="error")throw new b(`${t}: Unrecognized request argument`,{code:"unsupported_field",param:t});g(o,t)}const c=[],p=[],m=f((t,n)=>{const l=p.at(-1);l?.role===t?l.blocks.push(...n):p.push({role:t,blocks:[...n]})},"push");for(const t of e.messages)switch(t.role){case"system":case"developer":{const n=j(t.content);n!==""&&c.push(n);break}case"user":m("user",F(t.content,o));break;case"assistant":{const n=i.reasoningRoundTrip?E(t.reasoning_details):[],l=[],u=j(t.content);u!==""?l.push({type:"text",text:u}):typeof t.refusal=="string"&&t.refusal!==""&&l.push({type:"text",text:t.refusal}),M(t)&&!n.some(d=>d.block.type==="thinking")&&g(o,"reasoning");for(const d of t.tool_calls??[])l.push({type:"tool_use",id:w(d.id),name:a(d.function.name),input:S(d.function.arguments)});const k=A(n,l);k.length>0&&m("assistant",k);break}case"tool":{const n={type:"tool_result",tool_use_id:w(t.tool_call_id),content:j(t.content)};if(i.toolErrorInference){const u=i.toolErrorMarker.trimEnd();n.content.startsWith(u)&&(n.is_error=!0,n.content=n.content.slice(u.length).replace(/^\s/,""))}const l=p.at(-1);if(l?.role==="user"){const u=l.blocks.findIndex(k=>k.type!=="tool_result");l.blocks.splice(u===-1?l.blocks.length:u,0,n)}else p.push({role:"user",blocks:[n]});break}default:throw new b(`messages: unsupported role "${t.role}"`,{code:"invalid_request",param:"messages"})}const s={model:e.model,max_tokens:e.max_completion_tokens??e.max_tokens??i.defaultMaxTokens};c.length>0&&(s.system=c.join(`
1
+ var v=Object.defineProperty;var u=(e,s)=>v(e,"name",{value:s,configurable:!0});import{resolve as N}from"../../../../../../../lib/settings.mjs";import{TranslatorError as h}from"../../../../../../../lib/errors.mjs";import{anthropicToolId as O,createIdFitter as S,createNameFitter as q}from"../../../../../../../lib/ids.mjs";import{parseArguments as E}from"../../../../../../../lib/arguments.mjs";import{sanitize as $}from"../../../../../../../lib/schema.mjs";import{blocksOf as A}from"../../../../../../../lib/thinking.mjs";const F=new Set(["model","messages","max_tokens","max_completion_tokens","temperature","top_p","top_k","stop","stream","stream_options","tools","tool_choice","parallel_tool_calls","reasoning_effort","response_format","user","n"]),M=new Set(["metadata","store","service_tier"]),j=/^data:([^;,]+);base64,(.*)$/s;async function Q(e,s,a){const o=N(s);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new h("messages is a required property",{code:"invalid_request",param:"messages"});if(e.n!==void 0&&e.n!==1)throw new h("n: only a single choice (n = 1) is supported",{code:"unsupported_field",param:"n"});const r={front:"openai",model:e.model,stream:e.stream===!0,includeUsage:e.stream_options?.include_usage===!0,reasoningField:o.reasoningField,toolNames:{},toolIds:{},dropped:[]},m=q({max:128,map:r.toolNames}),f=a?.toolCallIds?S({policy:a.toolCallIds,ids:C(e.messages),map:r.toolIds}):O;for(const t of Object.keys(e))if(!(F.has(t)||M.has(t))){if(o.unknownFields==="error")throw new h(`${t}: Unrecognized request argument`,{code:"unsupported_field",param:t});k(r,t)}const c=[],_=[],I=u((t,n)=>{const l=_.at(-1);l?.role===t?l.blocks.push(...n):_.push({role:t,blocks:[...n]})},"push");for(const t of e.messages)switch(t.role){case"system":case"developer":{const n=w(t.content);n!==""&&c.push(n);break}case"user":I("user",z(t.content,r));break;case"assistant":{const n=o.reasoningRoundTrip?A(t.reasoning_details):[],l=[],p=w(t.content);p!==""?l.push({type:"text",text:p}):typeof t.refusal=="string"&&t.refusal!==""&&l.push({type:"text",text:t.refusal}),U(t)&&!n.some(g=>g.block.type==="thinking")&&k(r,"reasoning");for(const g of t.tool_calls??[])l.push({type:"tool_use",id:f(g.id),name:m(g.function.name),input:E(g.function.arguments)});const b=R(n,l);b.length>0&&I("assistant",b);break}case"tool":{const n={type:"tool_result",tool_use_id:f(t.tool_call_id),content:w(t.content)};if(o.toolErrorInference){const p=o.toolErrorMarker.trimEnd();n.content.startsWith(p)&&(n.is_error=!0,n.content=n.content.slice(p.length).replace(/^\s/,""))}const l=_.at(-1);if(l?.role==="user"){const p=l.blocks.findIndex(b=>b.type!=="tool_result");l.blocks.splice(p===-1?l.blocks.length:p,0,n)}else _.push({role:"user",blocks:[n]});break}default:throw new h(`messages: unsupported role "${t.role}"`,{code:"invalid_request",param:"messages"})}const i={model:e.model,max_tokens:e.max_completion_tokens??e.max_tokens??o.defaultMaxTokens};c.length>0&&(i.system=c.join(`
2
2
 
3
- `)),s.messages=p.map(t=>({role:t.role,content:t.blocks})),e.temperature!==void 0&&(s.temperature=Math.min(1,e.temperature)),e.top_p!==void 0&&(s.top_p=e.top_p),e.top_k!==void 0&&(s.top_k=e.top_k),e.stop!==void 0&&e.stop!==null&&(s.stop_sequences=[].concat(e.stop)),o.stream&&(s.stream=!0),typeof e.user=="string"&&(s.metadata={user_id:e.user});const y=[];for(const t of e.tools??[]){if(t.type!=="function"){g(o,`tool:${t.type}`);continue}const n=t.function,l=n.parameters??{type:"object",properties:{}},u={name:a(n.name),input_schema:i.toolSchema==="sanitize"?q(l):l};n.description!==void 0&&(u.description=n.description),y.push(u)}if(y.length>0){s.tools=y;let t=R(e.tool_choice,a);e.parallel_tool_calls===!1&&t.type!=="none"&&(t={...t,disable_parallel_tool_use:!0}),(e.tool_choice!==void 0||e.parallel_tool_calls===!1)&&(s.tool_choice=t)}const _=e.reasoning_effort;_==="none"?s.thinking={type:"disabled"}:_!=null&&(s.thinking={type:"adaptive"},s.output_config={effort:_==="minimal"?"low":_});const h=e.response_format;return h?.type==="json_schema"?s.output_config={...s.output_config,format:{type:"json_schema",schema:h.json_schema.schema}}:h?.type==="json_object"&&(i.jsonObject==="schema"?(s.output_config={...s.output_config,format:{type:"json_schema",schema:{type:"object"}}},o.jsonSchemaEmulated=!0):i.jsonObject==="instruction"?s.system=s.system===void 0?T:`${s.system}
3
+ `)),i.messages=_.map(t=>({role:t.role,content:t.blocks})),e.temperature!==void 0&&(i.temperature=Math.min(1,e.temperature)),e.top_p!==void 0&&(i.top_p=e.top_p),e.top_k!==void 0&&(i.top_k=e.top_k),e.stop!==void 0&&e.stop!==null&&(i.stop_sequences=[].concat(e.stop)),r.stream&&(i.stream=!0),typeof e.user=="string"&&(i.metadata={user_id:e.user});const y=[];for(const t of e.tools??[]){if(t.type!=="function"){k(r,`tool:${t.type}`);continue}const n=t.function,l=n.parameters??{type:"object",properties:{}},p={name:m(n.name),input_schema:o.toolSchema==="sanitize"?$(l):l};n.description!==void 0&&(p.description=n.description),n.strict!==void 0&&(p.strict=n.strict),y.push(p)}if(y.length>0){i.tools=y;let t=D(e.tool_choice,m);e.parallel_tool_calls===!1&&t.type!=="none"&&(t={...t,disable_parallel_tool_use:!0}),(e.tool_choice!==void 0||e.parallel_tool_calls===!1)&&(i.tool_choice=t)}const d=e.reasoning_effort;d==="none"?i.thinking={type:"disabled"}:d!=null&&(i.thinking={type:"adaptive"},i.output_config={effort:d==="minimal"?"low":d});const x=e.response_format;return x?.type==="json_schema"?i.output_config={...i.output_config,format:{type:"json_schema",schema:x.json_schema.schema}}:x?.type==="json_object"&&(o.jsonObject==="schema"?(i.output_config={...i.output_config,format:{type:"json_schema",schema:{type:"object"}}},r.jsonSchemaEmulated=!0):o.jsonObject==="instruction"?i.system=i.system===void 0?T:`${i.system}
4
4
 
5
- ${T}`:g(o,"response_format")),{body:s,state:o}}f(P,"request");const T="Respond with a single valid JSON object and nothing else.";function g(e,r){e.dropped.includes(r)||e.dropped.push(r)}f(g,"drop");function A(e,r){const i=[...e].sort((p,m)=>p.at-m.at),o=[];let a=0;const c=r.findIndex(p=>p.type==="tool_use");for(const p of i){const m=p.at<=1&&c!==-1?Math.min(p.at,c):p.at;for(;a<Math.min(m,r.length);)o.push(r[a++]);o.push(p.block)}return[...o,...r.slice(a)]}f(A,"interleave");function M(e){return!!(e.reasoning||e.reasoning_content)}f(M,"reasoningPresent");function j(e){return typeof e=="string"?e:(e??[]).filter(r=>r.type==="text").map(r=>r.text).join(`
5
+ ${T}`:k(r,"response_format")),{body:i,state:r}}u(Q,"request");const T="Respond with a single valid JSON object and nothing else.";function C(e){const s=[];for(const a of e)if(a.role==="assistant")for(const o of a.tool_calls??[])s.push(o.id);else a.role==="tool"&&s.push(a.tool_call_id);return s}u(C,"idsOf");function k(e,s){e.dropped.includes(s)||e.dropped.push(s)}u(k,"drop");function R(e,s){const a=[...e].sort((f,c)=>f.at-c.at),o=[];let r=0;const m=s.findIndex(f=>f.type==="tool_use");for(const f of a){const c=f.at<=1&&m!==-1?Math.min(f.at,m):f.at;for(;r<Math.min(c,s.length);)o.push(s[r++]);o.push(f.block)}return[...o,...s.slice(r)]}u(R,"interleave");function U(e){return!!(e.reasoning||e.reasoning_content)}u(U,"reasoningPresent");function w(e){return typeof e=="string"?e:(e??[]).filter(s=>s.type==="text").map(s=>s.text).join(`
6
6
 
7
- `)}f(j,"plainText");function F(e,r){if(typeof e=="string")return e===""?[]:[{type:"text",text:e}];const i=[];for(const o of e??[])if(o.type==="text")i.push({type:"text",text:o.text});else if(o.type==="image_url"){const a=x.exec(o.image_url.url);i.push({type:"image",source:a?{type:"base64",media_type:a[1],data:a[2]}:{type:"url",url:o.image_url.url}})}else if(o.type==="file"&&typeof o.file?.file_data=="string"&&x.test(o.file.file_data)){const a=x.exec(o.file.file_data);i.push({type:"document",source:{type:"base64",media_type:a[1],data:a[2]}})}else g(r,`content:${o.type}`);return i}f(F,"userBlocks");function R(e,r){return e==="none"?{type:"none"}:e==="required"?{type:"any"}:typeof e=="object"&&e!==null?{type:"tool",name:r(e.function.name)}:{type:"auto"}}f(R,"toolChoice");export{P as request};
7
+ `)}u(w,"plainText");function z(e,s){if(typeof e=="string")return e===""?[]:[{type:"text",text:e}];const a=[];for(const o of e??[])if(o.type==="text")a.push({type:"text",text:o.text});else if(o.type==="image_url"){const r=j.exec(o.image_url.url);a.push({type:"image",source:r?{type:"base64",media_type:r[1],data:r[2]}:{type:"url",url:o.image_url.url}})}else if(o.type==="file"&&typeof o.file?.file_data=="string"&&j.test(o.file.file_data)){const r=j.exec(o.file.file_data);a.push({type:"document",source:{type:"base64",media_type:r[1],data:r[2]}})}else k(s,`content:${o.type}`);return a}u(z,"userBlocks");function D(e,s){return e==="none"?{type:"none"}:e==="required"?{type:"any"}:typeof e=="object"&&e!==null?{type:"tool",name:s(e.function.name)}:{type:"auto"}}u(D,"toolChoice");export{Q as request};
8
8
  //# sourceMappingURL=request.mjs.map
@@ -1,2 +1,2 @@
1
- var y=Object.defineProperty;var c=(t,n)=>y(t,"name",{value:n,configurable:!0});import{resolve as _}from"../../../../../../../lib/settings.mjs";import{TranslatorError as m}from"../../../../../../../lib/errors.mjs";import{messageId as k,anthropicToolId as T,restoreName as x}from"../../../../../../../lib/ids.mjs";import{sign as A,reasoningOf as w,reasoningKey as C,detailsText as N,splitThink as O}from"../../../../../../../lib/thinking.mjs";import{isComplete as S,parseArguments as U}from"../../../../../../../lib/arguments.mjs";import{finishError as v,toAnthropicStop as E,toAnthropicUsage as I}from"../../../../../../../lib/finish.mjs";function D(t,n,u){const f=_(u),l=t?.choices?.[0];if(!l)throw new m("Upstream response contained no choices",{code:"invalid_upstream_response",status:502});const g=v(l.finish_reason);if(g)throw new m(g.message,{code:g.code,status:g.status,type:g.type});const e=l.message??{},i=[];let a=w(e),s=typeof e.content=="string"?e.content:"";const o={};if(f.reasoningRoundTrip){const r=C(e);r&&(o[r]=e[r]),Array.isArray(e.reasoning_details)&&e.reasoning_details.length>0&&(o.reasoning_details=e.reasoning_details)}a===""&&f.thinkTags&&({reasoning:a,text:s}=O(s,f.thinkOpen)),a===""&&o.reasoning_details&&(a=N(o.reasoning_details)),(a!==""||o.reasoning_details)&&(i.push({type:"thinking",thinking:a,signature:A(Object.keys(o).length>0?o:void 0,n?.producer)}),s=s.trimStart()),s===""&&typeof e.refusal=="string"&&(s=e.refusal),s!==""&&i.push({type:"text",text:s});const h=n?.toolNames??{},p=e.tool_calls??[];for(const r of p)i.push({type:"tool_use",id:T(r.id),name:x(h,r.function.name),input:U(r.function.arguments)});i.length===0&&i.push({type:"text",text:""});const d=E(l.finish_reason,{hasToolCalls:p.length>0,matchedStop:l.stop_reason,refusal:e.refusal,argumentsComplete:p.length===0||S(p.at(-1).function.arguments)});return n&&t.usage&&(n.upstreamUsage=t.usage),{id:k(),type:"message",role:"assistant",model:n?.model??t.model,content:i,...d,usage:I(t.usage)}}c(D,"response");export{D as response};
1
+ var _=Object.defineProperty;var c=(o,s)=>_(o,"name",{value:s,configurable:!0});import{resolve as k}from"../../../../../../../lib/settings.mjs";import{TranslatorError as m}from"../../../../../../../lib/errors.mjs";import{messageId as T,anthropicToolId as x,restoreName as A}from"../../../../../../../lib/ids.mjs";import{sign as w,reasoningOf as I,reasoningKey as C,detailsText as N,splitThink as O}from"../../../../../../../lib/thinking.mjs";import{isComplete as S,parseArguments as U}from"../../../../../../../lib/arguments.mjs";import{finishError as v,toAnthropicStop as E,toAnthropicUsage as b}from"../../../../../../../lib/finish.mjs";function F(o,s,u){const f=k(u),l=o?.choices?.[0];if(!l)throw new m("Upstream response contained no choices",{code:"invalid_upstream_response",status:502});const g=v(l.finish_reason);if(g)throw new m(g.message,{code:g.code,status:g.status,type:g.type});const e=l.message??{},i=[];let a=I(e),t=typeof e.content=="string"?e.content:"";const n={};if(f.reasoningRoundTrip){const r=C(e);r&&(n[r]=e[r]),Array.isArray(e.reasoning_details)&&e.reasoning_details.length>0&&(n.reasoning_details=e.reasoning_details)}a===""&&f.thinkTags&&({reasoning:a,text:t}=O(t,f.thinkOpen)),a===""&&n.reasoning_details&&(a=N(n.reasoning_details)),(a!==""||n.reasoning_details)&&(i.push({type:"thinking",thinking:a,signature:w(Object.keys(n).length>0?n:void 0,s?.producer)}),t=t.trimStart()),t===""&&typeof e.refusal=="string"&&(t=e.refusal),t!==""&&i.push({type:"text",text:t});const d=s?.toolNames??{},h=s?.toolIds??{},p=e.tool_calls??[];for(const r of p)i.push({type:"tool_use",id:x(r.id,h),name:A(d,r.function.name),input:U(r.function.arguments)});i.length===0&&i.push({type:"text",text:""});const y=E(l.finish_reason,{hasToolCalls:p.length>0,matchedStop:l.stop_reason,refusal:e.refusal,argumentsComplete:p.length===0||S(p.at(-1).function.arguments)});return s&&o.usage&&(s.upstreamUsage=o.usage),{id:T(),type:"message",role:"assistant",model:s?.model??o.model,content:i,...y,usage:b(o.usage)}}c(F,"response");export{F as response};
2
2
  //# sourceMappingURL=response.mjs.map
@@ -1,2 +1,2 @@
1
- var L=Object.defineProperty;var l=(b,a)=>L(b,"name",{value:a,configurable:!0});import{resolve as P}from"../../../../../../../lib/settings.mjs";import{TranslatorError as Q,openaiToAnthropicType as V,rewriteContextOverflow as W}from"../../../../../../../lib/errors.mjs";import{isComplete as X}from"../../../../../../../lib/arguments.mjs";import{messageId as Y,anthropicToolId as Z,restoreName as $}from"../../../../../../../lib/ids.mjs";import{sign as ee,reasoningOf as te,reasoningKey as ne,detailsText as ie,mergeDetail as oe,createThinkSplitter as se}from"../../../../../../../lib/thinking.mjs";import{finishError as le,toAnthropicStop as re,toAnthropicUsage as q}from"../../../../../../../lib/finish.mjs";import{messages as ae}from"../../../../../../../lib/sse.mjs";async function*_e(b,a,B){const u=P(B),F=a?.toolNames??{},d=u.thinkTags?se(u.thinkOpen):null,c=new Map;let _=!1,j=0,n=null,N=null,k=null,O=null,h="",v=!1,A=!1,C=0,T=!1,U=!1,E=!1;const w=[],s=l((e,t)=>({event:e,data:{type:e,...t}}),"event"),p=l((e,t)=>s("error",{error:{type:e,message:t}}),"fail"),J=l(e=>{const t={};return u.reasoningRoundTrip&&(e.key!==null&&(t[e.key]=e.text),e.details.length>0&&(t.reasoning_details=e.details)),Object.keys(t).length>0?t:void 0},"carried");function*y(){if(n!==null){n.kind==="thinking"?yield s("content_block_delta",{index:n.index,delta:{type:"signature_delta",signature:ee(J(n.carry),a?.producer)}}):n.kind==="tool"&&(n.entry.state="closed",n.entry.sent||(yield s("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:"{}"}}))),yield s("content_block_stop",{index:n.index}),n=null;for(const e of c.values()){if(e.state!=="pending")continue;e.state="closed";const t=j++,i=e.args.join("");yield s("content_block_start",{index:t,content_block:e.block}),yield s("content_block_delta",{index:t,delta:{type:"input_json_delta",partial_json:i===""?"{}":i}}),yield s("content_block_stop",{index:t})}}}l(y,"close");function*K(){for(const e of I()){if(e.state!=="buffered")continue;yield*g("tool",e.block,e);const t=e.args.join("");t!==""&&(e.sent=!0,yield s("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:t}})),yield*y()}}l(K,"flushBuffered");function I(){return[...c.entries()].sort((e,t)=>e[0]-t[0]).map(e=>e[1])}l(I,"ordered");function*g(e,t,i){yield*y(),n={kind:e,index:j++,entry:i,carry:{key:null,text:"",details:[]}},yield s("content_block_start",{index:n.index,content_block:t})}l(g,"begin");function*r(e,t,i=null){if(e==="text"&&E&&(t=t.trimStart()),t==="")return;if(E=e==="thinking",e==="text"&&(v=!0),n?.kind==="tool"){w.push({kind:e,text:t,key:i});return}n?.kind!==e&&(yield*g(e,e==="text"?{type:"text",text:""}:{type:"thinking",thinking:"",signature:""})),i!==null&&(n.carry.key??=i,n.carry.text+=t);const o=e==="text"?{type:"text_delta",text:t}:{type:"thinking_delta",thinking:t};yield s("content_block_delta",{index:n.index,delta:o})}l(r,"piece");function*R(e){if(n?.kind==="tool"){w.push({kind:"details",items:e});return}n?.kind!=="thinking"&&(yield*g("thinking",{type:"thinking",thinking:"",signature:""}));for(const t of e)oe(n.carry.details,t);n.carry.key===null&&(yield*r("thinking",ie(e)))}l(R,"details");function*z(e){yield*r("thinking",e.reasoning),yield*r("text",e.text)}l(z,"pieces");function*M(e){let t=e.index;t===void 0&&(t=e.id===void 0?C:c.size),C=t;let i=c.get(t);if(i===void 0){const x=e.function?.name??"",m={type:"tool_use",id:Z(e.id),name:$(F,x),input:{}};d?.pending()&&(U=!0);const f=u.toolStreaming==="buffer"||U;i={state:f?"buffered":"pending",sent:!1,args:[],block:m},c.set(t,i),!f&&n?.kind!=="tool"&&(i.state="open",yield*g("tool",m,i))}let o=e.function?.arguments;o!==null&&typeof o=="object"&&(o=JSON.stringify(o)),!(o==null||o==="")&&(i.args.push(o),i.state==="open"&&(i.sent=!0,yield s("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:o}})))}l(M,"toolCall");try{for await(const e of ae(b)){if(e.data==="[DONE]"){A=!0;break}const t=e.data;if(t.error){const f=t.error.message??"Upstream error";yield p(V(t.error),W(f)??f);return}_||(_=!0,yield s("message_start",{message:{id:Y(),type:"message",role:"assistant",model:a?.model??t.model,content:[],stop_reason:null,stop_sequence:null,usage:q(t.usage)}})),t.usage&&(N=t.usage,a&&(a.upstreamUsage=t.usage));const i=t.choices?.[0];if(!i)continue;const o=i.delta??{},x=te(o);let m="";x!==""&&!T&&(T=!0,d&&(m=d.release().text)),yield*r("thinking",x,ne(o)),yield*r("text",m),u.reasoningRoundTrip&&Array.isArray(o.reasoning_details)&&o.reasoning_details.length>0&&(yield*R(o.reasoning_details)),typeof o.content=="string"&&o.content!==""&&(d&&!T?yield*z(d.push(o.content)):yield*r("text",o.content)),typeof o.refusal=="string"&&(h+=o.refusal);for(const f of o.tool_calls??[])yield*M(f);i.finish_reason&&(k=i.finish_reason),typeof i.stop_reason=="string"&&(O=i.stop_reason)}}catch(e){yield e instanceof Q?p(e.type,e.message):p("api_error",e.message);return}if(!_||k===null&&!A){yield p("api_error",_?"Upstream stream ended before completion":"Upstream stream ended without data");return}const S=le(k);if(S){yield p(S.type,S.message);return}d&&(yield*z(d.end())),yield*K();const D=I().at(-1),G=D===void 0||X(D.args.join(""));yield*y();for(const e of w)yield*e.kind==="details"?R(e.items):r(e.kind,e.text,e.key);!v&&h!==""&&(yield*r("text",h)),yield*y(),j===0&&(yield*g("text",{type:"text",text:""}),yield*y());const H=re(k,{hasToolCalls:c.size>0,matchedStop:O,argumentsComplete:G,refusal:h});yield s("message_delta",{delta:H,usage:q(N)}),yield s("message_stop",{})}l(_e,"stream");export{_e as stream};
1
+ var P=Object.defineProperty;var l=(b,r)=>P(b,"name",{value:r,configurable:!0});import{resolve as Q}from"../../../../../../../lib/settings.mjs";import{TranslatorError as V,openaiToAnthropicType as W,rewriteContextOverflow as X}from"../../../../../../../lib/errors.mjs";import{isComplete as Y}from"../../../../../../../lib/arguments.mjs";import{messageId as Z,anthropicToolId as $,restoreName as ee}from"../../../../../../../lib/ids.mjs";import{sign as te,reasoningOf as ne,reasoningKey as ie,detailsText as oe,mergeDetail as se,createThinkSplitter as le}from"../../../../../../../lib/thinking.mjs";import{finishError as re,toAnthropicStop as ae,toAnthropicUsage as q}from"../../../../../../../lib/finish.mjs";import{messages as de}from"../../../../../../../lib/sse.mjs";async function*ke(b,r,B){const u=Q(B),F=r?.toolNames??{},J=r?.toolIds??{},d=u.thinkTags?le(u.thinkOpen):null,c=new Map;let _=!1,j=0,n=null,N=null,k=null,O=null,h="",v=!1,A=!1,C=0,T=!1,I=!1,U=!1;const w=[],s=l((e,t)=>({event:e,data:{type:e,...t}}),"event"),p=l((e,t)=>s("error",{error:{type:e,message:t}}),"fail"),K=l(e=>{const t={};return u.reasoningRoundTrip&&(e.key!==null&&(t[e.key]=e.text),e.details.length>0&&(t.reasoning_details=e.details)),Object.keys(t).length>0?t:void 0},"carried");function*y(){if(n!==null){n.kind==="thinking"?yield s("content_block_delta",{index:n.index,delta:{type:"signature_delta",signature:te(K(n.carry),r?.producer)}}):n.kind==="tool"&&(n.entry.state="closed",n.entry.sent||(yield s("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:"{}"}}))),yield s("content_block_stop",{index:n.index}),n=null;for(const e of c.values()){if(e.state!=="pending")continue;e.state="closed";const t=j++,i=e.args.join("");yield s("content_block_start",{index:t,content_block:e.block}),yield s("content_block_delta",{index:t,delta:{type:"input_json_delta",partial_json:i===""?"{}":i}}),yield s("content_block_stop",{index:t})}}}l(y,"close");function*M(){for(const e of E()){if(e.state!=="buffered")continue;yield*g("tool",e.block,e);const t=e.args.join("");t!==""&&(e.sent=!0,yield s("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:t}})),yield*y()}}l(M,"flushBuffered");function E(){return[...c.entries()].sort((e,t)=>e[0]-t[0]).map(e=>e[1])}l(E,"ordered");function*g(e,t,i){yield*y(),n={kind:e,index:j++,entry:i,carry:{key:null,text:"",details:[]}},yield s("content_block_start",{index:n.index,content_block:t})}l(g,"begin");function*a(e,t,i=null){if(e==="text"&&U&&(t=t.trimStart()),t==="")return;if(U=e==="thinking",e==="text"&&(v=!0),n?.kind==="tool"){w.push({kind:e,text:t,key:i});return}n?.kind!==e&&(yield*g(e,e==="text"?{type:"text",text:""}:{type:"thinking",thinking:"",signature:""})),i!==null&&(n.carry.key??=i,n.carry.text+=t);const o=e==="text"?{type:"text_delta",text:t}:{type:"thinking_delta",thinking:t};yield s("content_block_delta",{index:n.index,delta:o})}l(a,"piece");function*R(e){if(n?.kind==="tool"){w.push({kind:"details",items:e});return}n?.kind!=="thinking"&&(yield*g("thinking",{type:"thinking",thinking:"",signature:""}));for(const t of e)se(n.carry.details,t);n.carry.key===null&&(yield*a("thinking",oe(e)))}l(R,"details");function*z(e){yield*a("thinking",e.reasoning),yield*a("text",e.text)}l(z,"pieces");function*G(e){let t=e.index;t===void 0&&(t=e.id===void 0?C:c.size),C=t;let i=c.get(t);if(i===void 0){const x=e.function?.name??"",m={type:"tool_use",id:$(e.id,J),name:ee(F,x),input:{}};d?.pending()&&(I=!0);const f=u.toolStreaming==="buffer"||I;i={state:f?"buffered":"pending",sent:!1,args:[],block:m},c.set(t,i),!f&&n?.kind!=="tool"&&(i.state="open",yield*g("tool",m,i))}let o=e.function?.arguments;o!==null&&typeof o=="object"&&(o=JSON.stringify(o)),!(o==null||o==="")&&(i.args.push(o),i.state==="open"&&(i.sent=!0,yield s("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:o}})))}l(G,"toolCall");try{for await(const e of de(b)){if(e.data==="[DONE]"){A=!0;break}const t=e.data;if(t.error){const f=t.error.message??"Upstream error";yield p(W(t.error),X(f)??f);return}_||(_=!0,yield s("message_start",{message:{id:Z(),type:"message",role:"assistant",model:r?.model??t.model,content:[],stop_reason:null,stop_sequence:null,usage:q(t.usage)}})),t.usage&&(N=t.usage,r&&(r.upstreamUsage=t.usage));const i=t.choices?.[0];if(!i)continue;const o=i.delta??{},x=ne(o);let m="";x!==""&&!T&&(T=!0,d&&(m=d.release().text)),yield*a("thinking",x,ie(o)),yield*a("text",m),u.reasoningRoundTrip&&Array.isArray(o.reasoning_details)&&o.reasoning_details.length>0&&(yield*R(o.reasoning_details)),typeof o.content=="string"&&o.content!==""&&(d&&!T?yield*z(d.push(o.content)):yield*a("text",o.content)),typeof o.refusal=="string"&&(h+=o.refusal);for(const f of o.tool_calls??[])yield*G(f);i.finish_reason&&(k=i.finish_reason),typeof i.stop_reason=="string"&&(O=i.stop_reason)}}catch(e){yield e instanceof V?p(e.type,e.message):p("api_error",e.message);return}if(!_||k===null&&!A){yield p("api_error",_?"Upstream stream ended before completion":"Upstream stream ended without data");return}const S=re(k);if(S){yield p(S.type,S.message);return}d&&(yield*z(d.end())),yield*M();const D=E().at(-1),H=D===void 0||Y(D.args.join(""));yield*y();for(const e of w)yield*e.kind==="details"?R(e.items):a(e.kind,e.text,e.key);!v&&h!==""&&(yield*a("text",h)),yield*y(),j===0&&(yield*g("text",{type:"text",text:""}),yield*y());const L=ae(k,{hasToolCalls:c.size>0,matchedStop:O,argumentsComplete:H,refusal:h});yield s("message_delta",{delta:L,usage:q(N)}),yield s("message_stop",{})}l(ke,"stream");export{ke as stream};
2
2
  //# sourceMappingURL=stream.mjs.map
@@ -1,2 +1,2 @@
1
- var v=Object.defineProperty;var n=(r,e)=>v(r,"name",{value:e,configurable:!0});import{context as g,self as m}from"../lib/runtime.mjs";import{capture as w}from"../lib/capture.mjs";import{TranslatorError as d}from"../lib/errors.mjs";import{warn as y}from"../lib/warn.mjs";import{emit as h}from"../lib/events.mjs";import*as p from"../lib/http.mjs";async function D(r={}){const e=w(m,g),{status:o}=m,f=r.network!==!1;for(const a of e.drivers){const s=a.info.fields;if(s!==void 0&&!(Array.isArray(s)&&s.every(i=>typeof i=="string")))throw new d(`Driver ${a.name}: info().fields must be an array of field names`,{code:"invalid_driver",status:500})}const t={transport:e.state.transport,signal:r.signal,timeout:e.config.detectTimeout};return await Promise.all(e.state.endpoints.map(a=>k(e,a,f,t))),o()}n(D,"detect");async function k(r,e,o,f){let t,a=null;if(e.backend!=="auto"){if(t=r.drivers.find(s=>s.name===e.backend),!t)throw new d(`Unknown backend "${e.backend}" for upstream ${e.name}`,{code:"unknown_backend",status:500});a={version:e.version}}else if(o){const s={endpoint:e,...f,probes:new Map};let i=!1;for(const[l,c]of r.drivers.entries()){if(!i&&c.info.probes?.length>0){i=!0;for(const u of r.drivers.slice(l))for(const b of u.info.probes??[])p.probe(s,b)}if(a=await $(c,s),a){t=c;break}}}if(t||(t=r.drivers.find(s=>s.name==="compatible"),a={reachable:null,models:[]}),e.driver=t.name,e.protocol=t.info.protocol,e.facts=a,e.driverSettings=await t.settings?.(a)??{},e.probed={},o&&e.backend==="auto"&&t.probe){const s=n(l=>e.settings[l]!==void 0||r.config.settings[l]!==void 0,"given"),i={inlineSystem:r.settings(e).inlineSystem==="auto",images:!s("images")};(i.inlineSystem||i.images)&&(e.probed=await t.probe({endpoint:e,facts:a,want:i,...f}))}a.reachable===!1?y(r.config,r.state,`Upstream ${e.name} (${e.url}) did not answer detection; using the generic OpenAI-compatible driver`,{type:"detect",kind:"unreachable",endpoint:e.name,url:e.url,driver:t.name}):h({config:r.config,state:r.state},{type:"detect",kind:"driver",level:"info",message:`Upstream ${e.name} (${e.url}): driver ${t.name}`,endpoint:e.name,url:e.url,driver:t.name})}n(k,"bind");async function $(r,e){try{return await r.detect?.(e)??null}catch{return null}}n($,"detectWith");export{D as detect};
1
+ var g=Object.defineProperty;var o=(r,e)=>g(r,"name",{value:e,configurable:!0});import{context as h,self as u}from"../lib/runtime.mjs";import{capture as w}from"../lib/capture.mjs";import{TranslatorError as m}from"../lib/errors.mjs";import{warn as p}from"../lib/warn.mjs";import{emit as y}from"../lib/events.mjs";import*as k from"../lib/http.mjs";import{validToolIdPolicy as $}from"../lib/ids.mjs";async function D(r={}){const e=w(u,h),{status:f}=u,l=r.network!==!1;for(const t of e.drivers){const s=t.info.fields;if(s!==void 0&&!(Array.isArray(s)&&s.every(i=>typeof i=="string")))throw new m(`Driver ${t.name}: info().fields must be an array of field names`,{code:"invalid_driver",status:500})}for(const t of e.drivers){const s=t.info.toolCallIds;if(s!==void 0&&!$(s))throw new m(`Driver ${t.name}: info().toolCallIds must be { alphabet, min?, max? } with a character-class alphabet and positive integer bounds`,{code:"invalid_driver",status:500})}const a={transport:e.state.transport,signal:r.signal,timeout:e.config.detectTimeout};return await Promise.all(e.state.endpoints.map(t=>x(e,t,l,a))),f()}o(D,"detect");async function x(r,e,f,l){let a,t=null;if(e.backend!=="auto"){if(a=r.drivers.find(s=>s.name===e.backend),!a)throw new m(`Unknown backend "${e.backend}" for upstream ${e.name}`,{code:"unknown_backend",status:500});t={version:e.version}}else if(f){const s=URL.canParse(e.url)?new URL(e.url).hostname:"",i={endpoint:e,...l,probes:new Map};let n=!1;for(const[v,c]of r.drivers.entries()){if(!n&&c.info.probes?.length>0&&!c.info.hosts?.includes(s)){n=!0;for(const d of r.drivers.slice(v))for(const b of d.info.probes??[])k.probe(i,b,d.info)}if(t=await U(c,i),t){a=c;break}}}if(a||(a=r.drivers.find(s=>s.name==="compatible"),t={reachable:null,models:[]}),e.driver=a.name,e.protocol=a.info.protocol,e.facts=t,e.driverSettings=await a.settings?.(t)??{},e.probed={},f&&e.backend==="auto"&&a.probe){const s=o(n=>e.settings[n]!==void 0||r.config.settings[n]!==void 0,"given"),i={inlineSystem:r.settings(e).inlineSystem==="auto",images:!s("images")};(i.inlineSystem||i.images)&&(e.probed=await a.probe({endpoint:e,facts:t,want:i,...l}))}t.reachable===!1?p(r.config,r.state,`Upstream ${e.name} (${e.url}) did not answer detection; using the generic OpenAI-compatible driver`,{type:"detect",kind:"unreachable",endpoint:e.name,url:e.url,driver:a.name}):y({config:r.config,state:r.state},{type:"detect",kind:"driver",level:"info",message:`Upstream ${e.name} (${e.url}): driver ${a.name}`,endpoint:e.name,url:e.url,driver:a.name})}o(x,"bind");async function U(r,e){try{return await r.detect?.(e)??null}catch{return null}}o(U,"detectWith");export{D as detect};
2
2
  //# sourceMappingURL=detect.mjs.map
@@ -1,2 +1,2 @@
1
- var c=Object.defineProperty;var r=(t,n)=>c(t,"name",{value:n,configurable:!0});function o({endpoint:t}){const n=URL.canParse(t.url)?new URL(t.url).hostname:"";return n==="api.anthropic.com"?{host:n}:null}r(o,"detect");export{o as detect};
1
+ var l=Object.defineProperty;var i=(t,e)=>l(t,"name",{value:e,configurable:!0});import*as f from"../../../lib/http.mjs";import{info as a}from"./info.mjs";function o(t){if(t===null||typeof t!="object"||Array.isArray(t)||"object"in t||!Array.isArray(t.data)||typeof t.has_more!="boolean"||![t.first_id,t.last_id].every(r=>r===null||typeof r=="string")||!t.data.every(r=>r?.type==="model"&&typeof r.id=="string"&&typeof r.display_name=="string"))return!1;const n=t.data[0]?.id??null,s=t.data.at(-1)?.id??null;return t.first_id===n&&t.last_id===s}i(o,"isAnthropicList");async function c(t){const{endpoint:e}=t,n=URL.canParse(e.url)?new URL(e.url).hostname:"";if(a().hosts.includes(n))return{host:n};const s=await f.probe(t,"/v1/models",a());return!s.ok||!o(s.body)?null:{models:s.body.data.map(r=>r.id)}}i(c,"detect");export{c as detect};
2
2
  //# sourceMappingURL=detect.mjs.map
@@ -1,2 +1,2 @@
1
- var a=Object.defineProperty;var e=(s,o)=>a(s,"name",{value:o,configurable:!0});function t(){return{name:"anthropic",protocol:"anthropic.messages",order:20,auth:"x-api-key",headers:{"anthropic-version":"2023-06-01"},paths:{messages:"/v1/messages",models:"/v1/models",count:"/v1/messages/count_tokens"}}}e(t,"info");export{t as info};
1
+ var t=Object.defineProperty;var s=(e,o)=>t(e,"name",{value:o,configurable:!0});function a(){return{name:"anthropic",protocol:"anthropic.messages",order:38,auth:"x-api-key",strict:!0,hosts:["api.anthropic.com"],probes:["/v1/models"],headers:{"anthropic-version":"2023-06-01"},paths:{messages:"/v1/messages",models:"/v1/models",count:"/v1/messages/count_tokens"}}}s(a,"info");export{a as info};
2
2
  //# sourceMappingURL=info.mjs.map
@@ -1,2 +1,2 @@
1
- var r=Object.defineProperty;var o=(e,t)=>r(e,"name",{value:t,configurable:!0});import{VLLM_FIELDS as a}from"../../../lib/passthrough.mjs";function m(){return{name:"compatible",protocol:"openai.chat",order:100,auth:"bearer",fields:a,paths:{chat:"/v1/chat/completions",models:"/v1/models"}}}o(m,"info");export{m as info};
1
+ var r=Object.defineProperty;var o=(t,e)=>r(t,"name",{value:e,configurable:!0});import{VLLM_FIELDS as a}from"../../../lib/passthrough.mjs";function l(){return{name:"compatible",protocol:"openai.chat",order:100,auth:"bearer",strict:!1,fields:a,paths:{chat:"/v1/chat/completions",models:"/v1/models"}}}o(l,"info");export{l as info};
2
2
  //# sourceMappingURL=info.mjs.map
@@ -1,2 +1,2 @@
1
- var a=Object.defineProperty;var e=(o,t)=>a(o,"name",{value:t,configurable:!0});function n(){return{name:"deepseek",protocol:"openai.chat",order:35,auth:"bearer",paths:{chat:"/v1/chat/completions",models:"/v1/models"}}}e(n,"info");export{n as info};
1
+ var a=Object.defineProperty;var e=(o,t)=>a(o,"name",{value:t,configurable:!0});function c(){return{name:"deepseek",protocol:"openai.chat",order:35,auth:"bearer",strict:!1,paths:{chat:"/v1/chat/completions",models:"/v1/models"}}}e(c,"info");export{c as info};
2
2
  //# sourceMappingURL=info.mjs.map
@@ -1,2 +1,2 @@
1
- var a=Object.defineProperty;var p=(e,t)=>a(e,"name",{value:t,configurable:!0});function r(){return{name:"llamacpp",protocol:"openai.chat",order:40,auth:"bearer",probes:["/props"],fields:["top_k","min_p","chat_template_kwargs","repetition_penalty","repeat_penalty","grammar"],paths:{chat:"/v1/chat/completions",models:"/v1/models",props:"/props"}}}p(r,"info");export{r as info};
1
+ var a=Object.defineProperty;var e=(p,t)=>a(p,"name",{value:t,configurable:!0});function r(){return{name:"llamacpp",protocol:"openai.chat",order:40,auth:"bearer",strict:!1,probes:["/props"],fields:["top_k","min_p","chat_template_kwargs","repetition_penalty","repeat_penalty","grammar"],paths:{chat:"/v1/chat/completions",models:"/v1/models",props:"/props"}}}e(r,"info");export{r as info};
2
2
  //# sourceMappingURL=info.mjs.map
@@ -1,2 +1,2 @@
1
- var r=Object.defineProperty;var o=(e,a)=>r(e,"name",{value:a,configurable:!0});function t(){return{name:"ollama",protocol:"openai.chat",order:60,auth:"bearer",probes:["/api/version"],paths:{chat:"/v1/chat/completions",models:"/v1/models",version:"/api/version"}}}o(t,"info");export{t as info};
1
+ var r=Object.defineProperty;var o=(e,a)=>r(e,"name",{value:a,configurable:!0});function i(){return{name:"ollama",protocol:"openai.chat",order:60,auth:"bearer",strict:!1,probes:["/api/version"],paths:{chat:"/v1/chat/completions",models:"/v1/models",version:"/api/version"}}}o(i,"info");export{i as info};
2
2
  //# sourceMappingURL=info.mjs.map
@@ -1,2 +1,2 @@
1
- var a=Object.defineProperty;var o=(e,t)=>a(e,"name",{value:t,configurable:!0});function r(){return{name:"openai",protocol:"openai.chat",order:30,auth:"bearer",paths:{chat:"/v1/chat/completions",models:"/v1/models"}}}o(r,"info");export{r as info};
1
+ var r=Object.defineProperty;var o=(t,e)=>r(t,"name",{value:e,configurable:!0});function n(){return{name:"openai",protocol:"openai.chat",order:30,auth:"bearer",strict:!0,paths:{chat:"/v1/chat/completions",models:"/v1/models"}}}o(n,"info");export{n as info};
2
2
  //# sourceMappingURL=info.mjs.map
@@ -1,2 +1,2 @@
1
- var t=Object.defineProperty;var e=(o,r)=>t(o,"name",{value:r,configurable:!0});function a(){return{name:"sglang",protocol:"openai.chat",order:50,auth:"bearer",probes:["/get_server_info"],fields:["top_k","min_p","repetition_penalty","chat_template_kwargs","skip_special_tokens","ignore_eos","regex","ebnf","separate_reasoning"],paths:{chat:"/v1/chat/completions",models:"/v1/models",serverInfo:"/get_server_info"}}}e(a,"info");export{a as info};
1
+ var r=Object.defineProperty;var e=(t,o)=>r(t,"name",{value:o,configurable:!0});function s(){return{name:"sglang",protocol:"openai.chat",order:50,auth:"bearer",strict:!1,probes:["/get_server_info"],fields:["top_k","min_p","repetition_penalty","chat_template_kwargs","skip_special_tokens","ignore_eos","regex","ebnf","separate_reasoning"],paths:{chat:"/v1/chat/completions",models:"/v1/models",serverInfo:"/get_server_info"}}}e(s,"info");export{s as info};
2
2
  //# sourceMappingURL=info.mjs.map
@@ -1,2 +1,2 @@
1
- var r=Object.defineProperty;var o=(e,t)=>r(e,"name",{value:t,configurable:!0});import{VLLM_FIELDS as n}from"../../../lib/passthrough.mjs";function l(){return{name:"vllm",protocol:"openai.chat",order:10,auth:"bearer",fields:n,paths:{chat:"/v1/chat/completions",models:"/v1/models",tokenize:"/tokenize",version:"/version"}}}o(l,"info");export{l as info};
1
+ var r=Object.defineProperty;var e=(o,t)=>r(o,"name",{value:t,configurable:!0});import{VLLM_FIELDS as i}from"../../../lib/passthrough.mjs";function s(){return{name:"vllm",protocol:"openai.chat",order:10,auth:"bearer",strict:!0,fields:i,paths:{chat:"/v1/chat/completions",models:"/v1/models",tokenize:"/tokenize",version:"/version"}}}e(s,"info");export{s as info};
2
2
  //# sourceMappingURL=info.mjs.map
@@ -0,0 +1,2 @@
1
+ var x=Object.defineProperty;var h=(e,s)=>x(e,"name",{value:s,configurable:!0});import{context as q,self as w}from"../../lib/runtime.mjs";import{capture as H}from"../../lib/capture.mjs";import{resolve as S,serves as J}from"../../lib/route.mjs";import*as y from"../../lib/http.mjs";import*as A from"../../lib/pool.mjs";import{emit as N}from"../../lib/events.mjs";import{meter as U}from"../../lib/usage.mjs";import{warn as C}from"../../lib/warn.mjs";import{start as I}from"../../lib/record.mjs";const M="/v1/embeddings";async function Q(e,s={}){const{transport:$}=q.state,{error:g}=w,a=H(w,q,"openai"),O=e&&typeof e=="object"&&!Array.isArray(e),l=U({model:null,requested:O&&typeof e.model=="string"?e.model:null,upstream:null,front:"openai",stream:!1},s.onUsage,(o,t)=>C(a.config,a.state,o,{type:"usage",...t,log:s.log})),u=h((o,t)=>(l.finish(t),Object.defineProperty(o,"usage",{value:l.usage,enumerable:!1,configurable:!0})),"end"),n=h((o,t,f={})=>u(g.build(o,t,{...f,front:"openai"}),o===499?"aborted":"error"),"fail");if(!e||typeof e!="object"||Array.isArray(e))return n(400,"The request body must be a JSON object",{code:"invalid_body"});if(typeof e.model!="string"||e.model==="")return n(400,"model is required",{code:"invalid_body",param:"model"});const P=Object.hasOwn(a.config.aliases,e.model)?a.config.aliases[e.model]:e.model;if(!a.state.endpoints.some(o=>J(o,P)))return n(404,`The model "${e.model}" is not served by any configured upstream.`,{code:"model_not_found",param:"model"});const c=S(a,e,{advance:!0}),p=c.pool.filter(o=>o.protocol==="openai.chat");if(p.length===0)return n(400,`The model "${e.model}" is served by an Anthropic-protocol upstream, which has no embeddings API.`,{code:"embeddings_unsupported",param:"model"});const v={...e,model:c.model};l.meta.model=c.model;let m,j=null;for(let o=0;o<p.length;o++){const t=p[o];l.meta.upstream=t.name;const f=a.driver(t);t.health.inflight++,t.health.requests++;const T=y.join(t.url,M),_={"content-type":"application/json",accept:"application/json",...f.info.headers,...y.headers(t,f.info.auth,s.headers)},b=I(a,{body:e,sent:{upstream:t.name,url:T,headers:_,body:v,state:{stream:!1}},front:"openai",requested:l.meta.requested,log:s.log,exchange:j,attempt:o+1});j??=b.id;let r;try{r=await y.json($,T,{method:"POST",headers:_,body:v,signal:s.signal})}finally{t.health.inflight--}if(s.signal?.aborted)return n(499,"The request was aborted",{code:"aborted"});const d=r.status===0;if(!d&&r.status<500)return A.succeeded(t),b.reply(r.status,r.headers,r.body),r.ok?r.body===null||typeof r.body!="object"?n(502,"The upstream returned a body that is not JSON",{code:"bad_gateway"}):(l.json(r.body),u({status:200,headers:{"content-type":"application/json"},body:r.body},"done")):u(g(r.status,r.body,{},{front:"openai",upstream:t.name,native:!0,model:e.model}),"upstream_error");const i=d?`Could not reach the upstream (${t.name}): ${r.error?.message}`:`HTTP ${r.status}`;A.failed(a.config,t,i);const k=d?{error:i}:{status:r.status,headers:r.headers,body:r.body};b.attempt(k,o+1<p.length?i:void 0),N(a,{type:"error",kind:d?"transport":"upstream",level:"error",message:d?i:`Upstream ${t.name} answered HTTP ${r.status}`,endpoint:t.name,model:c.model,log:s.log}),m={endpoint:t,result:r,reason:i}}return m.result.status===0?n(502,m.reason,{code:"upstream_unreachable"}):u(g(m.result.status,m.result.body,{},{front:"openai",upstream:m.endpoint.name,native:!0,model:e.model}),"upstream_error")}h(Q,"send");export{Q as send};
2
+ //# sourceMappingURL=send.mjs.map
@@ -1,3 +1,3 @@
1
- var _=Object.defineProperty;var p=(e,o)=>_(e,"name",{value:o,configurable:!0});import{context as q,self as T}from"../lib/runtime.mjs";import{capture as I}from"../lib/capture.mjs";import{resolve as L}from"../lib/route.mjs";import*as w from"../lib/http.mjs";import{filterChatBody as B}from"../lib/passthrough.mjs";import*as $ from"../lib/images.mjs";import*as y from"../lib/tools.mjs";import{LIMITS as M}from"../lib/toolsearch.mjs";import{warn as h}from"../lib/warn.mjs";import{emit as P}from"../lib/events.mjs";import{fit as R,pinBody as C}from"../lib/effort.mjs";function E(e){return`${e.driver}:${e.protocol}:${URL.canParse(e.url)?new URL(e.url).host:e.url}`}p(E,"producerOf");async function V(e,o={}){const r=I(T,q,o.front),c=L(r,e,{endpoint:o.endpoint,advance:o.hold===!0&&o.endpoint===void 0});if(o.hold!==!0)return O(r,e,o,c);c.endpoint.health.inflight++;try{return await O(r,e,o,c)}catch(t){throw c.endpoint.health.inflight--,t}}p(V,"request");async function O(e,o,r,c){const{endpoint:t,model:s,unserved:F,convert:m,pool:U,pin:g}=c,d=Object.hasOwn(e.config.efforts,s)?e.config.efforts[s]:void 0;g&&(o=C(o,g,e.front));const i=e.driver(t),u=`${t.name}
2
- ${s}`,k=r.images===!1||e.state.textOnly.has(u)&&e.settings(t).imageFallback,v=e.settings(t),x=r.jsonObject==="instruction"||e.state.jsonInstruction.has(u)&&v.jsonObjectFallback,l={...k?{...v,images:!1}:v,...x?{jsonObject:"instruction"}:{},...g?.effort===void 0?{}:{effort:!0}},j=o.stream===!0;let n,a;if(m?({body:n,state:a}=await m.request(o,l,{producer:E(t),budget:r.budget,listed:d!==void 0}),a.toolSearch?.capped&&h(e.config,e.state,`Tool search: more than ${M.definitions} tool definitions came back in ToolSearch results for ${t.name}; the rest are not offered.`,{kind:"toolsearch",endpoint:t.name,log:r.log})):n=e.front==="openai"&&(r.filter??l.passthroughFilter===!0)?B(o,i.info.fields):{...o},r.usage===!0&&j&&e.front==="openai"){if(m)a.includeUsage=!0;else if(l.streamUsage){const f=n.stream_options&&typeof n.stream_options=="object"?n.stream_options:{};n.stream_options={...f,include_usage:!0}}}k&&!m&&e.front==="openai"&&(n=$.strip(n)),r.tools===!1&&(n=y.strip(n)),n.model=s,d&&t.protocol==="openai.chat"&&R(n,d,s,(f,S)=>h(e.config,e.state,f,{type:"effort",endpoint:t.name,model:s,...S,log:r.log})),a?.reasoningMissing&&h(e.config,e.state,`Upstream ${t.name}: assistant turns with tool calls have no reasoning to send back (the client dropped its thinking blocks or their signatures), which servers that need it with tools will refuse.`,{kind:"reasoning",endpoint:t.name,log:r.log}),i.request&&(n=await i.request(n,{endpoint:t,settings:l,levels:d,state:a,warn:p(f=>h(e.config,e.state,f,{kind:"driver",endpoint:t.name,log:r.log}),"warn")})),a?.dropped?.length>0&&r.retry!==!0&&P(e,{type:"dropped",level:"debug",message:`Upstream ${t.name}: dropped ${a.dropped.join(", ")}`,endpoint:t.name,model:s,fields:[...a.dropped],log:r.log});const b=t.protocol==="openai.chat"?i.info.paths.chat:i.info.paths.messages;return{upstream:t.name,pool:U.map(f=>f.name),method:"POST",url:w.join(t.url,b),path:b,headers:{"content-type":"application/json",accept:j?"text/event-stream":"application/json",...i.info.headers,...m===null&&t.protocol==="anthropic.messages"?r.clientHeaders:void 0,...w.headers(t,i.info.auth,r.headers)},body:n,state:{v:1,upstream:t.name,front:e.front,native:m===null,model:o.model,stream:j,unserved:F,convert:a},images:{sent:$.carries(n),retry:l.imageFallback&&t.protocol==="openai.chat",key:u},tools:{sent:y.carries(n),retry:l.toolFallback&&t.protocol==="openai.chat"},json:{sent:a?.jsonSchemaEmulated===!0,retry:l.jsonObjectFallback&&t.protocol==="anthropic.messages",key:u}}}p(O,"prepare");export{V as request};
1
+ var T=Object.defineProperty;var p=(e,n)=>T(e,"name",{value:n,configurable:!0});import{context as I,self as q}from"../lib/runtime.mjs";import{capture as C}from"../lib/capture.mjs";import{resolve as L}from"../lib/route.mjs";import*as U from"../lib/http.mjs";import{filterChatBody as B}from"../lib/passthrough.mjs";import{addSampling as E}from"../lib/sampling.mjs";import*as F from"../lib/images.mjs";import*as S from"../lib/tools.mjs";import{LIMITS as M}from"../lib/toolsearch.mjs";import{warn as b}from"../lib/warn.mjs";import{emit as P}from"../lib/events.mjs";import{fit as R,pinBody as H}from"../lib/effort.mjs";import{relax as z,strictTools as A}from"../lib/strict.mjs";import{TranslatorError as D}from"../lib/errors.mjs";function G(e){return`${e.driver}:${e.protocol}:${URL.canParse(e.url)?new URL(e.url).host:e.url}`}p(G,"producerOf");async function ne(e,n={}){const r=C(q,I,n.front),c=L(r,e,{endpoint:n.endpoint,advance:n.hold===!0&&n.endpoint===void 0});if(n.hold!==!0)return _(r,e,n,c);c.endpoint.health.inflight++;try{return await _(r,e,n,c)}catch(t){throw c.endpoint.health.inflight--,t}}p(ne,"request");function J(e,n,r,c,t,f){const g=A(n);if(g.length===0)return n;const i=g.filter(a=>a.problems.length>0);if(i.length>0&&t.unknownFields==="error")throw new D(`tools: ${i[0].name}: schema is outside the strict subset (${i[0].problems.join("; ")})`,{code:"unsupported_content",param:"tools"});const k=z(n);if(f.retry===!0)return k;const u=p((a,h)=>b(e.config,e.state,a,{kind:h,endpoint:r.name,log:f.log}),"tell");for(const a of i)u(`Upstream ${r.name}: strict tool ${a.name} has a schema outside the strict subset: ${a.problems.join("; ")} (sent without strict)`,"strict");const d=g.filter(a=>a.problems.length===0);return c.info.strict!==!0&&d.length>0&&u(`Upstream ${r.name}: the ${c.name} backend does not enforce strict tool use (${d.map(a=>a.name).join(", ")})`,"strict"),k}p(J,"checkStrict");async function _(e,n,r,c){const{endpoint:t,model:f,unserved:g,convert:i,pool:k,pin:u}=c,d=Object.hasOwn(e.config.efforts,f)?e.config.efforts[f]:void 0;u&&(n=H(n,u,e.front));const a=e.driver(t),h=`${t.name}
2
+ ${f}`,w=r.images===!1||e.state.textOnly.has(h)&&e.settings(t).imageFallback,$=e.settings(t),x=r.jsonObject==="instruction"||e.state.jsonInstruction.has(h)&&$.jsonObjectFallback,m={...w?{...$,images:!1}:$,...x?{jsonObject:"instruction"}:{},...u?.effort===void 0?{}:{effort:!0}},v=n.stream===!0;let o,s;i?({body:o,state:s}=await i.request(n,m,{producer:G(t),budget:r.budget,listed:d!==void 0,toolCallIds:a.info.toolCallIds}),s.toolSearch?.capped&&b(e.config,e.state,`Tool search: more than ${M.definitions} tool definitions came back in ToolSearch results for ${t.name}; the rest are not offered.`,{kind:"toolsearch",endpoint:t.name,log:r.log})):o=e.front==="openai"&&(r.filter??m.passthroughFilter===!0)?B(n,a.info.fields):{...n};const y=e.front==="anthropic"?n.metadata?.sampling:void 0;if(t.protocol==="openai.chat"){const l=[];o=E(o,y,m,a.info.fields,l),s&&s.dropped.push(...l.filter(j=>!s.dropped.includes(j)))}else if(!i&&y!==void 0&&o.metadata?.sampling!==void 0){const{sampling:l,...j}=o.metadata;o={...o,metadata:j}}if(r.usage===!0&&v&&e.front==="openai"){if(i)s.includeUsage=!0;else if(m.streamUsage){const l=o.stream_options&&typeof o.stream_options=="object"?o.stream_options:{};o.stream_options={...l,include_usage:!0}}}w&&!i&&e.front==="openai"&&(o=F.strip(o)),r.tools===!1&&(o=S.strip(o)),o.model=f,o=J(e,o,t,a,m,r),d&&t.protocol==="openai.chat"&&R(o,d,f,(l,j)=>b(e.config,e.state,l,{type:"effort",endpoint:t.name,model:f,...j,log:r.log})),s?.reasoningMissing&&b(e.config,e.state,`Upstream ${t.name}: assistant turns with tool calls have no reasoning to send back (the client dropped its thinking blocks or their signatures), which servers that need it with tools will refuse.`,{kind:"reasoning",endpoint:t.name,log:r.log}),a.request&&(o=await a.request(o,{endpoint:t,settings:m,levels:d,state:s,warn:p(l=>b(e.config,e.state,l,{kind:"driver",endpoint:t.name,log:r.log}),"warn")})),s?.dropped?.length>0&&r.retry!==!0&&P(e,{type:"dropped",level:"debug",message:`Upstream ${t.name}: dropped ${s.dropped.join(", ")}`,endpoint:t.name,model:f,fields:[...s.dropped],log:r.log});const O=t.protocol==="openai.chat"?a.info.paths.chat:a.info.paths.messages;return{upstream:t.name,pool:k.map(l=>l.name),method:"POST",url:U.join(t.url,O),path:O,headers:{"content-type":"application/json",accept:v?"text/event-stream":"application/json",...a.info.headers,...i===null&&t.protocol==="anthropic.messages"?r.clientHeaders:void 0,...U.headers(t,a.info.auth,r.headers)},body:o,state:{v:1,upstream:t.name,front:e.front,native:i===null,model:n.model,stream:v,unserved:g,convert:s},images:{sent:F.carries(o),retry:m.imageFallback&&t.protocol==="openai.chat",key:h},tools:{sent:S.carries(o),retry:m.toolFallback&&t.protocol==="openai.chat"},json:{sent:s?.jsonSchemaEmulated===!0,retry:m.jsonObjectFallback&&t.protocol==="anthropic.messages",key:h}}}p(_,"prepare");export{ne as request};
3
3
  //# sourceMappingURL=request.mjs.map
package/dist/api/send.mjs CHANGED
@@ -1,2 +1,2 @@
1
- var Z=Object.defineProperty;var l=(u,r)=>Z(u,"name",{value:r,configurable:!0});import{context as L,self as K}from"../lib/runtime.mjs";import{TranslatorError as J}from"../lib/errors.mjs";import*as tt from"../lib/http.mjs";import*as et from"../lib/images.mjs";import*as at from"../lib/tools.mjs";import{parse as nt}from"../lib/upstream-error.mjs";import{meter as rt,normalize as st}from"../lib/usage.mjs";import{capture as ot}from"../lib/capture.mjs";import{estimate as it,bodyLength as lt,output as ut,patch as ct,stream as mt}from"../lib/estimate.mjs";import{warn as dt}from"../lib/warn.mjs";import{emit as Q}from"../lib/events.mjs";import*as V from"../lib/pool.mjs";import{createBudget as pt}from"../lib/thinking.mjs";const ft={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"};async function St(u,r={}){const{transport:y,textOnly:d,jsonInstruction:k}=L.state,{request:j,response:_,stream:o,error:g}=K,p=g.build,{config:i,state:v}=L,h=ot(K,L,r.front),S=u&&typeof u=="object"&&!Array.isArray(u),x={model:null,requested:S&&typeof u.model=="string"?u.model:null,upstream:null,front:r.front??i.front,stream:S&&u.stream===!0},b=rt(x,r.onUsage,(c,E)=>dt(i,v,c,{type:"usage",...E,log:r.log})),O=l(c=>Object.defineProperty(c,"usage",{value:b.usage,enumerable:!1,configurable:!0}),"attach");try{return O(await gt({transport:y,textOnly:d,jsonInstruction:k,request:j,response:_,stream:o,error:g,captured:h},u,r,b))}catch(c){if(!(c instanceof J))throw r.signal?.aborted||Q(h,{type:"error",kind:"exception",level:"error",message:String(c?.message??c),log:r.log}),b.finish(r.signal?.aborted?"aborted":"error"),c;return b.finish("error"),O(p(c.status,c.message,{type:c.type,code:c.code,param:c.param,front:r.front}))}}l(St,"send");async function gt({transport:u,textOnly:r,jsonInstruction:y,request:d,response:k,stream:j,error:_,captured:o},g,p,i){const v={front:p.front,headers:p.headers,clientHeaders:p.clientHeaders,filter:p.filter,usage:!0,budget:pt(),log:p.log},h=l(e=>{Q(o,{...e,log:p.log})},"announce"),S=i.meta.front==="openai"&&g?.stream_options?.include_usage!==!0,x=l(e=>{i.meta.model=e.body.model??null,i.meta.upstream=e.upstream,e.state.native?i.source(null):i.source(()=>e.state.convert?.upstreamUsage,e.state.front==="anthropic"?"openai":"folded")},"note"),b=l(e=>{if(e.state.native)return{input:!1,output:!1};const t=st(e.state.front==="anthropic"?"openai":"folded",e.state.convert?.upstreamUsage);return t===null?{input:!0,output:!0}:{input:t.input_tokens===null&&t.cache_read_input_tokens===null&&t.cache_creation_input_tokens===null,output:t.output_tokens===null}},"gaps"),O=l(async e=>{const t=o.endpoint(e.upstream),a=o.driver(t);return(a.count&&!(S&&e.state.stream)?await a.count({endpoint:t,request:e.body,transport:u,signal:p.signal,timeout:o.config.detectTimeout}):null)??it(e.body)},"input"),c=l(async(e,t)=>{const a=b(e);return!a.input&&!a.output?null:{input_tokens:a.input?await O(e):null,output_tokens:a.output?ut(t):null}},"counts"),E=l((e,t,a,n,B)=>(i.finish(B),_(e,t,a,n)),"fail"),X=l(async e=>{let t=e,a=await W(u,t,p.signal,R());const n=a.status,B=n===400&&[t.images,t.tools].some(T=>T.sent&&T.retry),D=(n===400||n===422)&&t.json.sent&&t.json.retry;if(!a.ok&&(B||D)){const T=await M(a),$=nt(T,n).message,f={};if(n===400&&t.images.sent&&t.images.retry&&et.refused($)?(r.add(t.images.key),f.images=!1):n===400&&t.tools.sent&&t.tools.retry&&at.refused($)?f.tools=!1:D&&(f.jsonObject="instruction"),!("images"in f||"tools"in f||"jsonObject"in f))return{upstream:t,result:a,failure:T};const w={upstream:t,result:a,failure:T};if("images"in f||"tools"in f){const I="images"in f?"images":"tools";h({type:"fallback",kind:I,level:"warn",message:`Upstream ${t.upstream}: ${t.state.model} refused ${I==="images"?"images":"tools"} (${n}: "${String($).trim().slice(0,500)}"); sent the request again without them.`,endpoint:t.upstream,model:t.body.model,status:n,reason:String($).trim().slice(0,500),action:I==="images"?"text-only":"without-tools"})}if(t=await d(g,{...v,...f,endpoint:t.upstream,retry:!0}),x(t),a=await W(u,t,p.signal,R()),D&&"jsonObject"in f){if(!a.ok){try{await a.body?.cancel?.()}catch{}return x(w.upstream),w}y.add(w.upstream.json.key),h({type:"fallback",kind:"json_object",level:"warn",message:`Upstream ${w.upstream.upstream}: ${w.upstream.state.model} rejected the json_object schema emulation (${n}: "${String($).trim().slice(0,500)}"); sent it as a system instruction instead. Set jsonObject: "instruction" on this upstream to skip the first attempt.`,endpoint:w.upstream.upstream,model:w.upstream.body.model,status:n,reason:String($).trim().slice(0,500),action:"instruction"})}}return{upstream:t,result:a}},"attempt");let s=await d(g,{...v,hold:!0});const H=s.pool;let q=0;const P=H.length>1?o.config.pool:null,R=l(()=>P&&{connectTimeout:P.connectTimeout,headersTimeout:q+1<H.length?P.headersTimeout:0},"limitsNow");let U=o.endpoint(s.upstream);const F=l(()=>U.health.inflight--,"release");let m,N;try{for(;;){const e=U;e.health.requests++,x(s);let t=null;try{({upstream:s,result:m,failure:N}=await X(s)),!m.ok&&m.status>=500&&(t=`HTTP ${m.status}`,h({type:"error",kind:"upstream",level:"error",message:`Upstream ${e.name} answered HTTP ${m.status}`,endpoint:e.name,model:s.body.model,status:m.status}))}catch(n){if(!(n instanceof J)||n.code!=="upstream_unreachable")throw n;t=n.message,m=void 0,N=n,h({type:"error",kind:"transport",level:"error",message:n.message,endpoint:e.name,model:s.body.model})}if(t===null){V.succeeded(e);break}V.failed(o.config,e,t);let a=null;for(;a===null&&!p.signal?.aborted&&q+1<H.length;)try{a=await d(g,{...v,endpoint:H[++q],hold:!0})}catch(n){if(!(n instanceof J)||n.code!=="endpoint_not_in_pool")throw n}if(a===null){if(m===void 0)throw N;break}h({type:"failover",level:"warn",message:`Upstream ${e.name} failed (${t}); trying ${a.upstream}`,from:e.name,to:a.upstream,model:a.body.model,reason:t});try{await m?.body?.cancel?.()}catch{}F(),U=o.endpoint(a.upstream),s=a}}catch(e){throw F(),e}const Y=U;U=null,i.usage.then(()=>Y.health.inflight--);const G=tt.plain(m.headers);if(!m.ok)return E(m.status,N??await M(m),G,s.state,"upstream_error");if(s.state.stream){const e=j(m.body,s.state),t=s.state.native?e:mt(i.meta.front,e,{gaps:l(()=>b(s),"gaps"),input:l(()=>O(s),"input"),report:l(a=>i.estimate(a),"report")});return{status:200,headers:ft,stream:i.track(t,p.signal,S)}}const C=await M(m);if(C===null||typeof C!="object")return h({type:"error",kind:"upstream",level:"error",message:`Upstream ${s.upstream} returned a body that is not JSON`,endpoint:s.upstream,model:s.body.model,status:502}),E(502,"The upstream returned a body that is not JSON",G,s.state,"error");const A=await k(C,s.state),z=await c(s,lt(i.meta.front,A));return z&&(A.usage=ct(i.meta.front,A.usage,z),i.estimate(z)),i.json(A),i.finish("done"),{status:200,headers:{"content-type":"application/json"},body:A}}l(gt,"round");async function W(u,r,y,d=null){let k,j=!1,_=y;if(d&&d.headersTimeout>0){const o=new AbortController;_=y?AbortSignal.any([y,o.signal]):o.signal,k=setTimeout(()=>{j=!0,o.abort()},d.headersTimeout)}try{return await u(r.url,{method:r.method,headers:r.headers,body:JSON.stringify(r.body),signal:_,redirect:"error",...d&&d.connectTimeout>0?{connectTimeout:d.connectTimeout}:{}})}catch(o){if(y?.aborted)throw o;const g=j?`no response headers within ${d.headersTimeout} ms`:o.message;throw new J(`Could not reach the upstream (${r.upstream}): ${g}`,{code:"upstream_unreachable",status:502,details:{cause:o}})}finally{clearTimeout(k)}}l(W,"call");async function M(u){const r=await u.text();try{return JSON.parse(r)}catch{return r===""?null:r}}l(M,"parse");export{St as send};
1
+ var nt=Object.defineProperty;var u=(c,s)=>nt(c,"name",{value:s,configurable:!0});import{context as F,self as Y}from"../lib/runtime.mjs";import{TranslatorError as C}from"../lib/errors.mjs";import*as Z from"../lib/http.mjs";import*as ot from"../lib/images.mjs";import*as it from"../lib/tools.mjs";import{parse as ut}from"../lib/upstream-error.mjs";import{meter as lt,normalize as ct}from"../lib/usage.mjs";import{capture as mt}from"../lib/capture.mjs";import{estimate as dt,bodyLength as pt,output as ft,patch as gt,stream as ht}from"../lib/estimate.mjs";import{warn as yt}from"../lib/warn.mjs";import{start as wt}from"../lib/record.mjs";import{emit as tt}from"../lib/events.mjs";import*as et from"../lib/pool.mjs";import{createBudget as bt}from"../lib/thinking.mjs";const kt={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"};async function Ht(c,s={}){const{transport:y,textOnly:p,jsonInstruction:k}=F.state,{request:j,response:_,stream:l,error:g}=Y,d=g.build,{config:i,state:x}=F,h=mt(Y,F,s.front),S=c&&typeof c=="object"&&!Array.isArray(c),O={model:null,requested:S&&typeof c.model=="string"?c.model:null,upstream:null,front:s.front??i.front,stream:S&&c.stream===!0},w=lt(O,s.onUsage,(m,H)=>yt(i,x,m,{type:"usage",...H,log:s.log})),U=u(m=>Object.defineProperty(m,"usage",{value:w.usage,enumerable:!1,configurable:!0}),"attach");try{return U(await jt({transport:y,textOnly:p,jsonInstruction:k,request:j,response:_,stream:l,error:g,captured:h},c,s,w))}catch(m){if(!(m instanceof C))throw s.signal?.aborted||tt(h,{type:"error",kind:"exception",level:"error",message:String(m?.message??m),log:s.log}),w.finish(s.signal?.aborted?"aborted":"error"),m;return w.finish("error"),U(d(m.status,m.message,{type:m.type,code:m.code,param:m.param,front:s.front}))}}u(Ht,"send");async function jt({transport:c,textOnly:s,jsonInstruction:y,request:p,response:k,stream:j,error:_,captured:l},g,d,i){const x={front:d.front,headers:d.headers,clientHeaders:d.clientHeaders,filter:d.filter,usage:!0,budget:bt(),log:d.log},h=u(e=>{tt(l,{...e,log:d.log})},"announce"),S=i.meta.front==="openai"&&g?.stream_options?.include_usage!==!0,O=u(e=>{i.meta.model=e.body.model??null,i.meta.upstream=e.upstream,e.state.native?i.source(null):i.source(()=>e.state.convert?.upstreamUsage,e.state.front==="anthropic"?"openai":"folded")},"note"),w=u(e=>{if(e.state.native)return{input:!1,output:!1};const t=ct(e.state.front==="anthropic"?"openai":"folded",e.state.convert?.upstreamUsage);return t===null?{input:!0,output:!0}:{input:t.input_tokens===null&&t.cache_read_input_tokens===null&&t.cache_creation_input_tokens===null,output:t.output_tokens===null}},"gaps"),U=u(async e=>{const t=l.endpoint(e.upstream),a=l.driver(t);return(a.count&&!(S&&e.state.stream)?await a.count({endpoint:t,request:e.body,transport:c,signal:d.signal,timeout:l.config.detectTimeout}):null)??dt(e.body)},"input"),m=u(async(e,t)=>{const a=w(e);return!a.input&&!a.output?null:{input_tokens:a.input?await U(e):null,output_tokens:a.output?ft(t):null}},"counts"),H=u((e,t,a,r,L)=>(i.finish(L),_(e,t,a,r)),"fail"),rt=u(async e=>{let t=e,a=await at(c,t,d.signal,G());const r=a.status,L=r===400&&[t.images,t.tools].some($=>$.sent&&$.retry),M=(r===400||r===422)&&t.json.sent&&t.json.retry;if(!a.ok&&(L||M)){const $=await z(a),v=ut($,r).message,f={};if(r===400&&t.images.sent&&t.images.retry&&ot.refused(v)?(s.add(t.images.key),f.images=!1):r===400&&t.tools.sent&&t.tools.retry&&it.refused(v)?f.tools=!1:M&&(f.jsonObject="instruction"),!("images"in f||"tools"in f||"jsonObject"in f))return{upstream:t,result:a,failure:$};const b={upstream:t,result:a,failure:$};if("images"in f||"tools"in f){const R="images"in f?"images":"tools";h({type:"fallback",kind:R,level:"warn",message:`Upstream ${t.upstream}: ${t.state.model} refused ${R==="images"?"images":"tools"} (${r}: "${String(v).trim().slice(0,500)}"); sent the request again without them.`,endpoint:t.upstream,model:t.body.model,status:r,reason:String(v).trim().slice(0,500),action:R==="images"?"text-only":"without-tools"})}if(t=await p(g,{...x,...f,endpoint:t.upstream,retry:!0}),O(t),a=await at(c,t,d.signal,G()),M&&"jsonObject"in f){if(!a.ok){try{await a.body?.cancel?.()}catch{}return O(b.upstream),b}y.add(b.upstream.json.key),h({type:"fallback",kind:"json_object",level:"warn",message:`Upstream ${b.upstream.upstream}: ${b.upstream.state.model} rejected the json_object schema emulation (${r}: "${String(v).trim().slice(0,500)}"); sent it as a system instruction instead. Set jsonObject: "instruction" on this upstream to skip the first attempt.`,endpoint:b.upstream.upstream,model:b.upstream.body.model,status:r,reason:String(v).trim().slice(0,500),action:"instruction"})}}return{upstream:t,result:a}},"attempt");let n=await p(g,{...x,hold:!0});const N=n.pool;let B=0;const D=N.length>1?l.config.pool:null,G=u(()=>D&&{connectTimeout:D.connectTimeout,headersTimeout:B+1<N.length?D.headersTimeout:0},"limitsNow");let q=l.endpoint(n.upstream);const K=u(()=>q.health.inflight--,"release");let o,T,Q=null,V=0;const W=u(e=>{const t=wt(l,{body:g,sent:n,front:i.meta.front,requested:i.meta.requested,headers:e,log:d.log,exchange:Q,attempt:V});return Q??=t.id,t},"begin"),X=u(async e=>{const t=W();if(!t.on)return;if(o===void 0)return t.attempt({error:T.message},e);let a=null;try{a=T??await z(o)}catch{}t.attempt({status:o.status,headers:Z.plain(o.headers),body:a},e)},"keep");try{for(;;){const e=q;e.health.requests++,V++,O(n);let t=null;try{({upstream:n,result:o,failure:T}=await rt(n)),!o.ok&&o.status>=500&&(t=`HTTP ${o.status}`,h({type:"error",kind:"upstream",level:"error",message:`Upstream ${e.name} answered HTTP ${o.status}`,endpoint:e.name,model:n.body.model,status:o.status}))}catch(r){if(!(r instanceof C)||r.code!=="upstream_unreachable")throw r;t=r.message,o=void 0,T=r,h({type:"error",kind:"transport",level:"error",message:r.message,endpoint:e.name,model:n.body.model})}if(t===null){et.succeeded(e);break}et.failed(l.config,e,t);let a=null;for(;a===null&&!d.signal?.aborted&&B+1<N.length;)try{a=await p(g,{...x,endpoint:N[++B],hold:!0})}catch(r){if(!(r instanceof C)||r.code!=="endpoint_not_in_pool")throw r}if(a===null){if(o===void 0)throw await X(),T;break}await X(t),h({type:"failover",level:"warn",message:`Upstream ${e.name} failed (${t}); trying ${a.upstream}`,from:e.name,to:a.upstream,model:a.body.model,reason:t});try{await o?.body?.cancel?.()}catch{}K(),q=l.endpoint(a.upstream),n=a}}catch(e){throw K(),e}const st=q;q=null,i.usage.then(()=>st.health.inflight--);const A=Z.plain(o.headers),J=W(A);if(!o.ok){const e=T??await z(o);return J.reply(o.status,A,e),H(o.status,e,A,n.state,"upstream_error")}if(n.state.stream){const e=j(J.raw(o.body),n.state),t=n.state.native?e:ht(i.meta.front,e,{gaps:u(()=>w(n),"gaps"),input:u(()=>U(n),"input"),report:u(a=>i.estimate(a),"report")});return{status:200,headers:kt,stream:J.events(i.track(t,d.signal,S))}}const P=await z(o);if(J.reply(o.status,A,P),P===null||typeof P!="object")return h({type:"error",kind:"upstream",level:"error",message:`Upstream ${n.upstream} returned a body that is not JSON`,endpoint:n.upstream,model:n.body.model,status:502}),H(502,"The upstream returned a body that is not JSON",A,n.state,"error");const E=await k(P,n.state),I=await m(n,pt(i.meta.front,E));return I&&(E.usage=gt(i.meta.front,E.usage,I),i.estimate(I)),i.json(E),i.finish("done"),{status:200,headers:{"content-type":"application/json"},body:E}}u(jt,"round");async function at(c,s,y,p=null){let k,j=!1,_=y;if(p&&p.headersTimeout>0){const l=new AbortController;_=y?AbortSignal.any([y,l.signal]):l.signal,k=setTimeout(()=>{j=!0,l.abort()},p.headersTimeout)}try{return await c(s.url,{method:s.method,headers:s.headers,body:JSON.stringify(s.body),signal:_,redirect:"error",...p&&p.connectTimeout>0?{connectTimeout:p.connectTimeout}:{}})}catch(l){if(y?.aborted)throw l;const g=j?`no response headers within ${p.headersTimeout} ms`:l.message;throw new C(`Could not reach the upstream (${s.upstream}): ${g}`,{code:"upstream_unreachable",status:502,details:{cause:l}})}finally{clearTimeout(k)}}u(at,"call");async function z(c){const s=await c.text();try{return JSON.parse(s)}catch{return s===""?null:s}}u(z,"parse");export{Ht as send};
2
2
  //# sourceMappingURL=send.mjs.map
@@ -1,2 +1,2 @@
1
- var w=Object.defineProperty;var n=(o,t)=>w(o,"name",{value:t,configurable:!0});import{context as s,self as u}from"../../lib/runtime.mjs";import{TranslatorError as m}from"../../lib/errors.mjs";import{register as b}from"../../lib/routes.mjs";import{capture as k}from"../../lib/capture.mjs";import{resolve as x}from"../../lib/route.mjs";import{resolve as y}from"../../lib/settings.mjs";import{emit as I}from"../../lib/events.mjs";async function J(o,t={}){const{config:r}=s;if(t.front!==void 0&&t.front!=="anthropic"&&t.front!=="openai")throw new m(`front must be "anthropic" or "openai" (got ${JSON.stringify(t.front)})`,{code:"invalid_config",status:500});for(const e of["authorize","onUsage"])if(t[e]!==void 0&&typeof t[e]!="function")throw new m(`${e} must be a function`,{code:"invalid_config",status:500});const{send:d,route:p,token:h,model:c,error:v,sse:f}=u,l=t.front??r.front,{state:a}=s,{events:i}=a;i.logger=o.log,o.addHook("onClose",async()=>{i.logger===o.log&&(i.logger=null)});const g=k(u,s,l);b(o,t,{front:l,filter:r.settings.passthroughFilter!==!1,interval:n(e=>{try{return g.settings(x(g,e).endpoint).pingInterval}catch{return y(r.settings).pingInterval}},"interval"),send:d,route:p,count:h.count,list:c.list,get:c.get,emit:n(e=>I({config:r,state:a},e),"emit"),build:v.build,encode:f.encode,write:f.write})}n(J,"plugin");export{J as plugin};
1
+ var w=Object.defineProperty;var n=(o,t)=>w(o,"name",{value:t,configurable:!0});import{context as s,self as u}from"../../lib/runtime.mjs";import{TranslatorError as d}from"../../lib/errors.mjs";import{register as k}from"../../lib/routes.mjs";import{capture as x}from"../../lib/capture.mjs";import{resolve as y}from"../../lib/route.mjs";import{resolve as I}from"../../lib/settings.mjs";import{emit as T}from"../../lib/events.mjs";async function N(o,t={}){const{config:r}=s;if(t.front!==void 0&&t.front!=="anthropic"&&t.front!=="openai")throw new d(`front must be "anthropic" or "openai" (got ${JSON.stringify(t.front)})`,{code:"invalid_config",status:500});for(const e of["authorize","onUsage"])if(t[e]!==void 0&&typeof t[e]!="function")throw new d(`${e} must be a function`,{code:"invalid_config",status:500});const{send:m,route:p,token:h,model:c,error:v,sse:f,embeddings:b}=u,l=t.front??r.front,{state:g}=s,{events:i}=g;i.logger=o.log,o.addHook("onClose",async()=>{i.logger===o.log&&(i.logger=null)});const a=x(u,s,l);k(o,t,{front:l,filter:r.settings.passthroughFilter!==!1,interval:n(e=>{try{return a.settings(y(a,e).endpoint).pingInterval}catch{return I(r.settings).pingInterval}},"interval"),send:m,embed:b.send,route:p,count:h.count,list:c.list,get:c.get,emit:n(e=>T({config:r,state:g},e),"emit"),build:v.build,encode:f.encode,write:f.write})}n(N,"plugin");export{N as plugin};
2
2
  //# sourceMappingURL=plugin.mjs.map
@@ -1,2 +1,2 @@
1
- var i=Object.defineProperty;var n=(t,r)=>i(t,"name",{value:r,configurable:!0});import{TranslatorError as o}from"./errors.mjs";import*as a from"./pool.mjs";import{normalizeAliases as l,normalizeEfforts as u}from"./effort.mjs";function p(t){let r=t.length;for(;t[r-1]==="/";)r--;const e=t.slice(0,r);return e.endsWith("/v1")?e.slice(0,-3):e}n(p,"baseUrl");function d(t,r){const e=typeof t=="string"?{url:t}:t;if(!e||typeof e!="object"||typeof e.url!="string"||e.url==="")throw new o(`upstreams[${r}] needs a "url"`,{code:"INVALID_CONFIG",status:500});return{name:e.name??`upstream-${r}`,url:p(e.url),apiKey:e.apiKey,headers:{...e.headers},backend:e.backend??"auto",models:Array.isArray(e.models)?[...e.models]:void 0,default:e.default===!0,settings:{...e.settings},version:e.version,driver:null,facts:null,driverSettings:{},probed:{},health:a.health()}}n(d,"endpoint");function I(t={}){const r=t.front??"anthropic";if(r!=="anthropic"&&r!=="openai")throw new o(`front must be "anthropic" or "openai" (got ${JSON.stringify(r)})`,{code:"INVALID_CONFIG",status:500});let e=t.upstreams??t.upstream;if(e!==void 0&&!Array.isArray(e)&&(e=[e]),!e||e.length===0)throw new o("at least one upstream is required (config.upstreams)",{code:"INVALID_CONFIG",status:500});const s=t.logs??!1;if(typeof s!="boolean"&&(typeof s!="string"||s===""))throw new o("logs must be true, false or a file path (got "+JSON.stringify(s)+")",{code:"INVALID_CONFIG",status:500});if(t.onEvent!==void 0&&typeof t.onEvent!="function")throw new o("onEvent must be a function",{code:"INVALID_CONFIG",status:500});return{front:r,endpoints:e.map(d),...l(t.aliases),efforts:u(t.efforts),pool:a.normalize(t.pool),settings:{...t.settings},detect:t.detect!==!1,detectTimeout:t.detectTimeout??5e3,transport:t.transport,logs:s,events:t.events!==!1,onEvent:t.onEvent}}n(I,"normalize");export{d as endpoint,I as normalize};
1
+ var i=Object.defineProperty;var a=(t,r)=>i(t,"name",{value:r,configurable:!0});import{TranslatorError as s}from"./errors.mjs";import*as n from"./pool.mjs";import{valid as l}from"./record.mjs";import{normalizeAliases as u,normalizeEfforts as p}from"./effort.mjs";function d(t){let r=t.length;for(;t[r-1]==="/";)r--;const e=t.slice(0,r);return e.endsWith("/v1")?e.slice(0,-3):e}a(d,"baseUrl");function f(t,r){const e=typeof t=="string"?{url:t}:t;if(!e||typeof e!="object"||typeof e.url!="string"||e.url==="")throw new s(`upstreams[${r}] needs a "url"`,{code:"INVALID_CONFIG",status:500});return{name:e.name??`upstream-${r}`,url:d(e.url),apiKey:e.apiKey,headers:{...e.headers},backend:e.backend??"auto",models:Array.isArray(e.models)?[...e.models]:void 0,default:e.default===!0,settings:{...e.settings},version:e.version,driver:null,facts:null,driverSettings:{},probed:{},health:n.health()}}a(f,"endpoint");function N(t={}){const r=t.front??"anthropic";if(r!=="anthropic"&&r!=="openai")throw new s(`front must be "anthropic" or "openai" (got ${JSON.stringify(r)})`,{code:"INVALID_CONFIG",status:500});let e=t.upstreams??t.upstream;if(e!==void 0&&!Array.isArray(e)&&(e=[e]),!e||e.length===0)throw new s("at least one upstream is required (config.upstreams)",{code:"INVALID_CONFIG",status:500});const o=t.logs??!1;if(typeof o!="boolean"&&(typeof o!="string"||o===""))throw new s("logs must be true, false or a file path (got "+JSON.stringify(o)+")",{code:"INVALID_CONFIG",status:500});if(t.onEvent!==void 0&&typeof t.onEvent!="function")throw new s("onEvent must be a function",{code:"INVALID_CONFIG",status:500});if(!l(t.capture))throw new s("capture must be false, a directory path or a function (got "+JSON.stringify(t.capture)+")",{code:"INVALID_CONFIG",status:500});return{front:r,endpoints:e.map(f),...u(t.aliases),efforts:p(t.efforts),pool:n.normalize(t.pool),settings:{...t.settings},detect:t.detect!==!1,detectTimeout:t.detectTimeout??5e3,transport:t.transport,logs:o,capture:t.capture||!1,events:t.events!==!1,onEvent:t.onEvent}}a(N,"normalize");export{f as endpoint,N as normalize};
2
2
  //# sourceMappingURL=config.mjs.map
package/dist/lib/http.mjs CHANGED
@@ -1,2 +1,2 @@
1
- var y=Object.defineProperty;var o=(t,e)=>y(t,"name",{value:e,configurable:!0});function d(t,e){return t+e}o(d,"join");function p(t){return t==="localhost"||t==="::1"||/^127\./.test(t)}o(p,"isLoopback");function x(t,e="bearer",n){const r={...t.headers};return t.apiKey&&(e==="x-api-key"?r["x-api-key"]=t.apiKey:r.authorization=`Bearer ${t.apiKey}`),{...r,...n}}o(x,"headers");function b({endpoint:t,transport:e,signal:n,timeout:r,probes:a},i){const f=o(()=>g(e,d(t.url,i),{method:"GET",headers:x(t),signal:n,timeout:r}),"run");return a?(a.has(i)||a.set(i,f()),a.get(i)):f()}o(b,"probe");function k(t){const e={};if(!t)return e;if(typeof t.forEach=="function")return t.forEach((n,r)=>{e[r.toLowerCase()]=n}),e;for(const[n,r]of Object.entries(t))e[n.toLowerCase()]=String(r);return e}o(k,"plain");async function g(t,e,n={}){const{timeout:r,signal:a,body:i,...f}=n,s=[];a&&s.push(a),r>0&&s.push(AbortSignal.timeout(r));try{const u=await t(e,{...f,redirect:"error",body:i===void 0?void 0:JSON.stringify(i),signal:s.length>1?AbortSignal.any(s):s[0]}),c=await u.text();let l=null;try{l=JSON.parse(c)}catch{l=c===""?null:c}return{ok:u.ok,status:u.status,body:l}}catch(u){return{ok:!1,status:0,body:null,error:u}}}o(g,"json");export{x as headers,p as isLoopback,d as join,g as json,k as plain,b as probe};
1
+ var h=Object.defineProperty;var s=(t,e)=>h(t,"name",{value:e,configurable:!0});function y(t,e){return t+e}s(y,"join");function k(t){return t==="localhost"||t==="::1"||/^127\./.test(t)}s(k,"isLoopback");function d(t,e="bearer",a){const r={...t.headers};return t.apiKey&&(e==="x-api-key"?r["x-api-key"]=t.apiKey:r.authorization=`Bearer ${t.apiKey}`),{...r,...a}}s(d,"headers");function b({endpoint:t,transport:e,signal:a,timeout:r,probes:u},f,i){const o=i!==void 0&&(i.auth==="x-api-key"||i.headers!==void 0),n=o?{auth:i.auth,extra:i.headers}:{},l=s(()=>p(e,y(t.url,f),{method:"GET",headers:d(t,n.auth,n.extra),signal:a,timeout:r}),"run");if(!u)return l();const c=o?JSON.stringify([f,n.auth,n.extra]):f;return u.has(c)||u.set(c,l()),u.get(c)}s(b,"probe");function x(t){const e={};if(!t)return e;if(typeof t.forEach=="function")return t.forEach((a,r)=>{e[r.toLowerCase()]=a}),e;for(const[a,r]of Object.entries(t))e[a.toLowerCase()]=String(r);return e}s(x,"plain");async function p(t,e,a={}){const{timeout:r,signal:u,body:f,...i}=a,o=[];u&&o.push(u),r>0&&o.push(AbortSignal.timeout(r));try{const n=await t(e,{...i,redirect:"error",body:f===void 0?void 0:JSON.stringify(f),signal:o.length>1?AbortSignal.any(o):o[0]}),l=await n.text();let c=null;try{c=JSON.parse(l)}catch{c=l===""?null:l}return{ok:n.ok,status:n.status,body:c,headers:x(n.headers)}}catch(n){return{ok:!1,status:0,body:null,error:n}}}s(p,"json");export{d as headers,k as isLoopback,y as join,p as json,x as plain,b as probe};
2
2
  //# sourceMappingURL=http.mjs.map
package/dist/lib/ids.mjs CHANGED
@@ -1,2 +1,2 @@
1
- var f=Object.defineProperty;var n=(t,r)=>f(t,"name",{value:r,configurable:!0});import{randomBytes as p,createHash as h}from"node:crypto";function i(t=12){return p(t).toString("hex")}n(i,"random");function $(){return`msg_${i()}`}n($,"messageId");function a(){return`toolu_${i()}`}n(a,"toolUseId");function d(){return`chatcmpl-${i()}`}n(d,"completionId");function x(){return`call_${i()}`}n(x,"callId");function O(t){return typeof t!="string"||t===""?a():t.replace(/[^a-zA-Z0-9_-]/g,"_")}n(O,"anthropicToolId");function b({max:t,map:r={}}){const u=new Map(Object.entries(r).map(([e,c])=>[c,e]));return e=>{if(e.length<=t&&/^[a-zA-Z0-9_-]+$/.test(e))return e;if(u.has(e))return u.get(e);const c=e.replace(/[^a-zA-Z0-9_-]/g,"_");let o=c;if(c.length>t||c===""){const s=h("sha256").update(e).digest("hex").slice(0,8);o=`${c.slice(0,t-9)}_${s}`}const l=o;for(let s=2;Object.hasOwn(r,o);s++)o=`${l.slice(0,t-4)}_${s}`;return r[o]=e,u.set(e,o),o}}n(b,"createNameFitter");function I(t,r){return Object.hasOwn(t,r)?t[r]:r}n(I,"restoreName");export{O as anthropicToolId,x as callId,d as completionId,b as createNameFitter,$ as messageId,I as restoreName,a as toolUseId};
1
+ var I=Object.defineProperty;var o=(t,e)=>I(t,"name",{value:e,configurable:!0});import{randomBytes as _,createHash as w}from"node:crypto";import{TranslatorError as y}from"./errors.mjs";function h(t=12){return _(t).toString("hex")}o(h,"random");function M(){return`msg_${h()}`}o(M,"messageId");function j(){return`toolu_${h()}`}o(j,"toolUseId");function S(){return`chatcmpl-${h()}`}o(S,"completionId");function T(){return`call_${h()}`}o(T,"callId");function z(t,e={}){return typeof t=="string"&&Object.hasOwn(e,t)?e[t]:typeof t!="string"||t===""?j():t.replace(/[^a-zA-Z0-9_-]/g,"_")}o(z,"anthropicToolId");function O(t){let e;try{e=new RegExp(`^[${t}]$`)}catch{return[]}const c=[];for(let n=33;n<=126;n++)e.test(String.fromCharCode(n))&&c.push(String.fromCharCode(n));return c}o(O,"alphabetOf");function E(t){if(t===null||typeof t!="object"||Array.isArray(t))return!1;const{alphabet:e,min:c,max:n}=t;if(typeof e!="string"||O(e).length===0)return!1;const f=o(s=>s===void 0||Number.isInteger(s)&&s>=1,"bound");return f(c)&&f(n)&&(c??1)<=(n??1/0)}o(E,"validToolIdPolicy");function m(t,e,c){let n="";for(let f=0;n.length<c;f++)for(const s of w("sha256").update(`${t}\0${f}`).digest()){if(n.length===c)break;n+=e[s%e.length]}return n}o(m,"hashed");function N({policy:t,ids:e,map:c={}}){const n=O(t.alphabet),f=new RegExp(`^[${t.alphabet}]+$`),s=t.min??1,u=t.max??1/0,i=Math.min(u,Math.max(s,16)),l=new Map,d=new Set,g=o(r=>f.test(r)&&r.length>=s&&r.length<=u,"conforms"),b=o(r=>{let a=r;if(!g(r)){let x=0;for(a=m(r,n,i);d.has(a);){if(++x>255)throw new y("Tool call ids cannot be mapped into the upstream's id format: no free id is left",{code:"invalid_request",status:400});a=m(`${r}\0${x}`,n,i)}c[a]=r}return d.add(a),l.set(r,a),a},"assign"),p=o(r=>typeof r=="string"?r:"","keyOf"),$=o(r=>l.get(p(r))??b(p(r)),"fit");for(const r of e){const a=p(r);g(a)&&!l.has(a)&&b(a)}for(const r of e)$(r);return $}o(N,"createIdFitter");function Z(t,e){return Object.hasOwn(t,e)?t[e]:e}o(Z,"restoreId");function F({max:t,map:e={}}){const c=new Map(Object.entries(e).map(([n,f])=>[f,n]));return n=>{if(n.length<=t&&/^[a-zA-Z0-9_-]+$/.test(n))return n;if(c.has(n))return c.get(n);const f=n.replace(/[^a-zA-Z0-9_-]/g,"_");let s=f;if(f.length>t||f===""){const i=w("sha256").update(n).digest("hex").slice(0,8);s=`${f.slice(0,t-9)}_${i}`}const u=s;for(let i=2;Object.hasOwn(e,s);i++)s=`${u.slice(0,t-4)}_${i}`;return e[s]=n,c.set(n,s),s}}o(F,"createNameFitter");function R(t,e){return Object.hasOwn(t,e)?t[e]:e}o(R,"restoreName");export{z as anthropicToolId,T as callId,S as completionId,N as createIdFitter,F as createNameFitter,M as messageId,Z as restoreId,R as restoreName,j as toolUseId,E as validToolIdPolicy};
2
2
  //# sourceMappingURL=ids.mjs.map
@@ -0,0 +1,3 @@
1
+ var E=Object.defineProperty;var s=(t,a)=>E(t,"name",{value:a,configurable:!0});import{randomBytes as F}from"node:crypto";import{mkdirSync as O,writeFileSync as T}from"node:fs";import w from"node:path";import{items as v}from"./source.mjs";import{warn as $}from"./warn.mjs";const l=1e6,j=/key|token|secret|auth|cookie|password/i;function p(t){const a={};for(const[n,i]of Object.entries(t??{}))a[n.toLowerCase()]=j.test(n)?"[redacted]":i;return a}s(p,"redact");function L(t){return t===void 0||t===!1||typeof t=="function"||typeof t=="string"&&t!==""}s(L,"valid");const C={on:!1,id:null,raw:s(t=>t,"raw"),events:s(t=>t,"events"),reply(){},attempt(){}};function M({config:t,state:a},n){const i=t.capture;if(!i)return C;const{sent:f}=n,u=F(6).toString("hex"),g=Date.now(),c={time:g,id:u,exchange:n.exchange??u,attempt:n.attempt,front:n.front,requested:n.requested,model:f.body?.model??null,endpoint:f.upstream,stream:f.state.stream,client:{body:n.body},upstream:{url:f.url,headers:p(f.headers),body:f.body},response:{}};let d="",S=!1,m=0,y=!1;const b=[],h=s(()=>{try{if(typeof i=="function"){const e=i(c);typeof e?.then=="function"&&e.then(void 0,r=>k(r))}else O(w.resolve(i),{recursive:!0}),T(w.join(w.resolve(i),`${g}-${u}.json`),`${JSON.stringify(c,null,2)}
2
+ `)}catch(e){k(e)}},"emit"),k=s(e=>{$(t,a,`Could not capture the exchange (${e instanceof Error?e.message:String(e)}); the reply is not affected`,{type:"warning",kind:"capture",log:n.log})},"problem");return{on:!0,id:u,raw(e){return(async function*(){const r=new TextDecoder;for await(const o of v(e))S||(d+=typeof o=="string"?o:r.decode(o,{stream:!0}),d.length>l&&(d=d.slice(0,l),S=!0,y=!0)),yield o})()},events(e){return(async function*(){try{for await(const r of v(e)){if(m<=l){const o=JSON.stringify(r).length;m+=o,m>l?y=!0:b.push(r)}yield r}}finally{c.response={status:200,headers:p(n.headers),raw:d,events:b,truncated:y},h()}})()},reply(e,r,o){c.response={status:e,headers:p(r),body:o},h()},attempt(e,r){c.response=e.headers?{...e,headers:p(e.headers)}:e,r!==void 0&&(c.failover=r),h()}}}s(M,"start");export{l as STREAM_LIMIT,p as redact,M as start,L as valid};
3
+ //# sourceMappingURL=record.mjs.map