@cldmv/llm-translator 0.5.0 → 0.7.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 +7 -7
- 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 +4 -4
- 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/detect.mjs +1 -1
- package/dist/api/driver/anthropic/info.mjs +1 -1
- package/dist/api/driver/compatible/info.mjs +1 -1
- package/dist/api/driver/deepseek/info.mjs +1 -1
- package/dist/api/driver/llamacpp/info.mjs +1 -1
- package/dist/api/driver/ollama/info.mjs +1 -1
- package/dist/api/driver/openai/info.mjs +1 -1
- package/dist/api/driver/sglang/info.mjs +1 -1
- package/dist/api/driver/vllm/info.mjs +1 -1
- package/dist/api/embeddings/send.mjs +2 -0
- package/dist/api/request.mjs +2 -2
- package/dist/api/send.mjs +1 -1
- package/dist/api/server/plugin.mjs +1 -1
- package/dist/lib/config.mjs +1 -1
- package/dist/lib/http.mjs +1 -1
- package/dist/lib/ids.mjs +1 -1
- package/dist/lib/record.mjs +3 -0
- package/dist/lib/routes.mjs +1 -1
- package/dist/lib/sampling.mjs +2 -0
- package/dist/lib/settings.mjs +1 -1
- package/dist/lib/strict.mjs +2 -0
- package/package.json +4 -3
- package/types/src/api/convert/anthropic/messages/to/openai/chat/request.d.mts +6 -1
- package/types/src/api/convert/openai/chat/to/anthropic/messages/request.d.mts +8 -1
- package/types/src/api/driver/anthropic/detect.d.mts +10 -23
- package/types/src/api/driver/anthropic/info.d.mts +4 -1
- package/types/src/api/driver/compatible/info.d.mts +2 -1
- package/types/src/api/driver/deepseek/info.d.mts +2 -1
- package/types/src/api/driver/llamacpp/info.d.mts +2 -1
- package/types/src/api/driver/ollama/info.d.mts +2 -1
- package/types/src/api/driver/openai/info.d.mts +2 -1
- package/types/src/api/driver/sglang/info.d.mts +2 -1
- package/types/src/api/driver/vllm/info.d.mts +2 -1
- package/types/src/api/embeddings/send.d.mts +23 -0
- package/types/src/api/server/plugin.d.mts +1 -1
- package/types/src/index.d.mts +5 -0
- package/types/src/lib/http.d.mts +8 -2
- package/types/src/lib/ids.d.mts +40 -2
- package/types/src/lib/record.d.mts +41 -0
- package/types/src/lib/sampling.d.mts +13 -0
- package/types/src/lib/settings.d.mts +5 -0
- package/types/src/lib/strict.d.mts +26 -0
package/README.md
CHANGED
|
@@ -17,18 +17,18 @@ Use it as a library when you want the conversion without any I/O (`request`, `re
|
|
|
17
17
|
|
|
18
18
|
## ✨ What's New
|
|
19
19
|
|
|
20
|
-
### Latest: v0.
|
|
20
|
+
### Latest: v0.7.0 (November 2026)
|
|
21
21
|
|
|
22
|
-
- **
|
|
23
|
-
- **
|
|
24
|
-
- [View full v0.
|
|
22
|
+
- **Anthropic-protocol servers detected by probe** — an Anthropic-compatible server on any host is now recognised from its `GET /v1/models` answer, matched strictly on the Anthropic model-list shape, so it no longer needs `backend: "anthropic"`. api.anthropic.com is still matched by host name.
|
|
23
|
+
- **Tool-call ids, embeddings usage and capture** — a driver can declare a tool-call id format with `info().toolCallIds`, and ids are rewritten into it and mapped back statelessly. Embeddings usage now reaches `onUsage`. `capture` records every failed-over attempt, linked by `exchange`, and captures embeddings too.
|
|
24
|
+
- [View full v0.7.0 Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.7.0.md)
|
|
25
25
|
|
|
26
26
|
### Recent Releases
|
|
27
27
|
|
|
28
|
+
- **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))
|
|
29
|
+
- **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))
|
|
28
30
|
- **v0.4.0** (November 2026) — per-model effort levels and aliases that pin effort and thinking, non-standard finish reasons, a configurable tool-error marker, per-upstream `pingInterval` and a Node.js 22.15 floor ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.4.0.md))
|
|
29
31
|
- **v0.3.1** (November 2026) — patched the dev-only `minimatch` and `katex` lint dependencies and corrected the v0.3.0 issue links; no runtime changes ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.3.1.md))
|
|
30
|
-
- **v0.3.0** (November 2026) — usage reporting from `send()`, the `route()` leaf and the server `authorize` hook; drivers for llama.cpp, Ollama, SGLang and the DeepSeek API, endpoint pools, `reasoningRoundTrip` and `toolSearch`; requires Node.js 24 ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.3.0.md))
|
|
31
|
-
- **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))
|
|
32
32
|
|
|
33
33
|
📚 **For complete version history and detailed release notes, see the [docs/changelog/](https://github.com/CLDMV/llm-translator/tree/master/docs/changelog/) folder.**
|
|
34
34
|
|
|
@@ -127,7 +127,7 @@ Then point Claude Code at it with `ANTHROPIC_BASE_URL=http://127.0.0.1:8787`. Th
|
|
|
127
127
|
|
|
128
128
|
## 📚 Configuration
|
|
129
129
|
|
|
130
|
-
`createTranslator(config)` takes `front`, `upstreams` (URLs or objects with `url`, `apiKey`, `backend`, `models`, `default` and `settings`), `aliases` (a model name, or `{ model, effort, thinking }` to pin settings), `efforts` (the reasoning levels each model supports, see [docs/settings.md](https://github.com/CLDMV/llm-translator/blob/master/docs/settings.md#effort-levels-and-pinned-aliases-not-converter-settings)), global `settings`, `pool` (failover and load balancing between upstreams that serve the same model, see [docs/server.md](https://github.com/CLDMV/llm-translator/blob/master/docs/server.md#endpoint-pools-failover-and-load-balancing)), `detect`, `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)) 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.
|
|
130
|
+
`createTranslator(config)` takes `front`, `upstreams` (URLs or objects with `url`, `apiKey`, `backend`, `models`, `default` and `settings`), `aliases` (a model name, or `{ model, effort, thinking }` to pin settings), `efforts` (the reasoning levels each model supports, see [docs/settings.md](https://github.com/CLDMV/llm-translator/blob/master/docs/settings.md#effort-levels-and-pinned-aliases-not-converter-settings)), global `settings`, `pool` (failover and load balancing between upstreams that serve the same model, see [docs/server.md](https://github.com/CLDMV/llm-translator/blob/master/docs/server.md#endpoint-pools-failover-and-load-balancing)), `detect`, `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
131
|
|
|
132
132
|
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
133
|
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
var N=Object.defineProperty;var
|
|
1
|
+
var N=Object.defineProperty;var _=(e,t)=>N(e,"name",{value:t,configurable:!0});import{resolve as O}from"../../../../../../../lib/settings.mjs";import{TranslatorError as j}from"../../../../../../../lib/errors.mjs";import{createIdFitter as q,createNameFitter as B}from"../../../../../../../lib/ids.mjs";import{sanitize as C}from"../../../../../../../lib/schema.mjs";import{NOTE as A}from"../../../../../../../lib/images.mjs";import{expand as L}from"../../../../../../../lib/documents.mjs";import{createBudget as z,unsign as D}from"../../../../../../../lib/thinking.mjs";import{plan as P,scan as W}from"../../../../../../../lib/toolsearch.mjs";const H=new Set(["model","messages","system","max_tokens","stream","temperature","top_p","top_k","stop_sequences","tools","tool_choice","thinking","output_config","output_format","context_management"]),K=new Set(["metadata","cache_control","service_tier"]),R=new Set(["mcp_servers","container","inference_geo"]),U="x-anthropic-billing-header:";async function pe(e,t,o){const n=O(t);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new j("messages: Field required",{code:"invalid_request",param:"messages"});const s={front:"anthropic",model:e.model,stream:e.stream===!0,thinking:e.thinking?.type??null,reasoningField:n.reasoningField,toolNames:{},toolIds:{},dropped:[]};o?.producer!==void 0&&(s.producer=o.producer);const r=B({max:64,map:s.toolNames}),d=o?.toolCallIds?q({policy:o.toolCallIds,ids:G(e.messages),map:s.toolIds}):c=>c;for(const c of e.tools??[])(c.type===void 0||c.type==="custom")&&r(c.name);const m=W(e.messages,new Set((e.tools??[]).map(c=>c.name))),a=P(e.tools??[],m,n,e.tool_choice);s.toolSearch={mode:n.toolSearch,loaded:a.loaded,hidden:a.hidden,stubbed:a.stubbed,capped:m.capped};for(const c of Object.keys(e))if(!(H.has(c)||K.has(c)))if(R.has(c))x(s,c);else{if(n.unknownFields==="error")throw new j(`${c}: Extra inputs are not permitted`,{code:"unsupported_field",param:c});x(s,c)}const u={model:e.model},$=I(e.system,n),f=await J(e.messages,n,s,r,d,$,Q(e.context_management,s),o?.budget??z());u.messages=f.messages,f.prefill&&(u.continue_final_message=!0,u.add_generation_prompt=!1),e.max_tokens!==void 0&&(u[n.maxTokensField]=e.max_tokens),e.temperature!==void 0&&(u.temperature=e.temperature),e.top_p!==void 0&&(u.top_p=e.top_p),e.top_k!==void 0&&(n.topK?u.top_k=e.top_k:x(s,"top_k")),e.stop_sequences?.length>0&&(u.stop=e.stop_sequences),s.stream&&(u.stream=!0,n.streamUsage&&(u.stream_options={include_usage:!0},n.continuousUsage&&(u.stream_options.continuous_usage_stats=!0)));const h=b(a.offer,n,s,r);if(h.length>0){u.tools=h;const c=ee(e.tool_choice,n,s,r);c!==void 0&&(u.tool_choice=c),e.tool_choice?.disable_parallel_tool_use===!0&&n.parallelToolCalls&&(u.parallel_tool_calls=!1)}return te(e,n,u,o?.listed===!0),ne(e,n,s,u),{body:u,state:s}}_(pe,"request");function x(e,t){e.dropped.includes(t)||e.dropped.push(t)}_(x,"drop");function F(e,t,o){if(t.unknownFields==="error")throw new j(`${o}: is not supported by this translator`,{code:"unsupported_content",param:o});x(e,o)}_(F,"unsupported");function I(e,t){return e==null?"":(typeof e=="string"?[{type:"text",text:e}]:e).filter(n=>n.type==="text"&&!(t.stripBillingHeader&&n.text.trimStart().startsWith(U))).map(n=>n.text).join(`
|
|
2
2
|
|
|
3
|
-
`)}
|
|
4
|
-
`);return[{type:"text",text:`[Search result: ${
|
|
5
|
-
${
|
|
6
|
-
${
|
|
7
|
-
</system-reminder>`;c([{type:"text",text:
|
|
8
|
-
`);E.length>0&&(
|
|
3
|
+
`)}_(I,"textOf");function T(e){return typeof e=="string"?[{type:"text",text:e}]:e??[]}_(T,"asBlocks");function G(e){const t=[];for(const o of e)for(const n of T(o.content))n.type==="tool_use"?t.push(n.id):n.type==="tool_result"&&t.push(n.tool_use_id);return t}_(G,"idsOf");async function J(e,t,o,n,s,r,d,m){const a=[],u=[],$=new Map,f=t.inlineSystem==="auto"?"user":t.inlineSystem,h=new Map;if(t.reasoningRoundTrip){for(let i=e.length-1;i>=0;i--)if(e[i].role==="assistant")for(const l of[...T(e[i].content)].reverse())l.type==="thinking"&&h.set(l,await D(l.signature,{producer:o.producer,budget:m}))}const c=_(i=>{const l=a.at(-1);l?.role==="user"?l.parts.push(...i):a.push({role:"user",parts:i})},"pushUser"),v=_(async(i,l)=>{switch(i.type){case"text":return i.text===""?[]:[{type:"text",text:i.text}];case"image":return Z(i,t,o);case"document":{const g=[];for(const p of await L(i,{settings:t,toolInputs:$,toolUseId:l}))g.push(...await v(p,l));return g}case"search_result":{const g=(i.content??[]).map(p=>p.text).join(`
|
|
4
|
+
`);return[{type:"text",text:`[Search result: ${i.title??""} (${i.source??""})]
|
|
5
|
+
${g}`}]}case"tool_reference":return t.toolSearch==="native"&&t.deferredTools!=="drop"?[{type:"tool_reference",tool_name:i.tool_name}]:[{type:"text",text:o.toolSearch.loaded.includes(i.tool_name)?`[Tool loaded: ${i.tool_name}]`:`[Tool reference: ${i.tool_name}]`}];default:return F(o,t,`block:${i.type}`),[]}},"toParts");for(const i of e)if(i.role==="system"){const l=I(i.content,t);if(l===""||f==="drop")continue;if(f==="keep")a.push({role:"system",content:l});else if(f==="hoist")u.push(l);else{const g=l.trimStart().startsWith("<system-reminder>")?l:`<system-reminder>
|
|
6
|
+
${l}
|
|
7
|
+
</system-reminder>`;c([{type:"text",text:g}])}}else if(i.role==="user"){const l=[],g=[];for(const k of T(i.content)){if(k.type!=="tool_result"){l.push(...await v(k,null));continue}const S=[];for(const y of T(k.content))S.push(...await v(y,k.tool_use_id));const M=S.filter(y=>y.type==="text").map(y=>y.text),E=S.filter(y=>y.type==="image_url");let w=S.some(y=>y.type==="tool_reference")?S.filter(y=>y.type!=="image_url"):M.join(`
|
|
8
|
+
`);E.length>0&&(g.push({type:"text",text:`Image output of tool call ${s(k.tool_use_id)}:`},...E),w===""&&(w="(image output attached in the next message)")),k.is_error===!0&&t.toolErrors&&(w=typeof w=="string"?`${t.toolErrorMarker}${w}`:[{type:"text",text:t.toolErrorMarker.trimEnd()},...w]),a.push({role:"tool",tool_call_id:s(k.tool_use_id),content:w})}const p=[...g,...l];p.length>0&&c(p)}else if(i.role==="assistant"){const l={role:"assistant",texts:[],parts:[],tool_calls:[]};for(const p of T(i.content))if(p.type==="text")p.text!==""&&l.texts.push(p.text);else if(p.type==="thinking"){const k=h.get(p)??null;k?l.parts.push({restored:k}):p.thinking&&l.parts.push({text:p.thinking})}else p.type==="redacted_thinking"?x(o,"redacted_thinking"):p.type==="tool_use"?($.set(p.id,p.input),l.tool_calls.push({id:s(p.id),type:"function",function:{name:n(p.name),arguments:JSON.stringify(p.input??{})}})):F(o,t,`block:${p.type}`);const g=a.at(-1);g?.role==="assistant"?(g.texts.push(...l.texts),g.parts.push(...l.parts),g.tool_calls.push(...l.tool_calls)):a.push(l)}else throw new j(`messages: unsupported role "${i.role}"`,{code:"invalid_request",param:"messages"});return Y(a,t,r,u,d,o)}_(J,"convertMessages");function Q(e,t){let o=1/0;for(const n of e?.edits??[])typeof n.type=="string"&&n.type.startsWith("clear_thinking")?n.keep==="all"?o=1/0:o=n.keep?.value??1:x(t,`context_management.${n.type}`);return o}_(Q,"thinkingTurnsToKeep");function V(e,t){const o=e.find(a=>a.restored&&(a.restored.reasoning_content!==void 0||a.restored.reasoning!==void 0)),n=o?o.restored.reasoning_content!==void 0?"reasoning_content":"reasoning":t;let s="",r,d=null;for(const a of e){const u=a.restored?a.restored.reasoning_content??a.restored.reasoning:a.text;a.restored?.reasoning_details&&(r=[...r??[],...a.restored.reasoning_details]),u!==void 0&&(s+=(d!==null&&!(d.restored&&a.restored)?`
|
|
9
9
|
|
|
10
|
-
`:"")+
|
|
10
|
+
`:"")+u,d=a)}const m={};return s!==""&&(m[n]=s),r&&(m.reasoning_details=r),m}_(V,"reasoningFields");function X(e){return!!(e.reasoning_content||e.reasoning||e.reasoning_details)}_(X,"reasoningSent");function Y(e,t,o,n,s,r){let d=s;for(let f=e.length-1;f>=0;f--)e[f].role!=="assistant"||e[f].parts.length===0||(d>0?d--:e[f].parts=[]);const m=[],a=[o,...n].filter(f=>f!=="").join(`
|
|
11
11
|
|
|
12
|
-
`);
|
|
12
|
+
`);a!==""&&m.push({role:"system",content:a});for(const f of e)if(f.role==="user"){const h=f.parts.every(c=>c.type==="text");m.push({role:"user",content:h?f.parts.map(c=>c.text).join(`
|
|
13
13
|
|
|
14
|
-
`):f.parts})}else if(f.role==="assistant"){const
|
|
14
|
+
`):f.parts})}else if(f.role==="assistant"){const h={role:"assistant",content:f.texts.join(`
|
|
15
15
|
|
|
16
|
-
`)};f.parts.length>0&&Object.assign(
|
|
16
|
+
`)};f.parts.length>0&&Object.assign(h,V(f.parts,t.reasoningField)),t.reasoningRoundTrip&&f.tool_calls.length>0&&!X(h)&&(r.reasoningMissing=(r.reasoningMissing??0)+1),f.tool_calls.length>0&&(h.tool_calls=f.tool_calls),m.push(h)}else m.push(f);const u=m.at(-1),$=t.prefill==="continue_final_message"&&u?.role==="assistant"&&!u.tool_calls&&u.content!=="";return{messages:m,prefill:$}}_(Y,"finalize");function Z(e,t,o){if(!t.images)return x(o,"image"),[{type:"text",text:A}];const n=e.source;return n.type==="base64"?[{type:"image_url",image_url:{url:`data:${n.media_type};base64,${n.data}`}}]:n.type==="url"?[{type:"image_url",image_url:{url:n.url}}]:(x(o,"image.file"),[{type:"text",text:"[An image supplied as an uploaded file could not be forwarded.]"}])}_(Z,"imageParts");function b(e,t,o,n){const s=[];for(const r of e){if(r.type!==void 0&&r.type!=="custom"){x(o,`tool:${r.name}`);continue}if(r.defer_loading===!0&&t.deferredTools==="drop"){x(o,"deferred_tools");continue}const d=r.input_schema??{type:"object",properties:{}},m={name:n(r.name),parameters:t.toolSchema==="sanitize"?C(d):d};r.description!==void 0&&(m.description=r.description),r.strict!==void 0&&(m.strict=r.strict),s.push(r.defer_loading===!0&&t.toolSearch==="native"?{type:"function",function:m,defer_loading:!0}:{type:"function",function:m})}return s}_(b,"convertTools");function ee(e,t,o,n){switch(e?.type){case void 0:return;case"auto":return"auto";case"any":return"required";case"none":return"none";case"tool":return{type:"function",function:{name:n(e.name)}};default:F(o,t,`tool_choice:${e.type}`);return}}_(ee,"convertToolChoice");function te(e,t,o,n){const s=e.thinking,r=s?.type;let d;r==="disabled"?(t.thinking==="reasoning_effort"&&(d="none"),t.thinking==="chat_template_kwargs"&&(o.chat_template_kwargs={enable_thinking:!1})):(t.effort&&e.output_config?.effort!==void 0&&(d=e.output_config.effort),r==="enabled"&&(t.thinking==="chat_template_kwargs"&&(o.chat_template_kwargs={enable_thinking:!0},t.thinkingBudget&&s.budget_tokens!==void 0&&e.max_tokens!==void 0&&(o.thinking_token_budget=Math.max(0,Math.min(s.budget_tokens,e.max_tokens-1)))),t.thinking==="reasoning_effort"&&d===void 0&&!n&&(d="medium"))),d!==void 0&&(o.reasoning_effort=d)}_(te,"applyThinking");function ne(e,t,o,n){const s=e.output_config?.format??e.output_format;s!==void 0&&(t.responseFormat&&s.type==="json_schema"?n.response_format={type:"json_schema",json_schema:{name:"output",schema:s.schema}}:x(o,"output_config.format"));for(const r of Object.keys(e.output_config??{}))r!=="effort"&&r!=="format"&&x(o,`output_config.${r}`)}_(ne,"applyOutputConfig");export{pe as request};
|
|
17
17
|
//# sourceMappingURL=request.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var _=Object.defineProperty;var d=(o,e)=>_(o,"name",{value:e,configurable:!0});import{resolve as x}from"../../../../../../../lib/settings.mjs";import{restoreId as y,restoreName as I}from"../../../../../../../lib/ids.mjs";import{detailOf as N}from"../../../../../../../lib/thinking.mjs";import{toOpenAIFinish as O,toOpenAIUsage as j}from"../../../../../../../lib/finish.mjs";function F(o,e,h){const c=x(h),g=e?.toolNames??{},m=e?.toolIds??{},s=[],l=[],n=[],r=[];let p=0,f=!1;for(const t of o.content??[]){if(c.reasoningRoundTrip){const u=N(t,p+(f?1:0));u&&r.push(u)}t.type==="text"&&t.text!==""&&(f=!0),t.type==="tool_use"&&p++,t.type==="text"?s.push(t.text):t.type==="thinking"?l.push(t.thinking):t.type==="tool_use"&&n.push({id:y(m,t.id),type:"function",function:{name:I(g,t.name),arguments:JSON.stringify(t.input??{})}})}const i={role:"assistant",content:s.length===0&&n.length>0?null:s.join("")};l.length>0&&(i[c.reasoningField]=l.join("")),r.length>0&&(i.reasoning_details=r),n.length>0&&(i.tool_calls=n);const a={index:0,message:i,finish_reason:O(o.stop_reason)};return typeof o.stop_sequence=="string"&&(a.stop_reason=o.stop_sequence),e&&(e.upstreamUsage=o.usage),{id:`chatcmpl-${String(o.id).replace(/^msg_/,"")}`,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:e?.model??o.model,choices:[a],usage:j(o.usage)}}d(F,"response");export{F as response};
|
|
2
2
|
//# sourceMappingURL=response.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var I=Object.defineProperty;var r=(d,o)=>I(d,"name",{value:o,configurable:!0});import{resolve as N}from"../../../../../../../lib/settings.mjs";import{anthropicToOpenAIType as U,anthropicTypeToStatus as j}from"../../../../../../../lib/errors.mjs";import{completionId as D,restoreId as T,restoreName as E}from"../../../../../../../lib/ids.mjs";import{toOpenAIFinish as w,toOpenAIUsage as A}from"../../../../../../../lib/finish.mjs";import{messages as B}from"../../../../../../../lib/sse.mjs";import{detailOf as q}from"../../../../../../../lib/thinking.mjs";async function*C(d,o,k){const u=N(k),x=o?.toolNames??{},b=o?.toolIds??{},O=o?.includeUsage??!0,y=Math.floor(Date.now()/1e3),s=new Map;let c=D(),p=o?.model,a={},g=!1,n=null,_=!1,h=0;const i=r((e,t=null,l={})=>({data:{id:c,object:"chat.completion.chunk",created:y,model:p,choices:[{index:0,delta:e,finish_reason:t,...l}]}}),"chunk"),f=r((e,t)=>({data:{error:{message:t,type:U(e),param:null,code:j(e)}}}),"failure");try{for await(const{data:e}of B(d))if(e.type==="message_start")c=`chatcmpl-${String(e.message.id).replace(/^msg_/,"")}`,p=o?.model??e.message.model,a={...e.message.usage},o&&(o.upstreamUsage=a),yield i({role:"assistant",content:""});else if(e.type==="content_block_start"){const t=e.content_block;if((t.type==="thinking"||t.type==="redacted_thinking")&&(n={type:t.type,data:t.data,thinking:"",signature:"",slot:h+(_?1:0)}),t.type==="tool_use"&&h++,t.type==="tool_use"){s.set(e.index,s.size);const l=E(x,t.name);yield i({tool_calls:[{index:s.get(e.index),id:T(b,t.id),type:"function",function:{name:l,arguments:""}}]})}}else if(e.type==="content_block_delta"){const t=e.delta;t.type==="text_delta"?(t.text!==""&&(_=!0),yield i({content:t.text})):t.type==="thinking_delta"?(n&&(n.thinking+=t.thinking),yield i({[u.reasoningField]:t.thinking})):t.type==="signature_delta"?n&&(n.signature+=t.signature):t.type==="input_json_delta"&&s.has(e.index)&&(yield i({tool_calls:[{index:s.get(e.index),function:{arguments:t.partial_json}}]}))}else if(e.type==="content_block_stop"){const t=u.reasoningRoundTrip&&n?q(n,n.slot):null;t&&(yield i({reasoning_details:[t]})),n=null}else if(e.type==="message_delta"){for(const[l,m]of Object.entries(e.usage??{}))m!=null&&(a[l]=m);o&&(o.upstreamUsage=a);const t=typeof e.delta?.stop_sequence=="string"?{stop_reason:e.delta.stop_sequence}:{};yield i({},w(e.delta?.stop_reason),t)}else if(e.type==="message_stop"){g=!0;break}else if(e.type==="error"){yield f(e.error.type,e.error.message),yield{data:"[DONE]"};return}}catch(e){yield f(e.type??"api_error",e.message),yield{data:"[DONE]"};return}if(!g){yield f("api_error","Upstream stream ended before completion"),yield{data:"[DONE]"};return}O&&(yield{data:{id:c,object:"chat.completion.chunk",created:y,model:p,choices:[],usage:A(a)}}),yield{data:"[DONE]"}}r(C,"stream");export{C as stream};
|
|
2
2
|
//# sourceMappingURL=stream.mjs.map
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
var v=Object.defineProperty;var
|
|
1
|
+
var v=Object.defineProperty;var u=(e,s)=>v(e,"name",{value:s,configurable:!0});import{resolve as N}from"../../../../../../../lib/settings.mjs";import{TranslatorError as h}from"../../../../../../../lib/errors.mjs";import{anthropicToolId as O,createIdFitter as S,createNameFitter as q}from"../../../../../../../lib/ids.mjs";import{parseArguments as E}from"../../../../../../../lib/arguments.mjs";import{sanitize as $}from"../../../../../../../lib/schema.mjs";import{blocksOf as A}from"../../../../../../../lib/thinking.mjs";const F=new Set(["model","messages","max_tokens","max_completion_tokens","temperature","top_p","top_k","stop","stream","stream_options","tools","tool_choice","parallel_tool_calls","reasoning_effort","response_format","user","n"]),M=new Set(["metadata","store","service_tier"]),j=/^data:([^;,]+);base64,(.*)$/s;async function Q(e,s,a){const o=N(s);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new h("messages is a required property",{code:"invalid_request",param:"messages"});if(e.n!==void 0&&e.n!==1)throw new h("n: only a single choice (n = 1) is supported",{code:"unsupported_field",param:"n"});const r={front:"openai",model:e.model,stream:e.stream===!0,includeUsage:e.stream_options?.include_usage===!0,reasoningField:o.reasoningField,toolNames:{},toolIds:{},dropped:[]},m=q({max:128,map:r.toolNames}),f=a?.toolCallIds?S({policy:a.toolCallIds,ids:C(e.messages),map:r.toolIds}):O;for(const t of Object.keys(e))if(!(F.has(t)||M.has(t))){if(o.unknownFields==="error")throw new h(`${t}: Unrecognized request argument`,{code:"unsupported_field",param:t});k(r,t)}const c=[],_=[],I=u((t,n)=>{const l=_.at(-1);l?.role===t?l.blocks.push(...n):_.push({role:t,blocks:[...n]})},"push");for(const t of e.messages)switch(t.role){case"system":case"developer":{const n=w(t.content);n!==""&&c.push(n);break}case"user":I("user",z(t.content,r));break;case"assistant":{const n=o.reasoningRoundTrip?A(t.reasoning_details):[],l=[],p=w(t.content);p!==""?l.push({type:"text",text:p}):typeof t.refusal=="string"&&t.refusal!==""&&l.push({type:"text",text:t.refusal}),U(t)&&!n.some(g=>g.block.type==="thinking")&&k(r,"reasoning");for(const g of t.tool_calls??[])l.push({type:"tool_use",id:f(g.id),name:m(g.function.name),input:E(g.function.arguments)});const b=R(n,l);b.length>0&&I("assistant",b);break}case"tool":{const n={type:"tool_result",tool_use_id:f(t.tool_call_id),content:w(t.content)};if(o.toolErrorInference){const p=o.toolErrorMarker.trimEnd();n.content.startsWith(p)&&(n.is_error=!0,n.content=n.content.slice(p.length).replace(/^\s/,""))}const l=_.at(-1);if(l?.role==="user"){const p=l.blocks.findIndex(b=>b.type!=="tool_result");l.blocks.splice(p===-1?l.blocks.length:p,0,n)}else _.push({role:"user",blocks:[n]});break}default:throw new h(`messages: unsupported role "${t.role}"`,{code:"invalid_request",param:"messages"})}const i={model:e.model,max_tokens:e.max_completion_tokens??e.max_tokens??o.defaultMaxTokens};c.length>0&&(i.system=c.join(`
|
|
2
2
|
|
|
3
|
-
`)),
|
|
3
|
+
`)),i.messages=_.map(t=>({role:t.role,content:t.blocks})),e.temperature!==void 0&&(i.temperature=Math.min(1,e.temperature)),e.top_p!==void 0&&(i.top_p=e.top_p),e.top_k!==void 0&&(i.top_k=e.top_k),e.stop!==void 0&&e.stop!==null&&(i.stop_sequences=[].concat(e.stop)),r.stream&&(i.stream=!0),typeof e.user=="string"&&(i.metadata={user_id:e.user});const y=[];for(const t of e.tools??[]){if(t.type!=="function"){k(r,`tool:${t.type}`);continue}const n=t.function,l=n.parameters??{type:"object",properties:{}},p={name:m(n.name),input_schema:o.toolSchema==="sanitize"?$(l):l};n.description!==void 0&&(p.description=n.description),n.strict!==void 0&&(p.strict=n.strict),y.push(p)}if(y.length>0){i.tools=y;let t=D(e.tool_choice,m);e.parallel_tool_calls===!1&&t.type!=="none"&&(t={...t,disable_parallel_tool_use:!0}),(e.tool_choice!==void 0||e.parallel_tool_calls===!1)&&(i.tool_choice=t)}const d=e.reasoning_effort;d==="none"?i.thinking={type:"disabled"}:d!=null&&(i.thinking={type:"adaptive"},i.output_config={effort:d==="minimal"?"low":d});const x=e.response_format;return x?.type==="json_schema"?i.output_config={...i.output_config,format:{type:"json_schema",schema:x.json_schema.schema}}:x?.type==="json_object"&&(o.jsonObject==="schema"?(i.output_config={...i.output_config,format:{type:"json_schema",schema:{type:"object"}}},r.jsonSchemaEmulated=!0):o.jsonObject==="instruction"?i.system=i.system===void 0?T:`${i.system}
|
|
4
4
|
|
|
5
|
-
${T}`:
|
|
5
|
+
${T}`:k(r,"response_format")),{body:i,state:r}}u(Q,"request");const T="Respond with a single valid JSON object and nothing else.";function C(e){const s=[];for(const a of e)if(a.role==="assistant")for(const o of a.tool_calls??[])s.push(o.id);else a.role==="tool"&&s.push(a.tool_call_id);return s}u(C,"idsOf");function k(e,s){e.dropped.includes(s)||e.dropped.push(s)}u(k,"drop");function R(e,s){const a=[...e].sort((f,c)=>f.at-c.at),o=[];let r=0;const m=s.findIndex(f=>f.type==="tool_use");for(const f of a){const c=f.at<=1&&m!==-1?Math.min(f.at,m):f.at;for(;r<Math.min(c,s.length);)o.push(s[r++]);o.push(f.block)}return[...o,...s.slice(r)]}u(R,"interleave");function U(e){return!!(e.reasoning||e.reasoning_content)}u(U,"reasoningPresent");function w(e){return typeof e=="string"?e:(e??[]).filter(s=>s.type==="text").map(s=>s.text).join(`
|
|
6
6
|
|
|
7
|
-
`)}
|
|
7
|
+
`)}u(w,"plainText");function z(e,s){if(typeof e=="string")return e===""?[]:[{type:"text",text:e}];const a=[];for(const o of e??[])if(o.type==="text")a.push({type:"text",text:o.text});else if(o.type==="image_url"){const r=j.exec(o.image_url.url);a.push({type:"image",source:r?{type:"base64",media_type:r[1],data:r[2]}:{type:"url",url:o.image_url.url}})}else if(o.type==="file"&&typeof o.file?.file_data=="string"&&j.test(o.file.file_data)){const r=j.exec(o.file.file_data);a.push({type:"document",source:{type:"base64",media_type:r[1],data:r[2]}})}else k(s,`content:${o.type}`);return a}u(z,"userBlocks");function D(e,s){return e==="none"?{type:"none"}:e==="required"?{type:"any"}:typeof e=="object"&&e!==null?{type:"tool",name:s(e.function.name)}:{type:"auto"}}u(D,"toolChoice");export{Q as request};
|
|
8
8
|
//# sourceMappingURL=request.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var _=Object.defineProperty;var c=(o,s)=>_(o,"name",{value:s,configurable:!0});import{resolve as k}from"../../../../../../../lib/settings.mjs";import{TranslatorError as m}from"../../../../../../../lib/errors.mjs";import{messageId as T,anthropicToolId as x,restoreName as A}from"../../../../../../../lib/ids.mjs";import{sign as w,reasoningOf as I,reasoningKey as C,detailsText as N,splitThink as O}from"../../../../../../../lib/thinking.mjs";import{isComplete as S,parseArguments as U}from"../../../../../../../lib/arguments.mjs";import{finishError as v,toAnthropicStop as E,toAnthropicUsage as b}from"../../../../../../../lib/finish.mjs";function F(o,s,u){const f=k(u),l=o?.choices?.[0];if(!l)throw new m("Upstream response contained no choices",{code:"invalid_upstream_response",status:502});const g=v(l.finish_reason);if(g)throw new m(g.message,{code:g.code,status:g.status,type:g.type});const e=l.message??{},i=[];let a=I(e),t=typeof e.content=="string"?e.content:"";const n={};if(f.reasoningRoundTrip){const r=C(e);r&&(n[r]=e[r]),Array.isArray(e.reasoning_details)&&e.reasoning_details.length>0&&(n.reasoning_details=e.reasoning_details)}a===""&&f.thinkTags&&({reasoning:a,text:t}=O(t,f.thinkOpen)),a===""&&n.reasoning_details&&(a=N(n.reasoning_details)),(a!==""||n.reasoning_details)&&(i.push({type:"thinking",thinking:a,signature:w(Object.keys(n).length>0?n:void 0,s?.producer)}),t=t.trimStart()),t===""&&typeof e.refusal=="string"&&(t=e.refusal),t!==""&&i.push({type:"text",text:t});const d=s?.toolNames??{},h=s?.toolIds??{},p=e.tool_calls??[];for(const r of p)i.push({type:"tool_use",id:x(r.id,h),name:A(d,r.function.name),input:U(r.function.arguments)});i.length===0&&i.push({type:"text",text:""});const y=E(l.finish_reason,{hasToolCalls:p.length>0,matchedStop:l.stop_reason,refusal:e.refusal,argumentsComplete:p.length===0||S(p.at(-1).function.arguments)});return s&&o.usage&&(s.upstreamUsage=o.usage),{id:T(),type:"message",role:"assistant",model:s?.model??o.model,content:i,...y,usage:b(o.usage)}}c(F,"response");export{F as response};
|
|
2
2
|
//# sourceMappingURL=response.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var P=Object.defineProperty;var l=(b,r)=>P(b,"name",{value:r,configurable:!0});import{resolve as Q}from"../../../../../../../lib/settings.mjs";import{TranslatorError as V,openaiToAnthropicType as W,rewriteContextOverflow as X}from"../../../../../../../lib/errors.mjs";import{isComplete as Y}from"../../../../../../../lib/arguments.mjs";import{messageId as Z,anthropicToolId as $,restoreName as ee}from"../../../../../../../lib/ids.mjs";import{sign as te,reasoningOf as ne,reasoningKey as ie,detailsText as oe,mergeDetail as se,createThinkSplitter as le}from"../../../../../../../lib/thinking.mjs";import{finishError as re,toAnthropicStop as ae,toAnthropicUsage as q}from"../../../../../../../lib/finish.mjs";import{messages as de}from"../../../../../../../lib/sse.mjs";async function*ke(b,r,B){const u=Q(B),F=r?.toolNames??{},J=r?.toolIds??{},d=u.thinkTags?le(u.thinkOpen):null,c=new Map;let _=!1,j=0,n=null,N=null,k=null,O=null,h="",v=!1,A=!1,C=0,T=!1,I=!1,U=!1;const w=[],s=l((e,t)=>({event:e,data:{type:e,...t}}),"event"),p=l((e,t)=>s("error",{error:{type:e,message:t}}),"fail"),K=l(e=>{const t={};return u.reasoningRoundTrip&&(e.key!==null&&(t[e.key]=e.text),e.details.length>0&&(t.reasoning_details=e.details)),Object.keys(t).length>0?t:void 0},"carried");function*y(){if(n!==null){n.kind==="thinking"?yield s("content_block_delta",{index:n.index,delta:{type:"signature_delta",signature:te(K(n.carry),r?.producer)}}):n.kind==="tool"&&(n.entry.state="closed",n.entry.sent||(yield s("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:"{}"}}))),yield s("content_block_stop",{index:n.index}),n=null;for(const e of c.values()){if(e.state!=="pending")continue;e.state="closed";const t=j++,i=e.args.join("");yield s("content_block_start",{index:t,content_block:e.block}),yield s("content_block_delta",{index:t,delta:{type:"input_json_delta",partial_json:i===""?"{}":i}}),yield s("content_block_stop",{index:t})}}}l(y,"close");function*M(){for(const e of E()){if(e.state!=="buffered")continue;yield*g("tool",e.block,e);const t=e.args.join("");t!==""&&(e.sent=!0,yield s("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:t}})),yield*y()}}l(M,"flushBuffered");function E(){return[...c.entries()].sort((e,t)=>e[0]-t[0]).map(e=>e[1])}l(E,"ordered");function*g(e,t,i){yield*y(),n={kind:e,index:j++,entry:i,carry:{key:null,text:"",details:[]}},yield s("content_block_start",{index:n.index,content_block:t})}l(g,"begin");function*a(e,t,i=null){if(e==="text"&&U&&(t=t.trimStart()),t==="")return;if(U=e==="thinking",e==="text"&&(v=!0),n?.kind==="tool"){w.push({kind:e,text:t,key:i});return}n?.kind!==e&&(yield*g(e,e==="text"?{type:"text",text:""}:{type:"thinking",thinking:"",signature:""})),i!==null&&(n.carry.key??=i,n.carry.text+=t);const o=e==="text"?{type:"text_delta",text:t}:{type:"thinking_delta",thinking:t};yield s("content_block_delta",{index:n.index,delta:o})}l(a,"piece");function*R(e){if(n?.kind==="tool"){w.push({kind:"details",items:e});return}n?.kind!=="thinking"&&(yield*g("thinking",{type:"thinking",thinking:"",signature:""}));for(const t of e)se(n.carry.details,t);n.carry.key===null&&(yield*a("thinking",oe(e)))}l(R,"details");function*z(e){yield*a("thinking",e.reasoning),yield*a("text",e.text)}l(z,"pieces");function*G(e){let t=e.index;t===void 0&&(t=e.id===void 0?C:c.size),C=t;let i=c.get(t);if(i===void 0){const x=e.function?.name??"",m={type:"tool_use",id:$(e.id,J),name:ee(F,x),input:{}};d?.pending()&&(I=!0);const f=u.toolStreaming==="buffer"||I;i={state:f?"buffered":"pending",sent:!1,args:[],block:m},c.set(t,i),!f&&n?.kind!=="tool"&&(i.state="open",yield*g("tool",m,i))}let o=e.function?.arguments;o!==null&&typeof o=="object"&&(o=JSON.stringify(o)),!(o==null||o==="")&&(i.args.push(o),i.state==="open"&&(i.sent=!0,yield s("content_block_delta",{index:n.index,delta:{type:"input_json_delta",partial_json:o}})))}l(G,"toolCall");try{for await(const e of de(b)){if(e.data==="[DONE]"){A=!0;break}const t=e.data;if(t.error){const f=t.error.message??"Upstream error";yield p(W(t.error),X(f)??f);return}_||(_=!0,yield s("message_start",{message:{id:Z(),type:"message",role:"assistant",model:r?.model??t.model,content:[],stop_reason:null,stop_sequence:null,usage:q(t.usage)}})),t.usage&&(N=t.usage,r&&(r.upstreamUsage=t.usage));const i=t.choices?.[0];if(!i)continue;const o=i.delta??{},x=ne(o);let m="";x!==""&&!T&&(T=!0,d&&(m=d.release().text)),yield*a("thinking",x,ie(o)),yield*a("text",m),u.reasoningRoundTrip&&Array.isArray(o.reasoning_details)&&o.reasoning_details.length>0&&(yield*R(o.reasoning_details)),typeof o.content=="string"&&o.content!==""&&(d&&!T?yield*z(d.push(o.content)):yield*a("text",o.content)),typeof o.refusal=="string"&&(h+=o.refusal);for(const f of o.tool_calls??[])yield*G(f);i.finish_reason&&(k=i.finish_reason),typeof i.stop_reason=="string"&&(O=i.stop_reason)}}catch(e){yield e instanceof V?p(e.type,e.message):p("api_error",e.message);return}if(!_||k===null&&!A){yield p("api_error",_?"Upstream stream ended before completion":"Upstream stream ended without data");return}const S=re(k);if(S){yield p(S.type,S.message);return}d&&(yield*z(d.end())),yield*M();const D=E().at(-1),H=D===void 0||Y(D.args.join(""));yield*y();for(const e of w)yield*e.kind==="details"?R(e.items):a(e.kind,e.text,e.key);!v&&h!==""&&(yield*a("text",h)),yield*y(),j===0&&(yield*g("text",{type:"text",text:""}),yield*y());const L=ae(k,{hasToolCalls:c.size>0,matchedStop:O,argumentsComplete:H,refusal:h});yield s("message_delta",{delta:L,usage:q(N)}),yield s("message_stop",{})}l(ke,"stream");export{ke 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 o=(r,e)=>g(r,"name",{value:e,configurable:!0});import{context as h,self as u}from"../lib/runtime.mjs";import{capture as w}from"../lib/capture.mjs";import{TranslatorError as m}from"../lib/errors.mjs";import{warn as p}from"../lib/warn.mjs";import{emit as y}from"../lib/events.mjs";import*as k from"../lib/http.mjs";import{validToolIdPolicy as $}from"../lib/ids.mjs";async function D(r={}){const e=w(u,h),{status:f}=u,l=r.network!==!1;for(const t of e.drivers){const s=t.info.fields;if(s!==void 0&&!(Array.isArray(s)&&s.every(i=>typeof i=="string")))throw new m(`Driver ${t.name}: info().fields must be an array of field names`,{code:"invalid_driver",status:500})}for(const t of e.drivers){const s=t.info.toolCallIds;if(s!==void 0&&!$(s))throw new m(`Driver ${t.name}: info().toolCallIds must be { alphabet, min?, max? } with a character-class alphabet and positive integer bounds`,{code:"invalid_driver",status:500})}const a={transport:e.state.transport,signal:r.signal,timeout:e.config.detectTimeout};return await Promise.all(e.state.endpoints.map(t=>x(e,t,l,a))),f()}o(D,"detect");async function x(r,e,f,l){let a,t=null;if(e.backend!=="auto"){if(a=r.drivers.find(s=>s.name===e.backend),!a)throw new m(`Unknown backend "${e.backend}" for upstream ${e.name}`,{code:"unknown_backend",status:500});t={version:e.version}}else if(f){const s=URL.canParse(e.url)?new URL(e.url).hostname:"",i={endpoint:e,...l,probes:new Map};let n=!1;for(const[v,c]of r.drivers.entries()){if(!n&&c.info.probes?.length>0&&!c.info.hosts?.includes(s)){n=!0;for(const d of r.drivers.slice(v))for(const b of d.info.probes??[])k.probe(i,b,d.info)}if(t=await U(c,i),t){a=c;break}}}if(a||(a=r.drivers.find(s=>s.name==="compatible"),t={reachable:null,models:[]}),e.driver=a.name,e.protocol=a.info.protocol,e.facts=t,e.driverSettings=await a.settings?.(t)??{},e.probed={},f&&e.backend==="auto"&&a.probe){const s=o(n=>e.settings[n]!==void 0||r.config.settings[n]!==void 0,"given"),i={inlineSystem:r.settings(e).inlineSystem==="auto",images:!s("images")};(i.inlineSystem||i.images)&&(e.probed=await a.probe({endpoint:e,facts:t,want:i,...l}))}t.reachable===!1?p(r.config,r.state,`Upstream ${e.name} (${e.url}) did not answer detection; using the generic OpenAI-compatible driver`,{type:"detect",kind:"unreachable",endpoint:e.name,url:e.url,driver:a.name}):y({config:r.config,state:r.state},{type:"detect",kind:"driver",level:"info",message:`Upstream ${e.name} (${e.url}): driver ${a.name}`,endpoint:e.name,url:e.url,driver:a.name})}o(x,"bind");async function U(r,e){try{return await r.detect?.(e)??null}catch{return null}}o(U,"detectWith");export{D as detect};
|
|
2
2
|
//# sourceMappingURL=detect.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var l=Object.defineProperty;var i=(t,e)=>l(t,"name",{value:e,configurable:!0});import*as f from"../../../lib/http.mjs";import{info as a}from"./info.mjs";function o(t){if(t===null||typeof t!="object"||Array.isArray(t)||"object"in t||!Array.isArray(t.data)||typeof t.has_more!="boolean"||![t.first_id,t.last_id].every(r=>r===null||typeof r=="string")||!t.data.every(r=>r?.type==="model"&&typeof r.id=="string"&&typeof r.display_name=="string"))return!1;const n=t.data[0]?.id??null,s=t.data.at(-1)?.id??null;return t.first_id===n&&t.last_id===s}i(o,"isAnthropicList");async function c(t){const{endpoint:e}=t,n=URL.canParse(e.url)?new URL(e.url).hostname:"";if(a().hosts.includes(n))return{host:n};const s=await f.probe(t,"/v1/models",a());return!s.ok||!o(s.body)?null:{models:s.body.data.map(r=>r.id)}}i(c,"detect");export{c as detect};
|
|
2
2
|
//# sourceMappingURL=detect.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var t=Object.defineProperty;var s=(e,o)=>t(e,"name",{value:o,configurable:!0});function a(){return{name:"anthropic",protocol:"anthropic.messages",order:38,auth:"x-api-key",strict:!0,hosts:["api.anthropic.com"],probes:["/v1/models"],headers:{"anthropic-version":"2023-06-01"},paths:{messages:"/v1/messages",models:"/v1/models",count:"/v1/messages/count_tokens"}}}s(a,"info");export{a as info};
|
|
2
2
|
//# sourceMappingURL=info.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var r=Object.defineProperty;var o=(e
|
|
1
|
+
var r=Object.defineProperty;var o=(t,e)=>r(t,"name",{value:e,configurable:!0});import{VLLM_FIELDS as a}from"../../../lib/passthrough.mjs";function l(){return{name:"compatible",protocol:"openai.chat",order:100,auth:"bearer",strict:!1,fields:a,paths:{chat:"/v1/chat/completions",models:"/v1/models"}}}o(l,"info");export{l as info};
|
|
2
2
|
//# sourceMappingURL=info.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var a=Object.defineProperty;var e=(o,t)=>a(o,"name",{value:t,configurable:!0});function
|
|
1
|
+
var a=Object.defineProperty;var e=(o,t)=>a(o,"name",{value:t,configurable:!0});function c(){return{name:"deepseek",protocol:"openai.chat",order:35,auth:"bearer",strict:!1,paths:{chat:"/v1/chat/completions",models:"/v1/models"}}}e(c,"info");export{c as info};
|
|
2
2
|
//# sourceMappingURL=info.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var a=Object.defineProperty;var
|
|
1
|
+
var a=Object.defineProperty;var e=(p,t)=>a(p,"name",{value:t,configurable:!0});function r(){return{name:"llamacpp",protocol:"openai.chat",order:40,auth:"bearer",strict:!1,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"}}}e(r,"info");export{r as info};
|
|
2
2
|
//# sourceMappingURL=info.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var r=Object.defineProperty;var o=(e,a)=>r(e,"name",{value:a,configurable:!0});function
|
|
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};
|
|
2
2
|
//# sourceMappingURL=info.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var r=Object.defineProperty;var o=(t,e)=>r(t,"name",{value:e,configurable:!0});function n(){return{name:"openai",protocol:"openai.chat",order:30,auth:"bearer",strict:!0,paths:{chat:"/v1/chat/completions",models:"/v1/models"}}}o(n,"info");export{n as info};
|
|
2
2
|
//# sourceMappingURL=info.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var r=Object.defineProperty;var e=(t,o)=>r(t,"name",{value:o,configurable:!0});function s(){return{name:"sglang",protocol:"openai.chat",order:50,auth:"bearer",strict:!1,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(s,"info");export{s as info};
|
|
2
2
|
//# sourceMappingURL=info.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var r=Object.defineProperty;var
|
|
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};
|
|
2
2
|
//# sourceMappingURL=info.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var x=Object.defineProperty;var h=(e,s)=>x(e,"name",{value:s,configurable:!0});import{context as q,self as w}from"../../lib/runtime.mjs";import{capture as H}from"../../lib/capture.mjs";import{resolve as S,serves as J}from"../../lib/route.mjs";import*as y from"../../lib/http.mjs";import*as A from"../../lib/pool.mjs";import{emit as N}from"../../lib/events.mjs";import{meter as U}from"../../lib/usage.mjs";import{warn as C}from"../../lib/warn.mjs";import{start as I}from"../../lib/record.mjs";const M="/v1/embeddings";async function Q(e,s={}){const{transport:$}=q.state,{error:g}=w,a=H(w,q,"openai"),O=e&&typeof e=="object"&&!Array.isArray(e),l=U({model:null,requested:O&&typeof e.model=="string"?e.model:null,upstream:null,front:"openai",stream:!1},s.onUsage,(o,t)=>C(a.config,a.state,o,{type:"usage",...t,log:s.log})),u=h((o,t)=>(l.finish(t),Object.defineProperty(o,"usage",{value:l.usage,enumerable:!1,configurable:!0})),"end"),n=h((o,t,f={})=>u(g.build(o,t,{...f,front:"openai"}),o===499?"aborted":"error"),"fail");if(!e||typeof e!="object"||Array.isArray(e))return n(400,"The request body must be a JSON object",{code:"invalid_body"});if(typeof e.model!="string"||e.model==="")return n(400,"model is required",{code:"invalid_body",param:"model"});const P=Object.hasOwn(a.config.aliases,e.model)?a.config.aliases[e.model]:e.model;if(!a.state.endpoints.some(o=>J(o,P)))return n(404,`The model "${e.model}" is not served by any configured upstream.`,{code:"model_not_found",param:"model"});const c=S(a,e,{advance:!0}),p=c.pool.filter(o=>o.protocol==="openai.chat");if(p.length===0)return n(400,`The model "${e.model}" is served by an Anthropic-protocol upstream, which has no embeddings API.`,{code:"embeddings_unsupported",param:"model"});const v={...e,model:c.model};l.meta.model=c.model;let m,j=null;for(let o=0;o<p.length;o++){const t=p[o];l.meta.upstream=t.name;const f=a.driver(t);t.health.inflight++,t.health.requests++;const T=y.join(t.url,M),_={"content-type":"application/json",accept:"application/json",...f.info.headers,...y.headers(t,f.info.auth,s.headers)},b=I(a,{body:e,sent:{upstream:t.name,url:T,headers:_,body:v,state:{stream:!1}},front:"openai",requested:l.meta.requested,log:s.log,exchange:j,attempt:o+1});j??=b.id;let r;try{r=await y.json($,T,{method:"POST",headers:_,body:v,signal:s.signal})}finally{t.health.inflight--}if(s.signal?.aborted)return n(499,"The request was aborted",{code:"aborted"});const d=r.status===0;if(!d&&r.status<500)return A.succeeded(t),b.reply(r.status,r.headers,r.body),r.ok?r.body===null||typeof r.body!="object"?n(502,"The upstream returned a body that is not JSON",{code:"bad_gateway"}):(l.json(r.body),u({status:200,headers:{"content-type":"application/json"},body:r.body},"done")):u(g(r.status,r.body,{},{front:"openai",upstream:t.name,native:!0,model:e.model}),"upstream_error");const i=d?`Could not reach the upstream (${t.name}): ${r.error?.message}`:`HTTP ${r.status}`;A.failed(a.config,t,i);const k=d?{error:i}:{status:r.status,headers:r.headers,body:r.body};b.attempt(k,o+1<p.length?i:void 0),N(a,{type:"error",kind:d?"transport":"upstream",level:"error",message:d?i:`Upstream ${t.name} answered HTTP ${r.status}`,endpoint:t.name,model:c.model,log:s.log}),m={endpoint:t,result:r,reason:i}}return m.result.status===0?n(502,m.reason,{code:"upstream_unreachable"}):u(g(m.result.status,m.result.body,{},{front:"openai",upstream:m.endpoint.name,native:!0,model:e.model}),"upstream_error")}h(Q,"send");export{Q as send};
|
|
2
|
+
//# sourceMappingURL=send.mjs.map
|
package/dist/api/request.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var
|
|
2
|
-
${
|
|
1
|
+
var T=Object.defineProperty;var p=(e,n)=>T(e,"name",{value:n,configurable:!0});import{context as I,self as q}from"../lib/runtime.mjs";import{capture as C}from"../lib/capture.mjs";import{resolve as L}from"../lib/route.mjs";import*as U from"../lib/http.mjs";import{filterChatBody as B}from"../lib/passthrough.mjs";import{addSampling as E}from"../lib/sampling.mjs";import*as F from"../lib/images.mjs";import*as S from"../lib/tools.mjs";import{LIMITS as M}from"../lib/toolsearch.mjs";import{warn as b}from"../lib/warn.mjs";import{emit as P}from"../lib/events.mjs";import{fit as R,pinBody as H}from"../lib/effort.mjs";import{relax as z,strictTools as A}from"../lib/strict.mjs";import{TranslatorError as D}from"../lib/errors.mjs";function G(e){return`${e.driver}:${e.protocol}:${URL.canParse(e.url)?new URL(e.url).host:e.url}`}p(G,"producerOf");async function ne(e,n={}){const r=C(q,I,n.front),c=L(r,e,{endpoint:n.endpoint,advance:n.hold===!0&&n.endpoint===void 0});if(n.hold!==!0)return _(r,e,n,c);c.endpoint.health.inflight++;try{return await _(r,e,n,c)}catch(t){throw c.endpoint.health.inflight--,t}}p(ne,"request");function J(e,n,r,c,t,f){const g=A(n);if(g.length===0)return n;const i=g.filter(a=>a.problems.length>0);if(i.length>0&&t.unknownFields==="error")throw new D(`tools: ${i[0].name}: schema is outside the strict subset (${i[0].problems.join("; ")})`,{code:"unsupported_content",param:"tools"});const k=z(n);if(f.retry===!0)return k;const u=p((a,h)=>b(e.config,e.state,a,{kind:h,endpoint:r.name,log:f.log}),"tell");for(const a of i)u(`Upstream ${r.name}: strict tool ${a.name} has a schema outside the strict subset: ${a.problems.join("; ")} (sent without strict)`,"strict");const d=g.filter(a=>a.problems.length===0);return c.info.strict!==!0&&d.length>0&&u(`Upstream ${r.name}: the ${c.name} backend does not enforce strict tool use (${d.map(a=>a.name).join(", ")})`,"strict"),k}p(J,"checkStrict");async function _(e,n,r,c){const{endpoint:t,model:f,unserved:g,convert:i,pool:k,pin:u}=c,d=Object.hasOwn(e.config.efforts,f)?e.config.efforts[f]:void 0;u&&(n=H(n,u,e.front));const a=e.driver(t),h=`${t.name}
|
|
2
|
+
${f}`,w=r.images===!1||e.state.textOnly.has(h)&&e.settings(t).imageFallback,$=e.settings(t),x=r.jsonObject==="instruction"||e.state.jsonInstruction.has(h)&&$.jsonObjectFallback,m={...w?{...$,images:!1}:$,...x?{jsonObject:"instruction"}:{},...u?.effort===void 0?{}:{effort:!0}},v=n.stream===!0;let o,s;i?({body:o,state:s}=await i.request(n,m,{producer:G(t),budget:r.budget,listed:d!==void 0,toolCallIds:a.info.toolCallIds}),s.toolSearch?.capped&&b(e.config,e.state,`Tool search: more than ${M.definitions} tool definitions came back in ToolSearch results for ${t.name}; the rest are not offered.`,{kind:"toolsearch",endpoint:t.name,log:r.log})):o=e.front==="openai"&&(r.filter??m.passthroughFilter===!0)?B(n,a.info.fields):{...n};const y=e.front==="anthropic"?n.metadata?.sampling:void 0;if(t.protocol==="openai.chat"){const l=[];o=E(o,y,m,a.info.fields,l),s&&s.dropped.push(...l.filter(j=>!s.dropped.includes(j)))}else if(!i&&y!==void 0&&o.metadata?.sampling!==void 0){const{sampling:l,...j}=o.metadata;o={...o,metadata:j}}if(r.usage===!0&&v&&e.front==="openai"){if(i)s.includeUsage=!0;else if(m.streamUsage){const l=o.stream_options&&typeof o.stream_options=="object"?o.stream_options:{};o.stream_options={...l,include_usage:!0}}}w&&!i&&e.front==="openai"&&(o=F.strip(o)),r.tools===!1&&(o=S.strip(o)),o.model=f,o=J(e,o,t,a,m,r),d&&t.protocol==="openai.chat"&&R(o,d,f,(l,j)=>b(e.config,e.state,l,{type:"effort",endpoint:t.name,model:f,...j,log:r.log})),s?.reasoningMissing&&b(e.config,e.state,`Upstream ${t.name}: assistant turns with tool calls have no reasoning to send back (the client dropped its thinking blocks or their signatures), which servers that need it with tools will refuse.`,{kind:"reasoning",endpoint:t.name,log:r.log}),a.request&&(o=await a.request(o,{endpoint:t,settings:m,levels:d,state:s,warn:p(l=>b(e.config,e.state,l,{kind:"driver",endpoint:t.name,log:r.log}),"warn")})),s?.dropped?.length>0&&r.retry!==!0&&P(e,{type:"dropped",level:"debug",message:`Upstream ${t.name}: dropped ${s.dropped.join(", ")}`,endpoint:t.name,model:f,fields:[...s.dropped],log:r.log});const O=t.protocol==="openai.chat"?a.info.paths.chat:a.info.paths.messages;return{upstream:t.name,pool:k.map(l=>l.name),method:"POST",url:U.join(t.url,O),path:O,headers:{"content-type":"application/json",accept:v?"text/event-stream":"application/json",...a.info.headers,...i===null&&t.protocol==="anthropic.messages"?r.clientHeaders:void 0,...U.headers(t,a.info.auth,r.headers)},body:o,state:{v:1,upstream:t.name,front:e.front,native:i===null,model:n.model,stream:v,unserved:g,convert:s},images:{sent:F.carries(o),retry:m.imageFallback&&t.protocol==="openai.chat",key:h},tools:{sent:S.carries(o),retry:m.toolFallback&&t.protocol==="openai.chat"},json:{sent:s?.jsonSchemaEmulated===!0,retry:m.jsonObjectFallback&&t.protocol==="anthropic.messages",key:h}}}p(_,"prepare");export{ne as request};
|
|
3
3
|
//# sourceMappingURL=request.mjs.map
|
package/dist/api/send.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var nt=Object.defineProperty;var u=(c,s)=>nt(c,"name",{value:s,configurable:!0});import{context as F,self as Y}from"../lib/runtime.mjs";import{TranslatorError as C}from"../lib/errors.mjs";import*as Z from"../lib/http.mjs";import*as ot from"../lib/images.mjs";import*as it from"../lib/tools.mjs";import{parse as ut}from"../lib/upstream-error.mjs";import{meter as lt,normalize as ct}from"../lib/usage.mjs";import{capture as mt}from"../lib/capture.mjs";import{estimate as dt,bodyLength as pt,output as ft,patch as gt,stream as ht}from"../lib/estimate.mjs";import{warn as yt}from"../lib/warn.mjs";import{start as wt}from"../lib/record.mjs";import{emit as tt}from"../lib/events.mjs";import*as et from"../lib/pool.mjs";import{createBudget as bt}from"../lib/thinking.mjs";const kt={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"};async function Ht(c,s={}){const{transport:y,textOnly:p,jsonInstruction:k}=F.state,{request:j,response:_,stream:l,error:g}=Y,d=g.build,{config:i,state:x}=F,h=mt(Y,F,s.front),S=c&&typeof c=="object"&&!Array.isArray(c),O={model:null,requested:S&&typeof c.model=="string"?c.model:null,upstream:null,front:s.front??i.front,stream:S&&c.stream===!0},w=lt(O,s.onUsage,(m,H)=>yt(i,x,m,{type:"usage",...H,log:s.log})),U=u(m=>Object.defineProperty(m,"usage",{value:w.usage,enumerable:!1,configurable:!0}),"attach");try{return U(await jt({transport:y,textOnly:p,jsonInstruction:k,request:j,response:_,stream:l,error:g,captured:h},c,s,w))}catch(m){if(!(m instanceof C))throw s.signal?.aborted||tt(h,{type:"error",kind:"exception",level:"error",message:String(m?.message??m),log:s.log}),w.finish(s.signal?.aborted?"aborted":"error"),m;return w.finish("error"),U(d(m.status,m.message,{type:m.type,code:m.code,param:m.param,front:s.front}))}}u(Ht,"send");async function jt({transport:c,textOnly:s,jsonInstruction:y,request:p,response:k,stream:j,error:_,captured:l},g,d,i){const x={front:d.front,headers:d.headers,clientHeaders:d.clientHeaders,filter:d.filter,usage:!0,budget:bt(),log:d.log},h=u(e=>{tt(l,{...e,log:d.log})},"announce"),S=i.meta.front==="openai"&&g?.stream_options?.include_usage!==!0,O=u(e=>{i.meta.model=e.body.model??null,i.meta.upstream=e.upstream,e.state.native?i.source(null):i.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=ct(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"),U=u(async e=>{const t=l.endpoint(e.upstream),a=l.driver(t);return(a.count&&!(S&&e.state.stream)?await a.count({endpoint:t,request:e.body,transport:c,signal:d.signal,timeout:l.config.detectTimeout}):null)??dt(e.body)},"input"),m=u(async(e,t)=>{const a=w(e);return!a.input&&!a.output?null:{input_tokens:a.input?await U(e):null,output_tokens:a.output?ft(t):null}},"counts"),H=u((e,t,a,r,L)=>(i.finish(L),_(e,t,a,r)),"fail"),rt=u(async e=>{let t=e,a=await at(c,t,d.signal,G());const r=a.status,L=r===400&&[t.images,t.tools].some($=>$.sent&&$.retry),M=(r===400||r===422)&&t.json.sent&&t.json.retry;if(!a.ok&&(L||M)){const $=await z(a),v=ut($,r).message,f={};if(r===400&&t.images.sent&&t.images.retry&&ot.refused(v)?(s.add(t.images.key),f.images=!1):r===400&&t.tools.sent&&t.tools.retry&&it.refused(v)?f.tools=!1:M&&(f.jsonObject="instruction"),!("images"in f||"tools"in f||"jsonObject"in f))return{upstream:t,result:a,failure:$};const b={upstream:t,result:a,failure:$};if("images"in f||"tools"in f){const R="images"in f?"images":"tools";h({type:"fallback",kind:R,level:"warn",message:`Upstream ${t.upstream}: ${t.state.model} refused ${R==="images"?"images":"tools"} (${r}: "${String(v).trim().slice(0,500)}"); sent the request again without them.`,endpoint:t.upstream,model:t.body.model,status:r,reason:String(v).trim().slice(0,500),action:R==="images"?"text-only":"without-tools"})}if(t=await p(g,{...x,...f,endpoint:t.upstream,retry:!0}),O(t),a=await at(c,t,d.signal,G()),M&&"jsonObject"in f){if(!a.ok){try{await a.body?.cancel?.()}catch{}return O(b.upstream),b}y.add(b.upstream.json.key),h({type:"fallback",kind:"json_object",level:"warn",message:`Upstream ${b.upstream.upstream}: ${b.upstream.state.model} rejected the json_object schema emulation (${r}: "${String(v).trim().slice(0,500)}"); sent it as a system instruction instead. Set jsonObject: "instruction" on this upstream to skip the first attempt.`,endpoint:b.upstream.upstream,model:b.upstream.body.model,status:r,reason:String(v).trim().slice(0,500),action:"instruction"})}}return{upstream:t,result:a}},"attempt");let n=await p(g,{...x,hold:!0});const N=n.pool;let B=0;const D=N.length>1?l.config.pool:null,G=u(()=>D&&{connectTimeout:D.connectTimeout,headersTimeout:B+1<N.length?D.headersTimeout:0},"limitsNow");let q=l.endpoint(n.upstream);const K=u(()=>q.health.inflight--,"release");let o,T,Q=null,V=0;const W=u(e=>{const t=wt(l,{body:g,sent:n,front:i.meta.front,requested:i.meta.requested,headers:e,log:d.log,exchange:Q,attempt:V});return Q??=t.id,t},"begin"),X=u(async e=>{const t=W();if(!t.on)return;if(o===void 0)return t.attempt({error:T.message},e);let a=null;try{a=T??await z(o)}catch{}t.attempt({status:o.status,headers:Z.plain(o.headers),body:a},e)},"keep");try{for(;;){const e=q;e.health.requests++,V++,O(n);let t=null;try{({upstream:n,result:o,failure:T}=await rt(n)),!o.ok&&o.status>=500&&(t=`HTTP ${o.status}`,h({type:"error",kind:"upstream",level:"error",message:`Upstream ${e.name} answered HTTP ${o.status}`,endpoint:e.name,model:n.body.model,status:o.status}))}catch(r){if(!(r instanceof C)||r.code!=="upstream_unreachable")throw r;t=r.message,o=void 0,T=r,h({type:"error",kind:"transport",level:"error",message:r.message,endpoint:e.name,model:n.body.model})}if(t===null){et.succeeded(e);break}et.failed(l.config,e,t);let a=null;for(;a===null&&!d.signal?.aborted&&B+1<N.length;)try{a=await p(g,{...x,endpoint:N[++B],hold:!0})}catch(r){if(!(r instanceof C)||r.code!=="endpoint_not_in_pool")throw r}if(a===null){if(o===void 0)throw await X(),T;break}await X(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 o?.body?.cancel?.()}catch{}K(),q=l.endpoint(a.upstream),n=a}}catch(e){throw K(),e}const st=q;q=null,i.usage.then(()=>st.health.inflight--);const A=Z.plain(o.headers),J=W(A);if(!o.ok){const e=T??await z(o);return J.reply(o.status,A,e),H(o.status,e,A,n.state,"upstream_error")}if(n.state.stream){const e=j(J.raw(o.body),n.state),t=n.state.native?e:ht(i.meta.front,e,{gaps:u(()=>w(n),"gaps"),input:u(()=>U(n),"input"),report:u(a=>i.estimate(a),"report")});return{status:200,headers:kt,stream:J.events(i.track(t,d.signal,S))}}const P=await z(o);if(J.reply(o.status,A,P),P===null||typeof P!="object")return h({type:"error",kind:"upstream",level:"error",message:`Upstream ${n.upstream} returned a body that is not JSON`,endpoint:n.upstream,model:n.body.model,status:502}),H(502,"The upstream returned a body that is not JSON",A,n.state,"error");const E=await k(P,n.state),I=await m(n,pt(i.meta.front,E));return I&&(E.usage=gt(i.meta.front,E.usage,I),i.estimate(I)),i.json(E),i.finish("done"),{status:200,headers:{"content-type":"application/json"},body:E}}u(jt,"round");async function at(c,s,y,p=null){let k,j=!1,_=y;if(p&&p.headersTimeout>0){const l=new AbortController;_=y?AbortSignal.any([y,l.signal]):l.signal,k=setTimeout(()=>{j=!0,l.abort()},p.headersTimeout)}try{return await c(s.url,{method:s.method,headers:s.headers,body:JSON.stringify(s.body),signal:_,redirect:"error",...p&&p.connectTimeout>0?{connectTimeout:p.connectTimeout}:{}})}catch(l){if(y?.aborted)throw l;const g=j?`no response headers within ${p.headersTimeout} ms`:l.message;throw new C(`Could not reach the upstream (${s.upstream}): ${g}`,{code:"upstream_unreachable",status:502,details:{cause:l}})}finally{clearTimeout(k)}}u(at,"call");async function z(c){const s=await c.text();try{return JSON.parse(s)}catch{return s===""?null:s}}u(z,"parse");export{Ht as send};
|
|
2
2
|
//# sourceMappingURL=send.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var w=Object.defineProperty;var n=(o,t)=>w(o,"name",{value:t,configurable:!0});import{context as s,self as u}from"../../lib/runtime.mjs";import{TranslatorError as
|
|
1
|
+
var w=Object.defineProperty;var n=(o,t)=>w(o,"name",{value:t,configurable:!0});import{context as s,self as u}from"../../lib/runtime.mjs";import{TranslatorError as d}from"../../lib/errors.mjs";import{register as k}from"../../lib/routes.mjs";import{capture as x}from"../../lib/capture.mjs";import{resolve as y}from"../../lib/route.mjs";import{resolve as I}from"../../lib/settings.mjs";import{emit as T}from"../../lib/events.mjs";async function N(o,t={}){const{config:r}=s;if(t.front!==void 0&&t.front!=="anthropic"&&t.front!=="openai")throw new d(`front must be "anthropic" or "openai" (got ${JSON.stringify(t.front)})`,{code:"invalid_config",status:500});for(const e of["authorize","onUsage"])if(t[e]!==void 0&&typeof t[e]!="function")throw new d(`${e} must be a function`,{code:"invalid_config",status:500});const{send:m,route:p,token:h,model:c,error:v,sse:f,embeddings:b}=u,l=t.front??r.front,{state:g}=s,{events:i}=g;i.logger=o.log,o.addHook("onClose",async()=>{i.logger===o.log&&(i.logger=null)});const a=x(u,s,l);k(o,t,{front:l,filter:r.settings.passthroughFilter!==!1,interval:n(e=>{try{return a.settings(y(a,e).endpoint).pingInterval}catch{return I(r.settings).pingInterval}},"interval"),send:m,embed:b.send,route:p,count:h.count,list:c.list,get:c.get,emit:n(e=>T({config:r,state:g},e),"emit"),build:v.build,encode:f.encode,write:f.write})}n(N,"plugin");export{N as plugin};
|
|
2
2
|
//# sourceMappingURL=plugin.mjs.map
|
package/dist/lib/config.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var i=Object.defineProperty;var
|
|
1
|
+
var i=Object.defineProperty;var a=(t,r)=>i(t,"name",{value:r,configurable:!0});import{TranslatorError as s}from"./errors.mjs";import*as n from"./pool.mjs";import{valid as l}from"./record.mjs";import{normalizeAliases as u,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}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 s(`upstreams[${r}] needs a "url"`,{code:"INVALID_CONFIG",status:500});return{name:e.name??`upstream-${r}`,url:d(e.url),apiKey:e.apiKey,headers:{...e.headers},backend:e.backend??"auto",models:Array.isArray(e.models)?[...e.models]:void 0,default:e.default===!0,settings:{...e.settings},version:e.version,driver:null,facts:null,driverSettings:{},probed:{},health:n.health()}}a(f,"endpoint");function N(t={}){const r=t.front??"anthropic";if(r!=="anthropic"&&r!=="openai")throw new s(`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 s("at least one upstream is required (config.upstreams)",{code:"INVALID_CONFIG",status:500});const o=t.logs??!1;if(typeof o!="boolean"&&(typeof o!="string"||o===""))throw new s("logs must be true, false or a file path (got "+JSON.stringify(o)+")",{code:"INVALID_CONFIG",status:500});if(t.onEvent!==void 0&&typeof t.onEvent!="function")throw new s("onEvent must be a function",{code:"INVALID_CONFIG",status:500});if(!l(t.capture))throw new s("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:p(t.efforts),pool:n.normalize(t.pool),settings:{...t.settings},detect:t.detect!==!1,detectTimeout:t.detectTimeout??5e3,transport:t.transport,logs:o,capture:t.capture||!1,events:t.events!==!1,onEvent:t.onEvent}}a(N,"normalize");export{f as endpoint,N as normalize};
|
|
2
2
|
//# sourceMappingURL=config.mjs.map
|
package/dist/lib/http.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var h=Object.defineProperty;var s=(t,e)=>h(t,"name",{value:e,configurable:!0});function y(t,e){return t+e}s(y,"join");function k(t){return t==="localhost"||t==="::1"||/^127\./.test(t)}s(k,"isLoopback");function d(t,e="bearer",a){const r={...t.headers};return t.apiKey&&(e==="x-api-key"?r["x-api-key"]=t.apiKey:r.authorization=`Bearer ${t.apiKey}`),{...r,...a}}s(d,"headers");function b({endpoint:t,transport:e,signal:a,timeout:r,probes:u},f,i){const o=i!==void 0&&(i.auth==="x-api-key"||i.headers!==void 0),n=o?{auth:i.auth,extra:i.headers}:{},l=s(()=>p(e,y(t.url,f),{method:"GET",headers:d(t,n.auth,n.extra),signal:a,timeout:r}),"run");if(!u)return l();const c=o?JSON.stringify([f,n.auth,n.extra]):f;return u.has(c)||u.set(c,l()),u.get(c)}s(b,"probe");function x(t){const e={};if(!t)return e;if(typeof t.forEach=="function")return t.forEach((a,r)=>{e[r.toLowerCase()]=a}),e;for(const[a,r]of Object.entries(t))e[a.toLowerCase()]=String(r);return e}s(x,"plain");async function p(t,e,a={}){const{timeout:r,signal:u,body:f,...i}=a,o=[];u&&o.push(u),r>0&&o.push(AbortSignal.timeout(r));try{const n=await t(e,{...i,redirect:"error",body:f===void 0?void 0:JSON.stringify(f),signal:o.length>1?AbortSignal.any(o):o[0]}),l=await n.text();let c=null;try{c=JSON.parse(l)}catch{c=l===""?null:l}return{ok:n.ok,status:n.status,body:c,headers:x(n.headers)}}catch(n){return{ok:!1,status:0,body:null,error:n}}}s(p,"json");export{d as headers,k as isLoopback,y as join,p as json,x as plain,b as probe};
|
|
2
2
|
//# sourceMappingURL=http.mjs.map
|
package/dist/lib/ids.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var I=Object.defineProperty;var o=(t,e)=>I(t,"name",{value:e,configurable:!0});import{randomBytes as _,createHash as w}from"node:crypto";import{TranslatorError as y}from"./errors.mjs";function h(t=12){return _(t).toString("hex")}o(h,"random");function M(){return`msg_${h()}`}o(M,"messageId");function j(){return`toolu_${h()}`}o(j,"toolUseId");function S(){return`chatcmpl-${h()}`}o(S,"completionId");function T(){return`call_${h()}`}o(T,"callId");function z(t,e={}){return typeof t=="string"&&Object.hasOwn(e,t)?e[t]:typeof t!="string"||t===""?j():t.replace(/[^a-zA-Z0-9_-]/g,"_")}o(z,"anthropicToolId");function O(t){let e;try{e=new RegExp(`^[${t}]$`)}catch{return[]}const c=[];for(let n=33;n<=126;n++)e.test(String.fromCharCode(n))&&c.push(String.fromCharCode(n));return c}o(O,"alphabetOf");function E(t){if(t===null||typeof t!="object"||Array.isArray(t))return!1;const{alphabet:e,min:c,max:n}=t;if(typeof e!="string"||O(e).length===0)return!1;const f=o(s=>s===void 0||Number.isInteger(s)&&s>=1,"bound");return f(c)&&f(n)&&(c??1)<=(n??1/0)}o(E,"validToolIdPolicy");function m(t,e,c){let n="";for(let f=0;n.length<c;f++)for(const s of w("sha256").update(`${t}\0${f}`).digest()){if(n.length===c)break;n+=e[s%e.length]}return n}o(m,"hashed");function N({policy:t,ids:e,map:c={}}){const n=O(t.alphabet),f=new RegExp(`^[${t.alphabet}]+$`),s=t.min??1,u=t.max??1/0,i=Math.min(u,Math.max(s,16)),l=new Map,d=new Set,g=o(r=>f.test(r)&&r.length>=s&&r.length<=u,"conforms"),b=o(r=>{let a=r;if(!g(r)){let x=0;for(a=m(r,n,i);d.has(a);){if(++x>255)throw new y("Tool call ids cannot be mapped into the upstream's id format: no free id is left",{code:"invalid_request",status:400});a=m(`${r}\0${x}`,n,i)}c[a]=r}return d.add(a),l.set(r,a),a},"assign"),p=o(r=>typeof r=="string"?r:"","keyOf"),$=o(r=>l.get(p(r))??b(p(r)),"fit");for(const r of e){const a=p(r);g(a)&&!l.has(a)&&b(a)}for(const r of e)$(r);return $}o(N,"createIdFitter");function Z(t,e){return Object.hasOwn(t,e)?t[e]:e}o(Z,"restoreId");function F({max:t,map:e={}}){const c=new Map(Object.entries(e).map(([n,f])=>[f,n]));return n=>{if(n.length<=t&&/^[a-zA-Z0-9_-]+$/.test(n))return n;if(c.has(n))return c.get(n);const f=n.replace(/[^a-zA-Z0-9_-]/g,"_");let s=f;if(f.length>t||f===""){const i=w("sha256").update(n).digest("hex").slice(0,8);s=`${f.slice(0,t-9)}_${i}`}const u=s;for(let i=2;Object.hasOwn(e,s);i++)s=`${u.slice(0,t-4)}_${i}`;return e[s]=n,c.set(n,s),s}}o(F,"createNameFitter");function R(t,e){return Object.hasOwn(t,e)?t[e]:e}o(R,"restoreName");export{z as anthropicToolId,T as callId,S as completionId,N as createIdFitter,F as createNameFitter,M as messageId,Z as restoreId,R as restoreName,j as toolUseId,E as validToolIdPolicy};
|
|
2
2
|
//# sourceMappingURL=ids.mjs.map
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
var E=Object.defineProperty;var s=(t,a)=>E(t,"name",{value:a,configurable:!0});import{randomBytes as F}from"node:crypto";import{mkdirSync as O,writeFileSync as T}from"node:fs";import w from"node:path";import{items as v}from"./source.mjs";import{warn as $}from"./warn.mjs";const l=1e6,j=/key|token|secret|auth|cookie|password/i;function p(t){const a={};for(const[n,i]of Object.entries(t??{}))a[n.toLowerCase()]=j.test(n)?"[redacted]":i;return a}s(p,"redact");function L(t){return t===void 0||t===!1||typeof t=="function"||typeof t=="string"&&t!==""}s(L,"valid");const C={on:!1,id:null,raw:s(t=>t,"raw"),events:s(t=>t,"events"),reply(){},attempt(){}};function M({config:t,state:a},n){const i=t.capture;if(!i)return C;const{sent:f}=n,u=F(6).toString("hex"),g=Date.now(),c={time:g,id:u,exchange:n.exchange??u,attempt:n.attempt,front:n.front,requested:n.requested,model:f.body?.model??null,endpoint:f.upstream,stream:f.state.stream,client:{body:n.body},upstream:{url:f.url,headers:p(f.headers),body:f.body},response:{}};let d="",S=!1,m=0,y=!1;const b=[],h=s(()=>{try{if(typeof i=="function"){const e=i(c);typeof e?.then=="function"&&e.then(void 0,r=>k(r))}else O(w.resolve(i),{recursive:!0}),T(w.join(w.resolve(i),`${g}-${u}.json`),`${JSON.stringify(c,null,2)}
|
|
2
|
+
`)}catch(e){k(e)}},"emit"),k=s(e=>{$(t,a,`Could not capture the exchange (${e instanceof Error?e.message:String(e)}); the reply is not affected`,{type:"warning",kind:"capture",log:n.log})},"problem");return{on:!0,id:u,raw(e){return(async function*(){const r=new TextDecoder;for await(const o of v(e))S||(d+=typeof o=="string"?o:r.decode(o,{stream:!0}),d.length>l&&(d=d.slice(0,l),S=!0,y=!0)),yield o})()},events(e){return(async function*(){try{for await(const r of v(e)){if(m<=l){const o=JSON.stringify(r).length;m+=o,m>l?y=!0:b.push(r)}yield r}}finally{c.response={status:200,headers:p(n.headers),raw:d,events:b,truncated:y},h()}})()},reply(e,r,o){c.response={status:e,headers:p(r),body:o},h()},attempt(e,r){c.response=e.headers?{...e,headers:p(e.headers)}:e,r!==void 0&&(c.failover=r),h()}}}s(M,"start");export{l as STREAM_LIMIT,p as redact,M as start,L as valid};
|
|
3
|
+
//# sourceMappingURL=record.mjs.map
|