@chaoslabs/ai-sdk 0.0.2 → 0.0.4
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/blocks.d.ts +108 -0
- package/dist/blocks.js +246 -0
- package/dist/client.d.ts +21 -5
- package/dist/client.js +75 -29
- package/dist/conversation.d.ts +136 -0
- package/dist/conversation.js +230 -0
- package/dist/http-streaming.d.ts +55 -0
- package/dist/http-streaming.js +359 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/primitives.d.ts +157 -0
- package/dist/primitives.js +220 -0
- package/dist/schemas.d.ts +6 -6
- 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
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Swap tokens on a decentralized exchange.
|
|
3
|
+
* @example "Swap 1 ETH for USDC"
|
|
4
|
+
*/
|
|
5
|
+
export declare const PRIMITIVE_SWAP = "swap";
|
|
6
|
+
/**
|
|
7
|
+
* Supply assets to a lending protocol.
|
|
8
|
+
* @example "Supply 1000 USDC to Aave"
|
|
9
|
+
*/
|
|
10
|
+
export declare const PRIMITIVE_SUPPLY = "supply";
|
|
11
|
+
/**
|
|
12
|
+
* Withdraw supplied assets from a lending protocol.
|
|
13
|
+
* @example "Withdraw my USDC from Compound"
|
|
14
|
+
*/
|
|
15
|
+
export declare const PRIMITIVE_WITHDRAW = "withdraw";
|
|
16
|
+
/**
|
|
17
|
+
* Borrow assets from a lending protocol.
|
|
18
|
+
* @example "Borrow 500 DAI from Aave"
|
|
19
|
+
*/
|
|
20
|
+
export declare const PRIMITIVE_BORROW = "borrow";
|
|
21
|
+
/**
|
|
22
|
+
* Repay borrowed assets to a lending protocol.
|
|
23
|
+
* @example "Repay my DAI debt on Compound"
|
|
24
|
+
*/
|
|
25
|
+
export declare const PRIMITIVE_REPAY = "repay";
|
|
26
|
+
/**
|
|
27
|
+
* Stake tokens for rewards or governance.
|
|
28
|
+
* @example "Stake 10 ETH with Lido"
|
|
29
|
+
*/
|
|
30
|
+
export declare const PRIMITIVE_STAKE = "stake";
|
|
31
|
+
/**
|
|
32
|
+
* Unstake previously staked tokens.
|
|
33
|
+
* @example "Unstake my stETH"
|
|
34
|
+
*/
|
|
35
|
+
export declare const PRIMITIVE_UNSTAKE = "unstake";
|
|
36
|
+
/**
|
|
37
|
+
* Claim accrued rewards from protocols.
|
|
38
|
+
* @example "Claim my Aave rewards"
|
|
39
|
+
*/
|
|
40
|
+
export declare const PRIMITIVE_CLAIM = "claim";
|
|
41
|
+
/**
|
|
42
|
+
* Bridge assets between chains.
|
|
43
|
+
* @example "Bridge 100 USDC to Arbitrum"
|
|
44
|
+
*/
|
|
45
|
+
export declare const PRIMITIVE_BRIDGE = "bridge";
|
|
46
|
+
/**
|
|
47
|
+
* Provide liquidity to a pool.
|
|
48
|
+
* @example "Add liquidity to ETH-USDC pool"
|
|
49
|
+
*/
|
|
50
|
+
export declare const PRIMITIVE_ADD_LIQUIDITY = "add_liquidity";
|
|
51
|
+
/**
|
|
52
|
+
* Remove liquidity from a pool.
|
|
53
|
+
* @example "Remove liquidity from my ETH-USDC position"
|
|
54
|
+
*/
|
|
55
|
+
export declare const PRIMITIVE_REMOVE_LIQUIDITY = "remove_liquidity";
|
|
56
|
+
/**
|
|
57
|
+
* Open a perpetual futures position.
|
|
58
|
+
* @example "Open 2x long ETH position"
|
|
59
|
+
*/
|
|
60
|
+
export declare const PRIMITIVE_OPEN_POSITION = "open_position";
|
|
61
|
+
/**
|
|
62
|
+
* Close a perpetual futures position.
|
|
63
|
+
* @example "Close my ETH long"
|
|
64
|
+
*/
|
|
65
|
+
export declare const PRIMITIVE_CLOSE_POSITION = "close_position";
|
|
66
|
+
/**
|
|
67
|
+
* Deposit into a vault strategy.
|
|
68
|
+
* @example "Deposit 1000 USDC into Yearn"
|
|
69
|
+
*/
|
|
70
|
+
export declare const PRIMITIVE_DEPOSIT = "deposit";
|
|
71
|
+
/**
|
|
72
|
+
* Mint stablecoins or synthetic assets.
|
|
73
|
+
* @example "Mint 1000 DAI from my ETH collateral"
|
|
74
|
+
*/
|
|
75
|
+
export declare const PRIMITIVE_MINT = "mint";
|
|
76
|
+
/**
|
|
77
|
+
* Burn or repay minted assets.
|
|
78
|
+
* @example "Burn 500 DAI to reduce my debt"
|
|
79
|
+
*/
|
|
80
|
+
export declare const PRIMITIVE_BURN = "burn";
|
|
81
|
+
/**
|
|
82
|
+
* Restake assets for additional yield.
|
|
83
|
+
* @example "Restake my stETH on EigenLayer"
|
|
84
|
+
*/
|
|
85
|
+
export declare const PRIMITIVE_RESTAKE = "restake";
|
|
86
|
+
/**
|
|
87
|
+
* Approve token spending.
|
|
88
|
+
* @example "Approve USDC for Uniswap"
|
|
89
|
+
*/
|
|
90
|
+
export declare const PRIMITIVE_APPROVE = "approve";
|
|
91
|
+
/**
|
|
92
|
+
* Transfer tokens to another address.
|
|
93
|
+
* @example "Send 100 USDC to 0x..."
|
|
94
|
+
*/
|
|
95
|
+
export declare const PRIMITIVE_TRANSFER = "transfer";
|
|
96
|
+
/**
|
|
97
|
+
* Wrap native tokens (e.g., ETH to WETH).
|
|
98
|
+
* @example "Wrap 1 ETH to WETH"
|
|
99
|
+
*/
|
|
100
|
+
export declare const PRIMITIVE_WRAP = "wrap";
|
|
101
|
+
/**
|
|
102
|
+
* Unwrap wrapped tokens (e.g., WETH to ETH).
|
|
103
|
+
* @example "Unwrap 1 WETH to ETH"
|
|
104
|
+
*/
|
|
105
|
+
export declare const PRIMITIVE_UNWRAP = "unwrap";
|
|
106
|
+
/**
|
|
107
|
+
* All DeFi primitive types as a union type.
|
|
108
|
+
*/
|
|
109
|
+
export type PrimitiveType = typeof PRIMITIVE_SWAP | typeof PRIMITIVE_SUPPLY | typeof PRIMITIVE_WITHDRAW | typeof PRIMITIVE_BORROW | typeof PRIMITIVE_REPAY | typeof PRIMITIVE_STAKE | typeof PRIMITIVE_UNSTAKE | typeof PRIMITIVE_CLAIM | typeof PRIMITIVE_BRIDGE | typeof PRIMITIVE_ADD_LIQUIDITY | typeof PRIMITIVE_REMOVE_LIQUIDITY | typeof PRIMITIVE_OPEN_POSITION | typeof PRIMITIVE_CLOSE_POSITION | typeof PRIMITIVE_DEPOSIT | typeof PRIMITIVE_MINT | typeof PRIMITIVE_BURN | typeof PRIMITIVE_RESTAKE | typeof PRIMITIVE_APPROVE | typeof PRIMITIVE_TRANSFER | typeof PRIMITIVE_WRAP | typeof PRIMITIVE_UNWRAP;
|
|
110
|
+
/**
|
|
111
|
+
* All primitive type constants for iteration.
|
|
112
|
+
*/
|
|
113
|
+
export declare const ALL_PRIMITIVES: PrimitiveType[];
|
|
114
|
+
/**
|
|
115
|
+
* Lending-related primitives.
|
|
116
|
+
*/
|
|
117
|
+
export declare const LENDING_PRIMITIVES: PrimitiveType[];
|
|
118
|
+
/**
|
|
119
|
+
* Trading-related primitives.
|
|
120
|
+
*/
|
|
121
|
+
export declare const TRADING_PRIMITIVES: PrimitiveType[];
|
|
122
|
+
/**
|
|
123
|
+
* Staking-related primitives.
|
|
124
|
+
*/
|
|
125
|
+
export declare const STAKING_PRIMITIVES: PrimitiveType[];
|
|
126
|
+
/**
|
|
127
|
+
* Liquidity-related primitives.
|
|
128
|
+
*/
|
|
129
|
+
export declare const LIQUIDITY_PRIMITIVES: PrimitiveType[];
|
|
130
|
+
/**
|
|
131
|
+
* Transfer-related primitives.
|
|
132
|
+
*/
|
|
133
|
+
export declare const TRANSFER_PRIMITIVES: PrimitiveType[];
|
|
134
|
+
/**
|
|
135
|
+
* Check if a primitive type is valid.
|
|
136
|
+
*/
|
|
137
|
+
export declare function isValidPrimitive(primitive: string): primitive is PrimitiveType;
|
|
138
|
+
/**
|
|
139
|
+
* Check if a primitive is a lending operation.
|
|
140
|
+
*/
|
|
141
|
+
export declare function isLendingPrimitive(primitive: string): boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Check if a primitive is a trading operation.
|
|
144
|
+
*/
|
|
145
|
+
export declare function isTradingPrimitive(primitive: string): boolean;
|
|
146
|
+
/**
|
|
147
|
+
* Check if a primitive is a staking operation.
|
|
148
|
+
*/
|
|
149
|
+
export declare function isStakingPrimitive(primitive: string): boolean;
|
|
150
|
+
/**
|
|
151
|
+
* Check if a primitive is a liquidity operation.
|
|
152
|
+
*/
|
|
153
|
+
export declare function isLiquidityPrimitive(primitive: string): boolean;
|
|
154
|
+
/**
|
|
155
|
+
* Check if a primitive is a transfer operation.
|
|
156
|
+
*/
|
|
157
|
+
export declare function isTransferPrimitive(primitive: string): boolean;
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// Chaos AI SDK - Primitive Constants
|
|
2
|
+
//
|
|
3
|
+
// Constants for all DeFi primitive types supported by the SDK.
|
|
4
|
+
// Use these when checking primitive types in TransactionActionBlocks.
|
|
5
|
+
// ============================================================================
|
|
6
|
+
// Primitive Type Constants
|
|
7
|
+
// ============================================================================
|
|
8
|
+
/**
|
|
9
|
+
* Swap tokens on a decentralized exchange.
|
|
10
|
+
* @example "Swap 1 ETH for USDC"
|
|
11
|
+
*/
|
|
12
|
+
export const PRIMITIVE_SWAP = 'swap';
|
|
13
|
+
/**
|
|
14
|
+
* Supply assets to a lending protocol.
|
|
15
|
+
* @example "Supply 1000 USDC to Aave"
|
|
16
|
+
*/
|
|
17
|
+
export const PRIMITIVE_SUPPLY = 'supply';
|
|
18
|
+
/**
|
|
19
|
+
* Withdraw supplied assets from a lending protocol.
|
|
20
|
+
* @example "Withdraw my USDC from Compound"
|
|
21
|
+
*/
|
|
22
|
+
export const PRIMITIVE_WITHDRAW = 'withdraw';
|
|
23
|
+
/**
|
|
24
|
+
* Borrow assets from a lending protocol.
|
|
25
|
+
* @example "Borrow 500 DAI from Aave"
|
|
26
|
+
*/
|
|
27
|
+
export const PRIMITIVE_BORROW = 'borrow';
|
|
28
|
+
/**
|
|
29
|
+
* Repay borrowed assets to a lending protocol.
|
|
30
|
+
* @example "Repay my DAI debt on Compound"
|
|
31
|
+
*/
|
|
32
|
+
export const PRIMITIVE_REPAY = 'repay';
|
|
33
|
+
/**
|
|
34
|
+
* Stake tokens for rewards or governance.
|
|
35
|
+
* @example "Stake 10 ETH with Lido"
|
|
36
|
+
*/
|
|
37
|
+
export const PRIMITIVE_STAKE = 'stake';
|
|
38
|
+
/**
|
|
39
|
+
* Unstake previously staked tokens.
|
|
40
|
+
* @example "Unstake my stETH"
|
|
41
|
+
*/
|
|
42
|
+
export const PRIMITIVE_UNSTAKE = 'unstake';
|
|
43
|
+
/**
|
|
44
|
+
* Claim accrued rewards from protocols.
|
|
45
|
+
* @example "Claim my Aave rewards"
|
|
46
|
+
*/
|
|
47
|
+
export const PRIMITIVE_CLAIM = 'claim';
|
|
48
|
+
/**
|
|
49
|
+
* Bridge assets between chains.
|
|
50
|
+
* @example "Bridge 100 USDC to Arbitrum"
|
|
51
|
+
*/
|
|
52
|
+
export const PRIMITIVE_BRIDGE = 'bridge';
|
|
53
|
+
/**
|
|
54
|
+
* Provide liquidity to a pool.
|
|
55
|
+
* @example "Add liquidity to ETH-USDC pool"
|
|
56
|
+
*/
|
|
57
|
+
export const PRIMITIVE_ADD_LIQUIDITY = 'add_liquidity';
|
|
58
|
+
/**
|
|
59
|
+
* Remove liquidity from a pool.
|
|
60
|
+
* @example "Remove liquidity from my ETH-USDC position"
|
|
61
|
+
*/
|
|
62
|
+
export const PRIMITIVE_REMOVE_LIQUIDITY = 'remove_liquidity';
|
|
63
|
+
/**
|
|
64
|
+
* Open a perpetual futures position.
|
|
65
|
+
* @example "Open 2x long ETH position"
|
|
66
|
+
*/
|
|
67
|
+
export const PRIMITIVE_OPEN_POSITION = 'open_position';
|
|
68
|
+
/**
|
|
69
|
+
* Close a perpetual futures position.
|
|
70
|
+
* @example "Close my ETH long"
|
|
71
|
+
*/
|
|
72
|
+
export const PRIMITIVE_CLOSE_POSITION = 'close_position';
|
|
73
|
+
/**
|
|
74
|
+
* Deposit into a vault strategy.
|
|
75
|
+
* @example "Deposit 1000 USDC into Yearn"
|
|
76
|
+
*/
|
|
77
|
+
export const PRIMITIVE_DEPOSIT = 'deposit';
|
|
78
|
+
/**
|
|
79
|
+
* Mint stablecoins or synthetic assets.
|
|
80
|
+
* @example "Mint 1000 DAI from my ETH collateral"
|
|
81
|
+
*/
|
|
82
|
+
export const PRIMITIVE_MINT = 'mint';
|
|
83
|
+
/**
|
|
84
|
+
* Burn or repay minted assets.
|
|
85
|
+
* @example "Burn 500 DAI to reduce my debt"
|
|
86
|
+
*/
|
|
87
|
+
export const PRIMITIVE_BURN = 'burn';
|
|
88
|
+
/**
|
|
89
|
+
* Restake assets for additional yield.
|
|
90
|
+
* @example "Restake my stETH on EigenLayer"
|
|
91
|
+
*/
|
|
92
|
+
export const PRIMITIVE_RESTAKE = 'restake';
|
|
93
|
+
/**
|
|
94
|
+
* Approve token spending.
|
|
95
|
+
* @example "Approve USDC for Uniswap"
|
|
96
|
+
*/
|
|
97
|
+
export const PRIMITIVE_APPROVE = 'approve';
|
|
98
|
+
/**
|
|
99
|
+
* Transfer tokens to another address.
|
|
100
|
+
* @example "Send 100 USDC to 0x..."
|
|
101
|
+
*/
|
|
102
|
+
export const PRIMITIVE_TRANSFER = 'transfer';
|
|
103
|
+
/**
|
|
104
|
+
* Wrap native tokens (e.g., ETH to WETH).
|
|
105
|
+
* @example "Wrap 1 ETH to WETH"
|
|
106
|
+
*/
|
|
107
|
+
export const PRIMITIVE_WRAP = 'wrap';
|
|
108
|
+
/**
|
|
109
|
+
* Unwrap wrapped tokens (e.g., WETH to ETH).
|
|
110
|
+
* @example "Unwrap 1 WETH to ETH"
|
|
111
|
+
*/
|
|
112
|
+
export const PRIMITIVE_UNWRAP = 'unwrap';
|
|
113
|
+
/**
|
|
114
|
+
* All primitive type constants for iteration.
|
|
115
|
+
*/
|
|
116
|
+
export const ALL_PRIMITIVES = [
|
|
117
|
+
PRIMITIVE_SWAP,
|
|
118
|
+
PRIMITIVE_SUPPLY,
|
|
119
|
+
PRIMITIVE_WITHDRAW,
|
|
120
|
+
PRIMITIVE_BORROW,
|
|
121
|
+
PRIMITIVE_REPAY,
|
|
122
|
+
PRIMITIVE_STAKE,
|
|
123
|
+
PRIMITIVE_UNSTAKE,
|
|
124
|
+
PRIMITIVE_CLAIM,
|
|
125
|
+
PRIMITIVE_BRIDGE,
|
|
126
|
+
PRIMITIVE_ADD_LIQUIDITY,
|
|
127
|
+
PRIMITIVE_REMOVE_LIQUIDITY,
|
|
128
|
+
PRIMITIVE_OPEN_POSITION,
|
|
129
|
+
PRIMITIVE_CLOSE_POSITION,
|
|
130
|
+
PRIMITIVE_DEPOSIT,
|
|
131
|
+
PRIMITIVE_MINT,
|
|
132
|
+
PRIMITIVE_BURN,
|
|
133
|
+
PRIMITIVE_RESTAKE,
|
|
134
|
+
PRIMITIVE_APPROVE,
|
|
135
|
+
PRIMITIVE_TRANSFER,
|
|
136
|
+
PRIMITIVE_WRAP,
|
|
137
|
+
PRIMITIVE_UNWRAP,
|
|
138
|
+
];
|
|
139
|
+
/**
|
|
140
|
+
* Lending-related primitives.
|
|
141
|
+
*/
|
|
142
|
+
export const LENDING_PRIMITIVES = [
|
|
143
|
+
PRIMITIVE_SUPPLY,
|
|
144
|
+
PRIMITIVE_WITHDRAW,
|
|
145
|
+
PRIMITIVE_BORROW,
|
|
146
|
+
PRIMITIVE_REPAY,
|
|
147
|
+
];
|
|
148
|
+
/**
|
|
149
|
+
* Trading-related primitives.
|
|
150
|
+
*/
|
|
151
|
+
export const TRADING_PRIMITIVES = [
|
|
152
|
+
PRIMITIVE_SWAP,
|
|
153
|
+
PRIMITIVE_OPEN_POSITION,
|
|
154
|
+
PRIMITIVE_CLOSE_POSITION,
|
|
155
|
+
];
|
|
156
|
+
/**
|
|
157
|
+
* Staking-related primitives.
|
|
158
|
+
*/
|
|
159
|
+
export const STAKING_PRIMITIVES = [
|
|
160
|
+
PRIMITIVE_STAKE,
|
|
161
|
+
PRIMITIVE_UNSTAKE,
|
|
162
|
+
PRIMITIVE_RESTAKE,
|
|
163
|
+
PRIMITIVE_CLAIM,
|
|
164
|
+
];
|
|
165
|
+
/**
|
|
166
|
+
* Liquidity-related primitives.
|
|
167
|
+
*/
|
|
168
|
+
export const LIQUIDITY_PRIMITIVES = [
|
|
169
|
+
PRIMITIVE_ADD_LIQUIDITY,
|
|
170
|
+
PRIMITIVE_REMOVE_LIQUIDITY,
|
|
171
|
+
PRIMITIVE_DEPOSIT,
|
|
172
|
+
];
|
|
173
|
+
/**
|
|
174
|
+
* Transfer-related primitives.
|
|
175
|
+
*/
|
|
176
|
+
export const TRANSFER_PRIMITIVES = [
|
|
177
|
+
PRIMITIVE_BRIDGE,
|
|
178
|
+
PRIMITIVE_TRANSFER,
|
|
179
|
+
PRIMITIVE_WRAP,
|
|
180
|
+
PRIMITIVE_UNWRAP,
|
|
181
|
+
];
|
|
182
|
+
// ============================================================================
|
|
183
|
+
// Primitive Helpers
|
|
184
|
+
// ============================================================================
|
|
185
|
+
/**
|
|
186
|
+
* Check if a primitive type is valid.
|
|
187
|
+
*/
|
|
188
|
+
export function isValidPrimitive(primitive) {
|
|
189
|
+
return ALL_PRIMITIVES.includes(primitive);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Check if a primitive is a lending operation.
|
|
193
|
+
*/
|
|
194
|
+
export function isLendingPrimitive(primitive) {
|
|
195
|
+
return LENDING_PRIMITIVES.includes(primitive);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Check if a primitive is a trading operation.
|
|
199
|
+
*/
|
|
200
|
+
export function isTradingPrimitive(primitive) {
|
|
201
|
+
return TRADING_PRIMITIVES.includes(primitive);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Check if a primitive is a staking operation.
|
|
205
|
+
*/
|
|
206
|
+
export function isStakingPrimitive(primitive) {
|
|
207
|
+
return STAKING_PRIMITIVES.includes(primitive);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Check if a primitive is a liquidity operation.
|
|
211
|
+
*/
|
|
212
|
+
export function isLiquidityPrimitive(primitive) {
|
|
213
|
+
return LIQUIDITY_PRIMITIVES.includes(primitive);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Check if a primitive is a transfer operation.
|
|
217
|
+
*/
|
|
218
|
+
export function isTransferPrimitive(primitive) {
|
|
219
|
+
return TRANSFER_PRIMITIVES.includes(primitive);
|
|
220
|
+
}
|
package/dist/schemas.d.ts
CHANGED
|
@@ -237,9 +237,9 @@ export declare const TransactionGroupSchema: z.ZodObject<{
|
|
|
237
237
|
export declare const RiskInfoItemSchema: z.ZodObject<{
|
|
238
238
|
id: z.ZodOptional<z.ZodString>;
|
|
239
239
|
severity: z.ZodOptional<z.ZodEnum<{
|
|
240
|
+
error: "error";
|
|
240
241
|
info: "info";
|
|
241
242
|
warning: "warning";
|
|
242
|
-
error: "error";
|
|
243
243
|
critical: "critical";
|
|
244
244
|
}>>;
|
|
245
245
|
title: z.ZodOptional<z.ZodString>;
|
|
@@ -252,9 +252,9 @@ export declare const RisksSchema: z.ZodObject<{
|
|
|
252
252
|
info: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
253
253
|
id: z.ZodOptional<z.ZodString>;
|
|
254
254
|
severity: z.ZodOptional<z.ZodEnum<{
|
|
255
|
+
error: "error";
|
|
255
256
|
info: "info";
|
|
256
257
|
warning: "warning";
|
|
257
|
-
error: "error";
|
|
258
258
|
critical: "critical";
|
|
259
259
|
}>>;
|
|
260
260
|
title: z.ZodOptional<z.ZodString>;
|
|
@@ -313,9 +313,9 @@ export declare const TransactionActionBlockSchema: z.ZodObject<{
|
|
|
313
313
|
info: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
314
314
|
id: z.ZodOptional<z.ZodString>;
|
|
315
315
|
severity: z.ZodOptional<z.ZodEnum<{
|
|
316
|
+
error: "error";
|
|
316
317
|
info: "info";
|
|
317
318
|
warning: "warning";
|
|
318
|
-
error: "error";
|
|
319
319
|
critical: "critical";
|
|
320
320
|
}>>;
|
|
321
321
|
title: z.ZodOptional<z.ZodString>;
|
|
@@ -625,9 +625,9 @@ export declare const BlockSchema: z.ZodUnion<readonly [z.ZodPipe<z.ZodObject<{
|
|
|
625
625
|
info: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
626
626
|
id: z.ZodOptional<z.ZodString>;
|
|
627
627
|
severity: z.ZodOptional<z.ZodEnum<{
|
|
628
|
+
error: "error";
|
|
628
629
|
info: "info";
|
|
629
630
|
warning: "warning";
|
|
630
|
-
error: "error";
|
|
631
631
|
critical: "critical";
|
|
632
632
|
}>>;
|
|
633
633
|
title: z.ZodOptional<z.ZodString>;
|
|
@@ -688,7 +688,7 @@ export declare const BlockSchema: z.ZodUnion<readonly [z.ZodPipe<z.ZodObject<{
|
|
|
688
688
|
warnings?: string[] | undefined;
|
|
689
689
|
info?: {
|
|
690
690
|
id?: string | undefined;
|
|
691
|
-
severity?: "
|
|
691
|
+
severity?: "error" | "info" | "warning" | "critical" | undefined;
|
|
692
692
|
title?: string | undefined;
|
|
693
693
|
message?: string | undefined;
|
|
694
694
|
}[] | undefined;
|
|
@@ -748,7 +748,7 @@ export declare const BlockSchema: z.ZodUnion<readonly [z.ZodPipe<z.ZodObject<{
|
|
|
748
748
|
warnings?: string[] | undefined;
|
|
749
749
|
info?: {
|
|
750
750
|
id?: string | undefined;
|
|
751
|
-
severity?: "
|
|
751
|
+
severity?: "error" | "info" | "warning" | "critical" | undefined;
|
|
752
752
|
title?: string | undefined;
|
|
753
753
|
message?: string | undefined;
|
|
754
754
|
}[] | undefined;
|
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.4",
|
|
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",
|