@cldmv/llm-translator 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.md +6 -6
  2. package/dist/api/convert/anthropic/messages/to/openai/chat/request.mjs +11 -11
  3. package/dist/api/convert/anthropic/messages/to/openai/chat/response.mjs +1 -1
  4. package/dist/api/convert/anthropic/messages/to/openai/chat/stream.mjs +1 -1
  5. package/dist/api/convert/openai/chat/to/anthropic/messages/request.mjs +4 -4
  6. package/dist/api/convert/openai/chat/to/anthropic/messages/response.mjs +1 -1
  7. package/dist/api/convert/openai/chat/to/anthropic/messages/stream.mjs +1 -1
  8. package/dist/api/detect.mjs +1 -1
  9. package/dist/api/driver/anthropic/detect.mjs +1 -1
  10. package/dist/api/driver/anthropic/info.mjs +1 -1
  11. package/dist/api/driver/lmstudio/detect.mjs +2 -0
  12. package/dist/api/driver/lmstudio/info.mjs +2 -0
  13. package/dist/api/driver/lmstudio/request.mjs +2 -0
  14. package/dist/api/driver/lmstudio/settings.mjs +2 -0
  15. package/dist/api/driver/mistral/chunk.mjs +2 -0
  16. package/dist/api/driver/mistral/detect.mjs +2 -0
  17. package/dist/api/driver/mistral/info.mjs +2 -0
  18. package/dist/api/driver/mistral/request.mjs +2 -0
  19. package/dist/api/driver/mistral/response.mjs +2 -0
  20. package/dist/api/driver/mistral/settings.mjs +2 -0
  21. package/dist/api/embeddings/send.mjs +1 -1
  22. package/dist/api/request.mjs +2 -2
  23. package/dist/api/send.mjs +1 -1
  24. package/dist/lib/backends.mjs +1 -1
  25. package/dist/lib/fanout.mjs +2 -0
  26. package/dist/lib/finish.mjs +1 -1
  27. package/dist/lib/http.mjs +1 -1
  28. package/dist/lib/ids.mjs +1 -1
  29. package/dist/lib/record.mjs +2 -2
  30. package/dist/lib/routes.mjs +1 -1
  31. package/dist/lib/typed.mjs +2 -0
  32. package/dist/lib/usage.mjs +1 -1
  33. package/package.json +4 -3
  34. package/types/src/api/convert/anthropic/messages/to/openai/chat/request.d.mts +6 -1
  35. package/types/src/api/convert/openai/chat/to/anthropic/messages/request.d.mts +9 -2
  36. package/types/src/api/driver/anthropic/detect.d.mts +10 -23
  37. package/types/src/api/driver/anthropic/info.d.mts +3 -1
  38. package/types/src/api/driver/lmstudio/detect.d.mts +6 -0
  39. package/types/src/api/driver/lmstudio/info.d.mts +31 -0
  40. package/types/src/api/driver/lmstudio/request.d.mts +6 -0
  41. package/types/src/api/driver/lmstudio/settings.d.mts +23 -0
  42. package/types/src/api/driver/mistral/chunk.d.mts +8 -0
  43. package/types/src/api/driver/mistral/detect.d.mts +26 -0
  44. package/types/src/api/driver/mistral/info.d.mts +35 -0
  45. package/types/src/api/driver/mistral/request.d.mts +6 -0
  46. package/types/src/api/driver/mistral/response.d.mts +7 -0
  47. package/types/src/api/driver/mistral/settings.d.mts +24 -0
  48. package/types/src/api/embeddings/send.d.mts +10 -2
  49. package/types/src/api/send.d.mts +7 -0
  50. package/types/src/lib/backends.d.mts +7 -0
  51. package/types/src/lib/fanout.d.mts +68 -0
  52. package/types/src/lib/finish.d.mts +14 -3
  53. package/types/src/lib/http.d.mts +8 -2
  54. package/types/src/lib/ids.d.mts +40 -2
  55. package/types/src/lib/record.d.mts +7 -2
  56. package/types/src/lib/typed.d.mts +10 -0
  57. package/types/src/lib/usage.d.mts +20 -0
package/README.md CHANGED
@@ -17,18 +17,18 @@ Use it as a library when you want the conversion without any I/O (`request`, `re
17
17
 
18
18
  ## ✨ What's New
19
19
 
20
- ### Latest: v0.6.0 (November 2026)
20
+ ### Latest: v0.8.0 (November 2026)
21
21
 
22
- - **Embeddings pass-through** — `POST /v1/embeddings` on the OpenAI front, and `api.embeddings.send()` as a library call, forward to an OpenAI-protocol upstream that lists the model. Aliases, pools, failover, `apiKey` and `authorize` work as they do for chat.
23
- - **Sampling, strict tools and capture** — the `sampling` setting and `metadata.sampling` send `min_p`, `repetition_penalty` and similar fields to upstreams that accept them. `strict` now round trips: a tool whose schema is outside the strict subset is sent without `strict` and reported, and a backend that does not enforce strict use gets a warning. The `capture` option records redacted request and response pairs to a directory or a callback.
24
- - [View full v0.6.0 Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.6.0.md)
22
+ - **Mistral and LM Studio drivers** — api.mistral.ai is matched by host and gets 9-character tool-call ids, `random_seed`, `tool_choice: "any"` and flattened reasoning chunks. LM Studio is recognised from its own `/api/v0/models` listing, and its Anthropic-compatible endpoint is one `backend: "anthropic"` away.
23
+ - **`n` above 1 and refusal categories** — an OpenAI client's `n` above 1 sent to an Anthropic-protocol upstream is answered by `n` parallel requests, combined into one reply or stream with summed usage. A refusal gets `stop_details.category: "general_harms"` when an Azure-style content filter flags hate, sexual, violence or self-harm.
24
+ - [View full v0.8.0 Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.8.0.md)
25
25
 
26
26
  ### Recent Releases
27
27
 
28
+ - **v0.7.0** (November 2026) — Anthropic-protocol servers detected by probe on any host, tool-call ids rewritten for strict upstream formats, embeddings usage through `onUsage`, and capture records for every failed-over attempt ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.7.0.md))
29
+ - **v0.6.0** (November 2026) — `POST /v1/embeddings` pass-through, sampling extensions per upstream and per request, `strict` tool use end to end with a schema check, and the `capture` option for debugging ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.6.0.md))
28
30
  - **v0.5.0** (November 2026) — everything the library logs is available as events, logging is opt-in through `logs`, `json_object` reaches Anthropic upstreams as a schema with a fallback, and refusals carry `stop_details` ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.5.0.md))
29
31
  - **v0.4.0** (November 2026) — per-model effort levels and aliases that pin effort and thinking, non-standard finish reasons, a configurable tool-error marker, per-upstream `pingInterval` and a Node.js 22.15 floor ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.4.0.md))
30
- - **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))
31
- - **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))
32
32
 
33
33
  📚 **For complete version history and detailed release notes, see the [docs/changelog/](https://github.com/CLDMV/llm-translator/tree/master/docs/changelog/) folder.**
34
34
 
@@ -1,17 +1,17 @@
1
- var N=Object.defineProperty;var m=(e,t)=>N(e,"name",{value:t,configurable:!0});import{resolve as q}from"../../../../../../../lib/settings.mjs";import{TranslatorError as T}from"../../../../../../../lib/errors.mjs";import{createNameFitter as O}from"../../../../../../../lib/ids.mjs";import{sanitize as B}from"../../../../../../../lib/schema.mjs";import{NOTE as I}from"../../../../../../../lib/images.mjs";import{expand as A}from"../../../../../../../lib/documents.mjs";import{createBudget as L,unsign as z}from"../../../../../../../lib/thinking.mjs";import{plan as C,scan as D}from"../../../../../../../lib/toolsearch.mjs";const P=new Set(["model","messages","system","max_tokens","stream","temperature","top_p","top_k","stop_sequences","tools","tool_choice","thinking","output_config","output_format","context_management"]),W=new Set(["metadata","cache_control","service_tier"]),H=new Set(["mcp_servers","container","inference_geo"]),K="x-anthropic-billing-header:";async function ue(e,t,o){const n=q(t);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new T("messages: Field required",{code:"invalid_request",param:"messages"});const i={front:"anthropic",model:e.model,stream:e.stream===!0,thinking:e.thinking?.type??null,reasoningField:n.reasoningField,toolNames:{},dropped:[]};o?.producer!==void 0&&(i.producer=o.producer);const r=O({max:64,map:i.toolNames});for(const c of e.tools??[])(c.type===void 0||c.type==="custom")&&r(c.name);const d=D(e.messages,new Set((e.tools??[]).map(c=>c.name))),l=C(e.tools??[],d,n,e.tool_choice);i.toolSearch={mode:n.toolSearch,loaded:l.loaded,hidden:l.hidden,stubbed:l.stubbed,capped:d.capped};for(const c of Object.keys(e))if(!(P.has(c)||W.has(c)))if(H.has(c))g(i,c);else{if(n.unknownFields==="error")throw new T(`${c}: Extra inputs are not permitted`,{code:"unsupported_field",param:c});g(i,c)}const s={model:e.model},h=F(e.system,n),k=await R(e.messages,n,i,r,h,U(e.context_management,i),o?.budget??L());s.messages=k.messages,k.prefill&&(s.continue_final_message=!0,s.add_generation_prompt=!1),e.max_tokens!==void 0&&(s[n.maxTokensField]=e.max_tokens),e.temperature!==void 0&&(s.temperature=e.temperature),e.top_p!==void 0&&(s.top_p=e.top_p),e.top_k!==void 0&&(n.topK?s.top_k=e.top_k:g(i,"top_k")),e.stop_sequences?.length>0&&(s.stop=e.stop_sequences),i.stream&&(s.stream=!0,n.streamUsage&&(s.stream_options={include_usage:!0},n.continuousUsage&&(s.stream_options.continuous_usage_stats=!0)));const f=X(l.offer,n,i,r);if(f.length>0){s.tools=f;const c=Y(e.tool_choice,n,i,r);c!==void 0&&(s.tool_choice=c),e.tool_choice?.disable_parallel_tool_use===!0&&n.parallelToolCalls&&(s.parallel_tool_calls=!1)}return Z(e,n,s,o?.listed===!0),b(e,n,i,s),{body:s,state:i}}m(ue,"request");function g(e,t){e.dropped.includes(t)||e.dropped.push(t)}m(g,"drop");function v(e,t,o){if(t.unknownFields==="error")throw new T(`${o}: is not supported by this translator`,{code:"unsupported_content",param:o});g(e,o)}m(v,"unsupported");function F(e,t){return e==null?"":(typeof e=="string"?[{type:"text",text:e}]:e).filter(n=>n.type==="text"&&!(t.stripBillingHeader&&n.text.trimStart().startsWith(K))).map(n=>n.text).join(`
1
+ var N=Object.defineProperty;var _=(e,t)=>N(e,"name",{value:t,configurable:!0});import{resolve as O}from"../../../../../../../lib/settings.mjs";import{TranslatorError as j}from"../../../../../../../lib/errors.mjs";import{createIdFitter as q,createNameFitter as B}from"../../../../../../../lib/ids.mjs";import{sanitize as C}from"../../../../../../../lib/schema.mjs";import{NOTE as A}from"../../../../../../../lib/images.mjs";import{expand as L}from"../../../../../../../lib/documents.mjs";import{createBudget as z,unsign as D}from"../../../../../../../lib/thinking.mjs";import{plan as P,scan as W}from"../../../../../../../lib/toolsearch.mjs";const H=new Set(["model","messages","system","max_tokens","stream","temperature","top_p","top_k","stop_sequences","tools","tool_choice","thinking","output_config","output_format","context_management"]),K=new Set(["metadata","cache_control","service_tier"]),R=new Set(["mcp_servers","container","inference_geo"]),U="x-anthropic-billing-header:";async function pe(e,t,o){const n=O(t);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new j("messages: Field required",{code:"invalid_request",param:"messages"});const s={front:"anthropic",model:e.model,stream:e.stream===!0,thinking:e.thinking?.type??null,reasoningField:n.reasoningField,toolNames:{},toolIds:{},dropped:[]};o?.producer!==void 0&&(s.producer=o.producer);const r=B({max:64,map:s.toolNames}),d=o?.toolCallIds?q({policy:o.toolCallIds,ids:G(e.messages),map:s.toolIds}):c=>c;for(const c of e.tools??[])(c.type===void 0||c.type==="custom")&&r(c.name);const m=W(e.messages,new Set((e.tools??[]).map(c=>c.name))),a=P(e.tools??[],m,n,e.tool_choice);s.toolSearch={mode:n.toolSearch,loaded:a.loaded,hidden:a.hidden,stubbed:a.stubbed,capped:m.capped};for(const c of Object.keys(e))if(!(H.has(c)||K.has(c)))if(R.has(c))x(s,c);else{if(n.unknownFields==="error")throw new j(`${c}: Extra inputs are not permitted`,{code:"unsupported_field",param:c});x(s,c)}const u={model:e.model},$=I(e.system,n),f=await J(e.messages,n,s,r,d,$,Q(e.context_management,s),o?.budget??z());u.messages=f.messages,f.prefill&&(u.continue_final_message=!0,u.add_generation_prompt=!1),e.max_tokens!==void 0&&(u[n.maxTokensField]=e.max_tokens),e.temperature!==void 0&&(u.temperature=e.temperature),e.top_p!==void 0&&(u.top_p=e.top_p),e.top_k!==void 0&&(n.topK?u.top_k=e.top_k:x(s,"top_k")),e.stop_sequences?.length>0&&(u.stop=e.stop_sequences),s.stream&&(u.stream=!0,n.streamUsage&&(u.stream_options={include_usage:!0},n.continuousUsage&&(u.stream_options.continuous_usage_stats=!0)));const h=b(a.offer,n,s,r);if(h.length>0){u.tools=h;const c=ee(e.tool_choice,n,s,r);c!==void 0&&(u.tool_choice=c),e.tool_choice?.disable_parallel_tool_use===!0&&n.parallelToolCalls&&(u.parallel_tool_calls=!1)}return te(e,n,u,o?.listed===!0),ne(e,n,s,u),{body:u,state:s}}_(pe,"request");function x(e,t){e.dropped.includes(t)||e.dropped.push(t)}_(x,"drop");function F(e,t,o){if(t.unknownFields==="error")throw new j(`${o}: is not supported by this translator`,{code:"unsupported_content",param:o});x(e,o)}_(F,"unsupported");function I(e,t){return e==null?"":(typeof e=="string"?[{type:"text",text:e}]:e).filter(n=>n.type==="text"&&!(t.stripBillingHeader&&n.text.trimStart().startsWith(U))).map(n=>n.text).join(`
2
2
 
3
- `)}m(F,"textOf");function j(e){return typeof e=="string"?[{type:"text",text:e}]:e??[]}m(j,"asBlocks");async function R(e,t,o,n,i,r,d){const l=[],s=[],h=new Map,k=t.inlineSystem==="auto"?"user":t.inlineSystem,f=new Map;if(t.reasoningRoundTrip){for(let a=e.length-1;a>=0;a--)if(e[a].role==="assistant")for(const u of[...j(e[a].content)].reverse())u.type==="thinking"&&f.set(u,await z(u.signature,{producer:o.producer,budget:d}))}const c=m(a=>{const u=l.at(-1);u?.role==="user"?u.parts.push(...a):l.push({role:"user",parts:a})},"pushUser"),w=m(async(a,u)=>{switch(a.type){case"text":return a.text===""?[]:[{type:"text",text:a.text}];case"image":return V(a,t,o);case"document":{const _=[];for(const p of await A(a,{settings:t,toolInputs:h,toolUseId:u}))_.push(...await w(p,u));return _}case"search_result":{const _=(a.content??[]).map(p=>p.text).join(`
4
- `);return[{type:"text",text:`[Search result: ${a.title??""} (${a.source??""})]
5
- ${_}`}]}case"tool_reference":return t.toolSearch==="native"&&t.deferredTools!=="drop"?[{type:"tool_reference",tool_name:a.tool_name}]:[{type:"text",text:o.toolSearch.loaded.includes(a.tool_name)?`[Tool loaded: ${a.tool_name}]`:`[Tool reference: ${a.tool_name}]`}];default:return v(o,t,`block:${a.type}`),[]}},"toParts");for(const a of e)if(a.role==="system"){const u=F(a.content,t);if(u===""||k==="drop")continue;if(k==="keep")l.push({role:"system",content:u});else if(k==="hoist")s.push(u);else{const _=u.trimStart().startsWith("<system-reminder>")?u:`<system-reminder>
6
- ${u}
7
- </system-reminder>`;c([{type:"text",text:_}])}}else if(a.role==="user"){const u=[],_=[];for(const y of j(a.content)){if(y.type!=="tool_result"){u.push(...await w(y,null));continue}const S=[];for(const x of j(y.content))S.push(...await w(x,y.tool_use_id));const M=S.filter(x=>x.type==="text").map(x=>x.text),E=S.filter(x=>x.type==="image_url");let $=S.some(x=>x.type==="tool_reference")?S.filter(x=>x.type!=="image_url"):M.join(`
8
- `);E.length>0&&(_.push({type:"text",text:`Image output of tool call ${y.tool_use_id}:`},...E),$===""&&($="(image output attached in the next message)")),y.is_error===!0&&t.toolErrors&&($=typeof $=="string"?`${t.toolErrorMarker}${$}`:[{type:"text",text:t.toolErrorMarker.trimEnd()},...$]),l.push({role:"tool",tool_call_id:y.tool_use_id,content:$})}const p=[..._,...u];p.length>0&&c(p)}else if(a.role==="assistant"){const u={role:"assistant",texts:[],parts:[],tool_calls:[]};for(const p of j(a.content))if(p.type==="text")p.text!==""&&u.texts.push(p.text);else if(p.type==="thinking"){const y=f.get(p)??null;y?u.parts.push({restored:y}):p.thinking&&u.parts.push({text:p.thinking})}else p.type==="redacted_thinking"?g(o,"redacted_thinking"):p.type==="tool_use"?(h.set(p.id,p.input),u.tool_calls.push({id:p.id,type:"function",function:{name:n(p.name),arguments:JSON.stringify(p.input??{})}})):v(o,t,`block:${p.type}`);const _=l.at(-1);_?.role==="assistant"?(_.texts.push(...u.texts),_.parts.push(...u.parts),_.tool_calls.push(...u.tool_calls)):l.push(u)}else throw new T(`messages: unsupported role "${a.role}"`,{code:"invalid_request",param:"messages"});return Q(l,t,i,s,r,o)}m(R,"convertMessages");function U(e,t){let o=1/0;for(const n of e?.edits??[])typeof n.type=="string"&&n.type.startsWith("clear_thinking")?n.keep==="all"?o=1/0:o=n.keep?.value??1:g(t,`context_management.${n.type}`);return o}m(U,"thinkingTurnsToKeep");function G(e,t){const o=e.find(s=>s.restored&&(s.restored.reasoning_content!==void 0||s.restored.reasoning!==void 0)),n=o?o.restored.reasoning_content!==void 0?"reasoning_content":"reasoning":t;let i="",r,d=null;for(const s of e){const h=s.restored?s.restored.reasoning_content??s.restored.reasoning:s.text;s.restored?.reasoning_details&&(r=[...r??[],...s.restored.reasoning_details]),h!==void 0&&(i+=(d!==null&&!(d.restored&&s.restored)?`
3
+ `)}_(I,"textOf");function T(e){return typeof e=="string"?[{type:"text",text:e}]:e??[]}_(T,"asBlocks");function G(e){const t=[];for(const o of e)for(const n of T(o.content))n.type==="tool_use"?t.push(n.id):n.type==="tool_result"&&t.push(n.tool_use_id);return t}_(G,"idsOf");async function J(e,t,o,n,s,r,d,m){const a=[],u=[],$=new Map,f=t.inlineSystem==="auto"?"user":t.inlineSystem,h=new Map;if(t.reasoningRoundTrip){for(let i=e.length-1;i>=0;i--)if(e[i].role==="assistant")for(const l of[...T(e[i].content)].reverse())l.type==="thinking"&&h.set(l,await D(l.signature,{producer:o.producer,budget:m}))}const c=_(i=>{const l=a.at(-1);l?.role==="user"?l.parts.push(...i):a.push({role:"user",parts:i})},"pushUser"),v=_(async(i,l)=>{switch(i.type){case"text":return i.text===""?[]:[{type:"text",text:i.text}];case"image":return Z(i,t,o);case"document":{const g=[];for(const p of await L(i,{settings:t,toolInputs:$,toolUseId:l}))g.push(...await v(p,l));return g}case"search_result":{const g=(i.content??[]).map(p=>p.text).join(`
4
+ `);return[{type:"text",text:`[Search result: ${i.title??""} (${i.source??""})]
5
+ ${g}`}]}case"tool_reference":return t.toolSearch==="native"&&t.deferredTools!=="drop"?[{type:"tool_reference",tool_name:i.tool_name}]:[{type:"text",text:o.toolSearch.loaded.includes(i.tool_name)?`[Tool loaded: ${i.tool_name}]`:`[Tool reference: ${i.tool_name}]`}];default:return F(o,t,`block:${i.type}`),[]}},"toParts");for(const i of e)if(i.role==="system"){const l=I(i.content,t);if(l===""||f==="drop")continue;if(f==="keep")a.push({role:"system",content:l});else if(f==="hoist")u.push(l);else{const g=l.trimStart().startsWith("<system-reminder>")?l:`<system-reminder>
6
+ ${l}
7
+ </system-reminder>`;c([{type:"text",text:g}])}}else if(i.role==="user"){const l=[],g=[];for(const k of T(i.content)){if(k.type!=="tool_result"){l.push(...await v(k,null));continue}const S=[];for(const y of T(k.content))S.push(...await v(y,k.tool_use_id));const M=S.filter(y=>y.type==="text").map(y=>y.text),E=S.filter(y=>y.type==="image_url");let w=S.some(y=>y.type==="tool_reference")?S.filter(y=>y.type!=="image_url"):M.join(`
8
+ `);E.length>0&&(g.push({type:"text",text:`Image output of tool call ${s(k.tool_use_id)}:`},...E),w===""&&(w="(image output attached in the next message)")),k.is_error===!0&&t.toolErrors&&(w=typeof w=="string"?`${t.toolErrorMarker}${w}`:[{type:"text",text:t.toolErrorMarker.trimEnd()},...w]),a.push({role:"tool",tool_call_id:s(k.tool_use_id),content:w})}const p=[...g,...l];p.length>0&&c(p)}else if(i.role==="assistant"){const l={role:"assistant",texts:[],parts:[],tool_calls:[]};for(const p of T(i.content))if(p.type==="text")p.text!==""&&l.texts.push(p.text);else if(p.type==="thinking"){const k=h.get(p)??null;k?l.parts.push({restored:k}):p.thinking&&l.parts.push({text:p.thinking})}else p.type==="redacted_thinking"?x(o,"redacted_thinking"):p.type==="tool_use"?($.set(p.id,p.input),l.tool_calls.push({id:s(p.id),type:"function",function:{name:n(p.name),arguments:JSON.stringify(p.input??{})}})):F(o,t,`block:${p.type}`);const g=a.at(-1);g?.role==="assistant"?(g.texts.push(...l.texts),g.parts.push(...l.parts),g.tool_calls.push(...l.tool_calls)):a.push(l)}else throw new j(`messages: unsupported role "${i.role}"`,{code:"invalid_request",param:"messages"});return Y(a,t,r,u,d,o)}_(J,"convertMessages");function Q(e,t){let o=1/0;for(const n of e?.edits??[])typeof n.type=="string"&&n.type.startsWith("clear_thinking")?n.keep==="all"?o=1/0:o=n.keep?.value??1:x(t,`context_management.${n.type}`);return o}_(Q,"thinkingTurnsToKeep");function V(e,t){const o=e.find(a=>a.restored&&(a.restored.reasoning_content!==void 0||a.restored.reasoning!==void 0)),n=o?o.restored.reasoning_content!==void 0?"reasoning_content":"reasoning":t;let s="",r,d=null;for(const a of e){const u=a.restored?a.restored.reasoning_content??a.restored.reasoning:a.text;a.restored?.reasoning_details&&(r=[...r??[],...a.restored.reasoning_details]),u!==void 0&&(s+=(d!==null&&!(d.restored&&a.restored)?`
9
9
 
10
- `:"")+h,d=s)}const l={};return i!==""&&(l[n]=i),r&&(l.reasoning_details=r),l}m(G,"reasoningFields");function J(e){return!!(e.reasoning_content||e.reasoning||e.reasoning_details)}m(J,"reasoningSent");function Q(e,t,o,n,i,r){let d=i;for(let f=e.length-1;f>=0;f--)e[f].role!=="assistant"||e[f].parts.length===0||(d>0?d--:e[f].parts=[]);const l=[],s=[o,...n].filter(f=>f!=="").join(`
10
+ `:"")+u,d=a)}const m={};return s!==""&&(m[n]=s),r&&(m.reasoning_details=r),m}_(V,"reasoningFields");function X(e){return!!(e.reasoning_content||e.reasoning||e.reasoning_details)}_(X,"reasoningSent");function Y(e,t,o,n,s,r){let d=s;for(let f=e.length-1;f>=0;f--)e[f].role!=="assistant"||e[f].parts.length===0||(d>0?d--:e[f].parts=[]);const m=[],a=[o,...n].filter(f=>f!=="").join(`
11
11
 
12
- `);s!==""&&l.push({role:"system",content:s});for(const f of e)if(f.role==="user"){const c=f.parts.every(w=>w.type==="text");l.push({role:"user",content:c?f.parts.map(w=>w.text).join(`
12
+ `);a!==""&&m.push({role:"system",content:a});for(const f of e)if(f.role==="user"){const h=f.parts.every(c=>c.type==="text");m.push({role:"user",content:h?f.parts.map(c=>c.text).join(`
13
13
 
14
- `):f.parts})}else if(f.role==="assistant"){const c={role:"assistant",content:f.texts.join(`
14
+ `):f.parts})}else if(f.role==="assistant"){const h={role:"assistant",content:f.texts.join(`
15
15
 
16
- `)};f.parts.length>0&&Object.assign(c,G(f.parts,t.reasoningField)),t.reasoningRoundTrip&&f.tool_calls.length>0&&!J(c)&&(r.reasoningMissing=(r.reasoningMissing??0)+1),f.tool_calls.length>0&&(c.tool_calls=f.tool_calls),l.push(c)}else l.push(f);const h=l.at(-1),k=t.prefill==="continue_final_message"&&h?.role==="assistant"&&!h.tool_calls&&h.content!=="";return{messages:l,prefill:k}}m(Q,"finalize");function V(e,t,o){if(!t.images)return g(o,"image"),[{type:"text",text:I}];const n=e.source;return n.type==="base64"?[{type:"image_url",image_url:{url:`data:${n.media_type};base64,${n.data}`}}]:n.type==="url"?[{type:"image_url",image_url:{url:n.url}}]:(g(o,"image.file"),[{type:"text",text:"[An image supplied as an uploaded file could not be forwarded.]"}])}m(V,"imageParts");function X(e,t,o,n){const i=[];for(const r of e){if(r.type!==void 0&&r.type!=="custom"){g(o,`tool:${r.name}`);continue}if(r.defer_loading===!0&&t.deferredTools==="drop"){g(o,"deferred_tools");continue}const d=r.input_schema??{type:"object",properties:{}},l={name:n(r.name),parameters:t.toolSchema==="sanitize"?B(d):d};r.description!==void 0&&(l.description=r.description),r.strict!==void 0&&(l.strict=r.strict),i.push(r.defer_loading===!0&&t.toolSearch==="native"?{type:"function",function:l,defer_loading:!0}:{type:"function",function:l})}return i}m(X,"convertTools");function Y(e,t,o,n){switch(e?.type){case void 0:return;case"auto":return"auto";case"any":return"required";case"none":return"none";case"tool":return{type:"function",function:{name:n(e.name)}};default:v(o,t,`tool_choice:${e.type}`);return}}m(Y,"convertToolChoice");function Z(e,t,o,n){const i=e.thinking,r=i?.type;let d;r==="disabled"?(t.thinking==="reasoning_effort"&&(d="none"),t.thinking==="chat_template_kwargs"&&(o.chat_template_kwargs={enable_thinking:!1})):(t.effort&&e.output_config?.effort!==void 0&&(d=e.output_config.effort),r==="enabled"&&(t.thinking==="chat_template_kwargs"&&(o.chat_template_kwargs={enable_thinking:!0},t.thinkingBudget&&i.budget_tokens!==void 0&&e.max_tokens!==void 0&&(o.thinking_token_budget=Math.max(0,Math.min(i.budget_tokens,e.max_tokens-1)))),t.thinking==="reasoning_effort"&&d===void 0&&!n&&(d="medium"))),d!==void 0&&(o.reasoning_effort=d)}m(Z,"applyThinking");function b(e,t,o,n){const i=e.output_config?.format??e.output_format;i!==void 0&&(t.responseFormat&&i.type==="json_schema"?n.response_format={type:"json_schema",json_schema:{name:"output",schema:i.schema}}:g(o,"output_config.format"));for(const r of Object.keys(e.output_config??{}))r!=="effort"&&r!=="format"&&g(o,`output_config.${r}`)}m(b,"applyOutputConfig");export{ue as request};
16
+ `)};f.parts.length>0&&Object.assign(h,V(f.parts,t.reasoningField)),t.reasoningRoundTrip&&f.tool_calls.length>0&&!X(h)&&(r.reasoningMissing=(r.reasoningMissing??0)+1),f.tool_calls.length>0&&(h.tool_calls=f.tool_calls),m.push(h)}else m.push(f);const u=m.at(-1),$=t.prefill==="continue_final_message"&&u?.role==="assistant"&&!u.tool_calls&&u.content!=="";return{messages:m,prefill:$}}_(Y,"finalize");function Z(e,t,o){if(!t.images)return x(o,"image"),[{type:"text",text:A}];const n=e.source;return n.type==="base64"?[{type:"image_url",image_url:{url:`data:${n.media_type};base64,${n.data}`}}]:n.type==="url"?[{type:"image_url",image_url:{url:n.url}}]:(x(o,"image.file"),[{type:"text",text:"[An image supplied as an uploaded file could not be forwarded.]"}])}_(Z,"imageParts");function b(e,t,o,n){const s=[];for(const r of e){if(r.type!==void 0&&r.type!=="custom"){x(o,`tool:${r.name}`);continue}if(r.defer_loading===!0&&t.deferredTools==="drop"){x(o,"deferred_tools");continue}const d=r.input_schema??{type:"object",properties:{}},m={name:n(r.name),parameters:t.toolSchema==="sanitize"?C(d):d};r.description!==void 0&&(m.description=r.description),r.strict!==void 0&&(m.strict=r.strict),s.push(r.defer_loading===!0&&t.toolSearch==="native"?{type:"function",function:m,defer_loading:!0}:{type:"function",function:m})}return s}_(b,"convertTools");function ee(e,t,o,n){switch(e?.type){case void 0:return;case"auto":return"auto";case"any":return"required";case"none":return"none";case"tool":return{type:"function",function:{name:n(e.name)}};default:F(o,t,`tool_choice:${e.type}`);return}}_(ee,"convertToolChoice");function te(e,t,o,n){const s=e.thinking,r=s?.type;let d;r==="disabled"?(t.thinking==="reasoning_effort"&&(d="none"),t.thinking==="chat_template_kwargs"&&(o.chat_template_kwargs={enable_thinking:!1})):(t.effort&&e.output_config?.effort!==void 0&&(d=e.output_config.effort),r==="enabled"&&(t.thinking==="chat_template_kwargs"&&(o.chat_template_kwargs={enable_thinking:!0},t.thinkingBudget&&s.budget_tokens!==void 0&&e.max_tokens!==void 0&&(o.thinking_token_budget=Math.max(0,Math.min(s.budget_tokens,e.max_tokens-1)))),t.thinking==="reasoning_effort"&&d===void 0&&!n&&(d="medium"))),d!==void 0&&(o.reasoning_effort=d)}_(te,"applyThinking");function ne(e,t,o,n){const s=e.output_config?.format??e.output_format;s!==void 0&&(t.responseFormat&&s.type==="json_schema"?n.response_format={type:"json_schema",json_schema:{name:"output",schema:s.schema}}:x(o,"output_config.format"));for(const r of Object.keys(e.output_config??{}))r!=="effort"&&r!=="format"&&x(o,`output_config.${r}`)}_(ne,"applyOutputConfig");export{pe as request};
17
17
  //# sourceMappingURL=request.mjs.map
@@ -1,2 +1,2 @@
1
- var m=Object.defineProperty;var h=(o,e)=>m(o,"name",{value:e,configurable:!0});import{resolve as _}from"../../../../../../../lib/settings.mjs";import{restoreName as x}from"../../../../../../../lib/ids.mjs";import{detailOf as y}from"../../../../../../../lib/thinking.mjs";import{toOpenAIFinish as N,toOpenAIUsage as O}from"../../../../../../../lib/finish.mjs";function A(o,e,d){const c=_(d),g=e?.toolNames??{},s=[],l=[],n=[],r=[];let p=0,f=!1;for(const t of o.content??[]){if(c.reasoningRoundTrip){const u=y(t,p+(f?1:0));u&&r.push(u)}t.type==="text"&&t.text!==""&&(f=!0),t.type==="tool_use"&&p++,t.type==="text"?s.push(t.text):t.type==="thinking"?l.push(t.thinking):t.type==="tool_use"&&n.push({id:t.id,type:"function",function:{name:x(g,t.name),arguments:JSON.stringify(t.input??{})}})}const i={role:"assistant",content:s.length===0&&n.length>0?null:s.join("")};l.length>0&&(i[c.reasoningField]=l.join("")),r.length>0&&(i.reasoning_details=r),n.length>0&&(i.tool_calls=n);const a={index:0,message:i,finish_reason:N(o.stop_reason)};return typeof o.stop_sequence=="string"&&(a.stop_reason=o.stop_sequence),e&&(e.upstreamUsage=o.usage),{id:`chatcmpl-${String(o.id).replace(/^msg_/,"")}`,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:e?.model??o.model,choices:[a],usage:O(o.usage)}}h(A,"response");export{A as response};
1
+ var _=Object.defineProperty;var d=(o,e)=>_(o,"name",{value:e,configurable:!0});import{resolve as x}from"../../../../../../../lib/settings.mjs";import{restoreId as y,restoreName as I}from"../../../../../../../lib/ids.mjs";import{detailOf as N}from"../../../../../../../lib/thinking.mjs";import{toOpenAIFinish as O,toOpenAIUsage as j}from"../../../../../../../lib/finish.mjs";function F(o,e,h){const c=x(h),g=e?.toolNames??{},m=e?.toolIds??{},s=[],l=[],n=[],r=[];let p=0,f=!1;for(const t of o.content??[]){if(c.reasoningRoundTrip){const u=N(t,p+(f?1:0));u&&r.push(u)}t.type==="text"&&t.text!==""&&(f=!0),t.type==="tool_use"&&p++,t.type==="text"?s.push(t.text):t.type==="thinking"?l.push(t.thinking):t.type==="tool_use"&&n.push({id:y(m,t.id),type:"function",function:{name:I(g,t.name),arguments:JSON.stringify(t.input??{})}})}const i={role:"assistant",content:s.length===0&&n.length>0?null:s.join("")};l.length>0&&(i[c.reasoningField]=l.join("")),r.length>0&&(i.reasoning_details=r),n.length>0&&(i.tool_calls=n);const a={index:0,message:i,finish_reason:O(o.stop_reason)};return typeof o.stop_sequence=="string"&&(a.stop_reason=o.stop_sequence),e&&(e.upstreamUsage=o.usage),{id:`chatcmpl-${String(o.id).replace(/^msg_/,"")}`,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:e?.model??o.model,choices:[a],usage:j(o.usage)}}d(F,"response");export{F as response};
2
2
  //# sourceMappingURL=response.mjs.map
@@ -1,2 +1,2 @@
1
- var O=Object.defineProperty;var r=(d,o)=>O(d,"name",{value:o,configurable:!0});import{resolve as N}from"../../../../../../../lib/settings.mjs";import{anthropicToOpenAIType as U,anthropicTypeToStatus as j}from"../../../../../../../lib/errors.mjs";import{completionId as D,restoreName as T}from"../../../../../../../lib/ids.mjs";import{toOpenAIFinish as E,toOpenAIUsage as I}from"../../../../../../../lib/finish.mjs";import{messages as w}from"../../../../../../../lib/sse.mjs";import{detailOf as A}from"../../../../../../../lib/thinking.mjs";async function*R(d,o,k){const u=N(k),x=o?.toolNames??{},b=o?.includeUsage??!0,y=Math.floor(Date.now()/1e3),s=new Map;let c=D(),p=o?.model,a={},g=!1,n=null,_=!1,h=0;const i=r((e,t=null,l={})=>({data:{id:c,object:"chat.completion.chunk",created:y,model:p,choices:[{index:0,delta:e,finish_reason:t,...l}]}}),"chunk"),f=r((e,t)=>({data:{error:{message:t,type:U(e),param:null,code:j(e)}}}),"failure");try{for await(const{data:e}of w(d))if(e.type==="message_start")c=`chatcmpl-${String(e.message.id).replace(/^msg_/,"")}`,p=o?.model??e.message.model,a={...e.message.usage},o&&(o.upstreamUsage=a),yield i({role:"assistant",content:""});else if(e.type==="content_block_start"){const t=e.content_block;if((t.type==="thinking"||t.type==="redacted_thinking")&&(n={type:t.type,data:t.data,thinking:"",signature:"",slot:h+(_?1:0)}),t.type==="tool_use"&&h++,t.type==="tool_use"){s.set(e.index,s.size);const l=T(x,t.name);yield i({tool_calls:[{index:s.get(e.index),id:t.id,type:"function",function:{name:l,arguments:""}}]})}}else if(e.type==="content_block_delta"){const t=e.delta;t.type==="text_delta"?(t.text!==""&&(_=!0),yield i({content:t.text})):t.type==="thinking_delta"?(n&&(n.thinking+=t.thinking),yield i({[u.reasoningField]:t.thinking})):t.type==="signature_delta"?n&&(n.signature+=t.signature):t.type==="input_json_delta"&&s.has(e.index)&&(yield i({tool_calls:[{index:s.get(e.index),function:{arguments:t.partial_json}}]}))}else if(e.type==="content_block_stop"){const t=u.reasoningRoundTrip&&n?A(n,n.slot):null;t&&(yield i({reasoning_details:[t]})),n=null}else if(e.type==="message_delta"){for(const[l,m]of Object.entries(e.usage??{}))m!=null&&(a[l]=m);o&&(o.upstreamUsage=a);const t=typeof e.delta?.stop_sequence=="string"?{stop_reason:e.delta.stop_sequence}:{};yield i({},E(e.delta?.stop_reason),t)}else if(e.type==="message_stop"){g=!0;break}else if(e.type==="error"){yield f(e.error.type,e.error.message),yield{data:"[DONE]"};return}}catch(e){yield f(e.type??"api_error",e.message),yield{data:"[DONE]"};return}if(!g){yield f("api_error","Upstream stream ended before completion"),yield{data:"[DONE]"};return}b&&(yield{data:{id:c,object:"chat.completion.chunk",created:y,model:p,choices:[],usage:I(a)}}),yield{data:"[DONE]"}}r(R,"stream");export{R as stream};
1
+ var I=Object.defineProperty;var r=(d,o)=>I(d,"name",{value:o,configurable:!0});import{resolve as N}from"../../../../../../../lib/settings.mjs";import{anthropicToOpenAIType as U,anthropicTypeToStatus as j}from"../../../../../../../lib/errors.mjs";import{completionId as D,restoreId as T,restoreName as E}from"../../../../../../../lib/ids.mjs";import{toOpenAIFinish as w,toOpenAIUsage as A}from"../../../../../../../lib/finish.mjs";import{messages as B}from"../../../../../../../lib/sse.mjs";import{detailOf as q}from"../../../../../../../lib/thinking.mjs";async function*C(d,o,k){const u=N(k),x=o?.toolNames??{},b=o?.toolIds??{},O=o?.includeUsage??!0,y=Math.floor(Date.now()/1e3),s=new Map;let c=D(),p=o?.model,a={},g=!1,n=null,_=!1,h=0;const i=r((e,t=null,l={})=>({data:{id:c,object:"chat.completion.chunk",created:y,model:p,choices:[{index:0,delta:e,finish_reason:t,...l}]}}),"chunk"),f=r((e,t)=>({data:{error:{message:t,type:U(e),param:null,code:j(e)}}}),"failure");try{for await(const{data:e}of B(d))if(e.type==="message_start")c=`chatcmpl-${String(e.message.id).replace(/^msg_/,"")}`,p=o?.model??e.message.model,a={...e.message.usage},o&&(o.upstreamUsage=a),yield i({role:"assistant",content:""});else if(e.type==="content_block_start"){const t=e.content_block;if((t.type==="thinking"||t.type==="redacted_thinking")&&(n={type:t.type,data:t.data,thinking:"",signature:"",slot:h+(_?1:0)}),t.type==="tool_use"&&h++,t.type==="tool_use"){s.set(e.index,s.size);const l=E(x,t.name);yield i({tool_calls:[{index:s.get(e.index),id:T(b,t.id),type:"function",function:{name:l,arguments:""}}]})}}else if(e.type==="content_block_delta"){const t=e.delta;t.type==="text_delta"?(t.text!==""&&(_=!0),yield i({content:t.text})):t.type==="thinking_delta"?(n&&(n.thinking+=t.thinking),yield i({[u.reasoningField]:t.thinking})):t.type==="signature_delta"?n&&(n.signature+=t.signature):t.type==="input_json_delta"&&s.has(e.index)&&(yield i({tool_calls:[{index:s.get(e.index),function:{arguments:t.partial_json}}]}))}else if(e.type==="content_block_stop"){const t=u.reasoningRoundTrip&&n?q(n,n.slot):null;t&&(yield i({reasoning_details:[t]})),n=null}else if(e.type==="message_delta"){for(const[l,m]of Object.entries(e.usage??{}))m!=null&&(a[l]=m);o&&(o.upstreamUsage=a);const t=typeof e.delta?.stop_sequence=="string"?{stop_reason:e.delta.stop_sequence}:{};yield i({},w(e.delta?.stop_reason),t)}else if(e.type==="message_stop"){g=!0;break}else if(e.type==="error"){yield f(e.error.type,e.error.message),yield{data:"[DONE]"};return}}catch(e){yield f(e.type??"api_error",e.message),yield{data:"[DONE]"};return}if(!g){yield f("api_error","Upstream stream ended before completion"),yield{data:"[DONE]"};return}O&&(yield{data:{id:c,object:"chat.completion.chunk",created:y,model:p,choices:[],usage:A(a)}}),yield{data:"[DONE]"}}r(C,"stream");export{C as stream};
2
2
  //# sourceMappingURL=stream.mjs.map
@@ -1,8 +1,8 @@
1
- var v=Object.defineProperty;var f=(e,r)=>v(e,"name",{value:r,configurable:!0});import{resolve as N}from"../../../../../../../lib/settings.mjs";import{TranslatorError as b}from"../../../../../../../lib/errors.mjs";import{anthropicToolId as w,createNameFitter as O}from"../../../../../../../lib/ids.mjs";import{parseArguments as S}from"../../../../../../../lib/arguments.mjs";import{sanitize as q}from"../../../../../../../lib/schema.mjs";import{blocksOf as E}from"../../../../../../../lib/thinking.mjs";const I=new Set(["model","messages","max_tokens","max_completion_tokens","temperature","top_p","top_k","stop","stream","stream_options","tools","tool_choice","parallel_tool_calls","reasoning_effort","response_format","user","n"]),$=new Set(["metadata","store","service_tier"]),x=/^data:([^;,]+);base64,(.*)$/s;async function P(e,r){const i=N(r);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new b("messages is a required property",{code:"invalid_request",param:"messages"});if(e.n!==void 0&&e.n!==1)throw new b("n: only a single choice (n = 1) is supported",{code:"unsupported_field",param:"n"});const o={front:"openai",model:e.model,stream:e.stream===!0,includeUsage:e.stream_options?.include_usage===!0,reasoningField:i.reasoningField,toolNames:{},dropped:[]},a=O({max:128,map:o.toolNames});for(const t of Object.keys(e))if(!(I.has(t)||$.has(t))){if(i.unknownFields==="error")throw new b(`${t}: Unrecognized request argument`,{code:"unsupported_field",param:t});g(o,t)}const c=[],p=[],m=f((t,s)=>{const l=p.at(-1);l?.role===t?l.blocks.push(...s):p.push({role:t,blocks:[...s]})},"push");for(const t of e.messages)switch(t.role){case"system":case"developer":{const s=j(t.content);s!==""&&c.push(s);break}case"user":m("user",F(t.content,o));break;case"assistant":{const s=i.reasoningRoundTrip?E(t.reasoning_details):[],l=[],u=j(t.content);u!==""?l.push({type:"text",text:u}):typeof t.refusal=="string"&&t.refusal!==""&&l.push({type:"text",text:t.refusal}),M(t)&&!s.some(d=>d.block.type==="thinking")&&g(o,"reasoning");for(const d of t.tool_calls??[])l.push({type:"tool_use",id:w(d.id),name:a(d.function.name),input:S(d.function.arguments)});const k=A(s,l);k.length>0&&m("assistant",k);break}case"tool":{const s={type:"tool_result",tool_use_id:w(t.tool_call_id),content:j(t.content)};if(i.toolErrorInference){const u=i.toolErrorMarker.trimEnd();s.content.startsWith(u)&&(s.is_error=!0,s.content=s.content.slice(u.length).replace(/^\s/,""))}const l=p.at(-1);if(l?.role==="user"){const u=l.blocks.findIndex(k=>k.type!=="tool_result");l.blocks.splice(u===-1?l.blocks.length:u,0,s)}else p.push({role:"user",blocks:[s]});break}default:throw new b(`messages: unsupported role "${t.role}"`,{code:"invalid_request",param:"messages"})}const n={model:e.model,max_tokens:e.max_completion_tokens??e.max_tokens??i.defaultMaxTokens};c.length>0&&(n.system=c.join(`
1
+ var v=Object.defineProperty;var u=(e,s)=>v(e,"name",{value:s,configurable:!0});import{resolve as N}from"../../../../../../../lib/settings.mjs";import{TranslatorError as h}from"../../../../../../../lib/errors.mjs";import{anthropicToolId as O,createIdFitter as S,createNameFitter as q}from"../../../../../../../lib/ids.mjs";import{parseArguments as E}from"../../../../../../../lib/arguments.mjs";import{sanitize as $}from"../../../../../../../lib/schema.mjs";import{blocksOf as A}from"../../../../../../../lib/thinking.mjs";const F=new Set(["model","messages","max_tokens","max_completion_tokens","temperature","top_p","top_k","stop","stream","stream_options","tools","tool_choice","parallel_tool_calls","reasoning_effort","response_format","user","n"]),M=new Set(["metadata","store","service_tier"]),j=/^data:([^;,]+);base64,(.*)$/s;async function Q(e,s,a){const o=N(s);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new h("messages is a required property",{code:"invalid_request",param:"messages"});if(e.n!==void 0&&e.n!==1)throw new h("n: only a single choice (n = 1) is supported",{code:"unsupported_field",param:"n"});const r={front:"openai",model:e.model,stream:e.stream===!0,includeUsage:e.stream_options?.include_usage===!0,reasoningField:o.reasoningField,toolNames:{},toolIds:{},dropped:[]},m=q({max:128,map:r.toolNames}),f=a?.toolCallIds?S({policy:a.toolCallIds,ids:C(e.messages),map:r.toolIds}):O;for(const t of Object.keys(e))if(!(F.has(t)||M.has(t))){if(o.unknownFields==="error")throw new h(`${t}: Unrecognized request argument`,{code:"unsupported_field",param:t});k(r,t)}const c=[],_=[],I=u((t,n)=>{const l=_.at(-1);l?.role===t?l.blocks.push(...n):_.push({role:t,blocks:[...n]})},"push");for(const t of e.messages)switch(t.role){case"system":case"developer":{const n=w(t.content);n!==""&&c.push(n);break}case"user":I("user",z(t.content,r));break;case"assistant":{const n=o.reasoningRoundTrip?A(t.reasoning_details):[],l=[],p=w(t.content);p!==""?l.push({type:"text",text:p}):typeof t.refusal=="string"&&t.refusal!==""&&l.push({type:"text",text:t.refusal}),U(t)&&!n.some(g=>g.block.type==="thinking")&&k(r,"reasoning");for(const g of t.tool_calls??[])l.push({type:"tool_use",id:f(g.id),name:m(g.function.name),input:E(g.function.arguments)});const b=R(n,l);b.length>0&&I("assistant",b);break}case"tool":{const n={type:"tool_result",tool_use_id:f(t.tool_call_id),content:w(t.content)};if(o.toolErrorInference){const p=o.toolErrorMarker.trimEnd();n.content.startsWith(p)&&(n.is_error=!0,n.content=n.content.slice(p.length).replace(/^\s/,""))}const l=_.at(-1);if(l?.role==="user"){const p=l.blocks.findIndex(b=>b.type!=="tool_result");l.blocks.splice(p===-1?l.blocks.length:p,0,n)}else _.push({role:"user",blocks:[n]});break}default:throw new h(`messages: unsupported role "${t.role}"`,{code:"invalid_request",param:"messages"})}const i={model:e.model,max_tokens:e.max_completion_tokens??e.max_tokens??o.defaultMaxTokens};c.length>0&&(i.system=c.join(`
2
2
 
3
- `)),n.messages=p.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 y=[];for(const t of e.tools??[]){if(t.type!=="function"){g(o,`tool:${t.type}`);continue}const s=t.function,l=s.parameters??{type:"object",properties:{}},u={name:a(s.name),input_schema:i.toolSchema==="sanitize"?q(l):l};s.description!==void 0&&(u.description=s.description),s.strict!==void 0&&(u.strict=s.strict),y.push(u)}if(y.length>0){n.tools=y;let t=R(e.tool_choice,a);e.parallel_tool_calls===!1&&t.type!=="none"&&(t={...t,disable_parallel_tool_use:!0}),(e.tool_choice!==void 0||e.parallel_tool_calls===!1)&&(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 h=e.response_format;return h?.type==="json_schema"?n.output_config={...n.output_config,format:{type:"json_schema",schema:h.json_schema.schema}}:h?.type==="json_object"&&(i.jsonObject==="schema"?(n.output_config={...n.output_config,format:{type:"json_schema",schema:{type:"object"}}},o.jsonSchemaEmulated=!0):i.jsonObject==="instruction"?n.system=n.system===void 0?T:`${n.system}
3
+ `)),i.messages=_.map(t=>({role:t.role,content:t.blocks})),e.temperature!==void 0&&(i.temperature=Math.min(1,e.temperature)),e.top_p!==void 0&&(i.top_p=e.top_p),e.top_k!==void 0&&(i.top_k=e.top_k),e.stop!==void 0&&e.stop!==null&&(i.stop_sequences=[].concat(e.stop)),r.stream&&(i.stream=!0),typeof e.user=="string"&&(i.metadata={user_id:e.user});const y=[];for(const t of e.tools??[]){if(t.type!=="function"){k(r,`tool:${t.type}`);continue}const n=t.function,l=n.parameters??{type:"object",properties:{}},p={name:m(n.name),input_schema:o.toolSchema==="sanitize"?$(l):l};n.description!==void 0&&(p.description=n.description),n.strict!==void 0&&(p.strict=n.strict),y.push(p)}if(y.length>0){i.tools=y;let t=D(e.tool_choice,m);e.parallel_tool_calls===!1&&t.type!=="none"&&(t={...t,disable_parallel_tool_use:!0}),(e.tool_choice!==void 0||e.parallel_tool_calls===!1)&&(i.tool_choice=t)}const d=e.reasoning_effort;d==="none"?i.thinking={type:"disabled"}:d!=null&&(i.thinking={type:"adaptive"},i.output_config={effort:d==="minimal"?"low":d});const x=e.response_format;return x?.type==="json_schema"?i.output_config={...i.output_config,format:{type:"json_schema",schema:x.json_schema.schema}}:x?.type==="json_object"&&(o.jsonObject==="schema"?(i.output_config={...i.output_config,format:{type:"json_schema",schema:{type:"object"}}},r.jsonSchemaEmulated=!0):o.jsonObject==="instruction"?i.system=i.system===void 0?T:`${i.system}
4
4
 
5
- ${T}`:g(o,"response_format")),{body:n,state:o}}f(P,"request");const T="Respond with a single valid JSON object and nothing else.";function g(e,r){e.dropped.includes(r)||e.dropped.push(r)}f(g,"drop");function A(e,r){const i=[...e].sort((p,m)=>p.at-m.at),o=[];let a=0;const c=r.findIndex(p=>p.type==="tool_use");for(const p of i){const m=p.at<=1&&c!==-1?Math.min(p.at,c):p.at;for(;a<Math.min(m,r.length);)o.push(r[a++]);o.push(p.block)}return[...o,...r.slice(a)]}f(A,"interleave");function M(e){return!!(e.reasoning||e.reasoning_content)}f(M,"reasoningPresent");function j(e){return typeof e=="string"?e:(e??[]).filter(r=>r.type==="text").map(r=>r.text).join(`
5
+ ${T}`:k(r,"response_format")),{body:i,state:r}}u(Q,"request");const T="Respond with a single valid JSON object and nothing else.";function C(e){const s=[];for(const a of e)if(a.role==="assistant")for(const o of a.tool_calls??[])s.push(o.id);else a.role==="tool"&&s.push(a.tool_call_id);return s}u(C,"idsOf");function k(e,s){e.dropped.includes(s)||e.dropped.push(s)}u(k,"drop");function R(e,s){const a=[...e].sort((f,c)=>f.at-c.at),o=[];let r=0;const m=s.findIndex(f=>f.type==="tool_use");for(const f of a){const c=f.at<=1&&m!==-1?Math.min(f.at,m):f.at;for(;r<Math.min(c,s.length);)o.push(s[r++]);o.push(f.block)}return[...o,...s.slice(r)]}u(R,"interleave");function U(e){return!!(e.reasoning||e.reasoning_content)}u(U,"reasoningPresent");function w(e){return typeof e=="string"?e:(e??[]).filter(s=>s.type==="text").map(s=>s.text).join(`
6
6
 
7
- `)}f(j,"plainText");function F(e,r){if(typeof e=="string")return e===""?[]:[{type:"text",text:e}];const i=[];for(const o of e??[])if(o.type==="text")i.push({type:"text",text:o.text});else if(o.type==="image_url"){const a=x.exec(o.image_url.url);i.push({type:"image",source:a?{type:"base64",media_type:a[1],data:a[2]}:{type:"url",url:o.image_url.url}})}else if(o.type==="file"&&typeof o.file?.file_data=="string"&&x.test(o.file.file_data)){const a=x.exec(o.file.file_data);i.push({type:"document",source:{type:"base64",media_type:a[1],data:a[2]}})}else g(r,`content:${o.type}`);return i}f(F,"userBlocks");function R(e,r){return e==="none"?{type:"none"}:e==="required"?{type:"any"}:typeof e=="object"&&e!==null?{type:"tool",name:r(e.function.name)}:{type:"auto"}}f(R,"toolChoice");export{P as request};
7
+ `)}u(w,"plainText");function z(e,s){if(typeof e=="string")return e===""?[]:[{type:"text",text:e}];const a=[];for(const o of e??[])if(o.type==="text")a.push({type:"text",text:o.text});else if(o.type==="image_url"){const r=j.exec(o.image_url.url);a.push({type:"image",source:r?{type:"base64",media_type:r[1],data:r[2]}:{type:"url",url:o.image_url.url}})}else if(o.type==="file"&&typeof o.file?.file_data=="string"&&j.test(o.file.file_data)){const r=j.exec(o.file.file_data);a.push({type:"document",source:{type:"base64",media_type:r[1],data:r[2]}})}else k(s,`content:${o.type}`);return a}u(z,"userBlocks");function D(e,s){return e==="none"?{type:"none"}:e==="required"?{type:"any"}:typeof e=="object"&&e!==null?{type:"tool",name:s(e.function.name)}:{type:"auto"}}u(D,"toolChoice");export{Q as request};
8
8
  //# sourceMappingURL=request.mjs.map
@@ -1,2 +1,2 @@
1
- var y=Object.defineProperty;var c=(t,n)=>y(t,"name",{value:n,configurable:!0});import{resolve as _}from"../../../../../../../lib/settings.mjs";import{TranslatorError as m}from"../../../../../../../lib/errors.mjs";import{messageId as k,anthropicToolId as T,restoreName as x}from"../../../../../../../lib/ids.mjs";import{sign as A,reasoningOf as w,reasoningKey as C,detailsText as N,splitThink as O}from"../../../../../../../lib/thinking.mjs";import{isComplete as S,parseArguments as U}from"../../../../../../../lib/arguments.mjs";import{finishError as v,toAnthropicStop as E,toAnthropicUsage as I}from"../../../../../../../lib/finish.mjs";function D(t,n,u){const f=_(u),l=t?.choices?.[0];if(!l)throw new m("Upstream response contained no choices",{code:"invalid_upstream_response",status:502});const g=v(l.finish_reason);if(g)throw new m(g.message,{code:g.code,status:g.status,type:g.type});const e=l.message??{},i=[];let a=w(e),s=typeof e.content=="string"?e.content:"";const o={};if(f.reasoningRoundTrip){const r=C(e);r&&(o[r]=e[r]),Array.isArray(e.reasoning_details)&&e.reasoning_details.length>0&&(o.reasoning_details=e.reasoning_details)}a===""&&f.thinkTags&&({reasoning:a,text:s}=O(s,f.thinkOpen)),a===""&&o.reasoning_details&&(a=N(o.reasoning_details)),(a!==""||o.reasoning_details)&&(i.push({type:"thinking",thinking:a,signature:A(Object.keys(o).length>0?o:void 0,n?.producer)}),s=s.trimStart()),s===""&&typeof e.refusal=="string"&&(s=e.refusal),s!==""&&i.push({type:"text",text:s});const h=n?.toolNames??{},p=e.tool_calls??[];for(const r of p)i.push({type:"tool_use",id:T(r.id),name:x(h,r.function.name),input:U(r.function.arguments)});i.length===0&&i.push({type:"text",text:""});const d=E(l.finish_reason,{hasToolCalls:p.length>0,matchedStop:l.stop_reason,refusal:e.refusal,argumentsComplete:p.length===0||S(p.at(-1).function.arguments)});return n&&t.usage&&(n.upstreamUsage=t.usage),{id:k(),type:"message",role:"assistant",model:n?.model??t.model,content:i,...d,usage:I(t.usage)}}c(D,"response");export{D as response};
1
+ var _=Object.defineProperty;var p=(o,t)=>_(o,"name",{value:t,configurable:!0});import{resolve as k}from"../../../../../../../lib/settings.mjs";import{TranslatorError as u}from"../../../../../../../lib/errors.mjs";import{messageId as T,anthropicToolId as x,restoreName as A}from"../../../../../../../lib/ids.mjs";import{sign as w,reasoningOf as C,reasoningKey as I,detailsText as N,splitThink as O}from"../../../../../../../lib/thinking.mjs";import{isComplete as S,parseArguments as U}from"../../../../../../../lib/arguments.mjs";import{filterCategory as v,finishError as E,toAnthropicStop as b,toAnthropicUsage as j}from"../../../../../../../lib/finish.mjs";function G(o,t,m){const c=k(m),i=o?.choices?.[0];if(!i)throw new u("Upstream response contained no choices",{code:"invalid_upstream_response",status:502});const g=E(i.finish_reason);if(g)throw new u(g.message,{code:g.code,status:g.status,type:g.type});const e=i.message??{},a=[];let l=C(e),s=typeof e.content=="string"?e.content:"";const n={};if(c.reasoningRoundTrip){const r=I(e);r&&(n[r]=e[r]),Array.isArray(e.reasoning_details)&&e.reasoning_details.length>0&&(n.reasoning_details=e.reasoning_details)}l===""&&c.thinkTags&&({reasoning:l,text:s}=O(s,c.thinkOpen)),l===""&&n.reasoning_details&&(l=N(n.reasoning_details)),(l!==""||n.reasoning_details)&&(a.push({type:"thinking",thinking:l,signature:w(Object.keys(n).length>0?n:void 0,t?.producer)}),s=s.trimStart()),s===""&&typeof e.refusal=="string"&&(s=e.refusal),s!==""&&a.push({type:"text",text:s});const d=t?.toolNames??{},h=t?.toolIds??{},f=e.tool_calls??[];for(const r of f)a.push({type:"tool_use",id:x(r.id,h),name:A(d,r.function.name),input:U(r.function.arguments)});a.length===0&&a.push({type:"text",text:""});const y=b(i.finish_reason,{hasToolCalls:f.length>0,matchedStop:i.stop_reason,refusal:e.refusal,category:v(i.content_filter_results),argumentsComplete:f.length===0||S(f.at(-1).function.arguments)});return t&&o.usage&&(t.upstreamUsage=o.usage),{id:T(),type:"message",role:"assistant",model:t?.model??o.model,content:a,...y,usage:j(o.usage)}}p(G,"response");export{G as response};
2
2
  //# sourceMappingURL=response.mjs.map
@@ -1,2 +1,2 @@
1
- var L=Object.defineProperty;var l=(b,a)=>L(b,"name",{value:a,configurable:!0});import{resolve as P}from"../../../../../../../lib/settings.mjs";import{TranslatorError as Q,openaiToAnthropicType as V,rewriteContextOverflow as W}from"../../../../../../../lib/errors.mjs";import{isComplete as X}from"../../../../../../../lib/arguments.mjs";import{messageId as Y,anthropicToolId as Z,restoreName as $}from"../../../../../../../lib/ids.mjs";import{sign as ee,reasoningOf as te,reasoningKey as ne,detailsText as ie,mergeDetail as oe,createThinkSplitter as se}from"../../../../../../../lib/thinking.mjs";import{finishError as le,toAnthropicStop as re,toAnthropicUsage as q}from"../../../../../../../lib/finish.mjs";import{messages as ae}from"../../../../../../../lib/sse.mjs";async function*_e(b,a,B){const u=P(B),F=a?.toolNames??{},d=u.thinkTags?se(u.thinkOpen):null,c=new Map;let _=!1,j=0,n=null,N=null,k=null,O=null,h="",v=!1,A=!1,C=0,T=!1,U=!1,E=!1;const w=[],s=l((e,t)=>({event:e,data:{type:e,...t}}),"event"),p=l((e,t)=>s("error",{error:{type:e,message:t}}),"fail"),J=l(e=>{const t={};return u.reasoningRoundTrip&&(e.key!==null&&(t[e.key]=e.text),e.details.length>0&&(t.reasoning_details=e.details)),Object.keys(t).length>0?t:void 0},"carried");function*y(){if(n!==null){n.kind==="thinking"?yield s("content_block_delta",{index:n.index,delta:{type:"signature_delta",signature:ee(J(n.carry),a?.producer)}}):n.kind==="tool"&&(n.entry.state="closed",n.entry.sent||(yield s("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:"{}"}}))),yield s("content_block_stop",{index:n.index}),n=null;for(const e of c.values()){if(e.state!=="pending")continue;e.state="closed";const t=j++,i=e.args.join("");yield s("content_block_start",{index:t,content_block:e.block}),yield s("content_block_delta",{index:t,delta:{type:"input_json_delta",partial_json:i===""?"{}":i}}),yield s("content_block_stop",{index:t})}}}l(y,"close");function*K(){for(const e of I()){if(e.state!=="buffered")continue;yield*g("tool",e.block,e);const t=e.args.join("");t!==""&&(e.sent=!0,yield s("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:t}})),yield*y()}}l(K,"flushBuffered");function I(){return[...c.entries()].sort((e,t)=>e[0]-t[0]).map(e=>e[1])}l(I,"ordered");function*g(e,t,i){yield*y(),n={kind:e,index:j++,entry:i,carry:{key:null,text:"",details:[]}},yield s("content_block_start",{index:n.index,content_block:t})}l(g,"begin");function*r(e,t,i=null){if(e==="text"&&E&&(t=t.trimStart()),t==="")return;if(E=e==="thinking",e==="text"&&(v=!0),n?.kind==="tool"){w.push({kind:e,text:t,key:i});return}n?.kind!==e&&(yield*g(e,e==="text"?{type:"text",text:""}:{type:"thinking",thinking:"",signature:""})),i!==null&&(n.carry.key??=i,n.carry.text+=t);const o=e==="text"?{type:"text_delta",text:t}:{type:"thinking_delta",thinking:t};yield s("content_block_delta",{index:n.index,delta:o})}l(r,"piece");function*R(e){if(n?.kind==="tool"){w.push({kind:"details",items:e});return}n?.kind!=="thinking"&&(yield*g("thinking",{type:"thinking",thinking:"",signature:""}));for(const t of e)oe(n.carry.details,t);n.carry.key===null&&(yield*r("thinking",ie(e)))}l(R,"details");function*z(e){yield*r("thinking",e.reasoning),yield*r("text",e.text)}l(z,"pieces");function*M(e){let t=e.index;t===void 0&&(t=e.id===void 0?C:c.size),C=t;let i=c.get(t);if(i===void 0){const x=e.function?.name??"",m={type:"tool_use",id:Z(e.id),name:$(F,x),input:{}};d?.pending()&&(U=!0);const f=u.toolStreaming==="buffer"||U;i={state:f?"buffered":"pending",sent:!1,args:[],block:m},c.set(t,i),!f&&n?.kind!=="tool"&&(i.state="open",yield*g("tool",m,i))}let o=e.function?.arguments;o!==null&&typeof o=="object"&&(o=JSON.stringify(o)),!(o==null||o==="")&&(i.args.push(o),i.state==="open"&&(i.sent=!0,yield s("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:o}})))}l(M,"toolCall");try{for await(const e of ae(b)){if(e.data==="[DONE]"){A=!0;break}const t=e.data;if(t.error){const f=t.error.message??"Upstream error";yield p(V(t.error),W(f)??f);return}_||(_=!0,yield s("message_start",{message:{id:Y(),type:"message",role:"assistant",model:a?.model??t.model,content:[],stop_reason:null,stop_sequence:null,usage:q(t.usage)}})),t.usage&&(N=t.usage,a&&(a.upstreamUsage=t.usage));const i=t.choices?.[0];if(!i)continue;const o=i.delta??{},x=te(o);let m="";x!==""&&!T&&(T=!0,d&&(m=d.release().text)),yield*r("thinking",x,ne(o)),yield*r("text",m),u.reasoningRoundTrip&&Array.isArray(o.reasoning_details)&&o.reasoning_details.length>0&&(yield*R(o.reasoning_details)),typeof o.content=="string"&&o.content!==""&&(d&&!T?yield*z(d.push(o.content)):yield*r("text",o.content)),typeof o.refusal=="string"&&(h+=o.refusal);for(const f of o.tool_calls??[])yield*M(f);i.finish_reason&&(k=i.finish_reason),typeof i.stop_reason=="string"&&(O=i.stop_reason)}}catch(e){yield e instanceof Q?p(e.type,e.message):p("api_error",e.message);return}if(!_||k===null&&!A){yield p("api_error",_?"Upstream stream ended before completion":"Upstream stream ended without data");return}const S=le(k);if(S){yield p(S.type,S.message);return}d&&(yield*z(d.end())),yield*K();const D=I().at(-1),G=D===void 0||X(D.args.join(""));yield*y();for(const e of w)yield*e.kind==="details"?R(e.items):r(e.kind,e.text,e.key);!v&&h!==""&&(yield*r("text",h)),yield*y(),j===0&&(yield*g("text",{type:"text",text:""}),yield*y());const H=re(k,{hasToolCalls:c.size>0,matchedStop:O,argumentsComplete:G,refusal:h});yield s("message_delta",{delta:H,usage:q(N)}),yield s("message_stop",{})}l(_e,"stream");export{_e as stream};
1
+ var Q=Object.defineProperty;var s=(b,r)=>Q(b,"name",{value:r,configurable:!0});import{resolve as V}from"../../../../../../../lib/settings.mjs";import{TranslatorError as W,openaiToAnthropicType as X,rewriteContextOverflow as Y}from"../../../../../../../lib/errors.mjs";import{isComplete as Z}from"../../../../../../../lib/arguments.mjs";import{messageId as $,anthropicToolId as ee,restoreName as te}from"../../../../../../../lib/ids.mjs";import{sign as ne,reasoningOf as ie,reasoningKey as oe,detailsText as le,mergeDetail as se,createThinkSplitter as re}from"../../../../../../../lib/thinking.mjs";import{filterCategory as ae,finishError as de,toAnthropicStop as fe,toAnthropicUsage as B}from"../../../../../../../lib/finish.mjs";import{messages as ce}from"../../../../../../../lib/sse.mjs";async function*xe(b,r,F){const u=V(F),J=r?.toolNames??{},K=r?.toolIds??{},d=u.thinkTags?re(u.thinkOpen):null,c=new Map;let m=!1,j=0,i=null,N=null,k=null,O=null,h="",T=null,v=!1,A=!1,I=0,w=!1,U=!1,E=!1;const S=[],l=s((e,t)=>({event:e,data:{type:e,...t}}),"event"),p=s((e,t)=>l("error",{error:{type:e,message:t}}),"fail"),M=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*y(){if(i!==null){i.kind==="thinking"?yield l("content_block_delta",{index:i.index,delta:{type:"signature_delta",signature:ne(M(i.carry),r?.producer)}}):i.kind==="tool"&&(i.entry.state="closed",i.entry.sent||(yield l("content_block_delta",{index:i.index,delta:{type:"input_json_delta",partial_json:"{}"}}))),yield l("content_block_stop",{index:i.index}),i=null;for(const e of c.values()){if(e.state!=="pending")continue;e.state="closed";const t=j++,n=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:n===""?"{}":n}}),yield l("content_block_stop",{index:t})}}}s(y,"close");function*G(){for(const e of R()){if(e.state!=="buffered")continue;yield*g("tool",e.block,e);const t=e.args.join("");t!==""&&(e.sent=!0,yield l("content_block_delta",{index:i.index,delta:{type:"input_json_delta",partial_json:t}})),yield*y()}}s(G,"flushBuffered");function R(){return[...c.entries()].sort((e,t)=>e[0]-t[0]).map(e=>e[1])}s(R,"ordered");function*g(e,t,n){yield*y(),i={kind:e,index:j++,entry:n,carry:{key:null,text:"",details:[]}},yield l("content_block_start",{index:i.index,content_block:t})}s(g,"begin");function*a(e,t,n=null){if(e==="text"&&E&&(t=t.trimStart()),t==="")return;if(E=e==="thinking",e==="text"&&(v=!0),i?.kind==="tool"){S.push({kind:e,text:t,key:n});return}i?.kind!==e&&(yield*g(e,e==="text"?{type:"text",text:""}:{type:"thinking",thinking:"",signature:""})),n!==null&&(i.carry.key??=n,i.carry.text+=t);const o=e==="text"?{type:"text_delta",text:t}:{type:"thinking_delta",thinking:t};yield l("content_block_delta",{index:i.index,delta:o})}s(a,"piece");function*z(e){if(i?.kind==="tool"){S.push({kind:"details",items:e});return}i?.kind!=="thinking"&&(yield*g("thinking",{type:"thinking",thinking:"",signature:""}));for(const t of e)se(i.carry.details,t);i.carry.key===null&&(yield*a("thinking",le(e)))}s(z,"details");function*D(e){yield*a("thinking",e.reasoning),yield*a("text",e.text)}s(D,"pieces");function*H(e){let t=e.index;t===void 0&&(t=e.id===void 0||e.id===null?I:c.size),I=t;let n=c.get(t);if(n===void 0){const x=e.function?.name??"",_={type:"tool_use",id:ee(e.id,K),name:te(J,x),input:{}};d?.pending()&&(U=!0);const f=u.toolStreaming==="buffer"||U;n={state:f?"buffered":"pending",sent:!1,args:[],block:_},c.set(t,n),!f&&i?.kind!=="tool"&&(n.state="open",yield*g("tool",_,n))}let o=e.function?.arguments;o!==null&&typeof o=="object"&&(o=JSON.stringify(o)),!(o==null||o==="")&&(n.args.push(o),n.state==="open"&&(n.sent=!0,yield l("content_block_delta",{index:i.index,delta:{type:"input_json_delta",partial_json:o}})))}s(H,"toolCall");try{for await(const e of ce(b)){if(e.data==="[DONE]"){A=!0;break}const t=e.data;if(t.error){const f=t.error.message??"Upstream error";yield p(X(t.error),Y(f)??f);return}m||(m=!0,yield l("message_start",{message:{id:$(),type:"message",role:"assistant",model:r?.model??t.model,content:[],stop_reason:null,stop_sequence:null,usage:B(t.usage)}})),t.usage&&(N=t.usage,r&&(r.upstreamUsage=t.usage));const n=t.choices?.[0];if(!n)continue;const o=n.delta??{},x=ie(o);let _="";x!==""&&!w&&(w=!0,d&&(_=d.release().text)),yield*a("thinking",x,oe(o)),yield*a("text",_),u.reasoningRoundTrip&&Array.isArray(o.reasoning_details)&&o.reasoning_details.length>0&&(yield*z(o.reasoning_details)),typeof o.content=="string"&&o.content!==""&&(d&&!w?yield*D(d.push(o.content)):yield*a("text",o.content)),typeof o.refusal=="string"&&(h+=o.refusal),T=ae(n.content_filter_results)??T;for(const f of o.tool_calls??[])yield*H(f);n.finish_reason&&(k=n.finish_reason),typeof n.stop_reason=="string"&&(O=n.stop_reason)}}catch(e){yield e instanceof W?p(e.type,e.message):p("api_error",e.message);return}if(!m||k===null&&!A){yield p("api_error",m?"Upstream stream ended before completion":"Upstream stream ended without data");return}const C=de(k);if(C){yield p(C.type,C.message);return}d&&(yield*D(d.end())),yield*G();const q=R().at(-1),L=q===void 0||Z(q.args.join(""));yield*y();for(const e of S)yield*e.kind==="details"?z(e.items):a(e.kind,e.text,e.key);!v&&h!==""&&(yield*a("text",h)),yield*y(),j===0&&(yield*g("text",{type:"text",text:""}),yield*y());const P=fe(k,{hasToolCalls:c.size>0,matchedStop:O,argumentsComplete:L,refusal:h,category:T});yield l("message_delta",{delta:P,usage:B(N)}),yield l("message_stop",{})}s(xe,"stream");export{xe as stream};
2
2
  //# sourceMappingURL=stream.mjs.map
@@ -1,2 +1,2 @@
1
- var v=Object.defineProperty;var n=(r,e)=>v(r,"name",{value:e,configurable:!0});import{context as g,self as m}from"../lib/runtime.mjs";import{capture as w}from"../lib/capture.mjs";import{TranslatorError as d}from"../lib/errors.mjs";import{warn as y}from"../lib/warn.mjs";import{emit as h}from"../lib/events.mjs";import*as p from"../lib/http.mjs";async function D(r={}){const e=w(m,g),{status:o}=m,f=r.network!==!1;for(const a of e.drivers){const s=a.info.fields;if(s!==void 0&&!(Array.isArray(s)&&s.every(i=>typeof i=="string")))throw new d(`Driver ${a.name}: info().fields must be an array of field names`,{code:"invalid_driver",status:500})}const t={transport:e.state.transport,signal:r.signal,timeout:e.config.detectTimeout};return await Promise.all(e.state.endpoints.map(a=>k(e,a,f,t))),o()}n(D,"detect");async function k(r,e,o,f){let t,a=null;if(e.backend!=="auto"){if(t=r.drivers.find(s=>s.name===e.backend),!t)throw new d(`Unknown backend "${e.backend}" for upstream ${e.name}`,{code:"unknown_backend",status:500});a={version:e.version}}else if(o){const s={endpoint:e,...f,probes:new Map};let i=!1;for(const[l,c]of r.drivers.entries()){if(!i&&c.info.probes?.length>0){i=!0;for(const u of r.drivers.slice(l))for(const b of u.info.probes??[])p.probe(s,b)}if(a=await $(c,s),a){t=c;break}}}if(t||(t=r.drivers.find(s=>s.name==="compatible"),a={reachable:null,models:[]}),e.driver=t.name,e.protocol=t.info.protocol,e.facts=a,e.driverSettings=await t.settings?.(a)??{},e.probed={},o&&e.backend==="auto"&&t.probe){const s=n(l=>e.settings[l]!==void 0||r.config.settings[l]!==void 0,"given"),i={inlineSystem:r.settings(e).inlineSystem==="auto",images:!s("images")};(i.inlineSystem||i.images)&&(e.probed=await t.probe({endpoint:e,facts:a,want:i,...f}))}a.reachable===!1?y(r.config,r.state,`Upstream ${e.name} (${e.url}) did not answer detection; using the generic OpenAI-compatible driver`,{type:"detect",kind:"unreachable",endpoint:e.name,url:e.url,driver:t.name}):h({config:r.config,state:r.state},{type:"detect",kind:"driver",level:"info",message:`Upstream ${e.name} (${e.url}): driver ${t.name}`,endpoint:e.name,url:e.url,driver:t.name})}n(k,"bind");async function $(r,e){try{return await r.detect?.(e)??null}catch{return null}}n($,"detectWith");export{D as detect};
1
+ var g=Object.defineProperty;var o=(r,e)=>g(r,"name",{value:e,configurable:!0});import{context as h,self as u}from"../lib/runtime.mjs";import{capture as w}from"../lib/capture.mjs";import{TranslatorError as m}from"../lib/errors.mjs";import{warn as p}from"../lib/warn.mjs";import{emit as y}from"../lib/events.mjs";import*as k from"../lib/http.mjs";import{validToolIdPolicy as $}from"../lib/ids.mjs";async function D(r={}){const e=w(u,h),{status:f}=u,l=r.network!==!1;for(const t of e.drivers){const s=t.info.fields;if(s!==void 0&&!(Array.isArray(s)&&s.every(i=>typeof i=="string")))throw new m(`Driver ${t.name}: info().fields must be an array of field names`,{code:"invalid_driver",status:500})}for(const t of e.drivers){const s=t.info.toolCallIds;if(s!==void 0&&!$(s))throw new m(`Driver ${t.name}: info().toolCallIds must be { alphabet, min?, max? } with a character-class alphabet and positive integer bounds`,{code:"invalid_driver",status:500})}const a={transport:e.state.transport,signal:r.signal,timeout:e.config.detectTimeout};return await Promise.all(e.state.endpoints.map(t=>x(e,t,l,a))),f()}o(D,"detect");async function x(r,e,f,l){let a,t=null;if(e.backend!=="auto"){if(a=r.drivers.find(s=>s.name===e.backend),!a)throw new m(`Unknown backend "${e.backend}" for upstream ${e.name}`,{code:"unknown_backend",status:500});t={version:e.version}}else if(f){const s=URL.canParse(e.url)?new URL(e.url).hostname:"",i={endpoint:e,...l,probes:new Map};let n=!1;for(const[v,c]of r.drivers.entries()){if(!n&&c.info.probes?.length>0&&!c.info.hosts?.includes(s)){n=!0;for(const d of r.drivers.slice(v))for(const b of d.info.probes??[])k.probe(i,b,d.info)}if(t=await U(c,i),t){a=c;break}}}if(a||(a=r.drivers.find(s=>s.name==="compatible"),t={reachable:null,models:[]}),e.driver=a.name,e.protocol=a.info.protocol,e.facts=t,e.driverSettings=await a.settings?.(t)??{},e.probed={},f&&e.backend==="auto"&&a.probe){const s=o(n=>e.settings[n]!==void 0||r.config.settings[n]!==void 0,"given"),i={inlineSystem:r.settings(e).inlineSystem==="auto",images:!s("images")};(i.inlineSystem||i.images)&&(e.probed=await a.probe({endpoint:e,facts:t,want:i,...l}))}t.reachable===!1?p(r.config,r.state,`Upstream ${e.name} (${e.url}) did not answer detection; using the generic OpenAI-compatible driver`,{type:"detect",kind:"unreachable",endpoint:e.name,url:e.url,driver:a.name}):y({config:r.config,state:r.state},{type:"detect",kind:"driver",level:"info",message:`Upstream ${e.name} (${e.url}): driver ${a.name}`,endpoint:e.name,url:e.url,driver:a.name})}o(x,"bind");async function U(r,e){try{return await r.detect?.(e)??null}catch{return null}}o(U,"detectWith");export{D as detect};
2
2
  //# sourceMappingURL=detect.mjs.map
@@ -1,2 +1,2 @@
1
- var c=Object.defineProperty;var r=(t,n)=>c(t,"name",{value:n,configurable:!0});function o({endpoint:t}){const n=URL.canParse(t.url)?new URL(t.url).hostname:"";return n==="api.anthropic.com"?{host:n}:null}r(o,"detect");export{o as detect};
1
+ var l=Object.defineProperty;var i=(t,e)=>l(t,"name",{value:e,configurable:!0});import*as f from"../../../lib/http.mjs";import{info as a}from"./info.mjs";function o(t){if(t===null||typeof t!="object"||Array.isArray(t)||"object"in t||!Array.isArray(t.data)||typeof t.has_more!="boolean"||![t.first_id,t.last_id].every(r=>r===null||typeof r=="string")||!t.data.every(r=>r?.type==="model"&&typeof r.id=="string"&&typeof r.display_name=="string"))return!1;const n=t.data[0]?.id??null,s=t.data.at(-1)?.id??null;return t.first_id===n&&t.last_id===s}i(o,"isAnthropicList");async function c(t){const{endpoint:e}=t,n=URL.canParse(e.url)?new URL(e.url).hostname:"";if(a().hosts.includes(n))return{host:n};const s=await f.probe(t,"/v1/models",a());return!s.ok||!o(s.body)?null:{models:s.body.data.map(r=>r.id)}}i(c,"detect");export{c as detect};
2
2
  //# sourceMappingURL=detect.mjs.map
@@ -1,2 +1,2 @@
1
- var t=Object.defineProperty;var e=(s,o)=>t(s,"name",{value:o,configurable:!0});function a(){return{name:"anthropic",protocol:"anthropic.messages",order:20,auth:"x-api-key",strict:!0,headers:{"anthropic-version":"2023-06-01"},paths:{messages:"/v1/messages",models:"/v1/models",count:"/v1/messages/count_tokens"}}}e(a,"info");export{a as info};
1
+ var t=Object.defineProperty;var s=(e,o)=>t(e,"name",{value:o,configurable:!0});function a(){return{name:"anthropic",protocol:"anthropic.messages",order:38,auth:"x-api-key",strict:!0,hosts:["api.anthropic.com"],probes:["/v1/models"],headers:{"anthropic-version":"2023-06-01"},paths:{messages:"/v1/messages",models:"/v1/models",count:"/v1/messages/count_tokens"}}}s(a,"info");export{a as info};
2
2
  //# sourceMappingURL=info.mjs.map
@@ -0,0 +1,2 @@
1
+ var i=Object.defineProperty;var a=(o,e)=>i(o,"name",{value:e,configurable:!0});import*as r from"../../../lib/http.mjs";import{isLmstudioModels as s,objectOf as m}from"../../../lib/backends.mjs";async function p(o){const e=m(await r.probe(o,"/api/v0/models"));if(!s(e))return null;const d=e.data.filter(t=>t.type!=="embeddings");return{models:d.map(t=>t.id),loaded:d.filter(t=>t.state==="loaded").map(t=>t.id)}}a(p,"detect");export{p as detect};
2
+ //# sourceMappingURL=detect.mjs.map
@@ -0,0 +1,2 @@
1
+ var a=Object.defineProperty;var o=(e,t)=>a(e,"name",{value:t,configurable:!0});function i(){return{name:"lmstudio",protocol:"openai.chat",order:55,auth:"bearer",strict:!1,probes:["/api/v0/models"],paths:{chat:"/v1/chat/completions",models:"/v1/models",listing:"/api/v0/models"}}}o(i,"info");export{i as info};
2
+ //# sourceMappingURL=info.mjs.map
@@ -0,0 +1,2 @@
1
+ var e=Object.defineProperty;var r=(o,t)=>e(o,"name",{value:t,configurable:!0});import{noEmptyTools as n}from"../../../lib/normalize.mjs";function u(o){return n(o)}r(u,"request");export{u as request};
2
+ //# sourceMappingURL=request.mjs.map
@@ -0,0 +1,2 @@
1
+ var s=Object.defineProperty;var e=(n,t)=>s(n,"name",{value:t,configurable:!0});function i(){return{thinking:"none",thinkingBudget:!1,effort:!1,topK:!1,prefill:"none",streamUsage:!0,continuousUsage:!1}}e(i,"settings");export{i as settings};
2
+ //# sourceMappingURL=settings.mjs.map
@@ -0,0 +1,2 @@
1
+ var r=Object.defineProperty;var i=(t,n)=>r(t,"name",{value:n,configurable:!0});import{flatten as s}from"../../../lib/typed.mjs";function l(t){const n=s(t?.choices?.[0]?.delta?.content);if(!n)return t;const[e,...c]=t.choices,o={...e.delta,content:n.text};return n.thinking!==""&&(o.reasoning_content=n.thinking),{...t,choices:[{...e,delta:o},...c]}}i(l,"chunk");export{l as chunk};
2
+ //# sourceMappingURL=chunk.mjs.map
@@ -0,0 +1,2 @@
1
+ var e=Object.defineProperty;var a=(t,r)=>e(t,"name",{value:r,configurable:!0});function l({endpoint:t}){const r=URL.canParse(t.url)?new URL(t.url).hostname:"";return r==="api.mistral.ai"?{host:r}:null}a(l,"detect");export{l as detect};
2
+ //# sourceMappingURL=detect.mjs.map
@@ -0,0 +1,2 @@
1
+ var e=Object.defineProperty;var a=(t,o)=>e(t,"name",{value:o,configurable:!0});function r(){return{name:"mistral",protocol:"openai.chat",order:36,auth:"bearer",strict:!1,toolCallIds:{alphabet:"a-zA-Z0-9",min:9,max:9},paths:{chat:"/v1/chat/completions",models:"/v1/models"}}}a(r,"info");export{r as info};
2
+ //# sourceMappingURL=info.mjs.map
@@ -0,0 +1,2 @@
1
+ var n=Object.defineProperty;var d=(o,e)=>n(o,"name",{value:e,configurable:!0});import{noEmptyTools as t}from"../../../lib/normalize.mjs";function s(o){const e={...t(o)};return e.seed!==void 0&&(e.random_seed===void 0&&(e.random_seed=e.seed),delete e.seed),e.tool_choice==="required"&&(e.tool_choice="any"),e}d(s,"request");export{s as request};
2
+ //# sourceMappingURL=request.mjs.map
@@ -0,0 +1,2 @@
1
+ var i=Object.defineProperty;var s=(n,t)=>i(n,"name",{value:t,configurable:!0});import{flatten as a}from"../../../lib/typed.mjs";function f(n){return Array.isArray(n?.choices)?{...n,choices:n.choices.map(t=>{const e=a(t?.message?.content);if(!e)return t;const r={...t.message,content:e.text};return e.thinking!==""&&(r.reasoning_content=e.thinking),{...t,message:r}})}:n}s(f,"response");export{f as response};
2
+ //# sourceMappingURL=response.mjs.map
@@ -0,0 +1,2 @@
1
+ var s=Object.defineProperty;var e=(n,t)=>s(n,"name",{value:t,configurable:!0});function i(){return{reasoningField:"reasoning_content",maxTokensField:"max_tokens",thinking:"none",thinkingBudget:!1,effort:!1,topK:!1,prefill:"none",streamUsage:!0,continuousUsage:!1}}e(i,"settings");export{i as settings};
2
+ //# sourceMappingURL=settings.mjs.map
@@ -1,2 +1,2 @@
1
- var _=Object.defineProperty;var f=(e,s)=>_(e,"name",{value:s,configurable:!0});import{context as g,self as b}from"../../lib/runtime.mjs";import{capture as $}from"../../lib/capture.mjs";import{resolve as w,serves as A}from"../../lib/route.mjs";import*as h from"../../lib/http.mjs";import*as v from"../../lib/pool.mjs";import{emit as O}from"../../lib/events.mjs";const P="/v1/embeddings";async function J(e,s={}){const{transport:y}=g.state,{error:m}=b,a=$(b,g,"openai"),n=f((r,t,l={})=>m.build(r,t,{...l,front:"openai"}),"fail");if(!e||typeof e!="object"||Array.isArray(e))return n(400,"The request body must be a JSON object",{code:"invalid_body"});if(typeof e.model!="string"||e.model==="")return n(400,"model is required",{code:"invalid_body",param:"model"});const T=Object.hasOwn(a.config.aliases,e.model)?a.config.aliases[e.model]:e.model;if(!a.state.endpoints.some(r=>A(r,T)))return n(404,`The model "${e.model}" is not served by any configured upstream.`,{code:"model_not_found",param:"model"});const u=w(a,e,{advance:!0}),c=u.pool.filter(r=>r.protocol==="openai.chat");if(c.length===0)return n(400,`The model "${e.model}" is served by an Anthropic-protocol upstream, which has no embeddings API.`,{code:"embeddings_unsupported",param:"model"});const j={...e,model:u.model};let i;for(let r=0;r<c.length;r++){const t=c[r],l=a.driver(t);t.health.inflight++,t.health.requests++;let o;try{o=await h.json(y,h.join(t.url,P),{method:"POST",headers:{"content-type":"application/json",accept:"application/json",...l.info.headers,...h.headers(t,l.info.auth,s.headers)},body:j,signal:s.signal})}finally{t.health.inflight--}if(s.signal?.aborted)return n(499,"The request was aborted",{code:"aborted"});const d=o.status===0;if(!d&&o.status<500)return v.succeeded(t),o.ok?o.body===null||typeof o.body!="object"?n(502,"The upstream returned a body that is not JSON",{code:"bad_gateway"}):{status:200,headers:{"content-type":"application/json"},body:o.body}:m(o.status,o.body,{},{front:"openai",upstream:t.name,native:!0,model:e.model});const p=d?`Could not reach the upstream (${t.name}): ${o.error?.message}`:`HTTP ${o.status}`;v.failed(a.config,t,p),O(a,{type:"error",kind:d?"transport":"upstream",level:"error",message:d?p:`Upstream ${t.name} answered HTTP ${o.status}`,endpoint:t.name,model:u.model,log:s.log}),i={endpoint:t,result:o,reason:p}}return i.result.status===0?n(502,i.reason,{code:"upstream_unreachable"}):m(i.result.status,i.result.body,{},{front:"openai",upstream:i.endpoint.name,native:!0,model:e.model})}f(J,"send");export{J as send};
1
+ var x=Object.defineProperty;var h=(e,s)=>x(e,"name",{value:s,configurable:!0});import{context as q,self as w}from"../../lib/runtime.mjs";import{capture as H}from"../../lib/capture.mjs";import{resolve as S,serves as J}from"../../lib/route.mjs";import*as y from"../../lib/http.mjs";import*as A from"../../lib/pool.mjs";import{emit as N}from"../../lib/events.mjs";import{meter as U}from"../../lib/usage.mjs";import{warn as C}from"../../lib/warn.mjs";import{start as I}from"../../lib/record.mjs";const M="/v1/embeddings";async function Q(e,s={}){const{transport:$}=q.state,{error:g}=w,a=H(w,q,"openai"),O=e&&typeof e=="object"&&!Array.isArray(e),l=U({model:null,requested:O&&typeof e.model=="string"?e.model:null,upstream:null,front:"openai",stream:!1},s.onUsage,(o,t)=>C(a.config,a.state,o,{type:"usage",...t,log:s.log})),u=h((o,t)=>(l.finish(t),Object.defineProperty(o,"usage",{value:l.usage,enumerable:!1,configurable:!0})),"end"),n=h((o,t,f={})=>u(g.build(o,t,{...f,front:"openai"}),o===499?"aborted":"error"),"fail");if(!e||typeof e!="object"||Array.isArray(e))return n(400,"The request body must be a JSON object",{code:"invalid_body"});if(typeof e.model!="string"||e.model==="")return n(400,"model is required",{code:"invalid_body",param:"model"});const P=Object.hasOwn(a.config.aliases,e.model)?a.config.aliases[e.model]:e.model;if(!a.state.endpoints.some(o=>J(o,P)))return n(404,`The model "${e.model}" is not served by any configured upstream.`,{code:"model_not_found",param:"model"});const c=S(a,e,{advance:!0}),p=c.pool.filter(o=>o.protocol==="openai.chat");if(p.length===0)return n(400,`The model "${e.model}" is served by an Anthropic-protocol upstream, which has no embeddings API.`,{code:"embeddings_unsupported",param:"model"});const v={...e,model:c.model};l.meta.model=c.model;let m,j=null;for(let o=0;o<p.length;o++){const t=p[o];l.meta.upstream=t.name;const f=a.driver(t);t.health.inflight++,t.health.requests++;const T=y.join(t.url,M),_={"content-type":"application/json",accept:"application/json",...f.info.headers,...y.headers(t,f.info.auth,s.headers)},b=I(a,{body:e,sent:{upstream:t.name,url:T,headers:_,body:v,state:{stream:!1}},front:"openai",requested:l.meta.requested,log:s.log,exchange:j,attempt:o+1});j??=b.id;let r;try{r=await y.json($,T,{method:"POST",headers:_,body:v,signal:s.signal})}finally{t.health.inflight--}if(s.signal?.aborted)return n(499,"The request was aborted",{code:"aborted"});const d=r.status===0;if(!d&&r.status<500)return A.succeeded(t),b.reply(r.status,r.headers,r.body),r.ok?r.body===null||typeof r.body!="object"?n(502,"The upstream returned a body that is not JSON",{code:"bad_gateway"}):(l.json(r.body),u({status:200,headers:{"content-type":"application/json"},body:r.body},"done")):u(g(r.status,r.body,{},{front:"openai",upstream:t.name,native:!0,model:e.model}),"upstream_error");const i=d?`Could not reach the upstream (${t.name}): ${r.error?.message}`:`HTTP ${r.status}`;A.failed(a.config,t,i);const k=d?{error:i}:{status:r.status,headers:r.headers,body:r.body};b.attempt(k,o+1<p.length?i:void 0),N(a,{type:"error",kind:d?"transport":"upstream",level:"error",message:d?i:`Upstream ${t.name} answered HTTP ${r.status}`,endpoint:t.name,model:c.model,log:s.log}),m={endpoint:t,result:r,reason:i}}return m.result.status===0?n(502,m.reason,{code:"upstream_unreachable"}):u(g(m.result.status,m.result.body,{},{front:"openai",upstream:m.endpoint.name,native:!0,model:e.model}),"upstream_error")}h(Q,"send");export{Q as send};
2
2
  //# sourceMappingURL=send.mjs.map
@@ -1,3 +1,3 @@
1
- var T=Object.defineProperty;var p=(e,n)=>T(e,"name",{value:n,configurable:!0});import{context as q,self as I}from"../lib/runtime.mjs";import{capture as L}from"../lib/capture.mjs";import{resolve as B}from"../lib/route.mjs";import*as U from"../lib/http.mjs";import{filterChatBody as E}from"../lib/passthrough.mjs";import{addSampling as M}from"../lib/sampling.mjs";import*as F from"../lib/images.mjs";import*as S from"../lib/tools.mjs";import{LIMITS as P}from"../lib/toolsearch.mjs";import{warn as b}from"../lib/warn.mjs";import{emit as R}from"../lib/events.mjs";import{fit as C,pinBody as H}from"../lib/effort.mjs";import{relax as z,strictTools as A}from"../lib/strict.mjs";import{TranslatorError as D}from"../lib/errors.mjs";function G(e){return`${e.driver}:${e.protocol}:${URL.canParse(e.url)?new URL(e.url).host:e.url}`}p(G,"producerOf");async function ne(e,n={}){const r=L(I,q,n.front),c=B(r,e,{endpoint:n.endpoint,advance:n.hold===!0&&n.endpoint===void 0});if(n.hold!==!0)return _(r,e,n,c);c.endpoint.health.inflight++;try{return await _(r,e,n,c)}catch(t){throw c.endpoint.health.inflight--,t}}p(ne,"request");function J(e,n,r,c,t,f){const g=A(n);if(g.length===0)return n;const i=g.filter(a=>a.problems.length>0);if(i.length>0&&t.unknownFields==="error")throw new D(`tools: ${i[0].name}: schema is outside the strict subset (${i[0].problems.join("; ")})`,{code:"unsupported_content",param:"tools"});const k=z(n);if(f.retry===!0)return k;const u=p((a,h)=>b(e.config,e.state,a,{kind:h,endpoint:r.name,log:f.log}),"tell");for(const a of i)u(`Upstream ${r.name}: strict tool ${a.name} has a schema outside the strict subset: ${a.problems.join("; ")} (sent without strict)`,"strict");const d=g.filter(a=>a.problems.length===0);return c.info.strict!==!0&&d.length>0&&u(`Upstream ${r.name}: the ${c.name} backend does not enforce strict tool use (${d.map(a=>a.name).join(", ")})`,"strict"),k}p(J,"checkStrict");async function _(e,n,r,c){const{endpoint:t,model:f,unserved:g,convert:i,pool:k,pin:u}=c,d=Object.hasOwn(e.config.efforts,f)?e.config.efforts[f]:void 0;u&&(n=H(n,u,e.front));const a=e.driver(t),h=`${t.name}
2
- ${f}`,w=r.images===!1||e.state.textOnly.has(h)&&e.settings(t).imageFallback,$=e.settings(t),x=r.jsonObject==="instruction"||e.state.jsonInstruction.has(h)&&$.jsonObjectFallback,m={...w?{...$,images:!1}:$,...x?{jsonObject:"instruction"}:{},...u?.effort===void 0?{}:{effort:!0}},v=n.stream===!0;let o,s;i?({body:o,state:s}=await i.request(n,m,{producer:G(t),budget:r.budget,listed:d!==void 0}),s.toolSearch?.capped&&b(e.config,e.state,`Tool search: more than ${P.definitions} tool definitions came back in ToolSearch results for ${t.name}; the rest are not offered.`,{kind:"toolsearch",endpoint:t.name,log:r.log})):o=e.front==="openai"&&(r.filter??m.passthroughFilter===!0)?E(n,a.info.fields):{...n};const y=e.front==="anthropic"?n.metadata?.sampling:void 0;if(t.protocol==="openai.chat"){const l=[];o=M(o,y,m,a.info.fields,l),s&&s.dropped.push(...l.filter(j=>!s.dropped.includes(j)))}else if(!i&&y!==void 0&&o.metadata?.sampling!==void 0){const{sampling:l,...j}=o.metadata;o={...o,metadata:j}}if(r.usage===!0&&v&&e.front==="openai"){if(i)s.includeUsage=!0;else if(m.streamUsage){const l=o.stream_options&&typeof o.stream_options=="object"?o.stream_options:{};o.stream_options={...l,include_usage:!0}}}w&&!i&&e.front==="openai"&&(o=F.strip(o)),r.tools===!1&&(o=S.strip(o)),o.model=f,o=J(e,o,t,a,m,r),d&&t.protocol==="openai.chat"&&C(o,d,f,(l,j)=>b(e.config,e.state,l,{type:"effort",endpoint:t.name,model:f,...j,log:r.log})),s?.reasoningMissing&&b(e.config,e.state,`Upstream ${t.name}: assistant turns with tool calls have no reasoning to send back (the client dropped its thinking blocks or their signatures), which servers that need it with tools will refuse.`,{kind:"reasoning",endpoint:t.name,log:r.log}),a.request&&(o=await a.request(o,{endpoint:t,settings:m,levels:d,state:s,warn:p(l=>b(e.config,e.state,l,{kind:"driver",endpoint:t.name,log:r.log}),"warn")})),s?.dropped?.length>0&&r.retry!==!0&&R(e,{type:"dropped",level:"debug",message:`Upstream ${t.name}: dropped ${s.dropped.join(", ")}`,endpoint:t.name,model:f,fields:[...s.dropped],log:r.log});const O=t.protocol==="openai.chat"?a.info.paths.chat:a.info.paths.messages;return{upstream:t.name,pool:k.map(l=>l.name),method:"POST",url:U.join(t.url,O),path:O,headers:{"content-type":"application/json",accept:v?"text/event-stream":"application/json",...a.info.headers,...i===null&&t.protocol==="anthropic.messages"?r.clientHeaders:void 0,...U.headers(t,a.info.auth,r.headers)},body:o,state:{v:1,upstream:t.name,front:e.front,native:i===null,model:n.model,stream:v,unserved:g,convert:s},images:{sent:F.carries(o),retry:m.imageFallback&&t.protocol==="openai.chat",key:h},tools:{sent:S.carries(o),retry:m.toolFallback&&t.protocol==="openai.chat"},json:{sent:s?.jsonSchemaEmulated===!0,retry:m.jsonObjectFallback&&t.protocol==="anthropic.messages",key:h}}}p(_,"prepare");export{ne as request};
1
+ var T=Object.defineProperty;var p=(e,n)=>T(e,"name",{value:n,configurable:!0});import{context as I,self as q}from"../lib/runtime.mjs";import{capture as C}from"../lib/capture.mjs";import{resolve as L}from"../lib/route.mjs";import*as U from"../lib/http.mjs";import{filterChatBody as B}from"../lib/passthrough.mjs";import{addSampling as E}from"../lib/sampling.mjs";import*as F from"../lib/images.mjs";import*as S from"../lib/tools.mjs";import{LIMITS as M}from"../lib/toolsearch.mjs";import{warn as b}from"../lib/warn.mjs";import{emit as P}from"../lib/events.mjs";import{fit as R,pinBody as H}from"../lib/effort.mjs";import{relax as z,strictTools as A}from"../lib/strict.mjs";import{TranslatorError as D}from"../lib/errors.mjs";function G(e){return`${e.driver}:${e.protocol}:${URL.canParse(e.url)?new URL(e.url).host:e.url}`}p(G,"producerOf");async function ne(e,n={}){const r=C(q,I,n.front),c=L(r,e,{endpoint:n.endpoint,advance:n.hold===!0&&n.endpoint===void 0});if(n.hold!==!0)return _(r,e,n,c);c.endpoint.health.inflight++;try{return await _(r,e,n,c)}catch(t){throw c.endpoint.health.inflight--,t}}p(ne,"request");function J(e,n,r,c,t,f){const g=A(n);if(g.length===0)return n;const i=g.filter(a=>a.problems.length>0);if(i.length>0&&t.unknownFields==="error")throw new D(`tools: ${i[0].name}: schema is outside the strict subset (${i[0].problems.join("; ")})`,{code:"unsupported_content",param:"tools"});const k=z(n);if(f.retry===!0)return k;const u=p((a,h)=>b(e.config,e.state,a,{kind:h,endpoint:r.name,log:f.log}),"tell");for(const a of i)u(`Upstream ${r.name}: strict tool ${a.name} has a schema outside the strict subset: ${a.problems.join("; ")} (sent without strict)`,"strict");const d=g.filter(a=>a.problems.length===0);return c.info.strict!==!0&&d.length>0&&u(`Upstream ${r.name}: the ${c.name} backend does not enforce strict tool use (${d.map(a=>a.name).join(", ")})`,"strict"),k}p(J,"checkStrict");async function _(e,n,r,c){const{endpoint:t,model:f,unserved:g,convert:i,pool:k,pin:u}=c,d=Object.hasOwn(e.config.efforts,f)?e.config.efforts[f]:void 0;u&&(n=H(n,u,e.front));const a=e.driver(t),h=`${t.name}
2
+ ${f}`,w=r.images===!1||e.state.textOnly.has(h)&&e.settings(t).imageFallback,$=e.settings(t),x=r.jsonObject==="instruction"||e.state.jsonInstruction.has(h)&&$.jsonObjectFallback,m={...w?{...$,images:!1}:$,...x?{jsonObject:"instruction"}:{},...u?.effort===void 0?{}:{effort:!0}},v=n.stream===!0;let o,s;i?({body:o,state:s}=await i.request(n,m,{producer:G(t),budget:r.budget,listed:d!==void 0,toolCallIds:a.info.toolCallIds}),s.toolSearch?.capped&&b(e.config,e.state,`Tool search: more than ${M.definitions} tool definitions came back in ToolSearch results for ${t.name}; the rest are not offered.`,{kind:"toolsearch",endpoint:t.name,log:r.log})):o=e.front==="openai"&&(r.filter??m.passthroughFilter===!0)?B(n,a.info.fields):{...n};const y=e.front==="anthropic"?n.metadata?.sampling:void 0;if(t.protocol==="openai.chat"){const l=[];o=E(o,y,m,a.info.fields,l),s&&s.dropped.push(...l.filter(j=>!s.dropped.includes(j)))}else if(!i&&y!==void 0&&o.metadata?.sampling!==void 0){const{sampling:l,...j}=o.metadata;o={...o,metadata:j}}if(r.usage===!0&&v&&e.front==="openai"){if(i)s.includeUsage=!0;else if(m.streamUsage){const l=o.stream_options&&typeof o.stream_options=="object"?o.stream_options:{};o.stream_options={...l,include_usage:!0}}}w&&!i&&e.front==="openai"&&(o=F.strip(o)),r.tools===!1&&(o=S.strip(o)),o.model=f,o=J(e,o,t,a,m,r),d&&t.protocol==="openai.chat"&&R(o,d,f,(l,j)=>b(e.config,e.state,l,{type:"effort",endpoint:t.name,model:f,...j,log:r.log})),s?.reasoningMissing&&b(e.config,e.state,`Upstream ${t.name}: assistant turns with tool calls have no reasoning to send back (the client dropped its thinking blocks or their signatures), which servers that need it with tools will refuse.`,{kind:"reasoning",endpoint:t.name,log:r.log}),a.request&&(o=await a.request(o,{endpoint:t,settings:m,levels:d,state:s,warn:p(l=>b(e.config,e.state,l,{kind:"driver",endpoint:t.name,log:r.log}),"warn")})),s?.dropped?.length>0&&r.retry!==!0&&P(e,{type:"dropped",level:"debug",message:`Upstream ${t.name}: dropped ${s.dropped.join(", ")}`,endpoint:t.name,model:f,fields:[...s.dropped],log:r.log});const O=t.protocol==="openai.chat"?a.info.paths.chat:a.info.paths.messages;return{upstream:t.name,pool:k.map(l=>l.name),method:"POST",url:U.join(t.url,O),path:O,headers:{"content-type":"application/json",accept:v?"text/event-stream":"application/json",...a.info.headers,...i===null&&t.protocol==="anthropic.messages"?r.clientHeaders:void 0,...U.headers(t,a.info.auth,r.headers)},body:o,state:{v:1,upstream:t.name,front:e.front,native:i===null,model:n.model,stream:v,unserved:g,convert:s},images:{sent:F.carries(o),retry:m.imageFallback&&t.protocol==="openai.chat",key:h},tools:{sent:S.carries(o),retry:m.toolFallback&&t.protocol==="openai.chat"},json:{sent:s?.jsonSchemaEmulated===!0,retry:m.jsonObjectFallback&&t.protocol==="anthropic.messages",key:h}}}p(_,"prepare");export{ne as request};
3
3
  //# sourceMappingURL=request.mjs.map
package/dist/api/send.mjs CHANGED
@@ -1,2 +1,2 @@
1
- var tt=Object.defineProperty;var l=(u,s)=>tt(u,"name",{value:s,configurable:!0});import{context as R,self as Q}from"../lib/runtime.mjs";import{TranslatorError as C}from"../lib/errors.mjs";import*as et from"../lib/http.mjs";import*as at from"../lib/images.mjs";import*as rt from"../lib/tools.mjs";import{parse as st}from"../lib/upstream-error.mjs";import{meter as nt,normalize as ot}from"../lib/usage.mjs";import{capture as it}from"../lib/capture.mjs";import{estimate as lt,bodyLength as ut,output as ct,patch as mt,stream as dt}from"../lib/estimate.mjs";import{warn as ft}from"../lib/warn.mjs";import{start as pt}from"../lib/record.mjs";import{emit as V}from"../lib/events.mjs";import*as W from"../lib/pool.mjs";import{createBudget as gt}from"../lib/thinking.mjs";const ht={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"};async function Ut(u,s={}){const{transport:y,textOnly:f,jsonInstruction:k}=R.state,{request:j,response:_,stream:i,error:g}=Q,d=g.build,{config:o,state:$}=R,h=it(Q,R,s.front),S=u&&typeof u=="object"&&!Array.isArray(u),x={model:null,requested:S&&typeof u.model=="string"?u.model:null,upstream:null,front:s.front??o.front,stream:S&&u.stream===!0},w=nt(x,s.onUsage,(m,E)=>ft(o,$,m,{type:"usage",...E,log:s.log})),O=l(m=>Object.defineProperty(m,"usage",{value:w.usage,enumerable:!1,configurable:!0}),"attach");try{return O(await yt({transport:y,textOnly:f,jsonInstruction:k,request:j,response:_,stream:i,error:g,captured:h},u,s,w))}catch(m){if(!(m instanceof C))throw s.signal?.aborted||V(h,{type:"error",kind:"exception",level:"error",message:String(m?.message??m),log:s.log}),w.finish(s.signal?.aborted?"aborted":"error"),m;return w.finish("error"),O(d(m.status,m.message,{type:m.type,code:m.code,param:m.param,front:s.front}))}}l(Ut,"send");async function yt({transport:u,textOnly:s,jsonInstruction:y,request:f,response:k,stream:j,error:_,captured:i},g,d,o){const $={front:d.front,headers:d.headers,clientHeaders:d.clientHeaders,filter:d.filter,usage:!0,budget:gt(),log:d.log},h=l(e=>{V(i,{...e,log:d.log})},"announce"),S=o.meta.front==="openai"&&g?.stream_options?.include_usage!==!0,x=l(e=>{o.meta.model=e.body.model??null,o.meta.upstream=e.upstream,e.state.native?o.source(null):o.source(()=>e.state.convert?.upstreamUsage,e.state.front==="anthropic"?"openai":"folded")},"note"),w=l(e=>{if(e.state.native)return{input:!1,output:!1};const t=ot(e.state.front==="anthropic"?"openai":"folded",e.state.convert?.upstreamUsage);return t===null?{input:!0,output:!0}:{input:t.input_tokens===null&&t.cache_read_input_tokens===null&&t.cache_creation_input_tokens===null,output:t.output_tokens===null}},"gaps"),O=l(async e=>{const t=i.endpoint(e.upstream),a=i.driver(t);return(a.count&&!(S&&e.state.stream)?await a.count({endpoint:t,request:e.body,transport:u,signal:d.signal,timeout:i.config.detectTimeout}):null)??lt(e.body)},"input"),m=l(async(e,t)=>{const a=w(e);return!a.input&&!a.output?null:{input_tokens:a.input?await O(e):null,output_tokens:a.output?ct(t):null}},"counts"),E=l((e,t,a,r,I)=>(o.finish(I),_(e,t,a,r)),"fail"),Y=l(async e=>{let t=e,a=await X(u,t,d.signal,G());const r=a.status,I=r===400&&[t.images,t.tools].some(v=>v.sent&&v.retry),L=(r===400||r===422)&&t.json.sent&&t.json.retry;if(!a.ok&&(I||L)){const v=await F(a),T=st(v,r).message,p={};if(r===400&&t.images.sent&&t.images.retry&&at.refused(T)?(s.add(t.images.key),p.images=!1):r===400&&t.tools.sent&&t.tools.retry&&rt.refused(T)?p.tools=!1:L&&(p.jsonObject="instruction"),!("images"in p||"tools"in p||"jsonObject"in p))return{upstream:t,result:a,failure:v};const b={upstream:t,result:a,failure:v};if("images"in p||"tools"in p){const M="images"in p?"images":"tools";h({type:"fallback",kind:M,level:"warn",message:`Upstream ${t.upstream}: ${t.state.model} refused ${M==="images"?"images":"tools"} (${r}: "${String(T).trim().slice(0,500)}"); sent the request again without them.`,endpoint:t.upstream,model:t.body.model,status:r,reason:String(T).trim().slice(0,500),action:M==="images"?"text-only":"without-tools"})}if(t=await f(g,{...$,...p,endpoint:t.upstream,retry:!0}),x(t),a=await X(u,t,d.signal,G()),L&&"jsonObject"in p){if(!a.ok){try{await a.body?.cancel?.()}catch{}return x(b.upstream),b}y.add(b.upstream.json.key),h({type:"fallback",kind:"json_object",level:"warn",message:`Upstream ${b.upstream.upstream}: ${b.upstream.state.model} rejected the json_object schema emulation (${r}: "${String(T).trim().slice(0,500)}"); sent it as a system instruction instead. Set jsonObject: "instruction" on this upstream to skip the first attempt.`,endpoint:b.upstream.upstream,model:b.upstream.body.model,status:r,reason:String(T).trim().slice(0,500),action:"instruction"})}}return{upstream:t,result:a}},"attempt");let n=await f(g,{...$,hold:!0});const H=n.pool;let z=0;const B=H.length>1?i.config.pool:null,G=l(()=>B&&{connectTimeout:B.connectTimeout,headersTimeout:z+1<H.length?B.headersTimeout:0},"limitsNow");let U=i.endpoint(n.upstream);const K=l(()=>U.health.inflight--,"release");let c,N;try{for(;;){const e=U;e.health.requests++,x(n);let t=null;try{({upstream:n,result:c,failure:N}=await Y(n)),!c.ok&&c.status>=500&&(t=`HTTP ${c.status}`,h({type:"error",kind:"upstream",level:"error",message:`Upstream ${e.name} answered HTTP ${c.status}`,endpoint:e.name,model:n.body.model,status:c.status}))}catch(r){if(!(r instanceof C)||r.code!=="upstream_unreachable")throw r;t=r.message,c=void 0,N=r,h({type:"error",kind:"transport",level:"error",message:r.message,endpoint:e.name,model:n.body.model})}if(t===null){W.succeeded(e);break}W.failed(i.config,e,t);let a=null;for(;a===null&&!d.signal?.aborted&&z+1<H.length;)try{a=await f(g,{...$,endpoint:H[++z],hold:!0})}catch(r){if(!(r instanceof C)||r.code!=="endpoint_not_in_pool")throw r}if(a===null){if(c===void 0)throw N;break}h({type:"failover",level:"warn",message:`Upstream ${e.name} failed (${t}); trying ${a.upstream}`,from:e.name,to:a.upstream,model:a.body.model,reason:t});try{await c?.body?.cancel?.()}catch{}K(),U=i.endpoint(a.upstream),n=a}}catch(e){throw K(),e}const Z=U;U=null,o.usage.then(()=>Z.health.inflight--);const q=et.plain(c.headers),J=pt(i,{body:g,sent:n,front:o.meta.front,requested:o.meta.requested,headers:q,log:d.log});if(!c.ok){const e=N??await F(c);return J.reply(c.status,q,e),E(c.status,e,q,n.state,"upstream_error")}if(n.state.stream){const e=j(J.raw(c.body),n.state),t=n.state.native?e:dt(o.meta.front,e,{gaps:l(()=>w(n),"gaps"),input:l(()=>O(n),"input"),report:l(a=>o.estimate(a),"report")});return{status:200,headers:ht,stream:J.events(o.track(t,d.signal,S))}}const P=await F(c);if(J.reply(c.status,q,P),P===null||typeof P!="object")return h({type:"error",kind:"upstream",level:"error",message:`Upstream ${n.upstream} returned a body that is not JSON`,endpoint:n.upstream,model:n.body.model,status:502}),E(502,"The upstream returned a body that is not JSON",q,n.state,"error");const A=await k(P,n.state),D=await m(n,ut(o.meta.front,A));return D&&(A.usage=mt(o.meta.front,A.usage,D),o.estimate(D)),o.json(A),o.finish("done"),{status:200,headers:{"content-type":"application/json"},body:A}}l(yt,"round");async function X(u,s,y,f=null){let k,j=!1,_=y;if(f&&f.headersTimeout>0){const i=new AbortController;_=y?AbortSignal.any([y,i.signal]):i.signal,k=setTimeout(()=>{j=!0,i.abort()},f.headersTimeout)}try{return await u(s.url,{method:s.method,headers:s.headers,body:JSON.stringify(s.body),signal:_,redirect:"error",...f&&f.connectTimeout>0?{connectTimeout:f.connectTimeout}:{}})}catch(i){if(y?.aborted)throw i;const g=j?`no response headers within ${f.headersTimeout} ms`:i.message;throw new C(`Could not reach the upstream (${s.upstream}): ${g}`,{code:"upstream_unreachable",status:502,details:{cause:i}})}finally{clearTimeout(k)}}l(X,"call");async function F(u){const s=await u.text();try{return JSON.parse(s)}catch{return s===""?null:s}}l(F,"parse");export{Ut as send};
1
+ var it=Object.defineProperty;var u=(c,r)=>it(c,"name",{value:r,configurable:!0});import{context as G,self as Z}from"../lib/runtime.mjs";import{TranslatorError as z}from"../lib/errors.mjs";import*as tt from"../lib/http.mjs";import*as ut from"../lib/images.mjs";import*as lt from"../lib/tools.mjs";import{parse as ct}from"../lib/upstream-error.mjs";import{meter as mt,normalize as dt}from"../lib/usage.mjs";import{capture as ft}from"../lib/capture.mjs";import{estimate as pt,bodyLength as gt,output as ht,patch as yt,stream as wt}from"../lib/estimate.mjs";import{warn as et}from"../lib/warn.mjs";import{start as bt}from"../lib/record.mjs";import{emit as at}from"../lib/events.mjs";import*as rt from"../lib/pool.mjs";import{createBudget as kt}from"../lib/thinking.mjs";import*as nt from"../lib/fanout.mjs";const jt={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"};async function _t(c,r={}){const{transport:y,textOnly:f,jsonInstruction:k}=G.state,{request:j,response:_,stream:m,error:g}=Z,d=g.build,{config:o,state:T}=G,h=ft(Z,G,r.front),O=c&&typeof c=="object"&&!Array.isArray(c),U={model:null,requested:O&&typeof c.model=="string"?c.model:null,upstream:null,front:r.front??o.front,stream:O&&c.stream===!0},w=mt(U,r.onUsage,(l,A)=>et(o,T,l,{type:"usage",...A,log:r.log})),q=u(l=>Object.defineProperty(l,"usage",{value:w.usage,enumerable:!1,configurable:!0}),"attach");let J=!1;try{const l=nt.wants(h,c);if(l>0){J=!0;const A=u((s,$)=>et(o,T,s,{type:"usage",...$,log:r.log}),"warning");return await nt.run({body:c,count:l,options:r,send:_t,warn:A})}return q(await Tt({transport:y,textOnly:f,jsonInstruction:k,request:j,response:_,stream:m,error:g,captured:h},c,r,w))}catch(l){if(J)throw l;if(!(l instanceof z))throw r.signal?.aborted||at(h,{type:"error",kind:"exception",level:"error",message:String(l?.message??l),log:r.log}),w.finish(r.signal?.aborted?"aborted":"error"),l;return w.finish("error"),q(d(l.status,l.message,{type:l.type,code:l.code,param:l.param,front:r.front}))}}u(_t,"send");async function Tt({transport:c,textOnly:r,jsonInstruction:y,request:f,response:k,stream:j,error:_,captured:m},g,d,o){const T={front:d.front,headers:d.headers,clientHeaders:d.clientHeaders,filter:d.filter,usage:!0,budget:kt(),log:d.log},h=u(e=>{at(m,{...e,log:d.log})},"announce"),O=o.meta.front==="openai"&&g?.stream_options?.include_usage!==!0,U=u(e=>{o.meta.model=e.body.model??null,o.meta.upstream=e.upstream,e.state.native?o.source(null):o.source(()=>e.state.convert?.upstreamUsage,e.state.front==="anthropic"?"openai":"folded")},"note"),w=u(e=>{if(e.state.native)return{input:!1,output:!1};const t=dt(e.state.front==="anthropic"?"openai":"folded",e.state.convert?.upstreamUsage);return t===null?{input:!0,output:!0}:{input:t.input_tokens===null&&t.cache_read_input_tokens===null&&t.cache_creation_input_tokens===null,output:t.output_tokens===null}},"gaps"),q=u(async e=>{const t=m.endpoint(e.upstream),a=m.driver(t);return(a.count&&!(O&&e.state.stream)?await a.count({endpoint:t,request:e.body,transport:c,signal:d.signal,timeout:m.config.detectTimeout}):null)??pt(e.body)},"input"),J=u(async(e,t)=>{const a=w(e);return!a.input&&!a.output?null:{input_tokens:a.input?await q(e):null,output_tokens:a.output?ht(t):null}},"counts"),l=u((e,t,a,n,M)=>(o.finish(M),_(e,t,a,n)),"fail"),A=u(async e=>{let t=e,a=await st(c,t,d.signal,K());const n=a.status,M=n===400&&[t.images,t.tools].some(x=>x.sent&&x.retry),R=(n===400||n===422)&&t.json.sent&&t.json.retry;if(!a.ok&&(M||R)){const x=await B(a),S=ct(x,n).message,p={};if(n===400&&t.images.sent&&t.images.retry&&ut.refused(S)?(r.add(t.images.key),p.images=!1):n===400&&t.tools.sent&&t.tools.retry&&lt.refused(S)?p.tools=!1:R&&(p.jsonObject="instruction"),!("images"in p||"tools"in p||"jsonObject"in p))return{upstream:t,result:a,failure:x};const b={upstream:t,result:a,failure:x};if("images"in p||"tools"in p){const F="images"in p?"images":"tools";h({type:"fallback",kind:F,level:"warn",message:`Upstream ${t.upstream}: ${t.state.model} refused ${F==="images"?"images":"tools"} (${n}: "${String(S).trim().slice(0,500)}"); sent the request again without them.`,endpoint:t.upstream,model:t.body.model,status:n,reason:String(S).trim().slice(0,500),action:F==="images"?"text-only":"without-tools"})}if(t=await f(g,{...T,...p,endpoint:t.upstream,retry:!0}),U(t),a=await st(c,t,d.signal,K()),R&&"jsonObject"in p){if(!a.ok){try{await a.body?.cancel?.()}catch{}return U(b.upstream),b}y.add(b.upstream.json.key),h({type:"fallback",kind:"json_object",level:"warn",message:`Upstream ${b.upstream.upstream}: ${b.upstream.state.model} rejected the json_object schema emulation (${n}: "${String(S).trim().slice(0,500)}"); sent it as a system instruction instead. Set jsonObject: "instruction" on this upstream to skip the first attempt.`,endpoint:b.upstream.upstream,model:b.upstream.body.model,status:n,reason:String(S).trim().slice(0,500),action:"instruction"})}}return{upstream:t,result:a}},"attempt");let s=await f(g,{...T,hold:!0});const $=s.pool;let D=0;const I=$.length>1?m.config.pool:null,K=u(()=>I&&{connectTimeout:I.connectTimeout,headersTimeout:D+1<$.length?I.headersTimeout:0},"limitsNow");let E=m.endpoint(s.upstream);const Q=u(()=>E.health.inflight--,"release");let i,v,V=null,W=0;const X=u(e=>{const t=bt(m,{body:g,sent:s,front:o.meta.front,requested:o.meta.requested,headers:e,log:d.log,exchange:V,attempt:W});return V??=t.id,t},"begin"),Y=u(async e=>{const t=X();if(!t.on)return;if(i===void 0)return t.attempt({error:v.message},e);let a=null;try{a=v??await B(i)}catch{}t.attempt({status:i.status,headers:tt.plain(i.headers),body:a},e)},"keep");try{for(;;){const e=E;e.health.requests++,W++,U(s);let t=null;try{({upstream:s,result:i,failure:v}=await A(s)),!i.ok&&i.status>=500&&(t=`HTTP ${i.status}`,h({type:"error",kind:"upstream",level:"error",message:`Upstream ${e.name} answered HTTP ${i.status}`,endpoint:e.name,model:s.body.model,status:i.status}))}catch(n){if(!(n instanceof z)||n.code!=="upstream_unreachable")throw n;t=n.message,i=void 0,v=n,h({type:"error",kind:"transport",level:"error",message:n.message,endpoint:e.name,model:s.body.model})}if(t===null){rt.succeeded(e);break}rt.failed(m.config,e,t);let a=null;for(;a===null&&!d.signal?.aborted&&D+1<$.length;)try{a=await f(g,{...T,endpoint:$[++D],hold:!0})}catch(n){if(!(n instanceof z)||n.code!=="endpoint_not_in_pool")throw n}if(a===null){if(i===void 0)throw await Y(),v;break}await Y(t),h({type:"failover",level:"warn",message:`Upstream ${e.name} failed (${t}); trying ${a.upstream}`,from:e.name,to:a.upstream,model:a.body.model,reason:t});try{await i?.body?.cancel?.()}catch{}Q(),E=m.endpoint(a.upstream),s=a}}catch(e){throw Q(),e}const ot=E;E=null,o.usage.then(()=>ot.health.inflight--);const H=tt.plain(i.headers),P=X(H);if(!i.ok){const e=v??await B(i);return P.reply(i.status,H,e),l(i.status,e,H,s.state,"upstream_error")}if(s.state.stream){const e=j(P.raw(i.body),s.state),t=s.state.native?e:wt(o.meta.front,e,{gaps:u(()=>w(s),"gaps"),input:u(()=>q(s),"input"),report:u(a=>o.estimate(a),"report")});return{status:200,headers:jt,stream:P.events(o.track(t,d.signal,O))}}const C=await B(i);if(P.reply(i.status,H,C),C===null||typeof C!="object")return h({type:"error",kind:"upstream",level:"error",message:`Upstream ${s.upstream} returned a body that is not JSON`,endpoint:s.upstream,model:s.body.model,status:502}),l(502,"The upstream returned a body that is not JSON",H,s.state,"error");const N=await k(C,s.state),L=await J(s,gt(o.meta.front,N));return L&&(N.usage=yt(o.meta.front,N.usage,L),o.estimate(L)),o.json(N),o.finish("done"),{status:200,headers:{"content-type":"application/json"},body:N}}u(Tt,"round");async function st(c,r,y,f=null){let k,j=!1,_=y;if(f&&f.headersTimeout>0){const m=new AbortController;_=y?AbortSignal.any([y,m.signal]):m.signal,k=setTimeout(()=>{j=!0,m.abort()},f.headersTimeout)}try{return await c(r.url,{method:r.method,headers:r.headers,body:JSON.stringify(r.body),signal:_,redirect:"error",...f&&f.connectTimeout>0?{connectTimeout:f.connectTimeout}:{}})}catch(m){if(y?.aborted)throw m;const g=j?`no response headers within ${f.headersTimeout} ms`:m.message;throw new z(`Could not reach the upstream (${r.upstream}): ${g}`,{code:"upstream_unreachable",status:502,details:{cause:m}})}finally{clearTimeout(k)}}u(st,"call");async function B(c){const r=await c.text();try{return JSON.parse(r)}catch{return r===""?null:r}}u(B,"parse");export{_t as send};
2
2
  //# sourceMappingURL=send.mjs.map
@@ -1,2 +1,2 @@
1
- var e=Object.defineProperty;var t=(o,n)=>e(o,"name",{value:n,configurable:!0});const i=["default_generation_settings","total_slots","build_info"];function r(o){return o.ok&&o.body!==null&&typeof o.body=="object"?o.body:{}}t(r,"objectOf");function f(o){return i.some(n=>n in o)}t(f,"isLlamacppProps");function a(o){return typeof o.model_path=="string"||typeof o.tokenizer_path=="string"}t(a,"isSglangInfo");export{f as isLlamacppProps,a as isSglangInfo,r as objectOf};
1
+ var i=Object.defineProperty;var e=(t,o)=>i(t,"name",{value:o,configurable:!0});const n=["default_generation_settings","total_slots","build_info"];function a(t){return t.ok&&t.body!==null&&typeof t.body=="object"?t.body:{}}e(a,"objectOf");function p(t){return n.some(o=>o in t)}e(p,"isLlamacppProps");function f(t){return typeof t.model_path=="string"||typeof t.tokenizer_path=="string"}e(f,"isSglangInfo");const s=["llm","vlm","embeddings"];function l(t){return t.object!=="list"||!Array.isArray(t.data)||t.data.length===0?!1:t.data.every(o=>o?.object==="model"&&typeof o.id=="string"&&typeof o.arch=="string"&&typeof o.compatibility_type=="string"&&typeof o.state=="string"&&s.includes(o.type))}e(l,"isLmstudioModels");export{p as isLlamacppProps,l as isLmstudioModels,f as isSglangInfo,a as objectOf};
2
2
  //# sourceMappingURL=backends.mjs.map
@@ -0,0 +1,2 @@
1
+ var A=Object.defineProperty;var u=(t,r)=>A(t,"name",{value:r,configurable:!0});import{TranslatorError as x,openaiError as P}from"./errors.mjs";import{resolve as j}from"./route.mjs";import{combine as E,deliver as O}from"./usage.mjs";const b=128,g={data:"[DONE]"};function U(t,r){if(t.front!=="openai"||!r||typeof r!="object"||Array.isArray(r)||!Number.isInteger(r.n)||r.n<2||j(t,r).convert===null)return 0;if(r.n>b)throw new x(`n: at most ${b} choices`,{code:"invalid_request",param:"n"});return r.n}u(U,"wants");function v(t,r){if(typeof t=="number"&&typeof r=="number")return t+r;if(t&&r&&typeof t=="object"&&typeof r=="object"){const c={...t};for(const a of Object.keys(r))c[a]=a in t?v(t[a],r[a]):r[a];return c}return t??r}u(v,"add");function S(t){const r=t.flatMap((a,f)=>(a.choices??[]).map(l=>({...l,index:f}))),c=t.map(a=>a.usage).reduce((a,f)=>a===void 0?f:v(a,f),void 0);return{...t[0],choices:r,...c===void 0?{}:{usage:c}}}u(S,"join");async function N(t){try{for(;!(await t.next()).done;);}catch{}}u(N,"finished");function D(t,r,c){const a=t.map(s=>s[Symbol.asyncIterator]());let f=!1;const l=u(async()=>{f||(f=!0,r(),await Promise.allSettled(a.map(s=>s.return?.())))},"close"),h=(async function*(){const s=new Map,p=u(o=>s.set(o,a[o].next().then(i=>({index:o,step:i}),i=>({index:o,error:i}))),"arm");a.forEach((o,i)=>p(i));let y,w,m;try{for(;s.size>0;){const{index:o,step:i,error:e}=await Promise.race(s.values());if(s.delete(o),e){c(o),yield{data:P(String(e?.message??e),{type:e?.type??"server_error"})},yield g;return}if(i.done)continue;p(o);const n=i.value?.data;if(n!=="[DONE]"){if(n?.error){c(o),yield i.value,await N(a[o]),yield g;return}if(Array.isArray(n?.choices)&&n.choices.length===0&&n.usage){w=n,m=m===void 0?n.usage:v(m,n.usage);continue}y??=n?.id,yield{...i.value,data:{...n,id:y,choices:n.choices.map(d=>({...d,index:o}))}}}}m!==void 0&&(yield{data:{...w,id:y,choices:[],usage:m}}),yield g}finally{await l()}})();return{[Symbol.asyncIterator](){return this},next:u(()=>h.next(),"next"),async return(s){return await l(),h.return(s)},async throw(s){return await l(),h.throw(s)}}}u(D,"weave");async function q({body:t,count:r,options:c,send:a,warn:f}){const l=new AbortController,h=c.signal?AbortSignal.any([c.signal,l.signal]):l.signal,s=Array.from({length:r},()=>{let e;return{promise:new Promise(d=>{e=d}),resolve:e}});let p=-1;const y=u(e=>{p>=0||(p=e,l.abort())},"trip"),w=Promise.all(s.map(e=>e.promise)).then(e=>{const n=E(e,p);return O(n,c.onUsage,f),n}),m=u(e=>Object.defineProperty(e,"usage",{value:w,enumerable:!1,configurable:!0}),"attach"),o=await Promise.all(s.map((e,n)=>a({...t,n:1},{...c,signal:h,onUsage:e.resolve}).then(d=>(d.status>=400&&y(n),{reply:d}),d=>(y(n),{error:d}))));if(p>=0){await Promise.all(o.map(n=>n.reply?.stream?.return?.())),await w;const e=o[p];if("error"in e)throw e.error;return m(e.reply)}const i=o.map(e=>e.reply);if(t.stream===!0){const e=D(i.map(n=>n.stream),()=>l.abort(),y);return m({status:200,headers:i[0].headers,stream:e})}return await w,m({status:200,headers:i[0].headers,body:S(i.map(e=>e.body))})}u(q,"run");export{b as MAX,v as add,S as join,q as run,U as wants,D as weave};
2
+ //# sourceMappingURL=fanout.mjs.map
@@ -1,2 +1,2 @@
1
- var _=Object.defineProperty;var r=(t,e)=>_(t,"name",{value:e,configurable:!0});const p={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 l(t){if(typeof t!="string"||!Object.hasOwn(p,t))return null;const{type:e,status:o}=p[t];return{type:e,status:o,code:"upstream_finish_"+t,message:`Upstream ended the response with finish_reason "${t}"`}}r(l,"finishError");function a(t,{hasToolCalls:e,matchedStop:o,argumentsComplete:s=!0,refusal:n}){return e&&(t!=="length"&&t!=="model_length"||s)?{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,stop_details:{type:"refusal",category:null,explanation:typeof n=="string"&&n!==""?n:null}}:typeof o=="string"&&o!==""?{stop_reason:"stop_sequence",stop_sequence:o}:{stop_reason:"end_turn",stop_sequence:null}}r(a,"toAnthropicStop");function i(t){switch(t){case"max_tokens":return"length";case"tool_use":return"tool_calls";case"refusal":return"content_filter";default:return"stop"}}r(i,"toOpenAIFinish");function k(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}}r(k,"toAnthropicUsage");function d(t){const e=t?.cache_read_input_tokens??0,o=(t?.input_tokens??0)+e+(t?.cache_creation_input_tokens??0),s=t?.output_tokens??0,n={prompt_tokens:o,completion_tokens:s,total_tokens:o+s};return e>0&&(n.prompt_tokens_details={cached_tokens:e}),n}r(d,"toOpenAIUsage");export{l as finishError,a as toAnthropicStop,k as toAnthropicUsage,i as toOpenAIFinish,d as toOpenAIUsage};
1
+ var c=Object.defineProperty;var n=(t,e)=>c(t,"name",{value:e,configurable:!0});const p={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 i(t){if(typeof t!="string"||!Object.hasOwn(p,t))return null;const{type:e,status:o}=p[t];return{type:e,status:o,code:"upstream_finish_"+t,message:`Upstream ended the response with finish_reason "${t}"`}}n(i,"finishError");const u=["hate","sexual","violence","self_harm"];function f(t){return!t||typeof t!="object"?null:u.some(e=>t[e]?.filtered===!0)?"general_harms":null}n(f,"filterCategory");function k(t,{hasToolCalls:e,matchedStop:o,argumentsComplete:s=!0,refusal:r,category:_=null}){return e&&(t!=="length"&&t!=="model_length"||s)?{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,stop_details:{type:"refusal",category:_,explanation:typeof r=="string"&&r!==""?r:null}}:typeof o=="string"&&o!==""?{stop_reason:"stop_sequence",stop_sequence:o}:{stop_reason:"end_turn",stop_sequence:null}}n(k,"toAnthropicStop");function m(t){switch(t){case"max_tokens":return"length";case"tool_use":return"tool_calls";case"refusal":return"content_filter";default:return"stop"}}n(m,"toOpenAIFinish");function d(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(d,"toAnthropicUsage");function h(t){const e=t?.cache_read_input_tokens??0,o=(t?.input_tokens??0)+e+(t?.cache_creation_input_tokens??0),s=t?.output_tokens??0,r={prompt_tokens:o,completion_tokens:s,total_tokens:o+s};return e>0&&(r.prompt_tokens_details={cached_tokens:e}),r}n(h,"toOpenAIUsage");export{f as filterCategory,i as finishError,k as toAnthropicStop,d as toAnthropicUsage,m as toOpenAIFinish,h as toOpenAIUsage};
2
2
  //# sourceMappingURL=finish.mjs.map
package/dist/lib/http.mjs CHANGED
@@ -1,2 +1,2 @@
1
- var y=Object.defineProperty;var o=(t,e)=>y(t,"name",{value:e,configurable:!0});function d(t,e){return t+e}o(d,"join");function p(t){return t==="localhost"||t==="::1"||/^127\./.test(t)}o(p,"isLoopback");function x(t,e="bearer",n){const r={...t.headers};return t.apiKey&&(e==="x-api-key"?r["x-api-key"]=t.apiKey:r.authorization=`Bearer ${t.apiKey}`),{...r,...n}}o(x,"headers");function b({endpoint:t,transport:e,signal:n,timeout:r,probes:a},i){const f=o(()=>g(e,d(t.url,i),{method:"GET",headers:x(t),signal:n,timeout:r}),"run");return a?(a.has(i)||a.set(i,f()),a.get(i)):f()}o(b,"probe");function k(t){const e={};if(!t)return e;if(typeof t.forEach=="function")return t.forEach((n,r)=>{e[r.toLowerCase()]=n}),e;for(const[n,r]of Object.entries(t))e[n.toLowerCase()]=String(r);return e}o(k,"plain");async function g(t,e,n={}){const{timeout:r,signal:a,body:i,...f}=n,s=[];a&&s.push(a),r>0&&s.push(AbortSignal.timeout(r));try{const u=await t(e,{...f,redirect:"error",body:i===void 0?void 0:JSON.stringify(i),signal:s.length>1?AbortSignal.any(s):s[0]}),c=await u.text();let l=null;try{l=JSON.parse(c)}catch{l=c===""?null:c}return{ok:u.ok,status:u.status,body:l}}catch(u){return{ok:!1,status:0,body:null,error:u}}}o(g,"json");export{x as headers,p as isLoopback,d as join,g as json,k as plain,b as probe};
1
+ var h=Object.defineProperty;var s=(t,e)=>h(t,"name",{value:e,configurable:!0});function y(t,e){return t+e}s(y,"join");function k(t){return t==="localhost"||t==="::1"||/^127\./.test(t)}s(k,"isLoopback");function d(t,e="bearer",a){const r={...t.headers};return t.apiKey&&(e==="x-api-key"?r["x-api-key"]=t.apiKey:r.authorization=`Bearer ${t.apiKey}`),{...r,...a}}s(d,"headers");function b({endpoint:t,transport:e,signal:a,timeout:r,probes:u},f,i){const o=i!==void 0&&(i.auth==="x-api-key"||i.headers!==void 0),n=o?{auth:i.auth,extra:i.headers}:{},l=s(()=>p(e,y(t.url,f),{method:"GET",headers:d(t,n.auth,n.extra),signal:a,timeout:r}),"run");if(!u)return l();const c=o?JSON.stringify([f,n.auth,n.extra]):f;return u.has(c)||u.set(c,l()),u.get(c)}s(b,"probe");function x(t){const e={};if(!t)return e;if(typeof t.forEach=="function")return t.forEach((a,r)=>{e[r.toLowerCase()]=a}),e;for(const[a,r]of Object.entries(t))e[a.toLowerCase()]=String(r);return e}s(x,"plain");async function p(t,e,a={}){const{timeout:r,signal:u,body:f,...i}=a,o=[];u&&o.push(u),r>0&&o.push(AbortSignal.timeout(r));try{const n=await t(e,{...i,redirect:"error",body:f===void 0?void 0:JSON.stringify(f),signal:o.length>1?AbortSignal.any(o):o[0]}),l=await n.text();let c=null;try{c=JSON.parse(l)}catch{c=l===""?null:l}return{ok:n.ok,status:n.status,body:c,headers:x(n.headers)}}catch(n){return{ok:!1,status:0,body:null,error:n}}}s(p,"json");export{d as headers,k as isLoopback,y as join,p as json,x as plain,b as probe};
2
2
  //# sourceMappingURL=http.mjs.map