@cldmv/llm-translator 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -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 +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/lmstudio/detect.mjs +2 -0
- package/dist/api/driver/lmstudio/info.mjs +2 -0
- package/dist/api/driver/lmstudio/request.mjs +2 -0
- package/dist/api/driver/lmstudio/settings.mjs +2 -0
- package/dist/api/driver/mistral/chunk.mjs +2 -0
- package/dist/api/driver/mistral/detect.mjs +2 -0
- package/dist/api/driver/mistral/info.mjs +2 -0
- package/dist/api/driver/mistral/request.mjs +2 -0
- package/dist/api/driver/mistral/response.mjs +2 -0
- package/dist/api/driver/mistral/settings.mjs +2 -0
- package/dist/api/embeddings/send.mjs +1 -1
- package/dist/api/request.mjs +2 -2
- package/dist/api/send.mjs +1 -1
- package/dist/lib/backends.mjs +1 -1
- package/dist/lib/fanout.mjs +2 -0
- package/dist/lib/finish.mjs +1 -1
- package/dist/lib/http.mjs +1 -1
- package/dist/lib/ids.mjs +1 -1
- package/dist/lib/record.mjs +2 -2
- package/dist/lib/routes.mjs +1 -1
- package/dist/lib/typed.mjs +2 -0
- package/dist/lib/usage.mjs +1 -1
- 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 +9 -2
- package/types/src/api/driver/anthropic/detect.d.mts +10 -23
- package/types/src/api/driver/anthropic/info.d.mts +3 -1
- package/types/src/api/driver/lmstudio/detect.d.mts +6 -0
- package/types/src/api/driver/lmstudio/info.d.mts +31 -0
- package/types/src/api/driver/lmstudio/request.d.mts +6 -0
- package/types/src/api/driver/lmstudio/settings.d.mts +23 -0
- package/types/src/api/driver/mistral/chunk.d.mts +8 -0
- package/types/src/api/driver/mistral/detect.d.mts +26 -0
- package/types/src/api/driver/mistral/info.d.mts +35 -0
- package/types/src/api/driver/mistral/request.d.mts +6 -0
- package/types/src/api/driver/mistral/response.d.mts +7 -0
- package/types/src/api/driver/mistral/settings.d.mts +24 -0
- package/types/src/api/embeddings/send.d.mts +10 -2
- package/types/src/api/send.d.mts +7 -0
- package/types/src/lib/backends.d.mts +7 -0
- package/types/src/lib/fanout.d.mts +68 -0
- package/types/src/lib/finish.d.mts +14 -3
- 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 +7 -2
- package/types/src/lib/typed.d.mts +10 -0
- package/types/src/lib/usage.d.mts +20 -0
package/README.md
CHANGED
|
@@ -17,18 +17,18 @@ Use it as a library when you want the conversion without any I/O (`request`, `re
|
|
|
17
17
|
|
|
18
18
|
## ✨ What's New
|
|
19
19
|
|
|
20
|
-
### Latest: v0.
|
|
20
|
+
### Latest: v0.8.0 (November 2026)
|
|
21
21
|
|
|
22
|
-
- **
|
|
23
|
-
-
|
|
24
|
-
- [View full v0.
|
|
22
|
+
- **Mistral and LM Studio drivers** — api.mistral.ai is matched by host and gets 9-character tool-call ids, `random_seed`, `tool_choice: "any"` and flattened reasoning chunks. LM Studio is recognised from its own `/api/v0/models` listing, and its Anthropic-compatible endpoint is one `backend: "anthropic"` away.
|
|
23
|
+
- **`n` above 1 and refusal categories** — an OpenAI client's `n` above 1 sent to an Anthropic-protocol upstream is answered by `n` parallel requests, combined into one reply or stream with summed usage. A refusal gets `stop_details.category: "general_harms"` when an Azure-style content filter flags hate, sexual, violence or self-harm.
|
|
24
|
+
- [View full v0.8.0 Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.8.0.md)
|
|
25
25
|
|
|
26
26
|
### Recent Releases
|
|
27
27
|
|
|
28
|
+
- **v0.7.0** (November 2026) — Anthropic-protocol servers detected by probe on any host, tool-call ids rewritten for strict upstream formats, embeddings usage through `onUsage`, and capture records for every failed-over attempt ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.7.0.md))
|
|
29
|
+
- **v0.6.0** (November 2026) — `POST /v1/embeddings` pass-through, sampling extensions per upstream and per request, `strict` tool use end to end with a schema check, and the `capture` option for debugging ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.6.0.md))
|
|
28
30
|
- **v0.5.0** (November 2026) — everything the library logs is available as events, logging is opt-in through `logs`, `json_object` reaches Anthropic upstreams as a schema with a fallback, and refusals carry `stop_details` ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.5.0.md))
|
|
29
31
|
- **v0.4.0** (November 2026) — per-model effort levels and aliases that pin effort and thinking, non-standard finish reasons, a configurable tool-error marker, per-upstream `pingInterval` and a Node.js 22.15 floor ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.4.0.md))
|
|
30
|
-
- **v0.3.1** (November 2026) — patched the dev-only `minimatch` and `katex` lint dependencies and corrected the v0.3.0 issue links; no runtime changes ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.3.1.md))
|
|
31
|
-
- **v0.3.0** (November 2026) — usage reporting from `send()`, the `route()` leaf and the server `authorize` hook; drivers for llama.cpp, Ollama, SGLang and the DeepSeek API, endpoint pools, `reasoningRoundTrip` and `toolSearch`; requires Node.js 24 ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.3.0.md))
|
|
32
32
|
|
|
33
33
|
📚 **For complete version history and detailed release notes, see the [docs/changelog/](https://github.com/CLDMV/llm-translator/tree/master/docs/changelog/) folder.**
|
|
34
34
|
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
var N=Object.defineProperty;var
|
|
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 p=(o,t)=>_(o,"name",{value:t,configurable:!0});import{resolve as k}from"../../../../../../../lib/settings.mjs";import{TranslatorError as u}from"../../../../../../../lib/errors.mjs";import{messageId as T,anthropicToolId as x,restoreName as A}from"../../../../../../../lib/ids.mjs";import{sign as w,reasoningOf as C,reasoningKey as I,detailsText as N,splitThink as O}from"../../../../../../../lib/thinking.mjs";import{isComplete as S,parseArguments as U}from"../../../../../../../lib/arguments.mjs";import{filterCategory as v,finishError as E,toAnthropicStop as b,toAnthropicUsage as j}from"../../../../../../../lib/finish.mjs";function G(o,t,m){const c=k(m),i=o?.choices?.[0];if(!i)throw new u("Upstream response contained no choices",{code:"invalid_upstream_response",status:502});const g=E(i.finish_reason);if(g)throw new u(g.message,{code:g.code,status:g.status,type:g.type});const e=i.message??{},a=[];let l=C(e),s=typeof e.content=="string"?e.content:"";const n={};if(c.reasoningRoundTrip){const r=I(e);r&&(n[r]=e[r]),Array.isArray(e.reasoning_details)&&e.reasoning_details.length>0&&(n.reasoning_details=e.reasoning_details)}l===""&&c.thinkTags&&({reasoning:l,text:s}=O(s,c.thinkOpen)),l===""&&n.reasoning_details&&(l=N(n.reasoning_details)),(l!==""||n.reasoning_details)&&(a.push({type:"thinking",thinking:l,signature:w(Object.keys(n).length>0?n:void 0,t?.producer)}),s=s.trimStart()),s===""&&typeof e.refusal=="string"&&(s=e.refusal),s!==""&&a.push({type:"text",text:s});const d=t?.toolNames??{},h=t?.toolIds??{},f=e.tool_calls??[];for(const r of f)a.push({type:"tool_use",id:x(r.id,h),name:A(d,r.function.name),input:U(r.function.arguments)});a.length===0&&a.push({type:"text",text:""});const y=b(i.finish_reason,{hasToolCalls:f.length>0,matchedStop:i.stop_reason,refusal:e.refusal,category:v(i.content_filter_results),argumentsComplete:f.length===0||S(f.at(-1).function.arguments)});return t&&o.usage&&(t.upstreamUsage=o.usage),{id:T(),type:"message",role:"assistant",model:t?.model??o.model,content:a,...y,usage:j(o.usage)}}p(G,"response");export{G as response};
|
|
2
2
|
//# sourceMappingURL=response.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var Q=Object.defineProperty;var s=(b,r)=>Q(b,"name",{value:r,configurable:!0});import{resolve as V}from"../../../../../../../lib/settings.mjs";import{TranslatorError as W,openaiToAnthropicType as X,rewriteContextOverflow as Y}from"../../../../../../../lib/errors.mjs";import{isComplete as Z}from"../../../../../../../lib/arguments.mjs";import{messageId as $,anthropicToolId as ee,restoreName as te}from"../../../../../../../lib/ids.mjs";import{sign as ne,reasoningOf as ie,reasoningKey as oe,detailsText as le,mergeDetail as se,createThinkSplitter as re}from"../../../../../../../lib/thinking.mjs";import{filterCategory as ae,finishError as de,toAnthropicStop as fe,toAnthropicUsage as B}from"../../../../../../../lib/finish.mjs";import{messages as ce}from"../../../../../../../lib/sse.mjs";async function*xe(b,r,F){const u=V(F),J=r?.toolNames??{},K=r?.toolIds??{},d=u.thinkTags?re(u.thinkOpen):null,c=new Map;let m=!1,j=0,i=null,N=null,k=null,O=null,h="",T=null,v=!1,A=!1,I=0,w=!1,U=!1,E=!1;const S=[],l=s((e,t)=>({event:e,data:{type:e,...t}}),"event"),p=s((e,t)=>l("error",{error:{type:e,message:t}}),"fail"),M=s(e=>{const t={};return u.reasoningRoundTrip&&(e.key!==null&&(t[e.key]=e.text),e.details.length>0&&(t.reasoning_details=e.details)),Object.keys(t).length>0?t:void 0},"carried");function*y(){if(i!==null){i.kind==="thinking"?yield l("content_block_delta",{index:i.index,delta:{type:"signature_delta",signature:ne(M(i.carry),r?.producer)}}):i.kind==="tool"&&(i.entry.state="closed",i.entry.sent||(yield l("content_block_delta",{index:i.index,delta:{type:"input_json_delta",partial_json:"{}"}}))),yield l("content_block_stop",{index:i.index}),i=null;for(const e of c.values()){if(e.state!=="pending")continue;e.state="closed";const t=j++,n=e.args.join("");yield l("content_block_start",{index:t,content_block:e.block}),yield l("content_block_delta",{index:t,delta:{type:"input_json_delta",partial_json:n===""?"{}":n}}),yield l("content_block_stop",{index:t})}}}s(y,"close");function*G(){for(const e of R()){if(e.state!=="buffered")continue;yield*g("tool",e.block,e);const t=e.args.join("");t!==""&&(e.sent=!0,yield l("content_block_delta",{index:i.index,delta:{type:"input_json_delta",partial_json:t}})),yield*y()}}s(G,"flushBuffered");function R(){return[...c.entries()].sort((e,t)=>e[0]-t[0]).map(e=>e[1])}s(R,"ordered");function*g(e,t,n){yield*y(),i={kind:e,index:j++,entry:n,carry:{key:null,text:"",details:[]}},yield l("content_block_start",{index:i.index,content_block:t})}s(g,"begin");function*a(e,t,n=null){if(e==="text"&&E&&(t=t.trimStart()),t==="")return;if(E=e==="thinking",e==="text"&&(v=!0),i?.kind==="tool"){S.push({kind:e,text:t,key:n});return}i?.kind!==e&&(yield*g(e,e==="text"?{type:"text",text:""}:{type:"thinking",thinking:"",signature:""})),n!==null&&(i.carry.key??=n,i.carry.text+=t);const o=e==="text"?{type:"text_delta",text:t}:{type:"thinking_delta",thinking:t};yield l("content_block_delta",{index:i.index,delta:o})}s(a,"piece");function*z(e){if(i?.kind==="tool"){S.push({kind:"details",items:e});return}i?.kind!=="thinking"&&(yield*g("thinking",{type:"thinking",thinking:"",signature:""}));for(const t of e)se(i.carry.details,t);i.carry.key===null&&(yield*a("thinking",le(e)))}s(z,"details");function*D(e){yield*a("thinking",e.reasoning),yield*a("text",e.text)}s(D,"pieces");function*H(e){let t=e.index;t===void 0&&(t=e.id===void 0||e.id===null?I:c.size),I=t;let n=c.get(t);if(n===void 0){const x=e.function?.name??"",_={type:"tool_use",id:ee(e.id,K),name:te(J,x),input:{}};d?.pending()&&(U=!0);const f=u.toolStreaming==="buffer"||U;n={state:f?"buffered":"pending",sent:!1,args:[],block:_},c.set(t,n),!f&&i?.kind!=="tool"&&(n.state="open",yield*g("tool",_,n))}let o=e.function?.arguments;o!==null&&typeof o=="object"&&(o=JSON.stringify(o)),!(o==null||o==="")&&(n.args.push(o),n.state==="open"&&(n.sent=!0,yield l("content_block_delta",{index:i.index,delta:{type:"input_json_delta",partial_json:o}})))}s(H,"toolCall");try{for await(const e of ce(b)){if(e.data==="[DONE]"){A=!0;break}const t=e.data;if(t.error){const f=t.error.message??"Upstream error";yield p(X(t.error),Y(f)??f);return}m||(m=!0,yield l("message_start",{message:{id:$(),type:"message",role:"assistant",model:r?.model??t.model,content:[],stop_reason:null,stop_sequence:null,usage:B(t.usage)}})),t.usage&&(N=t.usage,r&&(r.upstreamUsage=t.usage));const n=t.choices?.[0];if(!n)continue;const o=n.delta??{},x=ie(o);let _="";x!==""&&!w&&(w=!0,d&&(_=d.release().text)),yield*a("thinking",x,oe(o)),yield*a("text",_),u.reasoningRoundTrip&&Array.isArray(o.reasoning_details)&&o.reasoning_details.length>0&&(yield*z(o.reasoning_details)),typeof o.content=="string"&&o.content!==""&&(d&&!w?yield*D(d.push(o.content)):yield*a("text",o.content)),typeof o.refusal=="string"&&(h+=o.refusal),T=ae(n.content_filter_results)??T;for(const f of o.tool_calls??[])yield*H(f);n.finish_reason&&(k=n.finish_reason),typeof n.stop_reason=="string"&&(O=n.stop_reason)}}catch(e){yield e instanceof W?p(e.type,e.message):p("api_error",e.message);return}if(!m||k===null&&!A){yield p("api_error",m?"Upstream stream ended before completion":"Upstream stream ended without data");return}const C=de(k);if(C){yield p(C.type,C.message);return}d&&(yield*D(d.end())),yield*G();const q=R().at(-1),L=q===void 0||Z(q.args.join(""));yield*y();for(const e of S)yield*e.kind==="details"?z(e.items):a(e.kind,e.text,e.key);!v&&h!==""&&(yield*a("text",h)),yield*y(),j===0&&(yield*g("text",{type:"text",text:""}),yield*y());const P=fe(k,{hasToolCalls:c.size>0,matchedStop:O,argumentsComplete:L,refusal:h,category:T});yield l("message_delta",{delta:P,usage:B(N)}),yield l("message_stop",{})}s(xe,"stream");export{xe as stream};
|
|
2
2
|
//# sourceMappingURL=stream.mjs.map
|
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 t=Object.defineProperty;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
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var i=Object.defineProperty;var a=(o,e)=>i(o,"name",{value:e,configurable:!0});import*as r from"../../../lib/http.mjs";import{isLmstudioModels as s,objectOf as m}from"../../../lib/backends.mjs";async function p(o){const e=m(await r.probe(o,"/api/v0/models"));if(!s(e))return null;const d=e.data.filter(t=>t.type!=="embeddings");return{models:d.map(t=>t.id),loaded:d.filter(t=>t.state==="loaded").map(t=>t.id)}}a(p,"detect");export{p as detect};
|
|
2
|
+
//# sourceMappingURL=detect.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var a=Object.defineProperty;var o=(e,t)=>a(e,"name",{value:t,configurable:!0});function i(){return{name:"lmstudio",protocol:"openai.chat",order:55,auth:"bearer",strict:!1,probes:["/api/v0/models"],paths:{chat:"/v1/chat/completions",models:"/v1/models",listing:"/api/v0/models"}}}o(i,"info");export{i as info};
|
|
2
|
+
//# sourceMappingURL=info.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var s=Object.defineProperty;var e=(n,t)=>s(n,"name",{value:t,configurable:!0});function i(){return{thinking:"none",thinkingBudget:!1,effort:!1,topK:!1,prefill:"none",streamUsage:!0,continuousUsage:!1}}e(i,"settings");export{i as settings};
|
|
2
|
+
//# sourceMappingURL=settings.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=Object.defineProperty;var i=(t,n)=>r(t,"name",{value:n,configurable:!0});import{flatten as s}from"../../../lib/typed.mjs";function l(t){const n=s(t?.choices?.[0]?.delta?.content);if(!n)return t;const[e,...c]=t.choices,o={...e.delta,content:n.text};return n.thinking!==""&&(o.reasoning_content=n.thinking),{...t,choices:[{...e,delta:o},...c]}}i(l,"chunk");export{l as chunk};
|
|
2
|
+
//# sourceMappingURL=chunk.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var e=Object.defineProperty;var a=(t,o)=>e(t,"name",{value:o,configurable:!0});function r(){return{name:"mistral",protocol:"openai.chat",order:36,auth:"bearer",strict:!1,toolCallIds:{alphabet:"a-zA-Z0-9",min:9,max:9},paths:{chat:"/v1/chat/completions",models:"/v1/models"}}}a(r,"info");export{r as info};
|
|
2
|
+
//# sourceMappingURL=info.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var n=Object.defineProperty;var d=(o,e)=>n(o,"name",{value:e,configurable:!0});import{noEmptyTools as t}from"../../../lib/normalize.mjs";function s(o){const e={...t(o)};return e.seed!==void 0&&(e.random_seed===void 0&&(e.random_seed=e.seed),delete e.seed),e.tool_choice==="required"&&(e.tool_choice="any"),e}d(s,"request");export{s as request};
|
|
2
|
+
//# sourceMappingURL=request.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var i=Object.defineProperty;var s=(n,t)=>i(n,"name",{value:t,configurable:!0});import{flatten as a}from"../../../lib/typed.mjs";function f(n){return Array.isArray(n?.choices)?{...n,choices:n.choices.map(t=>{const e=a(t?.message?.content);if(!e)return t;const r={...t.message,content:e.text};return e.thinking!==""&&(r.reasoning_content=e.thinking),{...t,message:r}})}:n}s(f,"response");export{f as response};
|
|
2
|
+
//# sourceMappingURL=response.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var s=Object.defineProperty;var e=(n,t)=>s(n,"name",{value:t,configurable:!0});function i(){return{reasoningField:"reasoning_content",maxTokensField:"max_tokens",thinking:"none",thinkingBudget:!1,effort:!1,topK:!1,prefill:"none",streamUsage:!0,continuousUsage:!1}}e(i,"settings");export{i as settings};
|
|
2
|
+
//# sourceMappingURL=settings.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var x=Object.defineProperty;var h=(e,s)=>x(e,"name",{value:s,configurable:!0});import{context as q,self as w}from"../../lib/runtime.mjs";import{capture as H}from"../../lib/capture.mjs";import{resolve as S,serves as J}from"../../lib/route.mjs";import*as y from"../../lib/http.mjs";import*as A from"../../lib/pool.mjs";import{emit as N}from"../../lib/events.mjs";import{meter as U}from"../../lib/usage.mjs";import{warn as C}from"../../lib/warn.mjs";import{start as I}from"../../lib/record.mjs";const M="/v1/embeddings";async function Q(e,s={}){const{transport:$}=q.state,{error:g}=w,a=H(w,q,"openai"),O=e&&typeof e=="object"&&!Array.isArray(e),l=U({model:null,requested:O&&typeof e.model=="string"?e.model:null,upstream:null,front:"openai",stream:!1},s.onUsage,(o,t)=>C(a.config,a.state,o,{type:"usage",...t,log:s.log})),u=h((o,t)=>(l.finish(t),Object.defineProperty(o,"usage",{value:l.usage,enumerable:!1,configurable:!0})),"end"),n=h((o,t,f={})=>u(g.build(o,t,{...f,front:"openai"}),o===499?"aborted":"error"),"fail");if(!e||typeof e!="object"||Array.isArray(e))return n(400,"The request body must be a JSON object",{code:"invalid_body"});if(typeof e.model!="string"||e.model==="")return n(400,"model is required",{code:"invalid_body",param:"model"});const P=Object.hasOwn(a.config.aliases,e.model)?a.config.aliases[e.model]:e.model;if(!a.state.endpoints.some(o=>J(o,P)))return n(404,`The model "${e.model}" is not served by any configured upstream.`,{code:"model_not_found",param:"model"});const c=S(a,e,{advance:!0}),p=c.pool.filter(o=>o.protocol==="openai.chat");if(p.length===0)return n(400,`The model "${e.model}" is served by an Anthropic-protocol upstream, which has no embeddings API.`,{code:"embeddings_unsupported",param:"model"});const v={...e,model:c.model};l.meta.model=c.model;let m,j=null;for(let o=0;o<p.length;o++){const t=p[o];l.meta.upstream=t.name;const f=a.driver(t);t.health.inflight++,t.health.requests++;const T=y.join(t.url,M),_={"content-type":"application/json",accept:"application/json",...f.info.headers,...y.headers(t,f.info.auth,s.headers)},b=I(a,{body:e,sent:{upstream:t.name,url:T,headers:_,body:v,state:{stream:!1}},front:"openai",requested:l.meta.requested,log:s.log,exchange:j,attempt:o+1});j??=b.id;let r;try{r=await y.json($,T,{method:"POST",headers:_,body:v,signal:s.signal})}finally{t.health.inflight--}if(s.signal?.aborted)return n(499,"The request was aborted",{code:"aborted"});const d=r.status===0;if(!d&&r.status<500)return A.succeeded(t),b.reply(r.status,r.headers,r.body),r.ok?r.body===null||typeof r.body!="object"?n(502,"The upstream returned a body that is not JSON",{code:"bad_gateway"}):(l.json(r.body),u({status:200,headers:{"content-type":"application/json"},body:r.body},"done")):u(g(r.status,r.body,{},{front:"openai",upstream:t.name,native:!0,model:e.model}),"upstream_error");const i=d?`Could not reach the upstream (${t.name}): ${r.error?.message}`:`HTTP ${r.status}`;A.failed(a.config,t,i);const k=d?{error:i}:{status:r.status,headers:r.headers,body:r.body};b.attempt(k,o+1<p.length?i:void 0),N(a,{type:"error",kind:d?"transport":"upstream",level:"error",message:d?i:`Upstream ${t.name} answered HTTP ${r.status}`,endpoint:t.name,model:c.model,log:s.log}),m={endpoint:t,result:r,reason:i}}return m.result.status===0?n(502,m.reason,{code:"upstream_unreachable"}):u(g(m.result.status,m.result.body,{},{front:"openai",upstream:m.endpoint.name,native:!0,model:e.model}),"upstream_error")}h(Q,"send");export{Q as send};
|
|
2
2
|
//# sourceMappingURL=send.mjs.map
|
package/dist/api/request.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var T=Object.defineProperty;var p=(e,n)=>T(e,"name",{value:n,configurable:!0});import{context as
|
|
2
|
-
${f}`,w=r.images===!1||e.state.textOnly.has(h)&&e.settings(t).imageFallback,$=e.settings(t),x=r.jsonObject==="instruction"||e.state.jsonInstruction.has(h)&&$.jsonObjectFallback,m={...w?{...$,images:!1}:$,...x?{jsonObject:"instruction"}:{},...u?.effort===void 0?{}:{effort:!0}},v=n.stream===!0;let o,s;i?({body:o,state:s}=await i.request(n,m,{producer:G(t),budget:r.budget,listed:d!==void 0}),s.toolSearch?.capped&&b(e.config,e.state,`Tool search: more than ${
|
|
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 it=Object.defineProperty;var u=(c,r)=>it(c,"name",{value:r,configurable:!0});import{context as G,self as Z}from"../lib/runtime.mjs";import{TranslatorError as z}from"../lib/errors.mjs";import*as tt from"../lib/http.mjs";import*as ut from"../lib/images.mjs";import*as lt from"../lib/tools.mjs";import{parse as ct}from"../lib/upstream-error.mjs";import{meter as mt,normalize as dt}from"../lib/usage.mjs";import{capture as ft}from"../lib/capture.mjs";import{estimate as pt,bodyLength as gt,output as ht,patch as yt,stream as wt}from"../lib/estimate.mjs";import{warn as et}from"../lib/warn.mjs";import{start as bt}from"../lib/record.mjs";import{emit as at}from"../lib/events.mjs";import*as rt from"../lib/pool.mjs";import{createBudget as kt}from"../lib/thinking.mjs";import*as nt from"../lib/fanout.mjs";const jt={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"};async function _t(c,r={}){const{transport:y,textOnly:f,jsonInstruction:k}=G.state,{request:j,response:_,stream:m,error:g}=Z,d=g.build,{config:o,state:T}=G,h=ft(Z,G,r.front),O=c&&typeof c=="object"&&!Array.isArray(c),U={model:null,requested:O&&typeof c.model=="string"?c.model:null,upstream:null,front:r.front??o.front,stream:O&&c.stream===!0},w=mt(U,r.onUsage,(l,A)=>et(o,T,l,{type:"usage",...A,log:r.log})),q=u(l=>Object.defineProperty(l,"usage",{value:w.usage,enumerable:!1,configurable:!0}),"attach");let J=!1;try{const l=nt.wants(h,c);if(l>0){J=!0;const A=u((s,$)=>et(o,T,s,{type:"usage",...$,log:r.log}),"warning");return await nt.run({body:c,count:l,options:r,send:_t,warn:A})}return q(await Tt({transport:y,textOnly:f,jsonInstruction:k,request:j,response:_,stream:m,error:g,captured:h},c,r,w))}catch(l){if(J)throw l;if(!(l instanceof z))throw r.signal?.aborted||at(h,{type:"error",kind:"exception",level:"error",message:String(l?.message??l),log:r.log}),w.finish(r.signal?.aborted?"aborted":"error"),l;return w.finish("error"),q(d(l.status,l.message,{type:l.type,code:l.code,param:l.param,front:r.front}))}}u(_t,"send");async function Tt({transport:c,textOnly:r,jsonInstruction:y,request:f,response:k,stream:j,error:_,captured:m},g,d,o){const T={front:d.front,headers:d.headers,clientHeaders:d.clientHeaders,filter:d.filter,usage:!0,budget:kt(),log:d.log},h=u(e=>{at(m,{...e,log:d.log})},"announce"),O=o.meta.front==="openai"&&g?.stream_options?.include_usage!==!0,U=u(e=>{o.meta.model=e.body.model??null,o.meta.upstream=e.upstream,e.state.native?o.source(null):o.source(()=>e.state.convert?.upstreamUsage,e.state.front==="anthropic"?"openai":"folded")},"note"),w=u(e=>{if(e.state.native)return{input:!1,output:!1};const t=dt(e.state.front==="anthropic"?"openai":"folded",e.state.convert?.upstreamUsage);return t===null?{input:!0,output:!0}:{input:t.input_tokens===null&&t.cache_read_input_tokens===null&&t.cache_creation_input_tokens===null,output:t.output_tokens===null}},"gaps"),q=u(async e=>{const t=m.endpoint(e.upstream),a=m.driver(t);return(a.count&&!(O&&e.state.stream)?await a.count({endpoint:t,request:e.body,transport:c,signal:d.signal,timeout:m.config.detectTimeout}):null)??pt(e.body)},"input"),J=u(async(e,t)=>{const a=w(e);return!a.input&&!a.output?null:{input_tokens:a.input?await q(e):null,output_tokens:a.output?ht(t):null}},"counts"),l=u((e,t,a,n,M)=>(o.finish(M),_(e,t,a,n)),"fail"),A=u(async e=>{let t=e,a=await st(c,t,d.signal,K());const n=a.status,M=n===400&&[t.images,t.tools].some(x=>x.sent&&x.retry),R=(n===400||n===422)&&t.json.sent&&t.json.retry;if(!a.ok&&(M||R)){const x=await B(a),S=ct(x,n).message,p={};if(n===400&&t.images.sent&&t.images.retry&&ut.refused(S)?(r.add(t.images.key),p.images=!1):n===400&&t.tools.sent&&t.tools.retry&<.refused(S)?p.tools=!1:R&&(p.jsonObject="instruction"),!("images"in p||"tools"in p||"jsonObject"in p))return{upstream:t,result:a,failure:x};const b={upstream:t,result:a,failure:x};if("images"in p||"tools"in p){const F="images"in p?"images":"tools";h({type:"fallback",kind:F,level:"warn",message:`Upstream ${t.upstream}: ${t.state.model} refused ${F==="images"?"images":"tools"} (${n}: "${String(S).trim().slice(0,500)}"); sent the request again without them.`,endpoint:t.upstream,model:t.body.model,status:n,reason:String(S).trim().slice(0,500),action:F==="images"?"text-only":"without-tools"})}if(t=await f(g,{...T,...p,endpoint:t.upstream,retry:!0}),U(t),a=await st(c,t,d.signal,K()),R&&"jsonObject"in p){if(!a.ok){try{await a.body?.cancel?.()}catch{}return U(b.upstream),b}y.add(b.upstream.json.key),h({type:"fallback",kind:"json_object",level:"warn",message:`Upstream ${b.upstream.upstream}: ${b.upstream.state.model} rejected the json_object schema emulation (${n}: "${String(S).trim().slice(0,500)}"); sent it as a system instruction instead. Set jsonObject: "instruction" on this upstream to skip the first attempt.`,endpoint:b.upstream.upstream,model:b.upstream.body.model,status:n,reason:String(S).trim().slice(0,500),action:"instruction"})}}return{upstream:t,result:a}},"attempt");let s=await f(g,{...T,hold:!0});const $=s.pool;let D=0;const I=$.length>1?m.config.pool:null,K=u(()=>I&&{connectTimeout:I.connectTimeout,headersTimeout:D+1<$.length?I.headersTimeout:0},"limitsNow");let E=m.endpoint(s.upstream);const Q=u(()=>E.health.inflight--,"release");let i,v,V=null,W=0;const X=u(e=>{const t=bt(m,{body:g,sent:s,front:o.meta.front,requested:o.meta.requested,headers:e,log:d.log,exchange:V,attempt:W});return V??=t.id,t},"begin"),Y=u(async e=>{const t=X();if(!t.on)return;if(i===void 0)return t.attempt({error:v.message},e);let a=null;try{a=v??await B(i)}catch{}t.attempt({status:i.status,headers:tt.plain(i.headers),body:a},e)},"keep");try{for(;;){const e=E;e.health.requests++,W++,U(s);let t=null;try{({upstream:s,result:i,failure:v}=await A(s)),!i.ok&&i.status>=500&&(t=`HTTP ${i.status}`,h({type:"error",kind:"upstream",level:"error",message:`Upstream ${e.name} answered HTTP ${i.status}`,endpoint:e.name,model:s.body.model,status:i.status}))}catch(n){if(!(n instanceof z)||n.code!=="upstream_unreachable")throw n;t=n.message,i=void 0,v=n,h({type:"error",kind:"transport",level:"error",message:n.message,endpoint:e.name,model:s.body.model})}if(t===null){rt.succeeded(e);break}rt.failed(m.config,e,t);let a=null;for(;a===null&&!d.signal?.aborted&&D+1<$.length;)try{a=await f(g,{...T,endpoint:$[++D],hold:!0})}catch(n){if(!(n instanceof z)||n.code!=="endpoint_not_in_pool")throw n}if(a===null){if(i===void 0)throw await Y(),v;break}await Y(t),h({type:"failover",level:"warn",message:`Upstream ${e.name} failed (${t}); trying ${a.upstream}`,from:e.name,to:a.upstream,model:a.body.model,reason:t});try{await i?.body?.cancel?.()}catch{}Q(),E=m.endpoint(a.upstream),s=a}}catch(e){throw Q(),e}const ot=E;E=null,o.usage.then(()=>ot.health.inflight--);const H=tt.plain(i.headers),P=X(H);if(!i.ok){const e=v??await B(i);return P.reply(i.status,H,e),l(i.status,e,H,s.state,"upstream_error")}if(s.state.stream){const e=j(P.raw(i.body),s.state),t=s.state.native?e:wt(o.meta.front,e,{gaps:u(()=>w(s),"gaps"),input:u(()=>q(s),"input"),report:u(a=>o.estimate(a),"report")});return{status:200,headers:jt,stream:P.events(o.track(t,d.signal,O))}}const C=await B(i);if(P.reply(i.status,H,C),C===null||typeof C!="object")return h({type:"error",kind:"upstream",level:"error",message:`Upstream ${s.upstream} returned a body that is not JSON`,endpoint:s.upstream,model:s.body.model,status:502}),l(502,"The upstream returned a body that is not JSON",H,s.state,"error");const N=await k(C,s.state),L=await J(s,gt(o.meta.front,N));return L&&(N.usage=yt(o.meta.front,N.usage,L),o.estimate(L)),o.json(N),o.finish("done"),{status:200,headers:{"content-type":"application/json"},body:N}}u(Tt,"round");async function st(c,r,y,f=null){let k,j=!1,_=y;if(f&&f.headersTimeout>0){const m=new AbortController;_=y?AbortSignal.any([y,m.signal]):m.signal,k=setTimeout(()=>{j=!0,m.abort()},f.headersTimeout)}try{return await c(r.url,{method:r.method,headers:r.headers,body:JSON.stringify(r.body),signal:_,redirect:"error",...f&&f.connectTimeout>0?{connectTimeout:f.connectTimeout}:{}})}catch(m){if(y?.aborted)throw m;const g=j?`no response headers within ${f.headersTimeout} ms`:m.message;throw new z(`Could not reach the upstream (${r.upstream}): ${g}`,{code:"upstream_unreachable",status:502,details:{cause:m}})}finally{clearTimeout(k)}}u(st,"call");async function B(c){const r=await c.text();try{return JSON.parse(r)}catch{return r===""?null:r}}u(B,"parse");export{_t as send};
|
|
2
2
|
//# sourceMappingURL=send.mjs.map
|
package/dist/lib/backends.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var i=Object.defineProperty;var e=(t,o)=>i(t,"name",{value:o,configurable:!0});const n=["default_generation_settings","total_slots","build_info"];function a(t){return t.ok&&t.body!==null&&typeof t.body=="object"?t.body:{}}e(a,"objectOf");function p(t){return n.some(o=>o in t)}e(p,"isLlamacppProps");function f(t){return typeof t.model_path=="string"||typeof t.tokenizer_path=="string"}e(f,"isSglangInfo");const s=["llm","vlm","embeddings"];function l(t){return t.object!=="list"||!Array.isArray(t.data)||t.data.length===0?!1:t.data.every(o=>o?.object==="model"&&typeof o.id=="string"&&typeof o.arch=="string"&&typeof o.compatibility_type=="string"&&typeof o.state=="string"&&s.includes(o.type))}e(l,"isLmstudioModels");export{p as isLlamacppProps,l as isLmstudioModels,f as isSglangInfo,a as objectOf};
|
|
2
2
|
//# sourceMappingURL=backends.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var A=Object.defineProperty;var u=(t,r)=>A(t,"name",{value:r,configurable:!0});import{TranslatorError as x,openaiError as P}from"./errors.mjs";import{resolve as j}from"./route.mjs";import{combine as E,deliver as O}from"./usage.mjs";const b=128,g={data:"[DONE]"};function U(t,r){if(t.front!=="openai"||!r||typeof r!="object"||Array.isArray(r)||!Number.isInteger(r.n)||r.n<2||j(t,r).convert===null)return 0;if(r.n>b)throw new x(`n: at most ${b} choices`,{code:"invalid_request",param:"n"});return r.n}u(U,"wants");function v(t,r){if(typeof t=="number"&&typeof r=="number")return t+r;if(t&&r&&typeof t=="object"&&typeof r=="object"){const c={...t};for(const a of Object.keys(r))c[a]=a in t?v(t[a],r[a]):r[a];return c}return t??r}u(v,"add");function S(t){const r=t.flatMap((a,f)=>(a.choices??[]).map(l=>({...l,index:f}))),c=t.map(a=>a.usage).reduce((a,f)=>a===void 0?f:v(a,f),void 0);return{...t[0],choices:r,...c===void 0?{}:{usage:c}}}u(S,"join");async function N(t){try{for(;!(await t.next()).done;);}catch{}}u(N,"finished");function D(t,r,c){const a=t.map(s=>s[Symbol.asyncIterator]());let f=!1;const l=u(async()=>{f||(f=!0,r(),await Promise.allSettled(a.map(s=>s.return?.())))},"close"),h=(async function*(){const s=new Map,p=u(o=>s.set(o,a[o].next().then(i=>({index:o,step:i}),i=>({index:o,error:i}))),"arm");a.forEach((o,i)=>p(i));let y,w,m;try{for(;s.size>0;){const{index:o,step:i,error:e}=await Promise.race(s.values());if(s.delete(o),e){c(o),yield{data:P(String(e?.message??e),{type:e?.type??"server_error"})},yield g;return}if(i.done)continue;p(o);const n=i.value?.data;if(n!=="[DONE]"){if(n?.error){c(o),yield i.value,await N(a[o]),yield g;return}if(Array.isArray(n?.choices)&&n.choices.length===0&&n.usage){w=n,m=m===void 0?n.usage:v(m,n.usage);continue}y??=n?.id,yield{...i.value,data:{...n,id:y,choices:n.choices.map(d=>({...d,index:o}))}}}}m!==void 0&&(yield{data:{...w,id:y,choices:[],usage:m}}),yield g}finally{await l()}})();return{[Symbol.asyncIterator](){return this},next:u(()=>h.next(),"next"),async return(s){return await l(),h.return(s)},async throw(s){return await l(),h.throw(s)}}}u(D,"weave");async function q({body:t,count:r,options:c,send:a,warn:f}){const l=new AbortController,h=c.signal?AbortSignal.any([c.signal,l.signal]):l.signal,s=Array.from({length:r},()=>{let e;return{promise:new Promise(d=>{e=d}),resolve:e}});let p=-1;const y=u(e=>{p>=0||(p=e,l.abort())},"trip"),w=Promise.all(s.map(e=>e.promise)).then(e=>{const n=E(e,p);return O(n,c.onUsage,f),n}),m=u(e=>Object.defineProperty(e,"usage",{value:w,enumerable:!1,configurable:!0}),"attach"),o=await Promise.all(s.map((e,n)=>a({...t,n:1},{...c,signal:h,onUsage:e.resolve}).then(d=>(d.status>=400&&y(n),{reply:d}),d=>(y(n),{error:d}))));if(p>=0){await Promise.all(o.map(n=>n.reply?.stream?.return?.())),await w;const e=o[p];if("error"in e)throw e.error;return m(e.reply)}const i=o.map(e=>e.reply);if(t.stream===!0){const e=D(i.map(n=>n.stream),()=>l.abort(),y);return m({status:200,headers:i[0].headers,stream:e})}return await w,m({status:200,headers:i[0].headers,body:S(i.map(e=>e.body))})}u(q,"run");export{b as MAX,v as add,S as join,q as run,U as wants,D as weave};
|
|
2
|
+
//# sourceMappingURL=fanout.mjs.map
|
package/dist/lib/finish.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var c=Object.defineProperty;var n=(t,e)=>c(t,"name",{value:e,configurable:!0});const p={abort:{type:"api_error",status:502},aborted:{type:"api_error",status:502},error:{type:"api_error",status:502},insufficient_system_resource:{type:"overloaded_error",status:503}};function i(t){if(typeof t!="string"||!Object.hasOwn(p,t))return null;const{type:e,status:o}=p[t];return{type:e,status:o,code:"upstream_finish_"+t,message:`Upstream ended the response with finish_reason "${t}"`}}n(i,"finishError");const u=["hate","sexual","violence","self_harm"];function f(t){return!t||typeof t!="object"?null:u.some(e=>t[e]?.filtered===!0)?"general_harms":null}n(f,"filterCategory");function k(t,{hasToolCalls:e,matchedStop:o,argumentsComplete:s=!0,refusal:r,category:_=null}){return e&&(t!=="length"&&t!=="model_length"||s)?{stop_reason:"tool_use",stop_sequence:null}:t==="length"||t==="model_length"?{stop_reason:"max_tokens",stop_sequence:null}:t==="content_filter"?{stop_reason:"refusal",stop_sequence:null,stop_details:{type:"refusal",category:_,explanation:typeof r=="string"&&r!==""?r:null}}:typeof o=="string"&&o!==""?{stop_reason:"stop_sequence",stop_sequence:o}:{stop_reason:"end_turn",stop_sequence:null}}n(k,"toAnthropicStop");function m(t){switch(t){case"max_tokens":return"length";case"tool_use":return"tool_calls";case"refusal":return"content_filter";default:return"stop"}}n(m,"toOpenAIFinish");function d(t){const e=t?.prompt_tokens??0,o=t?.prompt_tokens_details?.cached_tokens??0;return{input_tokens:Math.max(0,e-o),output_tokens:t?.completion_tokens??0,cache_creation_input_tokens:0,cache_read_input_tokens:o}}n(d,"toAnthropicUsage");function h(t){const e=t?.cache_read_input_tokens??0,o=(t?.input_tokens??0)+e+(t?.cache_creation_input_tokens??0),s=t?.output_tokens??0,r={prompt_tokens:o,completion_tokens:s,total_tokens:o+s};return e>0&&(r.prompt_tokens_details={cached_tokens:e}),r}n(h,"toOpenAIUsage");export{f as filterCategory,i as finishError,k as toAnthropicStop,d as toAnthropicUsage,m as toOpenAIFinish,h as toOpenAIUsage};
|
|
2
2
|
//# sourceMappingURL=finish.mjs.map
|
package/dist/lib/http.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
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
|