@cldmv/llm-translator 0.11.0 → 0.13.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 +8 -9
- package/dist/api/convert/anthropic/messages/to/openai/responses/request.mjs +2 -0
- package/dist/api/convert/openai/chat/to/openai/responses/request.mjs +2 -0
- package/dist/api/convert/openai/chat/to/openai/responses/response.mjs +2 -0
- package/dist/api/convert/openai/chat/to/openai/responses/stream.mjs +2 -0
- package/dist/api/convert/openai/responses/to/anthropic/messages/response.mjs +2 -0
- package/dist/api/convert/openai/responses/to/anthropic/messages/stream.mjs +2 -0
- package/dist/api/convert/openai/responses/to/openai/chat/request.mjs +6 -0
- package/dist/api/convert/openai/responses/to/openai/chat/response.mjs +2 -0
- package/dist/api/convert/openai/responses/to/openai/chat/stream.mjs +2 -0
- package/dist/api/detect.mjs +1 -1
- package/dist/api/driver/anthropic/info.mjs +1 -1
- package/dist/api/driver/azure/info.mjs +1 -1
- package/dist/api/driver/compatible/info.mjs +1 -1
- package/dist/api/driver/deepseek/info.mjs +1 -1
- package/dist/api/driver/llamacpp/info.mjs +1 -1
- package/dist/api/driver/lmstudio/info.mjs +1 -1
- package/dist/api/driver/localai/info.mjs +1 -1
- package/dist/api/driver/mistral/info.mjs +1 -1
- package/dist/api/driver/ollama/info.mjs +1 -1
- package/dist/api/driver/ollama/request.mjs +1 -1
- package/dist/api/driver/openai/info.mjs +1 -1
- package/dist/api/driver/openrouter/info.mjs +1 -1
- package/dist/api/driver/sglang/info.mjs +1 -1
- package/dist/api/driver/tgi/info.mjs +1 -1
- package/dist/api/driver/vllm/info.mjs +1 -1
- package/dist/api/request.mjs +2 -2
- package/dist/api/response.mjs +1 -1
- package/dist/api/route.mjs +1 -1
- package/dist/api/send.mjs +1 -1
- package/dist/api/server/plugin.mjs +1 -1
- package/dist/api/server/start.mjs +1 -1
- package/dist/api/status.mjs +1 -1
- package/dist/api/stream.mjs +1 -1
- package/dist/api/token/count.mjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/lib/capture.mjs +1 -1
- package/dist/lib/cli.mjs +16 -7
- package/dist/lib/config.mjs +1 -1
- package/dist/lib/estimate.mjs +1 -1
- package/dist/lib/guard.mjs +2 -0
- package/dist/lib/http.mjs +1 -1
- package/dist/lib/metrics.mjs +4 -0
- package/dist/lib/presets.mjs +1 -1
- package/dist/lib/protocol.mjs +2 -0
- package/dist/lib/responses-upstream.mjs +13 -0
- package/dist/lib/responses.mjs +2 -0
- package/dist/lib/route.mjs +1 -1
- package/dist/lib/routes.mjs +1 -1
- package/dist/lib/settings.mjs +1 -1
- package/dist/lib/vllm.mjs +2 -2
- package/package.json +1 -1
- package/types/src/api/convert/anthropic/messages/to/openai/responses/request.d.mts +16 -0
- package/types/src/api/convert/openai/chat/to/openai/responses/request.d.mts +12 -0
- package/types/src/api/convert/openai/chat/to/openai/responses/response.d.mts +14 -0
- package/types/src/api/convert/openai/chat/to/openai/responses/stream.d.mts +18 -0
- package/types/src/api/convert/openai/responses/to/anthropic/messages/response.d.mts +8 -0
- package/types/src/api/convert/openai/responses/to/anthropic/messages/stream.d.mts +8 -0
- package/types/src/api/convert/openai/responses/to/openai/chat/request.d.mts +17 -0
- package/types/src/api/convert/openai/responses/to/openai/chat/response.d.mts +8 -0
- package/types/src/api/convert/openai/responses/to/openai/chat/stream.d.mts +10 -0
- package/types/src/api/driver/anthropic/info.d.mts +2 -1
- package/types/src/api/driver/azure/info.d.mts +2 -1
- package/types/src/api/driver/compatible/info.d.mts +2 -1
- package/types/src/api/driver/deepseek/info.d.mts +2 -1
- package/types/src/api/driver/fireworks/info.d.mts +2 -1
- package/types/src/api/driver/gemini/info.d.mts +2 -1
- package/types/src/api/driver/groq/info.d.mts +2 -1
- package/types/src/api/driver/llamacpp/info.d.mts +2 -1
- package/types/src/api/driver/lmstudio/info.d.mts +2 -1
- package/types/src/api/driver/localai/info.d.mts +2 -1
- package/types/src/api/driver/mistral/info.d.mts +2 -1
- package/types/src/api/driver/ollama/info.d.mts +2 -1
- package/types/src/api/driver/ollama/request.d.mts +1 -1
- package/types/src/api/driver/openai/info.d.mts +2 -1
- package/types/src/api/driver/openrouter/info.d.mts +2 -1
- package/types/src/api/driver/sglang/info.d.mts +2 -1
- package/types/src/api/driver/tgi/info.d.mts +2 -1
- package/types/src/api/driver/together/info.d.mts +2 -1
- package/types/src/api/driver/vllm/info.d.mts +2 -1
- package/types/src/api/driver/xai/info.d.mts +2 -1
- package/types/src/api/request.d.mts +3 -2
- package/types/src/api/route.d.mts +2 -2
- package/types/src/api/send.d.mts +1 -1
- package/types/src/api/server/plugin.d.mts +2 -2
- package/types/src/api/server/start.d.mts +1 -1
- package/types/src/api/status.d.mts +1 -1
- package/types/src/lib/config.d.mts +4 -3
- package/types/src/lib/estimate.d.mts +1 -1
- package/types/src/lib/guard.d.mts +25 -0
- package/types/src/lib/http.d.mts +4 -3
- package/types/src/lib/metrics.d.mts +58 -0
- package/types/src/lib/presets.d.mts +2 -1
- package/types/src/lib/protocol.d.mts +84 -0
- package/types/src/lib/responses-upstream.d.mts +58 -0
- package/types/src/lib/responses.d.mts +72 -0
- package/types/src/lib/route.d.mts +7 -3
- package/types/src/lib/routes.d.mts +3 -2
- package/types/src/lib/settings.d.mts +11 -6
- package/types/src/lib/vllm.d.mts +4 -4
package/dist/lib/cli.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var O=Object.defineProperty;var p=(t,e)=>O(t,"name",{value:e,configurable:!0});import{readFile as d}from"node:fs/promises";import{parseArgs as N}from"node:util";const m=`llm-translator - run Claude Code (or any Anthropic/OpenAI client) against local LLM servers
|
|
2
2
|
|
|
3
3
|
Usage: llm-translator --upstream <url> [options]
|
|
4
4
|
|
|
@@ -12,6 +12,14 @@ Options:
|
|
|
12
12
|
--port <port> Listen port (default 8787)
|
|
13
13
|
--api-key <key> Key clients must send (x-api-key or Authorization: Bearer)
|
|
14
14
|
--allow-unauthenticated Allow a non-loopback --host without --api-key
|
|
15
|
+
--metrics Serve Prometheus metrics at GET /metrics (same authentication as the other routes)
|
|
16
|
+
--tls-key <file> TLS private key (PEM); with --tls-cert the server speaks HTTPS
|
|
17
|
+
--tls-cert <file> TLS certificate (PEM)
|
|
18
|
+
--tls-ca <file> Extra CA certificate (PEM)
|
|
19
|
+
--tls-passphrase <text> Passphrase of the TLS key
|
|
20
|
+
--cors-origin <origin> Origin browsers may call from (repeatable, * for any); CORS is off without it
|
|
21
|
+
--rate-limit <n> Allow n requests per window for each API key (remote address without one); off without it
|
|
22
|
+
--rate-limit-window <ms> Rate limit window (default 60000)
|
|
15
23
|
--config <file> JSON config file (translator config plus a "server" object)
|
|
16
24
|
--logs [path] Log warnings and errors to the console (default), or append every event as a JSON line to <path>
|
|
17
25
|
--silent Same as logs: false (no console output, no file)
|
|
@@ -20,11 +28,12 @@ Options:
|
|
|
20
28
|
--help Print this help
|
|
21
29
|
|
|
22
30
|
Environment: LLM_TRANSLATOR_FRONT, LLM_TRANSLATOR_UPSTREAM, LLM_TRANSLATOR_UPSTREAM_KEY, LLM_TRANSLATOR_BACKEND,
|
|
23
|
-
LLM_TRANSLATOR_HOST, LLM_TRANSLATOR_PORT, LLM_TRANSLATOR_API_KEY, LLM_TRANSLATOR_ALLOW_UNAUTHENTICATED (1),
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
`),"
|
|
27
|
-
`),"
|
|
31
|
+
LLM_TRANSLATOR_HOST, LLM_TRANSLATOR_PORT, LLM_TRANSLATOR_API_KEY, LLM_TRANSLATOR_ALLOW_UNAUTHENTICATED (1), LLM_TRANSLATOR_METRICS (1), LLM_TRANSLATOR_CONFIG,
|
|
32
|
+
LLM_TRANSLATOR_TLS_KEY, LLM_TRANSLATOR_TLS_CERT, LLM_TRANSLATOR_TLS_CA, LLM_TRANSLATOR_TLS_PASSPHRASE, LLM_TRANSLATOR_CORS_ORIGINS (comma separated),
|
|
33
|
+
LLM_TRANSLATOR_RATE_LIMIT, LLM_TRANSLATOR_RATE_LIMIT_WINDOW. Flags win over the environment, which wins over the config file.
|
|
34
|
+
`;function g(t){const e=t.map((s,n)=>s==="--logs"&&(n===t.length-1||t[n+1].startsWith("-"))?"--logs=":s);return N({args:e,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"},metrics:{type:"boolean"},"tls-key":{type:"string"},"tls-cert":{type:"string"},"tls-ca":{type:"string"},"tls-passphrase":{type:"string"},"cors-origin":{type:"string",multiple:!0},"rate-limit":{type:"string"},"rate-limit-window":{type:"string"},config:{type:"string"},logs:{type:"string"},silent:{type:"boolean"},"no-detect":{type:"boolean"},version:{type:"boolean"},help:{type:"boolean"}}}).values}p(g,"parse");function M(t,e,s){const{server:n,...T}=s,a=t.upstream??(e.LLM_TRANSLATOR_UPSTREAM?e.LLM_TRANSLATOR_UPSTREAM.split(","):void 0),r={...T};if(a){const i=t["upstream-key"]??e.LLM_TRANSLATOR_UPSTREAM_KEY,L=t.backend??e.LLM_TRANSLATOR_BACKEND;r.upstreams=a.map(u=>({url:u,apiKey:i,...L?{backend:L}:{}}))}const R=t.front??e.LLM_TRANSLATOR_FRONT;if(R&&(r.front=R),t.model){r.aliases={...r.aliases};for(const i of t.model){const L=i.indexOf("=");if(L<1)throw new Error(`--model expects alias=target (got "${i}")`);r.aliases[i.slice(0,L)]=i.slice(L+1)}}t.silent&&(r.logs=!1),t.logs!==void 0&&(r.logs=t.logs===""?!0:t.logs),r.logs??=r.silent!==!0,delete r.silent,t["no-detect"]&&(r.detect=!1);const S=t.port??e.LLM_TRANSLATOR_PORT,l={...n,...t.host??e.LLM_TRANSLATOR_HOST?{host:t.host??e.LLM_TRANSLATOR_HOST}:{},...S?{port:Number(S)}:{},...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}:{},...t.metrics||e.LLM_TRANSLATOR_METRICS==="1"?{metrics:!0}:{}},_={...n?.tls,...f({key:t["tls-key"]??e.LLM_TRANSLATOR_TLS_KEY,cert:t["tls-cert"]??e.LLM_TRANSLATOR_TLS_CERT}),...f({ca:t["tls-ca"]??e.LLM_TRANSLATOR_TLS_CA,passphrase:t["tls-passphrase"]??e.LLM_TRANSLATOR_TLS_PASSPHRASE})};Object.keys(_).length>0&&(l.tls=_);const c=t["cors-origin"]??(e.LLM_TRANSLATOR_CORS_ORIGINS?e.LLM_TRANSLATOR_CORS_ORIGINS.split(","):void 0);c&&(l.cors=c.map(i=>i.trim()).filter(Boolean));const o=t["rate-limit"]??e.LLM_TRANSLATOR_RATE_LIMIT,A=t["rate-limit-window"]??e.LLM_TRANSLATOR_RATE_LIMIT_WINDOW;return(o||A)&&(l.rateLimit={...n?.rateLimit,...o?{requests:Number(o)}:{},...A?{windowMs:Number(A)}:{}}),{translator:r,server:l}}p(M,"merge");function f(t){return Object.fromEntries(Object.entries(t).filter(([,e])=>e))}p(f,"pick");const h={out:p(t=>process.stdout.write(`${t}
|
|
35
|
+
`),"out"),err:p(t=>process.stderr.write(`${t}
|
|
36
|
+
`),"err"),onSignal:p(t=>{process.once("SIGINT",t),process.once("SIGTERM",t)},"onSignal")};async function I(t,e=process.env,s=h,n={}){let T;try{T=g(t)}catch(a){return s.err(`${a.message}
|
|
28
37
|
|
|
29
|
-
${
|
|
38
|
+
${m}`),2}if(T.help)return s.out(m),0;if(T.version)return s.out(n.version??(await import("../../package.json",{with:{type:"json"}})).default.version),0;try{const a=T.config??e.LLM_TRANSLATOR_CONFIG,{translator:r,server:R}=M(T,e,a?JSON.parse(await d(a,"utf8")):{}),l=await(n.createTranslator??(await import("../index.mjs")).createTranslator)(r),_=await l.server.start(R),c=l.status();s.out(`llm-translator (${c.front} front) listening on ${_.url}`);for(const o of c.upstreams??[]){const A=o.version??"unknown",i=o.probed?.inlineSystem??"auto",L=o.probed?.images??"default";s.out(` upstream ${o.name} ${o.url}: driver=${o.driver} version=${A} inlineSystem=${i} images=${L}`)}return await new Promise(o=>s.onSignal(()=>o())),await l.server.stop(),await l.slothlet.shutdown(),0}catch(a){return s.err(`llm-translator: ${a.message}`),1}}p(I,"main");export{I as main,M as merge,g as parse,h as processIo};
|
|
30
39
|
//# sourceMappingURL=cli.mjs.map
|
package/dist/lib/config.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var l=Object.defineProperty;var o=(t,s)=>l(t,"name",{value:s,configurable:!0});import{TranslatorError as n}from"./errors.mjs";import*as p from"./pool.mjs";import{valid as d}from"./record.mjs";import{normalizeAliases as f,normalizeEfforts as m}from"./effort.mjs";import{ALIASES as c,canonical as u}from"./protocol.mjs";function I(t){let s=t.length;for(;t[s-1]==="/";)s--;const r=t.slice(0,s);return r.endsWith("/v1")?r.slice(0,-3):r}o(I,"baseUrl");function y(t,s,r){const e=typeof t=="string"?{url:t}:t;if(!e||typeof e!="object"||typeof e.url!="string"||e.url==="")throw new n(`upstreams[${s}] needs a "url"`,{code:"INVALID_CONFIG",status:500});for(const a of["apiVersion","deployment"])if(e[a]!==void 0&&(typeof e[a]!="string"||e[a]===""))throw new n(`upstreams[${s}].${a} must be a non-empty string`,{code:"INVALID_CONFIG",status:500});if(e.api!==void 0&&u(e.api)===void 0)throw new n(`upstreams[${s}].api must be one of ${Object.keys(c).map(a=>JSON.stringify(a)).join(", ")} (got ${JSON.stringify(e.api)})`,{code:"INVALID_CONFIG",status:500});const i=e.settings?.nativeMessages??r?.nativeMessages;return{name:e.name??`upstream-${s}`,url:I(e.url),apiKey:e.apiKey,headers:{...e.headers},apiVersion:e.apiVersion,deployment:e.deployment,api:e.api===void 0?"auto":u(e.api),apiSet:e.api!==void 0,legacy:i,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:p.health()}}o(y,"endpoint");function O(t={}){const s=t.front??"anthropic";if(s!=="anthropic"&&s!=="openai")throw new n(`front must be "anthropic" or "openai" (got ${JSON.stringify(s)})`,{code:"INVALID_CONFIG",status:500});let r=t.upstreams??t.upstream;if(r!==void 0&&!Array.isArray(r)&&(r=[r]),!r||r.length===0)throw new n("at least one upstream is required (config.upstreams)",{code:"INVALID_CONFIG",status:500});const e=t.logs??!1;if(typeof e!="boolean"&&(typeof e!="string"||e===""))throw new n("logs must be true, false or a file path (got "+JSON.stringify(e)+")",{code:"INVALID_CONFIG",status:500});if(t.onEvent!==void 0&&typeof t.onEvent!="function")throw new n("onEvent must be a function",{code:"INVALID_CONFIG",status:500});if(!d(t.capture))throw new n("capture must be false, a directory path or a function (got "+JSON.stringify(t.capture)+")",{code:"INVALID_CONFIG",status:500});return{front:s,endpoints:r.map((i,a)=>y(i,a,t.settings)),...f(t.aliases),efforts:m(t.efforts),pool:p.normalize(t.pool),settings:{...t.settings},detect:t.detect!==!1,detectTimeout:t.detectTimeout??5e3,transport:t.transport,logs:e,capture:t.capture||!1,events:t.events!==!1,onEvent:t.onEvent}}o(O,"normalize");export{y as endpoint,O as normalize};
|
|
2
2
|
//# sourceMappingURL=config.mjs.map
|
package/dist/lib/estimate.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var p=Object.defineProperty;var i=(n,s)=>p(n,"name",{value:s,configurable:!0});const m=4,
|
|
1
|
+
var p=Object.defineProperty;var i=(n,s)=>p(n,"name",{value:s,configurable:!0});const m=4,a=i(n=>Math.ceil(n/4),"tokens");function x(n){return n.input!==void 0?a(JSON.stringify([n.instructions,n.input,n.tools]).length):a(JSON.stringify([n.system,n.messages,n.tools]).length)}i(x,"estimate");const r=i((...n)=>{let s=0;for(const t of n)typeof t=="string"&&(s+=t.length);return s},"length");function f(n,s){if(n==="anthropic"){if(s?.type==="content_block_start")return r(s.content_block?.name);const o=s?.type==="content_block_delta"?s.delta:null;return r(o?.text,o?.thinking,o?.partial_json)}const t=s?.choices?.[0]?.delta;let e=r(t?.content,t?.reasoning,t?.reasoning_content);for(const o of t?.tool_calls??[])e+=r(o.function?.name,o.function?.arguments);return e}i(f,"eventLength");function y(n,s){let t=0;if(n==="anthropic"){for(const o of s?.content??[])t+=r(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=r(e?.content,e?.reasoning,e?.reasoning_content);for(const o of e?.tool_calls??[])t+=r(o.function?.name,o.function?.arguments);return t}i(y,"bodyLength");const _=i(n=>Math.max(1,a(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*N(n,s,t){let e=0;for await(const o of s){const l=o?.data;e+=f(n,l);const u=(n==="anthropic"?l?.type==="message_delta":Array.isArray(l?.choices)&&l.choices.length===0&&l.usage)?t.gaps():null;if(u&&(u.input||u.output)){const c={input_tokens:u.input?await t.input():null,output_tokens:u.output?_(e):null};t.report(c),yield{...o,data:{...l,usage:g(n,l.usage,c)}}}else yield o}}i(N,"stream");export{m as CHARS_PER_TOKEN,y as bodyLength,x as estimate,f as eventLength,_ as output,g as patch,N as stream,a as tokens};
|
|
2
2
|
//# sourceMappingURL=estimate.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var y=Object.defineProperty;var c=(t,s)=>y(t,"name",{value:s,configurable:!0});import{readFile as h}from"node:fs/promises";import{TranslatorError as m}from"./errors.mjs";const u=c(t=>new m(t,{code:"invalid_config",status:500}),"invalid");async function x(t){if(t==null)return null;if(typeof t!="object"||Array.isArray(t))throw u("tls must be an object with key and cert file paths");const{key:s,cert:i,ca:n,passphrase:a}=t;if(!s&&!i&&!n&&!a)return null;if(!s||!i)throw u(`tls needs both key and cert (got ${s?"key":"cert"} only)`);const e=c(async(r,o)=>{if(typeof o!="string")throw u(`tls.${r} must be a file path`);try{return await h(o)}catch(f){throw u(`tls.${r}: cannot read ${o} (${f.code??f})`)}},"read"),l={key:await e("key",s),cert:await e("cert",i)};if(n&&(l.ca=await Promise.all([n].flat().map(r=>e("ca",r)))),a!==void 0){if(typeof a!="string")throw u("tls.passphrase must be a string");l.passphrase=a}return l}c(x,"tlsOptions");const g="authorization, x-api-key, content-type, anthropic-version, anthropic-beta",d=c(t=>t.toLowerCase().replace(/\/+$/,""),"comparable");function $(t){if(t==null||t===!1)return null;const s=[Array.isArray(t)||typeof t=="string"?t:t.origins??[]].flat();if(s.some(e=>typeof e!="string"||e===""))throw u("cors origins must be non-empty strings");if(s.length===0)return null;const i=s.includes("*"),n=new Set(s.map(d)),a=c(e=>e?!i&&!n.has(d(e))?{vary:"origin"}:{"access-control-allow-origin":i?"*":e,"access-control-expose-headers":"retry-after, request-id, x-request-id",vary:"origin"}:{},"headers");return{headers:a,preflight:c((e,l)=>{const r=a(e);return r["access-control-allow-origin"]?{...r,"access-control-allow-methods":"GET, HEAD, POST, OPTIONS","access-control-allow-headers":l||g,"access-control-max-age":"600"}:r},"preflight")}}c($,"cors");function v(t,s=Date.now){if(t==null||t===!1)return null;const i=t.requests,n=t.windowMs??6e4;if(!Number.isInteger(i)||i<1)throw u(`rateLimit.requests must be a positive integer (got ${JSON.stringify(i)})`);if(!Number.isInteger(n)||n<1)throw u(`rateLimit.windowMs must be a positive integer (got ${JSON.stringify(n)})`);const a=new Map;let e=s();return{take:c(l=>{const r=s();if(r-e>=n){for(const[f,w]of a)r-w.start>=n&&a.delete(f);e=r}let o=a.get(l);return(!o||r-o.start>=n)&&(o={start:r,count:0},a.set(l,o)),o.count>=i?Math.max(1,Math.ceil((o.start+n-r)/1e3)):(o.count++,0)},"take"),size:c(()=>a.size,"size")}}c(v,"rateLimit");export{$ as cors,v as rateLimit,x as tlsOptions};
|
|
2
|
+
//# sourceMappingURL=guard.mjs.map
|
package/dist/lib/http.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var l=Object.defineProperty;var r=(t,e)=>l(t,"name",{value:e,configurable:!0});function p(t,e){return t+e}r(p,"join");async function m(t,e,a,n,s){return t.path?t.path(e,{endpoint:a,model:n}):(t.info.native!==void 0&&s===t.info.native.protocol?t.info.native.paths:void 0)?.[e]??t.info.paths[e]??y[e]}r(m,"pathOf");const y={responses:"/v1/responses",embeddings:"/v1/embeddings",models:"/v1/models"};function k(t){return t==="localhost"||t==="::1"||/^127\./.test(t)}r(k,"isLoopback");function d(t,e="bearer",a){const n={...t.headers};return t.apiKey&&(e==="x-api-key"||e==="api-key"?n[e]=t.apiKey:n.authorization=`Bearer ${t.apiKey}`),{...n,...a}}r(d,"headers");function S({endpoint:t,transport:e,signal:a,timeout:n,probes:s},f,i){const u=i!==void 0&&(i.auth==="x-api-key"||i.auth==="api-key"||i.headers!==void 0),o=u?{auth:i.auth,extra:i.headers}:{},h=r(()=>g(e,p(t.url,f),{method:"GET",headers:d(t,o.auth,o.extra),signal:a,timeout:n}),"run");if(!s)return h();const c=u?JSON.stringify([f,o.auth,o.extra]):f;return s.has(c)||s.set(c,h()),s.get(c)}r(S,"probe");function x(t){const e={};if(!t)return e;if(typeof t.forEach=="function")return t.forEach((a,n)=>{e[n.toLowerCase()]=a}),e;for(const[a,n]of Object.entries(t))e[a.toLowerCase()]=String(n);return e}r(x,"plain");async function g(t,e,a={}){const{timeout:n,signal:s,body:f,...i}=a,u=[];s&&u.push(s),n>0&&u.push(AbortSignal.timeout(n));try{const o=await t(e,{...i,redirect:"error",body:f===void 0?void 0:JSON.stringify(f),signal:u.length>1?AbortSignal.any(u):u[0]}),h=await o.text();let c=null;try{c=JSON.parse(h)}catch{c=h===""?null:h}return{ok:o.ok,status:o.status,body:c,headers:x(o.headers)}}catch(o){return{ok:!1,status:0,body:null,error:o}}}r(g,"json");export{d as headers,k as isLoopback,p as join,g as json,m as pathOf,x as plain,S as probe};
|
|
2
2
|
//# sourceMappingURL=http.mjs.map
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
var x=Object.defineProperty;var c=(t,r)=>x(t,"name",{value:r,configurable:!0});import{describe as I}from"./pool.mjs";const A="text/plain; version=0.0.4; charset=utf-8",N=[.005,.01,.025,.05,.1,.25,.5,1,2.5,5,10,30,60,120,300],R=1e4,m="llm_translator_",T=`${m}requests_total`,w=`${m}request_duration_seconds`,v=`${m}tokens_total`,S=`${m}errors_total`,U=`${m}upstream_up`;function j(t){return String(t).replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\n/g,"\\n")}c(j,"escapeLabel");function P(t){return t.replace(/\\/g,"\\\\").replace(/\n/g,"\\n")}c(P,"escapeHelp");function $(t){return t===1/0?"+Inf":t===-1/0?"-Inf":String(t)}c($,"format");function f(t){const r=Object.entries(t).map(([i,n])=>`${i}="${j(n)}"`);return r.length===0?"":`{${r.join(",")}}`}c(f,"braces");function F(){const t=new Map,r=c(i=>JSON.stringify(Object.entries(i)),"key");return{counter(i,n){t.set(i,{type:"counter",help:n,series:new Map})},histogram(i,n,e=N){t.set(i,{type:"histogram",help:n,buckets:e,series:new Map})},inc(i,n,e=1){const{series:o}=t.get(i),s=r(n),a=o.get(s)??{labels:n,value:0};a.value+=e,o.set(s,a)},observe(i,n,e){const{series:o,buckets:s}=t.get(i),a=r(n),l=o.get(a)??{labels:n,counts:s.map(()=>0),sum:0,count:0};s.forEach((h,u)=>{e<=h&&l.counts[u]++}),l.sum+=e,l.count++,o.set(a,l)},render(i=[]){const n=[];for(const[e,o]of t){n.push(`# HELP ${e} ${P(o.help)}`,`# TYPE ${e} ${o.type}`);for(const s of o.series.values()){if(o.type==="counter"){n.push(`${e}${f(s.labels)} ${$(s.value)}`);continue}o.buckets.forEach((a,l)=>{n.push(`${e}_bucket${f({...s.labels,le:$(a)})} ${s.counts[l]}`)}),n.push(`${e}_bucket${f({...s.labels,le:"+Inf"})} ${s.count}`),n.push(`${e}_sum${f(s.labels)} ${$(s.sum)}`),n.push(`${e}_count${f(s.labels)} ${s.count}`)}}for(const e of i){n.push(`# HELP ${e.name} ${P(e.help)}`,`# TYPE ${e.name} gauge`);for(const o of e.samples)n.push(`${e.name}${f(o.labels)} ${$(o.value)}`)}return`${n.join(`
|
|
2
|
+
`)}
|
|
3
|
+
`}}}c(F,"create");function H(t,r){return t===499||r?.reason==="aborted"?null:r?.reason==="upstream_error"?"upstream":r?.reason==="error"?t>=400&&t<500?"client":"translator":t>=500?"translator":t>=400?"client":null}c(H,"kind");function C(t){const r=F();r.counter(T,"Requests handled, by front protocol, route, upstream driver and HTTP status code (499 when the client left before the reply finished)."),r.histogram(w,"Seconds from the request arriving to the end of the response, streams included."),r.counter(v,"Tokens reported by upstreams, by driver and type (input, output, cache_read, cache_creation)."),r.counter(S,"Failed requests by kind: upstream, translator or client.");const i=new WeakMap,n=c(async(e,o,s)=>{const a=o.raw,l=!a.writableFinished,h=i.get(e);let u=null;if(h){let p;const d=new Promise(g=>{p=setTimeout(g,R,null),p.unref()});u=await Promise.race([h.then(g=>g.usage,()=>null),d]),clearTimeout(p)}const O=Number(process.hrtime.bigint()-s)/1e9,b=l?499:a.statusCode,_=u?.upstream?t.endpoints().find(p=>p.name===u.upstream):void 0,y=_?String(_.driver??"unknown"):"none",k={front:t.front,route:e.routeOptions?.url??"unmatched",driver:y,code:b};r.inc(T,k),r.observe(w,k,O);const E=H(b,u);if(E&&r.inc(S,{kind:E}),u?.reported)for(const[p,d]of[["input","input_tokens"],["output","output_tokens"],["cache_read","cache_read_input_tokens"],["cache_creation","cache_creation_input_tokens"]])Number.isFinite(u[d])&&u[d]>0&&r.inc(v,{driver:y,type:p},u[d])},"finish");return{hook:c(async(e,o)=>{if(e.routeOptions?.config?.metrics)return;const s=process.hrtime.bigint();o.raw.once("close",()=>{n(e,o,s).catch(()=>{})})},"hook"),attach:c((e,o)=>{i.set(e,o)},"attach"),render:c(()=>r.render([{name:U,help:"1 while an upstream is healthy (not backing off after a failure and not reported not ready by its readiness probe), else 0.",samples:t.endpoints().map(e=>({labels:{upstream:e.name,driver:String(e.driver??"unknown")},value:I(e).healthy?1:0}))}]),"render")}}c(C,"recorder");export{N as BUCKETS,A as CONTENT_TYPE,F as create,P as escapeHelp,j as escapeLabel,H as kind,C as recorder};
|
|
4
|
+
//# sourceMappingURL=metrics.mjs.map
|
package/dist/lib/presets.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var f=Object.defineProperty;var s=(e,t)=>f(e,"name",{value:t,configurable:!0});import{noEmptyTools as d}from"./normalize.mjs";const p={chat:"/v1/chat/completions",embeddings:"/v1/embeddings",models:"/v1/models"},c={chat:"/chat/completions",embeddings:"/embeddings",models:"/models"};function m(e){return/^grok-(3-mini|4|code)/.test(String(e))&&!/non-reasoning/.test(String(e))}s(m,"grokReasons");const l=Object.freeze({groq:{order:21,hosts:["api.groq.com"],root:"/openai",paths:p,settings:{reasoningField:"reasoning",maxTokensField:"max_completion_tokens",thinking:"none",thinkingBudget:!1,effort:!0,topK:!1,prefill:"none",streamUsage:!0,continuousUsage:!1},drop:["logprobs","top_logprobs","logit_bias"],dropName:!0},together:{order:22,hosts:["api.together.ai","api.together.xyz","us-api.together.ai"],paths:p,fields:["top_k","min_p","repetition_penalty"],settings:{reasoningField:"reasoning",reasoningRoundTrip:!0,maxTokensField:"max_tokens",thinking:"none",thinkingBudget:!1,effort:!0,topK:!0,prefill:"none",parallelToolCalls:!1,streamUsage:!1,continuousUsage:!1}},fireworks:{order:23,hosts:["api.fireworks.ai"],root:"/inference",paths:p,fields:["top_k","min_p","repetition_penalty"],settings:{reasoningField:"reasoning_content",reasoningRoundTrip:!0,maxTokensField:"max_tokens",thinking:"reasoning_effort",thinkingBudget:!1,effort:!0,topK:!0,prefill:"none",streamUsage:!0,continuousUsage:!1}},xai:{order:24,hosts:["api.x.ai"],paths:p,settings:{reasoningField:"reasoning_content",maxTokensField:"max_completion_tokens",thinking:"none",thinkingBudget:!1,effort:!0,topK:!1,prefill:"none",streamUsage:!0,continuousUsage:!1},dropWhen:s(e=>m(e.model)?["stop","presence_penalty","frequency_penalty"]:[],"dropWhen")},gemini:{order:25,hosts:["generativelanguage.googleapis.com"],root:"/v1beta/openai",paths:c,settings:{maxTokensField:"max_tokens",thinking:"none",thinkingBudget:!1,effort:!0,topK:!1,prefill:"none",streamUsage:!0,continuousUsage:!1}}});function _(e){const t=l[e],
|
|
1
|
+
var f=Object.defineProperty;var s=(e,t)=>f(e,"name",{value:t,configurable:!0});import{noEmptyTools as d}from"./normalize.mjs";const p={chat:"/v1/chat/completions",embeddings:"/v1/embeddings",models:"/v1/models"},c={chat:"/chat/completions",embeddings:"/embeddings",models:"/models"};function m(e){return/^grok-(3-mini|4|code)/.test(String(e))&&!/non-reasoning/.test(String(e))}s(m,"grokReasons");const l=Object.freeze({groq:{order:21,hosts:["api.groq.com"],root:"/openai",paths:p,settings:{reasoningField:"reasoning",maxTokensField:"max_completion_tokens",thinking:"none",thinkingBudget:!1,effort:!0,topK:!1,prefill:"none",streamUsage:!0,continuousUsage:!1},drop:["logprobs","top_logprobs","logit_bias"],dropName:!0},together:{order:22,hosts:["api.together.ai","api.together.xyz","us-api.together.ai"],paths:p,fields:["top_k","min_p","repetition_penalty"],settings:{reasoningField:"reasoning",reasoningRoundTrip:!0,maxTokensField:"max_tokens",thinking:"none",thinkingBudget:!1,effort:!0,topK:!0,prefill:"none",parallelToolCalls:!1,streamUsage:!1,continuousUsage:!1}},fireworks:{order:23,hosts:["api.fireworks.ai"],root:"/inference",paths:p,fields:["top_k","min_p","repetition_penalty"],settings:{reasoningField:"reasoning_content",reasoningRoundTrip:!0,maxTokensField:"max_tokens",thinking:"reasoning_effort",thinkingBudget:!1,effort:!0,topK:!0,prefill:"none",streamUsage:!0,continuousUsage:!1}},xai:{order:24,hosts:["api.x.ai"],paths:p,settings:{reasoningField:"reasoning_content",maxTokensField:"max_completion_tokens",thinking:"none",thinkingBudget:!1,effort:!0,topK:!1,prefill:"none",streamUsage:!0,continuousUsage:!1},dropWhen:s(e=>m(e.model)?["stop","presence_penalty","frequency_penalty"]:[],"dropWhen")},gemini:{order:25,hosts:["generativelanguage.googleapis.com"],root:"/v1beta/openai",paths:c,settings:{maxTokensField:"max_tokens",thinking:"none",thinkingBudget:!1,effort:!0,topK:!1,prefill:"none",streamUsage:!0,continuousUsage:!1}}});function _(e){const t=l[e],n={name:e,protocol:"openai.chat",protocols:["openai.chat"],order:t.order,auth:"bearer",strict:!1,hosts:[...t.hosts],paths:{...t.paths}};return t.fields?{...n,fields:[...t.fields]}:n}s(_,"describe");function x(e,t){const n=URL.canParse(t.url)?new URL(t.url).hostname:"";return l[e].hosts.includes(n)?{host:n}:null}s(x,"match");function U(e){return{...l[e].settings}}s(U,"settingsOf");function b(e,t,n){const i=l[e],a=i.root??"";return(n.url.endsWith(a)?"":a)+i.paths[t]}s(b,"route");function y(e,t,n){const i=l[e],a=n?.state?.dropped,g=s(o=>{Array.isArray(a)&&!a.includes(o)&&a.push(o)},"note"),r={...d(t)};for(const o of[...i.drop??[],...i.dropWhen?.(t)??[]])r[o]!==void 0&&(delete r[o],g(o));return i.dropName&&Array.isArray(r.messages)&&r.messages.some(o=>o&&"name"in o)&&(r.messages=r.messages.map(o=>{const u={...o};return delete u.name,u}),g("messages[].name")),r}s(y,"trim");export{l as PRESETS,_ as describe,x as match,b as route,U as settingsOf,y as trim};
|
|
2
2
|
//# sourceMappingURL=presets.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var g=Object.defineProperty;var r=(t,e)=>g(t,"name",{value:e,configurable:!0});const n="anthropic.messages",o="openai.chat",s="openai.responses",i="ollama.chat",O=[n,o,s,i],h=Object.freeze({auto:"auto",[n]:n,messages:n,anthropic:n,claude:n,[o]:o,chat:o,openai:o,chatgpt:o,[s]:s,responses:s,codex:s,[i]:i,native:i,ollama:i});function w(t){return typeof t=="string"&&Object.hasOwn(h,t.toLowerCase())?h[t.toLowerCase()]:void 0}r(w,"canonical");function A(t){const e=[...Array.isArray(t.protocols)?t.protocols:[]];return t.protocol!==void 0&&e.push(t.protocol),t.responses===!0&&e.push(s),t.native?.protocol!==void 0&&e.push(t.native.protocol),[...new Set(e)]}r(A,"supported");function E(t,e){return t==="anthropic"?n:e==="responses"?s:o}r(E,"frontOf");function L({api:t,front:e,info:p,missing:l,usable:a}){const x=A(p),u=r(c=>x.includes(c)&&(a?a(c):!0),"ok");if(t!=="auto")return[u(t)?t:p.protocol];const d=[e,o].filter((c,f,S)=>S.indexOf(c)===f&&u(c)&&!l?.has(c));return d.length>0?d:[p.protocol]}r(L,"candidates");function y(t,e,p){t.missing.has(e)||t.missing.set(e,new Set),t.missing.get(e).add(p)}r(y,"remember");export{h as ALIASES,o as CHAT,O as IDS,n as MESSAGES,i as OLLAMA,s as RESPONSES,L as candidates,w as canonical,E as frontOf,y as remember,A as supported};
|
|
2
|
+
//# sourceMappingURL=protocol.mjs.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
var q=Object.defineProperty;var u=(e,o)=>q(e,"name",{value:o,configurable:!0});import{resolve as D}from"./settings.mjs";import{TranslatorError as b}from"./errors.mjs";import{completionId as U}from"./ids.mjs";import{messages as C}from"./sse.mjs";const Y="openai.responses",Z=[404,405,501],E="openai-responses-v1",L=new Set(["model","messages","stream","stream_options","max_tokens","max_completion_tokens","temperature","top_p","tools","tool_choice","parallel_tool_calls","reasoning_effort","response_format","user","n","metadata","store","service_tier","prompt_cache_key","safety_identifier","verbosity"]);function w(e,o){e.dropped.includes(o)||e.dropped.push(o)}u(w,"drop");function F(e){return typeof e=="string"?e:(Array.isArray(e)?e:[]).filter(o=>o?.type==="text").map(o=>o.text).join(`
|
|
2
|
+
|
|
3
|
+
`)}u(F,"plainText");function S(e,o){const n=[];for(const t of e)if(t?.type==="text")n.push({type:"input_text",text:t.text});else if(t?.type==="image_url"){const s=typeof t.image_url=="string"?{url:t.image_url}:t.image_url;n.push({type:"input_image",image_url:s.url,...s.detail===void 0?{}:{detail:s.detail}})}else if(t?.type==="file"&&t.file!==null&&typeof t.file=="object"){const{file_id:s,file_data:i,filename:r}=t.file;n.push({type:"input_file",...s===void 0?{}:{file_id:s},...i===void 0?{}:{file_data:i},...r===void 0?{}:{filename:r}})}else w(o,`content:${t?.type}`);return n}u(S,"inputParts");function P(e,o){if(typeof e=="string")return e;const n=Array.isArray(e)?e:[];return n.every(t=>t?.type==="text")?n.map(t=>t.text).join(`
|
|
4
|
+
`):S(n,o)}u(P,"toolOutput");function z(e){if(!Array.isArray(e.reasoning_details))return[];const o=[];for(const n of e.reasoning_details){if(n?.format!==E||typeof n.data!="string"||n.data==="")continue;const t={type:"reasoning",summary:[],encrypted_content:n.data};typeof n.id=="string"&&n.id!==""&&(t.id=n.id),typeof n.summary=="string"&&n.summary!==""&&(t.summary=[{type:"summary_text",text:n.summary}]),o.push({at:Number.isInteger(n.index)&&n.index>=0?n.index:0,item:t})}return o}u(z,"reasoningItems");function G(e,o,n){const t=[];let s=F(e.content);s===""&&typeof e.refusal=="string"&&(s=e.refusal),s!==""&&t.push({role:"assistant",content:s});for(const l of e.tool_calls??[]){if(l?.type!==void 0&&l.type!=="function"){w(o,`tool_call:${l.type}`);continue}const f=l.function?.arguments;t.push({type:"function_call",call_id:l.id,name:l.function?.name,arguments:typeof f=="string"?f:"{}"})}if(!n)return t;const i=[],r=z(e).sort((l,f)=>l.at-f.at);t.forEach((l,f)=>{for(;r.length>0&&r[0].at<=f;)i.push(r.shift().item);i.push(l)});for(const{item:l}of r)i.push(l);return i}u(G,"assistantItems");function H(e,o,n){const t=[],s=[];for(const i of e)switch(i?.role){case"system":case"developer":{const r=F(i.content);if(r==="")break;t.length===0?s.push(r):t.push({role:"system",content:r});break}case"user":t.push({role:"user",content:typeof i.content=="string"?i.content:S(i.content??[],o)});break;case"assistant":t.push(...G(i,o,n));break;case"tool":t.push({type:"function_call_output",call_id:i.tool_call_id,output:P(i.content,o)});break;default:throw new b(`messages: unsupported role "${i?.role}"`,{code:"invalid_request",param:"messages"})}return{input:t,instructions:s.join(`
|
|
5
|
+
|
|
6
|
+
`)}}u(H,"convertMessages");function I(e,o){const n=[];for(const t of e)if(t?.type==="function"&&t.function){const s=t.function;n.push({type:"function",name:s.name,...s.description===void 0?{}:{description:s.description},parameters:s.parameters??{type:"object",properties:{}},strict:s.strict===!0})}else if(t?.type==="custom"&&t.custom){const{name:s,description:i,format:r}=t.custom;n.push({type:"custom",name:s,...i===void 0?{}:{description:i},...r===void 0?{}:{format:r}})}else w(o,`tool:${t?.type}`);return n}u(I,"convertTools");function B(e,o){if(typeof e=="string")return e;if(e?.type==="function"&&e.function)return{type:"function",name:e.function.name};if(e?.type==="custom"&&e.custom)return{type:"custom",name:e.custom.name};if(e?.type==="allowed_tools"&&e.allowed_tools){const n=I(e.allowed_tools.tools??[],o).map(t=>({type:t.type,name:t.name}));return{type:"allowed_tools",mode:e.allowed_tools.mode,tools:n}}w(o,"tool_choice")}u(B,"convertChoice");function J(e,o,n){const t={},s=e.response_format;if(s!==void 0)if(!o.responseFormat)w(n,"response_format");else if(s.type==="json_schema"&&s.json_schema){const{name:i,schema:r,strict:l,description:f}=s.json_schema;t.format={type:"json_schema",name:i??"output",schema:r,...l===void 0?{}:{strict:l},...f===void 0?{}:{description:f}}}else s.type==="json_object"?t.format={type:"json_object"}:s.type==="text"?t.format={type:"text"}:w(n,"response_format");return e.verbosity!==void 0&&(t.verbosity=e.verbosity),Object.keys(t).length>0?t:void 0}u(J,"textOf");function ee(e,o){const n=D(o);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new b("messages is a required property",{code:"invalid_request",param:"messages"});if(e.n!==void 0&&e.n!==1)throw new b("n: only a single choice (n = 1) is supported",{code:"unsupported_field",param:"n"});const t={front:"openai",model:e.model,stream:e.stream===!0,includeUsage:e.stream_options?.include_usage===!0,reasoningField:n.reasoningField,toolNames:{},toolIds:{},dropped:[]};for(const m of Object.keys(e))if(!L.has(m)){if(n.unknownFields==="error")throw new b(`${m}: Unrecognized request argument`,{code:"unsupported_field",param:m});w(t,m)}const{input:s,instructions:i}=H(e.messages,t,n.reasoningRoundTrip),r={model:e.model,input:s};i!==""&&(r.instructions=i),t.stream&&(r.stream=!0),r.store=!1,n.reasoningRoundTrip&&(r.include=["reasoning.encrypted_content"]);const l=e.max_completion_tokens??e.max_tokens;l!==void 0&&(r.max_output_tokens=l);for(const m of["temperature","top_p","user","metadata","service_tier","prompt_cache_key","safety_identifier"])e[m]!==void 0&&(r[m]=e[m]);const f=I(e.tools??[],t);if(f.length>0){r.tools=f;const m=e.tool_choice===void 0?void 0:B(e.tool_choice,t);m!==void 0&&(r.tool_choice=m),e.parallel_tool_calls!==void 0&&n.parallelToolCalls&&(r.parallel_tool_calls=e.parallel_tool_calls)}typeof e.reasoning_effort=="string"&&(r.reasoning={effort:e.reasoning_effort},e.reasoning_effort!=="none"&&n.reasoningSummary!=="none"&&(r.reasoning.summary=n.reasoningSummary));const x=J(e,n,t);return x&&(r.text=x),{body:r,state:t}}u(ee,"request");function A(e){const o=u((n,t)=>(Array.isArray(n)?n:[]).filter(s=>s?.type===t&&typeof s.text=="string").map(s=>s.text).join(`
|
|
7
|
+
|
|
8
|
+
`),"pick");return o(e.summary,"summary_text")||o(e.content,"reasoning_text")}u(A,"reasoningText");function $(e,o){if(typeof e.encrypted_content!="string"||e.encrypted_content==="")return null;const n={type:"reasoning.encrypted",data:e.encrypted_content,format:E,index:o};typeof e.id=="string"&&e.id!==""&&(n.id=e.id);const t=A(e);return t!==""&&(n.summary=t),n}u($,"detailOf");function M(e){if(e===null||typeof e!="object")return;const o=e.input_tokens??0,n=e.output_tokens??0,t={prompt_tokens:o,completion_tokens:n,total_tokens:e.total_tokens??o+n},s=e.input_tokens_details?.cached_tokens;typeof s=="number"&&(t.prompt_tokens_details={cached_tokens:s});const i=e.output_tokens_details?.reasoning_tokens;return typeof i=="number"&&(t.completion_tokens_details={reasoning_tokens:i}),t}u(M,"usageOf");function R(e,o){if(e.status==="incomplete"){const n=e.incomplete_details?.reason;if(n==="max_output_tokens")return"length";if(n==="content_filter")return"content_filter"}return o?"tool_calls":"stop"}u(R,"finishOf");function N(e){if(e.status==="completed"||e.status==="incomplete")return null;const o=e.error?.message??`Upstream response ended with status "${e.status}"`;return new b(o,{code:"upstream_"+(e.error?.code??"failed"),status:502,type:"api_error"})}u(N,"failureOf");function te(e,o,n){const t=D(n);if(e===null||typeof e!="object"||!Array.isArray(e.output))throw(e?.error?N({status:"failed",error:e.error}):null)??new b("Upstream response contained no output",{code:"invalid_upstream_response",status:502});const s=N(e);if(s)throw s;const i=[],r=[],l=[],f=[],x=[];let m=!1;for(const c of e.output)if(c?.type==="reasoning"){const y=A(c);y!==""&&l.push(y);const j=t.reasoningRoundTrip?$(c,f.length+(m?1:0)):null;j&&x.push(j)}else if(c?.type==="message")for(const y of c.content??[])y?.type==="output_text"&&typeof y.text=="string"?(i.push(y.text),y.text!==""&&(m=!0)):y?.type==="refusal"&&typeof y.refusal=="string"&&r.push(y.refusal);else c?.type==="function_call"&&f.push({id:c.call_id??c.id,type:"function",function:{name:c.name,arguments:typeof c.arguments=="string"?c.arguments:"{}"}});const h={role:"assistant",content:i.length===0&&f.length>0?null:i.join("")};r.length>0&&(h.refusal=r.join("")),l.length>0&&(h[t.reasoningField]=l.join(`
|
|
9
|
+
|
|
10
|
+
`)),x.length>0&&(h.reasoning_details=x),f.length>0&&(h.tool_calls=f);const _=M(e.usage);return o&&_&&(o.upstreamUsage=_),{id:`chatcmpl-${String(e.id??"").replace(/^resp_/,"")||U().slice(9)}`,object:"chat.completion",created:e.created_at??Math.floor(Date.now()/1e3),model:o?.model??e.model,choices:[{index:0,message:h,finish_reason:R(e,f.length>0)}],..._?{usage:_}:{}}}u(te,"response");const T=u((e,o="server_error",n=null)=>({data:{error:{message:e,type:o,param:null,code:n}}}),"failureChunk");async function*ne(e,o,n){const t=D(n),s=o?.includeUsage??!0,i=Math.floor(Date.now()/1e3);let r=U(),l=o?.model;const f=new Map;let x=0,m=!1,h=null;const _=u((a,p=null)=>({data:{id:r,object:"chat.completion.chunk",created:i,model:l,choices:[{index:0,delta:a,finish_reason:p}]}}),"chunk"),c=u(a=>(f.has(a)||f.set(a,{tool:null,streamed:!1,parts:0}),f.get(a)),"entry");function*y(a,p){const g=c(a);g.tool===null&&(g.tool=x++,yield _({tool_calls:[{index:g.tool,id:p.call_id??p.id,type:"function",function:{name:p.name,arguments:""}}]}))}u(y,"start");function*j(a){a!==""&&(m=!0,yield _({content:a}))}u(j,"text");try{for await(const{data:a}of C(e)){if(a==="[DONE]")break;const p=a?.type,g=a?.output_index??0;if(p==="response.created")r=`chatcmpl-${String(a.response?.id??"").replace(/^resp_/,"")||r.slice(9)}`,l=o?.model??a.response?.model,yield _({role:"assistant",content:""});else if(p==="response.output_item.added"&&a.item?.type==="function_call")yield*y(g,a.item);else if(p==="response.output_text.delta")c(g).streamed=!0,yield*j(a.delta??"");else if(p==="response.refusal.delta")c(g).streamed=!0,yield _({refusal:a.delta??""});else if(p==="response.reasoning_summary_part.added"){const d=c(g);d.parts++>0&&(yield _({[t.reasoningField]:`
|
|
11
|
+
|
|
12
|
+
`}))}else if(p==="response.reasoning_summary_text.delta"||p==="response.reasoning_text.delta")c(g).streamed=!0,yield _({[t.reasoningField]:a.delta??""});else if(p==="response.function_call_arguments.delta"){const d=c(g);d.streamed=!0,d.tool!==null&&(yield _({tool_calls:[{index:d.tool,function:{arguments:a.delta??""}}]}))}else if(p==="response.output_item.done"){const d=a.item??{},O=c(g);if(d.type==="reasoning"){!O.streamed&&A(d)!==""&&(yield _({[t.reasoningField]:A(d)}));const k=t.reasoningRoundTrip?$(d,x+(m?1:0)):null;k&&(yield _({reasoning_details:[k]}))}else if(d.type==="message"){if(!O.streamed)for(const k of d.content??[])k?.type==="output_text"?yield*j(k.text??""):k?.type==="refusal"&&(yield _({refusal:k.refusal??""}))}else d.type==="function_call"&&(yield*y(g,d),!O.streamed&&typeof d.arguments=="string"&&d.arguments!==""&&(yield _({tool_calls:[{index:O.tool,function:{arguments:d.arguments}}]})))}else if(p==="response.completed"||p==="response.incomplete"){h=a.response??{};break}else if(p==="response.failed"){const d=N({...a.response,status:"failed"});yield T(d.message,"server_error",d.code),yield{data:"[DONE]"};return}else if(p==="error"){yield T(a.message??"Upstream error","server_error",a.code??null),yield{data:"[DONE]"};return}}}catch(a){yield T(a.message),yield{data:"[DONE]"};return}if(h===null){yield T("Upstream stream ended before completion"),yield{data:"[DONE]"};return}const v=M(h.usage);o&&v&&(o.upstreamUsage=v),yield _({},R(h,x>0)),s&&(yield{data:{id:r,object:"chat.completion.chunk",created:i,model:l,choices:[],...v?{usage:v}:{}}}),yield{data:"[DONE]"}}u(ne,"stream");export{E as FORMAT,Z as MISSING,Y as PROTOCOL,ee as request,te as response,ne as stream,M as usageOf};
|
|
13
|
+
//# sourceMappingURL=responses-upstream.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var s=Object.defineProperty;var n=(t,e)=>s(t,"name",{value:e,configurable:!0});import{randomBytes as u}from"node:crypto";const l="llmt-rs1:",i=64,c=1024*1024;function a(t){return`${t}_${u(12).toString("hex")}`}n(a,"itemId");function f(t){if(!(!Array.isArray(t)||t.length===0))return l+Buffer.from(JSON.stringify(t)).toString("base64url")}n(f,"pack");function d(t){if(typeof t!="string"||!t.startsWith(l)||t.length>c*2)return null;try{const e=JSON.parse(Buffer.from(t.slice(l.length),"base64url").toString("utf8")),o=n(r=>r!==null&&typeof r=="object"&&!Array.isArray(r),"plain");return Array.isArray(e)&&e.length<=i&&e.every(o)?e:null}catch{return null}}n(d,"unpack");function m(t){if(t===null||typeof t!="object")return null;const e=t.prompt_tokens??0,o=t.completion_tokens??0;return{input_tokens:e,input_tokens_details:{cached_tokens:t.prompt_tokens_details?.cached_tokens??0},output_tokens:o,output_tokens_details:{reasoning_tokens:t.completion_tokens_details?.reasoning_tokens??0},total_tokens:t.total_tokens??e+o}}n(m,"usageOf");function k(t){return t==="length"||t==="model_length"?{status:"incomplete",incomplete_details:{reason:"max_output_tokens"}}:t==="content_filter"?{status:"incomplete",incomplete_details:{reason:"content_filter"}}:{status:"completed"}}n(k,"endOf");function g(t,e){return{id:e.id,object:"response",created_at:e.created,status:e.status,error:e.error??null,incomplete_details:e.incomplete_details??null,...t.echo,model:e.model??t.model,output:e.output,usage:e.usage??null}}n(g,"envelope");function y(t){return{code:typeof t?.code=="string"?t.code:"server_error",message:String(t?.message??"Upstream stream failed")}}n(y,"failureOf");export{k as endOf,g as envelope,y as failureOf,a as itemId,f as pack,d as unpack,m as usageOf};
|
|
2
|
+
//# sourceMappingURL=responses.mjs.map
|
package/dist/lib/route.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var m=Object.defineProperty;var
|
|
1
|
+
var m=Object.defineProperty;var p=(e,n)=>m(e,"name",{value:n,configurable:!0});import{TranslatorError as f}from"./errors.mjs";import{order as v}from"./pool.mjs";import{baseModels as w}from"./models.mjs";function h(e,n){return(e.models??e.facts?.models??[]).includes(n)?!0:e.protocol==="anthropic.messages"&&e.models===void 0&&/^claude-/.test(n)}p(h,"serves");function k(e,n,t,o,i=!1){const c=Object.hasOwn(e.aliases,t),r=c?e.aliases[t]:t,s=c&&Object.hasOwn(e.pins,t)?e.pins[t]:void 0,d=n.filter(a=>h(a,r)),l=n.find(a=>a.default)??n[0];if(d.length>0){const a=v(e,o,d,r,i);return{endpoint:a[0],model:r,unserved:!1,pool:a,pin:s}}const u=w(l);return u.length===1?{endpoint:l,model:u[0],unserved:!1,pool:[l],pin:s}:{endpoint:l,model:r,unserved:!0,pool:[l],pin:s}}p(k,"pick");function x(e,n,t={}){if(!n||typeof n!="object"||Array.isArray(n))throw new f("The request body must be a JSON object",{code:"invalid_body",status:400});const o=k(e.config,e.state.endpoints,n.model,e.state,t.advance===!0);if(t.endpoint!==void 0){const s=o.pool.find(l=>l.name===t.endpoint);if(!s)throw new f(`Endpoint ${t.endpoint} no longer serves ${o.model}`,{code:"endpoint_not_in_pool",status:500});const d=e.plan(s,{body:n,wire:t.wire,tokens:t.tokens});return{endpoint:s,model:o.model,requested:n.model,unserved:o.unserved,pin:o.pin,convert:d.convert,protocol:d.protocol,more:d.more,pool:[s]}}const{endpoint:i,pool:c}=o,r=e.plan(i,{body:n,wire:t.wire,tokens:t.tokens});return{endpoint:i,model:o.model,requested:n.model,unserved:o.unserved,pin:o.pin,convert:r.convert,protocol:r.protocol,more:r.more,pool:c}}p(x,"resolve");export{k as pick,x as resolve,h as serves};
|
|
2
2
|
//# sourceMappingURL=route.mjs.map
|
package/dist/lib/routes.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var N=Object.defineProperty;var l=(n,o)=>N(n,"name",{value:o,configurable:!0});import{timingSafeEqual as U}from"node:crypto";import{TranslatorError as A}from"./errors.mjs";import{messageId as z}from"./ids.mjs";import{sign as B}from"./thinking.mjs";import{CONTENT_TYPE as $,recorder as G}from"./metrics.mjs";import{cors as M,rateLimit as W}from"./guard.mjs";const F=64*1024*1024,Y={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"},T=Symbol("stalled"),j=Symbol("gone");async function K(n,o){if(!(o>0))return n;let i;const d=new Promise(p=>{i=setTimeout(p,o,T)});try{return await Promise.race([n,d])}finally{clearTimeout(i)}}l(K,"settle");async function*J(n,o,i,d){let p=!1;try{const a=o.front==="anthropic";a&&(yield{event:"message_start",data:{type:"message_start",message:{id:z(),type:"message",role:"assistant",model:i,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 h=await n,f=l(function*(){yield{event:"content_block_delta",data:{type:"content_block_delta",index:0,delta:{type:"signature_delta",signature:B()}}},yield{event:"content_block_stop",data:{type:"content_block_stop",index:0}}},"closeEarly");if(h.stream){p=!0;let u=null;for await(const g of d?d(h.stream):h.stream){if(!a){yield g;continue}if(g.event!=="message_start"){if(u===null&&g.event==="content_block_start"&&g.data.content_block.type==="thinking"){u=0;continue}u===null&&(yield*f(),u=1),yield u>0&&typeof g.data?.index=="number"?{...g,data:{...g.data,index:g.data.index+u}}:g}}return}a&&(yield*f()),yield*C(o,h.body)}finally{p||n.then(a=>a.stream?.return?.(),()=>{})}}l(J,"late");function*C(n,o){if(n.wire==="responses"){yield{event:"error",data:{type:"error",code:o.error.code,message:o.error.message,param:o.error.param}};return}if(n.front==="anthropic"){yield{event:"error",data:o};return}yield{data:o},yield{data:"[DONE]"}}l(C,"failure");async function L(n,o,i,d,p,a=200,h=Y){n.hijack();const f=n.raw;f.writeHead(a,h);try{await o.write(f,d,{front:o.front,interval:p})}catch(u){if(!i.signal.aborted){const g=u instanceof A;g||(n.log.error(u),o.emit({type:"error",kind:"route",level:"error",message:String(u?.message??u),log:!1}));const{body:y}=o.build(g?u.status:502,g?u.message:"Internal server error",g?{type:u.type,code:u.code}:{});for(const b of C(o,y))f.write(o.encode(b))}}return f.end(),n}l(L,"stream");function I(n,o){const i=Buffer.from(n),d=Buffer.from(o);return i.length===d.length&&U(i,d)}l(I,"same");function P(n){const o=/^Bearer\s+(.+)$/i.exec(n.authorization??"");return n["x-api-key"]||o?.[1]||""}l(P,"keyOf");function Q(n){const o={};for(const[i,d]of Object.entries(n))(i==="anthropic-beta"||i==="anthropic-version"||i.startsWith("x-claude-code-"))&&(o[i]=String(d));return o}l(Q,"forwardable");function V(n){if(n===!0)return{ok:!0,context:void 0};if(n&&typeof n=="object"&&n.ok===!0)return{ok:!0,context:n.context,cacheSalt:n.cacheSalt};const o=n&&typeof n=="object"&&n.ok===!1?n:{};return{ok:!1,status:Number.isInteger(o.status)&&o.status>=400&&o.status<=599?o.status:401,message:typeof o.message=="string"&&o.message!==""?o.message:"unauthorized"}}l(V,"verdictOf");function re(n,o,i){const{front:d,filter:p}=i,a={...i,build:l((e,t,r={})=>i.build(e,t,{...r,front:d}),"build"),send:l((e,t)=>i.send(e,{...t,front:d,filter:p}),"send"),route:l(e=>i.route(e,{front:d}),"route"),count:l((e,t)=>i.count(e,{...t,front:d}),"count"),embed:l((e,t)=>i.embed(e,t),"embed"),list:l(e=>i.list({...e,front:d}),"list"),get:l((e,t)=>i.get(e,{...t,front:d}),"get")},h=[o.apiKey??[]].flat().filter(Boolean),f=o.bodyLimit??F,u=new WeakMap,g=new WeakMap,y=o.metrics===!0?G({front:d,endpoints:i.endpoints}):null,b=l((e,t)=>e.code(t.status).headers(t.headers).send(t.body),"reject");n.setErrorHandler((e,t,r)=>{if(e instanceof A)return b(r,a.build(e.status,e.message,{type:e.type,code:e.code,param:e.param}));const s=e.statusCode??500;return s<500?b(r,a.build(s,e.message)):(t.log.error(e),a.emit({type:"error",kind:"route",level:"error",message:String(e.message),status:s,log:!1}),b(r,a.build(s,"Internal server error")))});const x=M(o.cors),O=W(o.rateLimit);x&&n.addHook("onRequest",async(e,t)=>{for(const[r,s]of Object.entries(x.headers(e.headers.origin)))t.raw.setHeader(r,s)}),y&&n.addHook("onRequest",y.hook),O&&n.addHook("onRequest",async(e,t)=>{if(e.routeOptions.config?.public||e.routeOptions.config?.metrics)return;const r=P(e.headers),s=O.take(r&&h.some(k=>I(r,k))?`key:${r}`:`ip:${e.ip}`);if(s===0)return;const c=a.build(429,"rate limit exceeded",{type:"rate_limit_error"});return t.code(429).headers({...c.headers,"retry-after":String(s)}).send(c.body)}),x&&n.route({method:"OPTIONS",url:"/*",config:{public:!0,metrics:!0},handler:l((e,t)=>{const r=x.preflight(e.headers.origin,e.headers["access-control-request-headers"]);for(const[s,c]of Object.entries(r))t.raw.setHeader(s,c);return t.code(204).send()},"handler")}),n.addHook("onRequest",async(e,t)=>{if(h.length===0||e.routeOptions.config?.public)return;const r=P(e.headers);if(!(r&&h.some(s=>I(r,s))))return b(t,a.build(401,"invalid x-api-key"))}),typeof o.authorize=="function"&&n.addHook("preHandler",async(e,t)=>{if(e.routeOptions.config?.public)return;let r=null;if(e.body&&typeof e.body=="object"&&!Array.isArray(e.body))try{r=a.route(e.body)}catch{}let s;try{s=V(await o.authorize({method:e.method,url:e.url,headers:e.headers,body:e.body,front:a.front,route:r}))}catch(c){e.log.error(c),a.emit({type:"error",kind:"route",level:"error",message:String(c?.message??c),log:!1}),s={ok:!1,status:401,message:"unauthorized"}}if(!s.ok)return b(t,a.build(s.status,s.message));u.set(e,s.context),typeof s.cacheSalt=="string"&&s.cacheSalt!==""&&g.set(e,s.cacheSalt)}),n.route({method:["GET","HEAD"],url:"/",config:{public:!0},handler:l(async(e,t)=>t.send({ok:!0}),"handler")}),n.route({method:["GET","HEAD"],url:"/api/hello",config:{public:!0},handler:l(async(e,t)=>t.send({ok:!0}),"handler")}),y&&n.get("/metrics",{config:{metrics:!0}},async(e,t)=>t.header("content-type",$).send(y.render())),n.get("/v1/models",async(e,t)=>{const r=new AbortController;return t.raw.on("close",()=>r.abort()),a.list({signal:r.signal})}),n.get("/v1/models/*",async(e,t)=>{const r=new AbortController;return t.raw.on("close",()=>r.abort()),a.get(e.params["*"],{signal:r.signal})});const S={...a,wire:"responses"},H=l(async(e,t,r,s)=>{if(t.raw.destroyed)return t;const c=new AbortController;t.raw.on("close",()=>{t.raw.writableFinished||c.abort()});const k=typeof o.onUsage=="function"?w=>o.onUsage(w,u.get(e)):void 0,_=a.interval(r),v=a.send(r,{signal:c.signal,wire:s?"responses":void 0,clientHeaders:Q(e.headers),cacheSalt:g.get(e),onUsage:k,log:e.log});v.catch(()=>{}),y?.attach(e,v);const D=new Promise(w=>c.signal.addEventListener("abort",()=>w(j),{once:!0})),m=await K(Promise.race([v,D]),r?.stream===!0?_:0);if(m===j)return v.then(w=>w.stream?.return?.(),()=>{}),t;const E=s?w=>a.responses.stream(w,s.state,s.settings):void 0;return m===T?L(t,s?S:a,c,J(v,s?S:a,r?.model,E),_):m.stream?L(t,s?S:a,c,E?E(m.stream):m.stream,_,m.status,m.headers):!s||m.status>=400?b(t,m):b(t,{...m,body:await a.responses.response(m.body,s.state,s.settings)})},"converse"),R=a.front==="anthropic"?"/v1/messages":"/v1/chat/completions";if(n.post(R,{bodyLimit:f},(e,t)=>H(e,t,e.body,null)),a.front==="openai"){const e=l(async t=>{const r=a.settings(t.body),{body:s,state:c}=await a.responses.request(t.body,r);return c.dropped.length>0&&a.emit({type:"dropped",level:"debug",message:`Responses request: dropped ${c.dropped.join(", ")}`,fields:[...c.dropped],log:t.log}),{chat:s,via:{state:c,settings:r}}},"converted");n.post("/v1/responses",{bodyLimit:f},async(t,r)=>{const{chat:s,via:c}=await e(t);return H(t,r,s,c)}),n.post("/v1/responses/input_tokens",{bodyLimit:f},async(t,r)=>{const s=new AbortController;r.raw.on("close",()=>s.abort());const{chat:c}=await e(t),{input_tokens:k}=await a.count(c,{signal:s.signal});return r.send({object:"response.input_tokens",input_tokens:k})}),n.post("/v1/embeddings",{bodyLimit:f},async(t,r)=>{const s=new AbortController;r.raw.on("close",()=>{r.raw.writableFinished||s.abort()});const c=typeof o.onUsage=="function"?_=>o.onUsage(_,u.get(t)):void 0,k=a.embed(t.body,{signal:s.signal,onUsage:c,log:t.log});return y?.attach(t,k),b(r,await k)})}a.front==="anthropic"&&n.post("/v1/messages/count_tokens",{bodyLimit:f},async(e,t)=>{const r=new AbortController;return t.raw.on("close",()=>r.abort()),a.count(e.body,{signal:r.signal})})}l(re,"register");export{J as late,re as register};
|
|
2
2
|
//# sourceMappingURL=routes.mjs.map
|
package/dist/lib/settings.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var c=Object.defineProperty;var l=(o,e)=>c(o,"name",{value:e,configurable:!0});import{TranslatorError as n}from"./errors.mjs";import{check as f}from"./constraint.mjs";import{valid as d}from"./cache.mjs";const r=Object.freeze({inlineSystem:"auto",stripBillingHeader:!0,reasoningField:"reasoning",thinkTags:!0,thinkOpen:!1,reasoningRoundTrip:!1,reasoningSummary:"auto",thinking:"chat_template_kwargs",thinkingBudget:!1,effort:!1,images:!0,imageFallback:!0,toolFallback:!1,documents:"extract",documentConverter:null,pdf:Object.freeze({pages:!0,maxPages:100,maxBytes:32*1024*1024,maxImages:20,scale:1.5}),toolSchema:"pass",deferredTools:"forward",toolSearch:"expand",jsonObject:"schema",jsonObjectFallback:!0,toolErrors:!0,toolErrorMarker:"[error] ",toolErrorInference:!1,maxTokensField:"max_completion_tokens",streamUsage:!0,continuousUsage:!1,prefill:"continue_final_message",topK:!0,responseFormat:!0,parallelToolCalls:!0,unknownFields:"drop",sampling:Object.freeze({}),constraint:null,cacheSalt:null,promptCacheKey:null,pingInterval:1e4,toolStreaming:"buffer",effortPassthrough:!1,passthroughFilter:!1,nativeMessages:!1,contextWindow:0,defaultMaxTokens:8192}),g={inlineSystem:["auto","keep","hoist","user","drop"],reasoningField:["reasoning","reasoning_content"],reasoningSummary:["auto","concise","detailed","none"],thinking:["chat_template_kwargs","reasoning_effort","none"],documents:["extract","note","error"],toolSchema:["pass","sanitize"],deferredTools:["forward","drop"],toolSearch:["expand","inline","native"],jsonObject:["schema","instruction","drop"],maxTokensField:["max_completion_tokens","max_tokens"],prefill:["continue_final_message","none"],unknownFields:["drop","error"],toolStreaming:["buffer","stream"]},p=["effortPassthrough","passthroughFilter","imageFallback","toolFallback","jsonObjectFallback","thinkOpen","reasoningRoundTrip","toolErrorInference"];function y(...o){const e={...r,pdf:{...r.pdf},sampling:{...r.sampling}};for(const t of o)if(t)for(const[a,s]of Object.entries(t))s!==void 0&&(a==="pdf"?Object.assign(e.pdf,s):a==="sampling"&&s!==null&&typeof s=="object"&&!Array.isArray(s)?Object.assign(e.sampling,s):e[a]=s);for(const[t,a]of Object.entries(g))if(!a.includes(e[t]))throw new n(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected one of ${a.join(", ")})`,{code:"invalid_setting",status:500,details:{key:t,allowed:a}});for(const t of p)if(typeof e[t]!="boolean")throw new n(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected true or false)`,{code:"invalid_setting",status:500,details:{key:t}});if(e.nativeMessages!==!0&&e.nativeMessages!==!1&&e.nativeMessages!=="auto")throw new n(`Invalid setting nativeMessages: ${JSON.stringify(e.nativeMessages)} (expected true, false or "auto")`,{code:"invalid_setting",status:500,details:{key:"nativeMessages"}});if(typeof e.toolErrorMarker!="string"||e.toolErrorMarker.trim()==="")throw new n(`Invalid setting toolErrorMarker: ${JSON.stringify(e.toolErrorMarker)} (expected a non-empty string)`,{code:"invalid_setting",status:500,details:{key:"toolErrorMarker"}});if(e.sampling===null||typeof e.sampling!="object"||Array.isArray(e.sampling))throw new n(`Invalid setting sampling: ${JSON.stringify(e.sampling)} (expected an object of field names and values)`,{code:"invalid_setting",status:500,details:{key:"sampling"}});const i=f(e.constraint);if(i!==null)throw new n(`Invalid setting constraint: ${JSON.stringify(e.constraint)} (${i})`,{code:"invalid_setting",status:500,details:{key:"constraint"}});for(const t of["cacheSalt","promptCacheKey"])if(!d(e[t]))throw new n(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected a non-empty string or null)`,{code:"invalid_setting",status:500,details:{key:t}});if(!(Number.isFinite(e.pdf.scale)&&e.pdf.scale>0))throw new n(`Invalid setting pdf.scale: ${JSON.stringify(e.pdf.scale)} (expected a positive number)`,{code:"invalid_setting",status:500,details:{key:"pdf.scale"}});return e}l(y,"resolve");export{r as DEFAULTS,y as resolve};
|
|
2
2
|
//# sourceMappingURL=settings.mjs.map
|
package/dist/lib/vllm.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var c=Object.defineProperty;var s=(t,
|
|
1
|
+
var c=Object.defineProperty;var s=(t,e)=>c(t,"name",{value:e,configurable:!0});import{atLeast as l,semver as f}from"./normalize.mjs";import{sign as a}from"./thinking.mjs";const y="0.11.1",p="x-anthropic-billing-header:";function A(t){const e=t.facts?.version??t.version;return f(e)!==null&&l(e,y)}s(A,"eligible");function*u(t){for(const e of t)if(Array.isArray(e?.content))for(const r of e.content)yield r,r?.type==="tool_result"&&Array.isArray(r.content)&&(yield*r.content)}s(u,"blocks");function h(t,e){const r=Array.isArray(t.messages)?t.messages:[];if(r.at(-1)?.role==="assistant")return"prefill";const n=[...u(r)];return n.some(o=>o?.type==="document")?"document":e.toolSearch!=="native"&&(Array.isArray(t.tools)&&t.tools.some(i=>i?.defer_loading!==void 0)||n.some(i=>["tool_reference","tool_addition","tool_removal"].includes(i?.type)))?"toolsearch":null}s(h,"unsuited");function d(t,e){return(typeof t=="string"?[{type:"text",text:t}]:t).filter(n=>n?.type==="text"&&typeof n.text=="string").filter(n=>!(e.stripBillingHeader&&n.text.trimStart().startsWith(p))).map(n=>n.text).join(`
|
|
2
2
|
|
|
3
|
-
`)}s(d,"systemText");function m(t,
|
|
3
|
+
`)}s(d,"systemText");function m(t,e){const r=e.toolErrorMarker;return typeof t.content=="string"?{...t,content:`${r}${t.content}`}:Array.isArray(t.content)?{...t,content:[{type:"text",text:r.trimEnd()},...t.content]}:{...t,content:r.trimEnd()}}s(m,"marked");function v(t,e){const r={...t};if(r.system!==void 0&&r.system!==null){const o=d(r.system,e);o===""?delete r.system:r.system=o}const n=s(o=>o?.type==="tool_result"&&o.is_error===!0,"failed");return e.toolErrors&&Array.isArray(r.messages)&&r.messages.some(o=>Array.isArray(o?.content)&&o.content.some(n))&&(r.messages=r.messages.map(o=>Array.isArray(o?.content)&&o.content.some(n)?{...o,content:o.content.map(i=>n(i)?m(i,e):i)}:o)),r}s(v,"request");function E(t){return Array.isArray(t?.content)?{...t,content:t.content.map(e=>e?.type==="thinking"&&typeof e.signature!="string"?{...e,signature:a()}:e)}:t}s(E,"response");async function*I(t){const e=new Map;for await(const r of t){const n=r.data;n?.type==="content_block_start"&&n.content_block?.type==="thinking"?e.set(n.index,typeof n.content_block.signature=="string"):n?.type==="content_block_delta"&&n.delta?.type==="signature_delta"?e.set(n.index,!0):n?.type==="content_block_stop"&&e.get(n.index)===!1&&(yield{event:"content_block_delta",data:{type:"content_block_delta",index:n.index,delta:{type:"signature_delta",signature:a()}}}),n?.type==="content_block_stop"&&e.delete(n.index),yield r,n?.type==="message_start"&&(yield{event:"ping",data:{type:"ping"}})}}s(I,"stream");export{y as MIN_VERSION,A as eligible,v as request,E as response,I as stream,h as unsuited};
|
|
4
4
|
//# sourceMappingURL=vllm.mjs.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cldmv/llm-translator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "Bidirectional Anthropic Messages ⇄ OpenAI Chat Completions translator with backend auto-detection (vLLM and other OpenAI-compatible servers), built on slothlet, with an optional standalone server for running Claude Code against local models.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert an Anthropic Messages request body into a Responses request body (for an Anthropic-protocol client talking to an upstream on the
|
|
3
|
+
* Responses API). The request goes through the Anthropic to Chat Completions converter first, so tool search, documents, thinking,
|
|
4
|
+
* reasoning round trips and every other conversion apply as they do for Chat Completions, and the result is then written as a Responses
|
|
5
|
+
* body (see `lib/responses-upstream.mjs`). The state is the Chat Completions one, which the reply converters read, with the fields the
|
|
6
|
+
* second step dropped added to `dropped`.
|
|
7
|
+
* @param {any} body - Anthropic `POST /v1/messages` body.
|
|
8
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
9
|
+
* @param {any} [context] - See the Anthropic to Chat Completions converter.
|
|
10
|
+
* @returns {Promise<{ body: any, state: any }>} Responses request body and conversion state.
|
|
11
|
+
* @throws {import("../../../../../../../lib/errors.mjs").TranslatorError} As the Anthropic to Chat Completions converter, and for fields the Responses API lacks when configured to error.
|
|
12
|
+
*/
|
|
13
|
+
export function request(body: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>, context?: any): Promise<{
|
|
14
|
+
body: any;
|
|
15
|
+
state: any;
|
|
16
|
+
}>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a Chat Completions request body into a Responses request body (for an OpenAI-protocol client talking to an upstream on the
|
|
3
|
+
* Responses API). See `lib/responses-upstream.mjs` for the mapping.
|
|
4
|
+
* @param {any} chatBody - OpenAI `POST /v1/chat/completions` body.
|
|
5
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
6
|
+
* @returns {Promise<{ body: any, state: any }>} Responses request body and conversion state.
|
|
7
|
+
* @throws {import("../../../../../../../lib/errors.mjs").TranslatorError} For malformed requests, `n` above 1 (`send` fans that out instead), unknown roles, or unknown fields when configured to error.
|
|
8
|
+
*/
|
|
9
|
+
export function request(chatBody: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): Promise<{
|
|
10
|
+
body: any;
|
|
11
|
+
state: any;
|
|
12
|
+
}>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a (non-streaming) Chat Completions reply into a Responses object.
|
|
3
|
+
*
|
|
4
|
+
* The output holds, in this order, a `reasoning` item (the reply's reasoning text as one summary part, and with `reasoningRoundTrip` its
|
|
5
|
+
* `reasoning_details` as `encrypted_content`), a `message` item with an `output_text` part (or a `refusal` part) and one `function_call`
|
|
6
|
+
* item per tool call. A `length` stop makes the response `incomplete` with reason `max_output_tokens`, a content filter one with reason
|
|
7
|
+
* `content_filter`.
|
|
8
|
+
* @param {any} completion - Chat Completions `chat.completion` object.
|
|
9
|
+
* @param {any} state - State from the request converter (`model`, `echo`).
|
|
10
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
11
|
+
* @returns {any} Responses object.
|
|
12
|
+
* @throws {TranslatorError} When the completion has no choices.
|
|
13
|
+
*/
|
|
14
|
+
export function response(completion: any, state: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): any;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a Chat Completions chunk stream into Responses events.
|
|
3
|
+
*
|
|
4
|
+
* `source` is SSE text/bytes, an iterable of `{ data }` messages, or bare chunk objects. The result yields `{ event, data }` pairs
|
|
5
|
+
* (`data.type` repeats the event name, `data.sequence_number` counts from 0): `response.created` and `response.in_progress` first, then for each output
|
|
6
|
+
* item `response.output_item.added`, its part and delta events, and `response.output_item.done`, and last `response.completed` (or
|
|
7
|
+
* `response.incomplete` after a length stop or a content filter). Output items come in the order the deltas start them: reasoning
|
|
8
|
+
* (as a summary part), the message text (or refusal) and one function call per tool call index. An `error` chunk, a fatal finish reason or
|
|
9
|
+
* a stream that ends without a finish reason ends with `response.failed`. There is no `[DONE]` sentinel.
|
|
10
|
+
* @param {ReadableStream|AsyncIterable<unknown>|Iterable<unknown>|string|Uint8Array} source - Chat Completions stream.
|
|
11
|
+
* @param {any} state - State from the request converter (`model`, `echo`).
|
|
12
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
13
|
+
* @returns {AsyncGenerator<{ event: string, data: any }, void, undefined>} Responses events.
|
|
14
|
+
*/
|
|
15
|
+
export function stream(source: ReadableStream | AsyncIterable<unknown> | Iterable<unknown> | string | Uint8Array, state: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): AsyncGenerator<{
|
|
16
|
+
event: string;
|
|
17
|
+
data: any;
|
|
18
|
+
}, void, undefined>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a (non-streaming) Responses reply into an Anthropic message: read as a Chat Completions reply first, then converted from there.
|
|
3
|
+
* @param {any} reply - Responses object.
|
|
4
|
+
* @param {any} [state] - State from the request converter.
|
|
5
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
6
|
+
* @returns {any} Anthropic message.
|
|
7
|
+
*/
|
|
8
|
+
export function response(reply: any, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): any;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a Responses event stream into Anthropic events: read as Chat Completions chunks first, then converted from there.
|
|
3
|
+
* @param {ReadableStream|AsyncIterable<unknown>|Iterable<unknown>|string|Uint8Array} source - Responses stream.
|
|
4
|
+
* @param {any} [state] - State from the request converter.
|
|
5
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
6
|
+
* @returns {AsyncGenerator<any, void, undefined>} Anthropic events.
|
|
7
|
+
*/
|
|
8
|
+
export function stream(source: ReadableStream | AsyncIterable<unknown> | Iterable<unknown> | string | Uint8Array, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): AsyncGenerator<any, void, undefined>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert an OpenAI Responses request body into a Chat Completions request body, the pivot every upstream is reached through.
|
|
3
|
+
*
|
|
4
|
+
* The core is stateless: `previous_response_id`, `conversation`, `store: true`, `background: true` and stored prompts are refused, and so are hosted tools,
|
|
5
|
+
* non-function `tool_choice` values and input items other than messages, function calls, function call outputs and reasoning items.
|
|
6
|
+
* `instructions` becomes the first system message. Reasoning items are replayed to the upstream only with `reasoningRoundTrip` (their
|
|
7
|
+
* `encrypted_content` carries the `reasoning_details` the converter put there); otherwise they are dropped. Fields without a mapping are
|
|
8
|
+
* dropped and listed in `state.dropped`, or refused when `unknownFields` is `"error"`.
|
|
9
|
+
* @param {any} body - OpenAI `POST /v1/responses` body.
|
|
10
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
11
|
+
* @returns {{ body: any, state: any }} Chat Completions body and the state the reply converters need (`model`, `stream`, `echo`, `dropped`).
|
|
12
|
+
* @throws {TranslatorError} `invalid_request` for a malformed body, `unsupported_field` for stateful parameters and unmapped fields under `unknownFields: "error"`, `unsupported_tool` and `unsupported_content` for what has no counterpart.
|
|
13
|
+
*/
|
|
14
|
+
export function request(body: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): {
|
|
15
|
+
body: any;
|
|
16
|
+
state: any;
|
|
17
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a (non-streaming) Responses reply into a Chat Completions reply. See `lib/responses-upstream.mjs` for the mapping.
|
|
3
|
+
* @param {any} reply - Responses object.
|
|
4
|
+
* @param {any} [state] - State from the request converter.
|
|
5
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
6
|
+
* @returns {any} Chat completion.
|
|
7
|
+
*/
|
|
8
|
+
export function response(reply: any, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): any;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a Responses event stream into Chat Completions chunks followed by `[DONE]`. See `lib/responses-upstream.mjs`.
|
|
3
|
+
* @param {ReadableStream|AsyncIterable<unknown>|Iterable<unknown>|string|Uint8Array} source - Responses stream.
|
|
4
|
+
* @param {any} [state] - State from the request converter.
|
|
5
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
6
|
+
* @returns {AsyncGenerator<{ data: any|string }, void, undefined>} Chat chunks.
|
|
7
|
+
*/
|
|
8
|
+
export function stream(source: ReadableStream | AsyncIterable<unknown> | Iterable<unknown> | string | Uint8Array, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): AsyncGenerator<{
|
|
9
|
+
data: any | string;
|
|
10
|
+
}, void, undefined>;
|
|
@@ -18,11 +18,12 @@
|
|
|
18
18
|
*/
|
|
19
19
|
/**
|
|
20
20
|
* Describe the Anthropic driver (native Messages API upstream).
|
|
21
|
-
* @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, hosts: string[], probes: string[], headers: Record<string,string>, paths: Record<string,string>}} Driver facts.
|
|
21
|
+
* @returns {{name: string, protocol: string, protocols: string[], order: number, auth: string, strict: boolean, hosts: string[], probes: string[], headers: Record<string,string>, paths: Record<string,string>}} Driver facts.
|
|
22
22
|
*/
|
|
23
23
|
export function info(): {
|
|
24
24
|
name: string;
|
|
25
25
|
protocol: string;
|
|
26
|
+
protocols: string[];
|
|
26
27
|
order: number;
|
|
27
28
|
auth: string;
|
|
28
29
|
strict: boolean;
|
|
@@ -19,11 +19,12 @@
|
|
|
19
19
|
/**
|
|
20
20
|
* Describe the Azure OpenAI driver. The key goes in the `api-key` header. The real paths come from the `path` leaf, because they depend on the
|
|
21
21
|
* upstream's `apiVersion` and `deployment`; `paths` holds the v1 API paths for a base URL without `/openai`.
|
|
22
|
-
* @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, hostSuffixes: string[], paths: Record<string,string>}} Driver facts.
|
|
22
|
+
* @returns {{name: string, protocol: string, protocols: string[], order: number, auth: string, strict: boolean, hostSuffixes: string[], paths: Record<string,string>}} Driver facts.
|
|
23
23
|
*/
|
|
24
24
|
export function info(): {
|
|
25
25
|
name: string;
|
|
26
26
|
protocol: string;
|
|
27
|
+
protocols: string[];
|
|
27
28
|
order: number;
|
|
28
29
|
auth: string;
|
|
29
30
|
strict: boolean;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Describe the generic OpenAI-compatible driver (matches last, after every specific driver).
|
|
3
|
-
* @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, fields: string[], paths: Record<string,string>}} Driver facts.
|
|
3
|
+
* @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, protocols: string[], fields: string[], paths: Record<string,string>}} Driver facts.
|
|
4
4
|
*/
|
|
5
5
|
export function info(): {
|
|
6
6
|
name: string;
|
|
@@ -8,6 +8,7 @@ export function info(): {
|
|
|
8
8
|
order: number;
|
|
9
9
|
auth: string;
|
|
10
10
|
strict: boolean;
|
|
11
|
+
protocols: string[];
|
|
11
12
|
fields: string[];
|
|
12
13
|
paths: Record<string, string>;
|
|
13
14
|
};
|
|
@@ -18,11 +18,12 @@
|
|
|
18
18
|
*/
|
|
19
19
|
/**
|
|
20
20
|
* Describe the DeepSeek driver.
|
|
21
|
-
* @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, hosts: string[], paths: Record<string,string>}} Driver facts.
|
|
21
|
+
* @returns {{name: string, protocol: string, protocols: string[], order: number, auth: string, strict: boolean, hosts: string[], paths: Record<string,string>}} Driver facts.
|
|
22
22
|
*/
|
|
23
23
|
export function info(): {
|
|
24
24
|
name: string;
|
|
25
25
|
protocol: string;
|
|
26
|
+
protocols: string[];
|
|
26
27
|
order: number;
|
|
27
28
|
auth: string;
|
|
28
29
|
strict: boolean;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Describe the Fireworks AI driver (see `PRESETS.fireworks` in `lib/presets.mjs`).
|
|
3
|
-
* @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, fields?: string[], paths: Record<string,string>}} Driver facts.
|
|
3
|
+
* @returns {{name: string, protocol: string, protocols: string[], order: number, auth: string, strict: boolean, fields?: string[], paths: Record<string,string>}} Driver facts.
|
|
4
4
|
*/
|
|
5
5
|
export function info(): {
|
|
6
6
|
name: string;
|
|
7
7
|
protocol: string;
|
|
8
|
+
protocols: string[];
|
|
8
9
|
order: number;
|
|
9
10
|
auth: string;
|
|
10
11
|
strict: boolean;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Describe the Gemini (OpenAI-compatible endpoint) driver (see `PRESETS.gemini` in `lib/presets.mjs`).
|
|
3
|
-
* @returns {{name: string, protocol: string, order: number, auth: string, strict: boolean, fields?: string[], paths: Record<string,string>}} Driver facts.
|
|
3
|
+
* @returns {{name: string, protocol: string, protocols: string[], order: number, auth: string, strict: boolean, fields?: string[], paths: Record<string,string>}} Driver facts.
|
|
4
4
|
*/
|
|
5
5
|
export function info(): {
|
|
6
6
|
name: string;
|
|
7
7
|
protocol: string;
|
|
8
|
+
protocols: string[];
|
|
8
9
|
order: number;
|
|
9
10
|
auth: string;
|
|
10
11
|
strict: boolean;
|