assistant-stream 0.1.8 → 0.2.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/ai-sdk/package.json +2 -2
- package/dist/ai-sdk/index.js +7 -34
- package/dist/ai-sdk/index.js.map +1 -1
- package/dist/ai-sdk/language-model.js +5 -31
- package/dist/ai-sdk/language-model.js.map +1 -1
- package/dist/ai-sdk.js +4 -32
- package/dist/ai-sdk.js.map +1 -1
- package/dist/core/AssistantStream.js +3 -29
- package/dist/core/AssistantStream.js.map +1 -1
- package/dist/core/AssistantStreamChunk.js +0 -18
- package/dist/core/AssistantStreamChunk.js.map +1 -1
- package/dist/core/accumulators/AssistantMessageStream.js +9 -35
- package/dist/core/accumulators/AssistantMessageStream.js.map +1 -1
- package/dist/core/accumulators/assistant-message-accumulator.js +22 -48
- package/dist/core/accumulators/assistant-message-accumulator.js.map +1 -1
- package/dist/core/index.js +16 -46
- package/dist/core/index.js.map +1 -1
- package/dist/core/modules/assistant-stream.js +29 -52
- package/dist/core/modules/assistant-stream.js.map +1 -1
- package/dist/core/modules/text.js +6 -33
- package/dist/core/modules/text.js.map +1 -1
- package/dist/core/modules/tool-call.js +8 -35
- package/dist/core/modules/tool-call.js.map +1 -1
- package/dist/core/serialization/PlainText.js +9 -36
- package/dist/core/serialization/PlainText.js.map +1 -1
- package/dist/core/serialization/data-stream/DataStream.js +57 -79
- package/dist/core/serialization/data-stream/DataStream.js.map +1 -1
- package/dist/core/serialization/data-stream/chunk-types.js +2 -28
- package/dist/core/serialization/data-stream/chunk-types.js.map +1 -1
- package/dist/core/serialization/data-stream/serialization.js +6 -33
- package/dist/core/serialization/data-stream/serialization.js.map +1 -1
- package/dist/core/tool/ToolCallReader.js +30 -55
- package/dist/core/tool/ToolCallReader.js.map +1 -1
- package/dist/core/tool/ToolExecutionStream.js +18 -52
- package/dist/core/tool/ToolExecutionStream.js.map +1 -1
- package/dist/core/tool/ToolResponse.js +5 -31
- package/dist/core/tool/ToolResponse.js.map +1 -1
- package/dist/core/tool/index.js +9 -35
- package/dist/core/tool/index.js.map +1 -1
- package/dist/core/tool/tool-types.js +0 -18
- package/dist/core/tool/tool-types.js.map +1 -1
- package/dist/core/tool/toolResultStream.js +8 -35
- package/dist/core/tool/toolResultStream.js.map +1 -1
- package/dist/core/tool/type-path-utils.js +0 -18
- package/dist/core/tool/type-path-utils.js.map +1 -1
- package/dist/core/utils/Counter.js +4 -30
- package/dist/core/utils/Counter.js.map +1 -1
- package/dist/core/utils/generateId.js +4 -30
- package/dist/core/utils/generateId.js.map +1 -1
- package/dist/core/utils/stream/AssistantMetaTransformStream.js +4 -30
- package/dist/core/utils/stream/AssistantMetaTransformStream.js.map +1 -1
- package/dist/core/utils/stream/AssistantTransformStream.js +8 -32
- package/dist/core/utils/stream/AssistantTransformStream.js.map +1 -1
- package/dist/core/utils/stream/LineDecoderStream.js +4 -30
- package/dist/core/utils/stream/LineDecoderStream.js.map +1 -1
- package/dist/core/utils/stream/PipeableTransformStream.js +4 -30
- package/dist/core/utils/stream/PipeableTransformStream.js.map +1 -1
- package/dist/core/utils/stream/UnderlyingReadable.js +0 -18
- package/dist/core/utils/stream/UnderlyingReadable.js.map +1 -1
- package/dist/core/utils/stream/merge.js +5 -31
- package/dist/core/utils/stream/merge.js.map +1 -1
- package/dist/core/utils/stream/path-utils.js +10 -38
- package/dist/core/utils/stream/path-utils.js.map +1 -1
- package/dist/core/utils/types.js +0 -18
- package/dist/core/utils/types.js.map +1 -1
- package/dist/core/utils/withPromiseOrValue.js +2 -28
- package/dist/core/utils/withPromiseOrValue.js.map +1 -1
- package/dist/index.js +1 -24
- package/dist/index.js.map +1 -1
- package/dist/utils/AsyncIterableStream.js +2 -28
- package/dist/utils/AsyncIterableStream.js.map +1 -1
- package/dist/utils/json/fix-json.js +2 -28
- package/dist/utils/json/fix-json.js.map +1 -1
- package/dist/utils/json/is-json.js +2 -30
- package/dist/utils/json/is-json.js.map +1 -1
- package/dist/utils/json/json-value.js +0 -18
- package/dist/utils/json/json-value.js.map +1 -1
- package/dist/utils/json/parse-partial-json-object.js +12 -50
- package/dist/utils/json/parse-partial-json-object.js.map +1 -1
- package/dist/utils/promiseWithResolvers.js +3 -29
- package/dist/utils/promiseWithResolvers.js.map +1 -1
- package/dist/utils.js +9 -32
- package/dist/utils.js.map +1 -1
- package/package.json +7 -9
- package/{dist/ai-sdk/index.mjs → src/ai-sdk/index.ts} +64 -33
- package/{dist/ai-sdk/language-model.mjs → src/ai-sdk/language-model.ts} +35 -19
- package/src/ai-sdk.ts +2 -0
- package/src/core/AssistantStream.ts +39 -0
- package/src/core/AssistantStreamChunk.ts +93 -0
- package/src/core/accumulators/AssistantMessageStream.ts +56 -0
- package/{dist/core/accumulators/assistant-message-accumulator.mjs → src/core/accumulators/assistant-message-accumulator.ts} +152 -67
- package/src/core/index.ts +17 -0
- package/{dist/core/modules/assistant-stream.mjs → src/core/modules/assistant-stream.ts} +122 -60
- package/src/core/modules/text.ts +64 -0
- package/{dist/core/modules/tool-call.mjs → src/core/modules/tool-call.ts} +48 -27
- package/src/core/serialization/PlainText.ts +68 -0
- package/{dist/core/serialization/data-stream/DataStream.mjs → src/core/serialization/data-stream/DataStream.ts} +111 -67
- package/src/core/serialization/data-stream/chunk-types.ts +93 -0
- package/src/core/serialization/data-stream/serialization.ts +32 -0
- package/src/core/tool/ToolCallReader.ts +432 -0
- package/{dist/core/tool/ToolExecutionStream.mjs → src/core/tool/ToolExecutionStream.ts} +83 -35
- package/src/core/tool/ToolResponse.ts +31 -0
- package/src/core/tool/index.ts +8 -0
- package/src/core/tool/tool-types.ts +107 -0
- package/src/core/tool/toolResultStream.ts +119 -0
- package/src/core/tool/type-path-utils.ts +36 -0
- package/src/core/utils/Counter.ts +7 -0
- package/src/core/utils/generateId.tsx +6 -0
- package/src/core/utils/stream/AssistantMetaTransformStream.ts +74 -0
- package/src/core/utils/stream/AssistantTransformStream.ts +74 -0
- package/{dist/core/utils/stream/LineDecoderStream.mjs → src/core/utils/stream/LineDecoderStream.ts} +12 -10
- package/src/core/utils/stream/PipeableTransformStream.ts +10 -0
- package/src/core/utils/stream/UnderlyingReadable.ts +5 -0
- package/src/core/utils/stream/merge.ts +212 -0
- package/src/core/utils/stream/path-utils.ts +71 -0
- package/src/core/utils/types.ts +150 -0
- package/src/core/utils/withPromiseOrValue.ts +20 -0
- package/src/index.ts +1 -0
- package/src/utils/AsyncIterableStream.ts +24 -0
- package/{dist/utils/json/fix-json.mjs → src/utils/json/fix-json.ts} +146 -20
- package/src/utils/json/is-json.ts +43 -0
- package/src/utils/json/json-value.ts +13 -0
- package/src/utils/json/parse-partial-json-object.test.ts +225 -0
- package/src/utils/json/parse-partial-json-object.ts +103 -0
- package/src/utils/promiseWithResolvers.ts +10 -0
- package/src/utils.ts +13 -0
- package/dist/ai-sdk/index.mjs.map +0 -1
- package/dist/ai-sdk/language-model.mjs.map +0 -1
- package/dist/ai-sdk.mjs +0 -7
- package/dist/ai-sdk.mjs.map +0 -1
- package/dist/core/AssistantStream.mjs +0 -21
- package/dist/core/AssistantStream.mjs.map +0 -1
- package/dist/core/AssistantStreamChunk.mjs +0 -1
- package/dist/core/AssistantStreamChunk.mjs.map +0 -1
- package/dist/core/accumulators/AssistantMessageStream.mjs +0 -54
- package/dist/core/accumulators/AssistantMessageStream.mjs.map +0 -1
- package/dist/core/accumulators/assistant-message-accumulator.mjs.map +0 -1
- package/dist/core/index.mjs +0 -26
- package/dist/core/index.mjs.map +0 -1
- package/dist/core/modules/assistant-stream.mjs.map +0 -1
- package/dist/core/modules/text.mjs +0 -52
- package/dist/core/modules/text.mjs.map +0 -1
- package/dist/core/modules/tool-call.mjs.map +0 -1
- package/dist/core/serialization/PlainText.mjs +0 -44
- package/dist/core/serialization/PlainText.mjs.map +0 -1
- package/dist/core/serialization/data-stream/DataStream.mjs.map +0 -1
- package/dist/core/serialization/data-stream/chunk-types.mjs +0 -24
- package/dist/core/serialization/data-stream/chunk-types.mjs.map +0 -1
- package/dist/core/serialization/data-stream/serialization.mjs +0 -30
- package/dist/core/serialization/data-stream/serialization.mjs.map +0 -1
- package/dist/core/tool/ToolCallReader.mjs +0 -315
- package/dist/core/tool/ToolCallReader.mjs.map +0 -1
- package/dist/core/tool/ToolExecutionStream.mjs.map +0 -1
- package/dist/core/tool/ToolResponse.mjs +0 -22
- package/dist/core/tool/ToolResponse.mjs.map +0 -1
- package/dist/core/tool/index.mjs +0 -14
- package/dist/core/tool/index.mjs.map +0 -1
- package/dist/core/tool/tool-types.mjs +0 -1
- package/dist/core/tool/tool-types.mjs.map +0 -1
- package/dist/core/tool/toolResultStream.mjs +0 -78
- package/dist/core/tool/toolResultStream.mjs.map +0 -1
- package/dist/core/tool/type-path-utils.mjs +0 -1
- package/dist/core/tool/type-path-utils.mjs.map +0 -1
- package/dist/core/utils/Counter.mjs +0 -11
- package/dist/core/utils/Counter.mjs.map +0 -1
- package/dist/core/utils/generateId.mjs +0 -10
- package/dist/core/utils/generateId.mjs.map +0 -1
- package/dist/core/utils/stream/AssistantMetaTransformStream.mjs +0 -44
- package/dist/core/utils/stream/AssistantMetaTransformStream.mjs.map +0 -1
- package/dist/core/utils/stream/AssistantTransformStream.mjs +0 -46
- package/dist/core/utils/stream/AssistantTransformStream.mjs.map +0 -1
- package/dist/core/utils/stream/LineDecoderStream.mjs.map +0 -1
- package/dist/core/utils/stream/PipeableTransformStream.mjs +0 -15
- package/dist/core/utils/stream/PipeableTransformStream.mjs.map +0 -1
- package/dist/core/utils/stream/UnderlyingReadable.mjs +0 -1
- package/dist/core/utils/stream/UnderlyingReadable.mjs.map +0 -1
- package/dist/core/utils/stream/merge.mjs +0 -85
- package/dist/core/utils/stream/merge.mjs.map +0 -1
- package/dist/core/utils/stream/path-utils.mjs +0 -61
- package/dist/core/utils/stream/path-utils.mjs.map +0 -1
- package/dist/core/utils/types.mjs +0 -1
- package/dist/core/utils/types.mjs.map +0 -1
- package/dist/core/utils/withPromiseOrValue.mjs +0 -17
- package/dist/core/utils/withPromiseOrValue.mjs.map +0 -1
- package/dist/index.mjs +0 -3
- package/dist/index.mjs.map +0 -1
- package/dist/utils/AsyncIterableStream.mjs +0 -21
- package/dist/utils/AsyncIterableStream.mjs.map +0 -1
- package/dist/utils/json/fix-json.mjs.map +0 -1
- package/dist/utils/json/is-json.mjs +0 -29
- package/dist/utils/json/is-json.mjs.map +0 -1
- package/dist/utils/json/json-value.mjs +0 -1
- package/dist/utils/json/json-value.mjs.map +0 -1
- package/dist/utils/json/parse-partial-json-object.mjs +0 -65
- package/dist/utils/json/parse-partial-json-object.mjs.map +0 -1
- package/dist/utils/promiseWithResolvers.mjs +0 -15
- package/dist/utils/promiseWithResolvers.mjs.map +0 -1
- package/dist/utils.mjs +0 -14
- package/dist/utils.mjs.map +0 -1
- package/utils/README.md +0 -1
- package/utils/package.json +0 -5
|
@@ -1,29 +1,4 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
20
|
-
// src/utils/promiseWithResolvers.ts
|
|
21
|
-
var promiseWithResolvers_exports = {};
|
|
22
|
-
__export(promiseWithResolvers_exports, {
|
|
23
|
-
promiseWithResolvers: () => promiseWithResolvers
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(promiseWithResolvers_exports);
|
|
26
|
-
var promiseWithResolvers = function() {
|
|
1
|
+
const promiseWithResolvers = function() {
|
|
27
2
|
let resolve;
|
|
28
3
|
let reject;
|
|
29
4
|
const promise = new Promise((res, rej) => {
|
|
@@ -33,8 +8,7 @@ var promiseWithResolvers = function() {
|
|
|
33
8
|
if (!resolve || !reject) throw new Error("Failed to create promise");
|
|
34
9
|
return { promise, resolve, reject };
|
|
35
10
|
};
|
|
36
|
-
|
|
37
|
-
0 && (module.exports = {
|
|
11
|
+
export {
|
|
38
12
|
promiseWithResolvers
|
|
39
|
-
}
|
|
13
|
+
};
|
|
40
14
|
//# sourceMappingURL=promiseWithResolvers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/promiseWithResolvers.ts"],"sourcesContent":["export const promiseWithResolvers = function <T>() {\n let resolve: ((value: T | PromiseLike<T>) => void) | undefined;\n let reject: ((reason?: unknown) => void) | undefined;\n const promise = new Promise<T>((res, rej) => {\n resolve = res;\n reject = rej;\n });\n if (!resolve || !reject) throw new Error(\"Failed to create promise\");\n return { promise, resolve, reject };\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/utils/promiseWithResolvers.ts"],"sourcesContent":["export const promiseWithResolvers = function <T>() {\n let resolve: ((value: T | PromiseLike<T>) => void) | undefined;\n let reject: ((reason?: unknown) => void) | undefined;\n const promise = new Promise<T>((res, rej) => {\n resolve = res;\n reject = rej;\n });\n if (!resolve || !reject) throw new Error(\"Failed to create promise\");\n return { promise, resolve, reject };\n};\n"],"mappings":"AAAO,MAAM,uBAAuB,WAAe;AACjD,MAAI;AACJ,MAAI;AACJ,QAAM,UAAU,IAAI,QAAW,CAAC,KAAK,QAAQ;AAC3C,cAAU;AACV,aAAS;AAAA,EACX,CAAC;AACD,MAAI,CAAC,WAAW,CAAC,OAAQ,OAAM,IAAI,MAAM,0BAA0B;AACnE,SAAO,EAAE,SAAS,SAAS,OAAO;AACpC;","names":[]}
|
package/dist/utils.js
CHANGED
|
@@ -1,36 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
20
|
-
// src/utils.ts
|
|
21
|
-
var utils_exports = {};
|
|
22
|
-
__export(utils_exports, {
|
|
23
|
-
asAsyncIterableStream: () => import_AsyncIterableStream.asAsyncIterableStream,
|
|
24
|
-
getPartialJsonObjectFieldState: () => import_parse_partial_json_object.getPartialJsonObjectFieldState,
|
|
25
|
-
parsePartialJsonObject: () => import_parse_partial_json_object.parsePartialJsonObject
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(utils_exports);
|
|
28
|
-
var import_parse_partial_json_object = require("./utils/json/parse-partial-json-object.js");
|
|
29
|
-
var import_AsyncIterableStream = require("./utils/AsyncIterableStream.js");
|
|
30
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
31
|
-
0 && (module.exports = {
|
|
1
|
+
import {
|
|
2
|
+
parsePartialJsonObject,
|
|
3
|
+
getPartialJsonObjectFieldState
|
|
4
|
+
} from "./utils/json/parse-partial-json-object";
|
|
5
|
+
import {
|
|
6
|
+
asAsyncIterableStream
|
|
7
|
+
} from "./utils/AsyncIterableStream";
|
|
8
|
+
export {
|
|
32
9
|
asAsyncIterableStream,
|
|
33
10
|
getPartialJsonObjectFieldState,
|
|
34
11
|
parsePartialJsonObject
|
|
35
|
-
}
|
|
12
|
+
};
|
|
36
13
|
//# sourceMappingURL=utils.js.map
|
package/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils.ts"],"sourcesContent":["export {\n parsePartialJsonObject,\n getPartialJsonObjectFieldState,\n} from \"./utils/json/parse-partial-json-object\";\nexport {\n type AsyncIterableStream,\n asAsyncIterableStream,\n} from \"./utils/AsyncIterableStream\";\nexport type {\n ReadonlyJSONValue,\n ReadonlyJSONArray,\n ReadonlyJSONObject,\n} from \"./utils/json/json-value\";\n"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts"],"sourcesContent":["export {\n parsePartialJsonObject,\n getPartialJsonObjectFieldState,\n} from \"./utils/json/parse-partial-json-object\";\nexport {\n type AsyncIterableStream,\n asAsyncIterableStream,\n} from \"./utils/AsyncIterableStream\";\nexport type {\n ReadonlyJSONValue,\n ReadonlyJSONArray,\n ReadonlyJSONObject,\n} from \"./utils/json/json-value\";\n"],"mappings":"AAAA;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP;AAAA,EAEE;AAAA,OACK;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,30 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assistant-stream",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"exports": {
|
|
6
7
|
".": {
|
|
7
8
|
"types": "./dist/index.d.ts",
|
|
8
|
-
"
|
|
9
|
-
"require": "./dist/index.js"
|
|
9
|
+
"default": "./dist/index.js"
|
|
10
10
|
},
|
|
11
11
|
"./ai-sdk": {
|
|
12
12
|
"types": "./dist/ai-sdk.d.ts",
|
|
13
|
-
"
|
|
14
|
-
"require": "./dist/ai-sdk.js"
|
|
13
|
+
"default": "./dist/ai-sdk.js"
|
|
15
14
|
},
|
|
16
15
|
"./utils": {
|
|
17
16
|
"types": "./dist/utils.d.ts",
|
|
18
|
-
"
|
|
19
|
-
"require": "./dist/utils.js"
|
|
17
|
+
"default": "./dist/utils.js"
|
|
20
18
|
}
|
|
21
19
|
},
|
|
22
20
|
"source": "./src/index.ts",
|
|
23
21
|
"main": "./dist/index.js",
|
|
24
|
-
"module": "./dist/index.mjs",
|
|
25
22
|
"types": "./dist/index.d.ts",
|
|
26
23
|
"files": [
|
|
27
24
|
"dist",
|
|
25
|
+
"src",
|
|
28
26
|
"ai-sdk",
|
|
29
27
|
"utils",
|
|
30
28
|
"README.md"
|
|
@@ -47,7 +45,7 @@
|
|
|
47
45
|
"homepage": "https://www.assistant-ui.com/",
|
|
48
46
|
"repository": {
|
|
49
47
|
"type": "git",
|
|
50
|
-
"url": "
|
|
48
|
+
"url": "https://github.com/assistant-ui/assistant-ui/tree/main/packages/assistant-stream"
|
|
51
49
|
},
|
|
52
50
|
"bugs": {
|
|
53
51
|
"url": "https://github.com/assistant-ui/assistant-ui/issues"
|
|
@@ -1,19 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import type { TextStreamPart, ObjectStreamPart, Tool } from "ai";
|
|
2
|
+
import { AssistantStream } from "../core/AssistantStream";
|
|
3
|
+
import { AssistantTransformStream } from "../core/utils/stream/AssistantTransformStream";
|
|
4
|
+
import { ToolCallStreamController } from "../core/modules/tool-call";
|
|
5
|
+
import { ReadonlyJSONValue } from "../utils/json/json-value";
|
|
6
|
+
|
|
7
|
+
export const fromStreamText = (
|
|
8
|
+
stream: ReadableStream<TextStreamPart<Record<string, Tool>>>,
|
|
9
|
+
): AssistantStream => {
|
|
10
|
+
const toolControllers = new Map<string, ToolCallStreamController>();
|
|
11
|
+
let currentToolCallArgsText: ToolCallStreamController | undefined;
|
|
12
|
+
|
|
6
13
|
const endCurrentToolCallArgsText = () => {
|
|
7
14
|
if (!currentToolCallArgsText) return;
|
|
8
15
|
currentToolCallArgsText.argsText.close();
|
|
9
|
-
currentToolCallArgsText =
|
|
16
|
+
currentToolCallArgsText = undefined;
|
|
10
17
|
};
|
|
11
|
-
|
|
18
|
+
|
|
19
|
+
const transformer = new AssistantTransformStream<
|
|
20
|
+
TextStreamPart<Record<string, Tool>>
|
|
21
|
+
>({
|
|
12
22
|
transform(chunk, controller) {
|
|
13
23
|
const { type } = chunk;
|
|
14
|
-
|
|
24
|
+
|
|
25
|
+
if (
|
|
26
|
+
type !== "tool-call-delta" &&
|
|
27
|
+
type !== "tool-call" &&
|
|
28
|
+
type !== "error" &&
|
|
29
|
+
(type as string) !== "tool-result"
|
|
30
|
+
) {
|
|
15
31
|
endCurrentToolCallArgsText();
|
|
16
32
|
}
|
|
33
|
+
|
|
17
34
|
switch (type) {
|
|
18
35
|
case "text-delta": {
|
|
19
36
|
const { textDelta } = chunk;
|
|
@@ -29,7 +46,7 @@ var fromStreamText = (stream) => {
|
|
|
29
46
|
const { toolCallId, toolName } = chunk;
|
|
30
47
|
currentToolCallArgsText = controller.addToolCallPart({
|
|
31
48
|
toolCallId,
|
|
32
|
-
toolName
|
|
49
|
+
toolName,
|
|
33
50
|
});
|
|
34
51
|
toolControllers.set(toolCallId, currentToolCallArgsText);
|
|
35
52
|
break;
|
|
@@ -41,12 +58,15 @@ var fromStreamText = (stream) => {
|
|
|
41
58
|
toolController.argsText.append(argsTextDelta);
|
|
42
59
|
break;
|
|
43
60
|
}
|
|
44
|
-
case "tool-result": {
|
|
45
|
-
const { toolCallId, result } = chunk
|
|
61
|
+
case "tool-result" as string: {
|
|
62
|
+
const { toolCallId, result } = chunk as unknown as {
|
|
63
|
+
toolCallId: string;
|
|
64
|
+
result: ReadonlyJSONValue;
|
|
65
|
+
};
|
|
46
66
|
const toolController = toolControllers.get(toolCallId);
|
|
47
67
|
if (!toolController) throw new Error("Tool call not found");
|
|
48
68
|
toolController.setResponse({
|
|
49
|
-
result
|
|
69
|
+
result,
|
|
50
70
|
});
|
|
51
71
|
toolController.close();
|
|
52
72
|
toolControllers.delete(toolCallId);
|
|
@@ -56,18 +76,19 @@ var fromStreamText = (stream) => {
|
|
|
56
76
|
const { toolCallId, toolName, args } = chunk;
|
|
57
77
|
const toolController = controller.addToolCallPart({
|
|
58
78
|
toolCallId,
|
|
59
|
-
toolName
|
|
79
|
+
toolName,
|
|
60
80
|
});
|
|
61
81
|
toolController.argsText.append(JSON.stringify(args));
|
|
62
82
|
toolController.argsText.close();
|
|
63
83
|
toolControllers.set(toolCallId, toolController);
|
|
64
84
|
break;
|
|
65
85
|
}
|
|
86
|
+
|
|
66
87
|
case "step-start":
|
|
67
88
|
controller.enqueue({
|
|
68
89
|
type: "step-start",
|
|
69
90
|
path: [],
|
|
70
|
-
messageId: chunk.messageId
|
|
91
|
+
messageId: chunk.messageId,
|
|
71
92
|
});
|
|
72
93
|
break;
|
|
73
94
|
case "step-finish":
|
|
@@ -76,43 +97,49 @@ var fromStreamText = (stream) => {
|
|
|
76
97
|
path: [],
|
|
77
98
|
finishReason: chunk.finishReason,
|
|
78
99
|
usage: chunk.usage,
|
|
79
|
-
isContinued: chunk.isContinued
|
|
100
|
+
isContinued: chunk.isContinued,
|
|
80
101
|
});
|
|
81
102
|
break;
|
|
82
103
|
case "error":
|
|
83
104
|
controller.enqueue({
|
|
84
105
|
type: "error",
|
|
85
106
|
path: [],
|
|
86
|
-
error: String(chunk.error)
|
|
107
|
+
error: String(chunk.error),
|
|
87
108
|
});
|
|
88
109
|
break;
|
|
110
|
+
|
|
89
111
|
case "finish": {
|
|
90
112
|
controller.enqueue({
|
|
91
113
|
type: "message-finish",
|
|
92
114
|
path: [],
|
|
93
115
|
finishReason: chunk.finishReason,
|
|
94
|
-
usage: chunk.usage
|
|
116
|
+
usage: chunk.usage,
|
|
95
117
|
});
|
|
96
118
|
break;
|
|
97
119
|
}
|
|
120
|
+
|
|
98
121
|
case "source":
|
|
99
122
|
controller.appendSource({
|
|
100
123
|
type: "source",
|
|
101
|
-
...chunk.source
|
|
124
|
+
...chunk.source,
|
|
102
125
|
});
|
|
103
126
|
break;
|
|
127
|
+
|
|
104
128
|
case "file":
|
|
105
129
|
controller.appendFile({
|
|
106
130
|
type: "file",
|
|
107
131
|
mimeType: chunk.mimeType,
|
|
108
|
-
data: chunk.base64
|
|
132
|
+
data: chunk.base64,
|
|
109
133
|
});
|
|
110
134
|
break;
|
|
135
|
+
|
|
111
136
|
case "reasoning-signature":
|
|
112
137
|
case "redacted-reasoning":
|
|
138
|
+
// ignore these for now
|
|
113
139
|
break;
|
|
140
|
+
|
|
114
141
|
default: {
|
|
115
|
-
const unhandledType = type;
|
|
142
|
+
const unhandledType: never = type;
|
|
116
143
|
throw new Error(`Unhandled chunk type: ${unhandledType}`);
|
|
117
144
|
}
|
|
118
145
|
}
|
|
@@ -122,13 +149,18 @@ var fromStreamText = (stream) => {
|
|
|
122
149
|
toolController.close();
|
|
123
150
|
}
|
|
124
151
|
toolControllers.clear();
|
|
125
|
-
}
|
|
152
|
+
},
|
|
126
153
|
});
|
|
154
|
+
|
|
127
155
|
return stream.pipeThrough(transformer);
|
|
128
156
|
};
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
157
|
+
|
|
158
|
+
export const fromStreamObject = (
|
|
159
|
+
stream: ReadableStream<ObjectStreamPart<unknown>>,
|
|
160
|
+
toolName: string,
|
|
161
|
+
): AssistantStream => {
|
|
162
|
+
let toolCall!: ToolCallStreamController;
|
|
163
|
+
const transformer = new AssistantTransformStream<ObjectStreamPart<unknown>>({
|
|
132
164
|
start(controller) {
|
|
133
165
|
toolCall = controller.addToolCallPart(toolName);
|
|
134
166
|
},
|
|
@@ -143,31 +175,30 @@ var fromStreamObject = (stream, toolName) => {
|
|
|
143
175
|
case "finish": {
|
|
144
176
|
toolCall.argsText.close();
|
|
145
177
|
toolCall.setResponse({
|
|
146
|
-
result: "{}"
|
|
178
|
+
result: "{}",
|
|
147
179
|
});
|
|
148
180
|
break;
|
|
149
181
|
}
|
|
182
|
+
|
|
150
183
|
case "object":
|
|
151
184
|
break;
|
|
185
|
+
|
|
152
186
|
case "error": {
|
|
153
187
|
controller.enqueue({
|
|
154
188
|
type: "error",
|
|
155
189
|
path: [],
|
|
156
|
-
error: String(chunk.error)
|
|
190
|
+
error: String(chunk.error),
|
|
157
191
|
});
|
|
158
192
|
break;
|
|
159
193
|
}
|
|
194
|
+
|
|
160
195
|
default: {
|
|
161
|
-
const unhandledType = type;
|
|
196
|
+
const unhandledType: never = type;
|
|
162
197
|
throw new Error(`Unhandled chunk type: ${unhandledType}`);
|
|
163
198
|
}
|
|
164
199
|
}
|
|
165
|
-
}
|
|
200
|
+
},
|
|
166
201
|
});
|
|
202
|
+
|
|
167
203
|
return stream.pipeThrough(transformer);
|
|
168
204
|
};
|
|
169
|
-
export {
|
|
170
|
-
fromStreamObject,
|
|
171
|
-
fromStreamText
|
|
172
|
-
};
|
|
173
|
-
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,23 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
import { AssistantTransformStream } from "../core/utils/stream/AssistantTransformStream
|
|
3
|
-
|
|
1
|
+
import type { LanguageModelV1StreamPart } from "ai";
|
|
2
|
+
import { AssistantTransformStream } from "../core/utils/stream/AssistantTransformStream";
|
|
3
|
+
import { ToolCallStreamController } from "../core/modules/tool-call";
|
|
4
|
+
|
|
5
|
+
function bufferToBase64(buffer: Uint8Array) {
|
|
4
6
|
return btoa(String.fromCharCode(...buffer));
|
|
5
7
|
}
|
|
6
|
-
|
|
8
|
+
|
|
9
|
+
export class LanguageModelV1StreamDecoder extends AssistantTransformStream<LanguageModelV1StreamPart> {
|
|
7
10
|
constructor() {
|
|
8
|
-
let currentToolCall
|
|
11
|
+
let currentToolCall:
|
|
12
|
+
| { toolCallId: string; controller: ToolCallStreamController }
|
|
13
|
+
| undefined;
|
|
14
|
+
|
|
9
15
|
const endCurrentToolCall = () => {
|
|
10
16
|
if (!currentToolCall) return;
|
|
11
17
|
currentToolCall.controller.argsText.close();
|
|
12
18
|
currentToolCall.controller.close();
|
|
13
|
-
currentToolCall =
|
|
19
|
+
currentToolCall = undefined;
|
|
14
20
|
};
|
|
21
|
+
|
|
15
22
|
super({
|
|
16
23
|
transform(chunk, controller) {
|
|
17
24
|
const { type } = chunk;
|
|
18
25
|
if (type === "text-delta" || type === "reasoning") {
|
|
19
26
|
endCurrentToolCall();
|
|
20
27
|
}
|
|
28
|
+
|
|
21
29
|
switch (type) {
|
|
22
30
|
case "text-delta": {
|
|
23
31
|
controller.appendText(chunk.textDelta);
|
|
@@ -27,21 +35,27 @@ var LanguageModelV1StreamDecoder = class extends AssistantTransformStream {
|
|
|
27
35
|
controller.appendReasoning(chunk.textDelta);
|
|
28
36
|
break;
|
|
29
37
|
}
|
|
38
|
+
|
|
30
39
|
case "source": {
|
|
31
40
|
controller.appendSource({
|
|
32
41
|
type: "source",
|
|
33
|
-
...chunk.source
|
|
42
|
+
...chunk.source,
|
|
34
43
|
});
|
|
35
44
|
break;
|
|
36
45
|
}
|
|
46
|
+
|
|
37
47
|
case "file": {
|
|
38
48
|
controller.appendFile({
|
|
39
49
|
type: "file",
|
|
40
50
|
mimeType: chunk.mimeType,
|
|
41
|
-
data:
|
|
51
|
+
data:
|
|
52
|
+
typeof chunk.data === "string"
|
|
53
|
+
? chunk.data
|
|
54
|
+
: bufferToBase64(chunk.data),
|
|
42
55
|
});
|
|
43
56
|
break;
|
|
44
57
|
}
|
|
58
|
+
|
|
45
59
|
case "tool-call-delta": {
|
|
46
60
|
const { toolCallId, toolName, argsTextDelta } = chunk;
|
|
47
61
|
if (currentToolCall?.toolCallId === toolCallId) {
|
|
@@ -52,25 +66,29 @@ var LanguageModelV1StreamDecoder = class extends AssistantTransformStream {
|
|
|
52
66
|
toolCallId,
|
|
53
67
|
controller: controller.addToolCallPart({
|
|
54
68
|
toolCallId,
|
|
55
|
-
toolName
|
|
56
|
-
})
|
|
69
|
+
toolName,
|
|
70
|
+
}),
|
|
57
71
|
};
|
|
58
72
|
currentToolCall.controller.argsText.append(argsTextDelta);
|
|
59
73
|
}
|
|
74
|
+
|
|
60
75
|
break;
|
|
61
76
|
}
|
|
77
|
+
|
|
62
78
|
case "tool-call": {
|
|
63
79
|
const { toolCallId, toolName, args } = chunk;
|
|
80
|
+
|
|
64
81
|
if (currentToolCall?.toolCallId === toolCallId) {
|
|
65
82
|
currentToolCall.controller.argsText.close();
|
|
66
83
|
} else {
|
|
67
84
|
const toolController = controller.addToolCallPart({
|
|
68
85
|
toolCallId,
|
|
69
86
|
toolName,
|
|
70
|
-
argsText: args
|
|
87
|
+
argsText: args,
|
|
71
88
|
});
|
|
72
89
|
toolController.close();
|
|
73
90
|
}
|
|
91
|
+
|
|
74
92
|
break;
|
|
75
93
|
}
|
|
76
94
|
case "finish": {
|
|
@@ -78,29 +96,27 @@ var LanguageModelV1StreamDecoder = class extends AssistantTransformStream {
|
|
|
78
96
|
type: "message-finish",
|
|
79
97
|
finishReason: chunk.finishReason,
|
|
80
98
|
usage: chunk.usage,
|
|
81
|
-
path: []
|
|
99
|
+
path: [],
|
|
82
100
|
});
|
|
83
101
|
controller.close();
|
|
84
102
|
break;
|
|
85
103
|
}
|
|
104
|
+
|
|
86
105
|
case "error":
|
|
87
106
|
case "response-metadata":
|
|
88
107
|
case "reasoning-signature":
|
|
89
108
|
case "redacted-reasoning":
|
|
90
109
|
break;
|
|
110
|
+
|
|
91
111
|
default: {
|
|
92
|
-
const unhandledType = type;
|
|
112
|
+
const unhandledType: never = type;
|
|
93
113
|
throw new Error(`Unhandled chunk type: ${unhandledType}`);
|
|
94
114
|
}
|
|
95
115
|
}
|
|
96
116
|
},
|
|
97
117
|
flush() {
|
|
98
118
|
endCurrentToolCall();
|
|
99
|
-
}
|
|
119
|
+
},
|
|
100
120
|
});
|
|
101
121
|
}
|
|
102
|
-
}
|
|
103
|
-
export {
|
|
104
|
-
LanguageModelV1StreamDecoder
|
|
105
|
-
};
|
|
106
|
-
//# sourceMappingURL=language-model.mjs.map
|
|
122
|
+
}
|
package/src/ai-sdk.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { AssistantStreamChunk } from "./AssistantStreamChunk";
|
|
2
|
+
|
|
3
|
+
export type AssistantStream = ReadableStream<AssistantStreamChunk>;
|
|
4
|
+
|
|
5
|
+
export type AssistantStreamEncoder = ReadableWritablePair<
|
|
6
|
+
Uint8Array,
|
|
7
|
+
AssistantStreamChunk
|
|
8
|
+
> & {
|
|
9
|
+
headers?: Headers;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const AssistantStream = {
|
|
13
|
+
toResponse(stream: AssistantStream, transformer: AssistantStreamEncoder) {
|
|
14
|
+
return new Response(AssistantStream.toByteStream(stream, transformer), {
|
|
15
|
+
headers: transformer.headers ?? {},
|
|
16
|
+
});
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
fromResponse(
|
|
20
|
+
response: Response,
|
|
21
|
+
transformer: ReadableWritablePair<AssistantStreamChunk, Uint8Array>,
|
|
22
|
+
) {
|
|
23
|
+
return AssistantStream.fromByteStream(response.body!, transformer);
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
toByteStream(
|
|
27
|
+
stream: AssistantStream,
|
|
28
|
+
transformer: ReadableWritablePair<Uint8Array, AssistantStreamChunk>,
|
|
29
|
+
) {
|
|
30
|
+
return stream.pipeThrough(transformer);
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
fromByteStream(
|
|
34
|
+
readable: ReadableStream<Uint8Array>,
|
|
35
|
+
transformer: ReadableWritablePair<AssistantStreamChunk, Uint8Array>,
|
|
36
|
+
) {
|
|
37
|
+
return readable.pipeThrough(transformer);
|
|
38
|
+
},
|
|
39
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { ReadonlyJSONValue } from "../utils/json/json-value";
|
|
2
|
+
|
|
3
|
+
export type PartInit =
|
|
4
|
+
| {
|
|
5
|
+
readonly type: "text" | "reasoning";
|
|
6
|
+
}
|
|
7
|
+
| {
|
|
8
|
+
readonly type: "tool-call";
|
|
9
|
+
readonly toolCallId: string;
|
|
10
|
+
readonly toolName: string;
|
|
11
|
+
}
|
|
12
|
+
| {
|
|
13
|
+
readonly type: "source";
|
|
14
|
+
readonly sourceType: "url";
|
|
15
|
+
readonly id: string;
|
|
16
|
+
readonly url: string;
|
|
17
|
+
readonly title?: string;
|
|
18
|
+
}
|
|
19
|
+
| {
|
|
20
|
+
readonly type: "file";
|
|
21
|
+
readonly data: string;
|
|
22
|
+
readonly mimeType: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type AssistantStreamChunk = { readonly path: readonly number[] } & (
|
|
26
|
+
| {
|
|
27
|
+
readonly type: "part-start";
|
|
28
|
+
readonly part: PartInit;
|
|
29
|
+
}
|
|
30
|
+
| {
|
|
31
|
+
readonly type: "part-finish";
|
|
32
|
+
}
|
|
33
|
+
| {
|
|
34
|
+
readonly type: "tool-call-args-text-finish";
|
|
35
|
+
}
|
|
36
|
+
| {
|
|
37
|
+
readonly type: "text-delta";
|
|
38
|
+
readonly textDelta: string;
|
|
39
|
+
}
|
|
40
|
+
| {
|
|
41
|
+
readonly type: "annotations";
|
|
42
|
+
readonly annotations: ReadonlyJSONValue[];
|
|
43
|
+
}
|
|
44
|
+
| {
|
|
45
|
+
readonly type: "data";
|
|
46
|
+
readonly data: ReadonlyJSONValue[];
|
|
47
|
+
}
|
|
48
|
+
| {
|
|
49
|
+
readonly type: "step-start";
|
|
50
|
+
readonly messageId: string;
|
|
51
|
+
}
|
|
52
|
+
| {
|
|
53
|
+
readonly type: "step-finish";
|
|
54
|
+
readonly finishReason:
|
|
55
|
+
| "stop"
|
|
56
|
+
| "length"
|
|
57
|
+
| "content-filter"
|
|
58
|
+
| "tool-calls"
|
|
59
|
+
| "error"
|
|
60
|
+
| "other"
|
|
61
|
+
| "unknown";
|
|
62
|
+
readonly usage: {
|
|
63
|
+
readonly promptTokens: number;
|
|
64
|
+
readonly completionTokens: number;
|
|
65
|
+
};
|
|
66
|
+
readonly isContinued: boolean;
|
|
67
|
+
}
|
|
68
|
+
| {
|
|
69
|
+
readonly type: "message-finish";
|
|
70
|
+
readonly finishReason:
|
|
71
|
+
| "stop"
|
|
72
|
+
| "length"
|
|
73
|
+
| "content-filter"
|
|
74
|
+
| "tool-calls"
|
|
75
|
+
| "error"
|
|
76
|
+
| "other"
|
|
77
|
+
| "unknown";
|
|
78
|
+
readonly usage: {
|
|
79
|
+
readonly promptTokens: number;
|
|
80
|
+
readonly completionTokens: number;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
| {
|
|
84
|
+
readonly type: "result";
|
|
85
|
+
readonly artifact?: ReadonlyJSONValue | undefined;
|
|
86
|
+
readonly result: ReadonlyJSONValue;
|
|
87
|
+
readonly isError: boolean;
|
|
88
|
+
}
|
|
89
|
+
| {
|
|
90
|
+
readonly type: "error";
|
|
91
|
+
readonly error: string;
|
|
92
|
+
}
|
|
93
|
+
);
|