Guides
Plugins
Define local tools with typed schemas, permission allowlists, timeouts, and circuit breakers.
On this page6 sections
Plugins extend the runtime with tools, inference providers, stores, and lifecycle services. They load from the project directory, the user config directory, or a manifest, and tool calls pass through one validation and policy pipeline regardless of where the plugin came from.
What a plugin can register
| Factory | Category | Registers |
|---|---|---|
defineTool | Tool | A callable with a declared schema, permissions, timeout, and breaker. |
defineProvider | Inference provider | A complete or stream implementation for a model backend. |
defineStorage | Key/value store | get, set, delete, keys, plus flush and stats. |
defineVectorStore | Vector store | upsert, query, delete, and count. |
defineService | Lifecycle service | Hooks for start, turn start, tool call, tool result, turn end, and stop. |
useOpenAPI / ingestMcp | Bridge | Tools generated from an OpenAPI document or an existing MCP server. |
A file registers itself with export default defineTool(...), or by calling another factory directly. A file that loads but registers nothing is reported as broken; it does not abort discovery of the remaining plugins.
Define a tool
// plugins/weather.ts
import { defineTool, z } from "sofuu";
export default defineTool({
name: "weather",
description: "Get current weather for a city",
schema: z.object({ city: z.string() }),
timeoutMs: 5000,
permissions: { net: ["api.weatherapi.com"] }, // network allowlist
circuitBreaker: { failureThreshold: 5, resetTimeoutMs: 30000 },
async run({ city }, ctx) {
const res = await ctx.fetch(
`https://api.weatherapi.com/v1/current.json?q=${city}`,
);
return res.json();
},
});
| Field | Purpose |
|---|---|
name, description | Identity shown in the tool inventory and to the model. |
schema | Input contract. Accepts the bundled micro-Zod subset or a plain JSON Schema object. |
output / resultSchema | Optional output contract. A soft failure ({ok:false}) passes through untouched. |
permissions.net | Host allowlist applied by the guarded ctx.fetch. |
requiresApproval | Risk hint surfaced to the host approval UI. |
timeoutMs | Watchdog deadline. The default is 15 seconds. |
circuitBreaker | Failure threshold and reset window. The default is 5 failures in 30 seconds. |
run(args, ctx) | The implementation. Every call receives the validated arguments. |
ctx provides a guarded fetch, a filesystem jail scoped to the session root (readFile, writeFile, exists, readdir, mkdir, rm, and related helpers), an abort signal tied to the watchdog, a read-only environment snapshot, and prefixed logging. A blocked host raises E_DENIED, and the tool returns {ok:false, source:'plugin', error:{code,message}, text}.
Discovery and loading
| Source | Path | Notes |
|---|---|---|
| Project | ./plugins/*.{js,ts} | Dotfiles and *.d.ts are ignored. |
| User | ~/.sofuu/plugins/*.{js,ts} | Applies to every project. |
| Manifest | sofuu.json | plugins: [{ source, enabled? }]; source is a path, never a fetched URL. |
TypeScript is stripped textually and then evaluated. There is no separate tsc step, and the stripper is not tsc: decorators, abstract, implements, overload signatures, and some annotated class forms are not transformed. Write the plain-JavaScript equivalent for those, and run sofuu plugin validate before relying on discovery.
sofuu plugin init weather --category=tool
sofuu plugin list # name, category, circuit state, timeout
sofuu plugin validate plugins/weather.ts
sofuu plugin test weather '{"city":"Guwahati"}'
sofuu plugin add llamacpp # install from the provider catalog
The execution pipeline
Every tool call passes through the same ordered shields.
| # | Shield | Failure code | Behavior |
|---|---|---|---|
| 1 | Input schema | E_SCHEMA | Validated before run(); the callable never sees a bad argument. |
| 2 | Policy and network | E_DENIED | Checked before execution. ctx.fetch and ctx.fs add their own checks. A denial does not trip the breaker. |
| 3 | Circuit breaker | E_CIRCUIT_OPEN | Opens after the configured failures. In half-open state, one trial is admitted at a time. |
| 4 | Watchdog | E_TIMEOUT | Delivers an abort to ctx.signal at timeoutMs. |
| 5 | Panic guard | E_TOOL | Synchronous throws, rejected promises, circular returns, and BigInt results become typed results. |
The policy check denies when evaluation is unavailable, malformed, or fails. sofuu.plugins.invoke reads the evaluator's actual ask decision and requests approval when required, or accepts explicit host approval; an unattended ask is denied. A policy denial overrides a supplied approval. A host that calls the lower-level execute path directly must handle ask itself.
Limits before you ship
- Plugin code runs in-process. The shields contain faults, not malice; a plugin that allocates without bound can still hurt the process.
- Provider streams from JavaScript adapters are buffered and then replayed, not delivered incrementally. A body over 8 MB fails with
E_BACKPRESSURE. useOpenAPIgenerates one tool per operation but does not implement authentication flows. Pass headers throughopts.headersand keep secrets in the environment.useOpenAPIandingestMcpnever fetch a remote document. You supply the spec text or the server command.