@opencode-cockpit/shell 0.1.4 → 0.1.5
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/dist/connect.js +31 -0
- package/dist/server.js +152 -0
- package/dist/tools/find.js +66 -0
- package/dist/tools/format.js +63 -0
- package/dist/tools/index.js +433 -0
- package/{src/tools/keys.ts → dist/tools/keys.js} +11 -14
- package/dist/tui/badge.js +29 -0
- package/dist/tui/console.js +628 -0
- package/dist/tui/dock.js +214 -0
- package/dist/tui/index.js +264 -0
- package/dist/tui/keys.js +43 -0
- package/dist/tui/sidebar.js +121 -0
- package/dist/tui/store.js +156 -0
- package/dist/tui/view.js +154 -0
- package/package.json +22 -12
- package/types/connect.d.ts +9 -0
- package/types/server.d.ts +12 -0
- package/types/tools/find.d.ts +32 -0
- package/types/tools/format.d.ts +8 -0
- package/types/tools/index.d.ts +17 -0
- package/types/tools/keys.d.ts +3 -0
- package/types/tui/badge.d.ts +9 -0
- package/types/tui/console.d.ts +16 -0
- package/types/tui/dock.d.ts +12 -0
- package/types/tui/index.d.ts +19 -0
- package/types/tui/keys.d.ts +8 -0
- package/types/tui/sidebar.d.ts +14 -0
- package/types/tui/store.d.ts +46 -0
- package/types/tui/view.d.ts +67 -0
- package/src/connect.ts +0 -24
- package/src/server.ts +0 -156
- package/src/tools/find.ts +0 -80
- package/src/tools/format.ts +0 -78
- package/src/tools/index.ts +0 -467
- package/src/tui/badge.tsx +0 -17
- package/src/tui/console.tsx +0 -438
- package/src/tui/dock.tsx +0 -130
- package/src/tui/index.tsx +0 -286
- package/src/tui/keys.ts +0 -52
- package/src/tui/sidebar.tsx +0 -44
- package/src/tui/store.ts +0 -185
- package/src/tui/view.ts +0 -161
package/dist/connect.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { CockpitClient } from "@opencode-cockpit/client";
|
|
3
|
+
import daemonPkg from "@opencode-cockpit/daemon/package.json" with { type: "json" };
|
|
4
|
+
import { daemonBuildId } from "@opencode-cockpit/protocol";
|
|
5
|
+
import pkg from "../package.json" with { type: "json" };
|
|
6
|
+
|
|
7
|
+
/** Resolves the daemon entry shipped with this package. */
|
|
8
|
+
export function daemonEntry() {
|
|
9
|
+
return fileURLToPath(import.meta.resolve("@opencode-cockpit/daemon/main"));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Inside OpenCode `process.execPath` is the OpenCode binary; the client starts the daemon with
|
|
14
|
+
* BUN_BE_BUN=1 so it runs on OpenCode's embedded Bun (ADR 0001). The expected build lets the
|
|
15
|
+
* client replace a daemon left running from older plugin code.
|
|
16
|
+
*/
|
|
17
|
+
export function createClient(name) {
|
|
18
|
+
const entry = daemonEntry();
|
|
19
|
+
return new CockpitClient({
|
|
20
|
+
client: {
|
|
21
|
+
name,
|
|
22
|
+
version: pkg.version,
|
|
23
|
+
pid: process.pid
|
|
24
|
+
},
|
|
25
|
+
spawn: {
|
|
26
|
+
entry,
|
|
27
|
+
execPath: process.execPath
|
|
28
|
+
},
|
|
29
|
+
expectedBuild: daemonBuildId(entry, daemonPkg.version)
|
|
30
|
+
});
|
|
31
|
+
}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { claimFeature, duplicateFeatureMessage } from "@opencode-cockpit/client";
|
|
2
|
+
import { createClient } from "./connect.js";
|
|
3
|
+
import { describeStatus, formatLines } from "./tools/format.js";
|
|
4
|
+
import { createTools } from "./tools/index.js";
|
|
5
|
+
const GUIDANCE = `## Background shells (opencode-cockpit)
|
|
6
|
+
Long-running or interactive commands (dev servers, watchers, slow builds/tests, REPLs) go in shell_start, not bash with "&".
|
|
7
|
+
Block with shell_wait (pattern, port, idle, exit) instead of sleeping; follow output with shell_read(after=cursor).
|
|
8
|
+
You are messaged when a shell you started exits.`;
|
|
9
|
+
export const SHELL_PACKAGE = "@opencode-cockpit/shell";
|
|
10
|
+
/** Shell's server half as a factory, so bundles such as `opencode-cockpit` can include it. */
|
|
11
|
+
export function createShellServer({
|
|
12
|
+
source = SHELL_PACKAGE
|
|
13
|
+
} = {}) {
|
|
14
|
+
return async input => {
|
|
15
|
+
const claim = claimFeature(input, "shell", source);
|
|
16
|
+
if (!claim.active) {
|
|
17
|
+
// Logging through the server during plugin initialisation could wait on ourselves; defer it.
|
|
18
|
+
setTimeout(() => {
|
|
19
|
+
void input.client.app.log({
|
|
20
|
+
body: {
|
|
21
|
+
service: "opencode-cockpit",
|
|
22
|
+
level: "warn",
|
|
23
|
+
message: duplicateFeatureMessage("Shell", claim.owner, source)
|
|
24
|
+
}
|
|
25
|
+
}).catch(() => {});
|
|
26
|
+
}, 0);
|
|
27
|
+
return {};
|
|
28
|
+
}
|
|
29
|
+
const hooks = await shellHooks(input);
|
|
30
|
+
const dispose = hooks.dispose;
|
|
31
|
+
return {
|
|
32
|
+
...hooks,
|
|
33
|
+
dispose: async () => {
|
|
34
|
+
claim.release();
|
|
35
|
+
await dispose?.();
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
async function shellHooks({
|
|
41
|
+
client: opencode,
|
|
42
|
+
directory
|
|
43
|
+
}) {
|
|
44
|
+
const cockpit = createClient("opencode-cockpit/server");
|
|
45
|
+
const instance = crypto.randomUUID();
|
|
46
|
+
const quiet = new Set();
|
|
47
|
+
const userShell = process.env.SHELL && /(bash|zsh|fish|sh)$/.test(process.env.SHELL) ? process.env.SHELL : "/bin/bash";
|
|
48
|
+
const env = () => {
|
|
49
|
+
const out = {};
|
|
50
|
+
for (const [k, v] of Object.entries(process.env)) if (v !== undefined && !k.startsWith("OPENCODE_")) out[k] = v;
|
|
51
|
+
return out;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// Session titles rarely change; cache them so listing shells stays one round trip.
|
|
55
|
+
const titles = new Map();
|
|
56
|
+
const sessionTitle = async sessionID => {
|
|
57
|
+
const hit = titles.get(sessionID);
|
|
58
|
+
if (hit && Date.now() - hit.at < 60_000) return hit.title;
|
|
59
|
+
const result = await opencode.session.get({
|
|
60
|
+
path: {
|
|
61
|
+
id: sessionID
|
|
62
|
+
}
|
|
63
|
+
}).catch(() => undefined);
|
|
64
|
+
const title = result?.data?.title;
|
|
65
|
+
titles.set(sessionID, {
|
|
66
|
+
title,
|
|
67
|
+
at: Date.now()
|
|
68
|
+
});
|
|
69
|
+
return title;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// Wake the agent when a shell it owns ends on its own.
|
|
73
|
+
cockpit.on("shell.exited", info => {
|
|
74
|
+
if (info.owner.instance !== instance || !info.owner.session) return;
|
|
75
|
+
if (quiet.delete(info.id)) return;
|
|
76
|
+
void notifyExit(info).catch(() => {});
|
|
77
|
+
});
|
|
78
|
+
async function notifyExit(info) {
|
|
79
|
+
const session = info.owner.session;
|
|
80
|
+
const page = await cockpit.call("shell.read", {
|
|
81
|
+
id: info.id,
|
|
82
|
+
tail: 15
|
|
83
|
+
});
|
|
84
|
+
const failed = info.status === "failed" || info.status === "exited" && info.exitCode !== 0 || info.status === "killed";
|
|
85
|
+
const text = [`<shell_exited id="${info.id}" title="${info.title}">`, describeStatus(info), page.lines.length > 0 ? `last output:\n${formatLines(page.lines)}` : "(no output)", "</shell_exited>", failed ? `Investigate with shell_read id=${info.id} grep="error|fail" if the failure matters to the task.` : `Full output: shell_read id=${info.id}.`].join("\n");
|
|
86
|
+
await opencode.session.promptAsync({
|
|
87
|
+
path: {
|
|
88
|
+
id: session
|
|
89
|
+
},
|
|
90
|
+
body: {
|
|
91
|
+
parts: [{
|
|
92
|
+
type: "text",
|
|
93
|
+
text,
|
|
94
|
+
synthetic: true
|
|
95
|
+
}]
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
tool: createTools({
|
|
101
|
+
client: cockpit,
|
|
102
|
+
instance,
|
|
103
|
+
quiet,
|
|
104
|
+
env,
|
|
105
|
+
sessionTitle,
|
|
106
|
+
shellCommand: command => ({
|
|
107
|
+
command: userShell,
|
|
108
|
+
args: ["-c", command]
|
|
109
|
+
})
|
|
110
|
+
}),
|
|
111
|
+
"experimental.chat.system.transform": async (input, output) => {
|
|
112
|
+
output.system.push(GUIDANCE);
|
|
113
|
+
const running = await cockpit.call("shell.list", {
|
|
114
|
+
owner: {
|
|
115
|
+
project: directory
|
|
116
|
+
},
|
|
117
|
+
includeExited: false
|
|
118
|
+
}).catch(() => []);
|
|
119
|
+
if (running.length > 0) {
|
|
120
|
+
output.system.push(`Background shells currently running in this project:\n${running.slice(0, 15).map(s => {
|
|
121
|
+
const from = !s.owner.session ? ", started by the user" : s.owner.session === input.sessionID ? "" : ", another session";
|
|
122
|
+
return `- ${s.id} "${s.title}" (${describeStatus(s)}${from})`;
|
|
123
|
+
}).join("\n")}`);
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
event: async ({
|
|
127
|
+
event
|
|
128
|
+
}) => {
|
|
129
|
+
if (event.type !== "session.deleted") return;
|
|
130
|
+
const sessionID = event.properties.info.id;
|
|
131
|
+
const owned = await cockpit.call("shell.list", {
|
|
132
|
+
owner: {
|
|
133
|
+
session: sessionID
|
|
134
|
+
}
|
|
135
|
+
}).catch(() => []);
|
|
136
|
+
for (const shell of owned) {
|
|
137
|
+
quiet.add(shell.id);
|
|
138
|
+
await cockpit.call("shell.remove", {
|
|
139
|
+
id: shell.id
|
|
140
|
+
}).catch(() => {});
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
dispose: async () => {
|
|
144
|
+
cockpit.close();
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
const plugin = {
|
|
149
|
+
id: "opencode-cockpit.shell",
|
|
150
|
+
server: createShellServer()
|
|
151
|
+
};
|
|
152
|
+
export default plugin;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** The command as written, without the `$SHELL -c` wrapper. */
|
|
2
|
+
export function commandOf(s) {
|
|
3
|
+
return s.args.length === 2 && s.args[0] === "-c" ? s.args[1] : [s.command, ...s.args].join(" ");
|
|
4
|
+
}
|
|
5
|
+
export function isFailed(s) {
|
|
6
|
+
return s.status === "failed" || s.status === "exited" && s.exitCode !== 0;
|
|
7
|
+
}
|
|
8
|
+
export function filterShells(list, filter) {
|
|
9
|
+
const query = filter.query?.trim().toLowerCase();
|
|
10
|
+
return list.filter(s => {
|
|
11
|
+
if (query && !s.title.toLowerCase().includes(query) && !commandOf(s).toLowerCase().includes(query)) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
switch (filter.status ?? "any") {
|
|
15
|
+
case "running":
|
|
16
|
+
if (s.status !== "running") return false;
|
|
17
|
+
break;
|
|
18
|
+
case "failed":
|
|
19
|
+
if (!isFailed(s)) return false;
|
|
20
|
+
break;
|
|
21
|
+
case "finished":
|
|
22
|
+
if (s.status === "running") return false;
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
switch (filter.session ?? "any") {
|
|
26
|
+
case "this":
|
|
27
|
+
return s.owner.session === filter.currentSession;
|
|
28
|
+
case "others":
|
|
29
|
+
return s.owner.session !== filter.currentSession;
|
|
30
|
+
default:
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Finds the shell a name refers to. Exact names (ignoring case) beat partial matches on name or
|
|
37
|
+
* command. When several match, a single running shell is the obvious intent (earlier finished
|
|
38
|
+
* shells with the same name are history); otherwise the caller must choose.
|
|
39
|
+
*/
|
|
40
|
+
export function matchByName(list, name) {
|
|
41
|
+
const wanted = name.trim().toLowerCase();
|
|
42
|
+
const exact = list.filter(s => s.title.trim().toLowerCase() === wanted);
|
|
43
|
+
const matches = exact.length > 0 ? exact : list.filter(s => s.title.toLowerCase().includes(wanted) || commandOf(s).toLowerCase().includes(wanted));
|
|
44
|
+
if (matches.length === 0) return {
|
|
45
|
+
kind: "none",
|
|
46
|
+
available: [...list]
|
|
47
|
+
};
|
|
48
|
+
if (matches.length === 1) return {
|
|
49
|
+
kind: "found",
|
|
50
|
+
shell: matches[0],
|
|
51
|
+
alsoMatched: []
|
|
52
|
+
};
|
|
53
|
+
const running = matches.filter(s => s.status === "running");
|
|
54
|
+
if (running.length === 1) {
|
|
55
|
+
const shell = running[0];
|
|
56
|
+
return {
|
|
57
|
+
kind: "found",
|
|
58
|
+
shell,
|
|
59
|
+
alsoMatched: matches.filter(s => s !== shell)
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
kind: "ambiguous",
|
|
64
|
+
candidates: matches
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const MAX_LINE = 2000;
|
|
2
|
+
|
|
3
|
+
/** Compact log lines for a model: numbered, long lines cut, consecutive repeats collapsed. */
|
|
4
|
+
export function formatLines(lines) {
|
|
5
|
+
const out = [];
|
|
6
|
+
let i = 0;
|
|
7
|
+
while (i < lines.length) {
|
|
8
|
+
const line = lines[i];
|
|
9
|
+
let j = i + 1;
|
|
10
|
+
while (j < lines.length && lines[j].text === line.text) j++;
|
|
11
|
+
const repeats = j - i;
|
|
12
|
+
const text = line.text.length > MAX_LINE ? `${line.text.slice(0, MAX_LINE)}… [${line.text.length - MAX_LINE} chars cut]` : line.text;
|
|
13
|
+
out.push(repeats > 1 ? `${line.n}| ${text} (×${repeats}, lines ${line.n}-${line.n + repeats - 1})` : `${line.n}| ${text}`);
|
|
14
|
+
i = j;
|
|
15
|
+
}
|
|
16
|
+
return out.join("\n");
|
|
17
|
+
}
|
|
18
|
+
export function describeStatus(info) {
|
|
19
|
+
switch (info.status) {
|
|
20
|
+
case "running":
|
|
21
|
+
return `running (pid ${info.pid}, up ${duration(Date.now() - info.startedAt)})`;
|
|
22
|
+
case "exited":
|
|
23
|
+
return `exited with code ${info.exitCode ?? "?"} after ${duration((info.endedAt ?? Date.now()) - info.startedAt)}`;
|
|
24
|
+
case "killed":
|
|
25
|
+
return `killed${info.signal ? ` by ${info.signal}` : ""} after ${duration((info.endedAt ?? Date.now()) - info.startedAt)}`;
|
|
26
|
+
case "failed":
|
|
27
|
+
return `failed to start: ${info.error ?? "unknown error"}`;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export function header(info) {
|
|
31
|
+
const run = info.run > 1 ? ` run=${info.run}` : "";
|
|
32
|
+
return `<shell id="${info.id}" title="${info.title.replaceAll('"', "'")}" status="${info.status}"${run}>`;
|
|
33
|
+
}
|
|
34
|
+
export function formatRead(info, page, empty = "(no output yet)") {
|
|
35
|
+
const parts = [header(info), `status: ${describeStatus(info)}`];
|
|
36
|
+
if (page.truncated) parts.push(`(older lines were dropped from the buffer; oldest kept is ${page.firstLine})`);
|
|
37
|
+
parts.push(page.lines.length > 0 ? formatLines(page.lines) : empty);
|
|
38
|
+
if (page.hasMore) parts.push(`(more lines available: call shell_read with after=${page.nextCursor})`);
|
|
39
|
+
parts.push("</shell>", `cursor: ${page.nextCursor}`);
|
|
40
|
+
return parts.join("\n");
|
|
41
|
+
}
|
|
42
|
+
export function formatWait(result, timeoutSeconds) {
|
|
43
|
+
switch (result.reason) {
|
|
44
|
+
case "pattern":
|
|
45
|
+
return `condition met: pattern matched on line ${result.match?.n}: ${result.match?.text}`;
|
|
46
|
+
case "port":
|
|
47
|
+
return "condition met: port is accepting connections";
|
|
48
|
+
case "idle":
|
|
49
|
+
return "condition met: no output for the idle window (the program may be waiting for input)";
|
|
50
|
+
case "exit":
|
|
51
|
+
return `process ended: ${describeStatus(result.info)}`;
|
|
52
|
+
case "timeout":
|
|
53
|
+
return `timed out after ${timeoutSeconds}s without the condition being met; the shell is still ${result.info.status}`;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export function duration(ms) {
|
|
57
|
+
const s = Math.max(0, Math.round(ms / 1000));
|
|
58
|
+
if (s < 60) return `${s}s`;
|
|
59
|
+
const m = Math.floor(s / 60);
|
|
60
|
+
if (m < 60) return `${m}m${s % 60 ? `${s % 60}s` : ""}`;
|
|
61
|
+
const h = Math.floor(m / 60);
|
|
62
|
+
return `${h}h${m % 60 ? `${m % 60}m` : ""}`;
|
|
63
|
+
}
|