Writing a Mod¶
A mod is a folder with two files:
~/.titan/mods/hello/
├── mod.toml # what it is, what it needs, which events and slots it uses, its options
└── mod.py # register(on, options)
Titan reads mod.toml without running anything, so a mod can be listed, explained and switched off before its code ever executes. For where to put the folder (yours, the team's, a plugin's) see Mods.
A first mod¶
# mod.toml
[mod]
name = "hello"
version = "0.1.0"
icon = "👋"
description = "A pane that counts the workflows run this session"
events = ["app.start", "workflow.run", "ui.render"]
slots = ["pane"]
# mod.py
from titan_cli.ui.elements import Box, Button, Text
def draw(m, width):
runs = m.state.get("runs", 0)
return Box(
Text(f"Workflows run: {runs}", bold=True),
Button("Reset", lambda: m.state.set("runs", 0)),
border="info",
)
def register(on, options):
@on("app.start")
def start(m, e, next):
m.ui.open("hello", "Hello") # a pane in the side panel
m.state.set("runs", 0)
return next(e)
@on("workflow.run")
def counted(m, e, next):
try:
return next(e)
finally:
if not e.nested:
m.state.update("runs", lambda n: n + 1)
@on("ui.render", match={"component": "Pane", "pane": "hello"})
def render(m, e, next):
return draw(m, e.width)
Restart Titan with titan --mod-dir ~/code/hello, or drop the folder in ~/.titan/mods/. The folder is imported as the package titan_mod_<name>, with mod.py as its __init__, so a big mod splits into modules and imports them relatively (from .sections import prs).
mod.toml¶
[mod]
name = "github" # letters, digits, _ and -, up to 64; must equal the folder name
version = "0.4.0"
icon = "⎇" # one character or emoji, on the rail, dock and home card
description = "..."
requires_plugins = ["github"]
events = ["app.start", "ui.render"] # the only events register() may hook
slots = ["pane"]
last = false # true loads it after every other, so its pane and dock tile come last
ai_task = "PR check diagnosis" # label of the AI routing task, if the mod uses m.ai
ai_assist_task = "Fix with AI" # label of the help-button task, if it uses ai_assist_button
[options.refresh_seconds] # typed, validated options
type = "int" # string | int | float | bool | list | choice
title = "Refresh every (s)"
default = 30 # required
min = 1 # int/float only; max too; choice takes `values = [...]`
[sections.queue] # a part the user can switch off
title = "Merge queue"
default = true
eventsis enforced. Hooking an event not listed makesregisterfail and the mod load not at all.- Options reach
registeras theoptionsdict, defaults overridden by the user's[mods.<name>.options].sectionsis reserved as an option name. - Sections reach the mod as
options["sections"], a{name: bool}. Titan only keeps the switches: the mod decides what a switched-off section stops doing (polling, drawing).
Events¶
register(on, options) hooks events with @on(event, match={...}). A hook is hook(m, e, next):
mis the mod's handle on Titan (below),ethe event's frozen input.next(e)runs the hooks below this one and then Titan's own behaviour, and returns its result. Call it at most once.- Return
next(e)to observe, return something else to answer yourself, or passdataclasses.replace(e, ...)tonextto rewrite what the rest sees.
| Event | Input | Fires | Typical use |
|---|---|---|---|
app.start |
AppStart(project_root) |
The TUI has mounted | Open panes, start timers, bind keys. Never block. |
workflow.run |
WorkflowRun(workflow, source, nested) |
Around a whole workflow | React when it ends; outcome(result) reads the result as "success", "error", "skip" or "exit" |
step.call |
StepCall(workflow, step_id, step_name, plugin, step, command, nested_workflow, params) |
Around one step | Guard (return m.deny("reason")) or rewrite params |
ui.render |
UIRender(component, pane, width) |
A slot is drawn | Answer with an element tree |
section.change |
SectionChange(section, enabled) |
The user switches one of this mod's sections while Titan runs | Start work when a section turns on |
match filters on the event's fields: @on("ui.render", match={"component": "Pane", "pane": "hello"}).
Aborted workflows
A workflow the user aborts (Ctrl+C) never returns from next: it raises. A hook that must act however the run ends puts that code in a finally.
Hooks fail open
A hook that raises is logged and the chain carries on as if it had called next(e). There are no fail-closed guards and no per-hook time budget: a step.call guard that crashes lets the step run.
The m API¶
Every hook receives the same m for its mod.
m.ui.open(pane, title) |
Give the mod a pane in the side panel, drawn by its ui.render hook |
m.ui.badge(pane, text, severity) |
Text under the pane's rail icon (three characters at most); None removes it |
m.ui.pane_shown(pane) |
Whether that pane is on screen now (the panel unfolded on it). Ask it from a timer to read only what someone looks at; the badge shows either way |
w.ui.view, w.ui.open_workspace(name, focus), w.ui.is_open(), w.on_start, w.on_open |
Workspaces only. A workspace is a full screen with the same handle; a mod opens one with m.ui.open_workspace("github", focus={...}). See Writing a Workspace. |
m.ui.widget(name, cls) |
Bring your own widget: cls is a Textual widget with apply(props) (Updatable). A tree draws it with Component(name, props), and a redraw calls apply on the mounted widget, so its own state (focus, scroll) survives. name is global: prefix it with your mod's ("git.graph"). An unregistered name shows a line saying so. Titan removes it when the mod unloads. You accept the Textual coupling: Titan's Button, InputField and PressableRow are the catalog to copy from. |
m.ui.dock(label, badge, severity, on_click) |
A tile in the dock; None as label removes it |
m.ui.home_card(title, text, on_click, actions) |
A card on the home screen; None removes it. actions are Button elements drawn as buttons under the text, each with its own on_press (a press on one never runs on_click; slow work goes to m.run) |
m.ui.toast(text, severity) |
information or warning. An error is never a toast: it opens an error card (a modal with a copy button), so use error_card in your pane when the error belongs there |
m.ui.copy(text, what) |
Copy to the clipboard and say so |
m.state |
get(key, default) / set / update(key, fn, default): in-memory values (default stands in for a missing key); every change repaints the mod's panes |
m.store |
get / set: small JSON values that survive the session (~/.local/state/titan/mods/<mod>.json) |
m.clock.every(seconds, fn, immediately=True) |
Run fn periodically on a background thread; a run still going is not doubled |
m.run(fn) |
Run fn once on a background thread |
m.client(name) |
The client of an installed, enabled plugin ("git", "github", "jira"), or None |
m.keys.bind(key, description, fn) |
App-wide key in Textual's spelling ("f5", "ctrl+k"); refused if Titan or another mod has it, returning False |
m.workflows.run(name, params) |
Open a workflow as if the user picked it, with params over its own; refused while another runs |
m.workflows.resolve(name) |
The workflow run(name) would open (a repo's extension of it, if any), or None when this Titan has none |
m.ai |
See AI |
m.deny(reason) |
The answer for a step.call hook that blocks a step |
m.log |
A structured logger bound to the mod |
Threading rules¶
Hooks, button handlers and key handlers run on the UI thread. Anything that waits goes elsewhere:
- Network calls (
m.client(...)methods) and AI calls (m.ai.complete) block: make them from am.clock.everycallback or insidem.run(...), never straight from a hook. ui.rendermust read onlym.state. It never waits on the network: a timer fillsm.state, the render draws it.- Button
on_pressshould be a state update, or hand slow work tom.run.
Clients¶
m.client("github") returns what steps see: methods returning ClientResult, matched with ClientSuccess / ClientError. A mod uses the plugin's operations and UI models too; it keeps no copy of them. If a plugin lacks something generally useful, it belongs in the plugin (with its docs), not in the mod.
Elements¶
ui.render answers with a tree of plain values; Titan decides how to draw them. Mods never touch Textual widgets.
from titan_cli.ui.elements import Box, Button, Link, Text
Box(
Text("PRs", bold=True, color="info"),
Text(Link("#12", url), " fix the thing", Text(" · 2d", dim=True)),
has_error and Text(err, color="error"), # falsy children are dropped
Button("Review", on_review, variant="primary"),
border="info",
)
| Element | Notes |
|---|---|
Text(*parts, color, bold, dim, wrap) |
Parts are strings, Links or nested Text spans. Truncates with an ellipsis unless wrap=True. |
Link(label, url) |
Opens an http(s) URL. URLs in any text are links even when wrapped. |
Box(*children, border, row, indent, gap, pack) |
Vertical group; border is a colour name; row=True puts children on one line: the first at the left, the rest pushed to the right (a title with a switch beside it); with pack=True each child keeps its own width and they sit together at the left (buttons side by side). |
Columns(*children, ratio, gap, on_resize), responsive_columns(width, builders, ratio, collapse_below, gap, on_resize) |
Children side by side, each as wide as its share of ratio ((3, 2): three fifths and two fifths). responsive_columns is what to use in a ui.render or workspace view: each builder is called with the width its column has, so a choice_card fits either way, and under collapse_below columns (default 120) the builders stack in one column at the full width. Keep fields the user types in outside the columns: crossing the width rebuilds them. With on_resize(widths) the border before each column but the first can be dragged (min. 20 cells per column); on release it gets the cells each column ended up with, to keep (w.store) and pass back as the next ratio. |
Button(label, on_press, dim, variant) |
With a variant (primary, default, success, warning, error) it is Titan's own button, for something that does work; without one it reads as a line of the pane, right for a fold or picker. label is a string, or a Text when parts of the line need their own style (a name plain, its date dim); a variant button flattens a Text to its words. |
error_card(message, copy=m.ui.copy, title, details, copy_text, what, actions, color) |
An ErrorCard: a bordered box with the error (wrapped, never cut off), its details dim and a "⧉ Copy error" button that puts it on the clipboard. actions are your own Buttons beside Copy (a "✦ Ask AI to fix", say); copy_text replaces what is copied. Import it from titan_cli.ui.components. Steps show the same card with ctx.textual.error_card(error, title, actions). |
tabs(items, current, on_select) |
A strip of tabs for a screen that shows one pane at a time: items are (key, label), current the key on show, on_select(key) runs on a press. It only draws; keep the current key in state and draw the pane for it under the strip. Import it from titan_cli.ui.components. |
choice_card(options, width, title, hint, state, key, close, border) |
A choice card: a bordered box that asks the user to pick, the standard way to offer actions to choose from. options are Option(label, on_press, variant); pass the width your ui.render received: options that sit together go on one line when they fit, in a grid of equal cells when they do not, and in a column when only one fits. Option(label, options=[...]) is a dropdown inside the card (▸/▾) and Option(label, content=Box(...), on_open=fn) one with a body of your own, such as a small form; state=m.state and key keep which one is open (one at a time; two cards in one pane need two keys) and close folds it after a pick. Mix options and dropdowns in one card instead of one card per kind. A str among the options is a heading that starts a new titled group, and any other element (a Text note, an Input) is drawn as it is between them. None options are dropped. Import Option and choice_card from titan_cli.core.mods. |
ai_assist_button(m, prompt, label, cwd) |
A help button for an error_card's actions or anywhere a Button goes: pressing it opens the user's interactive AI CLI with prompt typed in, through m.ai.assist. Which CLI and model is the user's choice in AI Configuration, on the task mods.<name>.assist (declare ai_assist_task in mod.toml so the AI screen lists it). No workflow in between; use a workflow only when the button must do more than open the CLI (check out a branch, commit, push). Import it from titan_cli.ui.components. |
fix_prompt(error, context) |
The prompt for "help me fix this error": the error whole, context after it (a log excerpt, the branch). Pass it to ai_assist_button. |
ask_ai(m, key, work) |
Asks the routed AI in the background and keeps the outcome under key as an AIResult (status running/done/error, text, model, error, and a free value for whatever work makes of the answer). work(m) calls m.ai.complete and returns its AIAnswer (or an AIResult). A second call under a key that is still running starts nothing and returns False. Read it with ai_result(m, key). |
ai_ask_button(m, key, work, label, again_label, running_label) |
The button that runs ask_ai; its label follows the state (ask, "… asking |
ai_answer_box(m, key, title, render) |
What key answered, with a copy button, or an error card; nothing while it runs or was never asked. render(value) draws the mod's own value instead of the text. |
ai_chip(m, title) |
The "⟳ who answers" line: pressing it opens m.ai.configure. Fill it at start with m.run(lambda: refresh_ai_chip(m)). |
Input(value, placeholder, on_change, on_submit, size) |
A text field: one line (size="compact", the default) or, with size="normal", three lines with a border, for a field that is the point of a screen such as a search box. on_change fires on every keystroke — keep it to an m.state update, never a network or disk call; on_submit (optional) fires on Enter, the right place to kick off a search or write to m.store. |
Diff(diff, max_lines=400) |
One file's diff drawn as code: line numbers, a bar and a tinted background across added and removed lines, syntax colours by file extension. diff is shaped like the git plugin's UIFileDiff (path, is_binary, hunks of header and lines of kind, text, old_no, new_no), so the UIFileDiff inside the ClientSuccess that client.get_commit_file_diff(...) or client.get_worktree_file_diff(...) returns goes straight in. It never wraps: a line wider than the box scrolls sideways (scrollbar under the code, or shift + wheel), while the screen scrolls vertically; past max_lines it ends with "… N more lines" and the caller raises max_lines to show them. |
Component(name, props) |
A widget registered with m.ui.widget(name, cls), drawn with props. For what the elements above cannot say. |
Colours are semantic: primary, accent, success, warning, error, info, subtle. Lists are flattened and falsy children (None, False, "", 0, []) dropped, so conditionals and comprehensions read naturally.
Slots¶
| Slot | How | Notes |
|---|---|---|
| Side panel pane | m.ui.open + ui.render |
Right of every screen; rail icon from mod.toml's icon, with m.ui.badge |
| Dock tile | m.ui.dock |
Bottom bar, always visible |
| Home card | m.ui.home_card |
Beside Quick launch |
| Toast | m.ui.toast |
One-off notification |
| Key binding | m.keys.bind |
Listed in the footer and in ? |
The three visible slots are independent: a mod fills any mix, with different content in each (a count on the dock, detail in the pane, a summary and an action on the card). Titan's own prompts (ask_*) are never drawn by a mod, because their answer authorises an action.
AI¶
m.ai.complete(prompt, system=None, max_tokens=None, model=None, timeout=180) returns an AIAnswer (.text, .model, .error, .ok). It goes through Titan's AI routing under the task mods.<name>: the user's connection or headless CLI, task pin, and F2/F3 for the session all apply. It blocks, so call it from m.run or a timer.
To let the user choose who answers, call m.ai.configure(title, on_done) from a button: it opens the same pickers as the AI screen and writes the task pin. m.ai.assist(prompt, cwd=None) returns an AssistResult (.opened, .reason): it resolves the task mods.<name>.assist (pinned like any task, and separate from mods.<name> because it is the one that may be an interactive CLI, which a one-shot complete could not run) and, when that is an interactive CLI, opens it with prompt typed in. It suspends Titan while the CLI runs, so call it from a button or key; a refusal (AI off, no CLI, a task that is not interactive) is already toasted, reason says why. m.ai.pinned() and m.ai.describe() say who answers now (describe may probe: call it off the UI thread). Declare ai_task (and ai_assist_task when the mod has a help button) in mod.toml so the AI screen lists each task with a readable label. Every AI call a mod makes goes through this routing: a mod never picks a CLI, a connection or a model itself.
Secrets¶
The same rules as steps: no Titan API returns a secret string, and a mod never keeps a raw secret in m.state, m.store or a pane.
Testing¶
Keep reading and drawing as plain functions over fake clients, and test them without mounting anything. import_mod imports a mod exactly as Titan does and ModBus().api(name) gives the m a hook receives:
from pathlib import Path
import pytest
from titan_cli.core.mods import ModBus, import_mod
from titan_cli.ui.elements import Text
@pytest.fixture(scope="session")
def mod():
return import_mod(Path(__file__).resolve().parent.parent)
def test_draw_shows_the_count(mod):
m = ModBus().api("hello")
m.state.set("runs", 3)
tree = mod.draw(m, 40) # a plain function returning elements
assert "Workflows run: 3" in tree.children[0].parts
To drive hooks, build a ModBus, give it a host with the methods your mod calls (bus.host = SimpleNamespace(...)), register with mod.register(bus.on_for("hello"), options) and fire events with bus.dispatch("app.start", AppStart(project_root=...), lambda e: None). Without a host, m.ui only logs, timers never fire and no client is reachable.
A mod shipped by an official plugin keeps its tests beside it; they run with the plugin's own suite.
Shipping a mod with a plugin¶
An official plugin ships mods by overriding mods_path on its plugin class, returning the folder that holds one subfolder per mod:
They load while the plugin is enabled, and the user can still switch any of them off. Add the mods' folder to testpaths in pyproject.toml, and document the mod in docs/plugins/<plugin>/overview.md.
Verifying live¶
- Run
titan-dev --mod-dir <folder>and watch~/.local/state/titan/logs/titan.logformod_loaded,mod_load_failedormod_hook_failed. - Open the Mods screen (
mon the home screen) to see the mod's status and switch its sections. - Restart after every edit: mods load at start and Titan does not watch their folders.