A session-first TypeScript Agent SDK for local Node.js processes and for Node.js servers. One API covers multi-turn conversations, streaming tool execution, MCP, subagents, Skills, permissions, hooks, sandbox policies, structured output and observability.
- Node.js 22.14.0 or later
- An ESM project or an ESM-capable build tool; the package is ESM-only and does not support
require().
npm install @blade-ai/agent-sdkCreate agent.mjs:
import { createAgent } from '@blade-ai/agent-sdk';
const agent = await createAgent({
model: 'gpt-4o-mini',
apiKey: process.env.OPENAI_API_KEY,
filesystem: { roots: [process.cwd()], cwd: process.cwd() },
});
const response = await agent.send(
'Read package.json and summarize this project in three bullets',
);
for await (const chunk of response.textStream()) process.stdout.write(chunk);
await agent.close();OPENAI_API_KEY=your-key node agent.mjscreateAgent() defaults to OpenAI; set provider and baseUrl for another provider. Passing filesystem selects the local runtime profile, otherwise the server profile is used; choose explicitly with profile: 'local' | 'server'. Common options stay at the top level, and infrastructure and policy options live under advanced (permission, tokenBudget, skills, hooks).
Each send() returns one AgentResponse: text() for the full text, textStream() for deltas, on(type, listener) for selected events, or stream() for all typed events. Every view shares one execution and can replay events another view already saw.
npm exec --yes --package=@blade-ai/agent-sdk@latest -- create-blade-agent my-agent --preset local --verify
npm exec --yes --package=@blade-ai/agent-sdk@latest -- create-blade-agent my-agent --preset web
npm exec --yes --package=@blade-ai/agent-sdk@latest -- create-blade-agent my-agent --preset production --verifylocal needs no PostgreSQL or Docker; web adds a Browser + AgentServer app whose sessions survive a restart under .blade/; production adds the PostgreSQL, Worker, Docker, approval and recovery topology. Add --no-start to skip opening the browser or --skip-install to only write files.
- Sessions: the
createAgent()facade and low-levelcreateSession(),resumeSession(),forkSession()andprompt(). - Steering and recovery: durable
now,nextandlaterinputs with cancellation, lease-fenced execution ownership, worker handoff and reconnectable cursors. - Streaming: 17 typed events covering turns, content, reasoning, tools, usage, steering, results and errors.
- Providers and tools: OpenAI, Anthropic, Azure OpenAI, Gemini, DeepSeek and OpenAI-compatible APIs; async tools, TypeBox schemas, MCP tools, capability-grouped built-ins and
blade-tool-*packages. - Extensibility and collaboration: onion-style model/tool middleware, declarative plugins, foreground and background subagents, task tools and project Skills.
- Safety and runtime: bounded model, tool and hook execution, permission modes, policy callbacks, path checks, optional OS sandboxes, workspace context, structured output, crash-safe transcripts, compaction, token budgets and traces.
const agent = await createAgent({
model: 'gpt-4o-mini',
apiKey,
advanced: {
permission: async (request) =>
request.kind === 'readonly' ? 'allow' : 'ask',
hooks: {
PreToolUse: [
async (event) => ({ action: 'continue' }),
],
},
},
});advanced.hooks holds in-process TypeScript callbacks for the eight Session hook events; the low-level permissionMode and permissionHandler options remain for runtime integrations.
import { createAgent, defineTool } from '@blade-ai/agent-sdk';
import { AgentClient } from '@blade-ai/agent-sdk/browser';
import { createSession, createServerSession } from '@blade-ai/agent-sdk/advanced';The root exports createAgent, defineTool, middleware and public types; /browser is a browser-safe client with protocol contracts; /advanced holds low-level local and server Sessions; /server/infra holds the server and worker runtime; and /server/postgres is the optional PostgreSQL adapter.
- English documentation · 中文文档
- Middleware and plugins
- Server runtime · Worker runtime
- Runnable golden paths · Runtime benchmarks
- Changelog · 中文更新日志
pnpm install
pnpm run lint
pnpm run type-check
pnpm run test
pnpm run build
pnpm run docs:buildThe version comes from the Git tag, never from commit types. Every releasable change adds a bilingual JSON fragment under .changes/, validated by pnpm run changelog:check. Tag main and push the tag and the release workflow stamps that version, checks the package and docs, publishes to npm with provenance, records the changelogs and creates the GitHub Release. See CONTRIBUTING.md.