@cldmv/llm-translator 0.3.1 → 0.5.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 (43) hide show
  1. package/README.md +13 -7
  2. package/dist/api/convert/anthropic/messages/to/openai/chat/request.mjs +10 -10
  3. package/dist/api/convert/openai/chat/to/anthropic/messages/request.mjs +5 -3
  4. package/dist/api/convert/openai/chat/to/anthropic/messages/response.mjs +1 -1
  5. package/dist/api/convert/openai/chat/to/anthropic/messages/stream.mjs +1 -1
  6. package/dist/api/detect.mjs +1 -1
  7. package/dist/api/driver/vllm/request.mjs +1 -1
  8. package/dist/api/events/off.mjs +2 -0
  9. package/dist/api/events/on.mjs +2 -0
  10. package/dist/api/model/list.mjs +1 -1
  11. package/dist/api/request.mjs +2 -2
  12. package/dist/api/send.mjs +1 -1
  13. package/dist/api/server/plugin.mjs +1 -1
  14. package/dist/index.cjs +1 -1
  15. package/dist/index.mjs +1 -1
  16. package/dist/lib/cli.mjs +7 -6
  17. package/dist/lib/config.mjs +1 -1
  18. package/dist/lib/effort.mjs +2 -0
  19. package/dist/lib/events.mjs +4 -0
  20. package/dist/lib/finish.mjs +1 -1
  21. package/dist/lib/route.mjs +1 -1
  22. package/dist/lib/routes.mjs +1 -1
  23. package/dist/lib/settings.mjs +1 -1
  24. package/dist/lib/usage.mjs +1 -1
  25. package/dist/lib/warn.mjs +1 -1
  26. package/package.json +4 -4
  27. package/types/src/api/convert/anthropic/messages/to/openai/chat/request.d.mts +2 -1
  28. package/types/src/api/convert/openai/chat/to/anthropic/messages/response.d.mts +1 -1
  29. package/types/src/api/driver/vllm/request.d.mts +3 -2
  30. package/types/src/api/events/off.d.mts +7 -0
  31. package/types/src/api/events/on.d.mts +8 -0
  32. package/types/src/api/request.d.mts +7 -2
  33. package/types/src/api/send.d.mts +1 -1
  34. package/types/src/api/server/plugin.d.mts +1 -1
  35. package/types/src/index.d.mts +33 -6
  36. package/types/src/lib/effort.d.mts +69 -0
  37. package/types/src/lib/events.d.mts +86 -0
  38. package/types/src/lib/finish.d.mts +21 -18
  39. package/types/src/lib/route.d.mts +11 -3
  40. package/types/src/lib/routes.d.mts +1 -1
  41. package/types/src/lib/settings.d.mts +22 -2
  42. package/types/src/lib/usage.d.mts +4 -2
  43. package/types/src/lib/warn.d.mts +12 -23
@@ -1,2 +1,2 @@
1
- var A=Object.defineProperty;var i=(e,n)=>A(e,"name",{value:n,configurable:!0});import{timingSafeEqual as H}from"node:crypto";import{TranslatorError as _}from"./errors.mjs";import{messageId as O}from"./ids.mjs";import{sign as S}from"./thinking.mjs";const T=64*1024*1024,j={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"},p=Symbol("stalled"),w=Symbol("gone");async function D(e,n){if(!(n>0))return e;let s;const c=new Promise(f=>{s=setTimeout(f,n,p)});try{return await Promise.race([e,c])}finally{clearTimeout(s)}}i(D,"settle");async function*I(e,n,s){let c=!1;try{const f=n.front==="anthropic";f&&(yield{event:"message_start",data:{type:"message_start",message:{id:O(),type:"message",role:"assistant",model:s,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 a=await e,g=i(function*(){yield{event:"content_block_delta",data:{type:"content_block_delta",index:0,delta:{type:"signature_delta",signature:S()}}},yield{event:"content_block_stop",data:{type:"content_block_stop",index:0}}},"closeEarly");if(a.stream){c=!0;let u=null;for await(const l of a.stream){if(!f){yield l;continue}if(l.event!=="message_start"){if(u===null&&l.event==="content_block_start"&&l.data.content_block.type==="thinking"){u=0;continue}u===null&&(yield*g(),u=1),yield u>0&&typeof l.data?.index=="number"?{...l,data:{...l.data,index:l.data.index+u}}:l}}return}f&&(yield*g()),yield*x(n,a.body)}finally{c||e.then(f=>f.stream?.return?.(),()=>{})}}i(I,"late");function*x(e,n){if(e.front==="anthropic"){yield{event:"error",data:n};return}yield{data:n},yield{data:"[DONE]"}}i(x,"failure");async function v(e,n,s,c,f=200,a=j){e.hijack();const g=e.raw;g.writeHead(f,a);try{await n.write(g,c,{front:n.front,interval:n.interval})}catch(u){if(!s.signal.aborted){const l=u instanceof _;l||e.log.error(u);const{body:m}=n.build(l?u.status:502,l?u.message:"Internal server error",l?{type:u.type,code:u.code}:{});for(const k of x(n,m))g.write(n.encode(k))}}return g.end(),e}i(v,"stream");function L(e,n){const s=Buffer.from(e),c=Buffer.from(n);return s.length===c.length&&H(s,c)}i(L,"same");function z(e){const n=/^Bearer\s+(.+)$/i.exec(e.authorization??"");return e["x-api-key"]||n?.[1]||""}i(z,"keyOf");function B(e){const n={};for(const[s,c]of Object.entries(e))(s==="anthropic-beta"||s==="anthropic-version"||s.startsWith("x-claude-code-"))&&(n[s]=String(c));return n}i(B,"forwardable");function C(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 n=e&&typeof e=="object"&&e.ok===!1?e:{};return{ok:!1,status:Number.isInteger(n.status)&&n.status>=400&&n.status<=599?n.status:401,message:typeof n.message=="string"&&n.message!==""?n.message:"unauthorized"}}i(C,"verdictOf");function R(e,n,s){const{front:c,filter:f}=s,a={...s,build:i((t,o,r={})=>s.build(t,o,{...r,front:c}),"build"),send:i((t,o)=>s.send(t,{...o,front:c,filter:f}),"send"),route:i(t=>s.route(t,{front:c}),"route"),count:i((t,o)=>s.count(t,{...o,front:c}),"count"),list:i(t=>s.list({...t,front:c}),"list"),get:i((t,o)=>s.get(t,{...o,front:c}),"get")},g=[n.apiKey??[]].flat().filter(Boolean),u=n.bodyLimit??T,l=new WeakMap,m=i((t,o)=>t.code(o.status).headers(o.headers).send(o.body),"reject");e.setErrorHandler((t,o,r)=>{if(t instanceof _)return m(r,a.build(t.status,t.message,{type:t.type,code:t.code,param:t.param}));const d=t.statusCode??500;return d<500?m(r,a.build(d,t.message)):(o.log.error(t),m(r,a.build(d,"Internal server error")))}),e.addHook("onRequest",async(t,o)=>{if(g.length===0||t.routeOptions.config?.public)return;const r=z(t.headers);if(!(r&&g.some(d=>L(r,d))))return m(o,a.build(401,"invalid x-api-key"))}),typeof n.authorize=="function"&&e.addHook("preHandler",async(t,o)=>{if(t.routeOptions.config?.public)return;let r=null;if(t.body&&typeof t.body=="object"&&!Array.isArray(t.body))try{r=a.route(t.body)}catch{}let d;try{d=C(await n.authorize({method:t.method,url:t.url,headers:t.headers,body:t.body,front:a.front,route:r}))}catch(y){t.log.error(y),d={ok:!1,status:401,message:"unauthorized"}}if(!d.ok)return m(o,a.build(d.status,d.message));l.set(t,d.context)}),e.route({method:["GET","HEAD"],url:"/",config:{public:!0},handler:i(async(t,o)=>o.send({ok:!0}),"handler")}),e.route({method:["GET","HEAD"],url:"/api/hello",config:{public:!0},handler:i(async(t,o)=>o.send({ok:!0}),"handler")}),e.get("/v1/models",async(t,o)=>{const r=new AbortController;return o.raw.on("close",()=>r.abort()),a.list({signal:r.signal})}),e.get("/v1/models/*",async(t,o)=>{const r=new AbortController;return o.raw.on("close",()=>r.abort()),a.get(t.params["*"],{signal:r.signal})});const k=a.front==="anthropic"?"/v1/messages":"/v1/chat/completions";e.post(k,{bodyLimit:u},async(t,o)=>{if(o.raw.destroyed)return o;const r=new AbortController;o.raw.on("close",()=>{o.raw.writableFinished||r.abort()});const d=typeof n.onUsage=="function"?h=>n.onUsage(h,l.get(t)):void 0,y=a.send(t.body,{signal:r.signal,clientHeaders:B(t.headers),onUsage:d});y.catch(()=>{});const E=new Promise(h=>r.signal.addEventListener("abort",()=>h(w),{once:!0})),b=await D(Promise.race([y,E]),t.body?.stream===!0?a.interval:0);return b===w?(y.then(h=>h.stream?.return?.(),()=>{}),o):b===p?v(o,a,r,I(y,a,t.body?.model)):b.stream?v(o,a,r,b.stream,b.status,b.headers):m(o,b)}),a.front==="anthropic"&&e.post("/v1/messages/count_tokens",{bodyLimit:u},async(t,o)=>{const r=new AbortController;return o.raw.on("close",()=>r.abort()),a.count(t.body,{signal:r.signal})})}i(R,"register");export{I as late,R as register};
1
+ var A=Object.defineProperty;var c=(e,n)=>A(e,"name",{value:n,configurable:!0});import{timingSafeEqual as H}from"node:crypto";import{TranslatorError as p}from"./errors.mjs";import{messageId as O}from"./ids.mjs";import{sign as T}from"./thinking.mjs";const j=64*1024*1024,D={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"},w=Symbol("stalled"),v=Symbol("gone");async function I(e,n){if(!(n>0))return e;let a;const l=new Promise(f=>{a=setTimeout(f,n,w)});try{return await Promise.race([e,l])}finally{clearTimeout(a)}}c(I,"settle");async function*L(e,n,a){let l=!1;try{const f=n.front==="anthropic";f&&(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 u=null;for await(const i of s.stream){if(!f){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*m(),u=1),yield u>0&&typeof i.data?.index=="number"?{...i,data:{...i.data,index:i.data.index+u}}:i}}return}f&&(yield*m()),yield*x(n,s.body)}finally{l||e.then(f=>f.stream?.return?.(),()=>{})}}c(L,"late");function*x(e,n){if(e.front==="anthropic"){yield{event:"error",data:n};return}yield{data:n},yield{data:"[DONE]"}}c(x,"failure");async function E(e,n,a,l,f,s=200,m=D){e.hijack();const u=e.raw;u.writeHead(s,m);try{await n.write(u,l,{front:n.front,interval:f})}catch(i){if(!a.signal.aborted){const g=i instanceof p;g||(e.log.error(i),n.emit({type:"error",kind:"route",level:"error",message:String(i?.message??i),log:!1}));const{body:_}=n.build(g?i.status:502,g?i.message:"Internal server error",g?{type:i.type,code:i.code}:{});for(const t of x(n,_))u.write(n.encode(t))}}return u.end(),e}c(E,"stream");function z(e,n){const a=Buffer.from(e),l=Buffer.from(n);return a.length===l.length&&H(a,l)}c(z,"same");function B(e){const n=/^Bearer\s+(.+)$/i.exec(e.authorization??"");return e["x-api-key"]||n?.[1]||""}c(B,"keyOf");function C(e){const n={};for(const[a,l]of Object.entries(e))(a==="anthropic-beta"||a==="anthropic-version"||a.startsWith("x-claude-code-"))&&(n[a]=String(l));return n}c(C,"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 n=e&&typeof e=="object"&&e.ok===!1?e:{};return{ok:!1,status:Number.isInteger(n.status)&&n.status>=400&&n.status<=599?n.status:401,message:typeof n.message=="string"&&n.message!==""?n.message:"unauthorized"}}c(P,"verdictOf");function W(e,n,a){const{front:l,filter:f}=a,s={...a,build:c((t,o,r={})=>a.build(t,o,{...r,front:l}),"build"),send:c((t,o)=>a.send(t,{...o,front:l,filter:f}),"send"),route:c(t=>a.route(t,{front:l}),"route"),count:c((t,o)=>a.count(t,{...o,front:l}),"count"),list:c(t=>a.list({...t,front:l}),"list"),get:c((t,o)=>a.get(t,{...o,front:l}),"get")},m=[n.apiKey??[]].flat().filter(Boolean),u=n.bodyLimit??j,i=new WeakMap,g=c((t,o)=>t.code(o.status).headers(o.headers).send(o.body),"reject");e.setErrorHandler((t,o,r)=>{if(t instanceof p)return g(r,s.build(t.status,t.message,{type:t.type,code:t.code,param:t.param}));const d=t.statusCode??500;return d<500?g(r,s.build(d,t.message)):(o.log.error(t),s.emit({type:"error",kind:"route",level:"error",message:String(t.message),status:d,log:!1}),g(r,s.build(d,"Internal server error")))}),e.addHook("onRequest",async(t,o)=>{if(m.length===0||t.routeOptions.config?.public)return;const r=B(t.headers);if(!(r&&m.some(d=>z(r,d))))return g(o,s.build(401,"invalid x-api-key"))}),typeof n.authorize=="function"&&e.addHook("preHandler",async(t,o)=>{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=P(await n.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}),d={ok:!1,status:401,message:"unauthorized"}}if(!d.ok)return g(o,s.build(d.status,d.message));i.set(t,d.context)}),e.route({method:["GET","HEAD"],url:"/",config:{public:!0},handler:c(async(t,o)=>o.send({ok:!0}),"handler")}),e.route({method:["GET","HEAD"],url:"/api/hello",config:{public:!0},handler:c(async(t,o)=>o.send({ok:!0}),"handler")}),e.get("/v1/models",async(t,o)=>{const r=new AbortController;return o.raw.on("close",()=>r.abort()),s.list({signal:r.signal})}),e.get("/v1/models/*",async(t,o)=>{const r=new AbortController;return o.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,o)=>{if(o.raw.destroyed)return o;const r=new AbortController;o.raw.on("close",()=>{o.raw.writableFinished||r.abort()});const d=typeof n.onUsage=="function"?h=>n.onUsage(h,i.get(t)):void 0,b=s.interval(t.body),k=s.send(t.body,{signal:r.signal,clientHeaders:C(t.headers),onUsage:d,log:t.log});k.catch(()=>{});const S=new Promise(h=>r.signal.addEventListener("abort",()=>h(v),{once:!0})),y=await I(Promise.race([k,S]),t.body?.stream===!0?b:0);return y===v?(k.then(h=>h.stream?.return?.(),()=>{}),o):y===w?E(o,s,r,L(k,s,t.body?.model),b):y.stream?E(o,s,r,y.stream,b,y.status,y.headers):g(o,y)}),s.front==="anthropic"&&e.post("/v1/messages/count_tokens",{bodyLimit:u},async(t,o)=>{const r=new AbortController;return o.raw.on("close",()=>r.abort()),s.count(t.body,{signal:r.signal})})}c(W,"register");export{L as late,W as register};
2
2
  //# sourceMappingURL=routes.mjs.map
@@ -1,2 +1,2 @@
1
- var l=Object.defineProperty;var r=(o,e)=>l(o,"name",{value:e,configurable:!0});import{TranslatorError as s}from"./errors.mjs";const i=Object.freeze({inlineSystem:"auto",stripBillingHeader:!0,reasoningField:"reasoning",thinkTags:!0,thinkOpen:!1,reasoningRoundTrip:!1,thinking:"chat_template_kwargs",thinkingBudget:!1,effort:!1,images:!0,imageFallback:!0,toolFallback:!1,documents:"extract",documentConverter:null,pdf:Object.freeze({pages:!0,maxPages:100,maxBytes:32*1024*1024,maxImages:20,scale:1.5}),toolSchema:"pass",deferredTools:"forward",toolSearch:"expand",toolErrors:!1,maxTokensField:"max_completion_tokens",streamUsage:!0,continuousUsage:!1,prefill:"continue_final_message",topK:!0,responseFormat:!0,parallelToolCalls:!0,unknownFields:"drop",pingInterval:1e4,toolStreaming:"buffer",effortPassthrough:!1,passthroughFilter:!1,contextWindow:0,defaultMaxTokens:8192}),f={inlineSystem:["auto","keep","hoist","user","drop"],reasoningField:["reasoning","reasoning_content"],thinking:["chat_template_kwargs","reasoning_effort","none"],documents:["extract","note","error"],toolSchema:["pass","sanitize"],deferredTools:["forward","drop"],toolSearch:["expand","inline","native"],maxTokensField:["max_completion_tokens","max_tokens"],prefill:["continue_final_message","none"],unknownFields:["drop","error"],toolStreaming:["buffer","stream"]},d=["effortPassthrough","passthroughFilter","imageFallback","toolFallback","thinkOpen","reasoningRoundTrip"];function p(...o){const e={...i,pdf:{...i.pdf}};for(const t of o)if(t)for(const[n,a]of Object.entries(t))a!==void 0&&(n==="pdf"?Object.assign(e.pdf,a):e[n]=a);for(const[t,n]of Object.entries(f))if(!n.includes(e[t]))throw new s(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected one of ${n.join(", ")})`,{code:"invalid_setting",status:500,details:{key:t,allowed:n}});for(const t of d)if(typeof e[t]!="boolean")throw new s(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected true or false)`,{code:"invalid_setting",status:500,details:{key:t}});if(!(Number.isFinite(e.pdf.scale)&&e.pdf.scale>0))throw new s(`Invalid setting pdf.scale: ${JSON.stringify(e.pdf.scale)} (expected a positive number)`,{code:"invalid_setting",status:500,details:{key:"pdf.scale"}});return e}r(p,"resolve");export{i as DEFAULTS,p as resolve};
1
+ var l=Object.defineProperty;var s=(n,e)=>l(n,"name",{value:e,configurable:!0});import{TranslatorError as r}from"./errors.mjs";const i=Object.freeze({inlineSystem:"auto",stripBillingHeader:!0,reasoningField:"reasoning",thinkTags:!0,thinkOpen:!1,reasoningRoundTrip:!1,thinking:"chat_template_kwargs",thinkingBudget:!1,effort:!1,images:!0,imageFallback:!0,toolFallback:!1,documents:"extract",documentConverter:null,pdf:Object.freeze({pages:!0,maxPages:100,maxBytes:32*1024*1024,maxImages:20,scale:1.5}),toolSchema:"pass",deferredTools:"forward",toolSearch:"expand",jsonObject:"schema",jsonObjectFallback:!0,toolErrors:!1,toolErrorMarker:"[error] ",toolErrorInference:!1,maxTokensField:"max_completion_tokens",streamUsage:!0,continuousUsage:!1,prefill:"continue_final_message",topK:!0,responseFormat:!0,parallelToolCalls:!0,unknownFields:"drop",pingInterval:1e4,toolStreaming:"buffer",effortPassthrough:!1,passthroughFilter:!1,contextWindow:0,defaultMaxTokens:8192}),f={inlineSystem:["auto","keep","hoist","user","drop"],reasoningField:["reasoning","reasoning_content"],thinking:["chat_template_kwargs","reasoning_effort","none"],documents:["extract","note","error"],toolSchema:["pass","sanitize"],deferredTools:["forward","drop"],toolSearch:["expand","inline","native"],jsonObject:["schema","instruction","drop"],maxTokensField:["max_completion_tokens","max_tokens"],prefill:["continue_final_message","none"],unknownFields:["drop","error"],toolStreaming:["buffer","stream"]},c=["effortPassthrough","passthroughFilter","imageFallback","toolFallback","jsonObjectFallback","thinkOpen","reasoningRoundTrip","toolErrorInference"];function u(...n){const e={...i,pdf:{...i.pdf}};for(const t of n)if(t)for(const[o,a]of Object.entries(t))a!==void 0&&(o==="pdf"?Object.assign(e.pdf,a):e[o]=a);for(const[t,o]of Object.entries(f))if(!o.includes(e[t]))throw new r(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected one of ${o.join(", ")})`,{code:"invalid_setting",status:500,details:{key:t,allowed:o}});for(const t of c)if(typeof e[t]!="boolean")throw new r(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected true or false)`,{code:"invalid_setting",status:500,details:{key:t}});if(typeof e.toolErrorMarker!="string"||e.toolErrorMarker.trim()==="")throw new r(`Invalid setting toolErrorMarker: ${JSON.stringify(e.toolErrorMarker)} (expected a non-empty string)`,{code:"invalid_setting",status:500,details:{key:"toolErrorMarker"}});if(!(Number.isFinite(e.pdf.scale)&&e.pdf.scale>0))throw new r(`Invalid setting pdf.scale: ${JSON.stringify(e.pdf.scale)} (expected a positive number)`,{code:"invalid_setting",status:500,details:{key:"pdf.scale"}});return e}s(u,"resolve");export{i as DEFAULTS,u as resolve};
2
2
  //# sourceMappingURL=settings.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}`))}catch(n){i(`onUsage threw: ${n?.message??n}`)}},"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 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};
2
2
  //# sourceMappingURL=usage.mjs.map
package/dist/lib/warn.mjs CHANGED
@@ -1,2 +1,2 @@
1
- var t=Object.defineProperty;var l=(n,r)=>t(n,"name",{value:r,configurable:!0});function d(n,r,a){return n.silent||r.warned.has(a)?!1:(r.warned.add(a),console.warn(`[llm-translator] ${a}`),!0)}l(d,"warn");export{d as warn};
1
+ var o=Object.defineProperty;var e=(n,r)=>o(n,"name",{value:r,configurable:!0});import{emit as a}from"./events.mjs";function w(n,r,t,i={}){a({config:n,state:r},{type:"warning",...i,level:"warn",message:t})}e(w,"warn");export{w as warn};
2
2
  //# sourceMappingURL=warn.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cldmv/llm-translator",
3
- "version": "0.3.1",
3
+ "version": "0.5.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",
@@ -25,12 +25,12 @@
25
25
  },
26
26
  "sideEffects": false,
27
27
  "engines": {
28
- "node": ">=24.0.0"
28
+ "node": ">=22.15.0"
29
29
  },
30
30
  "devEngines": {
31
31
  "runtime": {
32
32
  "name": "node",
33
- "version": ">=24.0.0",
33
+ "version": ">=22.22.0",
34
34
  "onFail": "error"
35
35
  }
36
36
  },
@@ -110,7 +110,7 @@
110
110
  "unpdf": "^2.1.0"
111
111
  },
112
112
  "devDependencies": {
113
- "@cldmv/configs": "^1.3.0",
113
+ "@cldmv/configs": "^1.4.0",
114
114
  "@cldmv/eslint-plugin-jsonv": "^1.0.17",
115
115
  "@cldmv/fix-headers": "^2.2.2",
116
116
  "@cldmv/jsonv": "^1.1.5",
@@ -5,13 +5,14 @@
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> }} [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).
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.
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
  */
12
12
  export function request(body: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>, context?: {
13
13
  producer?: string;
14
14
  budget?: ReturnType<typeof createBudget>;
15
+ listed?: boolean;
15
16
  }): Promise<{
16
17
  body: any;
17
18
  state: any;
@@ -9,6 +9,6 @@
9
9
  * @param {any} [state] - State from the request converter (original model, tool-name map).
10
10
  * @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
11
11
  * @returns {any} Anthropic message.
12
- * @throws {TranslatorError} When the completion has no choices.
12
+ * @throws {TranslatorError} When the completion has no choices, or its `finish_reason` is a fatal one (`abort`, `aborted`, `error`, `insufficient_system_resource`).
13
13
  */
14
14
  export function response(completion: any, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): any;
@@ -1,8 +1,9 @@
1
1
  /**
2
2
  * Drop tool fields vLLM rejects when no tools are present, and clamp `reasoning_effort` to what vLLM accepts
3
- * (`xhigh` and `max` become `high`) unless the `effortPassthrough` setting is on.
3
+ * (`xhigh` and `max` become `high`) unless the `effortPassthrough` setting is on or the model has a configured level list (`ctx.levels`,
4
+ * which `request` has already fitted the effort to).
4
5
  * @param {any} body - Chat Completions request body.
5
- * @param {any} [ctx] - `{ settings }`.
6
+ * @param {any} [ctx] - `{ settings, levels }`.
6
7
  * @returns {any} Normalized body.
7
8
  */
8
9
  export function request(body: any, ctx?: any): any;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Remove a listener added with `events.on`. A listener that is not subscribed is ignored.
3
+ * @param {string} name - Event name or `"*"`.
4
+ * @param {Function} fn - The listener passed to `events.on`.
5
+ * @returns {void}
6
+ */
7
+ export function off(name: string, fn: Function): void;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Subscribe to an event (see docs/events.md for the catalogue). `"*"` receives every occurrence once. The listener only observes: an
3
+ * exception or a rejection is contained and reported once through logging.
4
+ * @param {string} name - Event name (`log`, `warning`, `error`, `fallback`, `failover`, `effort`, `dropped`, `detect`, `usage`) or `"*"`.
5
+ * @param {(event: import("../../lib/events.mjs").TranslatorEvent) => any} fn - Listener.
6
+ * @returns {() => void} Function that removes the listener.
7
+ */
8
+ export function on(name: string, fn: (event: import("../../lib/events.mjs").TranslatorEvent) => any): () => void;
@@ -2,8 +2,8 @@
2
2
  * Route a client request body (in the configured `front` protocol) to an upstream and convert it to that upstream's
3
3
  * protocol. Does no I/O.
4
4
  * @param {any} body - Client request body (Anthropic Messages or OpenAI Chat Completions, per `front`).
5
- * @param {any} [options] - `{ front, headers, clientHeaders, filter, images, usage }`. `front` overrides the configured front for this call, `headers` are extra upstream headers, `clientHeaders` are client headers forwarded only to a native Anthropic upstream, `filter` restricts a forwarded OpenAI body to known fields (default: the `passthroughFilter` setting, off), `images: false` sends the request without images (used by the image fallback), `endpoint` sends to that member of the model's pool (used for failover; default: the first in the pool's order), `hold: true` (used by `send`) advances a round robin and takes the chosen endpoint's in-flight slot, which the caller then releases, `tools: false` sends the request without tools (used by the tool fallback), `usage: true` (used by `send`) makes a streaming OpenAI-front request always ask the upstream for its final usage chunk, `budget` (from `createBudget`, one per `send` for all its attempts) limits the cost of decoding reasoning signatures.
6
- * @returns {Promise<{upstream: string, method: string, url: string, path: string, headers: Record<string,string>, body: any, state: any, pool: string[], images: {sent: boolean, retry: boolean, key: string}, tools: {sent: boolean, retry: boolean}}>} The upstream request and the state `response`/`stream`/`error` need. `pool` lists the names of the endpoints serving the model in the order they are tried (`upstream` is the first); `images` tells `send` whether the body carries images, whether a refusal may be retried without them and the key under which a refusal is remembered; `tools` tells it whether the body carries tools and whether a tool-parser refusal may be retried without them.
5
+ * @param {any} [options] - `{ front, headers, clientHeaders, filter, images, usage }`. `front` overrides the configured front for this call, `headers` are extra upstream headers, `clientHeaders` are client headers forwarded only to a native Anthropic upstream, `filter` restricts a forwarded OpenAI body to known fields (default: the `passthroughFilter` setting, off), `images: false` sends the request without images (used by the image fallback), `endpoint` sends to that member of the model's pool (used for failover; default: the first in the pool's order), `hold: true` (used by `send`) advances a round robin and takes the chosen endpoint's in-flight slot, which the caller then releases, `jsonObject: "instruction"` sends a `json_object` response format as a system instruction (used by the JSON fallback, and applied by itself once a refusal is remembered for the endpoint and model), `tools: false` sends the request without tools (used by the tool fallback), `usage: true` (used by `send`) makes a streaming OpenAI-front request always ask the upstream for its final usage chunk, `retry: true` (used by `send` for the same request sent again) keeps the `dropped` event from repeating, `log` is the logger events of this request also go to (used by the server), `budget` (from `createBudget`, one per `send` for all its attempts) limits the cost of decoding reasoning signatures.
6
+ * @returns {Promise<{upstream: string, method: string, url: string, path: string, headers: Record<string,string>, body: any, state: any, pool: string[], images: {sent: boolean, retry: boolean, key: string}, tools: {sent: boolean, retry: boolean}, json: {sent: boolean, retry: boolean, key: string}}>} The upstream request and the state `response`/`stream`/`error` need. `pool` lists the names of the endpoints serving the model in the order they are tried (`upstream` is the first); `images` tells `send` whether the body carries images, whether a refusal may be retried without them and the key under which a refusal is remembered; `tools` tells it whether the body carries tools and whether a tool-parser refusal may be retried without them; `json` tells it whether the body carries the schema emulation of `json_object`, whether a refusal may be retried with the instruction and the key under which a refusal is remembered.
7
7
  */
8
8
  export function request(body: any, options?: any): Promise<{
9
9
  upstream: string;
@@ -23,4 +23,9 @@ export function request(body: any, options?: any): Promise<{
23
23
  sent: boolean;
24
24
  retry: boolean;
25
25
  };
26
+ json: {
27
+ sent: boolean;
28
+ retry: boolean;
29
+ key: string;
30
+ };
26
31
  }>;
@@ -18,7 +18,7 @@
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
20
  * @param {any} body - Client request body (per `front`).
21
- * @param {any} [options] - `{ signal, front, headers, clientHeaders, filter, onUsage }`: 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 warning and never breaks the reply).
21
+ * @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
22
  * @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.
23
23
  */
24
24
  export function send(body: any, options?: any): Promise<{
@@ -3,7 +3,7 @@
3
3
  * `GET|HEAD /` and `/api/hello`. OpenAI front: `POST /v1/chat/completions`, `GET /v1/models`, `GET /v1/models/{id}`, `GET|HEAD /`. Responses stream as
4
4
  * `text/event-stream` with keep-alive pings; a client disconnect aborts the upstream request. Register it with
5
5
  * `app.register(api.server.plugin, { apiKey })`. `authorize(request)` (sync or async) runs after `apiKey` and the body parse and decides
6
- * with `true` / `{ ok: true, context }` or `false` / `{ ok: false, status, message }`; `onUsage(report, context)` receives each request's usage report.
6
+ * with `true` / `{ ok: true, context }` or `false` / `{ ok: false, status, message }`; `onUsage(report, context)` receives each request's usage report. When the Fastify `logger` is enabled, events also go to it as structured lines (`{ event }` plus the message), through the request's logger for events of a request.
7
7
  * @param {any} fastify - Fastify instance.
8
8
  * @param {any} [options] - `{ front, apiKey, bodyLimit, authorize, onUsage }`. `authorize(request)` and `onUsage(report, context)` are described above. `front` sets the protocol this plugin instance serves (default: the translator's front, other plugin instances and `api.send` are unaffected); `apiKey` (string or array) is required from clients. Forwarded OpenAI bodies are restricted to known fields unless the `passthroughFilter` setting is `false`.
9
9
  * @returns {Promise<void>} Resolves once the routes are registered.
@@ -2,20 +2,24 @@
2
2
  * @typedef {object} TranslatorConfig
3
3
  * @property {"anthropic" | "openai"} [front] Client protocol the translator speaks (default `"anthropic"`).
4
4
  * @property {Array<string | import("./lib/config.mjs").UpstreamConfig>} upstreams Upstream servers: `{ name, url, apiKey, headers, backend, models, default, settings, version }` (or a bare URL).
5
- * @property {Record<string, string>} [aliases] Client model name to upstream model name.
5
+ * @property {Record<string, string | {model: string, effort?: "minimal"|"low"|"medium"|"high"|"xhigh"|"max", thinking?: boolean}>} [aliases] Client model name to upstream model name, or to `{ model, effort, thinking }` to pin those settings over the request's.
6
+ * @property {Record<string, string[] | {levels: string[], default?: string | null}>} [efforts] Reasoning effort levels each model supports, keyed by model name after aliases (see docs/settings.md).
6
7
  * @property {import("./lib/pool.mjs").PoolConfig} [pool] Endpoint pools: `{ strategy, backoff: { base, max }, models }` (see docs/settings.md).
7
8
  * @property {Record<string, any>} [settings] Global converter settings (see `lib/settings.mjs`).
8
9
  * @property {boolean} [detect] Probe `auto` upstreams at startup (default true).
9
10
  * @property {number} [detectTimeout] Probe timeout in ms (default 5000).
10
11
  * @property {(url: string, init?: any) => Promise<Response>} [transport] fetch-compatible function used for all upstream calls.
11
- * @property {boolean} [silent] Suppress warnings.
12
+ * @property {boolean | string} [logs] Logging (default `false`: no console output, no file). `true` prints warnings and errors on the console, once per message; a path appends every event as a JSON line to that file (relative paths resolve against `process.cwd()`, a missing directory is created, a file that cannot be opened is reported once on the console and logging stays off).
13
+ * @property {boolean} [events] Emit events (default `true`); `false` turns emitting off. Logging does not depend on it.
14
+ * @property {(event: import("./lib/events.mjs").TranslatorEvent) => any} [onEvent] Callback for every event, registered before startup so it also sees startup events (see docs/events.md).
15
+ * @property {boolean} [silent] Deprecated alias for `logs: false` (the default); `logs` wins when both are given.
12
16
  * @property {{import?: (url: string) => Promise<any>}} [slothlet] Options forwarded to slothlet (for example `import` for coverage).
13
17
  */
14
18
  /**
15
19
  * Create a translator: a slothlet api instance (see the README for the leaves) bound to the given upstreams.
16
20
  * Endpoints with `backend: "auto"` are probed once before this resolves (set `detect: false` to skip all network access).
17
21
  * @param {TranslatorConfig} config - Translator config.
18
- * @returns {Promise<any>} The translator api (`request`, `response`, `stream`, `send`, `detect`, `status`, `token`, `model`, `error`, `sse`, `server`, `driver`, `convert`, `slothlet`).
22
+ * @returns {Promise<any>} The translator api (`request`, `response`, `stream`, `send`, `detect`, `status`, `token`, `model`, `error`, `events`, `sse`, `server`, `driver`, `convert`, `slothlet`).
19
23
  */
20
24
  export function createTranslator(config: TranslatorConfig): Promise<any>;
21
25
  export { TranslatorError } from "./lib/errors.mjs";
@@ -30,9 +34,20 @@ export type TranslatorConfig = {
30
34
  */
31
35
  upstreams: Array<string | import("./lib/config.mjs").UpstreamConfig>;
32
36
  /**
33
- * Client model name to upstream model name.
37
+ * Client model name to upstream model name, or to `{ model, effort, thinking }` to pin those settings over the request's.
34
38
  */
35
- aliases?: Record<string, string>;
39
+ aliases?: Record<string, string | {
40
+ model: string;
41
+ effort?: "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
42
+ thinking?: boolean;
43
+ }>;
44
+ /**
45
+ * Reasoning effort levels each model supports, keyed by model name after aliases (see docs/settings.md).
46
+ */
47
+ efforts?: Record<string, string[] | {
48
+ levels: string[];
49
+ default?: string | null;
50
+ }>;
36
51
  /**
37
52
  * Endpoint pools: `{ strategy, backoff: { base, max }, models }` (see docs/settings.md).
38
53
  */
@@ -54,7 +69,19 @@ export type TranslatorConfig = {
54
69
  */
55
70
  transport?: (url: string, init?: any) => Promise<Response>;
56
71
  /**
57
- * Suppress warnings.
72
+ * Logging (default `false`: no console output, no file). `true` prints warnings and errors on the console, once per message; a path appends every event as a JSON line to that file (relative paths resolve against `process.cwd()`, a missing directory is created, a file that cannot be opened is reported once on the console and logging stays off).
73
+ */
74
+ logs?: boolean | string;
75
+ /**
76
+ * Emit events (default `true`); `false` turns emitting off. Logging does not depend on it.
77
+ */
78
+ events?: boolean;
79
+ /**
80
+ * Callback for every event, registered before startup so it also sees startup events (see docs/events.md).
81
+ */
82
+ onEvent?: (event: import("./lib/events.mjs").TranslatorEvent) => any;
83
+ /**
84
+ * Deprecated alias for `logs: false` (the default); `logs` wins when both are given.
58
85
  */
59
86
  silent?: boolean;
60
87
  /**
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Validate and normalize the `efforts` config: model name to a list of levels, or `{ levels, default }`.
3
+ * @param {any} [raw] - Raw `efforts` value.
4
+ * @returns {Record<string, EffortConfig>} Normalized map.
5
+ * @throws {TranslatorError} `INVALID_CONFIG` for an unknown level, an empty list or a default that is not in the list.
6
+ */
7
+ export function normalizeEfforts(raw?: any): Record<string, EffortConfig>;
8
+ /**
9
+ * Split the `aliases` config into the plain name map and the pinned settings. An alias is a model name or `{ model, effort, thinking }`.
10
+ * @param {any} [raw] - Raw `aliases` value.
11
+ * @returns {{aliases: Record<string, string>, pins: Record<string, {effort?: string, thinking?: boolean}>}} Name map and pins.
12
+ * @throws {TranslatorError} `INVALID_CONFIG` for an object alias without a `model`, an unknown `effort` or a non-boolean `thinking`.
13
+ */
14
+ export function normalizeAliases(raw?: any): {
15
+ aliases: Record<string, string>;
16
+ pins: Record<string, {
17
+ effort?: string;
18
+ thinking?: boolean;
19
+ }>;
20
+ };
21
+ /**
22
+ * The supported level nearest to a wanted one on the scale; a tie goes to the higher level.
23
+ * @param {string[]} levels - Supported levels.
24
+ * @param {string} wanted - A level from {@link LEVELS}.
25
+ * @returns {string} Supported level.
26
+ */
27
+ export function nearest(levels: string[], wanted: string): string;
28
+ /**
29
+ * The level sent when a request names none.
30
+ * @param {EffortConfig} entry - Model's effort config.
31
+ * @returns {string | null} Level, or null to send none.
32
+ */
33
+ export function defaultOf(entry: EffortConfig): string | null;
34
+ /**
35
+ * Apply an alias's pinned settings to the client body (in the front protocol), before it is converted.
36
+ * @param {any} body - Client body.
37
+ * @param {{effort?: string, thinking?: boolean}} pin - Pinned settings.
38
+ * @param {"anthropic" | "openai"} front - Client protocol.
39
+ * @returns {any} New body.
40
+ */
41
+ export function pinBody(body: any, pin: {
42
+ effort?: string;
43
+ thinking?: boolean;
44
+ }, front: "anthropic" | "openai"): any;
45
+ /**
46
+ * Fit the `reasoning_effort` of an OpenAI Chat Completions body to a model's levels. A level in the list is kept, another one snaps to the
47
+ * nearest (the message is passed to `warn`), a request with no effort gets the model's default, and a request that turns reasoning off is left alone.
48
+ * @param {any} body - Upstream body (mutated).
49
+ * @param {EffortConfig} entry - Model's effort config.
50
+ * @param {string} model - Model name, for the message.
51
+ * @param {(message: string, details: {requested: string, used: string, levels: string[]}) => void} warn - Receives the message and the snapped levels.
52
+ */
53
+ export function fit(body: any, entry: EffortConfig, model: string, warn: (message: string, details: {
54
+ requested: string;
55
+ used: string;
56
+ levels: string[];
57
+ }) => void): void;
58
+ /** The effort levels a model can be configured with, lowest to highest. `none` is not one: it switches reasoning off and is never snapped. */
59
+ export const LEVELS: readonly string[];
60
+ export type EffortConfig = {
61
+ /**
62
+ * - Supported levels, lowest to highest.
63
+ */
64
+ levels: string[];
65
+ /**
66
+ * - Level sent when the request names none; `null` sends nothing (the model has its own default); `undefined` picks the level nearest to `medium`.
67
+ */
68
+ default: string | null | undefined;
69
+ };
@@ -0,0 +1,86 @@
1
+ /**
2
+ * @typedef {object} TranslatorEvent
3
+ * @property {string} type Specific event name (`fallback`, `failover`, `effort`, `dropped`, `detect`, `usage`, `warning`, `error` or `log`).
4
+ * @property {string} [kind] Sub-kind within the type (for example `json_object` for a `fallback`).
5
+ * @property {"debug" | "info" | "warn" | "error"} level Severity.
6
+ * @property {string} message Human readable text.
7
+ * @property {number} time Milliseconds since the epoch.
8
+ */
9
+ /**
10
+ * The state the emitter keeps in `state`: listeners by event name, the log sink, and the server's logger once one is registered.
11
+ * @param {((event: TranslatorEvent) => any) | undefined} [onEvent] - Config callback, registered for every event.
12
+ * @returns {{listeners: Map<string, Set<Function>>, sink: {console: boolean, file: string | null}, logger: any}} Emitter state.
13
+ */
14
+ export function create(onEvent?: ((event: TranslatorEvent) => any) | undefined): {
15
+ listeners: Map<string, Set<Function>>;
16
+ sink: {
17
+ console: boolean;
18
+ file: string | null;
19
+ };
20
+ logger: any;
21
+ };
22
+ /**
23
+ * Configure where logging goes: nothing, the console, or a JSON lines file. A file that cannot be opened is reported once on
24
+ * the console and logging stays off; this never throws.
25
+ * @param {any} emitter - Emitter state from {@link create}.
26
+ * @param {boolean | string} logs - Normalized `logs` option.
27
+ * @returns {void}
28
+ */
29
+ export function open(emitter: any, logs: boolean | string): void;
30
+ /**
31
+ * Subscribe to an event.
32
+ * @param {any} emitter - Emitter state.
33
+ * @param {string} name - Event name, or `"*"` for every event.
34
+ * @param {(event: TranslatorEvent) => any} fn - Listener.
35
+ * @returns {() => void} Unsubscribe function.
36
+ */
37
+ export function on(emitter: any, name: string, fn: (event: TranslatorEvent) => any): () => void;
38
+ /**
39
+ * Remove a listener (a listener that is not subscribed is ignored).
40
+ * @param {any} emitter - Emitter state.
41
+ * @param {string} name - Event name.
42
+ * @param {Function} fn - Listener passed to {@link on}.
43
+ * @returns {void}
44
+ */
45
+ export function off(emitter: any, name: string, fn: Function): void;
46
+ /**
47
+ * Emit one occurrence. The specific event fires first, then the general ones that fit (`warning` for level `warn`, `error` for level
48
+ * `error`, always `log`), then `"*"`; all with the same payload. Listeners only observe. Logging is fed from here too, and does not
49
+ * depend on `events`.
50
+ * @param {{config: any, state: any}} ctx - Translator `{ config, state }`.
51
+ * @param {{type: string, kind?: string, level?: "debug" | "info" | "warn" | "error", message: string, log?: any, [detail: string]: any}} spec - What happened: `type`, optional `kind`, `level` (default `info`), `message`, further details, and `log` (see {@link write}; not part of the payload).
52
+ * @returns {TranslatorEvent} The payload.
53
+ */
54
+ export function emit({ config, state }: {
55
+ config: any;
56
+ state: any;
57
+ }, spec: {
58
+ type: string;
59
+ kind?: string;
60
+ level?: "debug" | "info" | "warn" | "error";
61
+ message: string;
62
+ log?: any;
63
+ [detail: string]: any;
64
+ }): TranslatorEvent;
65
+ export type TranslatorEvent = {
66
+ /**
67
+ * Specific event name (`fallback`, `failover`, `effort`, `dropped`, `detect`, `usage`, `warning`, `error` or `log`).
68
+ */
69
+ type: string;
70
+ /**
71
+ * Sub-kind within the type (for example `json_object` for a `fallback`).
72
+ */
73
+ kind?: string;
74
+ /**
75
+ * Severity.
76
+ */
77
+ level: "debug" | "info" | "warn" | "error";
78
+ /**
79
+ * Human readable text.
80
+ */
81
+ message: string;
82
+ /**
83
+ * Milliseconds since the epoch.
84
+ */
85
+ time: number;
86
+ };
@@ -1,33 +1,36 @@
1
1
  /**
2
- *
3
- * @Project: @cldmv/llm-translator
4
- * @Filename: /src/lib/finish.mjs
5
- * @Date: 2026-10-25T12:28:19-07:00 (1792956499)
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:59:01-07:00 (1792958341)
11
- * -----
12
- * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
- *
14
- */
15
- /**
16
- * @fileoverview Stop-reason and usage mapping between the two protocols.
2
+ * The error a fatal OpenAI `finish_reason` stands for. Servers use `abort`, `aborted` and `error` when generation was cut off and
3
+ * `insufficient_system_resource` when they ran out of resources mid-answer; none of them is a finished answer.
4
+ * @param {string|null|undefined} finishReason - OpenAI `finish_reason`.
5
+ * @returns {{ type: string, status: number, code: string, message: string }|null} Error details, or null when the reason is not fatal.
17
6
  */
7
+ export function finishError(finishReason: string | null | undefined): {
8
+ type: string;
9
+ status: number;
10
+ code: string;
11
+ message: string;
12
+ } | null;
18
13
  /**
19
14
  * Anthropic stop reason (and stop sequence) for an OpenAI choice.
15
+ * `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.
20
16
  * @param {string|null|undefined} finishReason - OpenAI `finish_reason`.
21
- * @param {{ hasToolCalls: boolean, matchedStop?: unknown, argumentsComplete?: boolean }} info - Whether tool calls were produced, vLLM's `stop_reason` (a string for a matched stop string), and whether the last tool call's arguments are complete. A `length` stop with incomplete arguments is `max_tokens`, not `tool_use`.
22
- * @returns {{ stop_reason: string, stop_sequence: string|null }} Anthropic stop fields.
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`.
19
+ * @returns {{ stop_reason: string, stop_sequence: string|null, stop_details?: { type: string, category: string|null, explanation: string|null } }} Anthropic stop fields.
23
20
  */
24
- export function toAnthropicStop(finishReason: string | null | undefined, { hasToolCalls, matchedStop, argumentsComplete }: {
21
+ export function toAnthropicStop(finishReason: string | null | undefined, { hasToolCalls, matchedStop, argumentsComplete, refusal }: {
25
22
  hasToolCalls: boolean;
26
23
  matchedStop?: unknown;
27
24
  argumentsComplete?: boolean;
25
+ refusal?: unknown;
28
26
  }): {
29
27
  stop_reason: string;
30
28
  stop_sequence: string | null;
29
+ stop_details?: {
30
+ type: string;
31
+ category: string | null;
32
+ explanation: string | null;
33
+ };
31
34
  };
32
35
  /**
33
36
  * OpenAI `finish_reason` for an Anthropic stop reason.
@@ -13,7 +13,7 @@ export function serves(endpoint: any, model: string): boolean;
13
13
  * @param {string | undefined} model - Model named by the client.
14
14
  * @param {any} state - Translator state (`cursors`).
15
15
  * @param {boolean} [advance] - Move the round robin cursor (false to only look).
16
- * @returns {{endpoint: any, model: string | undefined, unserved: boolean, pool: any[]}} Chosen endpoint, the model name to send upstream, whether
16
+ * @returns {{endpoint: any, model: string | undefined, unserved: boolean, pool: any[], pin: {effort?: string, thinking?: boolean} | undefined}} Chosen endpoint, the model name to send upstream, whether
17
17
  * no endpoint serves the model and the endpoints to try in order (the chosen one first). When nothing serves it and the fallback endpoint knows
18
18
  * exactly one model, that model is sent instead.
19
19
  */
@@ -22,14 +22,18 @@ export function pick(config: any, endpoints: any[], model: string | undefined, s
22
22
  model: string | undefined;
23
23
  unserved: boolean;
24
24
  pool: any[];
25
+ pin: {
26
+ effort?: string;
27
+ thinking?: boolean;
28
+ } | undefined;
25
29
  };
26
30
  /**
27
31
  * Validate a client request body and resolve where it goes. Shared by `request` and `route` so both always agree.
28
32
  * @param {any} c - Captured translator parts (`capture`).
29
33
  * @param {any} body - Client request body.
30
34
  * @param {{endpoint?: string, advance?: boolean}} [options] - `endpoint` forces one pool member by name (used for failover; it must still serve the model, else `endpoint_not_in_pool`); `advance` moves the round robin cursor (`request` does, `route` does not).
31
- * @returns {{endpoint: any, model: string, requested: string | undefined, unserved: boolean, convert: any, pool: any[]}} The chosen endpoint, the model name sent
32
- * upstream, the model the client asked for, whether no endpoint serves it, the converters (null for a native pass-through) and the pool in try order.
35
+ * @returns {{endpoint: any, model: string, requested: string | undefined, unserved: boolean, pin: {effort?: string, thinking?: boolean} | undefined, convert: any, pool: any[]}} The chosen endpoint, the model name sent
36
+ * upstream, the model the client asked for, whether no endpoint serves it, the settings its alias pins, the converters (null for a native pass-through) and the pool in try order.
33
37
  * @throws {TranslatorError} `invalid_body` when the body is not a JSON object, `endpoint_not_in_pool` when a forced endpoint no longer serves the model.
34
38
  */
35
39
  export function resolve(c: any, body: any, options?: {
@@ -40,6 +44,10 @@ export function resolve(c: any, body: any, options?: {
40
44
  model: string;
41
45
  requested: string | undefined;
42
46
  unserved: boolean;
47
+ pin: {
48
+ effort?: string;
49
+ thinking?: boolean;
50
+ } | undefined;
43
51
  convert: any;
44
52
  pool: any[];
45
53
  };
@@ -11,7 +11,7 @@ export function late(sending: Promise<any>, api: any, model?: string): AsyncGene
11
11
  * Register the routes for one front on a Fastify instance.
12
12
  * @param {any} fastify - Fastify instance (usually an encapsulated plugin scope).
13
13
  * @param {any} options - `{ apiKey, bodyLimit, authorize, onUsage }`.
14
- * @param {any} captured - Captured translator parts `{ front, interval, filter, send, route, count, list, get, build, encode, write }`; `front` applies to this instance only.
14
+ * @param {any} captured - Captured translator parts `{ front, interval(body), filter, send, route, count, list, get, build, encode, write }`; `front` applies to this instance only.
15
15
  * @returns {void}
16
16
  */
17
17
  export function register(fastify: any, options: any, captured: any): void;
@@ -26,7 +26,11 @@ export function resolve(...layers: (Partial<Settings> | null | undefined)[]): Se
26
26
  * @property {"pass"|"sanitize"} toolSchema - Sanitize tool JSON Schemas (drop `$schema`, remove unbounded integer limits).
27
27
  * @property {"forward"|"drop"} deferredTools - Offer `defer_loading` tools as `toolSearch` says, or drop them (and the search tool).
28
28
  * @property {"expand"|"inline"|"native"} toolSearch - Claude Code's tool search. `expand` offers a deferred tool once a `tool_reference` (or a `ToolSearch` result) loads it; `inline` offers every deferred tool up front and hides `ToolSearch` (it stays when `DeferredToolPlaceholder` shows Claude Code holds tools whose definitions it did not send; `expand` does the same when the request has no `ToolSearch` at all); `native` passes `defer_loading` and `tool_reference` through to a server that understands them.
29
- * @property {boolean} toolErrors - Prefix `is_error` tool results with `[error] `.
29
+ * @property {"schema"|"instruction"|"drop"} jsonObject - OpenAI to Anthropic: how `response_format: json_object` is sent. `schema` sends `output_config.format` with the permissive schema `{ "type": "object" }`, `instruction` appends a short system instruction asking for one JSON object, `drop` omits it and records the drop.
30
+ * @property {boolean} jsonObjectFallback - Retry a request once with `jsonObject: "instruction"` when an Anthropic-protocol upstream answers 400 or 422 to the schema `jsonObject: "schema"` sent. The endpoint and model are remembered until the process ends only when the retry succeeds.
31
+ * @property {boolean} toolErrors - Prefix `is_error` tool results with `toolErrorMarker`. Off by default for every driver.
32
+ * @property {string} toolErrorMarker - Text that marks a failed tool result when `toolErrors` is on, and the text `toolErrorInference` looks for. Must contain a non-whitespace character.
33
+ * @property {boolean} toolErrorInference - OpenAI to Anthropic: a `tool` message whose text starts with `toolErrorMarker` becomes a `tool_result` with `is_error: true`, with the marker removed from the text.
30
34
  * @property {"max_completion_tokens"|"max_tokens"} maxTokensField - Field used for the output cap.
31
35
  * @property {boolean} streamUsage - Request `stream_options.include_usage` on streamed requests.
32
36
  * @property {boolean} continuousUsage - Also request `stream_options.continuous_usage_stats` (vLLM extension; real OpenAI may reject it).
@@ -127,9 +131,25 @@ export type Settings = {
127
131
  */
128
132
  toolSearch: "expand" | "inline" | "native";
129
133
  /**
130
- * - Prefix `is_error` tool results with `[error] `.
134
+ * - OpenAI to Anthropic: how `response_format: json_object` is sent. `schema` sends `output_config.format` with the permissive schema `{ "type": "object" }`, `instruction` appends a short system instruction asking for one JSON object, `drop` omits it and records the drop.
135
+ */
136
+ jsonObject: "schema" | "instruction" | "drop";
137
+ /**
138
+ * - Retry a request once with `jsonObject: "instruction"` when an Anthropic-protocol upstream answers 400 or 422 to the schema `jsonObject: "schema"` sent. The endpoint and model are remembered until the process ends only when the retry succeeds.
139
+ */
140
+ jsonObjectFallback: boolean;
141
+ /**
142
+ * - Prefix `is_error` tool results with `toolErrorMarker`. Off by default for every driver.
131
143
  */
132
144
  toolErrors: boolean;
145
+ /**
146
+ * - Text that marks a failed tool result when `toolErrors` is on, and the text `toolErrorInference` looks for. Must contain a non-whitespace character.
147
+ */
148
+ toolErrorMarker: string;
149
+ /**
150
+ * - OpenAI to Anthropic: a `tool` message whose text starts with `toolErrorMarker` becomes a `tool_result` with `is_error: true`, with the marker removed from the text.
151
+ */
152
+ toolErrorInference: boolean;
133
153
  /**
134
154
  * - Field used for the output cap.
135
155
  */