HewnDocs
v0.4.1
Search the docs⌘K Search
Reference › Writing and modifying tools
v0.4.1
Reference

Writing and modifying tools

You normally don't write tools by hand. You describe what you want (New on Home, or ask an agent), and Hewn's builder writes it (Building tools). This page describes what the builder produces, for when you want to read it, change it, or write one yourself. In the manifest and the ABI, a tool is a capability.

The full contract is spec/builder-brief.md. It's exactly what the builder reads. In short:

Layout

capability.json      the manifest: id, name, purpose, hue, operations, requires, ui
src/index.ts         the implementation (bundled with its npm dependencies)
package.json         optional: npm dependencies (must run in a Workers-like isolate)
migrations/NNN_*.sql the database schema (SQLite), append-only
tests/cases.json     test cases Hewn runs before promoting a version
ui/icon.svg          a 5×5 pixel glyph for Home
ui/index.html        optional: the tool's own UI

Operations

ts
export const operations = {
  "record_run@1": (input: { distanceKm: number }, ctx) => {
    ctx.db.prepare("INSERT INTO runs (distance_km) VALUES (?)").run(input.distanceKm);
    return { ok: true };
  },
};
  • ctx.db is the tool's own SQLite database. It's synchronous; use ctx.db.transaction(fn) to group writes.
  • ctx.secrets, ctx.call(id, "op@1", input) (another capability) and fetch are available, but only for what the manifest declares under requires and you've granted.
  • Input and output are validated against each operation's JSON Schemas.

Rules Hewn enforces

  • Contracts don't break. A published name@version keeps its inputs, outputs and side effect. A breaking change is a new name@2, next to the old one.
  • Migrations are append-only, and never destroy data: no DROP, DELETE FROM or TRUNCATE. Before a version is promoted, they're dry-run on a copy of your live data, and the data is backed up before they're applied.
  • Tests pass before promotion. The UI is rendered headlessly and must load without errors.

Changing one

Use Change… on the tool, or ask an agent ("add a weekly streak to the habit tracker"). The builder gets the current version and your request, and the same checks apply. If it breaks something you notice later, restore the version before it (Versions, or hewn rollback <id>).

Installing one you wrote

sh
hewn install path/to/my-capability

It goes through the same validation as a built one.

Examples

Three small tools in the repo's seeds/ are meant to be read and copied (Apache-2.0):

  • Running Log: operations, a migration and test cases, nothing more
  • Reading List: a widget, and an operation marked destructive
  • Habits: a widget with a button that runs a write operation

From a checkout of the repo: hewn install seeds/habits.

Reference

The manifest and the ABI (the module, ctx, environment, versions) are described in full in capability-model.md. Hewn implements ABI 2.3. A capability declares the lowest version it needs in "abi"; minor versions only add, so a capability built for 2.0 keeps running.

↑↓ to move↵ to open