zelio-cli 0.0.0-stage → 0.1.0-beta.10
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 +71 -2
- package/dist/attachments.js +121 -0
- package/dist/attachments.js.map +1 -0
- package/dist/cli-main.js +521 -0
- package/dist/cli-main.js.map +1 -0
- package/dist/cli.js +22 -0
- package/dist/cli.js.map +1 -0
- package/dist/cloud/account.js +109 -0
- package/dist/cloud/account.js.map +1 -0
- package/dist/cloud/board.js +311 -0
- package/dist/cloud/board.js.map +1 -0
- package/dist/cloud/command.js +265 -0
- package/dist/cloud/command.js.map +1 -0
- package/dist/cloud/config.js +132 -0
- package/dist/cloud/config.js.map +1 -0
- package/dist/cloud/credentials.js +134 -0
- package/dist/cloud/credentials.js.map +1 -0
- package/dist/cloud/login.js +166 -0
- package/dist/cloud/login.js.map +1 -0
- package/dist/cloud/pair.js +110 -0
- package/dist/cloud/pair.js.map +1 -0
- package/dist/cloud/pkce.js +16 -0
- package/dist/cloud/pkce.js.map +1 -0
- package/dist/cloud/push-nudge.js +65 -0
- package/dist/cloud/push-nudge.js.map +1 -0
- package/dist/cloud/relay.js +134 -0
- package/dist/cloud/relay.js.map +1 -0
- package/dist/cloud/token.js +66 -0
- package/dist/cloud/token.js.map +1 -0
- package/dist/cloud/tunnel.js +474 -0
- package/dist/cloud/tunnel.js.map +1 -0
- package/dist/db/db.js +1838 -0
- package/dist/db/db.js.map +1 -0
- package/dist/db/lifecycle.js +146 -0
- package/dist/db/lifecycle.js.map +1 -0
- package/dist/limits.js +35 -0
- package/dist/limits.js.map +1 -0
- package/dist/metrics.js +30 -0
- package/dist/metrics.js.map +1 -0
- package/dist/orchestrator/area-notes.js +87 -0
- package/dist/orchestrator/area-notes.js.map +1 -0
- package/dist/orchestrator/claude-session.js +208 -0
- package/dist/orchestrator/claude-session.js.map +1 -0
- package/dist/orchestrator/codex-session.js +327 -0
- package/dist/orchestrator/codex-session.js.map +1 -0
- package/dist/orchestrator/contract.js +189 -0
- package/dist/orchestrator/contract.js.map +1 -0
- package/dist/orchestrator/explore.js +176 -0
- package/dist/orchestrator/explore.js.map +1 -0
- package/dist/orchestrator/new-task-prompt.js +51 -0
- package/dist/orchestrator/new-task-prompt.js.map +1 -0
- package/dist/orchestrator/pi-session.js +234 -0
- package/dist/orchestrator/pi-session.js.map +1 -0
- package/dist/orchestrator/prompt.js +110 -0
- package/dist/orchestrator/prompt.js.map +1 -0
- package/dist/orchestrator/research.js +193 -0
- package/dist/orchestrator/research.js.map +1 -0
- package/dist/orchestrator/retrieval.js +333 -0
- package/dist/orchestrator/retrieval.js.map +1 -0
- package/dist/orchestrator/runtime.js +1217 -0
- package/dist/orchestrator/runtime.js.map +1 -0
- package/dist/orchestrator/session.js +2 -0
- package/dist/orchestrator/session.js.map +1 -0
- package/dist/orchestrator/types.js +2 -0
- package/dist/orchestrator/types.js.map +1 -0
- package/dist/orchestrator/web.js +276 -0
- package/dist/orchestrator/web.js.map +1 -0
- package/dist/repo/code-map.js +152 -0
- package/dist/repo/code-map.js.map +1 -0
- package/dist/repo/index.js +188 -0
- package/dist/repo/index.js.map +1 -0
- package/dist/repo/instructions.js +58 -0
- package/dist/repo/instructions.js.map +1 -0
- package/dist/repo/linked-boards.js +149 -0
- package/dist/repo/linked-boards.js.map +1 -0
- package/dist/repo/project-repos.js +78 -0
- package/dist/repo/project-repos.js.map +1 -0
- package/dist/run/actions.js +257 -0
- package/dist/run/actions.js.map +1 -0
- package/dist/run/auto-review.js +116 -0
- package/dist/run/auto-review.js.map +1 -0
- package/dist/run/bash-guard.js +183 -0
- package/dist/run/bash-guard.js.map +1 -0
- package/dist/run/claude-runtime.js +284 -0
- package/dist/run/claude-runtime.js.map +1 -0
- package/dist/run/claude-stream.js +306 -0
- package/dist/run/claude-stream.js.map +1 -0
- package/dist/run/codex-client.js +213 -0
- package/dist/run/codex-client.js.map +1 -0
- package/dist/run/codex-planner-config.js +26 -0
- package/dist/run/codex-planner-config.js.map +1 -0
- package/dist/run/codex-runtime.js +469 -0
- package/dist/run/codex-runtime.js.map +1 -0
- package/dist/run/consult.js +109 -0
- package/dist/run/consult.js.map +1 -0
- package/dist/run/context.js +87 -0
- package/dist/run/context.js.map +1 -0
- package/dist/run/developer-sandbox.js +67 -0
- package/dist/run/developer-sandbox.js.map +1 -0
- package/dist/run/diff.js +238 -0
- package/dist/run/diff.js.map +1 -0
- package/dist/run/evidence.js +48 -0
- package/dist/run/evidence.js.map +1 -0
- package/dist/run/finish.js +430 -0
- package/dist/run/finish.js.map +1 -0
- package/dist/run/handoff.js +51 -0
- package/dist/run/handoff.js.map +1 -0
- package/dist/run/harness-runtime.js +31 -0
- package/dist/run/harness-runtime.js.map +1 -0
- package/dist/run/harnesses.js +293 -0
- package/dist/run/harnesses.js.map +1 -0
- package/dist/run/issue.js +79 -0
- package/dist/run/issue.js.map +1 -0
- package/dist/run/keep-awake.js +58 -0
- package/dist/run/keep-awake.js.map +1 -0
- package/dist/run/lead.js +183 -0
- package/dist/run/lead.js.map +1 -0
- package/dist/run/manager.js +2737 -0
- package/dist/run/manager.js.map +1 -0
- package/dist/run/mentions.js +35 -0
- package/dist/run/mentions.js.map +1 -0
- package/dist/run/merge-watch.js +425 -0
- package/dist/run/merge-watch.js.map +1 -0
- package/dist/run/models.js +88 -0
- package/dist/run/models.js.map +1 -0
- package/dist/run/phases.js +23 -0
- package/dist/run/phases.js.map +1 -0
- package/dist/run/pi-packages.js +72 -0
- package/dist/run/pi-packages.js.map +1 -0
- package/dist/run/pi-rpc.js +464 -0
- package/dist/run/pi-rpc.js.map +1 -0
- package/dist/run/piped-check.js +56 -0
- package/dist/run/piped-check.js.map +1 -0
- package/dist/run/pr-checks.js +67 -0
- package/dist/run/pr-checks.js.map +1 -0
- package/dist/run/pr-merge.js +121 -0
- package/dist/run/pr-merge.js.map +1 -0
- package/dist/run/profiles.js +43 -0
- package/dist/run/profiles.js.map +1 -0
- package/dist/run/prompt.js +405 -0
- package/dist/run/prompt.js.map +1 -0
- package/dist/run/pull-request.js +23 -0
- package/dist/run/pull-request.js.map +1 -0
- package/dist/run/red-check.js +130 -0
- package/dist/run/red-check.js.map +1 -0
- package/dist/run/result-tool.js +93 -0
- package/dist/run/result-tool.js.map +1 -0
- package/dist/run/result.js +113 -0
- package/dist/run/result.js.map +1 -0
- package/dist/run/retro.js +346 -0
- package/dist/run/retro.js.map +1 -0
- package/dist/run/settings.js +28 -0
- package/dist/run/settings.js.map +1 -0
- package/dist/run/skills.js +146 -0
- package/dist/run/skills.js.map +1 -0
- package/dist/run/tests-first.js +146 -0
- package/dist/run/tests-first.js.map +1 -0
- package/dist/run/thinking.js +41 -0
- package/dist/run/thinking.js.map +1 -0
- package/dist/run/types.js +2 -0
- package/dist/run/types.js.map +1 -0
- package/dist/run/watchdog.js +266 -0
- package/dist/run/watchdog.js.map +1 -0
- package/dist/run/workflow.js +277 -0
- package/dist/run/workflow.js.map +1 -0
- package/dist/run/worktree.js +354 -0
- package/dist/run/worktree.js.map +1 -0
- package/dist/server/app.js +3286 -0
- package/dist/server/app.js.map +1 -0
- package/dist/server/auth.js +93 -0
- package/dist/server/auth.js.map +1 -0
- package/dist/server/demo.js +96 -0
- package/dist/server/demo.js.map +1 -0
- package/dist/server/idempotency.js +104 -0
- package/dist/server/idempotency.js.map +1 -0
- package/dist/server/port.js +36 -0
- package/dist/server/port.js.map +1 -0
- package/dist/server/principal.js +93 -0
- package/dist/server/principal.js.map +1 -0
- package/dist/server/provider-auth.js +206 -0
- package/dist/server/provider-auth.js.map +1 -0
- package/dist/server/repo-picker.js +18 -0
- package/dist/server/repo-picker.js.map +1 -0
- package/dist/server/terminal.js +285 -0
- package/dist/server/terminal.js.map +1 -0
- package/dist/setup/components.js +98 -0
- package/dist/setup/components.js.map +1 -0
- package/dist/setup/config.js +71 -0
- package/dist/setup/config.js.map +1 -0
- package/dist/setup/ink-ui.js +189 -0
- package/dist/setup/ink-ui.js.map +1 -0
- package/dist/setup/onboarding.js +157 -0
- package/dist/setup/onboarding.js.map +1 -0
- package/dist/setup/run.js +69 -0
- package/dist/setup/run.js.map +1 -0
- package/dist/skills.js +151 -0
- package/dist/skills.js.map +1 -0
- package/dist/ticket-cli.js +157 -0
- package/dist/ticket-cli.js.map +1 -0
- package/dist/version.js +13 -0
- package/dist/version.js.map +1 -0
- package/dist/voice/index.js +69 -0
- package/dist/voice/index.js.map +1 -0
- package/dist/voice/moonshine.js +247 -0
- package/dist/voice/moonshine.js.map +1 -0
- package/dist/voice/wav.js +54 -0
- package/dist/voice/wav.js.map +1 -0
- package/dist/voice/whisper.js +491 -0
- package/dist/voice/whisper.js.map +1 -0
- package/dist/web/assets/index-TN5sUF2z.css +1 -0
- package/dist/web/assets/index-vB3Cucuc.js +258 -0
- package/dist/web/icons/apple-touch-icon.png +0 -0
- package/dist/web/icons/icon-192.png +0 -0
- package/dist/web/icons/icon-512.png +0 -0
- package/dist/web/icons/maskable-512.png +0 -0
- package/dist/web/index.html +21 -0
- package/dist/web/manifest.webmanifest +30 -0
- package/dist/web/sw.js +185 -0
- package/media/board.png +0 -0
- package/media/mobile.png +0 -0
- package/media/new-task.png +0 -0
- package/media/review.png +0 -0
- package/package.json +105 -4
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createServer } from "node:net";
|
|
2
|
+
/** How many ports past the default a second or third board may take. */
|
|
3
|
+
export const PORT_ATTEMPTS = 10;
|
|
4
|
+
/** Whether nothing is listening on `port` at 127.0.0.1 right now. */
|
|
5
|
+
export function portIsFree(port, host = "127.0.0.1") {
|
|
6
|
+
return new Promise((resolve) => {
|
|
7
|
+
const probe = createServer();
|
|
8
|
+
probe.unref();
|
|
9
|
+
probe.once("error", () => resolve(false));
|
|
10
|
+
probe.listen({ port, host, exclusive: true }, () => {
|
|
11
|
+
probe.close(() => resolve(true));
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The port this board listens on. Each folder is its own board, so a second
|
|
17
|
+
* `zelio` in another folder takes the next free port instead of crashing on
|
|
18
|
+
* the first one's. A port the person named is theirs: when it is taken the
|
|
19
|
+
* answer is null and the caller says so, rather than quietly moving it.
|
|
20
|
+
*
|
|
21
|
+
* The port has to be known before the app is built (its origin check names
|
|
22
|
+
* it), so this probes rather than listens; a race with another process in the
|
|
23
|
+
* gap still fails loudly at listen.
|
|
24
|
+
*/
|
|
25
|
+
export async function choosePort(preferred, explicit, isFree = portIsFree) {
|
|
26
|
+
if (explicit)
|
|
27
|
+
return (await isFree(preferred)) ? preferred : null;
|
|
28
|
+
for (let port = preferred; port < preferred + PORT_ATTEMPTS; port++) {
|
|
29
|
+
// In order on purpose: the lowest free port wins, so boards land predictably.
|
|
30
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
31
|
+
if (await isFree(port))
|
|
32
|
+
return port;
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=port.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"port.js","sourceRoot":"","sources":["../../src/server/port.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAExC,wEAAwE;AACxE,MAAM,CAAC,MAAM,aAAa,GAAG,EAAE,CAAC;AAEhC,qEAAqE;AACrE,MAAM,UAAU,UAAU,CAAC,IAAY,EAAE,IAAI,GAAG,WAAW;IACzD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,MAAM,KAAK,GAAG,YAAY,EAAE,CAAC;QAC7B,KAAK,CAAC,KAAK,EAAE,CAAC;QACd,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;QAC1C,KAAK,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE;YACjD,KAAK,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;QACnC,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,SAAiB,EACjB,QAAiB,EACjB,MAAM,GAAuC,UAAU;IAEvD,IAAI,QAAQ;QAAE,OAAO,CAAC,MAAM,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC;IAClE,KAAK,IAAI,IAAI,GAAG,SAAS,EAAE,IAAI,GAAG,SAAS,GAAG,aAAa,EAAE,IAAI,EAAE,EAAE,CAAC;QACpE,8EAA8E;QAC9E,4CAA4C;QAC5C,IAAI,MAAM,MAAM,CAAC,IAAI,CAAC;YAAE,OAAO,IAAI,CAAC;IACtC,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who is making a request.
|
|
3
|
+
*
|
|
4
|
+
* The local browser is a "local" principal: it holds the shell token and is
|
|
5
|
+
* allowed to do anything the board offers. A request that arrived over the
|
|
6
|
+
* cloud tunnel is a "tunnel" principal: it was authenticated by the Worker,
|
|
7
|
+
* never has the shell token, and may only use the routes below.
|
|
8
|
+
*
|
|
9
|
+
* The marker lives in a WeakMap keyed by the Request object, not in a header.
|
|
10
|
+
* A network caller cannot reach app.fetch() with a marked Request, and there is
|
|
11
|
+
* no header to spoof. The relay strips x-zelio-* headers before building the
|
|
12
|
+
* Request, but the marker would still be the only thing the middleware trusts.
|
|
13
|
+
*/
|
|
14
|
+
const tunnelRequests = new WeakMap();
|
|
15
|
+
/** Marks a Request the tunnel client built in this process. */
|
|
16
|
+
export function markTunnelRequest(request, principal) {
|
|
17
|
+
tunnelRequests.set(request, principal);
|
|
18
|
+
return request;
|
|
19
|
+
}
|
|
20
|
+
export function tunnelPrincipalFor(request) {
|
|
21
|
+
return tunnelRequests.get(request) ?? null;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Deny by default. Everything the phone needs for the board, the ticket panel
|
|
25
|
+
* and a new task is here; settings, cloud pairing, agent-token doors and the
|
|
26
|
+
* event stream are not.
|
|
27
|
+
*/
|
|
28
|
+
export const TUNNEL_ROUTES = [
|
|
29
|
+
{ method: "GET", path: /^\/api\/health$/ },
|
|
30
|
+
{ method: "GET", path: /^\/api\/me$/ },
|
|
31
|
+
{ method: "GET", path: /^\/api\/version$/ },
|
|
32
|
+
{ method: "GET", path: /^\/api\/snapshot$/ },
|
|
33
|
+
{ method: "GET", path: /^\/api\/tickets$/ },
|
|
34
|
+
{ method: "GET", path: /^\/api\/tickets\/[A-Za-z0-9_-]{1,80}$/ },
|
|
35
|
+
{ method: "GET", path: /^\/api\/tickets\/[A-Za-z0-9_-]{1,80}\/diff$/ },
|
|
36
|
+
{ method: "GET", path: /^\/api\/tickets\/[A-Za-z0-9_-]{1,80}\/merge$/ },
|
|
37
|
+
{ method: "POST", path: /^\/api\/tickets\/[A-Za-z0-9_-]{1,80}\/merge$/ },
|
|
38
|
+
{ method: "GET", path: /^\/api\/runs\/latest$/ },
|
|
39
|
+
{ method: "GET", path: /^\/api\/profiles$/ },
|
|
40
|
+
{ method: "GET", path: /^\/api\/workflows$/ },
|
|
41
|
+
{ method: "POST", path: /^\/api\/tickets$/ },
|
|
42
|
+
{ method: "POST", path: /^\/api\/tickets\/[A-Za-z0-9_-]{1,80}\/confirm$/ },
|
|
43
|
+
{ method: "POST", path: /^\/api\/tickets\/[A-Za-z0-9_-]{1,80}\/transition$/ },
|
|
44
|
+
{ method: "POST", path: /^\/api\/tickets\/[A-Za-z0-9_-]{1,80}\/comments$/ },
|
|
45
|
+
{ method: "POST", path: /^\/api\/attachments$/ },
|
|
46
|
+
{ method: "DELETE", path: /^\/api\/attachments$/ },
|
|
47
|
+
{ method: "GET", path: /^\/api\/orchestrator$/ },
|
|
48
|
+
{ method: "POST", path: /^\/api\/orchestrator\/intent$/ },
|
|
49
|
+
{ method: "POST", path: /^\/api\/orchestrator\/planner-question$/ },
|
|
50
|
+
{ method: "POST", path: /^\/api\/orchestrator\/messages$/ },
|
|
51
|
+
{ method: "PUT", path: /^\/api\/orchestrator\/plan$/ },
|
|
52
|
+
{ method: "POST", path: /^\/api\/orchestrator\/reset$/ },
|
|
53
|
+
{ method: "POST", path: /^\/api\/orchestrator\/confirmed$/ },
|
|
54
|
+
];
|
|
55
|
+
/** The pathname of a relayed path, or null if it is not a path we can route. */
|
|
56
|
+
export function tunnelPathname(path) {
|
|
57
|
+
if (!path.startsWith("/"))
|
|
58
|
+
return null;
|
|
59
|
+
try {
|
|
60
|
+
return new URL(path, "http://relay.invalid").pathname;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export function tunnelAllows(method, path) {
|
|
67
|
+
const pathname = tunnelPathname(path);
|
|
68
|
+
if (!pathname)
|
|
69
|
+
return false;
|
|
70
|
+
const verb = method.toUpperCase();
|
|
71
|
+
return TUNNEL_ROUTES.some((route) => route.method === verb && route.path.test(pathname));
|
|
72
|
+
}
|
|
73
|
+
/** What `/api/me` tells the SPA it may render. */
|
|
74
|
+
export function capabilitiesFor(principal) {
|
|
75
|
+
if (principal.kind === "local") {
|
|
76
|
+
return [
|
|
77
|
+
"board:read",
|
|
78
|
+
"board:write",
|
|
79
|
+
"comment:write",
|
|
80
|
+
"task:write",
|
|
81
|
+
"config:write",
|
|
82
|
+
"cloud:pair",
|
|
83
|
+
"agent:run",
|
|
84
|
+
"pr:merge",
|
|
85
|
+
"issue:create",
|
|
86
|
+
// A real shell on the Mac. The phone never gets one: the routes are not
|
|
87
|
+
// in the allowlist below, and the panel hides itself without this.
|
|
88
|
+
"terminal:write",
|
|
89
|
+
];
|
|
90
|
+
}
|
|
91
|
+
return ["board:read", "board:write", "comment:write", "task:write", "agent:run", "pr:merge"];
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=principal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"principal.js","sourceRoot":"","sources":["../../src/server/principal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAIH,MAAM,cAAc,GAAG,IAAI,OAAO,EAAmD,CAAC;AAEtF,+DAA+D;AAC/D,MAAM,UAAU,iBAAiB,CAC/B,OAAgB,EAChB,SAAiD;IAEjD,cAAc,CAAC,GAAG,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IACvC,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,OAAgB;IACjD,OAAO,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC;AAC7C,CAAC;AAQD;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAA2B;IACnD,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,iBAAiB,EAAE;IAC1C,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE;IACtC,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,kBAAkB,EAAE;IAC3C,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,mBAAmB,EAAE;IAE5C,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,kBAAkB,EAAE;IAC3C,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,uCAAuC,EAAE;IAChE,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,6CAA6C,EAAE;IACtE,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,8CAA8C,EAAE;IACvE,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,8CAA8C,EAAE;IACxE,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,uBAAuB,EAAE;IAChD,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d,CAAC;AACH,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,MAAc,EAAE,IAAY;IACvD,MAAM,QAAQ,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;IACtC,IAAI,CAAC,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC5B,MAAM,IAAI,GAAG,MAAM,CAAC,WAAW,EAAE,CAAC;IAClC,OAAO,aAAa,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,KAAK,IAAI,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;AAC3F,CAAC;AAED,kDAAkD;AAClD,MAAM,UAAU,eAAe,CAAC,SAAoB;IAClD,IAAI,SAAS,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;QAC/B,OAAO;YACL,YAAY;YACZ,aAAa;YACb,eAAe;YACf,YAAY;YACZ,cAAc;YACd,YAAY;YACZ,WAAW;YACX,UAAU;YACV,cAAc;YACd,wEAAwE;YACxE,mEAAmE;YACnE,gBAAgB;SACjB,CAAC;IACJ,CAAC;IAED,OAAO,CAAC,YAAY,EAAE,aAAa,EAAE,eAAe,EAAE,YAAY,EAAE,WAAW,EAAE,UAAU,CAAC,CAAC;AAC/F,CAAC"}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { checkHarnessAuth, resolveHarnessCommand } from "../run/harnesses.js";
|
|
6
|
+
import { ptyEnv, spawnPty } from "./terminal.js";
|
|
7
|
+
/** The two CLIs whose own login the Providers page starts. */
|
|
8
|
+
export const SIGN_IN_COMMANDS = {
|
|
9
|
+
claude: { label: "Claude Code", args: ["auth", "login"] },
|
|
10
|
+
codex: { label: "Codex", args: ["login"] },
|
|
11
|
+
};
|
|
12
|
+
/** The Providers rows that take a key, and the store each key is written to. */
|
|
13
|
+
export const KEY_TARGETS = {
|
|
14
|
+
openai: { store: "codex" },
|
|
15
|
+
piOpenAIKey: { store: "pi", provider: "openai" },
|
|
16
|
+
deepseek: { store: "pi", provider: "deepseek" },
|
|
17
|
+
googleApiKey: { store: "pi", provider: "google" },
|
|
18
|
+
};
|
|
19
|
+
export function isSignInId(value) {
|
|
20
|
+
return Object.hasOwn(SIGN_IN_COMMANDS, value);
|
|
21
|
+
}
|
|
22
|
+
export function isKeyId(value) {
|
|
23
|
+
return Object.hasOwn(KEY_TARGETS, value);
|
|
24
|
+
}
|
|
25
|
+
const SIGN_IN_TIMEOUT_MS = 5 * 60_000;
|
|
26
|
+
const OUTPUT_LIMIT = 4_000;
|
|
27
|
+
const MESSAGE_LIMIT = 400;
|
|
28
|
+
const TIMEOUT_MESSAGE = "Sign-in timed out before it finished. Try again.";
|
|
29
|
+
export class SignInBusyError extends Error {
|
|
30
|
+
}
|
|
31
|
+
export class ProviderKeyError extends Error {
|
|
32
|
+
status;
|
|
33
|
+
constructor(status, message) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.status = status;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** Strips colour and cursor codes, so the row shows the CLI's words. */
|
|
39
|
+
function plainText(output) {
|
|
40
|
+
return (output
|
|
41
|
+
// oxlint-disable-next-line no-control-regex -- the escape byte is what this strips
|
|
42
|
+
.replace(/\u001b\[[0-9;?]*[ -/]*[@-~]/g, "")
|
|
43
|
+
.replace(/\r/g, "")
|
|
44
|
+
.trim());
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Runs each CLI's own login under a pseudoterminal, one at a time per CLI. The
|
|
48
|
+
* exit code is not the answer: a row is signed in only when the CLI's own check
|
|
49
|
+
* passes after the login ends, so a login that exits without a sign-in fails.
|
|
50
|
+
*/
|
|
51
|
+
export class ProviderSignIns {
|
|
52
|
+
deps;
|
|
53
|
+
runs = {
|
|
54
|
+
claude: { state: "idle" },
|
|
55
|
+
codex: { state: "idle" },
|
|
56
|
+
};
|
|
57
|
+
constructor(deps = {}) {
|
|
58
|
+
this.deps = deps;
|
|
59
|
+
}
|
|
60
|
+
/** The state each row reads on the status refresh. */
|
|
61
|
+
snapshot() {
|
|
62
|
+
return { claude: this.stateOf("claude"), codex: this.stateOf("codex") };
|
|
63
|
+
}
|
|
64
|
+
/** Starts the login. A second start while one runs is refused, not queued. */
|
|
65
|
+
async start(id) {
|
|
66
|
+
const { label, args } = SIGN_IN_COMMANDS[id];
|
|
67
|
+
if (this.runs[id].state === "running") {
|
|
68
|
+
throw new SignInBusyError(`${label} sign-in is already running`);
|
|
69
|
+
}
|
|
70
|
+
const command = await (this.deps.resolve ?? resolveHarnessCommand)(id);
|
|
71
|
+
if (!command) {
|
|
72
|
+
this.runs[id] = { state: "failed", message: `${label} is not installed on this machine.` };
|
|
73
|
+
return this.stateOf(id);
|
|
74
|
+
}
|
|
75
|
+
// The run is marked running only once the pty exists: a spawn that throws
|
|
76
|
+
// (node-pty missing, its helper not executable) must not leave the row waiting.
|
|
77
|
+
let pty;
|
|
78
|
+
try {
|
|
79
|
+
pty = (this.deps.spawn ?? spawnPty)({
|
|
80
|
+
shell: command,
|
|
81
|
+
args: [...args],
|
|
82
|
+
cwd: homedir(),
|
|
83
|
+
env: ptyEnv(),
|
|
84
|
+
cols: 100,
|
|
85
|
+
rows: 30,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
catch (cause) {
|
|
89
|
+
const reason = cause instanceof Error ? cause.message : String(cause);
|
|
90
|
+
this.runs[id] = { state: "failed", message: `Could not start ${label} sign-in: ${reason}` };
|
|
91
|
+
return this.stateOf(id);
|
|
92
|
+
}
|
|
93
|
+
this.runs[id] = { state: "running" };
|
|
94
|
+
let output = "";
|
|
95
|
+
let timedOut = false;
|
|
96
|
+
pty.onData((data) => {
|
|
97
|
+
output = (output + data).slice(-OUTPUT_LIMIT);
|
|
98
|
+
});
|
|
99
|
+
const timer = setTimeout(() => {
|
|
100
|
+
timedOut = true;
|
|
101
|
+
pty.kill();
|
|
102
|
+
}, this.deps.timeoutMs ?? SIGN_IN_TIMEOUT_MS);
|
|
103
|
+
pty.onExit(() => {
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
void this.settle(id, timedOut ? TIMEOUT_MESSAGE : plainText(output));
|
|
106
|
+
});
|
|
107
|
+
return this.stateOf(id);
|
|
108
|
+
}
|
|
109
|
+
/** The CLI's exit is not the answer; its own check after the exit is. */
|
|
110
|
+
async settle(id, cliMessage) {
|
|
111
|
+
const check = this.deps.checkAuth ?? checkHarnessAuth;
|
|
112
|
+
const auth = await check(id).catch(() => ({
|
|
113
|
+
ok: false,
|
|
114
|
+
message: `${SIGN_IN_COMMANDS[id].label} could not be checked after sign-in.`,
|
|
115
|
+
}));
|
|
116
|
+
if (auth.ok) {
|
|
117
|
+
this.runs[id] = { state: "idle" };
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
this.runs[id] = {
|
|
121
|
+
state: "failed",
|
|
122
|
+
message: cliMessage.slice(-MESSAGE_LIMIT) || auth.message,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
stateOf(id) {
|
|
126
|
+
return { ...this.runs[id] };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function defaultPiAuthFile() {
|
|
130
|
+
return join(homedir(), ".pi", "agent", "auth.json");
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Writes a key where the provider's own check reads it. The key is never
|
|
134
|
+
* returned, logged, or put in an error message.
|
|
135
|
+
*/
|
|
136
|
+
export async function saveProviderKey(id, key, deps = {}) {
|
|
137
|
+
const target = KEY_TARGETS[id];
|
|
138
|
+
if (target.store === "codex") {
|
|
139
|
+
const command = await (deps.resolve ?? resolveHarnessCommand)("codex");
|
|
140
|
+
if (!command)
|
|
141
|
+
throw new ProviderKeyError(409, "Codex is not installed on this machine.");
|
|
142
|
+
await (deps.codex ?? saveWithCodexLogin)(command, key);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
await savePiKey(deps.piAuthFile ?? defaultPiAuthFile(), target.provider, key);
|
|
146
|
+
}
|
|
147
|
+
/** `codex login --with-api-key` reads the key from stdin, so it never reaches argv. */
|
|
148
|
+
function saveWithCodexLogin(command, key) {
|
|
149
|
+
return new Promise((resolve, reject) => {
|
|
150
|
+
const child = spawn(command, ["login", "--with-api-key"], { stdio: ["pipe", "pipe", "pipe"] });
|
|
151
|
+
let output = "";
|
|
152
|
+
const collect = (chunk) => {
|
|
153
|
+
output = (output + chunk.toString("utf8")).slice(-OUTPUT_LIMIT);
|
|
154
|
+
};
|
|
155
|
+
child.stdout.on("data", collect);
|
|
156
|
+
child.stderr.on("data", collect);
|
|
157
|
+
child.on("error", (cause) => {
|
|
158
|
+
reject(new ProviderKeyError(500, `Could not run codex login: ${cause.message}`));
|
|
159
|
+
});
|
|
160
|
+
child.on("close", (code) => {
|
|
161
|
+
if (code === 0)
|
|
162
|
+
resolve();
|
|
163
|
+
else {
|
|
164
|
+
const message = plainText(output).split(key).join("[key]");
|
|
165
|
+
reject(new ProviderKeyError(500, message || "codex login did not save the key."));
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
child.stdin.end(`${key}\n`);
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
/** pi keeps one entry per provider in auth.json; every other entry is left as it is. */
|
|
172
|
+
async function savePiKey(file, provider, key) {
|
|
173
|
+
const store = await readPiAuth(file);
|
|
174
|
+
const existing = store[provider];
|
|
175
|
+
if (existing && existing.type !== "api_key") {
|
|
176
|
+
throw new ProviderKeyError(409, `pi already has a sign-in for ${provider}. Remove it in pi, then save the key here.`);
|
|
177
|
+
}
|
|
178
|
+
store[provider] = { type: "api_key", key };
|
|
179
|
+
await mkdir(dirname(file), { recursive: true, mode: 0o700 });
|
|
180
|
+
const temp = `${file}.${process.pid}.tmp`;
|
|
181
|
+
await writeFile(temp, `${JSON.stringify(store, null, 2)}\n`, { mode: 0o600 });
|
|
182
|
+
await rename(temp, file);
|
|
183
|
+
}
|
|
184
|
+
async function readPiAuth(file) {
|
|
185
|
+
let text;
|
|
186
|
+
try {
|
|
187
|
+
text = await readFile(file, "utf8");
|
|
188
|
+
}
|
|
189
|
+
catch (cause) {
|
|
190
|
+
if (cause.code === "ENOENT")
|
|
191
|
+
return {};
|
|
192
|
+
throw cause;
|
|
193
|
+
}
|
|
194
|
+
let parsed;
|
|
195
|
+
try {
|
|
196
|
+
parsed = JSON.parse(text);
|
|
197
|
+
}
|
|
198
|
+
catch {
|
|
199
|
+
parsed = null;
|
|
200
|
+
}
|
|
201
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
202
|
+
throw new ProviderKeyError(409, "pi's auth.json is not a JSON object, so Zelio will not overwrite it. Fix it in pi first.");
|
|
203
|
+
}
|
|
204
|
+
return parsed;
|
|
205
|
+
}
|
|
206
|
+
//# sourceMappingURL=provider-auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-auth.js","sourceRoot":"","sources":["../../src/server/provider-auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACtE,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAoB,MAAM,qBAAqB,CAAC;AAChG,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAkC,MAAM,eAAe,CAAC;AAEjF,8DAA8D;AAC9D,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,MAAM,EAAE,EAAE,KAAK,EAAE,aAAa,EAAE,IAAI,EAAE,CAA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AAC,GAAG,MAAM,CAAC;IAC1C,MAAM,SAAS,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;IAC9E,MAAM,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;AAC3B,CAAC;AAED,KAAK,UAAU,UAAU,CAAC,IAAY;IACpC,IAAI,IAAY,CAAC;IACjB,IAAI,CAAC;QACH,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACtC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAK,KAA+B,CAAC,IAAI,KAAK,QAAQ;YAAE,OAAO,EAAE,CAAC;QAClE,MAAM,KAAK,CAAC;IACd,CAAC;IACD,IAAI,MAAe,CAAC;IACpB,IAAI,CAAC;QACH,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC5B,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,GAAG,IAAI,CAAC;IAChB,CAAC;IACD,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAC3E,MAAM,IAAI,gBAAgB,CACxB,GAAG,EACH,0FAA0F,CAC3F,CAAC;IACJ,CAAC;IACD,OAAO,MAAiC,CAAC;AAC3C,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
// Catch only the standard user-cancel error. Other failures must reach the UI.
|
|
3
|
+
export const REPO_PICKER_SCRIPT = `try
|
|
4
|
+
return POSIX path of (choose folder with prompt "Choose a Git repository to add to Zelio")
|
|
5
|
+
on error number -128
|
|
6
|
+
return ""
|
|
7
|
+
end try`;
|
|
8
|
+
export function pickRepoFolder() {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
execFile("/usr/bin/osascript", ["-e", REPO_PICKER_SCRIPT], { encoding: "utf8" }, (error, stdout) => {
|
|
11
|
+
if (error)
|
|
12
|
+
reject(new Error("Could not open the folder picker. Please try again."));
|
|
13
|
+
else
|
|
14
|
+
resolve(stdout.replace(/\r?\n$/, "") || null);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=repo-picker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repo-picker.js","sourceRoot":"","sources":["../../src/server/repo-picker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAE9C,+EAA+E;AAC/E,MAAM,CAAC,MAAM,kBAAkB,GAAG;;;;QAI1B,CAAC;AAET,MAAM,UAAU,cAAc;IAC5B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,QAAQ,CACN,oBAAoB,EACpB,CAAC,IAAI,EAAE,kBAAkB,CAAC,EAC1B,EAAE,QAAQ,EAAE,MAAM,EAAE,EACpB,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE;YAChB,IAAI,KAAK;gBAAE,MAAM,CAAC,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC,CAAC;;gBAC/E,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAI,IAAI,CAAC,CAAC;QACrD,CAAC,CACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shells behind the docked terminal panel.
|
|
3
|
+
*
|
|
4
|
+
* A session is a real pseudoterminal running an interactive shell in the
|
|
5
|
+
* board's repo root. It is never started in a ticket's worktree: the panel is
|
|
6
|
+
* the person's own window on the repo, and a run's checkout is not theirs to
|
|
7
|
+
* type in.
|
|
8
|
+
*
|
|
9
|
+
* The process outlives the page. Closing the panel or reloading the tab only
|
|
10
|
+
* drops the reader, so reopening reattaches to the same shell and replays what
|
|
11
|
+
* it has printed so far. A shell that exits (`exit`, or a crash) keeps its row
|
|
12
|
+
* and its last output instead of vanishing, so the panel can say "exited"
|
|
13
|
+
* rather than pretend it is still reconnecting.
|
|
14
|
+
*
|
|
15
|
+
* Only the local principal may reach these routes. Nothing here is in
|
|
16
|
+
* TUNNEL_ROUTES, and the routes additionally refuse a tunnel principal, so a
|
|
17
|
+
* phone has no way to get a shell.
|
|
18
|
+
*/
|
|
19
|
+
import { randomBytes } from "node:crypto";
|
|
20
|
+
import { accessSync, chmodSync, constants, statSync } from "node:fs";
|
|
21
|
+
import { createRequire } from "node:module";
|
|
22
|
+
import { basename, dirname, join } from "node:path";
|
|
23
|
+
/** The panel asked for more sessions than the cap allows. */
|
|
24
|
+
export class TerminalLimitError extends Error {
|
|
25
|
+
max;
|
|
26
|
+
constructor(max) {
|
|
27
|
+
super(`the panel already holds ${max} sessions`);
|
|
28
|
+
this.max = max;
|
|
29
|
+
this.name = "TerminalLimitError";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** The id names no live session: either it never existed or it was deleted. */
|
|
33
|
+
export class NoSuchTerminalError extends Error {
|
|
34
|
+
id;
|
|
35
|
+
constructor(id) {
|
|
36
|
+
super(`no session ${id}`);
|
|
37
|
+
this.id = id;
|
|
38
|
+
this.name = "NoSuchTerminalError";
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** The shell ended, so its row stays readable but nothing can be typed into it. */
|
|
42
|
+
export class TerminalExitedError extends Error {
|
|
43
|
+
id;
|
|
44
|
+
constructor(id) {
|
|
45
|
+
super(`session ${id} has exited`);
|
|
46
|
+
this.id = id;
|
|
47
|
+
this.name = "TerminalExitedError";
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A person needs a handful of shells at most (git in one, tests in another,
|
|
52
|
+
* a stray `curl` in a third). The cap keeps a stuck key from filling memory
|
|
53
|
+
* and makes the refusal a sentence rather than a dead tab.
|
|
54
|
+
*/
|
|
55
|
+
export const MAX_TERMINAL_SESSIONS = 8;
|
|
56
|
+
/** How much output a reattaching reader replays. Older bytes are dropped. */
|
|
57
|
+
const MAX_REPLAY_CHARS = 200_000;
|
|
58
|
+
export const DEFAULT_TERMINAL_COLS = 80;
|
|
59
|
+
export const DEFAULT_TERMINAL_ROWS = 24;
|
|
60
|
+
export const MAX_TERMINAL_COLS = 500;
|
|
61
|
+
export const MAX_TERMINAL_ROWS = 300;
|
|
62
|
+
/** The smallest pty that still fits a prompt; anything smaller is a mistake. */
|
|
63
|
+
export const MIN_TERMINAL_COLS = 2;
|
|
64
|
+
export const MIN_TERMINAL_ROWS = 2;
|
|
65
|
+
const require = createRequire(import.meta.url);
|
|
66
|
+
/**
|
|
67
|
+
* node-pty is loaded only when a real shell starts. Tests inject a spawn
|
|
68
|
+
* function, so the native module is never loaded there — and the board still
|
|
69
|
+
* boots, builds and type-checks on a machine whose node-pty did not build.
|
|
70
|
+
*/
|
|
71
|
+
function loadNodePty() {
|
|
72
|
+
return require("node-pty");
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* npm publishes node-pty's prebuilt spawn helper without the executable bit, so
|
|
76
|
+
* the first `pty.spawn` fails with `posix_spawnp failed` on a clean install.
|
|
77
|
+
* Restore the bit once, before any session starts. A build from source has no
|
|
78
|
+
* helper, and Windows has no posix helper at all; both are fine.
|
|
79
|
+
*/
|
|
80
|
+
function repairSpawnHelper() {
|
|
81
|
+
if (process.platform === "win32")
|
|
82
|
+
return;
|
|
83
|
+
try {
|
|
84
|
+
const root = dirname(dirname(require.resolve("node-pty")));
|
|
85
|
+
const helper = join(root, "prebuilds", `${process.platform}-${process.arch}`, "spawn-helper");
|
|
86
|
+
const mode = statSync(helper).mode;
|
|
87
|
+
if ((mode & 0o111) === 0)
|
|
88
|
+
chmodSync(helper, mode | 0o755);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
// No prebuilds: the source build is what was installed.
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** An executable file, or nothing. The shell is named by the person's env. */
|
|
95
|
+
function isExecutable(path) {
|
|
96
|
+
try {
|
|
97
|
+
accessSync(path, constants.X_OK);
|
|
98
|
+
return statSync(path).isFile();
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function defaultShell() {
|
|
105
|
+
const own = process.env.SHELL?.trim();
|
|
106
|
+
if (own && isExecutable(own))
|
|
107
|
+
return own;
|
|
108
|
+
return process.platform === "win32" ? "powershell.exe" : "/bin/sh";
|
|
109
|
+
}
|
|
110
|
+
/** The env a pty gets: this process's own, with TERM set rather than guessed. */
|
|
111
|
+
export function ptyEnv() {
|
|
112
|
+
const env = {};
|
|
113
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
114
|
+
if (typeof value === "string")
|
|
115
|
+
env[key] = value;
|
|
116
|
+
}
|
|
117
|
+
env.TERM = "xterm-256color";
|
|
118
|
+
env.ZELIO_TERMINAL = "1";
|
|
119
|
+
return env;
|
|
120
|
+
}
|
|
121
|
+
/** The default pty: node-pty under a real pseudoterminal. */
|
|
122
|
+
export function spawnPty(options) {
|
|
123
|
+
repairSpawnHelper();
|
|
124
|
+
const pty = loadNodePty();
|
|
125
|
+
return pty.spawn(options.shell, options.args, {
|
|
126
|
+
name: "xterm-256color",
|
|
127
|
+
cols: options.cols,
|
|
128
|
+
rows: options.rows,
|
|
129
|
+
cwd: options.cwd,
|
|
130
|
+
env: options.env,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
export class TerminalManager {
|
|
134
|
+
terminals = new Map();
|
|
135
|
+
root;
|
|
136
|
+
spawn;
|
|
137
|
+
shell;
|
|
138
|
+
max;
|
|
139
|
+
constructor(options) {
|
|
140
|
+
this.root = options.root;
|
|
141
|
+
this.spawn = options.spawn ?? spawnPty;
|
|
142
|
+
this.shell = options.shell?.trim() || defaultShell();
|
|
143
|
+
this.max = options.max ?? MAX_TERMINAL_SESSIONS;
|
|
144
|
+
}
|
|
145
|
+
/** Every session, newest last. An exited shell is still a session. */
|
|
146
|
+
list() {
|
|
147
|
+
return [...this.terminals.values()].map((terminal) => ({ ...terminal.session }));
|
|
148
|
+
}
|
|
149
|
+
get(id) {
|
|
150
|
+
const terminal = this.terminals.get(id);
|
|
151
|
+
return terminal ? { ...terminal.session } : null;
|
|
152
|
+
}
|
|
153
|
+
has(id) {
|
|
154
|
+
return this.terminals.has(id);
|
|
155
|
+
}
|
|
156
|
+
/** Start one interactive shell in the repo root. */
|
|
157
|
+
create(size = {}) {
|
|
158
|
+
if (this.terminals.size >= this.max)
|
|
159
|
+
throw new TerminalLimitError(this.max);
|
|
160
|
+
const cols = asSize(size.cols, DEFAULT_TERMINAL_COLS, MIN_TERMINAL_COLS, MAX_TERMINAL_COLS);
|
|
161
|
+
const rows = asSize(size.rows, DEFAULT_TERMINAL_ROWS, MIN_TERMINAL_ROWS, MAX_TERMINAL_ROWS);
|
|
162
|
+
const pty = this.spawn({
|
|
163
|
+
shell: this.shell,
|
|
164
|
+
// Interactive, but not a login shell: the person's rc is enough, and a
|
|
165
|
+
// login shell would re-run the whole profile on every session.
|
|
166
|
+
args: ["-i"],
|
|
167
|
+
cwd: this.root,
|
|
168
|
+
env: ptyEnv(),
|
|
169
|
+
cols,
|
|
170
|
+
rows,
|
|
171
|
+
});
|
|
172
|
+
const session = {
|
|
173
|
+
id: randomBytes(9).toString("base64url"),
|
|
174
|
+
shell: this.shell,
|
|
175
|
+
cwd: this.root,
|
|
176
|
+
label: `${basename(this.shell)} ${basename(this.root)}`,
|
|
177
|
+
cols,
|
|
178
|
+
rows,
|
|
179
|
+
exited: false,
|
|
180
|
+
exitCode: null,
|
|
181
|
+
};
|
|
182
|
+
const live = { session, pty, replay: "", listeners: new Set() };
|
|
183
|
+
pty.onData((data) => {
|
|
184
|
+
live.replay = appendReplay(live.replay, data);
|
|
185
|
+
this.emit(live, { type: "data", data });
|
|
186
|
+
});
|
|
187
|
+
pty.onExit(({ exitCode }) => {
|
|
188
|
+
// The row and its output stay: the panel shows "exited" instead of an
|
|
189
|
+
// empty screen, and the person can read what the last command said.
|
|
190
|
+
session.exited = true;
|
|
191
|
+
session.exitCode = exitCode;
|
|
192
|
+
this.emit(live, { type: "exit", exitCode });
|
|
193
|
+
});
|
|
194
|
+
this.terminals.set(session.id, live);
|
|
195
|
+
return { ...session };
|
|
196
|
+
}
|
|
197
|
+
/** Type into the shell exactly as the keyboard would. */
|
|
198
|
+
input(id, data) {
|
|
199
|
+
const terminal = this.live(id);
|
|
200
|
+
terminal.pty.write(data);
|
|
201
|
+
}
|
|
202
|
+
/** Tell the shell and its programs the window changed. */
|
|
203
|
+
resize(id, cols, rows) {
|
|
204
|
+
const terminal = this.live(id);
|
|
205
|
+
if (!Number.isInteger(cols) || !Number.isInteger(rows))
|
|
206
|
+
return;
|
|
207
|
+
if (cols < MIN_TERMINAL_COLS || cols > MAX_TERMINAL_COLS)
|
|
208
|
+
return;
|
|
209
|
+
if (rows < MIN_TERMINAL_ROWS || rows > MAX_TERMINAL_ROWS)
|
|
210
|
+
return;
|
|
211
|
+
if (terminal.session.cols === cols && terminal.session.rows === rows)
|
|
212
|
+
return;
|
|
213
|
+
terminal.session.cols = cols;
|
|
214
|
+
terminal.session.rows = rows;
|
|
215
|
+
terminal.pty.resize(cols, rows);
|
|
216
|
+
}
|
|
217
|
+
/** End the shell and forget its row. */
|
|
218
|
+
delete(id) {
|
|
219
|
+
const terminal = this.terminals.get(id);
|
|
220
|
+
if (!terminal)
|
|
221
|
+
return false;
|
|
222
|
+
this.terminals.delete(id);
|
|
223
|
+
terminal.listeners.clear();
|
|
224
|
+
if (!terminal.session.exited) {
|
|
225
|
+
try {
|
|
226
|
+
terminal.pty.kill();
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
// The process was already gone; the row is what mattered.
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Attach a reader: replay what the shell has printed, then stream live bytes
|
|
236
|
+
* and the ending. Returns the unsubscribe, which the SSE route calls when the
|
|
237
|
+
* browser navigates away.
|
|
238
|
+
*/
|
|
239
|
+
subscribe(id, listener) {
|
|
240
|
+
const terminal = this.terminals.get(id);
|
|
241
|
+
if (!terminal)
|
|
242
|
+
throw new NoSuchTerminalError(id);
|
|
243
|
+
if (terminal.replay)
|
|
244
|
+
listener({ type: "data", data: terminal.replay });
|
|
245
|
+
if (terminal.session.exited)
|
|
246
|
+
listener({ type: "exit", exitCode: terminal.session.exitCode });
|
|
247
|
+
terminal.listeners.add(listener);
|
|
248
|
+
return () => {
|
|
249
|
+
terminal.listeners.delete(listener);
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
/** Kill every shell. Used when tests end and when the server shuts down. */
|
|
253
|
+
dispose() {
|
|
254
|
+
for (const id of this.terminals.keys())
|
|
255
|
+
this.delete(id);
|
|
256
|
+
}
|
|
257
|
+
live(id) {
|
|
258
|
+
const terminal = this.terminals.get(id);
|
|
259
|
+
if (!terminal)
|
|
260
|
+
throw new NoSuchTerminalError(id);
|
|
261
|
+
if (terminal.session.exited)
|
|
262
|
+
throw new TerminalExitedError(id);
|
|
263
|
+
return terminal;
|
|
264
|
+
}
|
|
265
|
+
emit(terminal, event) {
|
|
266
|
+
for (const listener of terminal.listeners) {
|
|
267
|
+
try {
|
|
268
|
+
listener(event);
|
|
269
|
+
}
|
|
270
|
+
catch {
|
|
271
|
+
// A broken reader must not take the shell or the other readers down.
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
function asSize(value, fallback, min, max) {
|
|
277
|
+
if (typeof value !== "number" || !Number.isInteger(value))
|
|
278
|
+
return fallback;
|
|
279
|
+
return Math.min(Math.max(value, min), max);
|
|
280
|
+
}
|
|
281
|
+
function appendReplay(replay, data) {
|
|
282
|
+
const next = replay + data;
|
|
283
|
+
return next.length > MAX_REPLAY_CHARS ? next.slice(next.length - MAX_REPLAY_CHARS) : next;
|
|
284
|
+
}
|
|
285
|
+
//# sourceMappingURL=terminal.js.map
|