ai 2.1.28 → 2.1.29

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,7 +5,7 @@ The Vercel AI SDK is **a library for building AI-powered streaming text and chat
5
5
  ## Features
6
6
 
7
7
  - [SWR](https://swr.vercel.app)-powered React, Svelte, Vue and Solid 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), [Cohere](https://cohere.com) and [Hugging Face](https://huggingface.co)
8
+ - First-class support for [LangChain](https://js.langchain.com/docs) and [OpenAI](https://openai.com), [Anthropic](https://www.anthropic.com), [Cohere](https://cohere.com) and [Hugging Face](https://huggingface.co)
9
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
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { ChatCompletionRequestMessageFunctionCall, CreateChatCompletionRequestFunctionCall } from 'openai-edge';
2
2
  import { ChatCompletionFunctions } from 'openai-edge/types/api';
3
3
  import { ServerResponse } from 'node:http';
4
+ import { Prediction } from 'replicate';
4
5
 
5
6
  interface FunctionCallPayload {
6
7
  name: string;
@@ -27,7 +28,7 @@ interface AIStreamParser {
27
28
  * @param {AIStreamParser} customParser - Function to handle event data.
28
29
  * @returns {TransformStream<Uint8Array, string>} TransformStream parsing events.
29
30
  */
30
- declare function createEventStreamTransformer(customParser: AIStreamParser): TransformStream<Uint8Array, string>;
31
+ declare function createEventStreamTransformer(customParser?: AIStreamParser): TransformStream<Uint8Array, string>;
31
32
  /**
32
33
  * Creates a transform stream that encodes input messages and invokes optional callback functions.
33
34
  * The transform stream uses the provided callbacks to execute custom logic at different stages of the stream's lifecycle.
@@ -83,7 +84,7 @@ declare function trimStartOfStreamHelper(): (text: string) => string;
83
84
  * @return {ReadableStream} The AIStream.
84
85
  * @throws Will throw an error if the response is not OK.
85
86
  */
86
- declare function AIStream(response: Response, customParser: AIStreamParser, callbacks?: AIStreamCallbacks): ReadableStream<Uint8Array>;
87
+ declare function AIStream(response: Response, customParser?: AIStreamParser, callbacks?: AIStreamCallbacks): ReadableStream<Uint8Array>;
87
88
  /**
88
89
  * Implements ReadableStream.from(asyncIterable), which isn't documented in MDN and isn't implemented in node.
89
90
  * https://github.com/whatwg/streams/commit/8d7a0bf26eb2cc23e884ddbaac7c1da4b91cf2bc
@@ -365,7 +366,28 @@ declare function LangChainStream(callbacks?: AIStreamCallbacks): {
365
366
  };
366
367
  };
367
368
 
369
+ /**
370
+ * Stream predictions from Replicate.
371
+ * Only certain models are supported and you must pass `stream: true` to
372
+ * replicate.predictions.create().
373
+ * @see https://github.com/replicate/replicate-javascript#streaming
374
+ *
375
+ * @example
376
+ * const response = await replicate.predictions.create({
377
+ * stream: true,
378
+ * input: {
379
+ * prompt: messages.join('\n')
380
+ * },
381
+ * version: '2c1608e18606fad2812020dc541930f2d0495ce32eee50074220b87300bc16e1'
382
+ * })
383
+ *
384
+ * const stream = await ReplicateStream(response)
385
+ * return new StreamingTextResponse(stream)
386
+ *
387
+ */
388
+ declare function ReplicateStream(res: Prediction, cb?: AIStreamCallbacks): Promise<ReadableStream>;
389
+
368
390
  declare const nanoid: (size?: number | undefined) => string;
369
391
  declare function createChunkDecoder(): (chunk: Uint8Array | undefined) => string;
370
392
 
371
- export { AIStream, AIStreamCallbacks, AIStreamParser, AnthropicStream, ChatRequest, ChatRequestOptions, CohereStream, CreateMessage, FunctionCallHandler, FunctionCallPayload, HuggingFaceStream, LangChainStream, Message, OpenAIStream, OpenAIStreamCallbacks, RequestOptions, StreamingTextResponse, UseChatOptions, UseCompletionOptions, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, nanoid, readableFromAsyncIterable, streamToResponse, trimStartOfStreamHelper };
393
+ export { AIStream, AIStreamCallbacks, AIStreamParser, AnthropicStream, ChatRequest, ChatRequestOptions, CohereStream, CreateMessage, FunctionCallHandler, FunctionCallPayload, HuggingFaceStream, LangChainStream, Message, OpenAIStream, OpenAIStreamCallbacks, ReplicateStream, RequestOptions, StreamingTextResponse, UseChatOptions, UseCompletionOptions, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, nanoid, readableFromAsyncIterable, streamToResponse, trimStartOfStreamHelper };
package/dist/index.js CHANGED
@@ -26,6 +26,7 @@ __export(streams_exports, {
26
26
  HuggingFaceStream: () => HuggingFaceStream,
27
27
  LangChainStream: () => LangChainStream,
28
28
  OpenAIStream: () => OpenAIStream,
29
+ ReplicateStream: () => ReplicateStream,
29
30
  StreamingTextResponse: () => StreamingTextResponse,
30
31
  createCallbacksTransformer: () => createCallbacksTransformer,
31
32
  createChunkDecoder: () => createChunkDecoder,
@@ -51,7 +52,7 @@ function createEventStreamTransformer(customParser) {
51
52
  return;
52
53
  }
53
54
  if ("data" in event) {
54
- const parsedMessage = customParser(event.data);
55
+ const parsedMessage = customParser ? customParser(event.data) : event.data;
55
56
  if (parsedMessage)
56
57
  controller.enqueue(parsedMessage);
57
58
  }
@@ -462,6 +463,25 @@ function LangChainStream(callbacks) {
462
463
  };
463
464
  }
464
465
 
466
+ // streams/replicate-stream.ts
467
+ async function ReplicateStream(res, cb) {
468
+ var _a;
469
+ const url = (_a = res.urls) == null ? void 0 : _a.stream;
470
+ if (!url) {
471
+ if (res.error)
472
+ throw new Error(res.error);
473
+ else
474
+ throw new Error("Missing stream URL in Replicate response");
475
+ }
476
+ const eventStream = await fetch(url, {
477
+ method: "GET",
478
+ headers: {
479
+ Accept: "text/event-stream"
480
+ }
481
+ });
482
+ return AIStream(eventStream, void 0, cb);
483
+ }
484
+
465
485
  // shared/utils.ts
466
486
  var import_non_secure = require("nanoid/non-secure");
467
487
  var nanoid = (0, import_non_secure.customAlphabet)(
@@ -484,6 +504,7 @@ function createChunkDecoder() {
484
504
  HuggingFaceStream,
485
505
  LangChainStream,
486
506
  OpenAIStream,
507
+ ReplicateStream,
487
508
  StreamingTextResponse,
488
509
  createCallbacksTransformer,
489
510
  createChunkDecoder,
package/dist/index.mjs CHANGED
@@ -14,7 +14,7 @@ function createEventStreamTransformer(customParser) {
14
14
  return;
15
15
  }
16
16
  if ("data" in event) {
17
- const parsedMessage = customParser(event.data);
17
+ const parsedMessage = customParser ? customParser(event.data) : event.data;
18
18
  if (parsedMessage)
19
19
  controller.enqueue(parsedMessage);
20
20
  }
@@ -425,6 +425,25 @@ function LangChainStream(callbacks) {
425
425
  };
426
426
  }
427
427
 
428
+ // streams/replicate-stream.ts
429
+ async function ReplicateStream(res, cb) {
430
+ var _a;
431
+ const url = (_a = res.urls) == null ? void 0 : _a.stream;
432
+ if (!url) {
433
+ if (res.error)
434
+ throw new Error(res.error);
435
+ else
436
+ throw new Error("Missing stream URL in Replicate response");
437
+ }
438
+ const eventStream = await fetch(url, {
439
+ method: "GET",
440
+ headers: {
441
+ Accept: "text/event-stream"
442
+ }
443
+ });
444
+ return AIStream(eventStream, void 0, cb);
445
+ }
446
+
428
447
  // shared/utils.ts
429
448
  import { customAlphabet } from "nanoid/non-secure";
430
449
  var nanoid = customAlphabet(
@@ -446,6 +465,7 @@ export {
446
465
  HuggingFaceStream,
447
466
  LangChainStream,
448
467
  OpenAIStream,
468
+ ReplicateStream,
449
469
  StreamingTextResponse,
450
470
  createCallbacksTransformer,
451
471
  createChunkDecoder,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "2.1.28",
3
+ "version": "2.1.29",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -61,11 +61,11 @@
61
61
  "dependencies": {
62
62
  "eventsource-parser": "1.0.0",
63
63
  "nanoid": "3.3.6",
64
+ "solid-swr-store": "0.10.7",
64
65
  "sswr": "2.0.0",
65
66
  "swr": "2.2.0",
66
- "swrv": "1.0.4",
67
- "solid-swr-store": "0.10.7",
68
- "swr-store": "0.10.6"
67
+ "swr-store": "0.10.6",
68
+ "swrv": "1.0.4"
69
69
  },
70
70
  "devDependencies": {
71
71
  "@edge-runtime/jest-environment": "1.1.0-beta.31",
@@ -76,11 +76,12 @@
76
76
  "eslint": "^7.32.0",
77
77
  "jest": "29.2.1",
78
78
  "openai-edge": "^1.1.0",
79
+ "replicate": "^0.14.1",
79
80
  "ts-jest": "29.0.3",
80
81
  "tsup": "^6.7.0",
81
82
  "typescript": "5.1.3",
82
- "@vercel/ai-tsconfig": "0.0.0",
83
- "eslint-config-vercel-ai": "0.0.0"
83
+ "eslint-config-vercel-ai": "0.0.0",
84
+ "@vercel/ai-tsconfig": "0.0.0"
84
85
  },
85
86
  "peerDependencies": {
86
87
  "react": "^18.2.0",