Type Guards
Updated
The SDK exports 47 runtime type guards for narrowing discriminated union types. After a guard returns true, TypeScript automatically infers the specific type, giving you full autocomplete and type safety.
All guards are exported from the package root.
import {
// Primitive guards
isSwapPrimitive,
isLendingDepositPrimitive,
// Block guards
isTableBlock,
isChartBlock,
// Chart subtype guards
isPieChartBlock,
// Message guards
isAgentMessage,
isBlock,
} from '@chaoslabs/ai-sdk';Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Primitive Type Guards (33)
Each primitive type guard narrows a Primitive union to its specific member type. They all follow the same signature pattern:
function is<Name>Primitive(p: Primitive): p is <Name>PrimitiveCopy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Base (3)
| Guard | Narrows To | Matches |
|---|---|---|
isSwapPrimitive(p) | SwapPrimitive | p.type === 'swap' |
isBridgePrimitive(p) | BridgePrimitive | p.type === 'bridge' |
isTransferTypedPrimitive(p) | TransferPrimitive | p.type === 'transfer' |
Lending (5)
| Guard | Narrows To | Matches |
|---|---|---|
isLendingDepositPrimitive(p) | LendingDepositPrimitive | p.type === 'lending_deposit' |
isLendingWithdrawPrimitive(p) | LendingWithdrawPrimitive | p.type === 'lending_withdraw' |
isLendingBorrowPrimitive(p) | LendingBorrowPrimitive | p.type === 'lending_borrow' |
isLendingRepayPrimitive(p) | LendingRepayPrimitive | p.type === 'lending_repay' |
isLendingClaimRewardsPrimitive(p) | LendingClaimRewardsPrimitive | p.type === 'lending_claim_rewards' |
Staking (8)
| Guard | Narrows To | Matches |
|---|---|---|
isStakingStakePrimitive(p) | StakingStakePrimitive | p.type === 'staking_stake' |
isStakingUnstakePrimitive(p) | StakingUnstakePrimitive | p.type === 'staking_unstake' |
isStakingClaimPrimitive(p) | StakingClaimPrimitive | p.type === 'staking_claim' |
isStakingCooldownPrimitive(p) | StakingCooldownPrimitive | p.type === 'staking_cooldown' |
isStakingDelegatePrimitive(p) | StakingDelegatePrimitive | p.type === 'staking_delegate' |
isStakingUndelegatePrimitive(p) | StakingUndelegatePrimitive | p.type === 'staking_undelegate' |
isStakingWrapPrimitive(p) | StakingWrapPrimitive | p.type === 'staking_wrap' |
isStakingUnwrapPrimitive(p) | StakingUnwrapPrimitive | p.type === 'staking_unwrap' |
Pendle (6)
| Guard | Narrows To | Matches |
|---|---|---|
isPendleAddLiquidityPrimitive(p) | PendleAddLiquidityPrimitive | p.type === 'pendle_add_liquidity' |
isPendleRemoveLiquidityPrimitive(p) | PendleRemoveLiquidityPrimitive | p.type === 'pendle_remove_liquidity' |
isPendleMintPtYtPrimitive(p) | PendleMintPtYtPrimitive | p.type === 'pendle_mint_pt_yt' |
isPendleRedeemPrimitive(p) | PendleRedeemPrimitive | p.type === 'pendle_redeem' |
isPendleSwapToPtPrimitive(p) | PendleSwapToPtPrimitive | p.type === 'pendle_swap_to_pt' |
isPendleSwapToYtPrimitive(p) | PendleSwapToYtPrimitive | p.type === 'pendle_swap_to_yt' |
Perpetuals (7)
| Guard | Narrows To | Matches |
|---|---|---|
isPerpOpenPrimitive(p) | PerpOpenPrimitive | p.type === 'perp_open' |
isPerpClosePrimitive(p) | PerpClosePrimitive | p.type === 'perp_close' |
isPerpIncreasePrimitive(p) | PerpIncreasePrimitive | p.type === 'perp_increase' |
isPerpDecreasePrimitive(p) | PerpDecreasePrimitive | p.type === 'perp_decrease' |
isPerpDepositPrimitive(p) | PerpDepositPrimitive | p.type === 'perp_deposit' |
isPerpWithdrawPrimitive(p) | PerpWithdrawPrimitive | p.type === 'perp_withdraw' |
isPerpSetLeveragePrimitive(p) | PerpSetLeveragePrimitive | p.type === 'perp_set_leverage' |
Vault (4)
| Guard | Narrows To | Matches |
|---|---|---|
isVaultDepositPrimitive(p) | VaultDepositPrimitive | p.type === 'vault_deposit' |
isVaultWithdrawPrimitive(p) | VaultWithdrawPrimitive | p.type === 'vault_withdraw' |
isVaultBorrowPrimitive(p) | VaultBorrowPrimitive | p.type === 'vault_borrow' |
isVaultRepayPrimitive(p) | VaultRepayPrimitive | p.type === 'vault_repay' |
Primitive Guard Example
import {
extractPrimitives,
isSwapPrimitive,
isLendingDepositPrimitive,
isPerpOpenPrimitive,
type Primitive,
} from '@chaoslabs/ai-sdk';
const primitives: Primitive[] = extractPrimitives(response);
for (const p of primitives) {
if (isSwapPrimitive(p)) {
// p is narrowed to SwapPrimitive
console.log('Swap:', p.params.token_in, '->', p.params.token_out);
} else if (isLendingDepositPrimitive(p)) {
// p is narrowed to LendingDepositPrimitive
console.log('Deposit:', p.params.amount, p.params.asset);
} else if (isPerpOpenPrimitive(p)) {
// p is narrowed to PerpOpenPrimitive
console.log('Open perp:', p.params.side, p.params.size);
}
}Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Block Type Guards (9)
Block guards narrow the Block discriminated union to specific block types.
| Guard | Narrows To | Matches |
|---|---|---|
isTableBlock(block) | TableBlock | block.type === 'table' |
isMarkdownBlock(block) | MarkdownBlock | block.type === 'markdown' |
isTransactionActionBlock(block) | ActionBlock | block.type === 'action' |
isInteractiveBlock(block) | InteractiveBlock | block.type === 'interactive' |
isCodeBlock(block) | CodeBlock | block.type === 'code' |
isFormBlock(block) | FormBlock | block.type === 'form' |
isChartBlock(block) | ChartBlock | block.type === 'pie_chart' | 'bar_chart' | 'timeseries_chart' |
isInfoBlock(block) | AlertBlock | block.type === 'alert' |
isErrorBlock(block) | AlertBlock & { severity: 'error' } | block.type === 'alert' && block.severity === 'error' |
Block Guard Signatures
export function isTableBlock(block: Block): block is TableBlock;
export function isMarkdownBlock(block: Block): block is MarkdownBlock;
export function isTransactionActionBlock(block: Block): block is ActionBlock;
export function isInteractiveBlock(block: Block): block is InteractiveBlock;
export function isCodeBlock(block: Block): block is CodeBlock;
export function isFormBlock(block: Block): block is FormBlock;
export function isChartBlock(block: Block): block is ChartBlock;
export function isInfoBlock(block: Block): block is AlertBlock;
export function isErrorBlock(block: Block): block is AlertBlock & { severity: 'error' };Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Chart Subtype Guards (3)
Narrow from Block directly to a specific chart type.
| Guard | Narrows To | Matches |
|---|---|---|
isPieChartBlock(block) | PieChartBlock | block.type === 'pie_chart' |
isBarChartBlock(block) | BarChartBlock | block.type === 'bar_chart' |
isTimeseriesChartBlock(block) | TimeseriesChartBlock | block.type === 'timeseries_chart' |
export function isPieChartBlock(block: Block): block is PieChartBlock;
export function isTimeseriesChartBlock(block: Block): block is TimeseriesChartBlock;
export function isBarChartBlock(block: Block): block is BarChartBlock;Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Message Type Guards (2)
Message guards check unknown values (not pre-typed unions), making them safe for validating data from external sources like parsed JSON.
| Guard | Narrows To | Checks |
|---|---|---|
isAgentMessage(thing) | ChaosSDKMessage | kind === 'message' and has type string |
isBlock(thing) | Block | kind === 'block' and has type string |
export function isAgentMessage(thing: unknown): thing is ChaosSDKMessage;
export function isBlock(thing: unknown): thing is Block;Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Message Guard Example
import { isAgentMessage, isBlock } from '@chaoslabs/ai-sdk';
// Validate parsed NDJSON lines during streaming
const parsed: unknown = JSON.parse(line);
if (isAgentMessage(parsed)) {
// parsed is ChaosSDKMessage
console.log('Message type:', parsed.type);
}
if (isBlock(parsed)) {
// parsed is Block
console.log('Block type:', parsed.type);
}Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Combining Guards
Type guards compose naturally with the block extraction helpers and primitive validators.
import {
extractBlocks,
isTableBlock,
isTransactionActionBlock,
isSwapPrimitive,
isPieChartBlock,
} from '@chaoslabs/ai-sdk';
const blocks = extractBlocks(response);
for (const block of blocks) {
if (isTableBlock(block)) {
console.log('Table headers:', block.tableHeaders);
} else if (isPieChartBlock(block)) {
console.log('Pie chart series:', block.series.length);
} else if (isTransactionActionBlock(block)) {
for (const p of block.primitives ?? []) {
if (isSwapPrimitive(p)) {
console.log(`Swap ${p.params.token_in} -> ${p.params.token_out}`);
}
}
}
}Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Was this helpful?