@cldmv/llm-translator 0.0.0-stage → 0.2.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 (153) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +185 -2
  3. package/dist/api/convert/anthropic/messages/to/openai/chat/request.mjs +17 -0
  4. package/dist/api/convert/anthropic/messages/to/openai/chat/response.mjs +2 -0
  5. package/dist/api/convert/anthropic/messages/to/openai/chat/stream.mjs +2 -0
  6. package/dist/api/convert/openai/chat/to/anthropic/messages/request.mjs +6 -0
  7. package/dist/api/convert/openai/chat/to/anthropic/messages/response.mjs +2 -0
  8. package/dist/api/convert/openai/chat/to/anthropic/messages/stream.mjs +2 -0
  9. package/dist/api/detect.mjs +2 -0
  10. package/dist/api/driver/anthropic/detect.mjs +2 -0
  11. package/dist/api/driver/anthropic/info.mjs +2 -0
  12. package/dist/api/driver/anthropic/model/list.mjs +2 -0
  13. package/dist/api/driver/anthropic/settings.mjs +2 -0
  14. package/dist/api/driver/anthropic/token/count.mjs +2 -0
  15. package/dist/api/driver/compatible/detect.mjs +2 -0
  16. package/dist/api/driver/compatible/info.mjs +2 -0
  17. package/dist/api/driver/compatible/model/list.mjs +2 -0
  18. package/dist/api/driver/compatible/request.mjs +2 -0
  19. package/dist/api/driver/compatible/settings.mjs +2 -0
  20. package/dist/api/driver/openai/detect.mjs +2 -0
  21. package/dist/api/driver/openai/info.mjs +2 -0
  22. package/dist/api/driver/openai/model/list.mjs +2 -0
  23. package/dist/api/driver/openai/request.mjs +2 -0
  24. package/dist/api/driver/openai/settings.mjs +2 -0
  25. package/dist/api/driver/vllm/detect.mjs +2 -0
  26. package/dist/api/driver/vllm/error.mjs +2 -0
  27. package/dist/api/driver/vllm/info.mjs +2 -0
  28. package/dist/api/driver/vllm/model/list.mjs +2 -0
  29. package/dist/api/driver/vllm/probe.mjs +2 -0
  30. package/dist/api/driver/vllm/request.mjs +2 -0
  31. package/dist/api/driver/vllm/settings.mjs +2 -0
  32. package/dist/api/driver/vllm/token/count.mjs +2 -0
  33. package/dist/api/error/build.mjs +2 -0
  34. package/dist/api/error/error.mjs +2 -0
  35. package/dist/api/model/list.mjs +2 -0
  36. package/dist/api/request.mjs +3 -0
  37. package/dist/api/response.mjs +2 -0
  38. package/dist/api/send.mjs +2 -0
  39. package/dist/api/server/plugin.mjs +2 -0
  40. package/dist/api/server/start.mjs +2 -0
  41. package/dist/api/server/stop.mjs +2 -0
  42. package/dist/api/sse/encode.mjs +4 -0
  43. package/dist/api/sse/parse.mjs +2 -0
  44. package/dist/api/sse/ping.mjs +2 -0
  45. package/dist/api/sse/write.mjs +2 -0
  46. package/dist/api/status.mjs +2 -0
  47. package/dist/api/stream.mjs +2 -0
  48. package/dist/api/token/count.mjs +2 -0
  49. package/dist/bin/llm-translator.mjs +3 -0
  50. package/dist/index.cjs +44 -0
  51. package/dist/index.mjs +2 -0
  52. package/dist/lib/arguments.mjs +2 -0
  53. package/dist/lib/capture.mjs +2 -0
  54. package/dist/lib/cli.mjs +29 -0
  55. package/dist/lib/config.mjs +2 -0
  56. package/dist/lib/documents.mjs +12 -0
  57. package/dist/lib/errors.mjs +2 -0
  58. package/dist/lib/estimate.mjs +2 -0
  59. package/dist/lib/finish.mjs +2 -0
  60. package/dist/lib/http.mjs +2 -0
  61. package/dist/lib/ids.mjs +2 -0
  62. package/dist/lib/images.mjs +2 -0
  63. package/dist/lib/models.mjs +2 -0
  64. package/dist/lib/normalize.mjs +2 -0
  65. package/dist/lib/passthrough.mjs +2 -0
  66. package/dist/lib/pdf.mjs +2 -0
  67. package/dist/lib/ping.mjs +2 -0
  68. package/dist/lib/route.mjs +2 -0
  69. package/dist/lib/routes.mjs +2 -0
  70. package/dist/lib/runtime.mjs +2 -0
  71. package/dist/lib/schema.mjs +2 -0
  72. package/dist/lib/settings.mjs +2 -0
  73. package/dist/lib/source.mjs +2 -0
  74. package/dist/lib/sse.mjs +10 -0
  75. package/dist/lib/thinking.mjs +2 -0
  76. package/dist/lib/transport.mjs +2 -0
  77. package/dist/lib/upstream-error.mjs +2 -0
  78. package/dist/lib/warn.mjs +2 -0
  79. package/package.json +127 -5
  80. package/types/src/api/convert/anthropic/messages/to/openai/chat/request.d.mts +28 -0
  81. package/types/src/api/convert/anthropic/messages/to/openai/chat/response.d.mts +24 -0
  82. package/types/src/api/convert/anthropic/messages/to/openai/chat/stream.d.mts +29 -0
  83. package/types/src/api/convert/openai/chat/to/anthropic/messages/request.d.mts +30 -0
  84. package/types/src/api/convert/openai/chat/to/anthropic/messages/response.d.mts +28 -0
  85. package/types/src/api/convert/openai/chat/to/anthropic/messages/stream.d.mts +38 -0
  86. package/types/src/api/detect.d.mts +21 -0
  87. package/types/src/api/driver/anthropic/detect.d.mts +26 -0
  88. package/types/src/api/driver/anthropic/info.d.mts +30 -0
  89. package/types/src/api/driver/anthropic/model/list.d.mts +20 -0
  90. package/types/src/api/driver/anthropic/settings.d.mts +23 -0
  91. package/types/src/api/driver/anthropic/token/count.d.mts +20 -0
  92. package/types/src/api/driver/compatible/detect.d.mts +23 -0
  93. package/types/src/api/driver/compatible/info.d.mts +29 -0
  94. package/types/src/api/driver/compatible/model/list.d.mts +20 -0
  95. package/types/src/api/driver/compatible/request.d.mts +20 -0
  96. package/types/src/api/driver/compatible/settings.d.mts +23 -0
  97. package/types/src/api/driver/openai/detect.d.mts +26 -0
  98. package/types/src/api/driver/openai/info.d.mts +29 -0
  99. package/types/src/api/driver/openai/model/list.d.mts +20 -0
  100. package/types/src/api/driver/openai/request.d.mts +20 -0
  101. package/types/src/api/driver/openai/settings.d.mts +23 -0
  102. package/types/src/api/driver/vllm/detect.d.mts +20 -0
  103. package/types/src/api/driver/vllm/error.d.mts +28 -0
  104. package/types/src/api/driver/vllm/info.d.mts +29 -0
  105. package/types/src/api/driver/vllm/model/list.d.mts +20 -0
  106. package/types/src/api/driver/vllm/probe.d.mts +39 -0
  107. package/types/src/api/driver/vllm/request.d.mts +22 -0
  108. package/types/src/api/driver/vllm/settings.d.mts +22 -0
  109. package/types/src/api/driver/vllm/token/count.d.mts +20 -0
  110. package/types/src/api/error/build.d.mts +26 -0
  111. package/types/src/api/error/error.d.mts +29 -0
  112. package/types/src/api/model/list.d.mts +21 -0
  113. package/types/src/api/request.d.mts +35 -0
  114. package/types/src/api/response.d.mts +22 -0
  115. package/types/src/api/send.d.mts +32 -0
  116. package/types/src/api/server/plugin.d.mts +24 -0
  117. package/types/src/api/server/start.d.mts +26 -0
  118. package/types/src/api/server/stop.d.mts +19 -0
  119. package/types/src/api/sse/encode.d.mts +26 -0
  120. package/types/src/api/sse/parse.d.mts +24 -0
  121. package/types/src/api/sse/ping.d.mts +30 -0
  122. package/types/src/api/sse/write.d.mts +26 -0
  123. package/types/src/api/status.d.mts +22 -0
  124. package/types/src/api/stream.d.mts +25 -0
  125. package/types/src/api/token/count.d.mts +24 -0
  126. package/types/src/index.d.mts +76 -0
  127. package/types/src/lib/arguments.d.mts +30 -0
  128. package/types/src/lib/capture.d.mts +28 -0
  129. package/types/src/lib/cli.d.mts +49 -0
  130. package/types/src/lib/config.d.mts +65 -0
  131. package/types/src/lib/documents.d.mts +43 -0
  132. package/types/src/lib/errors.d.mts +144 -0
  133. package/types/src/lib/estimate.d.mts +24 -0
  134. package/types/src/lib/finish.d.mts +73 -0
  135. package/types/src/lib/http.d.mts +58 -0
  136. package/types/src/lib/ids.d.mts +55 -0
  137. package/types/src/lib/images.d.mts +39 -0
  138. package/types/src/lib/models.d.mts +25 -0
  139. package/types/src/lib/normalize.d.mts +37 -0
  140. package/types/src/lib/passthrough.d.mts +22 -0
  141. package/types/src/lib/pdf.d.mts +67 -0
  142. package/types/src/lib/ping.d.mts +24 -0
  143. package/types/src/lib/route.d.mts +38 -0
  144. package/types/src/lib/routes.d.mts +22 -0
  145. package/types/src/lib/runtime.d.mts +24 -0
  146. package/types/src/lib/schema.d.mts +22 -0
  147. package/types/src/lib/settings.d.mts +175 -0
  148. package/types/src/lib/source.d.mts +32 -0
  149. package/types/src/lib/sse.d.mts +72 -0
  150. package/types/src/lib/thinking.d.mts +60 -0
  151. package/types/src/lib/transport.d.mts +31 -0
  152. package/types/src/lib/upstream-error.d.mts +57 -0
  153. package/types/src/lib/warn.d.mts +26 -0
@@ -0,0 +1,2 @@
1
+ var l=Object.defineProperty;var p=(r,o)=>l(r,"name",{value:o,configurable:!0});import{context as y,self as v}from"../../lib/runtime.mjs";import{capture as b}from"../../lib/capture.mjs";import*as u from"../../lib/upstream-error.mjs";const h=["retry-after","x-should-retry"];async function x(r,o,f={},e){const n=b(v,y,e?.front),t=e?n.endpoint(e.upstream):void 0,s=t?n.driver(t):void 0;let d=o;s?.error&&(d=await s.error(r,d,{endpoint:t,state:e}));const c=u.parse(d,r);e?.unserved===!0&&r===404&&(c.message+=` The model "${e.model}" is not served by any configured upstream; map it to a served model with "aliases".`);const a=u.render(n.front,r,c,{native:e?.native===!0}),m={"content-type":"application/json"};for(const i of h)f[i]!==void 0&&(m[i]=f[i]);return{status:a.status,headers:m,body:a.body}}p(x,"error");export{x as default};
2
+ //# sourceMappingURL=error.mjs.map
@@ -0,0 +1,2 @@
1
+ var c=Object.defineProperty;var d=(a,t)=>c(a,"name",{value:t,configurable:!0});import{context as r,self as f}from"../../lib/runtime.mjs";import{capture as _}from"../../lib/capture.mjs";async function w(a={}){const t=_(f,r,a.front),s=new Map;await Promise.all(t.state.endpoints.map(async e=>{const o=t.driver(e),m=(o.list?await o.list({endpoint:e,transport:t.state.transport,signal:a.signal,timeout:t.config.detectTimeout}):null)??(e.models??e.facts?.models??[]).map(l=>({id:l}));for(const l of m)s.set(l.id,{...l,upstream:e.name})}));for(const e of Object.keys(t.config.aliases)){const o=s.get(t.config.aliases[e]);s.set(e,{...o,id:e})}const n=[...s.values()];if(t.front==="openai")return{object:"list",data:n.map(e=>({id:e.id,object:"model",created:e.created??0,owned_by:e.owned_by??e.upstream??"llm-translator",...e.max_model_len===void 0?{}:{max_model_len:e.max_model_len}}))};const i=n.map(e=>({type:"model",id:e.id,display_name:e.id,created_at:new Date((e.created??0)*1e3).toISOString(),...e.max_model_len===void 0?{}:{max_input_tokens:e.max_model_len}}));return{data:i,has_more:!1,first_id:i[0]?.id??null,last_id:i.at(-1)?.id??null}}d(w,"list");export{w as list};
2
+ //# sourceMappingURL=list.mjs.map
@@ -0,0 +1,3 @@
1
+ var j=Object.defineProperty;var h=(t,s)=>j(t,"name",{value:s,configurable:!0});import{context as q,self as x}from"../lib/runtime.mjs";import{capture as y}from"../lib/capture.mjs";import{TranslatorError as k}from"../lib/errors.mjs";import{pick as w}from"../lib/route.mjs";import*as d from"../lib/http.mjs";import{filterChatBody as F}from"../lib/passthrough.mjs";import*as g from"../lib/images.mjs";async function B(t,s={}){const r=y(x,q,s.front);if(!t||typeof t!="object"||Array.isArray(t))throw new k("The request body must be a JSON object",{code:"invalid_body",status:400});const{endpoint:e,model:m,unserved:v}=w(r.config,r.state.endpoints,t.model),o=r.driver(e),l=`${e.name}
2
+ ${m}`,p=s.images===!1||r.state.textOnly.has(l)&&r.settings(e).imageFallback,i=p?{...r.settings(e),images:!1}:r.settings(e),n=r.converters(e),f=t.stream===!0;let a,c;n?{body:a,state:c}=await n.request(t,i):a=r.front==="openai"&&(s.filter??i.passthroughFilter===!0)?F(t):{...t},p&&!n&&r.front==="openai"&&(a=g.strip(a)),a.model=m,o.request&&(a=await o.request(a,{endpoint:e,settings:i,state:c}));const u=e.protocol==="openai.chat"?o.info.paths.chat:o.info.paths.messages;return{upstream:e.name,method:"POST",url:d.join(e.url,u),path:u,headers:{"content-type":"application/json",accept:f?"text/event-stream":"application/json",...o.info.headers,...n===null&&e.protocol==="anthropic.messages"?s.clientHeaders:void 0,...d.headers(e,o.info.auth,s.headers)},body:a,state:{v:1,upstream:e.name,front:r.front,native:n===null,model:t.model,stream:f,unserved:v,convert:c},images:{sent:g.carries(a),retry:i.imageFallback&&e.protocol==="openai.chat",key:l}}}h(B,"request");export{B as request};
3
+ //# sourceMappingURL=request.mjs.map
@@ -0,0 +1,2 @@
1
+ var d=Object.defineProperty;var i=(t,e)=>d(t,"name",{value:e,configurable:!0});import{context as p,self as c}from"../lib/runtime.mjs";import{capture as m}from"../lib/capture.mjs";async function v(t,e){const o=m(c,p,e.front),r=o.endpoint(e.upstream),s=o.driver(r);let n=t;return s.response&&(n=await s.response(n,{endpoint:r,settings:o.settings(r),state:e})),e.native?n.model===void 0?n:{...n,model:e.model??n.model}:o.converters(r).response(n,e.convert,o.settings(r))}i(v,"response");export{v as response};
2
+ //# sourceMappingURL=response.mjs.map
@@ -0,0 +1,2 @@
1
+ var b=Object.defineProperty;var i=(r,e)=>b(r,"name",{value:e,configurable:!0});import{context as x,self as S}from"../lib/runtime.mjs";import{TranslatorError as p}from"../lib/errors.mjs";import*as w from"../lib/http.mjs";import*as E from"../lib/images.mjs";import{parse as k}from"../lib/upstream-error.mjs";const O={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"};async function _(r,e={}){const{transport:n,textOnly:o}=x.state,{request:f,response:c,stream:u,error:s}=S,d=s.build;try{return await H({transport:n,textOnly:o,request:f,response:c,stream:u,error:s},r,e)}catch(t){if(!(t instanceof p))throw t;return d(t.status,t.message,{type:t.type,code:t.code,param:t.param,front:e.front})}}i(_,"send");async function H({transport:r,textOnly:e,request:n,response:o,stream:f,error:c},u,s){const d={front:s.front,headers:s.headers,clientHeaders:s.clientHeaders,filter:s.filter};let t=await n(u,d),a=await g(r,t,s.signal);if(!a.ok&&a.status===400&&t.images.sent&&t.images.retry){const y=await m(a);if(!E.refused(k(y,400).message))return c(400,y,w.plain(a.headers),t.state);e.add(t.images.key),t=await n(u,{...d,images:!1}),a=await g(r,t,s.signal)}const h=w.plain(a.headers);if(!a.ok)return c(a.status,await m(a),h,t.state);if(t.state.stream)return{status:200,headers:O,stream:f(a.body,t.state)};const l=await m(a);return l===null||typeof l!="object"?c(502,"The upstream returned a body that is not JSON",h,t.state):{status:200,headers:{"content-type":"application/json"},body:await o(l,t.state)}}i(H,"round");async function g(r,e,n){try{return await r(e.url,{method:e.method,headers:e.headers,body:JSON.stringify(e.body),signal:n,redirect:"error"})}catch(o){throw n?.aborted?o:new p(`Could not reach the upstream (${e.upstream}): ${o.message}`,{code:"upstream_unreachable",status:502,details:{cause:o}})}}i(g,"call");async function m(r){const e=await r.text();try{return JSON.parse(e)}catch{return e===""?null:e}}i(m,"parse");export{_ as send};
2
+ //# sourceMappingURL=send.mjs.map
@@ -0,0 +1,2 @@
1
+ var c=Object.defineProperty;var o=(t,r)=>c(t,"name",{value:r,configurable:!0});import{context as a,self as d}from"../../lib/runtime.mjs";import{TranslatorError as g}from"../../lib/errors.mjs";import{register as m}from"../../lib/routes.mjs";import{resolve as u}from"../../lib/settings.mjs";async function x(t,r={}){const{config:n}=a;if(r.front!==void 0&&r.front!=="anthropic"&&r.front!=="openai")throw new g(`front must be "anthropic" or "openai" (got ${JSON.stringify(r.front)})`,{code:"invalid_config",status:500});const{send:i,token:f,model:s,error:l,sse:e}=d;m(t,r,{front:r.front??n.front,filter:n.settings.passthroughFilter!==!1,interval:u(n.settings).pingInterval,send:i,count:f.count,list:s.list,build:l.build,encode:e.encode,write:e.write})}o(x,"plugin");export{x as plugin};
2
+ //# sourceMappingURL=plugin.mjs.map
@@ -0,0 +1,2 @@
1
+ var m=Object.defineProperty;var s=(t,r)=>m(t,"name",{value:r,configurable:!0});import{context as g,self as d}from"../../lib/runtime.mjs";import{TranslatorError as c}from"../../lib/errors.mjs";import{isLoopback as b}from"../../lib/http.mjs";async function C(t={}){const{state:r}=g,{plugin:u}=d.server,{build:f}=d.error;let i;try{({default:i}=await import("fastify"))}catch(o){throw new c('server.start needs the optional peer dependency "fastify" (npm install fastify)',{code:"fastify_missing",status:500,details:{cause:o}})}const e=t.host??"127.0.0.1";if(!b(e)&&[t.apiKey??[]].flat().filter(Boolean).length===0&&t.allowUnauthenticated!==!0)throw new c(`Refusing to listen on ${e} without an apiKey: anyone who can reach the port could use the upstream. Set \`apiKey\` (CLI: --api-key), bind to 127.0.0.1, or opt in with \`allowUnauthenticated: true\` (CLI: --allow-unauthenticated).`,{code:"unauthenticated_bind",status:500});const a=i({logger:t.logger??!1,forceCloseConnections:!0});a.setNotFoundHandler((o,h)=>{const{status:p,headers:y,body:w}=f(404,`Not found: ${o.method} ${o.url}`);return h.code(p).headers(y).send(w)}),await a.register(u,{front:t.front,apiKey:t.apiKey,bodyLimit:t.bodyLimit}),await a.listen({host:e,port:t.port??8787});const l=a.server.address().port,n={url:`http://${e.includes(":")?`[${e}]`:e}:${l}`,host:e,port:l,app:a,close:s(async()=>{r.servers.delete(n),await a.close()},"close")};return r.servers.add(n),n}s(C,"start");export{C as start};
2
+ //# sourceMappingURL=start.mjs.map
@@ -0,0 +1,2 @@
1
+ var s=Object.defineProperty;var o=(t,e)=>s(t,"name",{value:e,configurable:!0});import{context as a}from"../../lib/runtime.mjs";async function i(){await Promise.all([...a.state.servers].map(t=>t.close()))}o(i,"stop");export{i as stop};
2
+ //# sourceMappingURL=stop.mjs.map
@@ -0,0 +1,4 @@
1
+ var t=Object.defineProperty;var o=(n,e)=>t(n,"name",{value:e,configurable:!0});import*as c from"../../lib/sse.mjs";function d(n){return n.comment===void 0?c.encode(n):`: ${n.comment}
2
+
3
+ `}o(d,"encode");export{d as encode};
4
+ //# sourceMappingURL=encode.mjs.map
@@ -0,0 +1,2 @@
1
+ var s=Object.defineProperty;var e=(r,o)=>s(r,"name",{value:o,configurable:!0});import{messages as t}from"../../lib/sse.mjs";function p(r){return t(r)}e(p,"parse");export{p as parse};
2
+ //# sourceMappingURL=parse.mjs.map
@@ -0,0 +1,2 @@
1
+ var s=Object.defineProperty;var i=(n,t)=>s(n,"name",{value:t,configurable:!0});import{context as r}from"../../lib/runtime.mjs";import{withPings as l}from"../../lib/ping.mjs";import{resolve as d}from"../../lib/settings.mjs";function m(n,t={}){const o=t.front??r.config.front,c=t.interval??d(r.config.settings).pingInterval;if(o!=="anthropic")return l(n,c,{comment:"ping"});const a=new Map;async function*p(){for await(const e of n)e.event==="content_block_start"?a.set(e.data.index,e.data.content_block?.type):e.event==="content_block_stop"&&a.delete(e.data.index),yield e}return i(p,"watched"),l(p(),c,()=>f(a))}i(m,"ping");function f(n){const t=[...n.entries()].at(-1),o=t&&_[t[1]];return o?{event:"content_block_delta",data:{type:"content_block_delta",index:t[0],delta:o}}:{event:"ping",data:{type:"ping"}}}i(f,"keepAlive");const _={text:{type:"text_delta",text:""},thinking:{type:"thinking_delta",thinking:""},tool_use:{type:"input_json_delta",partial_json:""}};export{m as ping};
2
+ //# sourceMappingURL=ping.mjs.map
@@ -0,0 +1,2 @@
1
+ var r=Object.defineProperty;var i=(n,o)=>r(n,"name",{value:o,configurable:!0});import{self as c}from"../../lib/runtime.mjs";function w(n){return new Promise(o=>{const e=i(()=>{n.off("drain",e),n.off("close",e),o()},"done");n.once("drain",e),n.once("close",e)})}i(w,"drained");async function a(n,o,e={}){const{encode:s,ping:d}=c.sse;for await(const f of d(o,e)){if(n.destroyed||n.writableEnded)return!1;n.write(s(f))||await w(n)}return!(n.destroyed||n.writableEnded)}i(a,"write");export{a as write};
2
+ //# sourceMappingURL=write.mjs.map
@@ -0,0 +1,2 @@
1
+ var c=Object.defineProperty;var o=(s,a)=>c(s,"name",{value:a,configurable:!0});import{context as t}from"../lib/runtime.mjs";function l(){const{config:s,state:a}=t;return{front:s.front,upstreams:a.endpoints.map(r=>({name:r.name,url:r.url,backend:r.backend,driver:r.driver,protocol:r.protocol,version:r.facts?.version??r.version,models:r.models??r.facts?.models??[],default:r.default,facts:r.facts,probed:r.probed}))}}o(l,"status");export{l as status};
2
+ //# sourceMappingURL=status.mjs.map
@@ -0,0 +1,2 @@
1
+ var p=Object.defineProperty;var f=(e,n)=>p(e,"name",{value:n,configurable:!0});import{context as d,self as y}from"../lib/runtime.mjs";import{capture as l}from"../lib/capture.mjs";import{messages as u}from"../lib/sse.mjs";function v(e,n){return!n||!e||typeof e!="object"?e:e.message&&typeof e.message=="object"&&e.message.model!==void 0?{...e,message:{...e.message,model:n}}:e.model===void 0?e:{...e,model:n}}f(v,"restore");function D(e,n){const o=l(y,d,n.front),r=o.endpoint(n.upstream),a=o.driver(r),c=o.settings(r),s=o.converters(r),i=a.chunk,m={endpoint:r,settings:c,state:n};return s&&!i?s.stream(e,n.convert,c):s?s.stream((async function*(){for await(const t of u(e))t.data!=="[DONE]"&&(yield await i(t.data,m))})(),n.convert,c):(async function*(){for await(const t of u(e)){if(t.data==="[DONE]"){yield t;continue}const g=i?await i(t.data,m):t.data;yield{...t,data:v(g,n.model)}}})()}f(D,"stream");export{D as stream};
2
+ //# sourceMappingURL=stream.mjs.map
@@ -0,0 +1,2 @@
1
+ var u=Object.defineProperty;var a=(o,t)=>u(o,"name",{value:t,configurable:!0});import{context as p,self as s}from"../../lib/runtime.mjs";import{capture as m}from"../../lib/capture.mjs";import{estimate as f}from"../../lib/estimate.mjs";async function b(o,t={}){const n=m(s,p,t.front),{request:i}=s,e=await i(o,{front:t.front}),r=n.endpoint(e.upstream),c=n.driver(r);return{input_tokens:(c.count?await c.count({endpoint:r,request:e.body,transport:n.state.transport,signal:t.signal,timeout:n.config.detectTimeout}):null)??f(e.body)}}a(b,"count");export{b as count};
2
+ //# sourceMappingURL=count.mjs.map
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ import{main as e}from"../lib/cli.mjs";process.exitCode=await e(process.argv.slice(2));
3
+ //# sourceMappingURL=llm-translator.mjs.map
package/dist/index.cjs ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/llm-translator
4
+ * @Filename: /src/index.cjs
5
+ * @Date: 2026-10-24T20:20:04-07:00 (1792898404)
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-24T20:22:38-07:00 (1792898558)
11
+ * -----
12
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
+ *
14
+ */
15
+
16
+ /**
17
+ * CommonJS entry point — a thin, synchronous re-export of the real ESM build.
18
+ *
19
+ * Node's `require()` can load an ES module synchronously and get back its
20
+ * exports directly as long as the target module doesn't use top-level await,
21
+ * so this shim re-exports dist/index.mjs instead of tsup bundling a second,
22
+ * independent copy of the library for the CJS format.
23
+ *
24
+ * Node.js versions without require(esm) (before 20.19 / 22.12) would fail with
25
+ * a bare ERR_REQUIRE_ESM, so the check below fails early with a message that
26
+ * says what to do instead.
27
+ *
28
+ * tsup.config.mjs copies this file verbatim to dist/index.cjs, so it never
29
+ * passes through esbuild itself. Its `require("./index.mjs")` resolves to the
30
+ * sibling src/index.mjs in a checkout and to the bundle in dist/.
31
+ *
32
+ * @module @cldmv/llm-translator
33
+ */
34
+ "use strict";
35
+
36
+ if (!process.features?.require_module) {
37
+ const error = new Error(
38
+ `@cldmv/llm-translator: require() needs Node.js ^20.19.0 or >=22.12.0 (this is ${process.version}). On older Node.js, load the package with import() instead.`
39
+ );
40
+ error.code = "ERR_REQUIRE_ESM";
41
+ throw error;
42
+ }
43
+
44
+ module.exports = require("./index.mjs");
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ var i=Object.defineProperty;var n=(r,t)=>i(r,"name",{value:t,configurable:!0});import{fileURLToPath as d}from"node:url";import p from"@cldmv/slothlet";import{normalize as l}from"./lib/config.mjs";import{TranslatorError as m}from"./lib/errors.mjs";import{load as c}from"./lib/pdf.mjs";import{transport as f}from"./lib/transport.mjs";import{warn as s}from"./lib/warn.mjs";(async()=>{try{await import("../devcheck.mjs")}catch{}})();async function u(r){const t=l(r),o={endpoints:t.endpoints,transport:t.transport??f,servers:new Set,warned:new Set,textOnly:new Set,front:void 0},e=await c();e.errors.canvas&&s(t,o,`@napi-rs/canvas could not be loaded (${e.errors.canvas}); PDFs will be read as text only, without page images`),e.errors.unpdf&&s(t,o,`unpdf could not be loaded (${e.errors.unpdf}); PDF documents cannot be read and fall back to a note`);const a=await p({...r.slothlet,base:d(new URL("./api",import.meta.url)),mode:"eager",context:{config:t,state:o},silent:!0});return await a.detect({network:t.detect}),a}n(u,"createTranslator");var S=u;export{m as TranslatorError,u as createTranslator,S as default};
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,2 @@
1
+ var i=Object.defineProperty;var t=(e,n)=>i(e,"name",{value:n,configurable:!0});function u(e){let n=e;if(typeof e=="string")try{n=JSON.parse(e)}catch{return}return n!==null&&typeof n=="object"&&!Array.isArray(n)?n:void 0}t(u,"decode");function r(e){return e==null||e===""||u(e)!==void 0}t(r,"isComplete");function d(e){return e==null||e===""?{}:u(e)??{_raw_arguments:typeof e=="string"?e:JSON.stringify(e)}}t(d,"parseArguments");export{r as isComplete,d as parseArguments};
2
+ //# sourceMappingURL=arguments.mjs.map
@@ -0,0 +1,2 @@
1
+ var h=Object.defineProperty;var n=(o,r)=>h(o,"name",{value:r,configurable:!0});import{resolve as v}from"./settings.mjs";function g(o){const r=[];for(const s of Object.keys(o??{})){const e=o[s];typeof e?.info=="function"&&r.push({name:s,info:e.info(),detect:e.detect,settings:e.settings,probe:e.probe,request:e.request,response:e.response,chunk:e.chunk,error:e.error,count:e.token?.count,list:e.model?.list})}return r.sort((s,e)=>s.info.order-e.info.order)}n(g,"collect");function b(o,r,s){const{config:e,state:u}=r,c=o.convert.anthropic.messages.to.openai.chat,i=o.convert.openai.chat.to.anthropic.messages,a=s??e.front,f=g(o.driver),d=a==="anthropic"?"anthropic.messages":"openai.chat",m=a==="anthropic"?{request:c.request,response:i.response,stream:i.stream}:{request:i.request,response:c.response,stream:c.stream};return{config:e,state:u,front:a,drivers:f,endpoint:n(t=>u.endpoints.find(p=>p.name===t),"endpoint"),driver:n(t=>f.find(p=>p.name===t.driver),"driver"),settings:n(t=>v(t.driverSettings,t.probed,e.settings,t.settings),"settings"),converters:n(t=>t.protocol===d?null:m,"converters")}}n(b,"capture");export{b as capture,g as collect};
2
+ //# sourceMappingURL=capture.mjs.map
@@ -0,0 +1,29 @@
1
+ var f=Object.defineProperty;var l=(t,e)=>f(t,"name",{value:e,configurable:!0});import{readFile as d}from"node:fs/promises";import{parseArgs as S}from"node:util";const u=`llm-translator - run Claude Code (or any Anthropic/OpenAI client) against local LLM servers
2
+
3
+ Usage: llm-translator --upstream <url> [options]
4
+
5
+ Options:
6
+ --front <anthropic|openai> Client protocol to serve (default anthropic)
7
+ --upstream <url> Upstream server base URL (repeatable)
8
+ --upstream-key <key> API key sent to the upstream
9
+ --backend <name> Driver for the upstream (default auto-detect: vllm, compatible, openai, anthropic)
10
+ --model <alias=target> Model alias (repeatable)
11
+ --host <host> Listen address (default 127.0.0.1)
12
+ --port <port> Listen port (default 8787)
13
+ --api-key <key> Key clients must send (x-api-key or Authorization: Bearer)
14
+ --allow-unauthenticated Allow a non-loopback --host without --api-key
15
+ --config <file> JSON config file (translator config plus a "server" object)
16
+ --silent Suppress warnings
17
+ --no-detect Skip upstream detection
18
+ --version Print the version
19
+ --help Print this help
20
+
21
+ Environment: LLM_TRANSLATOR_FRONT, LLM_TRANSLATOR_UPSTREAM, LLM_TRANSLATOR_UPSTREAM_KEY, LLM_TRANSLATOR_BACKEND,
22
+ LLM_TRANSLATOR_HOST, LLM_TRANSLATOR_PORT, LLM_TRANSLATOR_API_KEY, LLM_TRANSLATOR_ALLOW_UNAUTHENTICATED (1), LLM_TRANSLATOR_CONFIG. Flags win over the
23
+ environment, which wins over the config file.
24
+ `;function O(t){return S({args:t,allowPositionals:!1,options:{front:{type:"string"},upstream:{type:"string",multiple:!0},"upstream-key":{type:"string"},backend:{type:"string"},model:{type:"string",multiple:!0},host:{type:"string"},port:{type:"string"},"api-key":{type:"string"},"allow-unauthenticated":{type:"boolean"},config:{type:"string"},silent:{type:"boolean"},"no-detect":{type:"boolean"},version:{type:"boolean"},help:{type:"boolean"}}}).values}l(O,"parse");function y(t,e,n){const{server:L,...p}=n,s=t.upstream??(e.LLM_TRANSLATOR_UPSTREAM?e.LLM_TRANSLATOR_UPSTREAM.split(","):void 0),r={...p};if(s){const i=t["upstream-key"]??e.LLM_TRANSLATOR_UPSTREAM_KEY,a=t.backend??e.LLM_TRANSLATOR_BACKEND;r.upstreams=s.map(o=>({url:o,apiKey:i,...a?{backend:a}:{}}))}const T=t.front??e.LLM_TRANSLATOR_FRONT;if(T&&(r.front=T),t.model){r.aliases={...r.aliases};for(const i of t.model){const a=i.indexOf("=");if(a<1)throw new Error(`--model expects alias=target (got "${i}")`);r.aliases[i.slice(0,a)]=i.slice(a+1)}}t.silent&&(r.silent=!0),t["no-detect"]&&(r.detect=!1);const A=t.port??e.LLM_TRANSLATOR_PORT,c={...L,...t.host??e.LLM_TRANSLATOR_HOST?{host:t.host??e.LLM_TRANSLATOR_HOST}:{},...A?{port:Number(A)}:{},...t["api-key"]??e.LLM_TRANSLATOR_API_KEY?{apiKey:t["api-key"]??e.LLM_TRANSLATOR_API_KEY}:{},...t["allow-unauthenticated"]||e.LLM_TRANSLATOR_ALLOW_UNAUTHENTICATED==="1"?{allowUnauthenticated:!0}:{}};return{translator:r,server:c}}l(y,"merge");const N={out:l(t=>process.stdout.write(`${t}
25
+ `),"out"),err:l(t=>process.stderr.write(`${t}
26
+ `),"err"),onSignal:l(t=>{process.once("SIGINT",t),process.once("SIGTERM",t)},"onSignal")};async function w(t,e=process.env,n=N,L={}){let p;try{p=O(t)}catch(s){return n.err(`${s.message}
27
+
28
+ ${u}`),2}if(p.help)return n.out(u),0;if(p.version)return n.out(L.version??(await import("../../package.json",{with:{type:"json"}})).default.version),0;try{const s=p.config??e.LLM_TRANSLATOR_CONFIG,{translator:r,server:T}=y(p,e,s?JSON.parse(await d(s,"utf8")):{}),c=await(L.createTranslator??(await import("../index.mjs")).createTranslator)(r),i=await c.server.start(T),a=c.status();n.out(`llm-translator (${a.front} front) listening on ${i.url}`);for(const o of a.upstreams??[]){const R=o.version??"unknown",m=o.probed?.inlineSystem??"auto",_=o.probed?.images??"default";n.out(` upstream ${o.name} ${o.url}: driver=${o.driver} version=${R} inlineSystem=${m} images=${_}`)}return await new Promise(o=>n.onSignal(()=>o())),await c.server.stop(),await c.slothlet.shutdown(),0}catch(s){return n.err(`llm-translator: ${s.message}`),1}}l(w,"main");export{w as main,y as merge,O as parse,N as processIo};
29
+ //# sourceMappingURL=cli.mjs.map
@@ -0,0 +1,2 @@
1
+ var a=Object.defineProperty;var s=(t,r)=>a(t,"name",{value:r,configurable:!0});import{TranslatorError as n}from"./errors.mjs";function i(t){let r=t.length;for(;t[r-1]==="/";)r--;const e=t.slice(0,r);return e.endsWith("/v1")?e.slice(0,-3):e}s(i,"baseUrl");function o(t,r){const e=typeof t=="string"?{url:t}:t;if(!e||typeof e!="object"||typeof e.url!="string"||e.url==="")throw new n(`upstreams[${r}] needs a "url"`,{code:"INVALID_CONFIG",status:500});return{name:e.name??`upstream-${r}`,url:i(e.url),apiKey:e.apiKey,headers:{...e.headers},backend:e.backend??"auto",models:Array.isArray(e.models)?[...e.models]:void 0,default:e.default===!0,settings:{...e.settings},version:e.version,driver:null,facts:null,driverSettings:{},probed:{}}}s(o,"endpoint");function d(t={}){const r=t.front??"anthropic";if(r!=="anthropic"&&r!=="openai")throw new n(`front must be "anthropic" or "openai" (got ${JSON.stringify(r)})`,{code:"INVALID_CONFIG",status:500});let e=t.upstreams??t.upstream;if(e!==void 0&&!Array.isArray(e)&&(e=[e]),!e||e.length===0)throw new n("at least one upstream is required (config.upstreams)",{code:"INVALID_CONFIG",status:500});return{front:r,endpoints:e.map(o),aliases:{...t.aliases},settings:{...t.settings},detect:t.detect!==!1,detectTimeout:t.detectTimeout??5e3,transport:t.transport,silent:t.silent===!0}}s(d,"normalize");export{o as endpoint,d as normalize};
2
+ //# sourceMappingURL=config.mjs.map
@@ -0,0 +1,12 @@
1
+ var w=Object.defineProperty;var p=(e,n)=>w(e,"name",{value:n,configurable:!0});import{TranslatorError as y}from"./errors.mjs";import*as f from"./pdf.mjs";const _=new Set(["pdf_unreadable","pdf_unavailable"]);async function E(e,n){const{settings:a}=n;if(a.documentConverter)return await a.documentConverter(e,n);if(a.documents==="error")throw new y("Document content blocks are not supported by this translator configuration",{code:"unsupported_document",param:"messages"});const o=v(e,n);if(a.documents==="note")return[c(o,"document conversion is disabled",n)];const t=e.source??{};return t.type==="text"?[g(e,t.data)]:t.type==="content"?typeof t.content=="string"?[{type:"text",text:t.content}]:t.content:t.type==="base64"&&t.media_type==="application/pdf"?await b(e,Buffer.from(t.data,"base64"),o,n):t.type==="base64"&&t.media_type==="text/plain"?[g(e,Buffer.from(t.data,"base64").toString("utf8"))]:t.type==="url"?[c(o,"documents referenced by URL cannot be fetched by the translator",n)]:[c(o,`document sources of type ${t.type}${t.media_type?` (${t.media_type})`:""} are not supported`,n)]}p(E,"expand");async function b(e,n,a,o){const{settings:t}=o;let s;try{s=await f.extract(n,t.pdf)}catch(r){if(r instanceof y&&_.has(r.code))return[c(a,r.message,o)];throw r}const m=e.title??s.title,l=`${m?`Document "${m}"`:"Document"} (${s.pageCount} page${s.pageCount===1?"":"s"})`;if(!(t.images&&t.pdf.pages&&f.available().canvas)){const r=s.pages.map((u,x)=>`--- Page ${x+1} ---
2
+ ${u}`);return[{type:"text",text:`${l}
3
+
4
+ ${r.join(`
5
+
6
+ `)}`}]}const $=Math.min(s.pageCount,t.pdf.maxImages);let i;try{i=await f.render(n,Array.from({length:$},(r,u)=>u+1),t.pdf)}catch{i=[]}const d=[{type:"text",text:l}];return s.pages.forEach((r,u)=>{d.push({type:"text",text:`--- Page ${u+1} ---
7
+ ${r}`}),u<i.length&&d.push({type:"image",source:{type:"base64",...i[u]}})}),d}p(b,"expandPdf");function g(e,n){const a=e.title?` title="${e.title}"`:"",o=e.context?`${e.context}
8
+
9
+ `:"";return{type:"text",text:`<document${a}>
10
+ ${o}${n}
11
+ </document>`}}p(g,"wrap");function v(e,n){return e.title??h(n)??"(unnamed)"}p(v,"nameOf");function h(e){return(e.toolUseId===null?void 0:e.toolInputs.get(e.toolUseId))?.file_path??null}p(h,"pathOf");function c(e,n,a){const o=h(a),t=o?` To read it, run \`pdftotext -layout ${o} -\` with the shell tool.`:"";return{type:"text",text:`[The document "${e}" could not be read by this model. ${n.replace(/\.$/,"")}.${t}]`}}p(c,"note");export{E as expand};
12
+ //# sourceMappingURL=documents.mjs.map
@@ -0,0 +1,2 @@
1
+ var p=Object.defineProperty;var o=(r,e)=>p(r,"name",{value:e,configurable:!0});class q extends Error{static{o(this,"TranslatorError")}constructor(e,t={}){super(e);const{code:n="translator_error",status:s=400,type:u=i(s),param:a=null,details:c}=t;this.name="TranslatorError",this.code=n,this.status=s,this.type=u,this.param=a,this.details=c}toAnthropic(){return l(this.type,this.message)}toOpenAI(){return d(this.message,{type:_(this.type),param:this.param,code:this.code})}}function i(r){switch(r){case 400:return"invalid_request_error";case 401:return"authentication_error";case 403:return"permission_error";case 404:return"not_found_error";case 413:return"request_too_large";case 429:return"rate_limit_error";case 503:case 529:return"overloaded_error";default:return"api_error"}}o(i,"statusToAnthropicType");function v(r){switch(r){case"invalid_request_error":return 400;case"authentication_error":return 401;case"permission_error":return 403;case"not_found_error":return 404;case"request_too_large":return 413;case"rate_limit_error":return 429;case"overloaded_error":return 529;default:return 500}}o(v,"anthropicTypeToStatus");function _(r){switch(r){case"invalid_request_error":case"not_found_error":case"request_too_large":return"invalid_request_error";case"authentication_error":return"authentication_error";case"permission_error":return"permission_error";case"rate_limit_error":return"rate_limit_error";default:return"server_error"}}o(_,"anthropicToOpenAIType");function N(r,e){if(typeof e=="number")return i(e);const t=Number(r.code);if(Number.isInteger(t)&&t>=400)return i(t);switch(r.type){case"invalid_request_error":case"BadRequestError":return"invalid_request_error";case"authentication_error":return"authentication_error";case"rate_limit_error":return"rate_limit_error";default:return"api_error"}}o(N,"openaiToAnthropicType");function l(r,e){return{type:"error",error:{type:r,message:e}}}o(l,"anthropicError");function d(r,{type:e="server_error",param:t=null,code:n=null}={}){return{error:{message:r,type:e,param:t,code:n}}}o(d,"openaiError");const m=/(?:maximum context length is|context length is only) (\d+) tokens/i,h=/you requested (\d+) tokens/i,x=/you passed (\d+) input tokens and requested (\d+) output tokens/i,f=/(?:prompt contains|request has|your request contains) (\d+) (?:input )?tokens/i,T=/(\d+) in the messages/i;function O(r){const e=y(r);return e===null?null:`prompt is too long: ${e.prompt} tokens > ${e.max} maximum`}o(O,"rewriteContextOverflow");function y(r){if(typeof r!="string")return null;const e=m.exec(r);if(!e)return null;const t=Number(e[1]),n=x.exec(r);if(n)return{prompt:Number(n[1])+Number(n[2]),max:t};const s=h.exec(r)??f.exec(r)??T.exec(r);return{prompt:s?Number(s[1]):t+1,max:t}}o(y,"contextOverflow");export{q as TranslatorError,l as anthropicError,_ as anthropicToOpenAIType,v as anthropicTypeToStatus,y as contextOverflow,d as openaiError,N as openaiToAnthropicType,O as rewriteContextOverflow,i as statusToAnthropicType};
2
+ //# sourceMappingURL=errors.mjs.map
@@ -0,0 +1,2 @@
1
+ var i=Object.defineProperty;var e=(t,s)=>i(t,"name",{value:s,configurable:!0});function r(t){return Math.ceil(JSON.stringify([t.system,t.messages,t.tools]).length/4)}e(r,"estimate");export{r as estimate};
2
+ //# sourceMappingURL=estimate.mjs.map
@@ -0,0 +1,2 @@
1
+ var _=Object.defineProperty;var o=(t,e)=>_(t,"name",{value:e,configurable:!0});function p(t,{hasToolCalls:e,matchedStop:n,argumentsComplete:s=!0}){return e&&(t!=="length"||s)?{stop_reason:"tool_use",stop_sequence:null}:t==="length"?{stop_reason:"max_tokens",stop_sequence:null}:t==="content_filter"?{stop_reason:"refusal",stop_sequence:null}:typeof n=="string"&&n!==""?{stop_reason:"stop_sequence",stop_sequence:n}:{stop_reason:"end_turn",stop_sequence:null}}o(p,"toAnthropicStop");function u(t){switch(t){case"max_tokens":return"length";case"tool_use":return"tool_calls";case"refusal":return"content_filter";default:return"stop"}}o(u,"toOpenAIFinish");function l(t){const e=t?.prompt_tokens??0,n=t?.prompt_tokens_details?.cached_tokens??0;return{input_tokens:Math.max(0,e-n),output_tokens:t?.completion_tokens??0,cache_creation_input_tokens:0,cache_read_input_tokens:n}}o(l,"toAnthropicUsage");function i(t){const e=t?.cache_read_input_tokens??0,n=(t?.input_tokens??0)+e+(t?.cache_creation_input_tokens??0),s=t?.output_tokens??0,r={prompt_tokens:n,completion_tokens:s,total_tokens:n+s};return e>0&&(r.prompt_tokens_details={cached_tokens:e}),r}o(i,"toOpenAIUsage");export{p as toAnthropicStop,l as toAnthropicUsage,u as toOpenAIFinish,i as toOpenAIUsage};
2
+ //# sourceMappingURL=finish.mjs.map
@@ -0,0 +1,2 @@
1
+ var y=Object.defineProperty;var o=(t,e)=>y(t,"name",{value:e,configurable:!0});function b(t,e){return t+e}o(b,"join");function d(t){return t==="localhost"||t==="::1"||/^127\./.test(t)}o(d,"isLoopback");function h(t,e="bearer",n){const r={...t.headers};return t.apiKey&&(e==="x-api-key"?r["x-api-key"]=t.apiKey:r.authorization=`Bearer ${t.apiKey}`),{...r,...n}}o(h,"headers");function x(t){const e={};if(!t)return e;if(typeof t.forEach=="function")return t.forEach((n,r)=>{e[r.toLowerCase()]=n}),e;for(const[n,r]of Object.entries(t))e[n.toLowerCase()]=String(r);return e}o(x,"plain");async function g(t,e,n={}){const{timeout:r,signal:c,body:f,...l}=n,a=[];c&&a.push(c),r>0&&a.push(AbortSignal.timeout(r));try{const i=await t(e,{...l,redirect:"error",body:f===void 0?void 0:JSON.stringify(f),signal:a.length>1?AbortSignal.any(a):a[0]}),s=await i.text();let u=null;try{u=JSON.parse(s)}catch{u=s===""?null:s}return{ok:i.ok,status:i.status,body:u}}catch(i){return{ok:!1,status:0,body:null,error:i}}}o(g,"json");export{h as headers,d as isLoopback,b as join,g as json,x as plain};
2
+ //# sourceMappingURL=http.mjs.map
@@ -0,0 +1,2 @@
1
+ var f=Object.defineProperty;var n=(t,r)=>f(t,"name",{value:r,configurable:!0});import{randomBytes as h,createHash as p}from"node:crypto";function u(t=12){return h(t).toString("hex")}n(u,"random");function $(){return`msg_${u()}`}n($,"messageId");function g(){return`toolu_${u()}`}n(g,"toolUseId");function d(){return`chatcmpl-${u()}`}n(d,"completionId");function O(t){return typeof t!="string"||t===""?g():t.replace(/[^a-zA-Z0-9_-]/g,"_")}n(O,"anthropicToolId");function b({max:t,map:r={}}){const i=new Map(Object.entries(r).map(([e,c])=>[c,e]));return e=>{if(e.length<=t&&/^[a-zA-Z0-9_-]+$/.test(e))return e;if(i.has(e))return i.get(e);const c=e.replace(/[^a-zA-Z0-9_-]/g,"_");let o=c;if(c.length>t||c===""){const s=p("sha256").update(e).digest("hex").slice(0,8);o=`${c.slice(0,t-9)}_${s}`}const l=o;for(let s=2;Object.hasOwn(r,o);s++)o=`${l.slice(0,t-4)}_${s}`;return r[o]=e,i.set(e,o),o}}n(b,"createNameFitter");function x(t,r){return Object.hasOwn(t,r)?t[r]:r}n(x,"restoreName");export{O as anthropicToolId,d as completionId,b as createNameFitter,$ as messageId,x as restoreName,g as toolUseId};
2
+ //# sourceMappingURL=ids.mjs.map
@@ -0,0 +1,2 @@
1
+ var s=Object.defineProperty;var i=(t,e)=>s(t,"name",{value:e,configurable:!0});const r="[An image was omitted because this model does not accept images.]",a=[/\bnot a multimodal\b/i,/\bat most 0 images?\b/i,/\bimage_url is only supported\b/i,/\b(?:does not|doesn't|do not|cannot|can't|not) (?:support|accept|handle)\b[^.]{0,30}\bimages?\b/i,/\bimages?(?: input| inputs| content)? (?:is|are) not (?:supported|enabled|accepted)\b/i,/\bimage input (?:is )?not (?:supported|enabled)\b/i];function c(t){return a.some(e=>e.test(t))}i(c,"refused");function u(t){return(t?.messages??[]).some(e=>Array.isArray(e?.content)&&e.content.some(n=>n?.type==="image_url"))}i(u,"carries");function m(t){const e=(t.messages??[]).map(n=>Array.isArray(n?.content)&&n.content.some(o=>o?.type==="image_url")?{...n,content:n.content.map(o=>o?.type==="image_url"?{type:"text",text:r}:o)}:n);return{...t,messages:e}}i(m,"strip");export{r as NOTE,u as carries,c as refused,m as strip};
2
+ //# sourceMappingURL=images.mjs.map
@@ -0,0 +1,2 @@
1
+ var f=Object.defineProperty;var s=(r,t)=>f(r,"name",{value:t,configurable:!0});import*as a from"./http.mjs";async function h({endpoint:r,transport:t,signal:d,timeout:i,auth:l,extraHeaders:n}){const o=await a.json(t,a.join(r.url,"/v1/models"),{method:"GET",headers:a.headers(r,l,n),signal:d,timeout:i});return!o.ok||!Array.isArray(o.body?.data)?null:o.body.data.filter(e=>e&&typeof e.id=="string")}s(h,"fetchModels");export{h as fetchModels};
2
+ //# sourceMappingURL=models.mjs.map
@@ -0,0 +1,2 @@
1
+ var s=Object.defineProperty;var n=(t,e)=>s(t,"name",{value:e,configurable:!0});function i(t){if(Array.isArray(t.tools)&&t.tools.length>0)return t;const e={...t};return delete e.tools,delete e.tool_choice,delete e.parallel_tool_calls,e}n(i,"noEmptyTools");function u(t){const e=/^v?(\d+)(?:\.(\d+))?(?:\.(\d+))?/.exec(String(t??""));return e?[Number(e[1]),Number(e[2]??0),Number(e[3]??0)]:null}n(u,"semver");function a(t,e){const o=u(t);if(!o)return!0;const l=u(e);for(let r=0;r<3;r++)if(o[r]!==l[r])return o[r]>l[r];return!0}n(a,"atLeast");export{a as atLeast,i as noEmptyTools,u as semver};
2
+ //# sourceMappingURL=normalize.mjs.map
@@ -0,0 +1,2 @@
1
+ var r=Object.defineProperty;var _=(t,e)=>r(t,"name",{value:e,configurable:!0});const i=["model","messages","stream","stream_options","max_tokens","max_completion_tokens","temperature","top_p","n","stop","presence_penalty","frequency_penalty","logit_bias","logprobs","top_logprobs","seed","response_format","tools","tool_choice","parallel_tool_calls","reasoning_effort","user","metadata","store","service_tier"],p=["top_k","min_p","repetition_penalty","length_penalty","min_tokens","stop_token_ids","include_stop_str_in_output","ignore_eos","skip_special_tokens","spaces_between_special_tokens","allowed_token_ids","bad_words","prompt_logprobs","truncate_prompt_tokens","add_generation_prompt","continue_final_message","add_special_tokens","chat_template_kwargs","thinking_token_budget","include_reasoning","structured_outputs"],l=new Set([...i,...p]),c=new Set(["include_usage","continuous_usage_stats"]);function m(t){const e={};for(const[o,s]of Object.entries(t))if(l.has(o))if(o==="stream_options"){const n=Object.fromEntries(Object.entries(s??{}).filter(([a])=>c.has(a)));Object.keys(n).length>0&&(e.stream_options=n)}else e[o]=s;return e}_(m,"filterChatBody");export{m as filterChatBody};
2
+ //# sourceMappingURL=passthrough.mjs.map
@@ -0,0 +1,2 @@
1
+ var f=Object.defineProperty;var r=(t,e)=>f(t,"name",{value:e,configurable:!0});import{TranslatorError as u}from"./errors.mjs";let n=null,l=null,i=!1;async function g(t=e=>import(e)){const e={};n=null,l=null;try{n=await t("unpdf")}catch(a){e.unpdf=a.message}try{l=await t("@napi-rs/canvas")}catch(a){e.canvas=a.message}return i=!0,{unpdf:n!==null,canvas:l!==null,errors:e}}r(g,"load");function x(){return{unpdf:n!==null,canvas:l!==null}}r(x,"available");async function y(){if(i||await g(),n===null)throw new u("PDF support is unavailable: the unpdf library could not be loaded",{code:"pdf_unavailable",status:501})}r(y,"ensure");async function c(t,e){if(await y(),t.length>e.maxBytes)throw new u(`PDF is too large: ${t.length} bytes exceeds the ${e.maxBytes} byte maximum`,{code:"pdf_too_large",status:413,type:"request_too_large"});let a;try{a=await n.getDocumentProxy(new Uint8Array(t))}catch(o){throw new u(`The PDF could not be read: ${o.message}`,{code:"pdf_unreadable",status:400,details:{name:o.name}})}if(a.numPages>e.maxPages)throw await a.loadingTask.destroy(),new u(`A maximum of ${e.maxPages} PDF pages may be provided (this document has ${a.numPages})`,{code:"pdf_too_many_pages",status:400});return a}r(c,"open");async function h(t,e){const a=await c(t,e);try{const{text:o}=await n.extractText(a,{mergePages:!1}),{info:s}=await n.getMeta(a);return{pageCount:a.numPages,title:typeof s?.Title=="string"&&s.Title!==""?s.Title:null,pages:o}}finally{await a.loadingTask.destroy()}}r(h,"extract");async function v(t,e,a){const o=await c(t,a);try{if(l===null)throw new u("PDF page rendering is unavailable: @napi-rs/canvas could not be loaded",{code:"pdf_unavailable",status:501});const s=[];for(const d of e){const p=await n.renderPageAsImage(o,d,{canvasImport:r(async()=>l,"canvasImport"),scale:a.scale});s.push({media_type:"image/png",data:Buffer.from(p).toString("base64")})}return s}finally{await o.loadingTask.destroy()}}r(v,"render");export{x as available,h as extract,g as load,v as render};
2
+ //# sourceMappingURL=pdf.mjs.map
@@ -0,0 +1,2 @@
1
+ var y=Object.defineProperty;var r=(n,i)=>y(n,"name",{value:i,configurable:!0});const s=Symbol("tick");async function*m(n,i,o){const l=n[Symbol.asyncIterator]();let t=null;try{for(;;){t??=l.next();let e;if(i>0){let c;const a=new Promise(u=>{c=setTimeout(u,i,s)});if(e=await Promise.race([t,a]),clearTimeout(c),e===s){yield typeof o=="function"?o():o;continue}}else e=await t;if(t=null,e.done)return;yield e.value}}finally{t?.catch(()=>{}),Promise.resolve(l.return?.()).catch(()=>{})}}r(m,"withPings");export{m as withPings};
2
+ //# sourceMappingURL=ping.mjs.map
@@ -0,0 +1,2 @@
1
+ var u=Object.defineProperty;var a=(e,s)=>u(e,"name",{value:s,configurable:!0});function d(e,s){return(e.models??e.facts?.models??[]).includes(s)?!0:e.protocol==="anthropic.messages"&&e.models===void 0&&/^claude-/.test(s)}a(d,"serves");function i(e,s,t){const r=Object.hasOwn(e.aliases,t)?e.aliases[t]:t,c=s.find(l=>d(l,r)),n=s.find(l=>l.default)??s[0];if(c)return{endpoint:c,model:r,unserved:!1};const o=n.models??n.facts?.models??[];return o.length===1?{endpoint:n,model:o[0],unserved:!1}:{endpoint:n,model:r,unserved:!0}}a(i,"pick");export{i as pick,d as serves};
2
+ //# sourceMappingURL=route.mjs.map
@@ -0,0 +1,2 @@
1
+ var E=Object.defineProperty;var l=(n,o)=>E(n,"name",{value:o,configurable:!0});import{timingSafeEqual as x}from"node:crypto";import{TranslatorError as h}from"./errors.mjs";import{messageId as S}from"./ids.mjs";import{sign as A}from"./thinking.mjs";const H=64*1024*1024,T={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"},y=Symbol("stalled"),_=Symbol("gone");async function D(n,o){if(!(o>0))return n;let r;const c=new Promise(f=>{r=setTimeout(f,o,y)});try{return await Promise.race([n,c])}finally{clearTimeout(r)}}l(D,"settle");async function*L(n,o,r){const c=o.front==="anthropic";c&&(yield{event:"message_start",data:{type:"message_start",message:{id:S(),type:"message",role:"assistant",model:r,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 f=await n,i=l(function*(){yield{event:"content_block_delta",data:{type:"content_block_delta",index:0,delta:{type:"signature_delta",signature:A()}}},yield{event:"content_block_stop",data:{type:"content_block_stop",index:0}}},"closeEarly");if(f.stream){let u=null;for await(const a of f.stream){if(!c){yield a;continue}if(a.event!=="message_start"){if(u===null&&a.event==="content_block_start"&&a.data.content_block.type==="thinking"){u=0;continue}u===null&&(yield*i(),u=1),yield u>0&&typeof a.data?.index=="number"?{...a,data:{...a.data,index:a.data.index+u}}:a}}return}c&&(yield*i()),yield*k(o,f.body)}l(L,"late");function*k(n,o){if(n.front==="anthropic"){yield{event:"error",data:o};return}yield{data:o},yield{data:"[DONE]"}}l(k,"failure");async function v(n,o,r,c,f=200,i=T){n.hijack();const u=n.raw;u.writeHead(f,i);try{await o.write(u,c,{front:o.front,interval:o.interval})}catch(a){if(!r.signal.aborted){const d=a instanceof h;d||n.log.error(a);const{body:b}=o.build(d?a.status:502,d?a.message:"Internal server error",d?{type:a.type,code:a.code}:{});for(const t of k(o,b))u.write(o.encode(t))}}return u.end(),n}l(v,"stream");function O(n,o){const r=Buffer.from(n),c=Buffer.from(o);return r.length===c.length&&x(r,c)}l(O,"same");function B(n){const o=/^Bearer\s+(.+)$/i.exec(n.authorization??"");return n["x-api-key"]||o?.[1]||""}l(B,"keyOf");function I(n){const o={};for(const[r,c]of Object.entries(n))(r==="anthropic-beta"||r==="anthropic-version"||r.startsWith("x-claude-code-"))&&(o[r]=String(c));return o}l(I,"forwardable");function N(n,o,r){const{front:c,filter:f}=r,i={...r,build:l((t,e,s={})=>r.build(t,e,{...s,front:c}),"build"),send:l((t,e)=>r.send(t,{...e,front:c,filter:f}),"send"),count:l((t,e)=>r.count(t,{...e,front:c}),"count"),list:l(t=>r.list({...t,front:c}),"list")},u=[o.apiKey??[]].flat().filter(Boolean),a=o.bodyLimit??H,d=l((t,e)=>t.code(e.status).headers(e.headers).send(e.body),"reject");n.setErrorHandler((t,e,s)=>{if(t instanceof h)return d(s,i.build(t.status,t.message,{type:t.type,code:t.code,param:t.param}));const m=t.statusCode??500;return m<500?d(s,i.build(m,t.message)):(e.log.error(t),d(s,i.build(m,"Internal server error")))}),n.addHook("onRequest",async(t,e)=>{if(u.length===0||t.routeOptions.config?.public)return;const s=B(t.headers);if(!(s&&u.some(m=>O(s,m))))return d(e,i.build(401,"invalid x-api-key"))}),n.route({method:["GET","HEAD"],url:"/",config:{public:!0},handler:l(async(t,e)=>e.send({ok:!0}),"handler")}),n.route({method:["GET","HEAD"],url:"/api/hello",config:{public:!0},handler:l(async(t,e)=>e.send({ok:!0}),"handler")}),n.get("/v1/models",async(t,e)=>{const s=new AbortController;return e.raw.on("close",()=>s.abort()),i.list({signal:s.signal})});const b=i.front==="anthropic"?"/v1/messages":"/v1/chat/completions";n.post(b,{bodyLimit:a},async(t,e)=>{if(e.raw.destroyed)return e;const s=new AbortController;e.raw.on("close",()=>{e.raw.writableFinished||s.abort()});const m=i.send(t.body,{signal:s.signal,clientHeaders:I(t.headers)});m.catch(()=>{});const w=new Promise(p=>s.signal.addEventListener("abort",()=>p(_),{once:!0})),g=await D(Promise.race([m,w]),t.body?.stream===!0?i.interval:0);return g===_?e:g===y?v(e,i,s,L(m,i,t.body?.model)):g.stream?v(e,i,s,g.stream,g.status,g.headers):d(e,g)}),i.front==="anthropic"&&n.post("/v1/messages/count_tokens",{bodyLimit:a},async(t,e)=>{const s=new AbortController;return e.raw.on("close",()=>s.abort()),i.count(t.body,{signal:s.signal})})}l(N,"register");export{N as register};
2
+ //# sourceMappingURL=routes.mjs.map
@@ -0,0 +1,2 @@
1
+ import*as t from"@cldmv/slothlet/runtime";const o=t.context,e=t.self;export{o as context,e as self};
2
+ //# sourceMappingURL=runtime.mjs.map
@@ -0,0 +1,2 @@
1
+ var o=Object.defineProperty;var u=(i,n)=>o(i,"name",{value:n,configurable:!0});const f=Number.MAX_SAFE_INTEGER,m=["maximum","minimum","exclusiveMaximum","exclusiveMinimum"];function r(i){if(Array.isArray(i))return i.map(r);if(i===null||typeof i!="object")return i;const n={};for(const[t,e]of Object.entries(i))t!=="$schema"&&(m.includes(t)&&typeof e=="number"&&Math.abs(e)>=f||(n[t]=r(e)));return n}u(r,"sanitize");export{r as sanitize};
2
+ //# sourceMappingURL=schema.mjs.map
@@ -0,0 +1,2 @@
1
+ var l=Object.defineProperty;var s=(n,t)=>l(n,"name",{value:t,configurable:!0});import{TranslatorError as a}from"./errors.mjs";const i=Object.freeze({inlineSystem:"auto",stripBillingHeader:!0,reasoningField:"reasoning",thinkTags:!0,thinking:"chat_template_kwargs",thinkingBudget:!1,effort:!1,images:!0,imageFallback:!0,documents:"extract",documentConverter:null,pdf:Object.freeze({pages:!0,maxPages:100,maxBytes:32*1024*1024,maxImages:20,scale:1.5}),toolSchema:"pass",deferredTools:"forward",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,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"],maxTokensField:["max_completion_tokens","max_tokens"],prefill:["continue_final_message","none"],unknownFields:["drop","error"],toolStreaming:["buffer","stream"]},d=["effortPassthrough","passthroughFilter","imageFallback"];function g(...n){const t={...i,pdf:{...i.pdf}};for(const e of n)if(e)for(const[o,r]of Object.entries(e))r!==void 0&&(o==="pdf"?Object.assign(t.pdf,r):t[o]=r);for(const[e,o]of Object.entries(f))if(!o.includes(t[e]))throw new a(`Invalid setting ${e}: ${JSON.stringify(t[e])} (expected one of ${o.join(", ")})`,{code:"invalid_setting",status:500,details:{key:e,allowed:o}});for(const e of d)if(typeof t[e]!="boolean")throw new a(`Invalid setting ${e}: ${JSON.stringify(t[e])} (expected true or false)`,{code:"invalid_setting",status:500,details:{key:e}});return t}s(g,"resolve");export{i as DEFAULTS,g as resolve};
2
+ //# sourceMappingURL=settings.mjs.map
@@ -0,0 +1,2 @@
1
+ var a=Object.defineProperty;var o=(e,t)=>a(e,"name",{value:t,configurable:!0});async function*f(e){if(typeof e?.getReader=="function"){const t=e.getReader();try{for(;;){const{done:i,value:n}=await t.read();if(i)return;yield n}}finally{await t.cancel().catch(()=>{})}}else if(typeof e?.[Symbol.asyncIterator]=="function")yield*e;else if(typeof e!="string"&&typeof e?.[Symbol.iterator]=="function"&&!(e instanceof Uint8Array))for(const t of e)yield t;else yield e}o(f,"items");async function*d(e){const t=new TextDecoder;for await(const n of f(e))yield typeof n=="string"?n:t.decode(n,{stream:!0});const i=t.decode();i&&(yield i)}o(d,"text");export{f as items,d as text};
2
+ //# sourceMappingURL=source.mjs.map
@@ -0,0 +1,10 @@
1
+ var g=Object.defineProperty;var c=(e,n)=>g(e,"name",{value:n,configurable:!0});import{items as h}from"./source.mjs";import{TranslatorError as v}from"./errors.mjs";function y(){let e="",n=!0,r,t,a=[];const l=c(i=>{if(a.length>0){const f={data:a.join(`
2
+ `)};r!==void 0&&(f.event=r),t!==void 0&&(f.id=t),i.push(f)}r=void 0,t=void 0,a=[]},"dispatch"),p=c((i,f)=>{if(i==="")return l(f);if(i.startsWith(":"))return;const d=i.indexOf(":"),s=d===-1?i:i.slice(0,d);let o=d===-1?"":i.slice(d+1);o.startsWith(" ")&&(o=o.slice(1)),s==="data"?a.push(o):s==="event"?r=o:s==="id"&&(t=o)},"line");return{push(i){const f=[];e+=i,n&&e.length>0&&(n=!1,e.charCodeAt(0)===65279&&(e=e.slice(1)));let d=0;for(let s=0;s<e.length;s++){const o=e[s];if(!(o!==`
3
+ `&&o!=="\r")){if(o==="\r"&&s===e.length-1)break;p(e.slice(d,s),f),o==="\r"&&e[s+1]===`
4
+ `&&s++,d=s+1}}return e=e.slice(d),f},flush(){const i=[];return e.length>0&&(p(e.replace(/\r$/,""),i),e=""),l(i),i}}}c(y,"createParser");async function*S(e){const n=y(),r=new TextDecoder;for await(const t of h(e)){const a=typeof t=="string"?t:r.decode(t,{stream:!0});yield*n.push(a)}yield*n.push(r.decode()),yield*n.flush()}c(S,"parse");function $({event:e,data:n,id:r}){let t="";e!==void 0&&(t+=`event: ${e}
5
+ `),r!==void 0&&(t+=`id: ${r}
6
+ `);const a=typeof n=="string"?n:JSON.stringify(n);for(const l of a.split(`
7
+ `))t+=`data: ${l}
8
+ `;return`${t}
9
+ `}c($,"encode");async function*E(e){const n=y(),r=new TextDecoder;for await(const t of h(e))if(typeof t=="string"||t instanceof Uint8Array){const a=typeof t=="string"?t:r.decode(t,{stream:!0});for(const l of n.push(a))yield u(l)}else"data"in t?yield typeof t.data=="string"?u(t):t:yield t.type===void 0?{data:t}:{event:t.type,data:t};for(const t of[...n.push(r.decode()),...n.flush()])yield u(t)}c(E,"messages");function u(e){if(e.data==="[DONE]")return e;try{return{...e,data:JSON.parse(e.data)}}catch{throw new v("Upstream sent an SSE event whose data is not valid JSON",{code:"invalid_stream_data",status:502,details:{data:e.data}})}}c(u,"decodeEvent");export{y as createParser,$ as encode,E as messages,S as parse};
10
+ //# sourceMappingURL=sse.mjs.map
@@ -0,0 +1,2 @@
1
+ var p=Object.defineProperty;var i=(t,e)=>p(t,"name",{value:e,configurable:!0});const g="llmt:",l="<think>",o="</think>";function x(){return g+Buffer.from(JSON.stringify({v:1})).toString("base64url")}i(x,"sign");function d(t){return typeof t=="string"&&t.startsWith(g)}i(d,"isOurs");function S(t){const e=t?.reasoning??t?.reasoning_content;return typeof e=="string"?e:""}i(S,"reasoningOf");function O(t){const e=t.trimStart();if(!e.startsWith(l))return{reasoning:"",text:t};const r=e.indexOf(o);return r===-1?{reasoning:e.slice(l.length),text:""}:{reasoning:e.slice(l.length,r),text:e.slice(r+o.length).trimStart()}}i(O,"splitThink");function b(){let t="start",e="";const r=i((a,f)=>{let c="",h="",n=e+a;for(e="";;){if(t==="plain"){h+=n;break}if(t==="start"){const s=n.trimStart();if(s===""&&!f){e=n;break}if(s.startsWith(l))t="think",n=s.slice(l.length);else if(!f&&s!==""&&l.startsWith(s)){e=n;break}else t="plain"}else if(t==="think"){const s=n.indexOf(o);if(s!==-1){c+=n.slice(0,s),n=n.slice(s+o.length),t="after";continue}const u=f?0:k(n,o);c+=n.slice(0,n.length-u),e=n.slice(n.length-u);break}else{if(n=n.trimStart(),n==="")break;t="plain"}}return{reasoning:c,text:h}},"step");return{push:i(a=>r(a,!1),"push"),end:i(()=>r("",!0),"end")}}i(b,"createThinkSplitter");function k(t,e){for(let r=Math.min(t.length,e.length-1);r>0;r--)if(e.startsWith(t.slice(t.length-r)))return r;return 0}i(k,"partialSuffix");export{b as createThinkSplitter,d as isOurs,S as reasoningOf,x as sign,O as splitThink};
2
+ //# sourceMappingURL=thinking.mjs.map
@@ -0,0 +1,2 @@
1
+ var E=Object.defineProperty;var d=(o,t)=>E(o,"name",{value:t,configurable:!0});import L from"node:http";import C from"node:https";import{Readable as x}from"node:stream";const O=new Set([204,205,304]);function m(o,t={},n={http:L,https:C}){const u=new URL(o),p={"accept-encoding":"identity",...t.headers};t.body!==void 0&&(p["content-length"]=String(Buffer.byteLength(t.body)));const{signal:r,redirect:b}=t;return new Promise((l,s)=>{if(r?.aborted)return s(r.reason);const a=(u.protocol==="https:"?n.https:n.http).request(u,{method:t.method??"GET",headers:p,agent:!1},e=>{if(r?.removeEventListener("abort",c),b==="error"&&e.statusCode>=300&&e.statusCode<400&&e.headers.location)return e.destroy(),s(new TypeError(`unexpected redirect to ${e.headers.location}`));const f=new Headers;for(const[y,w]of Object.entries(e.headers))for(const v of[].concat(w))f.append(y,v);const h=O.has(e.statusCode)?null:x.toWeb(e);h===null&&e.resume(),l(new Response(h,{status:e.statusCode,statusText:e.statusMessage,headers:f})),r?.addEventListener("abort",()=>e.destroy(r.reason),{once:!0})}),c=d(()=>a.destroy(r.reason),"onAbort");r?.addEventListener("abort",c,{once:!0}),a.on("error",s),a.end(t.body)})}d(m,"transport");export{m as transport};
2
+ //# sourceMappingURL=transport.mjs.map
@@ -0,0 +1,2 @@
1
+ var a=Object.defineProperty;var i=(e,t)=>a(e,"name",{value:t,configurable:!0});import{contextOverflow as c,rewriteContextOverflow as u,statusToAnthropicType as f}from"./errors.mjs";function m(e){if(typeof e=="string"&&e!=="")return e;if(!Array.isArray(e))return;const t=e.map(r=>r?.msg).filter(r=>typeof r=="string"&&r!=="");return t.length>0?t.join("; "):void 0}i(m,"detail");function g(e){const t=f(e);return t==="api_error"&&e<500?"invalid_request_error":t}i(g,"anthropicType");function y(e){return e===401?"authentication_error":e===403?"permission_denied_error":e===404?"not_found_error":e===429?"rate_limit_error":e>=500?"server_error":"invalid_request_error"}i(y,"openaiType");function l(e,t){const r=`upstream returned HTTP ${t}`;if(typeof e=="string")return{message:e||r};if(!e||typeof e!="object")return{message:r};const o=e.error;if(typeof o=="string")return{message:o};const n=o&&typeof o=="object"?o:e;return{message:typeof n.message=="string"&&n.message!==""?n.message:m(e.detail)??r,type:typeof n.type=="string"?n.type:void 0,code:n.code,param:n.param}}i(l,"parse");function x(e,t,r,o={}){if(e==="anthropic"){const s=u(r.message);if(s!==null)return{status:400,body:{type:"error",error:{type:"invalid_request_error",message:s}}};const p=o.native&&r.type?r.type:g(t);return{status:t,body:{type:"error",error:{type:p,message:r.message}}}}const n=o.native&&r.type?r.type:y(t);return{status:t,body:{error:{message:r.message,type:n,param:r.param??null,code:c(r.message)?"context_length_exceeded":r.code??null}}}}i(x,"render");export{g as anthropicType,y as openaiType,l as parse,x as render};
2
+ //# sourceMappingURL=upstream-error.mjs.map
@@ -0,0 +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};
2
+ //# sourceMappingURL=warn.mjs.map
package/package.json CHANGED
@@ -1,6 +1,128 @@
1
1
  {
2
- "name": "@cldmv/llm-translator",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@cldmv/llm-translator",
3
+ "version": "0.2.0",
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
+ "type": "module",
6
+ "license": "Apache-2.0",
7
+ "author": {
8
+ "name": "Shinrai",
9
+ "company": "CLDMV",
10
+ "email": "git+npm@cldmv.net",
11
+ "url": "https://cldmv.net"
12
+ },
13
+ "contributors": [
14
+ {
15
+ "name": "CLDMV",
16
+ "url": "https://github.com/CLDMV"
17
+ }
18
+ ],
19
+ "funding": {
20
+ "type": "github",
21
+ "url": "https://github.com/sponsors/shinrai"
22
+ },
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "sideEffects": false,
27
+ "engines": {
28
+ "node": ">=22.15.0"
29
+ },
30
+ "main": "./dist/index.cjs",
31
+ "module": "./dist/index.mjs",
32
+ "types": "./types/src/index.d.mts",
33
+ "bin": {
34
+ "llm-translator": "./dist/bin/llm-translator.mjs"
35
+ },
36
+ "exports": {
37
+ ".": {
38
+ "llm-translator-dev": {
39
+ "types": "./types/src/index.d.mts",
40
+ "import": "./src/index.mjs",
41
+ "require": "./src/index.cjs"
42
+ },
43
+ "types": "./types/src/index.d.mts",
44
+ "import": "./dist/index.mjs",
45
+ "require": "./dist/index.cjs"
46
+ },
47
+ "./package.json": "./package.json"
48
+ },
49
+ "files": [
50
+ "dist/",
51
+ "!dist/**/*.map",
52
+ "types/",
53
+ "!types/devcheck.d.mts"
54
+ ],
55
+ "repository": {
56
+ "type": "git",
57
+ "url": "git+https://github.com/CLDMV/llm-translator.git"
58
+ },
59
+ "bugs": {
60
+ "url": "https://github.com/CLDMV/llm-translator/issues"
61
+ },
62
+ "homepage": "https://github.com/CLDMV/llm-translator#readme",
63
+ "keywords": [
64
+ "llm",
65
+ "anthropic",
66
+ "openai",
67
+ "claude-code",
68
+ "vllm",
69
+ "chat-completions",
70
+ "messages-api",
71
+ "translator",
72
+ "proxy",
73
+ "slothlet"
74
+ ],
75
+ "scripts": {
76
+ "build": "tsup",
77
+ "build:ci": "npm run lint && npm run format:check && npm run types:build && npm run build",
78
+ "prepack": "npm run build",
79
+ "prepare": "node -e \"import('./.githooks/install.mjs').catch(()=>{})\"",
80
+ "test": "node tests/run-vitest.mjs && npm run test:dist",
81
+ "test:dist": "npm run build && node --conditions=llm-translator-dev --test tests/cjs/entry.test.cjs",
82
+ "test:watch": "vitest --config .configs/vitest.config.mjs",
83
+ "coverage": "node tests/run-vitest.mjs --coverage-quiet",
84
+ "ci:coverage": "npm run coverage && npm run test:dist",
85
+ "types:build": "tsc -p .configs/tsconfig.json",
86
+ "types:check": "tsc -p .configs/tsconfig.json --noEmit",
87
+ "lint": "eslint --config .configs/eslint.config.mjs .",
88
+ "lint:fix": "eslint --config .configs/eslint.config.mjs . --fix",
89
+ "format": "prettier --config .configs/.prettierrc --write .",
90
+ "format:check": "prettier --config .configs/.prettierrc --check .",
91
+ "fix:headers": "fix-headers --config .configs/fix-headers.json"
92
+ },
93
+ "peerDependencies": {
94
+ "fastify": "^5.12.7"
95
+ },
96
+ "peerDependenciesMeta": {
97
+ "fastify": {
98
+ "optional": true
99
+ }
100
+ },
101
+ "dependencies": {
102
+ "@cldmv/slothlet": "^3.22.1",
103
+ "@napi-rs/canvas": "^1.0.13",
104
+ "unpdf": "^1.9.1"
105
+ },
106
+ "devDependencies": {
107
+ "@cldmv/configs": "^1.3.0",
108
+ "@cldmv/eslint-plugin-jsonv": "^1.0.17",
109
+ "@cldmv/fix-headers": "^2.2.2",
110
+ "@cldmv/jsonv": "^1.1.5",
111
+ "@cldmv/prettier-plugin-jsonv": "^1.1.4",
112
+ "@cldmv/slothlet-types": "^3.22.1",
113
+ "@cldmv/vitest-runner": "^1.5.4",
114
+ "@eslint/css": "^2.0.1",
115
+ "@eslint/js": "^10.0.1",
116
+ "@eslint/json": "^2.1.1",
117
+ "@eslint/markdown": "^8.0.3",
118
+ "@types/node": "^26.6.5",
119
+ "@vitest/coverage-v8": "^5.0.5",
120
+ "eslint": "^10.13.0",
121
+ "fastify": "^5.12.7",
122
+ "globals": "^17.13.0",
123
+ "prettier": "^3.9.10",
124
+ "tsup": "^9.0.0",
125
+ "typescript": "^7.0.2",
126
+ "vitest": "^5.0.5"
127
+ }
128
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/llm-translator
4
+ * @Filename: /src/api/convert/anthropic/messages/to/openai/chat/request.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:58:59-07:00 (1792958339)
11
+ * -----
12
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
+ *
14
+ */
15
+ /**
16
+ * Convert an Anthropic Messages request body into an OpenAI Chat Completions request body.
17
+ *
18
+ * Returns the converted body plus a JSON-serializable `state` the response/stream converters need (original model,
19
+ * tool-name map, thinking mode, fields that were dropped).
20
+ * @param {any} body - Anthropic `POST /v1/messages` body.
21
+ * @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
22
+ * @returns {Promise<{ body: any, state: any }>} OpenAI request body and conversion state.
23
+ * @throws {TranslatorError} For malformed requests, unsupported content when configured to error, or PDF limit violations.
24
+ */
25
+ export declare function request(body: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): Promise<{
26
+ body: any;
27
+ state: any;
28
+ }>;
@@ -0,0 +1,24 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/llm-translator
4
+ * @Filename: /src/api/convert/anthropic/messages/to/openai/chat/response.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:58:59-07:00 (1792958339)
11
+ * -----
12
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
+ *
14
+ */
15
+ /**
16
+ * Convert an Anthropic message into an OpenAI `chat.completion`. Text blocks are concatenated, `thinking` blocks
17
+ * become the reasoning field named by `reasoningField`, `tool_use` blocks become `tool_calls` (tool names restored
18
+ * through `state.toolNames`), and other block types (server tool results, redacted thinking) are skipped.
19
+ * @param {any} message - Anthropic message.
20
+ * @param {any} [state] - State from the request converter (original model, tool-name map).
21
+ * @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
22
+ * @returns {any} OpenAI chat completion.
23
+ */
24
+ export declare function response(message: any, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): any;
@@ -0,0 +1,29 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/llm-translator
4
+ * @Filename: /src/api/convert/anthropic/messages/to/openai/chat/stream.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:58:59-07:00 (1792958339)
11
+ * -----
12
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
+ *
14
+ */
15
+ /**
16
+ * Convert an Anthropic Messages event stream into OpenAI `chat.completion.chunk` messages followed by `[DONE]`.
17
+ *
18
+ * `source` is SSE text/bytes, an iterable of `{ event, data }` pairs, or an iterable of bare event payloads. The
19
+ * result yields `{ data }` messages (chunk objects, then the string `"[DONE]"`); encode them with `lib/sse.mjs`.
20
+ * Usage is emitted as a final `choices: []` chunk when `state.includeUsage` is not false. An Anthropic `error`
21
+ * event, or a stream that ends before `message_stop`, becomes a `{ error }` chunk followed by `[DONE]`.
22
+ * @param {ReadableStream|AsyncIterable<unknown>|Iterable<unknown>|string|Uint8Array} source - Anthropic stream.
23
+ * @param {any} [state] - State from the request converter (model, `includeUsage`, tool-name map).
24
+ * @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
25
+ * @returns {AsyncGenerator<{ data: any|string }, void, undefined>} OpenAI chunks.
26
+ */
27
+ export declare function stream(source: ReadableStream | AsyncIterable<unknown> | Iterable<unknown> | string | Uint8Array, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): AsyncGenerator<{
28
+ data: any | string;
29
+ }, void, undefined>;