TypeScript: Tools, streaming, and structured JSON
Give a TypeScript agent typed function tools
Pass plain TypeScript functions (sync or async) into tools: […] so your agent can query databases, call internal microservices, or compute exact numbers.
Example
import { Agent } from 'praisonai';
async function getOrderStatus(orderId: string): Promise<string> {
const statuses: Record<string, string> = { '1042': 'Shipped via DHL' };
return statuses[orderId] ?? 'Order not found';
}
const support = new Agent({
name: 'OrderSupport',
instructions: 'Always call getOrderStatus before answering about an order.',
tools: [getOrderStatus],
});
console.log(await support.start('Where is order 1042?'));Example explained
- tools: [getOrderStatus] — Registers an async TypeScript function the model can invoke automatically.
- statuses[orderId] ?? … — Returns a deterministic fallback instead of throwing undefined.
Stream tokens for snappy user interfaces
Long replies feel slow when buffered. Enable streaming so tokens reach your CLI or browser UI as soon as the model emits them.
Example
import { Agent } from 'praisonai';
const writer = new Agent({
name: 'StreamWriter',
instructions: 'Write a 3-bullet release note.',
stream: true,
});
await writer.start('Summarize: added dark mode, fixed CSV export, cut startup time 40%.');Example explained
- stream: true — Flushes tokens progressively instead of waiting for the full completion.
Common pitfall: unhandled tool promise rejections
Wrap external fetch() calls inside tools with try/catch and return a clear error string (e.g. 'API timeout — retry later') so the agent can recover gracefully instead of crashing the Node process.
Reference: documentation