@chaoslabs/ai-sdk 0.0.1 → 0.0.2

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/dist/schemas.js CHANGED
@@ -1,143 +1,254 @@
1
- // Chaos AI SDK V1 - Zod Schemas
2
- import { z } from "zod";
1
+ // Zod schemas for block validation (match Python backend)
2
+ // Updated to match actual server response structure
3
+ import { z } from 'zod';
3
4
  // ============================================================================
4
- // Client Configuration
5
+ // Block Schemas - Matching Actual Server Response Structure
5
6
  // ============================================================================
6
- export const ChaosConfigSchema = z.object({
7
- apiKey: z.string(),
8
- baseUrl: z.string().optional(),
9
- timeout: z.number().optional(),
10
- });
11
- // ============================================================================
12
- // Request Types
13
- // ============================================================================
14
- export const InputItemSchema = z.object({
15
- type: z.literal("message"),
16
- role: z.enum(["user", "system"]),
7
+ // --- Markdown Block ---
8
+ // Server sends: { content: string } (no type field)
9
+ export const MarkdownBlockSchema = z.object({
17
10
  content: z.string(),
18
11
  });
19
- export const RequestMetadataSchema = z.object({
20
- user_id: z.string(),
21
- session_id: z.string(),
22
- wallet_id: z.string().optional(),
23
- });
24
- export const CreateResponseParamsSchema = z.object({
25
- model: z.string(),
26
- input: z.array(InputItemSchema),
27
- stream: z.boolean().optional(),
28
- metadata: RequestMetadataSchema,
12
+ // --- Table Block ---
13
+ // 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(),
29
30
  });
30
- // ============================================================================
31
- // Block Types
32
- // ============================================================================
33
- export const TableCellValueSchema = z.union([z.string(), z.number()]);
34
31
  export const TableBlockSchema = z.object({
35
- type: z.literal("table"),
32
+ blockType: z.literal('table'),
36
33
  title: z.string(),
37
34
  tableHeaders: z.array(z.string()),
38
- rows: z.array(z.array(TableCellValueSchema)),
35
+ tableRows: z.array(z.array(z.union([z.string(), z.number(), z.boolean(), z.null()]))),
36
+ tableHeadersTypes: z.record(z.string(), z.union([TableColumnTypeSchema, z.literal('currency'), z.literal('percentage')])).optional().nullable(),
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(),
47
+ });
48
+ // --- Chart Block ---
49
+ // Server sends: { blockType: "chart", data: [[label, value], ...], ... }
50
+ export const ChartDataPointSchema = z.union([
51
+ z.tuple([z.number(), z.number()]),
52
+ z.object({ x: z.union([z.number(), z.string()]), y: z.number() }),
53
+ z.array(z.unknown()),
54
+ ]);
55
+ export const ChartSeriesSchema = z.object({
56
+ name: z.string(),
57
+ data: z.array(ChartDataPointSchema),
58
+ color: z.string().optional(),
39
59
  });
40
- export const MarkdownBlockSchema = z.object({
41
- type: z.literal("markdown"),
42
- content: z.string(),
60
+ export const ChartSegmentSchema = z.object({
61
+ label: z.string(),
62
+ value: z.number(),
63
+ color: z.string().optional(),
64
+ });
65
+ export const ChartAxisSchema = z.object({
66
+ title: z.string().optional(),
67
+ min: z.number().optional(),
68
+ max: z.number().optional(),
69
+ type: z.string().optional(),
70
+ });
71
+ // Legacy data format for pie/donut charts: [[label, value], ...]
72
+ export const ChartLegacyDataSchema = z.array(z.tuple([z.string(), z.number()]));
73
+ export const ChartBlockSchema = z.object({
74
+ blockType: z.literal('chart'),
75
+ title: z.string(),
76
+ chartType: z.enum(['pie', 'donut', 'line', 'area', 'bar', 'timeseries']).optional().nullable(),
77
+ series: z.array(ChartSeriesSchema).optional().nullable(),
78
+ segments: z.array(ChartSegmentSchema).optional().nullable(),
79
+ // Legacy data format from server: [[label, value], ...]
80
+ data: ChartLegacyDataSchema.optional().nullable(),
81
+ categories: z.array(z.string()).optional().nullable(),
82
+ xAxis: ChartAxisSchema.optional().nullable(),
83
+ yAxis: ChartAxisSchema.optional().nullable(),
84
+ isCurrency: z.boolean().optional().nullable(),
85
+ sourceName: z.string().optional().nullable(),
86
+ timeframe: z.string().optional().nullable(),
87
+ tool_params: z.unknown().optional().nullable(),
88
+ tool_name: z.string().optional().nullable(),
89
+ });
90
+ // --- Transaction Action Block ---
91
+ // Server sends: { blockType: "transaction_action", primitives: [...], transactions: [...], risks: {...}, ... }
92
+ // Primitive display icon
93
+ export const PrimitiveIconSchema = z.object({
94
+ type: z.string(),
95
+ value: z.string(),
96
+ });
97
+ // Primitive display line item
98
+ export const PrimitiveLineItemSchema = z.object({
99
+ label: z.string(),
100
+ value: z.string(),
101
+ icon: PrimitiveIconSchema.optional(),
43
102
  });
44
- // Primitive params - accepts any JSON-compatible value
45
- export const PrimitiveParamsSchema = z.record(z.string(), z.unknown());
103
+ // Primitive display info
46
104
  export const PrimitiveDisplaySchema = z.object({
47
- headline: z.string(),
48
- amount: z.string().optional(),
105
+ headline: z.string().optional(),
49
106
  action_verb: z.string().optional(),
50
- from_asset: z.string().optional(),
51
- to_asset: z.string().optional(),
52
- from_chain: z.string().optional(),
53
- to_chain: z.string().optional(),
54
- protocol: z.string().optional(),
55
- leverage: z.string().optional(),
56
- direction: z.string().optional(),
57
- recipient: z.string().optional(),
107
+ primary_icon: PrimitiveIconSchema.optional(),
108
+ secondary_icon: PrimitiveIconSchema.optional(),
109
+ line_items: z.array(PrimitiveLineItemSchema).optional(),
58
110
  });
111
+ // Primitive (the actual action like swap, supply, etc.)
59
112
  export const PrimitiveSchema = z.object({
60
113
  primitive: z.string(),
61
- params: PrimitiveParamsSchema,
114
+ params: z.record(z.string(), z.unknown()).optional(),
62
115
  display: PrimitiveDisplaySchema.optional(),
63
116
  });
117
+ // Raw transaction data from server
118
+ export const RawTransactionSchema = z.object({
119
+ to: z.string().optional(),
120
+ data: z.string().optional(),
121
+ value: z.string().optional(),
122
+ chainId: z.number().optional(),
123
+ description: z.string().optional(),
124
+ contractName: z.string().optional(),
125
+ contractVerified: z.boolean().optional(),
126
+ protocolName: z.string().optional(),
127
+ contractIcon: z.string().optional(),
128
+ });
129
+ // Transaction group (contains multiple raw transactions)
130
+ export const TransactionGroupSchema = z.object({
131
+ transactions: z.array(RawTransactionSchema).optional(),
132
+ requiresApproval: z.boolean().optional(),
133
+ verificationUnavailable: z.boolean().optional(),
134
+ });
135
+ // Risk info item
136
+ export const RiskInfoItemSchema = z.object({
137
+ id: z.string().optional(),
138
+ severity: z.enum(['info', 'warning', 'error', 'critical']).optional(),
139
+ title: z.string().optional(),
140
+ message: z.string().optional(),
141
+ });
142
+ // Risks object
64
143
  export const RisksSchema = z.object({
65
- level: z.enum(["low", "medium", "high", "critical"]),
66
- warnings: z.array(z.string()),
144
+ level: z.string().optional(),
67
145
  blockers: z.array(z.string()).optional(),
146
+ warnings: z.array(z.string()).optional(),
147
+ info: z.array(RiskInfoItemSchema).optional(),
68
148
  });
69
149
  export const TransactionActionBlockSchema = z.object({
70
- type: z.literal("transaction_action"),
71
- title: z.string(),
72
- primitives: z.array(PrimitiveSchema),
150
+ blockType: z.literal('transaction_action'),
151
+ value: z.record(z.string(), z.unknown()).optional(),
152
+ sequence: z.boolean().optional(),
153
+ primitives: z.array(PrimitiveSchema).optional(),
154
+ transactions: z.array(TransactionGroupSchema).optional(),
155
+ needs_confirmation: z.boolean().optional(),
156
+ notes: z.string().optional(),
73
157
  risks: RisksSchema.optional(),
158
+ metadata: z.record(z.string(), z.unknown()).optional(),
159
+ tool_params: z.unknown().optional().nullable(),
160
+ tool_name: z.string().optional().nullable(),
74
161
  });
162
+ // --- Interactive Block ---
163
+ // Server sends: { blockType: "interactive", ... }
75
164
  export const InteractiveOptionSchema = z.object({
76
165
  id: z.string(),
77
166
  label: z.string(),
78
167
  description: z.string().optional(),
168
+ value: z.unknown().optional(),
79
169
  });
80
- export const InteractiveCardBlockSchema = z.object({
81
- type: z.literal("interactive_card"),
82
- title: z.string(),
83
- body: z.string().optional(),
84
- style: z.enum(["options", "confirm_cancel"]),
170
+ export const InteractiveBlockSchema = z.object({
171
+ blockType: z.literal('interactive'),
172
+ prompt: z.string().optional(),
85
173
  options: z.array(InteractiveOptionSchema).optional(),
174
+ allowCustom: z.boolean().optional(),
175
+ multiSelect: z.boolean().optional(),
86
176
  });
87
- export const TimeseriesDataPointSchema = z.tuple([z.number(), z.number()]);
88
- export const TimeseriesSeriesSchema = z.object({
89
- label: z.string(),
90
- data: z.array(TimeseriesDataPointSchema),
91
- });
92
- export const TimeseriesDataSchema = z.record(z.string(), z.array(TimeseriesSeriesSchema));
93
- export const TimeseriesBlockSchema = z.object({
94
- type: z.literal("timeseries"),
95
- title: z.string(),
96
- data: TimeseriesDataSchema,
97
- });
98
- export const PieChartBlockSchema = z.object({
99
- type: z.literal("pie_chart"),
100
- title: z.string(),
101
- data: z.array(z.tuple([z.string(), z.number()])),
102
- });
103
- export const BlockSchema = z.discriminatedUnion("type", [
104
- TableBlockSchema,
105
- MarkdownBlockSchema,
106
- TransactionActionBlockSchema,
107
- InteractiveCardBlockSchema,
108
- TimeseriesBlockSchema,
109
- PieChartBlockSchema,
110
- ]);
111
177
  // ============================================================================
112
- // Response Types
178
+ // Block Detection and Parsing
113
179
  // ============================================================================
114
- export const OutputTextSchema = z.object({
115
- type: z.literal("output_text"),
116
- text: z.string(),
117
- });
118
- export const ChaosBlockSchema = z.object({
119
- type: z.literal("chaos.block"),
120
- block: BlockSchema,
121
- });
122
- export const ContentPartSchema = z.discriminatedUnion("type", [
123
- OutputTextSchema,
124
- ChaosBlockSchema,
180
+ // Detect block type from raw server data
181
+ export function detectBlockType(raw) {
182
+ if (!raw || typeof raw !== 'object')
183
+ return 'unknown';
184
+ const obj = raw;
185
+ // Check blockType field first
186
+ if (obj.blockType === 'table')
187
+ return 'table';
188
+ if (obj.blockType === 'chart')
189
+ return 'chart';
190
+ if (obj.blockType === 'transaction_action')
191
+ return 'transaction_action';
192
+ if (obj.blockType === 'interactive')
193
+ return 'interactive';
194
+ // Markdown blocks have no blockType, just content
195
+ if ('content' in obj && typeof obj.content === 'string' && !('blockType' in obj)) {
196
+ return 'markdown';
197
+ }
198
+ return 'unknown';
199
+ }
200
+ // Parse a raw block based on detected type
201
+ export function parseRawBlock(raw) {
202
+ const blockType = detectBlockType(raw);
203
+ switch (blockType) {
204
+ case 'markdown': {
205
+ const result = MarkdownBlockSchema.safeParse(raw);
206
+ if (result.success) {
207
+ return { success: true, data: { type: 'markdown', ...result.data }, type: 'markdown' };
208
+ }
209
+ return { success: false, error: `Markdown parse error: ${result.error.message}` };
210
+ }
211
+ case 'table': {
212
+ const result = TableBlockSchema.safeParse(raw);
213
+ if (result.success) {
214
+ return { success: true, data: { type: 'table', ...result.data }, type: 'table' };
215
+ }
216
+ return { success: false, error: `Table parse error: ${result.error.message}` };
217
+ }
218
+ case 'chart': {
219
+ const result = ChartBlockSchema.safeParse(raw);
220
+ if (result.success) {
221
+ return { success: true, data: { type: 'chart', ...result.data }, type: 'chart' };
222
+ }
223
+ return { success: false, error: `Chart parse error: ${result.error.message}` };
224
+ }
225
+ case 'transaction_action': {
226
+ const result = TransactionActionBlockSchema.safeParse(raw);
227
+ if (result.success) {
228
+ return { success: true, data: { type: 'transaction_action', ...result.data }, type: 'transaction_action' };
229
+ }
230
+ return { success: false, error: `TransactionAction parse error: ${result.error.message}` };
231
+ }
232
+ case 'interactive': {
233
+ const result = InteractiveBlockSchema.safeParse(raw);
234
+ if (result.success) {
235
+ return { success: true, data: { type: 'interactive', ...result.data }, type: 'interactive' };
236
+ }
237
+ return { success: false, error: `Interactive parse error: ${result.error.message}` };
238
+ }
239
+ default:
240
+ return { success: false, error: `Unknown block type` };
241
+ }
242
+ }
243
+ // ============================================================================
244
+ // Legacy exports for backwards compatibility
245
+ // ============================================================================
246
+ // Create a discriminated union schema that adds 'type' field
247
+ // This is for SDK consumers who expect 'type' field
248
+ export const BlockSchema = z.union([
249
+ MarkdownBlockSchema.transform(data => ({ type: 'markdown', ...data })),
250
+ TableBlockSchema.transform(data => ({ type: 'table', ...data })),
251
+ ChartBlockSchema.transform(data => ({ type: 'chart', ...data })),
252
+ TransactionActionBlockSchema.transform(data => ({ type: 'transaction_action', ...data })),
253
+ InteractiveBlockSchema.transform(data => ({ type: 'interactive', ...data })),
125
254
  ]);
126
- export const OutputItemSchema = z.object({
127
- type: z.literal("message"),
128
- role: z.literal("assistant"),
129
- content: z.array(ContentPartSchema),
130
- });
131
- export const ResponseErrorSchema = z.object({
132
- message: z.string(),
133
- type: z.string(),
134
- code: z.string(),
135
- });
136
- export const ResponseSchema = z.object({
137
- id: z.string(),
138
- object: z.literal("response"),
139
- model: z.string(),
140
- status: z.enum(["completed", "failed"]),
141
- output: z.array(OutputItemSchema),
142
- error: ResponseErrorSchema.optional(),
143
- });