@cldmv/llm-translator 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +7 -7
  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/compatible/info.mjs +1 -1
  12. package/dist/api/driver/deepseek/info.mjs +1 -1
  13. package/dist/api/driver/llamacpp/info.mjs +1 -1
  14. package/dist/api/driver/ollama/info.mjs +1 -1
  15. package/dist/api/driver/openai/info.mjs +1 -1
  16. package/dist/api/driver/sglang/info.mjs +1 -1
  17. package/dist/api/driver/vllm/info.mjs +1 -1
  18. package/dist/api/embeddings/send.mjs +2 -0
  19. package/dist/api/request.mjs +2 -2
  20. package/dist/api/send.mjs +1 -1
  21. package/dist/api/server/plugin.mjs +1 -1
  22. package/dist/lib/config.mjs +1 -1
  23. package/dist/lib/http.mjs +1 -1
  24. package/dist/lib/ids.mjs +1 -1
  25. package/dist/lib/record.mjs +3 -0
  26. package/dist/lib/routes.mjs +1 -1
  27. package/dist/lib/sampling.mjs +2 -0
  28. package/dist/lib/settings.mjs +1 -1
  29. package/dist/lib/strict.mjs +2 -0
  30. package/package.json +4 -3
  31. package/types/src/api/convert/anthropic/messages/to/openai/chat/request.d.mts +6 -1
  32. package/types/src/api/convert/openai/chat/to/anthropic/messages/request.d.mts +8 -1
  33. package/types/src/api/driver/anthropic/detect.d.mts +10 -23
  34. package/types/src/api/driver/anthropic/info.d.mts +4 -1
  35. package/types/src/api/driver/compatible/info.d.mts +2 -1
  36. package/types/src/api/driver/deepseek/info.d.mts +2 -1
  37. package/types/src/api/driver/llamacpp/info.d.mts +2 -1
  38. package/types/src/api/driver/ollama/info.d.mts +2 -1
  39. package/types/src/api/driver/openai/info.d.mts +2 -1
  40. package/types/src/api/driver/sglang/info.d.mts +2 -1
  41. package/types/src/api/driver/vllm/info.d.mts +2 -1
  42. package/types/src/api/embeddings/send.d.mts +23 -0
  43. package/types/src/api/server/plugin.d.mts +1 -1
  44. package/types/src/index.d.mts +5 -0
  45. package/types/src/lib/http.d.mts +8 -2
  46. package/types/src/lib/ids.d.mts +40 -2
  47. package/types/src/lib/record.d.mts +41 -0
  48. package/types/src/lib/sampling.d.mts +13 -0
  49. package/types/src/lib/settings.d.mts +5 -0
  50. package/types/src/lib/strict.d.mts +26 -0
@@ -1,2 +1,2 @@
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};
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 a=Object.defineProperty;var u=(e,i)=>a(e,"name",{value:i,configurable:!0});import{TranslatorError as p}from"./errors.mjs";const f=["seed","frequency_penalty","presence_penalty"];function h(e,i,l,c=[],o=[]){const s={...l.sampling,...i!==null&&typeof i=="object"&&!Array.isArray(i)?i:{}};let r=e;for(const[n,t]of Object.entries(s))if(!(t==null||Object.hasOwn(e,n))){if(!f.includes(n)&&!c.includes(n)){if(l.unknownFields==="error")throw new p(`sampling.${n}: is not accepted by this upstream`,{code:"unsupported_field",param:`sampling.${n}`});o.includes(n)||o.push(n);continue}r===e&&(r={...e}),r[n]=t}return r}u(h,"addSampling");export{h as addSampling};
2
+ //# sourceMappingURL=sampling.mjs.map
@@ -1,2 +1,2 @@
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};
1
+ var l=Object.defineProperty;var i=(a,e)=>l(a,"name",{value:e,configurable:!0});import{TranslatorError as r}from"./errors.mjs";const s=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",sampling:Object.freeze({}),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 p(...a){const e={...s,pdf:{...s.pdf},sampling:{...s.sampling}};for(const t of a)if(t)for(const[n,o]of Object.entries(t))o!==void 0&&(n==="pdf"?Object.assign(e.pdf,o):n==="sampling"&&o!==null&&typeof o=="object"&&!Array.isArray(o)?Object.assign(e.sampling,o):e[n]=o);for(const[t,n]of Object.entries(f))if(!n.includes(e[t]))throw new r(`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 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(e.sampling===null||typeof e.sampling!="object"||Array.isArray(e.sampling))throw new r(`Invalid setting sampling: ${JSON.stringify(e.sampling)} (expected an object of field names and values)`,{code:"invalid_setting",status:500,details:{key:"sampling"}});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}i(p,"resolve");export{s as DEFAULTS,p as resolve};
2
2
  //# sourceMappingURL=settings.mjs.map
@@ -0,0 +1,2 @@
1
+ var l=Object.defineProperty;var p=(r,n)=>l(r,"name",{value:n,configurable:!0});const a=["allOf","not","if","then","else","dependentRequired","dependentSchemas","patternProperties","propertyNames","unevaluatedProperties","contains","minProperties","maxProperties","uniqueItems"],y=["items","additionalProperties"],d=["anyOf","oneOf","prefixItems"],$=["properties","$defs","definitions"];function f(r,n="$",i=[]){if(r===null||typeof r!="object"||Array.isArray(r))return i;const t=r.properties!==null&&typeof r.properties=="object"?Object.keys(r.properties):[];if(r.type==="object"||t.length>0){r.additionalProperties!==!1&&i.push(`${n}: object without additionalProperties: false`);const e=Array.isArray(r.required)?r.required:[],o=t.filter(s=>!e.includes(s));o.length>0&&i.push(`${n}: properties not in required: ${o.join(", ")}`)}for(const e of a)e in r&&i.push(`${n}: unsupported keyword ${e}`);for(const e of y)f(r[e],`${n}.${e}`,i);for(const e of d)Array.isArray(r[e])&&r[e].forEach((o,s)=>f(o,`${n}.${e}[${s}]`,i));for(const e of $){const o=r[e];if(!(o===null||typeof o!="object"))for(const[s,u]of Object.entries(o))f(u,`${n}.${e}.${s}`,i)}return i}p(f,"problems");function P(r){const n=[];for(const i of Array.isArray(r?.tools)?r.tools:[]){const t=i?.function??i;t?.strict===!0&&n.push({name:String(t.name),problems:f(t.parameters??t.input_schema)})}return n}p(P,"strictTools");function j(r){if(!Array.isArray(r?.tools))return r;let n=!1;const i=r.tools.map(t=>{const e=t?.function??t;if(e?.strict!==!0||f(e.parameters??e.input_schema).length===0)return t;if(n=!0,t.function){const{strict:u,...c}=t.function;return{...t,function:c}}const{strict:o,...s}=t;return s});return n?{...r,tools:i}:r}p(j,"relax");export{f as problems,j as relax,P as strictTools};
2
+ //# sourceMappingURL=strict.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cldmv/llm-translator",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "Bidirectional Anthropic Messages ⇄ OpenAI Chat Completions translator with backend auto-detection (vLLM and other OpenAI-compatible servers), built on slothlet, with an optional standalone server for running Claude Code against local models.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -83,8 +83,9 @@
83
83
  "build:ci": "npm run lint && npm run format:check && npm run types:build && npm run build",
84
84
  "prepack": "npm run build",
85
85
  "prepare": "node -e \"import('./.githooks/install.mjs').catch(()=>{})\"",
86
- "test": "node tests/run-vitest.mjs && npm run test: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
12
  * @throws {TranslatorError} For malformed requests, `n` above 1, unknown roles, or unknown fields when configured to error.
12
13
  */
13
- export function request(chatBody: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): 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,13 +18,16 @@
18
18
  */
19
19
  /**
20
20
  * Describe the Anthropic driver (native Messages API upstream).
21
- * @returns {{name: string, protocol: string, order: number, auth: string, 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;
25
25
  protocol: string;
26
26
  order: number;
27
27
  auth: string;
28
+ strict: boolean;
29
+ hosts: string[];
30
+ probes: string[];
28
31
  headers: Record<string, string>;
29
32
  paths: Record<string, string>;
30
33
  };
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * Describe the generic OpenAI-compatible driver (matches last, after every specific driver).
3
- * @returns {{name: string, protocol: string, order: number, auth: string, fields: string[], paths: Record<string,string>}} Driver facts.
3
+ * @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, fields: string[], paths: Record<string,string>}} Driver facts.
4
4
  */
5
5
  export function info(): {
6
6
  name: string;
7
7
  protocol: string;
8
8
  order: number;
9
9
  auth: string;
10
+ strict: boolean;
10
11
  fields: string[];
11
12
  paths: Record<string, string>;
12
13
  };
@@ -18,12 +18,13 @@
18
18
  */
19
19
  /**
20
20
  * Describe the DeepSeek driver.
21
- * @returns {{name: string, protocol: string, order: number, auth: string, paths: Record<string,string>}} Driver facts.
21
+ * @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, paths: Record<string,string>}} Driver facts.
22
22
  */
23
23
  export function info(): {
24
24
  name: string;
25
25
  protocol: string;
26
26
  order: number;
27
27
  auth: string;
28
+ strict: boolean;
28
29
  paths: Record<string, string>;
29
30
  };
@@ -18,13 +18,14 @@
18
18
  */
19
19
  /**
20
20
  * Describe the llama.cpp driver.
21
- * @returns {{name: string, protocol: string, order: number, auth: string, probes: string[], fields: string[], paths: Record<string,string>}} Driver facts.
21
+ * @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, probes: string[], fields: string[], paths: Record<string,string>}} Driver facts.
22
22
  */
23
23
  export function info(): {
24
24
  name: string;
25
25
  protocol: string;
26
26
  order: number;
27
27
  auth: string;
28
+ strict: boolean;
28
29
  probes: string[];
29
30
  fields: string[];
30
31
  paths: Record<string, string>;
@@ -18,13 +18,14 @@
18
18
  */
19
19
  /**
20
20
  * Describe the Ollama driver.
21
- * @returns {{name: string, protocol: string, order: number, auth: string, probes: string[], paths: Record<string,string>}} Driver facts.
21
+ * @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, probes: string[], paths: Record<string,string>}} Driver facts.
22
22
  */
23
23
  export function info(): {
24
24
  name: string;
25
25
  protocol: string;
26
26
  order: number;
27
27
  auth: string;
28
+ strict: boolean;
28
29
  probes: string[];
29
30
  paths: Record<string, string>;
30
31
  };
@@ -18,12 +18,13 @@
18
18
  */
19
19
  /**
20
20
  * Describe the OpenAI driver.
21
- * @returns {{name: string, protocol: string, order: number, auth: string, paths: Record<string,string>}} Driver facts.
21
+ * @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, paths: Record<string,string>}} Driver facts.
22
22
  */
23
23
  export function info(): {
24
24
  name: string;
25
25
  protocol: string;
26
26
  order: number;
27
27
  auth: string;
28
+ strict: boolean;
28
29
  paths: Record<string, string>;
29
30
  };
@@ -18,13 +18,14 @@
18
18
  */
19
19
  /**
20
20
  * Describe the SGLang driver.
21
- * @returns {{name: string, protocol: string, order: number, auth: string, probes: string[], fields: string[], paths: Record<string,string>}} Driver facts.
21
+ * @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, probes: string[], fields: string[], paths: Record<string,string>}} Driver facts.
22
22
  */
23
23
  export function info(): {
24
24
  name: string;
25
25
  protocol: string;
26
26
  order: number;
27
27
  auth: string;
28
+ strict: boolean;
28
29
  probes: string[];
29
30
  fields: string[];
30
31
  paths: Record<string, string>;
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * Describe the vLLM driver.
3
- * @returns {{name: string, protocol: string, order: number, auth: string, fields: string[], paths: Record<string,string>}} Driver facts.
3
+ * @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, fields: string[], paths: Record<string,string>}} Driver facts.
4
4
  */
5
5
  export function info(): {
6
6
  name: string;
7
7
  protocol: string;
8
8
  order: number;
9
9
  auth: string;
10
+ strict: boolean;
10
11
  fields: string[];
11
12
  paths: Record<string, string>;
12
13
  };
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Forward `POST /v1/embeddings` to an OpenAI-protocol upstream that lists the model. The body and the reply pass through unchanged
3
+ * apart from `model`, which is the upstream's name for it (after `aliases`). The model is routed like a chat model: aliases, then the
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
+ * 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
+ * (`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.
14
+ * @param {any} body - OpenAI embeddings request body (`{ model, input, ... }`).
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.
17
+ */
18
+ export function send(body: any, options?: any): Promise<{
19
+ status: number;
20
+ headers: Record<string, string>;
21
+ body: any;
22
+ usage: Promise<any>;
23
+ }>;
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Fastify plugin. Anthropic front: `POST /v1/messages`, `POST /v1/messages/count_tokens`, `GET /v1/models`, `GET /v1/models/{id}`,
3
- * `GET|HEAD /` and `/api/hello`. OpenAI front: `POST /v1/chat/completions`, `GET /v1/models`, `GET /v1/models/{id}`, `GET|HEAD /`. Responses stream as
3
+ * `GET|HEAD /` and `/api/hello`. OpenAI front: `POST /v1/chat/completions`, `POST /v1/embeddings`, `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
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.
@@ -10,6 +10,7 @@
10
10
  * @property {number} [detectTimeout] Probe timeout in ms (default 5000).
11
11
  * @property {(url: string, init?: any) => Promise<Response>} [transport] fetch-compatible function used for all upstream calls.
12
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 {false | string | ((record: object) => any)} [capture] Request and response capture for debugging (default `false`: off). A directory path writes one JSON file per exchange (`<ms>-<id>.json`, the directory is created); a function is called with each record. A record is `{ time, id, front, requested, model, endpoint, stream, client: { body }, upstream: { url, headers, body }, response }`; credential headers (names containing key, token, secret, auth, cookie or password) are replaced by `[redacted]`. A failing sink is reported once as a warning and never affects the reply (see docs/events.md).
13
14
  * @property {boolean} [events] Emit events (default `true`); `false` turns emitting off. Logging does not depend on it.
14
15
  * @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
16
  * @property {boolean} [silent] Deprecated alias for `logs: false` (the default); `logs` wins when both are given.
@@ -72,6 +73,10 @@ export type TranslatorConfig = {
72
73
  * 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
  */
74
75
  logs?: boolean | string;
76
+ /**
77
+ * Request and response capture for debugging (default `false`: off). A directory path writes one JSON file per exchange (`<ms>-<id>.json`, the directory is created); a function is called with each record. A record is `{ time, id, front, requested, model, endpoint, stream, client: { body }, upstream: { url, headers, body }, response }`; credential headers (names containing key, token, secret, auth, cookie or password) are replaced by `[redacted]`. A failing sink is reported once as a warning and never affects the reply (see docs/events.md).
78
+ */
79
+ capture?: false | string | ((record: object) => any);
75
80
  /**
76
81
  * Emit events (default `true`); `false` turns emitting off. Logging does not depend on it.
77
82
  */
@@ -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
  }>;
@@ -19,13 +19,51 @@ export function completionId(): string;
19
19
  */
20
20
  export function callId(): string;
21
21
  /**
22
- * Make a tool-call id valid for Anthropic (`[a-zA-Z0-9_-]+`). Valid ids pass through unchanged, invalid characters
22
+ * Make a tool-call id valid for Anthropic (`[a-zA-Z0-9_-]+`). An id found in `map` (`fitted -> original`, from a driver's `toolCallIds`
23
+ * policy) is the client's own and is restored first. Otherwise valid ids pass through unchanged, invalid characters
23
24
  * (for example kimi's `functions.name:0`) become `_`, and a missing or empty id is replaced by a fresh one. The
24
25
  * mapping is deterministic so a tool call and its later tool result agree.
25
26
  * @param {unknown} id - Upstream id.
27
+ * @param {Record<string, string>} [map] - `fitted -> original` ids from the conversion state.
26
28
  * @returns {string} Anthropic-valid id.
27
29
  */
28
- export function anthropicToolId(id: unknown): string;
30
+ export function anthropicToolId(id: unknown, map?: Record<string, string>): string;
31
+ /**
32
+ * Whether a driver's `info().toolCallIds` is usable: `{ alphabet, min?, max? }` where `alphabet` is the body of a regular
33
+ * expression character class that allows at least one printable ASCII character (`"a-zA-Z0-9"`), and `min` and `max` are
34
+ * positive integers (`min <= max`) bounding the id length. Mistral's ids are `{ alphabet: "a-zA-Z0-9", min: 9, max: 9 }`.
35
+ * @param {unknown} policy - Value to check.
36
+ * @returns {boolean} True when the policy can be applied.
37
+ */
38
+ export function validToolIdPolicy(policy: unknown): boolean;
39
+ /**
40
+ * Create a tool-call id fitter for an upstream with a strict id format (`info().toolCallIds`, see `validToolIdPolicy`). The translator
41
+ * keeps no state between requests, so the mapping is a pure function of the request: an id that already satisfies the policy passes
42
+ * through unchanged (so history replayed from the upstream's own ids survives), and any other id becomes a hash of itself in the
43
+ * allowed alphabet, the same text on every request. A tool call and its tool result carry the same original and so get the same
44
+ * value. `ids` lists every original id of the request: the conforming ones are reserved first, then each other one takes its hash,
45
+ * and when that text is already taken by a different original the hash is taken again with a counter until it is free. Every
46
+ * rewritten id is recorded in `map` (`fitted -> original`) so the original can be restored when the upstream echoes it back.
47
+ * @param {{ policy: { alphabet: string, min?: number, max?: number }, ids: unknown[], map?: Record<string, string> }} options - Policy, the request's ids and the (JSON-serializable) map to record into.
48
+ * @returns {(id: unknown) => string} Fitter function.
49
+ */
50
+ export function createIdFitter({ policy, ids, map }: {
51
+ policy: {
52
+ alphabet: string;
53
+ min?: number;
54
+ max?: number;
55
+ };
56
+ ids: unknown[];
57
+ map?: Record<string, string>;
58
+ }): (id: unknown) => string;
59
+ /**
60
+ * Original tool-call id for one rewritten by `createIdFitter` (ids that were never rewritten, including ones the upstream made itself,
61
+ * map to themselves).
62
+ * @param {Record<string, string>} map - `fitted -> original` map from the conversion state.
63
+ * @param {string} id - Id seen on the wire.
64
+ * @returns {string} Original id.
65
+ */
66
+ export function restoreId(map: Record<string, string>, id: string): string;
29
67
  /**
30
68
  * Create a tool-name fitter. Names that already satisfy `pattern` and `max` pass through; others are sanitized and,
31
69
  * when too long, shortened with a hash suffix. Every changed name is recorded in `map` (`fitted -> original`) so the
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Copy a header map with the value of every credential header replaced. A header is a credential when its name contains `key`,
3
+ * `token`, `secret`, `auth`, `cookie` or `password` (this covers `authorization`, `proxy-authorization`, `x-api-key`, `api-key`).
4
+ * @param {Record<string, any> | undefined} headers - Header map.
5
+ * @returns {Record<string, any>} Copy with lowercase names.
6
+ */
7
+ export function redact(headers: Record<string, any> | undefined): Record<string, any>;
8
+ /**
9
+ * Validate the `capture` option.
10
+ * @param {any} value - Raw option.
11
+ * @returns {boolean} True when the value is `false`/`undefined`, a non-empty directory path or a function.
12
+ */
13
+ export function valid(value: any): boolean;
14
+ /**
15
+ * Begin the record of one exchange. With `capture` unset this returns a stand-in that does nothing.
16
+ * @param {{config: any, state: any}} ctx - Translator `{ config, state }`.
17
+ * @param {{body: any, sent: any, front: string, requested: string | null, headers?: Record<string, string>, log?: any, exchange?: string | null, attempt: number}} exchange - Client body, the upstream request (result of `request`), front, the logger of the request, and where this attempt sits: `exchange` is the id of the first attempt of the request (omit it for the first attempt) and `attempt` the 1-based number.
18
+ * @returns {{on: boolean, id: string, raw: (source: any) => any, events: (source: any) => any, reply: (status: number, headers: Record<string, string>, body: any) => void, attempt: (response: any, failover?: string) => void}} Recorder: `id` is the record id (pass it as `exchange` for the later attempts), `raw` taps the upstream stream bytes, `events` taps the client events and writes the record when the stream ends, `reply` writes the record of a non-stream reply, `attempt` writes the record of an attempt that did not produce the reply (`failover` is the reason the request moved on to the next endpoint, absent for the final failure).
19
+ */
20
+ export function start({ config, state }: {
21
+ config: any;
22
+ state: any;
23
+ }, exchange: {
24
+ body: any;
25
+ sent: any;
26
+ front: string;
27
+ requested: string | null;
28
+ headers?: Record<string, string>;
29
+ log?: any;
30
+ exchange?: string | null;
31
+ attempt: number;
32
+ }): {
33
+ on: boolean;
34
+ id: string;
35
+ raw: (source: any) => any;
36
+ events: (source: any) => any;
37
+ reply: (status: number, headers: Record<string, string>, body: any) => void;
38
+ attempt: (response: any, failover?: string) => void;
39
+ };
40
+ /** Most characters of a stream kept per exchange, for the raw upstream text and for the client events each. */
41
+ export const STREAM_LIMIT: 1000000;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Sampling fields to add to an OpenAI-protocol request: the `sampling` setting for the upstream, overridden by `metadata.sampling` of an
3
+ * Anthropic request. Fields the body already carries are kept. A field the upstream's driver does not accept follows `unknownFields`:
4
+ * dropped (listed in `dropped`) or rejected. Names are the canonical vLLM ones; a driver such as llama.cpp renames them in its `request`.
5
+ * @param {any} body - Converted or forwarded Chat Completions body.
6
+ * @param {any} extras - `metadata.sampling` of the client request (ignored unless a plain object).
7
+ * @param {any} settings - Resolved settings (`sampling`, `unknownFields`).
8
+ * @param {string[]} [fields] - Extension fields the upstream's driver accepts.
9
+ * @param {string[]} [dropped] - Receives the names that were left out.
10
+ * @returns {any} The body, with the fields added (a copy when anything was added).
11
+ * @throws {TranslatorError} When a field is not accepted and `unknownFields` is `"error"`.
12
+ */
13
+ export function addSampling(body: any, extras: any, settings: any, fields?: string[], dropped?: string[]): any;
@@ -39,6 +39,7 @@ export function resolve(...layers: (Partial<Settings> | null | undefined)[]): Se
39
39
  * @property {boolean} responseFormat - Map `output_config.format` to `response_format`.
40
40
  * @property {boolean} parallelToolCalls - Map `disable_parallel_tool_use` to `parallel_tool_calls: false`.
41
41
  * @property {"drop"|"error"} unknownFields - Unknown request fields are dropped (and listed in `state.dropped`) or rejected.
42
+ * @property {Record<string, any>} sampling - Sampling extension fields (`min_p`, `repetition_penalty`, `seed`, ...) added to requests for an OpenAI-protocol upstream when the request does not carry them, merged key by key. A request may set them itself (OpenAI front, or `metadata.sampling` on the Anthropic front). A field the upstream's driver does not accept follows `unknownFields`.
42
43
  * @property {number} pingInterval - Milliseconds of upstream silence before the SSE writer emits a `ping` (0 disables).
43
44
  * @property {"buffer"|"stream"} toolStreaming - OpenAI to Anthropic streams: `buffer` holds each tool call until its arguments are complete and emits one whole `tool_use` block (text and thinking keep streaming and appear before the block); `stream` forwards argument fragments incrementally and holds text that arrives meanwhile until the tool blocks are done.
44
45
  * @property {boolean} effortPassthrough - vLLM driver: send `xhigh` and `max` reasoning effort unchanged instead of clamping them to `high`.
@@ -182,6 +183,10 @@ export type Settings = {
182
183
  * - Unknown request fields are dropped (and listed in `state.dropped`) or rejected.
183
184
  */
184
185
  unknownFields: "drop" | "error";
186
+ /**
187
+ * - Sampling extension fields (`min_p`, `repetition_penalty`, `seed`, ...) added to requests for an OpenAI-protocol upstream when the request does not carry them, merged key by key. A request may set them itself (OpenAI front, or `metadata.sampling` on the Anthropic front). A field the upstream's driver does not accept follows `unknownFields`.
188
+ */
189
+ sampling: Record<string, any>;
185
190
  /**
186
191
  * - Milliseconds of upstream silence before the SSE writer emits a `ping` (0 disables).
187
192
  */
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Walk a schema and list what falls outside the strict subset: an object schema must say `additionalProperties: false` and list every
3
+ * property in `required`, and the keywords in `UNSUPPORTED` are not allowed anywhere.
4
+ * @param {any} schema - A tool's JSON Schema.
5
+ * @param {string} [at] - Path of `schema` within the tool's schema.
6
+ * @param {string[]} [out] - Problems found so far.
7
+ * @returns {string[]} One line per problem, each starting with its path (`$` is the schema's root).
8
+ */
9
+ export function problems(schema: any, at?: string, out?: string[]): string[];
10
+ /**
11
+ * The tools of an upstream body that ask for strict use, with the problems of their schemas. Reads both wire shapes: Chat Completions
12
+ * (`function.strict`, `function.parameters`) and Anthropic Messages (`strict`, `input_schema`).
13
+ * @param {any} body - Upstream request body.
14
+ * @returns {{name: string, problems: string[]}[]} Strict tools in request order.
15
+ */
16
+ export function strictTools(body: any): {
17
+ name: string;
18
+ problems: string[];
19
+ }[];
20
+ /**
21
+ * Send the tools whose schema is outside the strict subset without `strict` (the key is removed, not set to false). Reads both wire shapes
22
+ * like `strictTools`. Nothing in `body` is modified: tools that change are rebuilt, and the body is copied only when one did.
23
+ * @param {any} body - Upstream request body.
24
+ * @returns {any} `body` itself when every strict tool conforms, otherwise a copy with the rebuilt `tools`.
25
+ */
26
+ export function relax(body: any): any;