ai 2.1.10 → 2.1.12

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/README.md CHANGED
@@ -5,8 +5,8 @@ The Vercel AI SDK is **a library for building edge-ready AI-powered streaming te
5
5
  ## Features
6
6
 
7
7
  - [SWR](https://swr.vercel.app)-powered React, Svelte and Vue helpers for streaming text responses and building chat and completion UIs
8
- - First-class support for [LangChain](js.langchain.com/docs) and [OpenAI](https://openai.com), [Anthropic](https://www.anthropic.com), and [HuggingFace](https://huggingface.co)
9
- - [Edge Runtime](https://edge-runtime.vercel.app/) compatibility
8
+ - First-class support for [LangChain](js.langchain.com/docs) and [OpenAI](https://openai.com), [Anthropic](https://www.anthropic.com), and [Hugging Face](https://huggingface.co)
9
+ - Node.js, Serverless, and [Edge Runtime](https://edge-runtime.vercel.app/) support
10
10
  - Callbacks for saving completed streaming responses to a database (in the same request)
11
11
 
12
12
  ## Installation
package/dist/index.d.ts CHANGED
@@ -99,8 +99,15 @@ declare function LangChainStream(callbacks?: AIStreamCallbacks): {
99
99
  stream: ReadableStream<Uint8Array>;
100
100
  handlers: {
101
101
  handleLLMNewToken: (token: string) => Promise<void>;
102
- handleChainEnd: () => Promise<void>;
103
- handleLLMError: (e: any) => Promise<void>;
102
+ handleLLMStart: (_llm: any, _prompts: string[], runId: string) => Promise<void>;
103
+ handleLLMEnd: (_output: any, runId: string) => Promise<void>;
104
+ handleLLMError: (e: Error, runId: string) => Promise<void>;
105
+ handleChainStart: (_chain: any, _inputs: any, runId: string) => Promise<void>;
106
+ handleChainEnd: (_outputs: any, runId: string) => Promise<void>;
107
+ handleChainError: (e: Error, runId: string) => Promise<void>;
108
+ handleToolStart: (_tool: any, _input: string, runId: string) => Promise<void>;
109
+ handleToolEnd: (_output: string, runId: string) => Promise<void>;
110
+ handleToolError: (e: Error, runId: string) => Promise<void>;
104
111
  };
105
112
  };
106
113
 
package/dist/index.js CHANGED
@@ -286,6 +286,22 @@ function AnthropicStream(res, cb) {
286
286
  function LangChainStream(callbacks) {
287
287
  const stream = new TransformStream();
288
288
  const writer = stream.writable.getWriter();
289
+ const runs = /* @__PURE__ */ new Set();
290
+ const handleError = (e, runId) => __async(this, null, function* () {
291
+ runs.delete(runId);
292
+ yield writer.ready;
293
+ yield writer.abort(e);
294
+ });
295
+ const handleStart = (runId) => __async(this, null, function* () {
296
+ runs.add(runId);
297
+ });
298
+ const handleEnd = (runId) => __async(this, null, function* () {
299
+ runs.delete(runId);
300
+ if (runs.size === 0) {
301
+ yield writer.ready;
302
+ yield writer.close();
303
+ }
304
+ });
289
305
  return {
290
306
  stream: stream.readable.pipeThrough(createCallbacksTransformer(callbacks)),
291
307
  handlers: {
@@ -293,13 +309,32 @@ function LangChainStream(callbacks) {
293
309
  yield writer.ready;
294
310
  yield writer.write(token);
295
311
  }),
296
- handleChainEnd: () => __async(this, null, function* () {
297
- yield writer.ready;
298
- yield writer.close();
312
+ handleLLMStart: (_llm, _prompts, runId) => __async(this, null, function* () {
313
+ handleStart(runId);
299
314
  }),
300
- handleLLMError: (e) => __async(this, null, function* () {
301
- yield writer.ready;
302
- yield writer.abort(e);
315
+ handleLLMEnd: (_output, runId) => __async(this, null, function* () {
316
+ yield handleEnd(runId);
317
+ }),
318
+ handleLLMError: (e, runId) => __async(this, null, function* () {
319
+ yield handleError(e, runId);
320
+ }),
321
+ handleChainStart: (_chain, _inputs, runId) => __async(this, null, function* () {
322
+ handleStart(runId);
323
+ }),
324
+ handleChainEnd: (_outputs, runId) => __async(this, null, function* () {
325
+ yield handleEnd(runId);
326
+ }),
327
+ handleChainError: (e, runId) => __async(this, null, function* () {
328
+ yield handleError(e, runId);
329
+ }),
330
+ handleToolStart: (_tool, _input, runId) => __async(this, null, function* () {
331
+ handleStart(runId);
332
+ }),
333
+ handleToolEnd: (_output, runId) => __async(this, null, function* () {
334
+ yield handleEnd(runId);
335
+ }),
336
+ handleToolError: (e, runId) => __async(this, null, function* () {
337
+ yield handleError(e, runId);
303
338
  })
304
339
  }
305
340
  };
package/dist/index.mjs CHANGED
@@ -253,6 +253,22 @@ function AnthropicStream(res, cb) {
253
253
  function LangChainStream(callbacks) {
254
254
  const stream = new TransformStream();
255
255
  const writer = stream.writable.getWriter();
256
+ const runs = /* @__PURE__ */ new Set();
257
+ const handleError = (e, runId) => __async(this, null, function* () {
258
+ runs.delete(runId);
259
+ yield writer.ready;
260
+ yield writer.abort(e);
261
+ });
262
+ const handleStart = (runId) => __async(this, null, function* () {
263
+ runs.add(runId);
264
+ });
265
+ const handleEnd = (runId) => __async(this, null, function* () {
266
+ runs.delete(runId);
267
+ if (runs.size === 0) {
268
+ yield writer.ready;
269
+ yield writer.close();
270
+ }
271
+ });
256
272
  return {
257
273
  stream: stream.readable.pipeThrough(createCallbacksTransformer(callbacks)),
258
274
  handlers: {
@@ -260,13 +276,32 @@ function LangChainStream(callbacks) {
260
276
  yield writer.ready;
261
277
  yield writer.write(token);
262
278
  }),
263
- handleChainEnd: () => __async(this, null, function* () {
264
- yield writer.ready;
265
- yield writer.close();
279
+ handleLLMStart: (_llm, _prompts, runId) => __async(this, null, function* () {
280
+ handleStart(runId);
266
281
  }),
267
- handleLLMError: (e) => __async(this, null, function* () {
268
- yield writer.ready;
269
- yield writer.abort(e);
282
+ handleLLMEnd: (_output, runId) => __async(this, null, function* () {
283
+ yield handleEnd(runId);
284
+ }),
285
+ handleLLMError: (e, runId) => __async(this, null, function* () {
286
+ yield handleError(e, runId);
287
+ }),
288
+ handleChainStart: (_chain, _inputs, runId) => __async(this, null, function* () {
289
+ handleStart(runId);
290
+ }),
291
+ handleChainEnd: (_outputs, runId) => __async(this, null, function* () {
292
+ yield handleEnd(runId);
293
+ }),
294
+ handleChainError: (e, runId) => __async(this, null, function* () {
295
+ yield handleError(e, runId);
296
+ }),
297
+ handleToolStart: (_tool, _input, runId) => __async(this, null, function* () {
298
+ handleStart(runId);
299
+ }),
300
+ handleToolEnd: (_output, runId) => __async(this, null, function* () {
301
+ yield handleEnd(runId);
302
+ }),
303
+ handleToolError: (e, runId) => __async(this, null, function* () {
304
+ yield handleError(e, runId);
270
305
  })
271
306
  }
272
307
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "2.1.10",
3
+ "version": "2.1.12",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -22,6 +22,7 @@
22
22
  },
23
23
  "./react": {
24
24
  "types": "./react/dist/index.d.ts",
25
+ "react-server": "./react/dist/index.server.mjs",
25
26
  "import": "./react/dist/index.mjs",
26
27
  "module": "./react/dist/index.mjs",
27
28
  "require": "./react/dist/index.js"
@@ -88,7 +89,7 @@
88
89
  "access": "public"
89
90
  },
90
91
  "scripts": {
91
- "build": "tsup",
92
+ "build": "tsup && cat react/dist/index.server.d.ts >> react/dist/index.d.ts",
92
93
  "clean": "rm -rf dist && rm -rf react/dist && rm -rf svelte/dist && rm -rf vue/dist",
93
94
  "dev": "tsup --watch",
94
95
  "lint": "eslint \"./**/*.ts*\"",
@@ -1,6 +1,5 @@
1
1
  import { ChatCompletionRequestMessageFunctionCall, CreateChatCompletionRequestFunctionCall } from 'openai-edge';
2
2
  import { ChatCompletionFunctions } from 'openai-edge/types/api';
3
- import * as react_jsx_runtime from 'react/jsx-runtime';
4
3
 
5
4
  /**
6
5
  * Shared types between the API and UI packages.
@@ -252,6 +251,9 @@ type UseCompletionHelpers = {
252
251
  };
253
252
  declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers;
254
253
 
254
+ export { CreateMessage, Message, UseChatHelpers, UseChatOptions, UseCompletionHelpers, useChat, useCompletion };
255
+ import * as react_jsx_runtime from 'react/jsx-runtime';
256
+
255
257
  type Props = {
256
258
  /**
257
259
  * A ReadableStream produced by the AI SDK.
@@ -260,7 +262,8 @@ type Props = {
260
262
  };
261
263
  /**
262
264
  * A React Server Component that recursively renders a stream of tokens.
265
+ * Can only be used inside of server components.
263
266
  */
264
267
  declare function Tokens(props: Props): Promise<react_jsx_runtime.JSX.Element>;
265
268
 
266
- export { CreateMessage, Message, Tokens, UseChatHelpers, UseChatOptions, UseCompletionHelpers, useChat, useCompletion };
269
+ export { Tokens };
@@ -65,7 +65,6 @@ var __async = (__this, __arguments, generator) => {
65
65
  // react/index.ts
66
66
  var react_exports = {};
67
67
  __export(react_exports, {
68
- Tokens: () => Tokens,
69
68
  useChat: () => useChat,
70
69
  useCompletion: () => useCompletion
71
70
  });
@@ -497,33 +496,8 @@ function useCompletion({
497
496
  isLoading: isMutating
498
497
  };
499
498
  }
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
- }
524
499
  // Annotate the CommonJS export names for ESM import in node:
525
500
  0 && (module.exports = {
526
- Tokens,
527
501
  useChat,
528
502
  useCompletion
529
503
  });
@@ -462,32 +462,7 @@ function useCompletion({
462
462
  isLoading: isMutating
463
463
  };
464
464
  }
465
-
466
- // react/tokens.tsx
467
- import { Suspense } from "react";
468
- import { Fragment, jsx, jsxs } from "react/jsx-runtime";
469
- function Tokens(props) {
470
- return __async(this, null, function* () {
471
- const { stream } = props;
472
- const reader = stream.getReader();
473
- return /* @__PURE__ */ jsx(Suspense, { children: /* @__PURE__ */ jsx(RecursiveTokens, { reader }) });
474
- });
475
- }
476
- function RecursiveTokens(_0) {
477
- return __async(this, arguments, function* ({ reader }) {
478
- const { done, value } = yield reader.read();
479
- if (done) {
480
- return null;
481
- }
482
- const text = new TextDecoder().decode(value);
483
- return /* @__PURE__ */ jsxs(Fragment, { children: [
484
- text,
485
- /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(RecursiveTokens, { reader }) })
486
- ] });
487
- });
488
- }
489
465
  export {
490
- Tokens,
491
466
  useChat,
492
467
  useCompletion
493
468
  };
@@ -0,0 +1,15 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+
3
+ type Props = {
4
+ /**
5
+ * A ReadableStream produced by the AI SDK.
6
+ */
7
+ stream: ReadableStream;
8
+ };
9
+ /**
10
+ * A React Server Component that recursively renders a stream of tokens.
11
+ * Can only be used inside of server components.
12
+ */
13
+ declare function Tokens(props: Props): Promise<react_jsx_runtime.JSX.Element>;
14
+
15
+ export { Tokens };
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var __async = (__this, __arguments, generator) => {
20
+ return new Promise((resolve, reject) => {
21
+ var fulfilled = (value) => {
22
+ try {
23
+ step(generator.next(value));
24
+ } catch (e) {
25
+ reject(e);
26
+ }
27
+ };
28
+ var rejected = (value) => {
29
+ try {
30
+ step(generator.throw(value));
31
+ } catch (e) {
32
+ reject(e);
33
+ }
34
+ };
35
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
36
+ step((generator = generator.apply(__this, __arguments)).next());
37
+ });
38
+ };
39
+
40
+ // react/index.server.ts
41
+ var index_server_exports = {};
42
+ __export(index_server_exports, {
43
+ Tokens: () => Tokens
44
+ });
45
+ module.exports = __toCommonJS(index_server_exports);
46
+
47
+ // react/tokens.tsx
48
+ var import_react = require("react");
49
+ var import_jsx_runtime = require("react/jsx-runtime");
50
+ function Tokens(props) {
51
+ return __async(this, null, function* () {
52
+ const { stream } = props;
53
+ const reader = stream.getReader();
54
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react.Suspense, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(RecursiveTokens, { reader }) });
55
+ });
56
+ }
57
+ function RecursiveTokens(_0) {
58
+ return __async(this, arguments, function* ({ reader }) {
59
+ const { done, value } = yield reader.read();
60
+ if (done) {
61
+ return null;
62
+ }
63
+ const text = new TextDecoder().decode(value);
64
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
65
+ text,
66
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react.Suspense, { fallback: null, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(RecursiveTokens, { reader }) })
67
+ ] });
68
+ });
69
+ }
70
+ // Annotate the CommonJS export names for ESM import in node:
71
+ 0 && (module.exports = {
72
+ Tokens
73
+ });
@@ -0,0 +1,47 @@
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+
22
+ // react/tokens.tsx
23
+ import { Suspense } from "react";
24
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
25
+ function Tokens(props) {
26
+ return __async(this, null, function* () {
27
+ const { stream } = props;
28
+ const reader = stream.getReader();
29
+ return /* @__PURE__ */ jsx(Suspense, { children: /* @__PURE__ */ jsx(RecursiveTokens, { reader }) });
30
+ });
31
+ }
32
+ function RecursiveTokens(_0) {
33
+ return __async(this, arguments, function* ({ reader }) {
34
+ const { done, value } = yield reader.read();
35
+ if (done) {
36
+ return null;
37
+ }
38
+ const text = new TextDecoder().decode(value);
39
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
40
+ text,
41
+ /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(RecursiveTokens, { reader }) })
42
+ ] });
43
+ });
44
+ }
45
+ export {
46
+ Tokens
47
+ };