@cldmv/llm-translator 0.12.0 → 0.14.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 -8
- package/dist/api/convert/anthropic/messages/to/bedrock/converse/request.mjs +2 -0
- package/dist/api/convert/anthropic/messages/to/openai/responses/request.mjs +2 -0
- package/dist/api/convert/bedrock/converse/to/anthropic/messages/response.mjs +2 -0
- package/dist/api/convert/bedrock/converse/to/anthropic/messages/stream.mjs +2 -0
- package/dist/api/convert/bedrock/converse/to/openai/chat/response.mjs +2 -0
- package/dist/api/convert/bedrock/converse/to/openai/chat/stream.mjs +2 -0
- package/dist/api/convert/openai/chat/to/bedrock/converse/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/bedrock/detect.mjs +2 -0
- package/dist/api/driver/bedrock/info.mjs +2 -0
- package/dist/api/driver/bedrock/model/list.mjs +2 -0
- package/dist/api/driver/bedrock/path.mjs +2 -0
- package/dist/api/driver/bedrock/request.mjs +2 -0
- package/dist/api/driver/bedrock/sign.mjs +2 -0
- 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/vertex/auth.mjs +2 -0
- package/dist/api/driver/vertex/detect.mjs +2 -0
- package/dist/api/driver/vertex/error.mjs +2 -0
- package/dist/api/driver/vertex/info.mjs +2 -0
- package/dist/api/driver/vertex/path.mjs +2 -0
- package/dist/api/driver/vertex/request.mjs +2 -0
- package/dist/api/driver/vertex/settings.mjs +2 -0
- package/dist/api/driver/vertex/token/count.mjs +2 -0
- 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/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/bedrock.mjs +2 -0
- package/dist/lib/capture.mjs +1 -1
- package/dist/lib/config.mjs +1 -1
- package/dist/lib/converse.mjs +3 -0
- package/dist/lib/document-urls.mjs +2 -0
- package/dist/lib/documents.mjs +4 -4
- package/dist/lib/estimate.mjs +1 -1
- package/dist/lib/eventstream.mjs +2 -0
- package/dist/lib/fetch-url.mjs +2 -0
- package/dist/lib/google-auth.mjs +2 -0
- package/dist/lib/http.mjs +1 -1
- 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/sigv4.mjs +7 -0
- package/dist/lib/vertex.mjs +2 -0
- package/dist/lib/vllm.mjs +2 -2
- package/package.json +1 -1
- package/types/src/api/convert/anthropic/messages/to/bedrock/converse/request.d.mts +14 -0
- package/types/src/api/convert/anthropic/messages/to/openai/responses/request.d.mts +16 -0
- package/types/src/api/convert/bedrock/converse/to/anthropic/messages/response.d.mts +10 -0
- package/types/src/api/convert/bedrock/converse/to/anthropic/messages/stream.d.mts +14 -0
- package/types/src/api/convert/bedrock/converse/to/openai/chat/response.d.mts +10 -0
- package/types/src/api/convert/bedrock/converse/to/openai/chat/stream.d.mts +13 -0
- package/types/src/api/convert/openai/chat/to/bedrock/converse/request.d.mts +11 -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/bedrock/detect.d.mts +9 -0
- package/types/src/api/driver/bedrock/info.d.mts +36 -0
- package/types/src/api/driver/bedrock/model/list.d.mts +11 -0
- package/types/src/api/driver/bedrock/path.d.mts +9 -0
- package/types/src/api/driver/bedrock/request.d.mts +9 -0
- package/types/src/api/driver/bedrock/sign.d.mts +9 -0
- 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/vertex/auth.d.mts +7 -0
- package/types/src/api/driver/vertex/detect.d.mts +8 -0
- package/types/src/api/driver/vertex/error.d.mts +15 -0
- package/types/src/api/driver/vertex/info.d.mts +16 -0
- package/types/src/api/driver/vertex/path.d.mts +7 -0
- package/types/src/api/driver/vertex/request.d.mts +6 -0
- package/types/src/api/driver/vertex/settings.d.mts +23 -0
- package/types/src/api/driver/vertex/token/count.d.mts +7 -0
- 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 +1 -1
- package/types/src/api/status.d.mts +1 -1
- package/types/src/lib/bedrock.d.mts +74 -0
- package/types/src/lib/config.d.mts +35 -5
- package/types/src/lib/converse.d.mts +81 -0
- package/types/src/lib/document-urls.d.mts +55 -0
- package/types/src/lib/estimate.d.mts +1 -1
- package/types/src/lib/eventstream.d.mts +34 -0
- package/types/src/lib/fetch-url.d.mts +64 -0
- package/types/src/lib/google-auth.d.mts +58 -0
- package/types/src/lib/http.d.mts +5 -3
- package/types/src/lib/presets.d.mts +2 -1
- package/types/src/lib/protocol.d.mts +87 -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 +2 -1
- package/types/src/lib/settings.d.mts +19 -2
- package/types/src/lib/sigv4.d.mts +41 -0
- package/types/src/lib/vertex.d.mts +55 -0
- package/types/src/lib/vllm.d.mts +4 -4
package/dist/api/status.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var e=Object.defineProperty;var l=(s,a)=>e(s,"name",{value:a,configurable:!0});import{context as c}from"../lib/runtime.mjs";import{describe as m}from"../lib/pool.mjs";import{capture as t}from"../lib/capture.mjs";import{self as f}from"../lib/runtime.mjs";import{MESSAGES as u,RESPONSES as p,supported as v}from"../lib/protocol.mjs";function k(){const{config:s,state:a}=c,o=t(f,c);return{front:s.front,upstreams:a.endpoints.map(r=>({name:r.name,url:r.url,backend:r.backend,driver:r.driver,api:r.api,protocols:v(o.driver(r).info),protocol:o.plan(r).protocol,version:r.facts?.version??r.version,models:r.models??r.facts?.models??[],default:r.default,facts:r.facts,probed:r.probed,responses:o.plan(r,{wire:"responses"}).protocol===p,messages:r.driver==="vllm"?o.plan(r,{front:"anthropic"}).protocol===u:null,health:m(r)}))}}l(k,"status");export{k as status};
|
|
2
2
|
//# sourceMappingURL=status.mjs.map
|
package/dist/api/stream.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var O=Object.defineProperty;var c=(e,o)=>O(e,"name",{value:o,configurable:!0});import{context as S,self as x}from"../lib/runtime.mjs";import{capture as b}from"../lib/capture.mjs";import{messages as w}from"../lib/sse.mjs";import*as j from"../lib/vllm.mjs";import{RESPONSES as k,CONVERSE as A}from"../lib/protocol.mjs";import{events as B}from"../lib/eventstream.mjs";function p(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}}c(p,"restore");function D(e){return!!e&&Array.isArray(e.choices)&&(e.choices.length===0||e.choices.some(o=>o?.finish_reason))}c(D,"ends");function q(e,o){const s=b(x,S,o.front),i=s.endpoint(o.upstream),f=s.driver(i),a=s.settings(i,s.protocol(i,o)),l=o.native?null:s.converters(o),t=s.protocol(i,o)===k?void 0:f.chunk,m={endpoint:i,settings:a,state:o},d=f.info.frames==="eventstream"?B(e,o.protocol===A?"converse":"invoke"):e;async function*u(){for(const n of await f.flush?.(m)??[])yield{data:n}}if(c(u,"flushed"),l&&!t)return l.stream(d,o.convert,a);if(l)return l.stream((async function*(){for await(const n of w(d))n.data!=="[DONE]"&&(yield await t(n.data,m));for await(const n of u())yield n.data})(),o.convert,a);const E=!!(t&&f.flush),y=(async function*(){const n=[];let g=!1;for await(const r of w(d)){if(r.data==="[DONE]"){for await(const N of u())yield{data:p(N.data,o.model)};yield*n.splice(0),g=!0,yield r;continue}const h=t?await t(r.data,m):r.data,v={...r,data:p(h,o.model)};E&&D(h)?n.push(v):yield v}if(!g){for await(const r of u())yield{data:p(r.data,o.model)};yield*n}})();return o.messages?j.stream(y):y}c(q,"stream");export{q as stream};
|
|
2
2
|
//# sourceMappingURL=stream.mjs.map
|
package/dist/api/token/count.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var u=Object.defineProperty;var
|
|
1
|
+
var u=Object.defineProperty;var s=(n,t)=>u(n,"name",{value:t,configurable:!0});import{context as p,self as a}from"../../lib/runtime.mjs";import{capture as m}from"../../lib/capture.mjs";import{estimate as f}from"../../lib/estimate.mjs";async function b(n,t={}){const e=m(a,p,t.front),{request:i}=a,o=await i(n,{front:t.front,tokens:!0}),r=e.endpoint(o.upstream),c=e.driver(r);return{input_tokens:(c.count?await c.count({endpoint:r,request:o.body,transport:e.state.transport,signal:t.signal,timeout:e.config.detectTimeout}):null)??f(o.body)}}s(b,"count");export{b as count};
|
|
2
2
|
//# sourceMappingURL=count.mjs.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var i=Object.defineProperty;var a=(r,e)=>i(r,"name",{value:e,configurable:!0});import{fileURLToPath as d}from"node:url";import p from"@cldmv/slothlet";import{normalize as c}from"./lib/config.mjs";import{load as l}from"./lib/pdf.mjs";import{transport as
|
|
1
|
+
var i=Object.defineProperty;var a=(r,e)=>i(r,"name",{value:e,configurable:!0});import{fileURLToPath as d}from"node:url";import p from"@cldmv/slothlet";import{normalize as c}from"./lib/config.mjs";import{load as l}from"./lib/pdf.mjs";import{transport as m}from"./lib/transport.mjs";import{create as f,open as u}from"./lib/events.mjs";import{warn as s}from"./lib/warn.mjs";async function w(r){const e=c(r),n={endpoints:e.endpoints,transport:e.transport??m,servers:new Set,warned:new Set,events:f(e.onEvent),textOnly:new Set,jsonInstruction:new Set,missing:new Map,cursors:new Map,front:void 0};u(n.events,e.logs);const t=await l();t.errors.canvas&&s(e,n,`@napi-rs/canvas could not be loaded (${t.errors.canvas}); PDFs will be read as text only, without page images`,{type:"detect",kind:"canvas",reason:String(t.errors.canvas)}),t.errors.unpdf&&s(e,n,`unpdf could not be loaded (${t.errors.unpdf}); PDF documents cannot be read and fall back to a note`,{type:"detect",kind:"unpdf",reason:String(t.errors.unpdf)});const o=await p({...r.slothlet,base:d(new URL("./api",import.meta.url)),mode:"eager",context:{config:e,state:n},silent:!0});return await o.detect({network:e.detect}),o}a(w,"createTranslator");import{TranslatorError as D}from"./lib/errors.mjs";var P=w;export{D as TranslatorError,w as createTranslator,P as default};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var _=Object.defineProperty;var t=(e,r)=>_(e,"name",{value:r,configurable:!0});import{TranslatorError as c}from"./errors.mjs";import{sign as u}from"./sigv4.mjs";const A=/(?:^|\.)bedrock-runtime(?:-fips)?\.([a-z0-9-]+)\.(?:vpce\.)?amazonaws\.com(?:\.cn)?$/,S=/(?:^|[/:])(?:[a-z-]+\.)?anthropic\.claude/;function m(e){return typeof e=="string"&&S.test(e)}t(m,"claude");function K(e){return A.exec(e)?.[1]??null}t(K,"regionOfHost");function i(e,r=process.env){const s=URL.canParse(e.url??"")?new URL(e.url).hostname:"";return e.region??K(s)??r.AWS_REGION??r.AWS_DEFAULT_REGION??null}t(i,"regionOf");function d(e,r=process.env){const s=i(e,r);if(s===null)throw new c("a bedrock upstream needs a url or a region (the region setting, AWS_REGION or AWS_DEFAULT_REGION)",{code:"INVALID_CONFIG",status:500});return`https://bedrock-runtime.${s}.amazonaws.com${s.startsWith("cn-")?".cn":""}`}t(d,"defaultUrl");function W(e){return`/model/${encodeURIComponent(e)}`}t(W,"modelPath");function l(e,r=process.env){return e.apiKey?{bearer:e.apiKey}:e.accessKeyId&&e.secretAccessKey?{keys:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey,sessionToken:e.sessionToken}}:r.AWS_BEARER_TOKEN_BEDROCK?{bearer:r.AWS_BEARER_TOKEN_BEDROCK}:r.AWS_ACCESS_KEY_ID&&r.AWS_SECRET_ACCESS_KEY?{keys:{accessKeyId:r.AWS_ACCESS_KEY_ID,secretAccessKey:r.AWS_SECRET_ACCESS_KEY,sessionToken:r.AWS_SESSION_TOKEN||void 0}}:null}t(l,"credential");function y(e,r,{env:s=process.env,date:E}={}){const o=l(e,s);if(o===null)throw new c(`Upstream ${e.name}: no AWS credentials; set apiKey (a Bedrock API key), accessKeyId and secretAccessKey, or AWS_BEARER_TOKEN_BEDROCK, or AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY`,{code:"missing_credentials",status:500});const{authorization:f,...n}=r.headers;if("bearer"in o)return{...n,authorization:`Bearer ${o.bearer}`};const a=i(e,s);if(a===null)throw new c(`Upstream ${e.name}: no AWS region; set region, or AWS_REGION or AWS_DEFAULT_REGION`,{code:"missing_region",status:500});return u({...r,headers:n,region:a,service:"bedrock",...o.keys,date:E}).headers}t(y,"authenticate");export{y as authenticate,m as claude,l as credential,d as defaultUrl,W as modelPath,i as regionOf,K as regionOfHost};
|
|
2
|
+
//# sourceMappingURL=bedrock.mjs.map
|
package/dist/lib/capture.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var H=Object.defineProperty;var s=(o,c)=>H(o,"name",{value:c,configurable:!0});import{resolve as y}from"./settings.mjs";import*as A from"./vllm.mjs";import{MESSAGES as i,CHAT as V,RESPONSES as C,OLLAMA as z,CONVERSE as R,candidates as B,frontOf as D}from"./protocol.mjs";import{claude as I}from"./bedrock.mjs";function J(o){const c=[];for(const a of Object.keys(o??{})){const t=o[a];typeof t?.info=="function"&&c.push({name:a,info:t.info(),detect:t.detect,settings:t.settings,probe:t.probe,path:t.path,auth:t.auth,request:t.request,sign:t.sign,response:t.response,chunk:t.chunk,flush:t.flush,error:t.error,count:t.token?.count,list:t.model?.list})}return c.sort((a,t)=>a.info.order-t.info.order)}s(J,"collect");function X(o,c,a){const{config:t,state:p}=c,u=o.convert.anthropic.messages.to.openai.chat,m=o.convert.openai.chat.to.anthropic.messages,x={anthropic:o.convert.anthropic.messages.to.ollama.chat,openai:o.convert.openai.chat.to.ollama.chat},d={anthropic:o.convert.ollama.chat.to.anthropic.messages,openai:o.convert.ollama.chat.to.openai.chat},L={anthropic:o.convert.anthropic.messages.to.bedrock.converse,openai:o.convert.openai.chat.to.bedrock.converse},g={anthropic:o.convert.bedrock.converse.to.anthropic.messages,openai:o.convert.bedrock.converse.to.openai.chat},n=a??t.front,h=J(o.driver),M=n==="anthropic"?i:V,N=n==="anthropic"?{request:u.request,response:m.response,stream:m.stream}:{request:m.request,response:u.response,stream:u.stream},P={request:x[n].request,response:d[n].response,stream:d[n].stream},T={request:L[n].request,response:g[n].response,stream:g[n].stream},b=n==="anthropic"?o.convert.openai.responses.to.anthropic.messages:o.convert.openai.responses.to.openai.chat,j={request:(n==="anthropic"?o.convert.anthropic.messages:o.convert.openai.chat).to.openai.responses.request,response:b.response,stream:b.stream},q=s(e=>e===z?P:e===R?T:e===M?null:e===C?j:N,"convertersFor"),S=s((e,r={})=>{const{info:v}=h.find(f=>f.name===e.driver),l=r.front??n,w=D(l,r.wire),O=r.body!==void 0&&e.driver==="vllm"?y(e.driverSettings,e.probed,t.settings,e.settings):null,F=s(f=>f!==i||v.protocol===i?!0:l!=="anthropic"||r.tokens===!0?!1:e.driver!=="vllm"?!0:O!==null&&A.unsuited(r.body,O)!==null?!1:e.api!=="auto"||A.eligible(e),"usable"),E=v.name==="bedrock"&&e.api==="auto"&&r.model!==void 0?[I(r.model)?i:R]:B({api:e.api,front:w,info:v,missing:p.missing.get(e.name),usable:F}),k=E[0],G=l===n;return{protocol:k,more:e.api==="auto"&&E.length>1,convert:G?q(k):void 0}},"plan");return{config:t,state:p,front:n,drivers:h,endpoint:s(e=>p.endpoints.find(r=>r.name===e),"endpoint"),protocol:s((e,r)=>r?.protocol??S(e).protocol,"protocol"),plan:S,driver:s(e=>h.find(r=>r.name===e.driver),"driver"),settings:s((e,r)=>y(r===C?{reasoningRoundTrip:!0}:void 0,e.driverSettings,e.probed,t.settings,e.settings),"settings"),converters:s(e=>q(e.protocol),"converters")}}s(X,"capture");export{X as capture,J as collect};
|
|
2
2
|
//# sourceMappingURL=capture.mjs.map
|
package/dist/lib/config.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var l=Object.defineProperty;var a=(t,r)=>l(t,"name",{value:r,configurable:!0});import{TranslatorError as o}from"./errors.mjs";import*as p from"./pool.mjs";import{valid as c}from"./record.mjs";import{normalizeAliases as m,normalizeEfforts as y}from"./effort.mjs";import{ALIASES as I,canonical as d}from"./protocol.mjs";import{host as h,regionFor as v}from"./vertex.mjs";import{defaultUrl as g}from"./bedrock.mjs";function N(t){let r=t.length;for(;t[r-1]==="/";)r--;const n=t.slice(0,r);return n.endsWith("/v1")?n.slice(0,-3):n}a(N,"baseUrl");function w(t,r,n){let e=typeof t=="string"?{url:t}:t;if(e&&typeof e=="object"&&e.url===void 0&&e.backend==="vertex"){const s=v({vertex:e.vertex});s!==void 0&&(e={...e,url:`https://${h(s)}`})}if(e?.url===void 0&&e?.backend==="bedrock"&&(e={...e,url:g(e)}),!e||typeof e!="object"||typeof e.url!="string"||e.url==="")throw new o(`upstreams[${r}] needs a "url"`,{code:"INVALID_CONFIG",status:500});for(const s of["apiVersion","deployment","region","accessKeyId","secretAccessKey","sessionToken"])if(e[s]!==void 0&&(typeof e[s]!="string"||e[s]===""))throw new o(`upstreams[${r}].${s} must be a non-empty string`,{code:"INVALID_CONFIG",status:500});if(e.vertex!==void 0){const s=e.vertex,f=["project","region","accessToken"];if(s===null||typeof s!="object"||Array.isArray(s)||f.some(u=>s[u]!==void 0&&(typeof s[u]!="string"||s[u]==="")))throw new o(`upstreams[${r}].vertex must be an object whose project, region and accessToken are non-empty strings`,{code:"INVALID_CONFIG",status:500});if(s.getToken!==void 0&&typeof s.getToken!="function")throw new o(`upstreams[${r}].vertex.getToken must be a function`,{code:"INVALID_CONFIG",status:500})}if(e.api!==void 0&&d(e.api)===void 0)throw new o(`upstreams[${r}].api must be one of ${Object.keys(I).map(s=>JSON.stringify(s)).join(", ")} (got ${JSON.stringify(e.api)})`,{code:"INVALID_CONFIG",status:500});const i=e.settings?.nativeMessages??n?.nativeMessages;return{name:e.name??`upstream-${r}`,url:N(e.url),apiKey:e.apiKey,headers:{...e.headers},apiVersion:e.apiVersion,deployment:e.deployment,vertex:e.vertex,region:e.region,accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey,sessionToken:e.sessionToken,api:e.api===void 0?"auto":d(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()}}a(w,"endpoint");function L(t={}){const r=t.front??"anthropic";if(r!=="anthropic"&&r!=="openai")throw new o(`front must be "anthropic" or "openai" (got ${JSON.stringify(r)})`,{code:"INVALID_CONFIG",status:500});let n=t.upstreams??t.upstream;if(n!==void 0&&!Array.isArray(n)&&(n=[n]),!n||n.length===0)throw new o("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 o("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 o("onEvent must be a function",{code:"INVALID_CONFIG",status:500});if(!c(t.capture))throw new o("capture must be false, a directory path or a function (got "+JSON.stringify(t.capture)+")",{code:"INVALID_CONFIG",status:500});return{front:r,endpoints:n.map((i,s)=>w(i,s,t.settings)),...m(t.aliases),efforts:y(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}}a(L,"normalize");export{w as endpoint,L as normalize};
|
|
2
2
|
//# sourceMappingURL=config.mjs.map
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
var I=Object.defineProperty;var u=(e,t)=>I(e,"name",{value:t,configurable:!0});import{TranslatorError as j,anthropicToOpenAIType as A,anthropicTypeToStatus as N}from"./errors.mjs";import{completionId as k}from"./ids.mjs";import{parseArguments as S}from"./arguments.mjs";import{reasoningOf as F}from"./thinking.mjs";const R=new Set(["model","messages","stream","tools","tool_choice","max_tokens","max_completion_tokens","stop","temperature","top_p","top_k"]),U=new Set(["stream_options","user","metadata","store","service_tier"]),x={png:"png",jpeg:"jpeg",jpg:"jpeg",gif:"gif",webp:"webp"},C="[An image was omitted because Converse accepts base64 PNG, JPEG, GIF and WebP images only and the translator does not fetch URLs.]",$="Do not call any tools in your next reply; answer with text only.",T="(no output)",O={max_tokens:"length",model_context_window_exceeded:"length",tool_use:"tool_calls",content_filtered:"content_filter",guardrail_intervened:"content_filter",malformed_model_output:"error",malformed_tool_use:"error"};function d(e,t){e.includes(t)||e.push(t)}u(d,"drop");function h(e,t){if(typeof e=="string")return e===""?[]:[{text:e}];const o=[];for(const s of Array.isArray(e)?e:[])if(s?.type==="text")typeof s.text=="string"&&s.text!==""&&o.push({text:s.text});else if(s?.type==="image_url"){const i=/^data:image\/([a-z]+);base64,(.*)$/s.exec(String(s.image_url?.url??s.image_url??""));i&&Object.hasOwn(x,i[1])?o.push({image:{format:x[i[1]],source:{bytes:i[2]}}}):(o.push({text:C}),d(t,"image_url"))}else d(t,`content.${s?.type??"part"}`);return o}u(h,"blocksOf");function B(e,t,o){const s=h(e.content,t),i=o!==void 0&&s[0]?.text?.startsWith(o)===!0;return i&&(s[0]={text:s[0].text.slice(o.length)||T}),{toolResult:{toolUseId:e.tool_call_id,content:s.length>0?s:[{text:T}],...i?{status:"error"}:{}}}}u(B,"resultOf");function M(e,t){const o=e.content.map(s=>s.text??"[image]").join(`
|
|
2
|
+
`);return e.status==="error"?`[Tool ${t??"call"} failed: ${o}]`:`[Result of tool ${t??"call"}: ${o}]`}u(M,"textOfResult");function b(e,t,o,s){const i=[],m=[],f=new Map;for(const n of e){let p="user",a;if(n?.role==="system"||n?.role==="developer"){m.push(...h(n.content,t));continue}else if(n?.role==="tool"){const r=B(n,t,o);a=s?[{text:M(r.toolResult,f.get(n.tool_call_id))}]:[r]}else if(n?.role==="user")a=h(n.content,t);else if(n?.role==="assistant"){p="assistant",a=h(n.content,t),F(n)!==""&&d(t,"reasoning");for(const r of n.tool_calls??[]){const l=S(r.function?.arguments);f.set(r.id,r.function?.name),a.push(s?{text:`[Called tool ${r.function?.name} with input ${JSON.stringify(l)}]`}:{toolUse:{toolUseId:r.id,name:r.function?.name,input:l}})}}else{d(t,`messages.${n?.role}`);continue}a.length!==0&&(i.at(-1)?.role===p?i.at(-1).content.push(...a):i.push({role:p,content:a}))}return{messages:i,system:m}}u(b,"conversationOf");function z(e,t={}){const o=t.dropped??[],s=Array.isArray(e.tools)&&e.tools.length>0,{messages:i,system:m}=b(Array.isArray(e.messages)?e.messages:[],o,t.marker,!s),f={messages:i},n={},p=e.max_completion_tokens??e.max_tokens;if(p!=null&&(n.maxTokens=p),e.temperature!==void 0&&e.temperature!==null&&(n.temperature=e.temperature),e.top_p!==void 0&&e.top_p!==null&&(n.topP=e.top_p),e.stop!==void 0&&e.stop!==null&&(n.stopSequences=Array.isArray(e.stop)?e.stop:[e.stop]),Object.keys(n).length>0&&(f.inferenceConfig=n),s){const r={tools:e.tools.map(c=>({toolSpec:{name:c.function?.name,...c.function?.description===void 0?{}:{description:c.function.description},inputSchema:{json:c.function?.parameters??{type:"object",properties:{}}}}}))},l=e.tool_choice;l==="required"?r.toolChoice={any:{}}:l?.type==="function"?r.toolChoice={tool:{name:l.function?.name}}:l==="none"?m.push({text:$}):l!==void 0&&l!=="auto"&&d(o,"tool_choice"),f.toolConfig=r}m.length>0&&(f.system=m);const a={...t.sampling};e.top_k!==void 0&&e.top_k!==null&&(a.top_k=e.top_k),t.thinkingBudget!==void 0&&(a.thinking={type:"enabled",budget_tokens:t.thinkingBudget}),Object.keys(a).length>0&&(f.additionalModelRequestFields=a);for(const r of Object.keys(e))R.has(r)||U.has(r)||r==="n"&&!(e.n>1)||d(o,r);return f}u(z,"chatBody");function y(e,t){const o=Object.hasOwn(O,e??"")?O[e]:"stop";return t&&o==="stop"?"tool_calls":o}u(y,"finishOf");function _(e){if(!Number.isFinite(e?.inputTokens)&&!Number.isFinite(e?.outputTokens))return;const t=Number.isFinite(e.cacheReadInputTokens)?e.cacheReadInputTokens:0,o=Number.isFinite(e.cacheWriteInputTokens)?e.cacheWriteInputTokens:0,s=(Number.isFinite(e.inputTokens)?e.inputTokens:0)+t+o,i=Number.isFinite(e.outputTokens)?e.outputTokens:0;return{prompt_tokens:s,completion_tokens:i,total_tokens:s+i,...t>0?{prompt_tokens_details:{cached_tokens:t}}:{}}}u(_,"usageOf");function G(e){if(_(e)===void 0)return;const t=u(o=>Number.isFinite(o)?o:0,"count");return{input_tokens:t(e.inputTokens),output_tokens:t(e.outputTokens),cache_read_input_tokens:t(e.cacheReadInputTokens),cache_creation_input_tokens:t(e.cacheWriteInputTokens)}}u(G,"folded");function L(e,t={}){const o=e?.output?.message?.content;if(!Array.isArray(o))throw new j("Upstream response contained no message",{code:"invalid_upstream_response",status:502});const s=o.filter(n=>n?.toolUse).map(({toolUse:n})=>({id:n.toolUseId,type:"function",function:{name:n.name,arguments:JSON.stringify(n.input??{})}})),i={role:"assistant",content:o.map(n=>n?.text??"").join("")},m=o.map(n=>n?.reasoningContent?.reasoningText?.text??"").join("");m!==""&&(i[t.reasoningField??"reasoning"]=m),s.length>0&&(i.tool_calls=s);const f=_(e.usage);return{id:k(),object:"chat.completion",created:Math.floor(Date.now()/1e3),model:t.model,choices:[{index:0,message:i,finish_reason:y(e.stopReason,s.length>0)}],...f===void 0?{}:{usage:f}}}u(L,"completion");function q(e={}){const t=k(),o=Math.floor(Date.now()/1e3),s=e.reasoningField??"reasoning",i=new Map;let m=!1,f=!1,n;const p=u((r,l=null)=>({id:t,object:"chat.completion.chunk",created:o,model:e.model,choices:[{index:0,delta:r,finish_reason:l}]}),"make"),a=u(()=>m?[]:(m=!0,[p({role:"assistant",content:""})]),"first");return{event(r,l){if(r==="error"){f=!0;const c=l?.error?.type;return[{error:{message:l?.error?.message,type:A(c),param:null,code:N(c)}}]}if(r==="contentBlockStart"){const c=l?.start?.toolUse;if(c===void 0)return[];i.set(l.contentBlockIndex,i.size);const g={index:i.size-1,id:c.toolUseId,type:"function",function:{name:c.name,arguments:""}};return[...a(),p({tool_calls:[g]})]}if(r==="contentBlockDelta"){const c=l?.delta;if(typeof c?.text=="string"&&c.text!=="")return[...a(),p({content:c.text})];if(typeof c?.toolUse?.input=="string"){const w={index:i.get(l.contentBlockIndex),function:{arguments:c.toolUse.input}};return[...a(),p({tool_calls:[w]})]}const g=c?.reasoningContent?.text;return typeof g=="string"&&g!==""?[...a(),p({[s]:g})]:[]}if(r==="messageStop")return f=!0,[...a(),p({},y(l?.stopReason,i.size>0))];if(r==="metadata"){n=l?.usage;const c=_(n);return e.includeUsage===!1||c===void 0?[]:[{id:t,object:"chat.completion.chunk",created:o,model:e.model,choices:[],usage:c}]}return a()},finished:u(()=>f,"finished"),usage:u(()=>n,"usage")}}u(q,"createChunker");export{z as chatBody,L as completion,q as createChunker,y as finishOf,G as folded,_ as usageOf};
|
|
3
|
+
//# sourceMappingURL=converse.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var k=Object.defineProperty;var a=(e,o)=>k(e,"name",{value:o,configurable:!0});import{TranslatorError as A}from"./errors.mjs";import{fetchUrl as D,FetchError as g}from"./fetch-url.mjs";const U=Object.freeze({maxBytes:32*1024*1024,timeout:3e4,maxRedirects:3,maxDocuments:10,allowPrivate:!1}),R=["allow","maxBytes","timeout","maxRedirects","maxDocuments","allowPrivate"];function l(e,o){return new A(`Invalid setting documentUrls: ${JSON.stringify(o)} (${e})`,{code:"invalid_setting",status:500,details:{key:"documentUrls"}})}a(l,"invalid");function j(e){if(e==null||e===!1)return null;if(typeof e!="object"||Array.isArray(e))throw l("expected null or an object",e);const o=Object.keys(e).filter(r=>!R.includes(r));if(o.length>0)throw l(`unknown ${o.length>1?"keys":"key"} ${o.join(", ")}`,e);const{allow:c}=e;if(!Array.isArray(c)||c.length===0||!c.every(r=>typeof r=="string"&&r.trim()!==""))throw l('allow must be a non-empty list of host names such as "files.example.com" or "*.example.com"',e);const i={...U,...e,allow:c.map(r=>r.trim().toLowerCase())};for(const r of["maxBytes","timeout"])if(!Number.isInteger(i[r])||i[r]<=0)throw l(`${r} must be a positive integer`,e);for(const r of["maxRedirects","maxDocuments"])if(!Number.isInteger(i[r])||i[r]<0)throw l(`${r} must be an integer of 0 or more`,e);if(typeof i.allowPrivate!="boolean")throw l("allowPrivate must be true or false",e);return i}a(j,"normalize");function m(e){if(typeof e!="string"||!URL.canParse(e))return"(invalid URL)";const o=new URL(e);return`${o.protocol}//${o.host}${o.pathname}`}a(m,"display");function P(e){return e.documentUrls!==null&&e.documents==="extract"&&!e.documentConverter}a(P,"wanted");async function S(e,{options:o,report:c,signal:i,fetch:r=D,deps:x}){if(!Array.isArray(e?.messages))return e;const u=new Map,b=a(t=>(u.has(t)||u.set(t,$(t)),u.get(t)),"get"),$=a(async t=>{if(u.size>=o.maxDocuments)return c({kind:"fetch",level:"warn",message:`Document ${m(t)} was not fetched: more than ${o.maxDocuments} URL documents in one request`,url:m(t),reason:"limit"}),{source:{type:"fetch_failed",url:m(t),reason:`no more than ${o.maxDocuments} documents are fetched per request`}};try{const n=await r(t,{...o,signal:i},x);return c({kind:"fetched",level:"debug",message:`Fetched document ${m(t)} (${n.bytes.length} bytes, ${n.mediaType})`,url:m(t),bytes:n.bytes.length,mediaType:n.mediaType}),{source:{type:"base64",media_type:n.mediaType,data:n.bytes.toString("base64")}}}catch(n){const f=n instanceof g?n:new g("network","the connection failed",String(n?.message??n));return c({kind:"fetch",level:"warn",message:`Document ${m(t)} was not fetched: ${f.detail}`,url:m(t),reason:f.kind}),{source:{type:"fetch_failed",url:m(t),reason:f.message}}}},"one"),h=a(async t=>{let n=!1;const f=[];for(const s of t){let d=s;if(s?.type==="document"&&s.source?.type==="url")d={...s,source:(await b(s.source.url)).source};else if(s?.type==="tool_result"&&Array.isArray(s.content)){const w=await h(s.content);w!==s.content&&(d={...s,content:w})}d!==s&&(n=!0),f.push(d)}return n?f:t},"blocks");let p=!1;const y=[];for(const t of e.messages){const n=Array.isArray(t?.content)?await h(t.content):t?.content;n!==t?.content?(p=!0,y.push({...t,content:n})):y.push(t)}return p?{...e,messages:y}:e}a(S,"resolve");export{U as DEFAULTS,m as display,j as normalize,S as resolve,P as wanted};
|
|
2
|
+
//# sourceMappingURL=document-urls.mjs.map
|
package/dist/lib/documents.mjs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
var w=Object.defineProperty;var
|
|
1
|
+
var w=Object.defineProperty;var i=(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[p(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==="fetch_failed"?[p(o,`the document at ${t.url} could not be fetched: ${t.reason}`,n)]:t.type==="url"?[p(o,"documents referenced by URL cannot be fetched by the translator",n)]:[p(o,`document sources of type ${t.type}${t.media_type?` (${t.media_type})`:""} are not supported`,n)]}i(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[p(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
2
|
${u}`);return[{type:"text",text:`${l}
|
|
3
3
|
|
|
4
4
|
${r.join(`
|
|
5
5
|
|
|
6
|
-
`)}`}]}const $=Math.min(s.pageCount,t.pdf.maxImages);let
|
|
7
|
-
${r}`}),u<
|
|
6
|
+
`)}`}]}const $=Math.min(s.pageCount,t.pdf.maxImages);let c;try{c=await f.render(n,Array.from({length:$},(r,u)=>u+1),t.pdf)}catch{c=[]}const d=[{type:"text",text:l}];return s.pages.forEach((r,u)=>{d.push({type:"text",text:`--- Page ${u+1} ---
|
|
7
|
+
${r}`}),u<c.length&&d.push({type:"image",source:{type:"base64",...c[u]}})}),d}i(b,"expandPdf");function g(e,n){const a=e.title?` title="${e.title}"`:"",o=e.context?`${e.context}
|
|
8
8
|
|
|
9
9
|
`:"";return{type:"text",text:`<document${a}>
|
|
10
10
|
${o}${n}
|
|
11
|
-
</document>`}}
|
|
11
|
+
</document>`}}i(g,"wrap");function v(e,n){return e.title??h(n)??"(unnamed)"}i(v,"nameOf");function h(e){return(e.toolUseId===null?void 0:e.toolInputs.get(e.toolUseId))?.file_path??null}i(h,"pathOf");function p(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}]`}}i(p,"note");export{E as expand};
|
|
12
12
|
//# sourceMappingURL=documents.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 p=Object.defineProperty;var s=(e,r)=>p(e,"name",{value:r,configurable:!0});import{crc32 as d}from"node:zlib";import{TranslatorError as m}from"./errors.mjs";import{anthropicType as y}from"./upstream-error.mjs";import{items as E}from"./source.mjs";const i=12,f=4,b=32*1024*1024,l={accessDeniedException:403,internalServerException:500,modelErrorException:424,modelNotReadyException:429,modelStreamErrorException:424,modelTimeoutException:408,resourceNotFoundException:404,serviceQuotaExceededException:429,serviceUnavailableException:503,throttlingException:429,validationException:400},c=s(e=>new m(e,{code:"invalid_stream_data",status:502}),"broken");function g(e){const r={};let n=0;const a=s(t=>{if(n+t>e.length)throw c("The event stream has a header that runs past its section");const o=e.subarray(n,n+t);return n+=t,o},"take");for(;n<e.length;){const t=a(a(1)[0]).toString("utf8"),o=a(1)[0];switch(o){case 0:r[t]=!0;break;case 1:r[t]=!1;break;case 2:r[t]=a(1).readInt8();break;case 3:r[t]=a(2).readInt16BE();break;case 4:r[t]=a(4).readInt32BE();break;case 5:r[t]=Number(a(8).readBigInt64BE());break;case 6:r[t]=Buffer.from(a(a(2).readUInt16BE()));break;case 7:r[t]=a(a(2).readUInt16BE()).toString("utf8");break;case 8:r[t]=new Date(Number(a(8).readBigInt64BE()));break;case 9:r[t]=a(16).toString("hex").replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/,"$1-$2-$3-$4-$5");break;default:throw c(`The event stream has a header of unknown type ${o}`)}}return r}s(g,"headersOf");function v(){let e=Buffer.alloc(0),r=null;return{push(n){if(r)throw r;e=e.length===0?Buffer.from(n):Buffer.concat([e,n]);const a=[];try{for(;e.length>=i;){if(d(e.subarray(0,8))!==e.readUInt32BE(8))throw c("The event stream has a frame with a bad prelude checksum");const t=e.readUInt32BE(0),o=e.readUInt32BE(4);if(t<i+f||t>b||o>t-i-f)throw c("The event stream has a frame with an impossible length");if(e.length<t)break;if(d(e.subarray(0,t-f))!==e.readUInt32BE(t-f))throw c("The event stream has a frame with a bad message checksum");a.push({headers:g(e.subarray(i,i+o)),payload:e.subarray(i+o,t-f)}),e=e.subarray(t)}}catch(t){if(a.length===0)throw t;r=t}return a},flush(){if(r)throw r;if(e.length>0)throw c("The event stream ended inside a frame")}}}s(v,"createParser");async function*w(e){const r=v();for await(const n of E(e))yield*r.push(n);r.flush()}s(w,"frames");function u(e){try{return JSON.parse(e.toString("utf8"))}catch{throw c("The event stream has an event whose payload is not valid JSON")}}s(u,"json");function x(e,r){const n=String(e[":exception-type"]??e[":error-code"]),a=String(e[":error-message"]??r.toString("utf8"));let t=a;try{t=JSON.parse(a).message??a}catch{}const o=Object.hasOwn(l,n)?l[n]:500;return{event:"error",data:{type:"error",error:{type:y(o),message:t}}}}s(x,"failure");async function*U(e,r){try{for await(const{headers:n,payload:a}of w(e)){const t=n[":message-type"];if(t==="exception"||t==="error")yield x(n,a);else if(r==="converse")yield{event:n[":event-type"],data:u(a)};else{const{bytes:o}=u(a),h=u(Buffer.from(String(o),"base64"));delete h["amazon-bedrock-invocationMetrics"],yield{event:h.type,data:h}}}}catch(n){yield{event:"error",data:{type:"error",error:{type:n.type??"api_error",message:n.message}}}}}s(U,"events");export{v as createParser,U as events,w as frames};
|
|
2
|
+
//# sourceMappingURL=eventstream.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var g=Object.defineProperty;var l=(t,e)=>g(t,"name",{value:e,configurable:!0});import $ from"node:dns/promises";import k from"node:http";import L from"node:https";import w from"node:net";const x=new Map([["application/pdf","application/pdf"],["text/plain","text/plain"]]),E=new Set([301,302,303,307,308]),P=[["unspecified","ipv4","0.0.0.0",8],["private","ipv4","10.0.0.0",8],["cgnat","ipv4","100.64.0.0",10],["loopback","ipv4","127.0.0.0",8],["link-local","ipv4","169.254.0.0",16],["private","ipv4","172.16.0.0",12],["reserved","ipv4","192.0.0.0",24],["reserved","ipv4","192.0.2.0",24],["reserved","ipv4","192.88.99.0",24],["private","ipv4","192.168.0.0",16],["reserved","ipv4","198.18.0.0",15],["reserved","ipv4","198.51.100.0",24],["reserved","ipv4","203.0.113.0",24],["multicast","ipv4","224.0.0.0",4],["reserved","ipv4","240.0.0.0",4],["unspecified","ipv6","::",128],["loopback","ipv6","::1",128],["reserved","ipv6","::",96],["mapped","ipv6","::ffff:0:0",96],["reserved","ipv6","64:ff9b::",96],["reserved","ipv6","64:ff9b:1::",48],["reserved","ipv6","100::",64],["reserved","ipv6","2001::",23],["reserved","ipv6","2001:db8::",32],["reserved","ipv6","2002::",16],["unique-local","ipv6","fc00::",7],["link-local","ipv6","fe80::",10],["link-local","ipv6","fec0::",10],["multicast","ipv6","ff00::",8]],R=P.map(([t,e,n,o])=>{const s=new w.BlockList;return s.addSubnet(n,o,e),{name:t,family:e,list:s}});class a extends Error{static{l(this,"FetchError")}constructor(e,n,o=n){super(n),this.name="FetchError",this.kind=e,this.detail=o}}function S(t){const e=/^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(t);if(e===null)return null;const n=Number.parseInt(e[1],16),o=Number.parseInt(e[2],16);return`${n>>8}.${n&255}.${o>>8}.${o&255}`}l(S,"unmap");function y(t){const e=t.replace(/%.*$/,""),n=w.isIP(e);if(n===0)return"invalid";const o=n===6?new URL(`http://[${e}]`).hostname.slice(1,-1):e,s=n===6?S(o):null;if(s!==null)return y(s)??"mapped";const r=n===4?"ipv4":"ipv6";return R.find(c=>c.family===r&&c.list.check(o,r))?.name??null}l(y,"classify");function T(t,e){return e.some(n=>{const o=n.toLowerCase();return o==="*"?!0:o.startsWith("*.")?t.endsWith(o.slice(1))&&t.length>o.length-1:t===o})}l(T,"allowed");function U(t){return $.lookup(t,{all:!0,verbatim:!0})}l(U,"lookup");async function C(t,e,n){if(t.protocol!=="http:"&&t.protocol!=="https:")throw new a("scheme","only http and https URLs are fetched",`scheme ${t.protocol}`);if(t.username!==""||t.password!=="")throw new a("scheme","URLs with credentials are refused");const s=(t.hostname.startsWith("[")?t.hostname.slice(1,-1):t.hostname).toLowerCase().replace(/\.$/,"");if(s===""||!T(s,e.allow))throw new a("allow","the host is not on the allow list",`host ${s}`);let r;if(w.isIP(s)!==0)r=[{address:s,family:w.isIP(s)}];else try{r=await n(s)}catch(c){throw new a("dns","the host name could not be resolved",`lookup of ${s}: ${c.code??c.message}`)}if(!Array.isArray(r)||r.length===0)throw new a("dns","the host name could not be resolved",`lookup of ${s}: no addresses`);if(e.allowPrivate!==!0){const c=r.find(p=>y(p.address)!==null);if(c!==void 0)throw new a("address","the host resolves to an address that is not allowed",`${s} resolves to ${c.address} (${y(c.address)})`)}return r[0]}l(C,"vet");function I(t,e,n,o){return new Promise((s,r)=>{const c=t.protocol==="https:"?o.https:o.http,p=l((h,u,f)=>u?.all?f(null,[{address:e.address,family:e.family}]):f(null,e.address,e.family),"pinned"),i=c.request(t,{method:"GET",agent:!1,lookup:p,headers:{accept:"application/pdf, text/plain","accept-encoding":"identity"}},s),d=l(()=>i.destroy(n.reason),"stop");n.addEventListener("abort",d,{once:!0}),i.once("close",()=>n.removeEventListener("abort",d)),i.on("error",r),i.end()})}l(I,"get");async function F(t,e){const n=[];let o=0;for await(const s of t){if(o+=s.length,o>e)throw t.destroy(),new a("size",`the document is larger than ${e} bytes`,`body passed ${e} bytes`);n.push(s)}return Buffer.concat(n)}l(F,"read");async function z(t,e,n={}){const o=n.lookup??U,s=n.modules??{http:k,https:L},r=new AbortController,c=setTimeout(()=>r.abort(new a("timeout",`the download did not finish within ${e.timeout} ms`)),e.timeout),p=l(()=>r.abort(e.signal.reason),"relay");e.signal?.addEventListener("abort",p,{once:!0});const i=new Promise((d,h)=>{r.signal.addEventListener("abort",()=>h(r.signal.reason),{once:!0})});i.catch(()=>{});try{return await Promise.race([i,N(t,e,o,s,r.signal)])}catch(d){if(d instanceof a)throw d;if(r.signal.aborted){const h=r.signal.reason;throw new a("network","the download was cancelled",String(h?.message??h))}throw new a("network","the connection failed",String(d?.code??d?.message??d))}finally{clearTimeout(c),e.signal?.removeEventListener("abort",p),r.abort(new a("network","the download was cancelled"))}}l(z,"fetchUrl");async function N(t,e,n,o,s){let r;try{r=new URL(t)}catch{throw new a("scheme","the URL is not valid","URL does not parse")}for(let c=0;;c++){const p=await C(r,e,n),i=await I(r,p,s,o),d=i.statusCode,h=i.headers.location;if(E.has(d)&&typeof h=="string"){if(i.resume(),c>=e.maxRedirects)throw new a("redirects","too many redirects",`more than ${e.maxRedirects} redirects`);let m;try{m=new URL(h,r)}catch{throw new a("status",`the server answered HTTP ${d} with an invalid location`)}if(r.protocol==="https:"&&m.protocol==="http:")throw new a("downgrade","a redirect from https to http was refused");r=m;continue}if(d!==200)throw i.resume(),new a("status",`the server answered HTTP ${d}`);const u=String(i.headers["content-encoding"]??"identity").toLowerCase();if(u!=="identity")throw i.destroy(),new a("encoding","the response is compressed, which is not supported",`content-encoding ${u}`);const f=x.get(String(i.headers["content-type"]??"").split(";")[0].trim().toLowerCase());if(f===void 0)throw i.destroy(),new a("type","the content type is not supported (application/pdf and text/plain are)",`content-type ${i.headers["content-type"]}`);const v=Number(i.headers["content-length"]);if(Number.isFinite(v)&&v>e.maxBytes)throw i.destroy(),new a("size",`the document is larger than ${e.maxBytes} bytes`,`content-length ${v}`);const b=await F(i,e.maxBytes);if(f==="application/pdf"&&!b.subarray(0,1024).includes("%PDF-"))throw new a("type","the content is not a PDF","no %PDF- header in the first 1024 bytes");return{bytes:b,mediaType:f,url:r.href}}}l(N,"run");export{a as FetchError,T as allowed,y as classify,z as fetchUrl,U as lookup};
|
|
2
|
+
//# sourceMappingURL=fetch-url.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var G=Object.defineProperty;var c=(e,t)=>G(e,"name",{value:t,configurable:!0});import S from"node:crypto";import O from"node:fs";import A from"node:os";import h from"node:path";import{TranslatorError as T}from"./errors.mjs";const $="https://www.googleapis.com/auth/cloud-platform",_="https://oauth2.googleapis.com/token",x="http://metadata.google.internal",N=6e4,L=3600,f=new Map,p=new Map;function F(){f.clear(),p.clear()}c(F,"reset");function g(e){return Buffer.from(e).toString("base64url")}c(g,"base64url");function l(e,t=401){return new T(e,{code:"upstream_auth",status:t})}c(l,"failure");function C(e,{now:t=Date.now(),scope:o=$}={}){const n=Math.floor(t/1e3),r={alg:"RS256",typ:"JWT",...e.private_key_id?{kid:e.private_key_id}:{}},a={iss:e.client_email,scope:o,aud:e.token_uri??_,iat:n,exp:n+L},i=`${g(JSON.stringify(r))}.${g(JSON.stringify(a))}`;let u;try{u=S.createSign("RSA-SHA256").update(i).sign(e.private_key)}catch(s){throw l(`Google credentials: the service account private key cannot sign (${s.message})`)}return`${i}.${g(u)}`}c(C,"assertion");function P(e,t=process.platform){const o=e.HOME||e.USERPROFILE||A.homedir(),n=e.CLOUDSDK_CONFIG??(t==="win32"&&e.APPDATA?h.join(e.APPDATA,"gcloud"):h.join(o,".config","gcloud"));return h.join(n,"application_default_credentials.json")}c(P,"wellKnown");function m(e,t){const o=e?.type==="service_account"?["client_email","private_key"]:e?.type==="authorized_user"?["client_id","client_secret","refresh_token"]:null;if(o===null)throw l(`Google credentials (${t}): type ${JSON.stringify(e?.type)} is not supported; use a service account key or an authorized_user file, or pass getToken`);const n=o.filter(r=>typeof e[r]!="string"||e[r]==="");if(n.length>0)throw l(`Google credentials (${t}): missing ${n.join(", ")}`);return e}c(m,"checked");function y(e,t){if(e!==null&&typeof e=="object")return m(e,t);if(typeof e!="string")throw l(`Google credentials (${t}): expected an object or a file path`);if(e.trimStart().startsWith("{"))try{return m(JSON.parse(e),t)}catch(o){throw o instanceof T?o:l(`Google credentials (${t}): the JSON text does not parse`)}if(!p.has(e)){let o;try{o=O.readFileSync(e,"utf8")}catch(n){throw l(`Google credentials (${t}): cannot read ${e} (${n.code})`)}try{p.set(e,JSON.parse(o))}catch{throw l(`Google credentials (${t}): ${e} is not JSON`)}}return m(p.get(e),`${t}: ${e}`)}c(y,"read");function I(e,t=process.env){if(e?.credentials!==void 0)return y(e.credentials,"credentials setting");if(t.GOOGLE_APPLICATION_CREDENTIALS)return y(t.GOOGLE_APPLICATION_CREDENTIALS,"GOOGLE_APPLICATION_CREDENTIALS");const o=P(t);return p.has(o)||O.existsSync(o)?y(o,"application default credentials"):null}c(I,"credentials");function v(e){const t=e.type==="service_account"?e.private_key:e.refresh_token,o=e.type==="service_account"?e.client_email:e.client_id;return[e.type,e.token_uri??_,o,S.createHash("sha256").update(t).digest("hex")].join("|")}c(v,"identity");async function w(e,t,o,n){let r,a;try{r=await e(t,{redirect:"error",...o,...n>0?{signal:AbortSignal.timeout(n)}:{}}),a=await r.text()}catch(s){throw l(`Google token request to ${t} failed: ${s.message}`,502)}let i=null;try{i=JSON.parse(a)}catch{}if(!r.ok){const s=i?.error_description??(typeof i?.error=="string"?i.error:i?.error?.message);throw l(`Google token request refused (HTTP ${r.status})${s?`: ${s}`:""}`,r.status>=500?502:401)}if(typeof i?.access_token!="string"||i.access_token==="")throw l(`Google token request to ${t} answered without an access_token`,502);const u=Number(i.expires_in);return{token:i.access_token,ttl:Number.isFinite(u)&&u>0?u:0}}c(w,"fetchToken");function E(e,t,o){const n=f.get(e);if(n?.pending)return n.pending;if(n&&n.expires-N>o())return Promise.resolve(n.token);const r=t().then(({token:a,ttl:i})=>(f.set(e,{token:a,expires:o()+i*1e3}),a),a=>{throw f.delete(e),a});return f.set(e,{pending:r}),r}c(E,"cached");function R(e,{transport:t,timeout:o,now:n}){const r=e.token_uri??_,a={"content-type":"application/x-www-form-urlencoded"};return E(v(e),()=>{const i=e.type==="service_account"?{grant_type:"urn:ietf:params:oauth:grant-type:jwt-bearer",assertion:C(e,{now:n()})}:{grant_type:"refresh_token",client_id:e.client_id,client_secret:e.client_secret,refresh_token:e.refresh_token};return w(t,r,{method:"POST",headers:a,body:new URLSearchParams(i).toString()},o)},n)}c(R,"exchange");function b(e,{transport:t,timeout:o,now:n}){const r=(e===!0?x:e).replace(/\/+$/,""),a=`${r}/computeMetadata/v1/instance/service-accounts/default/token`;return E(`metadata|${r}`,()=>w(t,a,{method:"GET",headers:{"metadata-flavor":"Google"}},o),n)}c(b,"metadata");async function H({settings:e={},apiKey:t,transport:o,timeout:n,signal:r,env:a=process.env,now:i=Date.now}){if(typeof e.getToken=="function"){let s;try{s=await e.getToken({signal:r})}catch(k){throw l(`Google getToken failed: ${k?.message??k}`)}const d=typeof s=="string"?s:s?.token;if(typeof d!="string"||d==="")throw l("Google getToken returned no access token");return d}if(typeof e.accessToken=="string"&&e.accessToken!=="")return e.accessToken;if(typeof t=="string"&&t!=="")return t;const u={transport:o,timeout:n,now:i};if(e.credentials!==void 0||!a.GOOGLE_ACCESS_TOKEN){const s=I(e,a);if(s!==null)return R(s,u)}if(a.GOOGLE_ACCESS_TOKEN)return a.GOOGLE_ACCESS_TOKEN;if(e.metadataServer)return b(e.metadataServer,u);throw l("No Google credentials for Vertex AI: set vertex.getToken, vertex.accessToken, vertex.credentials, GOOGLE_ACCESS_TOKEN or GOOGLE_APPLICATION_CREDENTIALS, run `gcloud auth application-default login`, or set vertex.metadataServer on Google Cloud")}c(H,"token");export{x as METADATA_URL,$ as SCOPE,_ as TOKEN_URI,C as assertion,g as base64url,I as credentials,F as reset,H as token,P as wellKnown};
|
|
2
|
+
//# sourceMappingURL=google-auth.mjs.map
|
package/dist/lib/http.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var l=Object.defineProperty;var i=(t,e)=>l(t,"name",{value:e,configurable:!0});function y(t,e){return t+e}i(y,"join");async function m(t,e,a,n,s,u){return t.path?t.path(e,{endpoint:a,model:n,protocol:s,stream:u}):(t.info.native!==void 0&&s===t.info.native.protocol?t.info.native.paths:void 0)?.[e]??t.info.paths[e]??p[e]}i(m,"pathOf");const p={responses:"/v1/responses",embeddings:"/v1/embeddings",models:"/v1/models"};function k(t){return t==="localhost"||t==="::1"||/^127\./.test(t)}i(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}}i(d,"headers");function S({endpoint:t,transport:e,signal:a,timeout:n,probes:s},u,r){const f=r!==void 0&&(r.auth==="x-api-key"||r.auth==="api-key"||r.headers!==void 0),o=f?{auth:r.auth,extra:r.headers}:{},h=i(()=>g(e,y(t.url,u),{method:"GET",headers:d(t,o.auth,o.extra),signal:a,timeout:n}),"run");if(!s)return h();const c=f?JSON.stringify([u,o.auth,o.extra]):u;return s.has(c)||s.set(c,h()),s.get(c)}i(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}i(x,"plain");async function g(t,e,a={}){const{timeout:n,signal:s,body:u,...r}=a,f=[];s&&f.push(s),n>0&&f.push(AbortSignal.timeout(n));try{const o=await t(e,{...r,redirect:"error",body:u===void 0?void 0:JSON.stringify(u),signal:f.length>1?AbortSignal.any(f):f[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}}}i(g,"json");export{d as headers,k as isLoopback,y as join,g as json,m as pathOf,x as plain,S as probe};
|
|
2
2
|
//# sourceMappingURL=http.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 A=Object.defineProperty;var r=(e,t)=>A(e,"name",{value:t,configurable:!0});const n="anthropic.messages",o="openai.chat",s="openai.responses",i="ollama.chat",a="bedrock.converse",E=[n,o,s,i,a],x=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,[a]:a,converse:a});function w(e){return typeof e=="string"&&Object.hasOwn(x,e.toLowerCase())?x[e.toLowerCase()]:void 0}r(w,"canonical");function O(e){const t=[...Array.isArray(e.protocols)?e.protocols:[]];return e.protocol!==void 0&&t.push(e.protocol),e.responses===!0&&t.push(s),e.native?.protocol!==void 0&&t.push(e.native.protocol),[...new Set(t)]}r(O,"supported");function L(e,t){return e==="anthropic"?n:t==="responses"?s:o}r(L,"frontOf");function C({api:e,front:t,info:p,missing:l,usable:u}){const f=O(p),d=r(c=>f.includes(c)&&(u?u(c):!0),"ok");if(e!=="auto")return[d(e)?e:p.protocol];const h=[t,o].filter((c,S,g)=>g.indexOf(c)===S&&d(c)&&!l?.has(c));return h.length>0?h:[p.protocol]}r(C,"candidates");function b(e,t,p){e.missing.has(t)||e.missing.set(t,new Set),e.missing.get(t).add(p)}r(b,"remember");export{x as ALIASES,o as CHAT,a as CONVERSE,E as IDS,n as MESSAGES,i as OLLAMA,s as RESPONSES,C as candidates,w as canonical,L as frontOf,b as remember,O 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
|
|
1
|
+
var f=Object.defineProperty;var p=(e,o)=>f(e,"name",{value:o,configurable:!0});import{TranslatorError as m}from"./errors.mjs";import{order as v}from"./pool.mjs";import{baseModels as w}from"./models.mjs";function h(e,o){return(e.models??e.facts?.models??[]).includes(o)?!0:e.protocol==="anthropic.messages"&&e.models===void 0&&/^claude-/.test(o)}p(h,"serves");function k(e,o,n,t,i=!1){const c=Object.hasOwn(e.aliases,n),r=c?e.aliases[n]:n,s=c&&Object.hasOwn(e.pins,n)?e.pins[n]:void 0,d=o.filter(a=>h(a,r)),l=o.find(a=>a.default)??o[0];if(d.length>0){const a=v(e,t,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,o,n={}){if(!o||typeof o!="object"||Array.isArray(o))throw new m("The request body must be a JSON object",{code:"invalid_body",status:400});const t=k(e.config,e.state.endpoints,o.model,e.state,n.advance===!0);if(n.endpoint!==void 0){const s=t.pool.find(l=>l.name===n.endpoint);if(!s)throw new m(`Endpoint ${n.endpoint} no longer serves ${t.model}`,{code:"endpoint_not_in_pool",status:500});const d=e.plan(s,{body:o,wire:n.wire,tokens:n.tokens,model:t.model});return{endpoint:s,model:t.model,requested:o.model,unserved:t.unserved,pin:t.pin,convert:d.convert,protocol:d.protocol,more:d.more,pool:[s]}}const{endpoint:i,pool:c}=t,r=e.plan(i,{body:o,wire:n.wire,tokens:n.tokens,model:t.model});return{endpoint:i,model:t.model,requested:o.model,unserved:t.unserved,pin:t.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 c=Object.defineProperty;var l=(
|
|
1
|
+
var c=Object.defineProperty;var l=(a,e)=>c(a,"name",{value:e,configurable:!0});import{TranslatorError as n}from"./errors.mjs";import{normalize as f}from"./document-urls.mjs";import{check as d}from"./constraint.mjs";import{valid as g}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,documentUrls: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}),p={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"]},u=["effortPassthrough","passthroughFilter","imageFallback","toolFallback","jsonObjectFallback","thinkOpen","reasoningRoundTrip","toolErrorInference"];function b(...a){const e={...r,pdf:{...r.pdf},sampling:{...r.sampling}};for(const t of a)if(t)for(const[o,s]of Object.entries(t))s!==void 0&&(o==="pdf"?Object.assign(e.pdf,s):o==="sampling"&&s!==null&&typeof s=="object"&&!Array.isArray(s)?Object.assign(e.sampling,s):e[o]=s);for(const[t,o]of Object.entries(p))if(!o.includes(e[t]))throw new n(`Invalid setting ${t}: ${JSON.stringify(e[t])} (expected one of ${o.join(", ")})`,{code:"invalid_setting",status:500,details:{key:t,allowed:o}});for(const t of u)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=d(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(!g(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(e.documentUrls=f(e.documentUrls),!(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(b,"resolve");export{r as DEFAULTS,b as resolve};
|
|
2
2
|
//# sourceMappingURL=settings.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
var R=Object.defineProperty;var a=(t,e)=>R(t,"name",{value:e,configurable:!0});import{createHash as W,createHmac as f}from"node:crypto";const w=a(t=>W("sha256").update(t).digest("hex"),"sha256"),r=a((t,e)=>f("sha256",t).update(e).digest(),"hmac"),d=a(t=>encodeURIComponent(t).replace(/[!'()*]/g,e=>`%${e.charCodeAt(0).toString(16).toUpperCase()}`),"encode");function b(t){return t.toISOString().replace(/[-:]/g,"").replace(/\.\d{3}/,"")}a(b,"stamp");const k=a(t=>t.split("/").map(d).join("/"),"canonicalPath");function I(t){const e=[...t].map(([n,o])=>[d(n),d(o)]);return e.sort((n,o)=>n.join("\0")<o.join("\0")?-1:1),e.map(([n,o])=>`${n}=${o}`).join("&")}a(I,"canonicalQuery");function P({method:t="GET",url:e,headers:n={},body:o="",region:m,service:g,accessKeyId:y,secretAccessKey:U,sessionToken:$,date:q=new Date}){const i=new URL(e),p=b(q),h=p.slice(0,8),S={"x-amz-date":p,...$?{"x-amz-security-token":$}:{}},s={};for(const[c,O]of Object.entries({...n,...S}))s[c.toLowerCase()]=String(O).trim().replace(/\s+/g," ");s.host=i.host;const l=Object.keys(s).sort(),u=l.join(";"),j=[t.toUpperCase(),k(i.pathname),I(i.searchParams),l.map(c=>`${c}:${s[c]}
|
|
2
|
+
`).join(""),u,w(o)].join(`
|
|
3
|
+
`),A=`${h}/${m}/${g}/aws4_request`,C=`AWS4-HMAC-SHA256
|
|
4
|
+
${p}
|
|
5
|
+
${A}
|
|
6
|
+
${w(j)}`,z=r(r(r(r(`AWS4${U}`,h),m),g),"aws4_request"),H=f("sha256",z).update(C).digest("hex"),x=`AWS4-HMAC-SHA256 Credential=${y}/${A}, SignedHeaders=${u}, Signature=${H}`;return{headers:{...n,...S,authorization:x},authorization:x,signature:H,signedHeaders:u,canonicalRequest:j,stringToSign:C}}a(P,"sign");export{P as sign,b as stamp};
|
|
7
|
+
//# sourceMappingURL=sigv4.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var p=Object.defineProperty;var n=(e,o)=>p(e,"name",{value:o,configurable:!0});import{TranslatorError as d}from"./errors.mjs";import{credentials as m}from"./google-auth.mjs";const O="vertex-2023-10-16",c="aiplatform.googleapis.com",u=["us","eu"];function f(e){return e==="global"?c:u.includes(e)?`aiplatform.${e}.rep.googleapis.com`:`${e}-${c}`}n(f,"host");const x=[c,...u.map(f)];function _(e){const o=URL.canParse(e)?new URL(e).hostname:"";if(o===c)return"global";const t=u.find(r=>o===f(r));return t!==void 0?t:o.endsWith(`-${c}`)?o.slice(0,-c.length-1):void 0}n(_,"regionOf");function I(e,o=process.env){return e.vertex?.region??_(e.url)??(o.CLOUD_ML_REGION||void 0)}n(I,"regionFor");function C(e,o=process.env){const t=e.vertex??{};let r=t.project??(o.ANTHROPIC_VERTEX_PROJECT_ID||o.GOOGLE_CLOUD_PROJECT||o.GCLOUD_PROJECT||void 0);if(r===void 0&&t.getToken===void 0&&t.accessToken===void 0){const i=m(t,o);r=i?.project_id??i?.quota_project_id}const s=I(e,o);if(r===void 0)throw new d(`Upstream ${e.name}: no Google Cloud project; set vertex.project, ANTHROPIC_VERTEX_PROJECT_ID or GOOGLE_CLOUD_PROJECT`,{code:"INVALID_CONFIG",status:500});if(s===void 0)throw new d(`Upstream ${e.name}: no Vertex AI region; set vertex.region or CLOUD_ML_REGION`,{code:"INVALID_CONFIG",status:500});return{project:r,region:s}}n(C,"locate");function L(e,o,t,{stream:r=!1,env:s}={}){const{project:i,region:l}=C(o,s),a=`/v1/projects/${encodeURIComponent(i)}/locations/${encodeURIComponent(l)}/publishers/anthropic/models`;return e==="count"?`${a}/count-tokens:rawPredict`:`${a}/${encodeURIComponent(t??"").replaceAll("%40","@")}:${r?"streamRawPredict":"rawPredict"}`}n(L,"route");function T(e){const{model:o,...t}=e,r={anthropic_version:O,...t};return Object.defineProperty(r,"model",{value:o,enumerable:!1,writable:!0,configurable:!0}),r}n(T,"body");export{x as HOSTS,O as VERSION,T as body,f as host,C as locate,I as regionFor,_ as regionOf,L as route};
|
|
2
|
+
//# sourceMappingURL=vertex.mjs.map
|
package/dist/lib/vllm.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var c=Object.defineProperty;var
|
|
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
|
-
`)}
|
|
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.14.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,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert an Anthropic Messages request to a Converse body. The Anthropic conversion to Chat Completions does the work (system, tool blocks,
|
|
3
|
+
* images, tool names, dropped fields); `chatBody` then puts the result in Converse's shape (see there). `thinking: { type: "enabled",
|
|
4
|
+
* budget_tokens }` is sent as `additionalModelRequestFields.thinking` (the Claude form); other thinking modes and an effort are left out and
|
|
5
|
+
* named in `dropped`. The upstream's `sampling` setting and `metadata.sampling` become further `additionalModelRequestFields`.
|
|
6
|
+
* @param {any} body - Anthropic request body.
|
|
7
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
8
|
+
* @param {any} [context] - `{ producer, budget, listed, toolCallIds }`, as for the Chat Completions conversion.
|
|
9
|
+
* @returns {Promise<{body: any, state: any}>} The Converse body and the conversion state the response converters read.
|
|
10
|
+
*/
|
|
11
|
+
export function request(body: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>, context?: any): Promise<{
|
|
12
|
+
body: any;
|
|
13
|
+
state: any;
|
|
14
|
+
}>;
|
|
@@ -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,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a Converse reply to an Anthropic message: the reply is read as a Chat Completions `chat.completion` (reasoning, tool calls with the
|
|
3
|
+
* model's ids, `stopReason` as `finish_reason`, `usage` with the cache counts in the prompt) and the Chat Completions conversion builds the message.
|
|
4
|
+
* @param {any} reply - Parsed Converse reply.
|
|
5
|
+
* @param {any} [state] - State from the request converter.
|
|
6
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
7
|
+
* @returns {any} Anthropic message.
|
|
8
|
+
* @throws {import("../../../../../../../lib/errors.mjs").TranslatorError} When the reply has no message or ends in a failure.
|
|
9
|
+
*/
|
|
10
|
+
export function response(reply: any, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): any;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert ConverseStream events (`{ event, data }` messages, as `lib/eventstream` makes them from the binary frames, or Server-Sent Events text
|
|
3
|
+
* of the same events) to Anthropic events. The events become Chat Completions chunks (one id for the reply, tool calls streamed in pieces, the
|
|
4
|
+
* stop reason as the finish chunk, the `metadata` usage as the usage chunk) and the Chat Completions conversion writes the events. A failure
|
|
5
|
+
* message, an event that cannot be read and a stream that ends before `messageStop` each end the stream with an `error` event.
|
|
6
|
+
* @param {ReadableStream|AsyncIterable<unknown>|Iterable<unknown>|string|Uint8Array} source - The events.
|
|
7
|
+
* @param {any} [state] - State from the request converter.
|
|
8
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
9
|
+
* @returns {AsyncGenerator<{ event: string, data: any }, void, undefined>} Anthropic events.
|
|
10
|
+
*/
|
|
11
|
+
export function stream(source: ReadableStream | AsyncIterable<unknown> | Iterable<unknown> | string | Uint8Array, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): AsyncGenerator<{
|
|
12
|
+
event: string;
|
|
13
|
+
data: any;
|
|
14
|
+
}, void, undefined>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a Converse reply to a Chat Completions `chat.completion` (see `completion`). The model name is the one the client asked for, and the
|
|
3
|
+
* counts are kept on the state for the usage report.
|
|
4
|
+
* @param {any} reply - Parsed Converse reply.
|
|
5
|
+
* @param {any} [state] - State from the request converter.
|
|
6
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
7
|
+
* @returns {any} `chat.completion` object.
|
|
8
|
+
* @throws {import("../../../../../../../lib/errors.mjs").TranslatorError} When the reply has no message.
|
|
9
|
+
*/
|
|
10
|
+
export function response(reply: any, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): any;
|