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/dist/index.d.ts
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
import { ServerResponse } from 'node:http';
|
2
|
+
import { ChatCompletionRequestMessageFunctionCall, CreateChatCompletionRequestFunctionCall } from 'openai-edge';
|
3
|
+
import { ChatCompletionFunctions } from 'openai-edge/types/api';
|
2
4
|
|
3
5
|
/**
|
4
6
|
* Helper callback methods for AIStream stream lifecycle events
|
@@ -89,6 +91,8 @@ declare function streamToResponse(res: ReadableStream, response: ServerResponse,
|
|
89
91
|
|
90
92
|
declare function HuggingFaceStream(res: AsyncGenerator<any>, callbacks?: AIStreamCallbacks): ReadableStream;
|
91
93
|
|
94
|
+
declare function CohereStream(res: AsyncGenerator<any>, callbacks?: AIStreamCallbacks): ReadableStream;
|
95
|
+
|
92
96
|
declare function AnthropicStream(res: Response, cb?: AIStreamCallbacks): ReadableStream;
|
93
97
|
|
94
98
|
declare function LangChainStream(callbacks?: AIStreamCallbacks): {
|
@@ -103,30 +107,50 @@ declare function LangChainStream(callbacks?: AIStreamCallbacks): {
|
|
103
107
|
/**
|
104
108
|
* Shared types between the API and UI packages.
|
105
109
|
*/
|
106
|
-
|
110
|
+
type Message = {
|
107
111
|
id: string;
|
108
112
|
createdAt?: Date;
|
109
113
|
content: string;
|
110
|
-
role: 'system' | 'user' | 'assistant';
|
114
|
+
role: 'system' | 'user' | 'assistant' | 'function';
|
115
|
+
/**
|
116
|
+
* If the message has a role of `function`, the `name` field is the name of the function.
|
117
|
+
* Otherwise, the name field should not be set.
|
118
|
+
*/
|
119
|
+
name?: string;
|
120
|
+
/**
|
121
|
+
* If the assistant role makes a function call, the `function_call` field
|
122
|
+
* contains the function call name and arguments. Otherwise, the field should
|
123
|
+
* not be set.
|
124
|
+
*/
|
125
|
+
function_call?: string | ChatCompletionRequestMessageFunctionCall;
|
111
126
|
};
|
112
|
-
|
113
|
-
id?:
|
114
|
-
|
115
|
-
|
116
|
-
|
127
|
+
type CreateMessage = Omit<Message, 'id'> & {
|
128
|
+
id?: Message['id'];
|
129
|
+
};
|
130
|
+
type ChatRequest = {
|
131
|
+
messages: Message[];
|
132
|
+
options?: RequestOptions;
|
133
|
+
functions?: Array<ChatCompletionFunctions>;
|
134
|
+
function_call?: CreateChatCompletionRequestFunctionCall;
|
117
135
|
};
|
118
|
-
|
136
|
+
type FunctionCallHandler = (chatMessages: Message[], functionCall: ChatCompletionRequestMessageFunctionCall) => Promise<ChatRequest | void>;
|
137
|
+
type RequestOptions = {
|
119
138
|
headers?: Record<string, string> | Headers;
|
120
139
|
body?: object;
|
121
140
|
};
|
122
|
-
|
141
|
+
type ChatRequestOptions = {
|
142
|
+
options?: RequestOptions;
|
143
|
+
functions?: Array<ChatCompletionFunctions>;
|
144
|
+
function_call?: CreateChatCompletionRequestFunctionCall;
|
145
|
+
};
|
146
|
+
type UseChatOptions = {
|
123
147
|
/**
|
124
148
|
* The API endpoint that accepts a `{ messages: Message[] }` object and returns
|
125
149
|
* a stream of tokens of the AI chat response. Defaults to `/api/chat`.
|
126
150
|
*/
|
127
151
|
api?: string;
|
128
152
|
/**
|
129
|
-
*
|
153
|
+
* A unique identifier for the chat. If not provided, a random one will be
|
130
154
|
* generated. When provided, the `useChat` hook with the same `id` will
|
131
155
|
* have shared states across components.
|
132
156
|
*/
|
@@ -139,6 +163,12 @@ declare type UseChatOptions = {
|
|
139
163
|
* Initial input of the chat.
|
140
164
|
*/
|
141
165
|
initialInput?: string;
|
166
|
+
/**
|
167
|
+
* Callback function to be called when a function call is received.
|
168
|
+
* If the function returns a `ChatRequest` object, the request will be sent
|
169
|
+
* automatically to the API and will be used to update the chat.
|
170
|
+
*/
|
171
|
+
experimental_onFunctionCall?: FunctionCallHandler;
|
142
172
|
/**
|
143
173
|
* Callback function to be called when the API response is received.
|
144
174
|
*/
|
@@ -151,6 +181,12 @@ declare type UseChatOptions = {
|
|
151
181
|
* Callback function to be called when an error is encountered.
|
152
182
|
*/
|
153
183
|
onError?: (error: Error) => void;
|
184
|
+
/**
|
185
|
+
* The credentials mode to be used for the fetch request.
|
186
|
+
* Possible values are: 'omit', 'same-origin', 'include'.
|
187
|
+
* Defaults to 'same-origin'.
|
188
|
+
*/
|
189
|
+
credentials?: RequestCredentials;
|
154
190
|
/**
|
155
191
|
* HTTP headers to be sent with the API request.
|
156
192
|
*/
|
@@ -175,7 +211,7 @@ declare type UseChatOptions = {
|
|
175
211
|
*/
|
176
212
|
sendExtraMessageFields?: boolean;
|
177
213
|
};
|
178
|
-
|
214
|
+
type UseCompletionOptions = {
|
179
215
|
/**
|
180
216
|
* The API endpoint that accepts a `{ prompt: string }` object and returns
|
181
217
|
* a stream of tokens of the AI completion response. Defaults to `/api/completion`.
|
@@ -207,6 +243,12 @@ declare type UseCompletionOptions = {
|
|
207
243
|
* Callback function to be called when an error is encountered.
|
208
244
|
*/
|
209
245
|
onError?: (error: Error) => void;
|
246
|
+
/**
|
247
|
+
* The credentials mode to be used for the fetch request.
|
248
|
+
* Possible values are: 'omit', 'same-origin', 'include'.
|
249
|
+
* Defaults to 'same-origin'.
|
250
|
+
*/
|
251
|
+
credentials?: RequestCredentials;
|
210
252
|
/**
|
211
253
|
* HTTP headers to be sent with the API request.
|
212
254
|
*/
|
@@ -226,4 +268,7 @@ declare type UseCompletionOptions = {
|
|
226
268
|
body?: object;
|
227
269
|
};
|
228
270
|
|
229
|
-
|
271
|
+
declare const nanoid: (size?: number | undefined) => string;
|
272
|
+
declare function createChunkDecoder(): (chunk: Uint8Array | undefined) => string;
|
273
|
+
|
274
|
+
export { AIStream, AIStreamCallbacks, AIStreamParser, AnthropicStream, ChatRequest, ChatRequestOptions, CohereStream, CreateMessage, FunctionCallHandler, HuggingFaceStream, LangChainStream, Message, OpenAIStream, RequestOptions, StreamingTextResponse, UseChatOptions, UseCompletionOptions, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, nanoid, streamToResponse, trimStartOfStreamHelper };
|
package/dist/index.js
CHANGED
@@ -59,12 +59,15 @@ var streams_exports = {};
|
|
59
59
|
__export(streams_exports, {
|
60
60
|
AIStream: () => AIStream,
|
61
61
|
AnthropicStream: () => AnthropicStream,
|
62
|
+
CohereStream: () => CohereStream,
|
62
63
|
HuggingFaceStream: () => HuggingFaceStream,
|
63
64
|
LangChainStream: () => LangChainStream,
|
64
65
|
OpenAIStream: () => OpenAIStream,
|
65
66
|
StreamingTextResponse: () => StreamingTextResponse,
|
66
67
|
createCallbacksTransformer: () => createCallbacksTransformer,
|
68
|
+
createChunkDecoder: () => createChunkDecoder,
|
67
69
|
createEventStreamTransformer: () => createEventStreamTransformer,
|
70
|
+
nanoid: () => nanoid,
|
68
71
|
streamToResponse: () => streamToResponse,
|
69
72
|
trimStartOfStreamHelper: () => trimStartOfStreamHelper
|
70
73
|
});
|
@@ -158,10 +161,19 @@ function createEmptyReadableStream() {
|
|
158
161
|
function parseOpenAIStream() {
|
159
162
|
const trimStartOfStream = trimStartOfStreamHelper();
|
160
163
|
return (data) => {
|
161
|
-
var _a, _b, _c, _d, _e;
|
164
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
162
165
|
const json = JSON.parse(data);
|
166
|
+
if ((_c = (_b = (_a = json.choices[0]) == null ? void 0 : _a.delta) == null ? void 0 : _b.function_call) == null ? void 0 : _c.name) {
|
167
|
+
return `{"function_call": {"name": "${(_e = (_d = json.choices[0]) == null ? void 0 : _d.delta) == null ? void 0 : _e.function_call.name}", "arguments": "`;
|
168
|
+
} else if ((_h = (_g = (_f = json.choices[0]) == null ? void 0 : _f.delta) == null ? void 0 : _g.function_call) == null ? void 0 : _h.arguments) {
|
169
|
+
const argumentChunk = json.choices[0].delta.function_call.arguments;
|
170
|
+
let escapedPartialJson = argumentChunk.replace(/\\/g, "\\\\").replace(/\//g, "\\/").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t").replace(/\f/g, "\\f");
|
171
|
+
return `${escapedPartialJson}`;
|
172
|
+
} else if (((_i = json.choices[0]) == null ? void 0 : _i.finish_reason) === "function_call") {
|
173
|
+
return '"}}';
|
174
|
+
}
|
163
175
|
const text = trimStartOfStream(
|
164
|
-
(
|
176
|
+
(_n = (_m = (_k = (_j = json.choices[0]) == null ? void 0 : _j.delta) == null ? void 0 : _k.content) != null ? _m : (_l = json.choices[0]) == null ? void 0 : _l.text) != null ? _n : ""
|
165
177
|
);
|
166
178
|
return text;
|
167
179
|
};
|
@@ -231,6 +243,30 @@ function HuggingFaceStream(res, callbacks) {
|
|
231
243
|
return createParser2(res).pipeThrough(createCallbacksTransformer(callbacks));
|
232
244
|
}
|
233
245
|
|
246
|
+
// streams/cohere-stream.ts
|
247
|
+
function createParser3(res) {
|
248
|
+
return new ReadableStream({
|
249
|
+
pull(controller) {
|
250
|
+
return __async(this, null, function* () {
|
251
|
+
const { value, done } = yield res.next();
|
252
|
+
if (done) {
|
253
|
+
controller.close();
|
254
|
+
return;
|
255
|
+
}
|
256
|
+
const { text, is_finished } = JSON.parse(value);
|
257
|
+
if (is_finished === true) {
|
258
|
+
controller.close();
|
259
|
+
} else {
|
260
|
+
controller.enqueue(text);
|
261
|
+
}
|
262
|
+
});
|
263
|
+
}
|
264
|
+
});
|
265
|
+
}
|
266
|
+
function CohereStream(res, callbacks) {
|
267
|
+
return createParser3(res).pipeThrough(createCallbacksTransformer(callbacks));
|
268
|
+
}
|
269
|
+
|
234
270
|
// streams/anthropic-stream.ts
|
235
271
|
function parseAnthropicStream() {
|
236
272
|
let previous = "";
|
@@ -268,16 +304,34 @@ function LangChainStream(callbacks) {
|
|
268
304
|
}
|
269
305
|
};
|
270
306
|
}
|
307
|
+
|
308
|
+
// shared/utils.ts
|
309
|
+
var import_nanoid = require("nanoid");
|
310
|
+
var nanoid = (0, import_nanoid.customAlphabet)(
|
311
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
312
|
+
7
|
313
|
+
);
|
314
|
+
function createChunkDecoder() {
|
315
|
+
const decoder = new TextDecoder();
|
316
|
+
return function(chunk) {
|
317
|
+
if (!chunk)
|
318
|
+
return "";
|
319
|
+
return decoder.decode(chunk, { stream: true });
|
320
|
+
};
|
321
|
+
}
|
271
322
|
// Annotate the CommonJS export names for ESM import in node:
|
272
323
|
0 && (module.exports = {
|
273
324
|
AIStream,
|
274
325
|
AnthropicStream,
|
326
|
+
CohereStream,
|
275
327
|
HuggingFaceStream,
|
276
328
|
LangChainStream,
|
277
329
|
OpenAIStream,
|
278
330
|
StreamingTextResponse,
|
279
331
|
createCallbacksTransformer,
|
332
|
+
createChunkDecoder,
|
280
333
|
createEventStreamTransformer,
|
334
|
+
nanoid,
|
281
335
|
streamToResponse,
|
282
336
|
trimStartOfStreamHelper
|
283
337
|
});
|
package/dist/index.mjs
CHANGED
@@ -128,10 +128,19 @@ function createEmptyReadableStream() {
|
|
128
128
|
function parseOpenAIStream() {
|
129
129
|
const trimStartOfStream = trimStartOfStreamHelper();
|
130
130
|
return (data) => {
|
131
|
-
var _a, _b, _c, _d, _e;
|
131
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
132
132
|
const json = JSON.parse(data);
|
133
|
+
if ((_c = (_b = (_a = json.choices[0]) == null ? void 0 : _a.delta) == null ? void 0 : _b.function_call) == null ? void 0 : _c.name) {
|
134
|
+
return `{"function_call": {"name": "${(_e = (_d = json.choices[0]) == null ? void 0 : _d.delta) == null ? void 0 : _e.function_call.name}", "arguments": "`;
|
135
|
+
} else if ((_h = (_g = (_f = json.choices[0]) == null ? void 0 : _f.delta) == null ? void 0 : _g.function_call) == null ? void 0 : _h.arguments) {
|
136
|
+
const argumentChunk = json.choices[0].delta.function_call.arguments;
|
137
|
+
let escapedPartialJson = argumentChunk.replace(/\\/g, "\\\\").replace(/\//g, "\\/").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t").replace(/\f/g, "\\f");
|
138
|
+
return `${escapedPartialJson}`;
|
139
|
+
} else if (((_i = json.choices[0]) == null ? void 0 : _i.finish_reason) === "function_call") {
|
140
|
+
return '"}}';
|
141
|
+
}
|
133
142
|
const text = trimStartOfStream(
|
134
|
-
(
|
143
|
+
(_n = (_m = (_k = (_j = json.choices[0]) == null ? void 0 : _j.delta) == null ? void 0 : _k.content) != null ? _m : (_l = json.choices[0]) == null ? void 0 : _l.text) != null ? _n : ""
|
135
144
|
);
|
136
145
|
return text;
|
137
146
|
};
|
@@ -201,6 +210,30 @@ function HuggingFaceStream(res, callbacks) {
|
|
201
210
|
return createParser2(res).pipeThrough(createCallbacksTransformer(callbacks));
|
202
211
|
}
|
203
212
|
|
213
|
+
// streams/cohere-stream.ts
|
214
|
+
function createParser3(res) {
|
215
|
+
return new ReadableStream({
|
216
|
+
pull(controller) {
|
217
|
+
return __async(this, null, function* () {
|
218
|
+
const { value, done } = yield res.next();
|
219
|
+
if (done) {
|
220
|
+
controller.close();
|
221
|
+
return;
|
222
|
+
}
|
223
|
+
const { text, is_finished } = JSON.parse(value);
|
224
|
+
if (is_finished === true) {
|
225
|
+
controller.close();
|
226
|
+
} else {
|
227
|
+
controller.enqueue(text);
|
228
|
+
}
|
229
|
+
});
|
230
|
+
}
|
231
|
+
});
|
232
|
+
}
|
233
|
+
function CohereStream(res, callbacks) {
|
234
|
+
return createParser3(res).pipeThrough(createCallbacksTransformer(callbacks));
|
235
|
+
}
|
236
|
+
|
204
237
|
// streams/anthropic-stream.ts
|
205
238
|
function parseAnthropicStream() {
|
206
239
|
let previous = "";
|
@@ -238,15 +271,33 @@ function LangChainStream(callbacks) {
|
|
238
271
|
}
|
239
272
|
};
|
240
273
|
}
|
274
|
+
|
275
|
+
// shared/utils.ts
|
276
|
+
import { customAlphabet } from "nanoid";
|
277
|
+
var nanoid = customAlphabet(
|
278
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
279
|
+
7
|
280
|
+
);
|
281
|
+
function createChunkDecoder() {
|
282
|
+
const decoder = new TextDecoder();
|
283
|
+
return function(chunk) {
|
284
|
+
if (!chunk)
|
285
|
+
return "";
|
286
|
+
return decoder.decode(chunk, { stream: true });
|
287
|
+
};
|
288
|
+
}
|
241
289
|
export {
|
242
290
|
AIStream,
|
243
291
|
AnthropicStream,
|
292
|
+
CohereStream,
|
244
293
|
HuggingFaceStream,
|
245
294
|
LangChainStream,
|
246
295
|
OpenAIStream,
|
247
296
|
StreamingTextResponse,
|
248
297
|
createCallbacksTransformer,
|
298
|
+
createChunkDecoder,
|
249
299
|
createEventStreamTransformer,
|
300
|
+
nanoid,
|
250
301
|
streamToResponse,
|
251
302
|
trimStartOfStreamHelper
|
252
303
|
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "ai",
|
3
|
-
"version": "2.1.
|
3
|
+
"version": "2.1.10",
|
4
4
|
"license": "Apache-2.0",
|
5
5
|
"sideEffects": false,
|
6
6
|
"main": "./dist/index.js",
|
@@ -45,28 +45,29 @@
|
|
45
45
|
},
|
46
46
|
"dependencies": {
|
47
47
|
"eventsource-parser": "1.0.0",
|
48
|
+
"swr": "2.1.5",
|
48
49
|
"nanoid": "^3.3.6",
|
49
50
|
"sswr": "^1.10.0",
|
50
|
-
"swr": "2.1.5",
|
51
51
|
"swrv": "1.0.3"
|
52
52
|
},
|
53
53
|
"devDependencies": {
|
54
54
|
"@edge-runtime/jest-environment": "1.1.0-beta.31",
|
55
55
|
"@types/jest": "29.2.0",
|
56
56
|
"@types/node": "^17.0.12",
|
57
|
-
"@types/react": "^18.2.
|
57
|
+
"@types/react": "^18.2.8",
|
58
58
|
"@types/react-dom": "^18.2.0",
|
59
59
|
"eslint": "^7.32.0",
|
60
60
|
"jest": "29.2.1",
|
61
|
+
"openai-edge": "^1.1.0",
|
61
62
|
"ts-jest": "29.0.3",
|
62
63
|
"tsup": "^6.7.0",
|
63
|
-
"typescript": "
|
64
|
-
"
|
65
|
-
"
|
64
|
+
"typescript": "5.1.3",
|
65
|
+
"eslint-config-vercel-ai": "0.0.0",
|
66
|
+
"@vercel/ai-tsconfig": "0.0.0"
|
66
67
|
},
|
67
68
|
"peerDependencies": {
|
68
|
-
"react": "^18.
|
69
|
-
"svelte": "^
|
69
|
+
"react": "^18.2.0",
|
70
|
+
"svelte": "^4.0.0",
|
70
71
|
"vue": "^3.3.4"
|
71
72
|
},
|
72
73
|
"peerDependenciesMeta": {
|
package/react/dist/index.d.ts
CHANGED
@@ -1,30 +1,54 @@
|
|
1
|
+
import { ChatCompletionRequestMessageFunctionCall, CreateChatCompletionRequestFunctionCall } from 'openai-edge';
|
2
|
+
import { ChatCompletionFunctions } from 'openai-edge/types/api';
|
3
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
4
|
+
|
1
5
|
/**
|
2
6
|
* Shared types between the API and UI packages.
|
3
7
|
*/
|
4
|
-
|
8
|
+
type Message = {
|
5
9
|
id: string;
|
6
10
|
createdAt?: Date;
|
7
11
|
content: string;
|
8
|
-
role: 'system' | 'user' | 'assistant';
|
12
|
+
role: 'system' | 'user' | 'assistant' | 'function';
|
13
|
+
/**
|
14
|
+
* If the message has a role of `function`, the `name` field is the name of the function.
|
15
|
+
* Otherwise, the name field should not be set.
|
16
|
+
*/
|
17
|
+
name?: string;
|
18
|
+
/**
|
19
|
+
* If the assistant role makes a function call, the `function_call` field
|
20
|
+
* contains the function call name and arguments. Otherwise, the field should
|
21
|
+
* not be set.
|
22
|
+
*/
|
23
|
+
function_call?: string | ChatCompletionRequestMessageFunctionCall;
|
9
24
|
};
|
10
|
-
|
11
|
-
id?:
|
12
|
-
|
13
|
-
|
14
|
-
|
25
|
+
type CreateMessage = Omit<Message, 'id'> & {
|
26
|
+
id?: Message['id'];
|
27
|
+
};
|
28
|
+
type ChatRequest = {
|
29
|
+
messages: Message[];
|
30
|
+
options?: RequestOptions;
|
31
|
+
functions?: Array<ChatCompletionFunctions>;
|
32
|
+
function_call?: CreateChatCompletionRequestFunctionCall;
|
15
33
|
};
|
16
|
-
|
34
|
+
type FunctionCallHandler = (chatMessages: Message[], functionCall: ChatCompletionRequestMessageFunctionCall) => Promise<ChatRequest | void>;
|
35
|
+
type RequestOptions = {
|
17
36
|
headers?: Record<string, string> | Headers;
|
18
37
|
body?: object;
|
19
38
|
};
|
20
|
-
|
39
|
+
type ChatRequestOptions = {
|
40
|
+
options?: RequestOptions;
|
41
|
+
functions?: Array<ChatCompletionFunctions>;
|
42
|
+
function_call?: CreateChatCompletionRequestFunctionCall;
|
43
|
+
};
|
44
|
+
type UseChatOptions = {
|
21
45
|
/**
|
22
46
|
* The API endpoint that accepts a `{ messages: Message[] }` object and returns
|
23
47
|
* a stream of tokens of the AI chat response. Defaults to `/api/chat`.
|
24
48
|
*/
|
25
49
|
api?: string;
|
26
50
|
/**
|
27
|
-
*
|
51
|
+
* A unique identifier for the chat. If not provided, a random one will be
|
28
52
|
* generated. When provided, the `useChat` hook with the same `id` will
|
29
53
|
* have shared states across components.
|
30
54
|
*/
|
@@ -37,6 +61,12 @@ declare type UseChatOptions = {
|
|
37
61
|
* Initial input of the chat.
|
38
62
|
*/
|
39
63
|
initialInput?: string;
|
64
|
+
/**
|
65
|
+
* Callback function to be called when a function call is received.
|
66
|
+
* If the function returns a `ChatRequest` object, the request will be sent
|
67
|
+
* automatically to the API and will be used to update the chat.
|
68
|
+
*/
|
69
|
+
experimental_onFunctionCall?: FunctionCallHandler;
|
40
70
|
/**
|
41
71
|
* Callback function to be called when the API response is received.
|
42
72
|
*/
|
@@ -49,6 +79,12 @@ declare type UseChatOptions = {
|
|
49
79
|
* Callback function to be called when an error is encountered.
|
50
80
|
*/
|
51
81
|
onError?: (error: Error) => void;
|
82
|
+
/**
|
83
|
+
* The credentials mode to be used for the fetch request.
|
84
|
+
* Possible values are: 'omit', 'same-origin', 'include'.
|
85
|
+
* Defaults to 'same-origin'.
|
86
|
+
*/
|
87
|
+
credentials?: RequestCredentials;
|
52
88
|
/**
|
53
89
|
* HTTP headers to be sent with the API request.
|
54
90
|
*/
|
@@ -73,7 +109,7 @@ declare type UseChatOptions = {
|
|
73
109
|
*/
|
74
110
|
sendExtraMessageFields?: boolean;
|
75
111
|
};
|
76
|
-
|
112
|
+
type UseCompletionOptions = {
|
77
113
|
/**
|
78
114
|
* The API endpoint that accepts a `{ prompt: string }` object and returns
|
79
115
|
* a stream of tokens of the AI completion response. Defaults to `/api/completion`.
|
@@ -105,6 +141,12 @@ declare type UseCompletionOptions = {
|
|
105
141
|
* Callback function to be called when an error is encountered.
|
106
142
|
*/
|
107
143
|
onError?: (error: Error) => void;
|
144
|
+
/**
|
145
|
+
* The credentials mode to be used for the fetch request.
|
146
|
+
* Possible values are: 'omit', 'same-origin', 'include'.
|
147
|
+
* Defaults to 'same-origin'.
|
148
|
+
*/
|
149
|
+
credentials?: RequestCredentials;
|
108
150
|
/**
|
109
151
|
* HTTP headers to be sent with the API request.
|
110
152
|
*/
|
@@ -124,7 +166,7 @@ declare type UseCompletionOptions = {
|
|
124
166
|
body?: object;
|
125
167
|
};
|
126
168
|
|
127
|
-
|
169
|
+
type UseChatHelpers = {
|
128
170
|
/** Current messages in the chat */
|
129
171
|
messages: Message[];
|
130
172
|
/** The error object of the API request */
|
@@ -135,13 +177,13 @@ declare type UseChatHelpers = {
|
|
135
177
|
* @param message The message to append
|
136
178
|
* @param options Additional options to pass to the API call
|
137
179
|
*/
|
138
|
-
append: (message: Message | CreateMessage,
|
180
|
+
append: (message: Message | CreateMessage, chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;
|
139
181
|
/**
|
140
182
|
* Reload the last AI chat response for the given chat history. If the last
|
141
183
|
* message isn't from the assistant, it will request the API to generate a
|
142
184
|
* new response.
|
143
185
|
*/
|
144
|
-
reload: (
|
186
|
+
reload: (chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;
|
145
187
|
/**
|
146
188
|
* Abort the current request immediately, keep the generated tokens if any.
|
147
189
|
*/
|
@@ -159,13 +201,14 @@ declare type UseChatHelpers = {
|
|
159
201
|
/** An input/textarea-ready onChange handler to control the value of the input */
|
160
202
|
handleInputChange: (e: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
|
161
203
|
/** Form submission handler to automattically reset input and append a user message */
|
162
|
-
handleSubmit: (e: React.FormEvent<HTMLFormElement
|
204
|
+
handleSubmit: (e: React.FormEvent<HTMLFormElement>, chatRequestOptions?: ChatRequestOptions) => void;
|
205
|
+
metadata?: Object;
|
163
206
|
/** Whether the API request is in progress */
|
164
207
|
isLoading: boolean;
|
165
208
|
};
|
166
|
-
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, headers, body }?: UseChatOptions): UseChatHelpers;
|
209
|
+
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, onResponse, onFinish, onError, credentials, headers, body }?: UseChatOptions): UseChatHelpers;
|
167
210
|
|
168
|
-
|
211
|
+
type UseCompletionHelpers = {
|
169
212
|
/** The current completion result */
|
170
213
|
completion: string;
|
171
214
|
/**
|
@@ -207,6 +250,17 @@ declare type UseCompletionHelpers = {
|
|
207
250
|
/** Whether the API request is in progress */
|
208
251
|
isLoading: boolean;
|
209
252
|
};
|
210
|
-
declare function useCompletion({ api, id, initialCompletion, initialInput, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers;
|
253
|
+
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers;
|
254
|
+
|
255
|
+
type Props = {
|
256
|
+
/**
|
257
|
+
* A ReadableStream produced by the AI SDK.
|
258
|
+
*/
|
259
|
+
stream: ReadableStream;
|
260
|
+
};
|
261
|
+
/**
|
262
|
+
* A React Server Component that recursively renders a stream of tokens.
|
263
|
+
*/
|
264
|
+
declare function Tokens(props: Props): Promise<react_jsx_runtime.JSX.Element>;
|
211
265
|
|
212
|
-
export { CreateMessage, Message, UseChatHelpers, UseChatOptions, UseCompletionHelpers, useChat, useCompletion };
|
266
|
+
export { CreateMessage, Message, Tokens, UseChatHelpers, UseChatOptions, UseCompletionHelpers, useChat, useCompletion };
|