@cldmv/llm-translator 0.7.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 +5 -5
- 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/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/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/typed.mjs +2 -0
- package/dist/lib/usage.mjs +1 -1
- package/package.json +1 -1
- package/types/src/api/convert/openai/chat/to/anthropic/messages/request.d.mts +1 -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/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/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))
|
|
28
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))
|
|
29
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))
|
|
30
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))
|
|
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))
|
|
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,2 +1,2 @@
|
|
|
1
|
-
var _=Object.defineProperty;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
|
|
@@ -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
|
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
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var o=Object.defineProperty;var e=(t,n)=>o(t,"name",{value:n,configurable:!0});function r(t){return typeof t=="string"?t:Array.isArray(t)?t.map(r).join(""):typeof t?.text=="string"?t.text:""}e(r,"join");function y(t){if(!Array.isArray(t))return null;let n="",f="";for(const i of t)i?.type==="text"?n+=r(i):i?.type==="thinking"&&(f+=r(i.thinking));return{text:n,thinking:f}}e(y,"flatten");export{y as flatten};
|
|
2
|
+
//# sourceMappingURL=typed.mjs.map
|
package/dist/lib/usage.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var E=Object.defineProperty;var c=(e,s)=>E(e,"name",{value:s,configurable:!0});const m=["input_tokens","output_tokens","cache_read_input_tokens","cache_creation_input_tokens"],f=c(e=>Number.isFinite(e)?e:null,"count");function B(e,s){if(!s||typeof s!="object")return null;let o;if(e==="openai"){const n=f(s.prompt_tokens),r=f(s.prompt_tokens_details?.cached_tokens);o={input_tokens:n===null?null:Math.max(0,n-(r??0)),output_tokens:f(s.completion_tokens),cache_read_input_tokens:r,cache_creation_input_tokens:null}}else if(e==="folded"){const n=f(s.input_tokens);o={input_tokens:n===null?null:n+(f(s.cache_creation_input_tokens)??0),output_tokens:f(s.output_tokens),cache_read_input_tokens:f(s.cache_read_input_tokens)||null,cache_creation_input_tokens:null}}else o=Object.fromEntries(m.map(n=>[n,f(s[n])]));return m.every(n=>!o[n])?null:o}c(B,"normalize");function I(e){return!!e&&typeof e=="object"&&Array.isArray(e.choices)&&e.choices.length===0&&!!e.usage}c(I,"usageChunk");function O(e,s,o){try{Promise.resolve(s?.(e)).catch(n=>o(`onUsage rejected: ${n?.message??n}`,{kind:"rejected"}))}catch(n){o(`onUsage threw: ${n?.message??n}`,{kind:"threw"})}}c(O,"deliver");const y=c(e=>{const s=e.filter(o=>o!=null);return s.length===0?null:s.reduce((o,n)=>o+n,0)},"total");function S(e,s=-1){const o={...e[0]};for(const r of m)o[r]=y(e.map(d=>d[r]));o.reported=e.some(r=>r.reported);const n=e.map(r=>r.estimated).filter(Boolean);return o.estimated=n.length===0?null:{input_tokens:y(n.map(r=>r.input_tokens)),output_tokens:y(n.map(r=>r.output_tokens))},o.complete=e.every(r=>r.complete),o.reason=(e[s]??e.find(r=>r.reason!=="done")??e[0]).reason,o.model=e.find(r=>r.model)?.model??null,o.upstream=e.find(r=>r.upstream)?.upstream??null,o}c(S,"combine");function A(e,s,o){const n=Object.fromEntries(m.map(t=>[t,null]));let r=!1,d=null,b=!1,w=!1,h=!1,p=null,g="openai",j;const x=new Promise(t=>{j=t}),k=c((t,a=e.front)=>{const l=B(a,t);if(l){r=!0;for(const i of m)l[i]!==null&&l[i]!==void 0&&(n[i]=l[i])}},"take"),_=c(t=>{if(b)return;b=!0,p&&k(p(),g);const a=t==="done"&&w?"error":t,l={...n,reported:r,estimated:d,complete:a==="done",reason:a,...e};j(l),O(l,s,o)},"finish");return{meta:e,usage:x,json(t){h=!0,p||k(t?.usage)},source(t,a="openai"){p=t,g=a},event(t){t==="[DONE]"&&(h=!0),!(!t||typeof t!="object")&&((t.type==="error"||e.front==="openai"&&t.error)&&(w=!0),e.front==="anthropic"?(t.type==="message_stop"&&(h=!0),p||k(t.type==="message_start"?t.message?.usage:t.usage)):p||k(t.usage))},finish:_,estimate(t){d=t},track(t,a,l=!1){const i=t[Symbol.asyncIterator](),v=this;return{[Symbol.asyncIterator](){return this},async next(){try{for(;;){const u=await i.next();if(u.done)_(h?"done":"error");else if(v.event(u.value?.data),l&&I(u.value?.data))continue;return u}}catch(u){throw _(a?.aborted?"aborted":"error"),u}},async return(u){return _("aborted"),i.return?i.return(u):{done:!0,value:u}},async throw(u){if(_("error"),i.throw)return i.throw(u);throw u}}}}}c(A,"meter");export{S as combine,O as deliver,A as meter,B as normalize};
|
|
2
2
|
//# sourceMappingURL=usage.mjs.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cldmv/llm-translator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.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",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
10
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).
|
|
11
11
|
* @returns {Promise<{ body: any, state: any }>} Anthropic request body and conversion state.
|
|
12
|
-
* @throws {TranslatorError} For malformed requests, `n` above 1, unknown roles, or unknown fields when configured to error.
|
|
12
|
+
* @throws {TranslatorError} For malformed requests, `n` above 1 (`send` fans that out instead), unknown roles, or unknown fields when configured to error.
|
|
13
13
|
*/
|
|
14
14
|
export function request(chatBody: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>, context?: {
|
|
15
15
|
toolCallIds?: {
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Probe an endpoint for LM Studio.
|
|
3
|
+
* @param {any} context - `{ endpoint, transport, signal, timeout, probes }`.
|
|
4
|
+
* @returns {Promise<any | null>} Facts `{ models, loaded }` (ids of chat models, and of those currently loaded), or null when the endpoint is not LM Studio.
|
|
5
|
+
*/
|
|
6
|
+
export function detect(context: any): Promise<any | null>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/llm-translator
|
|
4
|
+
* @Filename: /src/api/driver/lmstudio/info.mjs
|
|
5
|
+
* @Date: 2026-10-31T14:00:00-07:00 (1793480400)
|
|
6
|
+
* @Author: Nate Corcoran <CLDMV>
|
|
7
|
+
* @Email: <Shinrai@users.noreply.github.com>
|
|
8
|
+
* -----
|
|
9
|
+
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
10
|
+
* @Last modified time: 2026-10-31T14:00:00-07:00 (1793480400)
|
|
11
|
+
* -----
|
|
12
|
+
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @fileoverview LM Studio driver: static facts about the LM Studio local server.
|
|
17
|
+
* @module @cldmv/llm-translator/driver/lmstudio
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Describe the LM Studio driver. It is found through `GET /api/v0/models`, LM Studio's own REST listing.
|
|
21
|
+
* @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, probes: string[], paths: Record<string,string>}} Driver facts.
|
|
22
|
+
*/
|
|
23
|
+
export function info(): {
|
|
24
|
+
name: string;
|
|
25
|
+
protocol: string;
|
|
26
|
+
order: number;
|
|
27
|
+
auth: string;
|
|
28
|
+
strict: boolean;
|
|
29
|
+
probes: string[];
|
|
30
|
+
paths: Record<string, string>;
|
|
31
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/llm-translator
|
|
4
|
+
* @Filename: /src/api/driver/lmstudio/settings.mjs
|
|
5
|
+
* @Date: 2026-10-31T14:00:00-07:00 (1793480400)
|
|
6
|
+
* @Author: Nate Corcoran <CLDMV>
|
|
7
|
+
* @Email: <Shinrai@users.noreply.github.com>
|
|
8
|
+
* -----
|
|
9
|
+
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
10
|
+
* @Last modified time: 2026-10-31T14:00:00-07:00 (1793480400)
|
|
11
|
+
* -----
|
|
12
|
+
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @fileoverview LM Studio driver: translator settings for the LM Studio local server.
|
|
17
|
+
* @module @cldmv/llm-translator/driver/lmstudio
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Settings for LM Studio: the conservative generic set (no `top_k`, no prefill, no thinking switch) with usage on the last content chunk.
|
|
21
|
+
* @returns {any} Partial settings.
|
|
22
|
+
*/
|
|
23
|
+
export function settings(): any;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flatten typed content chunks in a streamed delta. While a reasoning model thinks, `delta.content` is a list holding a `thinking` chunk; the
|
|
3
|
+
* switch to the answer is a list with the closing `thinking` chunk and the first `text` chunk; the answer then streams as a plain string.
|
|
4
|
+
* The thinking goes to `reasoning_content` and the text to `content`.
|
|
5
|
+
* @param {any} chunk - Parsed `chat.completion.chunk`.
|
|
6
|
+
* @returns {any} Normalized chunk.
|
|
7
|
+
*/
|
|
8
|
+
export function chunk(chunk: any): any;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/llm-translator
|
|
4
|
+
* @Filename: /src/api/driver/mistral/detect.mjs
|
|
5
|
+
* @Date: 2026-10-31T14:00:00-07:00 (1793480400)
|
|
6
|
+
* @Author: Nate Corcoran <CLDMV>
|
|
7
|
+
* @Email: <Shinrai@users.noreply.github.com>
|
|
8
|
+
* -----
|
|
9
|
+
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
10
|
+
* @Last modified time: 2026-10-31T14:00:00-07:00 (1793480400)
|
|
11
|
+
* -----
|
|
12
|
+
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @fileoverview Mistral driver: matches the api.mistral.ai host.
|
|
17
|
+
* @module @cldmv/llm-translator/driver/mistral
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Match by host name, no network access.
|
|
21
|
+
* @param {any} context - `{ endpoint }`.
|
|
22
|
+
* @returns {{host: string} | null} Facts, or null when the endpoint is not api.mistral.ai.
|
|
23
|
+
*/
|
|
24
|
+
export function detect({ endpoint }: any): {
|
|
25
|
+
host: string;
|
|
26
|
+
} | null;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/llm-translator
|
|
4
|
+
* @Filename: /src/api/driver/mistral/info.mjs
|
|
5
|
+
* @Date: 2026-10-31T14:00:00-07:00 (1793480400)
|
|
6
|
+
* @Author: Nate Corcoran <CLDMV>
|
|
7
|
+
* @Email: <Shinrai@users.noreply.github.com>
|
|
8
|
+
* -----
|
|
9
|
+
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
10
|
+
* @Last modified time: 2026-10-31T14:00:00-07:00 (1793480400)
|
|
11
|
+
* -----
|
|
12
|
+
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @fileoverview Mistral driver: static facts about the hosted Mistral API.
|
|
17
|
+
* @module @cldmv/llm-translator/driver/mistral
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Describe the Mistral driver. Mistral accepts only 9-character alphanumeric tool-call ids, so the translator rewrites the client's ids.
|
|
21
|
+
* @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, toolCallIds: {alphabet: string, min: number, max: number}, paths: Record<string,string>}} Driver facts.
|
|
22
|
+
*/
|
|
23
|
+
export function info(): {
|
|
24
|
+
name: string;
|
|
25
|
+
protocol: string;
|
|
26
|
+
order: number;
|
|
27
|
+
auth: string;
|
|
28
|
+
strict: boolean;
|
|
29
|
+
toolCallIds: {
|
|
30
|
+
alphabet: string;
|
|
31
|
+
min: number;
|
|
32
|
+
max: number;
|
|
33
|
+
};
|
|
34
|
+
paths: Record<string, string>;
|
|
35
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drop tool fields when there are no tools, send `seed` as `random_seed` and `tool_choice: "required"` as `"any"` (Mistral's spelling).
|
|
3
|
+
* @param {any} body - Chat Completions request body.
|
|
4
|
+
* @returns {any} Normalized body.
|
|
5
|
+
*/
|
|
6
|
+
export function request(body: any): any;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flatten typed content chunks: a reasoning model answers with `content` as a list of `text` and `thinking` chunks. The text becomes the
|
|
3
|
+
* string `content` and the thinking becomes `reasoning_content`.
|
|
4
|
+
* @param {any} body - Chat Completions response body.
|
|
5
|
+
* @returns {any} Normalized body.
|
|
6
|
+
*/
|
|
7
|
+
export function response(body: any): any;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/llm-translator
|
|
4
|
+
* @Filename: /src/api/driver/mistral/settings.mjs
|
|
5
|
+
* @Date: 2026-10-31T14:00:00-07:00 (1793480400)
|
|
6
|
+
* @Author: Nate Corcoran <CLDMV>
|
|
7
|
+
* @Email: <Shinrai@users.noreply.github.com>
|
|
8
|
+
* -----
|
|
9
|
+
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
10
|
+
* @Last modified time: 2026-10-31T14:00:00-07:00 (1793480400)
|
|
11
|
+
* -----
|
|
12
|
+
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @fileoverview Mistral driver: translator settings for the hosted Mistral API.
|
|
17
|
+
* @module @cldmv/llm-translator/driver/mistral
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Settings for api.mistral.ai: reasoning is read from typed content chunks (flattened by the response normalizers into `reasoning_content`),
|
|
21
|
+
* no `top_k`, no prefill continuation, and usage on the last content chunk. Reasoning depth is chosen by the model, so no thinking switch is sent.
|
|
22
|
+
* @returns {any} Partial settings.
|
|
23
|
+
*/
|
|
24
|
+
export function settings(): any;
|
package/types/src/api/send.d.mts
CHANGED
|
@@ -17,6 +17,13 @@
|
|
|
17
17
|
* `estimated: { input_tokens, output_tokens }` (null for a side that was reported, and null altogether when nothing was estimated).
|
|
18
18
|
* On the Anthropic front a stream carries them in the final `message_delta`, because `message_start` is sent before the output is known.
|
|
19
19
|
* Replies passed through without conversion are never altered.
|
|
20
|
+
*
|
|
21
|
+
* An OpenAI-front request with `n` above 1 whose upstream speaks the Anthropic protocol (which has no `n`) is answered by `n` parallel
|
|
22
|
+
* single-choice requests, one `send` each (so pools, failover, capture and events apply per branch). The choices come back numbered 0 to
|
|
23
|
+
* n-1 and the usage is the sum of the branches; `onUsage` is called once with the summed report (`reason` and `complete` follow the
|
|
24
|
+
* branch that failed first). The first branch to fail ends the request: the others are aborted and the failure is the reply, or, once
|
|
25
|
+
* a stream has started, an error chunk and `[DONE]`. Aborting the caller's signal aborts every branch. An OpenAI-protocol upstream
|
|
26
|
+
* receives `n` unchanged and answers it itself.
|
|
20
27
|
* @param {any} body - Client request body (per `front`).
|
|
21
28
|
* @param {any} [options] - `{ signal, front, headers, clientHeaders, filter, onUsage, log }`: abort signal for the upstream call, per-call front, extra upstream headers, client headers forwarded to a native Anthropic upstream, the pass-through body filter (see `request`) and the usage callback (an exception in it is reported as a `usage` event and never breaks the reply) and the logger this request's events also go to (used by the server).
|
|
22
29
|
* @returns {Promise<{status: number, headers: Record<string,string>, body: any, usage: Promise<any>} | {status: number, headers: Record<string,string>, stream: AsyncIterable<{event?: string, data: *}>, usage: Promise<any>}>} A JSON reply, or a stream reply when the client asked for streaming and the upstream accepted.
|
|
@@ -19,3 +19,10 @@ export function isLlamacppProps(body: Record<string, any>): boolean;
|
|
|
19
19
|
* @returns {boolean} True when it names a model or tokenizer path.
|
|
20
20
|
*/
|
|
21
21
|
export function isSglangInfo(body: Record<string, any>): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Does a `GET /api/v0/models` reply come from LM Studio? The match is strict so no other server binds: `object: "list"`, a non-empty `data`
|
|
24
|
+
* array, and every entry an `object: "model"` with string `id`, `arch`, `compatibility_type` and `state` and a known `type`.
|
|
25
|
+
* @param {Record<string, any>} body - Reply object (see `objectOf`).
|
|
26
|
+
* @returns {boolean} True for LM Studio's model listing.
|
|
27
|
+
*/
|
|
28
|
+
export function isLmstudioModels(body: Record<string, any>): boolean;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How many branches a request needs: `n` when the OpenAI-front body asks for more than one choice and the router would send it to an
|
|
3
|
+
* Anthropic-protocol upstream, else 0 (the request goes the normal way: an OpenAI-protocol upstream handles `n` itself, and a value
|
|
4
|
+
* that is not an integer above 1 is left to the converter's own validation). The decision is made on the endpoint the router picks
|
|
5
|
+
* first; each branch is then routed and failed over like any request.
|
|
6
|
+
* @param {any} c - Captured translator parts (`capture`).
|
|
7
|
+
* @param {any} body - Client request body.
|
|
8
|
+
* @returns {number} Branch count, or 0 for no fan-out.
|
|
9
|
+
* @throws {TranslatorError} `invalid_request` when `n` is above {@link MAX}.
|
|
10
|
+
*/
|
|
11
|
+
export function wants(c: any, body: any): number;
|
|
12
|
+
/**
|
|
13
|
+
* Add two usage objects field by field (numbers add, nested objects such as `prompt_tokens_details` recurse, a field only one side has is kept).
|
|
14
|
+
* @param {any} left - Usage object.
|
|
15
|
+
* @param {any} right - Usage object.
|
|
16
|
+
* @returns {any} The sum.
|
|
17
|
+
*/
|
|
18
|
+
export function add(left: any, right: any): any;
|
|
19
|
+
/**
|
|
20
|
+
* Combine the replies of the branches of a non-streaming request: the first branch's reply with every branch's choice, numbered by
|
|
21
|
+
* branch, and the usage summed.
|
|
22
|
+
* @param {any[]} bodies - Chat completion bodies, one per branch, in order.
|
|
23
|
+
* @returns {any} The combined body.
|
|
24
|
+
*/
|
|
25
|
+
export function join(bodies: any[]): any;
|
|
26
|
+
/**
|
|
27
|
+
* Merge the streams of the branches into one: chunks go out as they arrive from any branch, each re-indexed to its branch and carrying
|
|
28
|
+
* the first chunk's id; the branches' own `[DONE]` sentinels and usage chunks are held back, and the stream ends with one usage chunk
|
|
29
|
+
* holding the sum (when the branches sent one) and a single `[DONE]`. A branch that sends an error chunk or throws ends the merged
|
|
30
|
+
* stream the way the stream converter ends a failed one: the error chunk, then `[DONE]`; the other branches are cancelled. Closing
|
|
31
|
+
* the merged stream early cancels every branch.
|
|
32
|
+
* @param {AsyncIterable<{event?: string, data: any}>[]} streams - Branch streams, in choice order.
|
|
33
|
+
* @param {() => void} abort - Aborts the branches' requests.
|
|
34
|
+
* @param {(index: number) => void} trip - Called with the index of the branch whose failure ended the stream.
|
|
35
|
+
* @returns {AsyncIterableIterator<{event?: string, data: any}>} Merged stream, closable with `return()`.
|
|
36
|
+
*/
|
|
37
|
+
export function weave(streams: AsyncIterable<{
|
|
38
|
+
event?: string;
|
|
39
|
+
data: any;
|
|
40
|
+
}>[], abort: () => void, trip: (index: number) => void): AsyncIterableIterator<{
|
|
41
|
+
event?: string;
|
|
42
|
+
data: any;
|
|
43
|
+
}>;
|
|
44
|
+
/**
|
|
45
|
+
* Answer a request with `count` parallel single-choice requests. Each branch is a full `send` of the body with `n: 1` (so it is routed,
|
|
46
|
+
* failed over, captured and metered like any request, and its own `onUsage` feeds the combined report). The first branch to fail
|
|
47
|
+
* (an error reply, or a thrown error such as an abort) ends the request: the others are aborted, the failure is the reply (or is
|
|
48
|
+
* rethrown), and the streams of branches that had already started are closed so their usage settles. The caller's signal cancels every
|
|
49
|
+
* branch. A branch that fails after the stream has started is handled by {@link weave}.
|
|
50
|
+
* @param {object} spec - `{ body, count, options, send, warn }`.
|
|
51
|
+
* @param {any} spec.body - Client request body (`n` above 1).
|
|
52
|
+
* @param {number} spec.count - Number of branches.
|
|
53
|
+
* @param {any} spec.options - The `send` options of the request.
|
|
54
|
+
* @param {(body: any, options: any) => Promise<any>} spec.send - The single-request send.
|
|
55
|
+
* @param {(message: string, details: {kind: "threw" | "rejected"}) => void} spec.warn - Receives a message when `onUsage` throws or rejects.
|
|
56
|
+
* @returns {Promise<any>} The reply, with a non-enumerable `usage` promise of the combined report.
|
|
57
|
+
*/
|
|
58
|
+
export function run({ body, count, options, send, warn }: {
|
|
59
|
+
body: any;
|
|
60
|
+
count: number;
|
|
61
|
+
options: any;
|
|
62
|
+
send: (body: any, options: any) => Promise<any>;
|
|
63
|
+
warn: (message: string, details: {
|
|
64
|
+
kind: "threw" | "rejected";
|
|
65
|
+
}) => void;
|
|
66
|
+
}): Promise<any>;
|
|
67
|
+
/** The largest `n` that is fanned out (OpenAI's own limit for the parameter). */
|
|
68
|
+
export const MAX: 128;
|
|
@@ -10,19 +10,30 @@ export function finishError(finishReason: string | null | undefined): {
|
|
|
10
10
|
code: string;
|
|
11
11
|
message: string;
|
|
12
12
|
} | null;
|
|
13
|
+
/**
|
|
14
|
+
* The Anthropic refusal category for the content filter annotations an OpenAI-style server put on a choice. Azure OpenAI (and Foundry) attach
|
|
15
|
+
* `content_filter_results` to each choice (and to streamed chunks): `{ hate: { filtered, severity }, sexual, violence, self_harm, ... }`.
|
|
16
|
+
* A harm category with `filtered: true` is a policy area outside Anthropic's four named ones (`cyber`, `bio`, `frontier_llm`,
|
|
17
|
+
* `reasoning_extraction`), which is what `general_harms` stands for. Everything else (jailbreak, protected material, blocklists, no
|
|
18
|
+
* annotations) gives no category and stays null. No other server the translator knows reports one (see D-50).
|
|
19
|
+
* @param {any} results - A choice's `content_filter_results`, if any.
|
|
20
|
+
* @returns {"general_harms"|null} The category, or null.
|
|
21
|
+
*/
|
|
22
|
+
export function filterCategory(results: any): "general_harms" | null;
|
|
13
23
|
/**
|
|
14
24
|
* Anthropic stop reason (and stop sequence) for an OpenAI choice.
|
|
15
25
|
* `model_length` is `max_tokens`; `eos`, `eos_token` and any other unknown value are `end_turn`. Fatal values are handled by {@link finishError} before this.
|
|
16
26
|
* @param {string|null|undefined} finishReason - OpenAI `finish_reason`.
|
|
17
|
-
* A `content_filter` stop also carries `stop_details` (`{ type: "refusal", category
|
|
18
|
-
* @param {{ hasToolCalls: boolean, matchedStop?: unknown, argumentsComplete?: boolean, refusal?: unknown }} info - Whether tool calls were produced, vLLM's `stop_reason` (a string for a matched stop string), whether the last tool call's arguments are complete,
|
|
27
|
+
* A `content_filter` stop also carries `stop_details` (`{ type: "refusal", category, explanation }`), the explanation being the upstream's refusal text when it sent one and the category what {@link filterCategory} found (else null). No other stop reason has the field.
|
|
28
|
+
* @param {{ hasToolCalls: boolean, matchedStop?: unknown, argumentsComplete?: boolean, refusal?: unknown, category?: string|null }} info - Whether tool calls were produced, vLLM's `stop_reason` (a string for a matched stop string), whether the last tool call's arguments are complete, the upstream's refusal text and the refusal category from {@link filterCategory}. A `length` stop with incomplete arguments is `max_tokens`, not `tool_use`.
|
|
19
29
|
* @returns {{ stop_reason: string, stop_sequence: string|null, stop_details?: { type: string, category: string|null, explanation: string|null } }} Anthropic stop fields.
|
|
20
30
|
*/
|
|
21
|
-
export function toAnthropicStop(finishReason: string | null | undefined, { hasToolCalls, matchedStop, argumentsComplete, refusal }: {
|
|
31
|
+
export function toAnthropicStop(finishReason: string | null | undefined, { hasToolCalls, matchedStop, argumentsComplete, refusal, category }: {
|
|
22
32
|
hasToolCalls: boolean;
|
|
23
33
|
matchedStop?: unknown;
|
|
24
34
|
argumentsComplete?: boolean;
|
|
25
35
|
refusal?: unknown;
|
|
36
|
+
category?: string | null;
|
|
26
37
|
}): {
|
|
27
38
|
stop_reason: string;
|
|
28
39
|
stop_sequence: string | null;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Split typed content chunks into answer text and thinking. `{type:"text"}` chunks make the text, `{type:"thinking"}` chunks make the thinking
|
|
3
|
+
* (their `thinking` field is a list of text chunks, or a string); other chunk types are skipped.
|
|
4
|
+
* @param {any} content - Message or delta `content`.
|
|
5
|
+
* @returns {{ text: string, thinking: string } | null} The parts, or null when `content` is not an array.
|
|
6
|
+
*/
|
|
7
|
+
export function flatten(content: any): {
|
|
8
|
+
text: string;
|
|
9
|
+
thinking: string;
|
|
10
|
+
} | null;
|
|
@@ -6,6 +6,26 @@
|
|
|
6
6
|
* converter writes when the upstream reported nothing and no real reply has).
|
|
7
7
|
*/
|
|
8
8
|
export function normalize(protocol: "anthropic" | "openai" | "folded", usage: any): Record<string, number | null> | null;
|
|
9
|
+
/**
|
|
10
|
+
* Hand a report to the caller's `onUsage`. An exception or a rejection becomes a warning and never reaches the reply.
|
|
11
|
+
* @param {UsageReport} report - The report.
|
|
12
|
+
* @param {((report: UsageReport) => any) | undefined} onUsage - Caller's callback.
|
|
13
|
+
* @param {(message: string, details: {kind: "threw" | "rejected"}) => void} warn - Receives a message when `onUsage` throws or rejects.
|
|
14
|
+
* @returns {void}
|
|
15
|
+
*/
|
|
16
|
+
export function deliver(report: UsageReport, onUsage: ((report: UsageReport) => any) | undefined, warn: (message: string, details: {
|
|
17
|
+
kind: "threw" | "rejected";
|
|
18
|
+
}) => void): void;
|
|
19
|
+
/**
|
|
20
|
+
* Combine the reports of the branches of one fanned-out request (`n` above 1) into the single report the caller gets. Token fields and
|
|
21
|
+
* estimates are summed (null stays null when no branch knew the field), `reported` is true when any branch reported, `complete` only
|
|
22
|
+
* when every branch did, and the reason is the lead branch's (the one that failed first) when there is one, else the first branch that
|
|
23
|
+
* did not finish normally, else `"done"`. The request facts (`model`, `upstream`) come from the first branch that knew them.
|
|
24
|
+
* @param {UsageReport[]} reports - One report per branch, in choice order.
|
|
25
|
+
* @param {number} [lead] - Index of the branch whose failure ended the request, or -1 when none did.
|
|
26
|
+
* @returns {UsageReport} The combined report.
|
|
27
|
+
*/
|
|
28
|
+
export function combine(reports: UsageReport[], lead?: number): UsageReport;
|
|
9
29
|
/**
|
|
10
30
|
* Create the meter for one request.
|
|
11
31
|
* @param {{front: "anthropic" | "openai", requested: string | null, model: string | null, upstream: string | null, stream: boolean}} meta - Request facts; `model` and `upstream` may be filled in later.
|