Create agent-scoped clients with their own signing keys, typed action maps, and allow/deny policies for multi-agent architectures.
The `inv.agent()` method creates a scoped client for a specific agent. Each agent gets its own Ed25519 signing key, optional typed action definitions via the `action()` helper, and allow/deny lists for action enforcement.
Typed actions provide compile-time type safety for action inputs and outputs. Agent clients create sessions automatically scoped to the agent ID.
import { Invariance, action } from '@invariance/sdk';
const inv = Invariance.init({ apiKey: 'dev_...' });
const traderActions = {
swap: action<{ from: string; to: string; amount: string }, { txHash: string }>(),
quote: action<{ pair: string }, { price: number }>(),
};
const trader = inv.agent({
id: 'trader-1',
privateKey: '...',
actions: traderActions,
allowActions: ['swap', 'quote'],
});
const session = trader.session({ name: 'morning-trades' });
await session.record('swap', { from: 'ETH', to: 'USDC', amount: '10' }, { txHash: '0x...' });
session.end();import { Invariance, action } from '@invariance/sdk';
const traderActions = {
swap: action<{ from: string; to: string; amount: string }, { txHash: string }>(),
quote: action<{ pair: string }, { price: number }>(),
};
const analystActions = {
analyze: action<{ symbol: string }, { signal: 'buy' | 'sell' | 'hold' }>(),
};const inv = Invariance.init({ apiKey: process.env.INVARIANCE_KEY! });
const trader = inv.agent({
id: 'trader-1',
privateKey: process.env.TRADER_KEY!,
actions: traderActions,
allowActions: ['swap', 'quote'],
});
const analyst = inv.agent({
id: 'analyst-1',
privateKey: process.env.ANALYST_KEY!,
actions: analystActions,
});const session = trader.session({ name: 'morning-trades' });
// Type-checked — 'swap' input must match { from, to, amount }
await session.record('swap',
{ from: 'ETH', to: 'USDC', amount: '10' },
{ txHash: '0xabc...' },
);
// Type error if you pass wrong fields:
// await session.record('swap', { wrong: 'field' });
session.end();
await inv.shutdown();import { InvarianceError } from '@invariance/sdk';
try {
const agent = inv.agent({
id: 'trader',
privateKey: '...',
denyActions: ['transfer'],
});
const session = agent.session({ name: 'run' });
// This will throw because 'transfer' is denied
await session.record('transfer', { to: '0x...', amount: '1' });
} catch (err) {
if (err instanceof InvarianceError) {
console.error(err.code); // 'POLICY_DENIED'
console.error(err.message); // 'Action transfer is denied'
}
}