WASM-Sandboxed Tools

medium10 minwasm-tools.ts

You want math, hashing, JSON queries and validation done by tools that cannot touch the host.

What it shows

  • Run the pre-built WASM tools directly or give them to an agent

The code

wasm-tools.ts
import { Agent, Cogitator } from '@cogitator-ai/core';
import {
  createBase64Tool,
  createCalcTool,
  createHashTool,
  createJsonTool,
  createValidationTool,
} from '@cogitator-ai/wasm-tools';

const apiKey = process.env.GOOGLE_API_KEY;
if (!apiKey) throw new Error('Set GOOGLE_API_KEY');

const calc = createCalcTool();
const hash = createHashTool();
const json = createJsonTool();

const context = { agentId: 'example', runId: 'direct', signal: AbortSignal.timeout(30_000) };
console.log(await calc.execute({ expression: '(2 + 3) * 4' }, context));
console.log(await hash.execute({ text: 'cogitator', algorithm: 'sha256' }, context));
console.log(await json.execute({ json: '{"users":[{"n":"a"},{"n":"b"}]}', query: '$.users[*].n' }, context));

const cog = new Cogitator({ llm: { providers: { google: { apiKey } } } });
const agent = new Agent({
  name: 'data-processor',
  model: 'google/gemini-3.5-flash-lite',
  instructions: 'You process data with sandboxed WASM tools. Show results clearly.',
  tools: [calc, hash, json, createBase64Tool(), createValidationTool()],
  temperature: 0.2,
  maxIterations: 15,
});

const result = await cog.run(agent, {
  input: `Validate "user@example.com" as an email, base64-encode it, and hash "Alice" with sha256.`,
});
console.log(result.output);
console.log('Tools:', result.toolCalls.map((call) => call.name).join(', '));

await cog.close();

Run it

install · bash
pnpm add @cogitator-ai/core @cogitator-ai/wasm-tools
EnvironmentGOOGLE_API_KEY
run · bash
GOOGLE_API_KEY=your-key npx tsx wasm-tools.ts

Or run the full example from a clone of the repository (keys in .env):

repository · bash
npx tsx examples/advanced/03-wasm-tools.ts

Learn more