Skip to content

Writing a Workspace

A workspace is a folder with two files, the same shape as a mod:

~/.titan/workspaces/hello/
├── workspace.toml   # what it is, what it needs, which events it hooks, its options and sections
└── workspace.py     # register(w, on, options)

Read Writing a Mod first: a workspace uses the same handle, the same state, clock, keys, store, ai and workflows, the same element tree and the same options and sections. This page covers only what differs.

A first workspace

# workspace.toml
[workspace]
name = "hello"
version = "0.1.0"
icon = "👋"
description = "A screen that counts the workflows run this session"
events = ["workflow.run"]
# workspace.py
from titan_cli.ui.elements import Box, Text


def register(w, on, options):
    w.state.set("runs", 0)

    w.ui.view(lambda width: Box(Text(f"{w.state.get('runs')} workflows run", bold=True)), title="Hello")
    w.on_start(lambda w: w.ui.dock("Hello", on_click=w.ui.open_workspace))

    @on("workflow.run")
    def count(w, e, next):
        try:
            return next(e)
        finally:
            if not e.nested:
                w.state.update("runs", lambda n: (n or 0) + 1)

Restart Titan: a "Hello" tile appears in the dock; it opens the screen and Esc goes back. Changing w.state redraws the screen.

What differs from a mod

Mod Workspace
Entry point mod.toml + mod.py, register(on, options) workspace.toml + workspace.py, register(w, on, options)
What it draws Panes, dock tile, home card (ui.render hook) One full screen: w.ui.view(fn, title)
Handle m, given to every hook w, given to register; hooks get it as their first argument
Config table [mods.<name>] [workspaces.<name>]
Events app.start, workflow.run, step.call, ui.render, section.change workflow.run, step.call, section.change
Run once the screen exists app.start hook w.on_start(fn)
Listed in the Mods screen Yes No (edit ~/.titan/config.toml)

The view

w.ui.view(fn, title=None) registers fn(width), which answers with an element tree for a screen width columns wide. Titan calls it again when the window is resized and whenever w.state changes, and patches what is on screen instead of redrawing it, so a focused button or a half-typed field survives. title heads the screen (it defaults to the workspace's name). fn reads w.state only: never the network or the disk.

For two columns that stack on a narrow window use responsive_columns, and keep any text field outside the columns:

from titan_cli.ui.elements import Box, responsive_columns

def view(width):
    return Box(
        search_field(),
        responsive_columns(width, [left, right], ratio=(3, 2), collapse_below=120),
    )

Each builder gets the width its column has, so a choice_card or a wrapped line fits either layout. Pass on_resize=fn to let the user drag the border between the columns: fn(widths) gets the cells each column ended up with, to keep in w.state and w.store and pass back as the next ratio. The elements are in Writing a Mod.

Starting and opening

register runs while Titan is still building, before there is a screen: a w.ui call there goes nowhere. Put what needs the screen (the dock tile, timers, keys) in w.on_start(fn), which runs fn(w) once the interface is up. w.ui.view is fine in register: it only stores the function.

w.on_open(fn) runs fn(w, focus) each time the screen is opened, before it draws. focus is whatever the opener passed to open_workspace(name, focus=...), None from the dock tile. A mod uses it to point you at the thing it listed:

# in the mod: pressing a PR opens the workspace on it
m.ui.open_workspace("github", focus={"pr": 42})

# in the workspace: unfold that row
def focus(w, what):
    if isinstance(what, dict) and what.get("pr"):
        w.state.update("rows_open", lambda rows: {**(rows or {}), str(what["pr"]): True}, default={})

w.on_open(focus)

open_workspace(name) returns False and shows a toast when there is no such workspace.

w.ui.is_open() says whether the screen is the one on top now. A timer that reads for the view asks it first, so a closed workspace costs the service nothing; w.on_open is where to read at once when it opens, since the next tick may be seconds away (the screen is pushed right after w.on_open runs, so do not ask is_open there).

Events

A workspace hooks only the events its workspace.toml lists in events, with the on it receives, exactly as a mod does. A workspace's hooks wrap a workflow outside the mods' ones. app.start and ui.render are not available: use w.on_start and w.ui.view.

Sharing code with a mod

When a mod and a workspace read the same service, put the readers and the snapshots they build in an ordinary package of the plugin and import them from both; neither imports the other, and either may be switched off. The github plugin does it with titan_plugin_github/readers/.

A plugin's mod and workspace with the same name are one component on two surfaces, the side panel and the full screen, so they also share:

  • AI tasks. Both run under mods.<name> (and mods.<name>.assist): the user pins each task once and the AI screen shows one row, "used by mod git · workspace git". Declare the same ai_task / ai_assist_task in both manifests, so either lists it while the other is off. A workspace that no plugin ships keeps its own task, mods.workspace.<name>.
  • Options marked shared. An option that means the same thing on both (a workflow both offer, how much of a diff the AI reads) carries shared = true in workspace.toml, with the mod's type and default; the user sets it once, in [mods.<name>.options], and [workspaces.<name>.options] still overrides it for the workspace alone. Leave unmarked what each surface has of its own: a refresh key (both binding one key would collide), a refresh interval, how many rows it shows.

Timers, keys and the store stay apart.

Where to put it

You want Put it in
Yours, for every project ~/.titan/workspaces/<name>/
The team's, in one repo <repo>/.titan/workspaces/<name>/
Shipped with an official plugin the folder the plugin's workspaces_path returns
Under development anywhere, then titan --workspace-dir <folder>

See Workspaces for the full order and the config.

Testing

Test the view as a pure function: build a ModBus(prefix="workspace.", store_dir="workspaces"), take bus.api(name), set w.state, call bus.render_view(name, width) and read the tree. import_mod(folder, kind=WORKSPACES) imports the folder exactly as Titan does. Run bus.start() to fire the w.on_start functions and bus.opened(name, focus) for w.on_open.