ai 2.1.8 → 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 +57 -12
- package/dist/index.js +56 -2
- package/dist/index.mjs +53 -2
- package/package.json +9 -8
- package/react/dist/index.d.ts +73 -19
- package/react/dist/index.js +160 -82
- package/react/dist/index.mjs +160 -83
- package/svelte/dist/index.d.ts +50 -15
- package/svelte/dist/index.js +7 -3
- package/svelte/dist/index.mjs +7 -3
- package/vue/dist/index.d.ts +50 -15
- package/vue/dist/index.js +6 -2
- package/vue/dist/index.mjs +6 -2
package/react/dist/index.js
CHANGED
@@ -65,6 +65,7 @@ var __async = (__this, __arguments, generator) => {
|
|
65
65
|
// react/index.ts
|
66
66
|
var react_exports = {};
|
67
67
|
__export(react_exports, {
|
68
|
+
Tokens: () => Tokens,
|
68
69
|
useChat: () => useChat,
|
69
70
|
useCompletion: () => useCompletion
|
70
71
|
});
|
@@ -91,15 +92,97 @@ function createChunkDecoder() {
|
|
91
92
|
}
|
92
93
|
|
93
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
|
+
});
|
94
175
|
function useChat({
|
95
176
|
api = "/api/chat",
|
96
177
|
id,
|
97
178
|
initialMessages = [],
|
98
179
|
initialInput = "",
|
99
180
|
sendExtraMessageFields,
|
181
|
+
experimental_onFunctionCall,
|
100
182
|
onResponse,
|
101
183
|
onFinish,
|
102
184
|
onError,
|
185
|
+
credentials,
|
103
186
|
headers,
|
104
187
|
body
|
105
188
|
} = {}) {
|
@@ -115,92 +198,52 @@ function useChat({
|
|
115
198
|
}, [messages]);
|
116
199
|
const abortControllerRef = (0, import_react.useRef)(null);
|
117
200
|
const extraMetadataRef = (0, import_react.useRef)({
|
201
|
+
credentials,
|
118
202
|
headers,
|
119
203
|
body
|
120
204
|
});
|
121
205
|
(0, import_react.useEffect)(() => {
|
122
206
|
extraMetadataRef.current = {
|
207
|
+
credentials,
|
123
208
|
headers,
|
124
209
|
body
|
125
210
|
};
|
126
|
-
}, [headers, body]);
|
211
|
+
}, [credentials, headers, body]);
|
127
212
|
const { error, trigger, isMutating } = (0, import_mutation.default)(
|
128
213
|
[api, chatId],
|
129
|
-
(_0, _1) => __async(this, [_0, _1], function* (_, { arg }) {
|
214
|
+
(_0, _1) => __async(this, [_0, _1], function* (_, { arg: initialChatRequest }) {
|
130
215
|
try {
|
131
|
-
const { messages: messagesSnapshot, options } = arg;
|
132
216
|
const abortController = new AbortController();
|
133
217
|
abortControllerRef.current = abortController;
|
134
|
-
|
135
|
-
mutate(messagesSnapshot, false);
|
136
|
-
const res = yield fetch(api, {
|
137
|
-
method: "POST",
|
138
|
-
body: JSON.stringify(__spreadValues(__spreadValues({
|
139
|
-
messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
|
140
|
-
role,
|
141
|
-
content
|
142
|
-
}))
|
143
|
-
}, extraMetadataRef.current.body), options == null ? void 0 : options.body)),
|
144
|
-
headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), options == null ? void 0 : options.headers),
|
145
|
-
signal: abortController.signal
|
146
|
-
}).catch((err) => {
|
147
|
-
mutate(previousMessages, false);
|
148
|
-
throw err;
|
149
|
-
});
|
150
|
-
if (onResponse) {
|
151
|
-
try {
|
152
|
-
yield onResponse(res);
|
153
|
-
} catch (err) {
|
154
|
-
throw err;
|
155
|
-
}
|
156
|
-
}
|
157
|
-
if (!res.ok) {
|
158
|
-
mutate(previousMessages, false);
|
159
|
-
throw new Error(
|
160
|
-
(yield res.text()) || "Failed to fetch the chat response."
|
161
|
-
);
|
162
|
-
}
|
163
|
-
if (!res.body) {
|
164
|
-
throw new Error("The response body is empty.");
|
165
|
-
}
|
166
|
-
let result = "";
|
167
|
-
const createdAt = /* @__PURE__ */ new Date();
|
168
|
-
const replyId = nanoid();
|
169
|
-
const reader = res.body.getReader();
|
170
|
-
const decode = createChunkDecoder();
|
218
|
+
let chatRequest = initialChatRequest;
|
171
219
|
while (true) {
|
172
|
-
const
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
createdAt,
|
183
|
-
content: result,
|
184
|
-
role: "assistant"
|
185
|
-
}
|
186
|
-
],
|
187
|
-
false
|
220
|
+
const streamedResponseMessage = yield getStreamedResponse(
|
221
|
+
api,
|
222
|
+
chatRequest,
|
223
|
+
mutate,
|
224
|
+
extraMetadataRef,
|
225
|
+
messagesRef,
|
226
|
+
abortControllerRef,
|
227
|
+
onFinish,
|
228
|
+
onResponse,
|
229
|
+
sendExtraMessageFields
|
188
230
|
);
|
189
|
-
if (
|
190
|
-
reader.cancel();
|
231
|
+
if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
|
191
232
|
break;
|
192
233
|
}
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
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
|
+
}
|
201
244
|
}
|
202
245
|
abortControllerRef.current = null;
|
203
|
-
return
|
246
|
+
return null;
|
204
247
|
} catch (err) {
|
205
248
|
if (err.name === "AbortError") {
|
206
249
|
abortControllerRef.current = null;
|
@@ -218,32 +261,35 @@ function useChat({
|
|
218
261
|
}
|
219
262
|
);
|
220
263
|
const append = (0, import_react.useCallback)(
|
221
|
-
(
|
264
|
+
(_0, ..._1) => __async(this, [_0, ..._1], function* (message, { options, functions, function_call } = {}) {
|
222
265
|
if (!message.id) {
|
223
266
|
message.id = nanoid();
|
224
267
|
}
|
225
|
-
|
268
|
+
const chatRequest = __spreadValues(__spreadValues({
|
226
269
|
messages: messagesRef.current.concat(message),
|
227
270
|
options
|
228
|
-
});
|
271
|
+
}, functions !== void 0 && { functions }), function_call !== void 0 && { function_call });
|
272
|
+
return trigger(chatRequest);
|
229
273
|
}),
|
230
274
|
[trigger]
|
231
275
|
);
|
232
276
|
const reload = (0, import_react.useCallback)(
|
233
|
-
(
|
277
|
+
(..._0) => __async(this, [..._0], function* ({ options, functions, function_call } = {}) {
|
234
278
|
if (messagesRef.current.length === 0)
|
235
279
|
return null;
|
236
280
|
const lastMessage = messagesRef.current[messagesRef.current.length - 1];
|
237
281
|
if (lastMessage.role === "assistant") {
|
238
|
-
|
282
|
+
const chatRequest2 = __spreadValues(__spreadValues({
|
239
283
|
messages: messagesRef.current.slice(0, -1),
|
240
284
|
options
|
241
|
-
});
|
285
|
+
}, functions !== void 0 && { functions }), function_call !== void 0 && { function_call });
|
286
|
+
return trigger(chatRequest2);
|
242
287
|
}
|
243
|
-
|
288
|
+
const chatRequest = __spreadValues(__spreadValues({
|
244
289
|
messages: messagesRef.current,
|
245
290
|
options
|
246
|
-
});
|
291
|
+
}, functions !== void 0 && { functions }), function_call !== void 0 && { function_call });
|
292
|
+
return trigger(chatRequest);
|
247
293
|
}),
|
248
294
|
[trigger]
|
249
295
|
);
|
@@ -262,18 +308,21 @@ function useChat({
|
|
262
308
|
);
|
263
309
|
const [input, setInput] = (0, import_react.useState)(initialInput);
|
264
310
|
const handleSubmit = (0, import_react.useCallback)(
|
265
|
-
(e, metadata) => {
|
311
|
+
(e, { options, functions, function_call } = {}, metadata) => {
|
266
312
|
if (metadata) {
|
267
313
|
extraMetadataRef.current = __spreadValues(__spreadValues({}, extraMetadataRef.current), metadata);
|
268
314
|
}
|
269
315
|
e.preventDefault();
|
270
316
|
if (!input)
|
271
317
|
return;
|
272
|
-
append(
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
318
|
+
append(
|
319
|
+
{
|
320
|
+
content: input,
|
321
|
+
role: "user",
|
322
|
+
createdAt: /* @__PURE__ */ new Date()
|
323
|
+
},
|
324
|
+
{ options, functions, function_call }
|
325
|
+
);
|
277
326
|
setInput("");
|
278
327
|
},
|
279
328
|
[input, append]
|
@@ -305,6 +354,7 @@ function useCompletion({
|
|
305
354
|
id,
|
306
355
|
initialCompletion = "",
|
307
356
|
initialInput = "",
|
357
|
+
credentials,
|
308
358
|
headers,
|
309
359
|
body,
|
310
360
|
onResponse,
|
@@ -319,15 +369,17 @@ function useCompletion({
|
|
319
369
|
const completion = data;
|
320
370
|
const [abortController, setAbortController] = (0, import_react2.useState)(null);
|
321
371
|
const extraMetadataRef = (0, import_react2.useRef)({
|
372
|
+
credentials,
|
322
373
|
headers,
|
323
374
|
body
|
324
375
|
});
|
325
376
|
(0, import_react2.useEffect)(() => {
|
326
377
|
extraMetadataRef.current = {
|
378
|
+
credentials,
|
327
379
|
headers,
|
328
380
|
body
|
329
381
|
};
|
330
|
-
}, [headers, body]);
|
382
|
+
}, [credentials, headers, body]);
|
331
383
|
const { error, trigger, isMutating } = (0, import_mutation2.default)(
|
332
384
|
[api, completionId],
|
333
385
|
(_0, _1) => __async(this, [_0, _1], function* (_, { arg }) {
|
@@ -341,6 +393,7 @@ function useCompletion({
|
|
341
393
|
body: JSON.stringify(__spreadValues(__spreadValues({
|
342
394
|
prompt
|
343
395
|
}, extraMetadataRef.current.body), options == null ? void 0 : options.body)),
|
396
|
+
credentials: extraMetadataRef.current.credentials,
|
344
397
|
headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), options == null ? void 0 : options.headers),
|
345
398
|
signal: abortController2.signal
|
346
399
|
}).catch((err) => {
|
@@ -444,8 +497,33 @@ function useCompletion({
|
|
444
497
|
isLoading: isMutating
|
445
498
|
};
|
446
499
|
}
|
500
|
+
|
501
|
+
// react/tokens.tsx
|
502
|
+
var import_react3 = require("react");
|
503
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
504
|
+
function Tokens(props) {
|
505
|
+
return __async(this, null, function* () {
|
506
|
+
const { stream } = props;
|
507
|
+
const reader = stream.getReader();
|
508
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react3.Suspense, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(RecursiveTokens, { reader }) });
|
509
|
+
});
|
510
|
+
}
|
511
|
+
function RecursiveTokens(_0) {
|
512
|
+
return __async(this, arguments, function* ({ reader }) {
|
513
|
+
const { done, value } = yield reader.read();
|
514
|
+
if (done) {
|
515
|
+
return null;
|
516
|
+
}
|
517
|
+
const text = new TextDecoder().decode(value);
|
518
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
|
519
|
+
text,
|
520
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react3.Suspense, { fallback: null, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(RecursiveTokens, { reader }) })
|
521
|
+
] });
|
522
|
+
});
|
523
|
+
}
|
447
524
|
// Annotate the CommonJS export names for ESM import in node:
|
448
525
|
0 && (module.exports = {
|
526
|
+
Tokens,
|
449
527
|
useChat,
|
450
528
|
useCompletion
|
451
529
|
});
|