From your own code
A program can do what the sidebar does. It can start the agent in a workspace, begin a fresh conversation, send the agent a task, follow its replies and collect what it made. A backend that gives a person’s task to an agent in their workspace works this way, and so does WineStudio.
This page is for integrators. It uses the runtime’s HTTP routes, which a running session describes in full at GET /v1/openapi.json. To drive the session’s applications yourself instead, see Your agent or script.
Credentials and address
Section titled “Credentials and address”The routes are served through the workspace’s runtime port map, at https://<kasm host>/desktop/<kasm_id>/runtime/. A request carries the user’s Kasm sign-in as cookies and a runtime token in the X-Kasm-Runtime-Session-Token header. Your agent or script describes both.
Two tokens exist, and they stand for different parties.
| Token | Set as | Acts as |
|---|---|---|
| Messaging token | KASM_RUNTIME_INBOX_TOKEN |
The person. Messages it posts are delivered to the agent as the person’s messages, and it can answer the agent’s questions |
| Session token | KASM_RUNTIME_SESSION_TOKEN |
The agent’s side. Messages it posts are treated as the agent’s own replies and aren’t delivered to the agent |
Use the messaging token to give the agent its task. Set both in the session’s environment when you request the session. The messaging token can also be sent as ?token= on the read routes.
The routes
Section titled “The routes”| Route | Use |
|---|---|
GET /agent |
Which agent the workspace selected, its activity, its sign-in state (auth) and which conversation controls it has. Never starts anything |
POST /agent/start |
Start the selected agent, or reuse the one running |
POST /agent/conversation |
Start a fresh conversation ({"mode": "fresh", "intent": "<your key>"}), or resume the previous one ("mode": "resume") |
POST /agent/terminal |
Open or raise the agent’s terminal for the person |
GET /inbox |
The current run’s id, counts by state and open questions |
POST /messages |
Send a message |
GET /messages?since=<rev> |
Messages whose revision is newer, oldest first |
GET /events |
The same record as a stream of server-sent events |
POST /attachments |
Upload a file to attach to a message |
GET /requests |
The agent’s open questions |
activity is one of not_started, working, idle, blocked, login_required, unavailable or unknown. POST /agent/conversation is idempotent on intent, so a retry with the same value never starts two conversations. On a busy agent it stops the agent first, as the sidebar’s New conversation does. It answers with state set to ready, or to setup_required when the agent is waiting at its sign-in. A 409 whose error says to try again means the first launch is still starting.
A working sequence
Section titled “A working sequence”The script below starts the agent, begins a fresh conversation, sends one task and prints the reply. It reads every credential from the environment.
import os, time, uuidimport requests
base = f"{os.environ['KASM_URL']}/desktop/{os.environ['KASM_ID']}/runtime"s = requests.Session()s.cookies.set("session_token", os.environ["KASM_COOKIE_TOKEN"])s.cookies.set("username", os.environ["KASM_USERNAME"])s.headers["X-Kasm-Runtime-Session-Token"] = os.environ["KASM_RUNTIME_INBOX_TOKEN"]
def agent(): return s.get(f"{base}/agent").json()
# 1. Start the agent and wait until it is ready for a message.if agent()["activity"] == "not_started": r = s.post(f"{base}/agent/start", json={}) r.raise_for_status()while True: a = agent() if a["activity"] == "idle": break if a["activity"] == "login_required": raise SystemExit("The agent is waiting for a person to sign in") time.sleep(2)
# 2. Begin a fresh conversation. Retry while the first launch finishes.intent = str(uuid.uuid4())for _ in range(8): r = s.post(f"{base}/agent/conversation", json={"mode": "fresh", "intent": intent}) if r.status_code != 409 or "try again" not in r.text: break time.sleep(15)r.raise_for_status()if r.json()["state"] != "ready": raise SystemExit("The agent is waiting at its sign-in")
# 3. Send the task. The key makes a retry safe.run = s.get(f"{base}/inbox").json()["run"]key = str(uuid.uuid4())task = "Write a shopping list of five items and save it as shopping-list.txt in my home folder."r = s.post(f"{base}/messages", json={"run": run, "key": key, "text": task})r.raise_for_status()
# 4. Wait for the reply.rev = 0while True: for m in s.get(f"{base}/messages", params={"since": rev}).json() or []: rev = max(rev, m["rev"]) if m["sender"] == "orchestrator" and m.get("text"): print(m["text"]) if m["key"] == key and m["state"] == "responded": raise SystemExit(0) if m["key"] == key and m["state"] in ("undelivered", "unknown"): raise SystemExit(f"Message {m['state']}") time.sleep(2)Messages and their states
Section titled “Messages and their states”A message you send starts as pending and moves to queued, dispatched and responded as the agent takes it, works on it and replies. The agent’s reply is a separate message, with sender set to orchestrator. A 202 from POST /messages means the runtime stored the message, not that the agent read it. If a response is lost, send the same run, key and text again. A 200 returns the stored message and a 202 means it is stored now.
unknown means the runtime can’t tell whether the agent saw the message, and undelivered means it never did. Don’t resend either automatically. Check the conversation first.
A message that starts with a slash, such as /clear, is refused with 400. Messages longer than 4,000 characters are refused with 413.
Questions the agent asks
Section titled “Questions the agent asks”When the agent asks a question, GET /requests lists it, and it appears in the sidebar for the person. Your program can answer with the messaging token by posting a message with reply_to set to the request’s id, its revision, and values keyed by field id. The session token can’t reply, so the agent’s own side never answers for the person.
{"run": "<run>", "key": "<new key>", "reply_to": "<request id>", "revision": 1, "values": {"shop": "Corner store"}}Answering questions in code removes the person from sign-ins and decisions that are theirs to make, so a program should answer only the ones it can answer reliably and leave the rest for a person.
Files the agent made
Section titled “Files the agent made”Files stay in the workspace. Read them with file.get and directory.get over POST /v1/call/<method> with the session token. Files, processes and the shell has the methods.