@adaline/gateway 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +49 -5
- package/dist/index.d.ts +49 -5
- package/dist/index.js +17 -21
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -9
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -51,7 +51,6 @@ declare class HttpRequestError extends GatewayBaseError {
|
|
|
51
51
|
interface HttpClientConfig {
|
|
52
52
|
timeoutInMilliseconds?: number;
|
|
53
53
|
axiosInstance?: AxiosInstance;
|
|
54
|
-
proxyUrl?: string;
|
|
55
54
|
}
|
|
56
55
|
declare class IsomorphicHttpClient implements HttpClient {
|
|
57
56
|
private defaultTimeout?;
|
|
@@ -61,8 +60,7 @@ declare class IsomorphicHttpClient implements HttpClient {
|
|
|
61
60
|
constructor(config: HttpClientConfig);
|
|
62
61
|
isNodeEnvironment: () => boolean;
|
|
63
62
|
private makeRequest;
|
|
64
|
-
stream<T>(url: string, method: "get" | "post", data?: Record<string, unknown>, headers?: Record<string, string>,
|
|
65
|
-
options?: {
|
|
63
|
+
stream<T>(url: string, method: "get" | "post", data?: Record<string, unknown>, headers?: Record<string, string>, options?: {
|
|
66
64
|
abortSignal?: AbortSignal;
|
|
67
65
|
}): AsyncGenerator<T, void, unknown>;
|
|
68
66
|
get<T>(url: string, params?: Record<string, unknown>, headers?: Record<string, string | undefined>): Promise<HttpClientResponse<T>>;
|
|
@@ -152,6 +150,27 @@ declare class SimpleQueue<Request, Response> implements Queue<Request, Response>
|
|
|
152
150
|
private processQueue;
|
|
153
151
|
}
|
|
154
152
|
|
|
153
|
+
type AnalyticsEvent = {
|
|
154
|
+
event: string;
|
|
155
|
+
timestamp: string;
|
|
156
|
+
dimensions: {
|
|
157
|
+
[key: string]: any;
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
interface AnalyticsRecorder {
|
|
161
|
+
record(event: AnalyticsEvent["event"], dimensions: AnalyticsEvent["dimensions"]): void;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
declare class PostAnalytics implements AnalyticsRecorder {
|
|
165
|
+
private events;
|
|
166
|
+
private batchSize;
|
|
167
|
+
private endpointUrl;
|
|
168
|
+
constructor(endpointUrl: string, batchSize: number);
|
|
169
|
+
record(event: AnalyticsEvent["event"], dimensions: AnalyticsEvent["dimensions"]): void;
|
|
170
|
+
private flushEvents;
|
|
171
|
+
private sendEvents;
|
|
172
|
+
}
|
|
173
|
+
|
|
155
174
|
declare const CompleteChatHandlerRequest: z.ZodObject<{
|
|
156
175
|
cache: z.ZodType<Cache<{
|
|
157
176
|
request: {
|
|
@@ -703,6 +722,7 @@ declare const CompleteChatHandlerRequest: z.ZodObject<{
|
|
|
703
722
|
metadata?: any;
|
|
704
723
|
}>]>, "many">>;
|
|
705
724
|
enableCache: z.ZodBoolean;
|
|
725
|
+
customHeaders: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
706
726
|
callbacks: z.ZodOptional<z.ZodArray<z.ZodType<CompleteChatCallbackType<any>, z.ZodTypeDef, CompleteChatCallbackType<any>>, "atleastone">>;
|
|
707
727
|
metadataForCallbacks: z.ZodOptional<z.ZodAny>;
|
|
708
728
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -912,6 +932,7 @@ declare const CompleteChatHandlerRequest: z.ZodObject<{
|
|
|
912
932
|
};
|
|
913
933
|
metadata?: any;
|
|
914
934
|
}[] | undefined;
|
|
935
|
+
customHeaders?: Record<string, string> | undefined;
|
|
915
936
|
metadataForCallbacks?: any;
|
|
916
937
|
callbacks?: [CompleteChatCallbackType<any>, ...CompleteChatCallbackType<any>[]] | undefined;
|
|
917
938
|
}, {
|
|
@@ -1121,6 +1142,7 @@ declare const CompleteChatHandlerRequest: z.ZodObject<{
|
|
|
1121
1142
|
};
|
|
1122
1143
|
metadata?: any;
|
|
1123
1144
|
}[] | undefined;
|
|
1145
|
+
customHeaders?: Record<string, string> | undefined;
|
|
1124
1146
|
metadataForCallbacks?: any;
|
|
1125
1147
|
callbacks?: [CompleteChatCallbackType<any>, ...CompleteChatCallbackType<any>[]] | undefined;
|
|
1126
1148
|
}>;
|
|
@@ -2234,6 +2256,7 @@ declare const GetEmbeddingsHandlerRequest: z.ZodObject<{
|
|
|
2234
2256
|
metadata?: undefined;
|
|
2235
2257
|
}>]>;
|
|
2236
2258
|
enableCache: z.ZodBoolean;
|
|
2259
|
+
customHeaders: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
2237
2260
|
callbacks: z.ZodOptional<z.ZodArray<z.ZodType<GetEmbeddingsCallbackType<any>, z.ZodTypeDef, GetEmbeddingsCallbackType<any>>, "atleastone">>;
|
|
2238
2261
|
metadataForCallbacks: z.ZodOptional<z.ZodAny>;
|
|
2239
2262
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -2333,6 +2356,7 @@ declare const GetEmbeddingsHandlerRequest: z.ZodObject<{
|
|
|
2333
2356
|
requests: number[][];
|
|
2334
2357
|
metadata?: undefined;
|
|
2335
2358
|
};
|
|
2359
|
+
customHeaders?: Record<string, string> | undefined;
|
|
2336
2360
|
metadataForCallbacks?: any;
|
|
2337
2361
|
callbacks?: [GetEmbeddingsCallbackType<any>, ...GetEmbeddingsCallbackType<any>[]] | undefined;
|
|
2338
2362
|
}, {
|
|
@@ -2432,6 +2456,7 @@ declare const GetEmbeddingsHandlerRequest: z.ZodObject<{
|
|
|
2432
2456
|
requests: number[][];
|
|
2433
2457
|
metadata?: undefined;
|
|
2434
2458
|
};
|
|
2459
|
+
customHeaders?: Record<string, string> | undefined;
|
|
2435
2460
|
metadataForCallbacks?: any;
|
|
2436
2461
|
callbacks?: [GetEmbeddingsCallbackType<any>, ...GetEmbeddingsCallbackType<any>[]] | undefined;
|
|
2437
2462
|
}>;
|
|
@@ -2993,6 +3018,7 @@ declare const StreamChatHandlerRequest: z.ZodObject<{
|
|
|
2993
3018
|
};
|
|
2994
3019
|
metadata?: any;
|
|
2995
3020
|
}>]>, "many">>;
|
|
3021
|
+
customHeaders: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
2996
3022
|
callbacks: z.ZodOptional<z.ZodArray<z.ZodType<StreamChatCallbackType<any>, z.ZodTypeDef, StreamChatCallbackType<any>>, "atleastone">>;
|
|
2997
3023
|
metadataForCallbacks: z.ZodOptional<z.ZodAny>;
|
|
2998
3024
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -3090,6 +3116,7 @@ declare const StreamChatHandlerRequest: z.ZodObject<{
|
|
|
3090
3116
|
};
|
|
3091
3117
|
metadata?: any;
|
|
3092
3118
|
}[] | undefined;
|
|
3119
|
+
customHeaders?: Record<string, string> | undefined;
|
|
3093
3120
|
metadataForCallbacks?: any;
|
|
3094
3121
|
callbacks?: [StreamChatCallbackType<any>, ...StreamChatCallbackType<any>[]] | undefined;
|
|
3095
3122
|
}, {
|
|
@@ -3187,6 +3214,7 @@ declare const StreamChatHandlerRequest: z.ZodObject<{
|
|
|
3187
3214
|
};
|
|
3188
3215
|
metadata?: any;
|
|
3189
3216
|
}[] | undefined;
|
|
3217
|
+
customHeaders?: Record<string, string> | undefined;
|
|
3190
3218
|
metadataForCallbacks?: any;
|
|
3191
3219
|
callbacks?: [StreamChatCallbackType<any>, ...StreamChatCallbackType<any>[]] | undefined;
|
|
3192
3220
|
}>;
|
|
@@ -3909,7 +3937,7 @@ type StreamChatCallbackType<M = any> = {
|
|
|
3909
3937
|
onStreamFirstResponse?: (metadata?: M, response?: StreamChatHandlerResponseType, chunk?: unknown) => Promise<void> | void;
|
|
3910
3938
|
onStreamNewResponse?: (metadata?: M, response?: StreamChatHandlerResponseType, chunk?: unknown) => Promise<void> | void;
|
|
3911
3939
|
onStreamEnd?: (metadata?: M, response?: StreamChatHandlerResponseType) => Promise<void> | void;
|
|
3912
|
-
onStreamError?: (metadata?: M, error?: GatewayError) => Promise<void> | void;
|
|
3940
|
+
onStreamError?: (metadata?: M, error?: GatewayError | HttpRequestError) => Promise<void> | void;
|
|
3913
3941
|
};
|
|
3914
3942
|
|
|
3915
3943
|
declare function handleStreamChat<M>(request: StreamChatHandlerRequestType, client: HttpClient): AsyncGenerator<StreamChatHandlerResponseType, void, unknown>;
|
|
@@ -4916,12 +4944,15 @@ declare const GatewayCompleteChatRequest: z.ZodObject<{
|
|
|
4916
4944
|
}>]>, "many">>;
|
|
4917
4945
|
options: z.ZodOptional<z.ZodObject<{
|
|
4918
4946
|
enableCache: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
4947
|
+
customHeaders: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
4919
4948
|
metadataForCallbacks: z.ZodOptional<z.ZodAny>;
|
|
4920
4949
|
}, "strip", z.ZodTypeAny, {
|
|
4921
4950
|
enableCache: boolean;
|
|
4951
|
+
customHeaders?: Record<string, string> | undefined;
|
|
4922
4952
|
metadataForCallbacks?: any;
|
|
4923
4953
|
}, {
|
|
4924
4954
|
enableCache?: boolean | undefined;
|
|
4955
|
+
customHeaders?: Record<string, string> | undefined;
|
|
4925
4956
|
metadataForCallbacks?: any;
|
|
4926
4957
|
}>>;
|
|
4927
4958
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -5009,6 +5040,7 @@ declare const GatewayCompleteChatRequest: z.ZodObject<{
|
|
|
5009
5040
|
}[];
|
|
5010
5041
|
options?: {
|
|
5011
5042
|
enableCache: boolean;
|
|
5043
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5012
5044
|
metadataForCallbacks?: any;
|
|
5013
5045
|
} | undefined;
|
|
5014
5046
|
tools?: {
|
|
@@ -5108,6 +5140,7 @@ declare const GatewayCompleteChatRequest: z.ZodObject<{
|
|
|
5108
5140
|
}[];
|
|
5109
5141
|
options?: {
|
|
5110
5142
|
enableCache?: boolean | undefined;
|
|
5143
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5111
5144
|
metadataForCallbacks?: any;
|
|
5112
5145
|
} | undefined;
|
|
5113
5146
|
tools?: {
|
|
@@ -5454,10 +5487,13 @@ declare const GatewayStreamChatRequest: z.ZodObject<{
|
|
|
5454
5487
|
metadata?: any;
|
|
5455
5488
|
}>]>, "many">>;
|
|
5456
5489
|
options: z.ZodOptional<z.ZodObject<{
|
|
5490
|
+
customHeaders: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
5457
5491
|
metadataForCallbacks: z.ZodOptional<z.ZodAny>;
|
|
5458
5492
|
}, "strip", z.ZodTypeAny, {
|
|
5493
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5459
5494
|
metadataForCallbacks?: any;
|
|
5460
5495
|
}, {
|
|
5496
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5461
5497
|
metadataForCallbacks?: any;
|
|
5462
5498
|
}>>;
|
|
5463
5499
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -5544,6 +5580,7 @@ declare const GatewayStreamChatRequest: z.ZodObject<{
|
|
|
5544
5580
|
metadata?: undefined;
|
|
5545
5581
|
}[];
|
|
5546
5582
|
options?: {
|
|
5583
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5547
5584
|
metadataForCallbacks?: any;
|
|
5548
5585
|
} | undefined;
|
|
5549
5586
|
tools?: {
|
|
@@ -5642,6 +5679,7 @@ declare const GatewayStreamChatRequest: z.ZodObject<{
|
|
|
5642
5679
|
metadata?: undefined;
|
|
5643
5680
|
}[];
|
|
5644
5681
|
options?: {
|
|
5682
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5645
5683
|
metadataForCallbacks?: any;
|
|
5646
5684
|
} | undefined;
|
|
5647
5685
|
tools?: {
|
|
@@ -5776,12 +5814,15 @@ declare const GatewayGetEmbeddingsRequest: z.ZodObject<{
|
|
|
5776
5814
|
}>]>;
|
|
5777
5815
|
options: z.ZodOptional<z.ZodObject<{
|
|
5778
5816
|
enableCache: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
5817
|
+
customHeaders: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
5779
5818
|
metadataForCallbacks: z.ZodOptional<z.ZodAny>;
|
|
5780
5819
|
}, "strip", z.ZodTypeAny, {
|
|
5781
5820
|
enableCache: boolean;
|
|
5821
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5782
5822
|
metadataForCallbacks?: any;
|
|
5783
5823
|
}, {
|
|
5784
5824
|
enableCache?: boolean | undefined;
|
|
5825
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5785
5826
|
metadataForCallbacks?: any;
|
|
5786
5827
|
}>>;
|
|
5787
5828
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -5842,6 +5883,7 @@ declare const GatewayGetEmbeddingsRequest: z.ZodObject<{
|
|
|
5842
5883
|
};
|
|
5843
5884
|
options?: {
|
|
5844
5885
|
enableCache: boolean;
|
|
5886
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5845
5887
|
metadataForCallbacks?: any;
|
|
5846
5888
|
} | undefined;
|
|
5847
5889
|
}, {
|
|
@@ -5902,6 +5944,7 @@ declare const GatewayGetEmbeddingsRequest: z.ZodObject<{
|
|
|
5902
5944
|
};
|
|
5903
5945
|
options?: {
|
|
5904
5946
|
enableCache?: boolean | undefined;
|
|
5947
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5905
5948
|
metadataForCallbacks?: any;
|
|
5906
5949
|
} | undefined;
|
|
5907
5950
|
}>;
|
|
@@ -5912,6 +5955,7 @@ declare class Gateway {
|
|
|
5912
5955
|
private httpClient;
|
|
5913
5956
|
private queues;
|
|
5914
5957
|
private caches;
|
|
5958
|
+
private analytics;
|
|
5915
5959
|
constructor(options: GatewayOptionsType);
|
|
5916
5960
|
completeChat(request: GatewayCompleteChatRequestType): Promise<CompleteChatHandlerResponseType>;
|
|
5917
5961
|
private executeCompleteChat;
|
|
@@ -5921,4 +5965,4 @@ declare class Gateway {
|
|
|
5921
5965
|
static GatewayError: typeof GatewayError;
|
|
5922
5966
|
}
|
|
5923
5967
|
|
|
5924
|
-
export { type Cache, type CompleteChatCallbackType, CompleteChatHandlerRequest, type CompleteChatHandlerRequestType, CompleteChatHandlerResponse, type CompleteChatHandlerResponseType, Gateway, GatewayError, type GatewayOptionsType, type GetEmbeddingsCallbackType, GetEmbeddingsHandlerRequest, type GetEmbeddingsHandlerRequestType, GetEmbeddingsHandlerResponse, type GetEmbeddingsHandlerResponseType, type HttpClient, HttpClientError, type HttpClientResponse, HttpRequestError, IsomorphicHttpClient, LRUCache, type Queue, QueueOptions, type QueueOptionsType, type QueueTask, QueueTaskTimeoutError, SimpleQueue, type StreamChatCallbackType, StreamChatHandlerRequest, type StreamChatHandlerRequestType, StreamChatHandlerResponse, type StreamChatHandlerResponseType, handleCompleteChat, handleGetEmbeddings, handleStreamChat };
|
|
5968
|
+
export { type AnalyticsEvent, type AnalyticsRecorder, type Cache, type CompleteChatCallbackType, CompleteChatHandlerRequest, type CompleteChatHandlerRequestType, CompleteChatHandlerResponse, type CompleteChatHandlerResponseType, Gateway, GatewayError, type GatewayOptionsType, type GetEmbeddingsCallbackType, GetEmbeddingsHandlerRequest, type GetEmbeddingsHandlerRequestType, GetEmbeddingsHandlerResponse, type GetEmbeddingsHandlerResponseType, type HttpClient, HttpClientError, type HttpClientResponse, HttpRequestError, IsomorphicHttpClient, LRUCache, PostAnalytics, type Queue, QueueOptions, type QueueOptionsType, type QueueTask, QueueTaskTimeoutError, SimpleQueue, type StreamChatCallbackType, StreamChatHandlerRequest, type StreamChatHandlerRequestType, StreamChatHandlerResponse, type StreamChatHandlerResponseType, handleCompleteChat, handleGetEmbeddings, handleStreamChat };
|
package/dist/index.d.ts
CHANGED
|
@@ -51,7 +51,6 @@ declare class HttpRequestError extends GatewayBaseError {
|
|
|
51
51
|
interface HttpClientConfig {
|
|
52
52
|
timeoutInMilliseconds?: number;
|
|
53
53
|
axiosInstance?: AxiosInstance;
|
|
54
|
-
proxyUrl?: string;
|
|
55
54
|
}
|
|
56
55
|
declare class IsomorphicHttpClient implements HttpClient {
|
|
57
56
|
private defaultTimeout?;
|
|
@@ -61,8 +60,7 @@ declare class IsomorphicHttpClient implements HttpClient {
|
|
|
61
60
|
constructor(config: HttpClientConfig);
|
|
62
61
|
isNodeEnvironment: () => boolean;
|
|
63
62
|
private makeRequest;
|
|
64
|
-
stream<T>(url: string, method: "get" | "post", data?: Record<string, unknown>, headers?: Record<string, string>,
|
|
65
|
-
options?: {
|
|
63
|
+
stream<T>(url: string, method: "get" | "post", data?: Record<string, unknown>, headers?: Record<string, string>, options?: {
|
|
66
64
|
abortSignal?: AbortSignal;
|
|
67
65
|
}): AsyncGenerator<T, void, unknown>;
|
|
68
66
|
get<T>(url: string, params?: Record<string, unknown>, headers?: Record<string, string | undefined>): Promise<HttpClientResponse<T>>;
|
|
@@ -152,6 +150,27 @@ declare class SimpleQueue<Request, Response> implements Queue<Request, Response>
|
|
|
152
150
|
private processQueue;
|
|
153
151
|
}
|
|
154
152
|
|
|
153
|
+
type AnalyticsEvent = {
|
|
154
|
+
event: string;
|
|
155
|
+
timestamp: string;
|
|
156
|
+
dimensions: {
|
|
157
|
+
[key: string]: any;
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
interface AnalyticsRecorder {
|
|
161
|
+
record(event: AnalyticsEvent["event"], dimensions: AnalyticsEvent["dimensions"]): void;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
declare class PostAnalytics implements AnalyticsRecorder {
|
|
165
|
+
private events;
|
|
166
|
+
private batchSize;
|
|
167
|
+
private endpointUrl;
|
|
168
|
+
constructor(endpointUrl: string, batchSize: number);
|
|
169
|
+
record(event: AnalyticsEvent["event"], dimensions: AnalyticsEvent["dimensions"]): void;
|
|
170
|
+
private flushEvents;
|
|
171
|
+
private sendEvents;
|
|
172
|
+
}
|
|
173
|
+
|
|
155
174
|
declare const CompleteChatHandlerRequest: z.ZodObject<{
|
|
156
175
|
cache: z.ZodType<Cache<{
|
|
157
176
|
request: {
|
|
@@ -703,6 +722,7 @@ declare const CompleteChatHandlerRequest: z.ZodObject<{
|
|
|
703
722
|
metadata?: any;
|
|
704
723
|
}>]>, "many">>;
|
|
705
724
|
enableCache: z.ZodBoolean;
|
|
725
|
+
customHeaders: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
706
726
|
callbacks: z.ZodOptional<z.ZodArray<z.ZodType<CompleteChatCallbackType<any>, z.ZodTypeDef, CompleteChatCallbackType<any>>, "atleastone">>;
|
|
707
727
|
metadataForCallbacks: z.ZodOptional<z.ZodAny>;
|
|
708
728
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -912,6 +932,7 @@ declare const CompleteChatHandlerRequest: z.ZodObject<{
|
|
|
912
932
|
};
|
|
913
933
|
metadata?: any;
|
|
914
934
|
}[] | undefined;
|
|
935
|
+
customHeaders?: Record<string, string> | undefined;
|
|
915
936
|
metadataForCallbacks?: any;
|
|
916
937
|
callbacks?: [CompleteChatCallbackType<any>, ...CompleteChatCallbackType<any>[]] | undefined;
|
|
917
938
|
}, {
|
|
@@ -1121,6 +1142,7 @@ declare const CompleteChatHandlerRequest: z.ZodObject<{
|
|
|
1121
1142
|
};
|
|
1122
1143
|
metadata?: any;
|
|
1123
1144
|
}[] | undefined;
|
|
1145
|
+
customHeaders?: Record<string, string> | undefined;
|
|
1124
1146
|
metadataForCallbacks?: any;
|
|
1125
1147
|
callbacks?: [CompleteChatCallbackType<any>, ...CompleteChatCallbackType<any>[]] | undefined;
|
|
1126
1148
|
}>;
|
|
@@ -2234,6 +2256,7 @@ declare const GetEmbeddingsHandlerRequest: z.ZodObject<{
|
|
|
2234
2256
|
metadata?: undefined;
|
|
2235
2257
|
}>]>;
|
|
2236
2258
|
enableCache: z.ZodBoolean;
|
|
2259
|
+
customHeaders: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
2237
2260
|
callbacks: z.ZodOptional<z.ZodArray<z.ZodType<GetEmbeddingsCallbackType<any>, z.ZodTypeDef, GetEmbeddingsCallbackType<any>>, "atleastone">>;
|
|
2238
2261
|
metadataForCallbacks: z.ZodOptional<z.ZodAny>;
|
|
2239
2262
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -2333,6 +2356,7 @@ declare const GetEmbeddingsHandlerRequest: z.ZodObject<{
|
|
|
2333
2356
|
requests: number[][];
|
|
2334
2357
|
metadata?: undefined;
|
|
2335
2358
|
};
|
|
2359
|
+
customHeaders?: Record<string, string> | undefined;
|
|
2336
2360
|
metadataForCallbacks?: any;
|
|
2337
2361
|
callbacks?: [GetEmbeddingsCallbackType<any>, ...GetEmbeddingsCallbackType<any>[]] | undefined;
|
|
2338
2362
|
}, {
|
|
@@ -2432,6 +2456,7 @@ declare const GetEmbeddingsHandlerRequest: z.ZodObject<{
|
|
|
2432
2456
|
requests: number[][];
|
|
2433
2457
|
metadata?: undefined;
|
|
2434
2458
|
};
|
|
2459
|
+
customHeaders?: Record<string, string> | undefined;
|
|
2435
2460
|
metadataForCallbacks?: any;
|
|
2436
2461
|
callbacks?: [GetEmbeddingsCallbackType<any>, ...GetEmbeddingsCallbackType<any>[]] | undefined;
|
|
2437
2462
|
}>;
|
|
@@ -2993,6 +3018,7 @@ declare const StreamChatHandlerRequest: z.ZodObject<{
|
|
|
2993
3018
|
};
|
|
2994
3019
|
metadata?: any;
|
|
2995
3020
|
}>]>, "many">>;
|
|
3021
|
+
customHeaders: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
2996
3022
|
callbacks: z.ZodOptional<z.ZodArray<z.ZodType<StreamChatCallbackType<any>, z.ZodTypeDef, StreamChatCallbackType<any>>, "atleastone">>;
|
|
2997
3023
|
metadataForCallbacks: z.ZodOptional<z.ZodAny>;
|
|
2998
3024
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -3090,6 +3116,7 @@ declare const StreamChatHandlerRequest: z.ZodObject<{
|
|
|
3090
3116
|
};
|
|
3091
3117
|
metadata?: any;
|
|
3092
3118
|
}[] | undefined;
|
|
3119
|
+
customHeaders?: Record<string, string> | undefined;
|
|
3093
3120
|
metadataForCallbacks?: any;
|
|
3094
3121
|
callbacks?: [StreamChatCallbackType<any>, ...StreamChatCallbackType<any>[]] | undefined;
|
|
3095
3122
|
}, {
|
|
@@ -3187,6 +3214,7 @@ declare const StreamChatHandlerRequest: z.ZodObject<{
|
|
|
3187
3214
|
};
|
|
3188
3215
|
metadata?: any;
|
|
3189
3216
|
}[] | undefined;
|
|
3217
|
+
customHeaders?: Record<string, string> | undefined;
|
|
3190
3218
|
metadataForCallbacks?: any;
|
|
3191
3219
|
callbacks?: [StreamChatCallbackType<any>, ...StreamChatCallbackType<any>[]] | undefined;
|
|
3192
3220
|
}>;
|
|
@@ -3909,7 +3937,7 @@ type StreamChatCallbackType<M = any> = {
|
|
|
3909
3937
|
onStreamFirstResponse?: (metadata?: M, response?: StreamChatHandlerResponseType, chunk?: unknown) => Promise<void> | void;
|
|
3910
3938
|
onStreamNewResponse?: (metadata?: M, response?: StreamChatHandlerResponseType, chunk?: unknown) => Promise<void> | void;
|
|
3911
3939
|
onStreamEnd?: (metadata?: M, response?: StreamChatHandlerResponseType) => Promise<void> | void;
|
|
3912
|
-
onStreamError?: (metadata?: M, error?: GatewayError) => Promise<void> | void;
|
|
3940
|
+
onStreamError?: (metadata?: M, error?: GatewayError | HttpRequestError) => Promise<void> | void;
|
|
3913
3941
|
};
|
|
3914
3942
|
|
|
3915
3943
|
declare function handleStreamChat<M>(request: StreamChatHandlerRequestType, client: HttpClient): AsyncGenerator<StreamChatHandlerResponseType, void, unknown>;
|
|
@@ -4916,12 +4944,15 @@ declare const GatewayCompleteChatRequest: z.ZodObject<{
|
|
|
4916
4944
|
}>]>, "many">>;
|
|
4917
4945
|
options: z.ZodOptional<z.ZodObject<{
|
|
4918
4946
|
enableCache: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
4947
|
+
customHeaders: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
4919
4948
|
metadataForCallbacks: z.ZodOptional<z.ZodAny>;
|
|
4920
4949
|
}, "strip", z.ZodTypeAny, {
|
|
4921
4950
|
enableCache: boolean;
|
|
4951
|
+
customHeaders?: Record<string, string> | undefined;
|
|
4922
4952
|
metadataForCallbacks?: any;
|
|
4923
4953
|
}, {
|
|
4924
4954
|
enableCache?: boolean | undefined;
|
|
4955
|
+
customHeaders?: Record<string, string> | undefined;
|
|
4925
4956
|
metadataForCallbacks?: any;
|
|
4926
4957
|
}>>;
|
|
4927
4958
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -5009,6 +5040,7 @@ declare const GatewayCompleteChatRequest: z.ZodObject<{
|
|
|
5009
5040
|
}[];
|
|
5010
5041
|
options?: {
|
|
5011
5042
|
enableCache: boolean;
|
|
5043
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5012
5044
|
metadataForCallbacks?: any;
|
|
5013
5045
|
} | undefined;
|
|
5014
5046
|
tools?: {
|
|
@@ -5108,6 +5140,7 @@ declare const GatewayCompleteChatRequest: z.ZodObject<{
|
|
|
5108
5140
|
}[];
|
|
5109
5141
|
options?: {
|
|
5110
5142
|
enableCache?: boolean | undefined;
|
|
5143
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5111
5144
|
metadataForCallbacks?: any;
|
|
5112
5145
|
} | undefined;
|
|
5113
5146
|
tools?: {
|
|
@@ -5454,10 +5487,13 @@ declare const GatewayStreamChatRequest: z.ZodObject<{
|
|
|
5454
5487
|
metadata?: any;
|
|
5455
5488
|
}>]>, "many">>;
|
|
5456
5489
|
options: z.ZodOptional<z.ZodObject<{
|
|
5490
|
+
customHeaders: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
5457
5491
|
metadataForCallbacks: z.ZodOptional<z.ZodAny>;
|
|
5458
5492
|
}, "strip", z.ZodTypeAny, {
|
|
5493
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5459
5494
|
metadataForCallbacks?: any;
|
|
5460
5495
|
}, {
|
|
5496
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5461
5497
|
metadataForCallbacks?: any;
|
|
5462
5498
|
}>>;
|
|
5463
5499
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -5544,6 +5580,7 @@ declare const GatewayStreamChatRequest: z.ZodObject<{
|
|
|
5544
5580
|
metadata?: undefined;
|
|
5545
5581
|
}[];
|
|
5546
5582
|
options?: {
|
|
5583
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5547
5584
|
metadataForCallbacks?: any;
|
|
5548
5585
|
} | undefined;
|
|
5549
5586
|
tools?: {
|
|
@@ -5642,6 +5679,7 @@ declare const GatewayStreamChatRequest: z.ZodObject<{
|
|
|
5642
5679
|
metadata?: undefined;
|
|
5643
5680
|
}[];
|
|
5644
5681
|
options?: {
|
|
5682
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5645
5683
|
metadataForCallbacks?: any;
|
|
5646
5684
|
} | undefined;
|
|
5647
5685
|
tools?: {
|
|
@@ -5776,12 +5814,15 @@ declare const GatewayGetEmbeddingsRequest: z.ZodObject<{
|
|
|
5776
5814
|
}>]>;
|
|
5777
5815
|
options: z.ZodOptional<z.ZodObject<{
|
|
5778
5816
|
enableCache: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
5817
|
+
customHeaders: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
5779
5818
|
metadataForCallbacks: z.ZodOptional<z.ZodAny>;
|
|
5780
5819
|
}, "strip", z.ZodTypeAny, {
|
|
5781
5820
|
enableCache: boolean;
|
|
5821
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5782
5822
|
metadataForCallbacks?: any;
|
|
5783
5823
|
}, {
|
|
5784
5824
|
enableCache?: boolean | undefined;
|
|
5825
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5785
5826
|
metadataForCallbacks?: any;
|
|
5786
5827
|
}>>;
|
|
5787
5828
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -5842,6 +5883,7 @@ declare const GatewayGetEmbeddingsRequest: z.ZodObject<{
|
|
|
5842
5883
|
};
|
|
5843
5884
|
options?: {
|
|
5844
5885
|
enableCache: boolean;
|
|
5886
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5845
5887
|
metadataForCallbacks?: any;
|
|
5846
5888
|
} | undefined;
|
|
5847
5889
|
}, {
|
|
@@ -5902,6 +5944,7 @@ declare const GatewayGetEmbeddingsRequest: z.ZodObject<{
|
|
|
5902
5944
|
};
|
|
5903
5945
|
options?: {
|
|
5904
5946
|
enableCache?: boolean | undefined;
|
|
5947
|
+
customHeaders?: Record<string, string> | undefined;
|
|
5905
5948
|
metadataForCallbacks?: any;
|
|
5906
5949
|
} | undefined;
|
|
5907
5950
|
}>;
|
|
@@ -5912,6 +5955,7 @@ declare class Gateway {
|
|
|
5912
5955
|
private httpClient;
|
|
5913
5956
|
private queues;
|
|
5914
5957
|
private caches;
|
|
5958
|
+
private analytics;
|
|
5915
5959
|
constructor(options: GatewayOptionsType);
|
|
5916
5960
|
completeChat(request: GatewayCompleteChatRequestType): Promise<CompleteChatHandlerResponseType>;
|
|
5917
5961
|
private executeCompleteChat;
|
|
@@ -5921,4 +5965,4 @@ declare class Gateway {
|
|
|
5921
5965
|
static GatewayError: typeof GatewayError;
|
|
5922
5966
|
}
|
|
5923
5967
|
|
|
5924
|
-
export { type Cache, type CompleteChatCallbackType, CompleteChatHandlerRequest, type CompleteChatHandlerRequestType, CompleteChatHandlerResponse, type CompleteChatHandlerResponseType, Gateway, GatewayError, type GatewayOptionsType, type GetEmbeddingsCallbackType, GetEmbeddingsHandlerRequest, type GetEmbeddingsHandlerRequestType, GetEmbeddingsHandlerResponse, type GetEmbeddingsHandlerResponseType, type HttpClient, HttpClientError, type HttpClientResponse, HttpRequestError, IsomorphicHttpClient, LRUCache, type Queue, QueueOptions, type QueueOptionsType, type QueueTask, QueueTaskTimeoutError, SimpleQueue, type StreamChatCallbackType, StreamChatHandlerRequest, type StreamChatHandlerRequestType, StreamChatHandlerResponse, type StreamChatHandlerResponseType, handleCompleteChat, handleGetEmbeddings, handleStreamChat };
|
|
5968
|
+
export { type AnalyticsEvent, type AnalyticsRecorder, type Cache, type CompleteChatCallbackType, CompleteChatHandlerRequest, type CompleteChatHandlerRequestType, CompleteChatHandlerResponse, type CompleteChatHandlerResponseType, Gateway, GatewayError, type GatewayOptionsType, type GetEmbeddingsCallbackType, GetEmbeddingsHandlerRequest, type GetEmbeddingsHandlerRequestType, GetEmbeddingsHandlerResponse, type GetEmbeddingsHandlerResponseType, type HttpClient, HttpClientError, type HttpClientResponse, HttpRequestError, IsomorphicHttpClient, LRUCache, PostAnalytics, type Queue, QueueOptions, type QueueOptionsType, type QueueTask, QueueTaskTimeoutError, SimpleQueue, type StreamChatCallbackType, StreamChatHandlerRequest, type StreamChatHandlerRequestType, StreamChatHandlerResponse, type StreamChatHandlerResponseType, handleCompleteChat, handleGetEmbeddings, handleStreamChat };
|
package/dist/index.js
CHANGED
|
@@ -2,40 +2,36 @@
|
|
|
2
2
|
|
|
3
3
|
var zod = require('zod');
|
|
4
4
|
var types = require('@adaline/types');
|
|
5
|
+
var U = require('axios');
|
|
5
6
|
var proxyAgent = require('proxy-agent');
|
|
6
|
-
var V = require('axios');
|
|
7
7
|
var xe = require('crypto-js/sha256.js');
|
|
8
8
|
var lruCache = require('lru-cache');
|
|
9
9
|
|
|
10
10
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
11
|
|
|
12
|
-
var
|
|
12
|
+
var U__default = /*#__PURE__*/_interopDefault(U);
|
|
13
13
|
var xe__default = /*#__PURE__*/_interopDefault(xe);
|
|
14
14
|
|
|
15
|
-
var be=Object.defineProperty,Re=Object.defineProperties;var Te=Object.getOwnPropertyDescriptors;var N=Object.getOwnPropertySymbols;var we=Object.prototype.hasOwnProperty,ke=Object.prototype.propertyIsEnumerable;var D=(a,t)=>(t=Symbol[a])?t:Symbol.for("Symbol."+a);var W=(a,t,e)=>t in a?be(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e,x=(a,t)=>{for(var e in t||(t={}))we.call(t,e)&&W(a,e,t[e]);if(N)for(var e of N(t))ke.call(t,e)&&W(a,e,t[e]);return a},U=(a,t)=>Re(a,Te(t));var m=(a,t,e)=>new Promise((s,n)=>{var o=r=>{try{d(e.next(r));}catch(c){n(c);}},p=r=>{try{d(e.throw(r));}catch(c){n(c);}},d=r=>r.done?s(r.value):Promise.resolve(r.value).then(o,p);d((e=e.apply(a,t)).next());}),h=function(a,t){this[0]=a,this[1]=t;},q=(a,t,e)=>{var s=(p,d,r,c)=>{try{var i=e[p](d),u=(d=i.value)instanceof h,G=i.done;Promise.resolve(u?d[0]:d).then(y=>u?s(p==="return"?p:"next",d[1]?{done:y.done,value:y.value}:y,r,c):r({value:y,done:G})).catch(y=>s("throw",y,r,c));}catch(y){c(y);}},n=p=>o[p]=d=>new Promise((r,c)=>s(p,d,r,c)),o={};return e=e.apply(a,t),o[D("asyncIterator")]=()=>o,n("next"),n("throw"),n("return"),o};var k=(a,t,e)=>(t=a[D("asyncIterator")])?t.call(a):(a=a[D("iterator")](),t={},e=(s,n)=>(n=a[s])&&(t[s]=o=>new Promise((p,d,r)=>(o=n.call(a,o),r=o.done,Promise.resolve(o.value).then(c=>p({value:c,done:r}),d)))),e("next"),e("return"),t);var f=class a extends Error{constructor(t,e=500,s){super(t),this.name="GatewayError",this.status=e,this.data=s,Error.captureStackTrace&&Error.captureStackTrace(this,a);}};var Ee="HttpClientError",H=class a extends types.GatewayBaseError{constructor({info:t,cause:e}){super({info:t,cause:e},Ee),this.info=t,this.cause=e,Object.setPrototypeOf(this,new.target.prototype);}static isHttpClientError(t){return t instanceof a}},Ge="HttpRequestError",g=class a extends types.GatewayBaseError{constructor(t,e=500,s,n){super({info:t,cause:{status:e,headers:s,data:n}},Ge),this.info=t,this.cause={status:e,headers:s,data:n},Object.setPrototypeOf(this,new.target.prototype);}static isHttpRequestError(t){return t instanceof a}};var F=a=>{let t={};return a&&(typeof a=="object"||a instanceof Headers)&&Object.entries(a).forEach(([e,s])=>{Array.isArray(s)?t[e]=s.join(", "):typeof s=="string"?t[e]=s:t[e]="";}),t},X=a=>{var o,p,d;let t=(a==null?void 0:a.message)||"An unexpected error occurred",e=((o=a==null?void 0:a.response)==null?void 0:o.status)||500,s=F((p=a==null?void 0:a.response)==null?void 0:p.headers)||{},n=((d=a==null?void 0:a.response)==null?void 0:d.data)||{};return new g(t,e,s,n)},j=class{constructor(t){this.isNodeEnvironment=()=>typeof process!="undefined"&&process.versions!=null&&process.versions.node!=null;let{axiosInstance:e,timeoutInMilliseconds:s}=t;this.client=e||V__default.default.create();let n=zod.z.number().int().positive().optional();if(this.defaultTimeout=n.parse(s),this.client.defaults.timeout=this.defaultTimeout,t.proxyUrl)if(t.proxyUrl.startsWith("http://"))process.env.HTTP_PROXY=t.proxyUrl,this.httpProxyAgent=new proxyAgent.ProxyAgent;else if(t.proxyUrl.startsWith("https://"))process.env.HTTPS_PROXY=t.proxyUrl,this.httpsProxyAgent=new proxyAgent.ProxyAgent({rejectUnauthorized:!1});else throw new H({info:"Invalid proxy URL, only http and https proxies are supported",cause:{proxyUrl:t.proxyUrl}})}makeRequest(p,d,r){return m(this,arguments,function*(t,e,s,n={},o){try{let c=U(x(x({},t==="get"||t==="delete"?{params:s}:{data:s}),n),{timeout:o||this.defaultTimeout});if(this.httpProxyAgent?c.httpsAgent=this.httpProxyAgent:this.httpsProxyAgent&&(c.httpsAgent=this.httpsProxyAgent),t==="get"||t==="delete"){let i=yield this.client[t](e,c);return {data:i.data,headers:F(i.headers),status:{code:i.status,text:i.statusText}}}else {let i=yield this.client[t](e,c.data,U(x({},c),{params:c.params}));return {data:i.data,headers:F(i.headers),status:{code:i.status,text:i.statusText}}}}catch(c){throw V__default.default.isAxiosError(c)?X(c):new H({info:"An unexpected error occurred",cause:c})}})}stream(t,e,s,n,o){return q(this,null,function*(){try{if(this.isNodeEnvironment()){let i=yield new h(this.client.request({method:e,url:t,headers:n,data:s,responseType:"stream",signal:o==null?void 0:o.abortSignal}));try{for(var p=k(i.data),d,r,c;d=!(r=yield new h(p.next())).done;d=!1){let u=r.value;yield u.toString();}}catch(r){c=[r];}finally{try{d&&(r=p.return)&&(yield new h(r.call(p)));}finally{if(c)throw c[0]}}}else {let i={method:e,headers:new Headers(x({},n)),body:e!=="get"?JSON.stringify(s):void 0,signal:o==null?void 0:o.abortSignal},u=yield new h(fetch(t,i));if(!u.ok){let G=yield new h(u.json());throw new g(`Request failed with status ${u.status}`,u.status,F(u.headers),G)}if(u.body){let G=u.body.getReader();for(;;){let{done:y,value:w}=yield new h(G.read());if(y){yield new TextDecoder().decode(w,{stream:!0});break}yield new TextDecoder().decode(w,{stream:!0});}}else throw new g("Cannot stream the body of the response.",500,{},u)}}catch(i){throw g.isHttpRequestError(i)?i:(i==null?void 0:i.name)==="AbortError"?new g("AbortError",408,{},{}):(i==null?void 0:i.name)==="CanceledError"?new g("AbortError",408,{},{}):V__default.default.isAxiosError(i)?X(i):new H({info:"An unexpected error occurred",cause:i})}})}get(t,e,s){return m(this,null,function*(){return this.makeRequest("get",t,e||{},{headers:s})})}post(t,e,s){return m(this,null,function*(){return this.makeRequest("post",t,e||{},{headers:s})})}put(t,e,s){return m(this,null,function*(){return this.makeRequest("put",t,e||{},{headers:s})})}delete(t,e,s){return m(this,null,function*(){return this.makeRequest("delete",t,e||{},{headers:s})})}patch(t,e,s){return m(this,null,function*(){return this.makeRequest("patch",t,e||{},{headers:s})})}};var ve="QueueTaskTimeoutError",Q=class a extends types.GatewayBaseError{constructor({info:t,cause:e}){super({info:t,cause:e},ve),this.info=t,this.cause=e,Object.setPrototypeOf(this,new.target.prototype);}static isQueueTaskTimeoutError(t){return t instanceof a}};var Z=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 I=(a,t)=>xe__default.default(a+JSON.stringify(t)).toString(),v=a=>a instanceof f?a:a instanceof Error?new f(a.message):new f(a),S=()=>typeof window!="undefined"&&typeof window.document!="undefined"&&typeof navigator!="undefined";var Se={error:"color: red",warn:"color: yellow",info:"color: green"},Pe=(a,t,...e)=>{if(S())S()&&console.log(`%c[${a.toUpperCase()}] [${t}]`,Se[a],...e);else switch(a){case"error":console.error(...e);break;case"warn":console.warn(...e);break;default:console.log(...e);}},Me=(a,t,...e)=>{var s;S()||((s=process==null?void 0:process.env)==null?void 0:s.DEBUG)==="true"&&Pe(a,t,...e);},b=(a,t,...e)=>m(void 0,null,function*(){let s=[];a.forEach(n=>{let o=n[t];if(typeof o=="function")try{let p=o(...e);p instanceof Promise&&s.push(p);}catch(p){Me("error",`SAFELY_INVOKE_CALLBACKS:${String(t)}:`,p);}}),yield Promise.allSettled(s);}),_=a=>new Promise(t=>setTimeout(t,a));var P=class{constructor(t){this.activeTasks=0;this.queue=[];this.options=t;}enqueue(t){this.queue.push(t),this.processQueue();}withTimeout(t){return new Promise((e,s)=>{let n=setTimeout(()=>s(new Q({info:"Queue task timeout",cause:new Error("Queue task timeout")})),this.options.timeout);t.execute(t.request).then(o=>{clearTimeout(n),e(o);}).catch(o=>{clearTimeout(n),s(o);});})}executeWithRetry(t,e){return m(this,null,function*(){try{return yield this.withTimeout(t)}catch(s){if(e===0)throw s;let n=!0,o=this.options.retry.initialDelay*Math.pow(this.options.retry.exponentialFactor,this.options.retryCount-e);if(g.isHttpRequestError(s)){if(s.cause.status===429){let p=M.safeParse(t.request);if(p.success){let d=p.data.model.getRetryDelay(s.cause.headers);n=d.shouldRetry,d.delayMs>0&&(o=d.delayMs);}}s.cause.status>=500&&s.cause.status<600;}if(n)return yield _(o),this.executeWithRetry(t,e-1);throw s}})}processQueue(){return m(this,null,function*(){if(this.activeTasks>=this.options.maxConcurrentTasks)return;let t=this.queue.shift();if(t){this.activeTasks+=1;try{let e=yield this.executeWithRetry(t,this.options.retryCount);t.resolve(e);}catch(e){t.reject(e);}finally{this.activeTasks-=1,this.processQueue();}}})}};var O=class{constructor(t=1e3){this.cache=new lruCache.LRUCache({max:t,allowStale:!1,updateAgeOnGet:!1});}get(t){return m(this,null,function*(){return new Promise((e,s)=>{e(this.cache.get(t));})})}set(t,e){return m(this,null,function*(){return new Promise((s,n)=>{this.cache.set(t,e),s();})})}delete(t){return m(this,null,function*(){return new Promise((e,s)=>{this.cache.delete(t),e();})})}clear(){return m(this,null,function*(){return new Promise((t,e)=>{this.cache.clear(),t();})})}};var ae=zod.z.object({queueOptions:Z.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()}),Fe=zod.z.object({enableCache:zod.z.boolean().optional().default(!0),metadataForCallbacks:zod.z.any().optional()}),M=zod.z.object({model:zod.z.custom(),config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional(),options:Fe.optional()}),je=zod.z.object({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:je.optional()}),Qe=zod.z.object({enableCache:zod.z.boolean().optional().default(!0),metadataForCallbacks:zod.z.any().optional()}),B=zod.z.object({model:zod.z.custom(),config:types.Config(),embeddingRequests:types.EmbeddingRequests(),options:Qe.optional()});var ie=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(),callbacks:zod.z.array(zod.z.custom()).nonempty().optional(),metadataForCallbacks:zod.z.any().optional()}),Gt=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(a,t){return m(this,null,function*(){let e=ie.parse(a),s=a.callbacks||[];try{b(s,"onChatStart",a.metadataForCallbacks);let n={config:e.config,messages:e.messages,tools:e.tools},o={url:yield e.model.getCompleteChatUrl(e.config,e.messages,e.tools),headers:yield e.model.getCompleteChatHeaders(e.config,e.messages,e.tools),data:yield e.model.getCompleteChatData(e.config,e.messages,e.tools)},p=I(`complete-chat:${o.url}:${e.model.modelSchema.name}`,n);if(e.enableCache){let u=yield a.cache.get(p);if(u)return u.cached=!0,b(s,"onChatCached",a.metadataForCallbacks,u),u}let d=Date.now(),r=yield t.post(o.url,o.data,o.headers),c=Date.now()-d,i={request:n,response:e.model.transformCompleteChatResponse(r.data),cached:!1,latencyInMs:c,metadataForCallbacks:a.metadataForCallbacks,provider:{request:o,response:r}};return e.enableCache&&(yield a.cache.set(p,i)),b(s,"onChatComplete",a.metadataForCallbacks,i),i}catch(n){let o;throw g.isHttpRequestError(n)||n instanceof f?o=n:o=v(n),b(s,"onChatError",a.metadataForCallbacks,o),o}})}var de=zod.z.object({cache:zod.z.custom(),model:zod.z.custom(),config:types.Config(),embeddingRequests:types.EmbeddingRequests(),enableCache:zod.z.boolean(),callbacks:zod.z.array(zod.z.custom()).nonempty().optional(),metadataForCallbacks:zod.z.any().optional()}),zt=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 me(a,t){return m(this,null,function*(){let e=de.parse(a),s=a.callbacks||[];try{b(s,"onGetEmbeddingsStart",a.metadataForCallbacks);let n={config:e.config,embeddingRequests:e.embeddingRequests},o={url:yield e.model.getGetEmbeddingsUrl(e.config,e.embeddingRequests),headers:yield e.model.getGetEmbeddingsHeaders(e.config,e.embeddingRequests),data:yield e.model.getGetEmbeddingsData(e.config,e.embeddingRequests)},p=I(`get-embeddings:${o.url}:${e.model.modelSchema.name}`,n);if(e.enableCache){let u=yield a.cache.get(p);if(u)return u.cached=!0,b(s,"onGetEmbeddingsCached",a.metadataForCallbacks,u),u}let d=Date.now(),r=yield t.post(o.url,o.data,o.headers),c=Date.now()-d,i={request:n,response:e.model.transformGetEmbeddingsResponse(r.data),cached:!1,latencyInMs:c,metadataForCallbacks:a.metadataForCallbacks,provider:{request:o,response:r}};return e.enableCache&&(yield a.cache.set(p,i)),b(s,"onGetEmbeddingsComplete",a.metadataForCallbacks,i),i}catch(n){let o;throw g.isHttpRequestError(n)||n instanceof f?o=n:o=v(n),b(s,"onGetEmbeddingsError",a.metadataForCallbacks,o),o}})}var Ce=zod.z.object({model:zod.z.custom(),config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional(),callbacks:zod.z.array(zod.z.custom()).nonempty().optional(),metadataForCallbacks:zod.z.any().optional()}),_t=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 fe(a,t){return q(this,null,function*(){let e=Ce.parse(a),s=a.callbacks||[],n={config:e.config,messages:e.messages,tools:e.tools};try{b(s,"onStreamStart",a.metadataForCallbacks);let y={url:yield new h(e.model.getStreamChatUrl(e.config,e.messages,e.tools)),headers:yield new h(e.model.getStreamChatHeaders(e.config,e.messages,e.tools)),data:yield new h(e.model.getStreamChatData(e.config,e.messages,e.tools))},w="",A=!0;try{for(var c=k(t.stream(y.url,"post",y.data,y.headers)),i,u,G;i=!(u=yield new h(c.next())).done;i=!1){let z=u.value;try{for(var o=k(e.model.transformStreamChatResponseChunk(z,w)),p,d,r;p=!(d=yield new h(o.next())).done;p=!1){let ge=d.value;let $={request:n,response:ge.partialResponse,metadataForCallbacks:a.metadataForCallbacks,provider:{request:y,response:z}};b(s,A?"onStreamFirstResponse":"onStreamNewResponse",a.metadataForCallbacks,$),A&&(A=!1),yield $;}}catch(d){r=[d];}finally{try{p&&(d=o.return)&&(yield new h(d.call(o)));}finally{if(r)throw r[0]}}}}catch(u){G=[u];}finally{try{i&&(u=c.return)&&(yield new h(u.call(c)));}finally{if(G)throw G[0]}}b(s,"onStreamEnd",a.metadataForCallbacks);}catch(y){let w;throw y instanceof f?w=y:w=v(y),b(s,"onStreamError",a.metadataForCallbacks,w),w}})}var K=class{constructor(t){var s,n,o,p;if(!t.dangerouslyAllowBrowser&&S())throw new f(`It looks like you're running in a browser-like environment.
|
|
15
|
+
var Re=Object.defineProperty,Te=Object.defineProperties;var we=Object.getOwnPropertyDescriptors;var W=Object.getOwnPropertySymbols;var ke=Object.prototype.hasOwnProperty,Ee=Object.prototype.propertyIsEnumerable;var V=(a,t)=>(t=Symbol[a])?t:Symbol.for("Symbol."+a);var J=(a,t,e)=>t in a?Re(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e,w=(a,t)=>{for(var e in t||(t={}))ke.call(t,e)&&J(a,e,t[e]);if(W)for(var e of W(t))Ee.call(t,e)&&J(a,e,t[e]);return a},L=(a,t)=>Te(a,we(t));var c=(a,t,e)=>new Promise((s,n)=>{var o=r=>{try{d(e.next(r));}catch(m){n(m);}},l=r=>{try{d(e.throw(r));}catch(m){n(m);}},d=r=>r.done?s(r.value):Promise.resolve(r.value).then(o,l);d((e=e.apply(a,t)).next());}),C=function(a,t){this[0]=a,this[1]=t;},v=(a,t,e)=>{var s=(l,d,r,m)=>{try{var i=e[l](d),u=(d=i.value)instanceof C,q=i.done;Promise.resolve(u?d[0]:d).then(y=>u?s(l==="return"?l:"next",d[1]?{done:y.done,value:y.value}:y,r,m):r({value:y,done:q})).catch(y=>s("throw",y,r,m));}catch(y){m(y);}},n=l=>o[l]=d=>new Promise((r,m)=>s(l,d,r,m)),o={};return e=e.apply(a,t),o[V("asyncIterator")]=()=>o,n("next"),n("throw"),n("return"),o};var E=(a,t,e)=>(t=a[V("asyncIterator")])?t.call(a):(a=a[V("iterator")](),t={},e=(s,n)=>(n=a[s])&&(t[s]=o=>new Promise((l,d,r)=>(o=n.call(a,o),r=o.done,Promise.resolve(o.value).then(m=>l({value:m,done:r}),d)))),e("next"),e("return"),t);var g=class a extends Error{constructor(t,e=500,s){super(t),this.name="GatewayError",this.status=e,this.data=s,Error.captureStackTrace&&Error.captureStackTrace(this,a);}};var He="HttpClientError",H=class a extends types.GatewayBaseError{constructor({info:t,cause:e}){super({info:t,cause:e},He),this.info=t,this.cause=e,Object.setPrototypeOf(this,new.target.prototype);}static isHttpClientError(t){return t instanceof a}},Ge="HttpRequestError",f=class a extends types.GatewayBaseError{constructor(t,e=500,s,n){super({info:t,cause:{status:e,headers:s,data:n}},Ge),this.info=t,this.cause={status:e,headers:s,data:n},Object.setPrototypeOf(this,new.target.prototype);}static isHttpRequestError(t){return t instanceof a}};var F=a=>{let t={};return a&&(typeof a=="object"||a instanceof Headers)&&Object.entries(a).forEach(([e,s])=>{Array.isArray(s)?t[e]=s.join(", "):typeof s=="string"?t[e]=s:t[e]="";}),t},Z=a=>{var o,l,d;let t=(a==null?void 0:a.message)||"An unexpected error occurred",e=((o=a==null?void 0:a.response)==null?void 0:o.status)||500,s=F((l=a==null?void 0:a.response)==null?void 0:l.headers)||{},n=((d=a==null?void 0:a.response)==null?void 0:d.data)||{};return new f(t,e,s,n)},j=class{constructor(t){this.isNodeEnvironment=()=>typeof process!="undefined"&&process.versions!=null&&process.versions.node!=null;let{axiosInstance:e,timeoutInMilliseconds:s}=t;this.client=e||U__default.default.create();let n=zod.z.number().int().positive().optional();this.defaultTimeout=n.parse(s),this.client.defaults.timeout=this.defaultTimeout,this.httpProxyAgent=new proxyAgent.ProxyAgent,this.httpsProxyAgent=new proxyAgent.ProxyAgent({rejectUnauthorized:!1});}makeRequest(l,d,r){return c(this,arguments,function*(t,e,s,n={},o){try{let m=L(w(w({},t==="get"||t==="delete"?{params:s}:{data:s}),n),{timeout:o||this.defaultTimeout,httpAgent:this.httpProxyAgent,httpsAgent:this.httpsProxyAgent});if(t==="get"||t==="delete"){let i=yield this.client[t](e,m);return {data:i.data,headers:F(i.headers),status:{code:i.status,text:i.statusText}}}else {let i=yield this.client[t](e,m.data,L(w({},m),{params:m.params}));return {data:i.data,headers:F(i.headers),status:{code:i.status,text:i.statusText}}}}catch(m){throw U__default.default.isAxiosError(m)?Z(m):new H({info:"An unexpected error occurred",cause:m})}})}stream(t,e,s,n,o){return v(this,null,function*(){try{if(this.isNodeEnvironment()){let i=yield new C(this.client.request({method:e,url:t,headers:n,data:s,responseType:"stream",signal:o==null?void 0:o.abortSignal}));try{for(var l=E(i.data),d,r,m;d=!(r=yield new C(l.next())).done;d=!1){let u=r.value;yield u.toString();}}catch(r){m=[r];}finally{try{d&&(r=l.return)&&(yield new C(r.call(l)));}finally{if(m)throw m[0]}}}else {let i={method:e,headers:new Headers(w({},n)),body:e!=="get"?JSON.stringify(s):void 0,signal:o==null?void 0:o.abortSignal},u=yield new C(fetch(t,i));if(!u.ok){let q=yield new C(u.json());throw new f(`Request failed with status ${u.status}`,u.status,F(u.headers),q)}if(u.body){let q=u.body.getReader();for(;;){let{done:y,value:k}=yield new C(q.read());if(y){yield new TextDecoder().decode(k,{stream:!0});break}yield new TextDecoder().decode(k,{stream:!0});}}else throw new f("Cannot stream the body of the response.",500,{},u)}}catch(i){throw f.isHttpRequestError(i)?i:(i==null?void 0:i.name)==="AbortError"?new f("AbortError",408,{},{}):(i==null?void 0:i.name)==="CanceledError"?new f("AbortError",408,{},{}):U__default.default.isAxiosError(i)?Z(i):new H({info:"An unexpected error occurred",cause:i})}})}get(t,e,s){return c(this,null,function*(){return this.makeRequest("get",t,e||{},{headers:s})})}post(t,e,s){return c(this,null,function*(){return this.makeRequest("post",t,e||{},{headers:s})})}put(t,e,s){return c(this,null,function*(){return this.makeRequest("put",t,e||{},{headers:s})})}delete(t,e,s){return c(this,null,function*(){return this.makeRequest("delete",t,e||{},{headers:s})})}patch(t,e,s){return c(this,null,function*(){return this.makeRequest("patch",t,e||{},{headers:s})})}};var Se="QueueTaskTimeoutError",Q=class a extends types.GatewayBaseError{constructor({info:t,cause:e}){super({info:t,cause:e},Se),this.info=t,this.cause=e,Object.setPrototypeOf(this,new.target.prototype);}static isQueueTaskTimeoutError(t){return t instanceof a}};var _=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 z=(a,t)=>xe__default.default(a+JSON.stringify(t)).toString(),S=a=>a instanceof g?a:H.isHttpClientError(a)?new g(a.message):a instanceof Error?new g(a.message):new g(a),x=()=>typeof window!="undefined"&&typeof window.document!="undefined"&&typeof navigator!="undefined";var Pe={error:"color: red",warn:"color: yellow",info:"color: green"},Ae=(a,t,...e)=>{if(x())x()&&console.log(`%c[${a.toUpperCase()}] [${t}]`,Pe[a],...e);else switch(a){case"error":console.error(...e);break;case"warn":console.warn(...e);break;default:console.log(...e);}},Me=(a,t,...e)=>{var s;x()||((s=process==null?void 0:process.env)==null?void 0:s.DEBUG)==="true"&&Ae(a,t,...e);},T=(a,t,...e)=>c(void 0,null,function*(){let s=[];a.forEach(n=>{let o=n[t];if(typeof o=="function")try{let l=o(...e);l instanceof Promise&&s.push(l);}catch(l){Me("error",`SAFELY_INVOKE_CALLBACKS:${String(t)}:`,l);}}),yield Promise.allSettled(s);}),ee=a=>new Promise(t=>setTimeout(t,a));var P=class{constructor(t){this.activeTasks=0;this.queue=[];this.options=t;}enqueue(t){this.queue.push(t),this.processQueue();}withTimeout(t){return new Promise((e,s)=>{let n=setTimeout(()=>s(new Q({info:"Queue task timeout",cause:new Error("Queue task timeout")})),this.options.timeout);t.execute(t.request).then(o=>{clearTimeout(n),e(o);}).catch(o=>{clearTimeout(n),s(o);});})}executeWithRetry(t,e){return c(this,null,function*(){try{return yield this.withTimeout(t)}catch(s){if(e===0)throw s;let n=!0,o=this.options.retry.initialDelay*Math.pow(this.options.retry.exponentialFactor,this.options.retryCount-e);if(f.isHttpRequestError(s)){if(s.cause.status===429){let l=A.safeParse(t.request);if(l.success){let d=l.data.model.getRetryDelay(s.cause.headers);n=d.shouldRetry,d.delayMs>0&&(o=d.delayMs);}}s.cause.status>=500&&s.cause.status<600;}if(n)return yield ee(o),this.executeWithRetry(t,e-1);throw s}})}processQueue(){return c(this,null,function*(){if(this.activeTasks>=this.options.maxConcurrentTasks)return;let t=this.queue.shift();if(t){this.activeTasks+=1;try{let e=yield this.executeWithRetry(t,this.options.retryCount);t.resolve(e);}catch(e){t.reject(e);}finally{this.activeTasks-=1,this.processQueue();}}})}};var M=class{constructor(t=1e3){this.cache=new lruCache.LRUCache({max:t,allowStale:!1,updateAgeOnGet:!1});}get(t){return c(this,null,function*(){return new Promise((e,s)=>{e(this.cache.get(t));})})}set(t,e){return c(this,null,function*(){return new Promise((s,n)=>{this.cache.set(t,e),s();})})}delete(t){return c(this,null,function*(){return new Promise((e,s)=>{this.cache.delete(t),e();})})}clear(){return c(this,null,function*(){return new Promise((t,e)=>{this.cache.clear(),t();})})}};var I=class{constructor(t,e){this.events=[];this.batchSize=e,this.endpointUrl=t;}record(t,e){let s={event:t,dimensions:e,timestamp:new Date().toISOString()};this.events.push(s),this.events.length>=this.batchSize&&this.flushEvents();}flushEvents(){if(this.events.length===0)return;let t=[...this.events];this.events=[],this.sendEvents(t);}sendEvents(t){return c(this,null,function*(){try{yield U__default.default.post(this.endpointUrl,{events:t},{headers:{"Content-Type":"application/json"}});}catch(e){}})}};var se=zod.z.object({queueOptions:_.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()}),Qe=zod.z.object({enableCache:zod.z.boolean().optional().default(!0),customHeaders:zod.z.record(zod.z.string()).optional(),metadataForCallbacks:zod.z.any().optional()}),A=zod.z.object({model:zod.z.custom(),config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional(),options:Qe.optional()}),ze=zod.z.object({customHeaders:zod.z.record(zod.z.string()).optional(),metadataForCallbacks:zod.z.any().optional()}),oe=zod.z.object({model:zod.z.custom(),config:types.Config(),messages:zod.z.array(types.Message()),tools:zod.z.array(types.Tool()).optional(),options:ze.optional()}),Ie=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(),embeddingRequests:types.EmbeddingRequests(),options:Ie.optional()});var pe=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()}),Pt=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(a,t){return c(this,null,function*(){let e=pe.parse(a),s=a.callbacks||[];try{T(s,"onChatStart",a.metadataForCallbacks);let n={config:e.config,messages:e.messages,tools:e.tools},o={url:yield e.model.getCompleteChatUrl(e.config,e.messages,e.tools),headers:yield e.model.getCompleteChatHeaders(e.config,e.messages,e.tools),data:yield e.model.getCompleteChatData(e.config,e.messages,e.tools)};e.customHeaders&&(o.headers=w(w({},o.headers),e.customHeaders));let l=z(`complete-chat:${o.url}:${e.model.modelSchema.name}`,n);if(e.enableCache){let u=yield a.cache.get(l);if(u)return u.cached=!0,T(s,"onChatCached",a.metadataForCallbacks,u),u}let d=Date.now(),r=yield t.post(o.url,o.data,o.headers),m=Date.now()-d,i={request:n,response:e.model.transformCompleteChatResponse(r.data),cached:!1,latencyInMs:m,metadataForCallbacks:a.metadataForCallbacks,provider:{request:o,response:r}};return e.enableCache&&(yield a.cache.set(l,i)),T(s,"onChatComplete",a.metadataForCallbacks,i),i}catch(n){let o;throw f.isHttpRequestError(n)||n instanceof g?o=n:o=S(n),T(s,"onChatError",a.metadataForCallbacks,o),o}})}var me=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()}),Kt=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 ue(a,t){return c(this,null,function*(){let e=me.parse(a),s=a.callbacks||[];try{T(s,"onGetEmbeddingsStart",a.metadataForCallbacks);let n={config:e.config,embeddingRequests:e.embeddingRequests},o={url:yield e.model.getGetEmbeddingsUrl(e.config,e.embeddingRequests),headers:yield e.model.getGetEmbeddingsHeaders(e.config,e.embeddingRequests),data:yield e.model.getGetEmbeddingsData(e.config,e.embeddingRequests)};e.customHeaders&&(o.headers=w(w({},o.headers),e.customHeaders));let l=z(`get-embeddings:${o.url}:${e.model.modelSchema.name}`,n);if(e.enableCache){let u=yield a.cache.get(l);if(u)return u.cached=!0,T(s,"onGetEmbeddingsCached",a.metadataForCallbacks,u),u}let d=Date.now(),r=yield t.post(o.url,o.data,o.headers),m=Date.now()-d,i={request:n,response:e.model.transformGetEmbeddingsResponse(r.data),cached:!1,latencyInMs:m,metadataForCallbacks:a.metadataForCallbacks,provider:{request:o,response:r}};return e.enableCache&&(yield a.cache.set(l,i)),T(s,"onGetEmbeddingsComplete",a.metadataForCallbacks,i),i}catch(n){let o;throw f.isHttpRequestError(n)||n instanceof g?o=n:o=S(n),T(s,"onGetEmbeddingsError",a.metadataForCallbacks,o),o}})}var ge=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()}),na=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 fe(a,t){return v(this,null,function*(){let e=ge.parse(a),s=a.callbacks||[],n={config:e.config,messages:e.messages,tools:e.tools};try{T(s,"onStreamStart",a.metadataForCallbacks);let y={url:yield new C(e.model.getStreamChatUrl(e.config,e.messages,e.tools)),headers:yield new C(e.model.getStreamChatHeaders(e.config,e.messages,e.tools)),data:yield new C(e.model.getStreamChatData(e.config,e.messages,e.tools))};e.customHeaders&&(y.headers=w(w({},y.headers),e.customHeaders));let k="",O=!0;try{for(var m=E(t.stream(y.url,"post",y.data,y.headers)),i,u,q;i=!(u=yield new C(m.next())).done;i=!1){let D=u.value;try{for(var o=E(e.model.transformStreamChatResponseChunk(D,k)),l,d,r;l=!(d=yield new C(o.next())).done;l=!1){let be=d.value;let $={request:n,response:be.partialResponse,metadataForCallbacks:a.metadataForCallbacks,provider:{request:y,response:D}};T(s,O?"onStreamFirstResponse":"onStreamNewResponse",a.metadataForCallbacks,$),O&&(O=!1),yield $;}}catch(d){r=[d];}finally{try{l&&(d=o.return)&&(yield new C(d.call(o)));}finally{if(r)throw r[0]}}}}catch(u){q=[u];}finally{try{i&&(u=m.return)&&(yield new C(u.call(m)));}finally{if(q)throw q[0]}}T(s,"onStreamEnd",a.metadataForCallbacks);}catch(y){let k;throw f.isHttpRequestError(y)||y instanceof g?k=y:k=S(y),T(s,"onStreamError",a.metadataForCallbacks,k),k}})}var N=class{constructor(t){this.analytics=new I("https://j954t34pkh.execute-api.us-east-1.amazonaws.com/v0/analytics",2);var s,n,o,l;if(!t.dangerouslyAllowBrowser&&x())throw new g("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=se.parse(t);let e={maxConcurrentTasks:((s=this.options.queueOptions)==null?void 0:s.maxConcurrentTasks)||4,retryCount:((n=this.options.queueOptions)==null?void 0:n.retryCount)||3,retry:((o=this.options.queueOptions)==null?void 0:o.retry)||{initialDelay:1e3,exponentialFactor:2},timeout:((l=this.options.queueOptions)==null?void 0:l.timeout)||12e4};this.queues={completeChat:new P(e),getEmbeddings:new P(e)},this.httpClient=t.httpClient||new j({timeoutInMilliseconds:e.timeout*.9}),this.caches={completeChat:t.completeChatCache||new M,getEmbeddings:t.getEmbeddingsCache||new M};}completeChat(t){return c(this,null,function*(){let e=A.parse(t);return this.analytics.record("completeChat",{modelName:e.model.modelSchema.name}),new Promise((s,n)=>{let o={request:e,cache:this.caches.completeChat,resolve:s,reject:n,execute:this.executeCompleteChat.bind(this)};this.queues.completeChat.enqueue(o);})})}executeCompleteChat(t){return c(this,null,function*(){var s,n,o;let e=A.parse(t);return le({cache:this.caches.completeChat,model:e.model,config:e.config,messages:e.messages,tools:e.tools,enableCache:(n=(s=e.options)==null?void 0:s.enableCache)!=null?n:!0,callbacks:this.options.completeChatCallbacks,metadataForCallbacks:(o=e.options)==null?void 0:o.metadataForCallbacks},this.httpClient)})}streamChat(t){return v(this,null,function*(){var e,d;try{let r=oe.parse(t);this.analytics.record("streamChat",{modelName:r.model.modelSchema.name});try{for(var s=E(fe({model:r.model,config:r.config,messages:r.messages,tools:r.tools,callbacks:this.options.streamChatCallbacks,metadataForCallbacks:(e=r.options)==null?void 0:e.metadataForCallbacks},this.httpClient)),n,o,l;n=!(o=yield new C(s.next())).done;n=!1){let m=o.value;yield m;}}catch(o){l=[o];}finally{try{n&&(o=s.return)&&(yield new C(o.call(s)));}finally{if(l)throw l[0]}}}catch(r){throw r instanceof g?r:new g(r==null?void 0:r.message,500,(d=r==null?void 0:r.response)==null?void 0:d.data)}})}getEmbeddings(t){return c(this,null,function*(){let e=K.parse(t);return this.analytics.record("getEmbeddings",{modelName:e.model.modelSchema.name}),new Promise((s,n)=>{let o={request:e,cache:this.caches.getEmbeddings,resolve:s,reject:n,execute:this.executeGetEmbeddingsTask.bind(this)};this.queues.getEmbeddings.enqueue(o);})})}executeGetEmbeddingsTask(t){return c(this,null,function*(){var s,n,o;let e=K.parse(t);return ue({cache:this.caches.getEmbeddings,model:e.model,config:e.config,embeddingRequests:e.embeddingRequests,enableCache:(n=(s=e.options)==null?void 0:s.enableCache)!=null?n:!0,callbacks:this.options.getEmbeddingsCallbacks,metadataForCallbacks:(o=e.options)==null?void 0:o.metadataForCallbacks},this.httpClient)})}};N.GatewayError=g;
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
exports.
|
|
23
|
-
exports.CompleteChatHandlerResponse = Gt;
|
|
24
|
-
exports.Gateway = K;
|
|
25
|
-
exports.GatewayError = f;
|
|
26
|
-
exports.GetEmbeddingsHandlerRequest = de;
|
|
27
|
-
exports.GetEmbeddingsHandlerResponse = zt;
|
|
17
|
+
exports.CompleteChatHandlerRequest = pe;
|
|
18
|
+
exports.CompleteChatHandlerResponse = Pt;
|
|
19
|
+
exports.Gateway = N;
|
|
20
|
+
exports.GatewayError = g;
|
|
21
|
+
exports.GetEmbeddingsHandlerRequest = me;
|
|
22
|
+
exports.GetEmbeddingsHandlerResponse = Kt;
|
|
28
23
|
exports.HttpClientError = H;
|
|
29
|
-
exports.HttpRequestError =
|
|
24
|
+
exports.HttpRequestError = f;
|
|
30
25
|
exports.IsomorphicHttpClient = j;
|
|
31
|
-
exports.LRUCache =
|
|
32
|
-
exports.
|
|
26
|
+
exports.LRUCache = M;
|
|
27
|
+
exports.PostAnalytics = I;
|
|
28
|
+
exports.QueueOptions = _;
|
|
33
29
|
exports.QueueTaskTimeoutError = Q;
|
|
34
30
|
exports.SimpleQueue = P;
|
|
35
|
-
exports.StreamChatHandlerRequest =
|
|
36
|
-
exports.StreamChatHandlerResponse =
|
|
37
|
-
exports.handleCompleteChat =
|
|
38
|
-
exports.handleGetEmbeddings =
|
|
31
|
+
exports.StreamChatHandlerRequest = ge;
|
|
32
|
+
exports.StreamChatHandlerResponse = na;
|
|
33
|
+
exports.handleCompleteChat = le;
|
|
34
|
+
exports.handleGetEmbeddings = ue;
|
|
39
35
|
exports.handleStreamChat = fe;
|
|
40
36
|
//# sourceMappingURL=index.js.map
|
|
41
37
|
//# sourceMappingURL=index.js.map
|