@cldmv/llm-translator 0.9.0 → 0.10.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 (105) hide show
  1. package/README.md +8 -6
  2. package/dist/api/convert/anthropic/messages/to/ollama/chat/request.mjs +2 -0
  3. package/dist/api/convert/ollama/chat/to/anthropic/messages/response.mjs +2 -0
  4. package/dist/api/convert/ollama/chat/to/anthropic/messages/stream.mjs +2 -0
  5. package/dist/api/convert/ollama/chat/to/openai/chat/response.mjs +2 -0
  6. package/dist/api/convert/ollama/chat/to/openai/chat/stream.mjs +2 -0
  7. package/dist/api/convert/openai/chat/to/ollama/chat/request.mjs +2 -0
  8. package/dist/api/detect.mjs +1 -1
  9. package/dist/api/driver/fireworks/detect.mjs +2 -0
  10. package/dist/api/driver/fireworks/info.mjs +2 -0
  11. package/dist/api/driver/fireworks/path.mjs +2 -0
  12. package/dist/api/driver/fireworks/request.mjs +2 -0
  13. package/dist/api/driver/fireworks/settings.mjs +2 -0
  14. package/dist/api/driver/gemini/detect.mjs +2 -0
  15. package/dist/api/driver/gemini/info.mjs +2 -0
  16. package/dist/api/driver/gemini/path.mjs +2 -0
  17. package/dist/api/driver/gemini/request.mjs +2 -0
  18. package/dist/api/driver/gemini/settings.mjs +2 -0
  19. package/dist/api/driver/groq/detect.mjs +2 -0
  20. package/dist/api/driver/groq/info.mjs +2 -0
  21. package/dist/api/driver/groq/path.mjs +2 -0
  22. package/dist/api/driver/groq/request.mjs +2 -0
  23. package/dist/api/driver/groq/settings.mjs +2 -0
  24. package/dist/api/driver/ollama/info.mjs +1 -1
  25. package/dist/api/driver/ollama/request.mjs +1 -1
  26. package/dist/api/driver/together/detect.mjs +2 -0
  27. package/dist/api/driver/together/info.mjs +2 -0
  28. package/dist/api/driver/together/request.mjs +2 -0
  29. package/dist/api/driver/together/settings.mjs +2 -0
  30. package/dist/api/driver/vllm/detect.mjs +1 -1
  31. package/dist/api/driver/vllm/info.mjs +1 -1
  32. package/dist/api/driver/vllm/model/list.mjs +1 -1
  33. package/dist/api/driver/xai/detect.mjs +2 -0
  34. package/dist/api/driver/xai/info.mjs +2 -0
  35. package/dist/api/driver/xai/request.mjs +2 -0
  36. package/dist/api/driver/xai/settings.mjs +2 -0
  37. package/dist/api/embeddings/send.mjs +1 -1
  38. package/dist/api/health/check.mjs +2 -0
  39. package/dist/api/model/list.mjs +1 -1
  40. package/dist/api/request.mjs +2 -2
  41. package/dist/api/response.mjs +1 -1
  42. package/dist/api/send.mjs +1 -1
  43. package/dist/api/status.mjs +1 -1
  44. package/dist/api/stream.mjs +1 -1
  45. package/dist/index.mjs +1 -1
  46. package/dist/lib/capture.mjs +1 -1
  47. package/dist/lib/config.mjs +1 -1
  48. package/dist/lib/health.mjs +2 -0
  49. package/dist/lib/http.mjs +1 -1
  50. package/dist/lib/models.mjs +1 -1
  51. package/dist/lib/ndjson.mjs +4 -0
  52. package/dist/lib/ollama.mjs +3 -0
  53. package/dist/lib/pool.mjs +1 -1
  54. package/dist/lib/presets.mjs +2 -0
  55. package/dist/lib/route.mjs +1 -1
  56. package/dist/lib/settings.mjs +1 -1
  57. package/dist/lib/vllm.mjs +4 -0
  58. package/package.json +1 -1
  59. package/types/src/api/convert/anthropic/messages/to/ollama/chat/request.d.mts +14 -0
  60. package/types/src/api/convert/ollama/chat/to/anthropic/messages/response.d.mts +10 -0
  61. package/types/src/api/convert/ollama/chat/to/anthropic/messages/stream.d.mts +14 -0
  62. package/types/src/api/convert/ollama/chat/to/openai/chat/response.d.mts +10 -0
  63. package/types/src/api/convert/ollama/chat/to/openai/chat/stream.d.mts +13 -0
  64. package/types/src/api/convert/openai/chat/to/ollama/chat/request.d.mts +12 -0
  65. package/types/src/api/driver/fireworks/detect.d.mts +8 -0
  66. package/types/src/api/driver/fireworks/info.d.mts +13 -0
  67. package/types/src/api/driver/fireworks/path.d.mts +7 -0
  68. package/types/src/api/driver/fireworks/request.d.mts +7 -0
  69. package/types/src/api/driver/fireworks/settings.d.mts +5 -0
  70. package/types/src/api/driver/gemini/detect.d.mts +8 -0
  71. package/types/src/api/driver/gemini/info.d.mts +13 -0
  72. package/types/src/api/driver/gemini/path.d.mts +7 -0
  73. package/types/src/api/driver/gemini/request.d.mts +7 -0
  74. package/types/src/api/driver/gemini/settings.d.mts +5 -0
  75. package/types/src/api/driver/groq/detect.d.mts +8 -0
  76. package/types/src/api/driver/groq/info.d.mts +13 -0
  77. package/types/src/api/driver/groq/path.d.mts +7 -0
  78. package/types/src/api/driver/groq/request.d.mts +7 -0
  79. package/types/src/api/driver/groq/settings.d.mts +5 -0
  80. package/types/src/api/driver/ollama/info.d.mts +7 -2
  81. package/types/src/api/driver/together/detect.d.mts +8 -0
  82. package/types/src/api/driver/together/info.d.mts +13 -0
  83. package/types/src/api/driver/together/request.d.mts +7 -0
  84. package/types/src/api/driver/together/settings.d.mts +5 -0
  85. package/types/src/api/driver/vllm/detect.d.mts +1 -1
  86. package/types/src/api/driver/vllm/info.d.mts +1 -1
  87. package/types/src/api/driver/vllm/model/list.d.mts +3 -1
  88. package/types/src/api/driver/xai/detect.d.mts +8 -0
  89. package/types/src/api/driver/xai/info.d.mts +13 -0
  90. package/types/src/api/driver/xai/request.d.mts +7 -0
  91. package/types/src/api/driver/xai/settings.d.mts +5 -0
  92. package/types/src/api/endpoint/status.d.mts +5 -2
  93. package/types/src/api/health/check.d.mts +8 -0
  94. package/types/src/api/status.d.mts +1 -1
  95. package/types/src/lib/config.d.mts +4 -0
  96. package/types/src/lib/events.d.mts +2 -2
  97. package/types/src/lib/health.d.mts +22 -0
  98. package/types/src/lib/models.d.mts +25 -0
  99. package/types/src/lib/ndjson.d.mts +16 -0
  100. package/types/src/lib/ollama.d.mts +86 -0
  101. package/types/src/lib/pool.d.mts +30 -4
  102. package/types/src/lib/presets.d.mts +98 -0
  103. package/types/src/lib/route.d.mts +1 -1
  104. package/types/src/lib/settings.d.mts +5 -0
  105. package/types/src/lib/vllm.d.mts +47 -0
package/README.md CHANGED
@@ -17,18 +17,20 @@ Use it as a library when you want the conversion without any I/O (`request`, `re
17
17
 
18
18
  ## ✨ What's New
19
19
 
20
- ### Latest: v0.9.0 (November 2026)
20
+ ### Latest: v0.10.0 (November 2026)
21
21
 
22
- - **Azure OpenAI and OpenRouter drivers** — Azure gets `api-key` auth and either the v1 API or the classic deployment URLs (set `apiVersion`, and `deployment` if it isn't the model name), with filtered categories named on content-filter errors. OpenRouter keeps `reasoning_details` round-tripping in order, turns errors returned inside a 200 into real errors, and maps effort and thinking budgets to its `reasoning` object.
23
- - **Hugging Face TGI and LocalAI drivers** — both are detected by probe. TGI's old reply quirks (a lone `tool_calls` object, numeric ids, object arguments, `eos_token`) are normalised and structured output goes out as its grammar. LocalAI's reasoning is read, and `disable_parallel_tool_use` is honoured even though LocalAI sets parallel calls per model.
24
- - [View full v0.9.0 Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.9.0.md)
22
+ - **Native `/v1/messages` for vLLM** — set `nativeMessages` (`true` or `"auto"`) and Anthropic requests go to vLLM's own endpoint with only normalisation. Prefills, document blocks and deferred tools are still converted, and an upstream without the route falls back to conversion.
23
+ - **Readiness probes and LoRA adapters** — pools check vLLM's `/health` and try ready upstreams first (`pool.healthInterval`, `pool.healthTimeout`, a `health` event, `health.check()`). Adapters are routable and carry `parent` in model lists.
24
+ - **Hosted provider drivers** — Groq, Together, Fireworks, xAI and Gemini are bound by host name, with their token fields, reasoning fields and rejected request fields handled from one table.
25
+ - **Ollama's native `/api/chat`** — set `api: "native"` on an Ollama upstream to use `think`, `options` and `format`, with NDJSON streaming converted for both fronts.
26
+ - [View full v0.10.0 Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.10.0.md)
25
27
 
26
28
  ### Recent Releases
27
29
 
30
+ - **v0.9.0** (November 2026) — Azure OpenAI, OpenRouter, Hugging Face TGI and LocalAI drivers, with Azure's `apiVersion` and `deployment` upstream fields and more startup probes ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.9.0.md))
28
31
  - **v0.8.0** (November 2026) — Mistral and LM Studio drivers, an OpenAI client's `n` above 1 answered by parallel requests to Anthropic-protocol upstreams, and `stop_details.category` from content-filter results ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.8.0.md))
29
32
  - **v0.7.0** (November 2026) — Anthropic-protocol servers detected by probe on any host, tool-call ids rewritten for strict upstream formats, embeddings usage through `onUsage`, and capture records for every failed-over attempt ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.7.0.md))
30
33
  - **v0.6.0** (November 2026) — `POST /v1/embeddings` pass-through, sampling extensions per upstream and per request, `strict` tool use end to end with a schema check, and the `capture` option for debugging ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.6.0.md))
31
- - **v0.5.0** (November 2026) — everything the library logs is available as events, logging is opt-in through `logs`, `json_object` reaches Anthropic upstreams as a schema with a fallback, and refusals carry `stop_details` ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.5.0.md))
32
34
 
33
35
  📚 **For complete version history and detailed release notes, see the [docs/changelog/](https://github.com/CLDMV/llm-translator/tree/master/docs/changelog/) folder.**
34
36
 
@@ -127,7 +129,7 @@ Then point Claude Code at it with `ANTHROPIC_BASE_URL=http://127.0.0.1:8787`. Th
127
129
 
128
130
  ## 📚 Configuration
129
131
 
130
- `createTranslator(config)` takes `front`, `upstreams` (URLs or objects with `url`, `apiKey`, `backend`, `models`, `default` and `settings`, plus `apiVersion` and `deployment` for Azure OpenAI, see [docs/drivers.md](https://github.com/CLDMV/llm-translator/blob/master/docs/drivers.md#azure-openai)), `aliases` (a model name, or `{ model, effort, thinking }` to pin settings), `efforts` (the reasoning levels each model supports, see [docs/settings.md](https://github.com/CLDMV/llm-translator/blob/master/docs/settings.md#effort-levels-and-pinned-aliases-not-converter-settings)), global `settings`, `pool` (failover and load balancing between upstreams that serve the same model, see [docs/server.md](https://github.com/CLDMV/llm-translator/blob/master/docs/server.md#endpoint-pools-failover-and-load-balancing)), `detect`, `logs` (off by default; `true` prints warnings, a path appends JSON lines), `events` and `onEvent` (see [docs/events.md](https://github.com/CLDMV/llm-translator/blob/master/docs/events.md)), `capture` (off by default; a directory or a callback receives redacted request and response records for debugging, see [docs/events.md](https://github.com/CLDMV/llm-translator/blob/master/docs/events.md#capture-for-debugging)) and a custom `transport`. `silent: true` is a deprecated alias for `logs: false`. The CLI reads the same keys from flags, `LLM_TRANSLATOR_*` environment variables and a JSON file, in that order of precedence.
132
+ `createTranslator(config)` takes `front`, `upstreams` (URLs or objects with `url`, `apiKey`, `backend`, `models`, `default` and `settings`, plus `apiVersion` and `deployment` for Azure OpenAI, see [docs/drivers.md](https://github.com/CLDMV/llm-translator/blob/master/docs/drivers.md#azure-openai), and `api: "native"` for Ollama's own `/api/chat`, see [docs/drivers.md](https://github.com/CLDMV/llm-translator/blob/master/docs/drivers.md#native-apichat-mode)), `aliases` (a model name, or `{ model, effort, thinking }` to pin settings), `efforts` (the reasoning levels each model supports, see [docs/settings.md](https://github.com/CLDMV/llm-translator/blob/master/docs/settings.md#effort-levels-and-pinned-aliases-not-converter-settings)), global `settings`, `pool` (failover and load balancing between upstreams that serve the same model, see [docs/server.md](https://github.com/CLDMV/llm-translator/blob/master/docs/server.md#endpoint-pools-failover-and-load-balancing)), `detect`, `logs` (off by default; `true` prints warnings, a path appends JSON lines), `events` and `onEvent` (see [docs/events.md](https://github.com/CLDMV/llm-translator/blob/master/docs/events.md)), `capture` (off by default; a directory or a callback receives redacted request and response records for debugging, see [docs/events.md](https://github.com/CLDMV/llm-translator/blob/master/docs/events.md#capture-for-debugging)) and a custom `transport`. `silent: true` is a deprecated alias for `logs: false`. The CLI reads the same keys from flags, `LLM_TRANSLATOR_*` environment variables and a JSON file, in that order of precedence.
131
133
 
132
134
  Settings tune how the converters map one protocol onto the other (`inlineSystem`, `images`, `imageFallback`, `toolStreaming`, `pingInterval` and more); they are resolved per upstream from defaults, probed values, driver defaults and your overrides. The full table is in [docs/settings.md](https://github.com/CLDMV/llm-translator/blob/master/docs/settings.md).
133
135
 
@@ -0,0 +1,2 @@
1
+ var p=Object.defineProperty;var n=(e,a)=>p(e,"name",{value:a,configurable:!0});import{resolve as m}from"../../../../../../../lib/settings.mjs";import{chatBody as c}from"../../../../../../../lib/ollama.mjs";import{request as f}from"../../openai/chat/request.mjs";const d={maxTokensField:"max_tokens",thinking:"chat_template_kwargs",thinkingBudget:!1,effort:!0,topK:!0,prefill:"none",streamUsage:!1,continuousUsage:!1,parallelToolCalls:!1,reasoningRoundTrip:!1,reasoningField:"reasoning_content",responseFormat:!0};async function y(e,a,r){const o=m(a),{body:l,state:s}=await f(e,{...o,...d},r),t=e.metadata?.sampling,i={...o.sampling,...t!==null&&typeof t=="object"&&!Array.isArray(t)?t:{}};return{body:c(l,{dropped:s.dropped,sampling:i}),state:s}}n(y,"request");export{y as request};
2
+ //# sourceMappingURL=request.mjs.map
@@ -0,0 +1,2 @@
1
+ var m=Object.defineProperty;var e=(o,r)=>m(o,"name",{value:r,configurable:!0});import{completion as n}from"../../../../../../../lib/ollama.mjs";import{response as p}from"../../../../../openai/chat/to/anthropic/messages/response.mjs";function a(o,r,t){return p(n(o),r,t)}e(a,"response");export{a as response};
2
+ //# sourceMappingURL=response.mjs.map
@@ -0,0 +1,2 @@
1
+ var a=Object.defineProperty;var o=(r,t)=>a(r,"name",{value:t,configurable:!0});import{createChunker as f}from"../../../../../../../lib/ollama.mjs";import{parse as m}from"../../../../../../../lib/ndjson.mjs";import{stream as s}from"../../../../../openai/chat/to/anthropic/messages/stream.mjs";function h(r,t,i){const e=f();return s((async function*(){for await(const n of m(r))yield*e.line(n);e.finished()&&(yield{data:"[DONE]"})})(),t,i)}o(h,"stream");export{h as stream};
2
+ //# sourceMappingURL=stream.mjs.map
@@ -0,0 +1,2 @@
1
+ var d=Object.defineProperty;var r=(e,o)=>d(e,"name",{value:o,configurable:!0});import{resolve as m}from"../../../../../../../lib/settings.mjs";import{completion as l,folded as f}from"../../../../../../../lib/ollama.mjs";function g(e,o,i){const n=l(e,{reasoningField:m(i).reasoningField});return o&&(o.upstreamUsage=f(n.usage)),o?.model===void 0?n:{...n,model:o.model}}r(g,"response");export{g as response};
2
+ //# sourceMappingURL=response.mjs.map
@@ -0,0 +1,2 @@
1
+ var s=Object.defineProperty;var n=(a,e)=>s(a,"name",{value:e,configurable:!0});import{resolve as c}from"../../../../../../../lib/settings.mjs";import{createChunker as u,folded as f}from"../../../../../../../lib/ollama.mjs";import{parse as m}from"../../../../../../../lib/ndjson.mjs";async function*k(a,e,t){const o=u({model:e?.model,reasoningField:c(t).reasoningField,includeUsage:e?.includeUsage??!0}),d=n((r,i="api_error")=>({data:{error:{message:r,type:i,param:null,code:null}}}),"failure");try{for await(const r of m(a)){const i=o.line(r);e&&o.usage()!==void 0&&(e.upstreamUsage=f(o.usage()));for(const l of i)if(yield{data:l},l.error){yield{data:"[DONE]"};return}}}catch(r){yield d(r.message,r.type??"api_error"),yield{data:"[DONE]"};return}o.finished()||(yield d("Upstream stream ended before completion")),yield{data:"[DONE]"}}n(k,"stream");export{k as stream};
2
+ //# sourceMappingURL=stream.mjs.map
@@ -0,0 +1,2 @@
1
+ var a=Object.defineProperty;var o=(e,r)=>a(e,"name",{value:r,configurable:!0});import{resolve as m}from"../../../../../../../lib/settings.mjs";import{TranslatorError as i}from"../../../../../../../lib/errors.mjs";import{chatBody as n}from"../../../../../../../lib/ollama.mjs";function c(e,r){const t=m(r);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new i("messages: Field required",{code:"invalid_request",param:"messages"});const s={front:"openai",model:e.model,stream:e.stream===!0,toolNames:{},toolIds:{},dropped:[],includeUsage:e.stream_options?.include_usage===!0};return{body:n(e,{dropped:s.dropped,sampling:t.sampling}),state:s}}o(c,"request");export{c as request};
2
+ //# sourceMappingURL=request.mjs.map
@@ -1,2 +1,2 @@
1
- var g=Object.defineProperty;var o=(r,e)=>g(r,"name",{value:e,configurable:!0});import{context as h,self as u}from"../lib/runtime.mjs";import{capture as w}from"../lib/capture.mjs";import{TranslatorError as m}from"../lib/errors.mjs";import{warn as p}from"../lib/warn.mjs";import{emit as y}from"../lib/events.mjs";import*as k from"../lib/http.mjs";import{validToolIdPolicy as $}from"../lib/ids.mjs";async function D(r={}){const e=w(u,h),{status:f}=u,l=r.network!==!1;for(const t of e.drivers){const s=t.info.fields;if(s!==void 0&&!(Array.isArray(s)&&s.every(i=>typeof i=="string")))throw new m(`Driver ${t.name}: info().fields must be an array of field names`,{code:"invalid_driver",status:500})}for(const t of e.drivers){const s=t.info.toolCallIds;if(s!==void 0&&!$(s))throw new m(`Driver ${t.name}: info().toolCallIds must be { alphabet, min?, max? } with a character-class alphabet and positive integer bounds`,{code:"invalid_driver",status:500})}const a={transport:e.state.transport,signal:r.signal,timeout:e.config.detectTimeout};return await Promise.all(e.state.endpoints.map(t=>x(e,t,l,a))),f()}o(D,"detect");async function x(r,e,f,l){let a,t=null;if(e.backend!=="auto"){if(a=r.drivers.find(s=>s.name===e.backend),!a)throw new m(`Unknown backend "${e.backend}" for upstream ${e.name}`,{code:"unknown_backend",status:500});t={version:e.version}}else if(f){const s=URL.canParse(e.url)?new URL(e.url).hostname:"",i={endpoint:e,...l,probes:new Map};let n=!1;for(const[v,c]of r.drivers.entries()){if(!n&&c.info.probes?.length>0&&!c.info.hosts?.includes(s)){n=!0;for(const d of r.drivers.slice(v))for(const b of d.info.probes??[])k.probe(i,b,d.info)}if(t=await U(c,i),t){a=c;break}}}if(a||(a=r.drivers.find(s=>s.name==="compatible"),t={reachable:null,models:[]}),e.driver=a.name,e.protocol=a.info.protocol,e.facts=t,e.driverSettings=await a.settings?.(t)??{},e.probed={},f&&e.backend==="auto"&&a.probe){const s=o(n=>e.settings[n]!==void 0||r.config.settings[n]!==void 0,"given"),i={inlineSystem:r.settings(e).inlineSystem==="auto",images:!s("images")};(i.inlineSystem||i.images)&&(e.probed=await a.probe({endpoint:e,facts:t,want:i,...l}))}t.reachable===!1?p(r.config,r.state,`Upstream ${e.name} (${e.url}) did not answer detection; using the generic OpenAI-compatible driver`,{type:"detect",kind:"unreachable",endpoint:e.name,url:e.url,driver:a.name}):y({config:r.config,state:r.state},{type:"detect",kind:"driver",level:"info",message:`Upstream ${e.name} (${e.url}): driver ${a.name}`,endpoint:e.name,url:e.url,driver:a.name})}o(x,"bind");async function U(r,e){try{return await r.detect?.(e)??null}catch{return null}}o(U,"detectWith");export{D as detect};
1
+ var h=Object.defineProperty;var o=(t,e)=>h(t,"name",{value:e,configurable:!0});import{context as g,self as u}from"../lib/runtime.mjs";import{capture as w}from"../lib/capture.mjs";import{TranslatorError as m}from"../lib/errors.mjs";import{warn as p}from"../lib/warn.mjs";import{emit as y}from"../lib/events.mjs";import*as k from"../lib/http.mjs";import{validToolIdPolicy as $}from"../lib/ids.mjs";async function j(t={}){const e=w(u,g),{status:l}=u,f=t.network!==!1;for(const a of e.drivers){const s=a.info.fields;if(s!==void 0&&!(Array.isArray(s)&&s.every(i=>typeof i=="string")))throw new m(`Driver ${a.name}: info().fields must be an array of field names`,{code:"invalid_driver",status:500})}for(const a of e.drivers){const s=a.info.toolCallIds;if(s!==void 0&&!$(s))throw new m(`Driver ${a.name}: info().toolCallIds must be { alphabet, min?, max? } with a character-class alphabet and positive integer bounds`,{code:"invalid_driver",status:500})}const r={transport:e.state.transport,signal:t.signal,timeout:e.config.detectTimeout};return await Promise.all(e.state.endpoints.map(a=>U(e,a,f,r))),l()}o(j,"detect");async function U(t,e,l,f){let r,a=null;if(e.backend!=="auto"){if(r=t.drivers.find(s=>s.name===e.backend),!r)throw new m(`Unknown backend "${e.backend}" for upstream ${e.name}`,{code:"unknown_backend",status:500});a={version:e.version}}else if(l){const s=URL.canParse(e.url)?new URL(e.url).hostname:"",i={endpoint:e,...f,probes:new Map};let n=!1;for(const[v,c]of t.drivers.entries()){if(!n&&c.info.probes?.length>0&&!c.info.hosts?.includes(s)){n=!0;for(const d of t.drivers.slice(v))for(const b of d.info.probes??[])k.probe(i,b,d.info)}if(a=await x(c,i),a){r=c;break}}}if(r||(r=t.drivers.find(s=>s.name==="compatible"),a={reachable:null,models:[]}),t.state.noMessages.delete(e.name),Object.assign(e.health,{ready:null,readyReason:null,checkedAt:null,unsupported:!1}),e.driver=r.name,e.protocol=r.info.protocol,e.api==="native"){if(!r.info.native){const s=t.drivers.filter(i=>i.info.native).map(i=>i.name);throw new m(`Upstream ${e.name}: api "native" needs a driver with a native API (${s.join(", ")}), but the upstream is bound to ${r.name}; set backend to one of them`,{code:"unsupported_api",status:500})}e.protocol=r.info.native.protocol}if(e.facts=a,e.driverSettings=await r.settings?.(a)??{},e.probed={},l&&e.backend==="auto"&&r.probe){const s=o(n=>e.settings[n]!==void 0||t.config.settings[n]!==void 0,"given"),i={inlineSystem:t.settings(e).inlineSystem==="auto",images:!s("images")};(i.inlineSystem||i.images)&&(e.probed=await r.probe({endpoint:e,facts:a,want:i,...f}))}a.reachable===!1?p(t.config,t.state,`Upstream ${e.name} (${e.url}) did not answer detection; using the generic OpenAI-compatible driver`,{type:"detect",kind:"unreachable",endpoint:e.name,url:e.url,driver:r.name}):y({config:t.config,state:t.state},{type:"detect",kind:"driver",level:"info",message:`Upstream ${e.name} (${e.url}): driver ${r.name}`,endpoint:e.name,url:e.url,driver:r.name})}o(U,"bind");async function x(t,e){try{return await t.detect?.(e)??null}catch{return null}}o(x,"detectWith");export{j as detect};
2
2
  //# sourceMappingURL=detect.mjs.map
@@ -0,0 +1,2 @@
1
+ var o=Object.defineProperty;var t=(r,e)=>o(r,"name",{value:e,configurable:!0});import{match as c}from"../../../lib/presets.mjs";function m({endpoint:r}){return c("fireworks",r)}t(m,"detect");export{m as detect};
2
+ //# sourceMappingURL=detect.mjs.map
@@ -0,0 +1,2 @@
1
+ var i=Object.defineProperty;var r=(o,e)=>i(o,"name",{value:e,configurable:!0});import{describe as f}from"../../../lib/presets.mjs";function c(){return f("fireworks")}r(c,"info");export{c as info};
2
+ //# sourceMappingURL=info.mjs.map
@@ -0,0 +1,2 @@
1
+ var e=Object.defineProperty;var t=(r,o)=>e(r,"name",{value:o,configurable:!0});import{route as f}from"../../../lib/presets.mjs";function p(r,{endpoint:o}){return f("fireworks",r,o)}t(p,"path");export{p as path};
2
+ //# sourceMappingURL=path.mjs.map
@@ -0,0 +1,2 @@
1
+ var o=Object.defineProperty;var e=(r,t)=>o(r,"name",{value:t,configurable:!0});import{trim as i}from"../../../lib/presets.mjs";function n(r,t){return i("fireworks",r,t)}e(n,"request");export{n as request};
2
+ //# sourceMappingURL=request.mjs.map
@@ -0,0 +1,2 @@
1
+ var i=Object.defineProperty;var t=(r,e)=>i(r,"name",{value:e,configurable:!0});import{settingsOf as n}from"../../../lib/presets.mjs";function f(){return n("fireworks")}t(f,"settings");export{f as settings};
2
+ //# sourceMappingURL=settings.mjs.map
@@ -0,0 +1,2 @@
1
+ var i=Object.defineProperty;var e=(t,r)=>i(t,"name",{value:r,configurable:!0});import{match as m}from"../../../lib/presets.mjs";function c({endpoint:t}){return m("gemini",t)}e(c,"detect");export{c as detect};
2
+ //# sourceMappingURL=detect.mjs.map
@@ -0,0 +1,2 @@
1
+ var n=Object.defineProperty;var i=(r,e)=>n(r,"name",{value:e,configurable:!0});import{describe as o}from"../../../lib/presets.mjs";function m(){return o("gemini")}i(m,"info");export{m as info};
2
+ //# sourceMappingURL=info.mjs.map
@@ -0,0 +1,2 @@
1
+ var e=Object.defineProperty;var o=(r,t)=>e(r,"name",{value:t,configurable:!0});import{route as i}from"../../../lib/presets.mjs";function p(r,{endpoint:t}){return i("gemini",r,t)}o(p,"path");export{p as path};
2
+ //# sourceMappingURL=path.mjs.map
@@ -0,0 +1,2 @@
1
+ var i=Object.defineProperty;var e=(r,t)=>i(r,"name",{value:t,configurable:!0});import{trim as m}from"../../../lib/presets.mjs";function u(r,t){return m("gemini",r,t)}e(u,"request");export{u as request};
2
+ //# sourceMappingURL=request.mjs.map
@@ -0,0 +1,2 @@
1
+ var e=Object.defineProperty;var t=(i,n)=>e(i,"name",{value:n,configurable:!0});import{settingsOf as r}from"../../../lib/presets.mjs";function f(){return r("gemini")}t(f,"settings");export{f as settings};
2
+ //# sourceMappingURL=settings.mjs.map
@@ -0,0 +1,2 @@
1
+ var e=Object.defineProperty;var r=(t,o)=>e(t,"name",{value:o,configurable:!0});import{match as c}from"../../../lib/presets.mjs";function f({endpoint:t}){return c("groq",t)}r(f,"detect");export{f as detect};
2
+ //# sourceMappingURL=detect.mjs.map
@@ -0,0 +1,2 @@
1
+ var i=Object.defineProperty;var r=(o,e)=>i(o,"name",{value:e,configurable:!0});import{describe as n}from"../../../lib/presets.mjs";function c(){return n("groq")}r(c,"info");export{c as info};
2
+ //# sourceMappingURL=info.mjs.map
@@ -0,0 +1,2 @@
1
+ var e=Object.defineProperty;var t=(r,o)=>e(r,"name",{value:o,configurable:!0});import{route as n}from"../../../lib/presets.mjs";function f(r,{endpoint:o}){return n("groq",r,o)}t(f,"path");export{f as path};
2
+ //# sourceMappingURL=path.mjs.map
@@ -0,0 +1,2 @@
1
+ var e=Object.defineProperty;var o=(r,t)=>e(r,"name",{value:t,configurable:!0});import{trim as i}from"../../../lib/presets.mjs";function u(r,t){return i("groq",r,t)}o(u,"request");export{u as request};
2
+ //# sourceMappingURL=request.mjs.map
@@ -0,0 +1,2 @@
1
+ var o=Object.defineProperty;var t=(r,n)=>o(r,"name",{value:n,configurable:!0});import{settingsOf as e}from"../../../lib/presets.mjs";function f(){return e("groq")}t(f,"settings");export{f as settings};
2
+ //# sourceMappingURL=settings.mjs.map
@@ -1,2 +1,2 @@
1
- var r=Object.defineProperty;var o=(e,a)=>r(e,"name",{value:a,configurable:!0});function i(){return{name:"ollama",protocol:"openai.chat",order:60,auth:"bearer",strict:!1,probes:["/api/version"],paths:{chat:"/v1/chat/completions",models:"/v1/models",version:"/api/version"}}}o(i,"info");export{i as info};
1
+ var e=Object.defineProperty;var a=(o,t)=>e(o,"name",{value:t,configurable:!0});function i(){return{name:"ollama",protocol:"openai.chat",order:60,auth:"bearer",strict:!1,probes:["/api/version"],paths:{chat:"/v1/chat/completions",models:"/v1/models",version:"/api/version"},native:{protocol:"ollama.chat",paths:{chat:"/api/chat"}}}}a(i,"info");export{i as info};
2
2
  //# sourceMappingURL=info.mjs.map
@@ -1,2 +1,2 @@
1
- var d=Object.defineProperty;var l=(s,r)=>d(s,"name",{value:r,configurable:!0});import{noEmptyTools as m}from"../../../lib/normalize.mjs";import{estimate as f}from"../../../lib/estimate.mjs";const p=["tool_choice","n","logit_bias"],h="[An image was omitted because Ollama accepts base64 images only and the translator does not fetch URLs.]";function g(s,r){let t=m(s);const e=r?.state?.dropped;t.tool_choice==="none"&&t.tools!==void 0&&(t={...t},delete t.tools,delete t.parallel_tool_calls,Array.isArray(e)&&!e.includes("tools")&&e.push("tools"));for(const o of p)t[o]!==void 0&&(t={...t},delete t[o],Array.isArray(e)&&!e.includes(o)&&e.push(o));if(Array.isArray(t.messages)){let o=!1;const c=t.messages.map(n=>{if(!Array.isArray(n?.content))return n;const u=n.content.map(i=>i?.type!=="image_url"||String(i.image_url?.url).startsWith("data:")?i:(o=!0,{type:"text",text:h}));return{...n,content:u}});o&&(t={...t,messages:c},Array.isArray(e)&&!e.includes("image_url")&&e.push("image_url"))}const a=r?.settings?.contextWindow??0;return a>0&&f(t)>a&&r?.warn?.(`Upstream ${r.endpoint?.name}: the prompt is estimated at more than ${a} tokens, which Ollama truncates without an error. Raise num_ctx (OLLAMA_CONTEXT_LENGTH or a Modelfile) and set the upstream's contextWindow setting to match.`),t}l(g,"request");export{g as request};
1
+ var m=Object.defineProperty;var a=(o,e)=>m(o,"name",{value:e,configurable:!0});import{noEmptyTools as p}from"../../../lib/normalize.mjs";import{estimate as d}from"../../../lib/estimate.mjs";const f=["tool_choice","n","logit_bias"],h="[An image was omitted because Ollama accepts base64 images only and the translator does not fetch URLs.]";function l(o,e){const t=Number.isFinite(o?.options?.num_ctx)?o.options.num_ctx:e?.settings?.contextWindow??0;t>0&&d(o)>t&&e?.warn?.(`Upstream ${e.endpoint?.name}: the prompt is estimated at more than ${t} tokens, which Ollama truncates without an error. Raise num_ctx (OLLAMA_CONTEXT_LENGTH or a Modelfile) and set the upstream's contextWindow setting to match.`)}a(l,"overflow");function y(o,e){if(e?.endpoint?.protocol==="ollama.chat")return l(o,e),o;let t=p(o);const n=e?.state?.dropped;t.tool_choice==="none"&&t.tools!==void 0&&(t={...t},delete t.tools,delete t.parallel_tool_calls,Array.isArray(n)&&!n.includes("tools")&&n.push("tools"));for(const r of f)t[r]!==void 0&&(t={...t},delete t[r],Array.isArray(n)&&!n.includes(r)&&n.push(r));if(Array.isArray(t.messages)){let r=!1;const u=t.messages.map(i=>{if(!Array.isArray(i?.content))return i;const c=i.content.map(s=>s?.type!=="image_url"||String(s.image_url?.url).startsWith("data:")?s:(r=!0,{type:"text",text:h}));return{...i,content:c}});r&&(t={...t,messages:u},Array.isArray(n)&&!n.includes("image_url")&&n.push("image_url"))}return l(t,e),t}a(y,"request");export{y as request};
2
2
  //# sourceMappingURL=request.mjs.map
@@ -0,0 +1,2 @@
1
+ var o=Object.defineProperty;var e=(t,r)=>o(t,"name",{value:r,configurable:!0});import{match as c}from"../../../lib/presets.mjs";function f({endpoint:t}){return c("together",t)}e(f,"detect");export{f as detect};
2
+ //# sourceMappingURL=detect.mjs.map
@@ -0,0 +1,2 @@
1
+ var t=Object.defineProperty;var r=(e,o)=>t(e,"name",{value:o,configurable:!0});import{describe as i}from"../../../lib/presets.mjs";function c(){return i("together")}r(c,"info");export{c as info};
2
+ //# sourceMappingURL=info.mjs.map
@@ -0,0 +1,2 @@
1
+ var o=Object.defineProperty;var e=(r,t)=>o(r,"name",{value:t,configurable:!0});import{trim as i}from"../../../lib/presets.mjs";function u(r,t){return i("together",r,t)}e(u,"request");export{u as request};
2
+ //# sourceMappingURL=request.mjs.map
@@ -0,0 +1,2 @@
1
+ var n=Object.defineProperty;var t=(e,r)=>n(e,"name",{value:r,configurable:!0});import{settingsOf as o}from"../../../lib/presets.mjs";function f(){return o("together")}t(f,"settings");export{f as settings};
2
+ //# sourceMappingURL=settings.mjs.map
@@ -1,2 +1,2 @@
1
- var g=Object.defineProperty;var p=(t,l)=>g(t,"name",{value:l,configurable:!0});import*as n from"../../../lib/http.mjs";import{isLlamacppProps as v,isLocalaiVersion as _,isSglangInfo as w,isTgiInfo as x,objectOf as s}from"../../../lib/backends.mjs";const L=new Set(["sglang","llamacpp"]);async function S({endpoint:t,transport:l,signal:f,timeout:c,probes:u}){const e={endpoint:t,transport:l,signal:f,timeout:c,probes:u},[i,a]=await Promise.all([n.probe(e,"/version"),n.probe(e,"/v1/models")]);if(_(s(i)))return null;const m=typeof i.body?.version=="string"?i.body.version:void 0,r=Array.isArray(a.body?.data)?a.body.data:[];if(r.some(o=>L.has(o?.owned_by)))return null;const d=r.some(o=>o?.owned_by==="vllm");if(!m&&!d)return null;if(!d){const[o,b,y]=await Promise.all([n.probe(e,"/get_server_info"),n.probe(e,"/props"),n.probe(e,"/info")]);if(w(s(o))||v(s(b))||x(s(y)))return null}return{version:m,models:r.map(o=>o.id),maxModelLen:r.find(o=>typeof o.max_model_len=="number")?.max_model_len}}p(S,"detect");export{S as detect};
1
+ var g=Object.defineProperty;var d=(t,a)=>g(t,"name",{value:a,configurable:!0});import*as r from"../../../lib/http.mjs";import{adaptersOf as v}from"../../../lib/models.mjs";import{isLlamacppProps as _,isLocalaiVersion as w,isSglangInfo as x,isTgiInfo as L,objectOf as s}from"../../../lib/backends.mjs";const O=new Set(["sglang","llamacpp"]);async function h({endpoint:t,transport:a,signal:f,timeout:c,probes:u}){const e={endpoint:t,transport:a,signal:f,timeout:c,probes:u},[l,i]=await Promise.all([r.probe(e,"/version"),r.probe(e,"/v1/models")]);if(w(s(l)))return null;const m=typeof l.body?.version=="string"?l.body.version:void 0,n=Array.isArray(i.body?.data)?i.body.data:[];if(n.some(o=>O.has(o?.owned_by)))return null;const p=n.some(o=>o?.owned_by==="vllm");if(!m&&!p)return null;if(!p){const[o,b,y]=await Promise.all([r.probe(e,"/get_server_info"),r.probe(e,"/props"),r.probe(e,"/info")]);if(x(s(o))||_(s(b))||L(s(y)))return null}return{version:m,models:n.map(o=>o.id),adapters:v(n),maxModelLen:n.find(o=>typeof o.max_model_len=="number")?.max_model_len}}d(h,"detect");export{h as detect};
2
2
  //# sourceMappingURL=detect.mjs.map
@@ -1,2 +1,2 @@
1
- var r=Object.defineProperty;var e=(o,t)=>r(o,"name",{value:t,configurable:!0});import{VLLM_FIELDS as i}from"../../../lib/passthrough.mjs";function s(){return{name:"vllm",protocol:"openai.chat",order:10,auth:"bearer",strict:!0,fields:i,paths:{chat:"/v1/chat/completions",models:"/v1/models",tokenize:"/tokenize",version:"/version"}}}e(s,"info");export{s as info};
1
+ var r=Object.defineProperty;var e=(o,t)=>r(o,"name",{value:t,configurable:!0});import{VLLM_FIELDS as s}from"../../../lib/passthrough.mjs";function n(){return{name:"vllm",protocol:"openai.chat",order:10,auth:"bearer",strict:!0,fields:s,paths:{chat:"/v1/chat/completions",messages:"/v1/messages",models:"/v1/models",tokenize:"/tokenize",version:"/version",health:"/health"}}}e(n,"info");export{n as info};
2
2
  //# sourceMappingURL=info.mjs.map
@@ -1,2 +1,2 @@
1
- var o=Object.defineProperty;var t=(r,e)=>o(r,"name",{value:e,configurable:!0});import{fetchModels as f}from"../../../../lib/models.mjs";function u(r){return f({...r,auth:"bearer"})}t(u,"list");export{u as list};
1
+ var r=Object.defineProperty;var e=(a,t)=>r(a,"name",{value:t,configurable:!0});import{adaptersOf as f,fetchModels as n}from"../../../../lib/models.mjs";async function i(a){const t=await n({...a,auth:"bearer"}),{endpoint:s}=a;return t!==null&&s.facts&&(s.facts={...s.facts,models:t.map(o=>o.id),adapters:f(t)}),t}e(i,"list");export{i as list};
2
2
  //# sourceMappingURL=list.mjs.map
@@ -0,0 +1,2 @@
1
+ var o=Object.defineProperty;var r=(t,e)=>o(t,"name",{value:e,configurable:!0});import{match as c}from"../../../lib/presets.mjs";function n({endpoint:t}){return c("xai",t)}r(n,"detect");export{n as detect};
2
+ //# sourceMappingURL=detect.mjs.map
@@ -0,0 +1,2 @@
1
+ var e=Object.defineProperty;var r=(i,o)=>e(i,"name",{value:o,configurable:!0});import{describe as n}from"../../../lib/presets.mjs";function c(){return n("xai")}r(c,"info");export{c as info};
2
+ //# sourceMappingURL=info.mjs.map
@@ -0,0 +1,2 @@
1
+ var i=Object.defineProperty;var e=(r,t)=>i(r,"name",{value:t,configurable:!0});import{trim as o}from"../../../lib/presets.mjs";function u(r,t){return o("xai",r,t)}e(u,"request");export{u as request};
2
+ //# sourceMappingURL=request.mjs.map
@@ -0,0 +1,2 @@
1
+ var r=Object.defineProperty;var t=(i,n)=>r(i,"name",{value:n,configurable:!0});import{settingsOf as e}from"../../../lib/presets.mjs";function f(){return e("xai")}t(f,"settings");export{f as settings};
2
+ //# sourceMappingURL=settings.mjs.map
@@ -1,2 +1,2 @@
1
- var x=Object.defineProperty;var h=(e,s)=>x(e,"name",{value:s,configurable:!0});import{context as w,self as q}from"../../lib/runtime.mjs";import{capture as S}from"../../lib/capture.mjs";import{resolve as H,serves as J}from"../../lib/route.mjs";import*as g from"../../lib/http.mjs";import*as O from"../../lib/pool.mjs";import{emit as N}from"../../lib/events.mjs";import{meter as U}from"../../lib/usage.mjs";import{warn as C}from"../../lib/warn.mjs";import{start as I}from"../../lib/record.mjs";async function L(e,s={}){const{transport:$}=w.state,{error:b}=q,a=S(q,w,"openai"),A=e&&typeof e=="object"&&!Array.isArray(e),l=U({model:null,requested:A&&typeof e.model=="string"?e.model:null,upstream:null,front:"openai",stream:!1},s.onUsage,(o,t)=>C(a.config,a.state,o,{type:"usage",...t,log:s.log})),c=h((o,t)=>(l.finish(t),Object.defineProperty(o,"usage",{value:l.usage,enumerable:!1,configurable:!0})),"end"),n=h((o,t,i={})=>c(b.build(o,t,{...i,front:"openai"}),o===499?"aborted":"error"),"fail");if(!e||typeof e!="object"||Array.isArray(e))return n(400,"The request body must be a JSON object",{code:"invalid_body"});if(typeof e.model!="string"||e.model==="")return n(400,"model is required",{code:"invalid_body",param:"model"});const P=Object.hasOwn(a.config.aliases,e.model)?a.config.aliases[e.model]:e.model;if(!a.state.endpoints.some(o=>J(o,P)))return n(404,`The model "${e.model}" is not served by any configured upstream.`,{code:"model_not_found",param:"model"});const d=H(a,e,{advance:!0}),f=d.pool.filter(o=>o.protocol==="openai.chat");if(f.length===0)return n(400,`The model "${e.model}" is served by an Anthropic-protocol upstream, which has no embeddings API.`,{code:"embeddings_unsupported",param:"model"});const j={...e,model:d.model};l.meta.model=d.model;let m,v=null;for(let o=0;o<f.length;o++){const t=f[o];l.meta.upstream=t.name;const i=a.driver(t);t.health.inflight++,t.health.requests++;const T=g.join(t.url,await g.pathOf(i,"embeddings",t,d.model)),_={"content-type":"application/json",accept:"application/json",...i.info.headers,...g.headers(t,i.info.auth,s.headers)},y=I(a,{body:e,sent:{upstream:t.name,url:T,headers:_,body:j,state:{stream:!1}},front:"openai",requested:l.meta.requested,log:s.log,exchange:v,attempt:o+1});v??=y.id;let r;try{r=await g.json($,T,{method:"POST",headers:_,body:j,signal:s.signal})}finally{t.health.inflight--}if(s.signal?.aborted)return n(499,"The request was aborted",{code:"aborted"});const u=r.status===0;if(!u&&r.status<500)return O.succeeded(t),y.reply(r.status,r.headers,r.body),r.ok?r.body===null||typeof r.body!="object"?n(502,"The upstream returned a body that is not JSON",{code:"bad_gateway"}):(l.json(r.body),c({status:200,headers:{"content-type":"application/json"},body:r.body},"done")):c(b(r.status,r.body,{},{front:"openai",upstream:t.name,native:!0,model:e.model}),"upstream_error");const p=u?`Could not reach the upstream (${t.name}): ${r.error?.message}`:`HTTP ${r.status}`;O.failed(a.config,t,p);const k=u?{error:p}:{status:r.status,headers:r.headers,body:r.body};y.attempt(k,o+1<f.length?p:void 0),N(a,{type:"error",kind:u?"transport":"upstream",level:"error",message:u?p:`Upstream ${t.name} answered HTTP ${r.status}`,endpoint:t.name,model:d.model,log:s.log}),m={endpoint:t,result:r,reason:p}}return m.result.status===0?n(502,m.reason,{code:"upstream_unreachable"}):c(b(m.result.status,m.result.body,{},{front:"openai",upstream:m.endpoint.name,native:!0,model:e.model}),"upstream_error")}h(L,"send");export{L as send};
1
+ var x=Object.defineProperty;var h=(e,s)=>x(e,"name",{value:s,configurable:!0});import{context as _,self as q}from"../../lib/runtime.mjs";import{capture as S}from"../../lib/capture.mjs";import{resolve as H,serves as J}from"../../lib/route.mjs";import*as g from"../../lib/http.mjs";import*as O from"../../lib/pool.mjs";import{emit as N}from"../../lib/events.mjs";import{refresh as U}from"../../lib/health.mjs";import{meter as C}from"../../lib/usage.mjs";import{warn as I}from"../../lib/warn.mjs";import{start as M}from"../../lib/record.mjs";async function R(e,s={}){const{transport:$}=_.state,{error:b}=q,a=S(q,_,"openai"),A=e&&typeof e=="object"&&!Array.isArray(e),l=C({model:null,requested:A&&typeof e.model=="string"?e.model:null,upstream:null,front:"openai",stream:!1},s.onUsage,(o,t)=>I(a.config,a.state,o,{type:"usage",...t,log:s.log})),c=h((o,t)=>(l.finish(t),Object.defineProperty(o,"usage",{value:l.usage,enumerable:!1,configurable:!0})),"end"),n=h((o,t,i={})=>c(b.build(o,t,{...i,front:"openai"}),o===499?"aborted":"error"),"fail");if(!e||typeof e!="object"||Array.isArray(e))return n(400,"The request body must be a JSON object",{code:"invalid_body"});if(typeof e.model!="string"||e.model==="")return n(400,"model is required",{code:"invalid_body",param:"model"});const P=Object.hasOwn(a.config.aliases,e.model)?a.config.aliases[e.model]:e.model;if(!a.state.endpoints.some(o=>J(o,P)))return n(404,`The model "${e.model}" is not served by any configured upstream.`,{code:"model_not_found",param:"model"});await U(a,e,s.signal);const d=H(a,e,{advance:!0}),f=d.pool.filter(o=>o.protocol!=="anthropic.messages");if(f.length===0)return n(400,`The model "${e.model}" is served by an Anthropic-protocol upstream, which has no embeddings API.`,{code:"embeddings_unsupported",param:"model"});const j={...e,model:d.model};l.meta.model=d.model;let m,v=null;for(let o=0;o<f.length;o++){const t=f[o];l.meta.upstream=t.name;const i=a.driver(t);t.health.inflight++,t.health.requests++;const T=g.join(t.url,await g.pathOf(i,"embeddings",t,d.model)),w={"content-type":"application/json",accept:"application/json",...i.info.headers,...g.headers(t,i.info.auth,s.headers)},y=M(a,{body:e,sent:{upstream:t.name,url:T,headers:w,body:j,state:{stream:!1}},front:"openai",requested:l.meta.requested,log:s.log,exchange:v,attempt:o+1});v??=y.id;let r;try{r=await g.json($,T,{method:"POST",headers:w,body:j,signal:s.signal})}finally{t.health.inflight--}if(s.signal?.aborted)return n(499,"The request was aborted",{code:"aborted"});const u=r.status===0;if(!u&&r.status<500)return O.succeeded(t),y.reply(r.status,r.headers,r.body),r.ok?r.body===null||typeof r.body!="object"?n(502,"The upstream returned a body that is not JSON",{code:"bad_gateway"}):(l.json(r.body),c({status:200,headers:{"content-type":"application/json"},body:r.body},"done")):c(b(r.status,r.body,{},{front:"openai",upstream:t.name,native:!0,model:e.model}),"upstream_error");const p=u?`Could not reach the upstream (${t.name}): ${r.error?.message}`:`HTTP ${r.status}`;O.failed(a.config,t,p);const k=u?{error:p}:{status:r.status,headers:r.headers,body:r.body};y.attempt(k,o+1<f.length?p:void 0),N(a,{type:"error",kind:u?"transport":"upstream",level:"error",message:u?p:`Upstream ${t.name} answered HTTP ${r.status}`,endpoint:t.name,model:d.model,log:s.log}),m={endpoint:t,result:r,reason:p}}return m.result.status===0?n(502,m.reason,{code:"upstream_unreachable"}):c(b(m.result.status,m.result.body,{},{front:"openai",upstream:m.endpoint.name,native:!0,model:e.model}),"upstream_error")}h(R,"send");export{R as send};
2
2
  //# sourceMappingURL=send.mjs.map
@@ -0,0 +1,2 @@
1
+ var n=Object.defineProperty;var r=(o,t)=>n(o,"name",{value:t,configurable:!0});import{context as c,self as e}from"../../lib/runtime.mjs";import{capture as i}from"../../lib/capture.mjs";import{probe as s}from"../../lib/health.mjs";async function u(o={}){const t=i(e,c);return await s(t,t.state.endpoints,{signal:o.signal,force:!0}),e.endpoint.status()}r(u,"check");export{u as check};
2
+ //# sourceMappingURL=check.mjs.map
@@ -1,2 +1,2 @@
1
- var m=Object.defineProperty;var n=(t,i)=>m(t,"name",{value:i,configurable:!0});import{context as c,self as u}from"../../lib/runtime.mjs";import{capture as _}from"../../lib/capture.mjs";import{defaultOf as p,nearest as g}from"../../lib/effort.mjs";async function b(t={}){const i=_(u,c,t.front),o=new Map;await Promise.all(i.state.endpoints.map(async e=>{const s=i.driver(e),r=(s.list?await s.list({endpoint:e,transport:i.state.transport,signal:t.signal,timeout:i.config.detectTimeout}):null)??(e.models??e.facts?.models??[]).map(l=>({id:l}));for(const l of r)o.set(l.id,{...l,upstream:e.name})}));for(const e of Object.keys(i.config.aliases)){const s=o.get(i.config.aliases[e]);o.set(e,{...s,id:e,alias_of:i.config.aliases[e]})}const f=[...o.values()].map(e=>({...e,...v(i.config,e)}));if(i.front==="openai")return{object:"list",data:f.map(e=>({id:e.id,object:"model",created:e.created??0,owned_by:e.owned_by??e.upstream??"llm-translator",...e.max_model_len===void 0?{}:{max_model_len:e.max_model_len},...d(e)}))};const a=f.map(e=>({type:"model",id:e.id,display_name:e.id,created_at:new Date((e.created??0)*1e3).toISOString(),...e.max_model_len===void 0?{}:{max_input_tokens:e.max_model_len},...d(e)}));return{data:a,has_more:!1,first_id:a[0]?.id??null,last_id:a.at(-1)?.id??null}}n(b,"list");function v(t,i){const o=i.alias_of??i.id,f=Object.hasOwn(t.efforts,o)?t.efforts[o]:void 0,a=i.alias_of!==void 0&&Object.hasOwn(t.pins,i.id)?t.pins[i.id]:{},e={};return a.thinking!==void 0&&(e.thinking=a.thinking),a.thinking===!1||(a.effort!==void 0?(e.effort=f?g(f.levels,a.effort):a.effort,e.levels=[e.effort],e.default_effort=e.effort):f&&(e.levels=f.levels,e.default_effort=p(f))),e}n(v,"levelsOf");function d(t){return{...t.alias_of===void 0?{}:{alias_of:t.alias_of},...t.levels===void 0?{}:{efforts:t.levels,default_effort:t.default_effort},...t.effort===void 0?{}:{effort:t.effort},...t.thinking===void 0?{}:{thinking:t.thinking}}}n(d,"extras");export{b as list};
1
+ var m=Object.defineProperty;var n=(t,i)=>m(t,"name",{value:i,configurable:!0});import{context as u,self as c}from"../../lib/runtime.mjs";import{capture as _}from"../../lib/capture.mjs";import{defaultOf as p,nearest as g}from"../../lib/effort.mjs";import{knownModels as v}from"../../lib/models.mjs";async function M(t={}){const i=_(c,u,t.front),o=new Map;await Promise.all(i.state.endpoints.map(async e=>{const s=i.driver(e),r=(s.list?await s.list({endpoint:e,transport:i.state.transport,signal:t.signal,timeout:i.config.detectTimeout}):null)??v(e);for(const l of r)o.set(l.id,{...l,upstream:e.name})}));for(const e of Object.keys(i.config.aliases)){const s=o.get(i.config.aliases[e]);o.set(e,{...s,id:e,alias_of:i.config.aliases[e]})}const f=[...o.values()].map(e=>({...e,...h(i.config,e)}));if(i.front==="openai")return{object:"list",data:f.map(e=>({id:e.id,object:"model",created:e.created??0,owned_by:e.owned_by??e.upstream??"llm-translator",...e.max_model_len===void 0?{}:{max_model_len:e.max_model_len},...d(e)}))};const a=f.map(e=>({type:"model",id:e.id,display_name:e.id,created_at:new Date((e.created??0)*1e3).toISOString(),...e.max_model_len===void 0?{}:{max_input_tokens:e.max_model_len},...d(e)}));return{data:a,has_more:!1,first_id:a[0]?.id??null,last_id:a.at(-1)?.id??null}}n(M,"list");function h(t,i){const o=i.alias_of??i.id,f=Object.hasOwn(t.efforts,o)?t.efforts[o]:void 0,a=i.alias_of!==void 0&&Object.hasOwn(t.pins,i.id)?t.pins[i.id]:{},e={};return a.thinking!==void 0&&(e.thinking=a.thinking),a.thinking===!1||(a.effort!==void 0?(e.effort=f?g(f.levels,a.effort):a.effort,e.levels=[e.effort],e.default_effort=e.effort):f&&(e.levels=f.levels,e.default_effort=p(f))),e}n(h,"levelsOf");function d(t){return{...typeof t.parent=="string"&&t.parent!==""?{parent:t.parent}:{},...t.alias_of===void 0?{}:{alias_of:t.alias_of},...t.levels===void 0?{}:{efforts:t.levels,default_effort:t.default_effort},...t.effort===void 0?{}:{effort:t.effort},...t.thinking===void 0?{}:{thinking:t.thinking}}}n(d,"extras");export{M as list};
2
2
  //# sourceMappingURL=list.mjs.map
@@ -1,3 +1,3 @@
1
- var T=Object.defineProperty;var p=(e,n)=>T(e,"name",{value:n,configurable:!0});import{context as 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 I=Object.defineProperty;var u=(e,o)=>I(e,"name",{value:o,configurable:!0});import{context as C,self as L}from"../lib/runtime.mjs";import{capture as M}from"../lib/capture.mjs";import{resolve as B}from"../lib/route.mjs";import*as y from"../lib/http.mjs";import{filterChatBody as E}from"../lib/passthrough.mjs";import{addSampling as P}from"../lib/sampling.mjs";import*as _ from"../lib/images.mjs";import*as x from"../lib/tools.mjs";import{LIMITS as R}from"../lib/toolsearch.mjs";import{warn as v}from"../lib/warn.mjs";import{emit as H}from"../lib/events.mjs";import{fit as z,pinBody as A}from"../lib/effort.mjs";import{relax as D,strictTools as G}from"../lib/strict.mjs";import{TranslatorError as J}from"../lib/errors.mjs";import*as K from"../lib/vllm.mjs";function N(e){return`${e.driver}:${e.protocol}:${URL.canParse(e.url)?new URL(e.url).host:e.url}`}u(N,"producerOf");async function ie(e,o={}){const n=M(L,C,o.front),m=B(n,e,{endpoint:o.endpoint,advance:o.hold===!0&&o.endpoint===void 0});if(o.hold!==!0)return T(n,e,o,m);m.endpoint.health.inflight++;try{return await T(n,e,o,m)}catch(t){throw m.endpoint.health.inflight--,t}}u(ie,"request");function Q(e,o,n,m,t,l){const j=G(o);if(j.length===0)return o;const a=j.filter(s=>s.problems.length>0);if(a.length>0&&t.unknownFields==="error")throw new J(`tools: ${a[0].name}: schema is outside the strict subset (${a[0].problems.join("; ")})`,{code:"unsupported_content",param:"tools"});const b=D(o);if(l.retry===!0)return b;const h=u((s,g)=>v(e.config,e.state,s,{kind:g,endpoint:n.name,log:l.log}),"tell");for(const s of a)h(`Upstream ${n.name}: strict tool ${s.name} has a schema outside the strict subset: ${s.problems.join("; ")} (sent without strict)`,"strict");const d=j.filter(s=>s.problems.length===0);return m.info.strict!==!0&&d.length>0&&h(`Upstream ${n.name}: the ${m.name} backend does not enforce strict tool use (${d.map(s=>s.name).join(", ")})`,"strict"),b}u(Q,"checkStrict");async function T(e,o,n,m){const{endpoint:t,model:l,unserved:j,convert:a,pool:b,pin:h}=m,d=Object.hasOwn(e.config.efforts,l)?e.config.efforts[l]:void 0;h&&(o=A(o,h,e.front));const s=e.driver(t),g=`${t.name}
2
+ ${l}`,U=n.images===!1||e.state.textOnly.has(g)&&e.settings(t).imageFallback,$=e.settings(t),q=n.jsonObject==="instruction"||e.state.jsonInstruction.has(g)&&$.jsonObjectFallback,c={...U?{...$,images:!1}:$,...q?{jsonObject:"instruction"}:{},...h?.effort===void 0?{}:{effort:!0}},w=o.stream===!0,p=a===null?e.front==="anthropic"?"anthropic.messages":"openai.chat":t.protocol,O=p==="anthropic.messages"&&t.protocol==="openai.chat";let r,i;a?({body:r,state:i}=await a.request(o,c,{producer:N(t),budget:n.budget,listed:d!==void 0,toolCallIds:s.info.toolCallIds}),i.toolSearch?.capped&&v(e.config,e.state,`Tool search: more than ${R.definitions} tool definitions came back in ToolSearch results for ${t.name}; the rest are not offered.`,{kind:"toolsearch",endpoint:t.name,log:n.log})):O?r=K.request(o,c):r=e.front==="openai"&&(n.filter??c.passthroughFilter===!0)?E(o,s.info.fields):{...o};const F=e.front==="anthropic"?o.metadata?.sampling:void 0;if(p==="openai.chat"){const f=[];r=P(r,F,c,s.info.fields,f),i&&i.dropped.push(...f.filter(k=>!i.dropped.includes(k)))}else if(!a&&F!==void 0&&r.metadata?.sampling!==void 0){const{sampling:f,...k}=r.metadata;r={...r,metadata:k}}if(n.usage===!0&&w&&e.front==="openai"){if(a)i.includeUsage=!0;else if(c.streamUsage){const f=r.stream_options&&typeof r.stream_options=="object"?r.stream_options:{};r.stream_options={...f,include_usage:!0}}}U&&!a&&e.front==="openai"&&(r=_.strip(r)),n.tools===!1&&(r=x.strip(r)),r.model=l,r=Q(e,r,t,s,c,n),d&&p==="openai.chat"&&z(r,d,l,(f,k)=>v(e.config,e.state,f,{type:"effort",endpoint:t.name,model:l,...k,log:n.log})),i?.reasoningMissing&&v(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:n.log}),s.request&&!O&&(r=await s.request(r,{endpoint:t,settings:c,levels:d,state:i,warn:u(f=>v(e.config,e.state,f,{kind:"driver",endpoint:t.name,log:n.log}),"warn")})),i?.dropped?.length>0&&n.retry!==!0&&H(e,{type:"dropped",level:"debug",message:`Upstream ${t.name}: dropped ${i.dropped.join(", ")}`,endpoint:t.name,model:l,fields:[...i.dropped],log:n.log});const S=await y.pathOf(s,p==="anthropic.messages"?"messages":"chat",t,l);return{upstream:t.name,pool:b.map(f=>f.name),method:"POST",url:y.join(t.url,S),path:S,headers:{"content-type":"application/json",accept:w?"text/event-stream":"application/json",...s.info.headers,...a===null&&t.protocol==="anthropic.messages"?n.clientHeaders:void 0,...y.headers(t,s.info.auth,n.headers)},body:r,state:{v:1,upstream:t.name,front:e.front,native:a===null,messages:O,model:o.model,stream:w,unserved:j,convert:i},images:{sent:_.carries(r),retry:c.imageFallback&&p==="openai.chat",key:g},tools:{sent:x.carries(r),retry:c.toolFallback&&p==="openai.chat"},json:{sent:i?.jsonSchemaEmulated===!0,retry:c.jsonObjectFallback&&p==="anthropic.messages",key:g}}}u(T,"prepare");export{ie 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,r)=>le(c,"name",{value:r,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 se}from"../lib/warn.mjs";import{start as be}from"../lib/record.mjs";import{emit as re}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,r={}){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,r.front),U=c&&typeof c=="object"&&!Array.isArray(c),T={model:null,requested:U&&typeof c.model=="string"?c.model:null,upstream:null,front:r.front??i.front,stream:U&&c.stream===!0},k=me(T,r.onUsage,(m,A)=>se(i,b,m,{type:"usage",...A,log:r.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)=>se(i,b,n,{type:"usage",...S,log:r.log}),"warning");return await oe.run({body:c,count:m,options:r,send:_e,warn:A})}return q(await $e({transport:w,textOnly:f,jsonInstruction:v,request:_,response:$,stream:o,error:p,captured:g},c,r,k))}catch(m){if(J)throw m;if(!(m instanceof z))throw r.signal?.aborted||re(g,{type:"error",kind:"exception",level:"error",message:String(m?.message??m),log:r.log}),k.finish(r.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:r.front}))}}u(_e,"send");async function $e({transport:c,textOnly:r,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,usage:!0,budget:ke(),log:d.log},g=u(t=>{re(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,s,R)=>(i.finish(R),$(t,e,a,s)),"fail"),A=u(async t=>{let e=t,a=await Q(c,e,d.signal,I());const s=a.status;if(s===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:s,reason:String(ae(y,s).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=s===400&&[e.images,e.tools].some(y=>y.sent&&y.retry),F=(s===400||s===422)&&e.json.sent&&e.json.retry;if(!a.ok&&(R||F)){const y=await C(a),O=ae(y,s).message,h={};if(s===400&&e.images.sent&&e.images.retry&&ue.refused(O)?(r.add(e.images.key),h.images=!1):s===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"} (${s}: "${String(O).trim().slice(0,500)}"); sent the request again without them.`,endpoint:e.upstream,model:e.body.model,status:s,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 (${s}: "${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:s,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(s){if(!(s instanceof z)||s.code!=="upstream_unreachable")throw s;e=s.message,l=void 0,x=s,g({type:"error",kind:"transport",level:"error",message:s.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(s){if(!(s instanceof z)||s.code!=="endpoint_not_in_pool")throw s}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,r,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(r.url,{method:r.method,headers:r.headers,body:JSON.stringify(r.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 (${r.upstream}): ${p}`,{code:"upstream_unreachable",status:502,details:{cause:o}})}finally{clearTimeout(v)}}u(Q,"call");async function C(c){const r=await c.text();try{return JSON.parse(r)}catch{return r===""?null:r}}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
@@ -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 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