@phren/cli 0.3.10 → 0.3.12
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/bridge/agent-hook-stores.d.ts +3 -0
- package/dist/bridge/agent-hook-stores.js +5 -0
- package/dist/bridge/agent-hooks.d.ts +159 -5
- package/dist/bridge/agent-hooks.js +663 -49
- package/dist/bridge/approval-summary.d.ts +16 -0
- package/dist/bridge/approval-summary.js +133 -0
- package/dist/bridge/authority-command.d.ts +20 -0
- package/dist/bridge/authority-command.js +120 -0
- package/dist/bridge/authority.d.ts +223 -0
- package/dist/bridge/authority.js +231 -0
- package/dist/bridge/claude-account-setup.d.ts +14 -0
- package/dist/bridge/claude-account-setup.js +69 -0
- package/dist/bridge/claude-accounts.d.ts +49 -0
- package/dist/bridge/claude-accounts.js +195 -0
- package/dist/bridge/code-routes.js +2 -2
- package/dist/bridge/codex-app-server.d.ts +144 -0
- package/dist/bridge/codex-app-server.js +319 -0
- package/dist/bridge/codex-auth-refresh.d.ts +33 -0
- package/dist/bridge/codex-auth-refresh.js +71 -0
- package/dist/bridge/codex-question-reply.d.ts +16 -0
- package/dist/bridge/codex-question-reply.js +57 -0
- package/dist/bridge/codex-review-mode.d.ts +10 -0
- package/dist/bridge/codex-review-mode.js +74 -0
- package/dist/bridge/codex-servers.d.ts +217 -0
- package/dist/bridge/codex-servers.js +636 -0
- package/dist/bridge/command.js +94 -4
- package/dist/bridge/computer-identity.d.ts +75 -0
- package/dist/bridge/computer-identity.js +166 -0
- package/dist/bridge/conductor-group.d.ts +91 -8
- package/dist/bridge/conductor-group.js +105 -9
- package/dist/bridge/conductor-role.d.ts +94 -0
- package/dist/bridge/conductor-role.js +176 -0
- package/dist/bridge/conductor-sets.d.ts +20 -0
- package/dist/bridge/conductor-sets.js +59 -0
- package/dist/bridge/dispatch-command.d.ts +3 -0
- package/dist/bridge/dispatch-command.js +75 -11
- package/dist/bridge/dispatch-returns.d.ts +123 -19
- package/dist/bridge/dispatch-returns.js +357 -50
- package/dist/bridge/dispatch.d.ts +103 -2
- package/dist/bridge/dispatch.js +272 -40
- package/dist/bridge/fanout-messages.js +2 -2
- package/dist/bridge/fanouts.d.ts +7 -1
- package/dist/bridge/fanouts.js +40 -5
- package/dist/bridge/folder-trust.d.ts +56 -0
- package/dist/bridge/folder-trust.js +195 -0
- package/dist/bridge/grants.d.ts +22 -1
- package/dist/bridge/grants.js +57 -9
- package/dist/bridge/hand-off.d.ts +21 -9
- package/dist/bridge/hand-off.js +58 -45
- package/dist/bridge/harness-status.d.ts +11 -0
- package/dist/bridge/harness-status.js +32 -0
- package/dist/bridge/harnesses.d.ts +39 -0
- package/dist/bridge/harnesses.js +65 -0
- package/dist/bridge/health.d.ts +2 -0
- package/dist/bridge/health.js +1 -1
- package/dist/bridge/herdr.d.ts +11 -2
- package/dist/bridge/herdr.js +98 -23
- package/dist/bridge/hook-fast.d.ts +4 -0
- package/dist/bridge/hook-fast.js +68 -0
- package/dist/bridge/install.d.ts +16 -1
- package/dist/bridge/install.js +88 -21
- package/dist/bridge/launch-brief.d.ts +96 -0
- package/dist/bridge/launch-brief.js +164 -0
- package/dist/bridge/model-switch.d.ts +3 -0
- package/dist/bridge/model-switch.js +17 -1
- package/dist/bridge/models.d.ts +8 -4
- package/dist/bridge/models.js +24 -8
- package/dist/bridge/mux-wire.d.ts +9 -0
- package/dist/bridge/mux-wire.js +19 -0
- package/dist/bridge/opencode-pane-server.d.ts +113 -0
- package/dist/bridge/opencode-pane-server.js +332 -0
- package/dist/bridge/opencode-panes.d.ts +111 -0
- package/dist/bridge/opencode-panes.js +295 -0
- package/dist/bridge/pane-accounts.d.ts +8 -0
- package/dist/bridge/pane-accounts.js +46 -0
- package/dist/bridge/prompt-startup.d.ts +5 -0
- package/dist/bridge/prompt-startup.js +24 -0
- package/dist/bridge/protocol.d.ts +5 -0
- package/dist/bridge/protocol.js +4 -0
- package/dist/bridge/push.d.ts +5 -4
- package/dist/bridge/push.js +6 -4
- package/dist/bridge/questions.d.ts +37 -2
- package/dist/bridge/questions.js +158 -15
- package/dist/bridge/resources.d.ts +125 -0
- package/dist/bridge/resources.js +314 -0
- package/dist/bridge/schedule-format.d.ts +2 -0
- package/dist/bridge/schedule-format.js +3 -0
- package/dist/bridge/schedule-launch.d.ts +2 -1
- package/dist/bridge/schedule-launch.js +23 -53
- package/dist/bridge/schedule-watch.d.ts +10 -1
- package/dist/bridge/schedule-watch.js +54 -2
- package/dist/bridge/server-launch.d.ts +22 -5
- package/dist/bridge/server-launch.js +225 -29
- package/dist/bridge/server-overview.d.ts +8 -2
- package/dist/bridge/server-overview.js +26 -5
- package/dist/bridge/server-pane-routes.d.ts +2 -0
- package/dist/bridge/server-pane-routes.js +145 -15
- package/dist/bridge/server-routes.d.ts +11 -1
- package/dist/bridge/server-routes.js +195 -36
- package/dist/bridge/server-stream.d.ts +2 -0
- package/dist/bridge/server-stream.js +22 -8
- package/dist/bridge/server.d.ts +6 -1
- package/dist/bridge/server.js +47 -12
- package/dist/bridge/session-activity.d.ts +81 -0
- package/dist/bridge/session-activity.js +117 -0
- package/dist/bridge/session-rename.d.ts +48 -0
- package/dist/bridge/session-rename.js +190 -0
- package/dist/bridge/session-servers.js +2 -2
- package/dist/bridge/settings-switch.d.ts +60 -0
- package/dist/bridge/settings-switch.js +287 -0
- package/dist/bridge/speech-voice.d.ts +55 -0
- package/dist/bridge/speech-voice.js +132 -0
- package/dist/bridge/speech.d.ts +70 -11
- package/dist/bridge/speech.js +169 -41
- package/dist/bridge/terminal-herdr.js +9 -3
- package/dist/bridge/terminal-tmux.d.ts +3 -3
- package/dist/bridge/terminal-tmux.js +24 -9
- package/dist/bridge/terminal.d.ts +27 -4
- package/dist/bridge/terminal.js +8 -1
- package/dist/bridge/transcript-claude.js +62 -12
- package/dist/bridge/transcript-codex.d.ts +0 -2
- package/dist/bridge/transcript-codex.js +29 -4
- package/dist/bridge/transcript-preview.d.ts +1 -0
- package/dist/bridge/transcript-preview.js +24 -4
- package/dist/bridge/transcripts.d.ts +15 -2
- package/dist/bridge/transcripts.js +47 -16
- package/dist/bridge/turn-records.d.ts +85 -0
- package/dist/bridge/turn-records.js +143 -0
- package/dist/bridge/uploads.d.ts +3 -0
- package/dist/bridge/uploads.js +14 -0
- package/dist/bridge/usage.d.ts +73 -28
- package/dist/bridge/usage.js +313 -340
- package/dist/bridge-hook.mjs +24624 -18450
- package/dist/cli/claude-unmanaged.d.ts +12 -0
- package/dist/cli/claude-unmanaged.js +105 -0
- package/dist/cli/schedules.js +2 -1
- package/dist/cli/session-start.js +10 -1
- package/dist/cli/session-stop.js +9 -1
- package/dist/cli-registry.d.ts +2 -0
- package/dist/cli-registry.js +37 -5
- package/dist/computers/command.d.ts +6 -0
- package/dist/computers/command.js +152 -0
- package/dist/computers/mcp.d.ts +1 -0
- package/dist/computers/mcp.js +97 -0
- package/dist/computers/read.d.ts +85 -0
- package/dist/computers/read.js +177 -0
- package/dist/data/task-receipts.d.ts +19 -0
- package/dist/data/task-receipts.js +68 -0
- package/dist/data/tasks.js +4 -1
- package/dist/entrypoint.js +10 -0
- package/dist/fanout/adapters/claude.js +15 -2
- package/dist/fanout/adapters/types.d.ts +6 -0
- package/dist/fanout/launcher.d.ts +5 -1
- package/dist/fanout/launcher.js +34 -3
- package/dist/fanout/picker.js +3 -2
- package/dist/fanout/usage.js +1 -1
- package/dist/generated/memory-ui-graph.browser.js +63 -63
- package/dist/graph-core/model.js +24 -3
- package/dist/graph-core/types.d.ts +4 -1
- package/dist/graph-core/types.js +1 -0
- package/dist/init/init-preset.js +7 -3
- package/dist/init/init-uninstall.js +2 -11
- package/dist/init/teardown.d.ts +3 -0
- package/dist/init/teardown.js +47 -0
- package/dist/link/context.js +21 -9
- package/dist/mcp/plugin-mode.js +1 -1
- package/dist/memory-ui-graph.runtime.js +63 -63
- package/dist/modules/code-package.d.ts +1 -0
- package/dist/modules/code-package.js +36 -7
- package/dist/modules/registry.js +14 -13
- package/dist/profile-store.d.ts +6 -0
- package/dist/profile-store.js +25 -1
- package/dist/shell/graph/glyphs.js +4 -0
- package/dist/shell/graph/graph-view.js +2 -0
- package/dist/status.js +7 -1
- package/dist/sync/task-claim.js +3 -2
- package/dist/task/lifecycle.js +25 -4
- package/dist/test-global-setup.js +5 -0
- package/dist/tools/dispatch.js +35 -2
- package/dist/tools/tasks.js +10 -1
- package/package.json +1 -1
- package/plugins/opencode/phren-transcript.js +87 -4
- package/starter/global/skills/conductor/SKILL.md +17 -2
- package/starter/global/skills/fanout/SKILL.md +3 -0
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import * as crypto from "node:crypto";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as net from "node:net";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import { atomicWriteText } from "../phren-paths.js";
|
|
6
|
+
/**
|
|
7
|
+
* The HTTP API an OpenCode TUI serves when started as
|
|
8
|
+
* `OPENCODE_SERVER_PASSWORD=<pw> opencode --port <port>`. The Hook drives a
|
|
9
|
+
* visible pane by talking to it instead of typing keys: register the pane's
|
|
10
|
+
* server here, then `openPaneClient` sends a prompt into the session the TUI is
|
|
11
|
+
* already showing and confirms the user message landed.
|
|
12
|
+
*
|
|
13
|
+
* Routes and bodies are the server's own spec (opencode 1.18.31, saved to
|
|
14
|
+
* `.scratch/openapi.json`): `session.list`, `session.prompt_async`,
|
|
15
|
+
* `session.messages`, `session.abort`, `event.subscribe`, `permission.list`,
|
|
16
|
+
* `permission.reply`, `question.list`, `question.reply`, `question.reject`,
|
|
17
|
+
* `session.create` and `tui.selectSession`. A question reply takes
|
|
18
|
+
* `{ answers }`, each answer an array of selected labels. A session created
|
|
19
|
+
* over HTTP is not what the TUI shows until `tui.selectSession` moves it there.
|
|
20
|
+
*/
|
|
21
|
+
const MAX_PASSWORD = 200;
|
|
22
|
+
const REQUEST_TIMEOUT_MS = 10_000;
|
|
23
|
+
const PROMPT_POLL_MS = 200;
|
|
24
|
+
const DEFAULT_PROMPT_TIMEOUT_MS = 3_000;
|
|
25
|
+
/** Enough recent messages to find the one just sent without paging a long history. */
|
|
26
|
+
const RECENT_MESSAGES = 20;
|
|
27
|
+
/** The prompt never reached the server (refused, or nothing listening), so
|
|
28
|
+
* the caller may still send it another way without a duplicate. */
|
|
29
|
+
export class PromptNotSent extends Error {
|
|
30
|
+
}
|
|
31
|
+
/** A segment safe to embed in a filename: never `.`, `..`, or a path separator. */
|
|
32
|
+
function safeSegment(value) {
|
|
33
|
+
return typeof value === "string" && value.length > 0
|
|
34
|
+
&& value !== "." && value !== ".." && !/[/\\\u0000-\u001f\u007f]/.test(value);
|
|
35
|
+
}
|
|
36
|
+
function validEntry(value) {
|
|
37
|
+
if (!value || typeof value !== "object")
|
|
38
|
+
return false;
|
|
39
|
+
const entry = value;
|
|
40
|
+
return safeSegment(entry.server) && safeSegment(entry.pane)
|
|
41
|
+
&& Number.isInteger(entry.port) && entry.port >= 1 && entry.port <= 65_535
|
|
42
|
+
&& typeof entry.password === "string" && entry.password.length > 0 && entry.password.length <= MAX_PASSWORD
|
|
43
|
+
&& Number.isInteger(entry.pid) && entry.pid > 0
|
|
44
|
+
&& typeof entry.directory === "string" && entry.directory.length > 0
|
|
45
|
+
&& typeof entry.createdAt === "string";
|
|
46
|
+
}
|
|
47
|
+
/** Signal 0 only checks existence; EPERM still means the process is alive. */
|
|
48
|
+
function alive(pid) {
|
|
49
|
+
try {
|
|
50
|
+
process.kill(pid, 0);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
return error.code === "EPERM";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** The `%2F`-joined name keeps a server/pane pair unique without a path separator. */
|
|
58
|
+
function paneServerFile(dir, server, pane) {
|
|
59
|
+
return path.join(dir, `${encodeURIComponent(`${server}/${pane}`)}.json`);
|
|
60
|
+
}
|
|
61
|
+
export function registerPaneServer(dir, entry) {
|
|
62
|
+
if (!validEntry(entry))
|
|
63
|
+
throw new Error("Invalid OpenCode pane server entry.");
|
|
64
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
65
|
+
const file = paneServerFile(dir, entry.server, entry.pane);
|
|
66
|
+
atomicWriteText(file, JSON.stringify(entry), { mode: 0o600 });
|
|
67
|
+
return file;
|
|
68
|
+
}
|
|
69
|
+
export function readPaneServer(dir, server, pane) {
|
|
70
|
+
if (!safeSegment(server) || !safeSegment(pane))
|
|
71
|
+
return undefined;
|
|
72
|
+
let raw;
|
|
73
|
+
try {
|
|
74
|
+
raw = fs.readFileSync(paneServerFile(dir, server, pane), "utf8");
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
let parsed;
|
|
80
|
+
try {
|
|
81
|
+
parsed = JSON.parse(raw);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
if (!validEntry(parsed))
|
|
87
|
+
return undefined;
|
|
88
|
+
if (parsed.server !== server || parsed.pane !== pane)
|
|
89
|
+
return undefined;
|
|
90
|
+
if (!alive(parsed.pid))
|
|
91
|
+
return undefined;
|
|
92
|
+
return parsed;
|
|
93
|
+
}
|
|
94
|
+
/** Every registered pane whose OpenCode process is still running. An entry
|
|
95
|
+
* whose process is gone is removed as it is found. */
|
|
96
|
+
export function listPaneServers(dir) {
|
|
97
|
+
let names;
|
|
98
|
+
try {
|
|
99
|
+
names = fs.readdirSync(dir);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return [];
|
|
103
|
+
}
|
|
104
|
+
const entries = [];
|
|
105
|
+
for (const name of names.slice(0, 1024)) {
|
|
106
|
+
if (!name.endsWith(".json"))
|
|
107
|
+
continue;
|
|
108
|
+
const file = path.join(dir, name);
|
|
109
|
+
let parsed;
|
|
110
|
+
try {
|
|
111
|
+
const info = fs.lstatSync(file);
|
|
112
|
+
if (!info.isFile() || info.size > 16_384)
|
|
113
|
+
continue;
|
|
114
|
+
parsed = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (!validEntry(parsed) || paneServerFile(dir, parsed.server, parsed.pane) !== file)
|
|
120
|
+
continue;
|
|
121
|
+
if (!alive(parsed.pid)) {
|
|
122
|
+
try {
|
|
123
|
+
fs.rmSync(file, { force: true });
|
|
124
|
+
}
|
|
125
|
+
catch { /* already gone */ }
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
entries.push(parsed);
|
|
129
|
+
}
|
|
130
|
+
return entries;
|
|
131
|
+
}
|
|
132
|
+
export function removePaneServer(dir, server, pane) {
|
|
133
|
+
if (!safeSegment(server) || !safeSegment(pane))
|
|
134
|
+
return;
|
|
135
|
+
try {
|
|
136
|
+
fs.rmSync(paneServerFile(dir, server, pane), { force: true });
|
|
137
|
+
}
|
|
138
|
+
catch { /* already gone */ }
|
|
139
|
+
}
|
|
140
|
+
export function freePort() {
|
|
141
|
+
return new Promise((resolve, reject) => {
|
|
142
|
+
const server = net.createServer();
|
|
143
|
+
server.once("error", reject);
|
|
144
|
+
server.listen(0, "127.0.0.1", () => {
|
|
145
|
+
const address = server.address();
|
|
146
|
+
const port = typeof address === "object" && address ? address.port : 0;
|
|
147
|
+
server.close(() => resolve(port));
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
export function newPassword() {
|
|
152
|
+
return crypto.randomBytes(32).toString("base64url");
|
|
153
|
+
}
|
|
154
|
+
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
155
|
+
export function openPaneClient(entry, fetchImpl = fetch) {
|
|
156
|
+
const base = `http://127.0.0.1:${entry.port}`;
|
|
157
|
+
const auth = "Basic " + Buffer.from(`opencode:${entry.password}`).toString("base64");
|
|
158
|
+
const send = (method, route, body, signal, timeout = true) => {
|
|
159
|
+
const url = new URL(route, base);
|
|
160
|
+
url.searchParams.set("directory", entry.directory);
|
|
161
|
+
const requestSignal = signal ?? (timeout === false ? undefined : AbortSignal.timeout(timeout === true ? REQUEST_TIMEOUT_MS : timeout));
|
|
162
|
+
return fetchImpl(url.toString(), { method,
|
|
163
|
+
headers: { Authorization: auth, ...(body === undefined ? {} : { "Content-Type": "application/json" }) },
|
|
164
|
+
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
165
|
+
...(requestSignal ? { signal: requestSignal } : {}) });
|
|
166
|
+
};
|
|
167
|
+
const json = async (method, route, body) => {
|
|
168
|
+
const response = await send(method, route, body);
|
|
169
|
+
const text = await response.text();
|
|
170
|
+
if (!response.ok)
|
|
171
|
+
throw new Error(`opencode ${method} ${route} failed with ${response.status}: ${text.slice(0, 300)}`);
|
|
172
|
+
return (text ? JSON.parse(text) : undefined);
|
|
173
|
+
};
|
|
174
|
+
const messages = async (sessionId) => {
|
|
175
|
+
const rows = await json("GET", `/session/${encodeURIComponent(sessionId)}/message?limit=${RECENT_MESSAGES}`);
|
|
176
|
+
return Array.isArray(rows) ? rows : [];
|
|
177
|
+
};
|
|
178
|
+
const sessionList = async () => {
|
|
179
|
+
const rows = await json("GET", "/session");
|
|
180
|
+
return Array.isArray(rows) ? rows : [];
|
|
181
|
+
};
|
|
182
|
+
const textOf = (message) => (message.parts ?? []).filter(part => part.type === "text").map(part => part.text ?? "").join("").trim();
|
|
183
|
+
return {
|
|
184
|
+
async ready(timeoutMs = 1_500) {
|
|
185
|
+
try {
|
|
186
|
+
return (await send("GET", "/session?limit=1", undefined, undefined, timeoutMs)).ok;
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
sessions: sessionList,
|
|
193
|
+
async session(id) {
|
|
194
|
+
const response = await send("GET", `/session/${encodeURIComponent(id)}`);
|
|
195
|
+
const text = await response.text();
|
|
196
|
+
if (response.status === 404)
|
|
197
|
+
return undefined;
|
|
198
|
+
if (!response.ok)
|
|
199
|
+
throw new Error(`opencode GET /session/{id} failed with ${response.status}.`);
|
|
200
|
+
const value = text ? JSON.parse(text) : undefined;
|
|
201
|
+
return value && typeof value.id === "string" ? value : undefined;
|
|
202
|
+
},
|
|
203
|
+
async createSession() {
|
|
204
|
+
const created = await json("POST", "/session", {});
|
|
205
|
+
if (!created || typeof created.id !== "string")
|
|
206
|
+
throw new Error("opencode POST /session returned no session.");
|
|
207
|
+
return created;
|
|
208
|
+
},
|
|
209
|
+
async selectSession(id) {
|
|
210
|
+
await json("POST", "/tui/select-session", { sessionID: id });
|
|
211
|
+
},
|
|
212
|
+
async setTitle(sessionId, title) {
|
|
213
|
+
const renamed = await json("PATCH", `/session/${encodeURIComponent(sessionId)}`, { title });
|
|
214
|
+
return typeof renamed?.title === "string" ? renamed.title : undefined;
|
|
215
|
+
},
|
|
216
|
+
async currentSession() {
|
|
217
|
+
const list = (await sessionList()).filter(session => typeof session.id === "string" && !session.parentID);
|
|
218
|
+
const matching = list.filter(session => !session.directory || session.directory === entry.directory);
|
|
219
|
+
const pool = matching.length > 0 ? matching : list;
|
|
220
|
+
return pool.sort((a, b) => (b.time?.updated ?? 0) - (a.time?.updated ?? 0))[0];
|
|
221
|
+
},
|
|
222
|
+
async prompt(sessionId, text, opts = {}) {
|
|
223
|
+
let before;
|
|
224
|
+
try {
|
|
225
|
+
before = await messages(sessionId);
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
throw new PromptNotSent(error.message);
|
|
229
|
+
}
|
|
230
|
+
const beforeIds = new Set(before.map(message => message.info?.id).filter((id) => Boolean(id)));
|
|
231
|
+
const body = { parts: [{ type: "text", text }] };
|
|
232
|
+
const last = opts.inherit ? before.filter(message => message.info?.role === "user").at(-1)?.info : undefined;
|
|
233
|
+
if (!opts.model && !opts.agent && last) {
|
|
234
|
+
if (typeof last.agent === "string" && last.agent)
|
|
235
|
+
body.agent = last.agent;
|
|
236
|
+
if (typeof last.model?.providerID === "string" && typeof last.model.modelID === "string") {
|
|
237
|
+
body.model = { providerID: last.model.providerID, modelID: last.model.modelID };
|
|
238
|
+
if (typeof last.model.variant === "string" && last.model.variant)
|
|
239
|
+
body.variant = last.model.variant;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (opts.model) {
|
|
243
|
+
const slash = opts.model.indexOf("/");
|
|
244
|
+
body.model = { providerID: slash < 0 ? opts.model : opts.model.slice(0, slash), modelID: slash < 0 ? "" : opts.model.slice(slash + 1) };
|
|
245
|
+
}
|
|
246
|
+
if (opts.agent)
|
|
247
|
+
body.agent = opts.agent;
|
|
248
|
+
if (opts.variant)
|
|
249
|
+
body.variant = opts.variant;
|
|
250
|
+
let response;
|
|
251
|
+
try {
|
|
252
|
+
response = await send("POST", `/session/${encodeURIComponent(sessionId)}/prompt_async`, body);
|
|
253
|
+
}
|
|
254
|
+
catch (error) {
|
|
255
|
+
// Nothing listening: the prompt never left. Any other failure (a
|
|
256
|
+
// timeout, a reset) may have reached the server, so it is unconfirmed.
|
|
257
|
+
const code = (error.cause)?.code;
|
|
258
|
+
if (code === "ECONNREFUSED")
|
|
259
|
+
throw new PromptNotSent("opencode is not listening.");
|
|
260
|
+
return { delivered: false, reason: "timeout" };
|
|
261
|
+
}
|
|
262
|
+
const refused = await response.text().catch(() => "");
|
|
263
|
+
if (!response.ok)
|
|
264
|
+
throw new PromptNotSent(`opencode POST prompt_async failed with ${response.status}: ${refused.slice(0, 300)}`);
|
|
265
|
+
const deadline = Date.now() + (opts.timeoutMs ?? DEFAULT_PROMPT_TIMEOUT_MS);
|
|
266
|
+
for (;;) {
|
|
267
|
+
await sleep(PROMPT_POLL_MS);
|
|
268
|
+
const after = await messages(sessionId).catch(() => []);
|
|
269
|
+
for (const message of after) {
|
|
270
|
+
const id = message.info?.id;
|
|
271
|
+
if (message.info?.role !== "user" || !id || beforeIds.has(id))
|
|
272
|
+
continue;
|
|
273
|
+
if (textOf(message) === text.trim())
|
|
274
|
+
return { delivered: true, messageId: id };
|
|
275
|
+
}
|
|
276
|
+
if (Date.now() >= deadline)
|
|
277
|
+
return { delivered: false, reason: "timeout" };
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
async permissions() {
|
|
281
|
+
const rows = await json("GET", "/permission");
|
|
282
|
+
return Array.isArray(rows) ? rows : [];
|
|
283
|
+
},
|
|
284
|
+
async replyPermission(id, reply, message) {
|
|
285
|
+
await json("POST", `/permission/${encodeURIComponent(id)}/reply`, { reply, ...(message === undefined ? {} : { message }) });
|
|
286
|
+
},
|
|
287
|
+
async questions() {
|
|
288
|
+
const rows = await json("GET", "/question");
|
|
289
|
+
return Array.isArray(rows) ? rows : [];
|
|
290
|
+
},
|
|
291
|
+
async replyQuestion(id, answers) {
|
|
292
|
+
await json("POST", `/question/${encodeURIComponent(id)}/reply`, { answers });
|
|
293
|
+
},
|
|
294
|
+
async rejectQuestion(id) {
|
|
295
|
+
await json("POST", `/question/${encodeURIComponent(id)}/reject`);
|
|
296
|
+
},
|
|
297
|
+
async abort(sessionId) {
|
|
298
|
+
return json("POST", `/session/${encodeURIComponent(sessionId)}/abort`);
|
|
299
|
+
},
|
|
300
|
+
async *events(signal) {
|
|
301
|
+
const response = await send("GET", "/event", undefined, signal, false);
|
|
302
|
+
if (!response.ok || !response.body)
|
|
303
|
+
throw new Error(`opencode GET /event failed with ${response.status}.`);
|
|
304
|
+
const reader = response.body.getReader();
|
|
305
|
+
const decoder = new TextDecoder();
|
|
306
|
+
let pending = "";
|
|
307
|
+
try {
|
|
308
|
+
for (;;) {
|
|
309
|
+
const { value, done } = await reader.read();
|
|
310
|
+
if (done)
|
|
311
|
+
return;
|
|
312
|
+
pending += decoder.decode(value, { stream: true });
|
|
313
|
+
let boundary;
|
|
314
|
+
while ((boundary = pending.indexOf("\n\n")) >= 0) {
|
|
315
|
+
const block = pending.slice(0, boundary);
|
|
316
|
+
pending = pending.slice(boundary + 2);
|
|
317
|
+
const data = block.split("\n").filter(row => row.startsWith("data:")).map(row => row.slice(5).trimStart()).join("\n");
|
|
318
|
+
if (!data)
|
|
319
|
+
continue;
|
|
320
|
+
try {
|
|
321
|
+
yield JSON.parse(data);
|
|
322
|
+
}
|
|
323
|
+
catch { /* keep-alive or malformed frame */ }
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
finally {
|
|
328
|
+
await reader.cancel().catch(() => { });
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { type OpenCodePermission, type OpenCodeQuestion, type PaneClient, type PaneServerEntry, type PromptOptions } from "./opencode-pane-server.js";
|
|
2
|
+
/** Set in a served pane's environment: the port its TUI listens on. The
|
|
3
|
+
* OpenCode plugin leaves permission asks to the Hook's HTTP client when its
|
|
4
|
+
* own process was started with this port. */
|
|
5
|
+
export declare const PANE_PORT_ENV = "PHREN_OPENCODE_PORT";
|
|
6
|
+
export declare function paneServersDir(): string;
|
|
7
|
+
/** The live server entry for a pane, if the Hook started its OpenCode. */
|
|
8
|
+
export declare function servedPane(server: string, pane: string): PaneServerEntry | undefined;
|
|
9
|
+
export declare function servedPanes(): PaneServerEntry[];
|
|
10
|
+
export declare function forgetServedPane(server: string, pane: string): void;
|
|
11
|
+
export declare function paneClient(entry: PaneServerEntry): PaneClient;
|
|
12
|
+
/** For tests: build clients against a fake server; returns the restore. */
|
|
13
|
+
export declare function setPaneClientFactory(factory: (entry: PaneServerEntry) => PaneClient): () => void;
|
|
14
|
+
export interface ServedLaunch {
|
|
15
|
+
port: number;
|
|
16
|
+
password: string;
|
|
17
|
+
args: string[];
|
|
18
|
+
env: Record<string, string>;
|
|
19
|
+
}
|
|
20
|
+
/** The port, password, extra arguments and pane variables for one launch. */
|
|
21
|
+
export declare function prepareServedLaunch(): Promise<ServedLaunch>;
|
|
22
|
+
/** `--port N` or `--port=N` in a command line. */
|
|
23
|
+
export declare function listensOn(command: string, port: number): boolean;
|
|
24
|
+
declare function processTable(): Promise<Array<{
|
|
25
|
+
pid: number;
|
|
26
|
+
command: string;
|
|
27
|
+
}>>;
|
|
28
|
+
/** The OpenCode process serving `port` in the pane: a foreground process of
|
|
29
|
+
* the pane whose command line carries that port, else any process that does
|
|
30
|
+
* (a launcher shim can hold the pane's foreground). The newest wins, which is
|
|
31
|
+
* the real binary under a shim. */
|
|
32
|
+
export declare function servingPid(server: string, pane: string, port: number, table?: typeof processTable): Promise<number | undefined>;
|
|
33
|
+
/**
|
|
34
|
+
* Registers a pane whose OpenCode the Hook just started with `launch`, once
|
|
35
|
+
* its server answers. Undefined when it never did: the pane then keeps the
|
|
36
|
+
* typed path, exactly like one started by hand.
|
|
37
|
+
*/
|
|
38
|
+
export declare function registerServedPane(server: string, pane: string, directory: string, launch: ServedLaunch, options?: {
|
|
39
|
+
readyMs?: number;
|
|
40
|
+
defaults?: PaneServerEntry["defaults"];
|
|
41
|
+
table?: () => Promise<Array<{
|
|
42
|
+
pid: number;
|
|
43
|
+
command: string;
|
|
44
|
+
}>>;
|
|
45
|
+
}): Promise<PaneServerEntry | undefined>;
|
|
46
|
+
export type ServedPrompt = {
|
|
47
|
+
sent: false;
|
|
48
|
+
reason: string;
|
|
49
|
+
} | {
|
|
50
|
+
sent: true;
|
|
51
|
+
delivered: boolean;
|
|
52
|
+
session: string;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Moves the TUI onto `session`, whose first turn is `text`. A TUI that has
|
|
56
|
+
* only just started answers HTTP before it listens for its own navigation
|
|
57
|
+
* events and drops the first request (seen live on 1.18.31), so the request
|
|
58
|
+
* is repeated until the pane draws that turn. False when it never did: the
|
|
59
|
+
* conversation is still real, only not on screen.
|
|
60
|
+
*/
|
|
61
|
+
export declare function showSession(entry: PaneServerEntry, client: PaneClient, session: string, text: string, options?: {
|
|
62
|
+
timeoutMs?: number;
|
|
63
|
+
intervalMs?: number;
|
|
64
|
+
shows?: (text: string) => Promise<boolean>;
|
|
65
|
+
}): Promise<boolean>;
|
|
66
|
+
/**
|
|
67
|
+
* Sends `text` into the session a served TUI shows. `session` is the
|
|
68
|
+
* conversation the phone means and must be a root session of this server; with
|
|
69
|
+
* none (a TUI still on its home screen) a new session is created, and once the
|
|
70
|
+
* prompt is in, the TUI is moved onto it so the owner sees it there.
|
|
71
|
+
* `sent: false` means nothing reached OpenCode, so the caller may type it.
|
|
72
|
+
*/
|
|
73
|
+
export declare function sendServedPrompt(entry: PaneServerEntry, session: string | undefined, text: string, options?: PromptOptions): Promise<ServedPrompt>;
|
|
74
|
+
/** A launch brief sent over HTTP: into a new session the TUI shows. */
|
|
75
|
+
export declare function sendServedBrief(entry: PaneServerEntry, text: string, options?: PromptOptions): Promise<ServedPrompt>;
|
|
76
|
+
/** The root of a (possibly subagent) session: the conversation the pane shows. */
|
|
77
|
+
export declare function rootSession(client: PaneClient, session: string): Promise<string>;
|
|
78
|
+
export interface PaneAsks {
|
|
79
|
+
permissions: OpenCodePermission[];
|
|
80
|
+
questions: OpenCodeQuestion[];
|
|
81
|
+
}
|
|
82
|
+
/** Where the watcher reports what each served pane is asking. `asks` is the
|
|
83
|
+
* whole current set for that pane; `gone` means the pane's entry is gone. */
|
|
84
|
+
export interface PaneAskSink {
|
|
85
|
+
asks(entry: PaneServerEntry, client: PaneClient, asks: PaneAsks): Promise<void>;
|
|
86
|
+
gone(key: string): void;
|
|
87
|
+
}
|
|
88
|
+
export declare const paneKey: (entry: {
|
|
89
|
+
server: string;
|
|
90
|
+
pane: string;
|
|
91
|
+
}) => string;
|
|
92
|
+
/**
|
|
93
|
+
* One event subscription per served pane. On every (re)connect and on each
|
|
94
|
+
* permission or question event it lists the pane's pending asks and hands the
|
|
95
|
+
* whole set to the sink, so an ask answered in the TUI disappears too. A
|
|
96
|
+
* dropped stream reconnects with backoff; `tick` stops panes whose entry is
|
|
97
|
+
* gone (the process exited) and starts new ones.
|
|
98
|
+
*/
|
|
99
|
+
export declare class PaneServerWatcher {
|
|
100
|
+
private sink;
|
|
101
|
+
private list;
|
|
102
|
+
private minBackoffMs;
|
|
103
|
+
private live;
|
|
104
|
+
private closed;
|
|
105
|
+
constructor(sink: PaneAskSink, list?: () => PaneServerEntry[], minBackoffMs?: number);
|
|
106
|
+
tick(): void;
|
|
107
|
+
watching(): string[];
|
|
108
|
+
close(): void;
|
|
109
|
+
private follow;
|
|
110
|
+
}
|
|
111
|
+
export {};
|