@dbx-tools/shared-mastra 0.6.218 → 0.6.219

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/src/stream.ts DELETED
@@ -1,164 +0,0 @@
1
- /**
2
- * Runtime-validated Mastra agent stream chunks shared by browser clients.
3
- *
4
- * Known event names are strict about the payload fields the UI consumes, so a
5
- * malformed upstream chunk fails before it reaches a state reducer. Unknown
6
- * event names remain forward-compatible through an explicit `unknown` variant
7
- * that preserves the original event name and payload.
8
- *
9
- * @module
10
- */
11
-
12
- import { z } from "zod";
13
-
14
- const runFields = {
15
- runId: z.string().min(1).optional(),
16
- };
17
-
18
- const EmptyStreamChunkSchema = <Type extends string>(type: Type) =>
19
- z
20
- .object({
21
- type: z.literal(type),
22
- payload: z.unknown().optional(),
23
- ...runFields,
24
- })
25
- .passthrough();
26
-
27
- const TextDeltaStreamChunkSchema = z
28
- .object({
29
- type: z.literal("text-delta"),
30
- payload: z.object({ text: z.string() }).passthrough(),
31
- ...runFields,
32
- })
33
- .passthrough();
34
-
35
- const ReasoningDeltaStreamChunkSchema = z
36
- .object({
37
- type: z.literal("reasoning-delta"),
38
- payload: z.object({ text: z.string() }).passthrough(),
39
- ...runFields,
40
- })
41
- .passthrough();
42
-
43
- const ToolCallStreamChunkSchema = z
44
- .object({
45
- type: z.literal("tool-call"),
46
- payload: z
47
- .object({
48
- toolCallId: z.string().min(1),
49
- toolName: z.string().min(1),
50
- args: z.unknown().optional(),
51
- })
52
- .passthrough(),
53
- ...runFields,
54
- })
55
- .passthrough();
56
-
57
- const ToolCallApprovalStreamChunkSchema = z
58
- .object({
59
- type: z.literal("tool-call-approval"),
60
- payload: z
61
- .object({
62
- toolCallId: z.string().min(1),
63
- toolName: z.string().min(1),
64
- args: z.unknown().optional(),
65
- runId: z.string().min(1).optional(),
66
- })
67
- .passthrough(),
68
- ...runFields,
69
- })
70
- .passthrough();
71
-
72
- const ToolTerminalStreamChunkSchema = <Type extends "tool-result" | "tool-error">(type: Type) =>
73
- z
74
- .object({
75
- type: z.literal(type),
76
- payload: z.object({ toolCallId: z.string().min(1) }).passthrough(),
77
- ...runFields,
78
- })
79
- .passthrough();
80
-
81
- const ToolOutputStreamChunkSchema = z
82
- .object({
83
- type: z.literal("tool-output"),
84
- payload: z
85
- .object({
86
- toolCallId: z.string().min(1),
87
- output: z.unknown(),
88
- })
89
- .passthrough(),
90
- ...runFields,
91
- })
92
- .passthrough();
93
-
94
- const StreamErrorDetailSchema = z.union([z.string(), z.record(z.string(), z.unknown())]);
95
-
96
- const ErrorStreamChunkSchema = z
97
- .object({
98
- type: z.literal("error"),
99
- payload: z
100
- .object({
101
- error: StreamErrorDetailSchema.optional(),
102
- message: z.string().optional(),
103
- })
104
- .passthrough()
105
- .optional(),
106
- ...runFields,
107
- })
108
- .passthrough();
109
-
110
- /** Known Mastra events consumed by the chat UI. */
111
- export const KnownMastraStreamChunkSchema = z.discriminatedUnion("type", [
112
- EmptyStreamChunkSchema("text-start"),
113
- TextDeltaStreamChunkSchema,
114
- EmptyStreamChunkSchema("text-end"),
115
- ReasoningDeltaStreamChunkSchema,
116
- ToolCallStreamChunkSchema,
117
- ToolCallApprovalStreamChunkSchema,
118
- ToolTerminalStreamChunkSchema("tool-result"),
119
- ToolTerminalStreamChunkSchema("tool-error"),
120
- ToolOutputStreamChunkSchema,
121
- ErrorStreamChunkSchema,
122
- ]);
123
-
124
- /** A validated stream event whose name this client version does not consume. */
125
- export type UnknownMastraStreamChunk = {
126
- type: "unknown";
127
- eventType: string;
128
- payload?: unknown;
129
- runId?: string;
130
- };
131
-
132
- const knownEventTypes: ReadonlySet<string> = new Set(
133
- KnownMastraStreamChunkSchema.options.map((option) => option.shape.type.value),
134
- );
135
-
136
- const UnknownMastraStreamChunkSchema = z
137
- .object({
138
- type: z.string().min(1),
139
- payload: z.unknown().optional(),
140
- ...runFields,
141
- })
142
- .passthrough()
143
- .refine((chunk) => !knownEventTypes.has(chunk.type), {
144
- path: ["type"],
145
- message: "known stream event has an invalid payload",
146
- })
147
- .transform((chunk): UnknownMastraStreamChunk => ({
148
- type: "unknown",
149
- eventType: chunk.type,
150
- ...(chunk.payload !== undefined ? { payload: chunk.payload } : {}),
151
- ...(chunk.runId !== undefined ? { runId: chunk.runId } : {}),
152
- }));
153
-
154
- /**
155
- * Complete validated stream union. Known events retain their discriminant;
156
- * future event names normalize to the explicit `unknown` variant.
157
- */
158
- export const MastraStreamChunkSchema = z.union([
159
- KnownMastraStreamChunkSchema,
160
- UnknownMastraStreamChunkSchema,
161
- ]);
162
-
163
- /** One runtime-validated event from a Mastra agent SSE stream. */
164
- export type MastraStreamChunk = z.output<typeof MastraStreamChunkSchema>;