ai 2.1.8 → 2.1.9

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 CHANGED
@@ -89,6 +89,8 @@ declare function streamToResponse(res: ReadableStream, response: ServerResponse,
89
89
 
90
90
  declare function HuggingFaceStream(res: AsyncGenerator<any>, callbacks?: AIStreamCallbacks): ReadableStream;
91
91
 
92
+ declare function CohereStream(res: AsyncGenerator<any>, callbacks?: AIStreamCallbacks): ReadableStream;
93
+
92
94
  declare function AnthropicStream(res: Response, cb?: AIStreamCallbacks): ReadableStream;
93
95
 
94
96
  declare function LangChainStream(callbacks?: AIStreamCallbacks): {
@@ -103,23 +105,23 @@ declare function LangChainStream(callbacks?: AIStreamCallbacks): {
103
105
  /**
104
106
  * Shared types between the API and UI packages.
105
107
  */
106
- declare type Message = {
108
+ type Message = {
107
109
  id: string;
108
110
  createdAt?: Date;
109
111
  content: string;
110
112
  role: 'system' | 'user' | 'assistant';
111
113
  };
112
- declare type CreateMessage = {
114
+ type CreateMessage = {
113
115
  id?: string;
114
116
  createdAt?: Date;
115
117
  content: string;
116
118
  role: 'system' | 'user' | 'assistant';
117
119
  };
118
- declare type RequestOptions = {
120
+ type RequestOptions = {
119
121
  headers?: Record<string, string> | Headers;
120
122
  body?: object;
121
123
  };
122
- declare type UseChatOptions = {
124
+ type UseChatOptions = {
123
125
  /**
124
126
  * The API endpoint that accepts a `{ messages: Message[] }` object and returns
125
127
  * a stream of tokens of the AI chat response. Defaults to `/api/chat`.
@@ -175,7 +177,7 @@ declare type UseChatOptions = {
175
177
  */
176
178
  sendExtraMessageFields?: boolean;
177
179
  };
178
- declare type UseCompletionOptions = {
180
+ type UseCompletionOptions = {
179
181
  /**
180
182
  * The API endpoint that accepts a `{ prompt: string }` object and returns
181
183
  * a stream of tokens of the AI completion response. Defaults to `/api/completion`.
@@ -226,4 +228,4 @@ declare type UseCompletionOptions = {
226
228
  body?: object;
227
229
  };
228
230
 
229
- export { AIStream, AIStreamCallbacks, AIStreamParser, AnthropicStream, CreateMessage, HuggingFaceStream, LangChainStream, Message, OpenAIStream, RequestOptions, StreamingTextResponse, UseChatOptions, UseCompletionOptions, createCallbacksTransformer, createEventStreamTransformer, streamToResponse, trimStartOfStreamHelper };
231
+ export { AIStream, AIStreamCallbacks, AIStreamParser, AnthropicStream, CohereStream, CreateMessage, HuggingFaceStream, LangChainStream, Message, OpenAIStream, RequestOptions, StreamingTextResponse, UseChatOptions, UseCompletionOptions, createCallbacksTransformer, createEventStreamTransformer, streamToResponse, trimStartOfStreamHelper };
package/dist/index.js CHANGED
@@ -59,6 +59,7 @@ 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,
@@ -231,6 +232,30 @@ function HuggingFaceStream(res, callbacks) {
231
232
  return createParser2(res).pipeThrough(createCallbacksTransformer(callbacks));
232
233
  }
233
234
 
235
+ // streams/cohere-stream.ts
236
+ function createParser3(res) {
237
+ return new ReadableStream({
238
+ pull(controller) {
239
+ return __async(this, null, function* () {
240
+ const { value, done } = yield res.next();
241
+ if (done) {
242
+ controller.close();
243
+ return;
244
+ }
245
+ const { text, is_finished } = JSON.parse(value);
246
+ if (is_finished === true) {
247
+ controller.close();
248
+ } else {
249
+ controller.enqueue(text);
250
+ }
251
+ });
252
+ }
253
+ });
254
+ }
255
+ function CohereStream(res, callbacks) {
256
+ return createParser3(res).pipeThrough(createCallbacksTransformer(callbacks));
257
+ }
258
+
234
259
  // streams/anthropic-stream.ts
235
260
  function parseAnthropicStream() {
236
261
  let previous = "";
@@ -272,6 +297,7 @@ function LangChainStream(callbacks) {
272
297
  0 && (module.exports = {
273
298
  AIStream,
274
299
  AnthropicStream,
300
+ CohereStream,
275
301
  HuggingFaceStream,
276
302
  LangChainStream,
277
303
  OpenAIStream,
package/dist/index.mjs CHANGED
@@ -201,6 +201,30 @@ function HuggingFaceStream(res, callbacks) {
201
201
  return createParser2(res).pipeThrough(createCallbacksTransformer(callbacks));
202
202
  }
203
203
 
204
+ // streams/cohere-stream.ts
205
+ function createParser3(res) {
206
+ return new ReadableStream({
207
+ pull(controller) {
208
+ return __async(this, null, function* () {
209
+ const { value, done } = yield res.next();
210
+ if (done) {
211
+ controller.close();
212
+ return;
213
+ }
214
+ const { text, is_finished } = JSON.parse(value);
215
+ if (is_finished === true) {
216
+ controller.close();
217
+ } else {
218
+ controller.enqueue(text);
219
+ }
220
+ });
221
+ }
222
+ });
223
+ }
224
+ function CohereStream(res, callbacks) {
225
+ return createParser3(res).pipeThrough(createCallbacksTransformer(callbacks));
226
+ }
227
+
204
228
  // streams/anthropic-stream.ts
205
229
  function parseAnthropicStream() {
206
230
  let previous = "";
@@ -241,6 +265,7 @@ function LangChainStream(callbacks) {
241
265
  export {
242
266
  AIStream,
243
267
  AnthropicStream,
268
+ CohereStream,
244
269
  HuggingFaceStream,
245
270
  LangChainStream,
246
271
  OpenAIStream,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "2.1.8",
3
+ "version": "2.1.9",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -54,18 +54,18 @@
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.0",
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
61
  "ts-jest": "29.0.3",
62
62
  "tsup": "^6.7.0",
63
- "typescript": "^4.5.3",
63
+ "typescript": "5.1.3",
64
64
  "@vercel/ai-tsconfig": "0.0.0",
65
65
  "eslint-config-vercel-ai": "0.0.0"
66
66
  },
67
67
  "peerDependencies": {
68
- "react": "^18.0.0",
68
+ "react": "^18.2.0",
69
69
  "svelte": "^3.29.0",
70
70
  "vue": "^3.3.4"
71
71
  },
@@ -1,23 +1,25 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+
1
3
  /**
2
4
  * Shared types between the API and UI packages.
3
5
  */
4
- declare type Message = {
6
+ type Message = {
5
7
  id: string;
6
8
  createdAt?: Date;
7
9
  content: string;
8
10
  role: 'system' | 'user' | 'assistant';
9
11
  };
10
- declare type CreateMessage = {
12
+ type CreateMessage = {
11
13
  id?: string;
12
14
  createdAt?: Date;
13
15
  content: string;
14
16
  role: 'system' | 'user' | 'assistant';
15
17
  };
16
- declare type RequestOptions = {
18
+ type RequestOptions = {
17
19
  headers?: Record<string, string> | Headers;
18
20
  body?: object;
19
21
  };
20
- declare type UseChatOptions = {
22
+ type UseChatOptions = {
21
23
  /**
22
24
  * The API endpoint that accepts a `{ messages: Message[] }` object and returns
23
25
  * a stream of tokens of the AI chat response. Defaults to `/api/chat`.
@@ -73,7 +75,7 @@ declare type UseChatOptions = {
73
75
  */
74
76
  sendExtraMessageFields?: boolean;
75
77
  };
76
- declare type UseCompletionOptions = {
78
+ type UseCompletionOptions = {
77
79
  /**
78
80
  * The API endpoint that accepts a `{ prompt: string }` object and returns
79
81
  * a stream of tokens of the AI completion response. Defaults to `/api/completion`.
@@ -124,7 +126,7 @@ declare type UseCompletionOptions = {
124
126
  body?: object;
125
127
  };
126
128
 
127
- declare type UseChatHelpers = {
129
+ type UseChatHelpers = {
128
130
  /** Current messages in the chat */
129
131
  messages: Message[];
130
132
  /** The error object of the API request */
@@ -165,7 +167,7 @@ declare type UseChatHelpers = {
165
167
  };
166
168
  declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, headers, body }?: UseChatOptions): UseChatHelpers;
167
169
 
168
- declare type UseCompletionHelpers = {
170
+ type UseCompletionHelpers = {
169
171
  /** The current completion result */
170
172
  completion: string;
171
173
  /**
@@ -209,4 +211,15 @@ declare type UseCompletionHelpers = {
209
211
  };
210
212
  declare function useCompletion({ api, id, initialCompletion, initialInput, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers;
211
213
 
212
- export { CreateMessage, Message, UseChatHelpers, UseChatOptions, UseCompletionHelpers, useChat, useCompletion };
214
+ type Props = {
215
+ /**
216
+ * A ReadableStream produced by the AI SDK.
217
+ */
218
+ stream: ReadableStream;
219
+ };
220
+ /**
221
+ * A React Server Component that recursively renders a stream of tokens.
222
+ */
223
+ declare function Tokens(props: Props): Promise<react_jsx_runtime.JSX.Element>;
224
+
225
+ export { CreateMessage, Message, Tokens, UseChatHelpers, UseChatOptions, UseCompletionHelpers, useChat, useCompletion };
@@ -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
  });
@@ -444,8 +445,33 @@ function useCompletion({
444
445
  isLoading: isMutating
445
446
  };
446
447
  }
448
+
449
+ // react/tokens.tsx
450
+ var import_react3 = require("react");
451
+ var import_jsx_runtime = require("react/jsx-runtime");
452
+ function Tokens(props) {
453
+ return __async(this, null, function* () {
454
+ const { stream } = props;
455
+ const reader = stream.getReader();
456
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react3.Suspense, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(RecursiveTokens, { reader }) });
457
+ });
458
+ }
459
+ function RecursiveTokens(_0) {
460
+ return __async(this, arguments, function* ({ reader }) {
461
+ const { done, value } = yield reader.read();
462
+ if (done) {
463
+ return null;
464
+ }
465
+ const text = new TextDecoder().decode(value);
466
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
467
+ text,
468
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react3.Suspense, { fallback: null, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(RecursiveTokens, { reader }) })
469
+ ] });
470
+ });
471
+ }
447
472
  // Annotate the CommonJS export names for ESM import in node:
448
473
  0 && (module.exports = {
474
+ Tokens,
449
475
  useChat,
450
476
  useCompletion
451
477
  });
@@ -410,7 +410,32 @@ function useCompletion({
410
410
  isLoading: isMutating
411
411
  };
412
412
  }
413
+
414
+ // react/tokens.tsx
415
+ import { Suspense } from "react";
416
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
417
+ function Tokens(props) {
418
+ return __async(this, null, function* () {
419
+ const { stream } = props;
420
+ const reader = stream.getReader();
421
+ return /* @__PURE__ */ jsx(Suspense, { children: /* @__PURE__ */ jsx(RecursiveTokens, { reader }) });
422
+ });
423
+ }
424
+ function RecursiveTokens(_0) {
425
+ return __async(this, arguments, function* ({ reader }) {
426
+ const { done, value } = yield reader.read();
427
+ if (done) {
428
+ return null;
429
+ }
430
+ const text = new TextDecoder().decode(value);
431
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
432
+ text,
433
+ /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(RecursiveTokens, { reader }) })
434
+ ] });
435
+ });
436
+ }
413
437
  export {
438
+ Tokens,
414
439
  useChat,
415
440
  useCompletion
416
441
  };
@@ -3,23 +3,23 @@ import { Readable, Writable } from 'svelte/store';
3
3
  /**
4
4
  * Shared types between the API and UI packages.
5
5
  */
6
- declare type Message = {
6
+ type Message = {
7
7
  id: string;
8
8
  createdAt?: Date;
9
9
  content: string;
10
10
  role: 'system' | 'user' | 'assistant';
11
11
  };
12
- declare type CreateMessage = {
12
+ type CreateMessage = {
13
13
  id?: string;
14
14
  createdAt?: Date;
15
15
  content: string;
16
16
  role: 'system' | 'user' | 'assistant';
17
17
  };
18
- declare type RequestOptions = {
18
+ type RequestOptions = {
19
19
  headers?: Record<string, string> | Headers;
20
20
  body?: object;
21
21
  };
22
- declare type UseChatOptions = {
22
+ type UseChatOptions = {
23
23
  /**
24
24
  * The API endpoint that accepts a `{ messages: Message[] }` object and returns
25
25
  * a stream of tokens of the AI chat response. Defaults to `/api/chat`.
@@ -75,7 +75,7 @@ declare type UseChatOptions = {
75
75
  */
76
76
  sendExtraMessageFields?: boolean;
77
77
  };
78
- declare type UseCompletionOptions = {
78
+ type UseCompletionOptions = {
79
79
  /**
80
80
  * The API endpoint that accepts a `{ prompt: string }` object and returns
81
81
  * a stream of tokens of the AI completion response. Defaults to `/api/completion`.
@@ -126,7 +126,7 @@ declare type UseCompletionOptions = {
126
126
  body?: object;
127
127
  };
128
128
 
129
- declare type UseChatHelpers = {
129
+ type UseChatHelpers = {
130
130
  /** Current messages in the chat */
131
131
  messages: Readable<Message[]>;
132
132
  /** The error object of the API request */
@@ -161,7 +161,7 @@ declare type UseChatHelpers = {
161
161
  };
162
162
  declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, headers, body }?: UseChatOptions): UseChatHelpers;
163
163
 
164
- declare type UseCompletionHelpers = {
164
+ type UseCompletionHelpers = {
165
165
  /** The current completion result */
166
166
  completion: Readable<string>;
167
167
  /** The error object of the API request */
@@ -3,23 +3,23 @@ import { Ref } from 'vue';
3
3
  /**
4
4
  * Shared types between the API and UI packages.
5
5
  */
6
- declare type Message = {
6
+ type Message = {
7
7
  id: string;
8
8
  createdAt?: Date;
9
9
  content: string;
10
10
  role: 'system' | 'user' | 'assistant';
11
11
  };
12
- declare type CreateMessage = {
12
+ type CreateMessage = {
13
13
  id?: string;
14
14
  createdAt?: Date;
15
15
  content: string;
16
16
  role: 'system' | 'user' | 'assistant';
17
17
  };
18
- declare type RequestOptions = {
18
+ type RequestOptions = {
19
19
  headers?: Record<string, string> | Headers;
20
20
  body?: object;
21
21
  };
22
- declare type UseChatOptions = {
22
+ type UseChatOptions = {
23
23
  /**
24
24
  * The API endpoint that accepts a `{ messages: Message[] }` object and returns
25
25
  * a stream of tokens of the AI chat response. Defaults to `/api/chat`.
@@ -75,7 +75,7 @@ declare type UseChatOptions = {
75
75
  */
76
76
  sendExtraMessageFields?: boolean;
77
77
  };
78
- declare type UseCompletionOptions = {
78
+ type UseCompletionOptions = {
79
79
  /**
80
80
  * The API endpoint that accepts a `{ prompt: string }` object and returns
81
81
  * a stream of tokens of the AI completion response. Defaults to `/api/completion`.
@@ -126,7 +126,7 @@ declare type UseCompletionOptions = {
126
126
  body?: object;
127
127
  };
128
128
 
129
- declare type UseChatHelpers = {
129
+ type UseChatHelpers = {
130
130
  /** Current messages in the chat */
131
131
  messages: Ref<Message[]>;
132
132
  /** The error object of the API request */
@@ -161,7 +161,7 @@ declare type UseChatHelpers = {
161
161
  };
162
162
  declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, headers, body }?: UseChatOptions): UseChatHelpers;
163
163
 
164
- declare type UseCompletionHelpers = {
164
+ type UseCompletionHelpers = {
165
165
  /** The current completion result */
166
166
  completion: Ref<string>;
167
167
  /** The error object of the API request */