@cldmv/llm-translator 0.6.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 +5 -5
- 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/embeddings/send.mjs +1 -1
- package/dist/api/request.mjs +2 -2
- package/dist/api/send.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/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 +3 -1
- package/types/src/api/embeddings/send.d.mts +10 -2
- 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/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))
|
|
28
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))
|
|
29
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))
|
|
30
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))
|
|
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 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 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
|
|
@@ -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 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
|
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
|
package/dist/lib/record.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var E=Object.defineProperty;var s=(
|
|
2
|
-
`)}catch(
|
|
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
3
|
//# sourceMappingURL=record.mjs.map
|
package/dist/lib/routes.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var S=Object.defineProperty;var c=(e,o)=>S(e,"name",{value:o,configurable:!0});import{timingSafeEqual as H}from"node:crypto";import{TranslatorError as w}from"./errors.mjs";import{messageId as O}from"./ids.mjs";import{sign as T}from"./thinking.mjs";const j=64*1024*1024,C={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"},p=Symbol("stalled"),v=Symbol("gone");async function D(e,o){if(!(o>0))return e;let a;const l=new Promise(g=>{a=setTimeout(g,o,p)});try{return await Promise.race([e,l])}finally{clearTimeout(a)}}c(D,"settle");async function*I(e,o,a){let l=!1;try{const g=o.front==="anthropic";g&&(yield{event:"message_start",data:{type:"message_start",message:{id:O(),type:"message",role:"assistant",model:a,content:[],stop_reason:null,stop_sequence:null,usage:{input_tokens:0,output_tokens:0,cache_creation_input_tokens:0,cache_read_input_tokens:0}}}},yield{event:"content_block_start",data:{type:"content_block_start",index:0,content_block:{type:"thinking",thinking:"",signature:""}}});const s=await e,
|
|
1
|
+
var S=Object.defineProperty;var c=(e,o)=>S(e,"name",{value:o,configurable:!0});import{timingSafeEqual as H}from"node:crypto";import{TranslatorError as w}from"./errors.mjs";import{messageId as O}from"./ids.mjs";import{sign as T}from"./thinking.mjs";const j=64*1024*1024,C={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"},p=Symbol("stalled"),v=Symbol("gone");async function D(e,o){if(!(o>0))return e;let a;const l=new Promise(g=>{a=setTimeout(g,o,p)});try{return await Promise.race([e,l])}finally{clearTimeout(a)}}c(D,"settle");async function*I(e,o,a){let l=!1;try{const g=o.front==="anthropic";g&&(yield{event:"message_start",data:{type:"message_start",message:{id:O(),type:"message",role:"assistant",model:a,content:[],stop_reason:null,stop_sequence:null,usage:{input_tokens:0,output_tokens:0,cache_creation_input_tokens:0,cache_read_input_tokens:0}}}},yield{event:"content_block_start",data:{type:"content_block_start",index:0,content_block:{type:"thinking",thinking:"",signature:""}}});const s=await e,b=c(function*(){yield{event:"content_block_delta",data:{type:"content_block_delta",index:0,delta:{type:"signature_delta",signature:T()}}},yield{event:"content_block_stop",data:{type:"content_block_stop",index:0}}},"closeEarly");if(s.stream){l=!0;let u=null;for await(const i of s.stream){if(!g){yield i;continue}if(i.event!=="message_start"){if(u===null&&i.event==="content_block_start"&&i.data.content_block.type==="thinking"){u=0;continue}u===null&&(yield*b(),u=1),yield u>0&&typeof i.data?.index=="number"?{...i,data:{...i.data,index:i.data.index+u}}:i}}return}g&&(yield*b()),yield*x(o,s.body)}finally{l||e.then(g=>g.stream?.return?.(),()=>{})}}c(I,"late");function*x(e,o){if(e.front==="anthropic"){yield{event:"error",data:o};return}yield{data:o},yield{data:"[DONE]"}}c(x,"failure");async function E(e,o,a,l,g,s=200,b=C){e.hijack();const u=e.raw;u.writeHead(s,b);try{await o.write(u,l,{front:o.front,interval:g})}catch(i){if(!a.signal.aborted){const f=i instanceof w;f||(e.log.error(i),o.emit({type:"error",kind:"route",level:"error",message:String(i?.message??i),log:!1}));const{body:_}=o.build(f?i.status:502,f?i.message:"Internal server error",f?{type:i.type,code:i.code}:{});for(const t of x(o,_))u.write(o.encode(t))}}return u.end(),e}c(E,"stream");function L(e,o){const a=Buffer.from(e),l=Buffer.from(o);return a.length===l.length&&H(a,l)}c(L,"same");function U(e){const o=/^Bearer\s+(.+)$/i.exec(e.authorization??"");return e["x-api-key"]||o?.[1]||""}c(U,"keyOf");function z(e){const o={};for(const[a,l]of Object.entries(e))(a==="anthropic-beta"||a==="anthropic-version"||a.startsWith("x-claude-code-"))&&(o[a]=String(l));return o}c(z,"forwardable");function B(e){if(e===!0)return{ok:!0,context:void 0};if(e&&typeof e=="object"&&e.ok===!0)return{ok:!0,context:e.context};const o=e&&typeof e=="object"&&e.ok===!1?e:{};return{ok:!1,status:Number.isInteger(o.status)&&o.status>=400&&o.status<=599?o.status:401,message:typeof o.message=="string"&&o.message!==""?o.message:"unauthorized"}}c(B,"verdictOf");function R(e,o,a){const{front:l,filter:g}=a,s={...a,build:c((t,n,r={})=>a.build(t,n,{...r,front:l}),"build"),send:c((t,n)=>a.send(t,{...n,front:l,filter:g}),"send"),route:c(t=>a.route(t,{front:l}),"route"),count:c((t,n)=>a.count(t,{...n,front:l}),"count"),embed:c((t,n)=>a.embed(t,n),"embed"),list:c(t=>a.list({...t,front:l}),"list"),get:c((t,n)=>a.get(t,{...n,front:l}),"get")},b=[o.apiKey??[]].flat().filter(Boolean),u=o.bodyLimit??j,i=new WeakMap,f=c((t,n)=>t.code(n.status).headers(n.headers).send(n.body),"reject");e.setErrorHandler((t,n,r)=>{if(t instanceof w)return f(r,s.build(t.status,t.message,{type:t.type,code:t.code,param:t.param}));const d=t.statusCode??500;return d<500?f(r,s.build(d,t.message)):(n.log.error(t),s.emit({type:"error",kind:"route",level:"error",message:String(t.message),status:d,log:!1}),f(r,s.build(d,"Internal server error")))}),e.addHook("onRequest",async(t,n)=>{if(b.length===0||t.routeOptions.config?.public)return;const r=U(t.headers);if(!(r&&b.some(d=>L(r,d))))return f(n,s.build(401,"invalid x-api-key"))}),typeof o.authorize=="function"&&e.addHook("preHandler",async(t,n)=>{if(t.routeOptions.config?.public)return;let r=null;if(t.body&&typeof t.body=="object"&&!Array.isArray(t.body))try{r=s.route(t.body)}catch{}let d;try{d=B(await o.authorize({method:t.method,url:t.url,headers:t.headers,body:t.body,front:s.front,route:r}))}catch(m){t.log.error(m),s.emit({type:"error",kind:"route",level:"error",message:String(m?.message??m),log:!1}),d={ok:!1,status:401,message:"unauthorized"}}if(!d.ok)return f(n,s.build(d.status,d.message));i.set(t,d.context)}),e.route({method:["GET","HEAD"],url:"/",config:{public:!0},handler:c(async(t,n)=>n.send({ok:!0}),"handler")}),e.route({method:["GET","HEAD"],url:"/api/hello",config:{public:!0},handler:c(async(t,n)=>n.send({ok:!0}),"handler")}),e.get("/v1/models",async(t,n)=>{const r=new AbortController;return n.raw.on("close",()=>r.abort()),s.list({signal:r.signal})}),e.get("/v1/models/*",async(t,n)=>{const r=new AbortController;return n.raw.on("close",()=>r.abort()),s.get(t.params["*"],{signal:r.signal})});const _=s.front==="anthropic"?"/v1/messages":"/v1/chat/completions";e.post(_,{bodyLimit:u},async(t,n)=>{if(n.raw.destroyed)return n;const r=new AbortController;n.raw.on("close",()=>{n.raw.writableFinished||r.abort()});const d=typeof o.onUsage=="function"?h=>o.onUsage(h,i.get(t)):void 0,m=s.interval(t.body),k=s.send(t.body,{signal:r.signal,clientHeaders:z(t.headers),onUsage:d,log:t.log});k.catch(()=>{});const A=new Promise(h=>r.signal.addEventListener("abort",()=>h(v),{once:!0})),y=await D(Promise.race([k,A]),t.body?.stream===!0?m:0);return y===v?(k.then(h=>h.stream?.return?.(),()=>{}),n):y===p?E(n,s,r,I(k,s,t.body?.model),m):y.stream?E(n,s,r,y.stream,m,y.status,y.headers):f(n,y)}),s.front==="openai"&&e.post("/v1/embeddings",{bodyLimit:u},async(t,n)=>{const r=new AbortController;n.raw.on("close",()=>{n.raw.writableFinished||r.abort()});const d=typeof o.onUsage=="function"?m=>o.onUsage(m,i.get(t)):void 0;return f(n,await s.embed(t.body,{signal:r.signal,onUsage:d,log:t.log}))}),s.front==="anthropic"&&e.post("/v1/messages/count_tokens",{bodyLimit:u},async(t,n)=>{const r=new AbortController;return n.raw.on("close",()=>r.abort()),s.count(t.body,{signal:r.signal})})}c(R,"register");export{I as late,R as register};
|
|
2
2
|
//# sourceMappingURL=routes.mjs.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cldmv/llm-translator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Bidirectional Anthropic Messages ⇄ OpenAI Chat Completions translator with backend auto-detection (vLLM and other OpenAI-compatible servers), built on slothlet, with an optional standalone server for running Claude Code against local models.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -83,8 +83,9 @@
|
|
|
83
83
|
"build:ci": "npm run lint && npm run format:check && npm run types:build && npm run build",
|
|
84
84
|
"prepack": "npm run build",
|
|
85
85
|
"prepare": "node -e \"import('./.githooks/install.mjs').catch(()=>{})\"",
|
|
86
|
-
"test": "node tests/run-vitest.mjs && npm run test:
|
|
87
|
-
"test:
|
|
86
|
+
"test": "node tests/run-vitest.mjs && npm run test:cjs",
|
|
87
|
+
"test:cjs": "npm run build && node --conditions=llm-translator-dev --test tests/cjs/entry.test.cjs",
|
|
88
|
+
"test:dist": "npm run build && node tests/run-vitest.mjs --dist && node --conditions=llm-translator-dev --test tests/cjs/entry.test.cjs",
|
|
88
89
|
"test:watch": "vitest --config .configs/vitest.config.mjs",
|
|
89
90
|
"coverage": "node tests/run-vitest.mjs --coverage-quiet",
|
|
90
91
|
"ci:coverage": "npm run coverage && npm run test:dist",
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* tool-name map, thinking mode, fields that were dropped).
|
|
6
6
|
* @param {any} body - Anthropic `POST /v1/messages` body.
|
|
7
7
|
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
8
|
-
* @param {{ producer?: string, budget?: ReturnType<typeof createBudget>, listed?: boolean }} [context] - `producer` is the `driver:protocol:host` the request goes to; signatures that carry provider reasoning data are restored only for the producer that made them. `budget` limits how much signature decoding the request may cost (one is made when absent; `send` passes one for all its attempts). `listed` says the model has a configured effort list, so the default `medium` effort is left to it.
|
|
8
|
+
* @param {{ producer?: string, budget?: ReturnType<typeof createBudget>, listed?: boolean, toolCallIds?: { alphabet: string, min?: number, max?: number } }} [context] - `toolCallIds` is the driver's tool-call id policy: ids that do not satisfy it are rewritten (and recorded in `state.toolIds` for the way back). `producer` is the `driver:protocol:host` the request goes to; signatures that carry provider reasoning data are restored only for the producer that made them. `budget` limits how much signature decoding the request may cost (one is made when absent; `send` passes one for all its attempts). `listed` says the model has a configured effort list, so the default `medium` effort is left to it.
|
|
9
9
|
* @returns {Promise<{ body: any, state: any }>} OpenAI request body and conversion state.
|
|
10
10
|
* @throws {TranslatorError} For malformed requests, unsupported content when configured to error, or PDF limit violations.
|
|
11
11
|
*/
|
|
@@ -13,6 +13,11 @@ export function request(body: any, settings?: Partial<import("../../../../../../
|
|
|
13
13
|
producer?: string;
|
|
14
14
|
budget?: ReturnType<typeof createBudget>;
|
|
15
15
|
listed?: boolean;
|
|
16
|
+
toolCallIds?: {
|
|
17
|
+
alphabet: string;
|
|
18
|
+
min?: number;
|
|
19
|
+
max?: number;
|
|
20
|
+
};
|
|
16
21
|
}): Promise<{
|
|
17
22
|
body: any;
|
|
18
23
|
state: any;
|
|
@@ -7,10 +7,17 @@
|
|
|
7
7
|
* tool results are grouped into one user turn with the results first, as Anthropic requires.
|
|
8
8
|
* @param {any} chatBody - OpenAI `POST /v1/chat/completions` body.
|
|
9
9
|
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
10
|
+
* @param {{ toolCallIds?: { alphabet: string, min?: number, max?: number } }} [context] - `toolCallIds` is the driver's tool-call id policy: ids that do not satisfy it are rewritten (and recorded in `state.toolIds` for the way back).
|
|
10
11
|
* @returns {Promise<{ body: any, state: any }>} Anthropic request body and conversion state.
|
|
11
12
|
* @throws {TranslatorError} For malformed requests, `n` above 1, unknown roles, or unknown fields when configured to error.
|
|
12
13
|
*/
|
|
13
|
-
export function request(chatBody: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings
|
|
14
|
+
export function request(chatBody: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>, context?: {
|
|
15
|
+
toolCallIds?: {
|
|
16
|
+
alphabet: string;
|
|
17
|
+
min?: number;
|
|
18
|
+
max?: number;
|
|
19
|
+
};
|
|
20
|
+
}): Promise<{
|
|
14
21
|
body: any;
|
|
15
22
|
state: any;
|
|
16
23
|
}>;
|
|
@@ -1,26 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* -----
|
|
9
|
-
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
10
|
-
* @Last modified time: 2026-10-25T12:58:59-07:00 (1792958339)
|
|
11
|
-
* -----
|
|
12
|
-
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
|
-
*
|
|
2
|
+
* Match `api.anthropic.com` by host name with no request. Any other host is asked for `GET /v1/models` with the Anthropic headers
|
|
3
|
+
* (`anthropic-version`, `x-api-key` from the upstream's key) and matches only when the answer is an Anthropic-shaped model list.
|
|
4
|
+
* The request is part of the batch started at the first probing driver, so it adds no round trip. A server that needs a key and was
|
|
5
|
+
* given none answers 401 and is not matched; give it the key or set `backend: "anthropic"`.
|
|
6
|
+
* @param {any} context - `{ endpoint, transport, signal, timeout, probes }`.
|
|
7
|
+
* @returns {Promise<{host: string} | {models: string[]} | null>} Facts, or null when the endpoint is not an Anthropic-protocol server.
|
|
14
8
|
*/
|
|
15
|
-
|
|
16
|
-
* @fileoverview Anthropic driver: matches the api.anthropic.com host.
|
|
17
|
-
* @module @cldmv/llm-translator/driver/anthropic
|
|
18
|
-
*/
|
|
19
|
-
/**
|
|
20
|
-
* Match by host name, no network access. Other Anthropic-compatible servers are selected with `backend: "anthropic"`.
|
|
21
|
-
* @param {any} context - `{ endpoint }`.
|
|
22
|
-
* @returns {{host: string} | null} Facts, or null when the endpoint is not api.anthropic.com.
|
|
23
|
-
*/
|
|
24
|
-
export function detect({ endpoint }: any): {
|
|
9
|
+
export function detect(context: any): Promise<{
|
|
25
10
|
host: string;
|
|
26
|
-
} |
|
|
11
|
+
} | {
|
|
12
|
+
models: string[];
|
|
13
|
+
} | null>;
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
*/
|
|
19
19
|
/**
|
|
20
20
|
* Describe the Anthropic driver (native Messages API upstream).
|
|
21
|
-
* @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, headers: Record<string,string>, paths: Record<string,string>}} Driver facts.
|
|
21
|
+
* @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, hosts: string[], probes: string[], headers: Record<string,string>, paths: Record<string,string>}} Driver facts.
|
|
22
22
|
*/
|
|
23
23
|
export function info(): {
|
|
24
24
|
name: string;
|
|
@@ -26,6 +26,8 @@ export function info(): {
|
|
|
26
26
|
order: number;
|
|
27
27
|
auth: string;
|
|
28
28
|
strict: boolean;
|
|
29
|
+
hosts: string[];
|
|
30
|
+
probes: string[];
|
|
29
31
|
headers: Record<string, string>;
|
|
30
32
|
paths: Record<string, string>;
|
|
31
33
|
};
|
|
@@ -4,12 +4,20 @@
|
|
|
4
4
|
* pool of endpoints serving it, which fails over on a connection error or a 5xx. Errors are replies in the OpenAI error shape (the
|
|
5
5
|
* return value, not a throw): `400` for a body that is not an object or has no `model`, `404` when no upstream serves the model and `400`
|
|
6
6
|
* (`embeddings_unsupported`) when the model is served only by Anthropic-protocol upstreams, which have no embeddings API, and `499` (`aborted`) when `signal` aborts.
|
|
7
|
+
*
|
|
8
|
+
* Like `api.send`, every reply, including error replies, carries a non-enumerable `usage` property (a `Promise` of the usage report that
|
|
9
|
+
* never rejects, already settled) and `onUsage(report)` is called with the same report exactly once. The report has the shape of
|
|
10
|
+
* `api.send`'s (see there) with `front: "openai"` and `stream: false`: `input_tokens` is the reply's `prompt_tokens`, the other token fields
|
|
11
|
+
* are `null`, and `reported` is false when the reply carried no `usage` (or only zeros). `reason` is `"done"` for a 200 reply, `"aborted"`
|
|
12
|
+
* for a 499, `"upstream_error"` for an error the upstream answered with, and `"error"` for the rest (an invalid body, an unknown or
|
|
13
|
+
* unsupported model, an unreachable upstream, a body that is not JSON); `model` and `upstream` are null until an endpoint was tried.
|
|
7
14
|
* @param {any} body - OpenAI embeddings request body (`{ model, input, ... }`).
|
|
8
|
-
* @param {any} [options] - `{ signal, headers, log }`: abort signal for the upstream call, extra upstream headers and the logger events of this request also go to (used by the server).
|
|
9
|
-
* @returns {Promise<{status: number, headers: Record<string,string>, body: any}>} The upstream's JSON reply, or an OpenAI-shaped error reply.
|
|
15
|
+
* @param {any} [options] - `{ signal, headers, onUsage, log }`: abort signal for the upstream call, extra upstream headers, the usage callback and the logger events of this request also go to (used by the server).
|
|
16
|
+
* @returns {Promise<{status: number, headers: Record<string,string>, body: any, usage: Promise<any>}>} The upstream's JSON reply, or an OpenAI-shaped error reply.
|
|
10
17
|
*/
|
|
11
18
|
export function send(body: any, options?: any): Promise<{
|
|
12
19
|
status: number;
|
|
13
20
|
headers: Record<string, string>;
|
|
14
21
|
body: any;
|
|
22
|
+
usage: Promise<any>;
|
|
15
23
|
}>;
|
package/types/src/lib/http.d.mts
CHANGED
|
@@ -41,11 +41,16 @@ export function headers(endpoint: any, auth?: string, extra?: Record<string, str
|
|
|
41
41
|
* GET a path of the endpoint for detection. Within one detection pass (`context.probes` is a shared `Map`) each path is requested
|
|
42
42
|
* once and every driver that asks for it gets the same result, so a path fetched for one backend (for example `/v1/models`) is
|
|
43
43
|
* not fetched again for the next. Never rejects; treat the result as read-only.
|
|
44
|
+
*
|
|
45
|
+
* Without `driver` the request carries the upstream's bearer key. A driver whose `info()` says `auth: "x-api-key"` or carries
|
|
46
|
+
* `headers` passes its `info()` so the probe is sent the way the driver talks to its own backend; that request is a different
|
|
47
|
+
* request from the bearer one, so it is cached under its own key.
|
|
44
48
|
* @param {any} context - `{ endpoint, transport, signal, timeout, probes? }`.
|
|
45
49
|
* @param {string} path - Path starting with `/`.
|
|
50
|
+
* @param {any} [driver] - A driver's `info()`; its `auth` and `headers` shape the request.
|
|
46
51
|
* @returns {Promise<{ok: boolean, status: number, body: any, error?: Error}>} Outcome (see `json`).
|
|
47
52
|
*/
|
|
48
|
-
export function probe({ endpoint, transport, signal, timeout, probes }: any, path: string): Promise<{
|
|
53
|
+
export function probe({ endpoint, transport, signal, timeout, probes }: any, path: string, driver?: any): Promise<{
|
|
49
54
|
ok: boolean;
|
|
50
55
|
status: number;
|
|
51
56
|
body: any;
|
|
@@ -62,11 +67,12 @@ export function plain(source: Headers | Record<string, string> | undefined): Rec
|
|
|
62
67
|
* @param {Function} transport - fetch-compatible function.
|
|
63
68
|
* @param {string} url - Absolute URL.
|
|
64
69
|
* @param {any} [options] - `{ method, headers, body, signal, timeout }`.
|
|
65
|
-
* @returns {Promise<{ok: boolean, status: number, body: any, error?: Error}>} Outcome.
|
|
70
|
+
* @returns {Promise<{ok: boolean, status: number, body: any, headers?: Record<string,string>, error?: Error}>} Outcome (`headers` only when a response arrived).
|
|
66
71
|
*/
|
|
67
72
|
export function json(transport: Function, url: string, options?: any): Promise<{
|
|
68
73
|
ok: boolean;
|
|
69
74
|
status: number;
|
|
70
75
|
body: any;
|
|
76
|
+
headers?: Record<string, string>;
|
|
71
77
|
error?: Error;
|
|
72
78
|
}>;
|
package/types/src/lib/ids.d.mts
CHANGED
|
@@ -19,13 +19,51 @@ export function completionId(): string;
|
|
|
19
19
|
*/
|
|
20
20
|
export function callId(): string;
|
|
21
21
|
/**
|
|
22
|
-
* Make a tool-call id valid for Anthropic (`[a-zA-Z0-9_-]+`).
|
|
22
|
+
* Make a tool-call id valid for Anthropic (`[a-zA-Z0-9_-]+`). An id found in `map` (`fitted -> original`, from a driver's `toolCallIds`
|
|
23
|
+
* policy) is the client's own and is restored first. Otherwise valid ids pass through unchanged, invalid characters
|
|
23
24
|
* (for example kimi's `functions.name:0`) become `_`, and a missing or empty id is replaced by a fresh one. The
|
|
24
25
|
* mapping is deterministic so a tool call and its later tool result agree.
|
|
25
26
|
* @param {unknown} id - Upstream id.
|
|
27
|
+
* @param {Record<string, string>} [map] - `fitted -> original` ids from the conversion state.
|
|
26
28
|
* @returns {string} Anthropic-valid id.
|
|
27
29
|
*/
|
|
28
|
-
export function anthropicToolId(id: unknown): string;
|
|
30
|
+
export function anthropicToolId(id: unknown, map?: Record<string, string>): string;
|
|
31
|
+
/**
|
|
32
|
+
* Whether a driver's `info().toolCallIds` is usable: `{ alphabet, min?, max? }` where `alphabet` is the body of a regular
|
|
33
|
+
* expression character class that allows at least one printable ASCII character (`"a-zA-Z0-9"`), and `min` and `max` are
|
|
34
|
+
* positive integers (`min <= max`) bounding the id length. Mistral's ids are `{ alphabet: "a-zA-Z0-9", min: 9, max: 9 }`.
|
|
35
|
+
* @param {unknown} policy - Value to check.
|
|
36
|
+
* @returns {boolean} True when the policy can be applied.
|
|
37
|
+
*/
|
|
38
|
+
export function validToolIdPolicy(policy: unknown): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Create a tool-call id fitter for an upstream with a strict id format (`info().toolCallIds`, see `validToolIdPolicy`). The translator
|
|
41
|
+
* keeps no state between requests, so the mapping is a pure function of the request: an id that already satisfies the policy passes
|
|
42
|
+
* through unchanged (so history replayed from the upstream's own ids survives), and any other id becomes a hash of itself in the
|
|
43
|
+
* allowed alphabet, the same text on every request. A tool call and its tool result carry the same original and so get the same
|
|
44
|
+
* value. `ids` lists every original id of the request: the conforming ones are reserved first, then each other one takes its hash,
|
|
45
|
+
* and when that text is already taken by a different original the hash is taken again with a counter until it is free. Every
|
|
46
|
+
* rewritten id is recorded in `map` (`fitted -> original`) so the original can be restored when the upstream echoes it back.
|
|
47
|
+
* @param {{ policy: { alphabet: string, min?: number, max?: number }, ids: unknown[], map?: Record<string, string> }} options - Policy, the request's ids and the (JSON-serializable) map to record into.
|
|
48
|
+
* @returns {(id: unknown) => string} Fitter function.
|
|
49
|
+
*/
|
|
50
|
+
export function createIdFitter({ policy, ids, map }: {
|
|
51
|
+
policy: {
|
|
52
|
+
alphabet: string;
|
|
53
|
+
min?: number;
|
|
54
|
+
max?: number;
|
|
55
|
+
};
|
|
56
|
+
ids: unknown[];
|
|
57
|
+
map?: Record<string, string>;
|
|
58
|
+
}): (id: unknown) => string;
|
|
59
|
+
/**
|
|
60
|
+
* Original tool-call id for one rewritten by `createIdFitter` (ids that were never rewritten, including ones the upstream made itself,
|
|
61
|
+
* map to themselves).
|
|
62
|
+
* @param {Record<string, string>} map - `fitted -> original` map from the conversion state.
|
|
63
|
+
* @param {string} id - Id seen on the wire.
|
|
64
|
+
* @returns {string} Original id.
|
|
65
|
+
*/
|
|
66
|
+
export function restoreId(map: Record<string, string>, id: string): string;
|
|
29
67
|
/**
|
|
30
68
|
* Create a tool-name fitter. Names that already satisfy `pattern` and `max` pass through; others are sanitized and,
|
|
31
69
|
* when too long, shortened with a hash suffix. Every changed name is recorded in `map` (`fitted -> original`) so the
|
|
@@ -14,8 +14,8 @@ export function valid(value: any): boolean;
|
|
|
14
14
|
/**
|
|
15
15
|
* Begin the record of one exchange. With `capture` unset this returns a stand-in that does nothing.
|
|
16
16
|
* @param {{config: any, state: any}} ctx - Translator `{ config, state }`.
|
|
17
|
-
* @param {{body: any, sent: any, front: string, requested: string | null, headers?: Record<string, string>, log?: any}} exchange - Client body, the upstream request (result of `request`), front and the
|
|
18
|
-
* @returns {{raw: (source: any) => any, events: (source: any) => any, reply: (status: number, headers: Record<string, string>, body: any) => void}} Recorder: `raw` taps the upstream stream bytes, `events` taps the client events and writes the record when the stream ends, `reply` writes the record of a non-stream reply.
|
|
17
|
+
* @param {{body: any, sent: any, front: string, requested: string | null, headers?: Record<string, string>, log?: any, exchange?: string | null, attempt: number}} exchange - Client body, the upstream request (result of `request`), front, the logger of the request, and where this attempt sits: `exchange` is the id of the first attempt of the request (omit it for the first attempt) and `attempt` the 1-based number.
|
|
18
|
+
* @returns {{on: boolean, id: string, raw: (source: any) => any, events: (source: any) => any, reply: (status: number, headers: Record<string, string>, body: any) => void, attempt: (response: any, failover?: string) => void}} Recorder: `id` is the record id (pass it as `exchange` for the later attempts), `raw` taps the upstream stream bytes, `events` taps the client events and writes the record when the stream ends, `reply` writes the record of a non-stream reply, `attempt` writes the record of an attempt that did not produce the reply (`failover` is the reason the request moved on to the next endpoint, absent for the final failure).
|
|
19
19
|
*/
|
|
20
20
|
export function start({ config, state }: {
|
|
21
21
|
config: any;
|
|
@@ -27,10 +27,15 @@ export function start({ config, state }: {
|
|
|
27
27
|
requested: string | null;
|
|
28
28
|
headers?: Record<string, string>;
|
|
29
29
|
log?: any;
|
|
30
|
+
exchange?: string | null;
|
|
31
|
+
attempt: number;
|
|
30
32
|
}): {
|
|
33
|
+
on: boolean;
|
|
34
|
+
id: string;
|
|
31
35
|
raw: (source: any) => any;
|
|
32
36
|
events: (source: any) => any;
|
|
33
37
|
reply: (status: number, headers: Record<string, string>, body: any) => void;
|
|
38
|
+
attempt: (response: any, failover?: string) => void;
|
|
34
39
|
};
|
|
35
40
|
/** Most characters of a stream kept per exchange, for the raw upstream text and for the client events each. */
|
|
36
41
|
export const STREAM_LIMIT: 1000000;
|