baychat 0.56.0 → 0.58.0
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/.claude-plugin/plugin.json +1 -1
- package/README.md +9 -1
- package/dist/approval-wait.js +1 -1
- package/dist/bay-gone.js +35 -0
- package/dist/bay-mcp.js +22 -8
- package/dist/channel/launch.js +10 -3
- package/dist/channel/mcp-server.js +37 -0
- package/dist/channel/server.js +6 -20
- package/dist/claude-command.js +130 -0
- package/dist/claude-register.js +162 -0
- package/dist/claude-user-config.js +215 -0
- package/dist/codex-onboarding.js +16 -2
- package/dist/commands.js +22 -187
- package/dist/connect-claude.js +26 -5
- package/dist/connect-link-budget.js +2 -0
- package/dist/connect-link-check.js +96 -0
- package/dist/connect-link-setup.js +17 -0
- package/dist/connect-link.js +41 -34
- package/dist/doctor-claude-file.js +17 -0
- package/dist/doctor-command.js +3 -0
- package/dist/doctor-questions.js +22 -0
- package/dist/doctor.js +13 -12
- package/dist/mcp-todos.js +49 -0
- package/dist/mcp.js +5 -5
- package/dist/notify-answer.js +53 -31
- package/dist/protocol-content.js +1 -1
- package/dist/quiet-claude-env.js +15 -0
- package/dist/refresh-clients.js +4 -6
- package/dist/relay/autostart-here.js +141 -0
- package/dist/relay/autostart.js +11 -60
- package/dist/relay/commands.js +9 -28
- package/dist/relay/launcher/blocking-screen.fixtures.js +147 -0
- package/dist/relay/launcher/blocking-screen.js +68 -0
- package/dist/relay/launcher/capabilities.js +8 -1
- package/dist/relay/launcher/dev-channel-screen.js +1 -1
- package/dist/relay/launcher/handle.js +3 -2
- package/dist/relay/launcher/loop.js +3 -0
- package/dist/relay/launcher/multiplexer.js +26 -0
- package/dist/relay/launcher/pane-watch.js +16 -0
- package/dist/relay/launcher/reset-time.js +82 -0
- package/dist/relay/launcher/session-watch-deps.js +116 -0
- package/dist/relay/launcher/session-watch.js +252 -0
- package/dist/relay/windows-task-xml.js +67 -0
- package/dist/relay/wsl-keeper.js +278 -0
- package/dist/runtimes.js +1 -3
- package/dist/tool-defs-outputs.js +73 -0
- package/dist/tool-defs.js +14 -24
- package/dist/tool-hints.js +13 -5
- package/dist/waiting-cards.js +33 -3
- package/package.json +2 -2
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.startSessionWatch = startSessionWatch;
|
|
4
|
+
exports.watchedSessions = watchedSessions;
|
|
5
|
+
exports.realSessionWatchDeps = realSessionWatchDeps;
|
|
6
|
+
const api_1 = require("../../api");
|
|
7
|
+
const approval_wait_1 = require("../../approval-wait");
|
|
8
|
+
const config_1 = require("../../config");
|
|
9
|
+
const terminal_pane_1 = require("../terminal-pane");
|
|
10
|
+
const waiting_cards_1 = require("../../waiting-cards");
|
|
11
|
+
const approval_settings_1 = require("./approval-settings");
|
|
12
|
+
const multiplexer_1 = require("./multiplexer");
|
|
13
|
+
const pane_watch_1 = require("./pane-watch");
|
|
14
|
+
const session_watch_1 = require("./session-watch");
|
|
15
|
+
const started_1 = require("./started");
|
|
16
|
+
const stop_registered_1 = require("./stop-registered");
|
|
17
|
+
const LABEL = "baychat relay (session watch)";
|
|
18
|
+
/**
|
|
19
|
+
* Start the session-long watch (session-watch.ts) beside the launcher. Never awaited and never
|
|
20
|
+
* fatal; it ends with `signal`.
|
|
21
|
+
*/
|
|
22
|
+
function startSessionWatch(auth, log, signal, known = {}) {
|
|
23
|
+
void (0, session_watch_1.watchSessions)(realSessionWatchDeps(auth, log, signal, known), signal);
|
|
24
|
+
}
|
|
25
|
+
/** The sessions to read: the launcher's Claude Code panes, then `baychat claude` panes registered in tmux. */
|
|
26
|
+
function watchedSessions(started, known) {
|
|
27
|
+
const own = started
|
|
28
|
+
.filter((s) => s.runtime === "claude")
|
|
29
|
+
.map((s) => ({ name: s.name, multiplexer: s.multiplexer, ...(s.bayId ? { bayId: s.bayId } : {}) }));
|
|
30
|
+
const names = new Set(own.map((s) => s.name));
|
|
31
|
+
for (const target of known.registeredSessions?.() ?? []) {
|
|
32
|
+
if (target.runtime !== "claude" || names.has(target.name))
|
|
33
|
+
continue;
|
|
34
|
+
const pane = known.registeredSession?.(target.name)?.pane;
|
|
35
|
+
if (pane?.kind === "tmux")
|
|
36
|
+
own.push({ name: target.name, multiplexer: "tmux", pane });
|
|
37
|
+
}
|
|
38
|
+
return own;
|
|
39
|
+
}
|
|
40
|
+
function tmuxOn(s, run, args) {
|
|
41
|
+
const res = run("tmux", ["-S", s.pane.socket, ...args]);
|
|
42
|
+
return res.error || res.status !== 0 ? null : res;
|
|
43
|
+
}
|
|
44
|
+
function realSessionWatchDeps(auth, log, signal, known = {}) {
|
|
45
|
+
const sleep = (ms) => new Promise((resolve) => {
|
|
46
|
+
if (signal.aborted)
|
|
47
|
+
return resolve();
|
|
48
|
+
const timer = setTimeout(done, ms);
|
|
49
|
+
function done() {
|
|
50
|
+
clearTimeout(timer);
|
|
51
|
+
signal.removeEventListener("abort", done);
|
|
52
|
+
resolve();
|
|
53
|
+
}
|
|
54
|
+
signal.addEventListener("abort", done, { once: true });
|
|
55
|
+
});
|
|
56
|
+
const waitDeps = { now: Date.now, sleep, log };
|
|
57
|
+
/** The login of the Bay the session is in; the relay's own when it is not known or not here. */
|
|
58
|
+
const authFor = (s) => {
|
|
59
|
+
const bay = s.bayId ? (0, config_1.loadBayLogins)().find((b) => b.bayId === s.bayId) : undefined;
|
|
60
|
+
return bay ? { baseUrl: auth.baseUrl, token: bay.token } : auth;
|
|
61
|
+
};
|
|
62
|
+
/** Which login a card was made with, so its waits and withdrawal go to the same Bay. */
|
|
63
|
+
const cardAuth = new Map();
|
|
64
|
+
const registered = (s) => known.registeredSession?.(s.name);
|
|
65
|
+
return {
|
|
66
|
+
sessions: () => watchedSessions((0, started_1.loadStarted)((0, started_1.startedRecordPath)((0, approval_settings_1.launchDir)())), known),
|
|
67
|
+
inStartWatch: pane_watch_1.startWatchActive,
|
|
68
|
+
capture: (s) => (s.pane ? (tmuxOn({ ...s, pane: s.pane }, multiplexer_1.realRun, ["capture-pane", "-p", "-J", "-t", s.pane.pane])?.stdout ?? null) : (0, multiplexer_1.capturePane)(s.multiplexer, s.name, multiplexer_1.realRun, { dir: (0, approval_settings_1.launchDir)() })),
|
|
69
|
+
press: (s, key) => {
|
|
70
|
+
if (!s.pane)
|
|
71
|
+
return (0, multiplexer_1.pressKey)(s.multiplexer, s.name, key, multiplexer_1.realRun);
|
|
72
|
+
// A pane BayChat did not make: only while it still runs this session's Claude Code.
|
|
73
|
+
if (!(0, terminal_pane_1.verifyPane)(s.pane, "claude", undefined, registered(s)?.ownerPid).ok)
|
|
74
|
+
return false;
|
|
75
|
+
return tmuxOn({ ...s, pane: s.pane }, multiplexer_1.realRun, ["send-keys", "-t", s.pane.pane, ...(key === "Escape" ? ["Escape"] : ["-l", "--", key])]) !== null;
|
|
76
|
+
},
|
|
77
|
+
typeLine: async (s, text) => (s.pane ? (await (0, terminal_pane_1.typeIntoPane)(s.pane, "claude", text, { ownerPid: registered(s)?.ownerPid })).ok : (0, multiplexer_1.typeLine)(s.multiplexer, s.name, text, multiplexer_1.realRun)),
|
|
78
|
+
stop: async (s) => (s.pane ? (await (0, stop_registered_1.stopRegistered)(s.name, registered(s), (0, stop_registered_1.realStopRegisteredDeps)(multiplexer_1.realRun, log))) === "stopped" : (0, multiplexer_1.killLaunchSession)(s.multiplexer, s.name, multiplexer_1.realRun)),
|
|
79
|
+
hookCardOpen: (name) => (0, waiting_cards_1.sessionHasWaitingCard)(name),
|
|
80
|
+
ask: async (s, q, deadline) => {
|
|
81
|
+
// A registered session names no Bay: the relay's login first, then each other Bay's, until
|
|
82
|
+
// one knows the session (a failed create leaves no card anywhere).
|
|
83
|
+
const logins = s.bayId ? [authFor(s)] : [auth, ...(0, config_1.loadBayLogins)().filter((b) => !b.isDefault).map((b) => ({ baseUrl: auth.baseUrl, token: b.token }))];
|
|
84
|
+
const first = await (0, approval_wait_1.askOwnerAndWait)(logins[0], q, deadline, waitDeps, LABEL);
|
|
85
|
+
let answer = first;
|
|
86
|
+
for (const login of logins.slice(1)) {
|
|
87
|
+
if (answer.kind !== "failed")
|
|
88
|
+
break;
|
|
89
|
+
answer = await (0, approval_wait_1.askOwnerAndWait)(login, q, deadline, waitDeps, LABEL);
|
|
90
|
+
if (answer.kind !== "failed" && "requestId" in answer)
|
|
91
|
+
cardAuth.set(answer.requestId, login);
|
|
92
|
+
}
|
|
93
|
+
// All failed: the session's own Bay's reason, not the last login tried (live run, 29 Nov:
|
|
94
|
+
// an expired second Bay's 401 hid why the first one said no).
|
|
95
|
+
return answer.kind === "failed" ? first : answer;
|
|
96
|
+
},
|
|
97
|
+
poll: (s, id, deadline) => (0, approval_wait_1.waitOnOwnerQuestion)(cardAuth.get(id) ?? authFor(s), id, deadline, waitDeps, LABEL),
|
|
98
|
+
withdraw: async (s, id) => {
|
|
99
|
+
await (0, approval_wait_1.withdrawOwnerQuestion)(cardAuth.get(id) ?? authFor(s), id, log);
|
|
100
|
+
cardAuth.delete(id);
|
|
101
|
+
},
|
|
102
|
+
notice: async (s, text) => {
|
|
103
|
+
try {
|
|
104
|
+
await (0, api_1.apiRequest)(authFor(s), "POST", "/api/device-api/notify", { text });
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
log(`session watch: the line to the owner could not be sent (${err instanceof Error ? err.message : String(err)}); trying again in a minute`);
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
now: Date.now,
|
|
113
|
+
sleep,
|
|
114
|
+
log,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.newWatchState = exports.RETRY_MS = exports.WAKE_LINE = exports.WAKE_AFTER_RESET_MS = exports.STABLE_MS = exports.WATCH_POLL_MS = void 0;
|
|
4
|
+
exports.tick = tick;
|
|
5
|
+
exports.watchSessions = watchSessions;
|
|
6
|
+
const notify_answer_1 = require("../../notify-answer");
|
|
7
|
+
const blocking_screen_1 = require("./blocking-screen");
|
|
8
|
+
const reset_time_1 = require("./reset-time");
|
|
9
|
+
/**
|
|
10
|
+
* The session-long watch: no question may stop a session BayChat started (Karmen, 28 Nov: "no
|
|
11
|
+
* question can block sessions"). pane-watch.ts covers a launch's first minute; from then on nothing
|
|
12
|
+
* read the pane, so Rocky sat at the rating survey and Windows_claude at a question nobody saw.
|
|
13
|
+
*
|
|
14
|
+
* Every launched Claude Code pane (started.json), and every `baychat claude` session that registered
|
|
15
|
+
* with the relay from a tmux pane (Rocky's `claude-570092` was one), is read every WATCH_POLL_MS.
|
|
16
|
+
* A plain `claude` outside tmux has no pane to read: `baychat doctor` says so. A screen that stays the
|
|
17
|
+
* same for STABLE_MS is dealt with ONCE (blocking-screen.ts names it):
|
|
18
|
+
* • the rating survey: "0" (Dismiss), by itself, logged — only over an empty prompt. Never a card.
|
|
19
|
+
* • folder trust: one line to the owner. Never answered from the network (launch/claude-trust.ts).
|
|
20
|
+
* • a numbered dialog: its own question and options on a card in the session's private chat; the
|
|
21
|
+
* tap presses that ONE digit (notify-answer.ts, the same rule: two reads a settle apart must
|
|
22
|
+
* still show the same question). Skipped while a hook's card for this session is up
|
|
23
|
+
* (waiting-cards.ts), so a permission prompt never gets two cards.
|
|
24
|
+
* • the usage limit: its menu is a dialog card that says when it resets. Waiting with Claude Code's
|
|
25
|
+
* own automatic continue on: one line. With it off: one line, and the session is woken with
|
|
26
|
+
* WAKE_LINE a minute after the reset, only if it is still idle then.
|
|
27
|
+
* • a screen nobody here knows: a card with NO text from the screen, "Press Esc" (the "no" key) or
|
|
28
|
+
* "Stop the session". Never Enter. A start question (channel warning, Code Mode) seen after the
|
|
29
|
+
* start watch: Stop only, since Esc there ends Claude Code.
|
|
30
|
+
* A session whose start pane-watch.ts is still watching is left alone.
|
|
31
|
+
*/
|
|
32
|
+
/** How often each pane is read. */
|
|
33
|
+
exports.WATCH_POLL_MS = 5_000;
|
|
34
|
+
/** How long a screen must stay the same before anything is done about it. */
|
|
35
|
+
exports.STABLE_MS = 15_000;
|
|
36
|
+
/** After a usage limit resets, the wait before the wake-up line (Claude Code may still be catching up). */
|
|
37
|
+
exports.WAKE_AFTER_RESET_MS = 60_000;
|
|
38
|
+
/** The one line typed to wake a session after its usage limit reset. Ours; never text from the network. */
|
|
39
|
+
exports.WAKE_LINE = "Your usage limit has reset. Carry on with the task you were working on, without repeating finished work.";
|
|
40
|
+
/** The same hour a terminal question's card waits (notify-answer.ts). */
|
|
41
|
+
const CARD_DEADLINE_MS = 60 * 60_000;
|
|
42
|
+
const WINDOW_MS = 20_000;
|
|
43
|
+
const GAP_MS = 1_000;
|
|
44
|
+
/** A card or line that could not be sent is tried again after this (a deploy's 503s pass; a dead
|
|
45
|
+
* login does not spam). Review, 29 Nov: a failed card left the screen "acted on" for good. */
|
|
46
|
+
exports.RETRY_MS = 60_000;
|
|
47
|
+
/** After a wait that failed (an API restart), the next wait starts this much later. */
|
|
48
|
+
const POLL_RETRY_MS = 15_000;
|
|
49
|
+
const newWatchState = () => ({ panes: new Map() });
|
|
50
|
+
exports.newWatchState = newWatchState;
|
|
51
|
+
/** What makes two looks "the same screen". An unknown one by its whole text: nothing else names it. */
|
|
52
|
+
function screenKey(screen, text) {
|
|
53
|
+
if (screen.kind === "unknown-picker" || screen.kind === "start-question")
|
|
54
|
+
return `${screen.kind}:${text}`;
|
|
55
|
+
return JSON.stringify(screen);
|
|
56
|
+
}
|
|
57
|
+
/** Idle at its prompt: nothing typed, nothing running, nothing asked. */
|
|
58
|
+
function idle(text) {
|
|
59
|
+
const screen = (0, blocking_screen_1.readBlockingScreen)(text);
|
|
60
|
+
const quiet = screen.kind === "none" || (screen.kind === "usage-limit" && !screen.dialog);
|
|
61
|
+
return quiet && (0, blocking_screen_1.promptEmpty)(text) && !/esc to interrupt/i.test(text);
|
|
62
|
+
}
|
|
63
|
+
/** One look at every pane. Returns what it started (a card's wait runs on while the watch reads on). */
|
|
64
|
+
async function tick(deps, state) {
|
|
65
|
+
const started = [];
|
|
66
|
+
const sessions = deps.sessions();
|
|
67
|
+
for (const name of [...state.panes.keys()])
|
|
68
|
+
if (!sessions.some((s) => s.name === name))
|
|
69
|
+
state.panes.delete(name);
|
|
70
|
+
for (const s of sessions) {
|
|
71
|
+
if (deps.inStartWatch(s.name)) {
|
|
72
|
+
state.panes.delete(s.name);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const st = state.panes.get(s.name) ?? { key: "", since: deps.now(), busy: false };
|
|
76
|
+
state.panes.set(s.name, st);
|
|
77
|
+
if (st.busy)
|
|
78
|
+
continue;
|
|
79
|
+
const text = deps.capture(s);
|
|
80
|
+
if (text === null)
|
|
81
|
+
continue;
|
|
82
|
+
if (st.wakeAt !== undefined && deps.now() >= st.wakeAt) {
|
|
83
|
+
st.wakeAt = undefined;
|
|
84
|
+
if (!idle(text))
|
|
85
|
+
deps.log(`session watch: ${s.name} is busy again after its usage limit; not woken.`);
|
|
86
|
+
else if (await deps.typeLine(s, exports.WAKE_LINE))
|
|
87
|
+
deps.log(`session watch: ${s.name}'s usage limit reset; woke it with one line.`);
|
|
88
|
+
else
|
|
89
|
+
deps.log(`session watch: ${s.name}'s usage limit reset, but the wake-up line could not be typed.`);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const screen = (0, blocking_screen_1.readBlockingScreen)(text);
|
|
93
|
+
const key = screenKey(screen, text);
|
|
94
|
+
if (key !== st.key) {
|
|
95
|
+
Object.assign(st, { key, since: deps.now(), acted: undefined, retryAt: undefined });
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (screen.kind === "none" || st.acted === key || deps.now() - st.since < exports.STABLE_MS)
|
|
99
|
+
continue;
|
|
100
|
+
if (st.retryAt !== undefined && deps.now() < st.retryAt)
|
|
101
|
+
continue;
|
|
102
|
+
if (screen.kind === "dialog" && deps.hookCardOpen(s.name))
|
|
103
|
+
continue;
|
|
104
|
+
st.acted = key;
|
|
105
|
+
st.busy = true;
|
|
106
|
+
started.push(act(s, screen, text, st, deps)
|
|
107
|
+
.then((sent) => {
|
|
108
|
+
if (sent !== false)
|
|
109
|
+
return;
|
|
110
|
+
// Nothing reached the owner: the same screen is tried again after RETRY_MS.
|
|
111
|
+
st.acted = undefined;
|
|
112
|
+
st.retryAt = deps.now() + exports.RETRY_MS;
|
|
113
|
+
})
|
|
114
|
+
.catch((err) => deps.log(`session watch: ${s.name}: ${err instanceof Error ? err.message : String(err)}`))
|
|
115
|
+
.finally(() => {
|
|
116
|
+
st.busy = false;
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
return started;
|
|
120
|
+
}
|
|
121
|
+
/** Read every pane until `signal` aborts. Never throws. */
|
|
122
|
+
async function watchSessions(deps, signal) {
|
|
123
|
+
const state = (0, exports.newWatchState)();
|
|
124
|
+
while (!signal.aborted) {
|
|
125
|
+
try {
|
|
126
|
+
await tick(deps, state);
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
deps.log(`session watch: ${err instanceof Error ? err.message : String(err)}`);
|
|
130
|
+
}
|
|
131
|
+
await deps.sleep(exports.WATCH_POLL_MS);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** Deal with one screen. false: nothing reached the owner (the card or the line failed), so try again. */
|
|
135
|
+
async function act(s, screen, text, st, deps) {
|
|
136
|
+
const { name } = s;
|
|
137
|
+
switch (screen.kind) {
|
|
138
|
+
case "survey":
|
|
139
|
+
return dismissSurvey(s, text, deps);
|
|
140
|
+
case "folder-trust":
|
|
141
|
+
if (!(await deps.notice(s, `${name} waits for you at the computer: Claude Code asks whether to trust its folder. Answer it there; BayChat never answers it for you.`)))
|
|
142
|
+
return false;
|
|
143
|
+
deps.log(`session watch: ${name}: at folder trust; its owner was told (nothing pressed).`);
|
|
144
|
+
return;
|
|
145
|
+
case "start-question":
|
|
146
|
+
return askUnknown(s, text, true, deps);
|
|
147
|
+
case "unknown-picker":
|
|
148
|
+
return askUnknown(s, text, false, deps);
|
|
149
|
+
case "dialog": {
|
|
150
|
+
const outcome = await (0, notify_answer_1.answerFromPhone)({ session: name, dialog: screen.dialog }, answerDeps(s, deps));
|
|
151
|
+
deps.log(`session watch: ${name}: ${outcome}`);
|
|
152
|
+
return !outcome.startsWith(notify_answer_1.NOT_ASKED);
|
|
153
|
+
}
|
|
154
|
+
case "usage-limit":
|
|
155
|
+
return usageLimit(s, screen, st, deps);
|
|
156
|
+
default:
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async function dismissSurvey(s, text, deps) {
|
|
161
|
+
if ((0, blocking_screen_1.surveyDismissable)(text)) {
|
|
162
|
+
await deps.sleep(notify_answer_1.SETTLE_MS);
|
|
163
|
+
const again = deps.capture(s);
|
|
164
|
+
if (again !== null && (0, blocking_screen_1.surveyDismissable)(again) && deps.press(s, "0")) {
|
|
165
|
+
deps.log(`session watch: ${s.name}: dismissed Claude Code's rating survey (pressed 0).`);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
deps.log(`session watch: ${s.name}: Claude Code's rating survey is showing, but not over an empty prompt; nothing pressed.`);
|
|
170
|
+
}
|
|
171
|
+
async function usageLimit(s, screen, st, deps) {
|
|
172
|
+
const { name } = s;
|
|
173
|
+
const when = screen.resets ? ` (it resets ${screen.resets})` : "";
|
|
174
|
+
if (screen.dialog) {
|
|
175
|
+
const outcome = await (0, notify_answer_1.answerFromPhone)({ session: name, dialog: screen.dialog, intro: `${name} reached its usage limit${when}:` }, answerDeps(s, deps));
|
|
176
|
+
deps.log(`session watch: ${name}: usage-limit menu: ${outcome}`);
|
|
177
|
+
return !outcome.startsWith(notify_answer_1.NOT_ASKED);
|
|
178
|
+
}
|
|
179
|
+
if (screen.autoContinue)
|
|
180
|
+
return deps.notice(s, `${name} reached its usage limit. Claude Code continues by itself${screen.resets ? ` at ${screen.resets}` : ""}.`);
|
|
181
|
+
const at = screen.resets ? (0, reset_time_1.parseResetAt)(screen.resets, deps.now()) : undefined;
|
|
182
|
+
if (at === undefined)
|
|
183
|
+
return deps.notice(s, `${name} reached its usage limit${when}. Send it a message after the reset to carry on.`);
|
|
184
|
+
st.wakeAt = at + exports.WAKE_AFTER_RESET_MS;
|
|
185
|
+
return deps.notice(s, `${name} reached its usage limit${when}. BayChat wakes it then.`);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* A screen nobody here knows (or a start question past its watch): a card with no text from the
|
|
189
|
+
* screen. Esc only on the very screen the card was for, read twice a settle apart.
|
|
190
|
+
*/
|
|
191
|
+
async function askUnknown(s, text, startQuestion, deps) {
|
|
192
|
+
const { name } = s;
|
|
193
|
+
const options = startQuestion ? ["Stop the session"] : ["Press Esc", "Stop the session"];
|
|
194
|
+
const question = startQuestion
|
|
195
|
+
? `${name} waits at one of Claude Code's start questions in its terminal. BayChat can't answer it from here; you can stop the session.`
|
|
196
|
+
: `${name} is stopped at a screen in its terminal that BayChat doesn't know. Nothing from that screen is shown here.`;
|
|
197
|
+
const same = () => deps.capture(s) === text;
|
|
198
|
+
const until = deps.now() + CARD_DEADLINE_MS;
|
|
199
|
+
let outcome = await deps.ask(s, { session: name, question, options }, Math.min(until, deps.now() + WINDOW_MS));
|
|
200
|
+
// Say whether the card went out at all (live run, 29 Nov: a card made during a deploy left no
|
|
201
|
+
// trace either way).
|
|
202
|
+
if (outcome.kind === "failed") {
|
|
203
|
+
deps.log(`session watch: ${name}: the card for an unknown screen could not be sent (${outcome.reason}); trying again in a minute.`);
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
deps.log(`session watch: ${name}: a card for an unknown screen went out (${options.join(" / ")}).`);
|
|
207
|
+
const { requestId } = outcome;
|
|
208
|
+
for (;;) {
|
|
209
|
+
if (outcome.kind === "answered") {
|
|
210
|
+
const choice = outcome.index === null ? undefined : options[outcome.index];
|
|
211
|
+
if (choice === "Stop the session") {
|
|
212
|
+
deps.log(`session watch: ${name}: ${(await deps.stop(s)) ? "stopped by its owner" : "the stop failed"}; nothing was pressed.`);
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
if (choice !== "Press Esc")
|
|
216
|
+
return true;
|
|
217
|
+
const first = same();
|
|
218
|
+
if (first)
|
|
219
|
+
await deps.sleep(notify_answer_1.SETTLE_MS);
|
|
220
|
+
if (!first || !same()) {
|
|
221
|
+
await deps.notice(s, `${name}: Esc was not sent. The screen changed before your answer arrived.`);
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
deps.log(`session watch: ${name}: ${deps.press(s, "Escape") ? "pressed Esc, as its owner chose" : "Esc could not be sent"}.`);
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
// A wait that failed (an API restart) is not the end: the card is still on her phone.
|
|
228
|
+
if (outcome.kind !== "deadline" && outcome.kind !== "failed") {
|
|
229
|
+
deps.log(`session watch: ${name}: the card ended (${outcome.kind}).`);
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
if (!same() || deps.now() >= until) {
|
|
233
|
+
await deps.withdraw(s, requestId);
|
|
234
|
+
deps.log(`session watch: ${name}: ${deps.now() >= until ? "no answer in time" : "the screen changed"}; the card was taken back.`);
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
await deps.sleep(outcome.kind === "failed" ? POLL_RETRY_MS : GAP_MS);
|
|
238
|
+
outcome = await deps.poll(s, requestId, Math.min(until, deps.now() + WINDOW_MS));
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function answerDeps(s, deps) {
|
|
242
|
+
return {
|
|
243
|
+
capture: async () => deps.capture(s) ?? "",
|
|
244
|
+
press: async (key) => (/^[1-9]$/.test(key) && deps.press(s, key) ? { ok: true } : { ok: false, reason: `"${key}" could not be sent` }),
|
|
245
|
+
ask: (q, deadline) => deps.ask(s, { ...q, session: s.name }, deadline),
|
|
246
|
+
poll: (id, deadline) => deps.poll(s, id, deadline),
|
|
247
|
+
withdraw: (id) => deps.withdraw(s, id),
|
|
248
|
+
notice: async (text) => void (await deps.notice(s, text)),
|
|
249
|
+
now: () => deps.now(),
|
|
250
|
+
sleep: (ms) => deps.sleep(ms),
|
|
251
|
+
};
|
|
252
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.windowsTaskXml = windowsTaskXml;
|
|
4
|
+
exports.escapeXml = escapeXml;
|
|
5
|
+
/**
|
|
6
|
+
* The task document, shared by the native Windows relay and the WSL keeper (./wsl-keeper.ts).
|
|
7
|
+
* RestartOnFailure and `ExecutionTimeLimit` PT0S are what the `/sc onlogon` flags cannot say:
|
|
8
|
+
* without them the relay would stay dead after a crash, and stop by itself every third day.
|
|
9
|
+
*/
|
|
10
|
+
function windowsTaskXml(task) {
|
|
11
|
+
return `<?xml version="1.0" encoding="UTF-16"?>
|
|
12
|
+
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
|
|
13
|
+
<RegistrationInfo>
|
|
14
|
+
<Description>${escapeXml(task.description)}</Description>
|
|
15
|
+
</RegistrationInfo>
|
|
16
|
+
<Triggers>
|
|
17
|
+
<LogonTrigger>
|
|
18
|
+
<Enabled>true</Enabled>
|
|
19
|
+
<UserId>${escapeXml(task.userId)}</UserId>
|
|
20
|
+
</LogonTrigger>
|
|
21
|
+
</Triggers>
|
|
22
|
+
<Principals>
|
|
23
|
+
<Principal id="Author">
|
|
24
|
+
<UserId>${escapeXml(task.userId)}</UserId>
|
|
25
|
+
<LogonType>InteractiveToken</LogonType>
|
|
26
|
+
<RunLevel>LeastPrivilege</RunLevel>
|
|
27
|
+
</Principal>
|
|
28
|
+
</Principals>
|
|
29
|
+
<Settings>
|
|
30
|
+
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
|
|
31
|
+
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
|
|
32
|
+
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
|
|
33
|
+
<StartWhenAvailable>true</StartWhenAvailable>
|
|
34
|
+
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
|
|
35
|
+
<IdleSettings>
|
|
36
|
+
<StopOnIdleEnd>false</StopOnIdleEnd>
|
|
37
|
+
<RestartOnIdle>false</RestartOnIdle>
|
|
38
|
+
</IdleSettings>
|
|
39
|
+
<AllowHardTerminate>true</AllowHardTerminate>
|
|
40
|
+
<Enabled>true</Enabled>
|
|
41
|
+
<Hidden>true</Hidden>
|
|
42
|
+
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
|
|
43
|
+
<RestartOnFailure>
|
|
44
|
+
<Interval>PT1M</Interval>
|
|
45
|
+
<Count>999</Count>
|
|
46
|
+
</RestartOnFailure>
|
|
47
|
+
</Settings>
|
|
48
|
+
<Actions Context="Author">
|
|
49
|
+
<Exec>
|
|
50
|
+
<Command>${escapeXml(task.command)}</Command>
|
|
51
|
+
<Arguments>${escapeXml(task.args)}</Arguments>
|
|
52
|
+
</Exec>
|
|
53
|
+
</Actions>
|
|
54
|
+
</Task>
|
|
55
|
+
`;
|
|
56
|
+
}
|
|
57
|
+
/** The five characters that would otherwise end the element early. Paths can
|
|
58
|
+
* legally contain `&` on every platform this ships to, and a home directory with
|
|
59
|
+
* one in it would produce a plist launchd rejects with no useful message. */
|
|
60
|
+
function escapeXml(value) {
|
|
61
|
+
return value
|
|
62
|
+
.replace(/&/g, "&")
|
|
63
|
+
.replace(/</g, "<")
|
|
64
|
+
.replace(/>/g, ">")
|
|
65
|
+
.replace(/"/g, """)
|
|
66
|
+
.replace(/'/g, "'");
|
|
67
|
+
}
|