@cldmv/llm-translator 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -6
- package/dist/api/convert/anthropic/messages/to/openai/chat/request.mjs +11 -11
- package/dist/api/convert/anthropic/messages/to/openai/chat/response.mjs +1 -1
- package/dist/api/convert/anthropic/messages/to/openai/chat/stream.mjs +1 -1
- package/dist/api/convert/openai/chat/to/anthropic/messages/request.mjs +3 -3
- package/dist/api/convert/openai/chat/to/anthropic/messages/response.mjs +1 -1
- package/dist/api/convert/openai/chat/to/anthropic/messages/stream.mjs +1 -1
- package/dist/api/detect.mjs +1 -1
- package/dist/api/driver/anthropic/token/count.mjs +1 -1
- package/dist/api/driver/compatible/detect.mjs +1 -1
- package/dist/api/driver/compatible/info.mjs +1 -1
- package/dist/api/driver/deepseek/detect.mjs +2 -0
- package/dist/api/driver/deepseek/error.mjs +2 -0
- package/dist/api/driver/deepseek/info.mjs +2 -0
- package/dist/api/driver/deepseek/request.mjs +2 -0
- package/dist/api/driver/deepseek/settings.mjs +2 -0
- package/dist/api/driver/llamacpp/chunk.mjs +2 -0
- package/dist/api/driver/llamacpp/detect.mjs +2 -0
- package/dist/api/driver/llamacpp/flush.mjs +2 -0
- package/dist/api/driver/llamacpp/info.mjs +2 -0
- package/dist/api/driver/llamacpp/request.mjs +2 -0
- package/dist/api/driver/llamacpp/response.mjs +2 -0
- package/dist/api/driver/llamacpp/settings.mjs +2 -0
- package/dist/api/driver/ollama/detect.mjs +2 -0
- package/dist/api/driver/ollama/info.mjs +2 -0
- package/dist/api/driver/ollama/request.mjs +2 -0
- package/dist/api/driver/ollama/settings.mjs +2 -0
- package/dist/api/driver/sglang/detect.mjs +2 -0
- package/dist/api/driver/sglang/info.mjs +2 -0
- package/dist/api/driver/sglang/request.mjs +2 -0
- package/dist/api/driver/sglang/settings.mjs +2 -0
- package/dist/api/driver/vllm/detect.mjs +1 -1
- package/dist/api/driver/vllm/info.mjs +1 -1
- package/dist/api/endpoint/status.mjs +2 -0
- package/dist/api/error/error.mjs +1 -1
- package/dist/api/model/get.mjs +2 -0
- package/dist/api/request.mjs +2 -2
- package/dist/api/route.mjs +2 -0
- package/dist/api/send.mjs +1 -1
- package/dist/api/server/plugin.mjs +1 -1
- package/dist/api/server/start.mjs +1 -1
- package/dist/api/status.mjs +1 -1
- package/dist/api/stream.mjs +1 -1
- package/dist/index.cjs +3 -3
- package/dist/index.mjs +1 -1
- package/dist/lib/backends.mjs +2 -0
- package/dist/lib/capture.mjs +1 -1
- package/dist/lib/cli.mjs +1 -1
- package/dist/lib/config.mjs +1 -1
- package/dist/lib/estimate.mjs +1 -1
- package/dist/lib/http.mjs +1 -1
- package/dist/lib/ids.mjs +1 -1
- package/dist/lib/models.mjs +1 -1
- package/dist/lib/passthrough.mjs +1 -1
- package/dist/lib/pool.mjs +2 -0
- package/dist/lib/route.mjs +1 -1
- package/dist/lib/routes.mjs +1 -1
- package/dist/lib/settings.mjs +1 -1
- package/dist/lib/thinking.mjs +2 -1
- package/dist/lib/tools.mjs +2 -0
- package/dist/lib/toolsearch.mjs +3 -0
- package/dist/lib/transport.mjs +1 -1
- package/dist/lib/usage.mjs +2 -0
- package/package.json +13 -7
- package/types/src/api/convert/anthropic/messages/to/openai/chat/request.d.mts +6 -15
- package/types/src/api/convert/anthropic/messages/to/openai/chat/response.d.mts +3 -16
- package/types/src/api/convert/anthropic/messages/to/openai/chat/stream.d.mts +1 -15
- package/types/src/api/convert/openai/chat/to/anthropic/messages/request.d.mts +1 -15
- package/types/src/api/convert/openai/chat/to/anthropic/messages/response.d.mts +3 -17
- package/types/src/api/convert/openai/chat/to/anthropic/messages/stream.d.mts +1 -15
- package/types/src/api/detect.d.mts +1 -15
- package/types/src/api/driver/anthropic/detect.d.mts +1 -1
- package/types/src/api/driver/anthropic/info.d.mts +1 -1
- package/types/src/api/driver/anthropic/model/list.d.mts +1 -15
- package/types/src/api/driver/anthropic/settings.d.mts +1 -1
- package/types/src/api/driver/anthropic/token/count.d.mts +1 -15
- package/types/src/api/driver/compatible/detect.d.mts +2 -16
- package/types/src/api/driver/compatible/info.d.mts +3 -20
- package/types/src/api/driver/compatible/model/list.d.mts +1 -15
- package/types/src/api/driver/compatible/request.d.mts +1 -15
- package/types/src/api/driver/compatible/settings.d.mts +1 -1
- package/types/src/api/driver/deepseek/detect.d.mts +26 -0
- package/types/src/api/driver/deepseek/error.d.mts +14 -0
- package/types/src/api/driver/deepseek/info.d.mts +29 -0
- package/types/src/api/driver/deepseek/request.d.mts +8 -0
- package/types/src/api/driver/deepseek/settings.d.mts +25 -0
- package/types/src/api/driver/llamacpp/chunk.d.mts +7 -0
- package/types/src/api/driver/llamacpp/detect.d.mts +6 -0
- package/types/src/api/driver/llamacpp/flush.d.mts +26 -0
- package/types/src/api/driver/llamacpp/info.d.mts +31 -0
- package/types/src/api/driver/llamacpp/request.d.mts +8 -0
- package/types/src/api/driver/llamacpp/response.d.mts +8 -0
- package/types/src/api/driver/llamacpp/settings.d.mts +27 -0
- package/types/src/api/driver/ollama/detect.d.mts +6 -0
- package/types/src/api/driver/ollama/info.d.mts +30 -0
- package/types/src/api/driver/ollama/request.d.mts +8 -0
- package/types/src/api/driver/ollama/settings.d.mts +7 -0
- package/types/src/api/driver/openai/detect.d.mts +1 -1
- package/types/src/api/driver/openai/info.d.mts +1 -1
- package/types/src/api/driver/openai/model/list.d.mts +1 -15
- package/types/src/api/driver/openai/request.d.mts +1 -15
- package/types/src/api/driver/openai/settings.d.mts +1 -1
- package/types/src/api/driver/sglang/detect.d.mts +6 -0
- package/types/src/api/driver/sglang/info.d.mts +31 -0
- package/types/src/api/driver/sglang/request.d.mts +8 -0
- package/types/src/api/driver/sglang/settings.d.mts +24 -0
- package/types/src/api/driver/vllm/detect.d.mts +3 -17
- package/types/src/api/driver/vllm/error.d.mts +1 -15
- package/types/src/api/driver/vllm/info.d.mts +3 -20
- package/types/src/api/driver/vllm/model/list.d.mts +1 -15
- package/types/src/api/driver/vllm/probe.d.mts +1 -15
- package/types/src/api/driver/vllm/request.d.mts +1 -15
- package/types/src/api/driver/vllm/settings.d.mts +1 -15
- package/types/src/api/driver/vllm/token/count.d.mts +1 -15
- package/types/src/api/endpoint/status.d.mts +27 -0
- package/types/src/api/error/build.d.mts +1 -15
- package/types/src/api/error/error.d.mts +1 -15
- package/types/src/api/model/get.d.mts +9 -0
- package/types/src/api/model/list.d.mts +1 -15
- package/types/src/api/request.d.mts +8 -17
- package/types/src/api/response.d.mts +1 -15
- package/types/src/api/route.d.mts +18 -0
- package/types/src/api/send.d.mts +21 -16
- package/types/src/api/server/plugin.d.mts +6 -19
- package/types/src/api/server/start.d.mts +2 -16
- package/types/src/api/server/stop.d.mts +1 -15
- package/types/src/api/sse/encode.d.mts +1 -15
- package/types/src/api/sse/parse.d.mts +1 -15
- package/types/src/api/sse/ping.d.mts +1 -15
- package/types/src/api/sse/write.d.mts +1 -15
- package/types/src/api/status.d.mts +2 -16
- package/types/src/api/stream.d.mts +1 -15
- package/types/src/api/token/count.d.mts +1 -15
- package/types/src/index.d.mts +24 -34
- package/types/src/lib/arguments.d.mts +2 -16
- package/types/src/lib/backends.d.mts +21 -0
- package/types/src/lib/capture.d.mts +2 -16
- package/types/src/lib/cli.d.mts +12 -26
- package/types/src/lib/config.d.mts +11 -25
- package/types/src/lib/documents.d.mts +16 -27
- package/types/src/lib/errors.d.mts +75 -75
- package/types/src/lib/estimate.d.mts +58 -7
- package/types/src/lib/finish.d.mts +4 -4
- package/types/src/lib/http.d.mts +19 -5
- package/types/src/lib/ids.d.mts +11 -20
- package/types/src/lib/images.d.mts +19 -19
- package/types/src/lib/models.d.mts +13 -13
- package/types/src/lib/normalize.d.mts +3 -3
- package/types/src/lib/passthrough.d.mts +11 -17
- package/types/src/lib/pdf.d.mts +4 -18
- package/types/src/lib/ping.d.mts +1 -15
- package/types/src/lib/pool.d.mts +124 -0
- package/types/src/lib/route.d.mts +31 -24
- package/types/src/lib/routes.d.mts +10 -15
- package/types/src/lib/runtime.d.mts +2 -16
- package/types/src/lib/schema.d.mts +1 -15
- package/types/src/lib/settings.d.mts +68 -54
- package/types/src/lib/source.d.mts +2 -2
- package/types/src/lib/sse.d.mts +21 -32
- package/types/src/lib/thinking.d.mts +117 -22
- package/types/src/lib/tools.d.mts +28 -0
- package/types/src/lib/toolsearch.d.mts +91 -0
- package/types/src/lib/transport.d.mts +3 -16
- package/types/src/lib/upstream-error.d.mts +4 -18
- package/types/src/lib/usage.d.mts +91 -0
- package/types/src/lib/warn.d.mts +1 -1
package/README.md
CHANGED
|
@@ -17,14 +17,15 @@ 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.
|
|
20
|
+
### Latest: v0.3.0 (November 2026)
|
|
21
21
|
|
|
22
|
-
- **
|
|
23
|
-
- **
|
|
24
|
-
- [View full v0.
|
|
22
|
+
- **Usage reporting, `route()` and the server `authorize` hook** — every `send()` reply has a `usage` promise (and an `onUsage` callback) with the final token counts; `api.route(body)` says which upstream and model a request would use; `server.start` and `server.plugin` take `authorize(request)` and `onUsage(report, context)` so an app with its own key system can gate and meter requests.
|
|
23
|
+
- **More servers and endpoint pools** — drivers for llama.cpp, Ollama, SGLang and the DeepSeek API, failover and load balancing between upstreams that serve the same model, the `reasoningRoundTrip` and `toolSearch` settings, estimated usage when an upstream sends none, and `GET /v1/models/{id}`. Requires Node.js 24 or newer.
|
|
24
|
+
- [View full v0.3.0 Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.3.0.md)
|
|
25
25
|
|
|
26
26
|
### Recent Releases
|
|
27
27
|
|
|
28
|
+
- **v0.2.0** (October 2026) — the first working translator: six Anthropic Messages and OpenAI Chat Completions converters, backend detection (vLLM, OpenAI, Anthropic, generic), model aliases, the optional Fastify server and the `llm-translator` CLI for Claude Code ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.2.0.md))
|
|
28
29
|
- **v0.1.0** (October 2026) — package scaffold: ESM and CommonJS entry points, tsup build, the `llm-translator-dev` condition with devcheck, tests on `@cldmv/vitest-runner` and the CLDMV v4 CI/release pipeline; the only export was a placeholder ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.1.0.md))
|
|
29
30
|
|
|
30
31
|
📚 **For complete version history and detailed release notes, see the [docs/changelog/](https://github.com/CLDMV/llm-translator/tree/master/docs/changelog/) folder.**
|
|
@@ -63,7 +64,8 @@ Lines, statements, functions and branches are all at 100% and enforced on every
|
|
|
63
64
|
|
|
64
65
|
### Requirements
|
|
65
66
|
|
|
66
|
-
- Node.js
|
|
67
|
+
- Node.js 24 or newer
|
|
68
|
+
- Developing the package also needs Node.js 24 or newer (`devEngines` enforces it on install and `npm run`)
|
|
67
69
|
- Fastify 5, only for the server and the Fastify plugin (an optional peer dependency)
|
|
68
70
|
|
|
69
71
|
### Install
|
|
@@ -97,6 +99,10 @@ const call = await api.request({ model: "claude-opus-5-5", max_tokens: 256, mess
|
|
|
97
99
|
// Or let the translator do the full round trip.
|
|
98
100
|
const reply = await api.send({ model: "qwen3-coder", max_tokens: 256, stream: true, messages: [{ role: "user", content: "hi" }] });
|
|
99
101
|
if (reply.stream) for await (const { event, data } of reply.stream) console.log(event, data);
|
|
102
|
+
|
|
103
|
+
// Where would a request go, and what did it cost? (see docs/server.md)
|
|
104
|
+
const { upstream, model } = api.route({ model: "claude-opus-5-5" }); // no I/O
|
|
105
|
+
const { input_tokens, output_tokens, reported, estimated } = await reply.usage; // or send(body, { onUsage }); `estimated` holds the guesses written into the client reply when the upstream sent no usage
|
|
100
106
|
```
|
|
101
107
|
|
|
102
108
|
### Server and Claude Code
|
|
@@ -116,7 +122,7 @@ Then point Claude Code at it with `ANTHROPIC_BASE_URL=http://127.0.0.1:8787`. Th
|
|
|
116
122
|
|
|
117
123
|
## 📚 Configuration
|
|
118
124
|
|
|
119
|
-
`createTranslator(config)` takes `front`, `upstreams` (URLs or objects with `url`, `apiKey`, `backend`, `models`, `default` and `settings`), `aliases`, global `settings`, `detect` and a custom `transport`. The CLI reads the same keys from flags, `LLM_TRANSLATOR_*` environment variables and a JSON file, in that order of precedence.
|
|
125
|
+
`createTranslator(config)` takes `front`, `upstreams` (URLs or objects with `url`, `apiKey`, `backend`, `models`, `default` and `settings`), `aliases`, global `settings`, `pool` (failover and load balancing between upstreams that serve the same model, see [docs/server.md](https://github.com/CLDMV/llm-translator/blob/master/docs/server.md#endpoint-pools-failover-and-load-balancing)), `detect` and a custom `transport`. The CLI reads the same keys from flags, `LLM_TRANSLATOR_*` environment variables and a JSON file, in that order of precedence.
|
|
120
126
|
|
|
121
127
|
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).
|
|
122
128
|
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
var
|
|
1
|
+
var O=Object.defineProperty;var d=(e,t)=>O(e,"name",{value:t,configurable:!0});import{resolve as E}from"../../../../../../../lib/settings.mjs";import{TranslatorError as T}from"../../../../../../../lib/errors.mjs";import{createNameFitter as M}from"../../../../../../../lib/ids.mjs";import{sanitize as B}from"../../../../../../../lib/schema.mjs";import{NOTE as I}from"../../../../../../../lib/images.mjs";import{expand as A}from"../../../../../../../lib/documents.mjs";import{createBudget as L,unsign as z}from"../../../../../../../lib/thinking.mjs";import{plan as C,scan as D}from"../../../../../../../lib/toolsearch.mjs";const P=new Set(["model","messages","system","max_tokens","stream","temperature","top_p","top_k","stop_sequences","tools","tool_choice","thinking","output_config","output_format","context_management"]),W=new Set(["metadata","cache_control","service_tier"]),H=new Set(["mcp_servers","container","inference_geo"]),K="x-anthropic-billing-header:";async function fe(e,t,o){const n=E(t);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new T("messages: Field required",{code:"invalid_request",param:"messages"});const i={front:"anthropic",model:e.model,stream:e.stream===!0,thinking:e.thinking?.type??null,reasoningField:n.reasoningField,toolNames:{},dropped:[]};o?.producer!==void 0&&(i.producer=o.producer);const s=M({max:64,map:i.toolNames});for(const c of e.tools??[])(c.type===void 0||c.type==="custom")&&s(c.name);const m=D(e.messages,new Set((e.tools??[]).map(c=>c.name))),l=C(e.tools??[],m,n,e.tool_choice);i.toolSearch={mode:n.toolSearch,loaded:l.loaded,hidden:l.hidden,stubbed:l.stubbed,capped:m.capped};for(const c of Object.keys(e))if(!(P.has(c)||W.has(c)))if(H.has(c))g(i,c);else{if(n.unknownFields==="error")throw new T(`${c}: Extra inputs are not permitted`,{code:"unsupported_field",param:c});g(i,c)}const r={model:e.model},h=N(e.system,n),k=await R(e.messages,n,i,s,h,U(e.context_management,i),o?.budget??L());r.messages=k.messages,k.prefill&&(r.continue_final_message=!0,r.add_generation_prompt=!1),e.max_tokens!==void 0&&(r[n.maxTokensField]=e.max_tokens),e.temperature!==void 0&&(r.temperature=e.temperature),e.top_p!==void 0&&(r.top_p=e.top_p),e.top_k!==void 0&&(n.topK?r.top_k=e.top_k:g(i,"top_k")),e.stop_sequences?.length>0&&(r.stop=e.stop_sequences),i.stream&&(r.stream=!0,n.streamUsage&&(r.stream_options={include_usage:!0},n.continuousUsage&&(r.stream_options.continuous_usage_stats=!0)));const u=X(l.offer,n,i,s);if(u.length>0){r.tools=u;const c=Y(e.tool_choice,n,i,s);c!==void 0&&(r.tool_choice=c),e.tool_choice?.disable_parallel_tool_use===!0&&n.parallelToolCalls&&(r.parallel_tool_calls=!1)}return Z(e,n,r),b(e,n,i,r),{body:r,state:i}}d(fe,"request");function g(e,t){e.dropped.includes(t)||e.dropped.push(t)}d(g,"drop");function v(e,t,o){if(t.unknownFields==="error")throw new T(`${o}: is not supported by this translator`,{code:"unsupported_content",param:o});g(e,o)}d(v,"unsupported");function N(e,t){return e==null?"":(typeof e=="string"?[{type:"text",text:e}]:e).filter(n=>n.type==="text"&&!(t.stripBillingHeader&&n.text.trimStart().startsWith(K))).map(n=>n.text).join(`
|
|
2
2
|
|
|
3
|
-
`)}
|
|
4
|
-
`);return[{type:"text",text:`[Search result: ${
|
|
5
|
-
${
|
|
6
|
-
${
|
|
7
|
-
</system-reminder>`;
|
|
8
|
-
`);
|
|
3
|
+
`)}d(N,"textOf");function j(e){return typeof e=="string"?[{type:"text",text:e}]:e??[]}d(j,"asBlocks");async function R(e,t,o,n,i,s,m){const l=[],r=[],h=new Map,k=t.inlineSystem==="auto"?"user":t.inlineSystem,u=new Map;if(t.reasoningRoundTrip){for(let a=e.length-1;a>=0;a--)if(e[a].role==="assistant")for(const f of[...j(e[a].content)].reverse())f.type==="thinking"&&u.set(f,await z(f.signature,{producer:o.producer,budget:m}))}const c=d(a=>{const f=l.at(-1);f?.role==="user"?f.parts.push(...a):l.push({role:"user",parts:a})},"pushUser"),w=d(async(a,f)=>{switch(a.type){case"text":return a.text===""?[]:[{type:"text",text:a.text}];case"image":return V(a,t,o);case"document":{const _=[];for(const p of await A(a,{settings:t,toolInputs:h,toolUseId:f}))_.push(...await w(p,f));return _}case"search_result":{const _=(a.content??[]).map(p=>p.text).join(`
|
|
4
|
+
`);return[{type:"text",text:`[Search result: ${a.title??""} (${a.source??""})]
|
|
5
|
+
${_}`}]}case"tool_reference":return t.toolSearch==="native"&&t.deferredTools!=="drop"?[{type:"tool_reference",tool_name:a.tool_name}]:[{type:"text",text:o.toolSearch.loaded.includes(a.tool_name)?`[Tool loaded: ${a.tool_name}]`:`[Tool reference: ${a.tool_name}]`}];default:return v(o,t,`block:${a.type}`),[]}},"toParts");for(const a of e)if(a.role==="system"){const f=N(a.content,t);if(f===""||k==="drop")continue;if(k==="keep")l.push({role:"system",content:f});else if(k==="hoist")r.push(f);else{const _=f.trimStart().startsWith("<system-reminder>")?f:`<system-reminder>
|
|
6
|
+
${f}
|
|
7
|
+
</system-reminder>`;c([{type:"text",text:_}])}}else if(a.role==="user"){const f=[],_=[];for(const y of j(a.content)){if(y.type!=="tool_result"){f.push(...await w(y,null));continue}const S=[];for(const x of j(y.content))S.push(...await w(x,y.tool_use_id));const q=S.filter(x=>x.type==="text").map(x=>x.text),F=S.filter(x=>x.type==="image_url");let $=S.some(x=>x.type==="tool_reference")?S.filter(x=>x.type!=="image_url"):q.join(`
|
|
8
|
+
`);F.length>0&&(_.push({type:"text",text:`Image output of tool call ${y.tool_use_id}:`},...F),$===""&&($="(image output attached in the next message)")),y.is_error===!0&&t.toolErrors&&($=typeof $=="string"?`[error] ${$}`:[{type:"text",text:"[error]"},...$]),l.push({role:"tool",tool_call_id:y.tool_use_id,content:$})}const p=[..._,...f];p.length>0&&c(p)}else if(a.role==="assistant"){const f={role:"assistant",texts:[],parts:[],tool_calls:[]};for(const p of j(a.content))if(p.type==="text")p.text!==""&&f.texts.push(p.text);else if(p.type==="thinking"){const y=u.get(p)??null;y?f.parts.push({restored:y}):p.thinking&&f.parts.push({text:p.thinking})}else p.type==="redacted_thinking"?g(o,"redacted_thinking"):p.type==="tool_use"?(h.set(p.id,p.input),f.tool_calls.push({id:p.id,type:"function",function:{name:n(p.name),arguments:JSON.stringify(p.input??{})}})):v(o,t,`block:${p.type}`);const _=l.at(-1);_?.role==="assistant"?(_.texts.push(...f.texts),_.parts.push(...f.parts),_.tool_calls.push(...f.tool_calls)):l.push(f)}else throw new T(`messages: unsupported role "${a.role}"`,{code:"invalid_request",param:"messages"});return Q(l,t,i,r,s,o)}d(R,"convertMessages");function U(e,t){let o=1/0;for(const n of e?.edits??[])typeof n.type=="string"&&n.type.startsWith("clear_thinking")?n.keep==="all"?o=1/0:o=n.keep?.value??1:g(t,`context_management.${n.type}`);return o}d(U,"thinkingTurnsToKeep");function G(e,t){const o=e.find(r=>r.restored&&(r.restored.reasoning_content!==void 0||r.restored.reasoning!==void 0)),n=o?o.restored.reasoning_content!==void 0?"reasoning_content":"reasoning":t;let i="",s,m=null;for(const r of e){const h=r.restored?r.restored.reasoning_content??r.restored.reasoning:r.text;r.restored?.reasoning_details&&(s=[...s??[],...r.restored.reasoning_details]),h!==void 0&&(i+=(m!==null&&!(m.restored&&r.restored)?`
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
`:"")+h,m=r)}const l={};return i!==""&&(l[n]=i),s&&(l.reasoning_details=s),l}d(G,"reasoningFields");function J(e){return!!(e.reasoning_content||e.reasoning||e.reasoning_details)}d(J,"reasoningSent");function Q(e,t,o,n,i,s){let m=i;for(let u=e.length-1;u>=0;u--)e[u].role!=="assistant"||e[u].parts.length===0||(m>0?m--:e[u].parts=[]);const l=[],r=[o,...n].filter(u=>u!=="").join(`
|
|
11
11
|
|
|
12
|
-
`):
|
|
12
|
+
`);r!==""&&l.push({role:"system",content:r});for(const u of e)if(u.role==="user"){const c=u.parts.every(w=>w.type==="text");l.push({role:"user",content:c?u.parts.map(w=>w.text).join(`
|
|
13
13
|
|
|
14
|
-
`)}
|
|
14
|
+
`):u.parts})}else if(u.role==="assistant"){const c={role:"assistant",content:u.texts.join(`
|
|
15
15
|
|
|
16
|
-
`)),
|
|
16
|
+
`)};u.parts.length>0&&Object.assign(c,G(u.parts,t.reasoningField)),t.reasoningRoundTrip&&u.tool_calls.length>0&&!J(c)&&(s.reasoningMissing=(s.reasoningMissing??0)+1),u.tool_calls.length>0&&(c.tool_calls=u.tool_calls),l.push(c)}else l.push(u);const h=l.at(-1),k=t.prefill==="continue_final_message"&&h?.role==="assistant"&&!h.tool_calls&&h.content!=="";return{messages:l,prefill:k}}d(Q,"finalize");function V(e,t,o){if(!t.images)return g(o,"image"),[{type:"text",text:I}];const n=e.source;return n.type==="base64"?[{type:"image_url",image_url:{url:`data:${n.media_type};base64,${n.data}`}}]:n.type==="url"?[{type:"image_url",image_url:{url:n.url}}]:(g(o,"image.file"),[{type:"text",text:"[An image supplied as an uploaded file could not be forwarded.]"}])}d(V,"imageParts");function X(e,t,o,n){const i=[];for(const s of e){if(s.type!==void 0&&s.type!=="custom"){g(o,`tool:${s.name}`);continue}if(s.defer_loading===!0&&t.deferredTools==="drop"){g(o,"deferred_tools");continue}const m=s.input_schema??{type:"object",properties:{}},l={name:n(s.name),parameters:t.toolSchema==="sanitize"?B(m):m};s.description!==void 0&&(l.description=s.description),s.strict!==void 0&&(l.strict=s.strict),i.push(s.defer_loading===!0&&t.toolSearch==="native"?{type:"function",function:l,defer_loading:!0}:{type:"function",function:l})}return i}d(X,"convertTools");function Y(e,t,o,n){switch(e?.type){case void 0:return;case"auto":return"auto";case"any":return"required";case"none":return"none";case"tool":return{type:"function",function:{name:n(e.name)}};default:v(o,t,`tool_choice:${e.type}`);return}}d(Y,"convertToolChoice");function Z(e,t,o){const n=e.thinking,i=n?.type;let s;i==="disabled"?(t.thinking==="reasoning_effort"&&(s="none"),t.thinking==="chat_template_kwargs"&&(o.chat_template_kwargs={enable_thinking:!1})):(t.effort&&e.output_config?.effort!==void 0&&(s=e.output_config.effort),i==="enabled"&&(t.thinking==="chat_template_kwargs"&&(o.chat_template_kwargs={enable_thinking:!0},t.thinkingBudget&&n.budget_tokens!==void 0&&e.max_tokens!==void 0&&(o.thinking_token_budget=Math.max(0,Math.min(n.budget_tokens,e.max_tokens-1)))),t.thinking==="reasoning_effort"&&s===void 0&&(s="medium"))),s!==void 0&&(o.reasoning_effort=s)}d(Z,"applyThinking");function b(e,t,o,n){const i=e.output_config?.format??e.output_format;i!==void 0&&(t.responseFormat&&i.type==="json_schema"?n.response_format={type:"json_schema",json_schema:{name:"output",schema:i.schema}}:g(o,"output_config.format"));for(const s of Object.keys(e.output_config??{}))s!=="effort"&&s!=="format"&&g(o,`output_config.${s}`)}d(b,"applyOutputConfig");export{fe as request};
|
|
17
17
|
//# sourceMappingURL=request.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var m=Object.defineProperty;var h=(o,e)=>m(o,"name",{value:e,configurable:!0});import{resolve as _}from"../../../../../../../lib/settings.mjs";import{restoreName as x}from"../../../../../../../lib/ids.mjs";import{detailOf as y}from"../../../../../../../lib/thinking.mjs";import{toOpenAIFinish as N,toOpenAIUsage as O}from"../../../../../../../lib/finish.mjs";function A(o,e,d){const c=_(d),g=e?.toolNames??{},s=[],l=[],n=[],r=[];let p=0,f=!1;for(const t of o.content??[]){if(c.reasoningRoundTrip){const u=y(t,p+(f?1:0));u&&r.push(u)}t.type==="text"&&t.text!==""&&(f=!0),t.type==="tool_use"&&p++,t.type==="text"?s.push(t.text):t.type==="thinking"?l.push(t.thinking):t.type==="tool_use"&&n.push({id:t.id,type:"function",function:{name:x(g,t.name),arguments:JSON.stringify(t.input??{})}})}const i={role:"assistant",content:s.length===0&&n.length>0?null:s.join("")};l.length>0&&(i[c.reasoningField]=l.join("")),r.length>0&&(i.reasoning_details=r),n.length>0&&(i.tool_calls=n);const a={index:0,message:i,finish_reason:N(o.stop_reason)};return typeof o.stop_sequence=="string"&&(a.stop_reason=o.stop_sequence),e&&(e.upstreamUsage=o.usage),{id:`chatcmpl-${String(o.id).replace(/^msg_/,"")}`,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:e?.model??o.model,choices:[a],usage:O(o.usage)}}h(A,"response");export{A as response};
|
|
2
2
|
//# sourceMappingURL=response.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var O=Object.defineProperty;var r=(d,o)=>O(d,"name",{value:o,configurable:!0});import{resolve as N}from"../../../../../../../lib/settings.mjs";import{anthropicToOpenAIType as U,anthropicTypeToStatus as j}from"../../../../../../../lib/errors.mjs";import{completionId as D,restoreName as T}from"../../../../../../../lib/ids.mjs";import{toOpenAIFinish as E,toOpenAIUsage as I}from"../../../../../../../lib/finish.mjs";import{messages as w}from"../../../../../../../lib/sse.mjs";import{detailOf as A}from"../../../../../../../lib/thinking.mjs";async function*R(d,o,k){const u=N(k),x=o?.toolNames??{},b=o?.includeUsage??!0,y=Math.floor(Date.now()/1e3),s=new Map;let c=D(),p=o?.model,a={},g=!1,n=null,_=!1,h=0;const i=r((e,t=null,l={})=>({data:{id:c,object:"chat.completion.chunk",created:y,model:p,choices:[{index:0,delta:e,finish_reason:t,...l}]}}),"chunk"),f=r((e,t)=>({data:{error:{message:t,type:U(e),param:null,code:j(e)}}}),"failure");try{for await(const{data:e}of w(d))if(e.type==="message_start")c=`chatcmpl-${String(e.message.id).replace(/^msg_/,"")}`,p=o?.model??e.message.model,a={...e.message.usage},o&&(o.upstreamUsage=a),yield i({role:"assistant",content:""});else if(e.type==="content_block_start"){const t=e.content_block;if((t.type==="thinking"||t.type==="redacted_thinking")&&(n={type:t.type,data:t.data,thinking:"",signature:"",slot:h+(_?1:0)}),t.type==="tool_use"&&h++,t.type==="tool_use"){s.set(e.index,s.size);const l=T(x,t.name);yield i({tool_calls:[{index:s.get(e.index),id:t.id,type:"function",function:{name:l,arguments:""}}]})}}else if(e.type==="content_block_delta"){const t=e.delta;t.type==="text_delta"?(t.text!==""&&(_=!0),yield i({content:t.text})):t.type==="thinking_delta"?(n&&(n.thinking+=t.thinking),yield i({[u.reasoningField]:t.thinking})):t.type==="signature_delta"?n&&(n.signature+=t.signature):t.type==="input_json_delta"&&s.has(e.index)&&(yield i({tool_calls:[{index:s.get(e.index),function:{arguments:t.partial_json}}]}))}else if(e.type==="content_block_stop"){const t=u.reasoningRoundTrip&&n?A(n,n.slot):null;t&&(yield i({reasoning_details:[t]})),n=null}else if(e.type==="message_delta"){for(const[l,m]of Object.entries(e.usage??{}))m!=null&&(a[l]=m);o&&(o.upstreamUsage=a);const t=typeof e.delta?.stop_sequence=="string"?{stop_reason:e.delta.stop_sequence}:{};yield i({},E(e.delta?.stop_reason),t)}else if(e.type==="message_stop"){g=!0;break}else if(e.type==="error"){yield f(e.error.type,e.error.message),yield{data:"[DONE]"};return}}catch(e){yield f(e.type??"api_error",e.message),yield{data:"[DONE]"};return}if(!g){yield f("api_error","Upstream stream ended before completion"),yield{data:"[DONE]"};return}b&&(yield{data:{id:c,object:"chat.completion.chunk",created:y,model:p,choices:[],usage:I(a)}}),yield{data:"[DONE]"}}r(R,"stream");export{R as stream};
|
|
2
2
|
//# sourceMappingURL=stream.mjs.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
var
|
|
1
|
+
var v=Object.defineProperty;var u=(e,s)=>v(e,"name",{value:s,configurable:!0});import{resolve as T}from"../../../../../../../lib/settings.mjs";import{TranslatorError as x}from"../../../../../../../lib/errors.mjs";import{anthropicToolId as j,createNameFitter as q}from"../../../../../../../lib/ids.mjs";import{parseArguments as A}from"../../../../../../../lib/arguments.mjs";import{sanitize as N}from"../../../../../../../lib/schema.mjs";import{blocksOf as $}from"../../../../../../../lib/thinking.mjs";const F=new Set(["model","messages","max_tokens","max_completion_tokens","temperature","top_p","top_k","stop","stream","stream_options","tools","tool_choice","parallel_tool_calls","reasoning_effort","response_format","user","n"]),I=new Set(["metadata","store","service_tier"]),h=/^data:([^;,]+);base64,(.*)$/s;async function P(e,s){const p=T(s);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new x("messages is a required property",{code:"invalid_request",param:"messages"});if(e.n!==void 0&&e.n!==1)throw new x("n: only a single choice (n = 1) is supported",{code:"unsupported_field",param:"n"});const o={front:"openai",model:e.model,stream:e.stream===!0,includeUsage:e.stream_options?.include_usage===!0,reasoningField:p.reasoningField,toolNames:{},dropped:[]},l=q({max:128,map:o.toolNames});for(const t of Object.keys(e))if(!(F.has(t)||I.has(t))){if(p.unknownFields==="error")throw new x(`${t}: Unrecognized request argument`,{code:"unsupported_field",param:t});g(o,t)}const c=[],a=[],m=u((t,r)=>{const i=a.at(-1);i?.role===t?i.blocks.push(...r):a.push({role:t,blocks:[...r]})},"push");for(const t of e.messages)switch(t.role){case"system":case"developer":{const r=w(t.content);r!==""&&c.push(r);break}case"user":m("user",z(t.content,o));break;case"assistant":{const r=p.reasoningRoundTrip?$(t.reasoning_details):[],i=[],f=w(t.content);f!==""?i.push({type:"text",text:f}):typeof t.refusal=="string"&&t.refusal!==""&&i.push({type:"text",text:t.refusal}),S(t)&&!r.some(d=>d.block.type==="thinking")&&g(o,"reasoning");for(const d of t.tool_calls??[])i.push({type:"tool_use",id:j(d.id),name:l(d.function.name),input:A(d.function.arguments)});const k=M(r,i);k.length>0&&m("assistant",k);break}case"tool":{const r={type:"tool_result",tool_use_id:j(t.tool_call_id),content:w(t.content)},i=a.at(-1);if(i?.role==="user"){const f=i.blocks.findIndex(k=>k.type!=="tool_result");i.blocks.splice(f===-1?i.blocks.length:f,0,r)}else a.push({role:"user",blocks:[r]});break}default:throw new x(`messages: unsupported role "${t.role}"`,{code:"invalid_request",param:"messages"})}const n={model:e.model,max_tokens:e.max_completion_tokens??e.max_tokens??p.defaultMaxTokens};c.length>0&&(n.system=c.join(`
|
|
2
2
|
|
|
3
|
-
`)),n.messages=
|
|
3
|
+
`)),n.messages=a.map(t=>({role:t.role,content:t.blocks})),e.temperature!==void 0&&(n.temperature=Math.min(1,e.temperature)),e.top_p!==void 0&&(n.top_p=e.top_p),e.top_k!==void 0&&(n.top_k=e.top_k),e.stop!==void 0&&e.stop!==null&&(n.stop_sequences=[].concat(e.stop)),o.stream&&(n.stream=!0),typeof e.user=="string"&&(n.metadata={user_id:e.user});const b=[];for(const t of e.tools??[]){if(t.type!=="function"){g(o,`tool:${t.type}`);continue}const r=t.function,i=r.parameters??{type:"object",properties:{}},f={name:l(r.name),input_schema:p.toolSchema==="sanitize"?N(i):i};r.description!==void 0&&(f.description=r.description),b.push(f)}if(b.length>0){n.tools=b;let t=D(e.tool_choice,l);e.parallel_tool_calls===!1&&t.type!=="none"&&(t={...t,disable_parallel_tool_use:!0}),(e.tool_choice!==void 0||e.parallel_tool_calls===!1)&&(n.tool_choice=t)}const _=e.reasoning_effort;_==="none"?n.thinking={type:"disabled"}:_!=null&&(n.thinking={type:"adaptive"},n.output_config={effort:_==="minimal"?"low":_});const y=e.response_format;return y?.type==="json_schema"?n.output_config={...n.output_config,format:{type:"json_schema",schema:y.json_schema.schema}}:y?.type==="json_object"&&g(o,"response_format"),{body:n,state:o}}u(P,"request");function g(e,s){e.dropped.includes(s)||e.dropped.push(s)}u(g,"drop");function M(e,s){const p=[...e].sort((a,m)=>a.at-m.at),o=[];let l=0;const c=s.findIndex(a=>a.type==="tool_use");for(const a of p){const m=a.at<=1&&c!==-1?Math.min(a.at,c):a.at;for(;l<Math.min(m,s.length);)o.push(s[l++]);o.push(a.block)}return[...o,...s.slice(l)]}u(M,"interleave");function S(e){return!!(e.reasoning||e.reasoning_content)}u(S,"reasoningPresent");function w(e){return typeof e=="string"?e:(e??[]).filter(s=>s.type==="text").map(s=>s.text).join(`
|
|
4
4
|
|
|
5
|
-
`)}u(
|
|
5
|
+
`)}u(w,"plainText");function z(e,s){if(typeof e=="string")return e===""?[]:[{type:"text",text:e}];const p=[];for(const o of e??[])if(o.type==="text")p.push({type:"text",text:o.text});else if(o.type==="image_url"){const l=h.exec(o.image_url.url);p.push({type:"image",source:l?{type:"base64",media_type:l[1],data:l[2]}:{type:"url",url:o.image_url.url}})}else if(o.type==="file"&&typeof o.file?.file_data=="string"&&h.test(o.file.file_data)){const l=h.exec(o.file.file_data);p.push({type:"document",source:{type:"base64",media_type:l[1],data:l[2]}})}else g(s,`content:${o.type}`);return p}u(z,"userBlocks");function D(e,s){return e==="none"?{type:"none"}:e==="required"?{type:"any"}:typeof e=="object"&&e!==null?{type:"tool",name:s(e.function.name)}:{type:"auto"}}u(D,"toolChoice");export{P as request};
|
|
6
6
|
//# sourceMappingURL=request.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var d=Object.defineProperty;var c=(s,n)=>d(s,"name",{value:n,configurable:!0});import{resolve as h}from"../../../../../../../lib/settings.mjs";import{TranslatorError as y}from"../../../../../../../lib/errors.mjs";import{messageId as _,anthropicToolId as k,restoreName as T}from"../../../../../../../lib/ids.mjs";import{sign as x,reasoningOf as A,reasoningKey as C,detailsText as N,splitThink as O}from"../../../../../../../lib/thinking.mjs";import{isComplete as S,parseArguments as U}from"../../../../../../../lib/arguments.mjs";import{toAnthropicStop as v,toAnthropicUsage as w}from"../../../../../../../lib/finish.mjs";function z(s,n,m){const p=h(m),g=s?.choices?.[0];if(!g)throw new y("Upstream response contained no choices",{code:"invalid_upstream_response",status:502});const e=g.message??{},r=[];let a=A(e),t=typeof e.content=="string"?e.content:"";const o={};if(p.reasoningRoundTrip){const i=C(e);i&&(o[i]=e[i]),Array.isArray(e.reasoning_details)&&e.reasoning_details.length>0&&(o.reasoning_details=e.reasoning_details)}a===""&&p.thinkTags&&({reasoning:a,text:t}=O(t,p.thinkOpen)),a===""&&o.reasoning_details&&(a=N(o.reasoning_details)),(a!==""||o.reasoning_details)&&(r.push({type:"thinking",thinking:a,signature:x(Object.keys(o).length>0?o:void 0,n?.producer)}),t=t.trimStart()),t===""&&typeof e.refusal=="string"&&(t=e.refusal),t!==""&&r.push({type:"text",text:t});const f=n?.toolNames??{},l=e.tool_calls??[];for(const i of l)r.push({type:"tool_use",id:k(i.id),name:T(f,i.function.name),input:U(i.function.arguments)});r.length===0&&r.push({type:"text",text:""});const u=v(g.finish_reason,{hasToolCalls:l.length>0,matchedStop:g.stop_reason,argumentsComplete:l.length===0||S(l.at(-1).function.arguments)});return n&&s.usage&&(n.upstreamUsage=s.usage),{id:_(),type:"message",role:"assistant",model:n?.model??s.model,content:r,...u,usage:w(s.usage)}}c(z,"response");export{z as response};
|
|
2
2
|
//# sourceMappingURL=response.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var M=Object.defineProperty;var s=(h,r)=>M(h,"name",{value:r,configurable:!0});import{resolve as G}from"../../../../../../../lib/settings.mjs";import{TranslatorError as H,openaiToAnthropicType as L,rewriteContextOverflow as P}from"../../../../../../../lib/errors.mjs";import{isComplete as Q}from"../../../../../../../lib/arguments.mjs";import{messageId as V,anthropicToolId as W,restoreName as X}from"../../../../../../../lib/ids.mjs";import{sign as Y,reasoningOf as Z,reasoningKey as $,detailsText as ee,mergeDetail as te,createThinkSplitter as ne}from"../../../../../../../lib/thinking.mjs";import{toAnthropicStop as ie,toAnthropicUsage as z}from"../../../../../../../lib/finish.mjs";import{messages as oe}from"../../../../../../../lib/sse.mjs";async function*pe(h,r,D){const u=G(D),E=r?.toolNames??{},a=u.thinkTags?ne(u.thinkOpen):null,c=new Map;let _=!1,x=0,n=null,w=null,b=null,S=null,N=!1,O=0,j=!1,v=!1,A=!1;const T=[],l=s((e,t)=>({event:e,data:{type:e,...t}}),"event"),m=s((e,t)=>l("error",{error:{type:e,message:t}}),"fail"),q=s(e=>{const t={};return u.reasoningRoundTrip&&(e.key!==null&&(t[e.key]=e.text),e.details.length>0&&(t.reasoning_details=e.details)),Object.keys(t).length>0?t:void 0},"carried");function*p(){if(n!==null){n.kind==="thinking"?yield l("content_block_delta",{index:n.index,delta:{type:"signature_delta",signature:Y(q(n.carry),r?.producer)}}):n.kind==="tool"&&(n.entry.state="closed",n.entry.sent||(yield l("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:"{}"}}))),yield l("content_block_stop",{index:n.index}),n=null;for(const e of c.values()){if(e.state!=="pending")continue;e.state="closed";const t=x++,i=e.args.join("");yield l("content_block_start",{index:t,content_block:e.block}),yield l("content_block_delta",{index:t,delta:{type:"input_json_delta",partial_json:i===""?"{}":i}}),yield l("content_block_stop",{index:t})}}}s(p,"close");function*B(){for(const e of C()){if(e.state!=="buffered")continue;yield*y("tool",e.block,e);const t=e.args.join("");t!==""&&(e.sent=!0,yield l("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:t}})),yield*p()}}s(B,"flushBuffered");function C(){return[...c.entries()].sort((e,t)=>e[0]-t[0]).map(e=>e[1])}s(C,"ordered");function*y(e,t,i){yield*p(),n={kind:e,index:x++,entry:i,carry:{key:null,text:"",details:[]}},yield l("content_block_start",{index:n.index,content_block:t})}s(y,"begin");function*d(e,t,i=null){if(e==="text"&&A&&(t=t.trimStart()),t==="")return;if(A=e==="thinking",n?.kind==="tool"){T.push({kind:e,text:t,key:i});return}n?.kind!==e&&(yield*y(e,e==="text"?{type:"text",text:""}:{type:"thinking",thinking:"",signature:""})),i!==null&&(n.carry.key??=i,n.carry.text+=t);const o=e==="text"?{type:"text_delta",text:t}:{type:"thinking_delta",thinking:t};yield l("content_block_delta",{index:n.index,delta:o})}s(d,"piece");function*U(e){if(n?.kind==="tool"){T.push({kind:"details",items:e});return}n?.kind!=="thinking"&&(yield*y("thinking",{type:"thinking",thinking:"",signature:""}));for(const t of e)te(n.carry.details,t);n.carry.key===null&&(yield*d("thinking",ee(e)))}s(U,"details");function*I(e){yield*d("thinking",e.reasoning),yield*d("text",e.text)}s(I,"pieces");function*F(e){let t=e.index;t===void 0&&(t=e.id===void 0?O:c.size),O=t;let i=c.get(t);if(i===void 0){const k=e.function?.name??"",g={type:"tool_use",id:W(e.id),name:X(E,k),input:{}};a?.pending()&&(v=!0);const f=u.toolStreaming==="buffer"||v;i={state:f?"buffered":"pending",sent:!1,args:[],block:g},c.set(t,i),!f&&n?.kind!=="tool"&&(i.state="open",yield*y("tool",g,i))}let o=e.function?.arguments;o!==null&&typeof o=="object"&&(o=JSON.stringify(o)),!(o==null||o==="")&&(i.args.push(o),i.state==="open"&&(i.sent=!0,yield l("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:o}})))}s(F,"toolCall");try{for await(const e of oe(h)){if(e.data==="[DONE]"){N=!0;break}const t=e.data;if(t.error){const f=t.error.message??"Upstream error";yield m(L(t.error),P(f)??f);return}_||(_=!0,yield l("message_start",{message:{id:V(),type:"message",role:"assistant",model:r?.model??t.model,content:[],stop_reason:null,stop_sequence:null,usage:z(t.usage)}})),t.usage&&(w=t.usage,r&&(r.upstreamUsage=t.usage));const i=t.choices?.[0];if(!i)continue;const o=i.delta??{},k=Z(o);let g="";k!==""&&!j&&(j=!0,a&&(g=a.release().text)),yield*d("thinking",k,$(o)),yield*d("text",g),u.reasoningRoundTrip&&Array.isArray(o.reasoning_details)&&o.reasoning_details.length>0&&(yield*U(o.reasoning_details)),typeof o.content=="string"&&o.content!==""&&(a&&!j?yield*I(a.push(o.content)):yield*d("text",o.content));for(const f of o.tool_calls??[])yield*F(f);i.finish_reason&&(b=i.finish_reason),typeof i.stop_reason=="string"&&(S=i.stop_reason)}}catch(e){yield e instanceof H?m(e.type,e.message):m("api_error",e.message);return}if(!_||b===null&&!N){yield m("api_error",_?"Upstream stream ended before completion":"Upstream stream ended without data");return}a&&(yield*I(a.end())),yield*B();const R=C().at(-1),J=R===void 0||Q(R.args.join(""));yield*p();for(const e of T)yield*e.kind==="details"?U(e.items):d(e.kind,e.text,e.key);yield*p(),x===0&&(yield*y("text",{type:"text",text:""}),yield*p());const K=ie(b,{hasToolCalls:c.size>0,matchedStop:S,argumentsComplete:J});yield l("message_delta",{delta:K,usage:z(w)}),yield l("message_stop",{})}s(pe,"stream");export{pe as stream};
|
|
2
2
|
//# sourceMappingURL=stream.mjs.map
|
package/dist/api/detect.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var g=Object.defineProperty;var n=(t,e)=>g(t,"name",{value:e,configurable:!0});import{context as v,self as m}from"../lib/runtime.mjs";import{capture as w}from"../lib/capture.mjs";import{TranslatorError as d}from"../lib/errors.mjs";import{warn as p}from"../lib/warn.mjs";import*as h from"../lib/http.mjs";async function U(t={}){const e=w(m,v),{status:o}=m,f=t.network!==!1;for(const r of e.drivers){const s=r.info.fields;if(s!==void 0&&!(Array.isArray(s)&&s.every(i=>typeof i=="string")))throw new d(`Driver ${r.name}: info().fields must be an array of field names`,{code:"invalid_driver",status:500})}const a={transport:e.state.transport,signal:t.signal,timeout:e.config.detectTimeout};return await Promise.all(e.state.endpoints.map(r=>y(e,r,f,a))),o()}n(U,"detect");async function y(t,e,o,f){let a,r=null;if(e.backend!=="auto"){if(a=t.drivers.find(s=>s.name===e.backend),!a)throw new d(`Unknown backend "${e.backend}" for upstream ${e.name}`,{code:"unknown_backend",status:500});r={version:e.version}}else if(o){const s={endpoint:e,...f,probes:new Map};let i=!1;for(const[c,l]of t.drivers.entries()){if(!i&&l.info.probes?.length>0){i=!0;for(const u of t.drivers.slice(c))for(const b of u.info.probes??[])h.probe(s,b)}if(r=await k(l,s),r){a=l;break}}}if(a||(a=t.drivers.find(s=>s.name==="compatible"),r={reachable:null,models:[]}),e.driver=a.name,e.protocol=a.info.protocol,e.facts=r,e.driverSettings=await a.settings?.(r)??{},e.probed={},o&&e.backend==="auto"&&a.probe){const s=n(c=>e.settings[c]!==void 0||t.config.settings[c]!==void 0,"given"),i={inlineSystem:t.settings(e).inlineSystem==="auto",images:!s("images")};(i.inlineSystem||i.images)&&(e.probed=await a.probe({endpoint:e,facts:r,want:i,...f}))}r.reachable===!1&&p(t.config,t.state,`Upstream ${e.name} (${e.url}) did not answer detection; using the generic OpenAI-compatible driver`)}n(y,"bind");async function k(t,e){try{return await t.detect?.(e)??null}catch{return null}}n(k,"detectWith");export{U as detect};
|
|
2
2
|
//# sourceMappingURL=detect.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var p=Object.defineProperty;var c=(o,t)=>p(o,"name",{value:t,configurable:!0});import*as s from"../../../../lib/http.mjs";const u=["model","messages","system","tools","tool_choice","thinking","mcp_servers","context_management"];async function l({endpoint:o,request:t,transport:a,signal:r,timeout:m}){const i={};for(const e of u)t[e]!==void 0&&(i[e]=t[e]);const n=await s.json(a,s.join(o.url,"/v1/messages/count_tokens"),{method:"POST",headers:{"content-type":"application/json","anthropic-version":"2023-06-01",...s.headers(o,"x-api-key")},body:i,signal:r,timeout:m});return n.ok&&typeof n.body?.input_tokens=="number"?n.body.input_tokens:null}c(l,"count");export{l as count};
|
|
2
2
|
//# sourceMappingURL=count.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var r=Object.defineProperty;var
|
|
1
|
+
var r=Object.defineProperty;var t=(e,o)=>r(e,"name",{value:o,configurable:!0});import*as a from"../../../lib/http.mjs";import{modelsOf as l}from"../../../lib/models.mjs";async function n(e){const o=l(await a.probe(e,"/v1/models"));return{reachable:o!==null,models:(o??[]).map(m=>m.id)}}t(n,"detect");export{n as detect};
|
|
2
2
|
//# sourceMappingURL=detect.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var r=Object.defineProperty;var o=(e,t)=>r(e,"name",{value:t,configurable:!0});import{VLLM_FIELDS as a}from"../../../lib/passthrough.mjs";function m(){return{name:"compatible",protocol:"openai.chat",order:100,auth:"bearer",fields:a,paths:{chat:"/v1/chat/completions",models:"/v1/models"}}}o(m,"info");export{m as info};
|
|
2
2
|
//# sourceMappingURL=info.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var s=Object.defineProperty;var o=(r,n)=>s(r,"name",{value:n,configurable:!0});import*as a from"../../../lib/upstream-error.mjs";const t="DeepSeek needs the reasoning of earlier turns sent back when tools are used in thinking mode. The conversation no longer carries it (thinking blocks were not replayed or were cleared), so start a new conversation.";function m(r,n){const e=a.parse(n,r);return{error:{message:r===400&&/reasoning_content/i.test(e.message)?`${e.message} ${t}`:e.message,type:e.type,param:e.param??null,code:e.code??r}}}o(m,"error");export{m as error};
|
|
2
|
+
//# sourceMappingURL=error.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var a=Object.defineProperty;var e=(o,t)=>a(o,"name",{value:t,configurable:!0});function n(){return{name:"deepseek",protocol:"openai.chat",order:35,auth:"bearer",paths:{chat:"/v1/chat/completions",models:"/v1/models"}}}e(n,"info");export{n as info};
|
|
2
|
+
//# sourceMappingURL=info.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var c=Object.defineProperty;var n=(e,r)=>c(e,"name",{value:r,configurable:!0});import{noEmptyTools as l}from"../../../lib/normalize.mjs";const p=["logprobs","top_logprobs"];function u(e){return e.thinking?.type==="disabled"?!1:e.thinking?.type==="enabled"||/reasoner/i.test(String(e.model))}n(u,"thinking");function a(e,r){let o=l(e);if(!u(o))return o;const i=r?.state?.dropped,s=n(t=>{o={...o},delete o[t],Array.isArray(i)&&!i.includes(t)&&i.push(t)},"remove");o.tool_choice!==void 0&&o.tool_choice!=="auto"&&o.tool_choice!=="none"&&s("tool_choice");for(const t of p)o[t]!==void 0&&s(t);return o}n(a,"request");export{a as request};
|
|
2
|
+
//# sourceMappingURL=request.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var t=Object.defineProperty;var e=(n,s)=>t(n,"name",{value:s,configurable:!0});function a(){return{reasoningField:"reasoning_content",reasoningRoundTrip:!0,maxTokensField:"max_tokens",thinking:"none",thinkingBudget:!1,effort:!1,topK:!1,prefill:"none",responseFormat:!1,images:!1,streamUsage:!0,continuousUsage:!1}}e(a,"settings");export{a as settings};
|
|
2
|
+
//# sourceMappingURL=settings.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var f=Object.defineProperty;var s=(t,o)=>f(t,"name",{value:o,configurable:!0});import{callId as d}from"../../../lib/ids.mjs";const c="<think>",a="</think>";function l(t,o){const e=o.thinkLegacy??={reasoning:!1,mode:"start",held:""};if(typeof t.reasoning_content=="string"&&t.reasoning_content!==""&&(e.reasoning=!0),!e.reasoning||e.mode==="plain"||typeof t.content!="string"||t.content==="")return t;let n=e.held+t.content;if(e.held="",e.mode==="start"){const i=n.trimStart();if(i.startsWith(c))e.mode="drop",n=i.slice(c.length);else return i===""||c.startsWith(i)?(e.held=n,{...t,content:""}):(e.mode="plain",{...t,content:n})}const r=n.indexOf(a);return r===-1?(e.held=n.slice(-(a.length-1)),{...t,content:""}):(e.mode="plain",{...t,content:n.slice(r+a.length).trimStart()})}s(l,"legacy");function m(t,o){if(!Array.isArray(t.tool_calls))return t;const e=o.toolIds??={};return{...t,tool_calls:t.tool_calls.map(n=>{const r=n?.index??0;return n?.id||!n?.function?.name||e[r]!==void 0?n:(e[r]=d(),{...n,id:e[r]})})}}s(m,"ids");function h(t,o){const e=t?.choices?.[0]?.delta;if(!o?.state||e===null||typeof e!="object")return t;o.state.thinkLegacy??={reasoning:!1,mode:"start",held:""},o.state.thinkLegacy.base={id:t.id,object:t.object,created:t.created,model:t.model};const n=m(l(e,o.state),o.state);if(n===e)return t;const[r,...i]=t.choices;return{...t,choices:[{...r,delta:n},...i]}}s(h,"chunk");export{h as chunk};
|
|
2
|
+
//# sourceMappingURL=chunk.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=Object.defineProperty;var t=(i,e)=>r(i,"name",{value:e,configurable:!0});import*as p from"../../../lib/http.mjs";import{modelsOf as d}from"../../../lib/models.mjs";import{isLlamacppProps as m,objectOf as a}from"../../../lib/backends.mjs";async function c(i){const[e,l]=await Promise.all([p.probe(i,"/props"),p.probe(i,"/v1/models")]),o=a(e),n=d(l)??[];return!m(o)&&!n.some(s=>s.owned_by==="llamacpp")?null:{models:n.map(s=>s.id),build:typeof o.build_info=="string"?o.build_info:void 0,vision:typeof o.modalities?.vision=="boolean"?o.modalities.vision:void 0}}t(c,"detect");export{c as detect};
|
|
2
|
+
//# sourceMappingURL=detect.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var h=Object.defineProperty;var n=(t,e)=>h(t,"name",{value:e,configurable:!0});function o(t){const e=t?.state?.thinkLegacy;if(!e||e.mode!=="start"||e.held==="")return[];const s=e.held;return e.held="",[{...e.base,choices:[{index:0,delta:{content:s},finish_reason:null}]}]}n(o,"flush");export{o as flush};
|
|
2
|
+
//# sourceMappingURL=flush.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var a=Object.defineProperty;var p=(e,t)=>a(e,"name",{value:t,configurable:!0});function r(){return{name:"llamacpp",protocol:"openai.chat",order:40,auth:"bearer",probes:["/props"],fields:["top_k","min_p","chat_template_kwargs","repetition_penalty","repeat_penalty","grammar"],paths:{chat:"/v1/chat/completions",models:"/v1/models",props:"/props"}}}p(r,"info");export{r as info};
|
|
2
|
+
//# sourceMappingURL=info.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var p=Object.defineProperty;var n=(l,t)=>p(l,"name",{value:t,configurable:!0});import{noEmptyTools as r}from"../../../lib/normalize.mjs";function f(l,t){let e=r(l);if(e.tools!==void 0&&e.parallel_tool_calls===void 0&&t?.settings?.parallelToolCalls!==!1&&(e={...e,parallel_tool_calls:!0}),e.repetition_penalty!==void 0&&e.repeat_penalty===void 0){const{repetition_penalty:o,...a}=e;e={...a,repeat_penalty:o}}return e}n(f,"request");export{f as request};
|
|
2
|
+
//# sourceMappingURL=request.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var i=Object.defineProperty;var s=(e,n)=>i(e,"name",{value:n,configurable:!0});import{callId as a}from"../../../lib/ids.mjs";import{splitThink as c}from"../../../lib/thinking.mjs";function m(e){return Array.isArray(e?.choices)?{...e,choices:e.choices.map(n=>{const t=n?.message;if(!t||typeof t!="object")return n;let r=t;if(typeof t.reasoning_content=="string"&&t.reasoning_content!==""&&typeof t.content=="string"){const o=c(t.content).text;o!==t.content&&(r={...r,content:o})}return Array.isArray(t.tool_calls)&&(r={...r,tool_calls:t.tool_calls.map(o=>o?.id?o:{...o,id:a()})}),r===t?n:{...n,message:r}})}:e}s(m,"response");export{m as response};
|
|
2
|
+
//# sourceMappingURL=response.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var s=Object.defineProperty;var n=(e,t)=>s(e,"name",{value:t,configurable:!0});function i(e){return{reasoningField:"reasoning_content",maxTokensField:"max_tokens",thinking:"chat_template_kwargs",thinkingBudget:!1,effort:!1,topK:!0,prefill:"none",responseFormat:!0,streamUsage:!0,continuousUsage:!1,images:e?.vision}}n(i,"settings");export{i as settings};
|
|
2
|
+
//# sourceMappingURL=settings.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var t=Object.defineProperty;var e=(o,r)=>t(o,"name",{value:r,configurable:!0});import*as i from"../../../lib/http.mjs";import{modelsOf as m}from"../../../lib/models.mjs";async function a(o){const[r,s]=await Promise.all([i.probe(o,"/api/version"),i.probe(o,"/v1/models")]);return typeof r.body?.version!="string"?null:{version:r.body.version,models:(m(s)??[]).map(n=>n.id)}}e(a,"detect");export{a as detect};
|
|
2
|
+
//# sourceMappingURL=detect.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=Object.defineProperty;var o=(e,a)=>r(e,"name",{value:a,configurable:!0});function t(){return{name:"ollama",protocol:"openai.chat",order:60,auth:"bearer",probes:["/api/version"],paths:{chat:"/v1/chat/completions",models:"/v1/models",version:"/api/version"}}}o(t,"info");export{t as info};
|
|
2
|
+
//# sourceMappingURL=info.mjs.map
|
|
@@ -0,0 +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};
|
|
2
|
+
//# sourceMappingURL=request.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var o=Object.defineProperty;var e=(n,t)=>o(n,"name",{value:t,configurable:!0});const a=4096;function l(){return{maxTokensField:"max_tokens",thinking:"none",thinkingBudget:!1,effort:!1,topK:!1,prefill:"none",parallelToolCalls:!1,responseFormat:!0,streamUsage:!0,continuousUsage:!1,contextWindow:4096}}e(l,"settings");export{l as settings};
|
|
2
|
+
//# sourceMappingURL=settings.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var m=Object.defineProperty;var t=(r,s)=>m(r,"name",{value:s,configurable:!0});import*as l from"../../../lib/http.mjs";import{modelsOf as d}from"../../../lib/models.mjs";import{isSglangInfo as f,objectOf as p}from"../../../lib/backends.mjs";async function b(r){const[s,a]=await Promise.all([l.probe(r,"/get_server_info"),l.probe(r,"/v1/models")]),e=p(s),i=d(a)??[];if(!f(e)&&!i.some(o=>o.owned_by==="sglang"))return null;const n=t(o=>typeof o=="string"&&o!==""?o:void 0,"text");return{version:n(e.version),models:i.map(o=>o.id),modelPath:n(e.model_path),reasoningParser:n(e.reasoning_parser),toolCallParser:n(e.tool_call_parser)}}t(b,"detect");export{b as detect};
|
|
2
|
+
//# sourceMappingURL=detect.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var t=Object.defineProperty;var e=(o,r)=>t(o,"name",{value:r,configurable:!0});function a(){return{name:"sglang",protocol:"openai.chat",order:50,auth:"bearer",probes:["/get_server_info"],fields:["top_k","min_p","repetition_penalty","chat_template_kwargs","skip_special_tokens","ignore_eos","regex","ebnf","separate_reasoning"],paths:{chat:"/v1/chat/completions",models:"/v1/models",serverInfo:"/get_server_info"}}}e(a,"info");export{a as info};
|
|
2
|
+
//# sourceMappingURL=info.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var o=Object.defineProperty;var t=(e,n)=>o(e,"name",{value:n,configurable:!0});import{noEmptyTools as s}from"../../../lib/normalize.mjs";function u(e,n){const r=s(e);return n?.endpoint?.facts?.reasoningParser&&r.separate_reasoning===void 0?{...r,separate_reasoning:!0}:r}t(u,"request");export{u as request};
|
|
2
|
+
//# sourceMappingURL=request.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var o=Object.defineProperty;var e=(t,n)=>o(t,"name",{value:n,configurable:!0});function a(){return{reasoningField:"reasoning_content",maxTokensField:"max_completion_tokens",thinking:"chat_template_kwargs",thinkingBudget:!1,effort:!1,topK:!0,prefill:"none",responseFormat:!0,streamUsage:!0,continuousUsage:!1}}e(a,"settings");export{a as settings};
|
|
2
|
+
//# sourceMappingURL=settings.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var y=Object.defineProperty;var
|
|
1
|
+
var y=Object.defineProperty;var d=(s,t)=>y(s,"name",{value:t,configurable:!0});import*as r from"../../../lib/http.mjs";import{isLlamacppProps as g,isSglangInfo as v,objectOf as p}from"../../../lib/backends.mjs";const _=new Set(["sglang","llamacpp"]);async function P({endpoint:s,transport:t,signal:c,timeout:f,probes:b}){const e={endpoint:s,transport:t,signal:c,timeout:f,probes:b},[l,a]=await Promise.all([r.probe(e,"/version"),r.probe(e,"/v1/models")]),i=typeof l.body?.version=="string"?l.body.version:void 0,n=Array.isArray(a.body?.data)?a.body.data:[];if(n.some(o=>_.has(o?.owned_by)))return null;const m=n.some(o=>o?.owned_by==="vllm");if(!i&&!m)return null;if(!m){const[o,u]=await Promise.all([r.probe(e,"/get_server_info"),r.probe(e,"/props")]);if(v(p(o))||g(p(u)))return null}return{version:i,models:n.map(o=>o.id),maxModelLen:n.find(o=>typeof o.max_model_len=="number")?.max_model_len}}d(P,"detect");export{P as detect};
|
|
2
2
|
//# sourceMappingURL=detect.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var r=Object.defineProperty;var o=(e,t)=>r(e,"name",{value:t,configurable:!0});import{VLLM_FIELDS as n}from"../../../lib/passthrough.mjs";function l(){return{name:"vllm",protocol:"openai.chat",order:10,auth:"bearer",fields:n,paths:{chat:"/v1/chat/completions",models:"/v1/models",tokenize:"/tokenize",version:"/version"}}}o(l,"info");export{l as info};
|
|
2
2
|
//# sourceMappingURL=info.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=Object.defineProperty;var m=(s,t)=>r(s,"name",{value:t,configurable:!0});import{context as e}from"../../lib/runtime.mjs";import{describe as a}from"../../lib/pool.mjs";function u(){const{config:s,state:t}=e;return{...s.pool,endpoints:t.endpoints.map(o=>({name:o.name,url:o.url,models:o.models??o.facts?.models??[],...a(o)}))}}m(u,"status");export{u as status};
|
|
2
|
+
//# sourceMappingURL=status.mjs.map
|
package/dist/api/error/error.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var y=Object.defineProperty;var p=(e,i)=>y(e,"name",{value:i,configurable:!0});import{context as v,self as g}from"../../lib/runtime.mjs";import{capture as b}from"../../lib/capture.mjs";import*as u from"../../lib/upstream-error.mjs";import*as l from"../../lib/tools.mjs";const h=["retry-after","x-should-retry"];async function x(e,i,f={},r){const t=b(g,v,r?.front),o=r?t.endpoint(r.upstream):void 0,a=o?t.driver(o):void 0;let s=i;a?.error&&(s=await a.error(e,s,{endpoint:o,state:r}));const n=u.parse(s,e);r?.unserved===!0&&e===404&&(n.message+=` The model "${r.model}" is not served by any configured upstream; map it to a served model with "aliases".`),e===400&&l.refused(n.message)&&(n.message+=` ${l.hint(o?t.settings(o).toolFallback:!1,n.message)}`);const m=u.render(t.front,e,n,{native:r?.native===!0}),c={"content-type":"application/json"};for(const d of h)f[d]!==void 0&&(c[d]=f[d]);return{status:m.status,headers:c,body:m.body}}p(x,"error");export{x as default};
|
|
2
2
|
//# sourceMappingURL=error.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var d=Object.defineProperty;var r=(o,t)=>d(o,"name",{value:t,configurable:!0});import{context as m,self as s}from"../../lib/runtime.mjs";import{TranslatorError as c}from"../../lib/errors.mjs";async function p(o,t={}){const e=t.front??m.config.front,{data:f}=await s.model.list(t),n=f.find(a=>a.id===o);if(n)return n;throw new c(e==="openai"?`The model '${o}' does not exist`:`model: ${o}`,{code:"model_not_found",status:404,param:"model"})}r(p,"get");export{p as get};
|
|
2
|
+
//# sourceMappingURL=get.mjs.map
|
package/dist/api/request.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var
|
|
2
|
-
${
|
|
1
|
+
var U=Object.defineProperty;var m=(e,a)=>U(e,"name",{value:a,configurable:!0});import{context as _,self as j}from"../lib/runtime.mjs";import{capture as q}from"../lib/capture.mjs";import{resolve as F}from"../lib/route.mjs";import*as w from"../lib/http.mjs";import{filterChatBody as S}from"../lib/passthrough.mjs";import*as k from"../lib/images.mjs";import*as $ from"../lib/tools.mjs";import{LIMITS as T}from"../lib/toolsearch.mjs";import{warn as u}from"../lib/warn.mjs";function L(e){return`${e.driver}:${e.protocol}:${URL.canParse(e.url)?new URL(e.url).host:e.url}`}m(L,"producerOf");async function H(e,a={}){const o=q(j,_,a.front),n=F(o,e,{endpoint:a.endpoint,advance:a.hold===!0&&a.endpoint===void 0});if(a.hold!==!0)return y(o,e,a,n);n.endpoint.health.inflight++;try{return await y(o,e,a,n)}catch(t){throw n.endpoint.health.inflight--,t}}m(H,"request");async function y(e,a,o,n){const{endpoint:t,model:h,unserved:b,convert:i,pool:x}=n,s=e.driver(t),d=`${t.name}
|
|
2
|
+
${h}`,g=o.images===!1||e.state.textOnly.has(d)&&e.settings(t).imageFallback,l=g?{...e.settings(t),images:!1}:e.settings(t),p=a.stream===!0;let r,f;if(i?({body:r,state:f}=await i.request(a,l,{producer:L(t),budget:o.budget}),f.toolSearch?.capped&&u(e.config,e.state,`Tool search: more than ${T.definitions} tool definitions came back in ToolSearch results for ${t.name}; the rest are not offered.`)):r=e.front==="openai"&&(o.filter??l.passthroughFilter===!0)?S(a,s.info.fields):{...a},o.usage===!0&&p&&e.front==="openai"){if(i)f.includeUsage=!0;else if(l.streamUsage){const c=r.stream_options&&typeof r.stream_options=="object"?r.stream_options:{};r.stream_options={...c,include_usage:!0}}}g&&!i&&e.front==="openai"&&(r=k.strip(r)),o.tools===!1&&(r=$.strip(r)),r.model=h,f?.reasoningMissing&&u(e.config,e.state,`Upstream ${t.name}: assistant turns with tool calls have no reasoning to send back (the client dropped its thinking blocks or their signatures), which servers that need it with tools will refuse.`),s.request&&(r=await s.request(r,{endpoint:t,settings:l,state:f,warn:m(c=>u(e.config,e.state,c),"warn")}));const v=t.protocol==="openai.chat"?s.info.paths.chat:s.info.paths.messages;return{upstream:t.name,pool:x.map(c=>c.name),method:"POST",url:w.join(t.url,v),path:v,headers:{"content-type":"application/json",accept:p?"text/event-stream":"application/json",...s.info.headers,...i===null&&t.protocol==="anthropic.messages"?o.clientHeaders:void 0,...w.headers(t,s.info.auth,o.headers)},body:r,state:{v:1,upstream:t.name,front:e.front,native:i===null,model:a.model,stream:p,unserved:b,convert:f},images:{sent:k.carries(r),retry:l.imageFallback&&t.protocol==="openai.chat",key:d},tools:{sent:$.carries(r),retry:l.toolFallback&&t.protocol==="openai.chat"}}}m(y,"prepare");export{H as request};
|
|
3
3
|
//# sourceMappingURL=request.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var i=Object.defineProperty;var t=(o,e)=>i(o,"name",{value:e,configurable:!0});import{context as s,self as d}from"../lib/runtime.mjs";import{capture as f}from"../lib/capture.mjs";import{resolve as v}from"../lib/route.mjs";function g(o,e={}){const n=f(d,s,e.front),{endpoint:r,model:p,requested:m,unserved:c,convert:l,pool:u}=v(n,o);return{upstream:r.name,model:p,requested:m,protocol:r.protocol,backend:r.driver,native:l===null,unserved:c,pool:u.map(a=>a.name)}}t(g,"route");export{g as route};
|
|
2
|
+
//# sourceMappingURL=route.mjs.map
|
package/dist/api/send.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var G=Object.defineProperty;var s=(o,n)=>G(o,"name",{value:n,configurable:!0});import{context as q,self as D}from"../lib/runtime.mjs";import{TranslatorError as H}from"../lib/errors.mjs";import*as I from"../lib/http.mjs";import*as K from"../lib/images.mjs";import*as Q from"../lib/tools.mjs";import{parse as V}from"../lib/upstream-error.mjs";import{meter as W,normalize as X}from"../lib/usage.mjs";import{capture as Y}from"../lib/capture.mjs";import{estimate as Z,bodyLength as tt,output as et,patch as at,stream as nt}from"../lib/estimate.mjs";import{warn as rt}from"../lib/warn.mjs";import*as L from"../lib/pool.mjs";import{createBudget as ot}from"../lib/thinking.mjs";const st={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"};async function yt(o,n={}){const{transport:h,textOnly:p}=q.state,{request:y,response:w,stream:d,error:u}=D,f=u.build,{config:r,state:b}=q,x=Y(D,q,n.front),_=o&&typeof o=="object"&&!Array.isArray(o),S={model:null,requested:_&&typeof o.model=="string"?o.model:null,upstream:null,front:n.front??r.front,stream:_&&o.stream===!0},g=W(S,n.onUsage,l=>rt(r,b,l)),j=s(l=>Object.defineProperty(l,"usage",{value:g.usage,enumerable:!1,configurable:!0}),"attach");try{return j(await ut({transport:h,textOnly:p,request:y,response:w,stream:d,error:u,captured:x},o,n,g))}catch(l){if(!(l instanceof H))throw g.finish(n.signal?.aborted?"aborted":"error"),l;return g.finish("error"),j(f(l.status,l.message,{type:l.type,code:l.code,param:l.param,front:n.front}))}}s(yt,"send");async function ut({transport:o,textOnly:n,request:h,response:p,stream:y,error:w,captured:d},u,f,r){const b={front:f.front,headers:f.headers,clientHeaders:f.clientHeaders,filter:f.filter,usage:!0,budget:ot()},x=r.meta.front==="openai"&&u?.stream_options?.include_usage!==!0,_=s(e=>{r.meta.model=e.body.model??null,r.meta.upstream=e.upstream,e.state.native?r.source(null):r.source(()=>e.state.convert?.upstreamUsage,e.state.front==="anthropic"?"openai":"folded")},"note"),S=s(e=>{if(e.state.native)return{input:!1,output:!1};const t=X(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"),g=s(async e=>{const t=d.endpoint(e.upstream),a=d.driver(t);return(a.count&&!(x&&e.state.stream)?await a.count({endpoint:t,request:e.body,transport:o,signal:f.signal,timeout:d.config.detectTimeout}):null)??Z(e.body)},"input"),j=s(async(e,t)=>{const a=S(e);return!a.input&&!a.output?null:{input_tokens:a.input?await g(e):null,output_tokens:a.output?et(t):null}},"counts"),l=s((e,t,a,c,O)=>(r.finish(O),w(e,t,a,c)),"fail"),R=s(async e=>{let t=e,a=await M(o,t,f.signal,P());if(!a.ok&&a.status===400&&(t.images.sent&&t.images.retry||t.tools.sent&&t.tools.retry)){const c=await C(a),O=V(c,400).message,v={};if(t.images.sent&&t.images.retry&&K.refused(O)?(n.add(t.images.key),v.images=!1):t.tools.sent&&t.tools.retry&&Q.refused(O)&&(v.tools=!1),!("images"in v||"tools"in v))return{upstream:t,result:a,failure:c};t=await h(u,{...b,...v,endpoint:t.upstream}),_(t),a=await M(o,t,f.signal,P())}return{upstream:t,result:a}},"attempt");let i=await h(u,{...b,hold:!0});const A=i.pool;let N=0;const $=A.length>1?d.config.pool:null,P=s(()=>$&&{connectTimeout:$.connectTimeout,headersTimeout:N+1<A.length?$.headersTimeout:0},"limitsNow");let T=d.endpoint(i.upstream);const z=s(()=>T.health.inflight--,"release");let m,E;try{for(;;){const e=T;e.health.requests++,_(i);let t=null;try{({upstream:i,result:m,failure:E}=await R(i)),!m.ok&&m.status>=500&&(t=`HTTP ${m.status}`)}catch(c){if(!(c instanceof H)||c.code!=="upstream_unreachable")throw c;t=c.message,m=void 0,E=c}if(t===null){L.succeeded(e);break}L.failed(d.config,e,t);let a=null;for(;a===null&&!f.signal?.aborted&&N+1<A.length;)try{a=await h(u,{...b,endpoint:A[++N],hold:!0})}catch(c){if(!(c instanceof H)||c.code!=="endpoint_not_in_pool")throw c}if(a===null){if(m===void 0)throw E;break}try{await m?.body?.cancel?.()}catch{}z(),T=d.endpoint(a.upstream),i=a}}catch(e){throw z(),e}const F=T;T=null,r.usage.then(()=>F.health.inflight--);const B=I.plain(m.headers);if(!m.ok)return l(m.status,E??await C(m),B,i.state,"upstream_error");if(i.state.stream){const e=y(m.body,i.state),t=i.state.native?e:nt(r.meta.front,e,{gaps:s(()=>S(i),"gaps"),input:s(()=>g(i),"input"),report:s(a=>r.estimate(a),"report")});return{status:200,headers:st,stream:r.track(t,f.signal,x)}}const J=await C(m);if(J===null||typeof J!="object")return l(502,"The upstream returned a body that is not JSON",B,i.state,"error");const k=await p(J,i.state),U=await j(i,tt(r.meta.front,k));return U&&(k.usage=at(r.meta.front,k.usage,U),r.estimate(U)),r.json(k),r.finish("done"),{status:200,headers:{"content-type":"application/json"},body:k}}s(ut,"round");async function M(o,n,h,p=null){let y,w=!1,d=h;if(p&&p.headersTimeout>0){const u=new AbortController;d=h?AbortSignal.any([h,u.signal]):u.signal,y=setTimeout(()=>{w=!0,u.abort()},p.headersTimeout)}try{return await o(n.url,{method:n.method,headers:n.headers,body:JSON.stringify(n.body),signal:d,redirect:"error",...p&&p.connectTimeout>0?{connectTimeout:p.connectTimeout}:{}})}catch(u){if(h?.aborted)throw u;const f=w?`no response headers within ${p.headersTimeout} ms`:u.message;throw new H(`Could not reach the upstream (${n.upstream}): ${f}`,{code:"upstream_unreachable",status:502,details:{cause:u}})}finally{clearTimeout(y)}}s(M,"call");async function C(o){const n=await o.text();try{return JSON.parse(n)}catch{return n===""?null:n}}s(C,"parse");export{yt as send};
|
|
2
2
|
//# sourceMappingURL=send.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var d=Object.defineProperty;var f=(e,t)=>d(e,"name",{value:t,configurable:!0});import{context as g,self as m}from"../../lib/runtime.mjs";import{TranslatorError as s}from"../../lib/errors.mjs";import{register as h}from"../../lib/routes.mjs";import{resolve as p}from"../../lib/settings.mjs";async function _(e,t={}){const{config:r}=g;if(t.front!==void 0&&t.front!=="anthropic"&&t.front!=="openai")throw new s(`front must be "anthropic" or "openai" (got ${JSON.stringify(t.front)})`,{code:"invalid_config",status:500});for(const n of["authorize","onUsage"])if(t[n]!==void 0&&typeof t[n]!="function")throw new s(`${n} must be a function`,{code:"invalid_config",status:500});const{send:a,route:c,token:u,model:o,error:l,sse:i}=m;h(e,t,{front:t.front??r.front,filter:r.settings.passthroughFilter!==!1,interval:p(r.settings).pingInterval,send:a,route:c,count:u.count,list:o.list,get:o.get,build:l.build,encode:i.encode,write:i.write})}f(_,"plugin");export{_ as plugin};
|
|
2
2
|
//# sourceMappingURL=plugin.mjs.map
|