@qlairoslabs/acp-client 0.1.0
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/LICENSE +21 -0
- package/README.md +222 -0
- package/package.json +62 -0
- package/src/adapters.ts +52 -0
- package/src/agent-client.ts +50 -0
- package/src/agent-session.ts +212 -0
- package/src/capabilities.ts +104 -0
- package/src/cli/index.ts +112 -0
- package/src/client.ts +613 -0
- package/src/config-options.ts +65 -0
- package/src/controller.ts +446 -0
- package/src/degraded/emulator.ts +77 -0
- package/src/degraded/extract.ts +87 -0
- package/src/degraded/index.ts +18 -0
- package/src/degraded/pty-client.ts +304 -0
- package/src/degraded/pty-transport.ts +130 -0
- package/src/errors.ts +28 -0
- package/src/index.ts +81 -0
- package/src/logger.ts +65 -0
- package/src/parser/index.ts +4 -0
- package/src/parser/ollama.ts +26 -0
- package/src/parser/types.ts +20 -0
- package/src/repl/colors.ts +15 -0
- package/src/repl/commands.ts +435 -0
- package/src/repl/index.ts +9 -0
- package/src/repl/repl.ts +312 -0
- package/src/session-manager.ts +125 -0
- package/src/session-store.ts +96 -0
- package/src/transport/acp-transport.ts +193 -0
- package/src/types.ts +112 -0
- package/src/updates.ts +45 -0
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
// Reusable slash-command handlers for interactive sessions.
|
|
2
|
+
// This extracts command handlers from the CLI so they can be shared across any
|
|
3
|
+
// interactive consumer (CLI, hub terminal mode, etc).
|
|
4
|
+
//
|
|
5
|
+
// Usage:
|
|
6
|
+
// const { onSlashCommand } = createAgentCommands({ client, note, color });
|
|
7
|
+
// await runRepl(client, { onSlashCommand });
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
AgentClient,
|
|
11
|
+
AgentMode,
|
|
12
|
+
AuthMethod,
|
|
13
|
+
Bridgeable,
|
|
14
|
+
Capabilities,
|
|
15
|
+
ConfigOption,
|
|
16
|
+
ConfigOptions,
|
|
17
|
+
ConfigValueParseResult,
|
|
18
|
+
ConnectResult,
|
|
19
|
+
ControllerCommand,
|
|
20
|
+
SetConfigFromStringResult,
|
|
21
|
+
SwitchSessionOptions,
|
|
22
|
+
} from "../index.ts";
|
|
23
|
+
import type {
|
|
24
|
+
AgentCommand,
|
|
25
|
+
SessionListEntry,
|
|
26
|
+
SessionListPage,
|
|
27
|
+
} from "../types.ts";
|
|
28
|
+
import type { SessionManager, MergedSession } from "../session-manager.ts";
|
|
29
|
+
|
|
30
|
+
/** The control-plane surface required by the reusable slash commands. */
|
|
31
|
+
export interface InteractiveAgentClient extends AgentClient, Bridgeable {
|
|
32
|
+
/** Optional durable catalog identity, when the client is session-backed. */
|
|
33
|
+
readonly id?: string;
|
|
34
|
+
readonly currentAdapterId: string;
|
|
35
|
+
readonly currentMode: AgentMode;
|
|
36
|
+
readonly currentSessionId: string | null;
|
|
37
|
+
readonly currentCwd?: string;
|
|
38
|
+
readonly isDegraded: boolean;
|
|
39
|
+
readonly capabilities: Capabilities;
|
|
40
|
+
readonly authMethods: AuthMethod[];
|
|
41
|
+
readonly agentCommands: AgentCommand[];
|
|
42
|
+
readonly configOptions: ConfigOptions;
|
|
43
|
+
|
|
44
|
+
getCommands(): ControllerCommand[];
|
|
45
|
+
authenticate(
|
|
46
|
+
methodId: string,
|
|
47
|
+
handlers?: { onOutput?: (line: string) => void },
|
|
48
|
+
): Promise<unknown>;
|
|
49
|
+
newSession(): Promise<ConnectResult>;
|
|
50
|
+
listSessions(): Promise<SessionListPage>;
|
|
51
|
+
loadSession(
|
|
52
|
+
sessionId: string,
|
|
53
|
+
options?: SwitchSessionOptions,
|
|
54
|
+
): Promise<ConnectResult>;
|
|
55
|
+
resumeSession(
|
|
56
|
+
sessionId: string,
|
|
57
|
+
options?: SwitchSessionOptions,
|
|
58
|
+
): Promise<ConnectResult>;
|
|
59
|
+
forkSession(
|
|
60
|
+
sessionId: string,
|
|
61
|
+
options?: SwitchSessionOptions,
|
|
62
|
+
): Promise<ConnectResult>;
|
|
63
|
+
setMode(mode: AgentMode): Promise<ConnectResult>;
|
|
64
|
+
parseConfigValue(
|
|
65
|
+
configId: string,
|
|
66
|
+
rawValue: string,
|
|
67
|
+
): ConfigValueParseResult;
|
|
68
|
+
setConfigFromString(
|
|
69
|
+
configId: string,
|
|
70
|
+
rawValue: string,
|
|
71
|
+
): Promise<SetConfigFromStringResult>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface CommandHandlerDeps {
|
|
75
|
+
/** Prompt client plus the interactive control-plane operations used by commands. */
|
|
76
|
+
client: InteractiveAgentClient;
|
|
77
|
+
/** Output sink for command responses. */
|
|
78
|
+
note: (msg: string) => void;
|
|
79
|
+
/** Whether to emit color codes. Default false. */
|
|
80
|
+
color?: boolean;
|
|
81
|
+
/** Optional catalog manager for persisting session results. */
|
|
82
|
+
sessionManager?: SessionManager;
|
|
83
|
+
/** Current adapter id for catalog recording. Needed if sessionManager is set. */
|
|
84
|
+
adapterId?: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Return value from createAgentCommands. */
|
|
88
|
+
export interface AgentCommands {
|
|
89
|
+
onSlashCommand: (command: string, args: string[]) => Promise<boolean>;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Create a reusable slash-command handler for interactive REPL sessions.
|
|
94
|
+
* Handles /help, /login, /new, /sessions, /load, etc.
|
|
95
|
+
*/
|
|
96
|
+
export function createAgentCommands(deps: CommandHandlerDeps): AgentCommands {
|
|
97
|
+
const { client, note, color = false, sessionManager, adapterId } = deps;
|
|
98
|
+
|
|
99
|
+
const recordSession = async (agentSessionId: string | null): Promise<void> => {
|
|
100
|
+
if (!sessionManager || !adapterId || !client.id) return;
|
|
101
|
+
try {
|
|
102
|
+
await sessionManager.record({
|
|
103
|
+
id: client.id,
|
|
104
|
+
agentSessionId,
|
|
105
|
+
adapter: adapterId,
|
|
106
|
+
mode: client.currentMode,
|
|
107
|
+
cwd: client.currentCwd,
|
|
108
|
+
});
|
|
109
|
+
} catch {
|
|
110
|
+
// Catalog write failure is not fatal — the session still works.
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const onSlashCommand = async (command: string, commandArgs: string[]): Promise<boolean> => {
|
|
115
|
+
switch (command) {
|
|
116
|
+
case "help": {
|
|
117
|
+
note(
|
|
118
|
+
`commands: /help ${client.getCommands().map((c) => c.usage).join(" ")} ` +
|
|
119
|
+
`/degrade /upgrade /exit`,
|
|
120
|
+
);
|
|
121
|
+
const agentCmds = client.agentCommands;
|
|
122
|
+
if (agentCmds.length > 0) {
|
|
123
|
+
note(`agent commands (send with //name): ${agentCmds.map((c: AgentCommand) => `/${c.name}`).join(" ")}`);
|
|
124
|
+
} else if (!client.isDegraded) {
|
|
125
|
+
note("agent commands: (none advertised) — //name still forwards a /name to the agent");
|
|
126
|
+
} else {
|
|
127
|
+
note("//name forwards a /name slash command to the agent's TUI");
|
|
128
|
+
}
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
case "caps": {
|
|
132
|
+
if (client.isDegraded) {
|
|
133
|
+
note("degraded (pty) mode — no ACP capabilities; /upgrade to reconnect over ACP");
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
const c = client.capabilities.agent;
|
|
137
|
+
note(
|
|
138
|
+
`loadSession=${c.loadSession} image=${c.promptCapabilities.image} ` +
|
|
139
|
+
`logout=${c.auth?.logout ?? false} list=${c.sessionCapabilities.list} ` +
|
|
140
|
+
`config=[${[...client.configOptions.keys()].join(",")}]`,
|
|
141
|
+
);
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
case "session":
|
|
145
|
+
note(
|
|
146
|
+
`${client.currentSessionId ?? "(no session)"} ` +
|
|
147
|
+
`(${client.currentAdapterId}, ${client.currentMode})`,
|
|
148
|
+
);
|
|
149
|
+
return true;
|
|
150
|
+
case "login": {
|
|
151
|
+
if (client.isDegraded) {
|
|
152
|
+
note("ACP login unavailable in degraded mode (terminal bridge not built yet)");
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
const methods = client.authMethods;
|
|
156
|
+
const methodId = commandArgs[0];
|
|
157
|
+
if (!methodId) {
|
|
158
|
+
if (methods.length === 0) {
|
|
159
|
+
note("agent advertises no auth methods (it may log in externally, or already be authenticated)");
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
note("auth methods:");
|
|
163
|
+
for (const m of methods) {
|
|
164
|
+
note(` ${m.id} ${m.name}${m.description ? ` — ${m.description}` : ""}`);
|
|
165
|
+
}
|
|
166
|
+
note("run: /login METHOD");
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
if (methods.length > 0 && !methods.some((m: AuthMethod) => m.id === methodId)) {
|
|
170
|
+
note(`unknown method: ${methodId}`);
|
|
171
|
+
note(`available: ${methods.map((m: AuthMethod) => m.id).join(", ") || "(none)"}`);
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
note(`authenticating with ${methodId}… (follow any URL/code shown below)`);
|
|
175
|
+
try {
|
|
176
|
+
await client.authenticate(methodId, {
|
|
177
|
+
onOutput: (line: string) => {
|
|
178
|
+
if (!isRpcNoise(line)) note(line);
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
note(`authenticated with ${methodId}. If replies were empty, run /new for a fresh session.`);
|
|
182
|
+
} catch (e) {
|
|
183
|
+
note(`login failed: ${String(e)}`);
|
|
184
|
+
}
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
case "new": {
|
|
188
|
+
try {
|
|
189
|
+
const result = await client.newSession();
|
|
190
|
+
note(`new session ${result.sessionId}`);
|
|
191
|
+
await recordSession(result.sessionId);
|
|
192
|
+
} catch (e) {
|
|
193
|
+
note(`could not start a new session: ${String(e)}`);
|
|
194
|
+
}
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
case "bridge": {
|
|
198
|
+
try {
|
|
199
|
+
await client.bridge();
|
|
200
|
+
} catch (e) {
|
|
201
|
+
note(`bridge failed: ${String(e)}`);
|
|
202
|
+
}
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
case "degrade":
|
|
206
|
+
case "upgrade": {
|
|
207
|
+
const mode = command === "degrade" ? "degraded" : "normal";
|
|
208
|
+
if (client.currentMode === mode) {
|
|
209
|
+
note(`already in ${mode} mode`);
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
try {
|
|
213
|
+
const result = await client.setMode(mode);
|
|
214
|
+
note(
|
|
215
|
+
mode === "degraded"
|
|
216
|
+
? `degraded to pty (${client.currentAdapterId})`
|
|
217
|
+
: `upgraded to ACP, new session ${result.sessionId}`,
|
|
218
|
+
);
|
|
219
|
+
} catch (e) {
|
|
220
|
+
note(`could not switch to ${mode}: ${String(e)}`);
|
|
221
|
+
}
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
case "sessions": {
|
|
225
|
+
try {
|
|
226
|
+
let sessions: (SessionListEntry | MergedSession)[] = [];
|
|
227
|
+
|
|
228
|
+
// If we have a sessionManager, show merged view (catalog + agent)
|
|
229
|
+
if (sessionManager && adapterId) {
|
|
230
|
+
let agentEntries: SessionListEntry[] = [];
|
|
231
|
+
if (!client.isDegraded && client.capabilities.agent.sessionCapabilities.list) {
|
|
232
|
+
try {
|
|
233
|
+
agentEntries = (await client.listSessions()).sessions;
|
|
234
|
+
} catch {
|
|
235
|
+
// Agent list broken — fall back to catalog only
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
sessions = await sessionManager.listMerged(agentEntries, adapterId);
|
|
239
|
+
} else if (client.capabilities.agent.sessionCapabilities.list) {
|
|
240
|
+
// No catalog — just show agent sessions
|
|
241
|
+
const page = await client.listSessions();
|
|
242
|
+
sessions = page.sessions;
|
|
243
|
+
} else {
|
|
244
|
+
note("agent does not support session/list");
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (sessions.length === 0) {
|
|
249
|
+
note("(no sessions)");
|
|
250
|
+
} else {
|
|
251
|
+
for (const s of sessions) {
|
|
252
|
+
const isMerged = "source" in s;
|
|
253
|
+
const merged = s as MergedSession;
|
|
254
|
+
const plain = s as SessionListEntry;
|
|
255
|
+
const source = isMerged ? ` [${merged.source}]` : "";
|
|
256
|
+
note(`${isMerged ? merged.id : plain.sessionId} ${s.title ?? ""} ${s.updatedAt ?? ""}${source}`);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
} catch (e) {
|
|
260
|
+
note(`sessions failed: ${String(e)}`);
|
|
261
|
+
}
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
case "load": {
|
|
265
|
+
const target = commandArgs[0];
|
|
266
|
+
if (!target) {
|
|
267
|
+
note("usage: /load SESSION_ID");
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
if (!client.capabilities.agent.loadSession) {
|
|
271
|
+
note("load not supported by this agent");
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
const replay = createSessionReplayRenderer(color);
|
|
275
|
+
clearTerminal();
|
|
276
|
+
const result = await client.loadSession(target, { onUpdate: replay.onUpdate });
|
|
277
|
+
replay.finish();
|
|
278
|
+
note(`loaded session ${result.sessionId}`);
|
|
279
|
+
await recordSession(result.sessionId);
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
case "resume": {
|
|
283
|
+
const target = commandArgs[0];
|
|
284
|
+
if (!target) {
|
|
285
|
+
note("usage: /resume SESSION_ID");
|
|
286
|
+
return true;
|
|
287
|
+
}
|
|
288
|
+
if (!client.capabilities.agent.sessionCapabilities.resume) {
|
|
289
|
+
note("resume not supported by this agent");
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
const result = await client.resumeSession(target);
|
|
293
|
+
note(`resumed session ${result.sessionId}`);
|
|
294
|
+
await recordSession(result.sessionId);
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
case "fork": {
|
|
298
|
+
const target = commandArgs[0];
|
|
299
|
+
if (!target) {
|
|
300
|
+
note("usage: /fork SESSION_ID");
|
|
301
|
+
return true;
|
|
302
|
+
}
|
|
303
|
+
if (!client.capabilities.agent.sessionCapabilities.fork) {
|
|
304
|
+
note("fork not supported by this agent");
|
|
305
|
+
return true;
|
|
306
|
+
}
|
|
307
|
+
const result = await client.forkSession(target);
|
|
308
|
+
note(`forked session ${target} -> ${result.sessionId}`);
|
|
309
|
+
await recordSession(result.sessionId);
|
|
310
|
+
return true;
|
|
311
|
+
}
|
|
312
|
+
case "config": {
|
|
313
|
+
const configId = commandArgs[0];
|
|
314
|
+
if (!configId) {
|
|
315
|
+
note(formatConfigList(client.configOptions));
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const option = client.configOptions.get(configId);
|
|
320
|
+
if (!option) {
|
|
321
|
+
note(`unknown config: ${configId}`);
|
|
322
|
+
note(`available configs: ${[...client.configOptions.keys()].join(", ") || "(none)"}`);
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const rawValue = commandArgs.slice(1).join(" ");
|
|
327
|
+
if (!rawValue) {
|
|
328
|
+
note(formatConfigDetail(option));
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const parsed = client.parseConfigValue(configId, rawValue);
|
|
333
|
+
if (!parsed.ok) {
|
|
334
|
+
note(parsed.error);
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const result = await client.setConfigFromString(configId, rawValue);
|
|
339
|
+
note(`set ${configId}=${formatConfigValue(result.currentValue)}`);
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
default:
|
|
343
|
+
return false; // unknown → treat as a normal prompt
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
return { onSlashCommand };
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function clearTerminal(): void {
|
|
351
|
+
if (process.stderr.isTTY) process.stderr.write("\x1b[2J\x1b[H");
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** True for lines that are part of an agent's raw JSON-RPC error dump (not user-facing). */
|
|
355
|
+
function isRpcNoise(line: string): boolean {
|
|
356
|
+
const t = line.trim();
|
|
357
|
+
if (t === "") return true;
|
|
358
|
+
if (/^Error handling request/i.test(t)) return true;
|
|
359
|
+
if (/^[{}],?$/.test(t)) return true;
|
|
360
|
+
if (/^(jsonrpc|id|method|params|code|message|data)\s*:/i.test(t)) return true;
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function formatConfigList(configOptions: Map<string, ConfigOption>): string {
|
|
365
|
+
if (configOptions.size === 0) return "no config options advertised by this session";
|
|
366
|
+
return [...configOptions.values()]
|
|
367
|
+
.map((option) => {
|
|
368
|
+
const label = option.label ? ` (${option.label})` : "";
|
|
369
|
+
return `${option.configId}${label} ${option.type} current=${formatConfigValue(option.currentValue)}`;
|
|
370
|
+
})
|
|
371
|
+
.join("\n");
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function formatConfigDetail(option: ConfigOption): string {
|
|
375
|
+
const lines = [
|
|
376
|
+
`${option.configId}${option.label ? `: ${option.label}` : ""}`,
|
|
377
|
+
`type: ${option.type}`,
|
|
378
|
+
`current: ${formatConfigValue(option.currentValue)}`,
|
|
379
|
+
];
|
|
380
|
+
if (option.description) lines.push(`description: ${option.description}`);
|
|
381
|
+
if (option.allowedValues?.length) {
|
|
382
|
+
lines.push(`values: ${option.allowedValues.map((choice) => formatConfigValue(choice.value)).join(", ")}`);
|
|
383
|
+
}
|
|
384
|
+
return lines.join("\n");
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function formatConfigValue(v: unknown): string {
|
|
388
|
+
if (typeof v === "boolean") return v ? "true" : "false";
|
|
389
|
+
if (typeof v === "string") return JSON.stringify(v);
|
|
390
|
+
if (v === null) return "null";
|
|
391
|
+
if (v === undefined) return "(unset)";
|
|
392
|
+
return String(v);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function createSessionReplayRenderer(
|
|
396
|
+
color: boolean,
|
|
397
|
+
): {
|
|
398
|
+
onUpdate(notification: unknown): void;
|
|
399
|
+
finish(): void;
|
|
400
|
+
} {
|
|
401
|
+
let currentRole: "user" | "agent" | null = null;
|
|
402
|
+
let wrote = false;
|
|
403
|
+
|
|
404
|
+
const paint = (text: string): string => {
|
|
405
|
+
if (!color) return text;
|
|
406
|
+
// BOLD for labels
|
|
407
|
+
return `\x1b[1m${text}\x1b[0m`;
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
const label = (role: "user" | "agent") => {
|
|
411
|
+
if (currentRole === role) return;
|
|
412
|
+
if (wrote) process.stderr.write("\n");
|
|
413
|
+
const text = role === "user" ? "you ›" : "agent ›";
|
|
414
|
+
process.stderr.write(paint(text) + " ");
|
|
415
|
+
currentRole = role;
|
|
416
|
+
wrote = true;
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
return {
|
|
420
|
+
onUpdate(notification: unknown) {
|
|
421
|
+
const update = (notification as Record<string, any>)?.update;
|
|
422
|
+
const kind = update?.sessionUpdate;
|
|
423
|
+
if (kind !== "user_message_chunk" && kind !== "agent_message_chunk") return;
|
|
424
|
+
|
|
425
|
+
const text = update.content?.type === "text" ? (update.content.text ?? "") : "";
|
|
426
|
+
if (!text) return;
|
|
427
|
+
|
|
428
|
+
label(kind === "user_message_chunk" ? "user" : "agent");
|
|
429
|
+
process.stderr.write(text);
|
|
430
|
+
},
|
|
431
|
+
finish() {
|
|
432
|
+
if (wrote) process.stderr.write("\n");
|
|
433
|
+
},
|
|
434
|
+
};
|
|
435
|
+
}
|