ai-stream-utils 1.6.0 → 2.0.1
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 +197 -416
- package/dist/convert-ui-message-stream-to-sse-stream-CcQRXKju.mjs +165 -0
- package/dist/index.d.mts +538 -110
- package/dist/index.mjs +414 -261
- package/dist/types-B4nePmEd.d.mts +53 -0
- package/dist/utils/index.d.mts +2 -46
- package/dist/utils/index.mjs +2 -81
- package/package.json +49 -31
- package/dist/create-async-iterable-stream-x_DKVIDi.mjs +0 -59
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { AsyncIterableStream, AsyncIterableStream as AsyncIterableStream$1, UIMessageChunk } from "ai";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/convert-array-to-async-iterable.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Converts an array to an AsyncIterable.
|
|
6
|
+
*/
|
|
7
|
+
declare function convertArrayToAsyncIterable<T>(array: Array<T>): AsyncIterable<T>;
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/utils/convert-array-to-stream.d.ts
|
|
10
|
+
/**
|
|
11
|
+
* Converts an array to a ReadableStream.
|
|
12
|
+
*/
|
|
13
|
+
declare function convertArrayToStream<T>(array: Array<T>): ReadableStream<T>;
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/utils/convert-async-iterable-to-array.d.ts
|
|
16
|
+
/**
|
|
17
|
+
* Converts an AsyncIterable to an array.
|
|
18
|
+
*/
|
|
19
|
+
declare function convertAsyncIterableToArray<T>(iterable: AsyncIterable<T>): Promise<Array<T>>;
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/utils/convert-async-iterable-to-stream.d.ts
|
|
22
|
+
/**
|
|
23
|
+
* Converts an AsyncIterable to a ReadableStream.
|
|
24
|
+
* Copied from https://github.com/vercel/ai/blob/main/packages/provider-utils/src/convert-async-iterator-to-readable-stream.ts
|
|
25
|
+
*/
|
|
26
|
+
declare function convertAsyncIterableToStream<T>(iterable: AsyncIterable<T>): ReadableStream<T>;
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/utils/convert-sse-stream-to-ui-message-stream.d.ts
|
|
29
|
+
/**
|
|
30
|
+
* Converts an SSE stream to a UI message stream.
|
|
31
|
+
*/
|
|
32
|
+
declare function convertSSEToUIMessageStream(stream: ReadableStream<string>): ReadableStream<UIMessageChunk>;
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/utils/convert-stream-to-array.d.ts
|
|
35
|
+
/**
|
|
36
|
+
* Converts a ReadableStream to an array.
|
|
37
|
+
*/
|
|
38
|
+
declare function convertStreamToArray<T>(stream: ReadableStream<T>): Promise<Array<T>>;
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/utils/convert-ui-message-stream-to-sse-stream.d.ts
|
|
41
|
+
/**
|
|
42
|
+
* Converts a UI message stream to an SSE stream.
|
|
43
|
+
*/
|
|
44
|
+
declare function convertUIMessageToSSEStream(stream: ReadableStream<UIMessageChunk>): ReadableStream<string>;
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/utils/create-async-iterable-stream.d.ts
|
|
47
|
+
/**
|
|
48
|
+
* Converts a ReadableStream to an AsyncIterableStream.
|
|
49
|
+
* Copied from https://github.com/vercel/ai/blob/main/packages/ai/src/util/async-iterable-stream.ts
|
|
50
|
+
*/
|
|
51
|
+
declare function createAsyncIterableStream<T>(source: ReadableStream<T>): AsyncIterableStream<T>;
|
|
52
|
+
//#endregion
|
|
53
|
+
export { convertSSEToUIMessageStream as a, convertArrayToStream as c, convertStreamToArray as i, convertArrayToAsyncIterable as l, createAsyncIterableStream as n, convertAsyncIterableToStream as o, convertUIMessageToSSEStream as r, convertAsyncIterableToArray as s, AsyncIterableStream$1 as t };
|
package/dist/utils/index.d.mts
CHANGED
|
@@ -1,46 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
//#region src/utils/convert-array-to-stream.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Converts an array to a ReadableStream.
|
|
6
|
-
*/
|
|
7
|
-
declare function convertArrayToStream<T>(array: Array<T>): ReadableStream<T>;
|
|
8
|
-
//#endregion
|
|
9
|
-
//#region src/utils/convert-async-iterable-to-array.d.ts
|
|
10
|
-
/**
|
|
11
|
-
* Converts an AsyncIterable to an array.
|
|
12
|
-
*/
|
|
13
|
-
declare function convertAsyncIterableToArray<T>(iterable: AsyncIterable<T>): Promise<Array<T>>;
|
|
14
|
-
//#endregion
|
|
15
|
-
//#region src/utils/convert-async-iterable-to-stream.d.ts
|
|
16
|
-
/**
|
|
17
|
-
* Converts an AsyncIterable to a ReadableStream.
|
|
18
|
-
*/
|
|
19
|
-
declare function convertAsyncIterableToStream<T>(iterable: AsyncIterable<T>): ReadableStream<T>;
|
|
20
|
-
//#endregion
|
|
21
|
-
//#region src/utils/convert-sse-stream-to-ui-message-stream.d.ts
|
|
22
|
-
/**
|
|
23
|
-
* Converts an SSE stream to a UI message stream.
|
|
24
|
-
*/
|
|
25
|
-
declare function convertSSEToUIMessageStream(stream: ReadableStream<string>): ReadableStream<UIMessageChunk>;
|
|
26
|
-
//#endregion
|
|
27
|
-
//#region src/utils/convert-stream-to-array.d.ts
|
|
28
|
-
/**
|
|
29
|
-
* Converts a ReadableStream to an array.
|
|
30
|
-
*/
|
|
31
|
-
declare function convertStreamToArray<T>(stream: ReadableStream<T>): Promise<Array<T>>;
|
|
32
|
-
//#endregion
|
|
33
|
-
//#region src/utils/convert-ui-message-stream-to-sse-stream.d.ts
|
|
34
|
-
/**
|
|
35
|
-
* Converts a UI message stream to an SSE stream.
|
|
36
|
-
*/
|
|
37
|
-
declare function convertUIMessageToSSEStream(stream: ReadableStream<UIMessageChunk>): ReadableStream<string>;
|
|
38
|
-
//#endregion
|
|
39
|
-
//#region src/utils/create-async-iterable-stream.d.ts
|
|
40
|
-
/**
|
|
41
|
-
* Converts a ReadableStream to an AsyncIterableStream.
|
|
42
|
-
* Copied from https://github.com/vercel/ai/blob/main/packages/ai/src/util/async-iterable-stream.ts
|
|
43
|
-
*/
|
|
44
|
-
declare function createAsyncIterableStream<T>(source: ReadableStream<T>): AsyncIterableStream$1<T>;
|
|
45
|
-
//#endregion
|
|
46
|
-
export { type AsyncIterableStream, convertArrayToStream, convertAsyncIterableToArray, convertAsyncIterableToStream, convertSSEToUIMessageStream, convertStreamToArray, convertUIMessageToSSEStream, createAsyncIterableStream };
|
|
1
|
+
import { a as convertSSEToUIMessageStream, c as convertArrayToStream, i as convertStreamToArray, l as convertArrayToAsyncIterable, n as createAsyncIterableStream, o as convertAsyncIterableToStream, r as convertUIMessageToSSEStream, s as convertAsyncIterableToArray, t as AsyncIterableStream } from "../types-B4nePmEd.mjs";
|
|
2
|
+
export { type AsyncIterableStream, convertArrayToAsyncIterable, convertArrayToStream, convertAsyncIterableToArray, convertAsyncIterableToStream, convertSSEToUIMessageStream, convertStreamToArray, convertUIMessageToSSEStream, createAsyncIterableStream };
|
package/dist/utils/index.mjs
CHANGED
|
@@ -1,82 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { JsonToSseTransformStream, parseJsonEventStream, uiMessageChunkSchema } from "ai";
|
|
1
|
+
import { a as convertArrayToStream, c as convertAsyncIterableToStream, i as convertAsyncIterableToArray, n as convertStreamToArray, o as convertArrayToAsyncIterable, r as convertSSEToUIMessageStream, s as createAsyncIterableStream, t as convertUIMessageToSSEStream } from "../convert-ui-message-stream-to-sse-stream-CcQRXKju.mjs";
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Converts an array to a ReadableStream.
|
|
7
|
-
*/
|
|
8
|
-
function convertArrayToStream(array) {
|
|
9
|
-
return new ReadableStream({ start(controller) {
|
|
10
|
-
for (const item of array) controller.enqueue(item);
|
|
11
|
-
controller.close();
|
|
12
|
-
} });
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
//#endregion
|
|
16
|
-
//#region src/utils/convert-async-iterable-to-array.ts
|
|
17
|
-
/**
|
|
18
|
-
* Converts an AsyncIterable to an array.
|
|
19
|
-
*/
|
|
20
|
-
async function convertAsyncIterableToArray(iterable) {
|
|
21
|
-
const result = [];
|
|
22
|
-
for await (const item of iterable) result.push(item);
|
|
23
|
-
return result;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
//#endregion
|
|
27
|
-
//#region src/utils/convert-async-iterable-to-stream.ts
|
|
28
|
-
/**
|
|
29
|
-
* Converts an AsyncIterable to a ReadableStream.
|
|
30
|
-
*/
|
|
31
|
-
function convertAsyncIterableToStream(iterable) {
|
|
32
|
-
return new ReadableStream({ async start(controller) {
|
|
33
|
-
for await (const item of iterable) controller.enqueue(item);
|
|
34
|
-
controller.close();
|
|
35
|
-
} });
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
//#endregion
|
|
39
|
-
//#region src/utils/convert-sse-stream-to-ui-message-stream.ts
|
|
40
|
-
/**
|
|
41
|
-
* Converts an SSE stream to a UI message stream.
|
|
42
|
-
*/
|
|
43
|
-
function convertSSEToUIMessageStream(stream) {
|
|
44
|
-
return parseJsonEventStream({
|
|
45
|
-
stream: stream.pipeThrough(new TextEncoderStream()),
|
|
46
|
-
schema: uiMessageChunkSchema
|
|
47
|
-
}).pipeThrough(new TransformStream({ transform(result, controller) {
|
|
48
|
-
if (result.success) controller.enqueue(result.value);
|
|
49
|
-
} }));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
//#endregion
|
|
53
|
-
//#region src/utils/convert-stream-to-array.ts
|
|
54
|
-
/**
|
|
55
|
-
* Converts a ReadableStream to an array.
|
|
56
|
-
*/
|
|
57
|
-
async function convertStreamToArray(stream) {
|
|
58
|
-
const reader = stream.getReader();
|
|
59
|
-
const result = [];
|
|
60
|
-
try {
|
|
61
|
-
while (true) {
|
|
62
|
-
const { done, value } = await reader.read();
|
|
63
|
-
if (done) break;
|
|
64
|
-
result.push(value);
|
|
65
|
-
}
|
|
66
|
-
} finally {
|
|
67
|
-
reader.releaseLock();
|
|
68
|
-
}
|
|
69
|
-
return result;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
//#endregion
|
|
73
|
-
//#region src/utils/convert-ui-message-stream-to-sse-stream.ts
|
|
74
|
-
/**
|
|
75
|
-
* Converts a UI message stream to an SSE stream.
|
|
76
|
-
*/
|
|
77
|
-
function convertUIMessageToSSEStream(stream) {
|
|
78
|
-
return stream.pipeThrough(new JsonToSseTransformStream());
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
//#endregion
|
|
82
|
-
export { convertArrayToStream, convertAsyncIterableToArray, convertAsyncIterableToStream, convertSSEToUIMessageStream, convertStreamToArray, convertUIMessageToSSEStream, createAsyncIterableStream };
|
|
3
|
+
export { convertArrayToAsyncIterable, convertArrayToStream, convertAsyncIterableToArray, convertAsyncIterableToStream, convertSSEToUIMessageStream, convertStreamToArray, convertUIMessageToSSEStream, createAsyncIterableStream };
|
package/package.json
CHANGED
|
@@ -1,59 +1,77 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-stream-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "AI SDK: Filter and transform UI messages while streaming to the client",
|
|
5
|
-
"types": "./dist/index.d.mts",
|
|
6
|
-
"type": "module",
|
|
7
|
-
"files": [
|
|
8
|
-
"dist"
|
|
9
|
-
],
|
|
10
|
-
"exports": {
|
|
11
|
-
".": "./dist/index.mjs",
|
|
12
|
-
"./utils": "./dist/utils/index.mjs",
|
|
13
|
-
"./package.json": "./package.json"
|
|
14
|
-
},
|
|
15
5
|
"keywords": [
|
|
16
6
|
"ai",
|
|
17
|
-
"sdk",
|
|
18
7
|
"ai-sdk",
|
|
19
|
-
"
|
|
8
|
+
"chunk",
|
|
9
|
+
"filter",
|
|
10
|
+
"map",
|
|
20
11
|
"message",
|
|
12
|
+
"sdk",
|
|
21
13
|
"stream",
|
|
22
|
-
"
|
|
14
|
+
"transform",
|
|
15
|
+
"ui",
|
|
23
16
|
"ui-message",
|
|
17
|
+
"ui-message-chunk",
|
|
24
18
|
"ui-message-stream",
|
|
25
|
-
"
|
|
19
|
+
"utils"
|
|
26
20
|
],
|
|
27
|
-
"author": "Chris Cook",
|
|
28
21
|
"license": "MIT",
|
|
22
|
+
"author": "Chris Cook",
|
|
29
23
|
"repository": {
|
|
30
24
|
"type": "git",
|
|
31
|
-
"url": "git+https://github.com/zirkelc/ai-stream-utils"
|
|
25
|
+
"url": "git+https://github.com/zirkelc/ai-stream-utils.git"
|
|
32
26
|
},
|
|
33
|
-
"
|
|
34
|
-
"
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"type": "module",
|
|
31
|
+
"types": "./dist/index.d.mts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": "./dist/index.mjs",
|
|
34
|
+
"./utils": "./dist/utils/index.mjs",
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"defu": "^6.1.4"
|
|
35
39
|
},
|
|
36
40
|
"devDependencies": {
|
|
37
|
-
"@ai-sdk/openai": "^3.0.
|
|
38
|
-
"@ai-sdk/provider
|
|
41
|
+
"@ai-sdk/openai": "^3.0.29",
|
|
42
|
+
"@ai-sdk/provider": "^3.0.8",
|
|
43
|
+
"@ai-sdk/provider-utils": "^4.0.15",
|
|
39
44
|
"@arethetypeswrong/cli": "^0.18.2",
|
|
40
|
-
"@biomejs/biome": "^2.3.11",
|
|
41
45
|
"@total-typescript/tsconfig": "^1.0.4",
|
|
42
|
-
"@types/node": "^25.
|
|
43
|
-
"ai": "^6.0.
|
|
46
|
+
"@types/node": "^25.2.3",
|
|
47
|
+
"ai": "^6.0.86",
|
|
44
48
|
"husky": "^9.1.7",
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
+
"lint-staged": "^16.2.7",
|
|
50
|
+
"msw": "^2.12.10",
|
|
51
|
+
"oxfmt": "^0.33.0",
|
|
52
|
+
"oxlint": "^1.48.0",
|
|
53
|
+
"pkg-pr-new": "^0.0.63",
|
|
54
|
+
"publint": "^0.3.17",
|
|
55
|
+
"tsdown": "0.20.3",
|
|
49
56
|
"tsx": "^4.21.0",
|
|
50
57
|
"typescript": "^5.9.3",
|
|
51
|
-
"vitest": "^4.0.
|
|
52
|
-
"zod": "^4.3.
|
|
58
|
+
"vitest": "^4.0.18",
|
|
59
|
+
"zod": "^4.3.6"
|
|
60
|
+
},
|
|
61
|
+
"peerDependencies": {
|
|
62
|
+
"ai": "5.x || 6.x"
|
|
63
|
+
},
|
|
64
|
+
"lint-staged": {
|
|
65
|
+
"*": "oxfmt --no-error-on-unmatched-pattern",
|
|
66
|
+
"*.{js,jsx,ts,tsx,mjs,cjs}": "oxlint"
|
|
53
67
|
},
|
|
54
68
|
"scripts": {
|
|
55
69
|
"build": "tsdown",
|
|
56
70
|
"test": "vitest",
|
|
57
|
-
"
|
|
71
|
+
"benchmark": "pnpm vitest bench --run",
|
|
72
|
+
"lint": "oxlint --fix",
|
|
73
|
+
"lint:ci": "oxlint",
|
|
74
|
+
"format": "oxfmt --write",
|
|
75
|
+
"format:ci": "oxfmt --check"
|
|
58
76
|
}
|
|
59
77
|
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
//#region src/utils/create-async-iterable-stream.ts
|
|
2
|
-
/**
|
|
3
|
-
* Converts a ReadableStream to an AsyncIterableStream.
|
|
4
|
-
* Copied from https://github.com/vercel/ai/blob/main/packages/ai/src/util/async-iterable-stream.ts
|
|
5
|
-
*/
|
|
6
|
-
function createAsyncIterableStream(source) {
|
|
7
|
-
/** Pipe through a TransformStream to ensure a fresh, unlocked stream. */
|
|
8
|
-
const stream = source.pipeThrough(new TransformStream());
|
|
9
|
-
/** Implements the async iterator protocol for the stream. */
|
|
10
|
-
return Object.assign(stream, { [Symbol.asyncIterator]() {
|
|
11
|
-
const reader = stream.getReader();
|
|
12
|
-
let finished = false;
|
|
13
|
-
/** Cleans up the reader by cancelling and releasing the lock. */
|
|
14
|
-
async function cleanup(cancelStream) {
|
|
15
|
-
finished = true;
|
|
16
|
-
try {
|
|
17
|
-
if (cancelStream) await reader.cancel?.();
|
|
18
|
-
} finally {
|
|
19
|
-
try {
|
|
20
|
-
reader.releaseLock();
|
|
21
|
-
} catch {}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return {
|
|
25
|
-
async next() {
|
|
26
|
-
if (finished) return {
|
|
27
|
-
done: true,
|
|
28
|
-
value: void 0
|
|
29
|
-
};
|
|
30
|
-
const { done, value } = await reader.read();
|
|
31
|
-
if (done) {
|
|
32
|
-
await cleanup(true);
|
|
33
|
-
return {
|
|
34
|
-
done: true,
|
|
35
|
-
value: void 0
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
return {
|
|
39
|
-
done: false,
|
|
40
|
-
value
|
|
41
|
-
};
|
|
42
|
-
},
|
|
43
|
-
async return() {
|
|
44
|
-
await cleanup(true);
|
|
45
|
-
return {
|
|
46
|
-
done: true,
|
|
47
|
-
value: void 0
|
|
48
|
-
};
|
|
49
|
-
},
|
|
50
|
-
async throw(err) {
|
|
51
|
-
await cleanup(true);
|
|
52
|
-
throw err;
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
} });
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
//#endregion
|
|
59
|
-
export { createAsyncIterableStream as t };
|