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,71 @@
1
+ import { AssistantStreamChunk } from "../../AssistantStreamChunk";
2
+ import { Counter } from "../Counter";
3
+
4
+ export class PathAppendEncoder extends TransformStream<
5
+ AssistantStreamChunk,
6
+ AssistantStreamChunk
7
+ > {
8
+ constructor(idx: number) {
9
+ super({
10
+ transform(chunk, controller) {
11
+ controller.enqueue({
12
+ ...chunk,
13
+ path: [idx, ...chunk.path],
14
+ });
15
+ },
16
+ });
17
+ }
18
+ }
19
+
20
+ export class PathAppendDecoder extends TransformStream<
21
+ AssistantStreamChunk,
22
+ AssistantStreamChunk
23
+ > {
24
+ constructor(idx: number) {
25
+ super({
26
+ transform(chunk, controller) {
27
+ const {
28
+ path: [idx2, ...path],
29
+ } = chunk;
30
+
31
+ if (idx !== idx2)
32
+ throw new Error(`Path mismatch: expected ${idx}, got ${idx2}`);
33
+
34
+ controller.enqueue({
35
+ ...chunk,
36
+ path,
37
+ });
38
+ },
39
+ });
40
+ }
41
+ }
42
+
43
+ export class PathMergeEncoder extends TransformStream<
44
+ AssistantStreamChunk,
45
+ AssistantStreamChunk
46
+ > {
47
+ constructor(counter: Counter) {
48
+ const innerCounter = new Counter();
49
+ const mapping = new Map<number, number>();
50
+ super({
51
+ transform(chunk, controller) {
52
+ if (chunk.type === "part-start" && chunk.path.length === 0) {
53
+ mapping.set(innerCounter.up(), counter.up());
54
+ }
55
+
56
+ const [idx, ...path] = chunk.path;
57
+ if (idx === undefined) {
58
+ controller.enqueue(chunk);
59
+ return;
60
+ }
61
+ const mappedIdx = mapping.get(idx);
62
+ if (mappedIdx === undefined) throw new Error("Path not found");
63
+
64
+ controller.enqueue({
65
+ ...chunk,
66
+ path: [mappedIdx, ...path],
67
+ });
68
+ },
69
+ });
70
+ }
71
+ }
@@ -0,0 +1,150 @@
1
+ import {
2
+ ReadonlyJSONObject,
3
+ ReadonlyJSONValue,
4
+ } from "../../utils/json/json-value";
5
+
6
+ type TextStatus =
7
+ | {
8
+ type: "running";
9
+ }
10
+ | {
11
+ type: "complete";
12
+ reason: "stop" | "unknown";
13
+ }
14
+ | {
15
+ type: "incomplete";
16
+ reason: "cancelled" | "length" | "content-filter" | "other";
17
+ };
18
+
19
+ // export type StepStartPart = {
20
+ // type: "step-start";
21
+ // };
22
+
23
+ export type TextPart = {
24
+ type: "text";
25
+ text: string;
26
+ status: TextStatus;
27
+ };
28
+
29
+ export type ReasoningPart = {
30
+ type: "reasoning";
31
+ text: string;
32
+ status: TextStatus;
33
+ };
34
+
35
+ type ToolCallStatus =
36
+ | {
37
+ type: "running";
38
+ isArgsComplete: boolean;
39
+ }
40
+ | {
41
+ type: "requires-action";
42
+ reason: "tool-call-result";
43
+ }
44
+ | {
45
+ type: "complete";
46
+ reason: "stop" | "unknown";
47
+ }
48
+ | {
49
+ type: "incomplete";
50
+ reason: "cancelled" | "length" | "content-filter" | "other";
51
+ };
52
+
53
+ export type ToolCallPart = {
54
+ type: "tool-call";
55
+ state: "partial-call" | "call" | "result";
56
+ status: ToolCallStatus;
57
+ toolCallId: string;
58
+ toolName: string;
59
+ argsText: string;
60
+ args: ReadonlyJSONObject;
61
+ artifact?: unknown;
62
+ result?: ReadonlyJSONValue;
63
+ isError?: boolean;
64
+ };
65
+
66
+ export type SourcePart = {
67
+ type: "source";
68
+ sourceType: "url";
69
+ id: string;
70
+ url: string;
71
+ title?: string;
72
+ };
73
+
74
+ export type FilePart = {
75
+ type: "file";
76
+ data: string;
77
+ mimeType: string;
78
+ };
79
+
80
+ export type AssistantMessagePart =
81
+ | TextPart
82
+ | ReasoningPart
83
+ | ToolCallPart
84
+ | SourcePart
85
+ | FilePart;
86
+
87
+ type AssistantMessageStepUsage = {
88
+ promptTokens: number;
89
+ completionTokens: number;
90
+ };
91
+
92
+ type AssistantMessageStepMetadata =
93
+ | {
94
+ state: "started";
95
+ messageId: string;
96
+ }
97
+ | {
98
+ state: "finished";
99
+ messageId: string;
100
+ finishReason:
101
+ | "stop"
102
+ | "length"
103
+ | "content-filter"
104
+ | "tool-calls"
105
+ | "error"
106
+ | "other"
107
+ | "unknown";
108
+ usage?: AssistantMessageStepUsage;
109
+ isContinued: boolean;
110
+ };
111
+
112
+ export type AssistantMessageStatus =
113
+ | {
114
+ type: "running";
115
+ }
116
+ | {
117
+ type: "requires-action";
118
+ reason: "tool-calls";
119
+ }
120
+ | {
121
+ type: "complete";
122
+ reason: "stop" | "unknown";
123
+ }
124
+ | {
125
+ type: "incomplete";
126
+ reason:
127
+ | "cancelled"
128
+ | "tool-calls"
129
+ | "length"
130
+ | "content-filter"
131
+ | "other"
132
+ | "error";
133
+ error?: ReadonlyJSONValue;
134
+ };
135
+
136
+ export type AssistantMessage = {
137
+ role: "assistant";
138
+ status: AssistantMessageStatus;
139
+ parts: AssistantMessagePart[];
140
+ /**
141
+ * @deprecated Use `parts` instead.
142
+ */
143
+ content: AssistantMessagePart[];
144
+ metadata: {
145
+ unstable_data: ReadonlyJSONValue[];
146
+ unstable_annotations: ReadonlyJSONValue[];
147
+ steps: AssistantMessageStepMetadata[];
148
+ custom: Record<string, unknown>;
149
+ };
150
+ };
@@ -0,0 +1,20 @@
1
+ export function withPromiseOrValue<T>(
2
+ callback: () => T | PromiseLike<T>,
3
+ thenHandler: (value: T) => PromiseLike<void> | void,
4
+ catchHandler: (error: unknown) => PromiseLike<void> | void,
5
+ ): PromiseLike<void> | void {
6
+ try {
7
+ const promiseOrValue = callback();
8
+ if (
9
+ typeof promiseOrValue === "object" &&
10
+ promiseOrValue !== null &&
11
+ "then" in promiseOrValue
12
+ ) {
13
+ return promiseOrValue.then(thenHandler, catchHandler);
14
+ } else {
15
+ thenHandler(promiseOrValue);
16
+ }
17
+ } catch (e) {
18
+ catchHandler(e);
19
+ }
20
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./core";
@@ -0,0 +1,24 @@
1
+ export type AsyncIterableStream<T> = AsyncIterable<T> & ReadableStream<T>;
2
+
3
+ async function* streamGeneratorPolyfill<T>(
4
+ this: ReadableStream<T>,
5
+ ): AsyncIterator<T, undefined, unknown> {
6
+ const reader = this.getReader();
7
+ try {
8
+ while (true) {
9
+ const { done, value } = await reader.read();
10
+ if (done) break;
11
+ yield value;
12
+ }
13
+ } finally {
14
+ reader.releaseLock();
15
+ }
16
+ }
17
+
18
+ export function asAsyncIterableStream<T>(
19
+ source: ReadableStream<T>,
20
+ ): AsyncIterableStream<T> {
21
+ (source as AsyncIterableStream<T>)[Symbol.asyncIterator] ??=
22
+ streamGeneratorPolyfill;
23
+ return source as AsyncIterableStream<T>;
24
+ }