@yitom/acp-client 0.1.2 → 0.1.4
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 +14 -0
- package/dist/client/acp-client.d.ts +8 -0
- package/dist/index.cjs +105 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +105 -0
- package/dist/session/available-commands.d.ts +43 -0
- package/dist/sidecar.cjs +108 -1
- package/dist/version.d.ts +1 -0
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -99,6 +99,20 @@ await client.disconnect()
|
|
|
99
99
|
|
|
100
100
|
---
|
|
101
101
|
|
|
102
|
+
## ⌨️ 斜杠命令(available_commands_update)
|
|
103
|
+
|
|
104
|
+
Agent 推送的 `available_commands_update` 会原样经 `onSessionUpdate` 透传,同时按会话缓存最新列表(含 `name` / `description` / `input.hint`),
|
|
105
|
+
即使宿主还没拿到 sessionId 也不会丢:
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
const handle = (await client.createSession()).value
|
|
109
|
+
const cmds = handle.availableCommands ?? (await client.waitForAvailableCommands(handle.sessionId, 500))
|
|
110
|
+
// 调用命令 = 普通文本 prompt(ACP 规范)
|
|
111
|
+
await handle.prompt(formatCommandPrompt('review', 'main')) // "/review main"
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Sidecar:`session/open` 结果带 `availableCommands`(若已收到),并新增 `session/commands` 查询方法。
|
|
115
|
+
|
|
102
116
|
## 📦 打包构建
|
|
103
117
|
|
|
104
118
|
在本包根目录一键构建生成 `dist`(ESM / CJS / `.d.ts`):
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type AcpAvailableCommand } from '../session/available-commands';
|
|
1
2
|
import { type InitializeResponse } from '@agentclientprotocol/sdk';
|
|
2
3
|
import type { AcpConnectionStatus, AcpPermissionOutcome, AcpProxySettings, AcpRuntimeInfo, AcpSessionRestoreMethod, AppError, InkdownVirtualResource } from "../contracts";
|
|
3
4
|
import { type Result } from "../contracts";
|
|
@@ -56,6 +57,8 @@ export declare class AcpSessionHandle {
|
|
|
56
57
|
data: string;
|
|
57
58
|
}>;
|
|
58
59
|
}): Promise<Result<unknown, AppError>>;
|
|
60
|
+
/** Agent 通过 available_commands_update 提供的斜杠命令(调用 = prompt 发送 `/name args` 文本) */
|
|
61
|
+
get availableCommands(): AcpAvailableCommand[] | undefined;
|
|
59
62
|
/** 修改会话配置项(如模型 ID、模式等) */
|
|
60
63
|
setConfigOption(configId: string, value: string | boolean): Promise<Result<unknown, AppError>>;
|
|
61
64
|
}
|
|
@@ -68,6 +71,7 @@ export declare class AcpSessionHandle {
|
|
|
68
71
|
*/
|
|
69
72
|
export declare class AcpClient {
|
|
70
73
|
private status;
|
|
74
|
+
private readonly commandsCache;
|
|
71
75
|
private runtimeInfo;
|
|
72
76
|
private adapter;
|
|
73
77
|
private readonly workspaceRoot;
|
|
@@ -99,6 +103,10 @@ export declare class AcpClient {
|
|
|
99
103
|
status: AcpConnectionStatus;
|
|
100
104
|
init: InitializeResponse;
|
|
101
105
|
}, AppError>>;
|
|
106
|
+
/** 最近一次 available_commands_update 的命令列表(未收到过则为 undefined) */
|
|
107
|
+
getAvailableCommands(sessionId: string): AcpAvailableCommand[] | undefined;
|
|
108
|
+
/** 等待某会话的命令列表(已缓存则立即返回;超时返回 undefined) */
|
|
109
|
+
waitForAvailableCommands(sessionId: string, timeoutMs?: number): Promise<AcpAvailableCommand[] | undefined>;
|
|
102
110
|
/** 创建或恢复会话 */
|
|
103
111
|
createSession(options?: {
|
|
104
112
|
resumeSessionId?: string | null;
|
package/dist/index.cjs
CHANGED
|
@@ -39,6 +39,7 @@ var __export = (target, all) => {
|
|
|
39
39
|
// src/index.ts
|
|
40
40
|
var exports_src = {};
|
|
41
41
|
__export(exports_src, {
|
|
42
|
+
ACP_CLIENT_VERSION: () => ACP_CLIENT_VERSION,
|
|
42
43
|
ACP_EARLY_EXIT_STDERR_TAIL_LINES: () => ACP_EARLY_EXIT_STDERR_TAIL_LINES,
|
|
43
44
|
ACP_EARLY_EXIT_WINDOW_MS: () => ACP_EARLY_EXIT_WINDOW_MS,
|
|
44
45
|
ACP_SDK_REQUEST_TIMEOUT_MS: () => ACP_SDK_REQUEST_TIMEOUT_MS,
|
|
@@ -46,6 +47,7 @@ __export(exports_src, {
|
|
|
46
47
|
AcpClient: () => AcpClient,
|
|
47
48
|
AcpSessionHandle: () => AcpSessionHandle,
|
|
48
49
|
AcpTerminalManager: () => AcpTerminalManager,
|
|
50
|
+
AvailableCommandsCache: () => AvailableCommandsCache,
|
|
49
51
|
BUILTIN_ACP_RUNTIMES: () => BUILTIN_ACP_RUNTIMES,
|
|
50
52
|
CLAUDE_ACP_NPM_PACKAGE: () => CLAUDE_ACP_NPM_PACKAGE,
|
|
51
53
|
CODEX_ACP_NPM_PACKAGE: () => CODEX_ACP_NPM_PACKAGE,
|
|
@@ -85,8 +87,10 @@ __export(exports_src, {
|
|
|
85
87
|
disposeAllAcpProcesses: () => disposeAllAcpProcesses,
|
|
86
88
|
emptyAcpAuthPreflight: () => emptyAcpAuthPreflight,
|
|
87
89
|
err: () => err,
|
|
90
|
+
extractAvailableCommands: () => extractAvailableCommands,
|
|
88
91
|
findBuiltinAcpRuntime: () => findBuiltinAcpRuntime,
|
|
89
92
|
findModelConfigOption: () => findModelConfigOption,
|
|
93
|
+
formatCommandPrompt: () => formatCommandPrompt,
|
|
90
94
|
geminiAdapter: () => geminiAdapter,
|
|
91
95
|
getAcpEarlyExitStderrDetail: () => getAcpEarlyExitStderrDetail,
|
|
92
96
|
getAcpProviderCodexHome: () => getAcpProviderCodexHome,
|
|
@@ -313,6 +317,96 @@ var BUILTIN_ACP_RUNTIMES = [
|
|
|
313
317
|
function findBuiltinAcpRuntime(id) {
|
|
314
318
|
return BUILTIN_ACP_RUNTIMES.find((item) => item.id === id);
|
|
315
319
|
}
|
|
320
|
+
// src/version.ts
|
|
321
|
+
var ACP_CLIENT_VERSION = "0.1.4";
|
|
322
|
+
// src/session/available-commands.ts
|
|
323
|
+
function normalizeCommand(raw) {
|
|
324
|
+
if (!raw || typeof raw !== "object")
|
|
325
|
+
return null;
|
|
326
|
+
const r = raw;
|
|
327
|
+
if (typeof r.name !== "string" || !r.name.trim())
|
|
328
|
+
return null;
|
|
329
|
+
const out = {
|
|
330
|
+
name: r.name.trim(),
|
|
331
|
+
description: typeof r.description === "string" ? r.description : ""
|
|
332
|
+
};
|
|
333
|
+
const input = r.input;
|
|
334
|
+
if (input && typeof input === "object" && typeof input.hint === "string") {
|
|
335
|
+
out.input = { hint: input.hint };
|
|
336
|
+
}
|
|
337
|
+
if (r._meta && typeof r._meta === "object")
|
|
338
|
+
out._meta = r._meta;
|
|
339
|
+
return out;
|
|
340
|
+
}
|
|
341
|
+
function extractAvailableCommands(params) {
|
|
342
|
+
if (!params || typeof params !== "object")
|
|
343
|
+
return null;
|
|
344
|
+
const p = params;
|
|
345
|
+
if (typeof p.sessionId !== "string" || !p.update)
|
|
346
|
+
return null;
|
|
347
|
+
if (p.update.sessionUpdate !== "available_commands_update")
|
|
348
|
+
return null;
|
|
349
|
+
const list = Array.isArray(p.update.availableCommands) ? p.update.availableCommands : [];
|
|
350
|
+
const commands = list.map(normalizeCommand).filter((c) => c !== null);
|
|
351
|
+
return { sessionId: p.sessionId, commands };
|
|
352
|
+
}
|
|
353
|
+
function formatCommandPrompt(name, args) {
|
|
354
|
+
const n = name.replace(/^\//, "");
|
|
355
|
+
const a = (args ?? "").trim();
|
|
356
|
+
return a ? `/${n} ${a}` : `/${n}`;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
class AvailableCommandsCache {
|
|
360
|
+
bySession = new Map;
|
|
361
|
+
waiters = new Map;
|
|
362
|
+
observe(params) {
|
|
363
|
+
const hit = extractAvailableCommands(params);
|
|
364
|
+
if (!hit)
|
|
365
|
+
return false;
|
|
366
|
+
this.bySession.set(hit.sessionId, hit.commands);
|
|
367
|
+
const ws = this.waiters.get(hit.sessionId);
|
|
368
|
+
if (ws) {
|
|
369
|
+
this.waiters.delete(hit.sessionId);
|
|
370
|
+
for (const w of ws)
|
|
371
|
+
w(hit.commands);
|
|
372
|
+
}
|
|
373
|
+
return true;
|
|
374
|
+
}
|
|
375
|
+
get(sessionId) {
|
|
376
|
+
return this.bySession.get(sessionId);
|
|
377
|
+
}
|
|
378
|
+
wait(sessionId, timeoutMs = 150) {
|
|
379
|
+
const cur = this.bySession.get(sessionId);
|
|
380
|
+
if (cur || timeoutMs <= 0)
|
|
381
|
+
return Promise.resolve(cur);
|
|
382
|
+
return new Promise((resolve) => {
|
|
383
|
+
const done = (c) => {
|
|
384
|
+
clearTimeout(timer);
|
|
385
|
+
const list = this.waiters.get(sessionId);
|
|
386
|
+
if (list) {
|
|
387
|
+
const i = list.indexOf(fn);
|
|
388
|
+
if (i >= 0)
|
|
389
|
+
list.splice(i, 1);
|
|
390
|
+
if (list.length === 0)
|
|
391
|
+
this.waiters.delete(sessionId);
|
|
392
|
+
}
|
|
393
|
+
resolve(c);
|
|
394
|
+
};
|
|
395
|
+
const fn = (c) => done(c);
|
|
396
|
+
const timer = setTimeout(() => done(undefined), timeoutMs);
|
|
397
|
+
const list = this.waiters.get(sessionId) ?? [];
|
|
398
|
+
list.push(fn);
|
|
399
|
+
this.waiters.set(sessionId, list);
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
delete(sessionId) {
|
|
403
|
+
this.bySession.delete(sessionId);
|
|
404
|
+
}
|
|
405
|
+
clear() {
|
|
406
|
+
this.bySession.clear();
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
316
410
|
// src/client/acp-client.ts
|
|
317
411
|
var import_sdk4 = require("@agentclientprotocol/sdk");
|
|
318
412
|
|
|
@@ -2144,6 +2238,9 @@ class AcpSessionHandle {
|
|
|
2144
2238
|
async prompt(promptText, options) {
|
|
2145
2239
|
return this.client.promptSession(this.sessionId, promptText, options);
|
|
2146
2240
|
}
|
|
2241
|
+
get availableCommands() {
|
|
2242
|
+
return this.client.getAvailableCommands(this.sessionId);
|
|
2243
|
+
}
|
|
2147
2244
|
async setConfigOption(configId, value) {
|
|
2148
2245
|
return this.client.setSessionConfigOption(this.sessionId, configId, value);
|
|
2149
2246
|
}
|
|
@@ -2151,6 +2248,7 @@ class AcpSessionHandle {
|
|
|
2151
2248
|
|
|
2152
2249
|
class AcpClient {
|
|
2153
2250
|
status = "disconnected";
|
|
2251
|
+
commandsCache = new AvailableCommandsCache;
|
|
2154
2252
|
runtimeInfo;
|
|
2155
2253
|
adapter;
|
|
2156
2254
|
workspaceRoot;
|
|
@@ -2260,6 +2358,7 @@ class AcpClient {
|
|
|
2260
2358
|
return { outcome: "cancelled" };
|
|
2261
2359
|
},
|
|
2262
2360
|
onSessionUpdate: (params) => {
|
|
2361
|
+
this.commandsCache.observe(params);
|
|
2263
2362
|
this.callbacks.onSessionUpdate?.(params);
|
|
2264
2363
|
}
|
|
2265
2364
|
};
|
|
@@ -2290,6 +2389,12 @@ class AcpClient {
|
|
|
2290
2389
|
return err({ code: "ACP_PROTOCOL_ERROR", message });
|
|
2291
2390
|
}
|
|
2292
2391
|
}
|
|
2392
|
+
getAvailableCommands(sessionId) {
|
|
2393
|
+
return this.commandsCache.get(sessionId);
|
|
2394
|
+
}
|
|
2395
|
+
waitForAvailableCommands(sessionId, timeoutMs = 150) {
|
|
2396
|
+
return this.commandsCache.wait(sessionId, timeoutMs);
|
|
2397
|
+
}
|
|
2293
2398
|
async createSession(options) {
|
|
2294
2399
|
if (!this.sdkConnection?.agent) {
|
|
2295
2400
|
return err({ code: "ACP_NOT_CONNECTED", message: "尚未连接到 Agent 服务端" });
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
* - transport: stdio 缓冲与终端输出处理
|
|
12
12
|
*/
|
|
13
13
|
export * from "./contracts";
|
|
14
|
+
export { ACP_CLIENT_VERSION } from "./version";
|
|
14
15
|
export * from "./client/acp-client";
|
|
15
16
|
export * from "./client/sdk-client";
|
|
16
17
|
export * from "./client/session-open";
|
|
@@ -28,6 +29,7 @@ export * from "./auth/connect-auth-gate";
|
|
|
28
29
|
export * from "./session/agent-registry";
|
|
29
30
|
export * from "./session/config-options";
|
|
30
31
|
export * from "./session/session-capabilities";
|
|
32
|
+
export * from "./session/available-commands";
|
|
31
33
|
export * from "./mcp/inkdown-mcp-tools";
|
|
32
34
|
export * from "./mcp/inkdown-mcp-toc-tools";
|
|
33
35
|
export * from "./mcp/inkdown-mcp-rpc";
|
package/dist/index.js
CHANGED
|
@@ -148,6 +148,96 @@ var BUILTIN_ACP_RUNTIMES = [
|
|
|
148
148
|
function findBuiltinAcpRuntime(id) {
|
|
149
149
|
return BUILTIN_ACP_RUNTIMES.find((item) => item.id === id);
|
|
150
150
|
}
|
|
151
|
+
// src/version.ts
|
|
152
|
+
var ACP_CLIENT_VERSION = "0.1.4";
|
|
153
|
+
// src/session/available-commands.ts
|
|
154
|
+
function normalizeCommand(raw) {
|
|
155
|
+
if (!raw || typeof raw !== "object")
|
|
156
|
+
return null;
|
|
157
|
+
const r = raw;
|
|
158
|
+
if (typeof r.name !== "string" || !r.name.trim())
|
|
159
|
+
return null;
|
|
160
|
+
const out = {
|
|
161
|
+
name: r.name.trim(),
|
|
162
|
+
description: typeof r.description === "string" ? r.description : ""
|
|
163
|
+
};
|
|
164
|
+
const input = r.input;
|
|
165
|
+
if (input && typeof input === "object" && typeof input.hint === "string") {
|
|
166
|
+
out.input = { hint: input.hint };
|
|
167
|
+
}
|
|
168
|
+
if (r._meta && typeof r._meta === "object")
|
|
169
|
+
out._meta = r._meta;
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
172
|
+
function extractAvailableCommands(params) {
|
|
173
|
+
if (!params || typeof params !== "object")
|
|
174
|
+
return null;
|
|
175
|
+
const p = params;
|
|
176
|
+
if (typeof p.sessionId !== "string" || !p.update)
|
|
177
|
+
return null;
|
|
178
|
+
if (p.update.sessionUpdate !== "available_commands_update")
|
|
179
|
+
return null;
|
|
180
|
+
const list = Array.isArray(p.update.availableCommands) ? p.update.availableCommands : [];
|
|
181
|
+
const commands = list.map(normalizeCommand).filter((c) => c !== null);
|
|
182
|
+
return { sessionId: p.sessionId, commands };
|
|
183
|
+
}
|
|
184
|
+
function formatCommandPrompt(name, args) {
|
|
185
|
+
const n = name.replace(/^\//, "");
|
|
186
|
+
const a = (args ?? "").trim();
|
|
187
|
+
return a ? `/${n} ${a}` : `/${n}`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
class AvailableCommandsCache {
|
|
191
|
+
bySession = new Map;
|
|
192
|
+
waiters = new Map;
|
|
193
|
+
observe(params) {
|
|
194
|
+
const hit = extractAvailableCommands(params);
|
|
195
|
+
if (!hit)
|
|
196
|
+
return false;
|
|
197
|
+
this.bySession.set(hit.sessionId, hit.commands);
|
|
198
|
+
const ws = this.waiters.get(hit.sessionId);
|
|
199
|
+
if (ws) {
|
|
200
|
+
this.waiters.delete(hit.sessionId);
|
|
201
|
+
for (const w of ws)
|
|
202
|
+
w(hit.commands);
|
|
203
|
+
}
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
get(sessionId) {
|
|
207
|
+
return this.bySession.get(sessionId);
|
|
208
|
+
}
|
|
209
|
+
wait(sessionId, timeoutMs = 150) {
|
|
210
|
+
const cur = this.bySession.get(sessionId);
|
|
211
|
+
if (cur || timeoutMs <= 0)
|
|
212
|
+
return Promise.resolve(cur);
|
|
213
|
+
return new Promise((resolve) => {
|
|
214
|
+
const done = (c) => {
|
|
215
|
+
clearTimeout(timer);
|
|
216
|
+
const list = this.waiters.get(sessionId);
|
|
217
|
+
if (list) {
|
|
218
|
+
const i = list.indexOf(fn);
|
|
219
|
+
if (i >= 0)
|
|
220
|
+
list.splice(i, 1);
|
|
221
|
+
if (list.length === 0)
|
|
222
|
+
this.waiters.delete(sessionId);
|
|
223
|
+
}
|
|
224
|
+
resolve(c);
|
|
225
|
+
};
|
|
226
|
+
const fn = (c) => done(c);
|
|
227
|
+
const timer = setTimeout(() => done(undefined), timeoutMs);
|
|
228
|
+
const list = this.waiters.get(sessionId) ?? [];
|
|
229
|
+
list.push(fn);
|
|
230
|
+
this.waiters.set(sessionId, list);
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
delete(sessionId) {
|
|
234
|
+
this.bySession.delete(sessionId);
|
|
235
|
+
}
|
|
236
|
+
clear() {
|
|
237
|
+
this.bySession.clear();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
151
241
|
// src/client/acp-client.ts
|
|
152
242
|
import {
|
|
153
243
|
methods as methods2
|
|
@@ -1984,6 +2074,9 @@ class AcpSessionHandle {
|
|
|
1984
2074
|
async prompt(promptText, options) {
|
|
1985
2075
|
return this.client.promptSession(this.sessionId, promptText, options);
|
|
1986
2076
|
}
|
|
2077
|
+
get availableCommands() {
|
|
2078
|
+
return this.client.getAvailableCommands(this.sessionId);
|
|
2079
|
+
}
|
|
1987
2080
|
async setConfigOption(configId, value) {
|
|
1988
2081
|
return this.client.setSessionConfigOption(this.sessionId, configId, value);
|
|
1989
2082
|
}
|
|
@@ -1991,6 +2084,7 @@ class AcpSessionHandle {
|
|
|
1991
2084
|
|
|
1992
2085
|
class AcpClient {
|
|
1993
2086
|
status = "disconnected";
|
|
2087
|
+
commandsCache = new AvailableCommandsCache;
|
|
1994
2088
|
runtimeInfo;
|
|
1995
2089
|
adapter;
|
|
1996
2090
|
workspaceRoot;
|
|
@@ -2100,6 +2194,7 @@ class AcpClient {
|
|
|
2100
2194
|
return { outcome: "cancelled" };
|
|
2101
2195
|
},
|
|
2102
2196
|
onSessionUpdate: (params) => {
|
|
2197
|
+
this.commandsCache.observe(params);
|
|
2103
2198
|
this.callbacks.onSessionUpdate?.(params);
|
|
2104
2199
|
}
|
|
2105
2200
|
};
|
|
@@ -2130,6 +2225,12 @@ class AcpClient {
|
|
|
2130
2225
|
return err({ code: "ACP_PROTOCOL_ERROR", message });
|
|
2131
2226
|
}
|
|
2132
2227
|
}
|
|
2228
|
+
getAvailableCommands(sessionId) {
|
|
2229
|
+
return this.commandsCache.get(sessionId);
|
|
2230
|
+
}
|
|
2231
|
+
waitForAvailableCommands(sessionId, timeoutMs = 150) {
|
|
2232
|
+
return this.commandsCache.wait(sessionId, timeoutMs);
|
|
2233
|
+
}
|
|
2133
2234
|
async createSession(options) {
|
|
2134
2235
|
if (!this.sdkConnection?.agent) {
|
|
2135
2236
|
return err({ code: "ACP_NOT_CONNECTED", message: "尚未连接到 Agent 服务端" });
|
|
@@ -3133,6 +3234,7 @@ async function handleInkdownMcpRpc(message, context, tools = INKDOWN_MCP_TOOLS,
|
|
|
3133
3234
|
}
|
|
3134
3235
|
}
|
|
3135
3236
|
export {
|
|
3237
|
+
ACP_CLIENT_VERSION,
|
|
3136
3238
|
ACP_EARLY_EXIT_STDERR_TAIL_LINES,
|
|
3137
3239
|
ACP_EARLY_EXIT_WINDOW_MS,
|
|
3138
3240
|
ACP_SDK_REQUEST_TIMEOUT_MS,
|
|
@@ -3140,6 +3242,7 @@ export {
|
|
|
3140
3242
|
AcpClient,
|
|
3141
3243
|
AcpSessionHandle,
|
|
3142
3244
|
AcpTerminalManager,
|
|
3245
|
+
AvailableCommandsCache,
|
|
3143
3246
|
BUILTIN_ACP_RUNTIMES,
|
|
3144
3247
|
CLAUDE_ACP_NPM_PACKAGE,
|
|
3145
3248
|
CODEX_ACP_NPM_PACKAGE,
|
|
@@ -3179,8 +3282,10 @@ export {
|
|
|
3179
3282
|
disposeAllAcpProcesses,
|
|
3180
3283
|
emptyAcpAuthPreflight,
|
|
3181
3284
|
err,
|
|
3285
|
+
extractAvailableCommands,
|
|
3182
3286
|
findBuiltinAcpRuntime,
|
|
3183
3287
|
findModelConfigOption,
|
|
3288
|
+
formatCommandPrompt,
|
|
3184
3289
|
geminiAdapter,
|
|
3185
3290
|
getAcpEarlyExitStderrDetail,
|
|
3186
3291
|
getAcpProviderCodexHome,
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ACP `available_commands_update` handling.
|
|
3
|
+
*
|
|
4
|
+
* Agents typically push their slash commands right after session/new|load|resume
|
|
5
|
+
* returns — i.e. before a host has learned the sessionId or wired a listener.
|
|
6
|
+
* Raw notifications are still forwarded untouched via `onSessionUpdate`; this
|
|
7
|
+
* cache additionally keeps the latest list per session so hosts can read it
|
|
8
|
+
* at any time (`AcpClient.getAvailableCommands`, `AcpSessionHandle.availableCommands`,
|
|
9
|
+
* sidecar `session/open` result + `session/commands`).
|
|
10
|
+
*
|
|
11
|
+
* Invoking a command is plain prompt text (`/name args`) per the ACP spec.
|
|
12
|
+
*/
|
|
13
|
+
export interface AcpAvailableCommandInput {
|
|
14
|
+
/** Placeholder shown while the user types the argument. */
|
|
15
|
+
hint: string;
|
|
16
|
+
}
|
|
17
|
+
export interface AcpAvailableCommand {
|
|
18
|
+
name: string;
|
|
19
|
+
description: string;
|
|
20
|
+
input?: AcpAvailableCommandInput | null;
|
|
21
|
+
_meta?: Record<string, unknown>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Extract `{ sessionId, commands }` from a session/update params object
|
|
25
|
+
* (`{ sessionId, update: { sessionUpdate: 'available_commands_update', availableCommands } }`).
|
|
26
|
+
*/
|
|
27
|
+
export declare function extractAvailableCommands(params: unknown): {
|
|
28
|
+
sessionId: string;
|
|
29
|
+
commands: AcpAvailableCommand[];
|
|
30
|
+
} | null;
|
|
31
|
+
/** Build the prompt text for a command invocation (ACP: plain text `/name args`). */
|
|
32
|
+
export declare function formatCommandPrompt(name: string, args?: string): string;
|
|
33
|
+
export declare class AvailableCommandsCache {
|
|
34
|
+
private bySession;
|
|
35
|
+
private waiters;
|
|
36
|
+
/** Feed every session/update params object; returns true if it was a commands update. */
|
|
37
|
+
observe(params: unknown): boolean;
|
|
38
|
+
get(sessionId: string): AcpAvailableCommand[] | undefined;
|
|
39
|
+
/** Resolve with the cached list, or wait up to `timeoutMs` for one (undefined on timeout). */
|
|
40
|
+
wait(sessionId: string, timeoutMs?: number): Promise<AcpAvailableCommand[] | undefined>;
|
|
41
|
+
delete(sessionId: string): void;
|
|
42
|
+
clear(): void;
|
|
43
|
+
}
|
package/dist/sidecar.cjs
CHANGED
|
@@ -35,6 +35,9 @@ var __toESM = (mod, isNodeMode, target) => {
|
|
|
35
35
|
// src/sidecar/main.ts
|
|
36
36
|
var readline = __toESM(require("node:readline"), 1);
|
|
37
37
|
|
|
38
|
+
// src/version.ts
|
|
39
|
+
var ACP_CLIENT_VERSION = "0.1.4";
|
|
40
|
+
|
|
38
41
|
// src/contracts/index.ts
|
|
39
42
|
function ok(value) {
|
|
40
43
|
return { ok: true, value };
|
|
@@ -163,6 +166,88 @@ var BUILTIN_ACP_RUNTIMES = [
|
|
|
163
166
|
function findBuiltinAcpRuntime(id) {
|
|
164
167
|
return BUILTIN_ACP_RUNTIMES.find((item) => item.id === id);
|
|
165
168
|
}
|
|
169
|
+
// src/session/available-commands.ts
|
|
170
|
+
function normalizeCommand(raw) {
|
|
171
|
+
if (!raw || typeof raw !== "object")
|
|
172
|
+
return null;
|
|
173
|
+
const r = raw;
|
|
174
|
+
if (typeof r.name !== "string" || !r.name.trim())
|
|
175
|
+
return null;
|
|
176
|
+
const out = {
|
|
177
|
+
name: r.name.trim(),
|
|
178
|
+
description: typeof r.description === "string" ? r.description : ""
|
|
179
|
+
};
|
|
180
|
+
const input = r.input;
|
|
181
|
+
if (input && typeof input === "object" && typeof input.hint === "string") {
|
|
182
|
+
out.input = { hint: input.hint };
|
|
183
|
+
}
|
|
184
|
+
if (r._meta && typeof r._meta === "object")
|
|
185
|
+
out._meta = r._meta;
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
188
|
+
function extractAvailableCommands(params) {
|
|
189
|
+
if (!params || typeof params !== "object")
|
|
190
|
+
return null;
|
|
191
|
+
const p = params;
|
|
192
|
+
if (typeof p.sessionId !== "string" || !p.update)
|
|
193
|
+
return null;
|
|
194
|
+
if (p.update.sessionUpdate !== "available_commands_update")
|
|
195
|
+
return null;
|
|
196
|
+
const list = Array.isArray(p.update.availableCommands) ? p.update.availableCommands : [];
|
|
197
|
+
const commands = list.map(normalizeCommand).filter((c) => c !== null);
|
|
198
|
+
return { sessionId: p.sessionId, commands };
|
|
199
|
+
}
|
|
200
|
+
class AvailableCommandsCache {
|
|
201
|
+
bySession = new Map;
|
|
202
|
+
waiters = new Map;
|
|
203
|
+
observe(params) {
|
|
204
|
+
const hit = extractAvailableCommands(params);
|
|
205
|
+
if (!hit)
|
|
206
|
+
return false;
|
|
207
|
+
this.bySession.set(hit.sessionId, hit.commands);
|
|
208
|
+
const ws = this.waiters.get(hit.sessionId);
|
|
209
|
+
if (ws) {
|
|
210
|
+
this.waiters.delete(hit.sessionId);
|
|
211
|
+
for (const w of ws)
|
|
212
|
+
w(hit.commands);
|
|
213
|
+
}
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
get(sessionId) {
|
|
217
|
+
return this.bySession.get(sessionId);
|
|
218
|
+
}
|
|
219
|
+
wait(sessionId, timeoutMs = 150) {
|
|
220
|
+
const cur = this.bySession.get(sessionId);
|
|
221
|
+
if (cur || timeoutMs <= 0)
|
|
222
|
+
return Promise.resolve(cur);
|
|
223
|
+
return new Promise((resolve) => {
|
|
224
|
+
const done = (c) => {
|
|
225
|
+
clearTimeout(timer);
|
|
226
|
+
const list = this.waiters.get(sessionId);
|
|
227
|
+
if (list) {
|
|
228
|
+
const i = list.indexOf(fn);
|
|
229
|
+
if (i >= 0)
|
|
230
|
+
list.splice(i, 1);
|
|
231
|
+
if (list.length === 0)
|
|
232
|
+
this.waiters.delete(sessionId);
|
|
233
|
+
}
|
|
234
|
+
resolve(c);
|
|
235
|
+
};
|
|
236
|
+
const fn = (c) => done(c);
|
|
237
|
+
const timer = setTimeout(() => done(undefined), timeoutMs);
|
|
238
|
+
const list = this.waiters.get(sessionId) ?? [];
|
|
239
|
+
list.push(fn);
|
|
240
|
+
this.waiters.set(sessionId, list);
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
delete(sessionId) {
|
|
244
|
+
this.bySession.delete(sessionId);
|
|
245
|
+
}
|
|
246
|
+
clear() {
|
|
247
|
+
this.bySession.clear();
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
166
251
|
// node_modules/@agentclientprotocol/sdk/dist/schema/index.js
|
|
167
252
|
var AGENT_METHODS = {
|
|
168
253
|
initialize: "initialize",
|
|
@@ -11615,6 +11700,9 @@ class AcpSessionHandle {
|
|
|
11615
11700
|
async prompt(promptText, options) {
|
|
11616
11701
|
return this.client.promptSession(this.sessionId, promptText, options);
|
|
11617
11702
|
}
|
|
11703
|
+
get availableCommands() {
|
|
11704
|
+
return this.client.getAvailableCommands(this.sessionId);
|
|
11705
|
+
}
|
|
11618
11706
|
async setConfigOption(configId, value) {
|
|
11619
11707
|
return this.client.setSessionConfigOption(this.sessionId, configId, value);
|
|
11620
11708
|
}
|
|
@@ -11622,6 +11710,7 @@ class AcpSessionHandle {
|
|
|
11622
11710
|
|
|
11623
11711
|
class AcpClient {
|
|
11624
11712
|
status = "disconnected";
|
|
11713
|
+
commandsCache = new AvailableCommandsCache;
|
|
11625
11714
|
runtimeInfo;
|
|
11626
11715
|
adapter;
|
|
11627
11716
|
workspaceRoot;
|
|
@@ -11731,6 +11820,7 @@ class AcpClient {
|
|
|
11731
11820
|
return { outcome: "cancelled" };
|
|
11732
11821
|
},
|
|
11733
11822
|
onSessionUpdate: (params) => {
|
|
11823
|
+
this.commandsCache.observe(params);
|
|
11734
11824
|
this.callbacks.onSessionUpdate?.(params);
|
|
11735
11825
|
}
|
|
11736
11826
|
};
|
|
@@ -11761,6 +11851,12 @@ class AcpClient {
|
|
|
11761
11851
|
return err({ code: "ACP_PROTOCOL_ERROR", message });
|
|
11762
11852
|
}
|
|
11763
11853
|
}
|
|
11854
|
+
getAvailableCommands(sessionId) {
|
|
11855
|
+
return this.commandsCache.get(sessionId);
|
|
11856
|
+
}
|
|
11857
|
+
waitForAvailableCommands(sessionId, timeoutMs = 150) {
|
|
11858
|
+
return this.commandsCache.wait(sessionId, timeoutMs);
|
|
11859
|
+
}
|
|
11764
11860
|
async createSession(options) {
|
|
11765
11861
|
if (!this.sdkConnection?.agent) {
|
|
11766
11862
|
return err({ code: "ACP_NOT_CONNECTED", message: "尚未连接到 Agent 服务端" });
|
|
@@ -12462,9 +12558,11 @@ async function handleRpcRequest(id, method, params) {
|
|
|
12462
12558
|
}
|
|
12463
12559
|
const handle = sessionResult.value;
|
|
12464
12560
|
activeSessions.set(handle.sessionId, handle);
|
|
12561
|
+
const availableCommands = await client2.waitForAvailableCommands(handle.sessionId, 150);
|
|
12465
12562
|
sendResponse(id, {
|
|
12466
12563
|
sessionId: handle.sessionId,
|
|
12467
12564
|
restoreMethod: handle.restoreMethod,
|
|
12565
|
+
...availableCommands ? { availableCommands } : {},
|
|
12468
12566
|
configOptions: handle.configOptions,
|
|
12469
12567
|
modelOptions: extractModelOptions(handle.configOptions)
|
|
12470
12568
|
});
|
|
@@ -12580,6 +12678,15 @@ async function handleRpcRequest(id, method, params) {
|
|
|
12580
12678
|
}
|
|
12581
12679
|
break;
|
|
12582
12680
|
}
|
|
12681
|
+
case "session/commands": {
|
|
12682
|
+
const { sessionId } = params ?? {};
|
|
12683
|
+
if (!client2) {
|
|
12684
|
+
sendError(id, -32002, "Client not connected");
|
|
12685
|
+
return;
|
|
12686
|
+
}
|
|
12687
|
+
sendResponse(id, { sessionId, availableCommands: client2.getAvailableCommands(String(sessionId ?? "")) ?? null });
|
|
12688
|
+
break;
|
|
12689
|
+
}
|
|
12583
12690
|
case "session/cancel": {
|
|
12584
12691
|
try {
|
|
12585
12692
|
const { sessionId } = params ?? {};
|
|
@@ -12656,7 +12763,7 @@ function startSidecar() {
|
|
|
12656
12763
|
process.exit(0);
|
|
12657
12764
|
});
|
|
12658
12765
|
sendNotification("ready", {
|
|
12659
|
-
version:
|
|
12766
|
+
version: ACP_CLIENT_VERSION,
|
|
12660
12767
|
platform: process.platform,
|
|
12661
12768
|
pid: process.pid
|
|
12662
12769
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const ACP_CLIENT_VERSION = "0.1.4";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yitom/acp-client",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Headless ACP (Agent Client Protocol) SDK for connecting to Codex, Claude, Cursor, DeepSeek and more",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -31,9 +31,10 @@
|
|
|
31
31
|
},
|
|
32
32
|
"homepage": "https://github.com/yitom486/acp-client#readme",
|
|
33
33
|
"scripts": {
|
|
34
|
-
"build": "bun build ./src/index.ts --outdir ./dist --format esm --target node --external @agentclientprotocol/sdk && bun build ./src/index.ts --outfile ./dist/index.cjs --format cjs --target node --external @agentclientprotocol/sdk && bun build ./src/sidecar/main.ts --outfile ./dist/sidecar.cjs --format cjs --target node && tsc -p tsconfig.json",
|
|
34
|
+
"build": "node scripts/sync-version.mjs && bun build ./src/index.ts --outdir ./dist --format esm --target node --external @agentclientprotocol/sdk && bun build ./src/index.ts --outfile ./dist/index.cjs --format cjs --target node --external @agentclientprotocol/sdk && bun build ./src/sidecar/main.ts --outfile ./dist/sidecar.cjs --format cjs --target node && tsc -p tsconfig.json",
|
|
35
35
|
"test": "bun test",
|
|
36
|
-
"prepublishOnly": "bun run build"
|
|
36
|
+
"prepublishOnly": "bun run build",
|
|
37
|
+
"version": "node scripts/sync-version.mjs && git add src/version.ts"
|
|
37
38
|
},
|
|
38
39
|
"dependencies": {
|
|
39
40
|
"@agentclientprotocol/sdk": "^1.0.0"
|