wave-code 1.0.6 → 1.0.8
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/components/AgentsManager.d.ts +7 -0
- package/dist/components/AgentsManager.js +109 -0
- package/dist/components/ChatInterface.js +1 -1
- package/dist/components/ConfirmationDetails.d.ts +1 -0
- package/dist/components/ConfirmationDetails.js +5 -3
- package/dist/components/ConfirmationSelector.js +17 -3
- package/dist/components/InputBox.js +7 -21
- package/dist/components/LoginCommand.js +31 -2
- package/dist/components/MarketplaceAddForm.js +16 -2
- package/dist/components/RewindCommand.js +11 -4
- package/dist/constants/commands.js +6 -0
- package/dist/contexts/useChat.d.ts +18 -2
- package/dist/contexts/useChat.js +114 -9
- package/dist/daemon/commands.d.ts +49 -0
- package/dist/daemon/commands.js +341 -0
- package/dist/daemon/jsonRpcClient.d.ts +38 -0
- package/dist/daemon/jsonRpcClient.js +129 -0
- package/dist/daemon/socketClient.d.ts +13 -0
- package/dist/daemon/socketClient.js +26 -0
- package/dist/hooks/useInputManager.d.ts +2 -0
- package/dist/hooks/useInputManager.js +8 -0
- package/dist/index.js +88 -0
- package/dist/managers/inputHandlers.js +3 -0
- package/dist/managers/inputReducer.d.ts +4 -0
- package/dist/managers/inputReducer.js +8 -0
- package/dist/reducers/agentsManagerReducer.d.ts +26 -0
- package/dist/reducers/agentsManagerReducer.js +54 -0
- package/dist/stdio/agentBridge.d.ts +15 -0
- package/dist/stdio/agentBridge.js +101 -20
- package/dist/stdio/protocol.d.ts +1 -1
- package/dist/utils/usageSummary.d.ts +0 -4
- package/dist/utils/usageSummary.js +1 -34
- package/package.json +2 -2
- package/src/components/AgentsManager.tsx +290 -0
- package/src/components/ChatInterface.tsx +2 -0
- package/src/components/ConfirmationDetails.tsx +6 -0
- package/src/components/ConfirmationSelector.tsx +18 -3
- package/src/components/InputBox.tsx +54 -45
- package/src/components/LoginCommand.tsx +35 -2
- package/src/components/MarketplaceAddForm.tsx +17 -2
- package/src/components/RewindCommand.tsx +10 -4
- package/src/constants/commands.ts +6 -0
- package/src/contexts/useChat.tsx +146 -7
- package/src/daemon/commands.ts +444 -0
- package/src/daemon/jsonRpcClient.ts +158 -0
- package/src/daemon/socketClient.ts +34 -0
- package/src/hooks/useInputManager.ts +8 -0
- package/src/index.ts +130 -0
- package/src/managers/inputHandlers.ts +2 -0
- package/src/managers/inputReducer.ts +10 -0
- package/src/reducers/agentsManagerReducer.ts +91 -0
- package/src/stdio/agentBridge.ts +123 -19
- package/src/stdio/protocol.ts +4 -0
- package/src/utils/usageSummary.ts +2 -46
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `wave daemon` client subcommands — talk to the wave daemon's unix socket
|
|
3
|
+
* (JSON-RPC over newline-delimited JSON) to list hosted sessions, inspect
|
|
4
|
+
* progress, inject messages and respond to pending permission requests.
|
|
5
|
+
*
|
|
6
|
+
* All subcommands are non-interactive: results go to stdout, diagnostics to
|
|
7
|
+
* stderr, and every handler calls process.exit() itself (yargs would fall
|
|
8
|
+
* through to the TUI otherwise). Every command connects to the fixed default
|
|
9
|
+
* socket `~/.wave/daemon.sock` — the daemon only runs on remote hosts, so no
|
|
10
|
+
* `--socket` override is offered (spec: daemon-command.md).
|
|
11
|
+
*
|
|
12
|
+
* Attach semantics: `initialize {workdir, restoreSessionId}` + `restoreSession`
|
|
13
|
+
* re-attach to a live session in the daemon's in-memory registry, or reload a
|
|
14
|
+
* transcript from disk under the current working directory. A session that is
|
|
15
|
+
* nowhere (live registry or disk) silently starts a FRESH session under a
|
|
16
|
+
* different id — the only reliable existence check is the `restoreSession`
|
|
17
|
+
* rejection ("Session not found: <id>"), after which the junk fresh session
|
|
18
|
+
* must be destroyed via the envelope sessionId returned by `initialize`.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import net from "node:net";
|
|
22
|
+
import os from "node:os";
|
|
23
|
+
import path from "node:path";
|
|
24
|
+
import {
|
|
25
|
+
ASK_USER_QUESTION_TOOL_NAME,
|
|
26
|
+
ENTER_PLAN_MODE_TOOL_NAME,
|
|
27
|
+
EXIT_PLAN_MODE_TOOL_NAME,
|
|
28
|
+
getMessageContent,
|
|
29
|
+
type Message,
|
|
30
|
+
type PermissionDecision,
|
|
31
|
+
type PermissionMode,
|
|
32
|
+
type ToolPermissionContext,
|
|
33
|
+
} from "wave-agent-sdk";
|
|
34
|
+
import { SocketClient } from "./socketClient.js";
|
|
35
|
+
|
|
36
|
+
/** Fixed default daemon socket (spec: 默认 socket 固定,无 --socket 覆盖). */
|
|
37
|
+
export const DEFAULT_DAEMON_SOCKET = path.join(
|
|
38
|
+
os.homedir(),
|
|
39
|
+
".wave",
|
|
40
|
+
"daemon.sock",
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const PERMISSION_MODES: PermissionMode[] = [
|
|
44
|
+
"default",
|
|
45
|
+
"bypassPermissions",
|
|
46
|
+
"acceptEdits",
|
|
47
|
+
"plan",
|
|
48
|
+
"dontAsk",
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
// ── Connection helpers ─────────────────────────────────────────
|
|
52
|
+
|
|
53
|
+
function connectDaemon(socketPath: string): Promise<SocketClient> {
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
const socket = net.createConnection(socketPath);
|
|
56
|
+
socket.once("connect", () => resolve(new SocketClient(socket)));
|
|
57
|
+
socket.once("error", (err) => {
|
|
58
|
+
socket.destroy();
|
|
59
|
+
reject(err);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Connect or fail fast with the spec'd error; daemon idle-exits after 60s. */
|
|
65
|
+
async function connectDaemonOrExit(socketPath: string): Promise<SocketClient> {
|
|
66
|
+
try {
|
|
67
|
+
return await connectDaemon(socketPath);
|
|
68
|
+
} catch (err) {
|
|
69
|
+
const code = (err as NodeJS.ErrnoException).code;
|
|
70
|
+
console.error(
|
|
71
|
+
`无法连接 daemon socket ${socketPath}:daemon 未运行?(daemon 空闲 60 秒自动退出)` +
|
|
72
|
+
(code ? ` (${code})` : ""),
|
|
73
|
+
);
|
|
74
|
+
process.exit(1);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function fail(message: string): never {
|
|
79
|
+
console.error(message);
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
interface DaemonSessionEntry {
|
|
84
|
+
sessionId: string;
|
|
85
|
+
workingDirectory: string;
|
|
86
|
+
isLoading: boolean;
|
|
87
|
+
messageCount: number;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
interface PendingPermission {
|
|
91
|
+
requestId: string;
|
|
92
|
+
sessionId?: string;
|
|
93
|
+
context: ToolPermissionContext;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Attach to a session; returns the initialized sessionId + working directory.
|
|
98
|
+
* Exits (nonzero) with the spec'd error when the session exists neither in the
|
|
99
|
+
* daemon registry nor on disk, destroying the fresh session that `initialize`
|
|
100
|
+
* silently created.
|
|
101
|
+
*/
|
|
102
|
+
async function attachSession(
|
|
103
|
+
client: SocketClient,
|
|
104
|
+
sessionId: string,
|
|
105
|
+
): Promise<{ sessionId: string; workingDirectory: string }> {
|
|
106
|
+
const init = (await client.request("initialize", {
|
|
107
|
+
workdir: process.cwd(),
|
|
108
|
+
restoreSessionId: sessionId,
|
|
109
|
+
})) as { sessionId: string; workingDirectory: string };
|
|
110
|
+
const initId = init.sessionId;
|
|
111
|
+
try {
|
|
112
|
+
await client.request("restoreSession", { sessionId }, initId);
|
|
113
|
+
} catch (err) {
|
|
114
|
+
if ((err as Error).message.includes("Session not found")) {
|
|
115
|
+
// initialize silently started a junk fresh session — remove it from the
|
|
116
|
+
// registry so the failed attach leaves no trace (spec: 会话不存在错误).
|
|
117
|
+
await client.request("destroy", undefined, initId).catch(() => {});
|
|
118
|
+
fail(`会话不存在或未被该 daemon 托管:${sessionId}`);
|
|
119
|
+
}
|
|
120
|
+
throw err;
|
|
121
|
+
}
|
|
122
|
+
return init;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function listPendingPermissions(
|
|
126
|
+
client: SocketClient,
|
|
127
|
+
): Promise<PendingPermission[]> {
|
|
128
|
+
const result = (await client.request("listPendingPermissions")) as {
|
|
129
|
+
requests: PendingPermission[];
|
|
130
|
+
};
|
|
131
|
+
return result.requests ?? [];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function sleep(ms: number): Promise<void> {
|
|
135
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// ── list ───────────────────────────────────────────────────────
|
|
139
|
+
|
|
140
|
+
export async function daemonListCommand(socketPath: string): Promise<void> {
|
|
141
|
+
let client: SocketClient | undefined;
|
|
142
|
+
try {
|
|
143
|
+
client = await connectDaemonOrExit(socketPath);
|
|
144
|
+
const result = (await client.request("listDaemonSessions")) as {
|
|
145
|
+
sessions: DaemonSessionEntry[];
|
|
146
|
+
};
|
|
147
|
+
const sessions = result.sessions ?? [];
|
|
148
|
+
|
|
149
|
+
if (sessions.length > 0) {
|
|
150
|
+
const rows = sessions.map((s) => ({
|
|
151
|
+
sessionId: s.sessionId,
|
|
152
|
+
status: s.isLoading ? "生成中" : "空闲",
|
|
153
|
+
messageCount: String(s.messageCount),
|
|
154
|
+
workingDirectory: s.workingDirectory,
|
|
155
|
+
}));
|
|
156
|
+
const width = (key: keyof (typeof rows)[number]) =>
|
|
157
|
+
Math.max(...rows.map((r) => r[key].length), key.length);
|
|
158
|
+
const pad = (value: string, w: number) => value.padEnd(w);
|
|
159
|
+
|
|
160
|
+
console.log(
|
|
161
|
+
`${pad("会话", width("sessionId"))} ${pad("状态", width("status"))} ${pad("消息数", width("messageCount"))} 工作目录`,
|
|
162
|
+
);
|
|
163
|
+
for (const r of rows) {
|
|
164
|
+
console.log(
|
|
165
|
+
`${pad(r.sessionId, width("sessionId"))} ${pad(r.status, width("status"))} ${pad(r.messageCount, width("messageCount"))} ${r.workingDirectory}`,
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
// Daemon idle-exit is normal — an empty registry is not an error.
|
|
170
|
+
console.log("无会话");
|
|
171
|
+
}
|
|
172
|
+
} catch (err) {
|
|
173
|
+
fail(`wave daemon list 失败:${(err as Error).message}`);
|
|
174
|
+
} finally {
|
|
175
|
+
await client?.dispose();
|
|
176
|
+
}
|
|
177
|
+
// Exits outside the try so the success path's exit is never re-wrapped by the
|
|
178
|
+
// error handler above.
|
|
179
|
+
process.exit(0);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ── status ─────────────────────────────────────────────────────
|
|
183
|
+
|
|
184
|
+
function summarizeToolInput(context: ToolPermissionContext): string {
|
|
185
|
+
const input = context.toolInput;
|
|
186
|
+
if (!input || Object.keys(input).length === 0) return "";
|
|
187
|
+
let text: string;
|
|
188
|
+
try {
|
|
189
|
+
text = JSON.stringify(input);
|
|
190
|
+
} catch {
|
|
191
|
+
text = "";
|
|
192
|
+
}
|
|
193
|
+
return text.length > 80 ? `${text.slice(0, 80)}…` : text;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export async function daemonStatusCommand(
|
|
197
|
+
socketPath: string,
|
|
198
|
+
sessionId: string,
|
|
199
|
+
lines = 20,
|
|
200
|
+
): Promise<void> {
|
|
201
|
+
let client: SocketClient | undefined;
|
|
202
|
+
try {
|
|
203
|
+
client = await connectDaemonOrExit(socketPath);
|
|
204
|
+
|
|
205
|
+
// Subscribe BEFORE initialize/restoreSession so the replayed loadingChange
|
|
206
|
+
// snapshot is captured (spec: 依据重放的 loadingChange 快照显示状态).
|
|
207
|
+
let loading = false;
|
|
208
|
+
client.onNotification("loadingChange", (params) => {
|
|
209
|
+
loading = (params as { loading: boolean }).loading;
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
const init = await attachSession(client, sessionId);
|
|
213
|
+
const initId = init.sessionId;
|
|
214
|
+
|
|
215
|
+
// listPendingPermissions is the authoritative "waiting for approval" signal
|
|
216
|
+
// (spec: 单凭消息无法区分等审批与执行中,须结合 listPendingPermissions).
|
|
217
|
+
const pending = (await listPendingPermissions(client)).filter(
|
|
218
|
+
(r) => r.sessionId === initId || r.sessionId === sessionId,
|
|
219
|
+
);
|
|
220
|
+
const messages = (await client.request(
|
|
221
|
+
"getMessages",
|
|
222
|
+
undefined,
|
|
223
|
+
initId,
|
|
224
|
+
)) as {
|
|
225
|
+
messages: Message[];
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const status =
|
|
229
|
+
pending.length > 0 ? "等待审批" : loading ? "生成中" : "空闲";
|
|
230
|
+
console.log(`会话: ${initId}`);
|
|
231
|
+
console.log(`工作目录: ${init.workingDirectory}`);
|
|
232
|
+
console.log(`状态: ${status}`);
|
|
233
|
+
|
|
234
|
+
if (pending.length > 0) {
|
|
235
|
+
console.log("");
|
|
236
|
+
console.log("待审批请求:");
|
|
237
|
+
for (const r of pending) {
|
|
238
|
+
const params = summarizeToolInput(r.context);
|
|
239
|
+
console.log(
|
|
240
|
+
` ${r.requestId} ${r.context.toolName}${params ? ` ${params}` : ""}`,
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const recent = messages.messages.slice(-lines);
|
|
246
|
+
if (recent.length > 0) {
|
|
247
|
+
console.log("");
|
|
248
|
+
console.log(`最近消息 (${recent.length}):`);
|
|
249
|
+
for (const m of recent) {
|
|
250
|
+
const text = getMessageContent(m).replace(/\s+/g, " ").trim();
|
|
251
|
+
if (!text) continue; // tool-only messages carry no readable text
|
|
252
|
+
console.log(` [${m.role}] ${text}`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
} catch (err) {
|
|
256
|
+
fail(`wave daemon status 失败:${(err as Error).message}`);
|
|
257
|
+
} finally {
|
|
258
|
+
await client?.dispose();
|
|
259
|
+
}
|
|
260
|
+
process.exit(0);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// ── send ───────────────────────────────────────────────────────
|
|
264
|
+
|
|
265
|
+
export interface SendOptions {
|
|
266
|
+
timeout: number; // seconds; 0 = no limit (default 600)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Send a message and wait for the reply that corresponds to it.
|
|
271
|
+
*
|
|
272
|
+
* Completion detection: `sendMessage` on an idle session resolves only after
|
|
273
|
+
* the whole turn finishes (InteractionService awaits sendAIMessage), while on a
|
|
274
|
+
* busy session it enqueues and returns immediately — so stopping on a bare
|
|
275
|
+
* `loadingChange:false` would exit early on the PREVIOUS turn's completion when
|
|
276
|
+
* queued behind a busy session. Instead, track the message IDs: `ourUserMessage`
|
|
277
|
+
* is the user message added when OUR turn starts (userMessageAdded), and the
|
|
278
|
+
* reply is the last assistantMessageAdded observed after it. A stale
|
|
279
|
+
* loading:false can then never satisfy the wait condition early (the reply has
|
|
280
|
+
* not been added yet).
|
|
281
|
+
*/
|
|
282
|
+
export async function daemonSendCommand(
|
|
283
|
+
socketPath: string,
|
|
284
|
+
sessionId: string,
|
|
285
|
+
message: string,
|
|
286
|
+
options: SendOptions = { timeout: 600 },
|
|
287
|
+
): Promise<void> {
|
|
288
|
+
// connectDaemonOrExit exits on failure — no client to dispose in that case.
|
|
289
|
+
const client = await connectDaemonOrExit(socketPath);
|
|
290
|
+
|
|
291
|
+
let loading = false;
|
|
292
|
+
let sent = false;
|
|
293
|
+
let ourUserMessageId: string | undefined;
|
|
294
|
+
let replyMessageId: string | undefined;
|
|
295
|
+
client.onNotification("userMessageAdded", (params) => {
|
|
296
|
+
if (!sent) return; // ignore messages added during attach
|
|
297
|
+
ourUserMessageId = (params as { message: Message }).message.id;
|
|
298
|
+
});
|
|
299
|
+
client.onNotification("assistantMessageAdded", (params) => {
|
|
300
|
+
if (!sent || ourUserMessageId === undefined) return; // not our turn yet
|
|
301
|
+
replyMessageId = (params as { message: Message }).message.id;
|
|
302
|
+
});
|
|
303
|
+
client.onNotification("loadingChange", (params) => {
|
|
304
|
+
loading = (params as { loading: boolean }).loading;
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
let initId: string;
|
|
308
|
+
try {
|
|
309
|
+
initId = (await attachSession(client, sessionId)).sessionId;
|
|
310
|
+
sent = true;
|
|
311
|
+
await client.request("sendMessage", { text: message }, initId);
|
|
312
|
+
} catch (err) {
|
|
313
|
+
client.dispose();
|
|
314
|
+
fail(`wave daemon send 失败:${(err as Error).message}`);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Wait for the reply that corresponds to our message.
|
|
318
|
+
const started = Date.now();
|
|
319
|
+
const timeoutMs = options.timeout === 0 ? Infinity : options.timeout * 1000;
|
|
320
|
+
while (!(loading === false && replyMessageId !== undefined)) {
|
|
321
|
+
if (Date.now() - started > timeoutMs) {
|
|
322
|
+
// Timeout backstop: the most likely cause is a session waiting on a
|
|
323
|
+
// permission approval — point the user at respond (spec: 不无限期挂起).
|
|
324
|
+
const pending = (await listPendingPermissions(client)).filter(
|
|
325
|
+
(r) => r.sessionId === sessionId || r.sessionId === initId,
|
|
326
|
+
);
|
|
327
|
+
client.dispose();
|
|
328
|
+
if (pending.length > 0) {
|
|
329
|
+
fail(
|
|
330
|
+
`会话等待权限审批,请通过 \`wave daemon respond ${sessionId} ${pending[0].requestId}\` 处理后重试`,
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
fail(
|
|
334
|
+
options.timeout === 0
|
|
335
|
+
? "等待回复超时"
|
|
336
|
+
: `等待回复超时(${options.timeout} 秒),未收到助手回复`,
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
await sleep(200);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
try {
|
|
343
|
+
const result = (await client.request("getMessages", undefined, initId)) as {
|
|
344
|
+
messages: Message[];
|
|
345
|
+
};
|
|
346
|
+
const reply = result.messages.find((m) => m.id === replyMessageId);
|
|
347
|
+
// Pure final-reply text only; streaming deltas / subagent internals never
|
|
348
|
+
// reach stdout (spec: send 输出纯净性).
|
|
349
|
+
if (reply) {
|
|
350
|
+
const content = getMessageContent(reply).replace(/\s+/g, " ").trim();
|
|
351
|
+
if (content) console.log(content);
|
|
352
|
+
}
|
|
353
|
+
} catch (err) {
|
|
354
|
+
fail(`wave daemon send 失败:${(err as Error).message}`);
|
|
355
|
+
} finally {
|
|
356
|
+
client.dispose();
|
|
357
|
+
}
|
|
358
|
+
process.exit(0);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// ── respond ────────────────────────────────────────────────────
|
|
362
|
+
|
|
363
|
+
export interface RespondOptions {
|
|
364
|
+
allow?: boolean;
|
|
365
|
+
deny?: boolean;
|
|
366
|
+
reason?: string;
|
|
367
|
+
answer?: string;
|
|
368
|
+
rule?: string;
|
|
369
|
+
mode?: string;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
export async function daemonRespondCommand(
|
|
373
|
+
socketPath: string,
|
|
374
|
+
sessionId: string,
|
|
375
|
+
requestId: string,
|
|
376
|
+
options: RespondOptions,
|
|
377
|
+
): Promise<void> {
|
|
378
|
+
if (!!options.allow === !!options.deny) {
|
|
379
|
+
fail("请指定 --allow 或 --deny(二选一)");
|
|
380
|
+
}
|
|
381
|
+
let client: SocketClient | undefined;
|
|
382
|
+
try {
|
|
383
|
+
client = await connectDaemonOrExit(socketPath);
|
|
384
|
+
|
|
385
|
+
// The server silently ignores permissionResponse for unknown requestIds —
|
|
386
|
+
// validate first so the user is never misled into thinking approval landed.
|
|
387
|
+
const pending = await listPendingPermissions(client);
|
|
388
|
+
const req = pending.find((r) => r.requestId === requestId);
|
|
389
|
+
if (!req) {
|
|
390
|
+
fail("该请求不存在或已处理");
|
|
391
|
+
}
|
|
392
|
+
if (req.sessionId && req.sessionId !== sessionId) {
|
|
393
|
+
// Cross-check before notifying; never touch another session's request.
|
|
394
|
+
fail("会话不存在或未被该 daemon 托管");
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
let decision: PermissionDecision;
|
|
398
|
+
if (options.deny) {
|
|
399
|
+
decision = { behavior: "deny", message: options.reason };
|
|
400
|
+
} else {
|
|
401
|
+
// Per-tool auto-completion, mirroring the desktop ConfirmationDialog
|
|
402
|
+
// semantics (spec: 决策并非单一 allow/deny,须按工具智能补全).
|
|
403
|
+
const toolName = req.context.toolName;
|
|
404
|
+
if (toolName === ENTER_PLAN_MODE_TOOL_NAME) {
|
|
405
|
+
decision = { behavior: "allow", newPermissionMode: "plan" };
|
|
406
|
+
} else if (toolName === EXIT_PLAN_MODE_TOOL_NAME) {
|
|
407
|
+
decision = { behavior: "allow", newPermissionMode: "default" };
|
|
408
|
+
} else if (toolName === ASK_USER_QUESTION_TOOL_NAME) {
|
|
409
|
+
if (!options.answer) {
|
|
410
|
+
fail("AskUserQuestion 请求需要 --answer 提供答案 JSON");
|
|
411
|
+
}
|
|
412
|
+
let answers: unknown;
|
|
413
|
+
try {
|
|
414
|
+
answers = JSON.parse(options.answer);
|
|
415
|
+
} catch {
|
|
416
|
+
fail("--answer 不是合法的 JSON");
|
|
417
|
+
}
|
|
418
|
+
decision = { behavior: "allow", message: JSON.stringify(answers) };
|
|
419
|
+
} else {
|
|
420
|
+
decision = { behavior: "allow" };
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
if (options.rule) decision.newPermissionRule = options.rule;
|
|
424
|
+
if (options.mode) {
|
|
425
|
+
if (!PERMISSION_MODES.includes(options.mode as PermissionMode)) {
|
|
426
|
+
fail(
|
|
427
|
+
`无效的权限模式:${options.mode}(可选:${PERMISSION_MODES.join("、")})`,
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
decision.newPermissionMode = options.mode as PermissionMode;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// Mirror desktop stdioAgent.sendPermissionResponse: envelope sessionId
|
|
435
|
+
// present, decision built from the pending request's tool.
|
|
436
|
+
client.notify("permissionResponse", { requestId, decision }, sessionId);
|
|
437
|
+
console.log(`已处理审批请求:${requestId}`);
|
|
438
|
+
} catch (err) {
|
|
439
|
+
fail(`wave daemon respond 失败:${(err as Error).message}`);
|
|
440
|
+
} finally {
|
|
441
|
+
await client?.dispose();
|
|
442
|
+
}
|
|
443
|
+
process.exit(0);
|
|
444
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JsonRpcClient — minimal JSON-RPC transport over a line-delimited duplex
|
|
3
|
+
* stream (one JSON object per line).
|
|
4
|
+
*
|
|
5
|
+
* Used by `wave daemon` subcommands to talk to the wave daemon's unix socket.
|
|
6
|
+
* Mirrors packages/desktop/src/main/stdio/jsonRpcClient.ts (packages/code
|
|
7
|
+
* cannot import from packages/desktop). Subclasses own the transport and hook
|
|
8
|
+
* in:
|
|
9
|
+
* - `writeLine(message)` writes one JSON line to the peer.
|
|
10
|
+
* - `attachReadable(readable)` wires the inbound half (socket).
|
|
11
|
+
* - `handleClosed(reason)` marks the transport dead and rejects every pending
|
|
12
|
+
* request. Idempotent — safe to call from both dispose() and an exit/close
|
|
13
|
+
* event on the underlying transport.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { createInterface } from "readline";
|
|
17
|
+
import type { Readable } from "stream";
|
|
18
|
+
|
|
19
|
+
export type NotificationHandler = (params: unknown, sessionId?: string) => void;
|
|
20
|
+
|
|
21
|
+
interface PendingRequest {
|
|
22
|
+
resolve: (value: unknown) => void;
|
|
23
|
+
reject: (error: Error) => void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export abstract class JsonRpcClient {
|
|
27
|
+
private nextId = 1;
|
|
28
|
+
private pending = new Map<number, PendingRequest>();
|
|
29
|
+
private handlers = new Map<string, Set<NotificationHandler>>();
|
|
30
|
+
private closedHandlers: Array<() => void> = [];
|
|
31
|
+
private closed = false;
|
|
32
|
+
|
|
33
|
+
// ── Transport hooks (subclass) ─────────────────────────────────
|
|
34
|
+
|
|
35
|
+
protected abstract writeLine(message: string): void;
|
|
36
|
+
|
|
37
|
+
/** Wire an inbound Readable (socket) to the line parser. */
|
|
38
|
+
protected attachReadable(readable: Readable): void {
|
|
39
|
+
const rl = createInterface({ input: readable });
|
|
40
|
+
rl.on("line", (line) => this.handleLine(line));
|
|
41
|
+
// readline re-emits input errors on the Interface; the transport subclass
|
|
42
|
+
// already handles errors on the underlying stream, swallow them here so
|
|
43
|
+
// they never surface as an uncaught 'error' on the Interface.
|
|
44
|
+
rl.on("error", () => {});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Mark the transport closed: reject every pending request. Idempotent. */
|
|
48
|
+
protected handleClosed(reason: string): void {
|
|
49
|
+
if (this.closed) return;
|
|
50
|
+
this.closed = true;
|
|
51
|
+
const error = new Error(reason);
|
|
52
|
+
for (const p of this.pending.values()) p.reject(error);
|
|
53
|
+
this.pending.clear();
|
|
54
|
+
for (const handler of this.closedHandlers) handler();
|
|
55
|
+
this.closedHandlers = [];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
protected get isClosed(): boolean {
|
|
59
|
+
return this.closed;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// ── Public API ─────────────────────────────────────────────────
|
|
63
|
+
|
|
64
|
+
/** Close the transport and reject pending requests (subclass tears down its stream). */
|
|
65
|
+
abstract dispose(): void;
|
|
66
|
+
|
|
67
|
+
/** Observe transport teardown (dispose or unexpected close), fired once. */
|
|
68
|
+
onClosed(handler: () => void): void {
|
|
69
|
+
this.closedHandlers.push(handler);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async request(
|
|
73
|
+
method: string,
|
|
74
|
+
params?: unknown,
|
|
75
|
+
sessionId?: string,
|
|
76
|
+
): Promise<unknown> {
|
|
77
|
+
if (this.closed) {
|
|
78
|
+
throw new Error(
|
|
79
|
+
"连接已断开。wave 进程已退出,请重启编辑器或检查 CLI 安装。",
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
const id = this.nextId++;
|
|
83
|
+
const envelope: Record<string, unknown> = { id, method, params };
|
|
84
|
+
if (sessionId) envelope.sessionId = sessionId;
|
|
85
|
+
const message = JSON.stringify(envelope) + "\n";
|
|
86
|
+
|
|
87
|
+
return new Promise((resolve, reject) => {
|
|
88
|
+
this.pending.set(id, { resolve, reject });
|
|
89
|
+
this.writeLine(message);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
notify(method: string, params?: unknown, sessionId?: string): void {
|
|
94
|
+
if (this.closed) return;
|
|
95
|
+
const envelope: Record<string, unknown> = { method, params };
|
|
96
|
+
if (sessionId) envelope.sessionId = sessionId;
|
|
97
|
+
this.writeLine(JSON.stringify(envelope) + "\n");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
onNotification(method: string, handler: NotificationHandler): void {
|
|
101
|
+
let set = this.handlers.get(method);
|
|
102
|
+
if (!set) {
|
|
103
|
+
set = new Set();
|
|
104
|
+
this.handlers.set(method, set);
|
|
105
|
+
}
|
|
106
|
+
set.add(handler);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
offNotification(method: string, handler: NotificationHandler): void {
|
|
110
|
+
this.handlers.get(method)?.delete(handler);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ── Internal ──────────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
private handleLine(line: string): void {
|
|
116
|
+
let msg: unknown;
|
|
117
|
+
try {
|
|
118
|
+
msg = JSON.parse(line);
|
|
119
|
+
} catch {
|
|
120
|
+
console.error("[wave-jsonrpc] Failed to parse:", line);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (typeof msg !== "object" || msg === null) return;
|
|
125
|
+
const obj = msg as Record<string, unknown>;
|
|
126
|
+
|
|
127
|
+
// Response (has id + result/error)
|
|
128
|
+
if ("id" in obj && ("result" in obj || "error" in obj)) {
|
|
129
|
+
const id = Number(obj.id);
|
|
130
|
+
const pending = this.pending.get(id);
|
|
131
|
+
if (pending) {
|
|
132
|
+
this.pending.delete(id);
|
|
133
|
+
if (obj.error) {
|
|
134
|
+
const err = obj.error as { code: number; message: string };
|
|
135
|
+
pending.reject(new Error(err.message));
|
|
136
|
+
} else {
|
|
137
|
+
pending.resolve(obj.result);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Notification (has method, no id)
|
|
144
|
+
if ("method" in obj && !("id" in obj)) {
|
|
145
|
+
const method = obj.method as string;
|
|
146
|
+
const params = obj.params;
|
|
147
|
+
const sessionId =
|
|
148
|
+
typeof obj.sessionId === "string" ? obj.sessionId : undefined;
|
|
149
|
+
const set = this.handlers.get(method);
|
|
150
|
+
if (set) {
|
|
151
|
+
for (const handler of set) {
|
|
152
|
+
handler(params, sessionId);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SocketClient — JSON-RPC transport over a unix socket connection to the wave
|
|
3
|
+
* daemon. Mirrors packages/desktop/src/main/stdio/socketClient.ts (packages/code
|
|
4
|
+
* cannot import from packages/desktop).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Socket } from "net";
|
|
8
|
+
import { JsonRpcClient } from "./jsonRpcClient.js";
|
|
9
|
+
|
|
10
|
+
export class SocketClient extends JsonRpcClient {
|
|
11
|
+
private socket: Socket;
|
|
12
|
+
|
|
13
|
+
constructor(socket: Socket) {
|
|
14
|
+
super();
|
|
15
|
+
this.socket = socket;
|
|
16
|
+
this.attachReadable(socket);
|
|
17
|
+
|
|
18
|
+
socket.on("close", () => {
|
|
19
|
+
this.handleClosed("远端连接已断开。");
|
|
20
|
+
});
|
|
21
|
+
socket.on("error", (err) => {
|
|
22
|
+
console.error("[wave-daemon] Socket error:", err.message);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
protected writeLine(message: string): void {
|
|
27
|
+
this.socket.write(message);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
dispose(): void {
|
|
31
|
+
this.handleClosed("远端连接已断开。");
|
|
32
|
+
this.socket.destroy();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -242,6 +242,8 @@ export const useInputManager = (
|
|
|
242
242
|
});
|
|
243
243
|
} else if (command === "mcp") {
|
|
244
244
|
dispatch({ type: "SET_SHOW_MCP_MANAGER", payload: true });
|
|
245
|
+
} else if (command === "agents") {
|
|
246
|
+
dispatch({ type: "SET_SHOW_AGENTS_MANAGER", payload: true });
|
|
245
247
|
} else if (command === "rewind") {
|
|
246
248
|
dispatch({ type: "SET_SHOW_REWIND_MANAGER", payload: true });
|
|
247
249
|
} else if (command === "help") {
|
|
@@ -493,6 +495,10 @@ export const useInputManager = (
|
|
|
493
495
|
dispatch({ type: "SET_SHOW_MCP_MANAGER", payload: show });
|
|
494
496
|
}, []);
|
|
495
497
|
|
|
498
|
+
const setShowAgentsManager = useCallback((show: boolean) => {
|
|
499
|
+
dispatch({ type: "SET_SHOW_AGENTS_MANAGER", payload: show });
|
|
500
|
+
}, []);
|
|
501
|
+
|
|
496
502
|
const setShowRewindManager = useCallback((show: boolean) => {
|
|
497
503
|
dispatch({ type: "SET_SHOW_REWIND_MANAGER", payload: show });
|
|
498
504
|
}, []);
|
|
@@ -657,6 +663,7 @@ export const useInputManager = (
|
|
|
657
663
|
historySearchQuery: state.historySearchQuery,
|
|
658
664
|
showBackgroundTaskManager: state.showBackgroundTaskManager,
|
|
659
665
|
showMcpManager: state.showMcpManager,
|
|
666
|
+
showAgentsManager: state.showAgentsManager,
|
|
660
667
|
showRewindManager: state.showRewindManager,
|
|
661
668
|
showHelp: state.showHelp,
|
|
662
669
|
showStatusCommand: state.showStatusCommand,
|
|
@@ -702,6 +709,7 @@ export const useInputManager = (
|
|
|
702
709
|
// Bash/MCP Manager
|
|
703
710
|
setShowBackgroundTaskManager,
|
|
704
711
|
setShowMcpManager,
|
|
712
|
+
setShowAgentsManager,
|
|
705
713
|
setShowRewindManager,
|
|
706
714
|
setShowHelp,
|
|
707
715
|
setShowStatusCommand,
|