ConnectedStorage

terminals

storage.terminals → LocalTerminals

Shells and servers on a computer that outlive a single answer. A dev server started here is still running on the next call.

a computer onlylives in this processpiped, no PTY
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

A command run and awaited dies with its answer. A dev server needs the opposite: start it, leave it, come back to its output. That is what a terminal is here.

const pc = await api.storage.get({ id: machineId });

const shell = await pc.terminals.create({ name: "dev", cwd: "myapp" });

await shell.run("npm run dev");
await shell.output({ since: 0 });   // what it has printed so far

shell.busy;   // whether a command is still running
shell.cwd;    // where it is now — it follows cd

await shell.interrupt();            // Ctrl+C
await shell.kill();
pc = api.storage.get(id=machine_id)

shell = pc.terminals.create(name="dev", cwd="myapp")

shell.run("npm run dev")
shell.output(since=0)   # what it has printed so far

shell.busy   # whether the shell is still running
shell.cwd    # the folder it was started in

shell.interrupt()       # Ctrl+C
shell.kill()

The surface

CallDoes
terminals.list()Which ones are open.
terminals.get(id)One of them.
terminals.create({ name, cwd, command })Opens a shell, optionally running something at once.
terminals.remove(id)Closes one.
terminal.run(line)Sends a command.
terminal.output({ since })What it has printed, from an offset.
terminal.interrupt()Ctrl+C — stops the command, keeps the shell.
terminal.kill()Ends the shell.

They live in the process that runs the SDK

Restart your program and every terminal is gone

They are children of your process, not of the machine. A dev server started through one dies when the process holding it exits — including on a reload in watch mode.

Two consequences worth designing around: a second instance of your app has its own terminals and cannot see the first's, and a terminal is not a way to keep something running after you are done.

Never write to a shell that is busy

A shell runs one command at a time. Sending a line while one is running pushes text into whatever is reading stdin — which is a prompt, an editor, or nothing at all.

if (shell.busy) {
  await shell.interrupt();     // or wait, but do not just send
}
await shell.run("npm test");
if shell.busy:
    shell.interrupt()     # or wait, but do not just send
shell.run("npm test")

Reading output as it comes

output({ since }) returns what was printed from an offset, so polling is a loop with a moving cursor rather than a re-read of everything.

let seen = 0;

for (let i = 0; i < 60 && shell.busy; i += 1) {
  const chunk = await shell.output({ since: seen });
  if (chunk.length) { process.stdout.write(chunk); seen += chunk.length; }
  await new Promise((r) => setTimeout(r, 500));
}
import sys, time

seen = 0

for _ in range(60):
    if not shell.busy:
        break
    chunk = shell.output(since=seen)
    if chunk["text"]:
        sys.stdout.write(chunk["text"])
    seen = chunk["cursor"]
    time.sleep(0.5)

Piped, not a terminal emulator

WorksDoes not
Ordinary commands, build tools, serversFull-screen interfaces that redraw
Ctrl+C, through interrupt()Interactive prompts that need a real terminal
cwd that follows cdColours and cursor control, in general

A program that insists on a terminal will say so or behave oddly. Prefer the non-interactive flags such tools almost always have.

A computer only

Anything else raises storage_not_local — including a phone, which has no shell of its own. A page can be given one through the shell bridge, and then an agent has bash there too.