assistant-stream 0.2.0 → 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.
Files changed (129) hide show
  1. package/dist/ai-sdk/index.d.ts +5 -0
  2. package/dist/ai-sdk/index.d.ts.map +1 -0
  3. package/dist/ai-sdk/language-model.d.ts +6 -0
  4. package/dist/ai-sdk/language-model.d.ts.map +1 -0
  5. package/dist/ai-sdk.d.ts +3 -0
  6. package/dist/ai-sdk.d.ts.map +1 -0
  7. package/dist/core/AssistantStream.d.ts +12 -0
  8. package/dist/core/AssistantStream.d.ts.map +1 -0
  9. package/dist/core/AssistantStreamChunk.d.ts +64 -0
  10. package/dist/core/AssistantStreamChunk.d.ts.map +1 -0
  11. package/dist/core/accumulators/AssistantMessageStream.d.ts +13 -0
  12. package/dist/core/accumulators/AssistantMessageStream.d.ts.map +1 -0
  13. package/dist/core/accumulators/assistant-message-accumulator.d.ts +8 -0
  14. package/dist/core/accumulators/assistant-message-accumulator.d.ts.map +1 -0
  15. package/dist/core/index.d.ts +11 -0
  16. package/dist/core/index.d.ts.map +1 -0
  17. package/dist/core/modules/assistant-stream.d.ts +31 -0
  18. package/dist/core/modules/assistant-stream.d.ts.map +1 -0
  19. package/dist/core/modules/text.d.ts +9 -0
  20. package/dist/core/modules/text.d.ts.map +1 -0
  21. package/dist/core/modules/tool-call.d.ts +13 -0
  22. package/dist/core/modules/tool-call.d.ts.map +1 -0
  23. package/dist/core/serialization/PlainText.d.ts +11 -0
  24. package/dist/core/serialization/PlainText.d.ts.map +1 -0
  25. package/dist/core/serialization/data-stream/DataStream.d.ts +11 -0
  26. package/dist/core/serialization/data-stream/DataStream.d.ts.map +1 -0
  27. package/dist/core/serialization/data-stream/chunk-types.d.ts +86 -0
  28. package/dist/core/serialization/data-stream/chunk-types.d.ts.map +1 -0
  29. package/dist/core/serialization/data-stream/serialization.d.ts +8 -0
  30. package/dist/core/serialization/data-stream/serialization.d.ts.map +1 -0
  31. package/dist/core/tool/ToolCallReader.d.ts +33 -0
  32. package/dist/core/tool/ToolCallReader.d.ts.map +1 -0
  33. package/dist/core/tool/ToolExecutionStream.d.ts +24 -0
  34. package/dist/core/tool/ToolExecutionStream.d.ts.map +1 -0
  35. package/dist/core/tool/ToolResponse.d.ts +17 -0
  36. package/dist/core/tool/ToolResponse.d.ts.map +1 -0
  37. package/dist/core/tool/index.d.ts +6 -0
  38. package/dist/core/tool/index.d.ts.map +1 -0
  39. package/dist/core/tool/tool-types.d.ts +76 -0
  40. package/dist/core/tool/tool-types.d.ts.map +1 -0
  41. package/dist/core/tool/toolResultStream.d.ts +6 -0
  42. package/dist/core/tool/toolResultStream.d.ts.map +1 -0
  43. package/dist/core/tool/type-path-utils.d.ts +23 -0
  44. package/dist/core/tool/type-path-utils.d.ts.map +1 -0
  45. package/dist/core/utils/Counter.d.ts +5 -0
  46. package/dist/core/utils/Counter.d.ts.map +1 -0
  47. package/dist/core/utils/generateId.d.ts +2 -0
  48. package/dist/core/utils/generateId.d.ts.map +1 -0
  49. package/dist/core/utils/stream/AssistantMetaTransformStream.d.ts +20 -0
  50. package/dist/core/utils/stream/AssistantMetaTransformStream.d.ts.map +1 -0
  51. package/dist/core/utils/stream/AssistantTransformStream.d.ts +15 -0
  52. package/dist/core/utils/stream/AssistantTransformStream.d.ts.map +1 -0
  53. package/dist/core/utils/stream/LineDecoderStream.d.ts +5 -0
  54. package/dist/core/utils/stream/LineDecoderStream.d.ts.map +1 -0
  55. package/dist/core/utils/stream/PipeableTransformStream.d.ts +4 -0
  56. package/dist/core/utils/stream/PipeableTransformStream.d.ts.map +1 -0
  57. package/dist/core/utils/stream/UnderlyingReadable.d.ts +6 -0
  58. package/dist/core/utils/stream/UnderlyingReadable.d.ts.map +1 -0
  59. package/dist/core/utils/stream/merge.d.ts +9 -0
  60. package/dist/core/utils/stream/merge.d.ts.map +1 -0
  61. package/dist/core/utils/stream/path-utils.d.ts +12 -0
  62. package/dist/core/utils/stream/path-utils.d.ts.map +1 -0
  63. package/dist/core/utils/types.d.ts +102 -0
  64. package/dist/core/utils/types.d.ts.map +1 -0
  65. package/dist/core/utils/withPromiseOrValue.d.ts +2 -0
  66. package/dist/core/utils/withPromiseOrValue.d.ts.map +1 -0
  67. package/dist/index.d.ts +2 -0
  68. package/dist/index.d.ts.map +1 -0
  69. package/dist/utils/AsyncIterableStream.d.ts +3 -0
  70. package/dist/utils/AsyncIterableStream.d.ts.map +1 -0
  71. package/dist/utils/json/fix-json.d.ts +2 -0
  72. package/dist/utils/json/fix-json.d.ts.map +1 -0
  73. package/dist/utils/json/is-json.d.ts +5 -0
  74. package/dist/utils/json/is-json.d.ts.map +1 -0
  75. package/dist/utils/json/json-value.d.ts +6 -0
  76. package/dist/utils/json/json-value.d.ts.map +1 -0
  77. package/dist/utils/json/parse-partial-json-object.d.ts +14 -0
  78. package/dist/utils/json/parse-partial-json-object.d.ts.map +1 -0
  79. package/dist/utils/json/parse-partial-json-object.test.d.ts +2 -0
  80. package/dist/utils/json/parse-partial-json-object.test.d.ts.map +1 -0
  81. package/dist/utils/promiseWithResolvers.d.ts +6 -0
  82. package/dist/utils/promiseWithResolvers.d.ts.map +1 -0
  83. package/dist/utils.d.ts +4 -0
  84. package/dist/utils.d.ts.map +1 -0
  85. package/package.json +3 -2
  86. package/src/ai-sdk/index.ts +204 -0
  87. package/src/ai-sdk/language-model.ts +122 -0
  88. package/src/ai-sdk.ts +2 -0
  89. package/src/core/AssistantStream.ts +39 -0
  90. package/src/core/AssistantStreamChunk.ts +93 -0
  91. package/src/core/accumulators/AssistantMessageStream.ts +56 -0
  92. package/src/core/accumulators/assistant-message-accumulator.ts +394 -0
  93. package/src/core/index.ts +17 -0
  94. package/src/core/modules/assistant-stream.ts +264 -0
  95. package/src/core/modules/text.ts +64 -0
  96. package/src/core/modules/tool-call.ts +105 -0
  97. package/src/core/serialization/PlainText.ts +68 -0
  98. package/src/core/serialization/data-stream/DataStream.ts +355 -0
  99. package/src/core/serialization/data-stream/chunk-types.ts +93 -0
  100. package/src/core/serialization/data-stream/serialization.ts +32 -0
  101. package/src/core/tool/ToolCallReader.ts +432 -0
  102. package/src/core/tool/ToolExecutionStream.ts +180 -0
  103. package/src/core/tool/ToolResponse.ts +31 -0
  104. package/src/core/tool/index.ts +8 -0
  105. package/src/core/tool/tool-types.ts +107 -0
  106. package/src/core/tool/toolResultStream.ts +119 -0
  107. package/src/core/tool/type-path-utils.ts +36 -0
  108. package/src/core/utils/Counter.ts +7 -0
  109. package/src/core/utils/generateId.tsx +6 -0
  110. package/src/core/utils/stream/AssistantMetaTransformStream.ts +74 -0
  111. package/src/core/utils/stream/AssistantTransformStream.ts +74 -0
  112. package/src/core/utils/stream/LineDecoderStream.ts +29 -0
  113. package/src/core/utils/stream/PipeableTransformStream.ts +10 -0
  114. package/src/core/utils/stream/UnderlyingReadable.ts +5 -0
  115. package/src/core/utils/stream/merge.ts +212 -0
  116. package/src/core/utils/stream/path-utils.ts +71 -0
  117. package/src/core/utils/types.ts +150 -0
  118. package/src/core/utils/withPromiseOrValue.ts +20 -0
  119. package/src/index.ts +1 -0
  120. package/src/utils/AsyncIterableStream.ts +24 -0
  121. package/src/utils/json/fix-json.ts +488 -0
  122. package/src/utils/json/is-json.ts +43 -0
  123. package/src/utils/json/json-value.ts +13 -0
  124. package/src/utils/json/parse-partial-json-object.test.ts +225 -0
  125. package/src/utils/json/parse-partial-json-object.ts +103 -0
  126. package/src/utils/promiseWithResolvers.ts +10 -0
  127. package/src/utils.ts +13 -0
  128. package/utils/README.md +0 -1
  129. package/utils/package.json +0 -5
@@ -0,0 +1,225 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import {
3
+ parsePartialJsonObject,
4
+ getPartialJsonObjectFieldState,
5
+ } from "./parse-partial-json-object";
6
+
7
+ type PartialJsonTest = {
8
+ input: string;
9
+ query: (string | number)[];
10
+ result: "partial" | "complete" | "undefined";
11
+ };
12
+
13
+ const tests: PartialJsonTest[] = [
14
+ // empty query
15
+ {
16
+ input: ``,
17
+ query: [],
18
+ result: "partial",
19
+ },
20
+ {
21
+ input: `{`,
22
+ query: [],
23
+ result: "partial",
24
+ },
25
+ {
26
+ input: `{}`,
27
+ query: [],
28
+ result: "complete",
29
+ },
30
+ // field query (missing)
31
+ {
32
+ input: ``,
33
+ query: ["test"],
34
+ result: "partial",
35
+ },
36
+ {
37
+ input: `{`,
38
+ query: ["test"],
39
+ result: "partial",
40
+ },
41
+ {
42
+ input: `{}`,
43
+ query: ["test"],
44
+ result: "complete",
45
+ },
46
+ // field query (partial)
47
+ {
48
+ input: `{"foo": `,
49
+ query: ["foo"],
50
+ result: "partial",
51
+ },
52
+ {
53
+ input: `{"foo": "b`,
54
+ query: ["foo"],
55
+ result: "partial",
56
+ },
57
+ {
58
+ input: `{"foo": 123`,
59
+ query: ["foo"],
60
+ result: "partial",
61
+ },
62
+ {
63
+ input: `{"foo": {`,
64
+ query: ["foo"],
65
+ result: "partial",
66
+ },
67
+ {
68
+ input: `{"foo": [`,
69
+ query: ["foo"],
70
+ result: "partial",
71
+ },
72
+ // field query (complete)
73
+ {
74
+ input: `{"foo": 123,`,
75
+ query: ["foo"],
76
+ result: "complete",
77
+ },
78
+ {
79
+ input: `{"foo": "b"`,
80
+ query: ["foo"],
81
+ result: "complete",
82
+ },
83
+ {
84
+ input: `{"foo": nu`,
85
+ query: ["foo"],
86
+ result: "complete",
87
+ },
88
+ {
89
+ input: `{"foo": fa`,
90
+ query: ["foo"],
91
+ result: "complete",
92
+ },
93
+ {
94
+ input: `{"foo": tr`,
95
+ query: ["foo"],
96
+ result: "complete",
97
+ },
98
+ {
99
+ input: `{"foo": {}`,
100
+ query: ["foo"],
101
+ result: "complete",
102
+ },
103
+ {
104
+ input: `{"foo": []`,
105
+ query: ["foo"],
106
+ result: "complete",
107
+ },
108
+ // field query (nested)
109
+ {
110
+ input: `{"foo": [{ "bar": "abc`,
111
+ query: ["foo", "0", "bar"],
112
+ result: "partial",
113
+ },
114
+ {
115
+ input: `{"foo": [{ "bar": "abc"`,
116
+ query: ["foo", "0", "bar"],
117
+ result: "complete",
118
+ },
119
+ {
120
+ input: `{"foo": [{ "bar": 123`,
121
+ query: ["foo", "0", "bar"],
122
+ result: "partial",
123
+ },
124
+ {
125
+ input: `{"foo": [{ "bar": nu`,
126
+ query: ["foo", 0, "bar"],
127
+ result: "complete",
128
+ },
129
+ // field non-existent
130
+ {
131
+ input: `{"bar": "hello"`,
132
+ query: ["foo"],
133
+ result: "partial",
134
+ },
135
+ {
136
+ input: `{"bar": "hello"}`,
137
+ query: ["foo"],
138
+ result: "complete",
139
+ },
140
+ // mismatch type
141
+ {
142
+ input: `{"foo": 123`,
143
+ query: ["foo", "bar", "baz"],
144
+ result: "partial",
145
+ },
146
+ {
147
+ input: `{"foo": fa`,
148
+ query: ["foo", "bar", "baz"],
149
+ result: "complete",
150
+ },
151
+ // numeric keys
152
+ {
153
+ input: `{"1": "value","0":"`,
154
+ query: ["0"],
155
+ result: "partial",
156
+ },
157
+ {
158
+ input: `{"1": "value","0":"`,
159
+ query: ["1"],
160
+ result: "complete",
161
+ },
162
+ {
163
+ input: `{"foo": "value", "0": "`,
164
+ query: ["0"],
165
+ result: "partial",
166
+ },
167
+ {
168
+ input: `{"foo": "value", "0": "`,
169
+ query: ["foo"],
170
+ result: "complete",
171
+ },
172
+ // duplicate keys
173
+ {
174
+ input: `{"foo": "foo","bar":"bar","foo": "`,
175
+ query: ["foo"],
176
+ result: "partial",
177
+ },
178
+ {
179
+ input: `{"foo": "foo","bar":"bar","foo": "`,
180
+ query: ["bar"],
181
+ result: "complete",
182
+ },
183
+ {
184
+ input: `{"foo": [1,"a",{"b":1},[],{},[1,[[2]]],{"1":1,"t":1`,
185
+ query: ["foo", 6, 1],
186
+ result: "complete",
187
+ },
188
+ {
189
+ input: `{"foo": [1,"\",{"b":1},[],{},[1,[[2]]],{"1":1,"t":1`,
190
+ query: ["foo", 6, "t"],
191
+ result: "partial",
192
+ },
193
+ {
194
+ input: `{"\\"": "t`,
195
+ query: ['"'],
196
+ result: "partial",
197
+ },
198
+ {
199
+ input: `{"\\"": "t"`,
200
+ query: ['"'],
201
+ result: "complete",
202
+ },
203
+ {
204
+ input: `{"\\u25CF": "t`,
205
+ query: ["\u25CF"],
206
+ result: "partial",
207
+ },
208
+ ];
209
+
210
+ describe("parsePartialJsonObject and getPartialJsonObjectFieldState", () => {
211
+ // Test each case in the tests array
212
+ tests.forEach((testCase, index) => {
213
+ it(`Test case #${index + 1}: ${testCase.input || `""`} with query ${JSON.stringify(testCase.query)} should return "${testCase.result}"`, () => {
214
+ const args = parsePartialJsonObject(testCase.input);
215
+ if (args === undefined) throw new Error("unable to parse args");
216
+
217
+ const fieldState = getPartialJsonObjectFieldState(
218
+ args as Record<string, unknown>,
219
+ testCase.query,
220
+ );
221
+
222
+ expect(fieldState).toBe(testCase.result);
223
+ });
224
+ });
225
+ });
@@ -0,0 +1,103 @@
1
+ import sjson from "secure-json-parse";
2
+ import { fixJson } from "./fix-json";
3
+ import { ReadonlyJSONObject } from "./json-value";
4
+
5
+ const PARTIAL_JSON_OBJECT_META_SYMBOL = Symbol(
6
+ "aui.parse-partial-json-object.meta",
7
+ );
8
+
9
+ type FieldState = "complete" | "partial";
10
+
11
+ type PartialJsonObjectMeta = {
12
+ state: "complete" | "partial";
13
+ partialPath: string[];
14
+ };
15
+
16
+ export const getPartialJsonObjectMeta = (
17
+ obj: Record<symbol, unknown>,
18
+ ): PartialJsonObjectMeta | undefined => {
19
+ return obj?.[PARTIAL_JSON_OBJECT_META_SYMBOL] as PartialJsonObjectMeta;
20
+ };
21
+
22
+ export const parsePartialJsonObject = (
23
+ json: string,
24
+ ):
25
+ | (ReadonlyJSONObject & {
26
+ [PARTIAL_JSON_OBJECT_META_SYMBOL]: PartialJsonObjectMeta;
27
+ })
28
+ | undefined => {
29
+ if (json.length === 0)
30
+ return {
31
+ [PARTIAL_JSON_OBJECT_META_SYMBOL]: { state: "partial", partialPath: [] },
32
+ };
33
+
34
+ try {
35
+ const res = sjson.parse(json);
36
+ if (typeof res !== "object" || res === null)
37
+ throw new Error("argsText is expected to be an object");
38
+
39
+ res[PARTIAL_JSON_OBJECT_META_SYMBOL] = {
40
+ state: "complete",
41
+ partialPath: [],
42
+ };
43
+ return res;
44
+ } catch {
45
+ try {
46
+ const [fixedJson, partialPath] = fixJson(json);
47
+ const res = sjson.parse(fixedJson);
48
+ if (typeof res !== "object" || res === null)
49
+ throw new Error("argsText is expected to be an object");
50
+
51
+ res[PARTIAL_JSON_OBJECT_META_SYMBOL] = {
52
+ state: "partial",
53
+ partialPath,
54
+ };
55
+ return res;
56
+ } catch {
57
+ return undefined;
58
+ }
59
+ }
60
+ };
61
+
62
+ const getFieldState = (
63
+ parent: unknown,
64
+ parentMeta: PartialJsonObjectMeta,
65
+ fieldPath: string[],
66
+ ): FieldState => {
67
+ if (typeof parent !== "object" || parent === null) return parentMeta.state;
68
+
69
+ // 1) parent is complete: return "complete"
70
+ if (parentMeta.state === "complete") return "complete";
71
+
72
+ // 2) we finished traversing: return parent state
73
+ if (fieldPath.length === 0) return parentMeta.state;
74
+
75
+ const [field, ...restPath] = fieldPath as [string, ...string[]];
76
+
77
+ // 3) field doesn't yet exist in parent: return "partial"
78
+ if (!Object.prototype.hasOwnProperty.call(parent, field)) return "partial";
79
+
80
+ const [partialField, ...restPartialPath] = parentMeta.partialPath;
81
+
82
+ // 4) field exists but is not partial: return "complete"
83
+ if (field !== partialField) return "complete";
84
+
85
+ // 5) field exists and is partial: return child state
86
+ const child = (parent as Record<string, unknown>)[field];
87
+ const childMeta: PartialJsonObjectMeta = {
88
+ state: "partial",
89
+ partialPath: restPartialPath,
90
+ };
91
+
92
+ return getFieldState(child, childMeta, restPath);
93
+ };
94
+
95
+ export const getPartialJsonObjectFieldState = (
96
+ obj: Record<string, unknown>,
97
+ fieldPath: (string | number)[],
98
+ ): FieldState => {
99
+ const meta = getPartialJsonObjectMeta(obj);
100
+ if (!meta) throw new Error("unable to determine object state");
101
+
102
+ return getFieldState(obj, meta, fieldPath.map(String));
103
+ };
@@ -0,0 +1,10 @@
1
+ export const promiseWithResolvers = function <T>() {
2
+ let resolve: ((value: T | PromiseLike<T>) => void) | undefined;
3
+ let reject: ((reason?: unknown) => void) | undefined;
4
+ const promise = new Promise<T>((res, rej) => {
5
+ resolve = res;
6
+ reject = rej;
7
+ });
8
+ if (!resolve || !reject) throw new Error("Failed to create promise");
9
+ return { promise, resolve, reject };
10
+ };
package/src/utils.ts ADDED
@@ -0,0 +1,13 @@
1
+ export {
2
+ parsePartialJsonObject,
3
+ getPartialJsonObjectFieldState,
4
+ } from "./utils/json/parse-partial-json-object";
5
+ export {
6
+ type AsyncIterableStream,
7
+ asAsyncIterableStream,
8
+ } from "./utils/AsyncIterableStream";
9
+ export type {
10
+ ReadonlyJSONValue,
11
+ ReadonlyJSONArray,
12
+ ReadonlyJSONObject,
13
+ } from "./utils/json/json-value";
package/utils/README.md DELETED
@@ -1 +0,0 @@
1
- This directory exists to support subpath imports for TypeScript projects using --moduleResolution node.
@@ -1,5 +0,0 @@
1
- {
2
- "type": "module",
3
- "main": "../dist/utils.js",
4
- "types": "../dist/utils.d.ts"
5
- }