ai 2.1.26 → 2.1.27
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 -3
- package/dist/index.js +236 -253
- package/dist/index.mjs +235 -256
- package/package.json +3 -3
- package/react/dist/index.js +177 -197
- package/react/dist/index.mjs +177 -199
- package/react/dist/index.server.js +14 -38
- package/react/dist/index.server.mjs +14 -39
- package/solid/dist/index.js +156 -184
- package/solid/dist/index.mjs +156 -187
- package/svelte/dist/index.d.ts +2 -2
- package/svelte/dist/index.js +232 -236
- package/svelte/dist/index.mjs +232 -239
- package/vue/dist/index.d.ts +2 -2
- package/vue/dist/index.js +153 -177
- package/vue/dist/index.mjs +153 -180
package/solid/dist/index.mjs
CHANGED
@@ -1,40 +1,3 @@
|
|
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
1
|
// solid/use-chat.ts
|
39
2
|
import { createSignal } from "solid-js";
|
40
3
|
import { useSWRStore } from "solid-swr-store";
|
@@ -59,10 +22,10 @@ function createChunkDecoder() {
|
|
59
22
|
var uniqueId = 0;
|
60
23
|
var store = {};
|
61
24
|
var chatApiStore = createSWRStore({
|
62
|
-
get: (key) =>
|
25
|
+
get: async (key) => {
|
63
26
|
var _a;
|
64
27
|
return (_a = store[key]) != null ? _a : [];
|
65
|
-
}
|
28
|
+
}
|
66
29
|
});
|
67
30
|
function useChat({
|
68
31
|
api = "/api/chat",
|
@@ -93,100 +56,103 @@ function useChat({
|
|
93
56
|
const [error, setError] = createSignal(void 0);
|
94
57
|
const [isLoading, setIsLoading] = createSignal(false);
|
95
58
|
let abortController = null;
|
96
|
-
function triggerRequest(messagesSnapshot, options) {
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
try {
|
124
|
-
yield onResponse(res);
|
125
|
-
} catch (err) {
|
126
|
-
throw err;
|
127
|
-
}
|
59
|
+
async function triggerRequest(messagesSnapshot, options) {
|
60
|
+
try {
|
61
|
+
setIsLoading(true);
|
62
|
+
abortController = new AbortController();
|
63
|
+
const previousMessages = chatApiStore.get([key], {
|
64
|
+
shouldRevalidate: false
|
65
|
+
});
|
66
|
+
mutate(messagesSnapshot);
|
67
|
+
const res = await fetch(api, {
|
68
|
+
method: "POST",
|
69
|
+
body: JSON.stringify({
|
70
|
+
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
71
|
+
role,
|
72
|
+
content
|
73
|
+
})),
|
74
|
+
...body,
|
75
|
+
...options == null ? void 0 : options.body
|
76
|
+
}),
|
77
|
+
headers: {
|
78
|
+
...headers,
|
79
|
+
...options == null ? void 0 : options.headers
|
80
|
+
},
|
81
|
+
signal: abortController.signal,
|
82
|
+
credentials
|
83
|
+
}).catch((err) => {
|
84
|
+
if (previousMessages.status === "success") {
|
85
|
+
mutate(previousMessages.data);
|
128
86
|
}
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
87
|
+
throw err;
|
88
|
+
});
|
89
|
+
if (onResponse) {
|
90
|
+
try {
|
91
|
+
await onResponse(res);
|
92
|
+
} catch (err) {
|
93
|
+
throw err;
|
136
94
|
}
|
137
|
-
|
138
|
-
|
95
|
+
}
|
96
|
+
if (!res.ok) {
|
97
|
+
if (previousMessages.status === "success") {
|
98
|
+
mutate(previousMessages.data);
|
139
99
|
}
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
content: result,
|
157
|
-
role: "assistant"
|
158
|
-
}
|
159
|
-
]);
|
160
|
-
if (abortController === null) {
|
161
|
-
reader.cancel();
|
162
|
-
break;
|
163
|
-
}
|
100
|
+
throw new Error(
|
101
|
+
await res.text() || "Failed to fetch the chat response."
|
102
|
+
);
|
103
|
+
}
|
104
|
+
if (!res.body) {
|
105
|
+
throw new Error("The response body is empty.");
|
106
|
+
}
|
107
|
+
let result = "";
|
108
|
+
const createdAt = /* @__PURE__ */ new Date();
|
109
|
+
const replyId = nanoid();
|
110
|
+
const reader = res.body.getReader();
|
111
|
+
const decoder = createChunkDecoder();
|
112
|
+
while (true) {
|
113
|
+
const { done, value } = await reader.read();
|
114
|
+
if (done) {
|
115
|
+
break;
|
164
116
|
}
|
165
|
-
|
166
|
-
|
117
|
+
result += decoder(value);
|
118
|
+
mutate([
|
119
|
+
...messagesSnapshot,
|
120
|
+
{
|
167
121
|
id: replyId,
|
168
122
|
createdAt,
|
169
123
|
content: result,
|
170
124
|
role: "assistant"
|
171
|
-
}
|
125
|
+
}
|
126
|
+
]);
|
127
|
+
if (abortController === null) {
|
128
|
+
reader.cancel();
|
129
|
+
break;
|
172
130
|
}
|
131
|
+
}
|
132
|
+
if (onFinish) {
|
133
|
+
onFinish({
|
134
|
+
id: replyId,
|
135
|
+
createdAt,
|
136
|
+
content: result,
|
137
|
+
role: "assistant"
|
138
|
+
});
|
139
|
+
}
|
140
|
+
abortController = null;
|
141
|
+
return result;
|
142
|
+
} catch (err) {
|
143
|
+
if (err.name === "AbortError") {
|
173
144
|
abortController = null;
|
174
|
-
return
|
175
|
-
} catch (err) {
|
176
|
-
if (err.name === "AbortError") {
|
177
|
-
abortController = null;
|
178
|
-
return null;
|
179
|
-
}
|
180
|
-
if (onError && err instanceof Error) {
|
181
|
-
onError(err);
|
182
|
-
}
|
183
|
-
setError(err);
|
184
|
-
} finally {
|
185
|
-
setIsLoading(false);
|
145
|
+
return null;
|
186
146
|
}
|
187
|
-
|
147
|
+
if (onError && err instanceof Error) {
|
148
|
+
onError(err);
|
149
|
+
}
|
150
|
+
setError(err);
|
151
|
+
} finally {
|
152
|
+
setIsLoading(false);
|
153
|
+
}
|
188
154
|
}
|
189
|
-
const append = (message, options) =>
|
155
|
+
const append = async (message, options) => {
|
190
156
|
var _a;
|
191
157
|
if (!message.id) {
|
192
158
|
message.id = nanoid();
|
@@ -195,8 +161,8 @@ function useChat({
|
|
195
161
|
((_a = messages()) != null ? _a : []).concat(message),
|
196
162
|
options
|
197
163
|
);
|
198
|
-
}
|
199
|
-
const reload = (options) =>
|
164
|
+
};
|
165
|
+
const reload = async (options) => {
|
200
166
|
const messagesSnapshot = messages();
|
201
167
|
if (!messagesSnapshot || messagesSnapshot.length === 0)
|
202
168
|
return null;
|
@@ -205,7 +171,7 @@ function useChat({
|
|
205
171
|
return triggerRequest(messagesSnapshot.slice(0, -1), options);
|
206
172
|
}
|
207
173
|
return triggerRequest(messagesSnapshot, options);
|
208
|
-
}
|
174
|
+
};
|
209
175
|
const stop = () => {
|
210
176
|
if (abortController) {
|
211
177
|
abortController.abort();
|
@@ -249,10 +215,10 @@ import { useSWRStore as useSWRStore2 } from "solid-swr-store";
|
|
249
215
|
var uniqueId2 = 0;
|
250
216
|
var store2 = {};
|
251
217
|
var completionApiStore = createSWRStore2({
|
252
|
-
get: (key) =>
|
218
|
+
get: async (key) => {
|
253
219
|
var _a;
|
254
220
|
return (_a = store2[key]) != null ? _a : [];
|
255
|
-
}
|
221
|
+
}
|
256
222
|
});
|
257
223
|
function useCompletion({
|
258
224
|
api = "/api/completion",
|
@@ -282,75 +248,78 @@ function useCompletion({
|
|
282
248
|
const [error, setError] = createSignal2(void 0);
|
283
249
|
const [isLoading, setIsLoading] = createSignal2(false);
|
284
250
|
let abortController = null;
|
285
|
-
function triggerRequest(prompt, options) {
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
251
|
+
async function triggerRequest(prompt, options) {
|
252
|
+
try {
|
253
|
+
setIsLoading(true);
|
254
|
+
abortController = new AbortController();
|
255
|
+
mutate("");
|
256
|
+
const res = await fetch(api, {
|
257
|
+
method: "POST",
|
258
|
+
body: JSON.stringify({
|
259
|
+
prompt,
|
260
|
+
...body,
|
261
|
+
...options == null ? void 0 : options.body
|
262
|
+
}),
|
263
|
+
headers: {
|
264
|
+
...headers,
|
265
|
+
...options == null ? void 0 : options.headers
|
266
|
+
},
|
267
|
+
signal: abortController.signal,
|
268
|
+
credentials
|
269
|
+
}).catch((err) => {
|
270
|
+
throw err;
|
271
|
+
});
|
272
|
+
if (onResponse) {
|
273
|
+
try {
|
274
|
+
await onResponse(res);
|
275
|
+
} catch (err) {
|
300
276
|
throw err;
|
301
|
-
});
|
302
|
-
if (onResponse) {
|
303
|
-
try {
|
304
|
-
yield onResponse(res);
|
305
|
-
} catch (err) {
|
306
|
-
throw err;
|
307
|
-
}
|
308
|
-
}
|
309
|
-
if (!res.ok) {
|
310
|
-
throw new Error(
|
311
|
-
(yield res.text()) || "Failed to fetch the chat response."
|
312
|
-
);
|
313
277
|
}
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
}
|
278
|
+
}
|
279
|
+
if (!res.ok) {
|
280
|
+
throw new Error(
|
281
|
+
await res.text() || "Failed to fetch the chat response."
|
282
|
+
);
|
283
|
+
}
|
284
|
+
if (!res.body) {
|
285
|
+
throw new Error("The response body is empty.");
|
286
|
+
}
|
287
|
+
let result = "";
|
288
|
+
const reader = res.body.getReader();
|
289
|
+
const decoder = createChunkDecoder();
|
290
|
+
while (true) {
|
291
|
+
const { done, value } = await reader.read();
|
292
|
+
if (done) {
|
293
|
+
break;
|
331
294
|
}
|
332
|
-
|
333
|
-
|
295
|
+
result += decoder(value);
|
296
|
+
mutate(result);
|
297
|
+
if (abortController === null) {
|
298
|
+
reader.cancel();
|
299
|
+
break;
|
334
300
|
}
|
301
|
+
}
|
302
|
+
if (onFinish) {
|
303
|
+
onFinish(prompt, result);
|
304
|
+
}
|
305
|
+
abortController = null;
|
306
|
+
return result;
|
307
|
+
} catch (err) {
|
308
|
+
if (err.name === "AbortError") {
|
335
309
|
abortController = null;
|
336
|
-
return
|
337
|
-
} catch (err) {
|
338
|
-
if (err.name === "AbortError") {
|
339
|
-
abortController = null;
|
340
|
-
return null;
|
341
|
-
}
|
342
|
-
if (onError && error instanceof Error) {
|
343
|
-
onError(error);
|
344
|
-
}
|
345
|
-
setError(err);
|
346
|
-
} finally {
|
347
|
-
setIsLoading(false);
|
310
|
+
return null;
|
348
311
|
}
|
349
|
-
|
312
|
+
if (onError && error instanceof Error) {
|
313
|
+
onError(error);
|
314
|
+
}
|
315
|
+
setError(err);
|
316
|
+
} finally {
|
317
|
+
setIsLoading(false);
|
318
|
+
}
|
350
319
|
}
|
351
|
-
const complete = (prompt, options) =>
|
320
|
+
const complete = async (prompt, options) => {
|
352
321
|
return triggerRequest(prompt, options);
|
353
|
-
}
|
322
|
+
};
|
354
323
|
const stop = () => {
|
355
324
|
if (abortController) {
|
356
325
|
abortController.abort();
|
package/svelte/dist/index.d.ts
CHANGED
@@ -200,7 +200,7 @@ type UseChatHelpers = {
|
|
200
200
|
handleSubmit: (e: any, chatRequestOptions?: ChatRequestOptions) => void;
|
201
201
|
metadata?: Object;
|
202
202
|
/** Whether the API request is in progress */
|
203
|
-
isLoading: Writable<boolean>;
|
203
|
+
isLoading: Writable<boolean | undefined>;
|
204
204
|
};
|
205
205
|
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, onResponse, onFinish, onError, credentials, headers, body }?: UseChatOptions): UseChatHelpers;
|
206
206
|
|
@@ -234,7 +234,7 @@ type UseCompletionHelpers = {
|
|
234
234
|
*/
|
235
235
|
handleSubmit: (e: any) => void;
|
236
236
|
/** Whether the API request is in progress */
|
237
|
-
isLoading: Writable<boolean>;
|
237
|
+
isLoading: Writable<boolean | undefined>;
|
238
238
|
};
|
239
239
|
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers;
|
240
240
|
|