@cldmv/llm-translator 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +182 -7
- package/dist/api/convert/anthropic/messages/to/openai/chat/request.mjs +17 -0
- package/dist/api/convert/anthropic/messages/to/openai/chat/response.mjs +2 -0
- package/dist/api/convert/anthropic/messages/to/openai/chat/stream.mjs +2 -0
- package/dist/api/convert/openai/chat/to/anthropic/messages/request.mjs +6 -0
- package/dist/api/convert/openai/chat/to/anthropic/messages/response.mjs +2 -0
- package/dist/api/convert/openai/chat/to/anthropic/messages/stream.mjs +2 -0
- package/dist/api/detect.mjs +2 -0
- package/dist/api/driver/anthropic/detect.mjs +2 -0
- package/dist/api/driver/anthropic/info.mjs +2 -0
- package/dist/api/driver/anthropic/model/list.mjs +2 -0
- package/dist/api/driver/anthropic/settings.mjs +2 -0
- package/dist/api/driver/anthropic/token/count.mjs +2 -0
- package/dist/api/driver/compatible/detect.mjs +2 -0
- package/dist/api/driver/compatible/info.mjs +2 -0
- package/dist/api/driver/compatible/model/list.mjs +2 -0
- package/dist/api/driver/compatible/request.mjs +2 -0
- package/dist/api/driver/compatible/settings.mjs +2 -0
- package/dist/api/driver/deepseek/detect.mjs +2 -0
- package/dist/api/driver/deepseek/error.mjs +2 -0
- package/dist/api/driver/deepseek/info.mjs +2 -0
- package/dist/api/driver/deepseek/request.mjs +2 -0
- package/dist/api/driver/deepseek/settings.mjs +2 -0
- package/dist/api/driver/llamacpp/chunk.mjs +2 -0
- package/dist/api/driver/llamacpp/detect.mjs +2 -0
- package/dist/api/driver/llamacpp/flush.mjs +2 -0
- package/dist/api/driver/llamacpp/info.mjs +2 -0
- package/dist/api/driver/llamacpp/request.mjs +2 -0
- package/dist/api/driver/llamacpp/response.mjs +2 -0
- package/dist/api/driver/llamacpp/settings.mjs +2 -0
- package/dist/api/driver/ollama/detect.mjs +2 -0
- package/dist/api/driver/ollama/info.mjs +2 -0
- package/dist/api/driver/ollama/request.mjs +2 -0
- package/dist/api/driver/ollama/settings.mjs +2 -0
- package/dist/api/driver/openai/detect.mjs +2 -0
- package/dist/api/driver/openai/info.mjs +2 -0
- package/dist/api/driver/openai/model/list.mjs +2 -0
- package/dist/api/driver/openai/request.mjs +2 -0
- package/dist/api/driver/openai/settings.mjs +2 -0
- package/dist/api/driver/sglang/detect.mjs +2 -0
- package/dist/api/driver/sglang/info.mjs +2 -0
- package/dist/api/driver/sglang/request.mjs +2 -0
- package/dist/api/driver/sglang/settings.mjs +2 -0
- package/dist/api/driver/vllm/detect.mjs +2 -0
- package/dist/api/driver/vllm/error.mjs +2 -0
- package/dist/api/driver/vllm/info.mjs +2 -0
- package/dist/api/driver/vllm/model/list.mjs +2 -0
- package/dist/api/driver/vllm/probe.mjs +2 -0
- package/dist/api/driver/vllm/request.mjs +2 -0
- package/dist/api/driver/vllm/settings.mjs +2 -0
- package/dist/api/driver/vllm/token/count.mjs +2 -0
- package/dist/api/endpoint/status.mjs +2 -0
- package/dist/api/error/build.mjs +2 -0
- package/dist/api/error/error.mjs +2 -0
- package/dist/api/model/get.mjs +2 -0
- package/dist/api/model/list.mjs +2 -0
- package/dist/api/request.mjs +3 -0
- package/dist/api/response.mjs +2 -0
- package/dist/api/route.mjs +2 -0
- package/dist/api/send.mjs +2 -0
- package/dist/api/server/plugin.mjs +2 -0
- package/dist/api/server/start.mjs +2 -0
- package/dist/api/server/stop.mjs +2 -0
- package/dist/api/sse/encode.mjs +4 -0
- package/dist/api/sse/parse.mjs +2 -0
- package/dist/api/sse/ping.mjs +2 -0
- package/dist/api/sse/write.mjs +2 -0
- package/dist/api/status.mjs +2 -0
- package/dist/api/stream.mjs +2 -0
- package/dist/api/token/count.mjs +2 -0
- package/dist/bin/llm-translator.mjs +3 -0
- package/dist/index.cjs +3 -3
- package/dist/index.mjs +1 -1
- package/dist/lib/arguments.mjs +2 -0
- package/dist/lib/backends.mjs +2 -0
- package/dist/lib/capture.mjs +2 -0
- package/dist/lib/cli.mjs +29 -0
- package/dist/lib/config.mjs +2 -0
- package/dist/lib/documents.mjs +12 -0
- package/dist/lib/errors.mjs +2 -0
- package/dist/lib/estimate.mjs +2 -0
- package/dist/lib/finish.mjs +2 -0
- package/dist/lib/http.mjs +2 -0
- package/dist/lib/ids.mjs +2 -0
- package/dist/lib/images.mjs +2 -0
- package/dist/lib/models.mjs +2 -0
- package/dist/lib/normalize.mjs +2 -0
- package/dist/lib/passthrough.mjs +2 -0
- package/dist/lib/pdf.mjs +2 -0
- package/dist/lib/ping.mjs +2 -0
- package/dist/lib/pool.mjs +2 -0
- package/dist/lib/route.mjs +2 -0
- package/dist/lib/routes.mjs +2 -0
- package/dist/lib/runtime.mjs +2 -0
- package/dist/lib/schema.mjs +2 -0
- package/dist/lib/settings.mjs +2 -0
- package/dist/lib/source.mjs +2 -0
- package/dist/lib/sse.mjs +10 -0
- package/dist/lib/thinking.mjs +3 -0
- package/dist/lib/tools.mjs +2 -0
- package/dist/lib/toolsearch.mjs +3 -0
- package/dist/lib/transport.mjs +2 -0
- package/dist/lib/upstream-error.mjs +2 -0
- package/dist/lib/usage.mjs +2 -0
- package/dist/lib/warn.mjs +2 -0
- package/package.json +40 -9
- package/types/src/api/convert/anthropic/messages/to/openai/chat/request.d.mts +19 -0
- package/types/src/api/convert/anthropic/messages/to/openai/chat/response.d.mts +11 -0
- package/types/src/api/convert/anthropic/messages/to/openai/chat/stream.d.mts +15 -0
- package/types/src/api/convert/openai/chat/to/anthropic/messages/request.d.mts +16 -0
- package/types/src/api/convert/openai/chat/to/anthropic/messages/response.d.mts +14 -0
- package/types/src/api/convert/openai/chat/to/anthropic/messages/stream.d.mts +24 -0
- package/types/src/api/detect.d.mts +7 -0
- package/types/src/api/driver/anthropic/detect.d.mts +26 -0
- package/types/src/api/driver/anthropic/info.d.mts +30 -0
- package/types/src/api/driver/anthropic/model/list.d.mts +6 -0
- package/types/src/api/driver/anthropic/settings.d.mts +23 -0
- package/types/src/api/driver/anthropic/token/count.d.mts +6 -0
- package/types/src/api/driver/compatible/detect.d.mts +9 -0
- package/types/src/api/driver/compatible/info.d.mts +12 -0
- package/types/src/api/driver/compatible/model/list.d.mts +6 -0
- package/types/src/api/driver/compatible/request.d.mts +6 -0
- package/types/src/api/driver/compatible/settings.d.mts +23 -0
- package/types/src/api/driver/deepseek/detect.d.mts +26 -0
- package/types/src/api/driver/deepseek/error.d.mts +14 -0
- package/types/src/api/driver/deepseek/info.d.mts +29 -0
- package/types/src/api/driver/deepseek/request.d.mts +8 -0
- package/types/src/api/driver/deepseek/settings.d.mts +25 -0
- package/types/src/api/driver/llamacpp/chunk.d.mts +7 -0
- package/types/src/api/driver/llamacpp/detect.d.mts +6 -0
- package/types/src/api/driver/llamacpp/flush.d.mts +26 -0
- package/types/src/api/driver/llamacpp/info.d.mts +31 -0
- package/types/src/api/driver/llamacpp/request.d.mts +8 -0
- package/types/src/api/driver/llamacpp/response.d.mts +8 -0
- package/types/src/api/driver/llamacpp/settings.d.mts +27 -0
- package/types/src/api/driver/ollama/detect.d.mts +6 -0
- package/types/src/api/driver/ollama/info.d.mts +30 -0
- package/types/src/api/driver/ollama/request.d.mts +8 -0
- package/types/src/api/driver/ollama/settings.d.mts +7 -0
- package/types/src/api/driver/openai/detect.d.mts +26 -0
- package/types/src/api/driver/openai/info.d.mts +29 -0
- package/types/src/api/driver/openai/model/list.d.mts +6 -0
- package/types/src/api/driver/openai/request.d.mts +6 -0
- package/types/src/api/driver/openai/settings.d.mts +23 -0
- package/types/src/api/driver/sglang/detect.d.mts +6 -0
- package/types/src/api/driver/sglang/info.d.mts +31 -0
- package/types/src/api/driver/sglang/request.d.mts +8 -0
- package/types/src/api/driver/sglang/settings.d.mts +24 -0
- package/types/src/api/driver/vllm/detect.d.mts +6 -0
- package/types/src/api/driver/vllm/error.d.mts +14 -0
- package/types/src/api/driver/vllm/info.d.mts +12 -0
- package/types/src/api/driver/vllm/model/list.d.mts +6 -0
- package/types/src/api/driver/vllm/probe.d.mts +25 -0
- package/types/src/api/driver/vllm/request.d.mts +8 -0
- package/types/src/api/driver/vllm/settings.d.mts +8 -0
- package/types/src/api/driver/vllm/token/count.d.mts +6 -0
- package/types/src/api/endpoint/status.d.mts +27 -0
- package/types/src/api/error/build.d.mts +12 -0
- package/types/src/api/error/error.d.mts +15 -0
- package/types/src/api/model/get.d.mts +9 -0
- package/types/src/api/model/list.d.mts +7 -0
- package/types/src/api/request.d.mts +26 -0
- package/types/src/api/response.d.mts +8 -0
- package/types/src/api/route.d.mts +18 -0
- package/types/src/api/send.d.mts +37 -0
- package/types/src/api/server/plugin.d.mts +11 -0
- package/types/src/api/server/start.d.mts +12 -0
- package/types/src/api/server/stop.d.mts +5 -0
- package/types/src/api/sse/encode.d.mts +12 -0
- package/types/src/api/sse/parse.d.mts +10 -0
- package/types/src/api/sse/ping.d.mts +16 -0
- package/types/src/api/sse/write.d.mts +12 -0
- package/types/src/api/status.d.mts +8 -0
- package/types/src/api/stream.d.mts +11 -0
- package/types/src/api/token/count.d.mts +10 -0
- package/types/src/index.d.mts +62 -15
- package/types/src/lib/arguments.d.mts +16 -0
- package/types/src/lib/backends.d.mts +21 -0
- package/types/src/lib/capture.d.mts +14 -0
- package/types/src/lib/cli.d.mts +35 -0
- package/types/src/lib/config.d.mts +51 -0
- package/types/src/lib/documents.d.mts +32 -0
- package/types/src/lib/errors.d.mts +144 -0
- package/types/src/lib/estimate.d.mts +75 -0
- package/types/src/lib/finish.d.mts +73 -0
- package/types/src/lib/http.d.mts +72 -0
- package/types/src/lib/ids.d.mts +46 -0
- package/types/src/lib/images.d.mts +39 -0
- package/types/src/lib/models.d.mts +25 -0
- package/types/src/lib/normalize.d.mts +37 -0
- package/types/src/lib/passthrough.d.mts +16 -0
- package/types/src/lib/pdf.d.mts +53 -0
- package/types/src/lib/ping.d.mts +10 -0
- package/types/src/lib/pool.d.mts +124 -0
- package/types/src/lib/route.d.mts +45 -0
- package/types/src/lib/routes.d.mts +17 -0
- package/types/src/lib/runtime.d.mts +10 -0
- package/types/src/lib/schema.d.mts +8 -0
- package/types/src/lib/settings.d.mts +189 -0
- package/types/src/lib/source.d.mts +32 -0
- package/types/src/lib/sse.d.mts +61 -0
- package/types/src/lib/thinking.d.mts +155 -0
- package/types/src/lib/tools.d.mts +28 -0
- package/types/src/lib/toolsearch.d.mts +91 -0
- package/types/src/lib/transport.d.mts +18 -0
- package/types/src/lib/upstream-error.d.mts +43 -0
- package/types/src/lib/usage.d.mts +91 -0
- package/types/src/lib/warn.d.mts +26 -0
|
@@ -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 i=Object.defineProperty;var t=(o,e)=>i(o,"name",{value:e,configurable:!0});import{context as s,self as d}from"../lib/runtime.mjs";import{capture as f}from"../lib/capture.mjs";import{resolve as v}from"../lib/route.mjs";function g(o,e={}){const n=f(d,s,e.front),{endpoint:r,model:p,requested:m,unserved:c,convert:l,pool:u}=v(n,o);return{upstream:r.name,model:p,requested:m,protocol:r.protocol,backend:r.driver,native:l===null,unserved:c,pool:u.map(a=>a.name)}}t(g,"route");export{g as route};
|
|
2
|
+
//# sourceMappingURL=route.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var G=Object.defineProperty;var s=(o,n)=>G(o,"name",{value:n,configurable:!0});import{context as q,self as D}from"../lib/runtime.mjs";import{TranslatorError as H}from"../lib/errors.mjs";import*as I from"../lib/http.mjs";import*as K from"../lib/images.mjs";import*as Q from"../lib/tools.mjs";import{parse as V}from"../lib/upstream-error.mjs";import{meter as W,normalize as X}from"../lib/usage.mjs";import{capture as Y}from"../lib/capture.mjs";import{estimate as Z,bodyLength as tt,output as et,patch as at,stream as nt}from"../lib/estimate.mjs";import{warn as rt}from"../lib/warn.mjs";import*as L from"../lib/pool.mjs";import{createBudget as ot}from"../lib/thinking.mjs";const st={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"};async function yt(o,n={}){const{transport:h,textOnly:p}=q.state,{request:y,response:w,stream:d,error:u}=D,f=u.build,{config:r,state:b}=q,x=Y(D,q,n.front),_=o&&typeof o=="object"&&!Array.isArray(o),S={model:null,requested:_&&typeof o.model=="string"?o.model:null,upstream:null,front:n.front??r.front,stream:_&&o.stream===!0},g=W(S,n.onUsage,l=>rt(r,b,l)),j=s(l=>Object.defineProperty(l,"usage",{value:g.usage,enumerable:!1,configurable:!0}),"attach");try{return j(await ut({transport:h,textOnly:p,request:y,response:w,stream:d,error:u,captured:x},o,n,g))}catch(l){if(!(l instanceof H))throw g.finish(n.signal?.aborted?"aborted":"error"),l;return g.finish("error"),j(f(l.status,l.message,{type:l.type,code:l.code,param:l.param,front:n.front}))}}s(yt,"send");async function ut({transport:o,textOnly:n,request:h,response:p,stream:y,error:w,captured:d},u,f,r){const b={front:f.front,headers:f.headers,clientHeaders:f.clientHeaders,filter:f.filter,usage:!0,budget:ot()},x=r.meta.front==="openai"&&u?.stream_options?.include_usage!==!0,_=s(e=>{r.meta.model=e.body.model??null,r.meta.upstream=e.upstream,e.state.native?r.source(null):r.source(()=>e.state.convert?.upstreamUsage,e.state.front==="anthropic"?"openai":"folded")},"note"),S=s(e=>{if(e.state.native)return{input:!1,output:!1};const t=X(e.state.front==="anthropic"?"openai":"folded",e.state.convert?.upstreamUsage);return t===null?{input:!0,output:!0}:{input:t.input_tokens===null&&t.cache_read_input_tokens===null&&t.cache_creation_input_tokens===null,output:t.output_tokens===null}},"gaps"),g=s(async e=>{const t=d.endpoint(e.upstream),a=d.driver(t);return(a.count&&!(x&&e.state.stream)?await a.count({endpoint:t,request:e.body,transport:o,signal:f.signal,timeout:d.config.detectTimeout}):null)??Z(e.body)},"input"),j=s(async(e,t)=>{const a=S(e);return!a.input&&!a.output?null:{input_tokens:a.input?await g(e):null,output_tokens:a.output?et(t):null}},"counts"),l=s((e,t,a,c,O)=>(r.finish(O),w(e,t,a,c)),"fail"),R=s(async e=>{let t=e,a=await M(o,t,f.signal,P());if(!a.ok&&a.status===400&&(t.images.sent&&t.images.retry||t.tools.sent&&t.tools.retry)){const c=await C(a),O=V(c,400).message,v={};if(t.images.sent&&t.images.retry&&K.refused(O)?(n.add(t.images.key),v.images=!1):t.tools.sent&&t.tools.retry&&Q.refused(O)&&(v.tools=!1),!("images"in v||"tools"in v))return{upstream:t,result:a,failure:c};t=await h(u,{...b,...v,endpoint:t.upstream}),_(t),a=await M(o,t,f.signal,P())}return{upstream:t,result:a}},"attempt");let i=await h(u,{...b,hold:!0});const A=i.pool;let N=0;const $=A.length>1?d.config.pool:null,P=s(()=>$&&{connectTimeout:$.connectTimeout,headersTimeout:N+1<A.length?$.headersTimeout:0},"limitsNow");let T=d.endpoint(i.upstream);const z=s(()=>T.health.inflight--,"release");let m,E;try{for(;;){const e=T;e.health.requests++,_(i);let t=null;try{({upstream:i,result:m,failure:E}=await R(i)),!m.ok&&m.status>=500&&(t=`HTTP ${m.status}`)}catch(c){if(!(c instanceof H)||c.code!=="upstream_unreachable")throw c;t=c.message,m=void 0,E=c}if(t===null){L.succeeded(e);break}L.failed(d.config,e,t);let a=null;for(;a===null&&!f.signal?.aborted&&N+1<A.length;)try{a=await h(u,{...b,endpoint:A[++N],hold:!0})}catch(c){if(!(c instanceof H)||c.code!=="endpoint_not_in_pool")throw c}if(a===null){if(m===void 0)throw E;break}try{await m?.body?.cancel?.()}catch{}z(),T=d.endpoint(a.upstream),i=a}}catch(e){throw z(),e}const F=T;T=null,r.usage.then(()=>F.health.inflight--);const B=I.plain(m.headers);if(!m.ok)return l(m.status,E??await C(m),B,i.state,"upstream_error");if(i.state.stream){const e=y(m.body,i.state),t=i.state.native?e:nt(r.meta.front,e,{gaps:s(()=>S(i),"gaps"),input:s(()=>g(i),"input"),report:s(a=>r.estimate(a),"report")});return{status:200,headers:st,stream:r.track(t,f.signal,x)}}const J=await C(m);if(J===null||typeof J!="object")return l(502,"The upstream returned a body that is not JSON",B,i.state,"error");const k=await p(J,i.state),U=await j(i,tt(r.meta.front,k));return U&&(k.usage=at(r.meta.front,k.usage,U),r.estimate(U)),r.json(k),r.finish("done"),{status:200,headers:{"content-type":"application/json"},body:k}}s(ut,"round");async function M(o,n,h,p=null){let y,w=!1,d=h;if(p&&p.headersTimeout>0){const u=new AbortController;d=h?AbortSignal.any([h,u.signal]):u.signal,y=setTimeout(()=>{w=!0,u.abort()},p.headersTimeout)}try{return await o(n.url,{method:n.method,headers:n.headers,body:JSON.stringify(n.body),signal:d,redirect:"error",...p&&p.connectTimeout>0?{connectTimeout:p.connectTimeout}:{}})}catch(u){if(h?.aborted)throw u;const f=w?`no response headers within ${p.headersTimeout} ms`:u.message;throw new H(`Could not reach the upstream (${n.upstream}): ${f}`,{code:"upstream_unreachable",status:502,details:{cause:u}})}finally{clearTimeout(y)}}s(M,"call");async function C(o){const n=await o.text();try{return JSON.parse(n)}catch{return n===""?null:n}}s(C,"parse");export{yt as send};
|
|
2
|
+
//# sourceMappingURL=send.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var d=Object.defineProperty;var f=(e,t)=>d(e,"name",{value:t,configurable:!0});import{context as g,self as m}from"../../lib/runtime.mjs";import{TranslatorError as s}from"../../lib/errors.mjs";import{register as h}from"../../lib/routes.mjs";import{resolve as p}from"../../lib/settings.mjs";async function _(e,t={}){const{config:r}=g;if(t.front!==void 0&&t.front!=="anthropic"&&t.front!=="openai")throw new s(`front must be "anthropic" or "openai" (got ${JSON.stringify(t.front)})`,{code:"invalid_config",status:500});for(const n of["authorize","onUsage"])if(t[n]!==void 0&&typeof t[n]!="function")throw new s(`${n} must be a function`,{code:"invalid_config",status:500});const{send:a,route:c,token:u,model:o,error:l,sse:i}=m;h(e,t,{front:t.front??r.front,filter:r.settings.passthroughFilter!==!1,interval:p(r.settings).pingInterval,send:a,route:c,count:u.count,list:o.list,get:o.get,build:l.build,encode:i.encode,write:i.write})}f(_,"plugin");export{_ 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 u}from"../../lib/runtime.mjs";import{TranslatorError as c}from"../../lib/errors.mjs";import{isLoopback as b}from"../../lib/http.mjs";async function L(t={}){const{state:r}=g,{plugin:d}=u.server,{build:h}=u.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&&typeof t.authorize!="function"&&t.allowUnauthenticated!==!0)throw new c(`Refusing to listen on ${e} without an apiKey or an authorize hook: anyone who can reach the port could use the upstream. Set \`apiKey\` (CLI: --api-key) or \`authorize\`, 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,f)=>{const{status:y,headers:p,body:w}=h(404,`Not found: ${o.method} ${o.url}`);return f.code(y).headers(p).send(w)}),await a.register(d,{front:t.front,apiKey:t.apiKey,bodyLimit:t.bodyLimit,authorize:t.authorize,onUsage:t.onUsage}),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(L,"start");export{L as start};
|
|
2
|
+
//# sourceMappingURL=start.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 e}from"../lib/runtime.mjs";import{describe as t}from"../lib/pool.mjs";function u(){const{config:s,state:a}=e;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,health:t(r)}))}}o(u,"status");export{u as status};
|
|
2
|
+
//# sourceMappingURL=status.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var x=Object.defineProperty;var i=(e,o)=>x(e,"name",{value:o,configurable:!0});import{context as b,self as j}from"../lib/runtime.mjs";import{capture as A}from"../lib/capture.mjs";import{messages as p}from"../lib/sse.mjs";function u(e,o){return!o||!e||typeof e!="object"?e:e.message&&typeof e.message=="object"&&e.message.model!==void 0?{...e,message:{...e.message,model:o}}:e.model===void 0?e:{...e,model:o}}i(u,"restore");function B(e){return!!e&&Array.isArray(e.choices)&&(e.choices.length===0||e.choices.some(o=>o?.finish_reason))}i(B,"ends");function k(e,o){const r=A(j,b,o.front),c=r.endpoint(o.upstream),a=r.driver(c),l=r.settings(c),f=r.converters(c),s=a.chunk,d={endpoint:c,settings:l,state:o};async function*m(){for(const n of await a.flush?.(d)??[])yield{data:n}}if(i(m,"flushed"),f&&!s)return f.stream(e,o.convert,l);if(f)return f.stream((async function*(){for await(const n of p(e))n.data!=="[DONE]"&&(yield await s(n.data,d));for await(const n of m())yield n.data})(),o.convert,l);const w=!!(s&&a.flush);return(async function*(){const n=[];let y=!1;for await(const t of p(e)){if(t.data==="[DONE]"){for await(const v of m())yield{data:u(v.data,o.model)};yield*n.splice(0),y=!0,yield t;continue}const h=s?await s(t.data,d):t.data,g={...t,data:u(h,o.model)};w&&B(h)?n.push(g):yield g}if(!y){for await(const t of m())yield{data:u(t.data,o.model)};yield*n}})()}i(k,"stream");export{k 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
|
package/dist/index.cjs
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* @Email: <Shinrai@users.noreply.github.com>
|
|
8
8
|
* -----
|
|
9
9
|
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
10
|
-
* @Last modified time: 2026-
|
|
10
|
+
* @Last modified time: 2026-11-01T16:24:44-08:00 (1793579084)
|
|
11
11
|
* -----
|
|
12
12
|
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
13
|
*
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
* so this shim re-exports dist/index.mjs instead of tsup bundling a second,
|
|
22
22
|
* independent copy of the library for the CJS format.
|
|
23
23
|
*
|
|
24
|
-
* Node.js versions without require(esm) (before
|
|
24
|
+
* Node.js versions without require(esm) (before 24) would fail with
|
|
25
25
|
* a bare ERR_REQUIRE_ESM, so the check below fails early with a message that
|
|
26
26
|
* says what to do instead.
|
|
27
27
|
*
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
|
|
36
36
|
if (!process.features?.require_module) {
|
|
37
37
|
const error = new Error(
|
|
38
|
-
`@cldmv/llm-translator: require() needs Node.js
|
|
38
|
+
`@cldmv/llm-translator: require() needs Node.js >=24.0.0 (this is ${process.version}). On older Node.js, load the package with import() instead.`
|
|
39
39
|
);
|
|
40
40
|
error.code = "ERR_REQUIRE_ESM";
|
|
41
41
|
throw error;
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(
|
|
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{load as m}from"./lib/pdf.mjs";import{transport as c}from"./lib/transport.mjs";import{warn as s}from"./lib/warn.mjs";async function f(r){const t=l(r),o={endpoints:t.endpoints,transport:t.transport??c,servers:new Set,warned:new Set,textOnly:new Set,cursors:new Map,front:void 0},e=await m();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(f,"createTranslator");import{TranslatorError as T}from"./lib/errors.mjs";var y=f;export{T as TranslatorError,f as createTranslator,y as default};
|
|
2
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 e=Object.defineProperty;var t=(o,n)=>e(o,"name",{value:n,configurable:!0});const i=["default_generation_settings","total_slots","build_info"];function r(o){return o.ok&&o.body!==null&&typeof o.body=="object"?o.body:{}}t(r,"objectOf");function f(o){return i.some(n=>n in o)}t(f,"isLlamacppProps");function a(o){return typeof o.model_path=="string"||typeof o.tokenizer_path=="string"}t(a,"isSglangInfo");export{f as isLlamacppProps,a as isSglangInfo,r as objectOf};
|
|
2
|
+
//# sourceMappingURL=backends.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var m=Object.defineProperty;var n=(o,r)=>m(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,flush:e.flush,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),h=a==="anthropic"?"anthropic.messages":"openai.chat",d=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===h?null:d,"converters")}}n(b,"capture");export{b as capture,g as collect};
|
|
2
|
+
//# sourceMappingURL=capture.mjs.map
|
package/dist/lib/cli.mjs
ADDED
|
@@ -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, llamacpp, 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 o=Object.defineProperty;var s=(t,r)=>o(t,"name",{value:r,configurable:!0});import{TranslatorError as a}from"./errors.mjs";import*as n from"./pool.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 l(t,r){const e=typeof t=="string"?{url:t}:t;if(!e||typeof e!="object"||typeof e.url!="string"||e.url==="")throw new a(`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:{},health:n.health()}}s(l,"endpoint");function p(t={}){const r=t.front??"anthropic";if(r!=="anthropic"&&r!=="openai")throw new a(`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 a("at least one upstream is required (config.upstreams)",{code:"INVALID_CONFIG",status:500});return{front:r,endpoints:e.map(l),aliases:{...t.aliases},pool:n.normalize(t.pool),settings:{...t.settings},detect:t.detect!==!1,detectTimeout:t.detectTimeout??5e3,transport:t.transport,silent:t.silent===!0}}s(p,"normalize");export{l as endpoint,p 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 p=Object.defineProperty;var i=(n,s)=>p(n,"name",{value:s,configurable:!0});const m=4,u=i(n=>Math.ceil(n/4),"tokens");function x(n){return u(JSON.stringify([n.system,n.messages,n.tools]).length)}i(x,"estimate");const a=i((...n)=>{let s=0;for(const t of n)typeof t=="string"&&(s+=t.length);return s},"length");function _(n,s){if(n==="anthropic"){if(s?.type==="content_block_start")return a(s.content_block?.name);const o=s?.type==="content_block_delta"?s.delta:null;return a(o?.text,o?.thinking,o?.partial_json)}const t=s?.choices?.[0]?.delta;let e=a(t?.content,t?.reasoning,t?.reasoning_content);for(const o of t?.tool_calls??[])e+=a(o.function?.name,o.function?.arguments);return e}i(_,"eventLength");function y(n,s){let t=0;if(n==="anthropic"){for(const o of s?.content??[])t+=a(o.text,o.thinking,o.name),o.type==="tool_use"&&(t+=JSON.stringify(o.input).length);return t}const e=s?.choices?.[0]?.message;t=a(e?.content,e?.reasoning,e?.reasoning_content);for(const o of e?.tool_calls??[])t+=a(o.function?.name,o.function?.arguments);return t}i(y,"bodyLength");const f=i(n=>Math.max(1,u(n)),"output");function g(n,s,t){const e={...s};return n==="anthropic"?(t.input_tokens!==null&&(e.input_tokens=t.input_tokens),t.output_tokens!==null&&(e.output_tokens=t.output_tokens),e):(t.input_tokens!==null&&(e.prompt_tokens=t.input_tokens),t.output_tokens!==null&&(e.completion_tokens=t.output_tokens),e.total_tokens=(e.prompt_tokens??0)+(e.completion_tokens??0),e)}i(g,"patch");async function*d(n,s,t){let e=0;for await(const o of s){const l=o?.data;e+=_(n,l);const r=(n==="anthropic"?l?.type==="message_delta":Array.isArray(l?.choices)&&l.choices.length===0&&l.usage)?t.gaps():null;if(r&&(r.input||r.output)){const c={input_tokens:r.input?await t.input():null,output_tokens:r.output?f(e):null};t.report(c),yield{...o,data:{...l,usage:g(n,l.usage,c)}}}else yield o}}i(d,"stream");export{m as CHARS_PER_TOKEN,y as bodyLength,x as estimate,_ as eventLength,f as output,g as patch,d as stream,u as tokens};
|
|
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 d(t,e){return t+e}o(d,"join");function p(t){return t==="localhost"||t==="::1"||/^127\./.test(t)}o(p,"isLoopback");function x(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(x,"headers");function b({endpoint:t,transport:e,signal:n,timeout:r,probes:a},i){const f=o(()=>g(e,d(t.url,i),{method:"GET",headers:x(t),signal:n,timeout:r}),"run");return a?(a.has(i)||a.set(i,f()),a.get(i)):f()}o(b,"probe");function k(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(k,"plain");async function g(t,e,n={}){const{timeout:r,signal:a,body:i,...f}=n,s=[];a&&s.push(a),r>0&&s.push(AbortSignal.timeout(r));try{const u=await t(e,{...f,redirect:"error",body:i===void 0?void 0:JSON.stringify(i),signal:s.length>1?AbortSignal.any(s):s[0]}),c=await u.text();let l=null;try{l=JSON.parse(c)}catch{l=c===""?null:c}return{ok:u.ok,status:u.status,body:l}}catch(u){return{ok:!1,status:0,body:null,error:u}}}o(g,"json");export{x as headers,p as isLoopback,d as join,g as json,k as plain,b as probe};
|
|
2
|
+
//# sourceMappingURL=http.mjs.map
|
package/dist/lib/ids.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var f=Object.defineProperty;var n=(t,r)=>f(t,"name",{value:r,configurable:!0});import{randomBytes as p,createHash as h}from"node:crypto";function i(t=12){return p(t).toString("hex")}n(i,"random");function $(){return`msg_${i()}`}n($,"messageId");function a(){return`toolu_${i()}`}n(a,"toolUseId");function d(){return`chatcmpl-${i()}`}n(d,"completionId");function x(){return`call_${i()}`}n(x,"callId");function O(t){return typeof t!="string"||t===""?a():t.replace(/[^a-zA-Z0-9_-]/g,"_")}n(O,"anthropicToolId");function b({max:t,map:r={}}){const u=new Map(Object.entries(r).map(([e,c])=>[c,e]));return e=>{if(e.length<=t&&/^[a-zA-Z0-9_-]+$/.test(e))return e;if(u.has(e))return u.get(e);const c=e.replace(/[^a-zA-Z0-9_-]/g,"_");let o=c;if(c.length>t||c===""){const s=h("sha256").update(e).digest("hex").slice(0,8);o=`${c.slice(0,t-9)}_${s}`}const l=o;for(let s=2;Object.hasOwn(r,o);s++)o=`${l.slice(0,t-4)}_${s}`;return r[o]=e,u.set(e,o),o}}n(b,"createNameFitter");function I(t,r){return Object.hasOwn(t,r)?t[r]:r}n(I,"restoreName");export{O as anthropicToolId,x as callId,d as completionId,b as createNameFitter,$ as messageId,I as restoreName,a as toolUseId};
|
|
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 t=(r,o)=>f(r,"name",{value:o,configurable:!0});import*as e from"./http.mjs";function l(r){return!r.ok||!Array.isArray(r.body?.data)?null:r.body.data.filter(o=>o&&typeof o.id=="string")}t(l,"modelsOf");async function c({endpoint:r,transport:o,signal:a,timeout:d,auth:n,extraHeaders:s}){const i=await e.json(o,e.join(r.url,"/v1/models"),{method:"GET",headers:e.headers(r,n,s),signal:a,timeout:d});return l(i)}t(c,"fetchModels");export{c as fetchModels,l as modelsOf};
|
|
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 i=Object.defineProperty;var a=(t,o)=>i(t,"name",{value:o,configurable:!0});const p=["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"],m=["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(p),c=new Set(["include_usage","continuous_usage_stats"]);function d(t,o=[]){const s={};for(const[e,n]of Object.entries(t))if(!(!l.has(e)&&!o.includes(e)))if(e==="stream_options"){const _=Object.fromEntries(Object.entries(n??{}).filter(([r])=>c.has(r)));Object.keys(_).length>0&&(s.stream_options=_)}else s[e]=n;return s}a(d,"filterChatBody");export{m as VLLM_FIELDS,d as filterChatBody};
|
|
2
|
+
//# sourceMappingURL=passthrough.mjs.map
|
package/dist/lib/pdf.mjs
ADDED
|
@@ -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 p=Object.defineProperty;var c=(t,r)=>p(t,"name",{value:r,configurable:!0});import{TranslatorError as g}from"./errors.mjs";const b=Object.freeze(["failover","round_robin","least_busy"]),m=Object.freeze({base:1e3,max:6e4}),d=Object.freeze({connect:1e4,headers:0});function T(){return{failures:0,until:0,inflight:0,requests:0,errors:0,lastError:null,lastFailure:null,lastSuccess:null}}c(T,"health");function S(t={}){const r=c(u=>new g(`pool: ${u}`,{code:"INVALID_CONFIG",status:500}),"bad");if(!t||typeof t!="object"||Array.isArray(t))throw r("must be an object");const e=t.strategy??"failover",s={...t.models};for(const[u,l]of[["strategy",e],...Object.entries(s).map(([n,f])=>[`models.${n}`,f])])if(!b.includes(l))throw r(`${u} must be one of ${b.join(", ")} (got ${JSON.stringify(l)})`);const o=t.backoff?.base??m.base,a=t.backoff?.max??m.max;if(!(Number.isFinite(o)&&o>0)||!(Number.isFinite(a)&&a>=o))throw r("backoff.base must be a positive number of ms and backoff.max at least backoff.base");const h=t.connectTimeout??d.connect,i=t.headersTimeout??d.headers;for(const u of["connectTimeout","headersTimeout"]){const l=u==="connectTimeout"?h:i;if(typeof l!="number"||!(Number.isFinite(l)&&l>=0))throw r(`${u} must be a number of ms, 0 or more (got ${JSON.stringify(l)})`)}return{strategy:e,backoff:{base:o,max:a},connectTimeout:h,headersTimeout:i,models:s}}c(S,"normalize");function F(t,r,e,s,o,a=Date.now()){const h=t.pool.models[s]??t.pool.strategy,i=e.filter(n=>n.health.until<=a),u=e.filter(n=>n.health.until>a).sort((n,f)=>n.health.until-f.health.until);let l=i;if(h==="least_busy")l=[...i].sort((n,f)=>n.health.inflight-f.health.inflight);else if(h==="round_robin"&&i.length>0){const n=r.cursors.get(s)??0;o&&r.cursors.set(s,n+1);const f=n%i.length;l=[...i.slice(f),...i.slice(0,f)]}return[...l,...u]}c(F,"order");function O(t,r,e,s=Date.now()){const o=r.health;o.failures++,o.errors++,o.lastError=e,o.lastFailure=s;const{base:a,max:h}=t.pool.backoff;o.until=s+Math.min(h,a*2**Math.min(o.failures-1,30))}c(O,"failed");function E(t,r=Date.now()){const e=t.health;e.failures=0,e.until=0,e.lastSuccess=r}c(E,"succeeded");function j(t,r=Date.now()){const e=t.health,s=c(a=>a===null?null:new Date(a).toISOString(),"iso"),o=e.until>r;return{healthy:!o,failures:e.failures,retryAt:o?s(e.until):null,inflight:e.inflight,requests:e.requests,errors:e.errors,lastError:e.lastError,lastFailure:s(e.lastFailure),lastSuccess:s(e.lastSuccess)}}c(j,"describe");export{m as BACKOFF,b as STRATEGIES,d as TIMEOUTS,j as describe,O as failed,T as health,S as normalize,F as order,E as succeeded};
|
|
2
|
+
//# sourceMappingURL=pool.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var f=Object.defineProperty;var u=(e,t)=>f(e,"name",{value:t,configurable:!0});import{TranslatorError as c}from"./errors.mjs";import{order as m}from"./pool.mjs";function p(e,t){return(e.models??e.facts?.models??[]).includes(t)?!0:e.protocol==="anthropic.messages"&&e.models===void 0&&/^claude-/.test(t)}u(p,"serves");function v(e,t,o,n,a=!1){const d=Object.hasOwn(e.aliases,o)?e.aliases[o]:o,s=t.filter(l=>p(l,d)),r=t.find(l=>l.default)??t[0];if(s.length>0){const l=m(e,n,s,d,a);return{endpoint:l[0],model:d,unserved:!1,pool:l}}const i=r.models??r.facts?.models??[];return i.length===1?{endpoint:r,model:i[0],unserved:!1,pool:[r]}:{endpoint:r,model:d,unserved:!0,pool:[r]}}u(v,"pick");function k(e,t,o={}){if(!t||typeof t!="object"||Array.isArray(t))throw new c("The request body must be a JSON object",{code:"invalid_body",status:400});const n=v(e.config,e.state.endpoints,t.model,e.state,o.advance===!0);if(o.endpoint!==void 0){const s=n.pool.find(r=>r.name===o.endpoint);if(!s)throw new c(`Endpoint ${o.endpoint} no longer serves ${n.model}`,{code:"endpoint_not_in_pool",status:500});return{endpoint:s,model:n.model,requested:t.model,unserved:n.unserved,convert:e.converters(s),pool:[s]}}const{endpoint:a,pool:d}=n;return{endpoint:a,model:n.model,requested:t.model,unserved:n.unserved,convert:e.converters(a),pool:d}}u(k,"resolve");export{v as pick,k as resolve,p as serves};
|
|
2
|
+
//# sourceMappingURL=route.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var A=Object.defineProperty;var i=(e,n)=>A(e,"name",{value:n,configurable:!0});import{timingSafeEqual as H}from"node:crypto";import{TranslatorError as _}from"./errors.mjs";import{messageId as O}from"./ids.mjs";import{sign as S}from"./thinking.mjs";const T=64*1024*1024,j={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"},p=Symbol("stalled"),w=Symbol("gone");async function D(e,n){if(!(n>0))return e;let s;const c=new Promise(f=>{s=setTimeout(f,n,p)});try{return await Promise.race([e,c])}finally{clearTimeout(s)}}i(D,"settle");async function*I(e,n,s){let c=!1;try{const f=n.front==="anthropic";f&&(yield{event:"message_start",data:{type:"message_start",message:{id:O(),type:"message",role:"assistant",model:s,content:[],stop_reason:null,stop_sequence:null,usage:{input_tokens:0,output_tokens:0,cache_creation_input_tokens:0,cache_read_input_tokens:0}}}},yield{event:"content_block_start",data:{type:"content_block_start",index:0,content_block:{type:"thinking",thinking:"",signature:""}}});const a=await e,g=i(function*(){yield{event:"content_block_delta",data:{type:"content_block_delta",index:0,delta:{type:"signature_delta",signature:S()}}},yield{event:"content_block_stop",data:{type:"content_block_stop",index:0}}},"closeEarly");if(a.stream){c=!0;let u=null;for await(const l of a.stream){if(!f){yield l;continue}if(l.event!=="message_start"){if(u===null&&l.event==="content_block_start"&&l.data.content_block.type==="thinking"){u=0;continue}u===null&&(yield*g(),u=1),yield u>0&&typeof l.data?.index=="number"?{...l,data:{...l.data,index:l.data.index+u}}:l}}return}f&&(yield*g()),yield*x(n,a.body)}finally{c||e.then(f=>f.stream?.return?.(),()=>{})}}i(I,"late");function*x(e,n){if(e.front==="anthropic"){yield{event:"error",data:n};return}yield{data:n},yield{data:"[DONE]"}}i(x,"failure");async function v(e,n,s,c,f=200,a=j){e.hijack();const g=e.raw;g.writeHead(f,a);try{await n.write(g,c,{front:n.front,interval:n.interval})}catch(u){if(!s.signal.aborted){const l=u instanceof _;l||e.log.error(u);const{body:m}=n.build(l?u.status:502,l?u.message:"Internal server error",l?{type:u.type,code:u.code}:{});for(const k of x(n,m))g.write(n.encode(k))}}return g.end(),e}i(v,"stream");function L(e,n){const s=Buffer.from(e),c=Buffer.from(n);return s.length===c.length&&H(s,c)}i(L,"same");function z(e){const n=/^Bearer\s+(.+)$/i.exec(e.authorization??"");return e["x-api-key"]||n?.[1]||""}i(z,"keyOf");function B(e){const n={};for(const[s,c]of Object.entries(e))(s==="anthropic-beta"||s==="anthropic-version"||s.startsWith("x-claude-code-"))&&(n[s]=String(c));return n}i(B,"forwardable");function C(e){if(e===!0)return{ok:!0,context:void 0};if(e&&typeof e=="object"&&e.ok===!0)return{ok:!0,context:e.context};const n=e&&typeof e=="object"&&e.ok===!1?e:{};return{ok:!1,status:Number.isInteger(n.status)&&n.status>=400&&n.status<=599?n.status:401,message:typeof n.message=="string"&&n.message!==""?n.message:"unauthorized"}}i(C,"verdictOf");function R(e,n,s){const{front:c,filter:f}=s,a={...s,build:i((t,o,r={})=>s.build(t,o,{...r,front:c}),"build"),send:i((t,o)=>s.send(t,{...o,front:c,filter:f}),"send"),route:i(t=>s.route(t,{front:c}),"route"),count:i((t,o)=>s.count(t,{...o,front:c}),"count"),list:i(t=>s.list({...t,front:c}),"list"),get:i((t,o)=>s.get(t,{...o,front:c}),"get")},g=[n.apiKey??[]].flat().filter(Boolean),u=n.bodyLimit??T,l=new WeakMap,m=i((t,o)=>t.code(o.status).headers(o.headers).send(o.body),"reject");e.setErrorHandler((t,o,r)=>{if(t instanceof _)return m(r,a.build(t.status,t.message,{type:t.type,code:t.code,param:t.param}));const d=t.statusCode??500;return d<500?m(r,a.build(d,t.message)):(o.log.error(t),m(r,a.build(d,"Internal server error")))}),e.addHook("onRequest",async(t,o)=>{if(g.length===0||t.routeOptions.config?.public)return;const r=z(t.headers);if(!(r&&g.some(d=>L(r,d))))return m(o,a.build(401,"invalid x-api-key"))}),typeof n.authorize=="function"&&e.addHook("preHandler",async(t,o)=>{if(t.routeOptions.config?.public)return;let r=null;if(t.body&&typeof t.body=="object"&&!Array.isArray(t.body))try{r=a.route(t.body)}catch{}let d;try{d=C(await n.authorize({method:t.method,url:t.url,headers:t.headers,body:t.body,front:a.front,route:r}))}catch(y){t.log.error(y),d={ok:!1,status:401,message:"unauthorized"}}if(!d.ok)return m(o,a.build(d.status,d.message));l.set(t,d.context)}),e.route({method:["GET","HEAD"],url:"/",config:{public:!0},handler:i(async(t,o)=>o.send({ok:!0}),"handler")}),e.route({method:["GET","HEAD"],url:"/api/hello",config:{public:!0},handler:i(async(t,o)=>o.send({ok:!0}),"handler")}),e.get("/v1/models",async(t,o)=>{const r=new AbortController;return o.raw.on("close",()=>r.abort()),a.list({signal:r.signal})}),e.get("/v1/models/*",async(t,o)=>{const r=new AbortController;return o.raw.on("close",()=>r.abort()),a.get(t.params["*"],{signal:r.signal})});const k=a.front==="anthropic"?"/v1/messages":"/v1/chat/completions";e.post(k,{bodyLimit:u},async(t,o)=>{if(o.raw.destroyed)return o;const r=new AbortController;o.raw.on("close",()=>{o.raw.writableFinished||r.abort()});const d=typeof n.onUsage=="function"?h=>n.onUsage(h,l.get(t)):void 0,y=a.send(t.body,{signal:r.signal,clientHeaders:B(t.headers),onUsage:d});y.catch(()=>{});const E=new Promise(h=>r.signal.addEventListener("abort",()=>h(w),{once:!0})),b=await D(Promise.race([y,E]),t.body?.stream===!0?a.interval:0);return b===w?(y.then(h=>h.stream?.return?.(),()=>{}),o):b===p?v(o,a,r,I(y,a,t.body?.model)):b.stream?v(o,a,r,b.stream,b.status,b.headers):m(o,b)}),a.front==="anthropic"&&e.post("/v1/messages/count_tokens",{bodyLimit:u},async(t,o)=>{const r=new AbortController;return o.raw.on("close",()=>r.abort()),a.count(t.body,{signal:r.signal})})}i(R,"register");export{I as late,R as register};
|
|
2
|
+
//# sourceMappingURL=routes.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 r=(o,e)=>l(o,"name",{value:e,configurable:!0});import{TranslatorError as s}from"./errors.mjs";const i=Object.freeze({inlineSystem:"auto",stripBillingHeader:!0,reasoningField:"reasoning",thinkTags:!0,thinkOpen:!1,reasoningRoundTrip:!1,thinking:"chat_template_kwargs",thinkingBudget:!1,effort:!1,images:!0,imageFallback:!0,toolFallback:!1,documents:"extract",documentConverter:null,pdf:Object.freeze({pages:!0,maxPages:100,maxBytes:32*1024*1024,maxImages:20,scale:1.5}),toolSchema:"pass",deferredTools:"forward",toolSearch:"expand",toolErrors:!1,maxTokensField:"max_completion_tokens",streamUsage:!0,continuousUsage:!1,prefill:"continue_final_message",topK:!0,responseFormat:!0,parallelToolCalls:!0,unknownFields:"drop",pingInterval:1e4,toolStreaming:"buffer",effortPassthrough:!1,passthroughFilter:!1,contextWindow:0,defaultMaxTokens:8192}),f={inlineSystem:["auto","keep","hoist","user","drop"],reasoningField:["reasoning","reasoning_content"],thinking:["chat_template_kwargs","reasoning_effort","none"],documents:["extract","note","error"],toolSchema:["pass","sanitize"],deferredTools:["forward","drop"],toolSearch:["expand","inline","native"],maxTokensField:["max_completion_tokens","max_tokens"],prefill:["continue_final_message","none"],unknownFields:["drop","error"],toolStreaming:["buffer","stream"]},d=["effortPassthrough","passthroughFilter","imageFallback","toolFallback","thinkOpen","reasoningRoundTrip"];function p(...o){const e={...i,pdf:{...i.pdf}};for(const t of o)if(t)for(const[n,a]of Object.entries(t))a!==void 0&&(n==="pdf"?Object.assign(e.pdf,a):e[n]=a);for(const[t,n]of Object.entries(f))if(!n.includes(e[t]))throw new s(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected one of ${n.join(", ")})`,{code:"invalid_setting",status:500,details:{key:t,allowed:n}});for(const t of d)if(typeof e[t]!="boolean")throw new s(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected true or false)`,{code:"invalid_setting",status:500,details:{key:t}});if(!(Number.isFinite(e.pdf.scale)&&e.pdf.scale>0))throw new s(`Invalid setting pdf.scale: ${JSON.stringify(e.pdf.scale)} (expected a positive number)`,{code:"invalid_setting",status:500,details:{key:"pdf.scale"}});return e}r(p,"resolve");export{i as DEFAULTS,p as resolve};
|
|
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
|
package/dist/lib/sse.mjs
ADDED
|
@@ -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,3 @@
|
|
|
1
|
+
var M=Object.defineProperty;var o=(t,n)=>M(t,"name",{value:n,configurable:!0});import{deflateRawSync as b,inflateRaw as R}from"node:zlib";import{promisify as X}from"node:util";const j=X(R),p="llmt:",g="<think>",l="</think>",d="~",w=/^[A-Za-z0-9_-]*$/,I=/^[A-Za-z][A-Za-z0-9_]{0,39}$/,N=512,S=256*1024,h=1024*1024,_=512*1024,P=256,U=32,E=5,B=8*1024*1024,W=1024,F=8,O=30,x="anthropic-claude-v1",K=["reasoning_content","reasoning"];function A(){return p+Buffer.from('{"v":1}').toString("base64url")}o(A,"marker");function T(t){if(t===null||typeof t!="object"||Array.isArray(t))return null;try{const n={};for(const e of K)if(t[e]!==void 0){if(typeof t[e]!="string"||t[e].length>_)return null;n[e]=t[e]}if(Object.keys(n).length>1)return null;if(t.reasoning_details!==void 0){const e=m(t.reasoning_details,0);if(!Array.isArray(e)||e.some(r=>r===null||typeof r!="object"||Array.isArray(r)))return null;n.reasoning_details=e}return Object.keys(n).length>0?n:null}catch{return null}}o(T,"validate");function Y(t,n){const e=t===void 0?null:T(t);if(e===null)return A();const r=Buffer.from(JSON.stringify(n===void 0?{v:1,d:e}:{v:1,p:n,d:e}));if(r.length>h)return A();let i=r.toString("base64url");if(r.length>N){const u=b(r);r.length<=O*u.length&&u.length<r.length&&(i=d+u.toString("base64url"))}const f=p+i;return f.length>S?A():f}o(Y,"sign");function L(t){return typeof t=="string"&&t.startsWith(p)}o(L,"isOurs");function m(t,n){if(t===null||typeof t=="boolean"||typeof t=="number"&&Number.isFinite(t)||typeof t=="string"&&t.length<=_)return t;if(n<E&&Array.isArray(t)&&t.length<=P)return t.map(e=>m(e,n+1));if(n<E&&typeof t=="object"&&!Array.isArray(t)&&Object.keys(t).length<=U){const e={};for(const[r,i]of Object.entries(t)){if(!I.test(r))throw new TypeError("key");e[r]=m(i,n+1)}return e}throw new TypeError("value")}o(m,"plain");function z(){return{bytes:B,signatures:W,failures:F,cache:new Map,queue:Promise.resolve()}}o(z,"createBudget");async function Z(t,{producer:n,budget:e=z()}={}){if(!L(t)||t.length>S)return null;const r=e.queue.then(()=>C(t,n,e));return e.queue=r.then(()=>{},()=>{}),r}o(Z,"unsign");async function C(t,n,e){const r=`${n}
|
|
2
|
+
${t}`;if(e.cache.has(r))return e.cache.get(r);if(e.signatures<=0||e.failures<=0)return null;e.signatures--;const i=await D(t,n,e);return i===void 0?(e.failures--,e.cache.set(r,null),null):(e.cache.set(r,i),i)}o(C,"account");async function D(t,n,e){try{let r=t.slice(p.length);const i=r.startsWith(d);if(i&&(r=r.slice(d.length)),!w.test(r))return;const f=Buffer.from(r,"base64url"),u=Math.min(h,e.bytes),a=i?Math.min(u,O*f.length):f.length;if(a<1||a>u)return;e.bytes-=a;const y=i?await j(f,{maxOutputLength:a}):f;e.bytes+=a-y.length;const s=JSON.parse(y.toString("utf8"));return s?.v!==1||s.d===void 0?void 0:s.p!==n?null:T(s.d)??void 0}catch{return}}o(D,"decode");function G(t,n){return t?.type==="thinking"&&typeof t.signature=="string"&&t.signature!==""?{type:"reasoning.text",text:typeof t.thinking=="string"?t.thinking:"",signature:t.signature,format:x,index:n}:t?.type==="redacted_thinking"&&typeof t.data=="string"&&t.data!==""?{type:"reasoning.encrypted",data:t.data,format:x,index:n}:null}o(G,"detailOf");function H(t){const n=o(i=>Number.isInteger(i.index)&&i.index>=0?i.index:0,"position");if(!Array.isArray(t))return[];const e=[];let r=0;for(const i of t.slice(0,64))i===null||typeof i!="object"||i.format!==x||(i.type==="reasoning.text"&&typeof i.text=="string"&&typeof i.signature=="string"&&i.signature!==""?(r+=i.text.length+i.signature.length,r<=h&&e.push({at:n(i),block:{type:"thinking",thinking:i.text,signature:i.signature}})):i.type==="reasoning.encrypted"&&typeof i.data=="string"&&i.data!==""&&(r+=i.data.length,r<=h&&e.push({at:n(i),block:{type:"redacted_thinking",data:i.data}})));return e}o(H,"blocksOf");function V(t){return typeof t?.reasoning=="string"&&t.reasoning!==""?"reasoning":typeof t?.reasoning_content=="string"&&t.reasoning_content!==""?"reasoning_content":null}o(V,"reasoningKey");function v(t,n){if(n===null||typeof n!="object"||Array.isArray(n))return t;const e=n.index===void 0?void 0:t.find(r=>r.index===n.index&&r.type===n.type);if(e===void 0)return t.push({...n}),t;for(const r of["text","summary"])typeof n[r]=="string"&&(e[r]=(typeof e[r]=="string"?e[r]:"")+n[r]);for(const r of["signature","data","format","id"])typeof n[r]=="string"&&n[r]!==""&&(e[r]=n[r]);return t}o(v,"mergeDetail");function tt(t){return Array.isArray(t)?t.map(n=>typeof n?.text=="string"?n.text:typeof n?.summary=="string"?n.summary:"").join(""):""}o(tt,"detailsText");function nt(t){const n=t?.reasoning??t?.reasoning_content;return typeof n=="string"?n:""}o(nt,"reasoningOf");function et(t,n=!1){const e=t.trimStart();if(!e.startsWith(g)){const i=n?t.indexOf(l):-1;return i===-1?{reasoning:"",text:t}:{reasoning:t.slice(0,i),text:t.slice(i+l.length).trimStart()}}const r=e.indexOf(l);return r===-1?{reasoning:e.slice(g.length),text:""}:{reasoning:e.slice(g.length,r),text:e.slice(r+l.length).trimStart()}}o(et,"splitThink");function rt(t=!1){let n="start",e="";const r=o((f,u)=>{let a="",y="",s=e+f;for(e="";;){if(n==="plain"){y+=s;break}if(n==="start"){const c=s.trimStart();if(c===""&&!u){e=s;break}if(c.startsWith(g))n="think",s=c.slice(g.length);else if(!u&&c!==""&&g.startsWith(c)){e=s;break}else n=t?"implicit":"plain"}else if(n==="implicit"){const c=s.indexOf(l);if(c!==-1)a+=s.slice(0,c),s=s.slice(c+l.length),n="after";else if(u){y+=s;break}else{e=s;break}}else if(n==="think"){const c=s.indexOf(l);if(c!==-1){a+=s.slice(0,c),s=s.slice(c+l.length),n="after";continue}const k=u?0:$(s,l);a+=s.slice(0,s.length-k),e=s.slice(s.length-k);break}else{if(s=s.trimStart(),s==="")break;n="plain"}}return{reasoning:a,text:y}},"step");return{push:o(f=>r(f,!1),"push"),end:o(()=>r("",!0),"end"),release:o(()=>{const f=e;return e="",n="plain",{reasoning:"",text:f}},"release"),pending:o(()=>t&&(n==="start"||n==="implicit"),"pending")}}o(rt,"createThinkSplitter");function $(t,n){for(let e=Math.min(t.length,n.length-1);e>0;e--)if(n.startsWith(t.slice(t.length-e)))return e;return 0}o($,"partialSuffix");export{x as ANTHROPIC_FORMAT,H as blocksOf,z as createBudget,rt as createThinkSplitter,G as detailOf,tt as detailsText,L as isOurs,v as mergeDetail,V as reasoningKey,nt as reasoningOf,Y as sign,et as splitThink,Z as unsign};
|
|
3
|
+
//# sourceMappingURL=thinking.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var l=Object.defineProperty;var e=(t,o)=>l(t,"name",{value:o,configurable:!0});const s="The vLLM server was started without tool-call parsing. Restart it with --enable-auto-tool-choice and --tool-call-parser <parser> (the parser depends on the model family)",p=`${s}, or set the translator setting toolFallback to retry without tools.`,n="The llama.cpp server was started without --jinja, which tool calls need. Restart it with --jinja (the model's chat template must support tools)";function u(t,o=""){const r=/--jinja\b/i.test(o)?n:s;return t?`${r}.`:`${r}, or set the translator setting toolFallback to retry without tools.`}e(u,"hint");const i=[/--enable-auto-tool-choice\b/i,/--tool-call-parser\b/i,/--jinja\b/i];function d(t){return i.some(o=>o.test(t))}e(d,"refused");function w(t){return Array.isArray(t?.tools)&&t.tools.length>0}e(w,"carries");function j(t){const{tools:o,tool_choice:r,parallel_tool_calls:c,...a}=t;return a}e(j,"strip");export{p as HINT,w as carries,u as hint,d as refused,j as strip};
|
|
2
|
+
//# sourceMappingURL=tools.mjs.map
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
var T=Object.defineProperty;var f=(n,t)=>T(n,"name",{value:t,configurable:!0});const A="ToolSearch",w="DeferredToolPlaceholder",S=/<function>([\s\S]*?)<\/function>/g,E=/\b(?:clear(?:ed)?|trimmed|removed|truncated|omitted|elided|pruned)\b/i,b=Object.freeze({definitions:512,line:131072,name:256,description:16384,schema:65536});function x(n){return typeof n=="string"?n:Array.isArray(n)?n.filter(t=>t?.type==="text"&&typeof t.text=="string").map(t=>t.text).join(`
|
|
2
|
+
`):""}f(x,"textOf");function L(n){if(n.length>b.line)return null;let t;try{t=JSON.parse(n)}catch{return null}if(!t||typeof t.name!="string"||t.name===""||t.name.length>b.name)return null;const r=t.parameters??{type:"object",properties:{}};if(typeof r!="object"||r===null||Array.isArray(r)||JSON.stringify(r).length>b.schema||r.type!==void 0&&r.type!=="object")return null;const l={name:t.name,input_schema:r.type===void 0?{type:"object",...r}:r};if(typeof t.description=="string"){if(t.description.length>b.description)return null;l.description=t.description}return l}f(L,"definition");function M(n){if(Array.isArray(n)&&n.some(r=>r?.type==="tool_reference"))return!1;const t=x(n).trim();return new RegExp(S.source).test(t)?!1:t===""||t.length<=200&&E.test(t)}f(M,"cleared");function j(n){return typeof n?.query!="string"||!/^\s*select:/i.test(n.query)?[]:n.query.replace(/^\s*select:/i,"").split(/[\s,]+/).filter(t=>t!=="")}f(j,"selected");function R(n,t=new Set){const r=[],l=[],d=[],u=new Map,h=new Map,a=new Map,p=new Map,y=new Set;let g=!1;const _=f(i=>{r.includes(i)||r.push(i)},"add"),m=f(i=>{_(i),l.includes(i)||l.push(i)},"search");for(const i of n)if(Array.isArray(i?.content))for(const e of i.content)e?.type==="tool_use"?a.set(e.id,(a.get(e.id)??0)+1):e?.type==="tool_result"&&!p.has(e.tool_use_id)&&p.set(e.tool_use_id,e);for(const i of n)if(Array.isArray(i?.content)){for(const e of i.content)if(e?.type==="tool_use"){if(a.get(e.id)===1&&h.set(e.id,e.name),_(e.name),d.includes(e.name)||d.push(e.name),p.get(e.id)?.is_error!==!0&&y.add(e.name),e.name===A){const s=a.get(e.id)===1?p.get(e.id):void 0;if(!s||M(s.content))for(const c of j(e.input))m(c)}}else if(e?.type==="tool_result"){for(const s of Array.isArray(e.content)?e.content:[])s?.type==="tool_reference"&&typeof s.tool_name=="string"&&m(s.tool_name);if(h.get(e.tool_use_id)!==A)continue;for(const s of x(e.content).matchAll(S)){const c=L(s[1]);c&&(m(c.name),!(t.has(c.name)||u.has(c.name))&&(u.size>=b.definitions?g=!0:u.set(c.name,c)))}}}return{loaded:r,searched:l,called:d,failed:d.filter(i=>!y.has(i)),defined:u,capped:g}}f(R,"scan");const I="The definition of this tool was trimmed from the conversation; use ToolSearch to see its parameters.";function v(n,t,r,l){const d=f(e=>(e.type===void 0||e.type==="custom")&&e.defer_loading===!0,"deferred");if(!n.some(d))return{offer:n,loaded:[],hidden:[],stubbed:[]};const u=n.some(e=>e.name===w&&d(e)),h=n.filter(e=>d(e)&&e.name!==w),a=new Set([w]);let p=n,y=[],g=[];const _=new Set(n.map(e=>e.name)),m=t.searched.filter(e=>!_.has(e)&&!t.defined.has(e)),i=u||n.some(e=>e.name===A);if(r.deferredTools==="drop")a.add(A);else if(r.toolSearch==="native")a.clear();else if(i&&(g=t.called.filter(e=>m.includes(e)&&!t.failed.includes(e))),r.toolSearch==="inline"||!i){!u&&m.length===0&&a.add(A);const e=[...t.defined.values()].filter(s=>!_.has(s.name));p=[...n,...e],y=[...h,...e].map(s=>s.name)}else{const e=new Set(t.loaded);l?.type==="tool"&&e.add(l.name);const s=[...e].map(o=>h.find(O=>O.name===o)).filter(o=>o!==void 0),c=[...e].filter(o=>!_.has(o)&&t.defined.has(o)).map(o=>t.defined.get(o));!u&&s.length===h.length&&m.length===0&&a.add(A),p=[...n.filter(o=>!d(o)),...s,...c],y=[...s,...c].map(o=>o.name)}return{offer:[...p.filter(e=>!a.has(e.name)),...g.map(e=>({name:e,description:I,input_schema:{type:"object"}}))],loaded:y,hidden:n.filter(e=>a.has(e.name)).map(e=>e.name),stubbed:g}}f(v,"plan");export{b as LIMITS,w as PLACEHOLDER_TOOL,A as SEARCH_TOOL,I as TRIMMED,v as plan,R as scan};
|
|
3
|
+
//# sourceMappingURL=toolsearch.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var E=Object.defineProperty;var f=(a,t)=>E(a,"name",{value:t,configurable:!0});import v from"node:http";import C from"node:https";import{Readable as L}from"node:stream";const i=new Set([204,205,304]);function S(a,t={},p={http:v,https:C}){const c=new URL(a),h={"accept-encoding":"identity",...t.headers};t.body!==void 0&&(h["content-length"]=String(Buffer.byteLength(t.body)));const{signal:o,redirect:b,connectTimeout:d}=t;return new Promise((y,u)=>{if(o?.aborted)return u(o.reason);const r=(c.protocol==="https:"?p.https:p.http).request(c,{method:t.method??"GET",headers:h,agent:!1},e=>{if(o?.removeEventListener("abort",l),b==="error"&&e.statusCode>=300&&e.statusCode<400&&e.headers.location)return e.destroy(),u(new TypeError(`unexpected redirect to ${e.headers.location}`));const s=new Headers;for(const[m,w]of Object.entries(e.headers))for(const T of[].concat(w))s.append(m,T);const n=i.has(e.statusCode)?null:L.toWeb(e);n===null&&e.resume(),y(new Response(n,{status:e.statusCode,statusText:e.statusMessage,headers:s})),o?.addEventListener("abort",()=>e.destroy(o.reason),{once:!0})}),l=f(()=>r.destroy(o.reason),"onAbort");o?.addEventListener("abort",l,{once:!0}),r.on("error",u),d>0&&r.once("socket",e=>{const s=c.protocol==="https:";if(!s&&!e.connecting)return;const n=setTimeout(()=>r.destroy(new Error(`connect timeout after ${d} ms`)),d);e.once(s?"secureConnect":"connect",()=>clearTimeout(n)),r.once("close",()=>clearTimeout(n))}),r.end(t.body)})}f(S,"transport");export{S 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 v=Object.defineProperty;var f=(t,o)=>v(t,"name",{value:o,configurable:!0});const m=["input_tokens","output_tokens","cache_read_input_tokens","cache_creation_input_tokens"],l=f(t=>Number.isFinite(t)?t:null,"count");function E(t,o){if(!o||typeof o!="object")return null;let i;if(t==="openai"){const r=l(o.prompt_tokens),a=l(o.prompt_tokens_details?.cached_tokens);i={input_tokens:r===null?null:Math.max(0,r-(a??0)),output_tokens:l(o.completion_tokens),cache_read_input_tokens:a,cache_creation_input_tokens:null}}else if(t==="folded"){const r=l(o.input_tokens);i={input_tokens:r===null?null:r+(l(o.cache_creation_input_tokens)??0),output_tokens:l(o.output_tokens),cache_read_input_tokens:l(o.cache_read_input_tokens)||null,cache_creation_input_tokens:null}}else i=Object.fromEntries(m.map(r=>[r,l(o[r])]));return m.every(r=>!i[r])?null:i}f(E,"normalize");function I(t){return!!t&&typeof t=="object"&&Array.isArray(t.choices)&&t.choices.length===0&&!!t.usage}f(I,"usageChunk");function P(t,o,i){const r=Object.fromEntries(m.map(e=>[e,null]));let a=!1,y=null,d=!1,b=!1,h=!1,p=null,w="openai",j;const g=new Promise(e=>{j=e}),k=f((e,u=t.front)=>{const c=E(u,e);if(c){a=!0;for(const n of m)c[n]!==null&&c[n]!==void 0&&(r[n]=c[n])}},"take"),_=f(e=>{if(d)return;d=!0,p&&k(p(),w);const u=e==="done"&&b?"error":e,c={...r,reported:a,estimated:y,complete:u==="done",reason:u,...t};j(c);try{Promise.resolve(o?.(c)).catch(n=>i(`onUsage rejected: ${n?.message??n}`))}catch(n){i(`onUsage threw: ${n?.message??n}`)}},"finish");return{meta:t,usage:g,json(e){h=!0,p||k(e?.usage)},source(e,u="openai"){p=e,w=u},event(e){e==="[DONE]"&&(h=!0),!(!e||typeof e!="object")&&((e.type==="error"||t.front==="openai"&&e.error)&&(b=!0),t.front==="anthropic"?(e.type==="message_stop"&&(h=!0),p||k(e.type==="message_start"?e.message?.usage:e.usage)):p||k(e.usage))},finish:_,estimate(e){y=e},track(e,u,c=!1){const n=e[Symbol.asyncIterator](),x=this;return{[Symbol.asyncIterator](){return this},async next(){try{for(;;){const s=await n.next();if(s.done)_(h?"done":"error");else if(x.event(s.value?.data),c&&I(s.value?.data))continue;return s}}catch(s){throw _(u?.aborted?"aborted":"error"),s}},async return(s){return _("aborted"),n.return?n.return(s):{done:!0,value:s}},async throw(s){if(_("error"),n.throw)return n.throw(s);throw s}}}}}f(P,"meter");export{P as meter,E as normalize};
|
|
2
|
+
//# sourceMappingURL=usage.mjs.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cldmv/llm-translator",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.3.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
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"author": {
|
|
@@ -25,11 +25,21 @@
|
|
|
25
25
|
},
|
|
26
26
|
"sideEffects": false,
|
|
27
27
|
"engines": {
|
|
28
|
-
"node": ">=
|
|
28
|
+
"node": ">=24.0.0"
|
|
29
|
+
},
|
|
30
|
+
"devEngines": {
|
|
31
|
+
"runtime": {
|
|
32
|
+
"name": "node",
|
|
33
|
+
"version": ">=24.0.0",
|
|
34
|
+
"onFail": "error"
|
|
35
|
+
}
|
|
29
36
|
},
|
|
30
37
|
"main": "./dist/index.cjs",
|
|
31
38
|
"module": "./dist/index.mjs",
|
|
32
39
|
"types": "./types/src/index.d.mts",
|
|
40
|
+
"bin": {
|
|
41
|
+
"llm-translator": "./dist/bin/llm-translator.mjs"
|
|
42
|
+
},
|
|
33
43
|
"exports": {
|
|
34
44
|
".": {
|
|
35
45
|
"llm-translator-dev": {
|
|
@@ -46,8 +56,7 @@
|
|
|
46
56
|
"files": [
|
|
47
57
|
"dist/",
|
|
48
58
|
"!dist/**/*.map",
|
|
49
|
-
"types/"
|
|
50
|
-
"!types/devcheck.d.mts"
|
|
59
|
+
"types/"
|
|
51
60
|
],
|
|
52
61
|
"repository": {
|
|
53
62
|
"type": "git",
|
|
@@ -59,8 +68,15 @@
|
|
|
59
68
|
"homepage": "https://github.com/CLDMV/llm-translator#readme",
|
|
60
69
|
"keywords": [
|
|
61
70
|
"llm",
|
|
62
|
-
"
|
|
63
|
-
"
|
|
71
|
+
"anthropic",
|
|
72
|
+
"openai",
|
|
73
|
+
"claude-code",
|
|
74
|
+
"vllm",
|
|
75
|
+
"chat-completions",
|
|
76
|
+
"messages-api",
|
|
77
|
+
"translator",
|
|
78
|
+
"proxy",
|
|
79
|
+
"slothlet"
|
|
64
80
|
],
|
|
65
81
|
"scripts": {
|
|
66
82
|
"build": "tsup",
|
|
@@ -72,7 +88,7 @@
|
|
|
72
88
|
"test:watch": "vitest --config .configs/vitest.config.mjs",
|
|
73
89
|
"coverage": "node tests/run-vitest.mjs --coverage-quiet",
|
|
74
90
|
"ci:coverage": "npm run coverage && npm run test:dist",
|
|
75
|
-
"types:build": "tsc -p .configs/tsconfig.json",
|
|
91
|
+
"types:build": "tsc -p .configs/tsconfig.json --noCheck",
|
|
76
92
|
"types:check": "tsc -p .configs/tsconfig.json --noEmit",
|
|
77
93
|
"lint": "eslint --config .configs/eslint.config.mjs .",
|
|
78
94
|
"lint:fix": "eslint --config .configs/eslint.config.mjs . --fix",
|
|
@@ -80,12 +96,26 @@
|
|
|
80
96
|
"format:check": "prettier --config .configs/.prettierrc --check .",
|
|
81
97
|
"fix:headers": "fix-headers --config .configs/fix-headers.json"
|
|
82
98
|
},
|
|
99
|
+
"peerDependencies": {
|
|
100
|
+
"fastify": "^5.12.7"
|
|
101
|
+
},
|
|
102
|
+
"peerDependenciesMeta": {
|
|
103
|
+
"fastify": {
|
|
104
|
+
"optional": true
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
"dependencies": {
|
|
108
|
+
"@cldmv/slothlet": "^3.22.1",
|
|
109
|
+
"@napi-rs/canvas": "^1.0.13",
|
|
110
|
+
"unpdf": "^2.1.0"
|
|
111
|
+
},
|
|
83
112
|
"devDependencies": {
|
|
84
113
|
"@cldmv/configs": "^1.3.0",
|
|
85
114
|
"@cldmv/eslint-plugin-jsonv": "^1.0.17",
|
|
86
115
|
"@cldmv/fix-headers": "^2.2.2",
|
|
87
116
|
"@cldmv/jsonv": "^1.1.5",
|
|
88
117
|
"@cldmv/prettier-plugin-jsonv": "^1.1.4",
|
|
118
|
+
"@cldmv/slothlet-types": "^3.22.1",
|
|
89
119
|
"@cldmv/vitest-runner": "^1.5.4",
|
|
90
120
|
"@eslint/css": "^2.0.1",
|
|
91
121
|
"@eslint/js": "^10.0.1",
|
|
@@ -94,10 +124,11 @@
|
|
|
94
124
|
"@types/node": "^26.6.5",
|
|
95
125
|
"@vitest/coverage-v8": "^5.0.5",
|
|
96
126
|
"eslint": "^10.13.0",
|
|
127
|
+
"fastify": "^5.12.7",
|
|
97
128
|
"globals": "^17.13.0",
|
|
98
129
|
"prettier": "^3.9.10",
|
|
99
130
|
"tsup": "^9.0.0",
|
|
100
|
-
"typescript": "^
|
|
131
|
+
"typescript": "^6.0.3",
|
|
101
132
|
"vitest": "^5.0.5"
|
|
102
133
|
}
|
|
103
134
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert an Anthropic Messages request body into an OpenAI Chat Completions request body.
|
|
3
|
+
*
|
|
4
|
+
* Returns the converted body plus a JSON-serializable `state` the response/stream converters need (original model,
|
|
5
|
+
* tool-name map, thinking mode, fields that were dropped).
|
|
6
|
+
* @param {any} body - Anthropic `POST /v1/messages` body.
|
|
7
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
8
|
+
* @param {{ producer?: string, budget?: ReturnType<typeof createBudget> }} [context] - `producer` is the `driver:protocol:host` the request goes to; signatures that carry provider reasoning data are restored only for the producer that made them. `budget` limits how much signature decoding the request may cost (one is made when absent; `send` passes one for all its attempts).
|
|
9
|
+
* @returns {Promise<{ body: any, state: any }>} OpenAI request body and conversion state.
|
|
10
|
+
* @throws {TranslatorError} For malformed requests, unsupported content when configured to error, or PDF limit violations.
|
|
11
|
+
*/
|
|
12
|
+
export function request(body: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>, context?: {
|
|
13
|
+
producer?: string;
|
|
14
|
+
budget?: ReturnType<typeof createBudget>;
|
|
15
|
+
}): Promise<{
|
|
16
|
+
body: any;
|
|
17
|
+
state: any;
|
|
18
|
+
}>;
|
|
19
|
+
import { createBudget } from "../../../../../../../lib/thinking.mjs";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert an Anthropic message into an OpenAI `chat.completion`. Text blocks are concatenated, `thinking` blocks
|
|
3
|
+
* become the reasoning field named by `reasoningField`, `tool_use` blocks become `tool_calls` (tool names restored
|
|
4
|
+
* through `state.toolNames`), and other block types (server tool results) are skipped. With `reasoningRoundTrip`, thinking and redacted thinking
|
|
5
|
+
* blocks are also returned as `reasoning_details` items that carry their signatures.
|
|
6
|
+
* @param {any} message - Anthropic message.
|
|
7
|
+
* @param {any} [state] - State from the request converter (original model, tool-name map).
|
|
8
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
9
|
+
* @returns {any} OpenAI chat completion.
|
|
10
|
+
*/
|
|
11
|
+
export function response(message: any, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): any;
|