ai 2.1.6 → 2.1.8
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 +7 -3
- package/package.json +1 -1
- package/react/dist/index.d.ts +11 -5
- package/react/dist/index.js +48 -28
- package/react/dist/index.mjs +48 -28
- package/svelte/dist/index.d.ts +9 -5
- package/svelte/dist/index.js +15 -15
- package/svelte/dist/index.mjs +15 -15
- package/vue/dist/index.d.ts +9 -5
- package/vue/dist/index.js +15 -15
- package/vue/dist/index.mjs +15 -15
package/dist/index.d.ts
CHANGED
@@ -115,6 +115,10 @@ declare type CreateMessage = {
|
|
115
115
|
content: string;
|
116
116
|
role: 'system' | 'user' | 'assistant';
|
117
117
|
};
|
118
|
+
declare type RequestOptions = {
|
119
|
+
headers?: Record<string, string> | Headers;
|
120
|
+
body?: object;
|
121
|
+
};
|
118
122
|
declare type UseChatOptions = {
|
119
123
|
/**
|
120
124
|
* The API endpoint that accepts a `{ messages: Message[] }` object and returns
|
@@ -138,7 +142,7 @@ declare type UseChatOptions = {
|
|
138
142
|
/**
|
139
143
|
* Callback function to be called when the API response is received.
|
140
144
|
*/
|
141
|
-
onResponse?: (response: Response) => void
|
145
|
+
onResponse?: (response: Response) => void | Promise<void>;
|
142
146
|
/**
|
143
147
|
* Callback function to be called when the chat is finished streaming.
|
144
148
|
*/
|
@@ -194,7 +198,7 @@ declare type UseCompletionOptions = {
|
|
194
198
|
/**
|
195
199
|
* Callback function to be called when the API response is received.
|
196
200
|
*/
|
197
|
-
onResponse?: (response: Response) => void
|
201
|
+
onResponse?: (response: Response) => void | Promise<void>;
|
198
202
|
/**
|
199
203
|
* Callback function to be called when the completion is finished streaming.
|
200
204
|
*/
|
@@ -222,4 +226,4 @@ declare type UseCompletionOptions = {
|
|
222
226
|
body?: object;
|
223
227
|
};
|
224
228
|
|
225
|
-
export { AIStream, AIStreamCallbacks, AIStreamParser, AnthropicStream, CreateMessage, HuggingFaceStream, LangChainStream, Message, OpenAIStream, StreamingTextResponse, UseChatOptions, UseCompletionOptions, createCallbacksTransformer, createEventStreamTransformer, streamToResponse, trimStartOfStreamHelper };
|
229
|
+
export { AIStream, AIStreamCallbacks, AIStreamParser, AnthropicStream, CreateMessage, HuggingFaceStream, LangChainStream, Message, OpenAIStream, RequestOptions, StreamingTextResponse, UseChatOptions, UseCompletionOptions, createCallbacksTransformer, createEventStreamTransformer, streamToResponse, trimStartOfStreamHelper };
|
package/package.json
CHANGED
package/react/dist/index.d.ts
CHANGED
@@ -13,6 +13,10 @@ declare type CreateMessage = {
|
|
13
13
|
content: string;
|
14
14
|
role: 'system' | 'user' | 'assistant';
|
15
15
|
};
|
16
|
+
declare type RequestOptions = {
|
17
|
+
headers?: Record<string, string> | Headers;
|
18
|
+
body?: object;
|
19
|
+
};
|
16
20
|
declare type UseChatOptions = {
|
17
21
|
/**
|
18
22
|
* The API endpoint that accepts a `{ messages: Message[] }` object and returns
|
@@ -36,7 +40,7 @@ declare type UseChatOptions = {
|
|
36
40
|
/**
|
37
41
|
* Callback function to be called when the API response is received.
|
38
42
|
*/
|
39
|
-
onResponse?: (response: Response) => void
|
43
|
+
onResponse?: (response: Response) => void | Promise<void>;
|
40
44
|
/**
|
41
45
|
* Callback function to be called when the chat is finished streaming.
|
42
46
|
*/
|
@@ -92,7 +96,7 @@ declare type UseCompletionOptions = {
|
|
92
96
|
/**
|
93
97
|
* Callback function to be called when the API response is received.
|
94
98
|
*/
|
95
|
-
onResponse?: (response: Response) => void
|
99
|
+
onResponse?: (response: Response) => void | Promise<void>;
|
96
100
|
/**
|
97
101
|
* Callback function to be called when the completion is finished streaming.
|
98
102
|
*/
|
@@ -128,14 +132,16 @@ declare type UseChatHelpers = {
|
|
128
132
|
/**
|
129
133
|
* Append a user message to the chat list. This triggers the API call to fetch
|
130
134
|
* the assistant's response.
|
135
|
+
* @param message The message to append
|
136
|
+
* @param options Additional options to pass to the API call
|
131
137
|
*/
|
132
|
-
append: (message: Message | CreateMessage) => Promise<string | null | undefined>;
|
138
|
+
append: (message: Message | CreateMessage, options?: RequestOptions) => Promise<string | null | undefined>;
|
133
139
|
/**
|
134
140
|
* Reload the last AI chat response for the given chat history. If the last
|
135
141
|
* message isn't from the assistant, it will request the API to generate a
|
136
142
|
* new response.
|
137
143
|
*/
|
138
|
-
reload: () => Promise<string | null | undefined>;
|
144
|
+
reload: (options?: RequestOptions) => Promise<string | null | undefined>;
|
139
145
|
/**
|
140
146
|
* Abort the current request immediately, keep the generated tokens if any.
|
141
147
|
*/
|
@@ -165,7 +171,7 @@ declare type UseCompletionHelpers = {
|
|
165
171
|
/**
|
166
172
|
* Send a new prompt to the API endpoint and update the completion state.
|
167
173
|
*/
|
168
|
-
complete: (prompt: string) => Promise<string | null | undefined>;
|
174
|
+
complete: (prompt: string, options?: RequestOptions) => Promise<string | null | undefined>;
|
169
175
|
/** The error object of the API request */
|
170
176
|
error: undefined | Error;
|
171
177
|
/**
|
package/react/dist/index.js
CHANGED
@@ -126,21 +126,22 @@ function useChat({
|
|
126
126
|
}, [headers, body]);
|
127
127
|
const { error, trigger, isMutating } = (0, import_mutation.default)(
|
128
128
|
[api, chatId],
|
129
|
-
(_0, _1) => __async(this, [_0, _1], function* (_, { arg
|
129
|
+
(_0, _1) => __async(this, [_0, _1], function* (_, { arg }) {
|
130
130
|
try {
|
131
|
+
const { messages: messagesSnapshot, options } = arg;
|
131
132
|
const abortController = new AbortController();
|
132
133
|
abortControllerRef.current = abortController;
|
133
134
|
const previousMessages = messagesRef.current;
|
134
135
|
mutate(messagesSnapshot, false);
|
135
136
|
const res = yield fetch(api, {
|
136
137
|
method: "POST",
|
137
|
-
body: JSON.stringify(__spreadValues({
|
138
|
+
body: JSON.stringify(__spreadValues(__spreadValues({
|
138
139
|
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
139
140
|
role,
|
140
141
|
content
|
141
142
|
}))
|
142
|
-
}, extraMetadataRef.current.body)),
|
143
|
-
headers: extraMetadataRef.current.headers
|
143
|
+
}, extraMetadataRef.current.body), options == null ? void 0 : options.body)),
|
144
|
+
headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), options == null ? void 0 : options.headers),
|
144
145
|
signal: abortController.signal
|
145
146
|
}).catch((err) => {
|
146
147
|
mutate(previousMessages, false);
|
@@ -217,23 +218,35 @@ function useChat({
|
|
217
218
|
}
|
218
219
|
);
|
219
220
|
const append = (0, import_react.useCallback)(
|
220
|
-
(message) => __async(this, null, function* () {
|
221
|
+
(message, options) => __async(this, null, function* () {
|
221
222
|
if (!message.id) {
|
222
223
|
message.id = nanoid();
|
223
224
|
}
|
224
|
-
return trigger(
|
225
|
+
return trigger({
|
226
|
+
messages: messagesRef.current.concat(message),
|
227
|
+
options
|
228
|
+
});
|
229
|
+
}),
|
230
|
+
[trigger]
|
231
|
+
);
|
232
|
+
const reload = (0, import_react.useCallback)(
|
233
|
+
(options) => __async(this, null, function* () {
|
234
|
+
if (messagesRef.current.length === 0)
|
235
|
+
return null;
|
236
|
+
const lastMessage = messagesRef.current[messagesRef.current.length - 1];
|
237
|
+
if (lastMessage.role === "assistant") {
|
238
|
+
return trigger({
|
239
|
+
messages: messagesRef.current.slice(0, -1),
|
240
|
+
options
|
241
|
+
});
|
242
|
+
}
|
243
|
+
return trigger({
|
244
|
+
messages: messagesRef.current,
|
245
|
+
options
|
246
|
+
});
|
225
247
|
}),
|
226
248
|
[trigger]
|
227
249
|
);
|
228
|
-
const reload = (0, import_react.useCallback)(() => __async(this, null, function* () {
|
229
|
-
if (messagesRef.current.length === 0)
|
230
|
-
return null;
|
231
|
-
const lastMessage = messagesRef.current[messagesRef.current.length - 1];
|
232
|
-
if (lastMessage.role === "assistant") {
|
233
|
-
return trigger(messagesRef.current.slice(0, -1));
|
234
|
-
}
|
235
|
-
return trigger(messagesRef.current);
|
236
|
-
}), [trigger]);
|
237
250
|
const stop = (0, import_react.useCallback)(() => {
|
238
251
|
if (abortControllerRef.current) {
|
239
252
|
abortControllerRef.current.abort();
|
@@ -249,7 +262,10 @@ function useChat({
|
|
249
262
|
);
|
250
263
|
const [input, setInput] = (0, import_react.useState)(initialInput);
|
251
264
|
const handleSubmit = (0, import_react.useCallback)(
|
252
|
-
(e) => {
|
265
|
+
(e, metadata) => {
|
266
|
+
if (metadata) {
|
267
|
+
extraMetadataRef.current = __spreadValues(__spreadValues({}, extraMetadataRef.current), metadata);
|
268
|
+
}
|
253
269
|
e.preventDefault();
|
254
270
|
if (!input)
|
255
271
|
return;
|
@@ -314,17 +330,18 @@ function useCompletion({
|
|
314
330
|
}, [headers, body]);
|
315
331
|
const { error, trigger, isMutating } = (0, import_mutation2.default)(
|
316
332
|
[api, completionId],
|
317
|
-
(_0, _1) => __async(this, [_0, _1], function* (_, { arg
|
333
|
+
(_0, _1) => __async(this, [_0, _1], function* (_, { arg }) {
|
318
334
|
try {
|
335
|
+
const { prompt, options } = arg;
|
319
336
|
const abortController2 = new AbortController();
|
320
337
|
setAbortController(abortController2);
|
321
338
|
mutate("", false);
|
322
339
|
const res = yield fetch(api, {
|
323
340
|
method: "POST",
|
324
|
-
body: JSON.stringify(__spreadValues({
|
341
|
+
body: JSON.stringify(__spreadValues(__spreadValues({
|
325
342
|
prompt
|
326
|
-
}, extraMetadataRef.current.body)),
|
327
|
-
headers: extraMetadataRef.current.headers
|
343
|
+
}, extraMetadataRef.current.body), options == null ? void 0 : options.body)),
|
344
|
+
headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), options == null ? void 0 : options.headers),
|
328
345
|
signal: abortController2.signal
|
329
346
|
}).catch((err) => {
|
330
347
|
throw err;
|
@@ -392,25 +409,28 @@ function useCompletion({
|
|
392
409
|
},
|
393
410
|
[mutate]
|
394
411
|
);
|
412
|
+
const complete = (0, import_react2.useCallback)(
|
413
|
+
(prompt, options) => __async(this, null, function* () {
|
414
|
+
return trigger({
|
415
|
+
prompt,
|
416
|
+
options
|
417
|
+
});
|
418
|
+
}),
|
419
|
+
[trigger]
|
420
|
+
);
|
395
421
|
const [input, setInput] = (0, import_react2.useState)(initialInput);
|
396
422
|
const handleSubmit = (0, import_react2.useCallback)(
|
397
423
|
(e) => {
|
398
424
|
e.preventDefault();
|
399
425
|
if (!input)
|
400
426
|
return;
|
401
|
-
return
|
427
|
+
return complete(input);
|
402
428
|
},
|
403
|
-
[input,
|
429
|
+
[input, complete]
|
404
430
|
);
|
405
431
|
const handleInputChange = (e) => {
|
406
432
|
setInput(e.target.value);
|
407
433
|
};
|
408
|
-
const complete = (0, import_react2.useCallback)(
|
409
|
-
(prompt) => __async(this, null, function* () {
|
410
|
-
return trigger(prompt);
|
411
|
-
}),
|
412
|
-
[trigger]
|
413
|
-
);
|
414
434
|
return {
|
415
435
|
completion,
|
416
436
|
complete,
|
package/react/dist/index.mjs
CHANGED
@@ -92,21 +92,22 @@ function useChat({
|
|
92
92
|
}, [headers, body]);
|
93
93
|
const { error, trigger, isMutating } = useSWRMutation(
|
94
94
|
[api, chatId],
|
95
|
-
(_0, _1) => __async(this, [_0, _1], function* (_, { arg
|
95
|
+
(_0, _1) => __async(this, [_0, _1], function* (_, { arg }) {
|
96
96
|
try {
|
97
|
+
const { messages: messagesSnapshot, options } = arg;
|
97
98
|
const abortController = new AbortController();
|
98
99
|
abortControllerRef.current = abortController;
|
99
100
|
const previousMessages = messagesRef.current;
|
100
101
|
mutate(messagesSnapshot, false);
|
101
102
|
const res = yield fetch(api, {
|
102
103
|
method: "POST",
|
103
|
-
body: JSON.stringify(__spreadValues({
|
104
|
+
body: JSON.stringify(__spreadValues(__spreadValues({
|
104
105
|
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
105
106
|
role,
|
106
107
|
content
|
107
108
|
}))
|
108
|
-
}, extraMetadataRef.current.body)),
|
109
|
-
headers: extraMetadataRef.current.headers
|
109
|
+
}, extraMetadataRef.current.body), options == null ? void 0 : options.body)),
|
110
|
+
headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), options == null ? void 0 : options.headers),
|
110
111
|
signal: abortController.signal
|
111
112
|
}).catch((err) => {
|
112
113
|
mutate(previousMessages, false);
|
@@ -183,23 +184,35 @@ function useChat({
|
|
183
184
|
}
|
184
185
|
);
|
185
186
|
const append = useCallback(
|
186
|
-
(message) => __async(this, null, function* () {
|
187
|
+
(message, options) => __async(this, null, function* () {
|
187
188
|
if (!message.id) {
|
188
189
|
message.id = nanoid();
|
189
190
|
}
|
190
|
-
return trigger(
|
191
|
+
return trigger({
|
192
|
+
messages: messagesRef.current.concat(message),
|
193
|
+
options
|
194
|
+
});
|
195
|
+
}),
|
196
|
+
[trigger]
|
197
|
+
);
|
198
|
+
const reload = useCallback(
|
199
|
+
(options) => __async(this, null, function* () {
|
200
|
+
if (messagesRef.current.length === 0)
|
201
|
+
return null;
|
202
|
+
const lastMessage = messagesRef.current[messagesRef.current.length - 1];
|
203
|
+
if (lastMessage.role === "assistant") {
|
204
|
+
return trigger({
|
205
|
+
messages: messagesRef.current.slice(0, -1),
|
206
|
+
options
|
207
|
+
});
|
208
|
+
}
|
209
|
+
return trigger({
|
210
|
+
messages: messagesRef.current,
|
211
|
+
options
|
212
|
+
});
|
191
213
|
}),
|
192
214
|
[trigger]
|
193
215
|
);
|
194
|
-
const reload = useCallback(() => __async(this, null, function* () {
|
195
|
-
if (messagesRef.current.length === 0)
|
196
|
-
return null;
|
197
|
-
const lastMessage = messagesRef.current[messagesRef.current.length - 1];
|
198
|
-
if (lastMessage.role === "assistant") {
|
199
|
-
return trigger(messagesRef.current.slice(0, -1));
|
200
|
-
}
|
201
|
-
return trigger(messagesRef.current);
|
202
|
-
}), [trigger]);
|
203
216
|
const stop = useCallback(() => {
|
204
217
|
if (abortControllerRef.current) {
|
205
218
|
abortControllerRef.current.abort();
|
@@ -215,7 +228,10 @@ function useChat({
|
|
215
228
|
);
|
216
229
|
const [input, setInput] = useState(initialInput);
|
217
230
|
const handleSubmit = useCallback(
|
218
|
-
(e) => {
|
231
|
+
(e, metadata) => {
|
232
|
+
if (metadata) {
|
233
|
+
extraMetadataRef.current = __spreadValues(__spreadValues({}, extraMetadataRef.current), metadata);
|
234
|
+
}
|
219
235
|
e.preventDefault();
|
220
236
|
if (!input)
|
221
237
|
return;
|
@@ -280,17 +296,18 @@ function useCompletion({
|
|
280
296
|
}, [headers, body]);
|
281
297
|
const { error, trigger, isMutating } = useSWRMutation2(
|
282
298
|
[api, completionId],
|
283
|
-
(_0, _1) => __async(this, [_0, _1], function* (_, { arg
|
299
|
+
(_0, _1) => __async(this, [_0, _1], function* (_, { arg }) {
|
284
300
|
try {
|
301
|
+
const { prompt, options } = arg;
|
285
302
|
const abortController2 = new AbortController();
|
286
303
|
setAbortController(abortController2);
|
287
304
|
mutate("", false);
|
288
305
|
const res = yield fetch(api, {
|
289
306
|
method: "POST",
|
290
|
-
body: JSON.stringify(__spreadValues({
|
307
|
+
body: JSON.stringify(__spreadValues(__spreadValues({
|
291
308
|
prompt
|
292
|
-
}, extraMetadataRef.current.body)),
|
293
|
-
headers: extraMetadataRef.current.headers
|
309
|
+
}, extraMetadataRef.current.body), options == null ? void 0 : options.body)),
|
310
|
+
headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), options == null ? void 0 : options.headers),
|
294
311
|
signal: abortController2.signal
|
295
312
|
}).catch((err) => {
|
296
313
|
throw err;
|
@@ -358,25 +375,28 @@ function useCompletion({
|
|
358
375
|
},
|
359
376
|
[mutate]
|
360
377
|
);
|
378
|
+
const complete = useCallback2(
|
379
|
+
(prompt, options) => __async(this, null, function* () {
|
380
|
+
return trigger({
|
381
|
+
prompt,
|
382
|
+
options
|
383
|
+
});
|
384
|
+
}),
|
385
|
+
[trigger]
|
386
|
+
);
|
361
387
|
const [input, setInput] = useState2(initialInput);
|
362
388
|
const handleSubmit = useCallback2(
|
363
389
|
(e) => {
|
364
390
|
e.preventDefault();
|
365
391
|
if (!input)
|
366
392
|
return;
|
367
|
-
return
|
393
|
+
return complete(input);
|
368
394
|
},
|
369
|
-
[input,
|
395
|
+
[input, complete]
|
370
396
|
);
|
371
397
|
const handleInputChange = (e) => {
|
372
398
|
setInput(e.target.value);
|
373
399
|
};
|
374
|
-
const complete = useCallback2(
|
375
|
-
(prompt) => __async(this, null, function* () {
|
376
|
-
return trigger(prompt);
|
377
|
-
}),
|
378
|
-
[trigger]
|
379
|
-
);
|
380
400
|
return {
|
381
401
|
completion,
|
382
402
|
complete,
|
package/svelte/dist/index.d.ts
CHANGED
@@ -15,6 +15,10 @@ declare type CreateMessage = {
|
|
15
15
|
content: string;
|
16
16
|
role: 'system' | 'user' | 'assistant';
|
17
17
|
};
|
18
|
+
declare type RequestOptions = {
|
19
|
+
headers?: Record<string, string> | Headers;
|
20
|
+
body?: object;
|
21
|
+
};
|
18
22
|
declare type UseChatOptions = {
|
19
23
|
/**
|
20
24
|
* The API endpoint that accepts a `{ messages: Message[] }` object and returns
|
@@ -38,7 +42,7 @@ declare type UseChatOptions = {
|
|
38
42
|
/**
|
39
43
|
* Callback function to be called when the API response is received.
|
40
44
|
*/
|
41
|
-
onResponse?: (response: Response) => void
|
45
|
+
onResponse?: (response: Response) => void | Promise<void>;
|
42
46
|
/**
|
43
47
|
* Callback function to be called when the chat is finished streaming.
|
44
48
|
*/
|
@@ -94,7 +98,7 @@ declare type UseCompletionOptions = {
|
|
94
98
|
/**
|
95
99
|
* Callback function to be called when the API response is received.
|
96
100
|
*/
|
97
|
-
onResponse?: (response: Response) => void
|
101
|
+
onResponse?: (response: Response) => void | Promise<void>;
|
98
102
|
/**
|
99
103
|
* Callback function to be called when the completion is finished streaming.
|
100
104
|
*/
|
@@ -131,13 +135,13 @@ declare type UseChatHelpers = {
|
|
131
135
|
* Append a user message to the chat list. This triggers the API call to fetch
|
132
136
|
* the assistant's response.
|
133
137
|
*/
|
134
|
-
append: (message: Message | CreateMessage) => Promise<string | null | undefined>;
|
138
|
+
append: (message: Message | CreateMessage, options?: RequestOptions) => Promise<string | null | undefined>;
|
135
139
|
/**
|
136
140
|
* Reload the last AI chat response for the given chat history. If the last
|
137
141
|
* message isn't from the assistant, it will request the API to generate a
|
138
142
|
* new response.
|
139
143
|
*/
|
140
|
-
reload: () => Promise<string | null | undefined>;
|
144
|
+
reload: (options?: RequestOptions) => Promise<string | null | undefined>;
|
141
145
|
/**
|
142
146
|
* Abort the current request immediately, keep the generated tokens if any.
|
143
147
|
*/
|
@@ -165,7 +169,7 @@ declare type UseCompletionHelpers = {
|
|
165
169
|
/**
|
166
170
|
* Send a new prompt to the API endpoint and update the completion state.
|
167
171
|
*/
|
168
|
-
complete: (prompt: string) => Promise<string | null | undefined>;
|
172
|
+
complete: (prompt: string, options?: RequestOptions) => Promise<string | null | undefined>;
|
169
173
|
/**
|
170
174
|
* Abort the current API request but keep the generated tokens.
|
171
175
|
*/
|
package/svelte/dist/index.js
CHANGED
@@ -545,7 +545,7 @@ function useChat({
|
|
545
545
|
const error = (0, import_store.writable)(void 0);
|
546
546
|
const isLoading = (0, import_store.writable)(false);
|
547
547
|
let abortController = null;
|
548
|
-
function triggerRequest(messagesSnapshot) {
|
548
|
+
function triggerRequest(messagesSnapshot, options) {
|
549
549
|
return __async(this, null, function* () {
|
550
550
|
try {
|
551
551
|
isLoading.set(true);
|
@@ -554,13 +554,13 @@ function useChat({
|
|
554
554
|
mutate(messagesSnapshot);
|
555
555
|
const res = yield fetch(api, {
|
556
556
|
method: "POST",
|
557
|
-
body: JSON.stringify(__spreadValues({
|
557
|
+
body: JSON.stringify(__spreadValues(__spreadValues({
|
558
558
|
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
559
559
|
role,
|
560
560
|
content
|
561
561
|
}))
|
562
|
-
}, body)),
|
563
|
-
headers: headers
|
562
|
+
}, body), options == null ? void 0 : options.body)),
|
563
|
+
headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
|
564
564
|
signal: abortController.signal
|
565
565
|
}).catch((err) => {
|
566
566
|
mutate(previousMessages);
|
@@ -631,21 +631,21 @@ function useChat({
|
|
631
631
|
}
|
632
632
|
});
|
633
633
|
}
|
634
|
-
const append = (message) => __async(this, null, function* () {
|
634
|
+
const append = (message, options) => __async(this, null, function* () {
|
635
635
|
if (!message.id) {
|
636
636
|
message.id = nanoid();
|
637
637
|
}
|
638
|
-
return triggerRequest((0, import_store.get)(messages).concat(message));
|
638
|
+
return triggerRequest((0, import_store.get)(messages).concat(message), options);
|
639
639
|
});
|
640
|
-
const reload = () => __async(this, null, function* () {
|
640
|
+
const reload = (options) => __async(this, null, function* () {
|
641
641
|
const messagesSnapshot = (0, import_store.get)(messages);
|
642
642
|
if (messagesSnapshot.length === 0)
|
643
643
|
return null;
|
644
644
|
const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];
|
645
645
|
if (lastMessage.role === "assistant") {
|
646
|
-
return triggerRequest(messagesSnapshot.slice(0, -1));
|
646
|
+
return triggerRequest(messagesSnapshot.slice(0, -1), options);
|
647
647
|
}
|
648
|
-
return triggerRequest(messagesSnapshot);
|
648
|
+
return triggerRequest(messagesSnapshot, options);
|
649
649
|
});
|
650
650
|
const stop = () => {
|
651
651
|
if (abortController) {
|
@@ -712,7 +712,7 @@ function useCompletion({
|
|
712
712
|
const error = (0, import_store2.writable)(void 0);
|
713
713
|
const isLoading = (0, import_store2.writable)(false);
|
714
714
|
let abortController = null;
|
715
|
-
function triggerRequest(prompt) {
|
715
|
+
function triggerRequest(prompt, options) {
|
716
716
|
return __async(this, null, function* () {
|
717
717
|
try {
|
718
718
|
isLoading.set(true);
|
@@ -720,10 +720,10 @@ function useCompletion({
|
|
720
720
|
mutate("");
|
721
721
|
const res = yield fetch(api, {
|
722
722
|
method: "POST",
|
723
|
-
body: JSON.stringify(__spreadValues({
|
723
|
+
body: JSON.stringify(__spreadValues(__spreadValues({
|
724
724
|
prompt
|
725
|
-
}, body)),
|
726
|
-
headers: headers
|
725
|
+
}, body), options == null ? void 0 : options.body)),
|
726
|
+
headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
|
727
727
|
signal: abortController.signal
|
728
728
|
}).catch((err) => {
|
729
729
|
throw err;
|
@@ -777,8 +777,8 @@ function useCompletion({
|
|
777
777
|
}
|
778
778
|
});
|
779
779
|
}
|
780
|
-
const complete = (prompt) => __async(this, null, function* () {
|
781
|
-
return triggerRequest(prompt);
|
780
|
+
const complete = (prompt, options) => __async(this, null, function* () {
|
781
|
+
return triggerRequest(prompt, options);
|
782
782
|
});
|
783
783
|
const stop = () => {
|
784
784
|
if (abortController) {
|
package/svelte/dist/index.mjs
CHANGED
@@ -521,7 +521,7 @@ function useChat({
|
|
521
521
|
const error = writable(void 0);
|
522
522
|
const isLoading = writable(false);
|
523
523
|
let abortController = null;
|
524
|
-
function triggerRequest(messagesSnapshot) {
|
524
|
+
function triggerRequest(messagesSnapshot, options) {
|
525
525
|
return __async(this, null, function* () {
|
526
526
|
try {
|
527
527
|
isLoading.set(true);
|
@@ -530,13 +530,13 @@ function useChat({
|
|
530
530
|
mutate(messagesSnapshot);
|
531
531
|
const res = yield fetch(api, {
|
532
532
|
method: "POST",
|
533
|
-
body: JSON.stringify(__spreadValues({
|
533
|
+
body: JSON.stringify(__spreadValues(__spreadValues({
|
534
534
|
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
535
535
|
role,
|
536
536
|
content
|
537
537
|
}))
|
538
|
-
}, body)),
|
539
|
-
headers: headers
|
538
|
+
}, body), options == null ? void 0 : options.body)),
|
539
|
+
headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
|
540
540
|
signal: abortController.signal
|
541
541
|
}).catch((err) => {
|
542
542
|
mutate(previousMessages);
|
@@ -607,21 +607,21 @@ function useChat({
|
|
607
607
|
}
|
608
608
|
});
|
609
609
|
}
|
610
|
-
const append = (message) => __async(this, null, function* () {
|
610
|
+
const append = (message, options) => __async(this, null, function* () {
|
611
611
|
if (!message.id) {
|
612
612
|
message.id = nanoid();
|
613
613
|
}
|
614
|
-
return triggerRequest(get(messages).concat(message));
|
614
|
+
return triggerRequest(get(messages).concat(message), options);
|
615
615
|
});
|
616
|
-
const reload = () => __async(this, null, function* () {
|
616
|
+
const reload = (options) => __async(this, null, function* () {
|
617
617
|
const messagesSnapshot = get(messages);
|
618
618
|
if (messagesSnapshot.length === 0)
|
619
619
|
return null;
|
620
620
|
const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];
|
621
621
|
if (lastMessage.role === "assistant") {
|
622
|
-
return triggerRequest(messagesSnapshot.slice(0, -1));
|
622
|
+
return triggerRequest(messagesSnapshot.slice(0, -1), options);
|
623
623
|
}
|
624
|
-
return triggerRequest(messagesSnapshot);
|
624
|
+
return triggerRequest(messagesSnapshot, options);
|
625
625
|
});
|
626
626
|
const stop = () => {
|
627
627
|
if (abortController) {
|
@@ -688,7 +688,7 @@ function useCompletion({
|
|
688
688
|
const error = writable2(void 0);
|
689
689
|
const isLoading = writable2(false);
|
690
690
|
let abortController = null;
|
691
|
-
function triggerRequest(prompt) {
|
691
|
+
function triggerRequest(prompt, options) {
|
692
692
|
return __async(this, null, function* () {
|
693
693
|
try {
|
694
694
|
isLoading.set(true);
|
@@ -696,10 +696,10 @@ function useCompletion({
|
|
696
696
|
mutate("");
|
697
697
|
const res = yield fetch(api, {
|
698
698
|
method: "POST",
|
699
|
-
body: JSON.stringify(__spreadValues({
|
699
|
+
body: JSON.stringify(__spreadValues(__spreadValues({
|
700
700
|
prompt
|
701
|
-
}, body)),
|
702
|
-
headers: headers
|
701
|
+
}, body), options == null ? void 0 : options.body)),
|
702
|
+
headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
|
703
703
|
signal: abortController.signal
|
704
704
|
}).catch((err) => {
|
705
705
|
throw err;
|
@@ -753,8 +753,8 @@ function useCompletion({
|
|
753
753
|
}
|
754
754
|
});
|
755
755
|
}
|
756
|
-
const complete = (prompt) => __async(this, null, function* () {
|
757
|
-
return triggerRequest(prompt);
|
756
|
+
const complete = (prompt, options) => __async(this, null, function* () {
|
757
|
+
return triggerRequest(prompt, options);
|
758
758
|
});
|
759
759
|
const stop = () => {
|
760
760
|
if (abortController) {
|
package/vue/dist/index.d.ts
CHANGED
@@ -15,6 +15,10 @@ declare type CreateMessage = {
|
|
15
15
|
content: string;
|
16
16
|
role: 'system' | 'user' | 'assistant';
|
17
17
|
};
|
18
|
+
declare type RequestOptions = {
|
19
|
+
headers?: Record<string, string> | Headers;
|
20
|
+
body?: object;
|
21
|
+
};
|
18
22
|
declare type UseChatOptions = {
|
19
23
|
/**
|
20
24
|
* The API endpoint that accepts a `{ messages: Message[] }` object and returns
|
@@ -38,7 +42,7 @@ declare type UseChatOptions = {
|
|
38
42
|
/**
|
39
43
|
* Callback function to be called when the API response is received.
|
40
44
|
*/
|
41
|
-
onResponse?: (response: Response) => void
|
45
|
+
onResponse?: (response: Response) => void | Promise<void>;
|
42
46
|
/**
|
43
47
|
* Callback function to be called when the chat is finished streaming.
|
44
48
|
*/
|
@@ -94,7 +98,7 @@ declare type UseCompletionOptions = {
|
|
94
98
|
/**
|
95
99
|
* Callback function to be called when the API response is received.
|
96
100
|
*/
|
97
|
-
onResponse?: (response: Response) => void
|
101
|
+
onResponse?: (response: Response) => void | Promise<void>;
|
98
102
|
/**
|
99
103
|
* Callback function to be called when the completion is finished streaming.
|
100
104
|
*/
|
@@ -131,13 +135,13 @@ declare type UseChatHelpers = {
|
|
131
135
|
* Append a user message to the chat list. This triggers the API call to fetch
|
132
136
|
* the assistant's response.
|
133
137
|
*/
|
134
|
-
append: (message: Message | CreateMessage) => Promise<string | null | undefined>;
|
138
|
+
append: (message: Message | CreateMessage, options?: RequestOptions) => Promise<string | null | undefined>;
|
135
139
|
/**
|
136
140
|
* Reload the last AI chat response for the given chat history. If the last
|
137
141
|
* message isn't from the assistant, it will request the API to generate a
|
138
142
|
* new response.
|
139
143
|
*/
|
140
|
-
reload: () => Promise<string | null | undefined>;
|
144
|
+
reload: (options?: RequestOptions) => Promise<string | null | undefined>;
|
141
145
|
/**
|
142
146
|
* Abort the current request immediately, keep the generated tokens if any.
|
143
147
|
*/
|
@@ -165,7 +169,7 @@ declare type UseCompletionHelpers = {
|
|
165
169
|
/**
|
166
170
|
* Send a new prompt to the API endpoint and update the completion state.
|
167
171
|
*/
|
168
|
-
complete: (prompt: string) => Promise<string | null | undefined>;
|
172
|
+
complete: (prompt: string, options?: RequestOptions) => Promise<string | null | undefined>;
|
169
173
|
/**
|
170
174
|
* Abort the current API request but keep the generated tokens.
|
171
175
|
*/
|
package/vue/dist/index.js
CHANGED
@@ -119,7 +119,7 @@ function useChat({
|
|
119
119
|
const error = (0, import_vue.ref)(void 0);
|
120
120
|
const isLoading = (0, import_vue.ref)(false);
|
121
121
|
let abortController = null;
|
122
|
-
function triggerRequest(messagesSnapshot) {
|
122
|
+
function triggerRequest(messagesSnapshot, options) {
|
123
123
|
return __async(this, null, function* () {
|
124
124
|
try {
|
125
125
|
isLoading.value = true;
|
@@ -128,13 +128,13 @@ function useChat({
|
|
128
128
|
mutate(messagesSnapshot);
|
129
129
|
const res = yield fetch(api, {
|
130
130
|
method: "POST",
|
131
|
-
body: JSON.stringify(__spreadValues({
|
131
|
+
body: JSON.stringify(__spreadValues(__spreadValues({
|
132
132
|
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
133
133
|
role,
|
134
134
|
content
|
135
135
|
}))
|
136
|
-
}, body)),
|
137
|
-
headers: headers
|
136
|
+
}, body), options == null ? void 0 : options.body)),
|
137
|
+
headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
|
138
138
|
signal: abortController.signal
|
139
139
|
}).catch((err) => {
|
140
140
|
mutate(previousMessages);
|
@@ -205,21 +205,21 @@ function useChat({
|
|
205
205
|
}
|
206
206
|
});
|
207
207
|
}
|
208
|
-
const append = (message) => __async(this, null, function* () {
|
208
|
+
const append = (message, options) => __async(this, null, function* () {
|
209
209
|
if (!message.id) {
|
210
210
|
message.id = nanoid();
|
211
211
|
}
|
212
|
-
return triggerRequest(messages.value.concat(message));
|
212
|
+
return triggerRequest(messages.value.concat(message), options);
|
213
213
|
});
|
214
|
-
const reload = () => __async(this, null, function* () {
|
214
|
+
const reload = (options) => __async(this, null, function* () {
|
215
215
|
const messagesSnapshot = messages.value;
|
216
216
|
if (messagesSnapshot.length === 0)
|
217
217
|
return null;
|
218
218
|
const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];
|
219
219
|
if (lastMessage.role === "assistant") {
|
220
|
-
return triggerRequest(messagesSnapshot.slice(0, -1));
|
220
|
+
return triggerRequest(messagesSnapshot.slice(0, -1), options);
|
221
221
|
}
|
222
|
-
return triggerRequest(messagesSnapshot);
|
222
|
+
return triggerRequest(messagesSnapshot, options);
|
223
223
|
});
|
224
224
|
const stop = () => {
|
225
225
|
if (abortController) {
|
@@ -287,7 +287,7 @@ function useCompletion({
|
|
287
287
|
const error = (0, import_vue2.ref)(void 0);
|
288
288
|
const isLoading = (0, import_vue2.ref)(false);
|
289
289
|
let abortController = null;
|
290
|
-
function triggerRequest(prompt) {
|
290
|
+
function triggerRequest(prompt, options) {
|
291
291
|
return __async(this, null, function* () {
|
292
292
|
try {
|
293
293
|
isLoading.value = true;
|
@@ -295,10 +295,10 @@ function useCompletion({
|
|
295
295
|
mutate("");
|
296
296
|
const res = yield fetch(api, {
|
297
297
|
method: "POST",
|
298
|
-
body: JSON.stringify(__spreadValues({
|
298
|
+
body: JSON.stringify(__spreadValues(__spreadValues({
|
299
299
|
prompt
|
300
|
-
}, body)),
|
301
|
-
headers: headers
|
300
|
+
}, body), options == null ? void 0 : options.body)),
|
301
|
+
headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
|
302
302
|
signal: abortController.signal
|
303
303
|
}).catch((err) => {
|
304
304
|
throw err;
|
@@ -352,8 +352,8 @@ function useCompletion({
|
|
352
352
|
}
|
353
353
|
});
|
354
354
|
}
|
355
|
-
const complete = (prompt) => __async(this, null, function* () {
|
356
|
-
return triggerRequest(prompt);
|
355
|
+
const complete = (prompt, options) => __async(this, null, function* () {
|
356
|
+
return triggerRequest(prompt, options);
|
357
357
|
});
|
358
358
|
const stop = () => {
|
359
359
|
if (abortController) {
|
package/vue/dist/index.mjs
CHANGED
@@ -85,7 +85,7 @@ function useChat({
|
|
85
85
|
const error = ref(void 0);
|
86
86
|
const isLoading = ref(false);
|
87
87
|
let abortController = null;
|
88
|
-
function triggerRequest(messagesSnapshot) {
|
88
|
+
function triggerRequest(messagesSnapshot, options) {
|
89
89
|
return __async(this, null, function* () {
|
90
90
|
try {
|
91
91
|
isLoading.value = true;
|
@@ -94,13 +94,13 @@ function useChat({
|
|
94
94
|
mutate(messagesSnapshot);
|
95
95
|
const res = yield fetch(api, {
|
96
96
|
method: "POST",
|
97
|
-
body: JSON.stringify(__spreadValues({
|
97
|
+
body: JSON.stringify(__spreadValues(__spreadValues({
|
98
98
|
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
99
99
|
role,
|
100
100
|
content
|
101
101
|
}))
|
102
|
-
}, body)),
|
103
|
-
headers: headers
|
102
|
+
}, body), options == null ? void 0 : options.body)),
|
103
|
+
headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
|
104
104
|
signal: abortController.signal
|
105
105
|
}).catch((err) => {
|
106
106
|
mutate(previousMessages);
|
@@ -171,21 +171,21 @@ function useChat({
|
|
171
171
|
}
|
172
172
|
});
|
173
173
|
}
|
174
|
-
const append = (message) => __async(this, null, function* () {
|
174
|
+
const append = (message, options) => __async(this, null, function* () {
|
175
175
|
if (!message.id) {
|
176
176
|
message.id = nanoid();
|
177
177
|
}
|
178
|
-
return triggerRequest(messages.value.concat(message));
|
178
|
+
return triggerRequest(messages.value.concat(message), options);
|
179
179
|
});
|
180
|
-
const reload = () => __async(this, null, function* () {
|
180
|
+
const reload = (options) => __async(this, null, function* () {
|
181
181
|
const messagesSnapshot = messages.value;
|
182
182
|
if (messagesSnapshot.length === 0)
|
183
183
|
return null;
|
184
184
|
const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];
|
185
185
|
if (lastMessage.role === "assistant") {
|
186
|
-
return triggerRequest(messagesSnapshot.slice(0, -1));
|
186
|
+
return triggerRequest(messagesSnapshot.slice(0, -1), options);
|
187
187
|
}
|
188
|
-
return triggerRequest(messagesSnapshot);
|
188
|
+
return triggerRequest(messagesSnapshot, options);
|
189
189
|
});
|
190
190
|
const stop = () => {
|
191
191
|
if (abortController) {
|
@@ -253,7 +253,7 @@ function useCompletion({
|
|
253
253
|
const error = ref2(void 0);
|
254
254
|
const isLoading = ref2(false);
|
255
255
|
let abortController = null;
|
256
|
-
function triggerRequest(prompt) {
|
256
|
+
function triggerRequest(prompt, options) {
|
257
257
|
return __async(this, null, function* () {
|
258
258
|
try {
|
259
259
|
isLoading.value = true;
|
@@ -261,10 +261,10 @@ function useCompletion({
|
|
261
261
|
mutate("");
|
262
262
|
const res = yield fetch(api, {
|
263
263
|
method: "POST",
|
264
|
-
body: JSON.stringify(__spreadValues({
|
264
|
+
body: JSON.stringify(__spreadValues(__spreadValues({
|
265
265
|
prompt
|
266
|
-
}, body)),
|
267
|
-
headers: headers
|
266
|
+
}, body), options == null ? void 0 : options.body)),
|
267
|
+
headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
|
268
268
|
signal: abortController.signal
|
269
269
|
}).catch((err) => {
|
270
270
|
throw err;
|
@@ -318,8 +318,8 @@ function useCompletion({
|
|
318
318
|
}
|
319
319
|
});
|
320
320
|
}
|
321
|
-
const complete = (prompt) => __async(this, null, function* () {
|
322
|
-
return triggerRequest(prompt);
|
321
|
+
const complete = (prompt, options) => __async(this, null, function* () {
|
322
|
+
return triggerRequest(prompt, options);
|
323
323
|
});
|
324
324
|
const stop = () => {
|
325
325
|
if (abortController) {
|