JSON-RPC Mode (tuse json)
tuse json [--config PATH] [--resume ID] (-c, -r) serves a Tuse session over JSON-RPC 2.0 on stdin and stdout using NDJSON (one JSON object per newline).
- Tuse writes only JSON-RPC protocol frames to
stdoutand separates captured Luaprint/io.writeoutput and Tuse diagnostic logs (stderr); however, arbitrary Lua code performing directio.stdoutwrites can still write tostdout. - Blank or whitespace-only lines on
stdinare ignored. - Requests are processed while turns, hooks, commands, and tasks execute asynchronously in the background.
- Requests that omit the
idkey are JSON-RPC notifications: they execute normally and produce no response. An explicit"id": nullis not a notification and produces a response with"id": null. Parse errors emit an error response with"id": null; invalid requests (-32600) echo the supplied"id"if present (otherwisenull). - Closing
stdin(EOF), callingshutdown, or receivingSIGINT,SIGTERM, orSIGHUPcloses the active session (clears pending UI requests, stops commands, cancels the active turn, stops all tasks, waits forclosehooks), briefly drains final events (~100 ms), and exits. tuse jsonshares~/.tuse/tuse.dband per-session file locks (~/.tuse/<id>/lock) with the TUI; sessions can be resumed freely between TUI and JSON-RPC, and opening a session already locked by another process fails.
Illustrative NDJSON transcript (> client to stdin, < server to stdout):
> {"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":1,"client":{"name":"ed"}}}
< {"jsonrpc":"2.0","id":1,"result":{"protocolVersion":1,"tuse":"0.9.2","session":{"id":"20261124T120000.000000000Z-a1b2c3d4e5f60708","title":"","cwd":"/work","busy":false,"turn":null},"model":"p/m","effort":"default","capabilities":{"methods":["initialize","turn.submit","..."]}}}
> {"jsonrpc":"2.0","id":2,"method":"turn.submit","params":{"text":"fix the test"}}
< {"jsonrpc":"2.0","id":2,"result":{"inputId":1,"queued":false}}
< {"jsonrpc":"2.0","method":"event","params":{"type":"text","text":"Let me"}}
< {"jsonrpc":"2.0","method":"event","params":{"type":"turn_end","elapsedMs":5120,"calls":2,"model":"p/m","effort":"default","cancelled":false}}
TUI vs JSON-RPC Capabilities
| Capability | TUI | JSON-RPC |
|---|---|---|
| Turns, steering, cancel | Enter, edit dimmed draft, Esc | turn.submit, turn.withdraw, turn.cancel |
# prompts, @ files, images |
interactive pickers, Ctrl+V | turn.submit (prompts, files, images) |
| Sessions: new, resume, list | /new, /resume |
session.new, session.resume, session.list |
| Model and effort | /models |
model.get, model.set, model.list |
| Config inspection and reload | /config, /reload |
config.get, config.reload |
| Background tasks | /tasks |
tasks.list, tasks.get, tasks.stop |
| Custom commands | /name |
commands.list, commands.run |
show, pick, ask |
rendered modals/blocks | ui_* events, ui.state, ui.reply, ui.cancel |
| Session data, history, trace, usage | chat, /session, /trace |
session.info, session.snapshot, session.records, session.trace, session.stats |
Clicked path openers (open.<ext>) |
yes | TUI-only |
| Rendering, themes, selection, hotkeys | yes | client's job |
Framing and Error Codes
Requests must be JSON objects with "jsonrpc": "2.0", string "method", optional "id", and optional object "params" (defaults to {}).
Success responses have {"jsonrpc":"2.0","id":...,"result":...}. Error responses have {"jsonrpc":"2.0","id":...,"error":{"code":...,"message":"..."}}. Server notifications have {"jsonrpc":"2.0","method":"event","params":{"type":"...",...}}.
| Code | Meaning |
|---|---|
-32700 |
Parse error (malformed JSON; id is null; server keeps running). |
-32600 |
Invalid request (jsonrpc is not "2.0", method is not a string, or params is not an object). Echoes supplied id when present, else null. |
-32601 |
Unknown method. |
-32602 |
Invalid params (missing/wrong-type param, unsupported protocolVersion, invalid effort, or invalid config.set key). |
-32000 |
Session/domain error (missing prompt/file, empty input, unavailable model, unknown task/command, invalid reload, locked/missing session, invalid UI request ID or picker option). |
-32002 |
Not ready: any method other than initialize or shutdown called before a successful initialize. |
Handshake and Event Ordering
- The client must send
initializefirst (shutdownis also accepted before initialization). protocolVersiondefaults to1when omitted ornull. Any other value fails with-32602and leaves the connection uninitialized so the client can retry.- Events emitted before the JSON-RPC server attaches its session subscriber are buffered up to a pre-subscription cap of 64 events; once subscribed, events queue in the server's channel and are delivered only after the
initializeresponse is written. - After initialization, ordinary
eventnotifications are emitted asynchronously by a background pump and can arrive before or after the reply to a concurrent or triggering method call (for example,usagebeforemodel.setreturns, orreloadedbeforeconfig.reloadreturns). Clients must correlate responses byid. - On
session.newandsession.resume, remaining events from the old session are drained before the switch response, and events from the new session begin arriving after the response.
Methods
Lifecycle
initialize:- Params:
{ protocolVersion?: 1, client?: object }(clientis accepted and ignored). - Result:
{ protocolVersion: 1, tuse: string, session: SessionInfo, model: string, effort: string, capabilities: { methods: string[] } }.
- Params:
shutdown:- Params:
{}. - Result:
null. Closes the active session and exits.
- Params:
Turns
turn.submit:- Params:
{ text: string, prompts?: string[], images?: string[], files?: string[] }.textis required and preserved as given.prompts: names from configprompts(deduplicated; unknown name fails with-32000).images:data:URLs (attached directly) or file paths relative tocwd(read, base64-encoded, and tagged<image id="N" path="..."/>; missing file fails with-32000).files: file or directory paths relative tocwd(deduplicated; images attach as images, directories as<dir>, text files up to 1 KiB as<file>, binary files as<file ... binary="true"/>; missing path fails with-32000). See prompts.md.- Fails with
-32000(no input) iftext(trimmed), hidden context, andimagesare all empty.
- Result:
{ inputId: number, queued: boolean }(queuedistrueif a turn was already running).
- Params:
turn.withdraw:- Params:
{ inputId: number }. - Result:
boolean(trueif the queued message was removed before the agent consumed it;falseotherwise).
- Params:
turn.cancel:- Params:
{}. - Result:
null. Stops running custom commands, cancels running hooks, and cancels the active turn (clearing queued inputs, emittingturn_endwithcancelled: true, and firingagent_messagehook withstatus = "cancelled"). Does not stop background tasks.
- Params:
Sessions
SessionInfo is { id: string, title: string, cwd: string, busy: boolean, turn: { elapsedMs: number, status: string } | null }.
session.info:- Params:
{}. - Result:
SessionInfo.
- Params:
session.snapshot:- Params:
{}. - Result: the full
session()snapshot object (functions.md) includingtrace. Optional absent fields are omitted.
- Params:
session.records:- Params:
{}. - Result: the session's live in-memory record list in chronological order (including records buffered before a new session materializes in SQLite on the first user message, such as the initial
settingsrecord andstarthook records). Canonical record schemas, tagged by"t":"user":{ t, text, context?, images?, at }"assistant":{ t, text, reasoning?, tool_calls?: [{ id, name, arguments }], replay?: { provider, api, model, items }, at }(replayis omitted when absent/empty)"tool":{ t, id, tool, input, output, code: int | null, images?, diffs?: [{ path, diff }], at }"notification":{ t, text }"notice":{ t, text }"error":{ t, text }"compaction":{ t, prune, drop }"usage":{ t, model, usage: { input, output, cached, cache_write }, llm?: true }"settings":{ t, model, effort }(on resume, the latest recordedsettingswins even whenmodelis"")"hook":{ t, name, event, vars: string, error: string | null, ms }(varsis a newline-separatedkey=valueformatted string excludingeventandnullfields)"turn_end":{ t, elapsed_ms, calls, model, effort, cancelled }"ui_markdown":{ t, name, text, pin, removed }(durableshow()event in chronological order; backward-compatible additive v1 tag; excluded from model context and trace)"ui_result":{ t, id, kind: "picker" | "question", prompt, value }(durable answeredpick()/ask()entry in chronological order; backward-compatible additive v1 tag; excluded from model context and trace)
- Params:
session.trace:- Params:
{}. - Result: array of
{ role: string, summary: string, text: string }.
- Params:
session.stats:- Params:
{}. - Result:
{ input, output, cached, cacheWrite, cost: number | null, llmCost: number | null, llmRequests, requests, context, turns, calls }:input,output,cached,cacheWrite: cumulative token totals across main turns,llm(), and resumed history (inputincludescachedandcacheWrite).cost,llmCost: cumulative USD totals (total andllm()subset), ornullwhen no priced usage has been recorded.requests: main-turn usage record count;llmRequests:llm()usage record count.context: latest main-turninput + output(or estimated tokens immediately after compaction; on resume restores the last recorded main-turn usageinput + output, not a post-compaction estimate).turns: recordedturn_endcount.calls: cumulative tool calls from recordedturn_endentries plus the active turn's in-progress calls whilebusyistrue.
- Params:
session.list:- Params:
{ cwd?: string }(omitted,null, or non-stringcwdfalls back to the active session'scwd). - Result: array of
{ id, cwd, title, model, effort, created, updated }forcwd, ordered byupdateddescending, excluding untitled sessions.
- Params:
session.new:- Params:
{}. - Result:
{ session: SessionInfo, model: string, effort: string }. - Prepares the new session (re-reading and validating config) before closing the current session (
reason = "new"). If preparation fails, returns-32000and keeps the current session open.
- Params:
session.resume:- Params:
{ id: string }. - Result:
{ session: SessionInfo, model: string, effort: string }. - Fails with
-32000ifidis already open, does not exist, is locked by another process, or config fails to load (keeping the current session active). On success, closes the current session (reason = "resume"), opens the resumed session (inheriting its savedcwdif the directory exists, plus its saved model and effort; if the last assistant message with tool calls lacks tool results, appends placeholder interrupted tool records; replays persistedui_markdownandui_resultrecords beforestarthooks fire while leaving pending prompts cleared), and switches the event stream. Readsession.recordsfor full chronological history (including interleavedui_markdown/ui_resultrecords) andui.statefor the effective restored UI blocks, answered entries, and any prompt queued by astarthook.
- Params:
Models
model.get:- Params:
{}. - Result:
{ model: string, effort: string }.
- Params:
model.set:- Params:
{ model: string, effort?: string | null, persist?: boolean | null }.effort: omitted ornulldefaults to"default"; must be one ofdefault,low,medium,high,xhigh,max(-32602otherwise).persist: omitted ornulldefaults totrue(-32602if non-boolean); whentrue, savesmodelandeffortin SQLite as defaults for future new sessions.model:""is accepted to clear the active model. Any non-emptymodelmust match a provider in the session's loaded provider snapshot (not live SQLite or the remote catalog; externaltuse connect/tuse disconnectchanges take effect on reload/reopen), else-32000.
- Result:
null. Every successful call appends asettingsrecord, updates session metadata, and emitsusage(even if(model, effort)is unchanged); themodel_changehook fires only when the pair actually changes.
- Params:
model.list:- Params:
{ cached?: boolean | null }(omitted ornulldefaults tofalse; non-boolean fails with-32602). - Result: sorted, deduplicated
string[]of"provider/model"names currently known (configured models whose provider exists in the loaded snapshot plus remote provider models fetched in this config generation). - Never waits for provider discovery: when
cachedisfalse, returns the current snapshot immediately and launches background provider model fetches. Each provider that responds updates the cache and emits amodelsevent (failures emiterrorevents). Onmodels, callmodel.list { "cached": true }to read the refreshed list without triggering another fetch. In-flight fetches started before aconfig.reloadare discarded.
- Params:
Config
config.get:- Params:
{}. - Result: structured resolved config object (no provider credentials, no Lua function bodies):
settings:{ compact_at?: u64, max_image_bytes: int, retries: int, ui: { fold_strings, tool_line, diff_line, output_line, reasoning_line, scroll_step, pad_x, message_gap, list_max_height, input_max_height } }(compact_atomitted when unset)theme:{ bg, surface, fg, dim, muted, border, accent, ok, err, info }(strings)models:[{ name, display, window, prices: { input, output, cache_read, cache_write } | null, compact_at: u64 | null }]systems:string[]prompts:{ [name]: string }functions:{ [name]: "custom" | "override" | "disabled" }hooks:{ [name]: { events: string[] } }commands:string[](sorted custom command names)open:string[](sorted file extensions without leading.)path:string(""when running on defaults)plugins:[{ name, dir, ref?, commit? }](refandcommitomitted when absent)
- Params:
config.reload:- Params:
{}. - Result:
null(or-32000on plugin sync, load, or validation failure, reporting the failing plugin/path and leaving the previous config and callbacks active). Synchronizes enabled Git plugins (cloning missing checkouts and updating moving refs; see plugins.md), emitsreloadedon success, and preserves live UI state, tasks, and active model.
- Params:
config.set(deprecated; prefermodel.set):- Params:
{ key: "model" | "effort", value: string }(any otherkeyfails with-32602). - Equivalent to
model.setchanging only that field withpersist: trueand the same validation rules. - Result:
null.
- Params:
Tasks and Custom Commands
tasks.list:- Params:
{}. - Result: array of
{ id, name, status, started, ended, log }.
- Params:
tasks.get:- Params:
{ id: string, lines?: number }(omitted,null, negative, or non-integerlinesdefaults to50;0returnsoutput: ""). - Result:
{ id, name, status, started, ended, log, output }. Fails with-32000(no task <id>) if unknown.
- Params:
tasks.stop:- Params:
{ id: string }. - Result:
{ id, name, status, started, ended, log }. Fails with-32000if unknown.
- Params:
commands.list:- Params:
{}. - Result: sorted
string[]of configured custom command names.
- Params:
commands.run:- Params:
{ name: string }. - Result:
null. Starts/nameasynchronously as a Lua job; fails with-32000(unknown command /<name>) if not configured.
- Params:
Interactive UI
ui.state:- Params:
{}. - Result:
{ markdowns: [{ name, text, pin }], active: UiRequest | null, entries: [{ id, kind, prompt, value }] }. On resume, persistedui_markdownandui_resultrecords replay beforestarthooks run (withactive = nullat the replay boundary and the UI request ID counter initialized above all restoredui_result.idvalues), soui.statereflects surviving restored blocks, restored answered entries, and any blocks or active prompt created bystarthooks.
- Params:
ui.reply:- Params:
{ id: number, value: string }. - Result:
null. Answers the activeui_request, persists aui_resultrecord, emitsui_result, and emits the next queuedui_requestif any. Validates strictly against the current session incarnation's active queue (stale IDs from before close/switch are rejected). Fails with-32000if there is no active request,iddoes not match the active request, or (for a picker)valueis not inoptions.
- Params:
ui.cancel:- Params:
{ id: number }. - Result:
null. Cancels the activeui_request(returnsnilto Lua, leaves noUiEntry, emits noui_clear, and emits the next queuedui_requestif any). The client clears its own prompt whenui.cancelsucceeds. Fails with-32000if there is no active request oriddoes not match.
- Params:
ui_request makes a prompt active; ui_clear { id } means Tuse withdrew the active request on its own (job cancelled, session closed, or reload dropped a hook's prompt). Call ui.state at any time to resync.
Events
Every session event arrives as a JSON-RPC notification with "method": "event" and "params" containing "type". All four provider API types (openai-chat, openai-responses, anthropic-messages, google-gemini) emit the same streaming and lifecycle event types:
type |
Additional params fields |
Meaning |
|---|---|---|
notification |
text |
Internal notification queued for the agent (e.g., background task completion). |
user |
inputId, text, images (count) |
User message accepted into the turn. |
turn_start |
— | A turn began. |
text |
text |
Streamed assistant text delta. |
reasoning |
text |
Streamed model reasoning delta. |
stream_reset |
— | A retry started; discard uncommitted streamed deltas from the failed attempt. |
assistant |
text |
Final assistant text for a step. |
tool_delta |
index, id, name, code |
Streamed lua tool code chunk. |
tool_start |
timeout (120), id, tool, input |
A tool call started executing. |
tool_end |
id, output, code (int | null), diffs ([{ path, diff }]) |
A tool call finished. |
status |
text |
Transient activity label (first line of argument, up to 60 characters, e.g., reading path..., running cmd...; identical consecutive values are suppressed). |
notice |
text |
Informational notice (e.g., compaction summary or hook NAME: ... error). |
flash |
text |
Short transient banner (e.g., retry countdown). |
error |
text |
Error message (turn/provider failure, unsupported provider capability, command error, model fetch error, deferred reload error). |
turn_end |
elapsedMs, calls, model, effort, cancelled |
Turn completed or was cancelled. |
tasks |
— | Task list or status changed; re-read with tasks.list / tasks.get. |
usage |
— | Usage totals or model/effort changed; re-read with session.stats / model.get. |
models |
— | A provider finished fetching models; call model.list { cached: true }. |
reloaded |
— | A config reload committed (from config.reload, /reload, or Lua reload()). |
ui_markdown |
name, text, pin, removed |
A show() block was created, updated, moved, or removed. |
ui_request |
id, kind ("picker" with options: string[], or "question" with text: string) |
An interactive Lua prompt became active. |
ui_clear |
id |
Server withdrew the active prompt (not emitted after ui.cancel). |
ui_result |
id, kind ("picker" | "question"), prompt, value |
An interactive prompt was answered. |