@santori/station 0.9.7 → 0.9.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/agentToken.js +26 -0
- package/dist/agents.js +137 -0
- package/dist/claude.js +14 -0
- package/dist/cli.js +84 -17
- package/dist/codex.js +76 -5
- package/dist/daemon.js +51 -14
- package/dist/frame.js +10 -0
- package/dist/handover.js +11 -7
- package/dist/home.js +7 -1
- package/dist/join.js +149 -40
- package/dist/limits.js +210 -0
- package/dist/login.js +17 -4
- package/dist/mcp.js +60 -9
- package/dist/proxy.js +5 -0
- package/dist/relay.js +38 -1
- package/dist/seats.js +24 -4
- package/dist/self.js +31 -1
- package/dist/sessions.js +12 -3
- package/dist/signup.js +4 -2
- package/dist/starts.js +174 -0
- package/dist/waking.js +293 -0
- package/dist/webhooks.js +16 -4
- package/package.json +2 -2
package/dist/daemon.js
CHANGED
|
@@ -16,6 +16,10 @@
|
|
|
16
16
|
// the seat present, and lets it go only after a day with no check-in: by
|
|
17
17
|
// then its agent is gone, and the room should not show it as here.
|
|
18
18
|
//
|
|
19
|
+
// On a signed-in computer it is also the local install of the person's
|
|
20
|
+
// Claude Code and Codex: it reads the relay's wakes and starts an agent
|
|
21
|
+
// with no window open for an ask to it (waking.ts, starts.ts).
|
|
22
|
+
//
|
|
19
23
|
// Commands talk to it over ~/.station/daemon.sock, one JSON line each way.
|
|
20
24
|
// It is the only writer of seats.json.
|
|
21
25
|
import { appendFileSync, chmodSync, existsSync } from "node:fs";
|
|
@@ -25,16 +29,15 @@ import { VERSION, selfCommand } from "./self.js";
|
|
|
25
29
|
import { freshGuard, throughOf } from "./guard.js";
|
|
26
30
|
import { handOver, heldRead, heldWaiting, messagesById, readHeld } from "./handover.js";
|
|
27
31
|
import { ensureHome, files, readCredentials, remove, writeJson } from "./home.js";
|
|
28
|
-
import { call,
|
|
32
|
+
import { call, seatOver } from "./relay.js";
|
|
29
33
|
import { authFor, loadSeats, roomLabel, withOwner } from "./seats.js";
|
|
30
34
|
import { HARNESS_NAMES, delivers, deliverTo, isRunning } from "./sessions.js";
|
|
35
|
+
import { daemonEnv, joinRoom } from "./join.js";
|
|
36
|
+
import { createWaking } from "./waking.js";
|
|
31
37
|
const SAFETY_MS = 30_000;
|
|
32
38
|
const RETRY_MS = 15_000;
|
|
33
39
|
/** A session gone this long has ended; its seats leave their rooms. */
|
|
34
40
|
const GONE_MS = 2 * 60_000;
|
|
35
|
-
/** A shell seat whose agent has not run `station wait` or joined for this
|
|
36
|
-
* long is gone. Not a limit on listening: a wait checks in every read. */
|
|
37
|
-
const SHELL_GONE_MS = 24 * 60 * 60_000;
|
|
38
41
|
/** With no seats for this long the daemon exits; the next join starts it. */
|
|
39
42
|
const IDLE_EXIT_MS = 30 * 60_000;
|
|
40
43
|
/** How long `station wait` has to write out held messages before the
|
|
@@ -42,6 +45,10 @@ const IDLE_EXIT_MS = 30 * 60_000;
|
|
|
42
45
|
const LENT_MS = 15_000;
|
|
43
46
|
const keyOf = (seat) => `${seat.sessionId}:${seat.roomId}`;
|
|
44
47
|
export async function runDaemon(home) {
|
|
48
|
+
// The computer's daemon acts with the computer's sign-in only (daemonEnv in join.ts).
|
|
49
|
+
for (const name of Object.keys(process.env))
|
|
50
|
+
if (!(name in daemonEnv()))
|
|
51
|
+
delete process.env[name];
|
|
45
52
|
ensureHome(home);
|
|
46
53
|
const paths = files(home);
|
|
47
54
|
const log = (line) => {
|
|
@@ -58,7 +65,8 @@ export async function runDaemon(home) {
|
|
|
58
65
|
return;
|
|
59
66
|
}
|
|
60
67
|
remove(paths.socket);
|
|
61
|
-
|
|
68
|
+
// Only the seats the daemon holds: an agent's own sign-in keeps its seats apart.
|
|
69
|
+
let seats = loadSeats(paths.seats).filter((seat) => !seat.own);
|
|
62
70
|
const closers = new Map();
|
|
63
71
|
const pumping = new Set();
|
|
64
72
|
const again = new Set();
|
|
@@ -131,7 +139,7 @@ export async function runDaemon(home) {
|
|
|
131
139
|
if (!seat)
|
|
132
140
|
break;
|
|
133
141
|
if (text) {
|
|
134
|
-
if (!isRunning(seat.harness, seat.sessionId)) {
|
|
142
|
+
if (!delivers(seat.harness) || !isRunning(seat.harness, seat.sessionId)) {
|
|
135
143
|
// The tick decides whether it ended.
|
|
136
144
|
if (handed)
|
|
137
145
|
report(seat, { waiting: `The ${HARNESS_NAMES[seat.harness]} session is not open.` });
|
|
@@ -171,7 +179,7 @@ export async function runDaemon(home) {
|
|
|
171
179
|
}
|
|
172
180
|
catch (error) {
|
|
173
181
|
log(`${key} read failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
174
|
-
if (
|
|
182
|
+
if (seatOver(error))
|
|
175
183
|
drop(key, false);
|
|
176
184
|
}
|
|
177
185
|
finally {
|
|
@@ -181,6 +189,12 @@ export async function runDaemon(home) {
|
|
|
181
189
|
}
|
|
182
190
|
}
|
|
183
191
|
function connectLive(seat) {
|
|
192
|
+
// Only a seat the daemon puts messages into is here by its connection.
|
|
193
|
+
// A shell's agent reads for itself, so its own calls say it is here:
|
|
194
|
+
// a connection the daemon held for it would keep it present, and its
|
|
195
|
+
// asks waiting for a reader, long after the agent stopped.
|
|
196
|
+
if (!delivers(seat.harness))
|
|
197
|
+
return;
|
|
184
198
|
const key = keyOf(seat);
|
|
185
199
|
const ticket = () => call(seat.relayUrl, `/${seat.roomId}/live`, authFor(seat, readCredentials(home)));
|
|
186
200
|
let client = null;
|
|
@@ -282,6 +296,23 @@ export async function runDaemon(home) {
|
|
|
282
296
|
log(`${key} held messages read with station wait`);
|
|
283
297
|
void pump(key);
|
|
284
298
|
}
|
|
299
|
+
// This computer's installs: the relay's wakes for its agents (waking.ts).
|
|
300
|
+
const waking = createWaking({
|
|
301
|
+
home,
|
|
302
|
+
log,
|
|
303
|
+
seats: () => seats,
|
|
304
|
+
join: async (session, url) => {
|
|
305
|
+
const { seat } = await joinRoom(home, session, url, {
|
|
306
|
+
forget: async (sessionId, roomId) => {
|
|
307
|
+
seats = seats.filter((s) => keyOf(s) !== keyOf({ sessionId, roomId }));
|
|
308
|
+
save();
|
|
309
|
+
},
|
|
310
|
+
});
|
|
311
|
+
add(seat);
|
|
312
|
+
return seat;
|
|
313
|
+
},
|
|
314
|
+
leave: (seat) => drop(keyOf(seat), true),
|
|
315
|
+
});
|
|
285
316
|
const respond = async (request) => {
|
|
286
317
|
if (request.op === "ping" || request.op === "list")
|
|
287
318
|
return { ok: true, seats, version: VERSION };
|
|
@@ -344,14 +375,13 @@ export async function runDaemon(home) {
|
|
|
344
375
|
log(`daemon ${process.pid} (Station ${VERSION}) listening; ${seats.length} seat(s)`);
|
|
345
376
|
for (const seat of seats.filter((s) => !s.left))
|
|
346
377
|
connectLive(seat);
|
|
378
|
+
void waking.loop();
|
|
347
379
|
const tick = setInterval(() => {
|
|
348
380
|
for (const seat of seats.filter((s) => !s.left)) {
|
|
349
381
|
const key = keyOf(seat);
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
drop(key, true);
|
|
382
|
+
// A shell's agent reads for itself: the daemon has nothing to do for it.
|
|
383
|
+
if (!delivers(seat.harness))
|
|
353
384
|
continue;
|
|
354
|
-
}
|
|
355
385
|
if (isRunning(seat.harness, seat.sessionId)) {
|
|
356
386
|
missingSince.delete(seat.sessionId);
|
|
357
387
|
void pump(key);
|
|
@@ -366,17 +396,24 @@ export async function runDaemon(home) {
|
|
|
366
396
|
void pump(key);
|
|
367
397
|
}
|
|
368
398
|
}
|
|
369
|
-
|
|
370
|
-
|
|
399
|
+
// An install that is on keeps the daemon up: it starts its agent for an ask.
|
|
400
|
+
if (emptySince && Date.now() - emptySince > IDLE_EXIT_MS && !waking.installsOn() && !waking.running())
|
|
401
|
+
stop("no seats or installs for a while");
|
|
371
402
|
}, SAFETY_MS);
|
|
372
403
|
const stop = (why) => {
|
|
373
404
|
log(`daemon stopping: ${why}`);
|
|
374
405
|
clearInterval(tick);
|
|
406
|
+
waking.stop();
|
|
407
|
+
// Station starts nothing on this computer from now: it hears so at once.
|
|
408
|
+
const credentials = readCredentials(home);
|
|
409
|
+
if (credentials)
|
|
410
|
+
void call(credentials.relayUrl, "/cli/wakes", { token: credentials.token }, { method: "DELETE" }).catch(() => { });
|
|
375
411
|
for (const close of closers.values())
|
|
376
412
|
close();
|
|
377
413
|
server.close();
|
|
378
414
|
remove(paths.socket);
|
|
379
|
-
|
|
415
|
+
// A moment for that word to go out.
|
|
416
|
+
setTimeout(() => process.exit(0), 500);
|
|
380
417
|
};
|
|
381
418
|
process.on("SIGTERM", () => stop("SIGTERM"));
|
|
382
419
|
process.on("SIGINT", () => stop("SIGINT"));
|
package/dist/frame.js
CHANGED
|
@@ -29,6 +29,16 @@ export function decisionFrame(seat, decisions, participants) {
|
|
|
29
29
|
});
|
|
30
30
|
return [`[Station · ${roomLabel(seat)}] Your card's answer (Station checked it came from their own sign-in):`, "", ...lines].join("\n");
|
|
31
31
|
}
|
|
32
|
+
const ASK_NEWS = ["unreached", "handed", "out"];
|
|
33
|
+
/** What became of this seat's own asks: Station could not reach an agent,
|
|
34
|
+
* or handed the ask to another, or the agent holding it ran out of usage.
|
|
35
|
+
* The event is about the sender's seat. */
|
|
36
|
+
export function askNewsFor(seat, messages) {
|
|
37
|
+
return messages.filter((m) => m.kind === "event" && ASK_NEWS.includes(String(m.event?.type)) && m.participant_id === seat.participantId);
|
|
38
|
+
}
|
|
39
|
+
export function askNewsFrame(seat, news) {
|
|
40
|
+
return [`[Station · ${roomLabel(seat)}] About your asks:`, "", ...news.map((m) => `- ${m.body}`)].join("\n");
|
|
41
|
+
}
|
|
32
42
|
/** Someone asking to join the room: an asked event in the stream. */
|
|
33
43
|
export function askedIn(messages) {
|
|
34
44
|
return messages.filter((m) => m.kind === "event" && m.event?.type === "asked" && typeof m.event.request === "string");
|
package/dist/handover.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
// What one read of a room puts into a seat's session:
|
|
2
|
-
// guard lets through, held messages it
|
|
3
|
-
// cards, people asking to join, and the
|
|
4
|
-
// daemon does the delivery; this decides
|
|
5
|
-
|
|
1
|
+
// What one read of a room puts into a seat's session: what became of its
|
|
2
|
+
// own asks, new chat the loop guard lets through, held messages it
|
|
3
|
+
// releases, answers on the seat's cards, people asking to join, and the
|
|
4
|
+
// note about held messages. The daemon does the delivery; this decides
|
|
5
|
+
// what goes in it.
|
|
6
|
+
import { askedFrame, askedIn, askNewsFor, askNewsFrame, decisionFrame, decisionsFor, frame, noteFrame } from "./frame.js";
|
|
6
7
|
import { AGENT_LIMIT, sift, throughOf, WINDOW_MS } from "./guard.js";
|
|
7
8
|
import { roomLabel } from "./seats.js";
|
|
8
9
|
/** Held messages by id, read again page by page. The pages are every row,
|
|
@@ -38,6 +39,7 @@ otherRooms = false) {
|
|
|
38
39
|
const chat = batch.messages.filter((m) => m.kind === "chat" && m.participant_id !== seat().participantId);
|
|
39
40
|
const decisions = decisionsFor(seat(), batch.messages);
|
|
40
41
|
const asked = askedIn(batch.messages);
|
|
42
|
+
const news = askNewsFor(seat(), batch.messages);
|
|
41
43
|
const authors = (list) => [...new Set(list.map((m) => m.participant_id).filter((id) => id !== null))];
|
|
42
44
|
let participants = await room.participants(authors([...chat, ...decisions]));
|
|
43
45
|
const kinds = new Map(participants.map((p) => [p.id, p.kind]));
|
|
@@ -59,7 +61,9 @@ otherRooms = false) {
|
|
|
59
61
|
// network access, so reading after a note could cost an approval each time.
|
|
60
62
|
const chatFrame = seat().harness === "claude-code" ? noteFrame(seat(), said, participants, command, before, otherRooms) : frame(seat(), said, participants, command, before);
|
|
61
63
|
const parts = [
|
|
62
|
-
//
|
|
64
|
+
// What became of its own asks: could not reach, handed on, out of usage.
|
|
65
|
+
news.length ? askNewsFrame(seat(), news) : null,
|
|
66
|
+
// Answers on its cards: they are what the agent is waiting for.
|
|
63
67
|
decisions.length ? decisionFrame(seat(), decisions, participants) : null,
|
|
64
68
|
admitted.length ? chatFrame : null,
|
|
65
69
|
// Someone asking to join: the people who can answer may not be looking
|
|
@@ -69,7 +73,7 @@ otherRooms = false) {
|
|
|
69
73
|
].filter((part) => Boolean(part));
|
|
70
74
|
return {
|
|
71
75
|
text: parts.length ? parts.join("\n\n") : null,
|
|
72
|
-
handed: admitted.length + decisions.length + asked.length,
|
|
76
|
+
handed: news.length + admitted.length + decisions.length + asked.length,
|
|
73
77
|
lastId: admitted.at(-1)?.id ?? 0,
|
|
74
78
|
guard: sifted.guard,
|
|
75
79
|
through: throughOf(sifted.guard, batch.next_after),
|
package/dist/home.js
CHANGED
|
@@ -83,7 +83,13 @@ export function remove(path) {
|
|
|
83
83
|
export const DEFAULT_RELAY = "https://rooms.station.site";
|
|
84
84
|
export const DEFAULT_WEB = "https://station.site";
|
|
85
85
|
const OLD_WEB = "https://stationsite.netlify.app";
|
|
86
|
-
export function readCredentials(home) {
|
|
86
|
+
export function readCredentials(home, env = process.env) {
|
|
87
|
+
// An agent Station started, or a cloud agent with its install's secret,
|
|
88
|
+
// signs in as itself (agentToken.ts), never with the person's token.
|
|
89
|
+
if (env.STATION_AGENT_TOKEN) {
|
|
90
|
+
const saved = readJson(files(home).credentials, null);
|
|
91
|
+
return { relayUrl: (env.STATION_RELAY_URL || saved?.relayUrl || DEFAULT_RELAY).replace(/\/$/, ""), token: env.STATION_AGENT_TOKEN, userId: "", name: env.STATION_AGENT_NAME ?? "", agent: true };
|
|
92
|
+
}
|
|
87
93
|
const credentials = readJson(files(home).credentials, null);
|
|
88
94
|
if (credentials?.webUrl === OLD_WEB)
|
|
89
95
|
delete credentials.webUrl;
|
package/dist/join.js
CHANGED
|
@@ -2,19 +2,21 @@
|
|
|
2
2
|
// joining a room for it, and what a seated session does there. `station
|
|
3
3
|
// join`, `say` and `file` go through here, and so do the MCP tools, so a
|
|
4
4
|
// room_say with no seat yet joins the way `station join` does.
|
|
5
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
5
6
|
import { execFileSync, spawn } from "node:child_process";
|
|
6
7
|
import { existsSync, openSync } from "node:fs";
|
|
7
8
|
import { basename } from "node:path";
|
|
8
9
|
import { KNOWN_PEER_PROTOCOL, claudeHome, whyNoInbox } from "./claude.js";
|
|
9
10
|
import { codexHome, whyNoThread } from "./codex.js";
|
|
10
11
|
import { answers, ask } from "./daemon.js";
|
|
11
|
-
import { askedFrame, askedIn, decisionFrame, decisionsFor, frame, hearerFor } from "./frame.js";
|
|
12
|
+
import { askNewsFor, askNewsFrame, askedFrame, askedIn, decisionFrame, decisionsFor, frame, hearerFor } from "./frame.js";
|
|
13
|
+
import { askLine, askable, myAgents } from "./agents.js";
|
|
12
14
|
import { lineOf, noteFor, threadHint, whereOf } from "./mark.js";
|
|
13
|
-
import {
|
|
15
|
+
import { files, installId, readCredentials, stationHome } from "./home.js";
|
|
14
16
|
import { call, parseRoomUrl, RelayError } from "./relay.js";
|
|
15
|
-
import { authFor, loadSeats, roomLabel, seatFor } from "./seats.js";
|
|
17
|
+
import { authFor, keepAgentSeat, loadSeats, roomLabel, seatFor } from "./seats.js";
|
|
16
18
|
import { addClaim, codeStep, elsewhere, linkLasts, readPending, startLogin } from "./login.js";
|
|
17
|
-
import { HARNESS_NAMES, callingAgent, delivers, shellSession, workerName } from "./sessions.js";
|
|
19
|
+
import { HARNESS_NAMES, callingAgent, delivers, shellSession, startedSession, workerName } from "./sessions.js";
|
|
18
20
|
import { VERSION, agentCommand, newer } from "./self.js";
|
|
19
21
|
import { Usage } from "./usage.js";
|
|
20
22
|
/** The session this process runs inside. Every command works in any
|
|
@@ -22,7 +24,11 @@ import { Usage } from "./usage.js";
|
|
|
22
24
|
* room messages into the session, so the agent reads them with `station
|
|
23
25
|
* wait`, and `why` says what kept a Claude Code or Codex session from
|
|
24
26
|
* taking them, when one was there. */
|
|
25
|
-
export function inSession(worker = workerName(process.env.STATION_SESSION)) {
|
|
27
|
+
export function inSession(worker = workerName(process.env.STATION_SESSION), env = process.env) {
|
|
28
|
+
// Station started this agent to answer an ask: its session is the one the daemon named.
|
|
29
|
+
const started = startedSession(env);
|
|
30
|
+
if (started)
|
|
31
|
+
return started;
|
|
26
32
|
// A named worker has its own seat, even in a Claude Code or Codex
|
|
27
33
|
// session: Station puts messages into the session, not into one of the
|
|
28
34
|
// agents working in it (subagents), so the worker reads its own with
|
|
@@ -30,22 +36,27 @@ export function inSession(worker = workerName(process.env.STATION_SESSION)) {
|
|
|
30
36
|
if (worker) {
|
|
31
37
|
// Within the session it works in, when there is one: another session's
|
|
32
38
|
// worker of the same name is another seat.
|
|
33
|
-
const parent = callingAgent();
|
|
39
|
+
const parent = callingAgent(env);
|
|
34
40
|
const host = parent && parent.harness !== "shell" ? parent.harness : undefined;
|
|
35
41
|
return shellSession(installId(stationHome()), { STATION_SESSION: worker }, process.cwd(), null, parent ? `${parent.harness}:${parent.id}` : null, host);
|
|
36
42
|
}
|
|
37
|
-
const session = callingAgent();
|
|
43
|
+
const session = callingAgent(env);
|
|
38
44
|
if (!session) {
|
|
39
|
-
const codex = whyNoThread(codexHome());
|
|
45
|
+
const codex = whyNoThread(codexHome(env), env);
|
|
40
46
|
if (codex)
|
|
41
|
-
return shellSession(installId(stationHome()),
|
|
42
|
-
return shellSession(installId(stationHome()),
|
|
47
|
+
return shellSession(installId(stationHome()), env, process.cwd(), `Station could not find this Codex session. ${codex}`);
|
|
48
|
+
return shellSession(installId(stationHome()), env, process.cwd(), whyNoInbox(claudeHome()));
|
|
49
|
+
}
|
|
50
|
+
// `claude -p` runs one turn and ends: messages put into it would never be read, so it reads with wait.
|
|
51
|
+
if (session.harness === "claude-code" && session.claude.entrypoint === "sdk-cli") {
|
|
52
|
+
// Its own seat, by its session: two agents in one folder are two seats.
|
|
53
|
+
return { ...shellSession(installId(stationHome()), env, session.cwd, "claude -p runs one turn and ends, so Station cannot put room messages into it.", null, "claude-code"), id: `shell-${createHash("sha256").update(`${installId(stationHome())}\nclaude-code:${session.id}`).digest("hex").slice(0, 32)}` };
|
|
43
54
|
}
|
|
44
55
|
if (session.harness === "claude-code" && session.claude.peerProtocol !== KNOWN_PEER_PROTOCOL) {
|
|
45
|
-
return shellSession(installId(stationHome()),
|
|
56
|
+
return shellSession(installId(stationHome()), env, session.cwd, `This Claude Code speaks inbox protocol ${session.claude.peerProtocol}; this Station CLI knows ${KNOWN_PEER_PROTOCOL}. Update the CLI to have messages arrive on their own.`, null, "claude-code");
|
|
46
57
|
}
|
|
47
58
|
if (session.harness === "codex" && session.codex.host === "exec") {
|
|
48
|
-
return shellSession(installId(stationHome()),
|
|
59
|
+
return shellSession(installId(stationHome()), env, session.cwd, "codex exec runs one task and takes no more messages, so Station cannot put room messages into it.", null, "codex");
|
|
49
60
|
}
|
|
50
61
|
return session;
|
|
51
62
|
}
|
|
@@ -57,6 +68,13 @@ function gitBranch(cwd) {
|
|
|
57
68
|
return "";
|
|
58
69
|
}
|
|
59
70
|
}
|
|
71
|
+
/** What an agent's own session has that the daemon must not: the daemon
|
|
72
|
+
* is the computer's, so it acts with the computer's sign-in only, even
|
|
73
|
+
* when an agent Station started is the one that starts it. */
|
|
74
|
+
const AGENT_ONLY = ["STATION_AGENT_TOKEN", "STATION_AGENT_TOKEN_FILE", "STATION_AGENT_TOKEN_UNTIL", "STATION_AGENT_NAME", "STATION_INSTALL_SECRET", "STATION_STARTED", "STATION_SESSION"];
|
|
75
|
+
export function daemonEnv(env = process.env) {
|
|
76
|
+
return Object.fromEntries(Object.entries(env).filter(([name]) => !AGENT_ONLY.includes(name)));
|
|
77
|
+
}
|
|
60
78
|
/** A running daemon, started now if none answers. One older than this CLI
|
|
61
79
|
* (an update, or npx fetching a new version) is replaced, so new messages
|
|
62
80
|
* always go through the newest code; the seats carry over. */
|
|
@@ -72,7 +90,7 @@ export async function ensureDaemon(home) {
|
|
|
72
90
|
}
|
|
73
91
|
const log = openSync(paths.log, "a", 0o600);
|
|
74
92
|
// With this process's own node flags (--import tsx, a preload), like the login helper.
|
|
75
|
-
const child = spawn(process.execPath, [...process.execArgv, process.argv[1], "daemon"], { detached: true, stdio: ["ignore", log, log] });
|
|
93
|
+
const child = spawn(process.execPath, [...process.execArgv, process.argv[1], "daemon"], { detached: true, stdio: ["ignore", log, log], env: daemonEnv() });
|
|
76
94
|
child.unref();
|
|
77
95
|
for (let i = 0; i < 50; i++) {
|
|
78
96
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
@@ -103,7 +121,10 @@ export function seatsOf(seats, sessionId) {
|
|
|
103
121
|
/** The join body for the session. client tells the relay a daemon holds
|
|
104
122
|
* the seat, so it is not put on the curl path's listening window. */
|
|
105
123
|
export function joinBody(name, session, client = "station-cli") {
|
|
106
|
-
|
|
124
|
+
// The program the session runs in, which says whose agent it is: a
|
|
125
|
+
// worker or a started session in Codex is Codex, a shell's is its own.
|
|
126
|
+
const kind = session.harness === "shell" ? session.host : session.harness;
|
|
127
|
+
return { name, cwd: session.cwd, git_branch: gitBranch(session.cwd), session_id: session.id, harness: session.harness, ...(kind ? { agent_kind: kind } : {}), client: `${client}/${VERSION}` };
|
|
107
128
|
}
|
|
108
129
|
/** What the CLI says when a private room has it ask to join. */
|
|
109
130
|
export const ASKED_TO_JOIN = "This room is private, so this session asked to join. Someone in the room has to let it in; waiting here until they do.";
|
|
@@ -119,9 +140,12 @@ export async function joinOrAsk(relayUrl, roomId, token, body, options = {}) {
|
|
|
119
140
|
// The room's invite token let it in: the next join takes the seat.
|
|
120
141
|
if (joined.status === "invited" && joined.key)
|
|
121
142
|
return call(relayUrl, `/${roomId}/join`, { token: joined.key }, { body });
|
|
122
|
-
if (joined.status !== "waiting"
|
|
143
|
+
if (joined.status !== "waiting")
|
|
144
|
+
return joined;
|
|
145
|
+
// A signed-in agent knocks with its own sign-in and gets no key: it asks again with that.
|
|
146
|
+
const key = joined.key ?? token;
|
|
147
|
+
if (!key)
|
|
123
148
|
return joined;
|
|
124
|
-
const key = joined.key;
|
|
125
149
|
options.onAsked?.(ASKED_TO_JOIN);
|
|
126
150
|
const until = Date.now() + (options.askForMs ?? ASK_FOR_MS);
|
|
127
151
|
while (joined.status === "waiting") {
|
|
@@ -131,7 +155,22 @@ export async function joinOrAsk(relayUrl, roomId, token, body, options = {}) {
|
|
|
131
155
|
}
|
|
132
156
|
return joined;
|
|
133
157
|
}
|
|
158
|
+
/** Takes a seat in the room for the session and hands it to the daemon,
|
|
159
|
+
* which brings the room's messages into the session from then on. */
|
|
134
160
|
export async function takeSeat(home, session, url, options = {}) {
|
|
161
|
+
const joined = await joinRoom(home, session, url, options);
|
|
162
|
+
if (readCredentials(home)?.agent) {
|
|
163
|
+
keepAgentSeat(files(home).seats, joined.seat);
|
|
164
|
+
return joined;
|
|
165
|
+
}
|
|
166
|
+
await ensureDaemon(home);
|
|
167
|
+
const answer = await ask(files(home).socket, { op: "add", seat: joined.seat });
|
|
168
|
+
if (!answer.ok)
|
|
169
|
+
throw new Error(answer.error);
|
|
170
|
+
return joined;
|
|
171
|
+
}
|
|
172
|
+
/** The join itself: the seat the room gave the session, not yet held by the daemon. */
|
|
173
|
+
export async function joinRoom(home, session, url, options = {}) {
|
|
135
174
|
// This computer's sign-in; else nothing. A public room takes nothing; a
|
|
136
175
|
// private one then has the agent ask to join, and it waits here until
|
|
137
176
|
// someone in the room lets it in.
|
|
@@ -151,7 +190,9 @@ export async function takeSeat(home, session, url, options = {}) {
|
|
|
151
190
|
const kept = loadSeats(files(home).seats).find((s) => s.sessionId === session.id && s.roomId === roomId && s.relayUrl === relayUrl);
|
|
152
191
|
// A named worker in a Claude Code or Codex session is called after it, so the room shows which agent it is.
|
|
153
192
|
const harness = session.harness === "shell" && session.host ? session.host : session.harness;
|
|
154
|
-
|
|
193
|
+
// An agent's own sign-in knows which agent it is (STATION_AGENT_NAME).
|
|
194
|
+
const who = credentials?.agent && credentials.name ? credentials.name : HARNESS_NAMES[harness];
|
|
195
|
+
const name = (options.name ?? kept?.name ?? `${who} · ${session.name || basename(session.cwd)}`).slice(0, 80);
|
|
155
196
|
const body = joinBody(name, session, options.client);
|
|
156
197
|
if (invite && !token)
|
|
157
198
|
body.invite_token = invite;
|
|
@@ -164,10 +205,14 @@ export async function takeSeat(home, session, url, options = {}) {
|
|
|
164
205
|
catch (error) {
|
|
165
206
|
if (!kept?.roomToken || !(error instanceof RelayError) || ![401, 403, 404].includes(error.status))
|
|
166
207
|
throw error;
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
208
|
+
if (options.forget)
|
|
209
|
+
await options.forget(session.id, roomId);
|
|
210
|
+
else {
|
|
211
|
+
await ensureDaemon(home);
|
|
212
|
+
const forgotten = await ask(files(home).socket, { op: "forget", sessionId: session.id, roomId });
|
|
213
|
+
if (!forgotten.ok)
|
|
214
|
+
throw new Error(forgotten.error);
|
|
215
|
+
}
|
|
171
216
|
joined = await joinOrAsk(relayUrl, roomId, token, body, options);
|
|
172
217
|
}
|
|
173
218
|
const participantId = joined.participant.id;
|
|
@@ -191,11 +236,9 @@ export async function takeSeat(home, session, url, options = {}) {
|
|
|
191
236
|
after: kept?.participantId === participantId ? kept.after : Math.max(0, ...joined.messages.map((m) => m.id)),
|
|
192
237
|
joinedAt: new Date().toISOString(),
|
|
193
238
|
seenAt: new Date().toISOString(),
|
|
239
|
+
...(joined.participant.agent_id ? { agentId: joined.participant.agent_id } : {}),
|
|
240
|
+
cwd: session.cwd,
|
|
194
241
|
};
|
|
195
|
-
await ensureDaemon(home);
|
|
196
|
-
const answer = await ask(files(home).socket, { op: "add", seat });
|
|
197
|
-
if (!answer.ok)
|
|
198
|
-
throw new Error(answer.error);
|
|
199
242
|
return { seat, room: joined };
|
|
200
243
|
}
|
|
201
244
|
/** The last `last` chat lines of a room, with who said each, each one
|
|
@@ -213,9 +256,12 @@ export function chatLines(seat, room, last, me) {
|
|
|
213
256
|
lines.unshift(`(${older} older message${older === 1 ? "" : "s"} not shown here. Read them with: ${me} read --after 0)`);
|
|
214
257
|
return { note: noteFor(hearer, authors), lines };
|
|
215
258
|
}
|
|
216
|
-
/** What `station join` says once seated. `me` is the command to type again.
|
|
217
|
-
|
|
259
|
+
/** What `station join` says once seated. `me` is the command to type again.
|
|
260
|
+
* `ask`: the person's agents the seat can ask, which need no seat here. */
|
|
261
|
+
export function joinedLines(me, { seat, room }, why = null, { ask = null } = {}) {
|
|
218
262
|
const lines = [`Joined ${roomLabel(seat)} as "${seat.name}".`, `People here: ${room.participants.filter((p) => !p.left_at).map((p) => p.name).join(", ")}`];
|
|
263
|
+
if (ask)
|
|
264
|
+
lines.push(ask);
|
|
219
265
|
const recent = chatLines(seat, room, 8, me);
|
|
220
266
|
if (recent.lines.length)
|
|
221
267
|
lines.push(["Recent messages:", ...(recent.note ? [recent.note] : []), ...recent.lines].join("\n"));
|
|
@@ -285,6 +331,9 @@ hand) {
|
|
|
285
331
|
let after = seat.after;
|
|
286
332
|
let text = null;
|
|
287
333
|
const tell = async () => {
|
|
334
|
+
// A seat an agent's own sign-in took: no daemon to tell.
|
|
335
|
+
if (seat.own)
|
|
336
|
+
return;
|
|
288
337
|
const told = await ask(files(home).socket, { op: "read", sessionId: seat.sessionId, roomId: seat.roomId, after }).catch(() => null);
|
|
289
338
|
if (told && !told.ok)
|
|
290
339
|
throw new Usage(told.error);
|
|
@@ -295,10 +344,11 @@ hand) {
|
|
|
295
344
|
const chat = answer.messages.filter((m) => m.kind === "chat" && m.participant_id !== seat.participantId);
|
|
296
345
|
const decisions = decisionsFor(seat, answer.messages);
|
|
297
346
|
const asked = askedIn(answer.messages);
|
|
347
|
+
const news = askNewsFor(seat, answer.messages);
|
|
298
348
|
after = Math.max(after, answer.next_after);
|
|
299
|
-
if (chat.length || decisions.length || asked.length) {
|
|
349
|
+
if (chat.length || decisions.length || asked.length || news.length) {
|
|
300
350
|
const { participants } = await call(seat.relayUrl, `/${seat.roomId}`, auth);
|
|
301
|
-
text = [decisions.length ? decisionFrame(seat, decisions, participants) : null, chat.length ? frame(seat, chat, participants, me) : null, asked.length ? askedFrame(seat, asked, me) : null]
|
|
351
|
+
text = [news.length ? askNewsFrame(seat, news) : null, decisions.length ? decisionFrame(seat, decisions, participants) : null, chat.length ? frame(seat, chat, participants, me) : null, asked.length ? askedFrame(seat, asked, me) : null]
|
|
302
352
|
.filter(Boolean)
|
|
303
353
|
.join("\n\n");
|
|
304
354
|
break;
|
|
@@ -314,6 +364,8 @@ hand) {
|
|
|
314
364
|
if (text && hand)
|
|
315
365
|
await hand(text);
|
|
316
366
|
await tell();
|
|
367
|
+
if (seat.own)
|
|
368
|
+
keepAgentSeat(files(home).seats, { ...seat, after });
|
|
317
369
|
if (text) {
|
|
318
370
|
// The board shows these as read under the newest message, as it does for a delivery.
|
|
319
371
|
await call(seat.relayUrl, `/${seat.roomId}/delivery`, auth, { body: { through: after, state: "delivered", waiting: null } }).catch(() => { });
|
|
@@ -385,6 +437,13 @@ export async function messagesIn(runs, page) {
|
|
|
385
437
|
* other read marks them, and how to answer. `otherRooms`: the session has
|
|
386
438
|
* seats in other rooms too, so each command it prints names this room;
|
|
387
439
|
* without it, a reply would go to the room the session joined last. */
|
|
440
|
+
/** How an ask stands with each agent it is to, as a read shows it. */
|
|
441
|
+
export function askHint(message) {
|
|
442
|
+
if (!message.asks?.length)
|
|
443
|
+
return "";
|
|
444
|
+
const words = { sent: "sent, not delivered yet", unreachable: "could not reach it", handed: "handed on" };
|
|
445
|
+
return ` [to ${message.asks.map((ask) => `${ask.name}: ${words[ask.state] ?? ask.state}${ask.why ? ` (${ask.why})` : ""}`).join("; ")}]`;
|
|
446
|
+
}
|
|
388
447
|
export function numberedText(seat, runs, read, participants, me, otherRooms = false) {
|
|
389
448
|
const room = otherRooms ? ` --room ${seat.roomId}` : "";
|
|
390
449
|
const hearer = hearerFor(seat, participants, me);
|
|
@@ -393,7 +452,7 @@ export function numberedText(seat, runs, read, participants, me, otherRooms = fa
|
|
|
393
452
|
const note = noteFor(hearer, authors);
|
|
394
453
|
const asked = runs.map((run) => (run.from === run.to ? String(run.from) : `${run.from}-${run.to}`)).join(", ");
|
|
395
454
|
const lines = [`[Station · ${roomLabel(seat)}] ${runs.length === 1 && runs[0].from === runs[0].to ? "Message" : "Messages"} ${asked}:`, "", ...(note ? [note, ""] : [])];
|
|
396
|
-
lines.push(...(read.messages.length ? read.messages.map((m, i) => lineOf(hearer, authors[i], m.body, whereOf(hearer, m, participants))) : ["(no chat message with that number in this room)"]), "");
|
|
455
|
+
lines.push(...(read.messages.length ? read.messages.map((m, i) => lineOf(hearer, authors[i], m.body, whereOf(hearer, m, participants) + askHint(m))) : ["(no chat message with that number in this room)"]), "");
|
|
397
456
|
if (read.more !== null) {
|
|
398
457
|
const rest = runs.flatMap((run) => (run.to < read.more ? [] : [{ from: Math.max(run.from, read.more), to: run.to }]));
|
|
399
458
|
lines.push(`There are more. Read on with: ${me} read ${rest.map((run) => (run.from === run.to ? String(run.from) : `${run.from}-${run.to}`)).join(",")}${room}`);
|
|
@@ -433,18 +492,66 @@ export function seatsNamed(wanted, participants) {
|
|
|
433
492
|
/** Says something in the room. `reply`: the thread to say it in. `to`:
|
|
434
493
|
* seat ids or names; only those agents are woken by it. The answer's
|
|
435
494
|
* thread, when it went in one. */
|
|
495
|
+
/** The session's seat for a reply: in the room of the message it answers,
|
|
496
|
+
* never the room it joined last, taking a seat there when it has none. */
|
|
497
|
+
export async function seatForReply(home, session, reply, room) {
|
|
498
|
+
if (room)
|
|
499
|
+
return mySeat(home, session, room);
|
|
500
|
+
const credentials = readCredentials(home);
|
|
501
|
+
if (!credentials)
|
|
502
|
+
return mySeat(home, session);
|
|
503
|
+
const found = await call(credentials.relayUrl, `/me/messages/${reply}`, { token: credentials.token });
|
|
504
|
+
return seatFor(loadSeats(files(home).seats), session.id, found.room.id) ?? (await takeSeat(home, session, found.room.url)).seat;
|
|
505
|
+
}
|
|
506
|
+
/** Says something as the seat. `to` takes seat ids, names of seats in the
|
|
507
|
+
* room, or the names of the person's agents (an agent with no session in
|
|
508
|
+
* the room yet: Codex, Claude Code). */
|
|
436
509
|
export async function sayIn(home, seat, text, options = {}) {
|
|
437
|
-
const
|
|
510
|
+
const credentials = readCredentials(home);
|
|
511
|
+
const auth = authFor(seat, credentials);
|
|
438
512
|
// An empty list is sent as it is: everyone, said outright.
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
513
|
+
let to = options.to;
|
|
514
|
+
if (to?.length && !to.every((want) => SEAT_ID.test(want))) {
|
|
515
|
+
const { participants } = await call(seat.relayUrl, `/${seat.roomId}`, auth);
|
|
516
|
+
const mine = seat.via === "cli" ? await myAgents(credentials) : null;
|
|
517
|
+
const agents = mine?.agents ?? [];
|
|
518
|
+
const names = askable(mine, seat.agentId);
|
|
519
|
+
to = to.map((want) => {
|
|
520
|
+
const agent = agents.find((one) => one.name.toLowerCase() === want.toLowerCase() || one.kind === want.toLowerCase());
|
|
521
|
+
try {
|
|
522
|
+
return seatsNamed([want], participants)[0];
|
|
523
|
+
}
|
|
524
|
+
catch (error) {
|
|
525
|
+
if (agent)
|
|
526
|
+
return agent.id;
|
|
527
|
+
if (error instanceof Usage && names.length)
|
|
528
|
+
throw new Usage(`${error.message} Your person's agents you can ask, seated here or not: ${names.join(", ")}.`);
|
|
529
|
+
throw error;
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
// One post, however often a lost answer makes it go again.
|
|
534
|
+
const body = { body: text, ...(options.reply ? { reply_to: options.reply } : {}), ...(to ? { to } : {}), client_id: randomUUID() };
|
|
535
|
+
const post = () => call(seat.relayUrl, `/${seat.roomId}/messages`, auth, { body });
|
|
536
|
+
const said = await post().catch((error) => {
|
|
537
|
+
if (error instanceof RelayError && error.status === 0)
|
|
538
|
+
return post();
|
|
539
|
+
throw error;
|
|
446
540
|
});
|
|
447
|
-
return said?.message?.reply_to ?? null;
|
|
541
|
+
return { thread: said?.message?.reply_to ?? null, message: said?.message?.id ?? null, asks: said?.message?.asks ?? [] };
|
|
542
|
+
}
|
|
543
|
+
/** What `station say` prints. `toNoOne`: a message with no --to and no
|
|
544
|
+
* --reply, or --to everyone, which wakes no agent when an agent says it (relay asks.ts,
|
|
545
|
+
* reaches); `names` are the person's agents it could have asked. */
|
|
546
|
+
export function saidLines(me, seat, said, toNoOne, names) {
|
|
547
|
+
const lines = [said.thread ? `Said in thread ${said.thread} in ${roomLabel(seat)}.` : `Said in ${roomLabel(seat)}${said.message ? ` as message ${said.message}` : ""}.`];
|
|
548
|
+
for (const ask of said.asks)
|
|
549
|
+
lines.push(`To ${ask.name}: ${ask.state === "unreachable" ? `Station could not reach it: ${ask.why ?? "no reason given"}` : ask.state === "out" ? `taken, but out of usage: ${ask.why ?? "no reset time known"}` : ask.state}.`);
|
|
550
|
+
if (said.asks.length && said.message)
|
|
551
|
+
lines.push(`See how it stands with: ${me} read ${said.message}`);
|
|
552
|
+
if (toNoOne)
|
|
553
|
+
lines.push(`It woke no agent: an agent's message wakes only the agents named in --to. ${askLine(names, me) ?? `Ask one with: ${me} say --to <name> "<what to do>"`}`);
|
|
554
|
+
return lines;
|
|
448
555
|
}
|
|
449
556
|
/** Give the room a name. The relay refuses when a person already named it. */
|
|
450
557
|
export async function nameRoom(home, seat, title) {
|
|
@@ -470,7 +577,9 @@ export async function putItem(home, seat, item) {
|
|
|
470
577
|
* person: a sign-in link for them. Whoever opens it, with an account or
|
|
471
578
|
* only their browser, signs this computer in, and the seat becomes theirs;
|
|
472
579
|
* later agents here join as theirs from the start. */
|
|
473
|
-
export async function ownerLines(home, seat, room, me,
|
|
580
|
+
export async function ownerLines(home, seat, room, me,
|
|
581
|
+
// Null: the relay's own site (siteOf), so a sandbox relay's link is its own.
|
|
582
|
+
webUrl = process.env.STATION_WEB_URL ?? null, start = startLogin, remote = elsewhere()) {
|
|
474
583
|
if (seat.via !== "room" || !seat.roomToken)
|
|
475
584
|
return [];
|
|
476
585
|
if (room.participants.find((one) => one.id === seat.participantId)?.for)
|