ai 2.0.1 → 2.1.2
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.ts +48 -8
- package/dist/index.mjs +234 -22
- package/package.json +20 -8
- package/react/dist/index.d.ts +206 -3
- package/react/dist/index.mjs +384 -7
- package/svelte/dist/index.d.ts +194 -4
- package/svelte/dist/index.mjs +779 -7
- package/{react/dist/types-f862f74a.d.ts → vue/dist/index.d.ts} +72 -1
- package/vue/dist/index.js +384 -0
- package/vue/dist/index.mjs +349 -0
- package/dist/ai-stream.d.ts +0 -18
- package/dist/ai-stream.js +0 -132
- package/dist/ai-stream.mjs +0 -13
- package/dist/anthropic-stream.d.ts +0 -5
- package/dist/anthropic-stream.js +0 -133
- package/dist/anthropic-stream.mjs +0 -8
- package/dist/chunk-265FSSO4.mjs +0 -91
- package/dist/chunk-2L3ZO4UM.mjs +0 -45
- package/dist/chunk-GT4HKF2X.mjs +0 -33
- package/dist/chunk-JGDC3BXD.mjs +0 -22
- package/dist/chunk-NK2CVBLI.mjs +0 -38
- package/dist/chunk-PEYAHBDF.mjs +0 -43
- package/dist/chunk-TJMME6CL.mjs +0 -24
- package/dist/huggingface-stream.d.ts +0 -5
- package/dist/huggingface-stream.js +0 -121
- package/dist/huggingface-stream.mjs +0 -8
- package/dist/index.test.d.ts +0 -2
- package/dist/index.test.js +0 -12
- package/dist/index.test.mjs +0 -10
- package/dist/langchain-stream.d.ts +0 -12
- package/dist/langchain-stream.js +0 -102
- package/dist/langchain-stream.mjs +0 -8
- package/dist/openai-stream.d.ts +0 -5
- package/dist/openai-stream.js +0 -144
- package/dist/openai-stream.mjs +0 -8
- package/dist/streaming-text-response.d.ts +0 -17
- package/dist/streaming-text-response.js +0 -75
- package/dist/streaming-text-response.mjs +0 -9
- package/react/dist/chunk-5PP6W52J.mjs +0 -202
- package/react/dist/chunk-6EH3SWMP.mjs +0 -55
- package/react/dist/chunk-PW6HSU2N.mjs +0 -154
- package/react/dist/use-chat.d.ts +0 -42
- package/react/dist/use-chat.js +0 -276
- package/react/dist/use-chat.mjs +0 -8
- package/react/dist/use-completion.d.ts +0 -47
- package/react/dist/use-completion.js +0 -229
- package/react/dist/use-completion.mjs +0 -8
- package/svelte/dist/chunk-6USBQIV6.mjs +0 -177
- package/svelte/dist/chunk-BQ64GHZ3.mjs +0 -136
- package/svelte/dist/chunk-CENOSGDG.mjs +0 -493
- package/svelte/dist/types-f862f74a.d.ts +0 -123
- package/svelte/dist/use-chat.d.ts +0 -39
- package/svelte/dist/use-chat.js +0 -680
- package/svelte/dist/use-chat.mjs +0 -7
- package/svelte/dist/use-completion.d.ts +0 -38
- package/svelte/dist/use-completion.js +0 -640
- package/svelte/dist/use-completion.mjs +0 -7
@@ -0,0 +1,349 @@
|
|
1
|
+
var __defProp = Object.defineProperty;
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
6
|
+
var __spreadValues = (a, b) => {
|
7
|
+
for (var prop in b || (b = {}))
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
10
|
+
if (__getOwnPropSymbols)
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
12
|
+
if (__propIsEnum.call(b, prop))
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
14
|
+
}
|
15
|
+
return a;
|
16
|
+
};
|
17
|
+
var __async = (__this, __arguments, generator) => {
|
18
|
+
return new Promise((resolve, reject) => {
|
19
|
+
var fulfilled = (value) => {
|
20
|
+
try {
|
21
|
+
step(generator.next(value));
|
22
|
+
} catch (e) {
|
23
|
+
reject(e);
|
24
|
+
}
|
25
|
+
};
|
26
|
+
var rejected = (value) => {
|
27
|
+
try {
|
28
|
+
step(generator.throw(value));
|
29
|
+
} catch (e) {
|
30
|
+
reject(e);
|
31
|
+
}
|
32
|
+
};
|
33
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
34
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
35
|
+
});
|
36
|
+
};
|
37
|
+
|
38
|
+
// vue/use-chat.ts
|
39
|
+
import swrv from "swrv";
|
40
|
+
import { ref } from "vue";
|
41
|
+
|
42
|
+
// shared/utils.ts
|
43
|
+
import { customAlphabet } from "nanoid";
|
44
|
+
var nanoid = customAlphabet(
|
45
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
46
|
+
7
|
47
|
+
);
|
48
|
+
var decoder = new TextDecoder();
|
49
|
+
function decodeAIStreamChunk(chunk) {
|
50
|
+
return decoder.decode(chunk);
|
51
|
+
}
|
52
|
+
|
53
|
+
// vue/use-chat.ts
|
54
|
+
var uniqueId = 0;
|
55
|
+
var useSWRV = swrv.default || swrv;
|
56
|
+
var store = {};
|
57
|
+
function useChat({
|
58
|
+
api = "/api/chat",
|
59
|
+
id,
|
60
|
+
initialMessages = [],
|
61
|
+
initialInput = "",
|
62
|
+
sendExtraMessageFields,
|
63
|
+
onResponse,
|
64
|
+
onFinish,
|
65
|
+
onError,
|
66
|
+
headers,
|
67
|
+
body
|
68
|
+
} = {}) {
|
69
|
+
const chatId = id || `chat-${uniqueId++}`;
|
70
|
+
const key = `${api}|${chatId}`;
|
71
|
+
const { data, mutate: originalMutate } = useSWRV(
|
72
|
+
key,
|
73
|
+
() => store[key] || initialMessages
|
74
|
+
);
|
75
|
+
data.value || (data.value = initialMessages);
|
76
|
+
const mutate = (data2) => {
|
77
|
+
store[key] = data2;
|
78
|
+
return originalMutate();
|
79
|
+
};
|
80
|
+
const messages = data;
|
81
|
+
const error = ref(void 0);
|
82
|
+
const isLoading = ref(false);
|
83
|
+
let abortController = null;
|
84
|
+
function triggerRequest(messagesSnapshot) {
|
85
|
+
return __async(this, null, function* () {
|
86
|
+
try {
|
87
|
+
isLoading.value = true;
|
88
|
+
abortController = new AbortController();
|
89
|
+
const previousMessages = messages.value;
|
90
|
+
mutate(messagesSnapshot);
|
91
|
+
const res = yield fetch(api, {
|
92
|
+
method: "POST",
|
93
|
+
body: JSON.stringify(__spreadValues({
|
94
|
+
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
95
|
+
role,
|
96
|
+
content
|
97
|
+
}))
|
98
|
+
}, body)),
|
99
|
+
headers: headers || {},
|
100
|
+
signal: abortController.signal
|
101
|
+
}).catch((err) => {
|
102
|
+
mutate(previousMessages);
|
103
|
+
throw err;
|
104
|
+
});
|
105
|
+
if (onResponse) {
|
106
|
+
try {
|
107
|
+
yield onResponse(res);
|
108
|
+
} catch (err) {
|
109
|
+
throw err;
|
110
|
+
}
|
111
|
+
}
|
112
|
+
if (!res.ok) {
|
113
|
+
mutate(previousMessages);
|
114
|
+
throw new Error(
|
115
|
+
(yield res.text()) || "Failed to fetch the chat response."
|
116
|
+
);
|
117
|
+
}
|
118
|
+
if (!res.body) {
|
119
|
+
throw new Error("The response body is empty.");
|
120
|
+
}
|
121
|
+
let result = "";
|
122
|
+
const createdAt = /* @__PURE__ */ new Date();
|
123
|
+
const replyId = nanoid();
|
124
|
+
const reader = res.body.getReader();
|
125
|
+
while (true) {
|
126
|
+
const { done, value } = yield reader.read();
|
127
|
+
if (done) {
|
128
|
+
break;
|
129
|
+
}
|
130
|
+
result += decodeAIStreamChunk(value);
|
131
|
+
mutate([
|
132
|
+
...messagesSnapshot,
|
133
|
+
{
|
134
|
+
id: replyId,
|
135
|
+
createdAt,
|
136
|
+
content: result,
|
137
|
+
role: "assistant"
|
138
|
+
}
|
139
|
+
]);
|
140
|
+
if (abortController === null) {
|
141
|
+
reader.cancel();
|
142
|
+
break;
|
143
|
+
}
|
144
|
+
}
|
145
|
+
if (onFinish) {
|
146
|
+
onFinish({
|
147
|
+
id: replyId,
|
148
|
+
createdAt,
|
149
|
+
content: result,
|
150
|
+
role: "assistant"
|
151
|
+
});
|
152
|
+
}
|
153
|
+
abortController = null;
|
154
|
+
return result;
|
155
|
+
} catch (err) {
|
156
|
+
if (err.name === "AbortError") {
|
157
|
+
abortController = null;
|
158
|
+
return null;
|
159
|
+
}
|
160
|
+
if (onError && err instanceof Error) {
|
161
|
+
onError(err);
|
162
|
+
}
|
163
|
+
error.value = err;
|
164
|
+
} finally {
|
165
|
+
isLoading.value = false;
|
166
|
+
}
|
167
|
+
});
|
168
|
+
}
|
169
|
+
const append = (message) => __async(this, null, function* () {
|
170
|
+
if (!message.id) {
|
171
|
+
message.id = nanoid();
|
172
|
+
}
|
173
|
+
return triggerRequest(messages.value.concat(message));
|
174
|
+
});
|
175
|
+
const reload = () => __async(this, null, function* () {
|
176
|
+
const messagesSnapshot = messages.value;
|
177
|
+
if (messagesSnapshot.length === 0)
|
178
|
+
return null;
|
179
|
+
const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];
|
180
|
+
if (lastMessage.role === "assistant") {
|
181
|
+
return triggerRequest(messagesSnapshot.slice(0, -1));
|
182
|
+
}
|
183
|
+
return triggerRequest(messagesSnapshot);
|
184
|
+
});
|
185
|
+
const stop = () => {
|
186
|
+
if (abortController) {
|
187
|
+
abortController.abort();
|
188
|
+
abortController = null;
|
189
|
+
}
|
190
|
+
};
|
191
|
+
const setMessages = (messages2) => {
|
192
|
+
mutate(messages2);
|
193
|
+
};
|
194
|
+
const input = ref(initialInput);
|
195
|
+
const handleSubmit = (e) => {
|
196
|
+
e.preventDefault();
|
197
|
+
const inputValue = input.value;
|
198
|
+
if (!inputValue)
|
199
|
+
return;
|
200
|
+
append({
|
201
|
+
content: inputValue,
|
202
|
+
role: "user"
|
203
|
+
});
|
204
|
+
input.value = "";
|
205
|
+
};
|
206
|
+
return {
|
207
|
+
messages,
|
208
|
+
append,
|
209
|
+
error,
|
210
|
+
reload,
|
211
|
+
stop,
|
212
|
+
setMessages,
|
213
|
+
input,
|
214
|
+
handleSubmit,
|
215
|
+
isLoading
|
216
|
+
};
|
217
|
+
}
|
218
|
+
|
219
|
+
// vue/use-completion.ts
|
220
|
+
import swrv2 from "swrv";
|
221
|
+
import { ref as ref2 } from "vue";
|
222
|
+
var uniqueId2 = 0;
|
223
|
+
var useSWRV2 = swrv2.default || swrv2;
|
224
|
+
var store2 = {};
|
225
|
+
function useCompletion({
|
226
|
+
api = "/api/completion",
|
227
|
+
id,
|
228
|
+
initialCompletion = "",
|
229
|
+
initialInput = "",
|
230
|
+
headers,
|
231
|
+
body,
|
232
|
+
onResponse,
|
233
|
+
onFinish,
|
234
|
+
onError
|
235
|
+
} = {}) {
|
236
|
+
const completionId = id || `completion-${uniqueId2++}`;
|
237
|
+
const key = `${api}|${completionId}`;
|
238
|
+
const { data, mutate: originalMutate } = useSWRV2(
|
239
|
+
key,
|
240
|
+
() => store2[key] || initialCompletion
|
241
|
+
);
|
242
|
+
data.value || (data.value = initialCompletion);
|
243
|
+
const mutate = (data2) => {
|
244
|
+
store2[key] = data2;
|
245
|
+
return originalMutate();
|
246
|
+
};
|
247
|
+
const completion = data;
|
248
|
+
const error = ref2(void 0);
|
249
|
+
const isLoading = ref2(false);
|
250
|
+
let abortController = null;
|
251
|
+
function triggerRequest(prompt) {
|
252
|
+
return __async(this, null, function* () {
|
253
|
+
try {
|
254
|
+
isLoading.value = true;
|
255
|
+
abortController = new AbortController();
|
256
|
+
mutate("");
|
257
|
+
const res = yield fetch(api, {
|
258
|
+
method: "POST",
|
259
|
+
body: JSON.stringify(__spreadValues({
|
260
|
+
prompt
|
261
|
+
}, body)),
|
262
|
+
headers: headers || {},
|
263
|
+
signal: abortController.signal
|
264
|
+
}).catch((err) => {
|
265
|
+
throw err;
|
266
|
+
});
|
267
|
+
if (onResponse) {
|
268
|
+
try {
|
269
|
+
yield onResponse(res);
|
270
|
+
} catch (err) {
|
271
|
+
throw err;
|
272
|
+
}
|
273
|
+
}
|
274
|
+
if (!res.ok) {
|
275
|
+
throw new Error(
|
276
|
+
(yield res.text()) || "Failed to fetch the chat response."
|
277
|
+
);
|
278
|
+
}
|
279
|
+
if (!res.body) {
|
280
|
+
throw new Error("The response body is empty.");
|
281
|
+
}
|
282
|
+
let result = "";
|
283
|
+
const reader = res.body.getReader();
|
284
|
+
while (true) {
|
285
|
+
const { done, value } = yield reader.read();
|
286
|
+
if (done) {
|
287
|
+
break;
|
288
|
+
}
|
289
|
+
result += decodeAIStreamChunk(value);
|
290
|
+
mutate(result);
|
291
|
+
if (abortController === null) {
|
292
|
+
reader.cancel();
|
293
|
+
break;
|
294
|
+
}
|
295
|
+
}
|
296
|
+
if (onFinish) {
|
297
|
+
onFinish(prompt, result);
|
298
|
+
}
|
299
|
+
abortController = null;
|
300
|
+
return result;
|
301
|
+
} catch (err) {
|
302
|
+
if (err.name === "AbortError") {
|
303
|
+
abortController = null;
|
304
|
+
return null;
|
305
|
+
}
|
306
|
+
if (onError && error instanceof Error) {
|
307
|
+
onError(error);
|
308
|
+
}
|
309
|
+
error.value = err;
|
310
|
+
} finally {
|
311
|
+
isLoading.value = false;
|
312
|
+
}
|
313
|
+
});
|
314
|
+
}
|
315
|
+
const complete = (prompt) => __async(this, null, function* () {
|
316
|
+
return triggerRequest(prompt);
|
317
|
+
});
|
318
|
+
const stop = () => {
|
319
|
+
if (abortController) {
|
320
|
+
abortController.abort();
|
321
|
+
abortController = null;
|
322
|
+
}
|
323
|
+
};
|
324
|
+
const setCompletion = (completion2) => {
|
325
|
+
mutate(completion2);
|
326
|
+
};
|
327
|
+
const input = ref2(initialInput);
|
328
|
+
const handleSubmit = (e) => {
|
329
|
+
e.preventDefault();
|
330
|
+
const inputValue = input.value;
|
331
|
+
if (!inputValue)
|
332
|
+
return;
|
333
|
+
return complete(inputValue);
|
334
|
+
};
|
335
|
+
return {
|
336
|
+
completion,
|
337
|
+
complete,
|
338
|
+
error,
|
339
|
+
stop,
|
340
|
+
setCompletion,
|
341
|
+
input,
|
342
|
+
handleSubmit,
|
343
|
+
isLoading
|
344
|
+
};
|
345
|
+
}
|
346
|
+
export {
|
347
|
+
useChat,
|
348
|
+
useCompletion
|
349
|
+
};
|
package/dist/ai-stream.d.ts
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
interface AIStreamCallbacks {
|
2
|
-
onStart?: () => Promise<void>;
|
3
|
-
onCompletion?: (completion: string) => Promise<void>;
|
4
|
-
onToken?: (token: string) => Promise<void>;
|
5
|
-
}
|
6
|
-
interface AIStreamParser {
|
7
|
-
(data: string): string | void;
|
8
|
-
}
|
9
|
-
declare function createEventStreamTransformer(customParser: AIStreamParser): TransformStream<Uint8Array, string>;
|
10
|
-
/**
|
11
|
-
* This stream forks input stream, allowing us to use the result as a
|
12
|
-
* bytestream of the messages and pass the messages to our callback interface.
|
13
|
-
*/
|
14
|
-
declare function createCallbacksTransformer(callbacks: AIStreamCallbacks | undefined): TransformStream<string, Uint8Array>;
|
15
|
-
declare function trimStartOfStreamHelper(): (text: string) => string;
|
16
|
-
declare function AIStream(res: Response, customParser: AIStreamParser, callbacks?: AIStreamCallbacks): ReadableStream;
|
17
|
-
|
18
|
-
export { AIStream, AIStreamCallbacks, AIStreamParser, createCallbacksTransformer, createEventStreamTransformer, trimStartOfStreamHelper };
|
package/dist/ai-stream.js
DELETED
@@ -1,132 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __defProp = Object.defineProperty;
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
-
var __export = (target, all) => {
|
7
|
-
for (var name in all)
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
-
};
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
-
for (let key of __getOwnPropNames(from))
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
-
}
|
16
|
-
return to;
|
17
|
-
};
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
-
var __async = (__this, __arguments, generator) => {
|
20
|
-
return new Promise((resolve, reject) => {
|
21
|
-
var fulfilled = (value) => {
|
22
|
-
try {
|
23
|
-
step(generator.next(value));
|
24
|
-
} catch (e) {
|
25
|
-
reject(e);
|
26
|
-
}
|
27
|
-
};
|
28
|
-
var rejected = (value) => {
|
29
|
-
try {
|
30
|
-
step(generator.throw(value));
|
31
|
-
} catch (e) {
|
32
|
-
reject(e);
|
33
|
-
}
|
34
|
-
};
|
35
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
36
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
37
|
-
});
|
38
|
-
};
|
39
|
-
|
40
|
-
// streams/ai-stream.ts
|
41
|
-
var ai_stream_exports = {};
|
42
|
-
__export(ai_stream_exports, {
|
43
|
-
AIStream: () => AIStream,
|
44
|
-
createCallbacksTransformer: () => createCallbacksTransformer,
|
45
|
-
createEventStreamTransformer: () => createEventStreamTransformer,
|
46
|
-
trimStartOfStreamHelper: () => trimStartOfStreamHelper
|
47
|
-
});
|
48
|
-
module.exports = __toCommonJS(ai_stream_exports);
|
49
|
-
var import_eventsource_parser = require("eventsource-parser");
|
50
|
-
function createEventStreamTransformer(customParser) {
|
51
|
-
const decoder = new TextDecoder();
|
52
|
-
let parser;
|
53
|
-
return new TransformStream({
|
54
|
-
start(controller) {
|
55
|
-
return __async(this, null, function* () {
|
56
|
-
function onParse(event) {
|
57
|
-
if (event.type === "event") {
|
58
|
-
const data = event.data;
|
59
|
-
if (data === "[DONE]") {
|
60
|
-
controller.terminate();
|
61
|
-
return;
|
62
|
-
}
|
63
|
-
const message = customParser(data);
|
64
|
-
if (message)
|
65
|
-
controller.enqueue(message);
|
66
|
-
}
|
67
|
-
}
|
68
|
-
parser = (0, import_eventsource_parser.createParser)(onParse);
|
69
|
-
});
|
70
|
-
},
|
71
|
-
transform(chunk) {
|
72
|
-
parser.feed(decoder.decode(chunk));
|
73
|
-
}
|
74
|
-
});
|
75
|
-
}
|
76
|
-
function createCallbacksTransformer(callbacks) {
|
77
|
-
const encoder = new TextEncoder();
|
78
|
-
let fullResponse = "";
|
79
|
-
const { onStart, onToken, onCompletion } = callbacks || {};
|
80
|
-
return new TransformStream({
|
81
|
-
start() {
|
82
|
-
return __async(this, null, function* () {
|
83
|
-
if (onStart)
|
84
|
-
yield onStart();
|
85
|
-
});
|
86
|
-
},
|
87
|
-
transform(message, controller) {
|
88
|
-
return __async(this, null, function* () {
|
89
|
-
controller.enqueue(encoder.encode(message));
|
90
|
-
if (onToken)
|
91
|
-
yield onToken(message);
|
92
|
-
if (onCompletion)
|
93
|
-
fullResponse += message;
|
94
|
-
});
|
95
|
-
},
|
96
|
-
flush() {
|
97
|
-
return __async(this, null, function* () {
|
98
|
-
yield onCompletion == null ? void 0 : onCompletion(fullResponse);
|
99
|
-
});
|
100
|
-
}
|
101
|
-
});
|
102
|
-
}
|
103
|
-
function trimStartOfStreamHelper() {
|
104
|
-
let start = true;
|
105
|
-
return (text) => {
|
106
|
-
if (start)
|
107
|
-
text = text.trimStart();
|
108
|
-
if (text)
|
109
|
-
start = false;
|
110
|
-
return text;
|
111
|
-
};
|
112
|
-
}
|
113
|
-
function AIStream(res, customParser, callbacks) {
|
114
|
-
if (!res.ok) {
|
115
|
-
throw new Error(
|
116
|
-
`Failed to convert the response to stream. Received status code: ${res.status}.`
|
117
|
-
);
|
118
|
-
}
|
119
|
-
const stream = res.body || new ReadableStream({
|
120
|
-
start(controller) {
|
121
|
-
controller.close();
|
122
|
-
}
|
123
|
-
});
|
124
|
-
return stream.pipeThrough(createEventStreamTransformer(customParser)).pipeThrough(createCallbacksTransformer(callbacks));
|
125
|
-
}
|
126
|
-
// Annotate the CommonJS export names for ESM import in node:
|
127
|
-
0 && (module.exports = {
|
128
|
-
AIStream,
|
129
|
-
createCallbacksTransformer,
|
130
|
-
createEventStreamTransformer,
|
131
|
-
trimStartOfStreamHelper
|
132
|
-
});
|
package/dist/ai-stream.mjs
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
AIStream,
|
3
|
-
createCallbacksTransformer,
|
4
|
-
createEventStreamTransformer,
|
5
|
-
trimStartOfStreamHelper
|
6
|
-
} from "./chunk-265FSSO4.mjs";
|
7
|
-
import "./chunk-2L3ZO4UM.mjs";
|
8
|
-
export {
|
9
|
-
AIStream,
|
10
|
-
createCallbacksTransformer,
|
11
|
-
createEventStreamTransformer,
|
12
|
-
trimStartOfStreamHelper
|
13
|
-
};
|
package/dist/anthropic-stream.js
DELETED
@@ -1,133 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __defProp = Object.defineProperty;
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
-
var __export = (target, all) => {
|
7
|
-
for (var name in all)
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
-
};
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
-
for (let key of __getOwnPropNames(from))
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
-
}
|
16
|
-
return to;
|
17
|
-
};
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
-
var __async = (__this, __arguments, generator) => {
|
20
|
-
return new Promise((resolve, reject) => {
|
21
|
-
var fulfilled = (value) => {
|
22
|
-
try {
|
23
|
-
step(generator.next(value));
|
24
|
-
} catch (e) {
|
25
|
-
reject(e);
|
26
|
-
}
|
27
|
-
};
|
28
|
-
var rejected = (value) => {
|
29
|
-
try {
|
30
|
-
step(generator.throw(value));
|
31
|
-
} catch (e) {
|
32
|
-
reject(e);
|
33
|
-
}
|
34
|
-
};
|
35
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
36
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
37
|
-
});
|
38
|
-
};
|
39
|
-
|
40
|
-
// streams/anthropic-stream.ts
|
41
|
-
var anthropic_stream_exports = {};
|
42
|
-
__export(anthropic_stream_exports, {
|
43
|
-
AnthropicStream: () => AnthropicStream
|
44
|
-
});
|
45
|
-
module.exports = __toCommonJS(anthropic_stream_exports);
|
46
|
-
|
47
|
-
// streams/ai-stream.ts
|
48
|
-
var import_eventsource_parser = require("eventsource-parser");
|
49
|
-
function createEventStreamTransformer(customParser) {
|
50
|
-
const decoder = new TextDecoder();
|
51
|
-
let parser;
|
52
|
-
return new TransformStream({
|
53
|
-
start(controller) {
|
54
|
-
return __async(this, null, function* () {
|
55
|
-
function onParse(event) {
|
56
|
-
if (event.type === "event") {
|
57
|
-
const data = event.data;
|
58
|
-
if (data === "[DONE]") {
|
59
|
-
controller.terminate();
|
60
|
-
return;
|
61
|
-
}
|
62
|
-
const message = customParser(data);
|
63
|
-
if (message)
|
64
|
-
controller.enqueue(message);
|
65
|
-
}
|
66
|
-
}
|
67
|
-
parser = (0, import_eventsource_parser.createParser)(onParse);
|
68
|
-
});
|
69
|
-
},
|
70
|
-
transform(chunk) {
|
71
|
-
parser.feed(decoder.decode(chunk));
|
72
|
-
}
|
73
|
-
});
|
74
|
-
}
|
75
|
-
function createCallbacksTransformer(callbacks) {
|
76
|
-
const encoder = new TextEncoder();
|
77
|
-
let fullResponse = "";
|
78
|
-
const { onStart, onToken, onCompletion } = callbacks || {};
|
79
|
-
return new TransformStream({
|
80
|
-
start() {
|
81
|
-
return __async(this, null, function* () {
|
82
|
-
if (onStart)
|
83
|
-
yield onStart();
|
84
|
-
});
|
85
|
-
},
|
86
|
-
transform(message, controller) {
|
87
|
-
return __async(this, null, function* () {
|
88
|
-
controller.enqueue(encoder.encode(message));
|
89
|
-
if (onToken)
|
90
|
-
yield onToken(message);
|
91
|
-
if (onCompletion)
|
92
|
-
fullResponse += message;
|
93
|
-
});
|
94
|
-
},
|
95
|
-
flush() {
|
96
|
-
return __async(this, null, function* () {
|
97
|
-
yield onCompletion == null ? void 0 : onCompletion(fullResponse);
|
98
|
-
});
|
99
|
-
}
|
100
|
-
});
|
101
|
-
}
|
102
|
-
function AIStream(res, customParser, callbacks) {
|
103
|
-
if (!res.ok) {
|
104
|
-
throw new Error(
|
105
|
-
`Failed to convert the response to stream. Received status code: ${res.status}.`
|
106
|
-
);
|
107
|
-
}
|
108
|
-
const stream = res.body || new ReadableStream({
|
109
|
-
start(controller) {
|
110
|
-
controller.close();
|
111
|
-
}
|
112
|
-
});
|
113
|
-
return stream.pipeThrough(createEventStreamTransformer(customParser)).pipeThrough(createCallbacksTransformer(callbacks));
|
114
|
-
}
|
115
|
-
|
116
|
-
// streams/anthropic-stream.ts
|
117
|
-
function parseAnthropicStream() {
|
118
|
-
let previous = "";
|
119
|
-
return (data) => {
|
120
|
-
const json = JSON.parse(data);
|
121
|
-
const text = json.completion;
|
122
|
-
const delta = text.slice(previous.length);
|
123
|
-
previous = text;
|
124
|
-
return delta;
|
125
|
-
};
|
126
|
-
}
|
127
|
-
function AnthropicStream(res, cb) {
|
128
|
-
return AIStream(res, parseAnthropicStream(), cb);
|
129
|
-
}
|
130
|
-
// Annotate the CommonJS export names for ESM import in node:
|
131
|
-
0 && (module.exports = {
|
132
|
-
AnthropicStream
|
133
|
-
});
|