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/vue/dist/index.d.ts
CHANGED
@@ -192,7 +192,7 @@ type UseChatHelpers = {
|
|
192
192
|
/** Form submission handler to automattically reset input and append a user message */
|
193
193
|
handleSubmit: (e: any) => void;
|
194
194
|
/** Whether the API request is in progress */
|
195
|
-
isLoading: Ref<boolean>;
|
195
|
+
isLoading: Ref<boolean | undefined>;
|
196
196
|
};
|
197
197
|
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, credentials, headers, body }?: UseChatOptions): UseChatHelpers;
|
198
198
|
|
@@ -226,7 +226,7 @@ type UseCompletionHelpers = {
|
|
226
226
|
*/
|
227
227
|
handleSubmit: (e: any) => void;
|
228
228
|
/** Whether the API request is in progress */
|
229
|
-
isLoading: Ref<boolean>;
|
229
|
+
isLoading: Ref<boolean | undefined>;
|
230
230
|
};
|
231
231
|
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers;
|
232
232
|
|
package/vue/dist/index.js
CHANGED
@@ -3,22 +3,8 @@ var __create = Object.create;
|
|
3
3
|
var __defProp = Object.defineProperty;
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
7
6
|
var __getProtoOf = Object.getPrototypeOf;
|
8
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
9
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
10
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
11
|
-
var __spreadValues = (a, b) => {
|
12
|
-
for (var prop in b || (b = {}))
|
13
|
-
if (__hasOwnProp.call(b, prop))
|
14
|
-
__defNormalProp(a, prop, b[prop]);
|
15
|
-
if (__getOwnPropSymbols)
|
16
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
17
|
-
if (__propIsEnum.call(b, prop))
|
18
|
-
__defNormalProp(a, prop, b[prop]);
|
19
|
-
}
|
20
|
-
return a;
|
21
|
-
};
|
22
8
|
var __export = (target, all) => {
|
23
9
|
for (var name in all)
|
24
10
|
__defProp(target, name, { get: all[name], enumerable: true });
|
@@ -40,26 +26,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
40
26
|
mod
|
41
27
|
));
|
42
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
43
|
-
var __async = (__this, __arguments, generator) => {
|
44
|
-
return new Promise((resolve, reject) => {
|
45
|
-
var fulfilled = (value) => {
|
46
|
-
try {
|
47
|
-
step(generator.next(value));
|
48
|
-
} catch (e) {
|
49
|
-
reject(e);
|
50
|
-
}
|
51
|
-
};
|
52
|
-
var rejected = (value) => {
|
53
|
-
try {
|
54
|
-
step(generator.throw(value));
|
55
|
-
} catch (e) {
|
56
|
-
reject(e);
|
57
|
-
}
|
58
|
-
};
|
59
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
60
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
61
|
-
});
|
62
|
-
};
|
63
29
|
|
64
30
|
// vue/index.ts
|
65
31
|
var vue_exports = {};
|
@@ -111,6 +77,9 @@ function useChat({
|
|
111
77
|
key,
|
112
78
|
() => store[key] || initialMessages
|
113
79
|
);
|
80
|
+
const { data: isLoading, mutate: mutateLoading } = useSWRV(
|
81
|
+
`${key}-loading`
|
82
|
+
);
|
114
83
|
data.value || (data.value = initialMessages);
|
115
84
|
const mutate = (data2) => {
|
116
85
|
store[key] = data2;
|
@@ -118,102 +87,104 @@ function useChat({
|
|
118
87
|
};
|
119
88
|
const messages = data;
|
120
89
|
const error = (0, import_vue.ref)(void 0);
|
121
|
-
const isLoading = (0, import_vue.ref)(false);
|
122
90
|
let abortController = null;
|
123
|
-
function triggerRequest(messagesSnapshot, options) {
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
91
|
+
async function triggerRequest(messagesSnapshot, options) {
|
92
|
+
try {
|
93
|
+
mutateLoading(() => true);
|
94
|
+
abortController = new AbortController();
|
95
|
+
const previousMessages = messages.value;
|
96
|
+
mutate(messagesSnapshot);
|
97
|
+
const res = await fetch(api, {
|
98
|
+
method: "POST",
|
99
|
+
body: JSON.stringify({
|
100
|
+
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
101
|
+
role,
|
102
|
+
content
|
103
|
+
})),
|
104
|
+
...body,
|
105
|
+
...options == null ? void 0 : options.body
|
106
|
+
}),
|
107
|
+
headers: {
|
108
|
+
...headers,
|
109
|
+
...options == null ? void 0 : options.headers
|
110
|
+
},
|
111
|
+
signal: abortController.signal,
|
112
|
+
credentials
|
113
|
+
}).catch((err) => {
|
114
|
+
mutate(previousMessages);
|
115
|
+
throw err;
|
116
|
+
});
|
117
|
+
if (onResponse) {
|
118
|
+
try {
|
119
|
+
await onResponse(res);
|
120
|
+
} catch (err) {
|
143
121
|
throw err;
|
144
|
-
});
|
145
|
-
if (onResponse) {
|
146
|
-
try {
|
147
|
-
yield onResponse(res);
|
148
|
-
} catch (err) {
|
149
|
-
throw err;
|
150
|
-
}
|
151
|
-
}
|
152
|
-
if (!res.ok) {
|
153
|
-
mutate(previousMessages);
|
154
|
-
throw new Error(
|
155
|
-
(yield res.text()) || "Failed to fetch the chat response."
|
156
|
-
);
|
157
122
|
}
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
content: result,
|
178
|
-
role: "assistant"
|
179
|
-
}
|
180
|
-
]);
|
181
|
-
if (abortController === null) {
|
182
|
-
reader.cancel();
|
183
|
-
break;
|
184
|
-
}
|
123
|
+
}
|
124
|
+
if (!res.ok) {
|
125
|
+
mutate(previousMessages);
|
126
|
+
throw new Error(
|
127
|
+
await res.text() || "Failed to fetch the chat response."
|
128
|
+
);
|
129
|
+
}
|
130
|
+
if (!res.body) {
|
131
|
+
throw new Error("The response body is empty.");
|
132
|
+
}
|
133
|
+
let result = "";
|
134
|
+
const createdAt = /* @__PURE__ */ new Date();
|
135
|
+
const replyId = nanoid();
|
136
|
+
const reader = res.body.getReader();
|
137
|
+
const decoder = createChunkDecoder();
|
138
|
+
while (true) {
|
139
|
+
const { done, value } = await reader.read();
|
140
|
+
if (done) {
|
141
|
+
break;
|
185
142
|
}
|
186
|
-
|
187
|
-
|
143
|
+
result += decoder(value);
|
144
|
+
mutate([
|
145
|
+
...messagesSnapshot,
|
146
|
+
{
|
188
147
|
id: replyId,
|
189
148
|
createdAt,
|
190
149
|
content: result,
|
191
150
|
role: "assistant"
|
192
|
-
}
|
151
|
+
}
|
152
|
+
]);
|
153
|
+
if (abortController === null) {
|
154
|
+
reader.cancel();
|
155
|
+
break;
|
193
156
|
}
|
157
|
+
}
|
158
|
+
if (onFinish) {
|
159
|
+
onFinish({
|
160
|
+
id: replyId,
|
161
|
+
createdAt,
|
162
|
+
content: result,
|
163
|
+
role: "assistant"
|
164
|
+
});
|
165
|
+
}
|
166
|
+
abortController = null;
|
167
|
+
return result;
|
168
|
+
} catch (err) {
|
169
|
+
if (err.name === "AbortError") {
|
194
170
|
abortController = null;
|
195
|
-
return
|
196
|
-
} catch (err) {
|
197
|
-
if (err.name === "AbortError") {
|
198
|
-
abortController = null;
|
199
|
-
return null;
|
200
|
-
}
|
201
|
-
if (onError && err instanceof Error) {
|
202
|
-
onError(err);
|
203
|
-
}
|
204
|
-
error.value = err;
|
205
|
-
} finally {
|
206
|
-
isLoading.value = false;
|
171
|
+
return null;
|
207
172
|
}
|
208
|
-
|
173
|
+
if (onError && err instanceof Error) {
|
174
|
+
onError(err);
|
175
|
+
}
|
176
|
+
error.value = err;
|
177
|
+
} finally {
|
178
|
+
mutateLoading(() => false);
|
179
|
+
}
|
209
180
|
}
|
210
|
-
const append = (message, options) =>
|
181
|
+
const append = async (message, options) => {
|
211
182
|
if (!message.id) {
|
212
183
|
message.id = nanoid();
|
213
184
|
}
|
214
185
|
return triggerRequest(messages.value.concat(message), options);
|
215
|
-
}
|
216
|
-
const reload = (options) =>
|
186
|
+
};
|
187
|
+
const reload = async (options) => {
|
217
188
|
const messagesSnapshot = messages.value;
|
218
189
|
if (messagesSnapshot.length === 0)
|
219
190
|
return null;
|
@@ -222,7 +193,7 @@ function useChat({
|
|
222
193
|
return triggerRequest(messagesSnapshot.slice(0, -1), options);
|
223
194
|
}
|
224
195
|
return triggerRequest(messagesSnapshot, options);
|
225
|
-
}
|
196
|
+
};
|
226
197
|
const stop = () => {
|
227
198
|
if (abortController) {
|
228
199
|
abortController.abort();
|
@@ -281,6 +252,9 @@ function useCompletion({
|
|
281
252
|
key,
|
282
253
|
() => store2[key] || initialCompletion
|
283
254
|
);
|
255
|
+
const { data: isLoading, mutate: mutateLoading } = useSWRV2(
|
256
|
+
`${key}-loading`
|
257
|
+
);
|
284
258
|
data.value || (data.value = initialCompletion);
|
285
259
|
const mutate = (data2) => {
|
286
260
|
store2[key] = data2;
|
@@ -288,77 +262,79 @@ function useCompletion({
|
|
288
262
|
};
|
289
263
|
const completion = data;
|
290
264
|
const error = (0, import_vue2.ref)(void 0);
|
291
|
-
const isLoading = (0, import_vue2.ref)(false);
|
292
265
|
let abortController = null;
|
293
|
-
function triggerRequest(prompt, options) {
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
266
|
+
async function triggerRequest(prompt, options) {
|
267
|
+
try {
|
268
|
+
mutateLoading(() => true);
|
269
|
+
abortController = new AbortController();
|
270
|
+
mutate("");
|
271
|
+
const res = await fetch(api, {
|
272
|
+
method: "POST",
|
273
|
+
body: JSON.stringify({
|
274
|
+
prompt,
|
275
|
+
...body,
|
276
|
+
...options == null ? void 0 : options.body
|
277
|
+
}),
|
278
|
+
headers: {
|
279
|
+
...headers,
|
280
|
+
...options == null ? void 0 : options.headers
|
281
|
+
},
|
282
|
+
signal: abortController.signal,
|
283
|
+
credentials
|
284
|
+
}).catch((err) => {
|
285
|
+
throw err;
|
286
|
+
});
|
287
|
+
if (onResponse) {
|
288
|
+
try {
|
289
|
+
await onResponse(res);
|
290
|
+
} catch (err) {
|
308
291
|
throw err;
|
309
|
-
});
|
310
|
-
if (onResponse) {
|
311
|
-
try {
|
312
|
-
yield onResponse(res);
|
313
|
-
} catch (err) {
|
314
|
-
throw err;
|
315
|
-
}
|
316
|
-
}
|
317
|
-
if (!res.ok) {
|
318
|
-
throw new Error(
|
319
|
-
(yield res.text()) || "Failed to fetch the chat response."
|
320
|
-
);
|
321
|
-
}
|
322
|
-
if (!res.body) {
|
323
|
-
throw new Error("The response body is empty.");
|
324
292
|
}
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
293
|
+
}
|
294
|
+
if (!res.ok) {
|
295
|
+
throw new Error(
|
296
|
+
await res.text() || "Failed to fetch the chat response."
|
297
|
+
);
|
298
|
+
}
|
299
|
+
if (!res.body) {
|
300
|
+
throw new Error("The response body is empty.");
|
301
|
+
}
|
302
|
+
let result = "";
|
303
|
+
const reader = res.body.getReader();
|
304
|
+
const decoder = createChunkDecoder();
|
305
|
+
while (true) {
|
306
|
+
const { done, value } = await reader.read();
|
307
|
+
if (done) {
|
308
|
+
break;
|
339
309
|
}
|
340
|
-
|
341
|
-
|
310
|
+
result += decoder(value);
|
311
|
+
mutate(result);
|
312
|
+
if (abortController === null) {
|
313
|
+
reader.cancel();
|
314
|
+
break;
|
342
315
|
}
|
316
|
+
}
|
317
|
+
if (onFinish) {
|
318
|
+
onFinish(prompt, result);
|
319
|
+
}
|
320
|
+
abortController = null;
|
321
|
+
return result;
|
322
|
+
} catch (err) {
|
323
|
+
if (err.name === "AbortError") {
|
343
324
|
abortController = null;
|
344
|
-
return
|
345
|
-
} catch (err) {
|
346
|
-
if (err.name === "AbortError") {
|
347
|
-
abortController = null;
|
348
|
-
return null;
|
349
|
-
}
|
350
|
-
if (onError && error instanceof Error) {
|
351
|
-
onError(error);
|
352
|
-
}
|
353
|
-
error.value = err;
|
354
|
-
} finally {
|
355
|
-
isLoading.value = false;
|
325
|
+
return null;
|
356
326
|
}
|
357
|
-
|
327
|
+
if (onError && error instanceof Error) {
|
328
|
+
onError(error);
|
329
|
+
}
|
330
|
+
error.value = err;
|
331
|
+
} finally {
|
332
|
+
mutateLoading(() => false);
|
333
|
+
}
|
358
334
|
}
|
359
|
-
const complete = (prompt, options) =>
|
335
|
+
const complete = async (prompt, options) => {
|
360
336
|
return triggerRequest(prompt, options);
|
361
|
-
}
|
337
|
+
};
|
362
338
|
const stop = () => {
|
363
339
|
if (abortController) {
|
364
340
|
abortController.abort();
|