@cldmv/llm-translator 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.md +6 -6
  2. package/dist/api/convert/anthropic/messages/to/openai/chat/request.mjs +11 -11
  3. package/dist/api/convert/anthropic/messages/to/openai/chat/response.mjs +1 -1
  4. package/dist/api/convert/anthropic/messages/to/openai/chat/stream.mjs +1 -1
  5. package/dist/api/convert/openai/chat/to/anthropic/messages/request.mjs +4 -4
  6. package/dist/api/convert/openai/chat/to/anthropic/messages/response.mjs +1 -1
  7. package/dist/api/convert/openai/chat/to/anthropic/messages/stream.mjs +1 -1
  8. package/dist/api/detect.mjs +1 -1
  9. package/dist/api/driver/anthropic/detect.mjs +1 -1
  10. package/dist/api/driver/anthropic/info.mjs +1 -1
  11. package/dist/api/driver/lmstudio/detect.mjs +2 -0
  12. package/dist/api/driver/lmstudio/info.mjs +2 -0
  13. package/dist/api/driver/lmstudio/request.mjs +2 -0
  14. package/dist/api/driver/lmstudio/settings.mjs +2 -0
  15. package/dist/api/driver/mistral/chunk.mjs +2 -0
  16. package/dist/api/driver/mistral/detect.mjs +2 -0
  17. package/dist/api/driver/mistral/info.mjs +2 -0
  18. package/dist/api/driver/mistral/request.mjs +2 -0
  19. package/dist/api/driver/mistral/response.mjs +2 -0
  20. package/dist/api/driver/mistral/settings.mjs +2 -0
  21. package/dist/api/embeddings/send.mjs +1 -1
  22. package/dist/api/request.mjs +2 -2
  23. package/dist/api/send.mjs +1 -1
  24. package/dist/lib/backends.mjs +1 -1
  25. package/dist/lib/fanout.mjs +2 -0
  26. package/dist/lib/finish.mjs +1 -1
  27. package/dist/lib/http.mjs +1 -1
  28. package/dist/lib/ids.mjs +1 -1
  29. package/dist/lib/record.mjs +2 -2
  30. package/dist/lib/routes.mjs +1 -1
  31. package/dist/lib/typed.mjs +2 -0
  32. package/dist/lib/usage.mjs +1 -1
  33. package/package.json +4 -3
  34. package/types/src/api/convert/anthropic/messages/to/openai/chat/request.d.mts +6 -1
  35. package/types/src/api/convert/openai/chat/to/anthropic/messages/request.d.mts +9 -2
  36. package/types/src/api/driver/anthropic/detect.d.mts +10 -23
  37. package/types/src/api/driver/anthropic/info.d.mts +3 -1
  38. package/types/src/api/driver/lmstudio/detect.d.mts +6 -0
  39. package/types/src/api/driver/lmstudio/info.d.mts +31 -0
  40. package/types/src/api/driver/lmstudio/request.d.mts +6 -0
  41. package/types/src/api/driver/lmstudio/settings.d.mts +23 -0
  42. package/types/src/api/driver/mistral/chunk.d.mts +8 -0
  43. package/types/src/api/driver/mistral/detect.d.mts +26 -0
  44. package/types/src/api/driver/mistral/info.d.mts +35 -0
  45. package/types/src/api/driver/mistral/request.d.mts +6 -0
  46. package/types/src/api/driver/mistral/response.d.mts +7 -0
  47. package/types/src/api/driver/mistral/settings.d.mts +24 -0
  48. package/types/src/api/embeddings/send.d.mts +10 -2
  49. package/types/src/api/send.d.mts +7 -0
  50. package/types/src/lib/backends.d.mts +7 -0
  51. package/types/src/lib/fanout.d.mts +68 -0
  52. package/types/src/lib/finish.d.mts +14 -3
  53. package/types/src/lib/http.d.mts +8 -2
  54. package/types/src/lib/ids.d.mts +40 -2
  55. package/types/src/lib/record.d.mts +7 -2
  56. package/types/src/lib/typed.d.mts +10 -0
  57. package/types/src/lib/usage.d.mts +20 -0
package/dist/lib/ids.mjs CHANGED
@@ -1,2 +1,2 @@
1
- var f=Object.defineProperty;var n=(t,r)=>f(t,"name",{value:r,configurable:!0});import{randomBytes as p,createHash as h}from"node:crypto";function i(t=12){return p(t).toString("hex")}n(i,"random");function $(){return`msg_${i()}`}n($,"messageId");function a(){return`toolu_${i()}`}n(a,"toolUseId");function d(){return`chatcmpl-${i()}`}n(d,"completionId");function x(){return`call_${i()}`}n(x,"callId");function O(t){return typeof t!="string"||t===""?a():t.replace(/[^a-zA-Z0-9_-]/g,"_")}n(O,"anthropicToolId");function b({max:t,map:r={}}){const u=new Map(Object.entries(r).map(([e,c])=>[c,e]));return e=>{if(e.length<=t&&/^[a-zA-Z0-9_-]+$/.test(e))return e;if(u.has(e))return u.get(e);const c=e.replace(/[^a-zA-Z0-9_-]/g,"_");let o=c;if(c.length>t||c===""){const s=h("sha256").update(e).digest("hex").slice(0,8);o=`${c.slice(0,t-9)}_${s}`}const l=o;for(let s=2;Object.hasOwn(r,o);s++)o=`${l.slice(0,t-4)}_${s}`;return r[o]=e,u.set(e,o),o}}n(b,"createNameFitter");function I(t,r){return Object.hasOwn(t,r)?t[r]:r}n(I,"restoreName");export{O as anthropicToolId,x as callId,d as completionId,b as createNameFitter,$ as messageId,I as restoreName,a as toolUseId};
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
@@ -1,3 +1,3 @@
1
- var E=Object.defineProperty;var s=(e,c)=>E(e,"name",{value:c,configurable:!0});import{randomBytes as F}from"node:crypto";import{mkdirSync as O,writeFileSync as T}from"node:fs";import y from"node:path";import{items as v}from"./source.mjs";import{warn as $}from"./warn.mjs";const u=1e6,j=/key|token|secret|auth|cookie|password/i;function m(e){const c={};for(const[r,i]of Object.entries(e??{}))c[r.toLowerCase()]=j.test(r)?"[redacted]":i;return c}s(m,"redact");function L(e){return e===void 0||e===!1||typeof e=="function"||typeof e=="string"&&e!==""}s(L,"valid");const C={raw:s(e=>e,"raw"),events:s(e=>e,"events"),reply(){}};function M({config:e,state:c},r){const i=e.capture;if(!i)return C;const{sent:f}=r,h=F(6).toString("hex"),w=Date.now(),d={time:w,id:h,front:r.front,requested:r.requested,model:f.body?.model??null,endpoint:f.upstream,stream:f.state.stream,client:{body:r.body},upstream:{url:f.url,headers:m(f.headers),body:f.body},response:{}};let a="",g=!1,l=0,p=!1;const S=[],b=s(()=>{try{if(typeof i=="function"){const t=i(d);typeof t?.then=="function"&&t.then(void 0,n=>k(n))}else O(y.resolve(i),{recursive:!0}),T(y.join(y.resolve(i),`${w}-${h}.json`),`${JSON.stringify(d,null,2)}
2
- `)}catch(t){k(t)}},"emit"),k=s(t=>{$(e,c,`Could not capture the exchange (${t instanceof Error?t.message:String(t)}); the reply is not affected`,{type:"warning",kind:"capture",log:r.log})},"problem");return{raw(t){return(async function*(){const n=new TextDecoder;for await(const o of v(t))g||(a+=typeof o=="string"?o:n.decode(o,{stream:!0}),a.length>u&&(a=a.slice(0,u),g=!0,p=!0)),yield o})()},events(t){return(async function*(){try{for await(const n of v(t)){if(l<=u){const o=JSON.stringify(n).length;l+=o,l>u?p=!0:S.push(n)}yield n}}finally{d.response={status:200,headers:m(r.headers),raw:a,events:S,truncated:p},b()}})()},reply(t,n,o){d.response={status:t,headers:m(n),body:o},b()}}}s(M,"start");export{u as STREAM_LIMIT,m as redact,M as start,L as valid};
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
@@ -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,m=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 d=null;for await(const i of s.stream){if(!g){yield i;continue}if(i.event!=="message_start"){if(d===null&&i.event==="content_block_start"&&i.data.content_block.type==="thinking"){d=0;continue}d===null&&(yield*m(),d=1),yield d>0&&typeof i.data?.index=="number"?{...i,data:{...i.data,index:i.data.index+d}}:i}}return}g&&(yield*m()),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,m=C){e.hijack();const d=e.raw;d.writeHead(s,m);try{await o.write(d,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,_))d.write(o.encode(t))}}return d.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 z(e){const o=/^Bearer\s+(.+)$/i.exec(e.authorization??"");return e["x-api-key"]||o?.[1]||""}c(z,"keyOf");function B(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(B,"forwardable");function P(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(P,"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")},m=[o.apiKey??[]].flat().filter(Boolean),d=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 u=t.statusCode??500;return u<500?f(r,s.build(u,t.message)):(n.log.error(t),s.emit({type:"error",kind:"route",level:"error",message:String(t.message),status:u,log:!1}),f(r,s.build(u,"Internal server error")))}),e.addHook("onRequest",async(t,n)=>{if(m.length===0||t.routeOptions.config?.public)return;const r=z(t.headers);if(!(r&&m.some(u=>L(r,u))))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 u;try{u=P(await o.authorize({method:t.method,url:t.url,headers:t.headers,body:t.body,front:s.front,route:r}))}catch(b){t.log.error(b),s.emit({type:"error",kind:"route",level:"error",message:String(b?.message??b),log:!1}),u={ok:!1,status:401,message:"unauthorized"}}if(!u.ok)return f(n,s.build(u.status,u.message));i.set(t,u.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:d},async(t,n)=>{if(n.raw.destroyed)return n;const r=new AbortController;n.raw.on("close",()=>{n.raw.writableFinished||r.abort()});const u=typeof o.onUsage=="function"?h=>o.onUsage(h,i.get(t)):void 0,b=s.interval(t.body),k=s.send(t.body,{signal:r.signal,clientHeaders:B(t.headers),onUsage:u,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?b:0);return y===v?(k.then(h=>h.stream?.return?.(),()=>{}),n):y===p?E(n,s,r,I(k,s,t.body?.model),b):y.stream?E(n,s,r,y.stream,b,y.status,y.headers):f(n,y)}),s.front==="openai"&&e.post("/v1/embeddings",{bodyLimit:d},async(t,n)=>{const r=new AbortController;return n.raw.on("close",()=>{n.raw.writableFinished||r.abort()}),f(n,await s.embed(t.body,{signal:r.signal,log:t.log}))}),s.front==="anthropic"&&e.post("/v1/messages/count_tokens",{bodyLimit:d},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};
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
@@ -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
@@ -1,2 +1,2 @@
1
- var v=Object.defineProperty;var f=(t,o)=>v(t,"name",{value:o,configurable:!0});const m=["input_tokens","output_tokens","cache_read_input_tokens","cache_creation_input_tokens"],l=f(t=>Number.isFinite(t)?t:null,"count");function E(t,o){if(!o||typeof o!="object")return null;let i;if(t==="openai"){const r=l(o.prompt_tokens),a=l(o.prompt_tokens_details?.cached_tokens);i={input_tokens:r===null?null:Math.max(0,r-(a??0)),output_tokens:l(o.completion_tokens),cache_read_input_tokens:a,cache_creation_input_tokens:null}}else if(t==="folded"){const r=l(o.input_tokens);i={input_tokens:r===null?null:r+(l(o.cache_creation_input_tokens)??0),output_tokens:l(o.output_tokens),cache_read_input_tokens:l(o.cache_read_input_tokens)||null,cache_creation_input_tokens:null}}else i=Object.fromEntries(m.map(r=>[r,l(o[r])]));return m.every(r=>!i[r])?null:i}f(E,"normalize");function I(t){return!!t&&typeof t=="object"&&Array.isArray(t.choices)&&t.choices.length===0&&!!t.usage}f(I,"usageChunk");function P(t,o,i){const r=Object.fromEntries(m.map(e=>[e,null]));let a=!1,y=null,d=!1,b=!1,h=!1,p=null,w="openai",j;const g=new Promise(e=>{j=e}),k=f((e,u=t.front)=>{const c=E(u,e);if(c){a=!0;for(const n of m)c[n]!==null&&c[n]!==void 0&&(r[n]=c[n])}},"take"),_=f(e=>{if(d)return;d=!0,p&&k(p(),w);const u=e==="done"&&b?"error":e,c={...r,reported:a,estimated:y,complete:u==="done",reason:u,...t};j(c);try{Promise.resolve(o?.(c)).catch(n=>i(`onUsage rejected: ${n?.message??n}`,{kind:"rejected"}))}catch(n){i(`onUsage threw: ${n?.message??n}`,{kind:"threw"})}},"finish");return{meta:t,usage:g,json(e){h=!0,p||k(e?.usage)},source(e,u="openai"){p=e,w=u},event(e){e==="[DONE]"&&(h=!0),!(!e||typeof e!="object")&&((e.type==="error"||t.front==="openai"&&e.error)&&(b=!0),t.front==="anthropic"?(e.type==="message_stop"&&(h=!0),p||k(e.type==="message_start"?e.message?.usage:e.usage)):p||k(e.usage))},finish:_,estimate(e){y=e},track(e,u,c=!1){const n=e[Symbol.asyncIterator](),x=this;return{[Symbol.asyncIterator](){return this},async next(){try{for(;;){const s=await n.next();if(s.done)_(h?"done":"error");else if(x.event(s.value?.data),c&&I(s.value?.data))continue;return s}}catch(s){throw _(u?.aborted?"aborted":"error"),s}},async return(s){return _("aborted"),n.return?n.return(s):{done:!0,value:s}},async throw(s){if(_("error"),n.throw)return n.throw(s);throw s}}}}}f(P,"meter");export{P as meter,E as normalize};
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.6.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",
@@ -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:dist",
87
- "test:dist": "npm run build && node --conditions=llm-translator-dev --test tests/cjs/entry.test.cjs",
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
- * @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.
12
13
  */
13
- export function request(chatBody: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): Promise<{
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
- * @Project: @cldmv/llm-translator
4
- * @Filename: /src/api/driver/anthropic/detect.mjs
5
- * @Date: 2026-10-25T12:29:36-07:00 (1792956576)
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-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
- } | null;
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
  };
@@ -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,6 @@
1
+ /**
2
+ * Drop tool fields when there are no tools.
3
+ * @param {any} body - Chat Completions request body.
4
+ * @returns {any} Normalized body.
5
+ */
6
+ export function request(body: any): any;
@@ -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;
@@ -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
  }>;
@@ -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: null, explanation }`), the explanation being the upstream's refusal text when it sent one. No other stop reason has the field.
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, and the upstream's refusal text. A `length` stop with incomplete arguments is `max_tokens`, not `tool_use`.
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;
@@ -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
  }>;