@chaoslabs/ai-sdk 0.0.3 → 0.0.5
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/README.md +46 -1
- package/dist/__tests__/http-streaming.test.d.ts +15 -0
- package/dist/__tests__/http-streaming.test.js +1401 -0
- package/dist/__tests__/stream.test.d.ts +1 -0
- package/dist/__tests__/stream.test.js +345 -0
- package/dist/__tests__/trivial.test.d.ts +1 -0
- package/dist/__tests__/trivial.test.js +6 -0
- package/dist/client.d.ts +21 -5
- package/dist/client.js +75 -29
- package/dist/http-streaming.d.ts +55 -0
- package/dist/http-streaming.js +359 -0
- package/dist/index.d.ts +4 -5
- package/dist/index.js +3 -26
- package/dist/schemas.d.ts +97 -405
- package/dist/schemas.js +18 -47
- package/dist/stream.d.ts +32 -0
- package/dist/stream.js +127 -0
- package/dist/types.d.ts +11 -0
- package/package.json +2 -1
package/dist/schemas.js
CHANGED
|
@@ -52,24 +52,11 @@ export const ChartDataPointSchema = z.union([
|
|
|
52
52
|
z.object({ x: z.union([z.number(), z.string()]), y: z.number() }),
|
|
53
53
|
z.array(z.unknown()),
|
|
54
54
|
]);
|
|
55
|
-
// Array format series item (legacy)
|
|
56
55
|
export const ChartSeriesSchema = z.object({
|
|
57
56
|
name: z.string(),
|
|
58
57
|
data: z.array(ChartDataPointSchema),
|
|
59
58
|
color: z.string().optional(),
|
|
60
59
|
});
|
|
61
|
-
// Timeseries format: server sends series as { "24H": [{ label, data }, ...] }
|
|
62
|
-
export const TimeseriesSeriesItemSchema = z.object({
|
|
63
|
-
label: z.string(),
|
|
64
|
-
data: z.array(z.array(z.union([z.number(), z.string()]))), // [[timestamp, value], ...]
|
|
65
|
-
});
|
|
66
|
-
// Series can be either:
|
|
67
|
-
// - Array format: [{ name, data, color }, ...]
|
|
68
|
-
// - Object format (timeseries): { "24H": [{ label, data }, ...], "7D": [...] }
|
|
69
|
-
export const ChartSeriesFieldSchema = z.union([
|
|
70
|
-
z.array(ChartSeriesSchema),
|
|
71
|
-
z.record(z.string(), z.array(TimeseriesSeriesItemSchema)),
|
|
72
|
-
]);
|
|
73
60
|
export const ChartSegmentSchema = z.object({
|
|
74
61
|
label: z.string(),
|
|
75
62
|
value: z.number(),
|
|
@@ -87,7 +74,7 @@ export const ChartBlockSchema = z.object({
|
|
|
87
74
|
blockType: z.literal('chart'),
|
|
88
75
|
title: z.string(),
|
|
89
76
|
chartType: z.enum(['pie', 'donut', 'line', 'area', 'bar', 'timeseries']).optional().nullable(),
|
|
90
|
-
series:
|
|
77
|
+
series: z.array(ChartSeriesSchema).optional().nullable(),
|
|
91
78
|
segments: z.array(ChartSegmentSchema).optional().nullable(),
|
|
92
79
|
// Legacy data format from server: [[label, value], ...]
|
|
93
80
|
data: ChartLegacyDataSchema.optional().nullable(),
|
|
@@ -97,8 +84,6 @@ export const ChartBlockSchema = z.object({
|
|
|
97
84
|
isCurrency: z.boolean().optional().nullable(),
|
|
98
85
|
sourceName: z.string().optional().nullable(),
|
|
99
86
|
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
87
|
tool_params: z.unknown().optional().nullable(),
|
|
103
88
|
tool_name: z.string().optional().nullable(),
|
|
104
89
|
});
|
|
@@ -147,31 +132,19 @@ export const TransactionGroupSchema = z.object({
|
|
|
147
132
|
requiresApproval: z.boolean().optional(),
|
|
148
133
|
verificationUnavailable: z.boolean().optional(),
|
|
149
134
|
});
|
|
150
|
-
// Risk
|
|
151
|
-
export const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
at_risk_usd: z.number().nullish(),
|
|
157
|
-
liquidation_price: z.string().nullish(),
|
|
158
|
-
});
|
|
159
|
-
// Risk check item (used in blockers, warnings, info arrays)
|
|
160
|
-
export const RiskCheckSchema = z.object({
|
|
161
|
-
id: z.string(),
|
|
162
|
-
severity: z.enum(['block', 'warn', 'info']),
|
|
163
|
-
title: z.string(),
|
|
164
|
-
message: z.string(),
|
|
165
|
-
impact: RiskImpactSchema.nullish(),
|
|
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(),
|
|
166
141
|
});
|
|
167
|
-
// Legacy alias for backwards compatibility
|
|
168
|
-
export const RiskInfoItemSchema = RiskCheckSchema;
|
|
169
142
|
// Risks object
|
|
170
143
|
export const RisksSchema = z.object({
|
|
171
|
-
level: z.
|
|
172
|
-
blockers: z.array(
|
|
173
|
-
warnings: z.array(
|
|
174
|
-
info: z.array(
|
|
144
|
+
level: z.string().optional(),
|
|
145
|
+
blockers: z.array(z.string()).optional(),
|
|
146
|
+
warnings: z.array(z.string()).optional(),
|
|
147
|
+
info: z.array(RiskInfoItemSchema).optional(),
|
|
175
148
|
});
|
|
176
149
|
export const TransactionActionBlockSchema = z.object({
|
|
177
150
|
blockType: z.literal('transaction_action'),
|
|
@@ -187,21 +160,19 @@ export const TransactionActionBlockSchema = z.object({
|
|
|
187
160
|
tool_name: z.string().optional().nullable(),
|
|
188
161
|
});
|
|
189
162
|
// --- Interactive Block ---
|
|
190
|
-
// Server sends: { blockType: "interactive",
|
|
191
|
-
// Note: Using .nullish() to accept both null and undefined (Python sends null for None)
|
|
163
|
+
// Server sends: { blockType: "interactive", ... }
|
|
192
164
|
export const InteractiveOptionSchema = z.object({
|
|
193
165
|
id: z.string(),
|
|
194
166
|
label: z.string(),
|
|
195
|
-
description: z.string().
|
|
196
|
-
|
|
167
|
+
description: z.string().optional(),
|
|
168
|
+
value: z.unknown().optional(),
|
|
197
169
|
});
|
|
198
170
|
export const InteractiveBlockSchema = z.object({
|
|
199
171
|
blockType: z.literal('interactive'),
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
options: z.array(InteractiveOptionSchema).nullish(),
|
|
172
|
+
prompt: z.string().optional(),
|
|
173
|
+
options: z.array(InteractiveOptionSchema).optional(),
|
|
174
|
+
allowCustom: z.boolean().optional(),
|
|
175
|
+
multiSelect: z.boolean().optional(),
|
|
205
176
|
});
|
|
206
177
|
// ============================================================================
|
|
207
178
|
// Block Detection and Parsing
|
package/dist/stream.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stream Message Types and Utilities
|
|
3
|
+
*
|
|
4
|
+
* Utilities for parsing and working with WebSocket stream messages
|
|
5
|
+
* from the Chaos AI backend.
|
|
6
|
+
*/
|
|
7
|
+
export type MessageType = "agent_status_change" | "agent_message" | "report" | "follow_up_suggestions" | "user_input";
|
|
8
|
+
export type AgentStatus = "processing" | "done" | "error" | "cancelled";
|
|
9
|
+
export interface StreamMessageContext {
|
|
10
|
+
sessionId: string;
|
|
11
|
+
artifactId: string;
|
|
12
|
+
query?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface StreamMessage {
|
|
15
|
+
id: string;
|
|
16
|
+
type: MessageType;
|
|
17
|
+
timestamp: number;
|
|
18
|
+
content: unknown;
|
|
19
|
+
context: StreamMessageContext;
|
|
20
|
+
}
|
|
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;
|
|
26
|
+
export declare function parseAgentStatus(msg: StreamMessage): AgentStatus | null;
|
|
27
|
+
export declare function isTerminalStatus(status: AgentStatus): boolean;
|
|
28
|
+
export declare function extractAgentMessageText(msg: StreamMessage): string | null;
|
|
29
|
+
export declare function extractSuggestions(msg: StreamMessage): string[];
|
|
30
|
+
export declare function extractReportBlock(msg: StreamMessage): unknown | null;
|
|
31
|
+
export declare function parseStreamLine(line: string): StreamMessage | null;
|
|
32
|
+
export declare function parseStreamLines(text: string): StreamMessage[];
|
package/dist/stream.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stream Message Types and Utilities
|
|
3
|
+
*
|
|
4
|
+
* Utilities for parsing and working with WebSocket stream messages
|
|
5
|
+
* from the Chaos AI backend.
|
|
6
|
+
*/
|
|
7
|
+
// ============================================================================
|
|
8
|
+
// Type Guards
|
|
9
|
+
// ============================================================================
|
|
10
|
+
export function isAgentStatusMessage(msg) {
|
|
11
|
+
return msg.type === "agent_status_change";
|
|
12
|
+
}
|
|
13
|
+
export function isAgentMessage(msg) {
|
|
14
|
+
return msg.type === "agent_message";
|
|
15
|
+
}
|
|
16
|
+
export function isReportMessage(msg) {
|
|
17
|
+
return msg.type === "report";
|
|
18
|
+
}
|
|
19
|
+
export function isFollowUpSuggestions(msg) {
|
|
20
|
+
return msg.type === "follow_up_suggestions";
|
|
21
|
+
}
|
|
22
|
+
export function isUserInputMessage(msg) {
|
|
23
|
+
return msg.type === "user_input";
|
|
24
|
+
}
|
|
25
|
+
// ============================================================================
|
|
26
|
+
// Status Utilities
|
|
27
|
+
// ============================================================================
|
|
28
|
+
const VALID_STATUSES = ["processing", "done", "error", "cancelled"];
|
|
29
|
+
export function parseAgentStatus(msg) {
|
|
30
|
+
if (msg.type !== "agent_status_change") {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
const content = msg.content;
|
|
34
|
+
if (typeof content?.status === "string" &&
|
|
35
|
+
VALID_STATUSES.includes(content.status)) {
|
|
36
|
+
return content.status;
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const TERMINAL_STATUSES = ["done", "error", "cancelled"];
|
|
41
|
+
export function isTerminalStatus(status) {
|
|
42
|
+
return TERMINAL_STATUSES.includes(status);
|
|
43
|
+
}
|
|
44
|
+
// ============================================================================
|
|
45
|
+
// Content Extraction
|
|
46
|
+
// ============================================================================
|
|
47
|
+
export function extractAgentMessageText(msg) {
|
|
48
|
+
if (msg.type !== "agent_message") {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
const content = msg.content;
|
|
52
|
+
if (typeof content?.data?.message === "string") {
|
|
53
|
+
return content.data.message;
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
export function extractSuggestions(msg) {
|
|
58
|
+
if (msg.type !== "follow_up_suggestions") {
|
|
59
|
+
return [];
|
|
60
|
+
}
|
|
61
|
+
const content = msg.content;
|
|
62
|
+
if (Array.isArray(content?.suggestions)) {
|
|
63
|
+
return content.suggestions.filter((s) => typeof s === "string");
|
|
64
|
+
}
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
export function extractReportBlock(msg) {
|
|
68
|
+
if (msg.type !== "report") {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
const content = msg.content;
|
|
72
|
+
if (content?.data !== undefined) {
|
|
73
|
+
return content.data;
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
// ============================================================================
|
|
78
|
+
// Stream Parser
|
|
79
|
+
// ============================================================================
|
|
80
|
+
const VALID_MESSAGE_TYPES = [
|
|
81
|
+
"agent_status_change",
|
|
82
|
+
"agent_message",
|
|
83
|
+
"report",
|
|
84
|
+
"follow_up_suggestions",
|
|
85
|
+
"user_input",
|
|
86
|
+
];
|
|
87
|
+
function isValidStreamMessage(obj) {
|
|
88
|
+
if (typeof obj !== "object" || obj === null) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
const msg = obj;
|
|
92
|
+
return (typeof msg.id === "string" &&
|
|
93
|
+
typeof msg.type === "string" &&
|
|
94
|
+
VALID_MESSAGE_TYPES.includes(msg.type) &&
|
|
95
|
+
typeof msg.timestamp === "number" &&
|
|
96
|
+
typeof msg.context === "object" &&
|
|
97
|
+
msg.context !== null &&
|
|
98
|
+
typeof msg.context.sessionId === "string" &&
|
|
99
|
+
typeof msg.context.artifactId === "string");
|
|
100
|
+
}
|
|
101
|
+
export function parseStreamLine(line) {
|
|
102
|
+
const trimmed = line.trim();
|
|
103
|
+
if (!trimmed) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
const parsed = JSON.parse(trimmed);
|
|
108
|
+
if (isValidStreamMessage(parsed)) {
|
|
109
|
+
return parsed;
|
|
110
|
+
}
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
export function parseStreamLines(text) {
|
|
118
|
+
const lines = text.split("\n");
|
|
119
|
+
const messages = [];
|
|
120
|
+
for (const line of lines) {
|
|
121
|
+
const msg = parseStreamLine(line);
|
|
122
|
+
if (msg !== null) {
|
|
123
|
+
messages.push(msg);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return messages;
|
|
127
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -8,7 +8,13 @@ export interface ChaosConfig {
|
|
|
8
8
|
baseUrl?: string;
|
|
9
9
|
/** Request timeout in milliseconds (defaults to 120000) */
|
|
10
10
|
timeout?: number;
|
|
11
|
+
/** Use native http/https modules for streaming instead of fetch (defaults to true) */
|
|
12
|
+
useNativeHttp?: boolean;
|
|
11
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Parameters for creating a response.
|
|
16
|
+
*/
|
|
17
|
+
import type { StreamMessage } from './stream.js';
|
|
12
18
|
/**
|
|
13
19
|
* Parameters for creating a response.
|
|
14
20
|
*/
|
|
@@ -25,6 +31,11 @@ export interface CreateResponseParams {
|
|
|
25
31
|
stream?: boolean;
|
|
26
32
|
/** Metadata for the request */
|
|
27
33
|
metadata: RequestMetadata;
|
|
34
|
+
/**
|
|
35
|
+
* Callback for real-time stream events.
|
|
36
|
+
* Called for each message as it arrives from the server.
|
|
37
|
+
*/
|
|
38
|
+
onStreamEvent?: (event: StreamMessage) => void;
|
|
28
39
|
}
|
|
29
40
|
/**
|
|
30
41
|
* An input message in the conversation.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chaoslabs/ai-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Chaos AI SDK - TypeScript SDK for the Chaos AI API",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"LICENSE"
|
|
18
18
|
],
|
|
19
19
|
"scripts": {
|
|
20
|
+
"test": "bun test",
|
|
20
21
|
"build": "tsc",
|
|
21
22
|
"typecheck": "tsc --noEmit",
|
|
22
23
|
"prepublishOnly": "bun run build",
|