@chaoslabs/ai-sdk 0.0.11 → 1.0.0
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/CHANGELOG.md +98 -0
- package/README.md +42 -13
- package/dist/blocks.d.ts +12 -4
- package/dist/blocks.js +18 -6
- package/dist/conversation.d.ts +5 -2
- package/dist/conversation.js +15 -25
- package/dist/index.d.ts +9 -6
- package/dist/index.js +33 -2
- package/dist/primitives.d.ts +458 -0
- package/dist/primitives.js +1 -0
- package/dist/request.d.ts +14 -12
- package/dist/request.js +42 -12
- package/dist/response.js +15 -24
- package/dist/schemas.d.ts +5728 -577
- package/dist/schemas.js +583 -92
- package/dist/stream.js +8 -4
- package/dist/type-guards.d.ts +316 -0
- package/dist/type-guards.js +373 -0
- package/dist/types.d.ts +87 -110
- package/dist/types.js +3 -2
- package/package.json +6 -8
package/dist/schemas.js
CHANGED
|
@@ -2,6 +2,48 @@
|
|
|
2
2
|
// Updated to match actual server response structure
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
// ============================================================================
|
|
5
|
+
// Client Configuration Schemas
|
|
6
|
+
// ============================================================================
|
|
7
|
+
export const ChaosConfigSchema = z.object({
|
|
8
|
+
apiKey: z.string(),
|
|
9
|
+
baseUrl: z.string().nullish(),
|
|
10
|
+
timeout: z.number().nullish(),
|
|
11
|
+
useNativeHttp: z.boolean().nullish(),
|
|
12
|
+
});
|
|
13
|
+
// ============================================================================
|
|
14
|
+
// Request Schemas
|
|
15
|
+
// ============================================================================
|
|
16
|
+
export const InputItemSchema = z.object({
|
|
17
|
+
type: z.literal('message'),
|
|
18
|
+
role: z.enum(['user', 'system', 'assistant']),
|
|
19
|
+
content: z.string(),
|
|
20
|
+
});
|
|
21
|
+
export const WalletInfoSchema = z.object({
|
|
22
|
+
address: z.string(),
|
|
23
|
+
chain: z.string(),
|
|
24
|
+
});
|
|
25
|
+
export const RequestMetadataSchema = z.object({
|
|
26
|
+
user_id: z.string(),
|
|
27
|
+
session_id: z.string(),
|
|
28
|
+
wallets: z.array(WalletInfoSchema).nullish(),
|
|
29
|
+
});
|
|
30
|
+
// Note: CreateResponseParams schema omitted as it contains a function callback (onStreamEvent)
|
|
31
|
+
// which cannot be validated with Zod
|
|
32
|
+
// ============================================================================
|
|
33
|
+
// Response Schemas
|
|
34
|
+
// ============================================================================
|
|
35
|
+
export const ResponseErrorSchema = z.object({
|
|
36
|
+
message: z.string(),
|
|
37
|
+
type: z.string(),
|
|
38
|
+
code: z.string(),
|
|
39
|
+
});
|
|
40
|
+
export const OutputTextSchema = z.object({
|
|
41
|
+
type: z.literal('output_text'),
|
|
42
|
+
text: z.string(),
|
|
43
|
+
});
|
|
44
|
+
// Forward declaration for ChaosBlockSchema (defined after BlockSchema)
|
|
45
|
+
// ChaosBlockSchema will be defined after all block schemas
|
|
46
|
+
// ============================================================================
|
|
5
47
|
// Block Schemas - Matching Actual Server Response Structure
|
|
6
48
|
// ============================================================================
|
|
7
49
|
// --- Markdown Block ---
|
|
@@ -11,39 +53,12 @@ export const MarkdownBlockSchema = z.object({
|
|
|
11
53
|
});
|
|
12
54
|
// --- Table Block ---
|
|
13
55
|
// Server sends: { blockType: "table", ... }
|
|
14
|
-
export const TableColumnTypeSchema = z.enum([
|
|
15
|
-
'text',
|
|
16
|
-
'number',
|
|
17
|
-
'currency',
|
|
18
|
-
'percentage',
|
|
19
|
-
'token',
|
|
20
|
-
'date',
|
|
21
|
-
'link',
|
|
22
|
-
'boolean',
|
|
23
|
-
]);
|
|
24
|
-
export const TableSortDirectionSchema = z.enum(['asc', 'desc']);
|
|
25
|
-
export const TableColumnConfigSchema = z.object({
|
|
26
|
-
type: TableColumnTypeSchema.optional(),
|
|
27
|
-
sortable: z.boolean().optional(),
|
|
28
|
-
align: z.enum(['left', 'center', 'right']).optional(),
|
|
29
|
-
width: z.string().optional(),
|
|
30
|
-
});
|
|
31
56
|
export const TableBlockSchema = z.object({
|
|
32
57
|
blockType: z.literal('table'),
|
|
33
|
-
title: z.string(),
|
|
58
|
+
title: z.string().nullish(),
|
|
34
59
|
tableHeaders: z.array(z.string()),
|
|
35
|
-
tableRows: z.array(z.array(z.
|
|
36
|
-
|
|
37
|
-
tableColumnConfigs: z.record(z.string(), TableColumnConfigSchema).optional().nullable(),
|
|
38
|
-
tableHeadersMetadata: z.record(z.string(), z.object({ type: z.string().optional() }).passthrough()).optional().nullable(),
|
|
39
|
-
sourceName: z.string().optional().nullable(),
|
|
40
|
-
defaultSortColumn: z.string().optional().nullable(),
|
|
41
|
-
defaultSortDirection: TableSortDirectionSchema.optional().nullable(),
|
|
42
|
-
maxRows: z.number().optional().nullable(),
|
|
43
|
-
searchable: z.boolean().optional().nullable(),
|
|
44
|
-
exportable: z.boolean().optional().nullable(),
|
|
45
|
-
tool_params: z.unknown().optional().nullable(),
|
|
46
|
-
tool_name: z.string().optional().nullable(),
|
|
60
|
+
tableRows: z.array(z.array(z.unknown())),
|
|
61
|
+
tableHeadersMetadata: z.record(z.string(), z.record(z.string(), z.unknown())).nullish(),
|
|
47
62
|
});
|
|
48
63
|
// --- Chart Block ---
|
|
49
64
|
// Server sends: { blockType: "chart", data: [[label, value], ...], ... }
|
|
@@ -56,7 +71,7 @@ export const ChartDataPointSchema = z.union([
|
|
|
56
71
|
export const ChartSeriesSchema = z.object({
|
|
57
72
|
name: z.string(),
|
|
58
73
|
data: z.array(ChartDataPointSchema),
|
|
59
|
-
color: z.string().
|
|
74
|
+
color: z.string().nullish(),
|
|
60
75
|
});
|
|
61
76
|
// Timeseries format: server sends series as { "24H": [{ label, data }, ...] }
|
|
62
77
|
export const TimeseriesSeriesItemSchema = z.object({
|
|
@@ -73,35 +88,44 @@ export const ChartSeriesFieldSchema = z.union([
|
|
|
73
88
|
export const ChartSegmentSchema = z.object({
|
|
74
89
|
label: z.string(),
|
|
75
90
|
value: z.number(),
|
|
76
|
-
color: z.string().
|
|
91
|
+
color: z.string().nullish(),
|
|
77
92
|
});
|
|
78
93
|
export const ChartAxisSchema = z.object({
|
|
79
|
-
title: z.string().
|
|
80
|
-
min: z.number().
|
|
81
|
-
max: z.number().
|
|
82
|
-
type: z.string().
|
|
94
|
+
title: z.string().nullish(),
|
|
95
|
+
min: z.number().nullish(),
|
|
96
|
+
max: z.number().nullish(),
|
|
97
|
+
type: z.string().nullish(),
|
|
83
98
|
});
|
|
84
99
|
// Legacy data format for pie/donut charts: [[label, value], ...]
|
|
85
100
|
export const ChartLegacyDataSchema = z.array(z.tuple([z.string(), z.number()]));
|
|
86
|
-
|
|
101
|
+
// --- Pie Chart Block ---
|
|
102
|
+
// Server sends: { blockType: "chart", chartType: "pie", data: [[label, value], ...] }
|
|
103
|
+
export const PieChartBlockSchema = z.object({
|
|
87
104
|
blockType: z.literal('chart'),
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
// Legacy data format from server: [[label, value], ...]
|
|
93
|
-
data: ChartLegacyDataSchema.optional().nullable(),
|
|
94
|
-
categories: z.array(z.string()).optional().nullable(),
|
|
95
|
-
xAxis: ChartAxisSchema.optional().nullable(),
|
|
96
|
-
yAxis: ChartAxisSchema.optional().nullable(),
|
|
97
|
-
isCurrency: z.boolean().optional().nullable(),
|
|
98
|
-
sourceName: z.string().optional().nullable(),
|
|
99
|
-
timeframe: z.string().optional().nullable(),
|
|
100
|
-
captions: z.array(z.object({ label: z.string(), value: z.string() })).nullish(),
|
|
101
|
-
titleCryptIcons: z.array(z.object({ name: z.string(), entity: z.string().optional() })).nullish(),
|
|
102
|
-
tool_params: z.unknown().optional().nullable(),
|
|
103
|
-
tool_name: z.string().optional().nullable(),
|
|
105
|
+
chartType: z.literal('pie'),
|
|
106
|
+
title: z.string().nullish(),
|
|
107
|
+
data: z.array(z.tuple([z.string(), z.number()])),
|
|
108
|
+
isCurrency: z.boolean().nullish(),
|
|
104
109
|
});
|
|
110
|
+
// --- Timeseries Chart Block ---
|
|
111
|
+
// Server sends: { blockType: "chart", chartType: "timeseries", series: { ... } }
|
|
112
|
+
export const TimeseriesDataPointSchema = z.object({
|
|
113
|
+
timestamp: z.number(),
|
|
114
|
+
value: z.number(),
|
|
115
|
+
});
|
|
116
|
+
export const TimeseriesChartBlockSchema = z.object({
|
|
117
|
+
blockType: z.literal('chart'),
|
|
118
|
+
chartType: z.literal('timeseries'),
|
|
119
|
+
title: z.string().nullish(),
|
|
120
|
+
series: z.record(z.string(), z.array(TimeseriesDataPointSchema)),
|
|
121
|
+
captions: z.array(z.record(z.string(), z.unknown())).nullish(),
|
|
122
|
+
titleCryptoIcons: z.array(z.record(z.string(), z.unknown())).nullish(),
|
|
123
|
+
isCurrency: z.boolean().nullish(),
|
|
124
|
+
});
|
|
125
|
+
export const ChartBlockSchema = z.discriminatedUnion('chartType', [
|
|
126
|
+
PieChartBlockSchema,
|
|
127
|
+
TimeseriesChartBlockSchema,
|
|
128
|
+
]);
|
|
105
129
|
// --- Transaction Action Block ---
|
|
106
130
|
// Server sends: { blockType: "transaction_action", primitives: [...], transactions: [...], risks: {...}, ... }
|
|
107
131
|
// Primitive display icon
|
|
@@ -113,39 +137,40 @@ export const PrimitiveIconSchema = z.object({
|
|
|
113
137
|
export const PrimitiveLineItemSchema = z.object({
|
|
114
138
|
label: z.string(),
|
|
115
139
|
value: z.string(),
|
|
116
|
-
icon: PrimitiveIconSchema.
|
|
140
|
+
icon: PrimitiveIconSchema.nullish(),
|
|
117
141
|
});
|
|
118
142
|
// Primitive display info
|
|
119
143
|
export const PrimitiveDisplaySchema = z.object({
|
|
120
|
-
headline: z.string().
|
|
121
|
-
action_verb: z.string().
|
|
122
|
-
primary_icon: PrimitiveIconSchema.
|
|
123
|
-
secondary_icon: PrimitiveIconSchema.
|
|
124
|
-
line_items: z.array(PrimitiveLineItemSchema).
|
|
144
|
+
headline: z.string().nullish(),
|
|
145
|
+
action_verb: z.string().nullish(),
|
|
146
|
+
primary_icon: PrimitiveIconSchema.nullish(),
|
|
147
|
+
secondary_icon: PrimitiveIconSchema.nullish(),
|
|
148
|
+
line_items: z.array(PrimitiveLineItemSchema).nullish(),
|
|
125
149
|
});
|
|
126
150
|
// Primitive (the actual action like swap, supply, etc.)
|
|
127
151
|
export const PrimitiveSchema = z.object({
|
|
128
152
|
primitive: z.string(),
|
|
129
|
-
params: z.record(z.string(), z.unknown()).
|
|
130
|
-
display: PrimitiveDisplaySchema.
|
|
153
|
+
params: z.record(z.string(), z.unknown()).nullish(),
|
|
154
|
+
display: PrimitiveDisplaySchema.nullish(),
|
|
131
155
|
});
|
|
132
156
|
// Raw transaction data from server
|
|
133
157
|
export const RawTransactionSchema = z.object({
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
protocolName: z.string().
|
|
142
|
-
|
|
158
|
+
chainId: z.number(),
|
|
159
|
+
contractIcon: z.string().nullish(),
|
|
160
|
+
contractName: z.string().nullish(),
|
|
161
|
+
contractVerified: z.boolean().nullish(),
|
|
162
|
+
data: z.string(),
|
|
163
|
+
description: z.string(),
|
|
164
|
+
gasEstimate: z.number().optional(),
|
|
165
|
+
protocolName: z.string().nullish(),
|
|
166
|
+
to: z.string(),
|
|
167
|
+
value: z.string(),
|
|
143
168
|
});
|
|
144
169
|
// Transaction group (contains multiple raw transactions)
|
|
145
170
|
export const TransactionGroupSchema = z.object({
|
|
146
171
|
transactions: z.array(RawTransactionSchema).optional(),
|
|
147
|
-
requiresApproval: z.boolean().
|
|
148
|
-
verificationUnavailable: z.boolean().
|
|
172
|
+
requiresApproval: z.boolean().nullish(),
|
|
173
|
+
verificationUnavailable: z.boolean().nullish(),
|
|
149
174
|
});
|
|
150
175
|
// Risk impact (quantified risk details)
|
|
151
176
|
export const RiskImpactSchema = z.object({
|
|
@@ -175,19 +200,17 @@ export const RisksSchema = z.object({
|
|
|
175
200
|
});
|
|
176
201
|
export const TransactionActionBlockSchema = z.object({
|
|
177
202
|
blockType: z.literal('transaction_action'),
|
|
178
|
-
value: z.record(z.string(), z.unknown()).
|
|
179
|
-
sequence: z.boolean()
|
|
180
|
-
primitives: z.array(PrimitiveSchema).
|
|
203
|
+
value: z.record(z.string(), z.unknown()).nullish(),
|
|
204
|
+
sequence: z.boolean(),
|
|
205
|
+
primitives: z.array(PrimitiveSchema).min(1),
|
|
181
206
|
transactions: z.array(TransactionGroupSchema).optional(),
|
|
182
|
-
needs_confirmation: z.boolean()
|
|
183
|
-
notes: z.string()
|
|
184
|
-
risks: RisksSchema
|
|
185
|
-
metadata: z.record(z.string(), z.unknown())
|
|
186
|
-
tool_params: z.unknown().optional().nullable(),
|
|
187
|
-
tool_name: z.string().optional().nullable(),
|
|
207
|
+
needs_confirmation: z.boolean(),
|
|
208
|
+
notes: z.string(),
|
|
209
|
+
risks: RisksSchema,
|
|
210
|
+
metadata: z.record(z.string(), z.unknown()),
|
|
188
211
|
});
|
|
189
212
|
// --- Interactive Block ---
|
|
190
|
-
// Server sends: { blockType: "interactive", title, body?, context?, style
|
|
213
|
+
// Server sends: { blockType: "interactive", title, body?, context?, style, options? }
|
|
191
214
|
// Note: Using .nullish() to accept both null and undefined (Python sends null for None)
|
|
192
215
|
export const InteractiveOptionSchema = z.object({
|
|
193
216
|
id: z.string(),
|
|
@@ -200,9 +223,23 @@ export const InteractiveBlockSchema = z.object({
|
|
|
200
223
|
title: z.string(), // Required, matches Python
|
|
201
224
|
body: z.string().nullish(), // Optional, matches Python
|
|
202
225
|
context: z.string().nullish(), // Optional, matches Python
|
|
203
|
-
style: z.enum(['options', 'confirm_cancel'])
|
|
226
|
+
style: z.enum(['options', 'confirm_cancel']), // Required, matches Python
|
|
204
227
|
options: z.array(InteractiveOptionSchema).nullish(),
|
|
205
228
|
});
|
|
229
|
+
// --- Info Block ---
|
|
230
|
+
// Server sends: { blockType: "info", content, error? }
|
|
231
|
+
export const InfoBlockSchema = z.object({
|
|
232
|
+
blockType: z.literal('info'),
|
|
233
|
+
content: z.string(),
|
|
234
|
+
error: z.string().nullish(),
|
|
235
|
+
});
|
|
236
|
+
// --- Code Block ---
|
|
237
|
+
// Server sends: { blockType: "code", content, language }
|
|
238
|
+
export const CodeBlockSchema = z.object({
|
|
239
|
+
blockType: z.literal('code'),
|
|
240
|
+
content: z.string(),
|
|
241
|
+
language: z.string(),
|
|
242
|
+
});
|
|
206
243
|
// ============================================================================
|
|
207
244
|
// Block Detection and Parsing
|
|
208
245
|
// ============================================================================
|
|
@@ -217,9 +254,13 @@ export function detectBlockType(raw) {
|
|
|
217
254
|
if (obj.blockType === 'chart')
|
|
218
255
|
return 'chart';
|
|
219
256
|
if (obj.blockType === 'transaction_action')
|
|
220
|
-
return '
|
|
257
|
+
return 'action';
|
|
221
258
|
if (obj.blockType === 'interactive')
|
|
222
259
|
return 'interactive';
|
|
260
|
+
if (obj.blockType === 'info')
|
|
261
|
+
return 'info';
|
|
262
|
+
if (obj.blockType === 'code')
|
|
263
|
+
return 'code';
|
|
223
264
|
// Markdown blocks have no blockType, just content
|
|
224
265
|
if ('content' in obj && typeof obj.content === 'string' && !('blockType' in obj)) {
|
|
225
266
|
return 'markdown';
|
|
@@ -251,20 +292,38 @@ export function parseRawBlock(raw) {
|
|
|
251
292
|
}
|
|
252
293
|
return { success: false, error: `Chart parse error: ${result.error.message}` };
|
|
253
294
|
}
|
|
254
|
-
case '
|
|
295
|
+
case 'action': {
|
|
255
296
|
const result = TransactionActionBlockSchema.safeParse(raw);
|
|
256
297
|
if (result.success) {
|
|
257
|
-
return { success: true, data: { type: '
|
|
298
|
+
return { success: true, data: { type: 'action', ...result.data }, type: 'action' };
|
|
258
299
|
}
|
|
259
300
|
return { success: false, error: `TransactionAction parse error: ${result.error.message}` };
|
|
260
301
|
}
|
|
261
302
|
case 'interactive': {
|
|
262
303
|
const result = InteractiveBlockSchema.safeParse(raw);
|
|
263
304
|
if (result.success) {
|
|
264
|
-
return {
|
|
305
|
+
return {
|
|
306
|
+
success: true,
|
|
307
|
+
data: { type: 'interactive', ...result.data },
|
|
308
|
+
type: 'interactive',
|
|
309
|
+
};
|
|
265
310
|
}
|
|
266
311
|
return { success: false, error: `Interactive parse error: ${result.error.message}` };
|
|
267
312
|
}
|
|
313
|
+
case 'info': {
|
|
314
|
+
const result = InfoBlockSchema.safeParse(raw);
|
|
315
|
+
if (result.success) {
|
|
316
|
+
return { success: true, data: { type: 'info', ...result.data }, type: 'info' };
|
|
317
|
+
}
|
|
318
|
+
return { success: false, error: `Info parse error: ${result.error.message}` };
|
|
319
|
+
}
|
|
320
|
+
case 'code': {
|
|
321
|
+
const result = CodeBlockSchema.safeParse(raw);
|
|
322
|
+
if (result.success) {
|
|
323
|
+
return { success: true, data: { type: 'code', ...result.data }, type: 'code' };
|
|
324
|
+
}
|
|
325
|
+
return { success: false, error: `Code parse error: ${result.error.message}` };
|
|
326
|
+
}
|
|
268
327
|
default:
|
|
269
328
|
return { success: false, error: `Unknown block type` };
|
|
270
329
|
}
|
|
@@ -275,9 +334,441 @@ export function parseRawBlock(raw) {
|
|
|
275
334
|
// Create a discriminated union schema that adds 'type' field
|
|
276
335
|
// This is for SDK consumers who expect 'type' field
|
|
277
336
|
export const BlockSchema = z.union([
|
|
278
|
-
MarkdownBlockSchema.transform(data => ({ type: 'markdown', ...data })),
|
|
279
|
-
TableBlockSchema.transform(data => ({ type: 'table', ...data })),
|
|
280
|
-
ChartBlockSchema.transform(data => ({ type: 'chart', ...data })),
|
|
281
|
-
|
|
282
|
-
|
|
337
|
+
MarkdownBlockSchema.transform((data) => ({ type: 'markdown', ...data })),
|
|
338
|
+
TableBlockSchema.transform((data) => ({ type: 'table', ...data })),
|
|
339
|
+
ChartBlockSchema.transform((data) => ({ type: 'chart', ...data })),
|
|
340
|
+
PieChartBlockSchema.transform((data) => ({ type: 'chart', ...data })),
|
|
341
|
+
TimeseriesChartBlockSchema.transform((data) => ({ type: 'chart', ...data })),
|
|
342
|
+
TransactionActionBlockSchema.transform((data) => ({ type: 'action', ...data })),
|
|
343
|
+
InteractiveBlockSchema.transform((data) => ({ type: 'interactive', ...data })),
|
|
344
|
+
InfoBlockSchema.transform((data) => ({ type: 'info', ...data })),
|
|
345
|
+
CodeBlockSchema.transform((data) => ({ type: 'code', ...data })),
|
|
346
|
+
]);
|
|
347
|
+
// ============================================================================
|
|
348
|
+
// Response Content Schemas (depend on BlockSchema)
|
|
349
|
+
// ============================================================================
|
|
350
|
+
export const ChaosBlockSchema = z.object({
|
|
351
|
+
type: z.literal('chaos.block'),
|
|
352
|
+
block: BlockSchema,
|
|
353
|
+
});
|
|
354
|
+
export const ContentPartSchema = z.union([OutputTextSchema, ChaosBlockSchema]);
|
|
355
|
+
export const OutputItemSchema = z.object({
|
|
356
|
+
type: z.literal('message'),
|
|
357
|
+
role: z.literal('assistant'),
|
|
358
|
+
content: z.array(ContentPartSchema),
|
|
359
|
+
});
|
|
360
|
+
export const ResponseSchema = z.object({
|
|
361
|
+
id: z.string(),
|
|
362
|
+
object: z.literal('response'),
|
|
363
|
+
model: z.string(),
|
|
364
|
+
status: z.enum(['completed', 'failed']),
|
|
365
|
+
output: z.array(OutputItemSchema),
|
|
366
|
+
error: ResponseErrorSchema.nullish(),
|
|
367
|
+
});
|
|
368
|
+
// ============================================================================
|
|
369
|
+
// V1 API Request Schemas (from request.ts)
|
|
370
|
+
// ============================================================================
|
|
371
|
+
export const PreviousMessageSchema = z.object({
|
|
372
|
+
type: z.literal('message'),
|
|
373
|
+
role: z.enum(['user', 'assistant']),
|
|
374
|
+
content: z.string(),
|
|
375
|
+
});
|
|
376
|
+
export const V1WalletRequestSchema = z.object({
|
|
377
|
+
model: z.string(),
|
|
378
|
+
query: z.string(),
|
|
379
|
+
user_id: z.string(),
|
|
380
|
+
session_id: z.string(),
|
|
381
|
+
wallets: z.array(WalletInfoSchema).nullish(),
|
|
382
|
+
previous_messages: z.array(PreviousMessageSchema).nullish(),
|
|
383
|
+
});
|
|
384
|
+
export const V1AskRequestSchema = z.object({
|
|
385
|
+
model: z.string(),
|
|
386
|
+
query: z.string(),
|
|
387
|
+
user_id: z.string(),
|
|
388
|
+
session_id: z.string(),
|
|
389
|
+
previous_messages: z.array(PreviousMessageSchema).nullish(),
|
|
390
|
+
});
|
|
391
|
+
// ============================================================================
|
|
392
|
+
// V1 API Response Schemas (from response.ts)
|
|
393
|
+
// ============================================================================
|
|
394
|
+
export const V1StreamEventSchema = z
|
|
395
|
+
.object({
|
|
396
|
+
type: z.string().nullish(),
|
|
397
|
+
content: z.record(z.string(), z.unknown()).nullish(),
|
|
398
|
+
})
|
|
399
|
+
.passthrough();
|
|
400
|
+
export const V1FinalStateSchema = z.object({
|
|
401
|
+
blocks: z.array(z.unknown()),
|
|
402
|
+
});
|
|
403
|
+
// ============================================================================
|
|
404
|
+
// Stream Message Schemas (from stream.ts)
|
|
405
|
+
// ============================================================================
|
|
406
|
+
export const MessageTypeSchema = z.enum([
|
|
407
|
+
'agent_status_change',
|
|
408
|
+
'agent_message',
|
|
409
|
+
'report',
|
|
410
|
+
'follow_up_suggestions',
|
|
411
|
+
'user_input',
|
|
412
|
+
]);
|
|
413
|
+
export const AgentStatusSchema = z.enum(['processing', 'done', 'error', 'cancelled']);
|
|
414
|
+
export const StreamMessageContextSchema = z.object({
|
|
415
|
+
sessionId: z.string(),
|
|
416
|
+
artifactId: z.string(),
|
|
417
|
+
query: z.string().nullish(),
|
|
418
|
+
});
|
|
419
|
+
export const AgentStatusContentSchema = z.object({
|
|
420
|
+
status: AgentStatusSchema,
|
|
421
|
+
});
|
|
422
|
+
export const AgentTextDataSchema = z.object({
|
|
423
|
+
message: z.string(),
|
|
424
|
+
});
|
|
425
|
+
export const AgentMessageContentSchema = z.object({
|
|
426
|
+
messageId: z.string(),
|
|
427
|
+
artifactId: z.string(),
|
|
428
|
+
type: z.literal('agent_message'),
|
|
429
|
+
data: AgentTextDataSchema,
|
|
430
|
+
order: z.number().int(),
|
|
431
|
+
createdAt: z.number(),
|
|
432
|
+
updatedAt: z.number(),
|
|
433
|
+
metadata: z.record(z.string(), z.unknown()).nullish(),
|
|
434
|
+
});
|
|
435
|
+
export const AgentMessageSchema = z.object({
|
|
436
|
+
id: z.string(),
|
|
437
|
+
type: z.literal('agent_message'),
|
|
438
|
+
timestamp: z.number(),
|
|
439
|
+
content: AgentMessageContentSchema,
|
|
440
|
+
context: StreamMessageContextSchema,
|
|
441
|
+
});
|
|
442
|
+
export const AgentStatusChangeMessageScheam = z.object({
|
|
443
|
+
id: z.string(),
|
|
444
|
+
type: z.literal('agent_status_change'),
|
|
445
|
+
timestamp: z.number().int(),
|
|
446
|
+
content: {
|
|
447
|
+
status: z.string(),
|
|
448
|
+
},
|
|
449
|
+
context: z.object({
|
|
450
|
+
sessionId: z.string(),
|
|
451
|
+
artifactId: z.string(),
|
|
452
|
+
query: z.string(),
|
|
453
|
+
}),
|
|
454
|
+
});
|
|
455
|
+
export const CaptionSchema = z.object({
|
|
456
|
+
label: z.string(),
|
|
457
|
+
value: z.unknown(),
|
|
458
|
+
group: z.string().nullish(),
|
|
459
|
+
collapsable: z.boolean().nullish(),
|
|
460
|
+
});
|
|
461
|
+
export const ReferenceSchema = z.object({
|
|
462
|
+
title: z.string(),
|
|
463
|
+
url: z.string(),
|
|
464
|
+
content: z.string(),
|
|
465
|
+
hiddenUrl: z.string().nullish(),
|
|
466
|
+
textSpans: z.array(z.array(z.number())).nullish(),
|
|
467
|
+
engineId: z.string().nullish(),
|
|
468
|
+
});
|
|
469
|
+
export const ReportContentSchema = z.object({
|
|
470
|
+
id: z.string(),
|
|
471
|
+
artifactId: z.string(),
|
|
472
|
+
type: z.string(),
|
|
473
|
+
data: BlockSchema,
|
|
474
|
+
order: z.number(),
|
|
475
|
+
createdAt: z.number(),
|
|
476
|
+
updatedAt: z.number(),
|
|
477
|
+
createdBy: z.string().nullish(),
|
|
478
|
+
metadata: z.record(z.string(), z.unknown()).nullish(),
|
|
479
|
+
captions: z.array(CaptionSchema).nullish(),
|
|
480
|
+
references: z.array(ReferenceSchema).nullish(),
|
|
481
|
+
version: z.number().nullish(),
|
|
482
|
+
});
|
|
483
|
+
export const FollowUpDataSchema = z.object({
|
|
484
|
+
followUpQueries: z.array(z.string()),
|
|
485
|
+
});
|
|
486
|
+
export const FollowUpSuggestionsContentSchema = z.object({
|
|
487
|
+
messageId: z.string(),
|
|
488
|
+
artifactId: z.string(),
|
|
489
|
+
type: z.string(),
|
|
490
|
+
data: FollowUpDataSchema,
|
|
491
|
+
order: z.number(),
|
|
492
|
+
createdAt: z.number(),
|
|
493
|
+
updatedAt: z.number(),
|
|
494
|
+
metadata: z.record(z.string(), z.unknown()).nullish(),
|
|
495
|
+
});
|
|
496
|
+
export const UserInputCandidateSchema = z.object({
|
|
497
|
+
symbol: z.string(),
|
|
498
|
+
name: z.string(),
|
|
499
|
+
market_cap: z.number(),
|
|
500
|
+
token_id: z.string(),
|
|
501
|
+
score: z.number(),
|
|
502
|
+
});
|
|
503
|
+
export const UserInputContentSchema = z.object({
|
|
504
|
+
type: z.string(),
|
|
505
|
+
candidates: z.record(z.string(), z.array(UserInputCandidateSchema)),
|
|
506
|
+
});
|
|
507
|
+
// Stream message base fields
|
|
508
|
+
const StreamMessageBaseSchema = z.object({
|
|
509
|
+
id: z.string(),
|
|
510
|
+
timestamp: z.number(),
|
|
511
|
+
context: StreamMessageContextSchema,
|
|
512
|
+
});
|
|
513
|
+
export const AgentStatusMessageSchema = StreamMessageBaseSchema.extend({
|
|
514
|
+
type: z.literal('agent_status_change'),
|
|
515
|
+
content: AgentStatusContentSchema,
|
|
516
|
+
});
|
|
517
|
+
export const AgentTextMessageSchema = StreamMessageBaseSchema.extend({
|
|
518
|
+
type: z.literal('agent_message'),
|
|
519
|
+
content: AgentMessageContentSchema,
|
|
520
|
+
});
|
|
521
|
+
export const ReportMessageSchema = StreamMessageBaseSchema.extend({
|
|
522
|
+
type: z.literal('report'),
|
|
523
|
+
content: ReportContentSchema,
|
|
524
|
+
});
|
|
525
|
+
export const FollowUpSuggestionsMessageSchema = StreamMessageBaseSchema.extend({
|
|
526
|
+
type: z.literal('follow_up_suggestions'),
|
|
527
|
+
content: FollowUpSuggestionsContentSchema,
|
|
528
|
+
});
|
|
529
|
+
export const UserInputMessageSchema = StreamMessageBaseSchema.extend({
|
|
530
|
+
type: z.literal('user_input'),
|
|
531
|
+
content: UserInputContentSchema,
|
|
532
|
+
});
|
|
533
|
+
export const StreamMessageSchema = z.discriminatedUnion('type', [
|
|
534
|
+
AgentStatusMessageSchema,
|
|
535
|
+
AgentTextMessageSchema,
|
|
536
|
+
ReportMessageSchema,
|
|
537
|
+
FollowUpSuggestionsMessageSchema,
|
|
538
|
+
UserInputMessageSchema,
|
|
283
539
|
]);
|
|
540
|
+
// ============================================================================
|
|
541
|
+
// Conversation Schemas (from conversation.ts)
|
|
542
|
+
// ============================================================================
|
|
543
|
+
export const ConversationOptionsSchema = z.object({
|
|
544
|
+
model: z.string().nullish(),
|
|
545
|
+
maxHistoryLength: z.number().nullish(),
|
|
546
|
+
userId: z.string(),
|
|
547
|
+
wallets: z.array(z.object({ address: z.string(), chain: z.string() })).nullish(),
|
|
548
|
+
sessionId: z.string().nullish(),
|
|
549
|
+
});
|
|
550
|
+
export const ConversationStatsSchema = z.object({
|
|
551
|
+
userTurns: z.number(),
|
|
552
|
+
assistantTurns: z.number(),
|
|
553
|
+
totalMessages: z.number(),
|
|
554
|
+
sessionId: z.string(),
|
|
555
|
+
startedAt: z.date(),
|
|
556
|
+
lastMessageAt: z.date().nullable(),
|
|
557
|
+
});
|
|
558
|
+
// ============================================================================
|
|
559
|
+
// Primitive Param Schemas (from primitives.ts)
|
|
560
|
+
// ============================================================================
|
|
561
|
+
export const PrimitiveTypeSchema = z.enum([
|
|
562
|
+
'swap',
|
|
563
|
+
'supply',
|
|
564
|
+
'withdraw',
|
|
565
|
+
'borrow',
|
|
566
|
+
'repay',
|
|
567
|
+
'stake',
|
|
568
|
+
'unstake',
|
|
569
|
+
'claim',
|
|
570
|
+
'bridge',
|
|
571
|
+
'add_liquidity',
|
|
572
|
+
'remove_liquidity',
|
|
573
|
+
'open_position',
|
|
574
|
+
'close_position',
|
|
575
|
+
'deposit',
|
|
576
|
+
'mint',
|
|
577
|
+
'burn',
|
|
578
|
+
'restake',
|
|
579
|
+
'approve',
|
|
580
|
+
'transfer',
|
|
581
|
+
'wrap',
|
|
582
|
+
'unwrap',
|
|
583
|
+
]);
|
|
584
|
+
export const SwapParamsSchema = z.object({
|
|
585
|
+
token_in: z.string(),
|
|
586
|
+
token_out: z.string(),
|
|
587
|
+
amount_in: z.string().nullish(),
|
|
588
|
+
amount_out: z.string().nullish(),
|
|
589
|
+
slippage: z.number().nullish(),
|
|
590
|
+
protocol: z.string().nullish(),
|
|
591
|
+
});
|
|
592
|
+
export const BridgeParamsSchema = z.object({
|
|
593
|
+
asset: z.string(),
|
|
594
|
+
amount: z.string(),
|
|
595
|
+
from_chain: z.string(),
|
|
596
|
+
to_chain: z.string(),
|
|
597
|
+
recipient: z.string().nullish(),
|
|
598
|
+
protocol: z.string().nullish(),
|
|
599
|
+
});
|
|
600
|
+
export const LendingParamsSchema = z.object({
|
|
601
|
+
asset: z.string(),
|
|
602
|
+
amount: z.string(),
|
|
603
|
+
protocol: z.string().nullish(),
|
|
604
|
+
interest_rate_mode: z.number().nullish(),
|
|
605
|
+
});
|
|
606
|
+
export const StakingParamsSchema = z.object({
|
|
607
|
+
amount: z.string(),
|
|
608
|
+
protocol: z.string().nullish(),
|
|
609
|
+
operator: z.string().nullish(),
|
|
610
|
+
});
|
|
611
|
+
export const ClaimParamsSchema = z.object({
|
|
612
|
+
protocol: z.string().nullish(),
|
|
613
|
+
assets: z.array(z.string()).nullish(),
|
|
614
|
+
});
|
|
615
|
+
export const LiquidityParamsSchema = z.object({
|
|
616
|
+
token_a: z.string(),
|
|
617
|
+
token_b: z.string(),
|
|
618
|
+
amount_a: z.string().nullish(),
|
|
619
|
+
amount_b: z.string().nullish(),
|
|
620
|
+
percentage: z.number().nullish(),
|
|
621
|
+
fee_tier: z.number().nullish(),
|
|
622
|
+
protocol: z.string().nullish(),
|
|
623
|
+
});
|
|
624
|
+
export const PositionParamsSchema = z.object({
|
|
625
|
+
market: z.string(),
|
|
626
|
+
side: z.enum(['long', 'short']).nullish(),
|
|
627
|
+
size: z.string().nullish(),
|
|
628
|
+
leverage: z.number().nullish(),
|
|
629
|
+
percentage: z.number().nullish(),
|
|
630
|
+
protocol: z.string().nullish(),
|
|
631
|
+
});
|
|
632
|
+
export const DepositParamsSchema = z.object({
|
|
633
|
+
vault: z.string(),
|
|
634
|
+
amount: z.string(),
|
|
635
|
+
protocol: z.string().nullish(),
|
|
636
|
+
});
|
|
637
|
+
export const MintParamsSchema = z.object({
|
|
638
|
+
asset: z.string(),
|
|
639
|
+
amount: z.string(),
|
|
640
|
+
collateral: z.string().nullish(),
|
|
641
|
+
protocol: z.string().nullish(),
|
|
642
|
+
});
|
|
643
|
+
export const ApproveParamsSchema = z.object({
|
|
644
|
+
token: z.string(),
|
|
645
|
+
spender: z.string(),
|
|
646
|
+
amount: z.string().nullish(),
|
|
647
|
+
});
|
|
648
|
+
export const TransferParamsSchema = z.object({
|
|
649
|
+
token: z.string(),
|
|
650
|
+
to: z.string(),
|
|
651
|
+
amount: z.string(),
|
|
652
|
+
});
|
|
653
|
+
export const WrapParamsSchema = z.object({
|
|
654
|
+
amount: z.string(),
|
|
655
|
+
});
|
|
656
|
+
// Typed primitive schemas
|
|
657
|
+
const TypedPrimitiveBaseSchema = z.object({
|
|
658
|
+
display: PrimitiveDisplaySchema.nullish(),
|
|
659
|
+
});
|
|
660
|
+
export const SwapPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
661
|
+
primitive: z.literal('swap'),
|
|
662
|
+
params: SwapParamsSchema,
|
|
663
|
+
});
|
|
664
|
+
export const BridgePrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
665
|
+
primitive: z.literal('bridge'),
|
|
666
|
+
params: BridgeParamsSchema,
|
|
667
|
+
});
|
|
668
|
+
export const SupplyPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
669
|
+
primitive: z.literal('supply'),
|
|
670
|
+
params: LendingParamsSchema,
|
|
671
|
+
});
|
|
672
|
+
export const WithdrawPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
673
|
+
primitive: z.literal('withdraw'),
|
|
674
|
+
params: LendingParamsSchema,
|
|
675
|
+
});
|
|
676
|
+
export const BorrowPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
677
|
+
primitive: z.literal('borrow'),
|
|
678
|
+
params: LendingParamsSchema,
|
|
679
|
+
});
|
|
680
|
+
export const RepayPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
681
|
+
primitive: z.literal('repay'),
|
|
682
|
+
params: LendingParamsSchema,
|
|
683
|
+
});
|
|
684
|
+
export const StakePrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
685
|
+
primitive: z.literal('stake'),
|
|
686
|
+
params: StakingParamsSchema,
|
|
687
|
+
});
|
|
688
|
+
export const UnstakePrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
689
|
+
primitive: z.literal('unstake'),
|
|
690
|
+
params: StakingParamsSchema,
|
|
691
|
+
});
|
|
692
|
+
export const ClaimPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
693
|
+
primitive: z.literal('claim'),
|
|
694
|
+
params: ClaimParamsSchema,
|
|
695
|
+
});
|
|
696
|
+
export const AddLiquidityPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
697
|
+
primitive: z.literal('add_liquidity'),
|
|
698
|
+
params: LiquidityParamsSchema,
|
|
699
|
+
});
|
|
700
|
+
export const RemoveLiquidityPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
701
|
+
primitive: z.literal('remove_liquidity'),
|
|
702
|
+
params: LiquidityParamsSchema,
|
|
703
|
+
});
|
|
704
|
+
export const OpenPositionPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
705
|
+
primitive: z.literal('open_position'),
|
|
706
|
+
params: PositionParamsSchema,
|
|
707
|
+
});
|
|
708
|
+
export const ClosePositionPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
709
|
+
primitive: z.literal('close_position'),
|
|
710
|
+
params: PositionParamsSchema,
|
|
711
|
+
});
|
|
712
|
+
export const DepositPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
713
|
+
primitive: z.literal('deposit'),
|
|
714
|
+
params: DepositParamsSchema,
|
|
715
|
+
});
|
|
716
|
+
export const MintPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
717
|
+
primitive: z.literal('mint'),
|
|
718
|
+
params: MintParamsSchema,
|
|
719
|
+
});
|
|
720
|
+
export const BurnPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
721
|
+
primitive: z.literal('burn'),
|
|
722
|
+
params: MintParamsSchema,
|
|
723
|
+
});
|
|
724
|
+
export const RestakePrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
725
|
+
primitive: z.literal('restake'),
|
|
726
|
+
params: StakingParamsSchema,
|
|
727
|
+
});
|
|
728
|
+
export const ApprovePrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
729
|
+
primitive: z.literal('approve'),
|
|
730
|
+
params: ApproveParamsSchema,
|
|
731
|
+
});
|
|
732
|
+
export const TransferPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
733
|
+
primitive: z.literal('transfer'),
|
|
734
|
+
params: TransferParamsSchema,
|
|
735
|
+
});
|
|
736
|
+
export const WrapPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
737
|
+
primitive: z.literal('wrap'),
|
|
738
|
+
params: WrapParamsSchema,
|
|
739
|
+
});
|
|
740
|
+
export const UnwrapPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
741
|
+
primitive: z.literal('unwrap'),
|
|
742
|
+
params: WrapParamsSchema,
|
|
743
|
+
});
|
|
744
|
+
export const UnknownPrimitiveSchema = TypedPrimitiveBaseSchema.extend({
|
|
745
|
+
primitive: z.string(),
|
|
746
|
+
params: z.record(z.string(), z.unknown()),
|
|
747
|
+
});
|
|
748
|
+
export const TypedPrimitiveSchema = z.union([
|
|
749
|
+
SwapPrimitiveSchema,
|
|
750
|
+
BridgePrimitiveSchema,
|
|
751
|
+
SupplyPrimitiveSchema,
|
|
752
|
+
WithdrawPrimitiveSchema,
|
|
753
|
+
BorrowPrimitiveSchema,
|
|
754
|
+
RepayPrimitiveSchema,
|
|
755
|
+
StakePrimitiveSchema,
|
|
756
|
+
UnstakePrimitiveSchema,
|
|
757
|
+
ClaimPrimitiveSchema,
|
|
758
|
+
AddLiquidityPrimitiveSchema,
|
|
759
|
+
RemoveLiquidityPrimitiveSchema,
|
|
760
|
+
OpenPositionPrimitiveSchema,
|
|
761
|
+
ClosePositionPrimitiveSchema,
|
|
762
|
+
DepositPrimitiveSchema,
|
|
763
|
+
MintPrimitiveSchema,
|
|
764
|
+
BurnPrimitiveSchema,
|
|
765
|
+
RestakePrimitiveSchema,
|
|
766
|
+
ApprovePrimitiveSchema,
|
|
767
|
+
TransferPrimitiveSchema,
|
|
768
|
+
WrapPrimitiveSchema,
|
|
769
|
+
UnwrapPrimitiveSchema,
|
|
770
|
+
UnknownPrimitiveSchema,
|
|
771
|
+
]);
|
|
772
|
+
// ============================================================================
|
|
773
|
+
// Chart Subtype Schemas (from type-guards.ts)
|
|
774
|
+
// ============================================================================
|