@adaline/gateway 0.22.0 → 0.24.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 +131 -0
- package/dist/index.js +19 -22
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -7
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -5,3 +5,134 @@
|
|
|
5
5
|

|
|
6
6
|

|
|
7
7
|
[](https://npmjs.org/package/@adaline/gateway)
|
|
8
|
+
|
|
9
|
+
The only fully local production-grade Super SDK that provides a simple, unified, and powerful interface for calling more than 200+ LLMs.
|
|
10
|
+
|
|
11
|
+
- Production-ready and used by enterprises.
|
|
12
|
+
- Fully local and _NOT_ a proxy. You can deploy it anywhere.
|
|
13
|
+
- Comes with batching, retries, caching, callbacks, and OpenTelemetry support.
|
|
14
|
+
- Supports custom plugins for caching, logging, HTTP client, and more. You can use it like LEGOs and make it work with your infrastructure.
|
|
15
|
+
- Supports plug-and-play providers. You can run fully custom providers and still leverage all the benefits of Adaline Gateway.
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
- 🔧 Strongly typed in TypeScript
|
|
20
|
+
- 📦 Isomorphic - works everywhere
|
|
21
|
+
- 🔒 100% local and private and _NOT_ a proxy
|
|
22
|
+
- 🛠️ Tool calling support across all compatible LLMs
|
|
23
|
+
- 📊 Batching for all requests with custom queue support
|
|
24
|
+
- 🔄 Automatic retries with exponential backoff
|
|
25
|
+
- ⏳ Caching with custom cache plug-in support
|
|
26
|
+
- 📞 Callbacks for full custom instrumentation and hooks
|
|
27
|
+
- 🔍 OpenTelemetry to plug tracing into your existing infrastructure
|
|
28
|
+
- 🔌 Plug-and-play custom providers for local and custom models
|
|
29
|
+
|
|
30
|
+
## Quickstart
|
|
31
|
+
|
|
32
|
+
### Install packages
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install @adaline/gateway @adaline/types @adaline/openai @adaline/anthropic
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Create a Gateway object
|
|
39
|
+
|
|
40
|
+
Gateway object maintains the queue, cache, callbacks, implements OpenTelemetry, etc. You should use the same Gateway object everywhere to get the benefits of all the features.
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { Gateway } from "@adaline/gateway";
|
|
44
|
+
|
|
45
|
+
const gateway = new Gateway();
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Create a provider object
|
|
49
|
+
|
|
50
|
+
Provider object stores the types/information about all the models within that provider. It exposes the list of all the chat `openai.chatModelLiterals()` and embedding `openai.embeddingModelLiterals()` models.
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { Anthropic } from "@adaline/anthropic";
|
|
54
|
+
import { OpenAI } from "@adaline/openai";
|
|
55
|
+
|
|
56
|
+
const openai = new OpenAI();
|
|
57
|
+
const anthropic = new Anthropic();
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Create a model object
|
|
61
|
+
|
|
62
|
+
Model object enforces the types from roles, to config, to different modalities that are supported by that model. You can also provide other keys like `baseUrl`, `organization`, etc.
|
|
63
|
+
|
|
64
|
+
Model object also exposes functions:
|
|
65
|
+
|
|
66
|
+
- `transformModelRequest` that takes a request formatted for the provider and converts it into the Adaline super-types.
|
|
67
|
+
- `getStreamChatData` that is then used to compose other provider calls. For example, calling an Anthropic model from Bedrock.
|
|
68
|
+
- and many more to enable deep composability and provide runtime validations.
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
const gpt4o = openai.chatModel({
|
|
72
|
+
modelName: "gpt-4o",
|
|
73
|
+
apiKey: "your-api-key",
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const haiku = anthropic.chatModel({
|
|
77
|
+
modelName: "claude-3-haiku-20240307",
|
|
78
|
+
apiKey: "your-api-key",
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Create the config object
|
|
83
|
+
|
|
84
|
+
Config object provides type checks and also accepts generics that can be used to add max, min, and other validation checks per model.
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { Config } from "@adaline/types";
|
|
88
|
+
|
|
89
|
+
const config = Config().parse({
|
|
90
|
+
maxTokens: 200,
|
|
91
|
+
temperature: 0.9,
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Create the messages object
|
|
96
|
+
|
|
97
|
+
Message object is the Adaline super-type that supports all the roles and modalities across 200+ LLMs.
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { MessageType } from "@adaline/types";
|
|
101
|
+
|
|
102
|
+
const messages: MessageType[] = [
|
|
103
|
+
{
|
|
104
|
+
role: "system",
|
|
105
|
+
content: [{
|
|
106
|
+
modality: "text",
|
|
107
|
+
value: "You are a helpful assistant. You are extremely concise.
|
|
108
|
+
}],
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
role: "user",
|
|
112
|
+
content: [{
|
|
113
|
+
modality: "text",
|
|
114
|
+
value: `What is ${Math.floor(Math.random() * 100) + 1} + ${Math.floor(Math.random() * 100) + 1}?`,
|
|
115
|
+
}],
|
|
116
|
+
},
|
|
117
|
+
];
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Stream chat using Gateway
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
await gateway.streamChat({
|
|
124
|
+
model: gpt4o,
|
|
125
|
+
config: config,
|
|
126
|
+
messages: messages,
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Complete chat using Gateway
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
await gateway.completeChat({
|
|
134
|
+
model: haiku,
|
|
135
|
+
config: config,
|
|
136
|
+
messages: messages,
|
|
137
|
+
});
|
|
138
|
+
```
|
package/dist/index.js
CHANGED
|
@@ -6,45 +6,42 @@ var uuid = require('uuid');
|
|
|
6
6
|
var zod = require('zod');
|
|
7
7
|
var semanticConventions = require('@opentelemetry/semantic-conventions');
|
|
8
8
|
var ae = require('axios');
|
|
9
|
-
var
|
|
10
|
-
var it = require('crypto-js/sha256.js');
|
|
9
|
+
var nt = require('crypto-js/sha256.js');
|
|
11
10
|
var lruCache = require('lru-cache');
|
|
12
|
-
var
|
|
13
|
-
var Te = require('os');
|
|
14
|
-
var path = require('path');
|
|
11
|
+
var Re = require('os');
|
|
15
12
|
|
|
16
13
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
17
14
|
|
|
18
15
|
var ae__default = /*#__PURE__*/_interopDefault(ae);
|
|
19
|
-
var
|
|
20
|
-
var
|
|
16
|
+
var nt__default = /*#__PURE__*/_interopDefault(nt);
|
|
17
|
+
var Re__default = /*#__PURE__*/_interopDefault(Re);
|
|
21
18
|
|
|
22
|
-
var Be=Object.defineProperty,We=Object.defineProperties;var Ye=Object.getOwnPropertyDescriptors;var ie=Object.getOwnPropertySymbols;var Je=Object.prototype.hasOwnProperty,Xe=Object.prototype.propertyIsEnumerable;var F=(r,t)=>(t=Symbol[r])?t:Symbol.for("Symbol."+r),Ze=r=>{throw TypeError(r)};var ce=(r,t,a)=>t in r?Be(r,t,{enumerable:!0,configurable:!0,writable:!0,value:a}):r[t]=a,k=(r,t)=>{for(var a in t||(t={}))Je.call(t,a)&&ce(r,a,t[a]);if(ie)for(var a of ie(t))Xe.call(t,a)&&ce(r,a,t[a]);return r},M=(r,t)=>We(r,Ye(t));var m=(r,t,a)=>new Promise((e,n)=>{var s=l=>{try{c(a.next(l));}catch(i){n(i);}},o=l=>{try{c(a.throw(l));}catch(i){n(i);}},c=l=>l.done?e(l.value):Promise.resolve(l.value).then(s,o);c((a=a.apply(r,t)).next());}),g=function(r,t){this[0]=r,this[1]=t;},L=(r,t,a)=>{var e=(o,c,l,i)=>{try{var d=a[o](c),p=(c=d.value)instanceof g,T=d.done;Promise.resolve(p?c[0]:c).then(y=>p?e(o==="return"?o:"next",c[1]?{done:y.done,value:y.value}:y,l,i):l({value:y,done:T})).catch(y=>e("throw",y,l,i));}catch(y){i(y);}},n=o=>s[o]=c=>new Promise((l,i)=>e(o,c,l,i)),s={};return a=a.apply(r,t),s[F("asyncIterator")]=()=>s,n("next"),n("throw"),n("return"),s},O=r=>{var t=r[F("asyncIterator")],a=!1,e,n={};return t==null?(t=r[F("iterator")](),e=s=>n[s]=o=>t[s](o)):(t=t.call(r),e=s=>n[s]=o=>{if(a){if(a=!1,s==="throw")throw o;return o}return a=!0,{done:!1,value:new g(new Promise(c=>{var l=t[s](o);l instanceof Object||Ze("Object expected"),c(l);}),1)}}),n[F("iterator")]=()=>n,e("next"),"throw"in t?e("throw"):n.throw=s=>{throw s},"return"in t&&e("return"),n},U=(r,t,a)=>(t=r[F("asyncIterator")])?t.call(r):(r=r[F("iterator")](),t={},a=(e,n)=>(n=r[e])&&(t[e]=s=>new Promise((o,c,l)=>(s=n.call(r,s),l=s.done,Promise.resolve(s.value).then(i=>o({value:i,done:l}),c)))),a("next"),a("return"),t);var v=class r extends Error{constructor(t,a=500,e){super(t),this.name="GatewayError",this.status=a,this.data=e,Error.captureStackTrace&&Error.captureStackTrace(this,r);}},tt="GatewayTelemetryError",de=class r extends types.GatewayBaseError{constructor({info:t,cause:a}){super({info:t,cause:a},tt),this.info=t,this.cause=a,Object.setPrototypeOf(this,new.target.prototype);}static isGatewayTelemetryError(t){return t instanceof r}};var at="HttpClientError",Q=class r extends types.GatewayBaseError{constructor({info:t,cause:a}){super({info:t,cause:a},at),this.info=t,this.cause=a,Object.setPrototypeOf(this,new.target.prototype);}static isHttpClientError(t){return t instanceof r}},st="HttpRequestError",S=class r extends types.GatewayBaseError{constructor(t,a=500,e,n){super({info:t,cause:{status:a,headers:e,data:n}},st),this.info=t,this.cause={status:a,headers:e,data:n},Object.setPrototypeOf(this,new.target.prototype);}static isHttpRequestError(t){return t instanceof r}};var b=class{static setTracer(t){this.tracer||(this.tracer=t||api.trace.getTracer(this.DEFAULT_TRACER_KEY));}static getTracer(){return this.tracer||api.trace.getTracer(this.DEFAULT_TRACER_KEY)}static setMeter(t){this.meter||(this.meter=t||api.metrics.getMeter(this.DEFAULT_METER_KEY));}static getMeter(){return this.meter||api.metrics.getMeter(this.DEFAULT_METER_KEY)}};b.DEFAULT_TRACER_KEY="gateway",b.DEFAULT_METER_KEY="gateway",b.tracer=void 0,b.meter=void 0;var V=r=>{let t={};return r&&(typeof r=="object"||r instanceof Headers)&&Object.entries(r).forEach(([a,e])=>{Array.isArray(e)?t[a]=e.join(", "):typeof e=="string"?t[a]=e:t[a]="";}),t},be=r=>{var s,o,c;let t=(r==null?void 0:r.message)||"An unexpected error occurred",a=((s=r==null?void 0:r.response)==null?void 0:s.status)||500,e=V((o=r==null?void 0:r.response)==null?void 0:o.headers)||{},n=((c=r==null?void 0:r.response)==null?void 0:c.data)||{};return new S(t,a,e,n)},_=class{constructor(t){this.isNodeEnvironment=()=>typeof process!="undefined"&&process.versions!=null&&process.versions.node!=null;let{axiosInstance:a,timeoutInMilliseconds:e}=t;this.client=a||ae__default.default.create();let n=zod.z.number().int().positive().optional();this.defaultTimeout=n.parse(e),this.client.defaults.timeout=this.defaultTimeout,this.httpProxyAgent=new proxyAgent.ProxyAgent,this.httpsProxyAgent=new proxyAgent.ProxyAgent({rejectUnauthorized:!1});let s=h.getLogger();s==null||s.debug(`IsomorphicHttpClient initialized with defaultTimeout: ${this.defaultTimeout}`);}makeRequest(o,c,l){return m(this,arguments,function*(t,a,e,n={},s){let i=h.getLogger(),d=p=>m(this,null,function*(){try{let T=M(k(k({},t==="get"||t==="delete"?{params:e}:{data:e}),n),{timeout:this.defaultTimeout,httpAgent:this.httpProxyAgent,httpsAgent:this.httpsProxyAgent});if(t==="get"||t==="delete"){let y=yield this.client[t](a,T);p==null||p.setStatus({code:api.SpanStatusCode.OK,message:"request successful"});let C={data:y.data,headers:V(y.headers),status:{code:y.status,text:y.statusText}};return i==null||i.debug("IsomorphicHttpClient.makeRequest response: ",C),C}else {let y=yield this.client[t](a,T.data,M(k({},T),{params:T.params}));p==null||p.setStatus({code:api.SpanStatusCode.OK,message:"request successful"});let C={data:y.data,headers:V(y.headers),status:{code:y.status,text:y.statusText}};return i==null||i.debug("IsomorphicHttpClient.makeRequest response: ",C),C}}catch(T){throw i==null||i.warn("IsomorphicHttpClient.makeRequest error: ",T),p==null||p.setStatus({code:api.SpanStatusCode.ERROR,message:"request failed"}),ae__default.default.isAxiosError(T)?be(T):new Q({info:"An unexpected error occurred",cause:T})}finally{p==null||p.end();}});return s?yield api.context.with(s,()=>m(this,null,function*(){return yield b.getTracer().startActiveSpan("http.request",T=>m(this,null,function*(){return T.setAttribute(semanticConventions.ATTR_HTTP_REQUEST_METHOD,t.toUpperCase()),T.setAttribute(semanticConventions.ATTR_URL_FULL,a),yield d(T)}))})):d()})}stream(t,a,e,n,s,o){return L(this,null,function*(){let c=h.getLogger();c==null||c.debug(`IsomorphicHttpClient.STREAM request to ${t}`,{data:e,headers:n});let l=function(i){return L(this,null,function*(){try{if(this.isNodeEnvironment()){c==null||c.debug("IsomorphicHttpClient.stream in node environment");let C=yield new g(this.client.request({method:a,url:t,headers:n,data:e,responseType:"stream",signal:s==null?void 0:s.abortSignal}));try{for(var d=U(C.data),p,T,y;p=!(T=yield new g(d.next())).done;p=!1){let f=T.value;i==null||i.addEvent("stream.chunk",{message:"stream chunk received"});let w=f.toString();c==null||c.debug("IsomorphicHttpClient.stream chunk: ",w),yield w;}}catch(T){y=[T];}finally{try{p&&(T=d.return)&&(yield new g(T.call(d)));}finally{if(y)throw y[0]}}i==null||i.setStatus({code:api.SpanStatusCode.OK,message:"stream successful"});}else {c==null||c.debug("IsomorphicHttpClient.stream in browser environment");let C={method:a,headers:new Headers(k({},n)),body:a!=="get"?JSON.stringify(e):void 0,signal:s==null?void 0:s.abortSignal},f=yield new g(fetch(t,C));if(!f.ok){c==null||c.warn("IsomorphicHttpClient.stream response not ok: ",f),i==null||i.setStatus({code:api.SpanStatusCode.ERROR,message:"stream failed"});let w=yield new g(f.json());throw new S(`Request failed with status ${f.status}`,f.status,V(f.headers),w)}if(f.body){let w=f.body.getReader();for(;;){let{done:E,value:A}=yield new g(w.read());if(E){i==null||i.addEvent("stream.chunk",{message:"stream chunk received"});let D=new TextDecoder().decode(A,{stream:!0});c==null||c.debug("IsomorphicHttpClient.stream chunk: ",D),yield D;break}i==null||i.addEvent("stream.chunk",{message:"stream chunk received"});let $=new TextDecoder().decode(A,{stream:!0});c==null||c.debug("IsomorphicHttpClient.stream chunk: ",$),yield $;}i==null||i.setStatus({code:api.SpanStatusCode.OK,message:"stream successful"});}else throw c==null||c.warn("IsomorphicHttpClient.stream response has no body"),i==null||i.setStatus({code:api.SpanStatusCode.ERROR,message:"stream failed"}),new S("Cannot stream the body of the response.",500,{},f)}}catch(C){throw c==null||c.warn("IsomorphicHttpClient.stream error: ",C),i==null||i.setStatus({code:api.SpanStatusCode.ERROR,message:"stream failed"}),S.isHttpRequestError(C)?C:(C==null?void 0:C.name)==="AbortError"?new S("AbortError",408,{},{}):(C==null?void 0:C.name)==="CanceledError"?new S("AbortError",408,{},{}):ae__default.default.isAxiosError(C)?be(C):new Q({info:"An unexpected error occurred",cause:C})}finally{i==null||i.end();}})}.bind(this);return o?yield*O(yield new g(api.context.with(o,()=>m(this,null,function*(){return yield b.getTracer().startActiveSpan("http.stream",d=>m(this,null,function*(){return d.setAttribute(semanticConventions.ATTR_HTTP_REQUEST_METHOD,a.toUpperCase()),d.setAttribute(semanticConventions.ATTR_URL_FULL,t),yield l(d)}))})))):yield*O(l())})}get(t,a,e,n){return m(this,null,function*(){let s=h.getLogger();return s==null||s.debug(`IsomorphicHttpClient.GET request to ${t}`,{params:a,headers:e}),this.makeRequest("get",t,a||{},{headers:e},n)})}post(t,a,e,n){return m(this,null,function*(){let s=h.getLogger();return s==null||s.debug(`IsomorphicHttpClient.POST request to ${t}`,{data:a,headers:e}),this.makeRequest("post",t,a||{},{headers:e},n)})}put(t,a,e,n){return m(this,null,function*(){let s=h.getLogger();return s==null||s.debug(`IsomorphicHttpClient.PUT request to ${t}`,{data:a,headers:e}),this.makeRequest("put",t,a||{},{headers:e},n)})}delete(t,a,e,n){return m(this,null,function*(){let s=h.getLogger();return s==null||s.debug(`IsomorphicHttpClient.DELETE request to ${t}`,{params:a,headers:e}),this.makeRequest("delete",t,a||{},{headers:e},n)})}patch(t,a,e,n){return m(this,null,function*(){let s=h.getLogger();return s==null||s.debug(`IsomorphicHttpClient.PATCH request to ${t}`,{data:a,headers:e}),this.makeRequest("patch",t,a||{},{headers:e},n)})}};var nt="QueueTaskTimeoutError",B=class r extends types.GatewayBaseError{constructor({info:t,cause:a}){super({info:t,cause:a},nt),this.info=t,this.cause=a,Object.setPrototypeOf(this,new.target.prototype);}static isQueueTaskTimeoutError(t){return t instanceof r}};var fe=zod.z.object({maxConcurrentTasks:zod.z.number().int().positive(),retryCount:zod.z.number().int().positive(),timeout:zod.z.number().int().positive(),retry:zod.z.object({initialDelay:zod.z.number().int().positive(),exponentialFactor:zod.z.number().int().positive()})});var W=(r,t)=>it__default.default(r+JSON.stringify(t)).toString(),j=r=>r instanceof v?r:Q.isHttpClientError(r)?new v(r.message):r instanceof Error?new v(r.message):new v(r),x=()=>typeof window!="undefined"&&typeof window.document!="undefined"&&typeof navigator!="undefined";var ct={error:"color: red",warn:"color: yellow",info:"color: green"},dt=(r,t,...a)=>{if(x())x()&&console.log(`%c[${r.toUpperCase()}] [${t}]`,ct[r],...a);else switch(r){case"error":console.error(...a);break;case"warn":console.warn(...a);break;default:console.log(...a);}},mt=(r,t,...a)=>{var e;x()||((e=process==null?void 0:process.env)==null?void 0:e.DEBUG)==="true"&&dt(r,t,...a);},q=(r,t,...a)=>m(void 0,null,function*(){let e=[];r.forEach(n=>{let s=n[t];if(typeof s=="function")try{let o=s(...a);o instanceof Promise&&e.push(o);}catch(o){mt("error",`SAFELY_INVOKE_CALLBACKS:${String(t)}:`,o);}}),yield Promise.allSettled(e);}),ge=r=>new Promise(t=>setTimeout(t,r));var h=class{static setLogger(t){this.logger=t;}static getLogger(){return this.logger}};var Re=class{debug(t,...a){console.debug(t,...a);}info(t,...a){console.info(t,...a);}warn(t,...a){x()?console.warn(`%WARN: %c${t}`,"color: yellow; font-weight: bold;","",...a):console.warn(`\x1B[33mWARN:\x1B[0m ${t}`,...a);}error(t,...a){x()?console.error(`%ERROR: %c${t}`,"color: lightcoral; font-weight: bold;","",...a):console.error(`\x1B[91mERROR:\x1B[0m ${t}`,...a);}critical(t,...a){x()?console.error(`%cCRITICAL: %c${t}`,"color: red; font-weight: bold;","",...a):console.error(`\x1B[31;1mCRITICAL:\x1B[0m ${t}`,...a);}};var z=class{constructor(t){this.activeTasks=0;this.queue=[];this.options=t;}enqueue(t){let a=h.getLogger();a==null||a.debug(`SimpleQueue.enqueue invoked, id: ${t.id}`),api.context.with(t.telemetryContext,()=>m(this,null,function*(){return b.getTracer().startActiveSpan("queue.task.pickup-wait",n=>m(this,null,function*(){n.setAttribute("id",t.id),this.queue.push({task:t,taskSpan:n}),a==null||a.debug(`SimpleQueue.enqueue task enqueued, id: ${t.id}`);}))})),this.processQueue();}executeWithTimeout(t,a){let e=h.getLogger();return e==null||e.debug(`SimpleQueue.executeWithTimeout invoked with timeout: ${this.options.timeout}, id: ${t.id}`),new Promise((n,s)=>{let o=setTimeout(()=>{e==null||e.warn(`SimpleQueue.executeWithTimeout timed out, id: ${t.id}`),s(new B({info:"Queue task timeout",cause:new Error("Queue task timeout")}));},this.options.timeout);e==null||e.debug(`SimpleQueue.executeWithTimeout task executing, id: ${t.id}`),t.execute(t.request,a).then(c=>{e==null||e.debug(`SimpleQueue.executeWithTimeout task completed, id: ${t.id}`),clearTimeout(o),n(c);}).catch(c=>{e==null||e.warn(`SimpleQueue.executeWithTimeout task errored, id: ${t.id}`),clearTimeout(o),s(c);});})}executeWithRetry(t,a){return m(this,null,function*(){let e=h.getLogger();return yield api.context.with(t.telemetryContext,()=>m(this,null,function*(){let n=b.getTracer();return yield n.startActiveSpan("queue.task.execute",s=>m(this,null,function*(){e==null||e.debug(`SimpleQueue.executeWithRetry invoked, attempt: ${this.options.retryCount-a}, id: ${t.id}`),s.setAttribute("attempt",this.options.retryCount-a);try{let o=api.context.active(),c=yield this.executeWithTimeout(t,o);return s.setStatus({code:api.SpanStatusCode.OK}),s.end(),c}catch(o){if(a===0)throw e==null||e.warn(`SimpleQueue.executeWithRetry retry count reached, id: ${t.id}`),s.end(),o;let c=!0,l=this.options.retry.initialDelay*Math.pow(this.options.retry.exponentialFactor,this.options.retryCount-a);if(S.isHttpRequestError(o)){if(o.cause.status===429){e==null||e.warn(`SimpleQueue.executeWithRetry rate limiting error, id: ${t.id}`);let i=K.safeParse(t.request);if(i.success){let d=i.data.model.getRetryDelay(o.cause.headers);c=d.shouldRetry,d.delayMs>0&&(l=d.delayMs);}}o.cause.status>=500&&o.cause.status<600&&(e==null||e.warn(`SimpleQueue.executeWithRetry ${o.cause.status} error, id: ${t.id}`));}else e==null||e.warn(`SimpleQueue.executeWithRetry non http-request error, id: ${t.id}`,{error:o});if(c)return yield n.startActiveSpan("queue.task.retry-wait",i=>m(this,null,function*(){return e==null||e.debug(`SimpleQueue.executeWithRetry retry wait: ${l}ms, id: ${t.id}`),yield ge(l),i.end(),s.end(),this.executeWithRetry(t,a-1)}));throw e==null||e.warn(`SimpleQueue.executeWithRetry model returned should not retry, id: ${t.id}`),s.end(),o}finally{}}))}))})}processQueue(){return m(this,null,function*(){var s;let t=h.getLogger();if(this.activeTasks>=this.options.maxConcurrentTasks){t==null||t.debug("SimpleQueue.processQueue max concurrent tasks reached");return}let a=this.queue.shift();if(!a){t==null||t.debug("SimpleQueue.processQueue no item to process");return}let{task:e,taskSpan:n}=a;n&&n.end(),this.activeTasks+=1,t==null||t.debug(`SimpleQueue.processQueue active tasks: ${this.activeTasks}`),t==null||t.debug(`SimpleQueue.processQueue processing task, id: ${e.id}`);try{let o=yield this.executeWithRetry(e,this.options.retryCount);t==null||t.debug(`SimpleQueue.processQueue task completed, id: ${e.id}`),e.resolve(o);}catch(o){t==null||t.warn(`SimpleQueue.processQueue task errored, id: ${e.id}`),e.reject(o);}finally{this.activeTasks-=1,t==null||t.debug(`SimpleQueue.processQueue active tasks: ${this.activeTasks}`),(s=api.trace.getSpan(e.telemetryContext))==null||s.end(),this.processQueue();}})}};var N=class{constructor(t=1e3){this.cache=new lruCache.LRUCache({max:t,allowStale:!1,updateAgeOnGet:!1});let a=h.getLogger();a==null||a.debug(`LRUCache initialized with maxEntries: ${t}`);}get(t){return m(this,null,function*(){let a=h.getLogger();return a==null||a.debug(`LRUCache.get invoked, key: ${t}`),new Promise(e=>{let n=this.cache.get(t);a==null||a.debug("LRUCache.get completed, value: ",n),e(n);})})}set(t,a){return m(this,null,function*(){let e=h.getLogger();return e==null||e.debug(`LRUCache.set invoked, key: ${t}, value: `,a),new Promise(n=>{this.cache.set(t,a),e==null||e.debug("LRUCache.set completed"),n();})})}delete(t){return m(this,null,function*(){let a=h.getLogger();return a==null||a.debug(`LRUCache.delete invoked, key: ${t}`),new Promise(e=>{this.cache.delete(t),a==null||a.debug("LRUCache.delete completed"),e();})})}clear(){return m(this,null,function*(){let t=h.getLogger();return t==null||t.debug("LRUCache.clear invoked"),new Promise(a=>{this.cache.clear(),t==null||t.debug("LRUCache.clear completed"),a();})})}};var Y=class{record(t,a,e){}stopRecorder(){}};var we=()=>({node:{version:process.version,platform:Te__default.default.platform(),architecture:Te__default.default.arch()}}),Ee=()=>({browser:{version:navigator.userAgent,userAgent:navigator.userAgent}}),ve=()=>{try{let r=path.join(__dirname,"../package.json");return JSON.parse(fs.readFileSync(r,"utf-8")).version}catch(r){return "unknown"}};var J=class{constructor(){this.eventVersion="0.1";this.gatewayVersion=ve();this.flushInterval=1e4;this.batchSize=1;this.maxAttempts=3;this.environment=x()?Ee():we();this.analyticsEndpointUrl="https://j954t34pkh.execute-api.us-east-1.amazonaws.com/v0/analytics";this.events=[];}startFlushTimer(){x()?this.flushTimer=window.setInterval(()=>this.flushEvents(),this.flushInterval):this.flushTimer=setInterval(()=>this.flushEvents(),this.flushInterval);}stopFlushTimer(){x()?window.clearInterval(this.flushTimer):clearInterval(this.flushTimer);}record(t,a,e){let n={event:t,status:a,dimensions:e,timestamp:new Date().toISOString(),eventVersion:this.eventVersion,gatewayVersion:this.gatewayVersion,environment:this.environment};this.events.push({event:n,attempt:0}),this.events.length>=this.batchSize&&this.flushEvents();}flushEvents(){return m(this,null,function*(){if(this.events.length===0)return;let t=[...this.events];this.events=[],(yield this.sendEvents(t.map(e=>e.event)))||this.events.push(...t.filter(e=>e.attempt<this.maxAttempts).map(e=>({event:e.event,attempt:e.attempt+1})));})}sendEvents(t){return m(this,null,function*(){try{return (yield ae__default.default.post(this.analyticsEndpointUrl,{events:t},{headers:{"Content-Type":"application/json"}})).status===200}catch(a){return !1}})}stopRecorder(){this.stopFlushTimer(),this.flushEvents();}};var X=class{static getAnalyticsRecorder(t){return this.analytics!==void 0?this.analytics:(this.analytics=t?new J:new Y,this.analytics)}};var He=zod.z.object({queueOptions:zod.z.lazy(()=>fe.partial()).optional(),dangerouslyAllowBrowser:zod.z.boolean().optional(),httpClient:zod.z.custom().optional(),completeChatCache:zod.z.custom().optional(),completeChatCallbacks:zod.z.array(zod.z.custom()).nonempty().optional(),getEmbeddingsCache:zod.z.custom().optional(),getEmbeddingsCallbacks:zod.z.array(zod.z.custom()).nonempty().optional(),streamChatCallbacks:zod.z.array(zod.z.custom()).nonempty().optional(),logger:zod.z.custom().optional(),telemetry:zod.z.object({tracer:zod.z.custom().optional(),meter:zod.z.custom().optional()}).optional(),analyticsEnabled:zod.z.boolean().optional()}),ft=zod.z.object({enableCache:zod.z.boolean().optional().default(!0),customHeaders:zod.z.record(zod.z.string()).optional(),metadataForCallbacks:zod.z.any().optional()}),K=zod.z.object({model:zod.z.custom(),config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional(),options:ft.optional()}),gt=zod.z.object({customHeaders:zod.z.record(zod.z.string()).optional(),metadataForCallbacks:zod.z.any().optional()}),Ge=zod.z.object({model:zod.z.custom(),config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional(),options:gt.optional()}),Rt=zod.z.object({enableCache:zod.z.boolean().optional().default(!0),customHeaders:zod.z.record(zod.z.string()).optional(),metadataForCallbacks:zod.z.any().optional()}),re=zod.z.object({model:zod.z.custom(),config:types.Config(),embeddingRequests:types.EmbeddingRequests(),options:Rt.optional()});var Me=zod.z.object({cache:zod.z.custom(),model:zod.z.custom(),config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional(),enableCache:zod.z.boolean(),customHeaders:zod.z.record(zod.z.string()).optional(),callbacks:zod.z.array(zod.z.custom()).nonempty().optional(),metadataForCallbacks:zod.z.any().optional()}),Ba=zod.z.object({request:zod.z.object({config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional()}),response:types.ChatResponse,cached:zod.z.boolean(),latencyInMs:zod.z.number().int().positive(),metadataForCallbacks:zod.z.any().optional(),provider:zod.z.object({request:zod.z.any(),response:zod.z.any()})});function Pe(r,t,a){return m(this,null,function*(){let e=h.getLogger(),n=s=>m(this,null,function*(){e==null||e.debug("handleCompleteChat invoked"),e==null||e.debug("handleCompleteChat request: ",{request:r});let o=Me.parse(r),c=r.callbacks||[],l=api.context.active();try{q(c,"onChatStart",r.metadataForCallbacks);let i={config:o.config,messages:o.messages,tools:o.tools},d={url:yield o.model.getCompleteChatUrl(o.config,o.messages,o.tools),headers:yield o.model.getCompleteChatHeaders(o.config,o.messages,o.tools),data:yield o.model.getCompleteChatData(o.config,o.messages,o.tools)};d.headers=M(k({},d.headers),{source:"adaline.ai"}),o.customHeaders&&(d.headers=k(k({},d.headers),o.customHeaders)),e==null||e.debug("handleCompleteChat providerRequest: ",{providerRequest:d});let p=W(`complete-chat:${d.url}:${o.model.modelSchema.name}`,i);if(o.enableCache){e==null||e.debug("handleCompleteChat checking cache");let w=yield r.cache.get(p);if(w)return w.cached=!0,e==null||e.debug("handleCompleteChat cached hit"),s==null||s.setAttribute("cached",!0),s==null||s.setStatus({code:api.SpanStatusCode.OK}),q(c,"onChatCached",r.metadataForCallbacks,w),e==null||e.debug("handleCompleteChat cached response: ",{cachedResponse:w}),w}e==null||e.debug("handleCompleteChat cache miss");let T=Date.now(),y=yield t.post(d.url,d.data,d.headers,l),C=Date.now()-T;e==null||e.debug("handleCompleteChat providerResponse: ",{providerResponse:y});let f={request:i,response:o.model.transformCompleteChatResponse(y.data),cached:!1,latencyInMs:C,metadataForCallbacks:r.metadataForCallbacks,provider:{request:d,response:y}};return e==null||e.debug("handleCompleteChat response: ",{response:f}),o.enableCache&&(yield r.cache.set(p,f),e==null||e.debug("handleCompleteChat response cached")),s==null||s.setAttribute("cached",!1),s==null||s.setStatus({code:api.SpanStatusCode.OK}),q(c,"onChatComplete",r.metadataForCallbacks,f),f}catch(i){e==null||e.warn("handleCompleteChat error: ",{error:i});let d;throw S.isHttpRequestError(i)||i instanceof v?d=i:d=j(i),q(c,"onChatError",r.metadataForCallbacks,d),d}finally{s==null||s.end();}});return a?yield api.context.with(a,()=>m(this,null,function*(){return yield b.getTracer().startActiveSpan("complete-chat.handler",o=>m(this,null,function*(){return yield n(o)}))})):yield n()})}var $e=zod.z.object({cache:zod.z.custom(),model:zod.z.custom(),config:types.Config(),embeddingRequests:types.EmbeddingRequests(),enableCache:zod.z.boolean(),customHeaders:zod.z.record(zod.z.string()).optional(),callbacks:zod.z.array(zod.z.custom()).nonempty().optional(),metadataForCallbacks:zod.z.any().optional()}),ps=zod.z.object({request:zod.z.object({config:types.Config(),embeddingRequests:types.EmbeddingRequests()}),response:types.EmbeddingResponse,cached:zod.z.boolean(),latencyInMs:zod.z.number().int().positive(),metadataForCallbacks:zod.z.any().optional(),provider:zod.z.object({request:zod.z.any(),response:zod.z.any()})});function De(r,t,a){return m(this,null,function*(){let e=h.getLogger(),n=s=>m(this,null,function*(){e==null||e.debug("handleGetEmbeddings invoked"),e==null||e.debug("handleGetEmbeddings request: ",{request:r});let o=$e.parse(r),c=r.callbacks||[],l=api.context.active();try{q(c,"onGetEmbeddingsStart",r.metadataForCallbacks);let i={config:o.config,embeddingRequests:o.embeddingRequests},d={url:yield o.model.getGetEmbeddingsUrl(o.config,o.embeddingRequests),headers:yield o.model.getGetEmbeddingsHeaders(o.config,o.embeddingRequests),data:yield o.model.getGetEmbeddingsData(o.config,o.embeddingRequests)};d.headers=M(k({},d.headers),{source:"adaline.ai"}),o.customHeaders&&(d.headers=k(k({},d.headers),o.customHeaders)),e==null||e.debug("handleGetEmbeddings providerRequest: ",{providerRequest:d});let p=W(`get-embeddings:${d.url}:${o.model.modelSchema.name}`,i);if(o.enableCache){e==null||e.debug("handleGetEmbeddings checking cache");let w=yield r.cache.get(p);if(w)return w.cached=!0,e==null||e.debug("handleGetEmbeddings cached hit"),s==null||s.setAttribute("cached",!0),s==null||s.setStatus({code:api.SpanStatusCode.OK}),q(c,"onGetEmbeddingsCached",r.metadataForCallbacks,w),e==null||e.debug("handleGetEmbeddings cached response: ",{cachedResponse:w}),w}e==null||e.debug("handleGetEmbeddings cache miss");let T=Date.now(),y=yield t.post(d.url,d.data,d.headers,l),C=Date.now()-T;e==null||e.debug("handleGetEmbeddings providerResponse: ",{providerResponse:y});let f={request:i,response:o.model.transformGetEmbeddingsResponse(y.data),cached:!1,latencyInMs:C,metadataForCallbacks:r.metadataForCallbacks,provider:{request:d,response:y}};return e==null||e.debug("handleGetEmbeddings response: ",{response:f}),o.enableCache&&(yield r.cache.set(p,f),e==null||e.debug("handleGetEmbeddings response cached")),s==null||s.setAttribute("cached",!1),s==null||s.setStatus({code:api.SpanStatusCode.OK}),q(c,"onGetEmbeddingsComplete",r.metadataForCallbacks,f),f}catch(i){e==null||e.warn("handleGetEmbeddings error: ",{error:i});let d;throw S.isHttpRequestError(i)||i instanceof v?d=i:d=j(i),q(c,"onGetEmbeddingsError",r.metadataForCallbacks,d),d}finally{s==null||s.end();}});return a?yield api.context.with(a,()=>m(this,null,function*(){return yield b.getTracer().startActiveSpan("get-embeddings.handler",o=>m(this,null,function*(){return yield n(o)}))})):yield n()})}var Ne=zod.z.object({model:zod.z.custom(),config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional(),customHeaders:zod.z.record(zod.z.string()).optional(),callbacks:zod.z.array(zod.z.custom()).nonempty().optional(),metadataForCallbacks:zod.z.any().optional()}),Gs=zod.z.object({request:zod.z.object({config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional()}),response:types.PartialChatResponse,metadataForCallbacks:zod.z.any().optional(),provider:zod.z.object({request:zod.z.any(),response:zod.z.any()})});function Ve(r,t,a){return L(this,null,function*(){let e=h.getLogger(),n=function(s){return L(this,null,function*(){e==null||e.debug("handleStreamChat invoked"),e==null||e.debug("handleStreamChat request: ",{request:r});let o=Ne.parse(r),c=r.callbacks||[],l={config:o.config,messages:o.messages,tools:o.tools};try{q(c,"onStreamStart",r.metadataForCallbacks);let E={url:yield new g(o.model.getStreamChatUrl(o.config,o.messages,o.tools)),headers:yield new g(o.model.getStreamChatHeaders(o.config,o.messages,o.tools)),data:yield new g(o.model.getStreamChatData(o.config,o.messages,o.tools))};E.headers=M(k({},E.headers),{source:"adaline.ai"}),o.customHeaders&&(E.headers=k(k({},E.headers),o.customHeaders)),e==null||e.debug("handleStreamChat providerRequest: ",{providerRequest:E});let A="",$=!0;try{for(var y=U(t.stream(E.url,"post",E.data,E.headers)),C,f,w;C=!(f=yield new g(y.next())).done;C=!1){let D=f.value;try{for(var i=U(o.model.transformStreamChatResponseChunk(D,A)),d,p,T;d=!(p=yield new g(i.next())).done;d=!1){let ee=p.value;if(ee.partialResponse.partialMessages.length>0){let te={request:l,response:ee.partialResponse,metadataForCallbacks:r.metadataForCallbacks,provider:{request:E,response:D}};q(c,$?"onStreamFirstResponse":"onStreamNewResponse",r.metadataForCallbacks,te),$&&($=!1),e==null||e.debug("handleStreamChat streamResponse: ",{streamResponse:te}),yield te;}else A=ee.buffer;}}catch(p){T=[p];}finally{try{d&&(p=i.return)&&(yield new g(p.call(i)));}finally{if(T)throw T[0]}}}}catch(f){w=[f];}finally{try{C&&(f=y.return)&&(yield new g(f.call(y)));}finally{if(w)throw w[0]}}s==null||s.setStatus({code:api.SpanStatusCode.OK}),q(c,"onStreamEnd",r.metadataForCallbacks);}catch(E){e==null||e.warn("handleStreamChat error: ",{error:E});let A;throw S.isHttpRequestError(E)||E instanceof v?A=E:A=j(E),q(c,"onStreamError",r.metadataForCallbacks,A),A}finally{s==null||s.end();}})};return a?yield*O(yield new g(api.context.with(a,()=>m(this,null,function*(){return yield b.getTracer().startActiveSpan("stream-chat.handler",o=>m(this,null,function*(){return yield n(o)}))})))):yield*O(n())})}var ne=class{constructor(t={}){var e,n,s,o,c,l,i;if(!t.dangerouslyAllowBrowser&&x())throw new v("It looks like you're running in a browser-like environment. This is disabled by default, as it risks exposing your provider secrets to attackers. If you understand the risks and have appropriate mitigation in place, you can set the `dangerouslyAllowBrowser` option to `true`.");this.options=He.parse(t),h.setLogger(t.logger),this.logger=t.logger,this.analytics=X.getAnalyticsRecorder(this.options.analyticsEnabled===void 0?!0:this.options.analyticsEnabled),b.setTracer((e=t.telemetry)==null?void 0:e.tracer),this.tracer=b.getTracer(),b.setMeter((n=t.telemetry)==null?void 0:n.meter),this.meter=b.getMeter();let a={maxConcurrentTasks:((s=this.options.queueOptions)==null?void 0:s.maxConcurrentTasks)||4,retryCount:((o=this.options.queueOptions)==null?void 0:o.retryCount)||3,retry:((c=this.options.queueOptions)==null?void 0:c.retry)||{initialDelay:1e3,exponentialFactor:2},timeout:((l=this.options.queueOptions)==null?void 0:l.timeout)||12e4};this.queues={completeChat:new z(a),getEmbeddings:new z(a)},this.httpClient=t.httpClient||new _({timeoutInMilliseconds:a.timeout*.9}),this.caches={completeChat:t.completeChatCache||new N,getEmbeddings:t.getEmbeddingsCache||new N},(i=this.logger)==null||i.debug("gateway initialized");}completeChat(t){return m(this,null,function*(){var n,s;(n=this.logger)==null||n.info("gateway.completeChat invoked"),(s=this.logger)==null||s.debug("request: ",{request:t});let a=K.parse(t),e=a.model.modelSchema.name;return yield this.tracer.startActiveSpan("complete-chat",o=>m(this,null,function*(){return o.setAttribute("modelName",e),new Promise((c,l)=>{var d;let i={id:uuid.v4(),request:a,cache:this.caches.completeChat,resolve:p=>{this.analytics.record("completeChat","success",{modelName:e,usage:p.response.usage||{}}),c(p);},reject:p=>{console.log("completeChat error",p),this.analytics.record("completeChat","error",{modelName:e}),l(p);},execute:this.executeCompleteChat.bind(this),telemetryContext:api.context.active()};this.queues.completeChat.enqueue(i),(d=this.logger)==null||d.debug(`gateway.completeChat task enqueued, id: ${i.id}`);})}))})}executeCompleteChat(t,a){return m(this,null,function*(){var n,s,o,c;(n=this.logger)==null||n.debug("gateway.executeCompleteChat invoked");let e=K.parse(t);return Pe({cache:this.caches.completeChat,model:e.model,config:e.config,messages:e.messages,tools:e.tools,enableCache:(o=(s=e.options)==null?void 0:s.enableCache)!=null?o:!0,callbacks:this.options.completeChatCallbacks,metadataForCallbacks:(c=e.options)==null?void 0:c.metadataForCallbacks},this.httpClient,a)})}streamChat(t){return L(this,null,function*(){var c,l,i,d;(c=this.logger)==null||c.info("gateway.streamChat invoked"),(l=this.logger)==null||l.debug("request: ",{request:t});let a=Ge.parse(t),e=a.model.modelSchema.name,n="success",s=this.tracer.startSpan("stream-chat"),o=api.trace.setSpan(api.context.active(),s);try{return s.setAttribute("modelName",e),yield*O(yield new g(api.context.with(o,()=>m(this,null,function*(){var p;return Ve({model:a.model,config:a.config,messages:a.messages,tools:a.tools,callbacks:this.options.streamChatCallbacks,metadataForCallbacks:(p=a.options)==null?void 0:p.metadataForCallbacks},this.httpClient,o)}))))}catch(p){throw n="error",s.setStatus({code:api.SpanStatusCode.ERROR,message:"stream failed"}),(i=this.logger)==null||i.error("gateway.streamChat error: ",{error:p}),p instanceof v?p:new v(p==null?void 0:p.message,500,(d=p==null?void 0:p.response)==null?void 0:d.data)}finally{this.analytics.record("streamChat",n,{modelName:e}),s.end();}})}getEmbeddings(t){return m(this,null,function*(){var n,s;(n=this.logger)==null||n.info("gateway.getEmbeddings invoked"),(s=this.logger)==null||s.debug("request: ",{request:t});let a=re.parse(t),e=a.model.modelSchema.name;return yield this.tracer.startActiveSpan("get-embeddings",o=>m(this,null,function*(){return o.setAttribute("modelName",e),new Promise((c,l)=>{var d;let i={id:uuid.v4(),request:a,cache:this.caches.getEmbeddings,resolve:p=>{this.analytics.record("getEmbeddings","success",{modelName:e,usage:p.response.usage||{}}),c(p);},reject:p=>{this.analytics.record("getEmbeddings","error",{modelName:e}),l(p);},execute:this.executeGetEmbeddings.bind(this),telemetryContext:api.context.active()};this.queues.getEmbeddings.enqueue(i),(d=this.logger)==null||d.debug(`gateway.getEmbeddings task enqueued, id: ${i.id}`);})}))})}executeGetEmbeddings(t,a){return m(this,null,function*(){var n,s,o,c;(n=this.logger)==null||n.debug("gateway.executeGetEmbeddings invoked");let e=re.parse(t);return De({cache:this.caches.getEmbeddings,model:e.model,config:e.config,embeddingRequests:e.embeddingRequests,enableCache:(o=(s=e.options)==null?void 0:s.enableCache)!=null?o:!0,callbacks:this.options.getEmbeddingsCallbacks,metadataForCallbacks:(c=e.options)==null?void 0:c.metadataForCallbacks},this.httpClient,a)})}};ne.GatewayError=v;
|
|
19
|
+
var _e=Object.defineProperty,Be=Object.defineProperties;var Ve=Object.getOwnPropertyDescriptors;var ie=Object.getOwnPropertySymbols;var We=Object.prototype.hasOwnProperty,Ye=Object.prototype.propertyIsEnumerable;var F=(r,t)=>(t=Symbol[r])?t:Symbol.for("Symbol."+r),Je=r=>{throw TypeError(r)};var ce=(r,t,a)=>t in r?_e(r,t,{enumerable:!0,configurable:!0,writable:!0,value:a}):r[t]=a,v=(r,t)=>{for(var a in t||(t={}))We.call(t,a)&&ce(r,a,t[a]);if(ie)for(var a of ie(t))Ye.call(t,a)&&ce(r,a,t[a]);return r},M=(r,t)=>Be(r,Ve(t));var Xe=(r=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(r,{get:(t,a)=>(typeof require!="undefined"?require:t)[a]}):r)(function(r){if(typeof require!="undefined")return require.apply(this,arguments);throw Error('Dynamic require of "'+r+'" is not supported')});var m=(r,t,a)=>new Promise((e,n)=>{var s=l=>{try{c(a.next(l));}catch(i){n(i);}},o=l=>{try{c(a.throw(l));}catch(i){n(i);}},c=l=>l.done?e(l.value):Promise.resolve(l.value).then(s,o);c((a=a.apply(r,t)).next());}),g=function(r,t){this[0]=r,this[1]=t;},L=(r,t,a)=>{var e=(o,c,l,i)=>{try{var d=a[o](c),p=(c=d.value)instanceof g,T=d.done;Promise.resolve(p?c[0]:c).then(y=>p?e(o==="return"?o:"next",c[1]?{done:y.done,value:y.value}:y,l,i):l({value:y,done:T})).catch(y=>e("throw",y,l,i));}catch(y){i(y);}},n=o=>s[o]=c=>new Promise((l,i)=>e(o,c,l,i)),s={};return a=a.apply(r,t),s[F("asyncIterator")]=()=>s,n("next"),n("throw"),n("return"),s},P=r=>{var t=r[F("asyncIterator")],a=!1,e,n={};return t==null?(t=r[F("iterator")](),e=s=>n[s]=o=>t[s](o)):(t=t.call(r),e=s=>n[s]=o=>{if(a){if(a=!1,s==="throw")throw o;return o}return a=!0,{done:!1,value:new g(new Promise(c=>{var l=t[s](o);l instanceof Object||Je("Object expected"),c(l);}),1)}}),n[F("iterator")]=()=>n,e("next"),"throw"in t?e("throw"):n.throw=s=>{throw s},"return"in t&&e("return"),n},U=(r,t,a)=>(t=r[F("asyncIterator")])?t.call(r):(r=r[F("iterator")](),t={},a=(e,n)=>(n=r[e])&&(t[e]=s=>new Promise((o,c,l)=>(s=n.call(r,s),l=s.done,Promise.resolve(s.value).then(i=>o({value:i,done:l}),c)))),a("next"),a("return"),t);var k=class r extends Error{constructor(t,a=500,e){super(t),this.name="GatewayError",this.status=a,this.data=e,Error.captureStackTrace&&Error.captureStackTrace(this,r);}},et="GatewayTelemetryError",de=class r extends types.GatewayBaseError{constructor({info:t,cause:a}){super({info:t,cause:a},et),this.info=t,this.cause=a,Object.setPrototypeOf(this,new.target.prototype);}static isGatewayTelemetryError(t){return t instanceof r}};var tt="HttpClientError",Q=class r extends types.GatewayBaseError{constructor({info:t,cause:a}){super({info:t,cause:a},tt),this.info=t,this.cause=a,Object.setPrototypeOf(this,new.target.prototype);}static isHttpClientError(t){return t instanceof r}},at="HttpRequestError",S=class r extends types.GatewayBaseError{constructor(t,a=500,e,n){super({info:t,cause:{status:a,headers:e,data:n}},at),this.info=t,this.cause={status:a,headers:e,data:n},Object.setPrototypeOf(this,new.target.prototype);}static isHttpRequestError(t){return t instanceof r}};var b=class{static setTracer(t){this.tracer||(this.tracer=t||api.trace.getTracer(this.DEFAULT_TRACER_KEY));}static getTracer(){return this.tracer||api.trace.getTracer(this.DEFAULT_TRACER_KEY)}static setMeter(t){this.meter||(this.meter=t||api.metrics.getMeter(this.DEFAULT_METER_KEY));}static getMeter(){return this.meter||api.metrics.getMeter(this.DEFAULT_METER_KEY)}};b.DEFAULT_TRACER_KEY="gateway",b.DEFAULT_METER_KEY="gateway",b.tracer=void 0,b.meter=void 0;var _=r=>{let t={};return r&&(typeof r=="object"||r instanceof Headers)&&Object.entries(r).forEach(([a,e])=>{Array.isArray(e)?t[a]=e.join(", "):typeof e=="string"?t[a]=e:t[a]="";}),t},Ce=r=>{var s,o,c;let t=(r==null?void 0:r.message)||"An unexpected error occurred",a=((s=r==null?void 0:r.response)==null?void 0:s.status)||500,e=_((o=r==null?void 0:r.response)==null?void 0:o.headers)||{},n=((c=r==null?void 0:r.response)==null?void 0:c.data)||{};return new S(t,a,e,n)},B=class{constructor(t){this.isNodeEnvironment=()=>typeof process!="undefined"&&process.versions!=null&&process.versions.node!=null;let{axiosInstance:a,timeoutInMilliseconds:e}=t;this.client=a||ae__default.default.create();let n=zod.z.number().int().positive().optional();if(this.defaultTimeout=n.parse(e),this.client.defaults.timeout=this.defaultTimeout,this.isNodeEnvironment()){let o=Xe("proxy-agent");this.httpProxyAgent=new o.ProxyAgent,this.httpsProxyAgent=new o.ProxyAgent({rejectUnauthorized:!1});}let s=h.getLogger();s==null||s.debug(`IsomorphicHttpClient initialized with defaultTimeout: ${this.defaultTimeout}`);}makeRequest(o,c,l){return m(this,arguments,function*(t,a,e,n={},s){let i=h.getLogger(),d=p=>m(this,null,function*(){try{let T=v(M(v(v({},t==="get"||t==="delete"?{params:e}:{data:e}),n),{timeout:this.defaultTimeout}),this.isNodeEnvironment()?{httpAgent:this.httpProxyAgent,httpsAgent:this.httpsProxyAgent}:{});if(t==="get"||t==="delete"){let y=yield this.client[t](a,T);p==null||p.setStatus({code:api.SpanStatusCode.OK,message:"request successful"});let C={data:y.data,headers:_(y.headers),status:{code:y.status,text:y.statusText}};return i==null||i.debug("IsomorphicHttpClient.makeRequest response: ",C),C}else {let y=yield this.client[t](a,T.data,M(v({},T),{params:T.params}));p==null||p.setStatus({code:api.SpanStatusCode.OK,message:"request successful"});let C={data:y.data,headers:_(y.headers),status:{code:y.status,text:y.statusText}};return i==null||i.debug("IsomorphicHttpClient.makeRequest response: ",C),C}}catch(T){throw i==null||i.warn("IsomorphicHttpClient.makeRequest error: ",T),p==null||p.setStatus({code:api.SpanStatusCode.ERROR,message:"request failed"}),ae__default.default.isAxiosError(T)?Ce(T):new Q({info:"An unexpected error occurred",cause:T})}finally{p==null||p.end();}});return s?yield api.context.with(s,()=>m(this,null,function*(){return yield b.getTracer().startActiveSpan("http.request",T=>m(this,null,function*(){return T.setAttribute(semanticConventions.ATTR_HTTP_REQUEST_METHOD,t.toUpperCase()),T.setAttribute(semanticConventions.ATTR_URL_FULL,a),yield d(T)}))})):d()})}stream(t,a,e,n,s,o){return L(this,null,function*(){let c=h.getLogger();c==null||c.debug(`IsomorphicHttpClient.STREAM request to ${t}`,{data:e,headers:n});let l=function(i){return L(this,null,function*(){try{if(this.isNodeEnvironment()){c==null||c.debug("IsomorphicHttpClient.stream in node environment");let C=yield new g(this.client.request({method:a,url:t,headers:n,data:e,responseType:"stream",signal:s==null?void 0:s.abortSignal}));try{for(var d=U(C.data),p,T,y;p=!(T=yield new g(d.next())).done;p=!1){let f=T.value;i==null||i.addEvent("stream.chunk",{message:"stream chunk received"});let w=f.toString();c==null||c.debug("IsomorphicHttpClient.stream chunk: ",w),yield w;}}catch(T){y=[T];}finally{try{p&&(T=d.return)&&(yield new g(T.call(d)));}finally{if(y)throw y[0]}}i==null||i.setStatus({code:api.SpanStatusCode.OK,message:"stream successful"});}else {c==null||c.debug("IsomorphicHttpClient.stream in browser environment");let C={method:a,headers:new Headers(v({},n)),body:a!=="get"?JSON.stringify(e):void 0,signal:s==null?void 0:s.abortSignal},f=yield new g(fetch(t,C));if(!f.ok){c==null||c.warn("IsomorphicHttpClient.stream response not ok: ",f),i==null||i.setStatus({code:api.SpanStatusCode.ERROR,message:"stream failed"});let w=yield new g(f.json());throw new S(`Request failed with status ${f.status}`,f.status,_(f.headers),w)}if(f.body){let w=f.body.getReader();for(;;){let{done:E,value:A}=yield new g(w.read());if(E){i==null||i.addEvent("stream.chunk",{message:"stream chunk received"});let D=new TextDecoder().decode(A,{stream:!0});c==null||c.debug("IsomorphicHttpClient.stream chunk: ",D),yield D;break}i==null||i.addEvent("stream.chunk",{message:"stream chunk received"});let $=new TextDecoder().decode(A,{stream:!0});c==null||c.debug("IsomorphicHttpClient.stream chunk: ",$),yield $;}i==null||i.setStatus({code:api.SpanStatusCode.OK,message:"stream successful"});}else throw c==null||c.warn("IsomorphicHttpClient.stream response has no body"),i==null||i.setStatus({code:api.SpanStatusCode.ERROR,message:"stream failed"}),new S("Cannot stream the body of the response.",500,{},f)}}catch(C){throw c==null||c.warn("IsomorphicHttpClient.stream error: ",C),i==null||i.setStatus({code:api.SpanStatusCode.ERROR,message:"stream failed"}),S.isHttpRequestError(C)?C:(C==null?void 0:C.name)==="AbortError"?new S("AbortError",408,{},{}):(C==null?void 0:C.name)==="CanceledError"?new S("AbortError",408,{},{}):ae__default.default.isAxiosError(C)?Ce(C):new Q({info:"An unexpected error occurred",cause:C})}finally{i==null||i.end();}})}.bind(this);return o?yield*P(yield new g(api.context.with(o,()=>m(this,null,function*(){return yield b.getTracer().startActiveSpan("http.stream",d=>m(this,null,function*(){return d.setAttribute(semanticConventions.ATTR_HTTP_REQUEST_METHOD,a.toUpperCase()),d.setAttribute(semanticConventions.ATTR_URL_FULL,t),yield l(d)}))})))):yield*P(l())})}get(t,a,e,n){return m(this,null,function*(){let s=h.getLogger();return s==null||s.debug(`IsomorphicHttpClient.GET request to ${t}`,{params:a,headers:e}),this.makeRequest("get",t,a||{},{headers:e},n)})}post(t,a,e,n){return m(this,null,function*(){let s=h.getLogger();return s==null||s.debug(`IsomorphicHttpClient.POST request to ${t}`,{data:a,headers:e}),this.makeRequest("post",t,a||{},{headers:e},n)})}put(t,a,e,n){return m(this,null,function*(){let s=h.getLogger();return s==null||s.debug(`IsomorphicHttpClient.PUT request to ${t}`,{data:a,headers:e}),this.makeRequest("put",t,a||{},{headers:e},n)})}delete(t,a,e,n){return m(this,null,function*(){let s=h.getLogger();return s==null||s.debug(`IsomorphicHttpClient.DELETE request to ${t}`,{params:a,headers:e}),this.makeRequest("delete",t,a||{},{headers:e},n)})}patch(t,a,e,n){return m(this,null,function*(){let s=h.getLogger();return s==null||s.debug(`IsomorphicHttpClient.PATCH request to ${t}`,{data:a,headers:e}),this.makeRequest("patch",t,a||{},{headers:e},n)})}};var rt="QueueTaskTimeoutError",V=class r extends types.GatewayBaseError{constructor({info:t,cause:a}){super({info:t,cause:a},rt),this.info=t,this.cause=a,Object.setPrototypeOf(this,new.target.prototype);}static isQueueTaskTimeoutError(t){return t instanceof r}};var be=zod.z.object({maxConcurrentTasks:zod.z.number().int().positive(),retryCount:zod.z.number().int().positive(),timeout:zod.z.number().int().positive(),retry:zod.z.object({initialDelay:zod.z.number().int().positive(),exponentialFactor:zod.z.number().int().positive()})});var W=(r,t)=>nt__default.default(r+JSON.stringify(t)).toString(),j=r=>r instanceof k?r:Q.isHttpClientError(r)?new k(r.message):r instanceof Error?new k(r.message):new k(r),x=()=>typeof window!="undefined"&&typeof window.document!="undefined"&&typeof navigator!="undefined";var it={error:"color: red",warn:"color: yellow",info:"color: green"},ct=(r,t,...a)=>{if(x())x()&&console.log(`%c[${r.toUpperCase()}] [${t}]`,it[r],...a);else switch(r){case"error":console.error(...a);break;case"warn":console.warn(...a);break;default:console.log(...a);}},dt=(r,t,...a)=>{var e;x()||((e=process==null?void 0:process.env)==null?void 0:e.DEBUG)==="true"&&ct(r,t,...a);},G=(r,t,...a)=>m(void 0,null,function*(){let e=[];r.forEach(n=>{let s=n[t];if(typeof s=="function")try{let o=s(...a);o instanceof Promise&&e.push(o);}catch(o){dt("error",`SAFELY_INVOKE_CALLBACKS:${String(t)}:`,o);}}),yield Promise.allSettled(e);}),fe=r=>new Promise(t=>setTimeout(t,r));var h=class{static setLogger(t){this.logger=t;}static getLogger(){return this.logger}};var ge=class{debug(t,...a){console.debug(t,...a);}info(t,...a){console.info(t,...a);}warn(t,...a){x()?console.warn(`%WARN: %c${t}`,"color: yellow; font-weight: bold;","",...a):console.warn(`\x1B[33mWARN:\x1B[0m ${t}`,...a);}error(t,...a){x()?console.error(`%ERROR: %c${t}`,"color: lightcoral; font-weight: bold;","",...a):console.error(`\x1B[91mERROR:\x1B[0m ${t}`,...a);}critical(t,...a){x()?console.error(`%cCRITICAL: %c${t}`,"color: red; font-weight: bold;","",...a):console.error(`\x1B[31;1mCRITICAL:\x1B[0m ${t}`,...a);}};var z=class{constructor(t){this.activeTasks=0;this.queue=[];this.options=t;}enqueue(t){let a=h.getLogger();a==null||a.debug(`SimpleQueue.enqueue invoked, id: ${t.id}`),api.context.with(t.telemetryContext,()=>m(this,null,function*(){return b.getTracer().startActiveSpan("queue.task.pickup-wait",n=>m(this,null,function*(){n.setAttribute("id",t.id),this.queue.push({task:t,taskSpan:n}),a==null||a.debug(`SimpleQueue.enqueue task enqueued, id: ${t.id}`);}))})),this.processQueue();}executeWithTimeout(t,a){let e=h.getLogger();return e==null||e.debug(`SimpleQueue.executeWithTimeout invoked with timeout: ${this.options.timeout}, id: ${t.id}`),new Promise((n,s)=>{let o=setTimeout(()=>{e==null||e.warn(`SimpleQueue.executeWithTimeout timed out, id: ${t.id}`),s(new V({info:"Queue task timeout",cause:new Error("Queue task timeout")}));},this.options.timeout);e==null||e.debug(`SimpleQueue.executeWithTimeout task executing, id: ${t.id}`),t.execute(t.request,a).then(c=>{e==null||e.debug(`SimpleQueue.executeWithTimeout task completed, id: ${t.id}`),clearTimeout(o),n(c);}).catch(c=>{e==null||e.warn(`SimpleQueue.executeWithTimeout task errored, id: ${t.id}`),clearTimeout(o),s(c);});})}executeWithRetry(t,a){return m(this,null,function*(){let e=h.getLogger();return yield api.context.with(t.telemetryContext,()=>m(this,null,function*(){let n=b.getTracer();return yield n.startActiveSpan("queue.task.execute",s=>m(this,null,function*(){e==null||e.debug(`SimpleQueue.executeWithRetry invoked, attempt: ${this.options.retryCount-a}, id: ${t.id}`),s.setAttribute("attempt",this.options.retryCount-a);try{let o=api.context.active(),c=yield this.executeWithTimeout(t,o);return s.setStatus({code:api.SpanStatusCode.OK}),s.end(),c}catch(o){if(a===0)throw e==null||e.warn(`SimpleQueue.executeWithRetry retry count reached, id: ${t.id}`),s.end(),o;let c=!0,l=this.options.retry.initialDelay*Math.pow(this.options.retry.exponentialFactor,this.options.retryCount-a);if(S.isHttpRequestError(o)){if(o.cause.status===429){e==null||e.warn(`SimpleQueue.executeWithRetry rate limiting error, id: ${t.id}`);let i=K.safeParse(t.request);if(i.success){let d=i.data.model.getRetryDelay(o.cause.headers);c=d.shouldRetry,d.delayMs>0&&(l=d.delayMs);}}o.cause.status>=500&&o.cause.status<600&&(e==null||e.warn(`SimpleQueue.executeWithRetry ${o.cause.status} error, id: ${t.id}`));}else e==null||e.warn(`SimpleQueue.executeWithRetry non http-request error, id: ${t.id}`,{error:o});if(c)return yield n.startActiveSpan("queue.task.retry-wait",i=>m(this,null,function*(){return e==null||e.debug(`SimpleQueue.executeWithRetry retry wait: ${l}ms, id: ${t.id}`),yield fe(l),i.end(),s.end(),this.executeWithRetry(t,a-1)}));throw e==null||e.warn(`SimpleQueue.executeWithRetry model returned should not retry, id: ${t.id}`),s.end(),o}finally{}}))}))})}processQueue(){return m(this,null,function*(){var s;let t=h.getLogger();if(this.activeTasks>=this.options.maxConcurrentTasks){t==null||t.debug("SimpleQueue.processQueue max concurrent tasks reached");return}let a=this.queue.shift();if(!a){t==null||t.debug("SimpleQueue.processQueue no item to process");return}let{task:e,taskSpan:n}=a;n&&n.end(),this.activeTasks+=1,t==null||t.debug(`SimpleQueue.processQueue active tasks: ${this.activeTasks}`),t==null||t.debug(`SimpleQueue.processQueue processing task, id: ${e.id}`);try{let o=yield this.executeWithRetry(e,this.options.retryCount);t==null||t.debug(`SimpleQueue.processQueue task completed, id: ${e.id}`),e.resolve(o);}catch(o){t==null||t.warn(`SimpleQueue.processQueue task errored, id: ${e.id}`),e.reject(o);}finally{this.activeTasks-=1,t==null||t.debug(`SimpleQueue.processQueue active tasks: ${this.activeTasks}`),(s=api.trace.getSpan(e.telemetryContext))==null||s.end(),this.processQueue();}})}};var N=class{constructor(t=1e3){this.cache=new lruCache.LRUCache({max:t,allowStale:!1,updateAgeOnGet:!1});let a=h.getLogger();a==null||a.debug(`LRUCache initialized with maxEntries: ${t}`);}get(t){return m(this,null,function*(){let a=h.getLogger();return a==null||a.debug(`LRUCache.get invoked, key: ${t}`),new Promise(e=>{let n=this.cache.get(t);a==null||a.debug("LRUCache.get completed, value: ",n),e(n);})})}set(t,a){return m(this,null,function*(){let e=h.getLogger();return e==null||e.debug(`LRUCache.set invoked, key: ${t}, value: `,a),new Promise(n=>{this.cache.set(t,a),e==null||e.debug("LRUCache.set completed"),n();})})}delete(t){return m(this,null,function*(){let a=h.getLogger();return a==null||a.debug(`LRUCache.delete invoked, key: ${t}`),new Promise(e=>{this.cache.delete(t),a==null||a.debug("LRUCache.delete completed"),e();})})}clear(){return m(this,null,function*(){let t=h.getLogger();return t==null||t.debug("LRUCache.clear invoked"),new Promise(a=>{this.cache.clear(),t==null||t.debug("LRUCache.clear completed"),a();})})}};var Y=class{record(t,a,e){}stopRecorder(){}};var Te=()=>({node:{version:process.version,platform:Re__default.default.platform(),architecture:Re__default.default.arch()}}),we=()=>({browser:{version:navigator.userAgent,userAgent:navigator.userAgent}});var J=class{constructor(){this.eventVersion="0.1";this.gatewayVersion="0.24.0";this.flushInterval=1e4;this.batchSize=1;this.maxAttempts=3;this.environment=x()?we():Te();this.analyticsEndpointUrl="https://j954t34pkh.execute-api.us-east-1.amazonaws.com/v0/analytics";this.events=[];}startFlushTimer(){x()?this.flushTimer=window.setInterval(()=>this.flushEvents(),this.flushInterval):this.flushTimer=setInterval(()=>this.flushEvents(),this.flushInterval);}stopFlushTimer(){x()?window.clearInterval(this.flushTimer):clearInterval(this.flushTimer);}record(t,a,e){let n={event:t,status:a,dimensions:e,timestamp:new Date().toISOString(),eventVersion:this.eventVersion,gatewayVersion:this.gatewayVersion,environment:this.environment};this.events.push({event:n,attempt:0}),this.events.length>=this.batchSize&&this.flushEvents();}flushEvents(){return m(this,null,function*(){if(this.events.length===0)return;let t=[...this.events];this.events=[],(yield this.sendEvents(t.map(e=>e.event)))||this.events.push(...t.filter(e=>e.attempt<this.maxAttempts).map(e=>({event:e.event,attempt:e.attempt+1})));})}sendEvents(t){return m(this,null,function*(){try{return (yield ae__default.default.post(this.analyticsEndpointUrl,{events:t},{headers:{"Content-Type":"application/json"}})).status===200}catch(a){return !1}})}stopRecorder(){this.stopFlushTimer(),this.flushEvents();}};var X=class{static getAnalyticsRecorder(t){return this.analytics!==void 0?this.analytics:(this.analytics=t?new J:new Y,this.analytics)}};var ke=zod.z.object({queueOptions:zod.z.lazy(()=>be.partial()).optional(),dangerouslyAllowBrowser:zod.z.boolean().optional(),httpClient:zod.z.custom().optional(),completeChatCache:zod.z.custom().optional(),completeChatCallbacks:zod.z.array(zod.z.custom()).nonempty().optional(),getEmbeddingsCache:zod.z.custom().optional(),getEmbeddingsCallbacks:zod.z.array(zod.z.custom()).nonempty().optional(),streamChatCallbacks:zod.z.array(zod.z.custom()).nonempty().optional(),logger:zod.z.custom().optional(),telemetry:zod.z.object({tracer:zod.z.custom().optional(),meter:zod.z.custom().optional()}).optional(),analyticsEnabled:zod.z.boolean().optional()}),yt=zod.z.object({enableCache:zod.z.boolean().optional().default(!0),customHeaders:zod.z.record(zod.z.string()).optional(),metadataForCallbacks:zod.z.any().optional()}),K=zod.z.object({model:zod.z.custom(),config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional(),options:yt.optional()}),Ct=zod.z.object({customHeaders:zod.z.record(zod.z.string()).optional(),metadataForCallbacks:zod.z.any().optional()}),Se=zod.z.object({model:zod.z.custom(),config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional(),options:Ct.optional()}),bt=zod.z.object({enableCache:zod.z.boolean().optional().default(!0),customHeaders:zod.z.record(zod.z.string()).optional(),metadataForCallbacks:zod.z.any().optional()}),re=zod.z.object({model:zod.z.custom(),config:types.Config(),embeddingRequests:types.EmbeddingRequests(),options:bt.optional()});var xe=zod.z.object({cache:zod.z.custom(),model:zod.z.custom(),config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional(),enableCache:zod.z.boolean(),customHeaders:zod.z.record(zod.z.string()).optional(),callbacks:zod.z.array(zod.z.custom()).nonempty().optional(),metadataForCallbacks:zod.z.any().optional()}),Ua=zod.z.object({request:zod.z.object({config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional()}),response:types.ChatResponse,cached:zod.z.boolean(),latencyInMs:zod.z.number().int().positive(),metadataForCallbacks:zod.z.any().optional(),provider:zod.z.object({request:zod.z.any(),response:zod.z.any()})});function Le(r,t,a){return m(this,null,function*(){let e=h.getLogger(),n=s=>m(this,null,function*(){e==null||e.debug("handleCompleteChat invoked"),e==null||e.debug("handleCompleteChat request: ",{request:r});let o=xe.parse(r),c=r.callbacks||[],l=api.context.active();try{G(c,"onChatStart",r.metadataForCallbacks);let i={config:o.config,messages:o.messages,tools:o.tools},d={url:yield o.model.getCompleteChatUrl(o.config,o.messages,o.tools),headers:yield o.model.getCompleteChatHeaders(o.config,o.messages,o.tools),data:yield o.model.getCompleteChatData(o.config,o.messages,o.tools)};d.headers=M(v({},d.headers),{source:"adaline.ai"}),o.customHeaders&&(d.headers=v(v({},d.headers),o.customHeaders)),e==null||e.debug("handleCompleteChat providerRequest: ",{providerRequest:d});let p=W(`complete-chat:${d.url}:${o.model.modelSchema.name}`,i);if(o.enableCache){e==null||e.debug("handleCompleteChat checking cache");let w=yield r.cache.get(p);if(w)return w.cached=!0,e==null||e.debug("handleCompleteChat cached hit"),s==null||s.setAttribute("cached",!0),s==null||s.setStatus({code:api.SpanStatusCode.OK}),G(c,"onChatCached",r.metadataForCallbacks,w),e==null||e.debug("handleCompleteChat cached response: ",{cachedResponse:w}),w}e==null||e.debug("handleCompleteChat cache miss");let T=Date.now(),y=yield t.post(d.url,d.data,d.headers,l),C=Date.now()-T;e==null||e.debug("handleCompleteChat providerResponse: ",{providerResponse:y});let f={request:i,response:o.model.transformCompleteChatResponse(y.data),cached:!1,latencyInMs:C,metadataForCallbacks:r.metadataForCallbacks,provider:{request:d,response:y}};return e==null||e.debug("handleCompleteChat response: ",{response:f}),o.enableCache&&(yield r.cache.set(p,f),e==null||e.debug("handleCompleteChat response cached")),s==null||s.setAttribute("cached",!1),s==null||s.setStatus({code:api.SpanStatusCode.OK}),G(c,"onChatComplete",r.metadataForCallbacks,f),f}catch(i){e==null||e.warn("handleCompleteChat error: ",{error:i});let d;throw S.isHttpRequestError(i)||i instanceof k?d=i:d=j(i),G(c,"onChatError",r.metadataForCallbacks,d),d}finally{s==null||s.end();}});return a?yield api.context.with(a,()=>m(this,null,function*(){return yield b.getTracer().startActiveSpan("complete-chat.handler",o=>m(this,null,function*(){return yield n(o)}))})):yield n()})}var Qe=zod.z.object({cache:zod.z.custom(),model:zod.z.custom(),config:types.Config(),embeddingRequests:types.EmbeddingRequests(),enableCache:zod.z.boolean(),customHeaders:zod.z.record(zod.z.string()).optional(),callbacks:zod.z.array(zod.z.custom()).nonempty().optional(),metadataForCallbacks:zod.z.any().optional()}),rs=zod.z.object({request:zod.z.object({config:types.Config(),embeddingRequests:types.EmbeddingRequests()}),response:types.EmbeddingResponse,cached:zod.z.boolean(),latencyInMs:zod.z.number().int().positive(),metadataForCallbacks:zod.z.any().optional(),provider:zod.z.object({request:zod.z.any(),response:zod.z.any()})});function Fe(r,t,a){return m(this,null,function*(){let e=h.getLogger(),n=s=>m(this,null,function*(){e==null||e.debug("handleGetEmbeddings invoked"),e==null||e.debug("handleGetEmbeddings request: ",{request:r});let o=Qe.parse(r),c=r.callbacks||[],l=api.context.active();try{G(c,"onGetEmbeddingsStart",r.metadataForCallbacks);let i={config:o.config,embeddingRequests:o.embeddingRequests},d={url:yield o.model.getGetEmbeddingsUrl(o.config,o.embeddingRequests),headers:yield o.model.getGetEmbeddingsHeaders(o.config,o.embeddingRequests),data:yield o.model.getGetEmbeddingsData(o.config,o.embeddingRequests)};d.headers=M(v({},d.headers),{source:"adaline.ai"}),o.customHeaders&&(d.headers=v(v({},d.headers),o.customHeaders)),e==null||e.debug("handleGetEmbeddings providerRequest: ",{providerRequest:d});let p=W(`get-embeddings:${d.url}:${o.model.modelSchema.name}`,i);if(o.enableCache){e==null||e.debug("handleGetEmbeddings checking cache");let w=yield r.cache.get(p);if(w)return w.cached=!0,e==null||e.debug("handleGetEmbeddings cached hit"),s==null||s.setAttribute("cached",!0),s==null||s.setStatus({code:api.SpanStatusCode.OK}),G(c,"onGetEmbeddingsCached",r.metadataForCallbacks,w),e==null||e.debug("handleGetEmbeddings cached response: ",{cachedResponse:w}),w}e==null||e.debug("handleGetEmbeddings cache miss");let T=Date.now(),y=yield t.post(d.url,d.data,d.headers,l),C=Date.now()-T;e==null||e.debug("handleGetEmbeddings providerResponse: ",{providerResponse:y});let f={request:i,response:o.model.transformGetEmbeddingsResponse(y.data),cached:!1,latencyInMs:C,metadataForCallbacks:r.metadataForCallbacks,provider:{request:d,response:y}};return e==null||e.debug("handleGetEmbeddings response: ",{response:f}),o.enableCache&&(yield r.cache.set(p,f),e==null||e.debug("handleGetEmbeddings response cached")),s==null||s.setAttribute("cached",!1),s==null||s.setStatus({code:api.SpanStatusCode.OK}),G(c,"onGetEmbeddingsComplete",r.metadataForCallbacks,f),f}catch(i){e==null||e.warn("handleGetEmbeddings error: ",{error:i});let d;throw S.isHttpRequestError(i)||i instanceof k?d=i:d=j(i),G(c,"onGetEmbeddingsError",r.metadataForCallbacks,d),d}finally{s==null||s.end();}});return a?yield api.context.with(a,()=>m(this,null,function*(){return yield b.getTracer().startActiveSpan("get-embeddings.handler",o=>m(this,null,function*(){return yield n(o)}))})):yield n()})}var ze=zod.z.object({model:zod.z.custom(),config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional(),customHeaders:zod.z.record(zod.z.string()).optional(),callbacks:zod.z.array(zod.z.custom()).nonempty().optional(),metadataForCallbacks:zod.z.any().optional()}),ws=zod.z.object({request:zod.z.object({config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional()}),response:types.PartialChatResponse,metadataForCallbacks:zod.z.any().optional(),provider:zod.z.object({request:zod.z.any(),response:zod.z.any()})});function Ke(r,t,a){return L(this,null,function*(){let e=h.getLogger(),n=function(s){return L(this,null,function*(){e==null||e.debug("handleStreamChat invoked"),e==null||e.debug("handleStreamChat request: ",{request:r});let o=ze.parse(r),c=r.callbacks||[],l={config:o.config,messages:o.messages,tools:o.tools};try{G(c,"onStreamStart",r.metadataForCallbacks);let E={url:yield new g(o.model.getStreamChatUrl(o.config,o.messages,o.tools)),headers:yield new g(o.model.getStreamChatHeaders(o.config,o.messages,o.tools)),data:yield new g(o.model.getStreamChatData(o.config,o.messages,o.tools))};E.headers=M(v({},E.headers),{source:"adaline.ai"}),o.customHeaders&&(E.headers=v(v({},E.headers),o.customHeaders)),e==null||e.debug("handleStreamChat providerRequest: ",{providerRequest:E});let A="",$=!0;try{for(var y=U(t.stream(E.url,"post",E.data,E.headers)),C,f,w;C=!(f=yield new g(y.next())).done;C=!1){let D=f.value;try{for(var i=U(o.model.transformStreamChatResponseChunk(D,A)),d,p,T;d=!(p=yield new g(i.next())).done;d=!1){let ee=p.value;if(ee.partialResponse.partialMessages.length>0){let te={request:l,response:ee.partialResponse,metadataForCallbacks:r.metadataForCallbacks,provider:{request:E,response:D}};G(c,$?"onStreamFirstResponse":"onStreamNewResponse",r.metadataForCallbacks,te),$&&($=!1),e==null||e.debug("handleStreamChat streamResponse: ",{streamResponse:te}),yield te;}else A=ee.buffer;}}catch(p){T=[p];}finally{try{d&&(p=i.return)&&(yield new g(p.call(i)));}finally{if(T)throw T[0]}}}}catch(f){w=[f];}finally{try{C&&(f=y.return)&&(yield new g(f.call(y)));}finally{if(w)throw w[0]}}s==null||s.setStatus({code:api.SpanStatusCode.OK}),G(c,"onStreamEnd",r.metadataForCallbacks);}catch(E){e==null||e.warn("handleStreamChat error: ",{error:E});let A;throw S.isHttpRequestError(E)||E instanceof k?A=E:A=j(E),G(c,"onStreamError",r.metadataForCallbacks,A),A}finally{s==null||s.end();}})};return a?yield*P(yield new g(api.context.with(a,()=>m(this,null,function*(){return yield b.getTracer().startActiveSpan("stream-chat.handler",o=>m(this,null,function*(){return yield n(o)}))})))):yield*P(n())})}var ne=class{constructor(t={}){var e,n,s,o,c,l,i;if(!t.dangerouslyAllowBrowser&&x())throw new k("It looks like you're running in a browser-like environment. This is disabled by default, as it risks exposing your provider secrets to attackers. If you understand the risks and have appropriate mitigation in place, you can set the `dangerouslyAllowBrowser` option to `true`.");this.options=ke.parse(t),h.setLogger(t.logger),this.logger=t.logger,this.analytics=X.getAnalyticsRecorder(this.options.analyticsEnabled===void 0?!0:this.options.analyticsEnabled),b.setTracer((e=t.telemetry)==null?void 0:e.tracer),this.tracer=b.getTracer(),b.setMeter((n=t.telemetry)==null?void 0:n.meter),this.meter=b.getMeter();let a={maxConcurrentTasks:((s=this.options.queueOptions)==null?void 0:s.maxConcurrentTasks)||4,retryCount:((o=this.options.queueOptions)==null?void 0:o.retryCount)||3,retry:((c=this.options.queueOptions)==null?void 0:c.retry)||{initialDelay:1e3,exponentialFactor:2},timeout:((l=this.options.queueOptions)==null?void 0:l.timeout)||12e4};this.queues={completeChat:new z(a),getEmbeddings:new z(a)},this.httpClient=t.httpClient||new B({timeoutInMilliseconds:a.timeout*.9}),this.caches={completeChat:t.completeChatCache||new N,getEmbeddings:t.getEmbeddingsCache||new N},(i=this.logger)==null||i.debug("gateway initialized");}completeChat(t){return m(this,null,function*(){var n,s;(n=this.logger)==null||n.info("gateway.completeChat invoked"),(s=this.logger)==null||s.debug("request: ",{request:t});let a=K.parse(t),e=a.model.modelSchema.name;return yield this.tracer.startActiveSpan("complete-chat",o=>m(this,null,function*(){return o.setAttribute("modelName",e),new Promise((c,l)=>{var d;let i={id:uuid.v4(),request:a,cache:this.caches.completeChat,resolve:p=>{this.analytics.record("completeChat","success",{modelName:e,usage:p.response.usage||{}}),c(p);},reject:p=>{console.log("completeChat error",p),this.analytics.record("completeChat","error",{modelName:e}),l(p);},execute:this.executeCompleteChat.bind(this),telemetryContext:api.context.active()};this.queues.completeChat.enqueue(i),(d=this.logger)==null||d.debug(`gateway.completeChat task enqueued, id: ${i.id}`);})}))})}executeCompleteChat(t,a){return m(this,null,function*(){var n,s,o,c;(n=this.logger)==null||n.debug("gateway.executeCompleteChat invoked");let e=K.parse(t);return Le({cache:this.caches.completeChat,model:e.model,config:e.config,messages:e.messages,tools:e.tools,enableCache:(o=(s=e.options)==null?void 0:s.enableCache)!=null?o:!0,callbacks:this.options.completeChatCallbacks,metadataForCallbacks:(c=e.options)==null?void 0:c.metadataForCallbacks},this.httpClient,a)})}streamChat(t){return L(this,null,function*(){var c,l,i,d;(c=this.logger)==null||c.info("gateway.streamChat invoked"),(l=this.logger)==null||l.debug("request: ",{request:t});let a=Se.parse(t),e=a.model.modelSchema.name,n="success",s=this.tracer.startSpan("stream-chat"),o=api.trace.setSpan(api.context.active(),s);try{return s.setAttribute("modelName",e),yield*P(yield new g(api.context.with(o,()=>m(this,null,function*(){var p;return Ke({model:a.model,config:a.config,messages:a.messages,tools:a.tools,callbacks:this.options.streamChatCallbacks,metadataForCallbacks:(p=a.options)==null?void 0:p.metadataForCallbacks},this.httpClient,o)}))))}catch(p){throw n="error",s.setStatus({code:api.SpanStatusCode.ERROR,message:"stream failed"}),(i=this.logger)==null||i.error("gateway.streamChat error: ",{error:p}),p instanceof k?p:new k(p==null?void 0:p.message,500,(d=p==null?void 0:p.response)==null?void 0:d.data)}finally{this.analytics.record("streamChat",n,{modelName:e}),s.end();}})}getEmbeddings(t){return m(this,null,function*(){var n,s;(n=this.logger)==null||n.info("gateway.getEmbeddings invoked"),(s=this.logger)==null||s.debug("request: ",{request:t});let a=re.parse(t),e=a.model.modelSchema.name;return yield this.tracer.startActiveSpan("get-embeddings",o=>m(this,null,function*(){return o.setAttribute("modelName",e),new Promise((c,l)=>{var d;let i={id:uuid.v4(),request:a,cache:this.caches.getEmbeddings,resolve:p=>{this.analytics.record("getEmbeddings","success",{modelName:e,usage:p.response.usage||{}}),c(p);},reject:p=>{this.analytics.record("getEmbeddings","error",{modelName:e}),l(p);},execute:this.executeGetEmbeddings.bind(this),telemetryContext:api.context.active()};this.queues.getEmbeddings.enqueue(i),(d=this.logger)==null||d.debug(`gateway.getEmbeddings task enqueued, id: ${i.id}`);})}))})}executeGetEmbeddings(t,a){return m(this,null,function*(){var n,s,o,c;(n=this.logger)==null||n.debug("gateway.executeGetEmbeddings invoked");let e=re.parse(t);return Fe({cache:this.caches.getEmbeddings,model:e.model,config:e.config,embeddingRequests:e.embeddingRequests,enableCache:(o=(s=e.options)==null?void 0:s.enableCache)!=null?o:!0,callbacks:this.options.getEmbeddingsCallbacks,metadataForCallbacks:(c=e.options)==null?void 0:c.metadataForCallbacks},this.httpClient,a)})}};ne.GatewayError=k;
|
|
23
20
|
|
|
24
21
|
exports.AnalyticsManager = X;
|
|
25
|
-
exports.CompleteChatHandlerRequest =
|
|
26
|
-
exports.CompleteChatHandlerResponse =
|
|
27
|
-
exports.ConsoleLogger =
|
|
22
|
+
exports.CompleteChatHandlerRequest = xe;
|
|
23
|
+
exports.CompleteChatHandlerResponse = Ua;
|
|
24
|
+
exports.ConsoleLogger = ge;
|
|
28
25
|
exports.Gateway = ne;
|
|
29
|
-
exports.GatewayError =
|
|
26
|
+
exports.GatewayError = k;
|
|
30
27
|
exports.GatewayTelemetryError = de;
|
|
31
|
-
exports.GetEmbeddingsHandlerRequest =
|
|
32
|
-
exports.GetEmbeddingsHandlerResponse =
|
|
28
|
+
exports.GetEmbeddingsHandlerRequest = Qe;
|
|
29
|
+
exports.GetEmbeddingsHandlerResponse = rs;
|
|
33
30
|
exports.HttpClientError = Q;
|
|
34
31
|
exports.HttpRequestError = S;
|
|
35
|
-
exports.IsomorphicHttpClient =
|
|
32
|
+
exports.IsomorphicHttpClient = B;
|
|
36
33
|
exports.LRUCache = N;
|
|
37
34
|
exports.LoggerManager = h;
|
|
38
35
|
exports.NoOpAnalytics = Y;
|
|
39
36
|
exports.PostAnalytics = J;
|
|
40
|
-
exports.QueueOptions =
|
|
41
|
-
exports.QueueTaskTimeoutError =
|
|
37
|
+
exports.QueueOptions = be;
|
|
38
|
+
exports.QueueTaskTimeoutError = V;
|
|
42
39
|
exports.SimpleQueue = z;
|
|
43
|
-
exports.StreamChatHandlerRequest =
|
|
44
|
-
exports.StreamChatHandlerResponse =
|
|
40
|
+
exports.StreamChatHandlerRequest = ze;
|
|
41
|
+
exports.StreamChatHandlerResponse = ws;
|
|
45
42
|
exports.TelemetryManager = b;
|
|
46
|
-
exports.handleCompleteChat =
|
|
47
|
-
exports.handleGetEmbeddings =
|
|
48
|
-
exports.handleStreamChat =
|
|
43
|
+
exports.handleCompleteChat = Le;
|
|
44
|
+
exports.handleGetEmbeddings = Fe;
|
|
45
|
+
exports.handleStreamChat = Ke;
|
|
49
46
|
//# sourceMappingURL=index.js.map
|
|
50
47
|
//# sourceMappingURL=index.js.map
|