@reinsjs/agent
v0.9.0
Published
reins — hand the reins to the model. createAgent() + re-exports of @reinsjs/core, @reinsjs/server and @reinsjs/ui-agui
Downloads
3,836
Readme
@reinsjs/agent
Hand the reins to the model. @reinsjs/agent is the umbrella package: createAgent() plus everything from @reinsjs/core, @reinsjs/server and @reinsjs/ui-agui re-exported, so one install gives you an agent with a Web-standard handler and an AG-UI stream. Bring a lowering layer (@reinsjs/lowering-fetch, recommended, or @reinsjs/lowering-pi) and, optionally, the brain (@reinsjs/brain) and a store (@reinsjs/store-sqlite, @reinsjs/store-pg).
pnpm add @reinsjs/agent @reinsjs/lowering-fetch @reinsjs/brainimport { anthropic } from "@reinsjs/lowering-fetch" // or "@reinsjs/lowering-pi" — same call, see its README
import { approval, compact, perception } from "@reinsjs/brain"
import { createAgent, defineTool, memoryStore } from "@reinsjs/agent"
const deploy = defineTool<{ env: "staging" | "prod" }>({
name: "deploy",
description: "Ship the current build",
inputSchema: { type: "object", properties: { env: { type: "string", enum: ["staging", "prod"] } }, required: ["env"] },
needsApproval: true,
execute: ({ env }) => `deployed to ${env}`,
})
export const agent = createAgent({
model: anthropic("claude-opus-5", { apiKey: process.env.ANTHROPIC_API_KEY! }),
tools: [deploy],
store: memoryStore(), // sqliteStores(...) / pgStores(...) for anything real
sockets: [perception(), compact(), approval()],
systemPrompt: "Be brief and direct.",
secret: process.env.REINS_SECRET, // signs the pause state so it can round-trip through a client
})
export const POST = agent.handler // (Request) => Promise<Response>, AG-UI over SSEWithout HTTP:
for await (const event of agent.run({ input: "Ship staging" })) console.log(event.type)run() yields every event of this session as it is appended (a handoff's opening events are in the new session — read them from toSessionId) and returns done | paused | handoff | error. A paused result carries a small signed state; resume from any process with agent.run({ sessionId, resume: state, decisions: [{ toolCallId, approved: true, by: "alice" }] }).
Messages that reach your host while a run is going can be handed to the model before its next call: pass agent.run({ input, pullInput }) (see "Input that arrives mid-run" in @reinsjs/core). It is a per-run option — createAgent does not take it, and asTool sub-agents never pull. Over HTTP, handler: { midRunInput: true } queues a message that arrives while the session's run is active into that run instead of answering 409 (see "Messages during a run" in @reinsjs/server).
Agents as tools
import { asTool } from "@reinsjs/agent"
const lead = createAgent({
model, store,
tools: [
asTool(analyst, { name: "ask_analyst", description: "Ask the inventory analyst. Give a self-contained question." }),
asTool(writer, { name: "ask_writer", description: "Ask the copywriter for customer-facing copy.", abort: "detached" }),
],
sockets: [approval(), budget({ limits: { totalTokens: 500_000 } })],
})Whether to call an expert, what to ask, and whether to trust the answer are the parent model's decisions; there is no orchestrator. asTool handles what a hand-rolled wrapper gets wrong: when the child pauses for approval the parent pauses too (Interruption { kind: "subagent" } carries the child's interruptions), the host answers with decisions: [{ toolCallId, sessionId: childSessionId, approved, by }], and the child's tokens count against the parent's budget. With the default abort: "linked", stopping the parent (abort(new RunStop())) stops the child too and closes its pending calls; a later call to the same expert starts a fresh task instead of resuming the stopped one. examples/team in the repository is a complete three-role setup on Postgres.
What you get for free
- An append-only timeline you can replay, fork and audit — the model reads what actually happened, in order.
- A loop that is one plain async generator with no private state — long, because the edge cases are handled in the open, but readable and copyable — with pause/resume as a return value.
- Brain modules that let the model tidy its own context, keep constraints, spill big results, remember, ask before dangerous actions, and stop at a budget — each optional, each measured before it became a default.
- Fail-closed defaults: unknown events are rejected, forged approval decisions cannot enter through the input, sessions are
404to anyone not explicitly authorized. - One run per session across processes when the store provides a
RunLease(pgStores()does):createAgentinstallsleasedRunRegistrywith its defaults (30 s lease, random owner). To tunettlMs/owner, build it yourself and passhandler: { runs }— see "Multiple instances" in@reinsjs/server. If you callcreateAgentper request (tools assembled per session), build the registry once at process level and pass the samehandler: { runs }every time; each call otherwise gets its own: stop requests and mid-run input do not reach a run started by another call, and a second request on a busy session answers409saying the run is "on another instance" — or, without aRunLease, starts a second run on the same session.
Documentation
The design documents are in Chinese under docs/ at the repository root: docs/系统全景图.md (overview), docs/archive/implementation-plans/2026-09-08-技术方案v1.0.md (spec), docs/archive/2026-10-30-决策日志DECISIONS-0.1至0.4.md (why). Package READMEs are in English.
MIT © 2026 NewRate Limited.
