@cldmv/llm-translator 0.13.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 +5 -5
- package/dist/api/convert/anthropic/messages/to/bedrock/converse/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/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/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/request.mjs +2 -2
- package/dist/api/send.mjs +1 -1
- package/dist/api/stream.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/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/protocol.mjs +1 -1
- package/dist/lib/route.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/package.json +1 -1
- package/types/src/api/convert/anthropic/messages/to/bedrock/converse/request.d.mts +14 -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/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/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/request.d.mts +1 -1
- package/types/src/lib/bedrock.d.mts +74 -0
- package/types/src/lib/config.d.mts +32 -3
- package/types/src/lib/converse.d.mts +81 -0
- package/types/src/lib/document-urls.d.mts +55 -0
- 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 +3 -2
- package/types/src/lib/protocol.d.mts +3 -0
- package/types/src/lib/settings.d.mts +12 -0
- package/types/src/lib/sigv4.d.mts +41 -0
- package/types/src/lib/vertex.d.mts +55 -0
package/README.md
CHANGED
|
@@ -17,18 +17,18 @@ Use it as a library when you want the conversion without any I/O (`request`, `re
|
|
|
17
17
|
|
|
18
18
|
## ✨ What's New
|
|
19
19
|
|
|
20
|
-
### Latest: v0.
|
|
20
|
+
### Latest: v0.14.0 (November 2026)
|
|
21
21
|
|
|
22
|
-
- **
|
|
23
|
-
- **
|
|
24
|
-
- [View full v0.
|
|
22
|
+
- **Claude on Google Vertex AI and Amazon Bedrock** — new `vertex` and `bedrock` drivers. Vertex AI gets its access tokens from a service account, gcloud's credentials, a token or `getToken`, or the metadata server. Bedrock signs with SigV4 or uses a Bedrock API key, sends Claude through InvokeModel and other models through the new Converse protocol (`api: "converse"`), and decodes AWS's event stream. No new dependencies.
|
|
23
|
+
- **URL documents** — `documentUrls: { allow: [...] }` downloads `document` blocks with a `url` source and converts them like inline PDFs or text. Off by default, with host allow-listing, public-address checks pinned to the connection, re-checked redirects and size, time and count limits.
|
|
24
|
+
- [View full v0.14.0 Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.14.0.md)
|
|
25
25
|
|
|
26
26
|
### Recent Releases
|
|
27
27
|
|
|
28
|
+
- **v0.13.0** (November 2026) — the OpenAI Responses API as a front and as an upstream, and one per-upstream `api` setting that defaults to `auto` ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.13.0.md))
|
|
28
29
|
- **v0.12.0** (November 2026) — Prometheus `/metrics`, HTTPS, per-key rate limits and CORS for the built-in server, and failed tool results marked by default over Chat Completions ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.12.0.md))
|
|
29
30
|
- **v0.11.0** (November 2026) — native constrained decoding (JSON Schema, regex, choice, grammar), `clear_tool_uses` context edits, `cache_salt` and `prompt_cache_key` hints, and host-first binding for hosted APIs ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.11.0.md))
|
|
30
31
|
- **v0.10.0** (November 2026) — native `/v1/messages` for vLLM, readiness probes and LoRA adapters, Groq, Together, Fireworks, xAI and Gemini drivers, and Ollama's native `/api/chat` ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.10.0.md))
|
|
31
|
-
- **v0.9.0** (November 2026) — Azure OpenAI, OpenRouter, Hugging Face TGI and LocalAI drivers, with Azure's `apiVersion` and `deployment` upstream fields and more startup probes ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.9.0.md))
|
|
32
32
|
|
|
33
33
|
📚 **For complete version history and detailed release notes, see the [docs/changelog/](https://github.com/CLDMV/llm-translator/tree/master/docs/changelog/) folder.**
|
|
34
34
|
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var l=Object.defineProperty;var i=(e,r)=>l(e,"name",{value:r,configurable:!0});import{resolve as g}from"../../../../../../../lib/settings.mjs";import{chatBody as p}from"../../../../../../../lib/converse.mjs";import{request as u}from"../../openai/chat/request.mjs";const f={maxTokensField:"max_tokens",thinking:"none",thinkingBudget:!1,effort:!1,topK:!0,prefill:"none",streamUsage:!1,continuousUsage:!1,reasoningRoundTrip:!1};async function y(e,r,s){const t=g(r),{body:a,state:n}=await u(e,{...t,...f},s),o=e.metadata?.sampling,d={...t.sampling,...o!==null&&typeof o=="object"&&!Array.isArray(o)?o:{}};return n.thinking!==null&&n.thinking!=="disabled"&&n.thinkingBudget===void 0&&n.dropped.push("thinking"),{body:p(a,{dropped:n.dropped,sampling:d,thinkingBudget:n.thinkingBudget,marker:t.toolErrors?t.toolErrorMarker:void 0}),state:n}}i(y,"request");export{y as request};
|
|
2
|
+
//# sourceMappingURL=request.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var m=Object.defineProperty;var e=(o,r)=>m(o,"name",{value:r,configurable:!0});import{completion as n}from"../../../../../../../lib/converse.mjs";import{response as p}from"../../../../../openai/chat/to/anthropic/messages/response.mjs";function a(o,r,t){return p(n(o),r,t)}e(a,"response");export{a as response};
|
|
2
|
+
//# sourceMappingURL=response.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var i=Object.defineProperty;var n=(e,r)=>i(e,"name",{value:r,configurable:!0});import{resolve as m}from"../../../../../../../lib/settings.mjs";import{createChunker as s}from"../../../../../../../lib/converse.mjs";import{messages as f}from"../../../../../../../lib/sse.mjs";import{stream as c}from"../../../../../openai/chat/to/anthropic/messages/stream.mjs";function g(e,r,o){const t=s({reasoningField:m(o).reasoningField});return c((async function*(){for await(const a of f(e))yield*t.event(a.event,a.data);t.finished()&&(yield{data:"[DONE]"})})(),r,o)}n(g,"stream");export{g as stream};
|
|
2
|
+
//# sourceMappingURL=stream.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var m=Object.defineProperty;var r=(e,o)=>m(e,"name",{value:o,configurable:!0});import{resolve as s}from"../../../../../../../lib/settings.mjs";import{completion as d,folded as l}from"../../../../../../../lib/converse.mjs";function u(e,o,n){const i=d(e,{reasoningField:s(n).reasoningField,model:o?.model});return o&&(o.upstreamUsage=l(e.usage)),i}r(u,"response");export{u as response};
|
|
2
|
+
//# sourceMappingURL=response.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var l=Object.defineProperty;var i=(o,r)=>l(o,"name",{value:r,configurable:!0});import{resolve as c}from"../../../../../../../lib/settings.mjs";import{createChunker as m,folded as u}from"../../../../../../../lib/converse.mjs";import{messages as f}from"../../../../../../../lib/sse.mjs";async function*k(o,r,s){const a=m({model:r?.model,reasoningField:c(s).reasoningField,includeUsage:r?.includeUsage??!0}),d=i((e,n="api_error")=>({data:{error:{message:e,type:n,param:null,code:null}}}),"failure");try{for await(const e of f(o)){const n=a.event(e.event,e.data);r&&a.usage()!==void 0&&(r.upstreamUsage=u(a.usage()));for(const t of n)if(yield{data:t},t.error){yield{data:"[DONE]"};return}}}catch(e){yield d(e.message,e.type??"api_error"),yield{data:"[DONE]"};return}a.finished()||(yield d("Upstream stream ended before completion")),yield{data:"[DONE]"}}i(k,"stream");export{k as stream};
|
|
2
|
+
//# sourceMappingURL=stream.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var a=Object.defineProperty;var t=(r,e)=>a(r,"name",{value:e,configurable:!0});import{resolve as m}from"../../../../../../../lib/settings.mjs";import{TranslatorError as n}from"../../../../../../../lib/errors.mjs";import{chatBody as i}from"../../../../../../../lib/converse.mjs";function c(r,e){const o=m(e);if(r===null||typeof r!="object"||!Array.isArray(r.messages))throw new n("messages: Field required",{code:"invalid_request",param:"messages"});const s={front:"openai",model:r.model,stream:r.stream===!0,toolNames:{},toolIds:{},dropped:[],includeUsage:r.stream_options?.include_usage===!0};return{body:i(r,{dropped:s.dropped,sampling:o.sampling,marker:o.toolErrors?o.toolErrorMarker:void 0}),state:s}}t(c,"request");export{c as request};
|
|
2
|
+
//# sourceMappingURL=request.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var e=Object.defineProperty;var t=(n,o)=>e(n,"name",{value:o,configurable:!0});import{regionOfHost as l}from"../../../lib/bedrock.mjs";function c({endpoint:n}){const o=URL.canParse(n.url)?new URL(n.url).hostname:"",r=l(o);return r===null?null:{host:o,region:r}}t(c,"detect");export{c as detect};
|
|
2
|
+
//# sourceMappingURL=detect.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=Object.defineProperty;var e=(o,a)=>r(o,"name",{value:a,configurable:!0});function t(){return{name:"bedrock",protocol:"anthropic.messages",protocols:["anthropic.messages","bedrock.converse"],order:33,auth:"bearer",strict:!1,frames:"eventstream",hostSuffixes:[".amazonaws.com",".amazonaws.com.cn"],paths:{}}}e(t,"info");export{t as info};
|
|
2
|
+
//# sourceMappingURL=info.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var m=Object.defineProperty;var i=(r,e)=>m(r,"name",{value:e,configurable:!0});import*as s from"../../../../lib/http.mjs";import{authenticate as u}from"../../../../lib/bedrock.mjs";function c(r){const e=new URL(r);return e.hostname=e.hostname.replace(/(^|\.)bedrock-runtime/,"$1bedrock"),`${e.origin}${e.pathname.replace(/\/$/,"")}/foundation-models`}i(c,"listUrl");async function f({endpoint:r,transport:e,signal:n,timeout:d}){let o,l;try{o=c(r.url),l=u(r,{method:"GET",url:o,headers:{accept:"application/json"},body:""})}catch{return null}const a=await s.json(e,o,{method:"GET",headers:l,signal:n,timeout:d});return!a.ok||!Array.isArray(a.body?.modelSummaries)?null:a.body.modelSummaries.filter(t=>typeof t?.modelId=="string"&&(t.outputModalities??["TEXT"]).includes("TEXT")).map(t=>({id:t.modelId,owned_by:t.providerName??"bedrock"}))}i(f,"list");export{f as list};
|
|
2
|
+
//# sourceMappingURL=list.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var t=Object.defineProperty;var s=(e,r)=>t(e,"name",{value:r,configurable:!0});import{TranslatorError as a}from"../../../lib/errors.mjs";import{modelPath as n}from"../../../lib/bedrock.mjs";function c(e,{model:r,stream:o}){if(e==="messages")return`${n(r)}/${o?"invoke-with-response-stream":"invoke"}`;if(e==="converse")return`${n(r)}/${o?"converse-stream":"converse"}`;throw new a(`The bedrock driver has no ${e} call; it serves Anthropic Messages (InvokeModel) and Converse`,{code:"unsupported_endpoint",status:501})}s(c,"path");export{c as path};
|
|
2
|
+
//# sourceMappingURL=path.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var c=Object.defineProperty;var r=(e,o)=>c(e,"name",{value:o,configurable:!0});const n=["model","stream","metadata","service_tier","container","mcp_servers"],a="bedrock-2023-05-31";function d(e,o){const t={...e};for(const s of n)delete t[s];return o?.protocol==="anthropic.messages"?{anthropic_version:a,...t}:t}r(d,"request");export{d as request};
|
|
2
|
+
//# sourceMappingURL=request.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var c=Object.defineProperty;var a=(t,n)=>c(t,"name",{value:n,configurable:!0});import{authenticate as p}from"../../../lib/bedrock.mjs";const s="application/vnd.amazon.eventstream";function f({endpoint:t,method:n,url:e,headers:i,body:o,stream:r}){return p(t,{method:n,url:e,headers:{...i,accept:r?s:"application/json"},body:JSON.stringify(o)})}a(f,"sign");export{f as sign};
|
|
2
|
+
//# sourceMappingURL=sign.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var s=Object.defineProperty;var r=(e,t)=>s(e,"name",{value:t,configurable:!0});import{token as c}from"../../../lib/google-auth.mjs";async function h({endpoint:e,headers:t,transport:a,timeout:i}){const{"x-api-key":_,"anthropic-version":p,...o}=t,n=await c({settings:e.vertex,apiKey:e.apiKey,transport:a,timeout:i});return{...o,authorization:`Bearer ${n}`}}r(h,"auth");export{h as auth};
|
|
2
|
+
//# sourceMappingURL=auth.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var u=Object.defineProperty;var o=(t,s)=>u(t,"name",{value:s,configurable:!0});import{info as c}from"./info.mjs";function i({endpoint:t}){const s=URL.canParse(t.url)?new URL(t.url).hostname:"",{hosts:e,hostSuffixes:n}=c();return e.includes(s)||n.some(r=>s.endsWith(r))?{host:s}:null}o(i,"detect");export{i as detect};
|
|
2
|
+
//# sourceMappingURL=detect.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var n=Object.defineProperty;var o=(e,r)=>n(e,"name",{value:r,configurable:!0});import*as p from"../../../lib/upstream-error.mjs";function m(e,r){const t=Array.isArray(r)?r[0]:r,s=p.parse(t,e),a=typeof t?.error?.status=="string"?t.error.status:s.code;return{error:{message:s.message,type:s.type,param:s.param??null,code:a??e}}}o(m,"error");export{m as error};
|
|
2
|
+
//# sourceMappingURL=error.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var e=Object.defineProperty;var o=(r,t)=>e(r,"name",{value:t,configurable:!0});import{HOSTS as s}from"../../../lib/vertex.mjs";function p(){return{name:"vertex",protocol:"anthropic.messages",protocols:["anthropic.messages"],order:39,auth:"bearer",strict:!1,hosts:s,hostSuffixes:["-aiplatform.googleapis.com"],paths:{}}}o(p,"info");export{p as info};
|
|
2
|
+
//# sourceMappingURL=info.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var u=Object.defineProperty;var i=(o,t)=>u(o,"name",{value:t,configurable:!0});import*as m from"../../../../lib/http.mjs";import{token as l}from"../../../../lib/google-auth.mjs";import{route as f}from"../../../../lib/vertex.mjs";const y=["messages","system","tools","tool_choice","thinking","mcp_servers","context_management"];async function k({endpoint:o,request:t,transport:s,signal:r,timeout:c}){const a={model:t.model};for(const e of y)t[e]!==void 0&&(a[e]=t[e]);try{const e=await l({settings:o.vertex,apiKey:o.apiKey,transport:s,timeout:c,signal:r}),n=await m.json(s,m.join(o.url,f("count",o)),{method:"POST",headers:{"content-type":"application/json",...o.headers,authorization:`Bearer ${e}`},body:a,signal:r,timeout:c});return n.ok&&typeof n.body?.input_tokens=="number"?n.body.input_tokens:null}catch{return null}}i(k,"count");export{k as count};
|
|
2
|
+
//# sourceMappingURL=count.mjs.map
|
package/dist/api/request.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var
|
|
2
|
-
${
|
|
1
|
+
var N=Object.defineProperty;var h=(e,n)=>N(e,"name",{value:n,configurable:!0});import{context as H,self as V}from"../lib/runtime.mjs";import{capture as z}from"../lib/capture.mjs";import{resolve as A}from"../lib/route.mjs";import*as U from"../lib/http.mjs";import{filterChatBody as D}from"../lib/passthrough.mjs";import{addSampling as G}from"../lib/sampling.mjs";import{addConstraint as J}from"../lib/constraint.mjs";import{addCache as Q}from"../lib/cache.mjs";import*as q from"../lib/images.mjs";import*as I from"../lib/tools.mjs";import{LIMITS as W}from"../lib/toolsearch.mjs";import{warn as _}from"../lib/warn.mjs";import{emit as P}from"../lib/events.mjs";import{fit as X,pinBody as Y}from"../lib/effort.mjs";import{relax as Z,strictTools as ee}from"../lib/strict.mjs";import{TranslatorError as te}from"../lib/errors.mjs";import*as ne from"../lib/vllm.mjs";import*as R from"../lib/document-urls.mjs";import{PROTOCOL as O}from"../lib/responses-upstream.mjs";import{CONVERSE as re}from"../lib/protocol.mjs";function ae(e,n){return`${e.driver}:${n}:${URL.canParse(e.url)?new URL(e.url).host:e.url}`}h(ae,"producerOf");async function $e(e,n={}){const a=z(V,H,n.front),u=A(a,e,{endpoint:n.endpoint,wire:n.wire,tokens:n.tokens,advance:n.hold===!0&&n.endpoint===void 0});if(n.hold!==!0)return L(a,e,n,u);u.endpoint.health.inflight++;try{return await L(a,e,n,u)}catch(r){throw u.endpoint.health.inflight--,r}}h($e,"request");function oe(e,n,a,u,r,f){const k=ee(n);if(k.length===0)return n;const m=k.filter(d=>d.problems.length>0);if(m.length>0&&r.unknownFields==="error")throw new te(`tools: ${m[0].name}: schema is outside the strict subset (${m[0].problems.join("; ")})`,{code:"unsupported_content",param:"tools"});const j=Z(n);if(f.retry===!0)return j;const g=h((d,l)=>_(e.config,e.state,d,{kind:l,endpoint:a.name,log:f.log}),"tell");for(const d of m)g(`Upstream ${a.name}: strict tool ${d.name} has a schema outside the strict subset: ${d.problems.join("; ")} (sent without strict)`,"strict");const s=k.filter(d=>d.problems.length===0);return u.info.strict!==!0&&s.length>0&&g(`Upstream ${a.name}: the ${u.name} backend does not enforce strict tool use (${s.map(d=>d.name).join(", ")})`,"strict"),j}h(oe,"checkStrict");async function L(e,n,a,u){const{endpoint:r,model:f,unserved:k,convert:m,pool:j,pin:g,protocol:s}=u,d=Object.hasOwn(e.config.efforts,f)?e.config.efforts[f]:void 0;g&&(n=Y(n,g,e.front));const l=e.driver(r),T=e.front==="anthropic"?n.metadata?.prompt_cache_key:void 0;let S=e.front==="anthropic"?n.metadata?.constraint:void 0;e.front==="openai"&&n.constraint!==void 0&&({constraint:S,...n}=n);const $=`${r.name}
|
|
2
|
+
${f}`,C=a.images===!1||e.state.textOnly.has($)&&e.settings(r,s).imageFallback,b=e.settings(r,s),M=a.jsonObject==="instruction"||e.state.jsonInstruction.has($)&&b.jsonObjectFallback,c={...C?{...b,images:!1}:b,...M?{jsonObject:"instruction"}:{},...g?.effort===void 0?{}:{effort:!0}},v=n.stream===!0,y=s==="anthropic.messages"&&m===null&&l.info.protocol!=="anthropic.messages";m&&e.front==="anthropic"&&R.wanted(c)&&(n=await R.resolve(n,{options:c.documentUrls,signal:a.signal,report:h(i=>P(e,{type:"document",...i,endpoint:r.name,model:f,log:a.log}),"report")}));let t,o;m?({body:t,state:o}=await m.request(n,c,{producer:ae(r,s),budget:a.budget,listed:d!==void 0,toolCallIds:l.info.toolCallIds}),o.toolSearch?.capped&&_(e.config,e.state,`Tool search: more than ${W.definitions} tool definitions came back in ToolSearch results for ${r.name}; the rest are not offered.`,{kind:"toolsearch",endpoint:r.name,log:a.log})):y?t=ne.request(n,c):t=e.front==="openai"&&(a.filter??c.passthroughFilter===!0)?D(n,l.info.fields):{...n};const F=e.front==="anthropic"?n.metadata?.sampling:void 0;if(s==="openai.chat"){const i=[];t=G(t,F,c,l.info.fields,i),o&&o.dropped.push(...i.filter(p=>!o.dropped.includes(p)))}else if(!m&&F!==void 0&&t.metadata?.sampling!==void 0){const{sampling:i,...p}=t.metadata;t={...t,metadata:p}}if(s==="openai.chat"){const i=[],p={name:l.info.name,version:r.facts?.version??r.version};t=J(t,S,c,p,i),t=Q(t,{salt:a.cacheSalt,key:T},c,p.name,i),o&&o.dropped.push(...i.filter(K=>!o.dropped.includes(K)))}else{if(o&&!o.dropped.includes("cache_salt")&&(a.cacheSalt??c.cacheSalt)!=null&&o.dropped.push("cache_salt"),o&&!o.dropped.includes("prompt_cache_key")&&(T??c.promptCacheKey)!=null&&o.dropped.push("prompt_cache_key"),!m&&t.metadata?.prompt_cache_key!==void 0){const{prompt_cache_key:i,...p}=t.metadata;t={...t,metadata:p}}if(o&&(S??c.constraint)!=null&&!o.dropped.includes("constraint")&&o.dropped.push("constraint"),!m&&t.metadata?.constraint!==void 0){const{constraint:i,...p}=t.metadata;t={...t,metadata:p}}}if(a.usage===!0&&v&&e.front==="openai"){if(m)o.includeUsage=!0;else if(c.streamUsage){const i=t.stream_options&&typeof t.stream_options=="object"?t.stream_options:{};t.stream_options={...i,include_usage:!0}}}C&&!m&&e.front==="openai"&&(t=q.strip(t)),a.tools===!1&&(t=I.strip(t)),t.model=f,t=oe(e,t,r,l,c,a),d&&s==="openai.chat"&&X(t,d,f,(i,p)=>_(e.config,e.state,i,{type:"effort",endpoint:r.name,model:f,...p,log:a.log})),o?.reasoningMissing&&s!==O&&_(e.config,e.state,`Upstream ${r.name}: assistant turns with tool calls have no reasoning to send back (the client dropped its thinking blocks or their signatures), which servers that need it with tools will refuse.`,{kind:"reasoning",endpoint:r.name,log:a.log}),l.request&&!y&&s!==O&&(t=await l.request(t,{endpoint:r,settings:c,levels:d,protocol:s,state:o,warn:h(i=>_(e.config,e.state,i,{kind:"driver",endpoint:r.name,log:a.log}),"warn")})),o?.dropped?.length>0&&a.retry!==!0&&P(e,{type:"dropped",level:"debug",message:`Upstream ${r.name}: dropped ${o.dropped.join(", ")}`,endpoint:r.name,model:f,fields:[...o.dropped],log:a.log});const B=s==="anthropic.messages"?"messages":s===O?"responses":s===re?"converse":"chat",x=await U.pathOf(l,B,r,f,s,v),E=U.join(r.url,x);let w={"content-type":"application/json",accept:v?"text/event-stream":"application/json",...l.info.headers,...m===null&&r.protocol==="anthropic.messages"?a.clientHeaders:void 0,...U.headers(r,l.info.auth,a.headers)};return l.auth&&a.tokens!==!0&&(w=await l.auth({endpoint:r,headers:w,transport:e.state.transport,timeout:e.config.detectTimeout})),l.sign&&(w=await l.sign({endpoint:r,protocol:s,method:"POST",url:E,headers:w,body:t,stream:v})),{upstream:r.name,pool:j.map(i=>i.name),method:"POST",url:E,path:x,headers:w,body:t,state:{v:1,upstream:r.name,front:e.front,native:m===null,protocol:s,messages:y,responses:s===O,model:n.model,stream:v,unserved:k,convert:o},more:u.more,images:{sent:q.carries(t),retry:c.imageFallback&&s==="openai.chat",key:$},tools:{sent:I.carries(t),retry:c.toolFallback&&s==="openai.chat"},json:{sent:o?.jsonSchemaEmulated===!0,retry:c.jsonObjectFallback&&s==="anthropic.messages",key:$}}}h(L,"prepare");export{$e as request};
|
|
3
3
|
//# sourceMappingURL=request.mjs.map
|
package/dist/api/send.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var me=Object.defineProperty;var i=(n,r)=>me(n,"name",{value:r,configurable:!0});import{context as K,self as ee}from"../lib/runtime.mjs";import{TranslatorError as C}from"../lib/errors.mjs";import*as te from"../lib/http.mjs";import*as pe from"../lib/images.mjs";import*as de from"../lib/tools.mjs";import{parse as ae}from"../lib/upstream-error.mjs";import{meter as fe,normalize as ge}from"../lib/usage.mjs";import{capture as he}from"../lib/capture.mjs";import{estimate as ye,bodyLength as we,output as be,patch as ke,stream as Se}from"../lib/estimate.mjs";import{warn as se}from"../lib/warn.mjs";import{start as je}from"../lib/record.mjs";import{emit as re}from"../lib/events.mjs";import{MISSING as ve}from"../lib/responses-upstream.mjs";import{MESSAGES as $e,RESPONSES as _e,remember as Te}from"../lib/protocol.mjs";import*as ne from"../lib/pool.mjs";import{createBudget as xe}from"../lib/thinking.mjs";import*as oe from"../lib/fanout.mjs";import{refresh as Oe}from"../lib/health.mjs";const Ee={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"};async function Ue(n,r={}){const{transport:w,textOnly:d,jsonInstruction:$}=K.state,{request:_,response:T,stream:l,error:g}=ee,p=g.build,{config:u,state:k}=K,h=he(ee,K,r.front),U=n&&typeof n=="object"&&!Array.isArray(n),x={model:null,requested:U&&typeof n.model=="string"?n.model:null,upstream:null,front:r.front??u.front,stream:U&&n.stream===!0},S=fe(x,r.onUsage,(m,N)=>se(u,k,m,{type:"usage",...N,log:r.log})),A=i(m=>Object.defineProperty(m,"usage",{value:S.usage,enumerable:!1,configurable:!0}),"attach");let M=!1;try{const m=oe.wants(h,n);if(m>0){M=!0;const N=i((o,O)=>se(u,k,o,{type:"usage",...O,log:r.log}),"warning");return await oe.run({body:n,count:m,options:r,send:Ue,warn:N})}return A(await Ae({transport:w,textOnly:d,jsonInstruction:$,request:_,response:T,stream:l,error:g,captured:h},n,r,S))}catch(m){if(M)throw m;if(!(m instanceof C))throw r.signal?.aborted||re(h,{type:"error",kind:"exception",level:"error",message:String(m?.message??m),log:r.log}),S.finish(r.signal?.aborted?"aborted":"error"),m;return S.finish("error"),A(p(m.status,m.message,{type:m.type,code:m.code,param:m.param,front:r.front}))}}i(Ue,"send");async function Ae({transport:n,textOnly:r,jsonInstruction:w,request:d,response:$,stream:_,error:T,captured:l},g,p,u){const k={front:p.front,wire:p.wire,headers:p.headers,clientHeaders:p.clientHeaders,filter:p.filter,cacheSalt:p.cacheSalt,usage:!0,budget:xe(),log:p.log},h=i(t=>{re(l,{...t,log:p.log})},"announce"),U=u.meta.front==="openai"&&g?.stream_options?.include_usage!==!0,x=i(t=>{u.meta.model=t.body.model??null,u.meta.upstream=t.upstream,t.state.native?u.source(null):u.source(()=>t.state.convert?.upstreamUsage,ie(t.state))},"note"),S=i(t=>{if(t.state.native)return{input:!1,output:!1};const e=ge(ie(t.state),t.state.convert?.upstreamUsage);return e===null?{input:!0,output:!0}:{input:e.input_tokens===null&&e.cache_read_input_tokens===null&&e.cache_creation_input_tokens===null,output:e.output_tokens===null}},"gaps"),A=i(async t=>{const e=l.endpoint(t.upstream),a=l.driver(e);return(a.count&&!t.state.responses&&!(U&&t.state.stream)?await a.count({endpoint:e,request:t.body,transport:n,signal:p.signal,timeout:l.config.detectTimeout}):null)??ye(t.body)},"input"),M=i(async(t,e)=>{const a=S(t);return!a.input&&!a.output?null:{input_tokens:a.input?await A(t):null,output_tokens:a.output?be(e):null}},"counts"),m=i((t,e,a,s,j)=>(u.finish(j),T(t,e,a,s)),"fail"),N=i(async t=>{let e=t,a=await Q(n,e,p.signal,B());const s=a.status,j=e.state.protocol===$e,ue=e.state.protocol===_e;if(e.more&&(s===404&&j||ve.includes(s)&&ue)){const y=await I(a);if(j&&y?.type==="error")return{upstream:e,result:a,failure:y};Te(l.state,e.upstream,e.state.protocol);const b=j?"messages":"responses",f=j?"Messages API (/v1/messages)":"Responses API (/v1/responses)";return h({type:"fallback",kind:b,level:"warn",message:`Upstream ${e.upstream}: ${f} answered ${s}; sent the request over the next protocol, and will do so for this upstream until the next detection. Set api on this upstream (for example "chat") to skip the first attempt.`,endpoint:e.upstream,model:e.body.model,status:s,reason:String(ae(y,s).message).trim().slice(0,500),action:j?"conversion":"chat"}),e=await d(g,{...k,endpoint:e.upstream,retry:!0}),x(e),{upstream:e,result:await Q(n,e,p.signal,B())}}const ce=s===400&&[e.images,e.tools].some(y=>y.sent&&y.retry),L=(s===400||s===422)&&e.json.sent&&e.json.retry;if(!a.ok&&(ce||L)){const y=await I(a),b=ae(y,s).message,f={};if(s===400&&e.images.sent&&e.images.retry&&pe.refused(b)?(r.add(e.images.key),f.images=!1):s===400&&e.tools.sent&&e.tools.retry&&de.refused(b)?f.tools=!1:L&&(f.jsonObject="instruction"),!("images"in f||"tools"in f||"jsonObject"in f))return{upstream:e,result:a,failure:y};const v={upstream:e,result:a,failure:y};if("images"in f||"tools"in f){const F="images"in f?"images":"tools";h({type:"fallback",kind:F,level:"warn",message:`Upstream ${e.upstream}: ${e.state.model} refused ${F==="images"?"images":"tools"} (${s}: "${String(b).trim().slice(0,500)}"); sent the request again without them.`,endpoint:e.upstream,model:e.body.model,status:s,reason:String(b).trim().slice(0,500),action:F==="images"?"text-only":"without-tools"})}if(e=await d(g,{...k,...f,endpoint:e.upstream,retry:!0}),x(e),a=await Q(n,e,p.signal,B()),L&&"jsonObject"in f){if(!a.ok){try{await a.body?.cancel?.()}catch{}return x(v.upstream),v}w.add(v.upstream.json.key),h({type:"fallback",kind:"json_object",level:"warn",message:`Upstream ${v.upstream.upstream}: ${v.upstream.state.model} rejected the json_object schema emulation (${s}: "${String(b).trim().slice(0,500)}"); sent it as a system instruction instead. Set jsonObject: "instruction" on this upstream to skip the first attempt.`,endpoint:v.upstream.upstream,model:v.upstream.body.model,status:s,reason:String(b).trim().slice(0,500),action:"instruction"})}}return{upstream:e,result:a}},"attempt");await Oe(l,g,p.signal);let o=await d(g,{...k,hold:!0});const O=o.pool;let G=0;const z=O.length>1?l.config.pool:null,B=i(()=>z&&{connectTimeout:z.connectTimeout,headersTimeout:G+1<O.length?z.headersTimeout:0},"limitsNow");let P=l.endpoint(o.upstream);const V=i(()=>P.health.inflight--,"release");let c,E,W=null,X=0;const Y=i(t=>{const e=je(l,{body:g,sent:o,front:u.meta.front,requested:u.meta.requested,headers:t,log:p.log,exchange:W,attempt:X});return W??=e.id,e},"begin"),Z=i(async t=>{const e=Y();if(!e.on)return;if(c===void 0)return e.attempt({error:E.message},t);let a=null;try{a=E??await I(c)}catch{}e.attempt({status:c.status,headers:te.plain(c.headers),body:a},t)},"keep");try{for(;;){const t=P;t.health.requests++,X++,x(o);let e=null;try{({upstream:o,result:c,failure:E}=await N(o)),!c.ok&&c.status>=500&&(e=`HTTP ${c.status}`,h({type:"error",kind:"upstream",level:"error",message:`Upstream ${t.name} answered HTTP ${c.status}`,endpoint:t.name,model:o.body.model,status:c.status}))}catch(s){if(!(s instanceof C)||s.code!=="upstream_unreachable")throw s;e=s.message,c=void 0,E=s,h({type:"error",kind:"transport",level:"error",message:s.message,endpoint:t.name,model:o.body.model})}if(e===null){ne.succeeded(t);break}ne.failed(l.config,t,e);let a=null;for(;a===null&&!p.signal?.aborted&&G+1<O.length;)try{a=await d(g,{...k,endpoint:O[++G],hold:!0})}catch(s){if(!(s instanceof C)||s.code!=="endpoint_not_in_pool")throw s}if(a===null){if(c===void 0)throw await Z(),E;break}await Z(e),h({type:"failover",level:"warn",message:`Upstream ${t.name} failed (${e}); trying ${a.upstream}`,from:t.name,to:a.upstream,model:a.body.model,reason:e});try{await c?.body?.cancel?.()}catch{}V(),P=l.endpoint(a.upstream),o=a}}catch(t){throw V(),t}const le=P;P=null,u.usage.then(()=>le.health.inflight--);const q=te.plain(c.headers),J=Y(q);if(!c.ok){const t=E??await I(c);return J.reply(c.status,q,t),m(c.status,t,q,o.state,"upstream_error")}if(o.state.stream){const t=_(J.raw(c.body),o.state),e=o.state.native?t:Se(u.meta.front,t,{gaps:i(()=>S(o),"gaps"),input:i(()=>A(o),"input"),report:i(a=>u.estimate(a),"report")});return{status:200,headers:Ee,stream:J.events(u.track(e,p.signal,U))}}const R=await I(c);if(J.reply(c.status,q,R),R===null||typeof R!="object")return h({type:"error",kind:"upstream",level:"error",message:`Upstream ${o.upstream} returned a body that is not JSON`,endpoint:o.upstream,model:o.body.model,status:502}),m(502,"The upstream returned a body that is not JSON",q,o.state,"error");const H=await $(R,o.state),D=await M(o,we(u.meta.front,H));return D&&(H.usage=ke(u.meta.front,H.usage,D),u.estimate(D)),u.json(H),u.finish("done"),{status:200,headers:{"content-type":"application/json"},body:H}}i(Ae,"round");function ie(n){return n.front==="anthropic"||n.responses?"openai":"folded"}i(ie,"usageShape");async function Q(n,r,w,d=null){let $,_=!1,T=w;if(d&&d.headersTimeout>0){const l=new AbortController;T=w?AbortSignal.any([w,l.signal]):l.signal,$=setTimeout(()=>{_=!0,l.abort()},d.headersTimeout)}try{return await n(r.url,{method:r.method,headers:r.headers,body:JSON.stringify(r.body),signal:T,redirect:"error",...d&&d.connectTimeout>0?{connectTimeout:d.connectTimeout}:{}})}catch(l){if(w?.aborted)throw l;const g=_?`no response headers within ${d.headersTimeout} ms`:l.message;throw new C(`Could not reach the upstream (${r.upstream}): ${g}`,{code:"upstream_unreachable",status:502,details:{cause:l}})}finally{clearTimeout($)}}i(Q,"call");async function I(n){const r=await n.text();try{return JSON.parse(r)}catch{return r===""?null:r}}i(I,"parse");export{Ue as send};
|
|
1
|
+
var me=Object.defineProperty;var i=(n,r)=>me(n,"name",{value:r,configurable:!0});import{context as K,self as ee}from"../lib/runtime.mjs";import{TranslatorError as C}from"../lib/errors.mjs";import*as te from"../lib/http.mjs";import*as pe from"../lib/images.mjs";import*as de from"../lib/tools.mjs";import{parse as ae}from"../lib/upstream-error.mjs";import{meter as fe,normalize as ge}from"../lib/usage.mjs";import{capture as he}from"../lib/capture.mjs";import{estimate as ye,bodyLength as we,output as be,patch as ke,stream as Se}from"../lib/estimate.mjs";import{warn as se}from"../lib/warn.mjs";import{start as je}from"../lib/record.mjs";import{emit as re}from"../lib/events.mjs";import{MISSING as ve}from"../lib/responses-upstream.mjs";import{MESSAGES as $e,RESPONSES as _e,remember as Te}from"../lib/protocol.mjs";import*as ne from"../lib/pool.mjs";import{createBudget as xe}from"../lib/thinking.mjs";import*as oe from"../lib/fanout.mjs";import{refresh as Oe}from"../lib/health.mjs";const Ee={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"};async function Ue(n,r={}){const{transport:w,textOnly:d,jsonInstruction:$}=K.state,{request:_,response:T,stream:l,error:g}=ee,p=g.build,{config:u,state:k}=K,h=he(ee,K,r.front),U=n&&typeof n=="object"&&!Array.isArray(n),x={model:null,requested:U&&typeof n.model=="string"?n.model:null,upstream:null,front:r.front??u.front,stream:U&&n.stream===!0},S=fe(x,r.onUsage,(m,N)=>se(u,k,m,{type:"usage",...N,log:r.log})),A=i(m=>Object.defineProperty(m,"usage",{value:S.usage,enumerable:!1,configurable:!0}),"attach");let M=!1;try{const m=oe.wants(h,n);if(m>0){M=!0;const N=i((o,O)=>se(u,k,o,{type:"usage",...O,log:r.log}),"warning");return await oe.run({body:n,count:m,options:r,send:Ue,warn:N})}return A(await Ae({transport:w,textOnly:d,jsonInstruction:$,request:_,response:T,stream:l,error:g,captured:h},n,r,S))}catch(m){if(M)throw m;if(!(m instanceof C))throw r.signal?.aborted||re(h,{type:"error",kind:"exception",level:"error",message:String(m?.message??m),log:r.log}),S.finish(r.signal?.aborted?"aborted":"error"),m;return S.finish("error"),A(p(m.status,m.message,{type:m.type,code:m.code,param:m.param,front:r.front}))}}i(Ue,"send");async function Ae({transport:n,textOnly:r,jsonInstruction:w,request:d,response:$,stream:_,error:T,captured:l},g,p,u){const k={front:p.front,wire:p.wire,headers:p.headers,clientHeaders:p.clientHeaders,filter:p.filter,cacheSalt:p.cacheSalt,signal:p.signal,usage:!0,budget:xe(),log:p.log},h=i(t=>{re(l,{...t,log:p.log})},"announce"),U=u.meta.front==="openai"&&g?.stream_options?.include_usage!==!0,x=i(t=>{u.meta.model=t.body.model??null,u.meta.upstream=t.upstream,t.state.native?u.source(null):u.source(()=>t.state.convert?.upstreamUsage,ie(t.state))},"note"),S=i(t=>{if(t.state.native)return{input:!1,output:!1};const e=ge(ie(t.state),t.state.convert?.upstreamUsage);return e===null?{input:!0,output:!0}:{input:e.input_tokens===null&&e.cache_read_input_tokens===null&&e.cache_creation_input_tokens===null,output:e.output_tokens===null}},"gaps"),A=i(async t=>{const e=l.endpoint(t.upstream),a=l.driver(e);return(a.count&&!t.state.responses&&!(U&&t.state.stream)?await a.count({endpoint:e,request:t.body,transport:n,signal:p.signal,timeout:l.config.detectTimeout}):null)??ye(t.body)},"input"),M=i(async(t,e)=>{const a=S(t);return!a.input&&!a.output?null:{input_tokens:a.input?await A(t):null,output_tokens:a.output?be(e):null}},"counts"),m=i((t,e,a,s,j)=>(u.finish(j),T(t,e,a,s)),"fail"),N=i(async t=>{let e=t,a=await Q(n,e,p.signal,B());const s=a.status,j=e.state.protocol===$e,ue=e.state.protocol===_e;if(e.more&&(s===404&&j||ve.includes(s)&&ue)){const y=await I(a);if(j&&y?.type==="error")return{upstream:e,result:a,failure:y};Te(l.state,e.upstream,e.state.protocol);const b=j?"messages":"responses",f=j?"Messages API (/v1/messages)":"Responses API (/v1/responses)";return h({type:"fallback",kind:b,level:"warn",message:`Upstream ${e.upstream}: ${f} answered ${s}; sent the request over the next protocol, and will do so for this upstream until the next detection. Set api on this upstream (for example "chat") to skip the first attempt.`,endpoint:e.upstream,model:e.body.model,status:s,reason:String(ae(y,s).message).trim().slice(0,500),action:j?"conversion":"chat"}),e=await d(g,{...k,endpoint:e.upstream,retry:!0}),x(e),{upstream:e,result:await Q(n,e,p.signal,B())}}const ce=s===400&&[e.images,e.tools].some(y=>y.sent&&y.retry),L=(s===400||s===422)&&e.json.sent&&e.json.retry;if(!a.ok&&(ce||L)){const y=await I(a),b=ae(y,s).message,f={};if(s===400&&e.images.sent&&e.images.retry&&pe.refused(b)?(r.add(e.images.key),f.images=!1):s===400&&e.tools.sent&&e.tools.retry&&de.refused(b)?f.tools=!1:L&&(f.jsonObject="instruction"),!("images"in f||"tools"in f||"jsonObject"in f))return{upstream:e,result:a,failure:y};const v={upstream:e,result:a,failure:y};if("images"in f||"tools"in f){const F="images"in f?"images":"tools";h({type:"fallback",kind:F,level:"warn",message:`Upstream ${e.upstream}: ${e.state.model} refused ${F==="images"?"images":"tools"} (${s}: "${String(b).trim().slice(0,500)}"); sent the request again without them.`,endpoint:e.upstream,model:e.body.model,status:s,reason:String(b).trim().slice(0,500),action:F==="images"?"text-only":"without-tools"})}if(e=await d(g,{...k,...f,endpoint:e.upstream,retry:!0}),x(e),a=await Q(n,e,p.signal,B()),L&&"jsonObject"in f){if(!a.ok){try{await a.body?.cancel?.()}catch{}return x(v.upstream),v}w.add(v.upstream.json.key),h({type:"fallback",kind:"json_object",level:"warn",message:`Upstream ${v.upstream.upstream}: ${v.upstream.state.model} rejected the json_object schema emulation (${s}: "${String(b).trim().slice(0,500)}"); sent it as a system instruction instead. Set jsonObject: "instruction" on this upstream to skip the first attempt.`,endpoint:v.upstream.upstream,model:v.upstream.body.model,status:s,reason:String(b).trim().slice(0,500),action:"instruction"})}}return{upstream:e,result:a}},"attempt");await Oe(l,g,p.signal);let o=await d(g,{...k,hold:!0});const O=o.pool;let G=0;const z=O.length>1?l.config.pool:null,B=i(()=>z&&{connectTimeout:z.connectTimeout,headersTimeout:G+1<O.length?z.headersTimeout:0},"limitsNow");let P=l.endpoint(o.upstream);const V=i(()=>P.health.inflight--,"release");let c,E,W=null,X=0;const Y=i(t=>{const e=je(l,{body:g,sent:o,front:u.meta.front,requested:u.meta.requested,headers:t,log:p.log,exchange:W,attempt:X});return W??=e.id,e},"begin"),Z=i(async t=>{const e=Y();if(!e.on)return;if(c===void 0)return e.attempt({error:E.message},t);let a=null;try{a=E??await I(c)}catch{}e.attempt({status:c.status,headers:te.plain(c.headers),body:a},t)},"keep");try{for(;;){const t=P;t.health.requests++,X++,x(o);let e=null;try{({upstream:o,result:c,failure:E}=await N(o)),!c.ok&&c.status>=500&&(e=`HTTP ${c.status}`,h({type:"error",kind:"upstream",level:"error",message:`Upstream ${t.name} answered HTTP ${c.status}`,endpoint:t.name,model:o.body.model,status:c.status}))}catch(s){if(!(s instanceof C)||s.code!=="upstream_unreachable")throw s;e=s.message,c=void 0,E=s,h({type:"error",kind:"transport",level:"error",message:s.message,endpoint:t.name,model:o.body.model})}if(e===null){ne.succeeded(t);break}ne.failed(l.config,t,e);let a=null;for(;a===null&&!p.signal?.aborted&&G+1<O.length;)try{a=await d(g,{...k,endpoint:O[++G],hold:!0})}catch(s){if(!(s instanceof C)||s.code!=="endpoint_not_in_pool")throw s}if(a===null){if(c===void 0)throw await Z(),E;break}await Z(e),h({type:"failover",level:"warn",message:`Upstream ${t.name} failed (${e}); trying ${a.upstream}`,from:t.name,to:a.upstream,model:a.body.model,reason:e});try{await c?.body?.cancel?.()}catch{}V(),P=l.endpoint(a.upstream),o=a}}catch(t){throw V(),t}const le=P;P=null,u.usage.then(()=>le.health.inflight--);const q=te.plain(c.headers),J=Y(q);if(!c.ok){const t=E??await I(c);return J.reply(c.status,q,t),m(c.status,t,q,o.state,"upstream_error")}if(o.state.stream){const t=_(J.raw(c.body),o.state),e=o.state.native?t:Se(u.meta.front,t,{gaps:i(()=>S(o),"gaps"),input:i(()=>A(o),"input"),report:i(a=>u.estimate(a),"report")});return{status:200,headers:Ee,stream:J.events(u.track(e,p.signal,U))}}const R=await I(c);if(J.reply(c.status,q,R),R===null||typeof R!="object")return h({type:"error",kind:"upstream",level:"error",message:`Upstream ${o.upstream} returned a body that is not JSON`,endpoint:o.upstream,model:o.body.model,status:502}),m(502,"The upstream returned a body that is not JSON",q,o.state,"error");const H=await $(R,o.state),D=await M(o,we(u.meta.front,H));return D&&(H.usage=ke(u.meta.front,H.usage,D),u.estimate(D)),u.json(H),u.finish("done"),{status:200,headers:{"content-type":"application/json"},body:H}}i(Ae,"round");function ie(n){return n.front==="anthropic"||n.responses?"openai":"folded"}i(ie,"usageShape");async function Q(n,r,w,d=null){let $,_=!1,T=w;if(d&&d.headersTimeout>0){const l=new AbortController;T=w?AbortSignal.any([w,l.signal]):l.signal,$=setTimeout(()=>{_=!0,l.abort()},d.headersTimeout)}try{return await n(r.url,{method:r.method,headers:r.headers,body:JSON.stringify(r.body),signal:T,redirect:"error",...d&&d.connectTimeout>0?{connectTimeout:d.connectTimeout}:{}})}catch(l){if(w?.aborted)throw l;const g=_?`no response headers within ${d.headersTimeout} ms`:l.message;throw new C(`Could not reach the upstream (${r.upstream}): ${g}`,{code:"upstream_unreachable",status:502,details:{cause:l}})}finally{clearTimeout($)}}i(Q,"call");async function I(n){const r=await n.text();try{return JSON.parse(r)}catch{return r===""?null:r}}i(I,"parse");export{Ue as send};
|
|
2
2
|
//# sourceMappingURL=send.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
|
|
@@ -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 l=Object.defineProperty;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
|
|
@@ -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 l=Object.defineProperty;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/protocol.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
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
2
|
//# sourceMappingURL=protocol.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/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/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
|
+
}>;
|