@cldmv/llm-translator 0.9.0 → 0.11.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 (131) hide show
  1. package/README.md +8 -7
  2. package/dist/api/convert/anthropic/messages/to/ollama/chat/request.mjs +2 -0
  3. package/dist/api/convert/anthropic/messages/to/openai/chat/request.mjs +10 -10
  4. package/dist/api/convert/ollama/chat/to/anthropic/messages/response.mjs +2 -0
  5. package/dist/api/convert/ollama/chat/to/anthropic/messages/stream.mjs +2 -0
  6. package/dist/api/convert/ollama/chat/to/openai/chat/response.mjs +2 -0
  7. package/dist/api/convert/ollama/chat/to/openai/chat/stream.mjs +2 -0
  8. package/dist/api/convert/openai/chat/to/anthropic/messages/response.mjs +1 -1
  9. package/dist/api/convert/openai/chat/to/anthropic/messages/stream.mjs +1 -1
  10. package/dist/api/convert/openai/chat/to/ollama/chat/request.mjs +2 -0
  11. package/dist/api/detect.mjs +1 -1
  12. package/dist/api/driver/azure/detect.mjs +1 -1
  13. package/dist/api/driver/azure/info.mjs +1 -1
  14. package/dist/api/driver/deepseek/info.mjs +1 -1
  15. package/dist/api/driver/fireworks/detect.mjs +2 -0
  16. package/dist/api/driver/fireworks/info.mjs +2 -0
  17. package/dist/api/driver/fireworks/path.mjs +2 -0
  18. package/dist/api/driver/fireworks/request.mjs +2 -0
  19. package/dist/api/driver/fireworks/settings.mjs +2 -0
  20. package/dist/api/driver/gemini/detect.mjs +2 -0
  21. package/dist/api/driver/gemini/info.mjs +2 -0
  22. package/dist/api/driver/gemini/path.mjs +2 -0
  23. package/dist/api/driver/gemini/request.mjs +2 -0
  24. package/dist/api/driver/gemini/settings.mjs +2 -0
  25. package/dist/api/driver/groq/detect.mjs +2 -0
  26. package/dist/api/driver/groq/info.mjs +2 -0
  27. package/dist/api/driver/groq/path.mjs +2 -0
  28. package/dist/api/driver/groq/request.mjs +2 -0
  29. package/dist/api/driver/groq/settings.mjs +2 -0
  30. package/dist/api/driver/llamacpp/info.mjs +1 -1
  31. package/dist/api/driver/mistral/info.mjs +1 -1
  32. package/dist/api/driver/ollama/info.mjs +1 -1
  33. package/dist/api/driver/ollama/request.mjs +1 -1
  34. package/dist/api/driver/openai/info.mjs +1 -1
  35. package/dist/api/driver/openrouter/info.mjs +1 -1
  36. package/dist/api/driver/together/detect.mjs +2 -0
  37. package/dist/api/driver/together/info.mjs +2 -0
  38. package/dist/api/driver/together/request.mjs +2 -0
  39. package/dist/api/driver/together/settings.mjs +2 -0
  40. package/dist/api/driver/vllm/detect.mjs +1 -1
  41. package/dist/api/driver/vllm/info.mjs +1 -1
  42. package/dist/api/driver/vllm/model/list.mjs +1 -1
  43. package/dist/api/driver/xai/detect.mjs +2 -0
  44. package/dist/api/driver/xai/info.mjs +2 -0
  45. package/dist/api/driver/xai/request.mjs +2 -0
  46. package/dist/api/driver/xai/settings.mjs +2 -0
  47. package/dist/api/embeddings/send.mjs +1 -1
  48. package/dist/api/health/check.mjs +2 -0
  49. package/dist/api/model/list.mjs +1 -1
  50. package/dist/api/request.mjs +2 -2
  51. package/dist/api/response.mjs +1 -1
  52. package/dist/api/send.mjs +1 -1
  53. package/dist/api/status.mjs +1 -1
  54. package/dist/api/stream.mjs +1 -1
  55. package/dist/index.mjs +1 -1
  56. package/dist/lib/cache.mjs +2 -0
  57. package/dist/lib/capture.mjs +1 -1
  58. package/dist/lib/config.mjs +1 -1
  59. package/dist/lib/constraint.mjs +2 -0
  60. package/dist/lib/context.mjs +2 -0
  61. package/dist/lib/health.mjs +2 -0
  62. package/dist/lib/http.mjs +1 -1
  63. package/dist/lib/models.mjs +1 -1
  64. package/dist/lib/ndjson.mjs +4 -0
  65. package/dist/lib/ollama.mjs +3 -0
  66. package/dist/lib/passthrough.mjs +1 -1
  67. package/dist/lib/pool.mjs +1 -1
  68. package/dist/lib/presets.mjs +2 -0
  69. package/dist/lib/route.mjs +1 -1
  70. package/dist/lib/routes.mjs +1 -1
  71. package/dist/lib/settings.mjs +1 -1
  72. package/dist/lib/vllm.mjs +4 -0
  73. package/package.json +1 -1
  74. package/types/src/api/convert/anthropic/messages/to/ollama/chat/request.d.mts +14 -0
  75. package/types/src/api/convert/ollama/chat/to/anthropic/messages/response.d.mts +10 -0
  76. package/types/src/api/convert/ollama/chat/to/anthropic/messages/stream.d.mts +14 -0
  77. package/types/src/api/convert/ollama/chat/to/openai/chat/response.d.mts +10 -0
  78. package/types/src/api/convert/ollama/chat/to/openai/chat/stream.d.mts +13 -0
  79. package/types/src/api/convert/openai/chat/to/ollama/chat/request.d.mts +12 -0
  80. package/types/src/api/detect.d.mts +1 -1
  81. package/types/src/api/driver/azure/info.d.mts +2 -1
  82. package/types/src/api/driver/deepseek/info.d.mts +2 -1
  83. package/types/src/api/driver/fireworks/detect.d.mts +8 -0
  84. package/types/src/api/driver/fireworks/info.d.mts +13 -0
  85. package/types/src/api/driver/fireworks/path.d.mts +7 -0
  86. package/types/src/api/driver/fireworks/request.d.mts +7 -0
  87. package/types/src/api/driver/fireworks/settings.d.mts +5 -0
  88. package/types/src/api/driver/gemini/detect.d.mts +8 -0
  89. package/types/src/api/driver/gemini/info.d.mts +13 -0
  90. package/types/src/api/driver/gemini/path.d.mts +7 -0
  91. package/types/src/api/driver/gemini/request.d.mts +7 -0
  92. package/types/src/api/driver/gemini/settings.d.mts +5 -0
  93. package/types/src/api/driver/groq/detect.d.mts +8 -0
  94. package/types/src/api/driver/groq/info.d.mts +13 -0
  95. package/types/src/api/driver/groq/path.d.mts +7 -0
  96. package/types/src/api/driver/groq/request.d.mts +7 -0
  97. package/types/src/api/driver/groq/settings.d.mts +5 -0
  98. package/types/src/api/driver/mistral/info.d.mts +2 -1
  99. package/types/src/api/driver/ollama/info.d.mts +7 -2
  100. package/types/src/api/driver/openai/info.d.mts +3 -1
  101. package/types/src/api/driver/openrouter/info.d.mts +2 -1
  102. package/types/src/api/driver/together/detect.d.mts +8 -0
  103. package/types/src/api/driver/together/info.d.mts +13 -0
  104. package/types/src/api/driver/together/request.d.mts +7 -0
  105. package/types/src/api/driver/together/settings.d.mts +5 -0
  106. package/types/src/api/driver/vllm/detect.d.mts +1 -1
  107. package/types/src/api/driver/vllm/info.d.mts +1 -1
  108. package/types/src/api/driver/vllm/model/list.d.mts +3 -1
  109. package/types/src/api/driver/xai/detect.d.mts +8 -0
  110. package/types/src/api/driver/xai/info.d.mts +13 -0
  111. package/types/src/api/driver/xai/request.d.mts +7 -0
  112. package/types/src/api/driver/xai/settings.d.mts +5 -0
  113. package/types/src/api/endpoint/status.d.mts +5 -2
  114. package/types/src/api/health/check.d.mts +8 -0
  115. package/types/src/api/request.d.mts +1 -1
  116. package/types/src/api/send.d.mts +1 -1
  117. package/types/src/api/status.d.mts +1 -1
  118. package/types/src/lib/cache.d.mts +30 -0
  119. package/types/src/lib/config.d.mts +4 -0
  120. package/types/src/lib/constraint.d.mts +36 -0
  121. package/types/src/lib/context.d.mts +34 -0
  122. package/types/src/lib/events.d.mts +2 -2
  123. package/types/src/lib/health.d.mts +22 -0
  124. package/types/src/lib/models.d.mts +25 -0
  125. package/types/src/lib/ndjson.d.mts +16 -0
  126. package/types/src/lib/ollama.d.mts +86 -0
  127. package/types/src/lib/pool.d.mts +30 -4
  128. package/types/src/lib/presets.d.mts +99 -0
  129. package/types/src/lib/route.d.mts +1 -1
  130. package/types/src/lib/settings.d.mts +25 -0
  131. package/types/src/lib/vllm.d.mts +47 -0
@@ -1,3 +1,3 @@
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 w 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,l){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(l.retry===!0)return k;const u=p((a,h)=>b(e.config,e.state,a,{kind:h,endpoint:r.name,log:l.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:l,unserved:g,convert:i,pool:k,pin:u}=c,d=Object.hasOwn(e.config.efforts,l)?e.config.efforts[l]:void 0;u&&(n=H(n,u,e.front));const a=e.driver(t),h=`${t.name}
2
- ${l}`,O=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={...O?{...$,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 f=[];o=E(o,y,m,a.info.fields,f),s&&s.dropped.push(...f.filter(j=>!s.dropped.includes(j)))}else if(!i&&y!==void 0&&o.metadata?.sampling!==void 0){const{sampling:f,...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 f=o.stream_options&&typeof o.stream_options=="object"?o.stream_options:{};o.stream_options={...f,include_usage:!0}}}O&&!i&&e.front==="openai"&&(o=F.strip(o)),r.tools===!1&&(o=S.strip(o)),o.model=l,o=J(e,o,t,a,m,r),d&&t.protocol==="openai.chat"&&R(o,d,l,(f,j)=>b(e.config,e.state,f,{type:"effort",endpoint:t.name,model:l,...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(f=>b(e.config,e.state,f,{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:l,fields:[...s.dropped],log:r.log});const U=await w.pathOf(a,t.protocol==="openai.chat"?"chat":"messages",t,l);return{upstream:t.name,pool:k.map(f=>f.name),method:"POST",url:w.join(t.url,U),path:U,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,...w.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 M=Object.defineProperty;var h=(e,a)=>M(e,"name",{value:a,configurable:!0});import{context as B,self as E}from"../lib/runtime.mjs";import{capture as K}from"../lib/capture.mjs";import{resolve as P}from"../lib/route.mjs";import*as S from"../lib/http.mjs";import{filterChatBody as R}from"../lib/passthrough.mjs";import{addSampling as H}from"../lib/sampling.mjs";import{addConstraint as z}from"../lib/constraint.mjs";import{addCache as A}from"../lib/cache.mjs";import*as C from"../lib/images.mjs";import*as T from"../lib/tools.mjs";import{LIMITS as D}from"../lib/toolsearch.mjs";import{warn as v}from"../lib/warn.mjs";import{emit as G}from"../lib/events.mjs";import{fit as J,pinBody as N}from"../lib/effort.mjs";import{relax as Q,strictTools as V}from"../lib/strict.mjs";import{TranslatorError as W}from"../lib/errors.mjs";import*as X from"../lib/vllm.mjs";function Y(e){return`${e.driver}:${e.protocol}:${URL.canParse(e.url)?new URL(e.url).host:e.url}`}h(Y,"producerOf");async function ue(e,a={}){const r=K(E,B,a.front),f=P(r,e,{endpoint:a.endpoint,advance:a.hold===!0&&a.endpoint===void 0});if(a.hold!==!0)return q(r,e,a,f);f.endpoint.health.inflight++;try{return await q(r,e,a,f)}catch(n){throw f.endpoint.health.inflight--,n}}h(ue,"request");function Z(e,a,r,f,n,p){const k=V(a);if(k.length===0)return a;const l=k.filter(s=>s.problems.length>0);if(l.length>0&&n.unknownFields==="error")throw new W(`tools: ${l[0].name}: schema is outside the strict subset (${l[0].problems.join("; ")})`,{code:"unsupported_content",param:"tools"});const _=Q(a);if(p.retry===!0)return _;const g=h((s,j)=>v(e.config,e.state,s,{kind:j,endpoint:r.name,log:p.log}),"tell");for(const s of l)g(`Upstream ${r.name}: strict tool ${s.name} has a schema outside the strict subset: ${s.problems.join("; ")} (sent without strict)`,"strict");const u=k.filter(s=>s.problems.length===0);return f.info.strict!==!0&&u.length>0&&g(`Upstream ${r.name}: the ${f.name} backend does not enforce strict tool use (${u.map(s=>s.name).join(", ")})`,"strict"),_}h(Z,"checkStrict");async function q(e,a,r,f){const{endpoint:n,model:p,unserved:k,convert:l,pool:_,pin:g}=f,u=Object.hasOwn(e.config.efforts,p)?e.config.efforts[p]:void 0;g&&(a=N(a,g,e.front));const s=e.driver(n),j=e.front==="anthropic"?a.metadata?.prompt_cache_key:void 0;let w=e.front==="anthropic"?a.metadata?.constraint:void 0;e.front==="openai"&&a.constraint!==void 0&&({constraint:w,...a}=a);const $=`${n.name}
2
+ ${p}`,U=r.images===!1||e.state.textOnly.has($)&&e.settings(n).imageFallback,b=e.settings(n),I=r.jsonObject==="instruction"||e.state.jsonInstruction.has($)&&b.jsonObjectFallback,c={...U?{...b,images:!1}:b,...I?{jsonObject:"instruction"}:{},...g?.effort===void 0?{}:{effort:!0}},y=a.stream===!0,m=l===null?e.front==="anthropic"?"anthropic.messages":"openai.chat":n.protocol,O=m==="anthropic.messages"&&n.protocol==="openai.chat";let t,o;l?({body:t,state:o}=await l.request(a,c,{producer:Y(n),budget:r.budget,listed:u!==void 0,toolCallIds:s.info.toolCallIds}),o.toolSearch?.capped&&v(e.config,e.state,`Tool search: more than ${D.definitions} tool definitions came back in ToolSearch results for ${n.name}; the rest are not offered.`,{kind:"toolsearch",endpoint:n.name,log:r.log})):O?t=X.request(a,c):t=e.front==="openai"&&(r.filter??c.passthroughFilter===!0)?R(a,s.info.fields):{...a};const F=e.front==="anthropic"?a.metadata?.sampling:void 0;if(m==="openai.chat"){const i=[];t=H(t,F,c,s.info.fields,i),o&&o.dropped.push(...i.filter(d=>!o.dropped.includes(d)))}else if(!l&&F!==void 0&&t.metadata?.sampling!==void 0){const{sampling:i,...d}=t.metadata;t={...t,metadata:d}}if(m==="openai.chat"){const i=[],d={name:s.info.name,version:n.facts?.version??n.version};t=z(t,w,c,d,i),t=A(t,{salt:r.cacheSalt,key:j},c,d.name,i),o&&o.dropped.push(...i.filter(L=>!o.dropped.includes(L)))}else{if(o&&!o.dropped.includes("cache_salt")&&(r.cacheSalt??c.cacheSalt)!=null&&o.dropped.push("cache_salt"),o&&!o.dropped.includes("prompt_cache_key")&&(j??c.promptCacheKey)!=null&&o.dropped.push("prompt_cache_key"),!l&&t.metadata?.prompt_cache_key!==void 0){const{prompt_cache_key:i,...d}=t.metadata;t={...t,metadata:d}}if(o&&(w??c.constraint)!=null&&!o.dropped.includes("constraint")&&o.dropped.push("constraint"),!l&&t.metadata?.constraint!==void 0){const{constraint:i,...d}=t.metadata;t={...t,metadata:d}}}if(r.usage===!0&&y&&e.front==="openai"){if(l)o.includeUsage=!0;else if(c.streamUsage){const i=t.stream_options&&typeof t.stream_options=="object"?t.stream_options:{};t.stream_options={...i,include_usage:!0}}}U&&!l&&e.front==="openai"&&(t=C.strip(t)),r.tools===!1&&(t=T.strip(t)),t.model=p,t=Z(e,t,n,s,c,r),u&&m==="openai.chat"&&J(t,u,p,(i,d)=>v(e.config,e.state,i,{type:"effort",endpoint:n.name,model:p,...d,log:r.log})),o?.reasoningMissing&&v(e.config,e.state,`Upstream ${n.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:n.name,log:r.log}),s.request&&!O&&(t=await s.request(t,{endpoint:n,settings:c,levels:u,state:o,warn:h(i=>v(e.config,e.state,i,{kind:"driver",endpoint:n.name,log:r.log}),"warn")})),o?.dropped?.length>0&&r.retry!==!0&&G(e,{type:"dropped",level:"debug",message:`Upstream ${n.name}: dropped ${o.dropped.join(", ")}`,endpoint:n.name,model:p,fields:[...o.dropped],log:r.log});const x=await S.pathOf(s,m==="anthropic.messages"?"messages":"chat",n,p);return{upstream:n.name,pool:_.map(i=>i.name),method:"POST",url:S.join(n.url,x),path:x,headers:{"content-type":"application/json",accept:y?"text/event-stream":"application/json",...s.info.headers,...l===null&&n.protocol==="anthropic.messages"?r.clientHeaders:void 0,...S.headers(n,s.info.auth,r.headers)},body:t,state:{v:1,upstream:n.name,front:e.front,native:l===null,messages:O,model:a.model,stream:y,unserved:k,convert:o},images:{sent:C.carries(t),retry:c.imageFallback&&m==="openai.chat",key:$},tools:{sent:T.carries(t),retry:c.toolFallback&&m==="openai.chat"},json:{sent:o?.jsonSchemaEmulated===!0,retry:c.jsonObjectFallback&&m==="anthropic.messages",key:$}}}h(q,"prepare");export{ue as request};
3
3
  //# sourceMappingURL=request.mjs.map
@@ -1,2 +1,2 @@
1
- var d=Object.defineProperty;var i=(t,e)=>d(t,"name",{value:e,configurable:!0});import{context as p,self as c}from"../lib/runtime.mjs";import{capture as m}from"../lib/capture.mjs";async function v(t,e){const o=m(c,p,e.front),r=o.endpoint(e.upstream),s=o.driver(r);let n=t;return s.response&&(n=await s.response(n,{endpoint:r,settings:o.settings(r),state:e})),e.native?n.model===void 0?n:{...n,model:e.model??n.model}:o.converters(r).response(n,e.convert,o.settings(r))}i(v,"response");export{v as response};
1
+ var m=Object.defineProperty;var t=(s,e)=>m(s,"name",{value:e,configurable:!0});import{context as p,self as d}from"../lib/runtime.mjs";import{capture as c}from"../lib/capture.mjs";import*as f from"../lib/vllm.mjs";async function g(s,e){const o=c(d,p,e.front),r=o.endpoint(e.upstream),i=o.driver(r);let n=s;return i.response&&(n=await i.response(n,{endpoint:r,settings:o.settings(r),state:e})),e.messages&&(n=f.response(n)),e.native?n.model===void 0?n:{...n,model:e.model??n.model}:o.converters(r).response(n,e.convert,o.settings(r))}t(g,"response");export{g as response};
2
2
  //# sourceMappingURL=response.mjs.map
package/dist/api/send.mjs CHANGED
@@ -1,2 +1,2 @@
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};
1
+ var le=Object.defineProperty;var u=(c,s)=>le(c,"name",{value:s,configurable:!0});import{context as K,self as ee}from"../lib/runtime.mjs";import{TranslatorError as z}from"../lib/errors.mjs";import*as te from"../lib/http.mjs";import*as ue from"../lib/images.mjs";import*as ce from"../lib/tools.mjs";import{parse as ae}from"../lib/upstream-error.mjs";import{meter as me,normalize as de}from"../lib/usage.mjs";import{capture as fe}from"../lib/capture.mjs";import{estimate as pe,bodyLength as ge,output as he,patch as ye,stream as we}from"../lib/estimate.mjs";import{warn as re}from"../lib/warn.mjs";import{start as be}from"../lib/record.mjs";import{emit as se}from"../lib/events.mjs";import*as ne from"../lib/pool.mjs";import{createBudget as ke}from"../lib/thinking.mjs";import*as oe from"../lib/fanout.mjs";import{refresh as je}from"../lib/health.mjs";const ve={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"};async function _e(c,s={}){const{transport:w,textOnly:f,jsonInstruction:v}=K.state,{request:_,response:$,stream:o,error:p}=ee,d=p.build,{config:i,state:b}=K,g=fe(ee,K,s.front),U=c&&typeof c=="object"&&!Array.isArray(c),T={model:null,requested:U&&typeof c.model=="string"?c.model:null,upstream:null,front:s.front??i.front,stream:U&&c.stream===!0},k=me(T,s.onUsage,(m,A)=>re(i,b,m,{type:"usage",...A,log:s.log})),q=u(m=>Object.defineProperty(m,"usage",{value:k.usage,enumerable:!1,configurable:!0}),"attach");let J=!1;try{const m=oe.wants(g,c);if(m>0){J=!0;const A=u((n,S)=>re(i,b,n,{type:"usage",...S,log:s.log}),"warning");return await oe.run({body:c,count:m,options:s,send:_e,warn:A})}return q(await $e({transport:w,textOnly:f,jsonInstruction:v,request:_,response:$,stream:o,error:p,captured:g},c,s,k))}catch(m){if(J)throw m;if(!(m instanceof z))throw s.signal?.aborted||se(g,{type:"error",kind:"exception",level:"error",message:String(m?.message??m),log:s.log}),k.finish(s.signal?.aborted?"aborted":"error"),m;return k.finish("error"),q(d(m.status,m.message,{type:m.type,code:m.code,param:m.param,front:s.front}))}}u(_e,"send");async function $e({transport:c,textOnly:s,jsonInstruction:w,request:f,response:v,stream:_,error:$,captured:o},p,d,i){const b={front:d.front,headers:d.headers,clientHeaders:d.clientHeaders,filter:d.filter,cacheSalt:d.cacheSalt,usage:!0,budget:ke(),log:d.log},g=u(t=>{se(o,{...t,log:d.log})},"announce"),U=i.meta.front==="openai"&&p?.stream_options?.include_usage!==!0,T=u(t=>{i.meta.model=t.body.model??null,i.meta.upstream=t.upstream,t.state.native?i.source(null):i.source(()=>t.state.convert?.upstreamUsage,t.state.front==="anthropic"?"openai":"folded")},"note"),k=u(t=>{if(t.state.native)return{input:!1,output:!1};const e=de(t.state.front==="anthropic"?"openai":"folded",t.state.convert?.upstreamUsage);return e===null?{input:!0,output:!0}:{input:e.input_tokens===null&&e.cache_read_input_tokens===null&&e.cache_creation_input_tokens===null,output:e.output_tokens===null}},"gaps"),q=u(async t=>{const e=o.endpoint(t.upstream),a=o.driver(e);return(a.count&&!(U&&t.state.stream)?await a.count({endpoint:e,request:t.body,transport:c,signal:d.signal,timeout:o.config.detectTimeout}):null)??pe(t.body)},"input"),J=u(async(t,e)=>{const a=k(t);return!a.input&&!a.output?null:{input_tokens:a.input?await q(t):null,output_tokens:a.output?he(e):null}},"counts"),m=u((t,e,a,r,R)=>(i.finish(R),$(t,e,a,r)),"fail"),A=u(async t=>{let e=t,a=await Q(c,e,d.signal,I());const r=a.status;if(r===404&&e.state.messages){const y=await C(a);return y?.type==="error"?{upstream:e,result:a,failure:y}:(o.state.noMessages.add(e.upstream),g({type:"fallback",kind:"messages",level:"warn",message:`Upstream ${e.upstream}: /v1/messages answered 404; sent the request converted to Chat Completions, and will do so for this upstream until the next detection. Set nativeMessages: false on this upstream to skip the first attempt.`,endpoint:e.upstream,model:e.body.model,status:r,reason:String(ae(y,r).message).trim().slice(0,500),action:"conversion"}),e=await f(p,{...b,endpoint:e.upstream,retry:!0}),T(e),{upstream:e,result:await Q(c,e,d.signal,I())})}const R=r===400&&[e.images,e.tools].some(y=>y.sent&&y.retry),F=(r===400||r===422)&&e.json.sent&&e.json.retry;if(!a.ok&&(R||F)){const y=await C(a),O=ae(y,r).message,h={};if(r===400&&e.images.sent&&e.images.retry&&ue.refused(O)?(s.add(e.images.key),h.images=!1):r===400&&e.tools.sent&&e.tools.retry&&ce.refused(O)?h.tools=!1:F&&(h.jsonObject="instruction"),!("images"in h||"tools"in h||"jsonObject"in h))return{upstream:e,result:a,failure:y};const j={upstream:e,result:a,failure:y};if("images"in h||"tools"in h){const G="images"in h?"images":"tools";g({type:"fallback",kind:G,level:"warn",message:`Upstream ${e.upstream}: ${e.state.model} refused ${G==="images"?"images":"tools"} (${r}: "${String(O).trim().slice(0,500)}"); sent the request again without them.`,endpoint:e.upstream,model:e.body.model,status:r,reason:String(O).trim().slice(0,500),action:G==="images"?"text-only":"without-tools"})}if(e=await f(p,{...b,...h,endpoint:e.upstream,retry:!0}),T(e),a=await Q(c,e,d.signal,I()),F&&"jsonObject"in h){if(!a.ok){try{await a.body?.cancel?.()}catch{}return T(j.upstream),j}w.add(j.upstream.json.key),g({type:"fallback",kind:"json_object",level:"warn",message:`Upstream ${j.upstream.upstream}: ${j.upstream.state.model} rejected the json_object schema emulation (${r}: "${String(O).trim().slice(0,500)}"); sent it as a system instruction instead. Set jsonObject: "instruction" on this upstream to skip the first attempt.`,endpoint:j.upstream.upstream,model:j.upstream.body.model,status:r,reason:String(O).trim().slice(0,500),action:"instruction"})}}return{upstream:e,result:a}},"attempt");await je(o,p,d.signal);let n=await f(p,{...b,hold:!0});const S=n.pool;let B=0;const D=S.length>1?o.config.pool:null,I=u(()=>D&&{connectTimeout:D.connectTimeout,headersTimeout:B+1<S.length?D.headersTimeout:0},"limitsNow");let E=o.endpoint(n.upstream);const V=u(()=>E.health.inflight--,"release");let l,x,W=null,X=0;const Y=u(t=>{const e=be(o,{body:p,sent:n,front:i.meta.front,requested:i.meta.requested,headers:t,log:d.log,exchange:W,attempt:X});return W??=e.id,e},"begin"),Z=u(async t=>{const e=Y();if(!e.on)return;if(l===void 0)return e.attempt({error:x.message},t);let a=null;try{a=x??await C(l)}catch{}e.attempt({status:l.status,headers:te.plain(l.headers),body:a},t)},"keep");try{for(;;){const t=E;t.health.requests++,X++,T(n);let e=null;try{({upstream:n,result:l,failure:x}=await A(n)),!l.ok&&l.status>=500&&(e=`HTTP ${l.status}`,g({type:"error",kind:"upstream",level:"error",message:`Upstream ${t.name} answered HTTP ${l.status}`,endpoint:t.name,model:n.body.model,status:l.status}))}catch(r){if(!(r instanceof z)||r.code!=="upstream_unreachable")throw r;e=r.message,l=void 0,x=r,g({type:"error",kind:"transport",level:"error",message:r.message,endpoint:t.name,model:n.body.model})}if(e===null){ne.succeeded(t);break}ne.failed(o.config,t,e);let a=null;for(;a===null&&!d.signal?.aborted&&B+1<S.length;)try{a=await f(p,{...b,endpoint:S[++B],hold:!0})}catch(r){if(!(r instanceof z)||r.code!=="endpoint_not_in_pool")throw r}if(a===null){if(l===void 0)throw await Z(),x;break}await Z(e),g({type:"failover",level:"warn",message:`Upstream ${t.name} failed (${e}); trying ${a.upstream}`,from:t.name,to:a.upstream,model:a.body.model,reason:e});try{await l?.body?.cancel?.()}catch{}V(),E=o.endpoint(a.upstream),n=a}}catch(t){throw V(),t}const ie=E;E=null,i.usage.then(()=>ie.health.inflight--);const H=te.plain(l.headers),M=Y(H);if(!l.ok){const t=x??await C(l);return M.reply(l.status,H,t),m(l.status,t,H,n.state,"upstream_error")}if(n.state.stream){const t=_(M.raw(l.body),n.state),e=n.state.native?t:we(i.meta.front,t,{gaps:u(()=>k(n),"gaps"),input:u(()=>q(n),"input"),report:u(a=>i.estimate(a),"report")});return{status:200,headers:ve,stream:M.events(i.track(e,d.signal,U))}}const P=await C(l);if(M.reply(l.status,H,P),P===null||typeof P!="object")return g({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}),m(502,"The upstream returned a body that is not JSON",H,n.state,"error");const N=await v(P,n.state),L=await J(n,ge(i.meta.front,N));return L&&(N.usage=ye(i.meta.front,N.usage,L),i.estimate(L)),i.json(N),i.finish("done"),{status:200,headers:{"content-type":"application/json"},body:N}}u($e,"round");async function Q(c,s,w,f=null){let v,_=!1,$=w;if(f&&f.headersTimeout>0){const o=new AbortController;$=w?AbortSignal.any([w,o.signal]):o.signal,v=setTimeout(()=>{_=!0,o.abort()},f.headersTimeout)}try{return await c(s.url,{method:s.method,headers:s.headers,body:JSON.stringify(s.body),signal:$,redirect:"error",...f&&f.connectTimeout>0?{connectTimeout:f.connectTimeout}:{}})}catch(o){if(w?.aborted)throw o;const p=_?`no response headers within ${f.headersTimeout} ms`:o.message;throw new z(`Could not reach the upstream (${s.upstream}): ${p}`,{code:"upstream_unreachable",status:502,details:{cause:o}})}finally{clearTimeout(v)}}u(Q,"call");async function C(c){const s=await c.text();try{return JSON.parse(s)}catch{return s===""?null:s}}u(C,"parse");export{_e as send};
2
2
  //# sourceMappingURL=send.mjs.map
@@ -1,2 +1,2 @@
1
- var c=Object.defineProperty;var o=(s,a)=>c(s,"name",{value:a,configurable:!0});import{context as e}from"../lib/runtime.mjs";import{describe as t}from"../lib/pool.mjs";function u(){const{config:s,state:a}=e;return{front:s.front,upstreams:a.endpoints.map(r=>({name:r.name,url:r.url,backend:r.backend,driver:r.driver,protocol:r.protocol,version:r.facts?.version??r.version,models:r.models??r.facts?.models??[],default:r.default,facts:r.facts,probed:r.probed,health:t(r)}))}}o(u,"status");export{u as status};
1
+ var m=Object.defineProperty;var e=(s,a)=>m(s,"name",{value:a,configurable:!0});import{context as t}from"../lib/runtime.mjs";import{describe as l}from"../lib/pool.mjs";import{resolve as o}from"../lib/settings.mjs";import{wants as f}from"../lib/vllm.mjs";function h(){const{config:s,state:a}=t;return{front:s.front,upstreams:a.endpoints.map(r=>({name:r.name,url:r.url,backend:r.backend,driver:r.driver,protocol:r.protocol,version:r.facts?.version??r.version,models:r.models??r.facts?.models??[],default:r.default,facts:r.facts,probed:r.probed,messages:r.driver==="vllm"?f(r,o(r.driverSettings,r.probed,s.settings,r.settings))&&!a.noMessages.has(r.name):null,health:l(r)}))}}e(h,"status");export{h as status};
2
2
  //# sourceMappingURL=status.mjs.map
@@ -1,2 +1,2 @@
1
- var x=Object.defineProperty;var i=(e,o)=>x(e,"name",{value:o,configurable:!0});import{context as b,self as j}from"../lib/runtime.mjs";import{capture as A}from"../lib/capture.mjs";import{messages as p}from"../lib/sse.mjs";function u(e,o){return!o||!e||typeof e!="object"?e:e.message&&typeof e.message=="object"&&e.message.model!==void 0?{...e,message:{...e.message,model:o}}:e.model===void 0?e:{...e,model:o}}i(u,"restore");function B(e){return!!e&&Array.isArray(e.choices)&&(e.choices.length===0||e.choices.some(o=>o?.finish_reason))}i(B,"ends");function k(e,o){const r=A(j,b,o.front),c=r.endpoint(o.upstream),a=r.driver(c),l=r.settings(c),f=r.converters(c),s=a.chunk,d={endpoint:c,settings:l,state:o};async function*m(){for(const n of await a.flush?.(d)??[])yield{data:n}}if(i(m,"flushed"),f&&!s)return f.stream(e,o.convert,l);if(f)return f.stream((async function*(){for await(const n of p(e))n.data!=="[DONE]"&&(yield await s(n.data,d));for await(const n of m())yield n.data})(),o.convert,l);const w=!!(s&&a.flush);return(async function*(){const n=[];let y=!1;for await(const t of p(e)){if(t.data==="[DONE]"){for await(const v of m())yield{data:u(v.data,o.model)};yield*n.splice(0),y=!0,yield t;continue}const h=s?await s(t.data,d):t.data,g={...t,data:u(h,o.model)};w&&B(h)?n.push(g):yield g}if(!y){for await(const t of m())yield{data:u(t.data,o.model)};yield*n}})()}i(k,"stream");export{k as stream};
1
+ var b=Object.defineProperty;var i=(e,o)=>b(e,"name",{value:o,configurable:!0});import{context as j,self as A}from"../lib/runtime.mjs";import{capture as B}from"../lib/capture.mjs";import{messages as v}from"../lib/sse.mjs";import*as D from"../lib/vllm.mjs";function u(e,o){return!o||!e||typeof e!="object"?e:e.message&&typeof e.message=="object"&&e.message.model!==void 0?{...e,message:{...e.message,model:o}}:e.model===void 0?e:{...e,model:o}}i(u,"restore");function E(e){return!!e&&Array.isArray(e.choices)&&(e.choices.length===0||e.choices.some(o=>o?.finish_reason))}i(E,"ends");function _(e,o){const r=B(A,j,o.front),c=r.endpoint(o.upstream),a=r.driver(c),l=r.settings(c),f=o.native?null:r.converters(c),t=a.chunk,m={endpoint:c,settings:l,state:o};async function*d(){for(const n of await a.flush?.(m)??[])yield{data:n}}if(i(d,"flushed"),f&&!t)return f.stream(e,o.convert,l);if(f)return f.stream((async function*(){for await(const n of v(e))n.data!=="[DONE]"&&(yield await t(n.data,m));for await(const n of d())yield n.data})(),o.convert,l);const w=!!(t&&a.flush),y=(async function*(){const n=[];let g=!1;for await(const s of v(e)){if(s.data==="[DONE]"){for await(const x of d())yield{data:u(x.data,o.model)};yield*n.splice(0),g=!0,yield s;continue}const h=t?await t(s.data,m):s.data,p={...s,data:u(h,o.model)};w&&E(h)?n.push(p):yield p}if(!g){for await(const s of d())yield{data:u(s.data,o.model)};yield*n}})();return o.messages?D.stream(y):y}i(_,"stream");export{_ as stream};
2
2
  //# sourceMappingURL=stream.mjs.map
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- var i=Object.defineProperty;var a=(r,t)=>i(r,"name",{value:t,configurable:!0});import{fileURLToPath as d}from"node:url";import p from"@cldmv/slothlet";import{normalize as c}from"./lib/config.mjs";import{load as l}from"./lib/pdf.mjs";import{transport as f}from"./lib/transport.mjs";import{create as m,open as u}from"./lib/events.mjs";import{warn as s}from"./lib/warn.mjs";async function w(r){const t=c(r),o={endpoints:t.endpoints,transport:t.transport??f,servers:new Set,warned:new Set,events:m(t.onEvent),textOnly:new Set,jsonInstruction:new Set,cursors:new Map,front:void 0};u(o.events,t.logs);const e=await l();e.errors.canvas&&s(t,o,`@napi-rs/canvas could not be loaded (${e.errors.canvas}); PDFs will be read as text only, without page images`,{type:"detect",kind:"canvas",reason:String(e.errors.canvas)}),e.errors.unpdf&&s(t,o,`unpdf could not be loaded (${e.errors.unpdf}); PDF documents cannot be read and fall back to a note`,{type:"detect",kind:"unpdf",reason:String(e.errors.unpdf)});const n=await p({...r.slothlet,base:d(new URL("./api",import.meta.url)),mode:"eager",context:{config:t,state:o},silent:!0});return await n.detect({network:t.detect}),n}a(w,"createTranslator");import{TranslatorError as D}from"./lib/errors.mjs";var P=w;export{D as TranslatorError,w as createTranslator,P as default};
1
+ var i=Object.defineProperty;var a=(r,e)=>i(r,"name",{value:e,configurable:!0});import{fileURLToPath as d}from"node:url";import p from"@cldmv/slothlet";import{normalize as c}from"./lib/config.mjs";import{load as l}from"./lib/pdf.mjs";import{transport as f}from"./lib/transport.mjs";import{create as m,open as u}from"./lib/events.mjs";import{warn as s}from"./lib/warn.mjs";async function w(r){const e=c(r),n={endpoints:e.endpoints,transport:e.transport??f,servers:new Set,warned:new Set,events:m(e.onEvent),textOnly:new Set,jsonInstruction:new Set,noMessages:new Set,cursors:new Map,front:void 0};u(n.events,e.logs);const t=await l();t.errors.canvas&&s(e,n,`@napi-rs/canvas could not be loaded (${t.errors.canvas}); PDFs will be read as text only, without page images`,{type:"detect",kind:"canvas",reason:String(t.errors.canvas)}),t.errors.unpdf&&s(e,n,`unpdf could not be loaded (${t.errors.unpdf}); PDF documents cannot be read and fall back to a note`,{type:"detect",kind:"unpdf",reason:String(t.errors.unpdf)});const o=await p({...r.slothlet,base:d(new URL("./api",import.meta.url)),mode:"eager",context:{config:e,state:n},silent:!0});return await o.detect({network:e.detect}),o}a(w,"createTranslator");import{TranslatorError as D}from"./lib/errors.mjs";var P=w;export{D as TranslatorError,w as createTranslator,P as default};
2
2
  //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,2 @@
1
+ var h=Object.defineProperty;var a=(e,t)=>h(e,"name",{value:t,configurable:!0});import{TranslatorError as i}from"./errors.mjs";const f=["vllm"],_=["openai"];function d(e){return e==null||typeof e=="string"&&e!==""}a(d,"valid");function w(e,t,r,l,s=[]){if(!d(t.salt))throw new i("cacheSalt: expected a non-empty string",{code:"invalid_request",param:"cacheSalt"});if(!d(t.key))throw new i("metadata.prompt_cache_key: expected a non-empty string",{code:"invalid_request",param:"metadata.prompt_cache_key"});let c=e;const u=a(n=>{if(r.unknownFields==="error")throw new i(`${n}: is not accepted by this upstream`,{code:"unsupported_field",param:n});s.includes(n)||s.push(n)},"refuse"),o=t.salt??r.cacheSalt;o!=null&&(f.includes(l)?c={...c,cache_salt:o}:u("cache_salt"));const p=t.key??r.promptCacheKey;return p!=null&&!Object.hasOwn(e,"prompt_cache_key")&&(_.includes(l)?c={...c,prompt_cache_key:p}:u("prompt_cache_key")),c}a(w,"addCache");export{_ as KEY_DRIVERS,f as SALT_DRIVERS,w as addCache,d as valid};
2
+ //# sourceMappingURL=cache.mjs.map
@@ -1,2 +1,2 @@
1
- var m=Object.defineProperty;var n=(o,r)=>m(o,"name",{value:r,configurable:!0});import{resolve as v}from"./settings.mjs";function g(o){const r=[];for(const s of Object.keys(o??{})){const e=o[s];typeof e?.info=="function"&&r.push({name:s,info:e.info(),detect:e.detect,settings:e.settings,probe:e.probe,path:e.path,request:e.request,response:e.response,chunk:e.chunk,flush:e.flush,error:e.error,count:e.token?.count,list:e.model?.list})}return r.sort((s,e)=>s.info.order-e.info.order)}n(g,"collect");function b(o,r,s){const{config:e,state:u}=r,c=o.convert.anthropic.messages.to.openai.chat,i=o.convert.openai.chat.to.anthropic.messages,a=s??e.front,f=g(o.driver),h=a==="anthropic"?"anthropic.messages":"openai.chat",d=a==="anthropic"?{request:c.request,response:i.response,stream:i.stream}:{request:i.request,response:c.response,stream:c.stream};return{config:e,state:u,front:a,drivers:f,endpoint:n(t=>u.endpoints.find(p=>p.name===t),"endpoint"),driver:n(t=>f.find(p=>p.name===t.driver),"driver"),settings:n(t=>v(t.driverSettings,t.probed,e.settings,t.settings),"settings"),converters:n(t=>t.protocol===h?null:d,"converters")}}n(b,"capture");export{b as capture,g as collect};
1
+ var b=Object.defineProperty;var s=(o,n)=>b(o,"name",{value:n,configurable:!0});import{resolve as f}from"./settings.mjs";import*as v from"./vllm.mjs";import{PROTOCOL as k}from"./ollama.mjs";function x(o){const n=[];for(const a of Object.keys(o??{})){const t=o[a];typeof t?.info=="function"&&n.push({name:a,info:t.info(),detect:t.detect,settings:t.settings,probe:t.probe,path:t.path,request:t.request,response:t.response,chunk:t.chunk,flush:t.flush,error:t.error,count:t.token?.count,list:t.model?.list})}return n.sort((a,t)=>a.info.order-t.info.order)}s(x,"collect");function w(o,n,a){const{config:t,state:i}=n,p=o.convert.anthropic.messages.to.openai.chat,u=o.convert.openai.chat.to.anthropic.messages,g={anthropic:o.convert.anthropic.messages.to.ollama.chat,openai:o.convert.openai.chat.to.ollama.chat},m={anthropic:o.convert.ollama.chat.to.anthropic.messages,openai:o.convert.ollama.chat.to.openai.chat},r=a??t.front,l=x(o.driver),d=r==="anthropic"?"anthropic.messages":"openai.chat",q=r==="anthropic"?{request:p.request,response:u.response,stream:u.stream}:{request:u.request,response:p.response,stream:p.stream},O={request:g[r].request,response:m[r].response,stream:m[r].stream};return{config:t,state:i,front:r,drivers:l,endpoint:s(e=>i.endpoints.find(c=>c.name===e),"endpoint"),driver:s(e=>l.find(c=>c.name===e.driver),"driver"),settings:s(e=>f(e.driverSettings,e.probed,t.settings,e.settings),"settings"),converters:s((e,c)=>{if(e.protocol===k)return O;if(e.protocol===d)return null;if(r==="anthropic"&&c!==void 0&&e.driver==="vllm"&&!i.noMessages.has(e.name)){const h=f(e.driverSettings,e.probed,t.settings,e.settings);if(v.wants(e,h)&&v.unsuited(c,h)===null)return null}return q},"converters")}}s(w,"capture");export{w as capture,x as collect};
2
2
  //# sourceMappingURL=capture.mjs.map
@@ -1,2 +1,2 @@
1
- var i=Object.defineProperty;var n=(t,r)=>i(t,"name",{value:r,configurable:!0});import{TranslatorError as o}from"./errors.mjs";import*as a from"./pool.mjs";import{valid as u}from"./record.mjs";import{normalizeAliases as l,normalizeEfforts as p}from"./effort.mjs";function d(t){let r=t.length;for(;t[r-1]==="/";)r--;const e=t.slice(0,r);return e.endsWith("/v1")?e.slice(0,-3):e}n(d,"baseUrl");function m(t,r){const e=typeof t=="string"?{url:t}:t;if(!e||typeof e!="object"||typeof e.url!="string"||e.url==="")throw new o(`upstreams[${r}] needs a "url"`,{code:"INVALID_CONFIG",status:500});for(const s of["apiVersion","deployment"])if(e[s]!==void 0&&(typeof e[s]!="string"||e[s]===""))throw new o(`upstreams[${r}].${s} must be a non-empty string`,{code:"INVALID_CONFIG",status:500});return{name:e.name??`upstream-${r}`,url:d(e.url),apiKey:e.apiKey,headers:{...e.headers},apiVersion:e.apiVersion,deployment:e.deployment,backend:e.backend??"auto",models:Array.isArray(e.models)?[...e.models]:void 0,default:e.default===!0,settings:{...e.settings},version:e.version,driver:null,facts:null,driverSettings:{},probed:{},health:a.health()}}n(m,"endpoint");function y(t={}){const r=t.front??"anthropic";if(r!=="anthropic"&&r!=="openai")throw new o(`front must be "anthropic" or "openai" (got ${JSON.stringify(r)})`,{code:"INVALID_CONFIG",status:500});let e=t.upstreams??t.upstream;if(e!==void 0&&!Array.isArray(e)&&(e=[e]),!e||e.length===0)throw new o("at least one upstream is required (config.upstreams)",{code:"INVALID_CONFIG",status:500});const s=t.logs??!1;if(typeof s!="boolean"&&(typeof s!="string"||s===""))throw new o("logs must be true, false or a file path (got "+JSON.stringify(s)+")",{code:"INVALID_CONFIG",status:500});if(t.onEvent!==void 0&&typeof t.onEvent!="function")throw new o("onEvent must be a function",{code:"INVALID_CONFIG",status:500});if(!u(t.capture))throw new o("capture must be false, a directory path or a function (got "+JSON.stringify(t.capture)+")",{code:"INVALID_CONFIG",status:500});return{front:r,endpoints:e.map(m),...l(t.aliases),efforts:p(t.efforts),pool:a.normalize(t.pool),settings:{...t.settings},detect:t.detect!==!1,detectTimeout:t.detectTimeout??5e3,transport:t.transport,logs:s,capture:t.capture||!1,events:t.events!==!1,onEvent:t.onEvent}}n(y,"normalize");export{m as endpoint,y as normalize};
1
+ var i=Object.defineProperty;var a=(t,r)=>i(t,"name",{value:r,configurable:!0});import{TranslatorError as o}from"./errors.mjs";import*as n from"./pool.mjs";import{valid as p}from"./record.mjs";import{normalizeAliases as u,normalizeEfforts as l}from"./effort.mjs";function d(t){let r=t.length;for(;t[r-1]==="/";)r--;const e=t.slice(0,r);return e.endsWith("/v1")?e.slice(0,-3):e}a(d,"baseUrl");function f(t,r){const e=typeof t=="string"?{url:t}:t;if(!e||typeof e!="object"||typeof e.url!="string"||e.url==="")throw new o(`upstreams[${r}] needs a "url"`,{code:"INVALID_CONFIG",status:500});for(const s of["apiVersion","deployment"])if(e[s]!==void 0&&(typeof e[s]!="string"||e[s]===""))throw new o(`upstreams[${r}].${s} must be a non-empty string`,{code:"INVALID_CONFIG",status:500});if(e.api!==void 0&&e.api!=="openai"&&e.api!=="native")throw new o(`upstreams[${r}].api must be "openai" or "native" (got ${JSON.stringify(e.api)})`,{code:"INVALID_CONFIG",status:500});return{name:e.name??`upstream-${r}`,url:d(e.url),apiKey:e.apiKey,headers:{...e.headers},apiVersion:e.apiVersion,deployment:e.deployment,api:e.api??"openai",backend:e.backend??"auto",models:Array.isArray(e.models)?[...e.models]:void 0,default:e.default===!0,settings:{...e.settings},version:e.version,driver:null,facts:null,driverSettings:{},probed:{},health:n.health()}}a(f,"endpoint");function y(t={}){const r=t.front??"anthropic";if(r!=="anthropic"&&r!=="openai")throw new o(`front must be "anthropic" or "openai" (got ${JSON.stringify(r)})`,{code:"INVALID_CONFIG",status:500});let e=t.upstreams??t.upstream;if(e!==void 0&&!Array.isArray(e)&&(e=[e]),!e||e.length===0)throw new o("at least one upstream is required (config.upstreams)",{code:"INVALID_CONFIG",status:500});const s=t.logs??!1;if(typeof s!="boolean"&&(typeof s!="string"||s===""))throw new o("logs must be true, false or a file path (got "+JSON.stringify(s)+")",{code:"INVALID_CONFIG",status:500});if(t.onEvent!==void 0&&typeof t.onEvent!="function")throw new o("onEvent must be a function",{code:"INVALID_CONFIG",status:500});if(!p(t.capture))throw new o("capture must be false, a directory path or a function (got "+JSON.stringify(t.capture)+")",{code:"INVALID_CONFIG",status:500});return{front:r,endpoints:e.map(f),...u(t.aliases),efforts:l(t.efforts),pool:n.normalize(t.pool),settings:{...t.settings},detect:t.detect!==!1,detectTimeout:t.detectTimeout??5e3,transport:t.transport,logs:s,capture:t.capture||!1,events:t.events!==!1,onEvent:t.onEvent}}a(y,"normalize");export{f as endpoint,y as normalize};
2
2
  //# sourceMappingURL=config.mjs.map
@@ -0,0 +1,2 @@
1
+ var g=Object.defineProperty;var i=(r,o)=>g(r,"name",{value:o,configurable:!0});import{atLeast as h}from"./normalize.mjs";import{TranslatorError as l}from"./errors.mjs";const u=["json","regex","choice","grammar"],j="0.11.0",x=i(r=>r.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),"escape");function y(r){if(r==null)return null;if(typeof r!="object"||Array.isArray(r))return`expected an object with one of ${u.join(", ")}`;const o=Object.keys(r).filter(t=>r[t]!==void 0&&r[t]!==null);if(o.length!==1||!u.includes(o[0]))return`expected exactly one of ${u.join(", ")}`;const[n]=o,e=r[n];return n==="json"&&(typeof e!="object"||Array.isArray(e))?"json must be a JSON Schema object":(n==="regex"||n==="grammar")&&(typeof e!="string"||e==="")?`${n} must be a non-empty string`:n==="choice"&&!(Array.isArray(e)&&e.length>0&&e.every(t=>typeof t=="string"&&t!==""))?"choice must be a non-empty list of non-empty strings":null}i(y,"check");function _(r,o,n,e){const t=i(()=>`(?:${e.map(x).join("|")})`,"alternation"),f={type:"json_schema",json_schema:{name:"constraint",schema:e,strict:!0}};switch(r){case"vllm":case"compatible":return h(o,j)?{structured_outputs:{[n]:e}}:{[`guided_${n}`]:e};case"sglang":return n==="json"?{response_format:f}:n==="regex"?{regex:e}:n==="choice"?{regex:t()}:{ebnf:e};case"llamacpp":return n==="json"?{json_schema:e}:n==="grammar"?{grammar:e}:n==="choice"?{grammar:`root ::= ${e.map(s=>JSON.stringify(s)).join(" | ")}`}:null;case"tgi":return n==="json"?{response_format:{type:"json",value:e}}:n==="regex"?{response_format:{type:"regex",value:e}}:n==="choice"?{response_format:{type:"regex",value:t()}}:null;default:return n==="json"?{response_format:f}:null}}i(_,"fields");function A(r,o,n,e,t=[]){const f=y(o);if(f!==null)throw new l(`constraint: ${f}`,{code:"invalid_request",param:"constraint"});const s=o??n.constraint;if(s==null)return r;const a=u.find(c=>s[c]!==void 0&&s[c]!==null),p=i(c=>{t.includes(c)||t.push(c)},"note"),m=_(e.name,e.version,a,s[a]);if(m===null){if(n.unknownFields==="error")throw new l(`constraint.${a}: is not accepted by this upstream`,{code:"unsupported_field",param:`constraint.${a}`});return p(`constraint.${a}`),r}return["vllm","compatible"].includes(e.name)&&Array.isArray(r.tools)&&r.tools.length>0&&r.tool_choice!=="none"?(p(`constraint.${a}`),r):Object.keys(m).some(c=>Object.hasOwn(r,c))?r:{...r,...m}}i(A,"addConstraint");export{u as KINDS,j as STRUCTURED_OUTPUTS_VERSION,A as addConstraint,y as check,_ as fields};
2
+ //# sourceMappingURL=constraint.mjs.map
@@ -0,0 +1,2 @@
1
+ var m=Object.defineProperty;var u=(e,o)=>m(e,"name",{value:o,configurable:!0});import{estimate as x,tokens as d}from"./estimate.mjs";import{restoreName as y}from"./ids.mjs";const v=1e5,k=3,a="[Tool result cleared to save context]",c=u((e,o)=>e!==null&&typeof e=="object"&&o.includes(e.type)&&Number.isInteger(e.value)&&e.value>=0,"threshold");function N(e){return e.trigger!==void 0&&!c(e.trigger,["input_tokens","tool_uses"])||e.keep!==void 0&&!c(e.keep,["tool_uses"])||e.clear_at_least!==void 0&&!c(e.clear_at_least,["input_tokens"])||e.exclude_tools!==void 0&&!(Array.isArray(e.exclude_tools)&&e.exclude_tools.every(o=>typeof o=="string"))?!1:e.clear_tool_inputs===void 0||typeof e.clear_tool_inputs=="boolean"}u(N,"valid");function D(e,o,p){const l=[];for(const t of o.messages)if(t.role==="assistant")for(const n of t.tool_calls??[])l.push({call:n,result:o.messages.find(r=>r.role==="tool"&&r.tool_call_id===n.id)});const f=e.trigger??{type:"input_tokens",value:v};if((f.type==="tool_uses"?l.length:x(o))<=f.value)return null;const g=e.keep?.value??k,h=new Set(e.exclude_tools??[]),s=l.slice(0,Math.max(0,l.length-g)).filter(t=>t.result!==void 0&&t.result.content!==a&&!h.has(y(p,t.call.function.name))),i=[];for(const t of s){const n=JSON.stringify([t.result.content,e.clear_tool_inputs===!0?t.call.function.arguments:""]).length,r=JSON.stringify([a,e.clear_tool_inputs===!0?"{}":""]).length;i.push(Math.max(0,n-r))}const _=d(i.reduce((t,n)=>t+n,0));if(s.length===0||_<(e.clear_at_least?.value??0))return null;for(const t of s)t.result.content=a,e.clear_tool_inputs===!0&&(t.call.function.arguments="{}");return{type:e.type,cleared_tool_uses:s.length,cleared_input_tokens:_}}u(D,"clearToolUses");export{k as DEFAULT_KEEP,v as DEFAULT_TRIGGER,a as PLACEHOLDER,D as clearToolUses,N as valid};
2
+ //# sourceMappingURL=context.mjs.map
@@ -0,0 +1,2 @@
1
+ var d=Object.defineProperty;var l=(e,a)=>d(e,"name",{value:a,configurable:!0});import*as i from"./http.mjs";import{emit as u}from"./events.mjs";import{pick as c}from"./route.mjs";async function m(e,a,n,o){const t=a.health,s=await i.json(e.state.transport,i.join(a.url,n.info.paths.health),{method:"GET",headers:i.headers(a,n.info.auth),signal:o,timeout:e.config.pool.healthTimeout});if(o?.aborted)return;const r=t.ready;t.checkedAt=Date.now(),s.ok?(t.ready=!0,t.readyReason=null):s.status===0||s.status>=500?(t.ready=!1,t.readyReason=s.status===0?String(s.error?.message??"no answer"):`HTTP ${s.status}`):(t.ready=null,t.readyReason=null,t.unsupported=!0),t.ready===!1&&r!==!1?u(e,{type:"health",kind:"down",level:"warn",message:`Upstream ${a.name} is not ready (${t.readyReason}); requests go to other endpoints that serve the same model first`,endpoint:a.name,reason:t.readyReason}):t.ready===!0&&r===!1&&u(e,{type:"health",kind:"up",level:"info",message:`Upstream ${a.name} is ready again`,endpoint:a.name})}l(m,"run");async function p(e,a,n={}){const o=Date.now(),{healthInterval:t}=e.config.pool;await Promise.all(a.map(s=>{const r=s.health,f=e.driver(s);if(!f.info.paths?.health)return;if(n.force)r.unsupported=!1;else if(r.unsupported||t<=0)return;const h=Math.max(r.checkedAt??0,r.ready===!1?0:r.lastSuccess??0);if(!(!n.force&&o-h<t))return r.checking??=m(e,s,f,n.signal).finally(()=>{r.checking=null}),r.checking}))}l(p,"probe");async function w(e,a,n){if(e.config.pool.healthInterval<=0||!a||typeof a!="object")return;const{pool:o}=c(e.config,e.state.endpoints,a.model,e.state);o.length>1&&await p(e,o,{signal:n})}l(w,"refresh");export{p as probe,w as refresh};
2
+ //# sourceMappingURL=health.mjs.map
package/dist/lib/http.mjs CHANGED
@@ -1,2 +1,2 @@
1
- var l=Object.defineProperty;var o=(t,e)=>l(t,"name",{value:e,configurable:!0});function y(t,e){return t+e}o(y,"join");async function k(t,e,a,n){return t.path?t.path(e,{endpoint:a,model:n}):t.info.paths[e]??p[e]}o(k,"pathOf");const p={embeddings:"/v1/embeddings",models:"/v1/models"};function m(t){return t==="localhost"||t==="::1"||/^127\./.test(t)}o(m,"isLoopback");function d(t,e="bearer",a){const n={...t.headers};return t.apiKey&&(e==="x-api-key"||e==="api-key"?n[e]=t.apiKey:n.authorization=`Bearer ${t.apiKey}`),{...n,...a}}o(d,"headers");function S({endpoint:t,transport:e,signal:a,timeout:n,probes:i},c,s){const u=s!==void 0&&(s.auth==="x-api-key"||s.auth==="api-key"||s.headers!==void 0),r=u?{auth:s.auth,extra:s.headers}:{},h=o(()=>g(e,y(t.url,c),{method:"GET",headers:d(t,r.auth,r.extra),signal:a,timeout:n}),"run");if(!i)return h();const f=u?JSON.stringify([c,r.auth,r.extra]):c;return i.has(f)||i.set(f,h()),i.get(f)}o(S,"probe");function x(t){const e={};if(!t)return e;if(typeof t.forEach=="function")return t.forEach((a,n)=>{e[n.toLowerCase()]=a}),e;for(const[a,n]of Object.entries(t))e[a.toLowerCase()]=String(n);return e}o(x,"plain");async function g(t,e,a={}){const{timeout:n,signal:i,body:c,...s}=a,u=[];i&&u.push(i),n>0&&u.push(AbortSignal.timeout(n));try{const r=await t(e,{...s,redirect:"error",body:c===void 0?void 0:JSON.stringify(c),signal:u.length>1?AbortSignal.any(u):u[0]}),h=await r.text();let f=null;try{f=JSON.parse(h)}catch{f=h===""?null:h}return{ok:r.ok,status:r.status,body:f,headers:x(r.headers)}}catch(r){return{ok:!1,status:0,body:null,error:r}}}o(g,"json");export{d as headers,m as isLoopback,y as join,g as json,k as pathOf,x as plain,S as probe};
1
+ var h=Object.defineProperty;var r=(t,e)=>h(t,"name",{value:e,configurable:!0});function y(t,e){return t+e}r(y,"join");async function m(t,e,a,n){return t.path?t.path(e,{endpoint:a,model:n}):(t.info.native!==void 0&&a.protocol===t.info.native.protocol?t.info.native.paths:void 0)?.[e]??t.info.paths[e]??p[e]}r(m,"pathOf");const p={embeddings:"/v1/embeddings",models:"/v1/models"};function k(t){return t==="localhost"||t==="::1"||/^127\./.test(t)}r(k,"isLoopback");function d(t,e="bearer",a){const n={...t.headers};return t.apiKey&&(e==="x-api-key"||e==="api-key"?n[e]=t.apiKey:n.authorization=`Bearer ${t.apiKey}`),{...n,...a}}r(d,"headers");function S({endpoint:t,transport:e,signal:a,timeout:n,probes:s},c,u){const i=u!==void 0&&(u.auth==="x-api-key"||u.auth==="api-key"||u.headers!==void 0),o=i?{auth:u.auth,extra:u.headers}:{},l=r(()=>g(e,y(t.url,c),{method:"GET",headers:d(t,o.auth,o.extra),signal:a,timeout:n}),"run");if(!s)return l();const f=i?JSON.stringify([c,o.auth,o.extra]):c;return s.has(f)||s.set(f,l()),s.get(f)}r(S,"probe");function x(t){const e={};if(!t)return e;if(typeof t.forEach=="function")return t.forEach((a,n)=>{e[n.toLowerCase()]=a}),e;for(const[a,n]of Object.entries(t))e[a.toLowerCase()]=String(n);return e}r(x,"plain");async function g(t,e,a={}){const{timeout:n,signal:s,body:c,...u}=a,i=[];s&&i.push(s),n>0&&i.push(AbortSignal.timeout(n));try{const o=await t(e,{...u,redirect:"error",body:c===void 0?void 0:JSON.stringify(c),signal:i.length>1?AbortSignal.any(i):i[0]}),l=await o.text();let f=null;try{f=JSON.parse(l)}catch{f=l===""?null:l}return{ok:o.ok,status:o.status,body:f,headers:x(o.headers)}}catch(o){return{ok:!1,status:0,body:null,error:o}}}r(g,"json");export{d as headers,k as isLoopback,y as join,g as json,m as pathOf,x as plain,S as probe};
2
2
  //# sourceMappingURL=http.mjs.map
@@ -1,2 +1,2 @@
1
- var l=Object.defineProperty;var t=(r,o)=>l(r,"name",{value:o,configurable:!0});import*as e from"./http.mjs";function u(r){return!r.ok||!Array.isArray(r.body?.data)?null:r.body.data.filter(o=>o&&typeof o.id=="string")}t(u,"modelsOf");async function h({endpoint:r,transport:o,signal:a,timeout:d,auth:n,extraHeaders:s,path:i="/v1/models"}){const f=await e.json(o,e.join(r.url,i),{method:"GET",headers:e.headers(r,n,s),signal:a,timeout:d});return u(f)}t(h,"fetchModels");export{h as fetchModels,u as modelsOf};
1
+ var c=Object.defineProperty;var r=(t,a)=>c(t,"name",{value:a,configurable:!0});import*as s from"./http.mjs";function l(t){return!t.ok||!Array.isArray(t.body?.data)?null:t.body.data.filter(a=>a&&typeof a.id=="string")}r(l,"modelsOf");function i(t){return t.filter(a=>typeof a?.parent=="string"&&a.parent!=="").map(a=>({id:a.id,parent:a.parent}))}r(i,"adaptersOf");function u(t){const a=new Set((t.facts?.adapters??[]).map(e=>e.id));return(t.models??t.facts?.models??[]).filter(e=>!a.has(e))}r(u,"baseModels");function h(t){const a=new Map((t.facts?.adapters??[]).map(e=>[e.id,e.parent]));return(t.models??t.facts?.models??[]).map(e=>a.has(e)?{id:e,parent:a.get(e)}:{id:e})}r(h,"knownModels");async function x({endpoint:t,transport:a,signal:e,timeout:o,auth:n,extraHeaders:p,path:f="/v1/models"}){const d=await s.json(a,s.join(t.url,f),{method:"GET",headers:s.headers(t,n,p),signal:e,timeout:o});return l(d)}r(x,"fetchModels");export{i as adaptersOf,u as baseModels,x as fetchModels,h as knownModels,l as modelsOf};
2
2
  //# sourceMappingURL=models.mjs.map
@@ -0,0 +1,4 @@
1
+ var i=Object.defineProperty;var o=(e,s)=>i(e,"name",{value:s,configurable:!0});import{items as c}from"./source.mjs";import{TranslatorError as d}from"./errors.mjs";function l(e){try{return JSON.parse(e)}catch{throw new d("Upstream sent a stream line that is not valid JSON",{code:"invalid_stream_data",status:502,details:{data:e}})}}o(l,"decode");function f(){let e="";const s=o((r,t)=>{const n=r.replace(/\r$/,"").trim();n!==""&&t.push(l(n))},"lines");return{push(r){const t=[];e+=r;let n=0;for(let a=e.indexOf(`
2
+ `);a!==-1;a=e.indexOf(`
3
+ `,n))s(e.slice(n,a),t),n=a+1;return e=e.slice(n),t},flush(){const r=[];return s(e,r),e="",r}}}o(f,"createParser");async function*h(e){const s=f(),r=new TextDecoder;for await(const t of c(e))typeof t=="string"||t instanceof Uint8Array?yield*s.push(typeof t=="string"?t:r.decode(t,{stream:!0})):yield t;yield*s.push(r.decode()),yield*s.flush()}o(h,"parse");export{f as createParser,h as parse};
4
+ //# sourceMappingURL=ndjson.mjs.map
@@ -0,0 +1,3 @@
1
+ var v=Object.defineProperty;var r=(t,o)=>v(t,"name",{value:o,configurable:!0});import{TranslatorError as F}from"./errors.mjs";import{callId as S,completionId as k}from"./ids.mjs";import{parseArguments as D}from"./arguments.mjs";import{reasoningOf as E}from"./thinking.mjs";const z="ollama.chat",x=["temperature","top_p","top_k","min_p","seed","frequency_penalty","presence_penalty"],L=new Set(["model","messages","stream","tools","tool_choice","max_tokens","max_completion_tokens","stop","response_format","reasoning_effort","chat_template_kwargs","think","options","format","keep_alive",...x,"repetition_penalty"]),M=new Set(["stream_options","user","metadata","store","service_tier"]),T="[An image was omitted because Ollama accepts base64 images only and the translator does not fetch URLs.]",O={none:!1,minimal:"low",low:"low",medium:"medium",high:"high",xhigh:"high",max:"max"};function h(t,o){t.includes(o)||t.push(o)}r(h,"drop");const A=r(t=>t!==null&&typeof t=="object"&&!Array.isArray(t),"isObject");function I(t,o){if(typeof t=="string")return{text:t,images:[]};const n=[],i=[];for(const e of Array.isArray(t)?t:[])if(e?.type==="text"&&typeof e.text=="string")n.push(e.text);else if(e?.type==="image_url"){const a=String(e.image_url?.url??e.image_url??""),c=/^data:[^;,]*;base64,(.*)$/s.exec(a);c?i.push(c[1]):(n.push(T),h(o,"image_url"))}else h(o,`content.${e?.type??"part"}`);return{text:n.join(`
2
+ `),images:i}}r(I,"contentOf");function R(t,o){return t.map(n=>(typeof n.id=="string"&&o.set(n.id,n.function?.name),{type:"function",function:{name:n.function?.name,arguments:D(n.function?.arguments)}}))}r(R,"callsOf");function U(t,o,n){const{text:i,images:e}=I(t?.content,n);if(t?.role==="tool"){const c=o.get(t.tool_call_id)??t.name;return{role:"tool",...typeof c=="string"?{tool_name:c}:{},content:i}}const a={role:t?.role==="developer"?"system":t?.role,content:i};if(e.length>0&&(a.images=e),t?.role==="assistant"){const c=E(t);c!==""&&(a.thinking=c),Array.isArray(t.tool_calls)&&t.tool_calls.length>0&&(a.tool_calls=R(t.tool_calls,o))}return a}r(U,"messageOf");function C(t){if(typeof t.reasoning_effort=="string")return Object.hasOwn(O,t.reasoning_effort)?O[t.reasoning_effort]:t.reasoning_effort;if(t.think!==void 0)return t.think;const o=t.chat_template_kwargs?.enable_thinking;return typeof o=="boolean"?o:void 0}r(C,"thinkOf");function H(t,o){const n={};for(const[e,a]of Object.entries(o))n[e==="repetition_penalty"?"repeat_penalty":e]=a;for(const e of x)t[e]!==void 0&&t[e]!==null&&(n[e]=t[e]);t.repetition_penalty!==void 0&&(n.repeat_penalty=t.repetition_penalty);const i=t.max_completion_tokens??t.max_tokens;return i!=null&&(n.num_predict=i),t.stop!==void 0&&t.stop!==null&&(n.stop=Array.isArray(t.stop)?t.stop:[t.stop]),A(t.options)?{...n,...t.options}:n}r(H,"optionsOf");function $(t){if(t.format!==void 0)return t.format;const o=t.response_format;if(o?.type==="json_object")return"json";if(o?.type==="json_schema"&&A(o.json_schema?.schema))return o.json_schema.schema}r($,"formatOf");function G(t,o={}){const n=o.dropped??[],i=new Map,e={model:t.model,messages:(Array.isArray(t.messages)?t.messages:[]).map(s=>U(s,i,n)),stream:t.stream===!0};Array.isArray(t.tools)&&t.tools.length>0&&t.tool_choice!=="none"?(e.tools=t.tools.map(s=>({type:"function",function:{name:s.function?.name,description:s.function?.description,parameters:s.function?.parameters??{type:"object",properties:{}}}})),t.tool_choice!==void 0&&t.tool_choice!=="auto"&&h(n,"tool_choice")):t.tool_choice==="none"&&Array.isArray(t.tools)&&h(n,"tools");const a=H(t,o.sampling??{});Object.keys(a).length>0&&(e.options=a);const c=$(t);c!==void 0&&(e.format=c);const f=C(t);f!==void 0&&(e.think=f),t.keep_alive!==void 0&&(e.keep_alive=t.keep_alive);for(const s of Object.keys(t))L.has(s)||M.has(s)||s==="n"&&!(t.n>1)||h(n,s);return e}r(G,"chatBody");function j(t,o=0){return(Array.isArray(t)?t:[]).map((n,i)=>{const e=n?.function?.arguments;return{index:o+i,id:typeof n?.id=="string"&&n.id!==""?n.id:S(),type:"function",function:{name:n?.function?.name,arguments:typeof e=="string"?e:JSON.stringify(e??{})}}})}r(j,"callsOfReply");function y(t,o){return o?"tool_calls":t?.done_reason==="length"?"length":"stop"}r(y,"finishOf");function w(t){const o=t?.prompt_eval_count,n=t?.eval_count;if(!Number.isFinite(o)&&!Number.isFinite(n))return;const i=Number.isFinite(o)?o:0,e=Number.isFinite(n)?n:0;return{prompt_tokens:i,completion_tokens:e,total_tokens:i+e}}r(w,"usageOf");function Q(t){return t===void 0?void 0:{input_tokens:t.prompt_tokens,output_tokens:t.completion_tokens}}r(Q,"folded");function N(t){return typeof t?.error=="string"?t.error:null}r(N,"errorOf");function V(t,o={}){const n=N(t);if(n!==null)throw new F(n,{code:"upstream_error",status:502});const i=t?.message??{},e=j(i.tool_calls),a={role:"assistant",content:typeof i.content=="string"?i.content:""};typeof i.thinking=="string"&&i.thinking!==""&&(a[o.reasoningField??"reasoning"]=i.thinking),e.length>0&&(a.tool_calls=e.map(s=>({id:s.id,type:s.type,function:s.function})));const c=w(t),f=Date.parse(t?.created_at);return{id:k(),object:"chat.completion",created:Number.isNaN(f)?Math.floor(Date.now()/1e3):Math.floor(f/1e3),model:t?.model,choices:[{index:0,message:a,finish_reason:y(t,e.length>0)}],...c===void 0?{}:{usage:c}}}r(V,"completion");function W(t={}){const o=k(),n=Math.floor(Date.now()/1e3),i=t.reasoningField??"reasoning";let e=t.model,a=!1,c=0,f=!1,s;const g=r((u,_=null)=>({id:o,object:"chat.completion.chunk",created:n,model:e,choices:[{index:0,delta:u,finish_reason:_}]}),"make");return{line(u){const _=N(u);if(_!==null)return f=!0,[{error:{message:_,type:"api_error",param:null,code:null}}];e=t.model??u?.model??e;const p=[];a||(a=!0,p.push(g({role:"assistant",content:""})));const l=u?.message??{},m={};typeof l.thinking=="string"&&l.thinking!==""&&(m[i]=l.thinking),typeof l.content=="string"&&l.content!==""&&(m.content=l.content);const d=j(l.tool_calls,c);return d.length>0&&(c+=d.length,m.tool_calls=d),Object.keys(m).length>0&&p.push(g(m)),u?.done===!0&&(f=!0,s=w(u),p.push(g({},y(u,c>0))),t.includeUsage!==!1&&s!==void 0&&p.push({id:o,object:"chat.completion.chunk",created:n,model:e,choices:[],usage:s})),p},finished:r(()=>f,"finished"),usage:r(()=>s,"usage")}}r(W,"createChunker");export{z as PROTOCOL,j as callsOfReply,G as chatBody,V as completion,W as createChunker,N as errorOf,y as finishOf,Q as folded,w as usageOf};
3
+ //# sourceMappingURL=ollama.mjs.map
@@ -1,2 +1,2 @@
1
- var i=Object.defineProperty;var a=(t,o)=>i(t,"name",{value:o,configurable:!0});const p=["model","messages","stream","stream_options","max_tokens","max_completion_tokens","temperature","top_p","n","stop","presence_penalty","frequency_penalty","logit_bias","logprobs","top_logprobs","seed","response_format","tools","tool_choice","parallel_tool_calls","reasoning_effort","user","metadata","store","service_tier"],m=["top_k","min_p","repetition_penalty","length_penalty","min_tokens","stop_token_ids","include_stop_str_in_output","ignore_eos","skip_special_tokens","spaces_between_special_tokens","allowed_token_ids","bad_words","prompt_logprobs","truncate_prompt_tokens","add_generation_prompt","continue_final_message","add_special_tokens","chat_template_kwargs","thinking_token_budget","include_reasoning","structured_outputs"],l=new Set(p),c=new Set(["include_usage","continuous_usage_stats"]);function d(t,o=[]){const s={};for(const[e,n]of Object.entries(t))if(!(!l.has(e)&&!o.includes(e)))if(e==="stream_options"){const _=Object.fromEntries(Object.entries(n??{}).filter(([r])=>c.has(r)));Object.keys(_).length>0&&(s.stream_options=_)}else s[e]=n;return s}a(d,"filterChatBody");export{m as VLLM_FIELDS,d as filterChatBody};
1
+ var a=Object.defineProperty;var i=(t,o)=>a(t,"name",{value:o,configurable:!0});const p=["model","messages","stream","stream_options","max_tokens","max_completion_tokens","temperature","top_p","n","stop","presence_penalty","frequency_penalty","logit_bias","logprobs","top_logprobs","seed","response_format","tools","tool_choice","parallel_tool_calls","reasoning_effort","user","metadata","store","service_tier"],d=["top_k","min_p","repetition_penalty","length_penalty","min_tokens","stop_token_ids","include_stop_str_in_output","ignore_eos","skip_special_tokens","spaces_between_special_tokens","allowed_token_ids","bad_words","prompt_logprobs","truncate_prompt_tokens","add_generation_prompt","continue_final_message","add_special_tokens","chat_template_kwargs","thinking_token_budget","include_reasoning","structured_outputs","guided_json","guided_regex","guided_choice","guided_grammar"],c=new Set(p),l=new Set(["include_usage","continuous_usage_stats"]);function g(t,o=[]){const s={};for(const[e,n]of Object.entries(t))if(!(!c.has(e)&&!o.includes(e)))if(e==="stream_options"){const _=Object.fromEntries(Object.entries(n??{}).filter(([r])=>l.has(r)));Object.keys(_).length>0&&(s.stream_options=_)}else s[e]=n;return s}i(g,"filterChatBody");export{d as VLLM_FIELDS,g as filterChatBody};
2
2
  //# sourceMappingURL=passthrough.mjs.map
package/dist/lib/pool.mjs CHANGED
@@ -1,2 +1,2 @@
1
- var p=Object.defineProperty;var c=(t,r)=>p(t,"name",{value:r,configurable:!0});import{TranslatorError as g}from"./errors.mjs";const b=Object.freeze(["failover","round_robin","least_busy"]),m=Object.freeze({base:1e3,max:6e4}),d=Object.freeze({connect:1e4,headers:0});function T(){return{failures:0,until:0,inflight:0,requests:0,errors:0,lastError:null,lastFailure:null,lastSuccess:null}}c(T,"health");function S(t={}){const r=c(u=>new g(`pool: ${u}`,{code:"INVALID_CONFIG",status:500}),"bad");if(!t||typeof t!="object"||Array.isArray(t))throw r("must be an object");const e=t.strategy??"failover",s={...t.models};for(const[u,l]of[["strategy",e],...Object.entries(s).map(([n,f])=>[`models.${n}`,f])])if(!b.includes(l))throw r(`${u} must be one of ${b.join(", ")} (got ${JSON.stringify(l)})`);const o=t.backoff?.base??m.base,a=t.backoff?.max??m.max;if(!(Number.isFinite(o)&&o>0)||!(Number.isFinite(a)&&a>=o))throw r("backoff.base must be a positive number of ms and backoff.max at least backoff.base");const h=t.connectTimeout??d.connect,i=t.headersTimeout??d.headers;for(const u of["connectTimeout","headersTimeout"]){const l=u==="connectTimeout"?h:i;if(typeof l!="number"||!(Number.isFinite(l)&&l>=0))throw r(`${u} must be a number of ms, 0 or more (got ${JSON.stringify(l)})`)}return{strategy:e,backoff:{base:o,max:a},connectTimeout:h,headersTimeout:i,models:s}}c(S,"normalize");function F(t,r,e,s,o,a=Date.now()){const h=t.pool.models[s]??t.pool.strategy,i=e.filter(n=>n.health.until<=a),u=e.filter(n=>n.health.until>a).sort((n,f)=>n.health.until-f.health.until);let l=i;if(h==="least_busy")l=[...i].sort((n,f)=>n.health.inflight-f.health.inflight);else if(h==="round_robin"&&i.length>0){const n=r.cursors.get(s)??0;o&&r.cursors.set(s,n+1);const f=n%i.length;l=[...i.slice(f),...i.slice(0,f)]}return[...l,...u]}c(F,"order");function O(t,r,e,s=Date.now()){const o=r.health;o.failures++,o.errors++,o.lastError=e,o.lastFailure=s;const{base:a,max:h}=t.pool.backoff;o.until=s+Math.min(h,a*2**Math.min(o.failures-1,30))}c(O,"failed");function E(t,r=Date.now()){const e=t.health;e.failures=0,e.until=0,e.lastSuccess=r}c(E,"succeeded");function j(t,r=Date.now()){const e=t.health,s=c(a=>a===null?null:new Date(a).toISOString(),"iso"),o=e.until>r;return{healthy:!o,failures:e.failures,retryAt:o?s(e.until):null,inflight:e.inflight,requests:e.requests,errors:e.errors,lastError:e.lastError,lastFailure:s(e.lastFailure),lastSuccess:s(e.lastSuccess)}}c(j,"describe");export{m as BACKOFF,b as STRATEGIES,d as TIMEOUTS,j as describe,O as failed,T as health,S as normalize,F as order,E as succeeded};
1
+ var k=Object.defineProperty;var i=(t,r)=>k(t,"name",{value:r,configurable:!0});import{TranslatorError as S}from"./errors.mjs";const d=Object.freeze(["failover","round_robin","least_busy"]),m=Object.freeze({base:1e3,max:6e4}),y=Object.freeze({connect:1e4,headers:0}),p=Object.freeze({interval:1e4,timeout:2e3});function F(){return{failures:0,until:0,inflight:0,requests:0,errors:0,lastError:null,lastFailure:null,lastSuccess:null,ready:null,checkedAt:null,readyReason:null,unsupported:!1,checking:null}}i(F,"health");function j(t={}){const r=i(a=>new S(`pool: ${a}`,{code:"INVALID_CONFIG",status:500}),"bad");if(!t||typeof t!="object"||Array.isArray(t))throw r("must be an object");const e=t.strategy??"failover",s={...t.models};for(const[a,f]of[["strategy",e],...Object.entries(s).map(([x,O])=>[`models.${x}`,O])])if(!d.includes(f))throw r(`${a} must be one of ${d.join(", ")} (got ${JSON.stringify(f)})`);const o=t.backoff?.base??m.base,l=t.backoff?.max??m.max;if(!(Number.isFinite(o)&&o>0)||!(Number.isFinite(l)&&l>=o))throw r("backoff.base must be a positive number of ms and backoff.max at least backoff.base");const u=t.connectTimeout??y.connect,c=t.headersTimeout??y.headers,b=t.healthInterval??p.interval,h=t.healthTimeout??p.timeout,n={connectTimeout:u,headersTimeout:c,healthInterval:b,healthTimeout:h};for(const a of Object.keys(n)){const f=n[a];if(typeof f!="number"||!(Number.isFinite(f)&&f>=0))throw r(`${a} must be a number of ms, 0 or more (got ${JSON.stringify(f)})`)}return{strategy:e,backoff:{base:o,max:l},connectTimeout:u,headersTimeout:c,healthInterval:b,healthTimeout:h,models:s}}i(j,"normalize");function g(t,r){return t.health.until<=r&&t.health.ready!==!1}i(g,"available");function A(t,r,e,s,o,l=Date.now()){const u=t.pool.models[s]??t.pool.strategy,c=e.filter(n=>g(n,l)),b=e.filter(n=>!g(n,l)).sort((n,a)=>n.health.until-a.health.until);let h=c;if(u==="least_busy")h=[...c].sort((n,a)=>n.health.inflight-a.health.inflight);else if(u==="round_robin"&&c.length>0){const n=r.cursors.get(s)??0;o&&r.cursors.set(s,n+1);const a=n%c.length;h=[...c.slice(a),...c.slice(0,a)]}return[...h,...b]}i(A,"order");function E(t,r,e,s=Date.now()){const o=r.health;o.failures++,o.errors++,o.lastError=e,o.lastFailure=s;const{base:l,max:u}=t.pool.backoff;o.until=s+Math.min(u,l*2**Math.min(o.failures-1,30))}i(E,"failed");function I(t,r=Date.now()){const e=t.health;e.failures=0,e.until=0,e.lastSuccess=r,e.ready===!1&&(e.ready=!0)}i(I,"succeeded");function N(t,r=Date.now()){const e=t.health,s=i(l=>l===null?null:new Date(l).toISOString(),"iso"),o=e.until>r;return{healthy:!o&&e.ready!==!1,ready:e.ready,readyReason:e.ready===!1?e.readyReason:null,checkedAt:s(e.checkedAt),failures:e.failures,retryAt:o?s(e.until):null,inflight:e.inflight,requests:e.requests,errors:e.errors,lastError:e.lastError,lastFailure:s(e.lastFailure),lastSuccess:s(e.lastSuccess)}}i(N,"describe");export{m as BACKOFF,p as PROBES,d as STRATEGIES,y as TIMEOUTS,N as describe,E as failed,F as health,j as normalize,A as order,I as succeeded};
2
2
  //# sourceMappingURL=pool.mjs.map
@@ -0,0 +1,2 @@
1
+ var f=Object.defineProperty;var s=(e,t)=>f(e,"name",{value:t,configurable:!0});import{noEmptyTools as d}from"./normalize.mjs";const p={chat:"/v1/chat/completions",embeddings:"/v1/embeddings",models:"/v1/models"},c={chat:"/chat/completions",embeddings:"/embeddings",models:"/models"};function m(e){return/^grok-(3-mini|4|code)/.test(String(e))&&!/non-reasoning/.test(String(e))}s(m,"grokReasons");const l=Object.freeze({groq:{order:21,hosts:["api.groq.com"],root:"/openai",paths:p,settings:{reasoningField:"reasoning",maxTokensField:"max_completion_tokens",thinking:"none",thinkingBudget:!1,effort:!0,topK:!1,prefill:"none",streamUsage:!0,continuousUsage:!1},drop:["logprobs","top_logprobs","logit_bias"],dropName:!0},together:{order:22,hosts:["api.together.ai","api.together.xyz","us-api.together.ai"],paths:p,fields:["top_k","min_p","repetition_penalty"],settings:{reasoningField:"reasoning",reasoningRoundTrip:!0,maxTokensField:"max_tokens",thinking:"none",thinkingBudget:!1,effort:!0,topK:!0,prefill:"none",parallelToolCalls:!1,streamUsage:!1,continuousUsage:!1}},fireworks:{order:23,hosts:["api.fireworks.ai"],root:"/inference",paths:p,fields:["top_k","min_p","repetition_penalty"],settings:{reasoningField:"reasoning_content",reasoningRoundTrip:!0,maxTokensField:"max_tokens",thinking:"reasoning_effort",thinkingBudget:!1,effort:!0,topK:!0,prefill:"none",streamUsage:!0,continuousUsage:!1}},xai:{order:24,hosts:["api.x.ai"],paths:p,settings:{reasoningField:"reasoning_content",maxTokensField:"max_completion_tokens",thinking:"none",thinkingBudget:!1,effort:!0,topK:!1,prefill:"none",streamUsage:!0,continuousUsage:!1},dropWhen:s(e=>m(e.model)?["stop","presence_penalty","frequency_penalty"]:[],"dropWhen")},gemini:{order:25,hosts:["generativelanguage.googleapis.com"],root:"/v1beta/openai",paths:c,settings:{maxTokensField:"max_tokens",thinking:"none",thinkingBudget:!1,effort:!0,topK:!1,prefill:"none",streamUsage:!0,continuousUsage:!1}}});function _(e){const t=l[e],o={name:e,protocol:"openai.chat",order:t.order,auth:"bearer",strict:!1,hosts:[...t.hosts],paths:{...t.paths}};return t.fields?{...o,fields:[...t.fields]}:o}s(_,"describe");function x(e,t){const o=URL.canParse(t.url)?new URL(t.url).hostname:"";return l[e].hosts.includes(o)?{host:o}:null}s(x,"match");function U(e){return{...l[e].settings}}s(U,"settingsOf");function b(e,t,o){const i=l[e],a=i.root??"";return(o.url.endsWith(a)?"":a)+i.paths[t]}s(b,"route");function y(e,t,o){const i=l[e],a=o?.state?.dropped,g=s(n=>{Array.isArray(a)&&!a.includes(n)&&a.push(n)},"note"),r={...d(t)};for(const n of[...i.drop??[],...i.dropWhen?.(t)??[]])r[n]!==void 0&&(delete r[n],g(n));return i.dropName&&Array.isArray(r.messages)&&r.messages.some(n=>n&&"name"in n)&&(r.messages=r.messages.map(n=>{const u={...n};return delete u.name,u}),g("messages[].name")),r}s(y,"trim");export{l as PRESETS,_ as describe,x as match,b as route,U as settingsOf,y as trim};
2
+ //# sourceMappingURL=presets.mjs.map
@@ -1,2 +1,2 @@
1
- var m=Object.defineProperty;var u=(e,n)=>m(e,"name",{value:n,configurable:!0});import{TranslatorError as f}from"./errors.mjs";import{order as v}from"./pool.mjs";function h(e,n){return(e.models??e.facts?.models??[]).includes(n)?!0:e.protocol==="anthropic.messages"&&e.models===void 0&&/^claude-/.test(n)}u(h,"serves");function w(e,n,t,o,i=!1){const a=Object.hasOwn(e.aliases,t),r=a?e.aliases[t]:t,l=a&&Object.hasOwn(e.pins,t)?e.pins[t]:void 0,c=n.filter(d=>h(d,r)),s=n.find(d=>d.default)??n[0];if(c.length>0){const d=v(e,o,c,r,i);return{endpoint:d[0],model:r,unserved:!1,pool:d,pin:l}}const p=s.models??s.facts?.models??[];return p.length===1?{endpoint:s,model:p[0],unserved:!1,pool:[s],pin:l}:{endpoint:s,model:r,unserved:!0,pool:[s],pin:l}}u(w,"pick");function _(e,n,t={}){if(!n||typeof n!="object"||Array.isArray(n))throw new f("The request body must be a JSON object",{code:"invalid_body",status:400});const o=w(e.config,e.state.endpoints,n.model,e.state,t.advance===!0);if(t.endpoint!==void 0){const r=o.pool.find(l=>l.name===t.endpoint);if(!r)throw new f(`Endpoint ${t.endpoint} no longer serves ${o.model}`,{code:"endpoint_not_in_pool",status:500});return{endpoint:r,model:o.model,requested:n.model,unserved:o.unserved,pin:o.pin,convert:e.converters(r),pool:[r]}}const{endpoint:i,pool:a}=o;return{endpoint:i,model:o.model,requested:n.model,unserved:o.unserved,pin:o.pin,convert:e.converters(i),pool:a}}u(_,"resolve");export{w as pick,_ as resolve,h as serves};
1
+ var m=Object.defineProperty;var u=(e,n)=>m(e,"name",{value:n,configurable:!0});import{TranslatorError as f}from"./errors.mjs";import{order as v}from"./pool.mjs";import{baseModels as h}from"./models.mjs";function w(e,n){return(e.models??e.facts?.models??[]).includes(n)?!0:e.protocol==="anthropic.messages"&&e.models===void 0&&/^claude-/.test(n)}u(w,"serves");function O(e,n,t,r,l=!1){const a=Object.hasOwn(e.aliases,t),o=a?e.aliases[t]:t,d=a&&Object.hasOwn(e.pins,t)?e.pins[t]:void 0,c=n.filter(s=>w(s,o)),i=n.find(s=>s.default)??n[0];if(c.length>0){const s=v(e,r,c,o,l);return{endpoint:s[0],model:o,unserved:!1,pool:s,pin:d}}const p=h(i);return p.length===1?{endpoint:i,model:p[0],unserved:!1,pool:[i],pin:d}:{endpoint:i,model:o,unserved:!0,pool:[i],pin:d}}u(O,"pick");function x(e,n,t={}){if(!n||typeof n!="object"||Array.isArray(n))throw new f("The request body must be a JSON object",{code:"invalid_body",status:400});const r=O(e.config,e.state.endpoints,n.model,e.state,t.advance===!0);if(t.endpoint!==void 0){const o=r.pool.find(d=>d.name===t.endpoint);if(!o)throw new f(`Endpoint ${t.endpoint} no longer serves ${r.model}`,{code:"endpoint_not_in_pool",status:500});return{endpoint:o,model:r.model,requested:n.model,unserved:r.unserved,pin:r.pin,convert:e.converters(o,n),pool:[o]}}const{endpoint:l,pool:a}=r;return{endpoint:l,model:r.model,requested:n.model,unserved:r.unserved,pin:r.pin,convert:e.converters(l,n),pool:a}}u(x,"resolve");export{O as pick,x as resolve,w as serves};
2
2
  //# sourceMappingURL=route.mjs.map
@@ -1,2 +1,2 @@
1
- var S=Object.defineProperty;var c=(e,o)=>S(e,"name",{value:o,configurable:!0});import{timingSafeEqual as H}from"node:crypto";import{TranslatorError as w}from"./errors.mjs";import{messageId as O}from"./ids.mjs";import{sign as T}from"./thinking.mjs";const j=64*1024*1024,C={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"},p=Symbol("stalled"),v=Symbol("gone");async function D(e,o){if(!(o>0))return e;let a;const l=new Promise(g=>{a=setTimeout(g,o,p)});try{return await Promise.race([e,l])}finally{clearTimeout(a)}}c(D,"settle");async function*I(e,o,a){let l=!1;try{const g=o.front==="anthropic";g&&(yield{event:"message_start",data:{type:"message_start",message:{id:O(),type:"message",role:"assistant",model:a,content:[],stop_reason:null,stop_sequence:null,usage:{input_tokens:0,output_tokens:0,cache_creation_input_tokens:0,cache_read_input_tokens:0}}}},yield{event:"content_block_start",data:{type:"content_block_start",index:0,content_block:{type:"thinking",thinking:"",signature:""}}});const s=await e,b=c(function*(){yield{event:"content_block_delta",data:{type:"content_block_delta",index:0,delta:{type:"signature_delta",signature:T()}}},yield{event:"content_block_stop",data:{type:"content_block_stop",index:0}}},"closeEarly");if(s.stream){l=!0;let u=null;for await(const i of s.stream){if(!g){yield i;continue}if(i.event!=="message_start"){if(u===null&&i.event==="content_block_start"&&i.data.content_block.type==="thinking"){u=0;continue}u===null&&(yield*b(),u=1),yield u>0&&typeof i.data?.index=="number"?{...i,data:{...i.data,index:i.data.index+u}}:i}}return}g&&(yield*b()),yield*x(o,s.body)}finally{l||e.then(g=>g.stream?.return?.(),()=>{})}}c(I,"late");function*x(e,o){if(e.front==="anthropic"){yield{event:"error",data:o};return}yield{data:o},yield{data:"[DONE]"}}c(x,"failure");async function E(e,o,a,l,g,s=200,b=C){e.hijack();const u=e.raw;u.writeHead(s,b);try{await o.write(u,l,{front:o.front,interval:g})}catch(i){if(!a.signal.aborted){const f=i instanceof w;f||(e.log.error(i),o.emit({type:"error",kind:"route",level:"error",message:String(i?.message??i),log:!1}));const{body:_}=o.build(f?i.status:502,f?i.message:"Internal server error",f?{type:i.type,code:i.code}:{});for(const t of x(o,_))u.write(o.encode(t))}}return u.end(),e}c(E,"stream");function L(e,o){const a=Buffer.from(e),l=Buffer.from(o);return a.length===l.length&&H(a,l)}c(L,"same");function U(e){const o=/^Bearer\s+(.+)$/i.exec(e.authorization??"");return e["x-api-key"]||o?.[1]||""}c(U,"keyOf");function z(e){const o={};for(const[a,l]of Object.entries(e))(a==="anthropic-beta"||a==="anthropic-version"||a.startsWith("x-claude-code-"))&&(o[a]=String(l));return o}c(z,"forwardable");function B(e){if(e===!0)return{ok:!0,context:void 0};if(e&&typeof e=="object"&&e.ok===!0)return{ok:!0,context:e.context};const o=e&&typeof e=="object"&&e.ok===!1?e:{};return{ok:!1,status:Number.isInteger(o.status)&&o.status>=400&&o.status<=599?o.status:401,message:typeof o.message=="string"&&o.message!==""?o.message:"unauthorized"}}c(B,"verdictOf");function R(e,o,a){const{front:l,filter:g}=a,s={...a,build:c((t,n,r={})=>a.build(t,n,{...r,front:l}),"build"),send:c((t,n)=>a.send(t,{...n,front:l,filter:g}),"send"),route:c(t=>a.route(t,{front:l}),"route"),count:c((t,n)=>a.count(t,{...n,front:l}),"count"),embed:c((t,n)=>a.embed(t,n),"embed"),list:c(t=>a.list({...t,front:l}),"list"),get:c((t,n)=>a.get(t,{...n,front:l}),"get")},b=[o.apiKey??[]].flat().filter(Boolean),u=o.bodyLimit??j,i=new WeakMap,f=c((t,n)=>t.code(n.status).headers(n.headers).send(n.body),"reject");e.setErrorHandler((t,n,r)=>{if(t instanceof w)return f(r,s.build(t.status,t.message,{type:t.type,code:t.code,param:t.param}));const d=t.statusCode??500;return d<500?f(r,s.build(d,t.message)):(n.log.error(t),s.emit({type:"error",kind:"route",level:"error",message:String(t.message),status:d,log:!1}),f(r,s.build(d,"Internal server error")))}),e.addHook("onRequest",async(t,n)=>{if(b.length===0||t.routeOptions.config?.public)return;const r=U(t.headers);if(!(r&&b.some(d=>L(r,d))))return f(n,s.build(401,"invalid x-api-key"))}),typeof o.authorize=="function"&&e.addHook("preHandler",async(t,n)=>{if(t.routeOptions.config?.public)return;let r=null;if(t.body&&typeof t.body=="object"&&!Array.isArray(t.body))try{r=s.route(t.body)}catch{}let d;try{d=B(await o.authorize({method:t.method,url:t.url,headers:t.headers,body:t.body,front:s.front,route:r}))}catch(m){t.log.error(m),s.emit({type:"error",kind:"route",level:"error",message:String(m?.message??m),log:!1}),d={ok:!1,status:401,message:"unauthorized"}}if(!d.ok)return f(n,s.build(d.status,d.message));i.set(t,d.context)}),e.route({method:["GET","HEAD"],url:"/",config:{public:!0},handler:c(async(t,n)=>n.send({ok:!0}),"handler")}),e.route({method:["GET","HEAD"],url:"/api/hello",config:{public:!0},handler:c(async(t,n)=>n.send({ok:!0}),"handler")}),e.get("/v1/models",async(t,n)=>{const r=new AbortController;return n.raw.on("close",()=>r.abort()),s.list({signal:r.signal})}),e.get("/v1/models/*",async(t,n)=>{const r=new AbortController;return n.raw.on("close",()=>r.abort()),s.get(t.params["*"],{signal:r.signal})});const _=s.front==="anthropic"?"/v1/messages":"/v1/chat/completions";e.post(_,{bodyLimit:u},async(t,n)=>{if(n.raw.destroyed)return n;const r=new AbortController;n.raw.on("close",()=>{n.raw.writableFinished||r.abort()});const d=typeof o.onUsage=="function"?h=>o.onUsage(h,i.get(t)):void 0,m=s.interval(t.body),k=s.send(t.body,{signal:r.signal,clientHeaders:z(t.headers),onUsage:d,log:t.log});k.catch(()=>{});const A=new Promise(h=>r.signal.addEventListener("abort",()=>h(v),{once:!0})),y=await D(Promise.race([k,A]),t.body?.stream===!0?m:0);return y===v?(k.then(h=>h.stream?.return?.(),()=>{}),n):y===p?E(n,s,r,I(k,s,t.body?.model),m):y.stream?E(n,s,r,y.stream,m,y.status,y.headers):f(n,y)}),s.front==="openai"&&e.post("/v1/embeddings",{bodyLimit:u},async(t,n)=>{const r=new AbortController;n.raw.on("close",()=>{n.raw.writableFinished||r.abort()});const d=typeof o.onUsage=="function"?m=>o.onUsage(m,i.get(t)):void 0;return f(n,await s.embed(t.body,{signal:r.signal,onUsage:d,log:t.log}))}),s.front==="anthropic"&&e.post("/v1/messages/count_tokens",{bodyLimit:u},async(t,n)=>{const r=new AbortController;return n.raw.on("close",()=>r.abort()),s.count(t.body,{signal:r.signal})})}c(R,"register");export{I as late,R as register};
1
+ var H=Object.defineProperty;var c=(e,o)=>H(e,"name",{value:o,configurable:!0});import{timingSafeEqual as O}from"node:crypto";import{TranslatorError as p}from"./errors.mjs";import{messageId as T}from"./ids.mjs";import{sign as j}from"./thinking.mjs";const C=64*1024*1024,D={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"},v=Symbol("stalled"),x=Symbol("gone");async function I(e,o){if(!(o>0))return e;let s;const d=new Promise(f=>{s=setTimeout(f,o,v)});try{return await Promise.race([e,d])}finally{clearTimeout(s)}}c(I,"settle");async function*L(e,o,s){let d=!1;try{const f=o.front==="anthropic";f&&(yield{event:"message_start",data:{type:"message_start",message:{id:T(),type:"message",role:"assistant",model:s,content:[],stop_reason:null,stop_sequence:null,usage:{input_tokens:0,output_tokens:0,cache_creation_input_tokens:0,cache_read_input_tokens:0}}}},yield{event:"content_block_start",data:{type:"content_block_start",index:0,content_block:{type:"thinking",thinking:"",signature:""}}});const a=await e,b=c(function*(){yield{event:"content_block_delta",data:{type:"content_block_delta",index:0,delta:{type:"signature_delta",signature:j()}}},yield{event:"content_block_stop",data:{type:"content_block_stop",index:0}}},"closeEarly");if(a.stream){d=!0;let u=null;for await(const i of a.stream){if(!f){yield i;continue}if(i.event!=="message_start"){if(u===null&&i.event==="content_block_start"&&i.data.content_block.type==="thinking"){u=0;continue}u===null&&(yield*b(),u=1),yield u>0&&typeof i.data?.index=="number"?{...i,data:{...i.data,index:i.data.index+u}}:i}}return}f&&(yield*b()),yield*S(o,a.body)}finally{d||e.then(f=>f.stream?.return?.(),()=>{})}}c(L,"late");function*S(e,o){if(e.front==="anthropic"){yield{event:"error",data:o};return}yield{data:o},yield{data:"[DONE]"}}c(S,"failure");async function E(e,o,s,d,f,a=200,b=D){e.hijack();const u=e.raw;u.writeHead(a,b);try{await o.write(u,d,{front:o.front,interval:f})}catch(i){if(!s.signal.aborted){const y=i instanceof p;y||(e.log.error(i),o.emit({type:"error",kind:"route",level:"error",message:String(i?.message??i),log:!1}));const{body:g}=o.build(y?i.status:502,y?i.message:"Internal server error",y?{type:i.type,code:i.code}:{});for(const w of S(o,g))u.write(o.encode(w))}}return u.end(),e}c(E,"stream");function U(e,o){const s=Buffer.from(e),d=Buffer.from(o);return s.length===d.length&&O(s,d)}c(U,"same");function z(e){const o=/^Bearer\s+(.+)$/i.exec(e.authorization??"");return e["x-api-key"]||o?.[1]||""}c(z,"keyOf");function B(e){const o={};for(const[s,d]of Object.entries(e))(s==="anthropic-beta"||s==="anthropic-version"||s.startsWith("x-claude-code-"))&&(o[s]=String(d));return o}c(B,"forwardable");function P(e){if(e===!0)return{ok:!0,context:void 0};if(e&&typeof e=="object"&&e.ok===!0)return{ok:!0,context:e.context,cacheSalt:e.cacheSalt};const o=e&&typeof e=="object"&&e.ok===!1?e:{};return{ok:!1,status:Number.isInteger(o.status)&&o.status>=400&&o.status<=599?o.status:401,message:typeof o.message=="string"&&o.message!==""?o.message:"unauthorized"}}c(P,"verdictOf");function R(e,o,s){const{front:d,filter:f}=s,a={...s,build:c((t,n,r={})=>s.build(t,n,{...r,front:d}),"build"),send:c((t,n)=>s.send(t,{...n,front:d,filter:f}),"send"),route:c(t=>s.route(t,{front:d}),"route"),count:c((t,n)=>s.count(t,{...n,front:d}),"count"),embed:c((t,n)=>s.embed(t,n),"embed"),list:c(t=>s.list({...t,front:d}),"list"),get:c((t,n)=>s.get(t,{...n,front:d}),"get")},b=[o.apiKey??[]].flat().filter(Boolean),u=o.bodyLimit??C,i=new WeakMap,y=new WeakMap,g=c((t,n)=>t.code(n.status).headers(n.headers).send(n.body),"reject");e.setErrorHandler((t,n,r)=>{if(t instanceof p)return g(r,a.build(t.status,t.message,{type:t.type,code:t.code,param:t.param}));const l=t.statusCode??500;return l<500?g(r,a.build(l,t.message)):(n.log.error(t),a.emit({type:"error",kind:"route",level:"error",message:String(t.message),status:l,log:!1}),g(r,a.build(l,"Internal server error")))}),e.addHook("onRequest",async(t,n)=>{if(b.length===0||t.routeOptions.config?.public)return;const r=z(t.headers);if(!(r&&b.some(l=>U(r,l))))return g(n,a.build(401,"invalid x-api-key"))}),typeof o.authorize=="function"&&e.addHook("preHandler",async(t,n)=>{if(t.routeOptions.config?.public)return;let r=null;if(t.body&&typeof t.body=="object"&&!Array.isArray(t.body))try{r=a.route(t.body)}catch{}let l;try{l=P(await o.authorize({method:t.method,url:t.url,headers:t.headers,body:t.body,front:a.front,route:r}))}catch(m){t.log.error(m),a.emit({type:"error",kind:"route",level:"error",message:String(m?.message??m),log:!1}),l={ok:!1,status:401,message:"unauthorized"}}if(!l.ok)return g(n,a.build(l.status,l.message));i.set(t,l.context),typeof l.cacheSalt=="string"&&l.cacheSalt!==""&&y.set(t,l.cacheSalt)}),e.route({method:["GET","HEAD"],url:"/",config:{public:!0},handler:c(async(t,n)=>n.send({ok:!0}),"handler")}),e.route({method:["GET","HEAD"],url:"/api/hello",config:{public:!0},handler:c(async(t,n)=>n.send({ok:!0}),"handler")}),e.get("/v1/models",async(t,n)=>{const r=new AbortController;return n.raw.on("close",()=>r.abort()),a.list({signal:r.signal})}),e.get("/v1/models/*",async(t,n)=>{const r=new AbortController;return n.raw.on("close",()=>r.abort()),a.get(t.params["*"],{signal:r.signal})});const w=a.front==="anthropic"?"/v1/messages":"/v1/chat/completions";e.post(w,{bodyLimit:u},async(t,n)=>{if(n.raw.destroyed)return n;const r=new AbortController;n.raw.on("close",()=>{n.raw.writableFinished||r.abort()});const l=typeof o.onUsage=="function"?k=>o.onUsage(k,i.get(t)):void 0,m=a.interval(t.body),_=a.send(t.body,{signal:r.signal,clientHeaders:B(t.headers),cacheSalt:y.get(t),onUsage:l,log:t.log});_.catch(()=>{});const A=new Promise(k=>r.signal.addEventListener("abort",()=>k(x),{once:!0})),h=await I(Promise.race([_,A]),t.body?.stream===!0?m:0);return h===x?(_.then(k=>k.stream?.return?.(),()=>{}),n):h===v?E(n,a,r,L(_,a,t.body?.model),m):h.stream?E(n,a,r,h.stream,m,h.status,h.headers):g(n,h)}),a.front==="openai"&&e.post("/v1/embeddings",{bodyLimit:u},async(t,n)=>{const r=new AbortController;n.raw.on("close",()=>{n.raw.writableFinished||r.abort()});const l=typeof o.onUsage=="function"?m=>o.onUsage(m,i.get(t)):void 0;return g(n,await a.embed(t.body,{signal:r.signal,onUsage:l,log:t.log}))}),a.front==="anthropic"&&e.post("/v1/messages/count_tokens",{bodyLimit:u},async(t,n)=>{const r=new AbortController;return n.raw.on("close",()=>r.abort()),a.count(t.body,{signal:r.signal})})}c(R,"register");export{L as late,R as register};
2
2
  //# sourceMappingURL=routes.mjs.map
@@ -1,2 +1,2 @@
1
- var l=Object.defineProperty;var i=(a,e)=>l(a,"name",{value:e,configurable:!0});import{TranslatorError as r}from"./errors.mjs";const s=Object.freeze({inlineSystem:"auto",stripBillingHeader:!0,reasoningField:"reasoning",thinkTags:!0,thinkOpen:!1,reasoningRoundTrip:!1,thinking:"chat_template_kwargs",thinkingBudget:!1,effort:!1,images:!0,imageFallback:!0,toolFallback:!1,documents:"extract",documentConverter:null,pdf:Object.freeze({pages:!0,maxPages:100,maxBytes:32*1024*1024,maxImages:20,scale:1.5}),toolSchema:"pass",deferredTools:"forward",toolSearch:"expand",jsonObject:"schema",jsonObjectFallback:!0,toolErrors:!1,toolErrorMarker:"[error] ",toolErrorInference:!1,maxTokensField:"max_completion_tokens",streamUsage:!0,continuousUsage:!1,prefill:"continue_final_message",topK:!0,responseFormat:!0,parallelToolCalls:!0,unknownFields:"drop",sampling:Object.freeze({}),pingInterval:1e4,toolStreaming:"buffer",effortPassthrough:!1,passthroughFilter:!1,contextWindow:0,defaultMaxTokens:8192}),f={inlineSystem:["auto","keep","hoist","user","drop"],reasoningField:["reasoning","reasoning_content"],thinking:["chat_template_kwargs","reasoning_effort","none"],documents:["extract","note","error"],toolSchema:["pass","sanitize"],deferredTools:["forward","drop"],toolSearch:["expand","inline","native"],jsonObject:["schema","instruction","drop"],maxTokensField:["max_completion_tokens","max_tokens"],prefill:["continue_final_message","none"],unknownFields:["drop","error"],toolStreaming:["buffer","stream"]},c=["effortPassthrough","passthroughFilter","imageFallback","toolFallback","jsonObjectFallback","thinkOpen","reasoningRoundTrip","toolErrorInference"];function p(...a){const e={...s,pdf:{...s.pdf},sampling:{...s.sampling}};for(const t of a)if(t)for(const[n,o]of Object.entries(t))o!==void 0&&(n==="pdf"?Object.assign(e.pdf,o):n==="sampling"&&o!==null&&typeof o=="object"&&!Array.isArray(o)?Object.assign(e.sampling,o):e[n]=o);for(const[t,n]of Object.entries(f))if(!n.includes(e[t]))throw new r(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected one of ${n.join(", ")})`,{code:"invalid_setting",status:500,details:{key:t,allowed:n}});for(const t of c)if(typeof e[t]!="boolean")throw new r(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected true or false)`,{code:"invalid_setting",status:500,details:{key:t}});if(typeof e.toolErrorMarker!="string"||e.toolErrorMarker.trim()==="")throw new r(`Invalid setting toolErrorMarker: ${JSON.stringify(e.toolErrorMarker)} (expected a non-empty string)`,{code:"invalid_setting",status:500,details:{key:"toolErrorMarker"}});if(e.sampling===null||typeof e.sampling!="object"||Array.isArray(e.sampling))throw new r(`Invalid setting sampling: ${JSON.stringify(e.sampling)} (expected an object of field names and values)`,{code:"invalid_setting",status:500,details:{key:"sampling"}});if(!(Number.isFinite(e.pdf.scale)&&e.pdf.scale>0))throw new r(`Invalid setting pdf.scale: ${JSON.stringify(e.pdf.scale)} (expected a positive number)`,{code:"invalid_setting",status:500,details:{key:"pdf.scale"}});return e}i(p,"resolve");export{s as DEFAULTS,p as resolve};
1
+ var f=Object.defineProperty;var l=(o,e)=>f(o,"name",{value:e,configurable:!0});import{TranslatorError as n}from"./errors.mjs";import{check as c}from"./constraint.mjs";import{valid as d}from"./cache.mjs";const r=Object.freeze({inlineSystem:"auto",stripBillingHeader:!0,reasoningField:"reasoning",thinkTags:!0,thinkOpen:!1,reasoningRoundTrip:!1,thinking:"chat_template_kwargs",thinkingBudget:!1,effort:!1,images:!0,imageFallback:!0,toolFallback:!1,documents:"extract",documentConverter:null,pdf:Object.freeze({pages:!0,maxPages:100,maxBytes:32*1024*1024,maxImages:20,scale:1.5}),toolSchema:"pass",deferredTools:"forward",toolSearch:"expand",jsonObject:"schema",jsonObjectFallback:!0,toolErrors:!1,toolErrorMarker:"[error] ",toolErrorInference:!1,maxTokensField:"max_completion_tokens",streamUsage:!0,continuousUsage:!1,prefill:"continue_final_message",topK:!0,responseFormat:!0,parallelToolCalls:!0,unknownFields:"drop",sampling:Object.freeze({}),constraint:null,cacheSalt:null,promptCacheKey:null,pingInterval:1e4,toolStreaming:"buffer",effortPassthrough:!1,passthroughFilter:!1,nativeMessages:!1,contextWindow:0,defaultMaxTokens:8192}),g={inlineSystem:["auto","keep","hoist","user","drop"],reasoningField:["reasoning","reasoning_content"],thinking:["chat_template_kwargs","reasoning_effort","none"],documents:["extract","note","error"],toolSchema:["pass","sanitize"],deferredTools:["forward","drop"],toolSearch:["expand","inline","native"],jsonObject:["schema","instruction","drop"],maxTokensField:["max_completion_tokens","max_tokens"],prefill:["continue_final_message","none"],unknownFields:["drop","error"],toolStreaming:["buffer","stream"]},p=["effortPassthrough","passthroughFilter","imageFallback","toolFallback","jsonObjectFallback","thinkOpen","reasoningRoundTrip","toolErrorInference"];function y(...o){const e={...r,pdf:{...r.pdf},sampling:{...r.sampling}};for(const t of o)if(t)for(const[s,a]of Object.entries(t))a!==void 0&&(s==="pdf"?Object.assign(e.pdf,a):s==="sampling"&&a!==null&&typeof a=="object"&&!Array.isArray(a)?Object.assign(e.sampling,a):e[s]=a);for(const[t,s]of Object.entries(g))if(!s.includes(e[t]))throw new n(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected one of ${s.join(", ")})`,{code:"invalid_setting",status:500,details:{key:t,allowed:s}});for(const t of p)if(typeof e[t]!="boolean")throw new n(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected true or false)`,{code:"invalid_setting",status:500,details:{key:t}});if(e.nativeMessages!==!0&&e.nativeMessages!==!1&&e.nativeMessages!=="auto")throw new n(`Invalid setting nativeMessages: ${JSON.stringify(e.nativeMessages)} (expected true, false or "auto")`,{code:"invalid_setting",status:500,details:{key:"nativeMessages"}});if(typeof e.toolErrorMarker!="string"||e.toolErrorMarker.trim()==="")throw new n(`Invalid setting toolErrorMarker: ${JSON.stringify(e.toolErrorMarker)} (expected a non-empty string)`,{code:"invalid_setting",status:500,details:{key:"toolErrorMarker"}});if(e.sampling===null||typeof e.sampling!="object"||Array.isArray(e.sampling))throw new n(`Invalid setting sampling: ${JSON.stringify(e.sampling)} (expected an object of field names and values)`,{code:"invalid_setting",status:500,details:{key:"sampling"}});const i=c(e.constraint);if(i!==null)throw new n(`Invalid setting constraint: ${JSON.stringify(e.constraint)} (${i})`,{code:"invalid_setting",status:500,details:{key:"constraint"}});for(const t of["cacheSalt","promptCacheKey"])if(!d(e[t]))throw new n(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected a non-empty string or null)`,{code:"invalid_setting",status:500,details:{key:t}});if(!(Number.isFinite(e.pdf.scale)&&e.pdf.scale>0))throw new n(`Invalid setting pdf.scale: ${JSON.stringify(e.pdf.scale)} (expected a positive number)`,{code:"invalid_setting",status:500,details:{key:"pdf.scale"}});return e}l(y,"resolve");export{r as DEFAULTS,y as resolve};
2
2
  //# sourceMappingURL=settings.mjs.map
@@ -0,0 +1,4 @@
1
+ var c=Object.defineProperty;var s=(t,r)=>c(t,"name",{value:r,configurable:!0});import{atLeast as f,semver as l}from"./normalize.mjs";import{sign as a}from"./thinking.mjs";const u="0.11.1",y="x-anthropic-billing-header:";function A(t,r){if(t.driver!=="vllm"||r.nativeMessages===!1)return!1;if(r.nativeMessages===!0)return!0;const n=t.facts?.version??t.version;return l(n)!==null&&f(n,u)}s(A,"wants");function*p(t){for(const r of t)if(Array.isArray(r?.content))for(const n of r.content)yield n,n?.type==="tool_result"&&Array.isArray(n.content)&&(yield*n.content)}s(p,"blocks");function v(t,r){const n=Array.isArray(t.messages)?t.messages:[];if(n.at(-1)?.role==="assistant")return"prefill";const e=[...p(n)];return e.some(o=>o?.type==="document")?"document":r.toolSearch!=="native"&&(Array.isArray(t.tools)&&t.tools.some(i=>i?.defer_loading!==void 0)||e.some(i=>["tool_reference","tool_addition","tool_removal"].includes(i?.type)))?"toolsearch":null}s(v,"unsuited");function d(t,r){return(typeof t=="string"?[{type:"text",text:t}]:t).filter(e=>e?.type==="text"&&typeof e.text=="string").filter(e=>!(r.stripBillingHeader&&e.text.trimStart().startsWith(y))).map(e=>e.text).join(`
2
+
3
+ `)}s(d,"systemText");function m(t,r){const n=r.toolErrorMarker;return typeof t.content=="string"?{...t,content:`${n}${t.content}`}:Array.isArray(t.content)?{...t,content:[{type:"text",text:n.trimEnd()},...t.content]}:{...t,content:n.trimEnd()}}s(m,"marked");function h(t,r){const n={...t};if(n.system!==void 0&&n.system!==null){const e=d(n.system,r);e===""?delete n.system:n.system=e}return r.toolErrors&&Array.isArray(n.messages)&&(n.messages=n.messages.map(e=>Array.isArray(e?.content)?{...e,content:e.content.map(o=>o?.type==="tool_result"&&o.is_error===!0?m(o,r):o)}:e)),n}s(h,"request");function E(t){return Array.isArray(t?.content)?{...t,content:t.content.map(r=>r?.type==="thinking"&&typeof r.signature!="string"?{...r,signature:a()}:r)}:t}s(E,"response");async function*M(t){const r=new Map;for await(const n of t){const e=n.data;e?.type==="content_block_start"&&e.content_block?.type==="thinking"?r.set(e.index,typeof e.content_block.signature=="string"):e?.type==="content_block_delta"&&e.delta?.type==="signature_delta"?r.set(e.index,!0):e?.type==="content_block_stop"&&r.get(e.index)===!1&&(yield{event:"content_block_delta",data:{type:"content_block_delta",index:e.index,delta:{type:"signature_delta",signature:a()}}}),e?.type==="content_block_stop"&&r.delete(e.index),yield n,e?.type==="message_start"&&(yield{event:"ping",data:{type:"ping"}})}}s(M,"stream");export{u as MIN_VERSION,h as request,E as response,M as stream,v as unsuited,A as wants};
4
+ //# sourceMappingURL=vllm.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cldmv/llm-translator",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "description": "Bidirectional Anthropic Messages ⇄ OpenAI Chat Completions translator with backend auto-detection (vLLM and other OpenAI-compatible servers), built on slothlet, with an optional standalone server for running Claude Code against local models.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Convert an Anthropic Messages request to an Ollama `/api/chat` body. The Anthropic conversion to Chat Completions does the work (system, tool
3
+ * blocks, images, tool names, dropped fields); `chatBody` then puts the result in Ollama's shape: tool arguments as objects, tool results by tool
4
+ * name, `images` as base64, the sampling fields and `max_tokens` in `options`, `thinking` and `output_config.effort` as `think`, and
5
+ * `output_config.format` as `format`. The upstream's `sampling` setting and `metadata.sampling` become further `options`.
6
+ * @param {any} body - Anthropic request body.
7
+ * @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
8
+ * @param {any} [context] - `{ producer, budget, listed, toolCallIds }`, as for the Chat Completions conversion.
9
+ * @returns {Promise<{body: any, state: any}>} The Ollama body and the conversion state the response converters read.
10
+ */
11
+ export function request(body: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>, context?: any): Promise<{
12
+ body: any;
13
+ state: any;
14
+ }>;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Convert an Ollama reply to an Anthropic message: the reply is read as a Chat Completions `chat.completion` (thinking, tool calls with generated
3
+ * ids, `done_reason` as `finish_reason`, the evaluation counts as `usage`) and the Chat Completions conversion builds the message.
4
+ * @param {any} reply - Parsed Ollama reply.
5
+ * @param {any} [state] - State from the request converter.
6
+ * @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
7
+ * @returns {any} Anthropic message.
8
+ * @throws {import("../../../../../../../lib/errors.mjs").TranslatorError} When the reply is an error.
9
+ */
10
+ export function response(reply: any, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): any;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Convert an Ollama stream (one JSON object per line, possibly split anywhere across chunks) to Anthropic events. The lines become Chat
3
+ * Completions chunks (one id for the reply, tool calls whole with generated ids, the final line as the finish and usage chunks) and the Chat
4
+ * Completions conversion writes the events. A line that is not JSON, an `{ error }` line and a stream that ends before its final line each
5
+ * end the stream with an `error` event.
6
+ * @param {ReadableStream|AsyncIterable<unknown>|Iterable<unknown>|string|Uint8Array} source - Ollama NDJSON bytes or text.
7
+ * @param {any} [state] - State from the request converter.
8
+ * @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
9
+ * @returns {AsyncGenerator<{ event: string, data: any }, void, undefined>} Anthropic events.
10
+ */
11
+ export function stream(source: ReadableStream | AsyncIterable<unknown> | Iterable<unknown> | string | Uint8Array, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): AsyncGenerator<{
12
+ event: string;
13
+ data: any;
14
+ }, void, undefined>;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Convert an Ollama reply to a Chat Completions `chat.completion` (see `completion`). The model name is the one the client asked for, and the
3
+ * counts are kept on the state for the usage report.
4
+ * @param {any} reply - Parsed Ollama reply.
5
+ * @param {any} [state] - State from the request converter.
6
+ * @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
7
+ * @returns {any} `chat.completion` object.
8
+ * @throws {import("../../../../../../../lib/errors.mjs").TranslatorError} When the reply is an error.
9
+ */
10
+ export function response(reply: any, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): any;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Convert an Ollama stream (one JSON object per line, possibly split anywhere across chunks) to Chat Completions chunks followed by `[DONE]`.
3
+ * Every chunk carries one completion id, tool calls come whole with generated ids, and the final line gives the finish chunk and, unless
4
+ * `state.includeUsage` is false, a usage chunk. A line that is not JSON, an `{ error }` line and a stream that ends before its final line
5
+ * each become an `{ error }` chunk followed by `[DONE]`.
6
+ * @param {ReadableStream|AsyncIterable<unknown>|Iterable<unknown>|string|Uint8Array} source - Ollama NDJSON bytes or text.
7
+ * @param {any} [state] - State from the request converter (model, `includeUsage`).
8
+ * @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
9
+ * @returns {AsyncGenerator<{ data: any|string }, void, undefined>} Chat Completions chunks.
10
+ */
11
+ export function stream(source: ReadableStream | AsyncIterable<unknown> | Iterable<unknown> | string | Uint8Array, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): AsyncGenerator<{
12
+ data: any | string;
13
+ }, void, undefined>;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Convert a Chat Completions request to an Ollama `/api/chat` body (see `chatBody`). A client may also send Ollama's own `think`, `options`,
3
+ * `format` and `keep_alive`; they are used as sent. The upstream's `sampling` setting fills in `options` fields the request does not name.
4
+ * @param {any} body - Chat Completions request body.
5
+ * @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
6
+ * @returns {{body: any, state: any}} The Ollama body and the conversion state the response converters read.
7
+ * @throws {TranslatorError} `invalid_request` when `messages` is missing.
8
+ */
9
+ export function request(body: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): {
10
+ body: any;
11
+ state: any;
12
+ };
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Bind every endpoint to a driver. `backend: "auto"` endpoints are probed (drivers tried in `info().order`, the generic
2
+ * Bind every endpoint to a driver. `backend: "auto"` endpoints are probed (drivers tried in `info().order` after any driver whose `info().hosts` names the endpoint host, the generic
3
3
  * driver matches last); endpoints with an explicit `backend` skip detection. Call it again to re-probe.
4
4
  * @param {any} [options] - `{ network, signal }`. `network: false` skips all probing (auto endpoints fall back to the generic driver).
5
5
  * @returns {Promise<any>} The `status()` result.
@@ -19,7 +19,7 @@
19
19
  /**
20
20
  * Describe the Azure OpenAI driver. The key goes in the `api-key` header. The real paths come from the `path` leaf, because they depend on the
21
21
  * upstream's `apiVersion` and `deployment`; `paths` holds the v1 API paths for a base URL without `/openai`.
22
- * @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, paths: Record<string,string>}} Driver facts.
22
+ * @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, hostSuffixes: string[], paths: Record<string,string>}} Driver facts.
23
23
  */
24
24
  export function info(): {
25
25
  name: string;
@@ -27,5 +27,6 @@ export function info(): {
27
27
  order: number;
28
28
  auth: string;
29
29
  strict: boolean;
30
+ hostSuffixes: string[];
30
31
  paths: Record<string, string>;
31
32
  };