Writing a mod

A mod is a folder with a main.js that describes native elements. Woolshed draws them on the GPU and reloads the mod every time you save.

Mods use gpui-kit (its gpui-shell): JavaScript that describes GPUI elements, a native GPU UI (div(), flex, children, .on_click). There's no DOM: the script describes, Rust draws. Background: https://gpui-kit.com. Woolshed carries its own copy with additions (frames, loop), so the type files here are the truth: gpui-kit.d.ts for elements and woolshed.d.ts for the app's data and commands. They're rewritten on every start to match this version.

A mod's folder

~/.config/woolshed/mods/<name>/
  main.js       the mod (required)
  mod.json    where it first appears: { "place": "right" | "left" | "top" | "bottom" | "none", "size": 320 }
  *.js, *.svg   more modules ("./ui.js"), its own icons
  • "focusable": true in mod.json lets pane.go give a shown copy the keyboard. It gets "focus" from nextCommand(): focus your own handle then (this.handle.focus()) and handle keys with on_key_down. The copy gets "focus" again after a popup over it closes. Leave with run("pane.go", {direction}). Without this opt-in, navigation and clicks stay unchanged.
  • The first time Woolshed sees a folder, it docks one copy per mod.json into the shown layout ("none": only where a layout or another mod puts it). After that, the layouts decide. Saving any file reloads every copy in place (not files the app writes: jsconfig.json, .d.ts, hidden ones, node_modules). The old build's tasks and timers stop: a loop ends at its next cx use. A broken save keeps the old version running and shows in woolshed status; its top-level code has already run, so keep side effects on globalThis out of the module's top level.
  • A copy starts the first time it shows, then stays alive while its block is in the layout, shown or not (a hidden column, the other child of a stack, a pane in another tab or behind a zoom): switching back finds it as it was. Its timers keep running while hidden, so poll gently. Another layout starts its mods fresh (pane mods belong to their tab and stay); a block removed or given another mod or props starts over. <mod>.<command> reaches shown copies only.
  • Images and SVGs are referenced from ~/.config/woolshed: image("mods/<name>/logo.svg") (full color), svg("mods/<name>/icon.svg") (tinted with the text color). Saving an asset under mods/ updates every mod showing it, including images that were missing or invalid on first load. Each image path stays loaded until Woolshed quits, so a picture that changes often (a screen, a camera, frames written as data/<mod>/frame-<n>.png) uses image(path).id("screen").live(): it holds one frame, frees each one it replaces, and keeps the last on screen while the next loads; live(version) with a new version reads the same path again.
  • The defaults are mods too, read them as examples: spaces, agents, tabs (the tab bar), menu, prompt, confirm, help, toast, Terminals' picker, peek, and pane (an example pane header, not used by the default layouts), and worktree (git worktrees as spaces: the space menu's and the + menu's New worktree, Open worktree (the repo's checkouts, filtered; one already shown is gone to) and Remove worktree open it as a dialog with props {space, folder?, action: "new" | "open" | "remove"} (the repo: folder, else where the space's first terminal is), or with {branch, tab?, argv?} to make one at once with that first tab (forTask: true, from the start screen: kept in shared "worktree.tasks", checkout -> {main, branch, from, task, on, at}, for Changes' clean-up); checkouts go to ~/.woolshed/worktrees/<repo>/<branch> on the space's machine, a constant at its top; the spaces sidebar nests worktree spaces under their repo's and shows each space's repo, branch, ahead/behind and uncommitted changes), and chat (below), history, get-started, accounts, repos, release, device, new-app, mailbox, team, issues and automations (below). Shipped mods are Woolshed's; duplicate with mod.clone before editing.

An example

import { View, div } from "gpui-kit";
import { v_flex, h_flex } from "gpui-base";
import { exec, spaces, theme, run } from "woolshed";

export default class Branches extends View {
  init(_props, cx) {
    this.branch = {};
    cx.timer.every(3000, (cx) => this.refresh(cx));
    this.refresh(cx);
  }

  refresh(cx) {
    cx.spawn(async (cx) => {
      for (const p of spaces()) {
        // on: a space on another machine runs git there.
        const r = await exec("git", ["branch", "--show-current"], { cwd: p.folder, on: p.machine });
        this.branch[p.id] = r.stdout.trim();
      }
      cx.notify();
    });
  }

  render(cx) {
    const t = theme();
    return v_flex().size_full().bg(t.surface).p(10).gap(4).text_size(13).children(
      spaces().map((p) =>
        h_flex().id(`b-${p.id}`).gap(8).hover((s) => s.bg(t.raised))
          .on_click(() => run("space.select", { id: p.id }))
          .child(div().text_color(p.active ? t.foreground : t.muted).child(p.name))
          .child(div().text_color(t.dim).child(this.branch[p.id] ?? ""))));
  }
}

Rules:

  • main.js default-exports a class extending View. init(props, cx) runs once; render(cx) describes the UI and runs again after cx.notify() and whenever Woolshed's live data changes (spaces, agents, terminals, focus, theme, shared state). Don't poll those.
  • Never keep a cx and use it later. Every callback gets its own: on_click((event, cx) => …), cx.spawn(async (cx) => …), cx.timer.every(ms, (cx) => …).
  • No setTimeout/setInterval (use cx.timer), no top-level await (use cx.spawn in init). Anything slow goes through exec or fetch.
  • Size: put .id("root").on_size_change((size, cx) => { this.size = size; cx.notify(); }) on the root div(), h_flex() or v_flex(). It reports {width, height} in logical pixels after the first layout and whenever the size changes, even with the pointer elsewhere. It does not change props().
  • Cleanup: an optional async close(cx) on the exported View gets one final call when its pane, tab, layout-owned slot or overlay goes, and on app quit. It is not called for a hidden copy or an inactive tab. Start cleanup inside close(); don't await work started earlier. Await cleanup directly, for example async close(cx) { await exec("hc", ["kill", this.browser]); }. Closing stays responsive: cleanup has at most 250 ms (shared across copies on quit), then unfinished tasks are cancelled. On quit, use exec(...) and run("terminal.kill", { terminal: address }): those go out before the event loop stops; other commands may not run. An exec already started keeps running; don't start new long-lived work in close. This is best-effort, not a guarantee after a crash or forced termination.
  • Use theme() colors so the mod follows the user's theme.
  • Animate natively, never with a timer. Every redraw of a mod redraws the whole window, so cx.timer.every(90, () => cx.notify()) for a spinner costs real battery. Use .frames(["⠋", "⠙", …], 90) (shows each string in turn as the element's text), svg(arc).spin(900) (turns an icon once every 900 ms: a ring spinner, e.g. mods/workspace-chrome/spinner.svg), or .loop("opacity", { from: 0.4, to: 1, period: 1200 }) (also width, height, left, top), and .transition(...) for changes: they run without your script and stop when not shown. Timers are for data (a clock, a fetch), and only as often as what they show changes.

Check before you finish

woolshed check ~/.config/woolshed/mods/<name>

Loads the mod, renders it once, exits nonzero with the exact error. Run it after every change. Never call a mod done without a passing check.

Keep in mind

  • A task's cx belongs to the copy that started it: cx.notify() redraws that copy, even when another copy woke it (a shared promise).
  • One mod per idea. Don't edit AGENTS.md or the .d.ts files (the app rewrites them). Don't touch other mods unless the request needs it.

Your agents read these same words: Woolshed writes this guide to ~/.config/woolshed/AGENTS.md on every start. In it, “you” is the agent and “the person” is you.