Skip to content

Latest commit

 

History

History
494 lines (387 loc) · 23.3 KB

api.md

File metadata and controls

494 lines (387 loc) · 23.3 KB

Automation API

TermDeck exposes a local HTTP API for agents and scripts. The default server is http://127.0.0.1:8530. Authentication is disabled by default; configure TERMDECK_ACCESS_TOKEN before direct network exposure. When enabled, add Authorization: Bearer <token> to every request. TERMDECK_READ_ONLY=1 rejects all mutating API calls even after authentication.

These endpoints start real persistent TermDeck terminals. A successful prompt response means the prompt was written to the terminal and submitted; it does not mean the agent has finished processing it. Use GET /api/sessions/{session_id}/response for the status and for whatever the agent has said back.

Project-scoped creation

POST /api/projects/{project}/sessions takes the same body as POST /api/sessions, with optional worktree_id (omitted means root). The project must already exist. Without an explicit project, /api/sessions inherits the originating session's project and worktree; an explicit project defaults to its root, not the parent's working directory. A conflicting cwd or body project is rejected. Responses include project, worktree_id, and cwd. MCP spawn_agent(project="stock", worktree_id="wt-id", ...) uses the scoped route; list_projects and list_worktrees(project="stock") discover destinations. Session IDs remain global for prompt, response, status, and interrupt calls.

Projects and worktrees

The project header can switch between the repository root and every Git worktree discovered for that repository. Sessions, closed sessions, terminal layout, files, search, and Git views are scoped to the selected worktree. The root worktree always has the stable ID root; child IDs are stable across server restarts.

List the worktrees for a registered project:

curl -sS 'http://127.0.0.1:8530/api/worktrees?project=stock'

Create a worktree from the current branch, or provide base_ref and an explicit branch:

curl -sS -X POST http://127.0.0.1:8530/api/worktrees \
  -H 'Content-Type: application/json' \
  -d '{"project":"stock","name":"review","base_ref":"main"}'

TermDeck creates the branch and a free folder below <repository>-worktrees/, beside the repository itself. Pass location to put it somewhere else — it must be an absolute path outside the repository. Select the returned id when creating a terminal:

{"project":"stock","worktree_id":"wt-abc123","model":"codex","title":"reviewer"}

Delete a worktree only after its terminals are closed and purged. Set move_to_trash to move its folder to the configured trash directory; false leaves the files in place and detaches the Git worktree:

curl -sS -X DELETE http://127.0.0.1:8530/api/worktrees/wt-abc123 \
  -H 'Content-Type: application/json' \
  -d '{"project":"stock","move_to_trash":true}'

Start one terminal (create, and run a prompt in the same call)

POST /api/sessions creates one terminal. Give it a prompt and it starts immediately, submits that prompt, and returns the created session summary; leave the prompt out and it just makes the terminal. Set origin_session to have the completed child result sent back to that session.

task_json=$(curl -sS -X POST http://127.0.0.1:8530/api/sessions \
  -H 'Content-Type: application/json' \
  -d '{
    "title": "reviewer",
    "cwd": "/Users/dan/workspace/stock",
    "project": "stock",
    "model": "codex",
    "model_name": "gpt-5.6-luna xhigh",
    "permission": "workspace-write",
    "prompt": "Please review this terminal state and summarize the top 3 risks.",
    "output_path": "/tmp/termdeck/reviewer.out",
    "after": "termde",
    "origin_session": "termde",
    "bracketed": true
  }')

If project is omitted and after is the unique session/group name in a single project, TermDeck infers that project from the anchor before creating the new terminal.

For the result, use GET /api/sessions/{session_id}/response:

session_id=$(python3 -c 'import json,sys; print(json.load(sys.stdin)["session_id"])' <<< "$task_json")
curl -sS "http://127.0.0.1:8530/api/sessions/$session_id/response"
curl -sS "http://127.0.0.1:8530/api/sessions/$session_id/response?limit=5"

Relative output_path values are resolved under cwd before writing.

Response:

{
  "session_id": "abc123...",
  "status": "completed",
  "processing": false,
  "needs_attention": false,
  "responses": [{"role": "assistant", "text": "...", "final": true}]
}

limit counts responses, not transcript entries: asking for five gives the five most recent things the agent said, oldest first, however much thinking and however many commands ran between them. It defaults to 1 — the latest response — and is capped at 50. A session can be named by its id or, when the name is unique among open terminals, by its title.

GET /api/sessions/{session_id}/response/final takes the same parameters and leaves out what an agent says on its way through the work — "I'll check the config first" — keeping the responses it ended a turn with. Only Codex marks which those are; for every other agent each response is taken as one, so this never comes back empty merely because an agent says nothing about it.

Waiting for the answer to your own prompt

since is what ties a response to a prompt. POST /api/sessions/{session_id}/prompt returns the instant the prompt went in — 2026-09-23T08:42:54.345Z, the same ISO-8601 UTC shape the transcript stamps its turns with, and one a query string takes as it is. The server finds the prompt the transcript recorded at or after that instant and returns what the agent said after it:

since=$(curl -sS -X POST "http://127.0.0.1:8530/api/sessions/$session_id/prompt" \
  -H 'Content-Type: application/json' -d '{"text":"Summarize the risks."}' |
  python3 -c 'import json,sys; print(json.load(sys.stdin)["since"])')

curl -sS "http://127.0.0.1:8530/api/sessions/$session_id/response?since=$since"

Poll until responses is not empty. POST /api/sessions returns a since of its own for the prompt it submits. The boundary is the prompt rather than the bare instant because a prompt sent while the agent is busy waits its turn: the one ahead of it can be answered in between, and that answer is before this prompt in the transcript, so it is not returned. Until the transcript has the prompt, nothing has answered it and responses stays empty — which is also the right answer while a queued prompt waits.

Nothing else in the response establishes that association, and the other fields are easy to misread on their own:

  • status is the terminal's process: running while the terminal is open, completed once it has exited, error if it exited badly. A terminal stays open between prompts, so this never says a response arrived.
  • processing is whether the agent is working on a turn. False also means "has not started yet" and "is waiting on a person", and in both cases the newest response in the transcript belongs to the request before.
  • needs_attention is that second case on its own: the agent is asking for permission or input.

A response with no timestamp on it is never returned under since: no stamp is no proof it is new, and ruling out the earlier response is the whole point of asking. limit can also return fewer responses than asked for even when older ones exist, because one call reads back through a bounded number of transcript pages. Ask history-page for the transcript itself when you need all of it.

The whole transcript, with the thinking, the commands and their output, is GET /api/sessions/{session_id}/history-page?before=&limit= — that limit counts transcript entries.

output_path is where raw terminal bytes are appended. Set a per-project path and include that file in any monitor process that needs deterministic logs. model_name is passed as an explicit --model argument to Codex, Claude, or AGY. The request is not blocking; it returns after prompt submission. Poll response for the status and what the agent said back.

Session IDs are globally unique, so agent callers address a terminal only by /api/sessions/{session_id}. A worktree_id is session metadata supplied in the create/task request or used as a list/layout filter; it is not part of the agent session URL.

Set worktree_id to run the child in an existing project worktree. The older worktree: true option remains available for automation callers that want TermDeck to create a worktree for that individual terminal. The optional worktree_branch and worktree_base fields select the branch name and base ref; otherwise TermDeck generates a branch under termdeck/ from the terminal title. The response includes worktree_path, worktree_branch, and the base commit. This does not make the request blocking and it does not commit changes for the agent.

Launch several named terminals

POST /api/sessions/batch creates up to 32 terminals and submits their prompts. The top-level prompt, cwd, project, model, permission, bracketed, after, and worktree values are defaults for every item. An item can override any of them. after accepts an existing session name or group name (case-insensitive), or the stable session:<id> / group:<id> layout token. It inserts each new terminal immediately after that anchor. If the anchor is a member of a group, the new terminal inherits that group and is inserted immediately after that member; when several items share the same after, their request order is preserved. Every request creates new terminals; it is not idempotent. A top-level origin_session files every new terminal under that session as a spawned child and, like a single create, defaults after, project, worktree, and cwd to it.

For a new Codex terminal, name is also submitted as Codex's thread name after the new session is detected. The initial prompt is sent only after that rename and the Codex composer are ready, so it is not lost during startup. Resuming an existing Codex session does not rename the resumed thread.

curl -sS -X POST http://127.0.0.1:8530/api/sessions/batch \
  -H 'Content-Type: application/json' \
  -d '{
    "cwd": "/Users/dan/workspace/stock",
    "project": "stock",
    "model": "codex",
    "model_name": "gpt-5.6-luna xhigh",
    "permission": "workspace-write",
    "after": "existing session or group name",
    "prompt": "Inspect the current task, make the requested change, and report the result.",
    "terminals": [
      {"name": "x"},
      {"name": "y"},
      {"name": "z"}
    ]
  }'

The response contains one result per requested terminal. A terminal that was created successfully remains available even if its prompt submission fails, so a partial failure does not silently kill work already started.

{
  "requested": 3,
  "created": 3,
  "prompt_submitted": 3,
  "failed": 0,
  "items": [
    {
      "name": "x",
      "session": {
        "session_id": "abc123...",
        "title": "x",
        "project": "stock",
        "agent_kind": "codex",
        "agent_session_id": null,
        "running": true
      },
      "prompt_submitted": true,
      "queued": false
    }
  ]
}

The agent session ID may initially be null; TermDeck discovers the Codex or Claude session asynchronously after the CLI creates its session file.

Create one terminal, then submit a prompt

The same call without a prompt creates the terminal and leaves it waiting; the prompt endpoint sends to it later. It also accepts the optional after field, using the same session/group name or stable layout token rules as the batch endpoint.

session_json=$(curl -sS -X POST http://127.0.0.1:8530/api/sessions \
  -H 'Content-Type: application/json' \
  -d '{
    "title": "x",
    "cwd": "/Users/dan/workspace/stock",
    "project": "stock",
    "model": "codex",
    "model_name": "gpt-5.6-luna xhigh",
    "permission": "workspace-write",
    "after": "termde"
  }')

session_id=$(python3 -c 'import json,sys; print(json.load(sys.stdin)["session_id"])' <<< "$session_json")

curl -sS -X POST "http://127.0.0.1:8530/api/sessions/$session_id/prompt" \
  -H 'Content-Type: application/json' \
  -d '{
    "text": "Inspect the current task, make the requested change, and report the result.",
    "bracketed": true
  }'

bracketed defaults to true and sends the prompt as one paste operation before pressing Enter. Prompts always steer: the prompt is submitted immediately, even while the agent is mid-turn, and the agent changes direction. There is no option to queue it behind the current turn; a steer or queue field from an older script is ignored. If a requested placement target is missing or ambiguous, that terminal is still created and its prompt is still submitted; the item reports placement_error and the response increments placement_failed.

Did the prompt go in

Sending a prompt — POST /api/sessions/{session_id}/prompt, POST /api/sessions with a prompt, and each item of a batch — waits until the agent has recorded it, or about fifteen seconds without that, and says which:

delivery prompt_submitted Meaning Send it again?
confirmed true The agent recorded it as submitted. No
unconfirmed true It went in; nothing showed whether the agent took it. delivery_detail says what is known. No — watch /response
failed false It did not go in. delivery_detail says why. There is no since. Yes

A prompt fails when a dialog in the terminal would take its keys as an answer — Codex's offer to update, whose first choice ends the session to run the update, or Claude asking whether to trust a folder — in which case nothing is typed and the dialog is left for a person to answer, or when the terminal exits before taking it. A new terminal whose prompt failed still exists; its session_id is in the response.

The confirmation reads what each agent writes the moment a prompt is submitted, because a transcript can hold a prompt sent mid-turn back until the agent's next step: Codex's prompt history (~/.codex/history.jsonl) and the queued-message records in Claude's transcript; other agents go by their transcript. unconfirmed is never reported as a failure, since sending again would give the agent the same prompt twice.

Review an isolated worktree

Every session created with worktree: true exposes its branch and worktree folder in the terminal list. Review the parent-relative commits, changed files, and diff with:

curl -sS "http://127.0.0.1:8530/api/sessions/$session_id/worktree/review"

Finish it with one of these actions:

curl -sS -X POST "http://127.0.0.1:8530/api/sessions/$session_id/worktree/finish" \
  -H 'Content-Type: application/json' \
  -d '{"action":"keep"}'

keep leaves the folder and branch in place and detaches them from TermDeck cleanup. merge requires the worktree to be clean and merges its branch into the recorded base branch, then removes the worktree and session. discard force-removes the worktree and branch and removes the session. The finish calls are separate from prompt execution; an agent must commit its changes before merge can proceed.

Other useful calls

List active terminals:

curl -sS 'http://127.0.0.1:8530/api/sessions?project=stock'

Delete a terminal and stop its running process:

curl -sS -X DELETE "http://127.0.0.1:8530/api/sessions/$session_id"

The delete succeeds only after TermDeck has terminated that session's dtach process tree and verified that its socket was removed. A failed cleanup returns HTTP 409 and leaves the terminal session visible for inspection.

Every version of a terminal's typed-but-unsent draft is kept, alongside pushes from older copies that were refused so they could not overwrite newer text, and the fifty most recent are served:

curl -sS 'http://127.0.0.1:8530/api/sessions/<session_id>/draft/history'
curl -sS 'http://127.0.0.1:8530/api/sessions/<session_id>/draft/history/<version_id>'

Notebook notes

The notebook of a project is a list of notes, each one addressed by an id that nothing else reuses. Every call works on a single note, so two open decks -- another window, another device, a script -- can write at the same time without either one erasing what the other added. All four calls take the usual project and worktree_id query parameters.

List the notes, and which one the notebook currently shows:

curl -sS 'http://127.0.0.1:8530/api/notebook/notes?project=stock'

Create one. The response carries the note under note, including the note_id it lives at from now on:

curl -sS -X POST 'http://127.0.0.1:8530/api/notebook/notes?project=stock' \
  -H 'Content-Type: application/json' -d '{"text": "# Release checklist\n"}'

A caller with an id of its own may send note_id as well; the desktop client does, because it has to show the note before the request comes back. Creating an id that is already there is not an error and changes nothing: the note keeps its text, and the response reports "created": false.

Read or replace the text of one note. A note carries a revision, and a write says which revision it was made from; the write is refused with HTTP 409 when the note has moved on since, so a caller holding an old copy cannot overwrite what it never saw. The refusal carries the current note, so the caller can write again from it:

curl -sS 'http://127.0.0.1:8530/api/notebook/notes/<note_id>?project=stock'
curl -sS -X PUT 'http://127.0.0.1:8530/api/notebook/notes/<note_id>?project=stock' \
  -H 'Content-Type: application/json' -d '{"text": "# Release checklist\n\n- tag\n", "base_revision": 4}'

A write to an id the server has never seen creates the note, so text is not refused because a create was lost. A note still at revision 0 — one written into settings before revisions existed — takes one write without base_revision and carries a revision from then on.

Every version a note is saved at is kept, alongside the text each write replaced, and the fifty most recent are served:

curl -sS 'http://127.0.0.1:8530/api/notebook/notes/<note_id>/history?project=stock'
curl -sS 'http://127.0.0.1:8530/api/notebook/notes/<note_id>/history/<version_id>?project=stock'

Delete one. Nothing else is touched, and an unknown id returns HTTP 404:

curl -sS -X DELETE 'http://127.0.0.1:8530/api/notebook/notes/<note_id>?project=stock'

POST /api/notebook/trash is separate: it writes a note's text to the OS trash as Markdown, which is what the close button in the notebook does before the note is deleted.

Project state

Everything the deck remembers about a project — which terminal is selected, the colours, the open files, which note the notebook is on — is read whole and written one field at a time:

curl -sS 'http://127.0.0.1:8530/api/terminal-layout?project=stock'
curl -sS 'http://127.0.0.1:8530/api/project-state/active_session_id?project=stock'
curl -sS -X PUT 'http://127.0.0.1:8530/api/project-state/active_session_id?project=stock' \
  -H 'Content-Type: application/json' -d '{"value": "abc123"}'

There is no call that writes the whole state. One that did wrote every field from the copy the caller was holding, so a deck open in another window put its own stale copy back over what had been saved since it loaded.

The fields several windows add to are refused by this call with HTTP 409, because replacing such a list is itself the loss: terminal_groups, session_groups, terminal_layout, session_order, unread_sessions, recently_opened_terminal_ids, session_view_modes, notebook_notes, selection_copy_history and open_files. Each has calls of its own that add, move or remove one entry — /api/terminal-groups, /api/session-group-assignments, /api/terminal-layout/move, /api/session-order/move, /api/session-unread, /api/recently-opened-terminals/<session_id>, /api/session-view-modes/<session_id>, /api/notebook/notes, /api/notebook/copies and /api/open-files. All of them can still be read through /api/project-state/<field>.

A full settings replace (PUT /api/settings?replace=true) saves the settings and leaves every project's state exactly as the server has it, whatever the request carries.

Moving one item in the side panel is one call naming the item and where it lands. token is session:<id> or group:<id>; moving a terminal four places up is a move before the terminal that is now four above it:

# before another terminal (after: true lands it below instead)
curl -sS -X PATCH 'http://127.0.0.1:8530/api/terminal-layout/move?project=stock' \
  -H 'Content-Type: application/json' \
  -d '{"token": "session:abc123", "target_token": "session:def456", "after": false}'
# to the top of the panel
curl -sS -X PATCH 'http://127.0.0.1:8530/api/terminal-layout/move?project=stock' \
  -H 'Content-Type: application/json' -d '{"token": "group:g1", "to_top": true}'

Terminals inside a parent's agent stack are ordered by PATCH /api/session-order/move ({"session_ids": [...], "target_session_id": "...", "after": false}).

Open files

The files a project has open are the project's, not one window's, and every window of the deck opens and closes its own. One call opens one file, one closes one:

curl -sS 'http://127.0.0.1:8530/api/open-files?project=stock'
curl -sS -X POST 'http://127.0.0.1:8530/api/open-files?project=stock' \
  -H 'Content-Type: application/json' \
  -d '{"root": "/Users/dan/workspace/stock", "path": "trainer/model.py", "mtime": "1758500000"}'
curl -sS -X DELETE 'http://127.0.0.1:8530/api/open-files?project=stock' \
  -H 'Content-Type: application/json' \
  -d '{"root": "/Users/dan/workspace/stock", "path": "trainer/model.py"}'

A file is named by its root and path together. Opening one already open leaves it where it is and takes the newer mtime and git_status; closing one that is not open returns HTTP 404.

Copied text

The copied-text list beside the notes is written one copy at a time, for the same reason the notes are: a deck that has been open for a while holds an older list, and sending that whole list back is how copies made elsewhere disappear.

curl -sS 'http://127.0.0.1:8530/api/notebook/copies?project=stock'
curl -sS -X POST 'http://127.0.0.1:8530/api/notebook/copies?project=stock' \
  -H 'Content-Type: application/json' -d '{"text": "the copied text", "copied_at_ms": 1758500000000}'

The copy goes to the front of the list, a copy of the same text already there moves rather than repeats, and the fifty most recent are kept. copied_at_ms is when the copy was made; leaving it out stamps it now.

Terminal process health and orphan cleanup

GET /api/terminals/processes is a local, read-only inventory of processes reachable from TermDeck's own dtach sockets. It reports each socket's session/title, whether it is attached or detached, descendant PIDs, RSS, CPU, process state, node_repl count, zombie count, and any socket with no persisted session record.

curl -sS http://127.0.0.1:8530/api/terminals/processes

POST /api/terminals/reclaim-orphans is deliberately explicit: it terminates only process trees reachable from dtach sockets that are not present in the TermDeck session store, then verifies their socket removal. It does not affect named/current sessions.

curl -sS -X POST http://127.0.0.1:8530/api/terminals/reclaim-orphans

The normal terminal WebSocket remains available at /ws/<session_id> for interactive input, resize events, scrollback, and live output. The HTTP prompt endpoint is intended for automation that does not need to attach a terminal renderer.