@cldmv/llm-translator 0.3.0 → 0.4.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.
package/README.md CHANGED
@@ -17,14 +17,16 @@ 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.3.0 (November 2026)
20
+ ### Latest: v0.4.0 (November 2026)
21
21
 
22
- - **Usage reporting, `route()` and the server `authorize` hook** — every `send()` reply has a `usage` promise (and an `onUsage` callback) with the final token counts; `api.route(body)` says which upstream and model a request would use; `server.start` and `server.plugin` take `authorize(request)` and `onUsage(report, context)` so an app with its own key system can gate and meter requests.
23
- - **More servers and endpoint pools** — drivers for llama.cpp, Ollama, SGLang and the DeepSeek API, failover and load balancing between upstreams that serve the same model, the `reasoningRoundTrip` and `toolSearch` settings, estimated usage when an upstream sends none, and `GET /v1/models/{id}`. Requires Node.js 24 or newer.
24
- - [View full v0.3.0 Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.3.0.md)
22
+ - **Per-model effort levels and pinned aliases** — an `efforts` config lists the reasoning efforts each model honours, unsupported levels snap to the nearest listed one, and an alias can pin `effort` and `thinking`, so one local model can serve Claude Code's tiers at different depths. `/v1/models` lists the aliases and levels.
23
+ - **Also:** non-standard finish reasons map to `max_tokens`, `end_turn` or an error; a configurable tool-error marker with opt-in `is_error` inference; `pingInterval` per upstream; and the Node.js floor drops to 22.15.
24
+ - [View full v0.4.0 Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.4.0.md)
25
25
 
26
26
  ### Recent Releases
27
27
 
28
+ - **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))
29
+ - **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))
28
30
  - **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))
29
31
  - **v0.1.0** (October 2026) — package scaffold: ESM and CommonJS entry points, tsup build, the `llm-translator-dev` condition with devcheck, tests on `@cldmv/vitest-runner` and the CLDMV v4 CI/release pipeline; the only export was a placeholder ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.1.0.md))
30
32
 
@@ -64,8 +66,8 @@ Lines, statements, functions and branches are all at 100% and enforced on every
64
66
 
65
67
  ### Requirements
66
68
 
67
- - Node.js 24 or newer
68
- - Developing the package also needs Node.js 24 or newer (`devEngines` enforces it on install and `npm run`)
69
+ - Node.js 22.15 or newer. Node.js 26 is fastest: PDF.js (through `unpdf`) uses newer built-ins such as `Promise.try`, `Float16Array` and `Map.prototype.getOrInsert`, which Node.js 22 and 24 run through PDF.js' own polyfills. npm warns on Node.js 22 because `unpdf` 2 declares Node.js 24; text extraction and page rendering work on 22.15.
70
+ - Developing the package needs Node.js 22.22 or newer (`devEngines` enforces it on install and `npm run`)
69
71
  - Fastify 5, only for the server and the Fastify plugin (an optional peer dependency)
70
72
 
71
73
  ### Install
@@ -122,7 +124,7 @@ Then point Claude Code at it with `ANTHROPIC_BASE_URL=http://127.0.0.1:8787`. Th
122
124
 
123
125
  ## 📚 Configuration
124
126
 
125
- `createTranslator(config)` takes `front`, `upstreams` (URLs or objects with `url`, `apiKey`, `backend`, `models`, `default` and `settings`), `aliases`, 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` and a custom `transport`. The CLI reads the same keys from flags, `LLM_TRANSLATOR_*` environment variables and a JSON file, in that order of precedence.
127
+ `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` and a custom `transport`. The CLI reads the same keys from flags, `LLM_TRANSLATOR_*` environment variables and a JSON file, in that order of precedence.
126
128
 
127
129
  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).
128
130
 
@@ -1,17 +1,17 @@
1
- var O=Object.defineProperty;var d=(e,t)=>O(e,"name",{value:t,configurable:!0});import{resolve as E}from"../../../../../../../lib/settings.mjs";import{TranslatorError as T}from"../../../../../../../lib/errors.mjs";import{createNameFitter as M}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 fe(e,t,o){const n=E(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 s=M({max:64,map:i.toolNames});for(const c of e.tools??[])(c.type===void 0||c.type==="custom")&&s(c.name);const m=D(e.messages,new Set((e.tools??[]).map(c=>c.name))),l=C(e.tools??[],m,n,e.tool_choice);i.toolSearch={mode:n.toolSearch,loaded:l.loaded,hidden:l.hidden,stubbed:l.stubbed,capped:m.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 r={model:e.model},h=N(e.system,n),k=await R(e.messages,n,i,s,h,U(e.context_management,i),o?.budget??L());r.messages=k.messages,k.prefill&&(r.continue_final_message=!0,r.add_generation_prompt=!1),e.max_tokens!==void 0&&(r[n.maxTokensField]=e.max_tokens),e.temperature!==void 0&&(r.temperature=e.temperature),e.top_p!==void 0&&(r.top_p=e.top_p),e.top_k!==void 0&&(n.topK?r.top_k=e.top_k:g(i,"top_k")),e.stop_sequences?.length>0&&(r.stop=e.stop_sequences),i.stream&&(r.stream=!0,n.streamUsage&&(r.stream_options={include_usage:!0},n.continuousUsage&&(r.stream_options.continuous_usage_stats=!0)));const u=X(l.offer,n,i,s);if(u.length>0){r.tools=u;const c=Y(e.tool_choice,n,i,s);c!==void 0&&(r.tool_choice=c),e.tool_choice?.disable_parallel_tool_use===!0&&n.parallelToolCalls&&(r.parallel_tool_calls=!1)}return Z(e,n,r),b(e,n,i,r),{body:r,state:i}}d(fe,"request");function g(e,t){e.dropped.includes(t)||e.dropped.push(t)}d(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)}d(v,"unsupported");function N(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 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(`
2
2
 
3
- `)}d(N,"textOf");function j(e){return typeof e=="string"?[{type:"text",text:e}]:e??[]}d(j,"asBlocks");async function R(e,t,o,n,i,s,m){const l=[],r=[],h=new Map,k=t.inlineSystem==="auto"?"user":t.inlineSystem,u=new Map;if(t.reasoningRoundTrip){for(let a=e.length-1;a>=0;a--)if(e[a].role==="assistant")for(const f of[...j(e[a].content)].reverse())f.type==="thinking"&&u.set(f,await z(f.signature,{producer:o.producer,budget:m}))}const c=d(a=>{const f=l.at(-1);f?.role==="user"?f.parts.push(...a):l.push({role:"user",parts:a})},"pushUser"),w=d(async(a,f)=>{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:f}))_.push(...await w(p,f));return _}case"search_result":{const _=(a.content??[]).map(p=>p.text).join(`
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
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 f=N(a.content,t);if(f===""||k==="drop")continue;if(k==="keep")l.push({role:"system",content:f});else if(k==="hoist")r.push(f);else{const _=f.trimStart().startsWith("<system-reminder>")?f:`<system-reminder>
6
- ${f}
7
- </system-reminder>`;c([{type:"text",text:_}])}}else if(a.role==="user"){const f=[],_=[];for(const y of j(a.content)){if(y.type!=="tool_result"){f.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 q=S.filter(x=>x.type==="text").map(x=>x.text),F=S.filter(x=>x.type==="image_url");let $=S.some(x=>x.type==="tool_reference")?S.filter(x=>x.type!=="image_url"):q.join(`
8
- `);F.length>0&&(_.push({type:"text",text:`Image output of tool call ${y.tool_use_id}:`},...F),$===""&&($="(image output attached in the next message)")),y.is_error===!0&&t.toolErrors&&($=typeof $=="string"?`[error] ${$}`:[{type:"text",text:"[error]"},...$]),l.push({role:"tool",tool_call_id:y.tool_use_id,content:$})}const p=[..._,...f];p.length>0&&c(p)}else if(a.role==="assistant"){const f={role:"assistant",texts:[],parts:[],tool_calls:[]};for(const p of j(a.content))if(p.type==="text")p.text!==""&&f.texts.push(p.text);else if(p.type==="thinking"){const y=u.get(p)??null;y?f.parts.push({restored:y}):p.thinking&&f.parts.push({text:p.thinking})}else p.type==="redacted_thinking"?g(o,"redacted_thinking"):p.type==="tool_use"?(h.set(p.id,p.input),f.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(...f.texts),_.parts.push(...f.parts),_.tool_calls.push(...f.tool_calls)):l.push(f)}else throw new T(`messages: unsupported role "${a.role}"`,{code:"invalid_request",param:"messages"});return Q(l,t,i,r,s,o)}d(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}d(U,"thinkingTurnsToKeep");function G(e,t){const o=e.find(r=>r.restored&&(r.restored.reasoning_content!==void 0||r.restored.reasoning!==void 0)),n=o?o.restored.reasoning_content!==void 0?"reasoning_content":"reasoning":t;let i="",s,m=null;for(const r of e){const h=r.restored?r.restored.reasoning_content??r.restored.reasoning:r.text;r.restored?.reasoning_details&&(s=[...s??[],...r.restored.reasoning_details]),h!==void 0&&(i+=(m!==null&&!(m.restored&&r.restored)?`
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)?`
9
9
 
10
- `:"")+h,m=r)}const l={};return i!==""&&(l[n]=i),s&&(l.reasoning_details=s),l}d(G,"reasoningFields");function J(e){return!!(e.reasoning_content||e.reasoning||e.reasoning_details)}d(J,"reasoningSent");function Q(e,t,o,n,i,s){let m=i;for(let u=e.length-1;u>=0;u--)e[u].role!=="assistant"||e[u].parts.length===0||(m>0?m--:e[u].parts=[]);const l=[],r=[o,...n].filter(u=>u!=="").join(`
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(`
11
11
 
12
- `);r!==""&&l.push({role:"system",content:r});for(const u of e)if(u.role==="user"){const c=u.parts.every(w=>w.type==="text");l.push({role:"user",content:c?u.parts.map(w=>w.text).join(`
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(`
13
13
 
14
- `):u.parts})}else if(u.role==="assistant"){const c={role:"assistant",content:u.texts.join(`
14
+ `):f.parts})}else if(f.role==="assistant"){const c={role:"assistant",content:f.texts.join(`
15
15
 
16
- `)};u.parts.length>0&&Object.assign(c,G(u.parts,t.reasoningField)),t.reasoningRoundTrip&&u.tool_calls.length>0&&!J(c)&&(s.reasoningMissing=(s.reasoningMissing??0)+1),u.tool_calls.length>0&&(c.tool_calls=u.tool_calls),l.push(c)}else l.push(u);const h=l.at(-1),k=t.prefill==="continue_final_message"&&h?.role==="assistant"&&!h.tool_calls&&h.content!=="";return{messages:l,prefill:k}}d(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.]"}])}d(V,"imageParts");function X(e,t,o,n){const i=[];for(const s of e){if(s.type!==void 0&&s.type!=="custom"){g(o,`tool:${s.name}`);continue}if(s.defer_loading===!0&&t.deferredTools==="drop"){g(o,"deferred_tools");continue}const m=s.input_schema??{type:"object",properties:{}},l={name:n(s.name),parameters:t.toolSchema==="sanitize"?B(m):m};s.description!==void 0&&(l.description=s.description),s.strict!==void 0&&(l.strict=s.strict),i.push(s.defer_loading===!0&&t.toolSearch==="native"?{type:"function",function:l,defer_loading:!0}:{type:"function",function:l})}return i}d(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}}d(Y,"convertToolChoice");function Z(e,t,o){const n=e.thinking,i=n?.type;let s;i==="disabled"?(t.thinking==="reasoning_effort"&&(s="none"),t.thinking==="chat_template_kwargs"&&(o.chat_template_kwargs={enable_thinking:!1})):(t.effort&&e.output_config?.effort!==void 0&&(s=e.output_config.effort),i==="enabled"&&(t.thinking==="chat_template_kwargs"&&(o.chat_template_kwargs={enable_thinking:!0},t.thinkingBudget&&n.budget_tokens!==void 0&&e.max_tokens!==void 0&&(o.thinking_token_budget=Math.max(0,Math.min(n.budget_tokens,e.max_tokens-1)))),t.thinking==="reasoning_effort"&&s===void 0&&(s="medium"))),s!==void 0&&(o.reasoning_effort=s)}d(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 s of Object.keys(e.output_config??{}))s!=="effort"&&s!=="format"&&g(o,`output_config.${s}`)}d(b,"applyOutputConfig");export{fe as request};
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};
17
17
  //# sourceMappingURL=request.mjs.map
@@ -1,6 +1,6 @@
1
- var v=Object.defineProperty;var u=(e,s)=>v(e,"name",{value:s,configurable:!0});import{resolve as T}from"../../../../../../../lib/settings.mjs";import{TranslatorError as x}from"../../../../../../../lib/errors.mjs";import{anthropicToolId as j,createNameFitter as q}from"../../../../../../../lib/ids.mjs";import{parseArguments as A}from"../../../../../../../lib/arguments.mjs";import{sanitize as N}from"../../../../../../../lib/schema.mjs";import{blocksOf as $}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"]),I=new Set(["metadata","store","service_tier"]),h=/^data:([^;,]+);base64,(.*)$/s;async function P(e,s){const p=T(s);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new x("messages is a required property",{code:"invalid_request",param:"messages"});if(e.n!==void 0&&e.n!==1)throw new x("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:p.reasoningField,toolNames:{},dropped:[]},l=q({max:128,map:o.toolNames});for(const t of Object.keys(e))if(!(F.has(t)||I.has(t))){if(p.unknownFields==="error")throw new x(`${t}: Unrecognized request argument`,{code:"unsupported_field",param:t});g(o,t)}const c=[],a=[],m=u((t,r)=>{const i=a.at(-1);i?.role===t?i.blocks.push(...r):a.push({role:t,blocks:[...r]})},"push");for(const t of e.messages)switch(t.role){case"system":case"developer":{const r=w(t.content);r!==""&&c.push(r);break}case"user":m("user",z(t.content,o));break;case"assistant":{const r=p.reasoningRoundTrip?$(t.reasoning_details):[],i=[],f=w(t.content);f!==""?i.push({type:"text",text:f}):typeof t.refusal=="string"&&t.refusal!==""&&i.push({type:"text",text:t.refusal}),S(t)&&!r.some(d=>d.block.type==="thinking")&&g(o,"reasoning");for(const d of t.tool_calls??[])i.push({type:"tool_use",id:j(d.id),name:l(d.function.name),input:A(d.function.arguments)});const k=M(r,i);k.length>0&&m("assistant",k);break}case"tool":{const r={type:"tool_result",tool_use_id:j(t.tool_call_id),content:w(t.content)},i=a.at(-1);if(i?.role==="user"){const f=i.blocks.findIndex(k=>k.type!=="tool_result");i.blocks.splice(f===-1?i.blocks.length:f,0,r)}else a.push({role:"user",blocks:[r]});break}default:throw new x(`messages: unsupported role "${t.role}"`,{code:"invalid_request",param:"messages"})}const n={model:e.model,max_tokens:e.max_completion_tokens??e.max_tokens??p.defaultMaxTokens};c.length>0&&(n.system=c.join(`
1
+ var v=Object.defineProperty;var f=(e,s)=>v(e,"name",{value:s,configurable:!0});import{resolve as T}from"../../../../../../../lib/settings.mjs";import{TranslatorError as x}from"../../../../../../../lib/errors.mjs";import{anthropicToolId as j,createNameFitter as q}from"../../../../../../../lib/ids.mjs";import{parseArguments as A}from"../../../../../../../lib/arguments.mjs";import{sanitize as E}from"../../../../../../../lib/schema.mjs";import{blocksOf as I}from"../../../../../../../lib/thinking.mjs";const M=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"]),N=new Set(["metadata","store","service_tier"]),h=/^data:([^;,]+);base64,(.*)$/s;async function P(e,s){const l=T(s);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new x("messages is a required property",{code:"invalid_request",param:"messages"});if(e.n!==void 0&&e.n!==1)throw new x("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:l.reasoningField,toolNames:{},dropped:[]},a=q({max:128,map:o.toolNames});for(const t of Object.keys(e))if(!(M.has(t)||N.has(t))){if(l.unknownFields==="error")throw new x(`${t}: Unrecognized request argument`,{code:"unsupported_field",param:t});g(o,t)}const c=[],p=[],m=f((t,n)=>{const i=p.at(-1);i?.role===t?i.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=w(t.content);n!==""&&c.push(n);break}case"user":m("user",S(t.content,o));break;case"assistant":{const n=l.reasoningRoundTrip?I(t.reasoning_details):[],i=[],u=w(t.content);u!==""?i.push({type:"text",text:u}):typeof t.refusal=="string"&&t.refusal!==""&&i.push({type:"text",text:t.refusal}),F(t)&&!n.some(d=>d.block.type==="thinking")&&g(o,"reasoning");for(const d of t.tool_calls??[])i.push({type:"tool_use",id:j(d.id),name:a(d.function.name),input:A(d.function.arguments)});const k=$(n,i);k.length>0&&m("assistant",k);break}case"tool":{const n={type:"tool_result",tool_use_id:j(t.tool_call_id),content:w(t.content)};if(l.toolErrorInference){const u=l.toolErrorMarker.trimEnd();n.content.startsWith(u)&&(n.is_error=!0,n.content=n.content.slice(u.length).replace(/^\s/,""))}const i=p.at(-1);if(i?.role==="user"){const u=i.blocks.findIndex(k=>k.type!=="tool_result");i.blocks.splice(u===-1?i.blocks.length:u,0,n)}else p.push({role:"user",blocks:[n]});break}default:throw new x(`messages: unsupported role "${t.role}"`,{code:"invalid_request",param:"messages"})}const r={model:e.model,max_tokens:e.max_completion_tokens??e.max_tokens??l.defaultMaxTokens};c.length>0&&(r.system=c.join(`
2
2
 
3
- `)),n.messages=a.map(t=>({role:t.role,content:t.blocks})),e.temperature!==void 0&&(n.temperature=Math.min(1,e.temperature)),e.top_p!==void 0&&(n.top_p=e.top_p),e.top_k!==void 0&&(n.top_k=e.top_k),e.stop!==void 0&&e.stop!==null&&(n.stop_sequences=[].concat(e.stop)),o.stream&&(n.stream=!0),typeof e.user=="string"&&(n.metadata={user_id:e.user});const b=[];for(const t of e.tools??[]){if(t.type!=="function"){g(o,`tool:${t.type}`);continue}const r=t.function,i=r.parameters??{type:"object",properties:{}},f={name:l(r.name),input_schema:p.toolSchema==="sanitize"?N(i):i};r.description!==void 0&&(f.description=r.description),b.push(f)}if(b.length>0){n.tools=b;let t=D(e.tool_choice,l);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)&&(n.tool_choice=t)}const _=e.reasoning_effort;_==="none"?n.thinking={type:"disabled"}:_!=null&&(n.thinking={type:"adaptive"},n.output_config={effort:_==="minimal"?"low":_});const y=e.response_format;return y?.type==="json_schema"?n.output_config={...n.output_config,format:{type:"json_schema",schema:y.json_schema.schema}}:y?.type==="json_object"&&g(o,"response_format"),{body:n,state:o}}u(P,"request");function g(e,s){e.dropped.includes(s)||e.dropped.push(s)}u(g,"drop");function M(e,s){const p=[...e].sort((a,m)=>a.at-m.at),o=[];let l=0;const c=s.findIndex(a=>a.type==="tool_use");for(const a of p){const m=a.at<=1&&c!==-1?Math.min(a.at,c):a.at;for(;l<Math.min(m,s.length);)o.push(s[l++]);o.push(a.block)}return[...o,...s.slice(l)]}u(M,"interleave");function S(e){return!!(e.reasoning||e.reasoning_content)}u(S,"reasoningPresent");function w(e){return typeof e=="string"?e:(e??[]).filter(s=>s.type==="text").map(s=>s.text).join(`
3
+ `)),r.messages=p.map(t=>({role:t.role,content:t.blocks})),e.temperature!==void 0&&(r.temperature=Math.min(1,e.temperature)),e.top_p!==void 0&&(r.top_p=e.top_p),e.top_k!==void 0&&(r.top_k=e.top_k),e.stop!==void 0&&e.stop!==null&&(r.stop_sequences=[].concat(e.stop)),o.stream&&(r.stream=!0),typeof e.user=="string"&&(r.metadata={user_id:e.user});const b=[];for(const t of e.tools??[]){if(t.type!=="function"){g(o,`tool:${t.type}`);continue}const n=t.function,i=n.parameters??{type:"object",properties:{}},u={name:a(n.name),input_schema:l.toolSchema==="sanitize"?E(i):i};n.description!==void 0&&(u.description=n.description),b.push(u)}if(b.length>0){r.tools=b;let t=z(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)&&(r.tool_choice=t)}const _=e.reasoning_effort;_==="none"?r.thinking={type:"disabled"}:_!=null&&(r.thinking={type:"adaptive"},r.output_config={effort:_==="minimal"?"low":_});const y=e.response_format;return y?.type==="json_schema"?r.output_config={...r.output_config,format:{type:"json_schema",schema:y.json_schema.schema}}:y?.type==="json_object"&&g(o,"response_format"),{body:r,state:o}}f(P,"request");function g(e,s){e.dropped.includes(s)||e.dropped.push(s)}f(g,"drop");function $(e,s){const l=[...e].sort((p,m)=>p.at-m.at),o=[];let a=0;const c=s.findIndex(p=>p.type==="tool_use");for(const p of l){const m=p.at<=1&&c!==-1?Math.min(p.at,c):p.at;for(;a<Math.min(m,s.length);)o.push(s[a++]);o.push(p.block)}return[...o,...s.slice(a)]}f($,"interleave");function F(e){return!!(e.reasoning||e.reasoning_content)}f(F,"reasoningPresent");function w(e){return typeof e=="string"?e:(e??[]).filter(s=>s.type==="text").map(s=>s.text).join(`
4
4
 
5
- `)}u(w,"plainText");function z(e,s){if(typeof e=="string")return e===""?[]:[{type:"text",text:e}];const p=[];for(const o of e??[])if(o.type==="text")p.push({type:"text",text:o.text});else if(o.type==="image_url"){const l=h.exec(o.image_url.url);p.push({type:"image",source:l?{type:"base64",media_type:l[1],data:l[2]}:{type:"url",url:o.image_url.url}})}else if(o.type==="file"&&typeof o.file?.file_data=="string"&&h.test(o.file.file_data)){const l=h.exec(o.file.file_data);p.push({type:"document",source:{type:"base64",media_type:l[1],data:l[2]}})}else g(s,`content:${o.type}`);return p}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{P as request};
5
+ `)}f(w,"plainText");function S(e,s){if(typeof e=="string")return e===""?[]:[{type:"text",text:e}];const l=[];for(const o of e??[])if(o.type==="text")l.push({type:"text",text:o.text});else if(o.type==="image_url"){const a=h.exec(o.image_url.url);l.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"&&h.test(o.file.file_data)){const a=h.exec(o.file.file_data);l.push({type:"document",source:{type:"base64",media_type:a[1],data:a[2]}})}else g(s,`content:${o.type}`);return l}f(S,"userBlocks");function z(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"}}f(z,"toolChoice");export{P as request};
6
6
  //# sourceMappingURL=request.mjs.map
@@ -1,2 +1,2 @@
1
- var d=Object.defineProperty;var c=(s,n)=>d(s,"name",{value:n,configurable:!0});import{resolve as h}from"../../../../../../../lib/settings.mjs";import{TranslatorError as y}from"../../../../../../../lib/errors.mjs";import{messageId as _,anthropicToolId as k,restoreName as T}from"../../../../../../../lib/ids.mjs";import{sign as x,reasoningOf as A,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{toAnthropicStop as v,toAnthropicUsage as w}from"../../../../../../../lib/finish.mjs";function z(s,n,m){const p=h(m),g=s?.choices?.[0];if(!g)throw new y("Upstream response contained no choices",{code:"invalid_upstream_response",status:502});const e=g.message??{},r=[];let a=A(e),t=typeof e.content=="string"?e.content:"";const o={};if(p.reasoningRoundTrip){const i=C(e);i&&(o[i]=e[i]),Array.isArray(e.reasoning_details)&&e.reasoning_details.length>0&&(o.reasoning_details=e.reasoning_details)}a===""&&p.thinkTags&&({reasoning:a,text:t}=O(t,p.thinkOpen)),a===""&&o.reasoning_details&&(a=N(o.reasoning_details)),(a!==""||o.reasoning_details)&&(r.push({type:"thinking",thinking:a,signature:x(Object.keys(o).length>0?o:void 0,n?.producer)}),t=t.trimStart()),t===""&&typeof e.refusal=="string"&&(t=e.refusal),t!==""&&r.push({type:"text",text:t});const f=n?.toolNames??{},l=e.tool_calls??[];for(const i of l)r.push({type:"tool_use",id:k(i.id),name:T(f,i.function.name),input:U(i.function.arguments)});r.length===0&&r.push({type:"text",text:""});const u=v(g.finish_reason,{hasToolCalls:l.length>0,matchedStop:g.stop_reason,argumentsComplete:l.length===0||S(l.at(-1).function.arguments)});return n&&s.usage&&(n.upstreamUsage=s.usage),{id:_(),type:"message",role:"assistant",model:n?.model??s.model,content:r,...u,usage:w(s.usage)}}c(z,"response");export{z as response};
1
+ var y=Object.defineProperty;var f=(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 c=_(u),g=t?.choices?.[0];if(!g)throw new m("Upstream response contained no choices",{code:"invalid_upstream_response",status:502});const l=v(g.finish_reason);if(l)throw new m(l.message,{code:l.code,status:l.status,type:l.type});const e=g.message??{},r=[];let a=w(e),s=typeof e.content=="string"?e.content:"";const o={};if(c.reasoningRoundTrip){const i=C(e);i&&(o[i]=e[i]),Array.isArray(e.reasoning_details)&&e.reasoning_details.length>0&&(o.reasoning_details=e.reasoning_details)}a===""&&c.thinkTags&&({reasoning:a,text:s}=O(s,c.thinkOpen)),a===""&&o.reasoning_details&&(a=N(o.reasoning_details)),(a!==""||o.reasoning_details)&&(r.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!==""&&r.push({type:"text",text:s});const h=n?.toolNames??{},p=e.tool_calls??[];for(const i of p)r.push({type:"tool_use",id:T(i.id),name:x(h,i.function.name),input:U(i.function.arguments)});r.length===0&&r.push({type:"text",text:""});const d=E(g.finish_reason,{hasToolCalls:p.length>0,matchedStop:g.stop_reason,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:r,...d,usage:I(t.usage)}}f(D,"response");export{D as response};
2
2
  //# sourceMappingURL=response.mjs.map
@@ -1,2 +1,2 @@
1
- var M=Object.defineProperty;var s=(h,r)=>M(h,"name",{value:r,configurable:!0});import{resolve as G}from"../../../../../../../lib/settings.mjs";import{TranslatorError as H,openaiToAnthropicType as L,rewriteContextOverflow as P}from"../../../../../../../lib/errors.mjs";import{isComplete as Q}from"../../../../../../../lib/arguments.mjs";import{messageId as V,anthropicToolId as W,restoreName as X}from"../../../../../../../lib/ids.mjs";import{sign as Y,reasoningOf as Z,reasoningKey as $,detailsText as ee,mergeDetail as te,createThinkSplitter as ne}from"../../../../../../../lib/thinking.mjs";import{toAnthropicStop as ie,toAnthropicUsage as z}from"../../../../../../../lib/finish.mjs";import{messages as oe}from"../../../../../../../lib/sse.mjs";async function*pe(h,r,D){const u=G(D),E=r?.toolNames??{},a=u.thinkTags?ne(u.thinkOpen):null,c=new Map;let _=!1,x=0,n=null,w=null,b=null,S=null,N=!1,O=0,j=!1,v=!1,A=!1;const T=[],l=s((e,t)=>({event:e,data:{type:e,...t}}),"event"),m=s((e,t)=>l("error",{error:{type:e,message:t}}),"fail"),q=s(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*p(){if(n!==null){n.kind==="thinking"?yield l("content_block_delta",{index:n.index,delta:{type:"signature_delta",signature:Y(q(n.carry),r?.producer)}}):n.kind==="tool"&&(n.entry.state="closed",n.entry.sent||(yield l("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:"{}"}}))),yield l("content_block_stop",{index:n.index}),n=null;for(const e of c.values()){if(e.state!=="pending")continue;e.state="closed";const t=x++,i=e.args.join("");yield l("content_block_start",{index:t,content_block:e.block}),yield l("content_block_delta",{index:t,delta:{type:"input_json_delta",partial_json:i===""?"{}":i}}),yield l("content_block_stop",{index:t})}}}s(p,"close");function*B(){for(const e of C()){if(e.state!=="buffered")continue;yield*y("tool",e.block,e);const t=e.args.join("");t!==""&&(e.sent=!0,yield l("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:t}})),yield*p()}}s(B,"flushBuffered");function C(){return[...c.entries()].sort((e,t)=>e[0]-t[0]).map(e=>e[1])}s(C,"ordered");function*y(e,t,i){yield*p(),n={kind:e,index:x++,entry:i,carry:{key:null,text:"",details:[]}},yield l("content_block_start",{index:n.index,content_block:t})}s(y,"begin");function*d(e,t,i=null){if(e==="text"&&A&&(t=t.trimStart()),t==="")return;if(A=e==="thinking",n?.kind==="tool"){T.push({kind:e,text:t,key:i});return}n?.kind!==e&&(yield*y(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 l("content_block_delta",{index:n.index,delta:o})}s(d,"piece");function*U(e){if(n?.kind==="tool"){T.push({kind:"details",items:e});return}n?.kind!=="thinking"&&(yield*y("thinking",{type:"thinking",thinking:"",signature:""}));for(const t of e)te(n.carry.details,t);n.carry.key===null&&(yield*d("thinking",ee(e)))}s(U,"details");function*I(e){yield*d("thinking",e.reasoning),yield*d("text",e.text)}s(I,"pieces");function*F(e){let t=e.index;t===void 0&&(t=e.id===void 0?O:c.size),O=t;let i=c.get(t);if(i===void 0){const k=e.function?.name??"",g={type:"tool_use",id:W(e.id),name:X(E,k),input:{}};a?.pending()&&(v=!0);const f=u.toolStreaming==="buffer"||v;i={state:f?"buffered":"pending",sent:!1,args:[],block:g},c.set(t,i),!f&&n?.kind!=="tool"&&(i.state="open",yield*y("tool",g,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 l("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:o}})))}s(F,"toolCall");try{for await(const e of oe(h)){if(e.data==="[DONE]"){N=!0;break}const t=e.data;if(t.error){const f=t.error.message??"Upstream error";yield m(L(t.error),P(f)??f);return}_||(_=!0,yield l("message_start",{message:{id:V(),type:"message",role:"assistant",model:r?.model??t.model,content:[],stop_reason:null,stop_sequence:null,usage:z(t.usage)}})),t.usage&&(w=t.usage,r&&(r.upstreamUsage=t.usage));const i=t.choices?.[0];if(!i)continue;const o=i.delta??{},k=Z(o);let g="";k!==""&&!j&&(j=!0,a&&(g=a.release().text)),yield*d("thinking",k,$(o)),yield*d("text",g),u.reasoningRoundTrip&&Array.isArray(o.reasoning_details)&&o.reasoning_details.length>0&&(yield*U(o.reasoning_details)),typeof o.content=="string"&&o.content!==""&&(a&&!j?yield*I(a.push(o.content)):yield*d("text",o.content));for(const f of o.tool_calls??[])yield*F(f);i.finish_reason&&(b=i.finish_reason),typeof i.stop_reason=="string"&&(S=i.stop_reason)}}catch(e){yield e instanceof H?m(e.type,e.message):m("api_error",e.message);return}if(!_||b===null&&!N){yield m("api_error",_?"Upstream stream ended before completion":"Upstream stream ended without data");return}a&&(yield*I(a.end())),yield*B();const R=C().at(-1),J=R===void 0||Q(R.args.join(""));yield*p();for(const e of T)yield*e.kind==="details"?U(e.items):d(e.kind,e.text,e.key);yield*p(),x===0&&(yield*y("text",{type:"text",text:""}),yield*p());const K=ie(b,{hasToolCalls:c.size>0,matchedStop:S,argumentsComplete:J});yield l("message_delta",{delta:K,usage:z(w)}),yield l("message_stop",{})}s(pe,"stream");export{pe as stream};
1
+ var G=Object.defineProperty;var l=(x,r)=>G(x,"name",{value:r,configurable:!0});import{resolve as H}from"../../../../../../../lib/settings.mjs";import{TranslatorError as L,openaiToAnthropicType as P,rewriteContextOverflow as Q}from"../../../../../../../lib/errors.mjs";import{isComplete as V}from"../../../../../../../lib/arguments.mjs";import{messageId as W,anthropicToolId as X,restoreName as Y}from"../../../../../../../lib/ids.mjs";import{sign as Z,reasoningOf as $,reasoningKey as ee,detailsText as te,mergeDetail as ne,createThinkSplitter as ie}from"../../../../../../../lib/thinking.mjs";import{finishError as oe,toAnthropicStop as se,toAnthropicUsage as z}from"../../../../../../../lib/finish.mjs";import{messages as le}from"../../../../../../../lib/sse.mjs";async function*ge(x,r,D){const u=H(D),q=r?.toolNames??{},a=u.thinkTags?ie(u.thinkOpen):null,c=new Map;let _=!1,b=0,n=null,S=null,k=null,N=null,O=!1,v=0,j=!1,A=!1,C=!1;const T=[],s=l((e,t)=>({event:e,data:{type:e,...t}}),"event"),p=l((e,t)=>s("error",{error:{type:e,message:t}}),"fail"),B=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:Z(B(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=b++,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*F(){for(const e of U()){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(F,"flushBuffered");function U(){return[...c.entries()].sort((e,t)=>e[0]-t[0]).map(e=>e[1])}l(U,"ordered");function*g(e,t,i){yield*y(),n={kind:e,index:b++,entry:i,carry:{key:null,text:"",details:[]}},yield s("content_block_start",{index:n.index,content_block:t})}l(g,"begin");function*d(e,t,i=null){if(e==="text"&&C&&(t=t.trimStart()),t==="")return;if(C=e==="thinking",n?.kind==="tool"){T.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(d,"piece");function*E(e){if(n?.kind==="tool"){T.push({kind:"details",items:e});return}n?.kind!=="thinking"&&(yield*g("thinking",{type:"thinking",thinking:"",signature:""}));for(const t of e)ne(n.carry.details,t);n.carry.key===null&&(yield*d("thinking",te(e)))}l(E,"details");function*I(e){yield*d("thinking",e.reasoning),yield*d("text",e.text)}l(I,"pieces");function*J(e){let t=e.index;t===void 0&&(t=e.id===void 0?v:c.size),v=t;let i=c.get(t);if(i===void 0){const h=e.function?.name??"",m={type:"tool_use",id:X(e.id),name:Y(q,h),input:{}};a?.pending()&&(A=!0);const f=u.toolStreaming==="buffer"||A;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(J,"toolCall");try{for await(const e of le(x)){if(e.data==="[DONE]"){O=!0;break}const t=e.data;if(t.error){const f=t.error.message??"Upstream error";yield p(P(t.error),Q(f)??f);return}_||(_=!0,yield s("message_start",{message:{id:W(),type:"message",role:"assistant",model:r?.model??t.model,content:[],stop_reason:null,stop_sequence:null,usage:z(t.usage)}})),t.usage&&(S=t.usage,r&&(r.upstreamUsage=t.usage));const i=t.choices?.[0];if(!i)continue;const o=i.delta??{},h=$(o);let m="";h!==""&&!j&&(j=!0,a&&(m=a.release().text)),yield*d("thinking",h,ee(o)),yield*d("text",m),u.reasoningRoundTrip&&Array.isArray(o.reasoning_details)&&o.reasoning_details.length>0&&(yield*E(o.reasoning_details)),typeof o.content=="string"&&o.content!==""&&(a&&!j?yield*I(a.push(o.content)):yield*d("text",o.content));for(const f of o.tool_calls??[])yield*J(f);i.finish_reason&&(k=i.finish_reason),typeof i.stop_reason=="string"&&(N=i.stop_reason)}}catch(e){yield e instanceof L?p(e.type,e.message):p("api_error",e.message);return}if(!_||k===null&&!O){yield p("api_error",_?"Upstream stream ended before completion":"Upstream stream ended without data");return}const w=oe(k);if(w){yield p(w.type,w.message);return}a&&(yield*I(a.end())),yield*F();const R=U().at(-1),K=R===void 0||V(R.args.join(""));yield*y();for(const e of T)yield*e.kind==="details"?E(e.items):d(e.kind,e.text,e.key);yield*y(),b===0&&(yield*g("text",{type:"text",text:""}),yield*y());const M=se(k,{hasToolCalls:c.size>0,matchedStop:N,argumentsComplete:K});yield s("message_delta",{delta:M,usage:z(S)}),yield s("message_stop",{})}l(ge,"stream");export{ge as stream};
2
2
  //# sourceMappingURL=stream.mjs.map
@@ -1,2 +1,2 @@
1
- var n=Object.defineProperty;var e=(t,o)=>n(t,"name",{value:o,configurable:!0});import{noEmptyTools as s}from"../../../lib/normalize.mjs";const f=new Set(["xhigh","max"]);function u(t,o){const r=s(t);return f.has(r.reasoning_effort)&&o?.settings?.effortPassthrough!==!0?{...r,reasoning_effort:"high"}:r}e(u,"request");export{u as request};
1
+ var r=Object.defineProperty;var n=(t,e)=>r(t,"name",{value:e,configurable:!0});import{noEmptyTools as s}from"../../../lib/normalize.mjs";const f=new Set(["xhigh","max"]);function h(t,e){const o=s(t);return f.has(o.reasoning_effort)&&e?.levels===void 0&&e?.settings?.effortPassthrough!==!0?{...o,reasoning_effort:"high"}:o}n(h,"request");export{h as request};
2
2
  //# sourceMappingURL=request.mjs.map
@@ -1,2 +1,2 @@
1
- var c=Object.defineProperty;var d=(a,t)=>c(a,"name",{value:t,configurable:!0});import{context as r,self as f}from"../../lib/runtime.mjs";import{capture as _}from"../../lib/capture.mjs";async function w(a={}){const t=_(f,r,a.front),s=new Map;await Promise.all(t.state.endpoints.map(async e=>{const o=t.driver(e),m=(o.list?await o.list({endpoint:e,transport:t.state.transport,signal:a.signal,timeout:t.config.detectTimeout}):null)??(e.models??e.facts?.models??[]).map(l=>({id:l}));for(const l of m)s.set(l.id,{...l,upstream:e.name})}));for(const e of Object.keys(t.config.aliases)){const o=s.get(t.config.aliases[e]);s.set(e,{...o,id:e})}const n=[...s.values()];if(t.front==="openai")return{object:"list",data:n.map(e=>({id:e.id,object:"model",created:e.created??0,owned_by:e.owned_by??e.upstream??"llm-translator",...e.max_model_len===void 0?{}:{max_model_len:e.max_model_len}}))};const i=n.map(e=>({type:"model",id:e.id,display_name:e.id,created_at:new Date((e.created??0)*1e3).toISOString(),...e.max_model_len===void 0?{}:{max_input_tokens:e.max_model_len}}));return{data:i,has_more:!1,first_id:i[0]?.id??null,last_id:i.at(-1)?.id??null}}d(w,"list");export{w as list};
1
+ var m=Object.defineProperty;var n=(t,i)=>m(t,"name",{value:i,configurable:!0});import{context as c,self as u}from"../../lib/runtime.mjs";import{capture as _}from"../../lib/capture.mjs";import{defaultOf as p,nearest as g}from"../../lib/effort.mjs";async function b(t={}){const i=_(u,c,t.front),o=new Map;await Promise.all(i.state.endpoints.map(async e=>{const s=i.driver(e),r=(s.list?await s.list({endpoint:e,transport:i.state.transport,signal:t.signal,timeout:i.config.detectTimeout}):null)??(e.models??e.facts?.models??[]).map(l=>({id:l}));for(const l of r)o.set(l.id,{...l,upstream:e.name})}));for(const e of Object.keys(i.config.aliases)){const s=o.get(i.config.aliases[e]);o.set(e,{...s,id:e,alias_of:i.config.aliases[e]})}const f=[...o.values()].map(e=>({...e,...v(i.config,e)}));if(i.front==="openai")return{object:"list",data:f.map(e=>({id:e.id,object:"model",created:e.created??0,owned_by:e.owned_by??e.upstream??"llm-translator",...e.max_model_len===void 0?{}:{max_model_len:e.max_model_len},...d(e)}))};const a=f.map(e=>({type:"model",id:e.id,display_name:e.id,created_at:new Date((e.created??0)*1e3).toISOString(),...e.max_model_len===void 0?{}:{max_input_tokens:e.max_model_len},...d(e)}));return{data:a,has_more:!1,first_id:a[0]?.id??null,last_id:a.at(-1)?.id??null}}n(b,"list");function v(t,i){const o=i.alias_of??i.id,f=Object.hasOwn(t.efforts,o)?t.efforts[o]:void 0,a=i.alias_of!==void 0&&Object.hasOwn(t.pins,i.id)?t.pins[i.id]:{},e={};return a.thinking!==void 0&&(e.thinking=a.thinking),a.thinking===!1||(a.effort!==void 0?(e.effort=f?g(f.levels,a.effort):a.effort,e.levels=[e.effort],e.default_effort=e.effort):f&&(e.levels=f.levels,e.default_effort=p(f))),e}n(v,"levelsOf");function d(t){return{...t.alias_of===void 0?{}:{alias_of:t.alias_of},...t.levels===void 0?{}:{efforts:t.levels,default_effort:t.default_effort},...t.effort===void 0?{}:{effort:t.effort},...t.thinking===void 0?{}:{thinking:t.thinking}}}n(d,"extras");export{b as list};
2
2
  //# sourceMappingURL=list.mjs.map
@@ -1,3 +1,3 @@
1
- var U=Object.defineProperty;var m=(e,a)=>U(e,"name",{value:a,configurable:!0});import{context as _,self as j}from"../lib/runtime.mjs";import{capture as q}from"../lib/capture.mjs";import{resolve as F}from"../lib/route.mjs";import*as w from"../lib/http.mjs";import{filterChatBody as S}from"../lib/passthrough.mjs";import*as k from"../lib/images.mjs";import*as $ from"../lib/tools.mjs";import{LIMITS as T}from"../lib/toolsearch.mjs";import{warn as u}from"../lib/warn.mjs";function L(e){return`${e.driver}:${e.protocol}:${URL.canParse(e.url)?new URL(e.url).host:e.url}`}m(L,"producerOf");async function H(e,a={}){const o=q(j,_,a.front),n=F(o,e,{endpoint:a.endpoint,advance:a.hold===!0&&a.endpoint===void 0});if(a.hold!==!0)return y(o,e,a,n);n.endpoint.health.inflight++;try{return await y(o,e,a,n)}catch(t){throw n.endpoint.health.inflight--,t}}m(H,"request");async function y(e,a,o,n){const{endpoint:t,model:h,unserved:b,convert:i,pool:x}=n,s=e.driver(t),d=`${t.name}
2
- ${h}`,g=o.images===!1||e.state.textOnly.has(d)&&e.settings(t).imageFallback,l=g?{...e.settings(t),images:!1}:e.settings(t),p=a.stream===!0;let r,f;if(i?({body:r,state:f}=await i.request(a,l,{producer:L(t),budget:o.budget}),f.toolSearch?.capped&&u(e.config,e.state,`Tool search: more than ${T.definitions} tool definitions came back in ToolSearch results for ${t.name}; the rest are not offered.`)):r=e.front==="openai"&&(o.filter??l.passthroughFilter===!0)?S(a,s.info.fields):{...a},o.usage===!0&&p&&e.front==="openai"){if(i)f.includeUsage=!0;else if(l.streamUsage){const c=r.stream_options&&typeof r.stream_options=="object"?r.stream_options:{};r.stream_options={...c,include_usage:!0}}}g&&!i&&e.front==="openai"&&(r=k.strip(r)),o.tools===!1&&(r=$.strip(r)),r.model=h,f?.reasoningMissing&&u(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.`),s.request&&(r=await s.request(r,{endpoint:t,settings:l,state:f,warn:m(c=>u(e.config,e.state,c),"warn")}));const v=t.protocol==="openai.chat"?s.info.paths.chat:s.info.paths.messages;return{upstream:t.name,pool:x.map(c=>c.name),method:"POST",url:w.join(t.url,v),path:v,headers:{"content-type":"application/json",accept:p?"text/event-stream":"application/json",...s.info.headers,...i===null&&t.protocol==="anthropic.messages"?o.clientHeaders:void 0,...w.headers(t,s.info.auth,o.headers)},body:r,state:{v:1,upstream:t.name,front:e.front,native:i===null,model:a.model,stream:p,unserved:b,convert:f},images:{sent:k.carries(r),retry:l.imageFallback&&t.protocol==="openai.chat",key:d},tools:{sent:$.carries(r),retry:l.toolFallback&&t.protocol==="openai.chat"}}}m(y,"prepare");export{H as request};
1
+ var _=Object.defineProperty;var p=(e,r)=>_(e,"name",{value:r,configurable:!0});import{context as b,self as q}from"../lib/runtime.mjs";import{capture as F}from"../lib/capture.mjs";import{resolve as S}from"../lib/route.mjs";import*as $ from"../lib/http.mjs";import{filterChatBody as T}from"../lib/passthrough.mjs";import*as j from"../lib/images.mjs";import*as x from"../lib/tools.mjs";import{LIMITS as L}from"../lib/toolsearch.mjs";import{warn as h}from"../lib/warn.mjs";import{fit as B,pinBody as I}from"../lib/effort.mjs";function M(e){return`${e.driver}:${e.protocol}:${URL.canParse(e.url)?new URL(e.url).host:e.url}`}p(M,"producerOf");async function G(e,r={}){const a=F(q,b,r.front),i=S(a,e,{endpoint:r.endpoint,advance:r.hold===!0&&r.endpoint===void 0});if(r.hold!==!0)return y(a,e,r,i);i.endpoint.health.inflight++;try{return await y(a,e,r,i)}catch(t){throw i.endpoint.health.inflight--,t}}p(G,"request");async function y(e,r,a,i){const{endpoint:t,model:m,unserved:O,convert:f,pool:U,pin:d}=i,u=Object.hasOwn(e.config.efforts,m)?e.config.efforts[m]:void 0;d&&(r=I(r,d,e.front));const n=e.driver(t),v=`${t.name}
2
+ ${m}`,w=a.images===!1||e.state.textOnly.has(v)&&e.settings(t).imageFallback,l={...w?{...e.settings(t),images:!1}:e.settings(t),...d?.effort===void 0?{}:{effort:!0}},g=r.stream===!0;let o,c;if(f?({body:o,state:c}=await f.request(r,l,{producer:M(t),budget:a.budget,listed:u!==void 0}),c.toolSearch?.capped&&h(e.config,e.state,`Tool search: more than ${L.definitions} tool definitions came back in ToolSearch results for ${t.name}; the rest are not offered.`)):o=e.front==="openai"&&(a.filter??l.passthroughFilter===!0)?T(r,n.info.fields):{...r},a.usage===!0&&g&&e.front==="openai"){if(f)c.includeUsage=!0;else if(l.streamUsage){const s=o.stream_options&&typeof o.stream_options=="object"?o.stream_options:{};o.stream_options={...s,include_usage:!0}}}w&&!f&&e.front==="openai"&&(o=j.strip(o)),a.tools===!1&&(o=x.strip(o)),o.model=m,u&&t.protocol==="openai.chat"&&B(o,u,m,s=>h(e.config,e.state,s)),c?.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.`),n.request&&(o=await n.request(o,{endpoint:t,settings:l,levels:u,state:c,warn:p(s=>h(e.config,e.state,s),"warn")}));const k=t.protocol==="openai.chat"?n.info.paths.chat:n.info.paths.messages;return{upstream:t.name,pool:U.map(s=>s.name),method:"POST",url:$.join(t.url,k),path:k,headers:{"content-type":"application/json",accept:g?"text/event-stream":"application/json",...n.info.headers,...f===null&&t.protocol==="anthropic.messages"?a.clientHeaders:void 0,...$.headers(t,n.info.auth,a.headers)},body:o,state:{v:1,upstream:t.name,front:e.front,native:f===null,model:r.model,stream:g,unserved:O,convert:c},images:{sent:j.carries(o),retry:l.imageFallback&&t.protocol==="openai.chat",key:v},tools:{sent:x.carries(o),retry:l.toolFallback&&t.protocol==="openai.chat"}}}p(y,"prepare");export{G as request};
3
3
  //# sourceMappingURL=request.mjs.map
@@ -1,2 +1,2 @@
1
- var d=Object.defineProperty;var f=(e,t)=>d(e,"name",{value:t,configurable:!0});import{context as g,self as m}from"../../lib/runtime.mjs";import{TranslatorError as s}from"../../lib/errors.mjs";import{register as h}from"../../lib/routes.mjs";import{resolve as p}from"../../lib/settings.mjs";async function _(e,t={}){const{config:r}=g;if(t.front!==void 0&&t.front!=="anthropic"&&t.front!=="openai")throw new s(`front must be "anthropic" or "openai" (got ${JSON.stringify(t.front)})`,{code:"invalid_config",status:500});for(const n of["authorize","onUsage"])if(t[n]!==void 0&&typeof t[n]!="function")throw new s(`${n} must be a function`,{code:"invalid_config",status:500});const{send:a,route:c,token:u,model:o,error:l,sse:i}=m;h(e,t,{front:t.front??r.front,filter:r.settings.passthroughFilter!==!1,interval:p(r.settings).pingInterval,send:a,route:c,count:u.count,list:o.list,get:o.get,build:l.build,encode:i.encode,write:i.write})}f(_,"plugin");export{_ as plugin};
1
+ var h=Object.defineProperty;var o=(e,t)=>h(e,"name",{value:t,configurable:!0});import{context as a,self as u}from"../../lib/runtime.mjs";import{TranslatorError as l}from"../../lib/errors.mjs";import{register as v}from"../../lib/routes.mjs";import{capture as w}from"../../lib/capture.mjs";import{resolve as b}from"../../lib/route.mjs";import{resolve as y}from"../../lib/settings.mjs";async function E(e,t={}){const{config:n}=a;if(t.front!==void 0&&t.front!=="anthropic"&&t.front!=="openai")throw new l(`front must be "anthropic" or "openai" (got ${JSON.stringify(t.front)})`,{code:"invalid_config",status:500});for(const r of["authorize","onUsage"])if(t[r]!==void 0&&typeof t[r]!="function")throw new l(`${r} must be a function`,{code:"invalid_config",status:500});const{send:d,route:g,token:m,model:i,error:p,sse:f}=u,s=t.front??n.front,c=w(u,a,s);v(e,t,{front:s,filter:n.settings.passthroughFilter!==!1,interval:o(r=>{try{return c.settings(b(c,r).endpoint).pingInterval}catch{return y(n.settings).pingInterval}},"interval"),send:d,route:g,count:m.count,list:i.list,get:i.get,build:p.build,encode:f.encode,write:f.write})}o(E,"plugin");export{E as plugin};
2
2
  //# sourceMappingURL=plugin.mjs.map
package/dist/index.cjs CHANGED
@@ -35,7 +35,7 @@
35
35
 
36
36
  if (!process.features?.require_module) {
37
37
  const error = new Error(
38
- `@cldmv/llm-translator: require() needs Node.js >=24.0.0 (this is ${process.version}). On older Node.js, load the package with import() instead.`
38
+ `@cldmv/llm-translator: require() needs Node.js >=22.15.0 (this is ${process.version}). On older Node.js, load the package with import() instead.`
39
39
  );
40
40
  error.code = "ERR_REQUIRE_ESM";
41
41
  throw error;
@@ -1,2 +1,2 @@
1
- var o=Object.defineProperty;var s=(t,r)=>o(t,"name",{value:r,configurable:!0});import{TranslatorError as a}from"./errors.mjs";import*as n from"./pool.mjs";function i(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}s(i,"baseUrl");function l(t,r){const e=typeof t=="string"?{url:t}:t;if(!e||typeof e!="object"||typeof e.url!="string"||e.url==="")throw new a(`upstreams[${r}] needs a "url"`,{code:"INVALID_CONFIG",status:500});return{name:e.name??`upstream-${r}`,url:i(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()}}s(l,"endpoint");function p(t={}){const r=t.front??"anthropic";if(r!=="anthropic"&&r!=="openai")throw new a(`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 a("at least one upstream is required (config.upstreams)",{code:"INVALID_CONFIG",status:500});return{front:r,endpoints:e.map(l),aliases:{...t.aliases},pool:n.normalize(t.pool),settings:{...t.settings},detect:t.detect!==!1,detectTimeout:t.detectTimeout??5e3,transport:t.transport,silent:t.silent===!0}}s(p,"normalize");export{l as endpoint,p as normalize};
1
+ var n=Object.defineProperty;var s=(t,r)=>n(t,"name",{value:r,configurable:!0});import{TranslatorError as o}from"./errors.mjs";import*as a from"./pool.mjs";import{normalizeAliases as i,normalizeEfforts as l}from"./effort.mjs";function u(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}s(u,"baseUrl");function p(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:u(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()}}s(p,"endpoint");function h(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});return{front:r,endpoints:e.map(p),...i(t.aliases),efforts:l(t.efforts),pool:a.normalize(t.pool),settings:{...t.settings},detect:t.detect!==!1,detectTimeout:t.detectTimeout??5e3,transport:t.transport,silent:t.silent===!0}}s(h,"normalize");export{p as endpoint,h as normalize};
2
2
  //# sourceMappingURL=config.mjs.map
@@ -0,0 +1,2 @@
1
+ var c=Object.defineProperty;var l=(t,f)=>c(t,"name",{value:f,configurable:!0});import{TranslatorError as g}from"./errors.mjs";const s=Object.freeze(["minimal","low","medium","high","xhigh","max"]);function a(t){return new g(t,{code:"INVALID_CONFIG",status:500})}l(a,"invalid");function k(t){const f={};for(const[i,n]of Object.entries(t??{})){const e=Array.isArray(n)?n:n?.levels;if(!Array.isArray(e)||e.length===0)throw a(`efforts.${i} must be a non-empty list of levels or { levels, default }`);for(const u of e)if(!s.includes(u))throw a(`efforts.${i}: unknown level ${JSON.stringify(u)} (expected ${s.join(", ")})`);const o=s.filter(u=>e.includes(u)),r=Array.isArray(n)?void 0:n.default;if(r!=null&&!o.includes(r))throw a(`efforts.${i}.default must be one of its levels or null (got ${JSON.stringify(r)})`);f[i]={levels:o,default:r}}return f}l(k,"normalizeEfforts");function v(t){const f={},i={};for(const[n,e]of Object.entries(t??{})){if(typeof e=="string"){f[n]=e;continue}if(e===null||typeof e!="object"||typeof e.model!="string")throw a(`aliases.${n} must be a model name or { model, effort, thinking }`);if(e.effort!==void 0&&!s.includes(e.effort))throw a(`aliases.${n}.effort must be one of ${s.join(", ")} (got ${JSON.stringify(e.effort)})`);if(e.thinking!==void 0&&typeof e.thinking!="boolean")throw a(`aliases.${n}.thinking must be true or false`);f[n]=e.model;const o={};e.effort!==void 0&&(o.effort=e.effort),e.thinking!==void 0&&(o.thinking=e.thinking),Object.keys(o).length>0&&(i[n]=o)}return{aliases:f,pins:i}}l(v,"normalizeAliases");function d(t,f){const i=s.indexOf(f);let n=t[0];for(const e of t){const o=Math.abs(s.indexOf(e)-i),r=Math.abs(s.indexOf(n)-i);(o<r||o===r&&s.indexOf(e)>s.indexOf(n))&&(n=e)}return n}l(d,"nearest");function h(t){return t.default===void 0?d(t.levels,"medium"):t.default}l(h,"defaultOf");function x(t,f,i){const n={...t};return i==="openai"?(f.effort!==void 0&&(n.reasoning_effort=f.effort),f.thinking===!1?n.reasoning_effort="none":f.thinking===!0&&n.reasoning_effort==="none"&&delete n.reasoning_effort,n):(f.effort!==void 0&&(n.output_config={...t.output_config,effort:f.effort}),f.thinking===!1?n.thinking={type:"disabled"}:f.thinking===!0&&(t.thinking===void 0||t.thinking?.type==="disabled")&&(n.thinking={type:"adaptive"}),n)}l(x,"pinBody");function $(t,f,i,n){if(t.reasoning_effort==="none"||t.chat_template_kwargs?.enable_thinking===!1)return;const e=t.reasoning_effort;if(e==null){const r=h(f);r!==null&&(t.reasoning_effort=r);return}if(f.levels.includes(e)||!s.includes(e))return;const o=d(f.levels,e);n(`Effort "${e}" is not supported by ${i} (${f.levels.join(", ")}); using "${o}".`),t.reasoning_effort=o}l($,"fit");export{s as LEVELS,h as defaultOf,$ as fit,d as nearest,v as normalizeAliases,k as normalizeEfforts,x as pinBody};
2
+ //# sourceMappingURL=effort.mjs.map
@@ -1,2 +1,2 @@
1
- var _=Object.defineProperty;var o=(t,e)=>_(t,"name",{value:e,configurable:!0});function p(t,{hasToolCalls:e,matchedStop:n,argumentsComplete:s=!0}){return e&&(t!=="length"||s)?{stop_reason:"tool_use",stop_sequence:null}:t==="length"?{stop_reason:"max_tokens",stop_sequence:null}:t==="content_filter"?{stop_reason:"refusal",stop_sequence:null}:typeof n=="string"&&n!==""?{stop_reason:"stop_sequence",stop_sequence:n}:{stop_reason:"end_turn",stop_sequence:null}}o(p,"toAnthropicStop");function u(t){switch(t){case"max_tokens":return"length";case"tool_use":return"tool_calls";case"refusal":return"content_filter";default:return"stop"}}o(u,"toOpenAIFinish");function l(t){const e=t?.prompt_tokens??0,n=t?.prompt_tokens_details?.cached_tokens??0;return{input_tokens:Math.max(0,e-n),output_tokens:t?.completion_tokens??0,cache_creation_input_tokens:0,cache_read_input_tokens:n}}o(l,"toAnthropicUsage");function i(t){const e=t?.cache_read_input_tokens??0,n=(t?.input_tokens??0)+e+(t?.cache_creation_input_tokens??0),s=t?.output_tokens??0,r={prompt_tokens:n,completion_tokens:s,total_tokens:n+s};return e>0&&(r.prompt_tokens_details={cached_tokens:e}),r}o(i,"toOpenAIUsage");export{p as toAnthropicStop,l as toAnthropicUsage,u as toOpenAIFinish,i as toOpenAIUsage};
1
+ var p=Object.defineProperty;var n=(t,e)=>p(t,"name",{value:e,configurable:!0});const _={abort:{type:"api_error",status:502},aborted:{type:"api_error",status:502},error:{type:"api_error",status:502},insufficient_system_resource:{type:"overloaded_error",status:503}};function u(t){if(typeof t!="string"||!Object.hasOwn(_,t))return null;const{type:e,status:o}=_[t];return{type:e,status:o,code:"upstream_finish_"+t,message:`Upstream ended the response with finish_reason "${t}"`}}n(u,"finishError");function a(t,{hasToolCalls:e,matchedStop:o,argumentsComplete:r=!0}){return e&&(t!=="length"&&t!=="model_length"||r)?{stop_reason:"tool_use",stop_sequence:null}:t==="length"||t==="model_length"?{stop_reason:"max_tokens",stop_sequence:null}:t==="content_filter"?{stop_reason:"refusal",stop_sequence:null}:typeof o=="string"&&o!==""?{stop_reason:"stop_sequence",stop_sequence:o}:{stop_reason:"end_turn",stop_sequence:null}}n(a,"toAnthropicStop");function l(t){switch(t){case"max_tokens":return"length";case"tool_use":return"tool_calls";case"refusal":return"content_filter";default:return"stop"}}n(l,"toOpenAIFinish");function i(t){const e=t?.prompt_tokens??0,o=t?.prompt_tokens_details?.cached_tokens??0;return{input_tokens:Math.max(0,e-o),output_tokens:t?.completion_tokens??0,cache_creation_input_tokens:0,cache_read_input_tokens:o}}n(i,"toAnthropicUsage");function k(t){const e=t?.cache_read_input_tokens??0,o=(t?.input_tokens??0)+e+(t?.cache_creation_input_tokens??0),r=t?.output_tokens??0,s={prompt_tokens:o,completion_tokens:r,total_tokens:o+r};return e>0&&(s.prompt_tokens_details={cached_tokens:e}),s}n(k,"toOpenAIUsage");export{u as finishError,a as toAnthropicStop,i as toAnthropicUsage,l as toOpenAIFinish,k as toOpenAIUsage};
2
2
  //# sourceMappingURL=finish.mjs.map
@@ -1,2 +1,2 @@
1
- var f=Object.defineProperty;var u=(e,t)=>f(e,"name",{value:t,configurable:!0});import{TranslatorError as c}from"./errors.mjs";import{order as m}from"./pool.mjs";function p(e,t){return(e.models??e.facts?.models??[]).includes(t)?!0:e.protocol==="anthropic.messages"&&e.models===void 0&&/^claude-/.test(t)}u(p,"serves");function v(e,t,o,n,a=!1){const d=Object.hasOwn(e.aliases,o)?e.aliases[o]:o,s=t.filter(l=>p(l,d)),r=t.find(l=>l.default)??t[0];if(s.length>0){const l=m(e,n,s,d,a);return{endpoint:l[0],model:d,unserved:!1,pool:l}}const i=r.models??r.facts?.models??[];return i.length===1?{endpoint:r,model:i[0],unserved:!1,pool:[r]}:{endpoint:r,model:d,unserved:!0,pool:[r]}}u(v,"pick");function k(e,t,o={}){if(!t||typeof t!="object"||Array.isArray(t))throw new c("The request body must be a JSON object",{code:"invalid_body",status:400});const n=v(e.config,e.state.endpoints,t.model,e.state,o.advance===!0);if(o.endpoint!==void 0){const s=n.pool.find(r=>r.name===o.endpoint);if(!s)throw new c(`Endpoint ${o.endpoint} no longer serves ${n.model}`,{code:"endpoint_not_in_pool",status:500});return{endpoint:s,model:n.model,requested:t.model,unserved:n.unserved,convert:e.converters(s),pool:[s]}}const{endpoint:a,pool:d}=n;return{endpoint:a,model:n.model,requested:t.model,unserved:n.unserved,convert:e.converters(a),pool:d}}u(k,"resolve");export{v as pick,k as resolve,p as serves};
1
+ var m=Object.defineProperty;var u=(e,n)=>m(e,"name",{value:n,configurable:!0});import{TranslatorError as f}from"./errors.mjs";import{order as v}from"./pool.mjs";function h(e,n){return(e.models??e.facts?.models??[]).includes(n)?!0:e.protocol==="anthropic.messages"&&e.models===void 0&&/^claude-/.test(n)}u(h,"serves");function w(e,n,t,o,i=!1){const a=Object.hasOwn(e.aliases,t),r=a?e.aliases[t]:t,l=a&&Object.hasOwn(e.pins,t)?e.pins[t]:void 0,c=n.filter(d=>h(d,r)),s=n.find(d=>d.default)??n[0];if(c.length>0){const d=v(e,o,c,r,i);return{endpoint:d[0],model:r,unserved:!1,pool:d,pin:l}}const p=s.models??s.facts?.models??[];return p.length===1?{endpoint:s,model:p[0],unserved:!1,pool:[s],pin:l}:{endpoint:s,model:r,unserved:!0,pool:[s],pin:l}}u(w,"pick");function _(e,n,t={}){if(!n||typeof n!="object"||Array.isArray(n))throw new f("The request body must be a JSON object",{code:"invalid_body",status:400});const o=w(e.config,e.state.endpoints,n.model,e.state,t.advance===!0);if(t.endpoint!==void 0){const r=o.pool.find(l=>l.name===t.endpoint);if(!r)throw new f(`Endpoint ${t.endpoint} no longer serves ${o.model}`,{code:"endpoint_not_in_pool",status:500});return{endpoint:r,model:o.model,requested:n.model,unserved:o.unserved,pin:o.pin,convert:e.converters(r),pool:[r]}}const{endpoint:i,pool:a}=o;return{endpoint:i,model:o.model,requested:n.model,unserved:o.unserved,pin:o.pin,convert:e.converters(i),pool:a}}u(_,"resolve");export{w as pick,_ as resolve,h as serves};
2
2
  //# sourceMappingURL=route.mjs.map
@@ -1,2 +1,2 @@
1
- var A=Object.defineProperty;var i=(e,n)=>A(e,"name",{value:n,configurable:!0});import{timingSafeEqual as H}from"node:crypto";import{TranslatorError as _}from"./errors.mjs";import{messageId as O}from"./ids.mjs";import{sign as S}from"./thinking.mjs";const T=64*1024*1024,j={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"},p=Symbol("stalled"),w=Symbol("gone");async function D(e,n){if(!(n>0))return e;let s;const c=new Promise(f=>{s=setTimeout(f,n,p)});try{return await Promise.race([e,c])}finally{clearTimeout(s)}}i(D,"settle");async function*I(e,n,s){let c=!1;try{const f=n.front==="anthropic";f&&(yield{event:"message_start",data:{type:"message_start",message:{id:O(),type:"message",role:"assistant",model:s,content:[],stop_reason:null,stop_sequence:null,usage:{input_tokens:0,output_tokens:0,cache_creation_input_tokens:0,cache_read_input_tokens:0}}}},yield{event:"content_block_start",data:{type:"content_block_start",index:0,content_block:{type:"thinking",thinking:"",signature:""}}});const a=await e,g=i(function*(){yield{event:"content_block_delta",data:{type:"content_block_delta",index:0,delta:{type:"signature_delta",signature:S()}}},yield{event:"content_block_stop",data:{type:"content_block_stop",index:0}}},"closeEarly");if(a.stream){c=!0;let u=null;for await(const l of a.stream){if(!f){yield l;continue}if(l.event!=="message_start"){if(u===null&&l.event==="content_block_start"&&l.data.content_block.type==="thinking"){u=0;continue}u===null&&(yield*g(),u=1),yield u>0&&typeof l.data?.index=="number"?{...l,data:{...l.data,index:l.data.index+u}}:l}}return}f&&(yield*g()),yield*x(n,a.body)}finally{c||e.then(f=>f.stream?.return?.(),()=>{})}}i(I,"late");function*x(e,n){if(e.front==="anthropic"){yield{event:"error",data:n};return}yield{data:n},yield{data:"[DONE]"}}i(x,"failure");async function v(e,n,s,c,f=200,a=j){e.hijack();const g=e.raw;g.writeHead(f,a);try{await n.write(g,c,{front:n.front,interval:n.interval})}catch(u){if(!s.signal.aborted){const l=u instanceof _;l||e.log.error(u);const{body:m}=n.build(l?u.status:502,l?u.message:"Internal server error",l?{type:u.type,code:u.code}:{});for(const k of x(n,m))g.write(n.encode(k))}}return g.end(),e}i(v,"stream");function L(e,n){const s=Buffer.from(e),c=Buffer.from(n);return s.length===c.length&&H(s,c)}i(L,"same");function z(e){const n=/^Bearer\s+(.+)$/i.exec(e.authorization??"");return e["x-api-key"]||n?.[1]||""}i(z,"keyOf");function B(e){const n={};for(const[s,c]of Object.entries(e))(s==="anthropic-beta"||s==="anthropic-version"||s.startsWith("x-claude-code-"))&&(n[s]=String(c));return n}i(B,"forwardable");function C(e){if(e===!0)return{ok:!0,context:void 0};if(e&&typeof e=="object"&&e.ok===!0)return{ok:!0,context:e.context};const n=e&&typeof e=="object"&&e.ok===!1?e:{};return{ok:!1,status:Number.isInteger(n.status)&&n.status>=400&&n.status<=599?n.status:401,message:typeof n.message=="string"&&n.message!==""?n.message:"unauthorized"}}i(C,"verdictOf");function R(e,n,s){const{front:c,filter:f}=s,a={...s,build:i((t,o,r={})=>s.build(t,o,{...r,front:c}),"build"),send:i((t,o)=>s.send(t,{...o,front:c,filter:f}),"send"),route:i(t=>s.route(t,{front:c}),"route"),count:i((t,o)=>s.count(t,{...o,front:c}),"count"),list:i(t=>s.list({...t,front:c}),"list"),get:i((t,o)=>s.get(t,{...o,front:c}),"get")},g=[n.apiKey??[]].flat().filter(Boolean),u=n.bodyLimit??T,l=new WeakMap,m=i((t,o)=>t.code(o.status).headers(o.headers).send(o.body),"reject");e.setErrorHandler((t,o,r)=>{if(t instanceof _)return m(r,a.build(t.status,t.message,{type:t.type,code:t.code,param:t.param}));const d=t.statusCode??500;return d<500?m(r,a.build(d,t.message)):(o.log.error(t),m(r,a.build(d,"Internal server error")))}),e.addHook("onRequest",async(t,o)=>{if(g.length===0||t.routeOptions.config?.public)return;const r=z(t.headers);if(!(r&&g.some(d=>L(r,d))))return m(o,a.build(401,"invalid x-api-key"))}),typeof n.authorize=="function"&&e.addHook("preHandler",async(t,o)=>{if(t.routeOptions.config?.public)return;let r=null;if(t.body&&typeof t.body=="object"&&!Array.isArray(t.body))try{r=a.route(t.body)}catch{}let d;try{d=C(await n.authorize({method:t.method,url:t.url,headers:t.headers,body:t.body,front:a.front,route:r}))}catch(y){t.log.error(y),d={ok:!1,status:401,message:"unauthorized"}}if(!d.ok)return m(o,a.build(d.status,d.message));l.set(t,d.context)}),e.route({method:["GET","HEAD"],url:"/",config:{public:!0},handler:i(async(t,o)=>o.send({ok:!0}),"handler")}),e.route({method:["GET","HEAD"],url:"/api/hello",config:{public:!0},handler:i(async(t,o)=>o.send({ok:!0}),"handler")}),e.get("/v1/models",async(t,o)=>{const r=new AbortController;return o.raw.on("close",()=>r.abort()),a.list({signal:r.signal})}),e.get("/v1/models/*",async(t,o)=>{const r=new AbortController;return o.raw.on("close",()=>r.abort()),a.get(t.params["*"],{signal:r.signal})});const k=a.front==="anthropic"?"/v1/messages":"/v1/chat/completions";e.post(k,{bodyLimit:u},async(t,o)=>{if(o.raw.destroyed)return o;const r=new AbortController;o.raw.on("close",()=>{o.raw.writableFinished||r.abort()});const d=typeof n.onUsage=="function"?h=>n.onUsage(h,l.get(t)):void 0,y=a.send(t.body,{signal:r.signal,clientHeaders:B(t.headers),onUsage:d});y.catch(()=>{});const E=new Promise(h=>r.signal.addEventListener("abort",()=>h(w),{once:!0})),b=await D(Promise.race([y,E]),t.body?.stream===!0?a.interval:0);return b===w?(y.then(h=>h.stream?.return?.(),()=>{}),o):b===p?v(o,a,r,I(y,a,t.body?.model)):b.stream?v(o,a,r,b.stream,b.status,b.headers):m(o,b)}),a.front==="anthropic"&&e.post("/v1/messages/count_tokens",{bodyLimit:u},async(t,o)=>{const r=new AbortController;return o.raw.on("close",()=>r.abort()),a.count(t.body,{signal:r.signal})})}i(R,"register");export{I as late,R as register};
1
+ var H=Object.defineProperty;var i=(e,n)=>H(e,"name",{value:n,configurable:!0});import{timingSafeEqual as O}from"node:crypto";import{TranslatorError as p}from"./errors.mjs";import{messageId as S}from"./ids.mjs";import{sign as T}from"./thinking.mjs";const j=64*1024*1024,D={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"},w=Symbol("stalled"),x=Symbol("gone");async function I(e,n){if(!(n>0))return e;let s;const l=new Promise(f=>{s=setTimeout(f,n,w)});try{return await Promise.race([e,l])}finally{clearTimeout(s)}}i(I,"settle");async function*L(e,n,s){let l=!1;try{const f=n.front==="anthropic";f&&(yield{event:"message_start",data:{type:"message_start",message:{id:S(),type:"message",role:"assistant",model:s,content:[],stop_reason:null,stop_sequence:null,usage:{input_tokens:0,output_tokens:0,cache_creation_input_tokens:0,cache_read_input_tokens:0}}}},yield{event:"content_block_start",data:{type:"content_block_start",index:0,content_block:{type:"thinking",thinking:"",signature:""}}});const a=await e,m=i(function*(){yield{event:"content_block_delta",data:{type:"content_block_delta",index:0,delta:{type:"signature_delta",signature:T()}}},yield{event:"content_block_stop",data:{type:"content_block_stop",index:0}}},"closeEarly");if(a.stream){l=!0;let d=null;for await(const c of a.stream){if(!f){yield c;continue}if(c.event!=="message_start"){if(d===null&&c.event==="content_block_start"&&c.data.content_block.type==="thinking"){d=0;continue}d===null&&(yield*m(),d=1),yield d>0&&typeof c.data?.index=="number"?{...c,data:{...c.data,index:c.data.index+d}}:c}}return}f&&(yield*m()),yield*v(n,a.body)}finally{l||e.then(f=>f.stream?.return?.(),()=>{})}}i(L,"late");function*v(e,n){if(e.front==="anthropic"){yield{event:"error",data:n};return}yield{data:n},yield{data:"[DONE]"}}i(v,"failure");async function E(e,n,s,l,f,a=200,m=D){e.hijack();const d=e.raw;d.writeHead(a,m);try{await n.write(d,l,{front:n.front,interval:f})}catch(c){if(!s.signal.aborted){const g=c instanceof p;g||e.log.error(c);const{body:_}=n.build(g?c.status:502,g?c.message:"Internal server error",g?{type:c.type,code:c.code}:{});for(const t of v(n,_))d.write(n.encode(t))}}return d.end(),e}i(E,"stream");function z(e,n){const s=Buffer.from(e),l=Buffer.from(n);return s.length===l.length&&O(s,l)}i(z,"same");function B(e){const n=/^Bearer\s+(.+)$/i.exec(e.authorization??"");return e["x-api-key"]||n?.[1]||""}i(B,"keyOf");function C(e){const n={};for(const[s,l]of Object.entries(e))(s==="anthropic-beta"||s==="anthropic-version"||s.startsWith("x-claude-code-"))&&(n[s]=String(l));return n}i(C,"forwardable");function P(e){if(e===!0)return{ok:!0,context:void 0};if(e&&typeof e=="object"&&e.ok===!0)return{ok:!0,context:e.context};const n=e&&typeof e=="object"&&e.ok===!1?e:{};return{ok:!1,status:Number.isInteger(n.status)&&n.status>=400&&n.status<=599?n.status:401,message:typeof n.message=="string"&&n.message!==""?n.message:"unauthorized"}}i(P,"verdictOf");function W(e,n,s){const{front:l,filter:f}=s,a={...s,build:i((t,o,r={})=>s.build(t,o,{...r,front:l}),"build"),send:i((t,o)=>s.send(t,{...o,front:l,filter:f}),"send"),route:i(t=>s.route(t,{front:l}),"route"),count:i((t,o)=>s.count(t,{...o,front:l}),"count"),list:i(t=>s.list({...t,front:l}),"list"),get:i((t,o)=>s.get(t,{...o,front:l}),"get")},m=[n.apiKey??[]].flat().filter(Boolean),d=n.bodyLimit??j,c=new WeakMap,g=i((t,o)=>t.code(o.status).headers(o.headers).send(o.body),"reject");e.setErrorHandler((t,o,r)=>{if(t instanceof p)return g(r,a.build(t.status,t.message,{type:t.type,code:t.code,param:t.param}));const u=t.statusCode??500;return u<500?g(r,a.build(u,t.message)):(o.log.error(t),g(r,a.build(u,"Internal server error")))}),e.addHook("onRequest",async(t,o)=>{if(m.length===0||t.routeOptions.config?.public)return;const r=B(t.headers);if(!(r&&m.some(u=>z(r,u))))return g(o,a.build(401,"invalid x-api-key"))}),typeof n.authorize=="function"&&e.addHook("preHandler",async(t,o)=>{if(t.routeOptions.config?.public)return;let r=null;if(t.body&&typeof t.body=="object"&&!Array.isArray(t.body))try{r=a.route(t.body)}catch{}let u;try{u=P(await n.authorize({method:t.method,url:t.url,headers:t.headers,body:t.body,front:a.front,route:r}))}catch(y){t.log.error(y),u={ok:!1,status:401,message:"unauthorized"}}if(!u.ok)return g(o,a.build(u.status,u.message));c.set(t,u.context)}),e.route({method:["GET","HEAD"],url:"/",config:{public:!0},handler:i(async(t,o)=>o.send({ok:!0}),"handler")}),e.route({method:["GET","HEAD"],url:"/api/hello",config:{public:!0},handler:i(async(t,o)=>o.send({ok:!0}),"handler")}),e.get("/v1/models",async(t,o)=>{const r=new AbortController;return o.raw.on("close",()=>r.abort()),a.list({signal:r.signal})}),e.get("/v1/models/*",async(t,o)=>{const r=new AbortController;return o.raw.on("close",()=>r.abort()),a.get(t.params["*"],{signal:r.signal})});const _=a.front==="anthropic"?"/v1/messages":"/v1/chat/completions";e.post(_,{bodyLimit:d},async(t,o)=>{if(o.raw.destroyed)return o;const r=new AbortController;o.raw.on("close",()=>{o.raw.writableFinished||r.abort()});const u=typeof n.onUsage=="function"?h=>n.onUsage(h,c.get(t)):void 0,y=a.interval(t.body),k=a.send(t.body,{signal:r.signal,clientHeaders:C(t.headers),onUsage:u});k.catch(()=>{});const A=new Promise(h=>r.signal.addEventListener("abort",()=>h(x),{once:!0})),b=await I(Promise.race([k,A]),t.body?.stream===!0?y:0);return b===x?(k.then(h=>h.stream?.return?.(),()=>{}),o):b===w?E(o,a,r,L(k,a,t.body?.model),y):b.stream?E(o,a,r,b.stream,y,b.status,b.headers):g(o,b)}),a.front==="anthropic"&&e.post("/v1/messages/count_tokens",{bodyLimit:d},async(t,o)=>{const r=new AbortController;return o.raw.on("close",()=>r.abort()),a.count(t.body,{signal:r.signal})})}i(W,"register");export{L as late,W as register};
2
2
  //# sourceMappingURL=routes.mjs.map
@@ -1,2 +1,2 @@
1
- var l=Object.defineProperty;var r=(o,e)=>l(o,"name",{value:e,configurable:!0});import{TranslatorError as s}from"./errors.mjs";const i=Object.freeze({inlineSystem:"auto",stripBillingHeader:!0,reasoningField:"reasoning",thinkTags:!0,thinkOpen:!1,reasoningRoundTrip:!1,thinking:"chat_template_kwargs",thinkingBudget:!1,effort:!1,images:!0,imageFallback:!0,toolFallback:!1,documents:"extract",documentConverter:null,pdf:Object.freeze({pages:!0,maxPages:100,maxBytes:32*1024*1024,maxImages:20,scale:1.5}),toolSchema:"pass",deferredTools:"forward",toolSearch:"expand",toolErrors:!1,maxTokensField:"max_completion_tokens",streamUsage:!0,continuousUsage:!1,prefill:"continue_final_message",topK:!0,responseFormat:!0,parallelToolCalls:!0,unknownFields:"drop",pingInterval:1e4,toolStreaming:"buffer",effortPassthrough:!1,passthroughFilter:!1,contextWindow:0,defaultMaxTokens:8192}),f={inlineSystem:["auto","keep","hoist","user","drop"],reasoningField:["reasoning","reasoning_content"],thinking:["chat_template_kwargs","reasoning_effort","none"],documents:["extract","note","error"],toolSchema:["pass","sanitize"],deferredTools:["forward","drop"],toolSearch:["expand","inline","native"],maxTokensField:["max_completion_tokens","max_tokens"],prefill:["continue_final_message","none"],unknownFields:["drop","error"],toolStreaming:["buffer","stream"]},d=["effortPassthrough","passthroughFilter","imageFallback","toolFallback","thinkOpen","reasoningRoundTrip"];function p(...o){const e={...i,pdf:{...i.pdf}};for(const t of o)if(t)for(const[n,a]of Object.entries(t))a!==void 0&&(n==="pdf"?Object.assign(e.pdf,a):e[n]=a);for(const[t,n]of Object.entries(f))if(!n.includes(e[t]))throw new s(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected one of ${n.join(", ")})`,{code:"invalid_setting",status:500,details:{key:t,allowed:n}});for(const t of d)if(typeof e[t]!="boolean")throw new s(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected true or false)`,{code:"invalid_setting",status:500,details:{key:t}});if(!(Number.isFinite(e.pdf.scale)&&e.pdf.scale>0))throw new s(`Invalid setting pdf.scale: ${JSON.stringify(e.pdf.scale)} (expected a positive number)`,{code:"invalid_setting",status:500,details:{key:"pdf.scale"}});return e}r(p,"resolve");export{i as DEFAULTS,p as resolve};
1
+ var l=Object.defineProperty;var s=(n,e)=>l(n,"name",{value:e,configurable:!0});import{TranslatorError as r}from"./errors.mjs";const i=Object.freeze({inlineSystem:"auto",stripBillingHeader:!0,reasoningField:"reasoning",thinkTags:!0,thinkOpen:!1,reasoningRoundTrip:!1,thinking:"chat_template_kwargs",thinkingBudget:!1,effort:!1,images:!0,imageFallback:!0,toolFallback:!1,documents:"extract",documentConverter:null,pdf:Object.freeze({pages:!0,maxPages:100,maxBytes:32*1024*1024,maxImages:20,scale:1.5}),toolSchema:"pass",deferredTools:"forward",toolSearch:"expand",toolErrors:!1,toolErrorMarker:"[error] ",toolErrorInference:!1,maxTokensField:"max_completion_tokens",streamUsage:!0,continuousUsage:!1,prefill:"continue_final_message",topK:!0,responseFormat:!0,parallelToolCalls:!0,unknownFields:"drop",pingInterval:1e4,toolStreaming:"buffer",effortPassthrough:!1,passthroughFilter:!1,contextWindow:0,defaultMaxTokens:8192}),f={inlineSystem:["auto","keep","hoist","user","drop"],reasoningField:["reasoning","reasoning_content"],thinking:["chat_template_kwargs","reasoning_effort","none"],documents:["extract","note","error"],toolSchema:["pass","sanitize"],deferredTools:["forward","drop"],toolSearch:["expand","inline","native"],maxTokensField:["max_completion_tokens","max_tokens"],prefill:["continue_final_message","none"],unknownFields:["drop","error"],toolStreaming:["buffer","stream"]},d=["effortPassthrough","passthroughFilter","imageFallback","toolFallback","thinkOpen","reasoningRoundTrip","toolErrorInference"];function p(...n){const e={...i,pdf:{...i.pdf}};for(const t of n)if(t)for(const[o,a]of Object.entries(t))a!==void 0&&(o==="pdf"?Object.assign(e.pdf,a):e[o]=a);for(const[t,o]of Object.entries(f))if(!o.includes(e[t]))throw new r(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected one of ${o.join(", ")})`,{code:"invalid_setting",status:500,details:{key:t,allowed:o}});for(const t of d)if(typeof e[t]!="boolean")throw new r(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected true or false)`,{code:"invalid_setting",status:500,details:{key:t}});if(typeof e.toolErrorMarker!="string"||e.toolErrorMarker.trim()==="")throw new r(`Invalid setting toolErrorMarker: ${JSON.stringify(e.toolErrorMarker)} (expected a non-empty string)`,{code:"invalid_setting",status:500,details:{key:"toolErrorMarker"}});if(!(Number.isFinite(e.pdf.scale)&&e.pdf.scale>0))throw new r(`Invalid setting pdf.scale: ${JSON.stringify(e.pdf.scale)} (expected a positive number)`,{code:"invalid_setting",status:500,details:{key:"pdf.scale"}});return e}s(p,"resolve");export{i as DEFAULTS,p as resolve};
2
2
  //# sourceMappingURL=settings.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cldmv/llm-translator",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Bidirectional Anthropic Messages ⇄ OpenAI Chat Completions translator with backend auto-detection (vLLM and other OpenAI-compatible servers), built on slothlet, with an optional standalone server for running Claude Code against local models.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -25,12 +25,12 @@
25
25
  },
26
26
  "sideEffects": false,
27
27
  "engines": {
28
- "node": ">=24.0.0"
28
+ "node": ">=22.15.0"
29
29
  },
30
30
  "devEngines": {
31
31
  "runtime": {
32
32
  "name": "node",
33
- "version": ">=24.0.0",
33
+ "version": ">=22.22.0",
34
34
  "onFail": "error"
35
35
  }
36
36
  },
@@ -110,7 +110,7 @@
110
110
  "unpdf": "^2.1.0"
111
111
  },
112
112
  "devDependencies": {
113
- "@cldmv/configs": "^1.3.0",
113
+ "@cldmv/configs": "^1.4.0",
114
114
  "@cldmv/eslint-plugin-jsonv": "^1.0.17",
115
115
  "@cldmv/fix-headers": "^2.2.2",
116
116
  "@cldmv/jsonv": "^1.1.5",
@@ -130,5 +130,9 @@
130
130
  "tsup": "^9.0.0",
131
131
  "typescript": "^6.0.3",
132
132
  "vitest": "^5.0.5"
133
+ },
134
+ "overrides": {
135
+ "katex": ">=0.19.1 <1.0.0",
136
+ "minimatch": "^11.0.3"
133
137
  }
134
138
  }
@@ -5,13 +5,14 @@
5
5
  * tool-name map, thinking mode, fields that were dropped).
6
6
  * @param {any} body - Anthropic `POST /v1/messages` body.
7
7
  * @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
8
- * @param {{ producer?: string, budget?: ReturnType<typeof createBudget> }} [context] - `producer` is the `driver:protocol:host` the request goes to; signatures that carry provider reasoning data are restored only for the producer that made them. `budget` limits how much signature decoding the request may cost (one is made when absent; `send` passes one for all its attempts).
8
+ * @param {{ producer?: string, budget?: ReturnType<typeof createBudget>, listed?: boolean }} [context] - `producer` is the `driver:protocol:host` the request goes to; signatures that carry provider reasoning data are restored only for the producer that made them. `budget` limits how much signature decoding the request may cost (one is made when absent; `send` passes one for all its attempts). `listed` says the model has a configured effort list, so the default `medium` effort is left to it.
9
9
  * @returns {Promise<{ body: any, state: any }>} OpenAI request body and conversion state.
10
10
  * @throws {TranslatorError} For malformed requests, unsupported content when configured to error, or PDF limit violations.
11
11
  */
12
12
  export function request(body: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>, context?: {
13
13
  producer?: string;
14
14
  budget?: ReturnType<typeof createBudget>;
15
+ listed?: boolean;
15
16
  }): Promise<{
16
17
  body: any;
17
18
  state: any;
@@ -9,6 +9,6 @@
9
9
  * @param {any} [state] - State from the request converter (original model, tool-name map).
10
10
  * @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
11
11
  * @returns {any} Anthropic message.
12
- * @throws {TranslatorError} When the completion has no choices.
12
+ * @throws {TranslatorError} When the completion has no choices, or its `finish_reason` is a fatal one (`abort`, `aborted`, `error`, `insufficient_system_resource`).
13
13
  */
14
14
  export function response(completion: any, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): any;
@@ -1,8 +1,9 @@
1
1
  /**
2
2
  * Drop tool fields vLLM rejects when no tools are present, and clamp `reasoning_effort` to what vLLM accepts
3
- * (`xhigh` and `max` become `high`) unless the `effortPassthrough` setting is on.
3
+ * (`xhigh` and `max` become `high`) unless the `effortPassthrough` setting is on or the model has a configured level list (`ctx.levels`,
4
+ * which `request` has already fitted the effort to).
4
5
  * @param {any} body - Chat Completions request body.
5
- * @param {any} [ctx] - `{ settings }`.
6
+ * @param {any} [ctx] - `{ settings, levels }`.
6
7
  * @returns {any} Normalized body.
7
8
  */
8
9
  export function request(body: any, ctx?: any): any;
@@ -2,7 +2,8 @@
2
2
  * @typedef {object} TranslatorConfig
3
3
  * @property {"anthropic" | "openai"} [front] Client protocol the translator speaks (default `"anthropic"`).
4
4
  * @property {Array<string | import("./lib/config.mjs").UpstreamConfig>} upstreams Upstream servers: `{ name, url, apiKey, headers, backend, models, default, settings, version }` (or a bare URL).
5
- * @property {Record<string, string>} [aliases] Client model name to upstream model name.
5
+ * @property {Record<string, string | {model: string, effort?: "minimal"|"low"|"medium"|"high"|"xhigh"|"max", thinking?: boolean}>} [aliases] Client model name to upstream model name, or to `{ model, effort, thinking }` to pin those settings over the request's.
6
+ * @property {Record<string, string[] | {levels: string[], default?: string | null}>} [efforts] Reasoning effort levels each model supports, keyed by model name after aliases (see docs/settings.md).
6
7
  * @property {import("./lib/pool.mjs").PoolConfig} [pool] Endpoint pools: `{ strategy, backoff: { base, max }, models }` (see docs/settings.md).
7
8
  * @property {Record<string, any>} [settings] Global converter settings (see `lib/settings.mjs`).
8
9
  * @property {boolean} [detect] Probe `auto` upstreams at startup (default true).
@@ -30,9 +31,20 @@ export type TranslatorConfig = {
30
31
  */
31
32
  upstreams: Array<string | import("./lib/config.mjs").UpstreamConfig>;
32
33
  /**
33
- * Client model name to upstream model name.
34
+ * Client model name to upstream model name, or to `{ model, effort, thinking }` to pin those settings over the request's.
34
35
  */
35
- aliases?: Record<string, string>;
36
+ aliases?: Record<string, string | {
37
+ model: string;
38
+ effort?: "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
39
+ thinking?: boolean;
40
+ }>;
41
+ /**
42
+ * Reasoning effort levels each model supports, keyed by model name after aliases (see docs/settings.md).
43
+ */
44
+ efforts?: Record<string, string[] | {
45
+ levels: string[];
46
+ default?: string | null;
47
+ }>;
36
48
  /**
37
49
  * Endpoint pools: `{ strategy, backoff: { base, max }, models }` (see docs/settings.md).
38
50
  */
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Validate and normalize the `efforts` config: model name to a list of levels, or `{ levels, default }`.
3
+ * @param {any} [raw] - Raw `efforts` value.
4
+ * @returns {Record<string, EffortConfig>} Normalized map.
5
+ * @throws {TranslatorError} `INVALID_CONFIG` for an unknown level, an empty list or a default that is not in the list.
6
+ */
7
+ export function normalizeEfforts(raw?: any): Record<string, EffortConfig>;
8
+ /**
9
+ * Split the `aliases` config into the plain name map and the pinned settings. An alias is a model name or `{ model, effort, thinking }`.
10
+ * @param {any} [raw] - Raw `aliases` value.
11
+ * @returns {{aliases: Record<string, string>, pins: Record<string, {effort?: string, thinking?: boolean}>}} Name map and pins.
12
+ * @throws {TranslatorError} `INVALID_CONFIG` for an object alias without a `model`, an unknown `effort` or a non-boolean `thinking`.
13
+ */
14
+ export function normalizeAliases(raw?: any): {
15
+ aliases: Record<string, string>;
16
+ pins: Record<string, {
17
+ effort?: string;
18
+ thinking?: boolean;
19
+ }>;
20
+ };
21
+ /**
22
+ * The supported level nearest to a wanted one on the scale; a tie goes to the higher level.
23
+ * @param {string[]} levels - Supported levels.
24
+ * @param {string} wanted - A level from {@link LEVELS}.
25
+ * @returns {string} Supported level.
26
+ */
27
+ export function nearest(levels: string[], wanted: string): string;
28
+ /**
29
+ * The level sent when a request names none.
30
+ * @param {EffortConfig} entry - Model's effort config.
31
+ * @returns {string | null} Level, or null to send none.
32
+ */
33
+ export function defaultOf(entry: EffortConfig): string | null;
34
+ /**
35
+ * Apply an alias's pinned settings to the client body (in the front protocol), before it is converted.
36
+ * @param {any} body - Client body.
37
+ * @param {{effort?: string, thinking?: boolean}} pin - Pinned settings.
38
+ * @param {"anthropic" | "openai"} front - Client protocol.
39
+ * @returns {any} New body.
40
+ */
41
+ export function pinBody(body: any, pin: {
42
+ effort?: string;
43
+ thinking?: boolean;
44
+ }, front: "anthropic" | "openai"): any;
45
+ /**
46
+ * Fit the `reasoning_effort` of an OpenAI Chat Completions body to a model's levels. A level in the list is kept, another one snaps to the
47
+ * nearest (the message is passed to `warn`), a request with no effort gets the model's default, and a request that turns reasoning off is left alone.
48
+ * @param {any} body - Upstream body (mutated).
49
+ * @param {EffortConfig} entry - Model's effort config.
50
+ * @param {string} model - Model name, for the message.
51
+ * @param {(message: string) => void} warn - Warning sink.
52
+ */
53
+ export function fit(body: any, entry: EffortConfig, model: string, warn: (message: string) => void): void;
54
+ /** The effort levels a model can be configured with, lowest to highest. `none` is not one: it switches reasoning off and is never snapped. */
55
+ export const LEVELS: readonly string[];
56
+ export type EffortConfig = {
57
+ /**
58
+ * - Supported levels, lowest to highest.
59
+ */
60
+ levels: string[];
61
+ /**
62
+ * - Level sent when the request names none; `null` sends nothing (the model has its own default); `undefined` picks the level nearest to `medium`.
63
+ */
64
+ default: string | null | undefined;
65
+ };
@@ -1,22 +1,18 @@
1
1
  /**
2
- *
3
- * @Project: @cldmv/llm-translator
4
- * @Filename: /src/lib/finish.mjs
5
- * @Date: 2026-10-25T12:28:19-07:00 (1792956499)
6
- * @Author: Nate Corcoran <CLDMV>
7
- * @Email: <Shinrai@users.noreply.github.com>
8
- * -----
9
- * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
10
- * @Last modified time: 2026-10-25T12:59:01-07:00 (1792958341)
11
- * -----
12
- * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
- *
14
- */
15
- /**
16
- * @fileoverview Stop-reason and usage mapping between the two protocols.
2
+ * The error a fatal OpenAI `finish_reason` stands for. Servers use `abort`, `aborted` and `error` when generation was cut off and
3
+ * `insufficient_system_resource` when they ran out of resources mid-answer; none of them is a finished answer.
4
+ * @param {string|null|undefined} finishReason - OpenAI `finish_reason`.
5
+ * @returns {{ type: string, status: number, code: string, message: string }|null} Error details, or null when the reason is not fatal.
17
6
  */
7
+ export function finishError(finishReason: string | null | undefined): {
8
+ type: string;
9
+ status: number;
10
+ code: string;
11
+ message: string;
12
+ } | null;
18
13
  /**
19
14
  * Anthropic stop reason (and stop sequence) for an OpenAI choice.
15
+ * `model_length` is `max_tokens`; `eos`, `eos_token` and any other unknown value are `end_turn`. Fatal values are handled by {@link finishError} before this.
20
16
  * @param {string|null|undefined} finishReason - OpenAI `finish_reason`.
21
17
  * @param {{ hasToolCalls: boolean, matchedStop?: unknown, argumentsComplete?: boolean }} info - Whether tool calls were produced, vLLM's `stop_reason` (a string for a matched stop string), and whether the last tool call's arguments are complete. A `length` stop with incomplete arguments is `max_tokens`, not `tool_use`.
22
18
  * @returns {{ stop_reason: string, stop_sequence: string|null }} Anthropic stop fields.
@@ -13,7 +13,7 @@ export function serves(endpoint: any, model: string): boolean;
13
13
  * @param {string | undefined} model - Model named by the client.
14
14
  * @param {any} state - Translator state (`cursors`).
15
15
  * @param {boolean} [advance] - Move the round robin cursor (false to only look).
16
- * @returns {{endpoint: any, model: string | undefined, unserved: boolean, pool: any[]}} Chosen endpoint, the model name to send upstream, whether
16
+ * @returns {{endpoint: any, model: string | undefined, unserved: boolean, pool: any[], pin: {effort?: string, thinking?: boolean} | undefined}} Chosen endpoint, the model name to send upstream, whether
17
17
  * no endpoint serves the model and the endpoints to try in order (the chosen one first). When nothing serves it and the fallback endpoint knows
18
18
  * exactly one model, that model is sent instead.
19
19
  */
@@ -22,14 +22,18 @@ export function pick(config: any, endpoints: any[], model: string | undefined, s
22
22
  model: string | undefined;
23
23
  unserved: boolean;
24
24
  pool: any[];
25
+ pin: {
26
+ effort?: string;
27
+ thinking?: boolean;
28
+ } | undefined;
25
29
  };
26
30
  /**
27
31
  * Validate a client request body and resolve where it goes. Shared by `request` and `route` so both always agree.
28
32
  * @param {any} c - Captured translator parts (`capture`).
29
33
  * @param {any} body - Client request body.
30
34
  * @param {{endpoint?: string, advance?: boolean}} [options] - `endpoint` forces one pool member by name (used for failover; it must still serve the model, else `endpoint_not_in_pool`); `advance` moves the round robin cursor (`request` does, `route` does not).
31
- * @returns {{endpoint: any, model: string, requested: string | undefined, unserved: boolean, convert: any, pool: any[]}} The chosen endpoint, the model name sent
32
- * upstream, the model the client asked for, whether no endpoint serves it, the converters (null for a native pass-through) and the pool in try order.
35
+ * @returns {{endpoint: any, model: string, requested: string | undefined, unserved: boolean, pin: {effort?: string, thinking?: boolean} | undefined, convert: any, pool: any[]}} The chosen endpoint, the model name sent
36
+ * upstream, the model the client asked for, whether no endpoint serves it, the settings its alias pins, the converters (null for a native pass-through) and the pool in try order.
33
37
  * @throws {TranslatorError} `invalid_body` when the body is not a JSON object, `endpoint_not_in_pool` when a forced endpoint no longer serves the model.
34
38
  */
35
39
  export function resolve(c: any, body: any, options?: {
@@ -40,6 +44,10 @@ export function resolve(c: any, body: any, options?: {
40
44
  model: string;
41
45
  requested: string | undefined;
42
46
  unserved: boolean;
47
+ pin: {
48
+ effort?: string;
49
+ thinking?: boolean;
50
+ } | undefined;
43
51
  convert: any;
44
52
  pool: any[];
45
53
  };
@@ -11,7 +11,7 @@ export function late(sending: Promise<any>, api: any, model?: string): AsyncGene
11
11
  * Register the routes for one front on a Fastify instance.
12
12
  * @param {any} fastify - Fastify instance (usually an encapsulated plugin scope).
13
13
  * @param {any} options - `{ apiKey, bodyLimit, authorize, onUsage }`.
14
- * @param {any} captured - Captured translator parts `{ front, interval, filter, send, route, count, list, get, build, encode, write }`; `front` applies to this instance only.
14
+ * @param {any} captured - Captured translator parts `{ front, interval(body), filter, send, route, count, list, get, build, encode, write }`; `front` applies to this instance only.
15
15
  * @returns {void}
16
16
  */
17
17
  export function register(fastify: any, options: any, captured: any): void;
@@ -26,7 +26,9 @@ export function resolve(...layers: (Partial<Settings> | null | undefined)[]): Se
26
26
  * @property {"pass"|"sanitize"} toolSchema - Sanitize tool JSON Schemas (drop `$schema`, remove unbounded integer limits).
27
27
  * @property {"forward"|"drop"} deferredTools - Offer `defer_loading` tools as `toolSearch` says, or drop them (and the search tool).
28
28
  * @property {"expand"|"inline"|"native"} toolSearch - Claude Code's tool search. `expand` offers a deferred tool once a `tool_reference` (or a `ToolSearch` result) loads it; `inline` offers every deferred tool up front and hides `ToolSearch` (it stays when `DeferredToolPlaceholder` shows Claude Code holds tools whose definitions it did not send; `expand` does the same when the request has no `ToolSearch` at all); `native` passes `defer_loading` and `tool_reference` through to a server that understands them.
29
- * @property {boolean} toolErrors - Prefix `is_error` tool results with `[error] `.
29
+ * @property {boolean} toolErrors - Prefix `is_error` tool results with `toolErrorMarker`. Off by default for every driver.
30
+ * @property {string} toolErrorMarker - Text that marks a failed tool result when `toolErrors` is on, and the text `toolErrorInference` looks for. Must contain a non-whitespace character.
31
+ * @property {boolean} toolErrorInference - OpenAI to Anthropic: a `tool` message whose text starts with `toolErrorMarker` becomes a `tool_result` with `is_error: true`, with the marker removed from the text.
30
32
  * @property {"max_completion_tokens"|"max_tokens"} maxTokensField - Field used for the output cap.
31
33
  * @property {boolean} streamUsage - Request `stream_options.include_usage` on streamed requests.
32
34
  * @property {boolean} continuousUsage - Also request `stream_options.continuous_usage_stats` (vLLM extension; real OpenAI may reject it).
@@ -127,9 +129,17 @@ export type Settings = {
127
129
  */
128
130
  toolSearch: "expand" | "inline" | "native";
129
131
  /**
130
- * - Prefix `is_error` tool results with `[error] `.
132
+ * - Prefix `is_error` tool results with `toolErrorMarker`. Off by default for every driver.
131
133
  */
132
134
  toolErrors: boolean;
135
+ /**
136
+ * - Text that marks a failed tool result when `toolErrors` is on, and the text `toolErrorInference` looks for. Must contain a non-whitespace character.
137
+ */
138
+ toolErrorMarker: string;
139
+ /**
140
+ * - OpenAI to Anthropic: a `tool` message whose text starts with `toolErrorMarker` becomes a `tool_result` with `is_error: true`, with the marker removed from the text.
141
+ */
142
+ toolErrorInference: boolean;
133
143
  /**
134
144
  * - Field used for the output cap.
135
145
  */