@chaoslabs/ai-sdk 0.0.10 → 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/dist/stream.d.ts CHANGED
@@ -3,7 +3,12 @@
3
3
  *
4
4
  * Utilities for parsing and working with WebSocket stream messages
5
5
  * from the Chaos AI backend.
6
+ *
7
+ * StreamMessage is a discriminated union - the `type` field determines
8
+ * the shape of `content`. TypeScript will narrow the content type
9
+ * automatically when you check the type field.
6
10
  */
11
+ import type { Block } from "./types.js";
7
12
  export type MessageType = "agent_status_change" | "agent_message" | "report" | "follow_up_suggestions" | "user_input";
8
13
  export type AgentStatus = "processing" | "done" | "error" | "cancelled";
9
14
  export interface StreamMessageContext {
@@ -11,22 +16,212 @@ export interface StreamMessageContext {
11
16
  artifactId: string;
12
17
  query?: string;
13
18
  }
14
- export interface StreamMessage {
19
+ /**
20
+ * Content for agent_status_change messages.
21
+ * Indicates the current status of the agent workflow.
22
+ */
23
+ export interface AgentStatusContent {
24
+ status: AgentStatus;
25
+ }
26
+ /**
27
+ * Content for agent_message messages (thinking/progress messages).
28
+ * Contains the agent's current thinking or progress update.
29
+ */
30
+ export interface AgentMessageContent {
31
+ messageId: string;
32
+ artifactId: string;
33
+ type: string;
34
+ data: AgentTextData;
35
+ order: number;
36
+ createdAt: number;
37
+ updatedAt: number;
38
+ metadata?: Record<string, unknown>;
39
+ }
40
+ /**
41
+ * The data payload within an agent message.
42
+ */
43
+ export interface AgentTextData {
44
+ message: string;
45
+ }
46
+ /**
47
+ * A caption for a report block.
48
+ */
49
+ export interface Caption {
50
+ label: string;
51
+ value: unknown;
52
+ group?: string;
53
+ collapsable?: boolean;
54
+ }
55
+ /**
56
+ * A reference used to generate the response.
57
+ */
58
+ export interface Reference {
59
+ title: string;
60
+ url: string;
61
+ content: string;
62
+ hiddenUrl?: string;
63
+ textSpans?: number[][];
64
+ engineId?: string;
65
+ }
66
+ /**
67
+ * Content for report messages.
68
+ * Contains a block of content (markdown, table, chart, etc.)
69
+ */
70
+ export interface ReportContent {
71
+ id: string;
72
+ artifactId: string;
73
+ type: string;
74
+ data: Block;
75
+ order: number;
76
+ createdAt: number;
77
+ updatedAt: number;
78
+ createdBy?: string;
79
+ metadata?: Record<string, unknown>;
80
+ captions?: Caption[];
81
+ references?: Reference[];
82
+ version?: number;
83
+ }
84
+ /**
85
+ * Content for follow_up_suggestions messages.
86
+ * Contains suggested follow-up queries.
87
+ */
88
+ export interface FollowUpSuggestionsContent {
89
+ messageId: string;
90
+ artifactId: string;
91
+ type: string;
92
+ data: FollowUpData;
93
+ order: number;
94
+ createdAt: number;
95
+ updatedAt: number;
96
+ metadata?: Record<string, unknown>;
97
+ }
98
+ /**
99
+ * The data payload for follow-up suggestions.
100
+ */
101
+ export interface FollowUpData {
102
+ followUpQueries: string[];
103
+ }
104
+ /**
105
+ * Content for user_input messages.
106
+ * Indicates the agent needs clarification from the user.
107
+ */
108
+ export interface UserInputContent {
109
+ type: string;
110
+ candidates: Record<string, UserInputCandidate[]>;
111
+ }
112
+ /**
113
+ * A candidate option for user disambiguation.
114
+ */
115
+ export interface UserInputCandidate {
116
+ symbol: string;
117
+ name: string;
118
+ market_cap: number;
119
+ token_id: string;
120
+ score: number;
121
+ }
122
+ interface StreamMessageBase {
15
123
  id: string;
16
- type: MessageType;
17
124
  timestamp: number;
18
- content: unknown;
19
125
  context: StreamMessageContext;
20
126
  }
21
- export declare function isAgentStatusMessage(msg: StreamMessage): boolean;
22
- export declare function isAgentMessage(msg: StreamMessage): boolean;
23
- export declare function isReportMessage(msg: StreamMessage): boolean;
24
- export declare function isFollowUpSuggestions(msg: StreamMessage): boolean;
25
- export declare function isUserInputMessage(msg: StreamMessage): boolean;
127
+ /**
128
+ * Agent status change message.
129
+ * @example
130
+ * if (msg.type === 'agent_status_change') {
131
+ * console.log(msg.content.status); // TypeScript knows this is AgentStatus
132
+ * }
133
+ */
134
+ export interface AgentStatusMessage extends StreamMessageBase {
135
+ type: "agent_status_change";
136
+ content: AgentStatusContent;
137
+ }
138
+ /**
139
+ * Agent thinking/progress message.
140
+ * @example
141
+ * if (msg.type === 'agent_message') {
142
+ * console.log(msg.content.data.message); // TypeScript knows the shape
143
+ * }
144
+ */
145
+ export interface AgentTextMessage extends StreamMessageBase {
146
+ type: "agent_message";
147
+ content: AgentMessageContent;
148
+ }
149
+ /**
150
+ * Report block message (markdown, table, chart, etc.)
151
+ * @example
152
+ * if (msg.type === 'report') {
153
+ * const blockData = msg.content.data; // The actual block
154
+ * }
155
+ */
156
+ export interface ReportMessage extends StreamMessageBase {
157
+ type: "report";
158
+ content: ReportContent;
159
+ }
160
+ /**
161
+ * Follow-up suggestions message.
162
+ * @example
163
+ * if (msg.type === 'follow_up_suggestions') {
164
+ * const queries = msg.content.data.followUpQueries;
165
+ * }
166
+ */
167
+ export interface FollowUpSuggestionsMessage extends StreamMessageBase {
168
+ type: "follow_up_suggestions";
169
+ content: FollowUpSuggestionsContent;
170
+ }
171
+ /**
172
+ * User input request message.
173
+ * @example
174
+ * if (msg.type === 'user_input') {
175
+ * const candidates = msg.content.candidates;
176
+ * }
177
+ */
178
+ export interface UserInputMessage extends StreamMessageBase {
179
+ type: "user_input";
180
+ content: UserInputContent;
181
+ }
182
+ /**
183
+ * Union of all stream message types.
184
+ *
185
+ * This is a discriminated union - check the `type` field to narrow
186
+ * the content type automatically:
187
+ *
188
+ * @example
189
+ * function handleMessage(msg: StreamMessage) {
190
+ * switch (msg.type) {
191
+ * case 'agent_status_change':
192
+ * // TypeScript knows: msg.content is AgentStatusContent
193
+ * console.log(msg.content.status);
194
+ * break;
195
+ * case 'agent_message':
196
+ * // TypeScript knows: msg.content is AgentMessageContent
197
+ * console.log(msg.content.data.message);
198
+ * break;
199
+ * case 'report':
200
+ * // TypeScript knows: msg.content is ReportContent
201
+ * console.log(msg.content.data);
202
+ * break;
203
+ * case 'follow_up_suggestions':
204
+ * // TypeScript knows: msg.content is FollowUpSuggestionsContent
205
+ * console.log(msg.content.data.followUpQueries);
206
+ * break;
207
+ * case 'user_input':
208
+ * // TypeScript knows: msg.content is UserInputContent
209
+ * console.log(msg.content.candidates);
210
+ * break;
211
+ * }
212
+ * }
213
+ */
214
+ export type StreamMessage = AgentStatusMessage | AgentTextMessage | ReportMessage | FollowUpSuggestionsMessage | UserInputMessage;
215
+ export declare function isAgentStatusMessage(msg: StreamMessage): msg is AgentStatusMessage;
216
+ export declare function isAgentMessage(msg: StreamMessage): msg is AgentTextMessage;
217
+ export declare function isReportMessage(msg: StreamMessage): msg is ReportMessage;
218
+ export declare function isFollowUpSuggestions(msg: StreamMessage): msg is FollowUpSuggestionsMessage;
219
+ export declare function isUserInputMessage(msg: StreamMessage): msg is UserInputMessage;
26
220
  export declare function parseAgentStatus(msg: StreamMessage): AgentStatus | null;
27
221
  export declare function isTerminalStatus(status: AgentStatus): boolean;
28
222
  export declare function extractAgentMessageText(msg: StreamMessage): string | null;
29
223
  export declare function extractSuggestions(msg: StreamMessage): string[];
30
- export declare function extractReportBlock(msg: StreamMessage): unknown | null;
224
+ export declare function extractReportBlock(msg: StreamMessage): Block | null;
31
225
  export declare function parseStreamLine(line: string): StreamMessage | null;
32
226
  export declare function parseStreamLines(text: string): StreamMessage[];
227
+ export {};
package/dist/stream.js CHANGED
@@ -3,6 +3,10 @@
3
3
  *
4
4
  * Utilities for parsing and working with WebSocket stream messages
5
5
  * from the Chaos AI backend.
6
+ *
7
+ * StreamMessage is a discriminated union - the `type` field determines
8
+ * the shape of `content`. TypeScript will narrow the content type
9
+ * automatically when you check the type field.
6
10
  */
7
11
  // ============================================================================
8
12
  // Type Guards
@@ -30,49 +34,36 @@ export function parseAgentStatus(msg) {
30
34
  if (msg.type !== "agent_status_change") {
31
35
  return null;
32
36
  }
33
- const content = msg.content;
34
- if (typeof content?.status === "string" &&
35
- VALID_STATUSES.includes(content.status)) {
36
- return content.status;
37
+ const status = msg.content?.status;
38
+ if (!status || !VALID_STATUSES.includes(status)) {
39
+ return null;
37
40
  }
38
- return null;
41
+ return status;
39
42
  }
40
43
  const TERMINAL_STATUSES = ["done", "error", "cancelled"];
41
44
  export function isTerminalStatus(status) {
42
45
  return TERMINAL_STATUSES.includes(status);
43
46
  }
44
47
  // ============================================================================
45
- // Content Extraction
48
+ // Content Extraction (for convenience, type guards provide same functionality)
46
49
  // ============================================================================
47
50
  export function extractAgentMessageText(msg) {
48
51
  if (msg.type !== "agent_message") {
49
52
  return null;
50
53
  }
51
- const content = msg.content;
52
- if (typeof content?.data?.message === "string") {
53
- return content.data.message;
54
- }
55
- return null;
54
+ return msg.content?.data?.message ?? null;
56
55
  }
57
56
  export function extractSuggestions(msg) {
58
57
  if (msg.type !== "follow_up_suggestions") {
59
58
  return [];
60
59
  }
61
- const content = msg.content;
62
- if (Array.isArray(content?.suggestions)) {
63
- return content.suggestions.filter((s) => typeof s === "string");
64
- }
65
- return [];
60
+ return msg.content?.data?.followUpQueries ?? [];
66
61
  }
67
62
  export function extractReportBlock(msg) {
68
63
  if (msg.type !== "report") {
69
64
  return null;
70
65
  }
71
- const content = msg.content;
72
- if (content?.data !== undefined) {
73
- return content.data;
74
- }
75
- return null;
66
+ return msg.content?.data ?? null;
76
67
  }
77
68
  // ============================================================================
78
69
  // Stream Parser
@@ -0,0 +1,316 @@
1
+ import type { Block, ChartBlock, InfoBlock, CodeBlock } from './types.js';
2
+ import type { Primitive, SwapPrimitive, BridgePrimitive, SupplyPrimitive, WithdrawPrimitive, BorrowPrimitive, RepayPrimitive, StakePrimitive, UnstakePrimitive, ClaimPrimitive, AddLiquidityPrimitive, RemoveLiquidityPrimitive, OpenPositionPrimitive, ClosePositionPrimitive, DepositPrimitive, MintPrimitive, BurnPrimitive, RestakePrimitive, ApprovePrimitive, TransferPrimitive, WrapPrimitive, UnwrapPrimitive } from './primitives.js';
3
+ /**
4
+ * Type guard to check if a primitive is a swap operation.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * if (isSwapPrimitive(primitive)) {
9
+ * // TypeScript knows primitive.params is SwapParams
10
+ * console.log(primitive.params.token_in);
11
+ * console.log(primitive.params.token_out);
12
+ * }
13
+ * ```
14
+ */
15
+ export declare function isSwapPrimitive(p: Primitive): p is SwapPrimitive;
16
+ /**
17
+ * Type guard to check if a primitive is a bridge operation.
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * if (isBridgePrimitive(primitive)) {
22
+ * // TypeScript knows primitive.params is BridgeParams
23
+ * console.log(primitive.params.from_chain);
24
+ * console.log(primitive.params.to_chain);
25
+ * }
26
+ * ```
27
+ */
28
+ export declare function isBridgePrimitive(p: Primitive): p is BridgePrimitive;
29
+ /**
30
+ * Type guard to check if a primitive is a supply operation.
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * if (isSupplyPrimitive(primitive)) {
35
+ * // TypeScript knows primitive.params is LendingParams
36
+ * console.log(primitive.params.asset);
37
+ * console.log(primitive.params.amount);
38
+ * }
39
+ * ```
40
+ */
41
+ export declare function isSupplyPrimitive(p: Primitive): p is SupplyPrimitive;
42
+ /**
43
+ * Type guard to check if a primitive is a withdraw operation.
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * if (isWithdrawPrimitive(primitive)) {
48
+ * // TypeScript knows primitive.params is LendingParams
49
+ * console.log(primitive.params.asset);
50
+ * }
51
+ * ```
52
+ */
53
+ export declare function isWithdrawPrimitive(p: Primitive): p is WithdrawPrimitive;
54
+ /**
55
+ * Type guard to check if a primitive is a borrow operation.
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * if (isBorrowPrimitive(primitive)) {
60
+ * // TypeScript knows primitive.params is LendingParams
61
+ * console.log(primitive.params.asset);
62
+ * }
63
+ * ```
64
+ */
65
+ export declare function isBorrowPrimitive(p: Primitive): p is BorrowPrimitive;
66
+ /**
67
+ * Type guard to check if a primitive is a repay operation.
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * if (isRepayPrimitive(primitive)) {
72
+ * // TypeScript knows primitive.params is LendingParams
73
+ * console.log(primitive.params.asset);
74
+ * }
75
+ * ```
76
+ */
77
+ export declare function isRepayPrimitive(p: Primitive): p is RepayPrimitive;
78
+ /**
79
+ * Type guard to check if a primitive is a stake operation.
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * if (isStakePrimitive(primitive)) {
84
+ * // TypeScript knows primitive.params is StakingParams
85
+ * console.log(primitive.params.amount);
86
+ * console.log(primitive.params.protocol);
87
+ * }
88
+ * ```
89
+ */
90
+ export declare function isStakePrimitive(p: Primitive): p is StakePrimitive;
91
+ /**
92
+ * Type guard to check if a primitive is an unstake operation.
93
+ *
94
+ * @example
95
+ * ```typescript
96
+ * if (isUnstakePrimitive(primitive)) {
97
+ * // TypeScript knows primitive.params is StakingParams
98
+ * console.log(primitive.params.amount);
99
+ * }
100
+ * ```
101
+ */
102
+ export declare function isUnstakePrimitive(p: Primitive): p is UnstakePrimitive;
103
+ /**
104
+ * Type guard to check if a primitive is a claim operation.
105
+ *
106
+ * @example
107
+ * ```typescript
108
+ * if (isClaimPrimitive(primitive)) {
109
+ * // TypeScript knows primitive.params is ClaimParams
110
+ * console.log(primitive.params.protocol);
111
+ * }
112
+ * ```
113
+ */
114
+ export declare function isClaimPrimitive(p: Primitive): p is ClaimPrimitive;
115
+ /**
116
+ * Type guard to check if a primitive is an add liquidity operation.
117
+ *
118
+ * @example
119
+ * ```typescript
120
+ * if (isAddLiquidityPrimitive(primitive)) {
121
+ * // TypeScript knows primitive.params is LiquidityParams
122
+ * console.log(primitive.params.token_a);
123
+ * console.log(primitive.params.token_b);
124
+ * }
125
+ * ```
126
+ */
127
+ export declare function isAddLiquidityPrimitive(p: Primitive): p is AddLiquidityPrimitive;
128
+ /**
129
+ * Type guard to check if a primitive is a remove liquidity operation.
130
+ *
131
+ * @example
132
+ * ```typescript
133
+ * if (isRemoveLiquidityPrimitive(primitive)) {
134
+ * // TypeScript knows primitive.params is LiquidityParams
135
+ * console.log(primitive.params.token_a);
136
+ * }
137
+ * ```
138
+ */
139
+ export declare function isRemoveLiquidityPrimitive(p: Primitive): p is RemoveLiquidityPrimitive;
140
+ /**
141
+ * Type guard to check if a primitive is an open position operation.
142
+ *
143
+ * @example
144
+ * ```typescript
145
+ * if (isOpenPositionPrimitive(primitive)) {
146
+ * // TypeScript knows primitive.params is PositionParams
147
+ * console.log(primitive.params.market);
148
+ * console.log(primitive.params.side);
149
+ * }
150
+ * ```
151
+ */
152
+ export declare function isOpenPositionPrimitive(p: Primitive): p is OpenPositionPrimitive;
153
+ /**
154
+ * Type guard to check if a primitive is a close position operation.
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * if (isClosePositionPrimitive(primitive)) {
159
+ * // TypeScript knows primitive.params is PositionParams
160
+ * console.log(primitive.params.market);
161
+ * }
162
+ * ```
163
+ */
164
+ export declare function isClosePositionPrimitive(p: Primitive): p is ClosePositionPrimitive;
165
+ /**
166
+ * Type guard to check if a primitive is a deposit operation.
167
+ *
168
+ * @example
169
+ * ```typescript
170
+ * if (isDepositPrimitive(primitive)) {
171
+ * // TypeScript knows primitive.params is DepositParams
172
+ * console.log(primitive.params.vault);
173
+ * }
174
+ * ```
175
+ */
176
+ export declare function isDepositPrimitive(p: Primitive): p is DepositPrimitive;
177
+ /**
178
+ * Type guard to check if a primitive is a mint operation.
179
+ *
180
+ * @example
181
+ * ```typescript
182
+ * if (isMintPrimitive(primitive)) {
183
+ * // TypeScript knows primitive.params is MintParams
184
+ * console.log(primitive.params.asset);
185
+ * console.log(primitive.params.collateral);
186
+ * }
187
+ * ```
188
+ */
189
+ export declare function isMintPrimitive(p: Primitive): p is MintPrimitive;
190
+ /**
191
+ * Type guard to check if a primitive is a burn operation.
192
+ *
193
+ * @example
194
+ * ```typescript
195
+ * if (isBurnPrimitive(primitive)) {
196
+ * // TypeScript knows primitive.params is MintParams
197
+ * console.log(primitive.params.asset);
198
+ * }
199
+ * ```
200
+ */
201
+ export declare function isBurnPrimitive(p: Primitive): p is BurnPrimitive;
202
+ /**
203
+ * Type guard to check if a primitive is a restake operation.
204
+ *
205
+ * @example
206
+ * ```typescript
207
+ * if (isRestakePrimitive(primitive)) {
208
+ * // TypeScript knows primitive.params is StakingParams
209
+ * console.log(primitive.params.amount);
210
+ * console.log(primitive.params.operator);
211
+ * }
212
+ * ```
213
+ */
214
+ export declare function isRestakePrimitive(p: Primitive): p is RestakePrimitive;
215
+ /**
216
+ * Type guard to check if a primitive is an approve operation.
217
+ *
218
+ * @example
219
+ * ```typescript
220
+ * if (isApprovePrimitive(primitive)) {
221
+ * // TypeScript knows primitive.params is ApproveParams
222
+ * console.log(primitive.params.token);
223
+ * console.log(primitive.params.spender);
224
+ * }
225
+ * ```
226
+ */
227
+ export declare function isApprovePrimitive(p: Primitive): p is ApprovePrimitive;
228
+ /**
229
+ * Type guard to check if a primitive is a transfer operation.
230
+ *
231
+ * @example
232
+ * ```typescript
233
+ * if (isTransferPrimitive(primitive)) {
234
+ * // TypeScript knows primitive.params is TransferParams
235
+ * console.log(primitive.params.token);
236
+ * console.log(primitive.params.to);
237
+ * }
238
+ * ```
239
+ */
240
+ export declare function isTransferPrimitive(p: Primitive): p is TransferPrimitive;
241
+ /**
242
+ * Type guard to check if a primitive is a wrap operation.
243
+ *
244
+ * @example
245
+ * ```typescript
246
+ * if (isWrapPrimitive(primitive)) {
247
+ * // TypeScript knows primitive.params is WrapParams
248
+ * console.log(primitive.params.amount);
249
+ * }
250
+ * ```
251
+ */
252
+ export declare function isWrapPrimitive(p: Primitive): p is WrapPrimitive;
253
+ /**
254
+ * Type guard to check if a primitive is an unwrap operation.
255
+ *
256
+ * @example
257
+ * ```typescript
258
+ * if (isUnwrapPrimitive(primitive)) {
259
+ * // TypeScript knows primitive.params is WrapParams
260
+ * console.log(primitive.params.amount);
261
+ * }
262
+ * ```
263
+ */
264
+ export declare function isUnwrapPrimitive(p: Primitive): p is UnwrapPrimitive;
265
+ import type { PieChartBlock, TimeseriesChartBlock } from './types.js';
266
+ export type { PieChartBlock, TimeseriesChartBlock };
267
+ /**
268
+ * Type guard to check if a chart block is a pie chart.
269
+ *
270
+ * @example
271
+ * ```typescript
272
+ * if (isPieChartBlock(block)) {
273
+ * // TypeScript knows block.chartType is 'pie'
274
+ * const data = block.data; // [[label, value], ...]
275
+ * }
276
+ * ```
277
+ */
278
+ export declare function isPieChartBlock(block: ChartBlock): block is PieChartBlock;
279
+ /**
280
+ * Type guard to check if a chart block is a timeseries chart.
281
+ *
282
+ * @example
283
+ * ```typescript
284
+ * if (isTimeseriesChartBlock(block)) {
285
+ * // TypeScript knows block.chartType is 'timeseries'
286
+ * const series = block.series;
287
+ * }
288
+ * ```
289
+ */
290
+ export declare function isTimeseriesChartBlock(block: ChartBlock): block is TimeseriesChartBlock;
291
+ /**
292
+ * Type guard to check if a block is an info block.
293
+ *
294
+ * @example
295
+ * ```typescript
296
+ * if (isInfoBlock(block)) {
297
+ * // TypeScript knows block is InfoBlock
298
+ * console.log(block.message);
299
+ * console.log(block.severity);
300
+ * }
301
+ * ```
302
+ */
303
+ export declare function isInfoBlock(block: Block): block is InfoBlock;
304
+ /**
305
+ * Type guard to check if a block is a code block.
306
+ *
307
+ * @example
308
+ * ```typescript
309
+ * if (isCodeBlock(block)) {
310
+ * // TypeScript knows block is CodeBlock
311
+ * console.log(block.code);
312
+ * console.log(block.language);
313
+ * }
314
+ * ```
315
+ */
316
+ export declare function isCodeBlock(block: Block): block is CodeBlock;