assistant-stream 0.1.5 → 0.1.7

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 (261) 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/index.js +199 -0
  4. package/dist/ai-sdk/index.js.map +1 -0
  5. package/dist/ai-sdk/index.mjs +173 -0
  6. package/dist/ai-sdk/index.mjs.map +1 -0
  7. package/dist/ai-sdk/language-model.d.ts +6 -0
  8. package/dist/ai-sdk/language-model.d.ts.map +1 -0
  9. package/dist/ai-sdk/language-model.js +131 -0
  10. package/dist/ai-sdk/language-model.js.map +1 -0
  11. package/dist/ai-sdk/language-model.mjs +106 -0
  12. package/dist/ai-sdk/language-model.mjs.map +1 -0
  13. package/dist/ai-sdk.d.ts +3 -24
  14. package/dist/ai-sdk.d.ts.map +1 -0
  15. package/dist/ai-sdk.js +5 -782
  16. package/dist/ai-sdk.js.map +1 -1
  17. package/dist/ai-sdk.mjs +4 -276
  18. package/dist/ai-sdk.mjs.map +1 -1
  19. package/dist/core/AssistantStream.d.ts +12 -0
  20. package/dist/core/AssistantStream.d.ts.map +1 -0
  21. package/dist/core/AssistantStream.js +46 -0
  22. package/dist/core/AssistantStream.js.map +1 -0
  23. package/dist/core/AssistantStream.mjs +21 -0
  24. package/dist/core/AssistantStream.mjs.map +1 -0
  25. package/dist/core/AssistantStreamChunk.d.ts +64 -0
  26. package/dist/core/AssistantStreamChunk.d.ts.map +1 -0
  27. package/dist/core/AssistantStreamChunk.js +19 -0
  28. package/dist/core/AssistantStreamChunk.js.map +1 -0
  29. package/dist/core/AssistantStreamChunk.mjs +1 -0
  30. package/dist/core/AssistantStreamChunk.mjs.map +1 -0
  31. package/dist/core/accumulators/AssistantMessageStream.d.ts +13 -0
  32. package/dist/core/accumulators/AssistantMessageStream.d.ts.map +1 -0
  33. package/dist/core/accumulators/AssistantMessageStream.js +79 -0
  34. package/dist/core/accumulators/AssistantMessageStream.js.map +1 -0
  35. package/dist/core/accumulators/AssistantMessageStream.mjs +54 -0
  36. package/dist/core/accumulators/AssistantMessageStream.mjs.map +1 -0
  37. package/dist/core/accumulators/assistant-message-accumulator.d.ts +8 -0
  38. package/dist/core/accumulators/assistant-message-accumulator.d.ts.map +1 -0
  39. package/dist/core/accumulators/assistant-message-accumulator.js +334 -0
  40. package/dist/core/accumulators/assistant-message-accumulator.js.map +1 -0
  41. package/dist/core/accumulators/assistant-message-accumulator.mjs +309 -0
  42. package/dist/core/accumulators/assistant-message-accumulator.mjs.map +1 -0
  43. package/dist/core/index.d.ts +11 -0
  44. package/dist/core/index.d.ts.map +1 -0
  45. package/dist/core/index.js +55 -0
  46. package/dist/core/index.js.map +1 -0
  47. package/dist/core/index.mjs +26 -0
  48. package/dist/core/index.mjs.map +1 -0
  49. package/dist/core/modules/assistant-stream.d.ts +31 -0
  50. package/dist/core/modules/assistant-stream.d.ts.map +1 -0
  51. package/dist/core/modules/assistant-stream.js +224 -0
  52. package/dist/core/modules/assistant-stream.js.map +1 -0
  53. package/dist/core/modules/assistant-stream.mjs +202 -0
  54. package/dist/core/modules/assistant-stream.mjs.map +1 -0
  55. package/dist/core/modules/text.d.ts +9 -0
  56. package/dist/core/modules/text.d.ts.map +1 -0
  57. package/dist/core/modules/text.js +78 -0
  58. package/dist/core/modules/text.js.map +1 -0
  59. package/dist/core/modules/text.mjs +52 -0
  60. package/dist/core/modules/text.mjs.map +1 -0
  61. package/dist/core/modules/tool-call.d.ts +13 -0
  62. package/dist/core/modules/tool-call.d.ts.map +1 -0
  63. package/dist/core/modules/tool-call.js +110 -0
  64. package/dist/core/modules/tool-call.js.map +1 -0
  65. package/dist/core/modules/tool-call.mjs +84 -0
  66. package/dist/core/modules/tool-call.mjs.map +1 -0
  67. package/dist/core/serialization/PlainText.d.ts +11 -0
  68. package/dist/core/serialization/PlainText.d.ts.map +1 -0
  69. package/dist/core/serialization/PlainText.js +70 -0
  70. package/dist/core/serialization/PlainText.js.map +1 -0
  71. package/dist/core/serialization/PlainText.mjs +44 -0
  72. package/dist/core/serialization/PlainText.mjs.map +1 -0
  73. package/dist/core/serialization/data-stream/DataStream.d.ts +11 -0
  74. package/dist/core/serialization/data-stream/DataStream.d.ts.map +1 -0
  75. package/dist/core/serialization/data-stream/DataStream.js +332 -0
  76. package/dist/core/serialization/data-stream/DataStream.js.map +1 -0
  77. package/dist/core/serialization/data-stream/DataStream.mjs +311 -0
  78. package/dist/core/serialization/data-stream/DataStream.mjs.map +1 -0
  79. package/dist/core/serialization/data-stream/chunk-types.d.ts +86 -0
  80. package/dist/core/serialization/data-stream/chunk-types.d.ts.map +1 -0
  81. package/dist/core/serialization/data-stream/chunk-types.js +49 -0
  82. package/dist/core/serialization/data-stream/chunk-types.js.map +1 -0
  83. package/dist/core/serialization/data-stream/chunk-types.mjs +24 -0
  84. package/dist/core/serialization/data-stream/chunk-types.mjs.map +1 -0
  85. package/dist/core/serialization/data-stream/serialization.d.ts +8 -0
  86. package/dist/core/serialization/data-stream/serialization.d.ts.map +1 -0
  87. package/dist/core/serialization/data-stream/serialization.js +56 -0
  88. package/dist/core/serialization/data-stream/serialization.js.map +1 -0
  89. package/dist/core/serialization/data-stream/serialization.mjs +30 -0
  90. package/dist/core/serialization/data-stream/serialization.mjs.map +1 -0
  91. package/dist/core/tool/ToolCallReader.d.ts +33 -0
  92. package/dist/core/tool/ToolCallReader.d.ts.map +1 -0
  93. package/dist/core/tool/ToolCallReader.js +339 -0
  94. package/dist/core/tool/ToolCallReader.js.map +1 -0
  95. package/dist/core/tool/ToolCallReader.mjs +315 -0
  96. package/dist/core/tool/ToolCallReader.mjs.map +1 -0
  97. package/dist/core/tool/ToolExecutionStream.d.ts +24 -0
  98. package/dist/core/tool/ToolExecutionStream.d.ts.map +1 -0
  99. package/dist/core/tool/ToolExecutionStream.js +165 -0
  100. package/dist/core/tool/ToolExecutionStream.js.map +1 -0
  101. package/dist/core/tool/ToolExecutionStream.mjs +132 -0
  102. package/dist/core/tool/ToolExecutionStream.mjs.map +1 -0
  103. package/dist/core/tool/ToolResponse.d.ts +17 -0
  104. package/dist/core/tool/ToolResponse.d.ts.map +1 -0
  105. package/dist/core/tool/ToolResponse.js +47 -0
  106. package/dist/core/tool/ToolResponse.js.map +1 -0
  107. package/dist/core/tool/ToolResponse.mjs +22 -0
  108. package/dist/core/tool/ToolResponse.mjs.map +1 -0
  109. package/dist/core/tool/index.d.ts +6 -0
  110. package/dist/core/tool/index.d.ts.map +1 -0
  111. package/dist/core/tool/index.js +39 -0
  112. package/dist/core/tool/index.js.map +1 -0
  113. package/dist/core/tool/index.mjs +14 -0
  114. package/dist/core/tool/index.mjs.map +1 -0
  115. package/dist/core/tool/tool-types.d.ts +76 -0
  116. package/dist/core/tool/tool-types.d.ts.map +1 -0
  117. package/dist/core/tool/tool-types.js +19 -0
  118. package/dist/core/tool/tool-types.js.map +1 -0
  119. package/dist/core/tool/tool-types.mjs +1 -0
  120. package/dist/core/tool/tool-types.mjs.map +1 -0
  121. package/dist/core/tool/toolResultStream.d.ts +6 -0
  122. package/dist/core/tool/toolResultStream.d.ts.map +1 -0
  123. package/dist/core/tool/toolResultStream.js +104 -0
  124. package/dist/core/tool/toolResultStream.js.map +1 -0
  125. package/dist/core/tool/toolResultStream.mjs +78 -0
  126. package/dist/core/tool/toolResultStream.mjs.map +1 -0
  127. package/dist/core/tool/type-path-utils.d.ts +23 -0
  128. package/dist/core/tool/type-path-utils.d.ts.map +1 -0
  129. package/dist/core/tool/type-path-utils.js +19 -0
  130. package/dist/core/tool/type-path-utils.js.map +1 -0
  131. package/dist/core/tool/type-path-utils.mjs +1 -0
  132. package/dist/core/tool/type-path-utils.mjs.map +1 -0
  133. package/dist/core/utils/Counter.d.ts +5 -0
  134. package/dist/core/utils/Counter.d.ts.map +1 -0
  135. package/dist/core/utils/Counter.js +36 -0
  136. package/dist/core/utils/Counter.js.map +1 -0
  137. package/dist/core/utils/Counter.mjs +11 -0
  138. package/dist/core/utils/Counter.mjs.map +1 -0
  139. package/dist/core/utils/generateId.d.ts +2 -0
  140. package/dist/core/utils/generateId.d.ts.map +1 -0
  141. package/dist/core/utils/generateId.js +35 -0
  142. package/dist/core/utils/generateId.js.map +1 -0
  143. package/dist/core/utils/generateId.mjs +10 -0
  144. package/dist/core/utils/generateId.mjs.map +1 -0
  145. package/dist/core/utils/stream/AssistantMetaTransformStream.d.ts +20 -0
  146. package/dist/core/utils/stream/AssistantMetaTransformStream.d.ts.map +1 -0
  147. package/dist/core/utils/stream/AssistantMetaTransformStream.js +69 -0
  148. package/dist/core/utils/stream/AssistantMetaTransformStream.js.map +1 -0
  149. package/dist/core/utils/stream/AssistantMetaTransformStream.mjs +44 -0
  150. package/dist/core/utils/stream/AssistantMetaTransformStream.mjs.map +1 -0
  151. package/dist/core/utils/stream/AssistantTransformStream.d.ts +15 -0
  152. package/dist/core/utils/stream/AssistantTransformStream.d.ts.map +1 -0
  153. package/dist/core/utils/stream/AssistantTransformStream.js +69 -0
  154. package/dist/core/utils/stream/AssistantTransformStream.js.map +1 -0
  155. package/dist/core/utils/stream/AssistantTransformStream.mjs +46 -0
  156. package/dist/core/utils/stream/AssistantTransformStream.mjs.map +1 -0
  157. package/dist/core/utils/stream/LineDecoderStream.d.ts +5 -0
  158. package/dist/core/utils/stream/LineDecoderStream.d.ts.map +1 -0
  159. package/dist/core/utils/stream/LineDecoderStream.js +52 -0
  160. package/dist/core/utils/stream/LineDecoderStream.js.map +1 -0
  161. package/dist/core/utils/stream/LineDecoderStream.mjs +27 -0
  162. package/dist/core/utils/stream/LineDecoderStream.mjs.map +1 -0
  163. package/dist/core/utils/stream/PipeableTransformStream.d.ts +4 -0
  164. package/dist/core/utils/stream/PipeableTransformStream.d.ts.map +1 -0
  165. package/dist/core/utils/stream/PipeableTransformStream.js +40 -0
  166. package/dist/core/utils/stream/PipeableTransformStream.js.map +1 -0
  167. package/dist/core/utils/stream/PipeableTransformStream.mjs +15 -0
  168. package/dist/core/utils/stream/PipeableTransformStream.mjs.map +1 -0
  169. package/dist/core/utils/stream/UnderlyingReadable.d.ts +6 -0
  170. package/dist/core/utils/stream/UnderlyingReadable.d.ts.map +1 -0
  171. package/dist/core/utils/stream/UnderlyingReadable.js +19 -0
  172. package/dist/core/utils/stream/UnderlyingReadable.js.map +1 -0
  173. package/dist/core/utils/stream/UnderlyingReadable.mjs +1 -0
  174. package/dist/core/utils/stream/UnderlyingReadable.mjs.map +1 -0
  175. package/dist/core/utils/stream/merge.d.ts +9 -0
  176. package/dist/core/utils/stream/merge.d.ts.map +1 -0
  177. package/dist/core/utils/stream/merge.js +110 -0
  178. package/dist/core/utils/stream/merge.js.map +1 -0
  179. package/dist/core/utils/stream/merge.mjs +85 -0
  180. package/dist/core/utils/stream/merge.mjs.map +1 -0
  181. package/dist/core/utils/stream/path-utils.d.ts +12 -0
  182. package/dist/core/utils/stream/path-utils.d.ts.map +1 -0
  183. package/dist/core/utils/stream/path-utils.js +88 -0
  184. package/dist/core/utils/stream/path-utils.js.map +1 -0
  185. package/dist/core/utils/stream/path-utils.mjs +61 -0
  186. package/dist/core/utils/stream/path-utils.mjs.map +1 -0
  187. package/dist/core/utils/types.d.ts +102 -0
  188. package/dist/core/utils/types.d.ts.map +1 -0
  189. package/dist/core/utils/types.js +19 -0
  190. package/dist/core/utils/types.js.map +1 -0
  191. package/dist/core/utils/types.mjs +1 -0
  192. package/dist/core/utils/types.mjs.map +1 -0
  193. package/dist/core/utils/withPromiseOrValue.d.ts +2 -0
  194. package/dist/core/utils/withPromiseOrValue.d.ts.map +1 -0
  195. package/dist/core/utils/withPromiseOrValue.js +42 -0
  196. package/dist/core/utils/withPromiseOrValue.js.map +1 -0
  197. package/dist/core/utils/withPromiseOrValue.mjs +17 -0
  198. package/dist/core/utils/withPromiseOrValue.mjs.map +1 -0
  199. package/dist/index.d.ts +2 -153
  200. package/dist/index.d.ts.map +1 -0
  201. package/dist/index.js +3 -2318
  202. package/dist/index.js.map +1 -1
  203. package/dist/index.mjs +2 -957
  204. package/dist/index.mjs.map +1 -1
  205. package/dist/utils/AsyncIterableStream.d.ts +3 -0
  206. package/dist/utils/AsyncIterableStream.d.ts.map +1 -0
  207. package/dist/utils/AsyncIterableStream.js +46 -0
  208. package/dist/utils/AsyncIterableStream.js.map +1 -0
  209. package/dist/utils/AsyncIterableStream.mjs +21 -0
  210. package/dist/utils/AsyncIterableStream.mjs.map +1 -0
  211. package/dist/utils/json/fix-json.d.ts +2 -0
  212. package/dist/utils/json/fix-json.d.ts.map +1 -0
  213. package/dist/{chunk-PQLDKUPN.mjs → utils/json/fix-json.js} +28 -66
  214. package/dist/utils/json/fix-json.js.map +1 -0
  215. package/dist/utils/json/fix-json.mjs +362 -0
  216. package/dist/utils/json/fix-json.mjs.map +1 -0
  217. package/dist/utils/json/is-json.d.ts +5 -0
  218. package/dist/utils/json/is-json.d.ts.map +1 -0
  219. package/dist/utils/json/is-json.js +56 -0
  220. package/dist/utils/json/is-json.js.map +1 -0
  221. package/dist/utils/json/is-json.mjs +29 -0
  222. package/dist/utils/json/is-json.mjs.map +1 -0
  223. package/dist/utils/json/json-value.d.ts +6 -0
  224. package/dist/utils/json/json-value.d.ts.map +1 -0
  225. package/dist/utils/json/json-value.js +19 -0
  226. package/dist/utils/json/json-value.js.map +1 -0
  227. package/dist/utils/json/json-value.mjs +1 -0
  228. package/dist/utils/json/json-value.mjs.map +1 -0
  229. package/dist/utils/json/parse-partial-json-object.d.ts +14 -0
  230. package/dist/utils/json/parse-partial-json-object.d.ts.map +1 -0
  231. package/dist/utils/json/parse-partial-json-object.js +102 -0
  232. package/dist/utils/json/parse-partial-json-object.js.map +1 -0
  233. package/dist/utils/json/parse-partial-json-object.mjs +65 -0
  234. package/dist/utils/json/parse-partial-json-object.mjs.map +1 -0
  235. package/dist/utils/json/parse-partial-json-object.test.d.ts +2 -0
  236. package/dist/utils/json/parse-partial-json-object.test.d.ts.map +1 -0
  237. package/dist/utils/promiseWithResolvers.d.ts +6 -0
  238. package/dist/utils/promiseWithResolvers.d.ts.map +1 -0
  239. package/dist/utils/promiseWithResolvers.js +40 -0
  240. package/dist/utils/promiseWithResolvers.js.map +1 -0
  241. package/dist/utils/promiseWithResolvers.mjs +15 -0
  242. package/dist/utils/promiseWithResolvers.mjs.map +1 -0
  243. package/dist/utils.d.ts +4 -16
  244. package/dist/utils.d.ts.map +1 -0
  245. package/dist/utils.js +5 -451
  246. package/dist/utils.js.map +1 -1
  247. package/dist/utils.mjs +7 -21
  248. package/dist/utils.mjs.map +1 -1
  249. package/package.json +2 -1
  250. package/dist/AsyncIterableStream-C3C8ZoXv.d.mts +0 -4
  251. package/dist/AsyncIterableStream-C3C8ZoXv.d.ts +0 -4
  252. package/dist/ai-sdk.d.mts +0 -24
  253. package/dist/assistant-stream-ISFjQ0mQ.d.mts +0 -225
  254. package/dist/assistant-stream-kAoIMgvk.d.ts +0 -225
  255. package/dist/chunk-EDE6WQ2R.mjs +0 -943
  256. package/dist/chunk-EDE6WQ2R.mjs.map +0 -1
  257. package/dist/chunk-PQLDKUPN.mjs.map +0 -1
  258. package/dist/index.d.mts +0 -153
  259. package/dist/json-value-Ch5eKkQ_.d.mts +0 -7
  260. package/dist/json-value-Ch5eKkQ_.d.ts +0 -7
  261. package/dist/utils.d.mts +0 -16
package/dist/index.mjs CHANGED
@@ -1,958 +1,3 @@
1
- import {
2
- AssistantMetaTransformStream,
3
- AssistantStream,
4
- AssistantTransformStream,
5
- DataStreamDecoder,
6
- DataStreamEncoder,
7
- PipeableTransformStream,
8
- createAssistantStream,
9
- createAssistantStreamResponse,
10
- generateId,
11
- promiseWithResolvers
12
- } from "./chunk-EDE6WQ2R.mjs";
13
- import {
14
- getPartialJsonObjectFieldState,
15
- parsePartialJsonObject
16
- } from "./chunk-PQLDKUPN.mjs";
17
-
18
- // src/core/accumulators/assistant-message-accumulator.ts
19
- var createInitialMessage = () => ({
20
- role: "assistant",
21
- status: { type: "running" },
22
- parts: [],
23
- get content() {
24
- return this.parts;
25
- },
26
- metadata: {
27
- unstable_data: [],
28
- unstable_annotations: [],
29
- steps: [],
30
- custom: {}
31
- }
32
- });
33
- var updatePartForPath = (message, chunk, updater) => {
34
- if (message.parts.length === 0) {
35
- throw new Error("No parts available to update.");
36
- }
37
- if (chunk.path.length !== 1)
38
- throw new Error("Nested paths are not supported yet.");
39
- const partIndex = chunk.path[0];
40
- const updatedPart = updater(message.parts[partIndex]);
41
- return {
42
- ...message,
43
- parts: [
44
- ...message.parts.slice(0, partIndex),
45
- updatedPart,
46
- ...message.parts.slice(partIndex + 1)
47
- ],
48
- get content() {
49
- return this.parts;
50
- }
51
- };
52
- };
53
- var handlePartStart = (message, chunk) => {
54
- const partInit = chunk.part;
55
- if (partInit.type === "text" || partInit.type === "reasoning") {
56
- const newTextPart = {
57
- type: partInit.type,
58
- text: "",
59
- status: { type: "running" }
60
- };
61
- return {
62
- ...message,
63
- parts: [...message.parts, newTextPart],
64
- get content() {
65
- return this.parts;
66
- }
67
- };
68
- } else if (partInit.type === "tool-call") {
69
- const newToolCallPart = {
70
- type: "tool-call",
71
- state: "partial-call",
72
- status: { type: "running", isArgsComplete: false },
73
- toolCallId: partInit.toolCallId,
74
- toolName: partInit.toolName,
75
- argsText: "",
76
- args: {}
77
- };
78
- return {
79
- ...message,
80
- parts: [...message.parts, newToolCallPart],
81
- get content() {
82
- return this.parts;
83
- }
84
- };
85
- } else if (partInit.type === "source") {
86
- const newSourcePart = {
87
- type: "source",
88
- sourceType: partInit.sourceType,
89
- id: partInit.id,
90
- url: partInit.url,
91
- ...partInit.title ? { title: partInit.title } : void 0
92
- };
93
- return {
94
- ...message,
95
- parts: [...message.parts, newSourcePart],
96
- get content() {
97
- return this.parts;
98
- }
99
- };
100
- } else if (partInit.type === "file") {
101
- const newFilePart = {
102
- type: "file",
103
- mimeType: partInit.mimeType,
104
- data: partInit.data
105
- };
106
- return {
107
- ...message,
108
- parts: [...message.parts, newFilePart],
109
- get content() {
110
- return this.parts;
111
- }
112
- };
113
- } else {
114
- throw new Error(`Unsupported part type: ${partInit.type}`);
115
- }
116
- };
117
- var handleToolCallArgsTextFinish = (message, chunk) => {
118
- return updatePartForPath(message, chunk, (part) => {
119
- if (part.type !== "tool-call") {
120
- throw new Error("Last is not a tool call");
121
- }
122
- return {
123
- ...part,
124
- state: "call"
125
- };
126
- });
127
- };
128
- var handlePartFinish = (message, chunk) => {
129
- return updatePartForPath(message, chunk, (part) => ({
130
- ...part,
131
- status: { type: "complete", reason: "unknown" }
132
- }));
133
- };
134
- var handleTextDelta = (message, chunk) => {
135
- return updatePartForPath(message, chunk, (part) => {
136
- if (part.type === "text" || part.type === "reasoning") {
137
- return { ...part, text: part.text + chunk.textDelta };
138
- } else if (part.type === "tool-call") {
139
- const newArgsText = part.argsText + chunk.textDelta;
140
- const newArgs = parsePartialJsonObject(newArgsText) ?? part.args;
141
- return { ...part, argsText: newArgsText, args: newArgs };
142
- } else {
143
- throw new Error(
144
- "text-delta received but part is neither text nor tool-call"
145
- );
146
- }
147
- });
148
- };
149
- var handleResult = (message, chunk) => {
150
- return updatePartForPath(message, chunk, (part) => {
151
- if (part.type === "tool-call") {
152
- return {
153
- ...part,
154
- state: "result",
155
- artifact: chunk.artifact,
156
- result: chunk.result,
157
- isError: chunk.isError ?? false,
158
- status: { type: "complete", reason: "stop" }
159
- };
160
- } else {
161
- throw new Error("Result chunk received but part is not a tool-call");
162
- }
163
- });
164
- };
165
- var handleMessageFinish = (message, chunk) => {
166
- const newStatus = getStatus(chunk);
167
- return { ...message, status: newStatus };
168
- };
169
- var getStatus = (chunk) => {
170
- if (chunk.finishReason === "tool-calls") {
171
- return {
172
- type: "requires-action",
173
- reason: "tool-calls"
174
- };
175
- } else if (chunk.finishReason === "stop" || chunk.finishReason === "unknown") {
176
- return {
177
- type: "complete",
178
- reason: chunk.finishReason
179
- };
180
- } else {
181
- return {
182
- type: "incomplete",
183
- reason: chunk.finishReason
184
- };
185
- }
186
- };
187
- var handleAnnotations = (message, chunk) => {
188
- return {
189
- ...message,
190
- metadata: {
191
- ...message.metadata,
192
- unstable_annotations: [
193
- ...message.metadata.unstable_annotations,
194
- ...chunk.annotations
195
- ]
196
- }
197
- };
198
- };
199
- var handleData = (message, chunk) => {
200
- return {
201
- ...message,
202
- metadata: {
203
- ...message.metadata,
204
- unstable_data: [...message.metadata.unstable_data, ...chunk.data]
205
- }
206
- };
207
- };
208
- var handleStepStart = (message, chunk) => {
209
- return {
210
- ...message,
211
- metadata: {
212
- ...message.metadata,
213
- steps: [
214
- ...message.metadata.steps,
215
- { state: "started", messageId: chunk.messageId }
216
- ]
217
- }
218
- };
219
- };
220
- var handleStepFinish = (message, chunk) => {
221
- const steps = message.metadata.steps.slice();
222
- const lastIndex = steps.length - 1;
223
- if (steps.length > 0 && steps[lastIndex]?.state === "started") {
224
- steps[lastIndex] = {
225
- ...steps[lastIndex],
226
- state: "finished",
227
- finishReason: chunk.finishReason,
228
- usage: chunk.usage,
229
- isContinued: chunk.isContinued
230
- };
231
- } else {
232
- steps.push({
233
- state: "finished",
234
- messageId: generateId(),
235
- finishReason: chunk.finishReason,
236
- usage: chunk.usage,
237
- isContinued: chunk.isContinued
238
- });
239
- }
240
- return {
241
- ...message,
242
- metadata: {
243
- ...message.metadata,
244
- steps
245
- }
246
- };
247
- };
248
- var handleErrorChunk = (message, chunk) => {
249
- return {
250
- ...message,
251
- status: { type: "incomplete", reason: "error", error: chunk.error }
252
- };
253
- };
254
- var AssistantMessageAccumulator = class extends TransformStream {
255
- constructor({
256
- initialMessage
257
- } = {}) {
258
- let message = initialMessage ?? createInitialMessage();
259
- super({
260
- transform(chunk, controller) {
261
- const type = chunk.type;
262
- switch (type) {
263
- case "part-start":
264
- message = handlePartStart(message, chunk);
265
- break;
266
- case "tool-call-args-text-finish":
267
- message = handleToolCallArgsTextFinish(message, chunk);
268
- break;
269
- case "part-finish":
270
- message = handlePartFinish(message, chunk);
271
- break;
272
- case "text-delta":
273
- message = handleTextDelta(message, chunk);
274
- break;
275
- case "result":
276
- message = handleResult(message, chunk);
277
- break;
278
- case "message-finish":
279
- message = handleMessageFinish(message, chunk);
280
- break;
281
- case "annotations":
282
- message = handleAnnotations(message, chunk);
283
- break;
284
- case "data":
285
- message = handleData(message, chunk);
286
- break;
287
- case "step-start":
288
- message = handleStepStart(message, chunk);
289
- break;
290
- case "step-finish":
291
- message = handleStepFinish(message, chunk);
292
- break;
293
- case "error":
294
- message = handleErrorChunk(message, chunk);
295
- break;
296
- default: {
297
- const unhandledType = type;
298
- throw new Error(`Unsupported chunk type: ${unhandledType}`);
299
- }
300
- }
301
- controller.enqueue(message);
302
- },
303
- flush(controller) {
304
- if (message.status?.type === "running") {
305
- const requiresAction = message.parts?.at(-1)?.type === "tool-call";
306
- message = handleMessageFinish(message, {
307
- type: "message-finish",
308
- path: [],
309
- finishReason: requiresAction ? "tool-calls" : "unknown",
310
- usage: {
311
- promptTokens: 0,
312
- completionTokens: 0
313
- }
314
- });
315
- controller.enqueue(message);
316
- }
317
- }
318
- });
319
- }
320
- };
321
-
322
- // src/core/serialization/PlainText.ts
323
- var PlainTextEncoder = class extends PipeableTransformStream {
324
- headers = new Headers({
325
- "Content-Type": "text/plain; charset=utf-8",
326
- "x-vercel-ai-data-stream": "v1"
327
- });
328
- constructor() {
329
- super((readable) => {
330
- const transform = new TransformStream({
331
- transform(chunk, controller) {
332
- const type = chunk.type;
333
- switch (type) {
334
- case "text-delta":
335
- controller.enqueue(chunk.textDelta);
336
- break;
337
- default:
338
- const unsupportedType = type;
339
- throw new Error(`unsupported chunk type: ${unsupportedType}`);
340
- }
341
- }
342
- });
343
- return readable.pipeThrough(transform).pipeThrough(new TextEncoderStream());
344
- });
345
- }
346
- };
347
- var PlainTextDecoder = class extends PipeableTransformStream {
348
- constructor() {
349
- super((readable) => {
350
- const transform = new AssistantTransformStream({
351
- transform(chunk, controller) {
352
- controller.appendText(chunk);
353
- }
354
- });
355
- return readable.pipeThrough(new TextDecoderStream()).pipeThrough(transform);
356
- });
357
- }
358
- };
359
-
360
- // src/core/accumulators/AssistantMessageStream.ts
361
- var AssistantMessageStream = class _AssistantMessageStream {
362
- constructor(readable) {
363
- this.readable = readable;
364
- this.readable = readable;
365
- }
366
- static fromAssistantStream(stream) {
367
- return new _AssistantMessageStream(
368
- stream.pipeThrough(new AssistantMessageAccumulator())
369
- );
370
- }
371
- async unstable_result() {
372
- let last;
373
- for await (const chunk of this) {
374
- last = chunk;
375
- }
376
- if (!last) {
377
- return {
378
- role: "assistant",
379
- status: { type: "complete", reason: "unknown" },
380
- parts: [],
381
- content: [],
382
- metadata: {
383
- unstable_data: [],
384
- unstable_annotations: [],
385
- steps: [],
386
- custom: {}
387
- }
388
- };
389
- }
390
- return last;
391
- }
392
- [Symbol.asyncIterator]() {
393
- const reader = this.readable.getReader();
394
- return {
395
- async next() {
396
- const { done, value } = await reader.read();
397
- return done ? { done: true, value: void 0 } : { done: false, value };
398
- }
399
- };
400
- }
401
- tee() {
402
- const [readable1, readable2] = this.readable.tee();
403
- return [
404
- new _AssistantMessageStream(readable1),
405
- new _AssistantMessageStream(readable2)
406
- ];
407
- }
408
- };
409
-
410
- // src/core/tool/ToolResponse.ts
411
- var TOOL_RESPONSE_SYMBOL = Symbol.for("aui.tool-response");
412
- var ToolResponse = class {
413
- get [TOOL_RESPONSE_SYMBOL]() {
414
- return true;
415
- }
416
- artifact;
417
- result;
418
- isError;
419
- constructor(options) {
420
- this.artifact = options.artifact;
421
- this.result = options.result;
422
- this.isError = options.isError ?? false;
423
- }
424
- static [Symbol.hasInstance](obj) {
425
- return typeof obj === "object" && obj !== null && TOOL_RESPONSE_SYMBOL in obj;
426
- }
427
- };
428
-
429
- // src/core/tool/ToolExecutionStream.ts
430
- import sjson from "secure-json-parse";
431
-
432
- // src/core/utils/withPromiseOrValue.ts
433
- function withPromiseOrValue(callback, thenHandler, catchHandler) {
434
- try {
435
- const promiseOrValue = callback();
436
- if (typeof promiseOrValue === "object" && promiseOrValue !== null && "then" in promiseOrValue) {
437
- return promiseOrValue.then(thenHandler, catchHandler);
438
- } else {
439
- thenHandler(promiseOrValue);
440
- }
441
- } catch (e) {
442
- catchHandler(e);
443
- }
444
- }
445
-
446
- // src/core/tool/ToolCallReader.ts
447
- function getField(obj, fieldPath) {
448
- let current = obj;
449
- for (const key of fieldPath) {
450
- if (current === void 0 || current === null) {
451
- return void 0;
452
- }
453
- current = current[key];
454
- }
455
- return current;
456
- }
457
- var GetHandle = class {
458
- resolve;
459
- reject;
460
- disposed = false;
461
- fieldPath;
462
- constructor(resolve, reject, fieldPath) {
463
- this.resolve = resolve;
464
- this.reject = reject;
465
- this.fieldPath = fieldPath;
466
- }
467
- update(args) {
468
- if (this.disposed) return;
469
- try {
470
- if (getPartialJsonObjectFieldState(
471
- args,
472
- this.fieldPath
473
- ) === "complete") {
474
- const value = getField(args, this.fieldPath);
475
- if (value !== void 0) {
476
- this.resolve(value);
477
- this.dispose();
478
- }
479
- }
480
- } catch (e) {
481
- this.reject(e);
482
- this.dispose();
483
- }
484
- }
485
- dispose() {
486
- this.disposed = true;
487
- }
488
- };
489
- var StreamValuesHandle = class {
490
- controller;
491
- disposed = false;
492
- fieldPath;
493
- constructor(controller, fieldPath) {
494
- this.controller = controller;
495
- this.fieldPath = fieldPath;
496
- }
497
- update(args) {
498
- if (this.disposed) return;
499
- try {
500
- const value = getField(args, this.fieldPath);
501
- if (value !== void 0) {
502
- this.controller.enqueue(value);
503
- }
504
- if (getPartialJsonObjectFieldState(
505
- args,
506
- this.fieldPath
507
- ) === "complete") {
508
- this.controller.close();
509
- this.dispose();
510
- }
511
- } catch (e) {
512
- this.controller.error(e);
513
- this.dispose();
514
- }
515
- }
516
- dispose() {
517
- this.disposed = true;
518
- }
519
- };
520
- var StreamTextHandle = class {
521
- controller;
522
- disposed = false;
523
- fieldPath;
524
- lastValue = void 0;
525
- constructor(controller, fieldPath) {
526
- this.controller = controller;
527
- this.fieldPath = fieldPath;
528
- }
529
- update(args) {
530
- if (this.disposed) return;
531
- try {
532
- const value = getField(args, this.fieldPath);
533
- if (value !== void 0 && typeof value === "string") {
534
- const delta = value.substring(this.lastValue?.length || 0);
535
- this.lastValue = value;
536
- this.controller.enqueue(delta);
537
- }
538
- if (getPartialJsonObjectFieldState(
539
- args,
540
- this.fieldPath
541
- ) === "complete") {
542
- this.controller.close();
543
- this.dispose();
544
- }
545
- } catch (e) {
546
- this.controller.error(e);
547
- this.dispose();
548
- }
549
- }
550
- dispose() {
551
- this.disposed = true;
552
- }
553
- };
554
- var ForEachHandle = class {
555
- controller;
556
- disposed = false;
557
- fieldPath;
558
- processedIndexes = /* @__PURE__ */ new Set();
559
- constructor(controller, fieldPath) {
560
- this.controller = controller;
561
- this.fieldPath = fieldPath;
562
- }
563
- update(args) {
564
- if (this.disposed) return;
565
- try {
566
- const array = getField(args, this.fieldPath);
567
- if (!Array.isArray(array)) {
568
- return;
569
- }
570
- for (let i = 0; i < array.length; i++) {
571
- if (!this.processedIndexes.has(i)) {
572
- const elementPath = [...this.fieldPath, i];
573
- if (getPartialJsonObjectFieldState(
574
- args,
575
- elementPath
576
- ) === "complete") {
577
- this.controller.enqueue(array[i]);
578
- this.processedIndexes.add(i);
579
- }
580
- }
581
- }
582
- if (getPartialJsonObjectFieldState(
583
- args,
584
- this.fieldPath
585
- ) === "complete") {
586
- this.controller.close();
587
- this.dispose();
588
- }
589
- } catch (e) {
590
- this.controller.error(e);
591
- this.dispose();
592
- }
593
- }
594
- dispose() {
595
- this.disposed = true;
596
- }
597
- };
598
- var ToolCallArgsReaderImpl = class {
599
- argTextDeltas;
600
- handles = /* @__PURE__ */ new Set();
601
- args = parsePartialJsonObject("");
602
- constructor(argTextDeltas) {
603
- this.argTextDeltas = argTextDeltas;
604
- this.processStream();
605
- }
606
- async processStream() {
607
- try {
608
- let accumulatedText = "";
609
- const reader = this.argTextDeltas.getReader();
610
- while (true) {
611
- const { value, done } = await reader.read();
612
- if (done) break;
613
- accumulatedText += value;
614
- const parsedArgs = parsePartialJsonObject(accumulatedText);
615
- if (parsedArgs !== void 0) {
616
- this.args = parsedArgs;
617
- for (const handle of this.handles) {
618
- handle.update(parsedArgs);
619
- }
620
- }
621
- }
622
- } catch (error) {
623
- console.error("Error processing argument stream:", error);
624
- for (const handle of this.handles) {
625
- handle.dispose();
626
- }
627
- }
628
- }
629
- get(...fieldPath) {
630
- return new Promise((resolve, reject) => {
631
- const handle = new GetHandle(resolve, reject, fieldPath);
632
- if (this.args && getPartialJsonObjectFieldState(
633
- this.args,
634
- fieldPath
635
- ) === "complete") {
636
- const value = getField(this.args, fieldPath);
637
- if (value !== void 0) {
638
- resolve(value);
639
- return;
640
- }
641
- }
642
- this.handles.add(handle);
643
- handle.update(this.args);
644
- });
645
- }
646
- streamValues(...fieldPath) {
647
- const simplePath = fieldPath;
648
- const stream = new ReadableStream({
649
- start: (controller) => {
650
- const handle = new StreamValuesHandle(controller, simplePath);
651
- this.handles.add(handle);
652
- handle.update(this.args);
653
- },
654
- cancel: () => {
655
- for (const handle of this.handles) {
656
- if (handle instanceof StreamValuesHandle) {
657
- handle.dispose();
658
- this.handles.delete(handle);
659
- break;
660
- }
661
- }
662
- }
663
- });
664
- return stream;
665
- }
666
- streamText(...fieldPath) {
667
- const simplePath = fieldPath;
668
- const stream = new ReadableStream({
669
- start: (controller) => {
670
- const handle = new StreamTextHandle(controller, simplePath);
671
- this.handles.add(handle);
672
- handle.update(this.args);
673
- },
674
- cancel: () => {
675
- for (const handle of this.handles) {
676
- if (handle instanceof StreamTextHandle) {
677
- handle.dispose();
678
- this.handles.delete(handle);
679
- break;
680
- }
681
- }
682
- }
683
- });
684
- return stream;
685
- }
686
- forEach(...fieldPath) {
687
- const simplePath = fieldPath;
688
- const stream = new ReadableStream({
689
- start: (controller) => {
690
- const handle = new ForEachHandle(controller, simplePath);
691
- this.handles.add(handle);
692
- handle.update(this.args);
693
- },
694
- cancel: () => {
695
- for (const handle of this.handles) {
696
- if (handle instanceof ForEachHandle) {
697
- handle.dispose();
698
- this.handles.delete(handle);
699
- break;
700
- }
701
- }
702
- }
703
- });
704
- return stream;
705
- }
706
- };
707
- var ToolCallResponseReaderImpl = class {
708
- constructor(promise) {
709
- this.promise = promise;
710
- }
711
- get() {
712
- return this.promise;
713
- }
714
- };
715
- var ToolCallReaderImpl = class {
716
- args;
717
- response;
718
- writable;
719
- resolve;
720
- argsText = "";
721
- constructor() {
722
- const stream = new TransformStream();
723
- this.writable = stream.writable;
724
- this.args = new ToolCallArgsReaderImpl(stream.readable);
725
- const { promise, resolve } = promiseWithResolvers();
726
- this.resolve = resolve;
727
- this.response = new ToolCallResponseReaderImpl(promise);
728
- }
729
- async appendArgsTextDelta(text) {
730
- const writer = this.writable.getWriter();
731
- try {
732
- await writer.write(text);
733
- } catch (err) {
734
- console.warn(err);
735
- } finally {
736
- writer.releaseLock();
737
- }
738
- this.argsText += text;
739
- }
740
- setResponse(value) {
741
- this.resolve(value);
742
- }
743
- result = {
744
- get: async () => {
745
- const response = await this.response.get();
746
- return response.result;
747
- }
748
- };
749
- };
750
-
751
- // src/core/tool/ToolExecutionStream.ts
752
- var ToolExecutionStream = class extends PipeableTransformStream {
753
- constructor(options) {
754
- const toolCallPromises = /* @__PURE__ */ new Map();
755
- const toolCallControllers = /* @__PURE__ */ new Map();
756
- super((readable) => {
757
- const transform = new TransformStream({
758
- transform(chunk, controller) {
759
- if (chunk.type !== "part-finish" || chunk.meta.type !== "tool-call") {
760
- controller.enqueue(chunk);
761
- }
762
- const type = chunk.type;
763
- switch (type) {
764
- case "part-start":
765
- if (chunk.part.type === "tool-call") {
766
- const reader = new ToolCallReaderImpl();
767
- toolCallControllers.set(chunk.part.toolCallId, reader);
768
- options.streamCall({
769
- reader,
770
- toolCallId: chunk.part.toolCallId,
771
- toolName: chunk.part.toolName
772
- });
773
- }
774
- break;
775
- case "text-delta": {
776
- if (chunk.meta.type === "tool-call") {
777
- const toolCallId = chunk.meta.toolCallId;
778
- const controller2 = toolCallControllers.get(toolCallId);
779
- if (!controller2)
780
- throw new Error("No controller found for tool call");
781
- controller2.appendArgsTextDelta(chunk.textDelta);
782
- }
783
- break;
784
- }
785
- case "tool-call-args-text-finish": {
786
- if (chunk.meta.type !== "tool-call") break;
787
- const { toolCallId, toolName } = chunk.meta;
788
- const streamController = toolCallControllers.get(toolCallId);
789
- if (!streamController)
790
- throw new Error("No controller found for tool call");
791
- const promise = withPromiseOrValue(
792
- () => {
793
- if (!streamController.argsText) {
794
- console.log(
795
- "Encountered tool call without args, this should never happen"
796
- );
797
- throw new Error(
798
- "Encountered tool call without args, this is unexpected."
799
- );
800
- }
801
- let args;
802
- try {
803
- args = sjson.parse(streamController.argsText);
804
- } catch (e) {
805
- throw new Error(
806
- `Function parameter parsing failed. ${JSON.stringify(e.message)}`
807
- );
808
- }
809
- return options.execute({
810
- toolCallId,
811
- toolName,
812
- args
813
- });
814
- },
815
- (c) => {
816
- if (c === void 0) return;
817
- const result = new ToolResponse({
818
- artifact: c.artifact,
819
- result: c.result,
820
- isError: c.isError
821
- });
822
- streamController.setResponse(result);
823
- controller.enqueue({
824
- type: "result",
825
- path: chunk.path,
826
- ...result
827
- });
828
- },
829
- (e) => {
830
- const result = new ToolResponse({
831
- result: String(e),
832
- isError: true
833
- });
834
- streamController.setResponse(result);
835
- controller.enqueue({
836
- type: "result",
837
- path: chunk.path,
838
- ...result
839
- });
840
- }
841
- );
842
- if (promise) {
843
- toolCallPromises.set(toolCallId, promise);
844
- }
845
- break;
846
- }
847
- case "part-finish": {
848
- if (chunk.meta.type !== "tool-call") break;
849
- const { toolCallId } = chunk.meta;
850
- const toolCallPromise = toolCallPromises.get(toolCallId);
851
- if (toolCallPromise) {
852
- toolCallPromise.then(() => {
853
- toolCallPromises.delete(toolCallId);
854
- toolCallControllers.delete(toolCallId);
855
- controller.enqueue(chunk);
856
- });
857
- } else {
858
- controller.enqueue(chunk);
859
- }
860
- }
861
- }
862
- },
863
- async flush() {
864
- await Promise.all(toolCallPromises.values());
865
- }
866
- });
867
- return readable.pipeThrough(new AssistantMetaTransformStream()).pipeThrough(transform);
868
- });
869
- }
870
- };
871
-
872
- // src/core/tool/toolResultStream.ts
873
- var isStandardSchemaV1 = (schema) => {
874
- return typeof schema === "object" && schema !== null && "~standard" in schema && schema["~standard"].version === 1;
875
- };
876
- function getToolResponse(tools, abortSignal, toolCall) {
877
- const tool = tools?.[toolCall.toolName];
878
- if (!tool || !tool.execute) return void 0;
879
- const getResult = async (toolExecute) => {
880
- let executeFn = toolExecute;
881
- if (isStandardSchemaV1(tool.parameters)) {
882
- let result2 = tool.parameters["~standard"].validate(toolCall.args);
883
- if (result2 instanceof Promise) result2 = await result2;
884
- if (result2.issues) {
885
- executeFn = tool.experimental_onSchemaValidationError ?? (() => {
886
- throw new Error(
887
- `Function parameter validation failed. ${JSON.stringify(result2.issues)}`
888
- );
889
- });
890
- }
891
- }
892
- const result = await executeFn(toolCall.args, {
893
- toolCallId: toolCall.toolCallId,
894
- abortSignal
895
- });
896
- if (result instanceof ToolResponse) return result;
897
- return new ToolResponse({
898
- result: result === void 0 ? "<no result>" : result
899
- });
900
- };
901
- return getResult(tool.execute);
902
- }
903
- function getToolStreamResponse(tools, abortSignal, reader, context) {
904
- tools?.[context.toolName]?.streamCall?.(reader, {
905
- toolCallId: context.toolCallId,
906
- abortSignal
907
- });
908
- }
909
- async function unstable_runPendingTools(message, tools, abortSignal) {
910
- for (const part of message.parts) {
911
- if (part.type === "tool-call") {
912
- const promiseOrUndefined = getToolResponse(tools, abortSignal, part);
913
- if (promiseOrUndefined) {
914
- const result = await promiseOrUndefined;
915
- const updatedParts = message.parts.map((p) => {
916
- if (p.type === "tool-call" && p.toolCallId === part.toolCallId) {
917
- return {
918
- ...p,
919
- state: "result",
920
- artifact: result.artifact,
921
- result: result.result,
922
- isError: result.isError
923
- };
924
- }
925
- return p;
926
- });
927
- message = {
928
- ...message,
929
- parts: updatedParts,
930
- content: updatedParts
931
- };
932
- }
933
- }
934
- }
935
- return message;
936
- }
937
- function toolResultStream(tools, abortSignal) {
938
- return new ToolExecutionStream({
939
- execute: (toolCall) => getToolResponse(tools, abortSignal, toolCall),
940
- streamCall: ({ reader, ...context }) => getToolStreamResponse(tools, abortSignal, reader, context)
941
- });
942
- }
943
- export {
944
- AssistantMessageAccumulator,
945
- AssistantMessageStream,
946
- AssistantStream,
947
- DataStreamDecoder,
948
- DataStreamEncoder,
949
- PlainTextDecoder,
950
- PlainTextEncoder,
951
- ToolExecutionStream,
952
- ToolResponse,
953
- createAssistantStream,
954
- createAssistantStreamResponse,
955
- unstable_runPendingTools,
956
- toolResultStream as unstable_toolResultStream
957
- };
1
+ // src/index.ts
2
+ export * from "./core/index.mjs";
958
3
  //# sourceMappingURL=index.mjs.map