ai 2.1.9 → 2.1.10
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 +51 -8
- package/dist/index.js +30 -2
- package/dist/index.mjs +28 -2
- package/package.json +6 -5
- package/react/dist/index.d.ts +53 -12
- package/react/dist/index.js +134 -82
- package/react/dist/index.mjs +135 -83
- package/svelte/dist/index.d.ts +44 -9
- package/svelte/dist/index.js +7 -3
- package/svelte/dist/index.mjs +7 -3
- package/vue/dist/index.d.ts +44 -9
- package/vue/dist/index.js +6 -2
- package/vue/dist/index.mjs +6 -2
package/react/dist/index.js
CHANGED
@@ -92,15 +92,97 @@ function createChunkDecoder() {
|
|
92
92
|
}
|
93
93
|
|
94
94
|
// react/use-chat.ts
|
95
|
+
var getStreamedResponse = (api, chatRequest, mutate, extraMetadataRef, messagesRef, abortControllerRef, onFinish, onResponse, sendExtraMessageFields) => __async(void 0, null, function* () {
|
96
|
+
var _a, _b;
|
97
|
+
const previousMessages = messagesRef.current;
|
98
|
+
mutate(chatRequest.messages, false);
|
99
|
+
const res = yield fetch(api, __spreadValues({
|
100
|
+
method: "POST",
|
101
|
+
body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
102
|
+
messages: sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
|
103
|
+
({ role, content, name, function_call }) => __spreadValues(__spreadValues({
|
104
|
+
role,
|
105
|
+
content
|
106
|
+
}, name !== void 0 && { name }), function_call !== void 0 && {
|
107
|
+
function_call
|
108
|
+
})
|
109
|
+
)
|
110
|
+
}, extraMetadataRef.current.body), (_a = chatRequest.options) == null ? void 0 : _a.body), chatRequest.functions !== void 0 && {
|
111
|
+
functions: chatRequest.functions
|
112
|
+
}), chatRequest.function_call !== void 0 && {
|
113
|
+
function_call: chatRequest.function_call
|
114
|
+
})),
|
115
|
+
credentials: extraMetadataRef.current.credentials,
|
116
|
+
headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), (_b = chatRequest.options) == null ? void 0 : _b.headers)
|
117
|
+
}, abortControllerRef.current !== null && {
|
118
|
+
signal: abortControllerRef.current.signal
|
119
|
+
})).catch((err) => {
|
120
|
+
mutate(previousMessages, false);
|
121
|
+
throw err;
|
122
|
+
});
|
123
|
+
if (onResponse) {
|
124
|
+
try {
|
125
|
+
yield onResponse(res);
|
126
|
+
} catch (err) {
|
127
|
+
throw err;
|
128
|
+
}
|
129
|
+
}
|
130
|
+
if (!res.ok) {
|
131
|
+
mutate(previousMessages, false);
|
132
|
+
throw new Error((yield res.text()) || "Failed to fetch the chat response.");
|
133
|
+
}
|
134
|
+
if (!res.body) {
|
135
|
+
throw new Error("The response body is empty.");
|
136
|
+
}
|
137
|
+
let streamedResponse = "";
|
138
|
+
const createdAt = /* @__PURE__ */ new Date();
|
139
|
+
const replyId = nanoid();
|
140
|
+
const reader = res.body.getReader();
|
141
|
+
const decode = createChunkDecoder();
|
142
|
+
let responseMessage = {
|
143
|
+
id: replyId,
|
144
|
+
createdAt,
|
145
|
+
content: "",
|
146
|
+
role: "assistant"
|
147
|
+
};
|
148
|
+
while (true) {
|
149
|
+
const { done, value } = yield reader.read();
|
150
|
+
if (done) {
|
151
|
+
break;
|
152
|
+
}
|
153
|
+
streamedResponse += decode(value);
|
154
|
+
if (streamedResponse.startsWith('{"function_call":')) {
|
155
|
+
responseMessage["function_call"] = streamedResponse;
|
156
|
+
} else {
|
157
|
+
responseMessage["content"] = streamedResponse;
|
158
|
+
}
|
159
|
+
mutate([...chatRequest.messages, __spreadValues({}, responseMessage)], false);
|
160
|
+
if (abortControllerRef.current === null) {
|
161
|
+
reader.cancel();
|
162
|
+
break;
|
163
|
+
}
|
164
|
+
}
|
165
|
+
if (streamedResponse.startsWith('{"function_call":')) {
|
166
|
+
const parsedFunctionCall = JSON.parse(streamedResponse).function_call;
|
167
|
+
responseMessage["function_call"] = parsedFunctionCall;
|
168
|
+
mutate([...chatRequest.messages, __spreadValues({}, responseMessage)]);
|
169
|
+
}
|
170
|
+
if (onFinish) {
|
171
|
+
onFinish(responseMessage);
|
172
|
+
}
|
173
|
+
return responseMessage;
|
174
|
+
});
|
95
175
|
function useChat({
|
96
176
|
api = "/api/chat",
|
97
177
|
id,
|
98
178
|
initialMessages = [],
|
99
179
|
initialInput = "",
|
100
180
|
sendExtraMessageFields,
|
181
|
+
experimental_onFunctionCall,
|
101
182
|
onResponse,
|
102
183
|
onFinish,
|
103
184
|
onError,
|
185
|
+
credentials,
|
104
186
|
headers,
|
105
187
|
body
|
106
188
|
} = {}) {
|
@@ -116,92 +198,52 @@ function useChat({
|
|
116
198
|
}, [messages]);
|
117
199
|
const abortControllerRef = (0, import_react.useRef)(null);
|
118
200
|
const extraMetadataRef = (0, import_react.useRef)({
|
201
|
+
credentials,
|
119
202
|
headers,
|
120
203
|
body
|
121
204
|
});
|
122
205
|
(0, import_react.useEffect)(() => {
|
123
206
|
extraMetadataRef.current = {
|
207
|
+
credentials,
|
124
208
|
headers,
|
125
209
|
body
|
126
210
|
};
|
127
|
-
}, [headers, body]);
|
211
|
+
}, [credentials, headers, body]);
|
128
212
|
const { error, trigger, isMutating } = (0, import_mutation.default)(
|
129
213
|
[api, chatId],
|
130
|
-
(_0, _1) => __async(this, [_0, _1], function* (_, { arg }) {
|
214
|
+
(_0, _1) => __async(this, [_0, _1], function* (_, { arg: initialChatRequest }) {
|
131
215
|
try {
|
132
|
-
const { messages: messagesSnapshot, options } = arg;
|
133
216
|
const abortController = new AbortController();
|
134
217
|
abortControllerRef.current = abortController;
|
135
|
-
|
136
|
-
mutate(messagesSnapshot, false);
|
137
|
-
const res = yield fetch(api, {
|
138
|
-
method: "POST",
|
139
|
-
body: JSON.stringify(__spreadValues(__spreadValues({
|
140
|
-
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
141
|
-
role,
|
142
|
-
content
|
143
|
-
}))
|
144
|
-
}, extraMetadataRef.current.body), options == null ? void 0 : options.body)),
|
145
|
-
headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), options == null ? void 0 : options.headers),
|
146
|
-
signal: abortController.signal
|
147
|
-
}).catch((err) => {
|
148
|
-
mutate(previousMessages, false);
|
149
|
-
throw err;
|
150
|
-
});
|
151
|
-
if (onResponse) {
|
152
|
-
try {
|
153
|
-
yield onResponse(res);
|
154
|
-
} catch (err) {
|
155
|
-
throw err;
|
156
|
-
}
|
157
|
-
}
|
158
|
-
if (!res.ok) {
|
159
|
-
mutate(previousMessages, false);
|
160
|
-
throw new Error(
|
161
|
-
(yield res.text()) || "Failed to fetch the chat response."
|
162
|
-
);
|
163
|
-
}
|
164
|
-
if (!res.body) {
|
165
|
-
throw new Error("The response body is empty.");
|
166
|
-
}
|
167
|
-
let result = "";
|
168
|
-
const createdAt = /* @__PURE__ */ new Date();
|
169
|
-
const replyId = nanoid();
|
170
|
-
const reader = res.body.getReader();
|
171
|
-
const decode = createChunkDecoder();
|
218
|
+
let chatRequest = initialChatRequest;
|
172
219
|
while (true) {
|
173
|
-
const
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
createdAt,
|
184
|
-
content: result,
|
185
|
-
role: "assistant"
|
186
|
-
}
|
187
|
-
],
|
188
|
-
false
|
220
|
+
const streamedResponseMessage = yield getStreamedResponse(
|
221
|
+
api,
|
222
|
+
chatRequest,
|
223
|
+
mutate,
|
224
|
+
extraMetadataRef,
|
225
|
+
messagesRef,
|
226
|
+
abortControllerRef,
|
227
|
+
onFinish,
|
228
|
+
onResponse,
|
229
|
+
sendExtraMessageFields
|
189
230
|
);
|
190
|
-
if (
|
191
|
-
reader.cancel();
|
231
|
+
if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
|
192
232
|
break;
|
193
233
|
}
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
234
|
+
if (experimental_onFunctionCall) {
|
235
|
+
const functionCall = streamedResponseMessage.function_call;
|
236
|
+
const functionCallResponse = yield experimental_onFunctionCall(
|
237
|
+
messagesRef.current,
|
238
|
+
functionCall
|
239
|
+
);
|
240
|
+
if (functionCallResponse === void 0)
|
241
|
+
break;
|
242
|
+
chatRequest = functionCallResponse;
|
243
|
+
}
|
202
244
|
}
|
203
245
|
abortControllerRef.current = null;
|
204
|
-
return
|
246
|
+
return null;
|
205
247
|
} catch (err) {
|
206
248
|
if (err.name === "AbortError") {
|
207
249
|
abortControllerRef.current = null;
|
@@ -219,32 +261,35 @@ function useChat({
|
|
219
261
|
}
|
220
262
|
);
|
221
263
|
const append = (0, import_react.useCallback)(
|
222
|
-
(
|
264
|
+
(_0, ..._1) => __async(this, [_0, ..._1], function* (message, { options, functions, function_call } = {}) {
|
223
265
|
if (!message.id) {
|
224
266
|
message.id = nanoid();
|
225
267
|
}
|
226
|
-
|
268
|
+
const chatRequest = __spreadValues(__spreadValues({
|
227
269
|
messages: messagesRef.current.concat(message),
|
228
270
|
options
|
229
|
-
});
|
271
|
+
}, functions !== void 0 && { functions }), function_call !== void 0 && { function_call });
|
272
|
+
return trigger(chatRequest);
|
230
273
|
}),
|
231
274
|
[trigger]
|
232
275
|
);
|
233
276
|
const reload = (0, import_react.useCallback)(
|
234
|
-
(
|
277
|
+
(..._0) => __async(this, [..._0], function* ({ options, functions, function_call } = {}) {
|
235
278
|
if (messagesRef.current.length === 0)
|
236
279
|
return null;
|
237
280
|
const lastMessage = messagesRef.current[messagesRef.current.length - 1];
|
238
281
|
if (lastMessage.role === "assistant") {
|
239
|
-
|
282
|
+
const chatRequest2 = __spreadValues(__spreadValues({
|
240
283
|
messages: messagesRef.current.slice(0, -1),
|
241
284
|
options
|
242
|
-
});
|
285
|
+
}, functions !== void 0 && { functions }), function_call !== void 0 && { function_call });
|
286
|
+
return trigger(chatRequest2);
|
243
287
|
}
|
244
|
-
|
288
|
+
const chatRequest = __spreadValues(__spreadValues({
|
245
289
|
messages: messagesRef.current,
|
246
290
|
options
|
247
|
-
});
|
291
|
+
}, functions !== void 0 && { functions }), function_call !== void 0 && { function_call });
|
292
|
+
return trigger(chatRequest);
|
248
293
|
}),
|
249
294
|
[trigger]
|
250
295
|
);
|
@@ -263,18 +308,21 @@ function useChat({
|
|
263
308
|
);
|
264
309
|
const [input, setInput] = (0, import_react.useState)(initialInput);
|
265
310
|
const handleSubmit = (0, import_react.useCallback)(
|
266
|
-
(e, metadata) => {
|
311
|
+
(e, { options, functions, function_call } = {}, metadata) => {
|
267
312
|
if (metadata) {
|
268
313
|
extraMetadataRef.current = __spreadValues(__spreadValues({}, extraMetadataRef.current), metadata);
|
269
314
|
}
|
270
315
|
e.preventDefault();
|
271
316
|
if (!input)
|
272
317
|
return;
|
273
|
-
append(
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
318
|
+
append(
|
319
|
+
{
|
320
|
+
content: input,
|
321
|
+
role: "user",
|
322
|
+
createdAt: /* @__PURE__ */ new Date()
|
323
|
+
},
|
324
|
+
{ options, functions, function_call }
|
325
|
+
);
|
278
326
|
setInput("");
|
279
327
|
},
|
280
328
|
[input, append]
|
@@ -306,6 +354,7 @@ function useCompletion({
|
|
306
354
|
id,
|
307
355
|
initialCompletion = "",
|
308
356
|
initialInput = "",
|
357
|
+
credentials,
|
309
358
|
headers,
|
310
359
|
body,
|
311
360
|
onResponse,
|
@@ -320,15 +369,17 @@ function useCompletion({
|
|
320
369
|
const completion = data;
|
321
370
|
const [abortController, setAbortController] = (0, import_react2.useState)(null);
|
322
371
|
const extraMetadataRef = (0, import_react2.useRef)({
|
372
|
+
credentials,
|
323
373
|
headers,
|
324
374
|
body
|
325
375
|
});
|
326
376
|
(0, import_react2.useEffect)(() => {
|
327
377
|
extraMetadataRef.current = {
|
378
|
+
credentials,
|
328
379
|
headers,
|
329
380
|
body
|
330
381
|
};
|
331
|
-
}, [headers, body]);
|
382
|
+
}, [credentials, headers, body]);
|
332
383
|
const { error, trigger, isMutating } = (0, import_mutation2.default)(
|
333
384
|
[api, completionId],
|
334
385
|
(_0, _1) => __async(this, [_0, _1], function* (_, { arg }) {
|
@@ -342,6 +393,7 @@ function useCompletion({
|
|
342
393
|
body: JSON.stringify(__spreadValues(__spreadValues({
|
343
394
|
prompt
|
344
395
|
}, extraMetadataRef.current.body), options == null ? void 0 : options.body)),
|
396
|
+
credentials: extraMetadataRef.current.credentials,
|
345
397
|
headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), options == null ? void 0 : options.headers),
|
346
398
|
signal: abortController2.signal
|
347
399
|
}).catch((err) => {
|
package/react/dist/index.mjs
CHANGED
@@ -37,7 +37,7 @@ var __async = (__this, __arguments, generator) => {
|
|
37
37
|
};
|
38
38
|
|
39
39
|
// react/use-chat.ts
|
40
|
-
import { useCallback, useId, useRef,
|
40
|
+
import { useCallback, useEffect, useId, useRef, useState } from "react";
|
41
41
|
import useSWRMutation from "swr/mutation";
|
42
42
|
import useSWR from "swr";
|
43
43
|
|
@@ -57,15 +57,97 @@ function createChunkDecoder() {
|
|
57
57
|
}
|
58
58
|
|
59
59
|
// react/use-chat.ts
|
60
|
+
var getStreamedResponse = (api, chatRequest, mutate, extraMetadataRef, messagesRef, abortControllerRef, onFinish, onResponse, sendExtraMessageFields) => __async(void 0, null, function* () {
|
61
|
+
var _a, _b;
|
62
|
+
const previousMessages = messagesRef.current;
|
63
|
+
mutate(chatRequest.messages, false);
|
64
|
+
const res = yield fetch(api, __spreadValues({
|
65
|
+
method: "POST",
|
66
|
+
body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
67
|
+
messages: sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
|
68
|
+
({ role, content, name, function_call }) => __spreadValues(__spreadValues({
|
69
|
+
role,
|
70
|
+
content
|
71
|
+
}, name !== void 0 && { name }), function_call !== void 0 && {
|
72
|
+
function_call
|
73
|
+
})
|
74
|
+
)
|
75
|
+
}, extraMetadataRef.current.body), (_a = chatRequest.options) == null ? void 0 : _a.body), chatRequest.functions !== void 0 && {
|
76
|
+
functions: chatRequest.functions
|
77
|
+
}), chatRequest.function_call !== void 0 && {
|
78
|
+
function_call: chatRequest.function_call
|
79
|
+
})),
|
80
|
+
credentials: extraMetadataRef.current.credentials,
|
81
|
+
headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), (_b = chatRequest.options) == null ? void 0 : _b.headers)
|
82
|
+
}, abortControllerRef.current !== null && {
|
83
|
+
signal: abortControllerRef.current.signal
|
84
|
+
})).catch((err) => {
|
85
|
+
mutate(previousMessages, false);
|
86
|
+
throw err;
|
87
|
+
});
|
88
|
+
if (onResponse) {
|
89
|
+
try {
|
90
|
+
yield onResponse(res);
|
91
|
+
} catch (err) {
|
92
|
+
throw err;
|
93
|
+
}
|
94
|
+
}
|
95
|
+
if (!res.ok) {
|
96
|
+
mutate(previousMessages, false);
|
97
|
+
throw new Error((yield res.text()) || "Failed to fetch the chat response.");
|
98
|
+
}
|
99
|
+
if (!res.body) {
|
100
|
+
throw new Error("The response body is empty.");
|
101
|
+
}
|
102
|
+
let streamedResponse = "";
|
103
|
+
const createdAt = /* @__PURE__ */ new Date();
|
104
|
+
const replyId = nanoid();
|
105
|
+
const reader = res.body.getReader();
|
106
|
+
const decode = createChunkDecoder();
|
107
|
+
let responseMessage = {
|
108
|
+
id: replyId,
|
109
|
+
createdAt,
|
110
|
+
content: "",
|
111
|
+
role: "assistant"
|
112
|
+
};
|
113
|
+
while (true) {
|
114
|
+
const { done, value } = yield reader.read();
|
115
|
+
if (done) {
|
116
|
+
break;
|
117
|
+
}
|
118
|
+
streamedResponse += decode(value);
|
119
|
+
if (streamedResponse.startsWith('{"function_call":')) {
|
120
|
+
responseMessage["function_call"] = streamedResponse;
|
121
|
+
} else {
|
122
|
+
responseMessage["content"] = streamedResponse;
|
123
|
+
}
|
124
|
+
mutate([...chatRequest.messages, __spreadValues({}, responseMessage)], false);
|
125
|
+
if (abortControllerRef.current === null) {
|
126
|
+
reader.cancel();
|
127
|
+
break;
|
128
|
+
}
|
129
|
+
}
|
130
|
+
if (streamedResponse.startsWith('{"function_call":')) {
|
131
|
+
const parsedFunctionCall = JSON.parse(streamedResponse).function_call;
|
132
|
+
responseMessage["function_call"] = parsedFunctionCall;
|
133
|
+
mutate([...chatRequest.messages, __spreadValues({}, responseMessage)]);
|
134
|
+
}
|
135
|
+
if (onFinish) {
|
136
|
+
onFinish(responseMessage);
|
137
|
+
}
|
138
|
+
return responseMessage;
|
139
|
+
});
|
60
140
|
function useChat({
|
61
141
|
api = "/api/chat",
|
62
142
|
id,
|
63
143
|
initialMessages = [],
|
64
144
|
initialInput = "",
|
65
145
|
sendExtraMessageFields,
|
146
|
+
experimental_onFunctionCall,
|
66
147
|
onResponse,
|
67
148
|
onFinish,
|
68
149
|
onError,
|
150
|
+
credentials,
|
69
151
|
headers,
|
70
152
|
body
|
71
153
|
} = {}) {
|
@@ -81,92 +163,52 @@ function useChat({
|
|
81
163
|
}, [messages]);
|
82
164
|
const abortControllerRef = useRef(null);
|
83
165
|
const extraMetadataRef = useRef({
|
166
|
+
credentials,
|
84
167
|
headers,
|
85
168
|
body
|
86
169
|
});
|
87
170
|
useEffect(() => {
|
88
171
|
extraMetadataRef.current = {
|
172
|
+
credentials,
|
89
173
|
headers,
|
90
174
|
body
|
91
175
|
};
|
92
|
-
}, [headers, body]);
|
176
|
+
}, [credentials, headers, body]);
|
93
177
|
const { error, trigger, isMutating } = useSWRMutation(
|
94
178
|
[api, chatId],
|
95
|
-
(_0, _1) => __async(this, [_0, _1], function* (_, { arg }) {
|
179
|
+
(_0, _1) => __async(this, [_0, _1], function* (_, { arg: initialChatRequest }) {
|
96
180
|
try {
|
97
|
-
const { messages: messagesSnapshot, options } = arg;
|
98
181
|
const abortController = new AbortController();
|
99
182
|
abortControllerRef.current = abortController;
|
100
|
-
|
101
|
-
mutate(messagesSnapshot, false);
|
102
|
-
const res = yield fetch(api, {
|
103
|
-
method: "POST",
|
104
|
-
body: JSON.stringify(__spreadValues(__spreadValues({
|
105
|
-
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
106
|
-
role,
|
107
|
-
content
|
108
|
-
}))
|
109
|
-
}, extraMetadataRef.current.body), options == null ? void 0 : options.body)),
|
110
|
-
headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), options == null ? void 0 : options.headers),
|
111
|
-
signal: abortController.signal
|
112
|
-
}).catch((err) => {
|
113
|
-
mutate(previousMessages, false);
|
114
|
-
throw err;
|
115
|
-
});
|
116
|
-
if (onResponse) {
|
117
|
-
try {
|
118
|
-
yield onResponse(res);
|
119
|
-
} catch (err) {
|
120
|
-
throw err;
|
121
|
-
}
|
122
|
-
}
|
123
|
-
if (!res.ok) {
|
124
|
-
mutate(previousMessages, false);
|
125
|
-
throw new Error(
|
126
|
-
(yield res.text()) || "Failed to fetch the chat response."
|
127
|
-
);
|
128
|
-
}
|
129
|
-
if (!res.body) {
|
130
|
-
throw new Error("The response body is empty.");
|
131
|
-
}
|
132
|
-
let result = "";
|
133
|
-
const createdAt = /* @__PURE__ */ new Date();
|
134
|
-
const replyId = nanoid();
|
135
|
-
const reader = res.body.getReader();
|
136
|
-
const decode = createChunkDecoder();
|
183
|
+
let chatRequest = initialChatRequest;
|
137
184
|
while (true) {
|
138
|
-
const
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
createdAt,
|
149
|
-
content: result,
|
150
|
-
role: "assistant"
|
151
|
-
}
|
152
|
-
],
|
153
|
-
false
|
185
|
+
const streamedResponseMessage = yield getStreamedResponse(
|
186
|
+
api,
|
187
|
+
chatRequest,
|
188
|
+
mutate,
|
189
|
+
extraMetadataRef,
|
190
|
+
messagesRef,
|
191
|
+
abortControllerRef,
|
192
|
+
onFinish,
|
193
|
+
onResponse,
|
194
|
+
sendExtraMessageFields
|
154
195
|
);
|
155
|
-
if (
|
156
|
-
reader.cancel();
|
196
|
+
if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
|
157
197
|
break;
|
158
198
|
}
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
199
|
+
if (experimental_onFunctionCall) {
|
200
|
+
const functionCall = streamedResponseMessage.function_call;
|
201
|
+
const functionCallResponse = yield experimental_onFunctionCall(
|
202
|
+
messagesRef.current,
|
203
|
+
functionCall
|
204
|
+
);
|
205
|
+
if (functionCallResponse === void 0)
|
206
|
+
break;
|
207
|
+
chatRequest = functionCallResponse;
|
208
|
+
}
|
167
209
|
}
|
168
210
|
abortControllerRef.current = null;
|
169
|
-
return
|
211
|
+
return null;
|
170
212
|
} catch (err) {
|
171
213
|
if (err.name === "AbortError") {
|
172
214
|
abortControllerRef.current = null;
|
@@ -184,32 +226,35 @@ function useChat({
|
|
184
226
|
}
|
185
227
|
);
|
186
228
|
const append = useCallback(
|
187
|
-
(
|
229
|
+
(_0, ..._1) => __async(this, [_0, ..._1], function* (message, { options, functions, function_call } = {}) {
|
188
230
|
if (!message.id) {
|
189
231
|
message.id = nanoid();
|
190
232
|
}
|
191
|
-
|
233
|
+
const chatRequest = __spreadValues(__spreadValues({
|
192
234
|
messages: messagesRef.current.concat(message),
|
193
235
|
options
|
194
|
-
});
|
236
|
+
}, functions !== void 0 && { functions }), function_call !== void 0 && { function_call });
|
237
|
+
return trigger(chatRequest);
|
195
238
|
}),
|
196
239
|
[trigger]
|
197
240
|
);
|
198
241
|
const reload = useCallback(
|
199
|
-
(
|
242
|
+
(..._0) => __async(this, [..._0], function* ({ options, functions, function_call } = {}) {
|
200
243
|
if (messagesRef.current.length === 0)
|
201
244
|
return null;
|
202
245
|
const lastMessage = messagesRef.current[messagesRef.current.length - 1];
|
203
246
|
if (lastMessage.role === "assistant") {
|
204
|
-
|
247
|
+
const chatRequest2 = __spreadValues(__spreadValues({
|
205
248
|
messages: messagesRef.current.slice(0, -1),
|
206
249
|
options
|
207
|
-
});
|
250
|
+
}, functions !== void 0 && { functions }), function_call !== void 0 && { function_call });
|
251
|
+
return trigger(chatRequest2);
|
208
252
|
}
|
209
|
-
|
253
|
+
const chatRequest = __spreadValues(__spreadValues({
|
210
254
|
messages: messagesRef.current,
|
211
255
|
options
|
212
|
-
});
|
256
|
+
}, functions !== void 0 && { functions }), function_call !== void 0 && { function_call });
|
257
|
+
return trigger(chatRequest);
|
213
258
|
}),
|
214
259
|
[trigger]
|
215
260
|
);
|
@@ -228,18 +273,21 @@ function useChat({
|
|
228
273
|
);
|
229
274
|
const [input, setInput] = useState(initialInput);
|
230
275
|
const handleSubmit = useCallback(
|
231
|
-
(e, metadata) => {
|
276
|
+
(e, { options, functions, function_call } = {}, metadata) => {
|
232
277
|
if (metadata) {
|
233
278
|
extraMetadataRef.current = __spreadValues(__spreadValues({}, extraMetadataRef.current), metadata);
|
234
279
|
}
|
235
280
|
e.preventDefault();
|
236
281
|
if (!input)
|
237
282
|
return;
|
238
|
-
append(
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
283
|
+
append(
|
284
|
+
{
|
285
|
+
content: input,
|
286
|
+
role: "user",
|
287
|
+
createdAt: /* @__PURE__ */ new Date()
|
288
|
+
},
|
289
|
+
{ options, functions, function_call }
|
290
|
+
);
|
243
291
|
setInput("");
|
244
292
|
},
|
245
293
|
[input, append]
|
@@ -271,6 +319,7 @@ function useCompletion({
|
|
271
319
|
id,
|
272
320
|
initialCompletion = "",
|
273
321
|
initialInput = "",
|
322
|
+
credentials,
|
274
323
|
headers,
|
275
324
|
body,
|
276
325
|
onResponse,
|
@@ -285,15 +334,17 @@ function useCompletion({
|
|
285
334
|
const completion = data;
|
286
335
|
const [abortController, setAbortController] = useState2(null);
|
287
336
|
const extraMetadataRef = useRef2({
|
337
|
+
credentials,
|
288
338
|
headers,
|
289
339
|
body
|
290
340
|
});
|
291
341
|
useEffect2(() => {
|
292
342
|
extraMetadataRef.current = {
|
343
|
+
credentials,
|
293
344
|
headers,
|
294
345
|
body
|
295
346
|
};
|
296
|
-
}, [headers, body]);
|
347
|
+
}, [credentials, headers, body]);
|
297
348
|
const { error, trigger, isMutating } = useSWRMutation2(
|
298
349
|
[api, completionId],
|
299
350
|
(_0, _1) => __async(this, [_0, _1], function* (_, { arg }) {
|
@@ -307,6 +358,7 @@ function useCompletion({
|
|
307
358
|
body: JSON.stringify(__spreadValues(__spreadValues({
|
308
359
|
prompt
|
309
360
|
}, extraMetadataRef.current.body), options == null ? void 0 : options.body)),
|
361
|
+
credentials: extraMetadataRef.current.credentials,
|
310
362
|
headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), options == null ? void 0 : options.headers),
|
311
363
|
signal: abortController2.signal
|
312
364
|
}).catch((err) => {
|