Skip to main content

Module js

Module js 

Source
Available on crate feature js only.
Expand description

JavaScript bindings (feature js): an Agent class whose tools are JavaScript functions.

import init, { Agent } from "./wasm_agent.js";
await init();
const agent = new Agent(10, 8192, true);            // max steps, context tokens, recover from errors
agent.registerTool("add", "Adds two numbers",
  '{"type":"object","properties":{"a":{"type":"number"},"b":{"type":"number"}}}',
  (args) => String(JSON.parse(args).a + JSON.parse(args).b));
const body = { model: "gpt-4o-mini", tools: agent.openaiTools(), messages: [...] };
const step = agent.stepResponse(await (await fetch(url, ...)).text());
// step = { kind: "tool_calls", results: [...] } or { kind: "final", text }

A tool function receives its input as a string and returns a string (other values are JSON-encoded). If it throws, the model sees Error: <message>.

Structsยง

JsAgent
A ReAct / tool-calling agent for JavaScript.