@qlairoslabs/acp-client 0.1.0 → 0.1.2

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 CHANGED
@@ -128,7 +128,7 @@ await session.prompt("Start working on the auth module.");
128
128
 
129
129
  // later. List everything we know about, catalog ∪ the agent's live list
130
130
  for (const s of await session.listSessions()) {
131
- console.log(s.source, s.id, s.title); // source: "catalog" | "agent" | "both"
131
+ console.log(s.source, s.agentSessionId, s.title); // source: "catalog" | "agent" | "both"
132
132
  }
133
133
 
134
134
  await session.stop();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qlairoslabs/acp-client",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Embeddable Agent Client Protocol (ACP) client library with degraded PTY fallback.",
5
5
  "type": "module",
6
6
  "module": "src/index.ts",
@@ -16,6 +16,7 @@
16
16
  },
17
17
  "keywords": [
18
18
  "acp",
19
+ "agnostic acp client",
19
20
  "agent-client-protocol",
20
21
  "ai-agent",
21
22
  "coding-agent",
@@ -23,6 +24,7 @@
23
24
  "codex",
24
25
  "claude",
25
26
  "kimi",
27
+ "opencode",
26
28
  "repl",
27
29
  "bun"
28
30
  ],
@@ -2,7 +2,7 @@
2
2
  // consumer (the orchestration hub, the CLI) holds: a conversation with a stable identity
3
3
  // that persists across reconnects/mode-swaps, backed by a SessionManager catalog.
4
4
  //
5
- // It owns identity (our `id`, adapter, mode, agentSessionId, title) and persistence, and
5
+ // It owns identity (our `agentSessionId`, adapter, mode, internalSessionId, title) and persistence, and
6
6
  // HOLDS an AgentController for the live connection (the control plane). It implements
7
7
  // AgentClient, so the REPL and any caller can prompt it directly.
8
8
  //
@@ -10,6 +10,7 @@
10
10
  // agent dies on switch). Mode-swap (degrade/upgrade) stays within one AgentSession.
11
11
 
12
12
  import { AgentController, type AgentMode } from "./controller.ts";
13
+ import type * as schema from "@agentclientprotocol/sdk";
13
14
  import type { Adapter } from "./adapters.ts";
14
15
  import type { AgentClient, Bridgeable, BridgeOptions, InterruptOptions } from "./agent-client.ts";
15
16
  import type { Capabilities } from "./capabilities.ts";
@@ -35,38 +36,40 @@ export interface AgentSessionConfig {
35
36
  execPrefix?: string[];
36
37
  env?: Record<string, string>;
37
38
  cwd?: string;
39
+ /** Default MCP servers bound at session creation/switch, unless a call overrides them. */
40
+ mcpServers?: schema.McpServer[];
38
41
  defaultPermission?: "approve" | "cancel";
39
42
  clientInfo?: { name: string; title?: string; version?: string };
40
43
  logger?: Logger;
41
44
  /** Optional catalog. When present, the session records itself here. */
42
45
  sessions?: SessionManager;
43
- /** Our stable catalog id. Minted if omitted (a fresh conversation); pass a record's id to resume. */
44
- id?: string;
45
- /** Agent session to load on start (resume an existing agent conversation). */
46
+ /** Our stable AgentSession id. Minted if omitted; pass a record's agentSessionId to resume. */
46
47
  agentSessionId?: string;
48
+ /** ACP/harness session to load on start (resume an existing provider conversation). */
49
+ internalSessionId?: string;
47
50
  title?: string;
48
51
  }
49
52
 
50
53
  export class AgentSession implements AgentClient, Bridgeable {
51
- /** Our stable catalog id — decoupled from the agent's (mutable) session id. */
52
- readonly id: string;
54
+ /** Our stable AgentSession id — decoupled from the ACP/harness session id. */
55
+ readonly agentSessionId: string;
53
56
  readonly adapterId: string;
54
57
 
55
58
  private readonly controller: AgentController;
56
59
  private readonly sessions?: SessionManager;
57
- private _agentSessionId: string | null;
60
+ private _internalSessionId: string | null;
58
61
  private _mode: AgentMode;
59
62
  private _cwd?: string;
60
63
  private _title?: string;
61
64
 
62
65
  private constructor(config: AgentSessionConfig) {
63
- this.id = config.id ?? crypto.randomUUID();
66
+ this.agentSessionId = config.agentSessionId ?? crypto.randomUUID();
64
67
  this.adapterId = config.adapterId;
65
68
  this.sessions = config.sessions;
66
69
  this._mode = config.mode ?? "normal";
67
70
  this._cwd = config.cwd;
68
71
  this._title = config.title;
69
- this._agentSessionId = config.agentSessionId ?? null;
72
+ this._internalSessionId = config.internalSessionId ?? null;
70
73
  this.controller = new AgentController({
71
74
  adapter: config.adapter,
72
75
  adapterId: config.adapterId,
@@ -74,7 +77,8 @@ export class AgentSession implements AgentClient, Bridgeable {
74
77
  execPrefix: config.execPrefix,
75
78
  env: config.env,
76
79
  cwd: config.cwd,
77
- sessionId: config.agentSessionId,
80
+ sessionId: config.internalSessionId,
81
+ mcpServers: config.mcpServers,
78
82
  defaultPermission: config.defaultPermission,
79
83
  clientInfo: config.clientInfo,
80
84
  logger: config.logger,
@@ -82,8 +86,8 @@ export class AgentSession implements AgentClient, Bridgeable {
82
86
  }
83
87
 
84
88
  /**
85
- * Start a conversation. A FRESH one if no `id`/`agentSessionId`; a RESUMED one if
86
- * `agentSessionId` is given (the controller loads it). To resume from the catalog, the
89
+ * Start a conversation. A FRESH one if no `agentSessionId`/`internalSessionId`; a RESUMED one if
90
+ * `internalSessionId` is given (the controller loads it). To resume from the catalog, the
87
91
  * caller resolves the record's adapter and passes the record's fields here.
88
92
  */
89
93
  static async create(config: AgentSessionConfig): Promise<AgentSession> {
@@ -94,13 +98,13 @@ export class AgentSession implements AgentClient, Bridgeable {
94
98
 
95
99
  private async start(): Promise<void> {
96
100
  const result = await this.controller.start();
97
- this._agentSessionId = result.sessionId || this._agentSessionId;
101
+ this._internalSessionId = result.sessionId || this._internalSessionId;
98
102
  await this.persist();
99
103
  }
100
104
 
101
105
  // --- identity ----------------------------------------------------------------
102
- get agentSessionId(): string | null {
103
- return this._agentSessionId;
106
+ get internalSessionId(): string | null {
107
+ return this._internalSessionId;
104
108
  }
105
109
  get mode(): AgentMode {
106
110
  return this._mode;
@@ -127,7 +131,7 @@ export class AgentSession implements AgentClient, Bridgeable {
127
131
  this._title = text.trim().replace(/\s+/g, " ").slice(0, 60);
128
132
  }
129
133
  // A session may have been created lazily on first prompt (degraded has none).
130
- this._agentSessionId = this.controller.currentSessionId ?? this._agentSessionId;
134
+ this._internalSessionId = this.controller.currentSessionId ?? this._internalSessionId;
131
135
  await this.persist();
132
136
  return res;
133
137
  }
@@ -144,7 +148,7 @@ export class AgentSession implements AgentClient, Bridgeable {
144
148
  async setMode(mode: AgentMode): Promise<ConnectResult> {
145
149
  const result = await this.controller.setMode(mode);
146
150
  this._mode = this.controller.currentMode;
147
- this._agentSessionId = result.sessionId || null; // the agent session changes on swap
151
+ this._internalSessionId = result.sessionId || null; // the ACP/harness session changes on swap
148
152
  await this.persist();
149
153
  return result;
150
154
  }
@@ -188,8 +192,8 @@ export class AgentSession implements AgentClient, Bridgeable {
188
192
  }
189
193
  if (this.sessions) return this.sessions.listMerged(agentEntries, this.adapterId);
190
194
  return agentEntries.map((e) => ({
191
- id: e.sessionId,
192
195
  agentSessionId: e.sessionId,
196
+ internalSessionId: e.sessionId,
193
197
  adapter: this.adapterId,
194
198
  cwd: e.cwd,
195
199
  title: e.title,
@@ -201,8 +205,8 @@ export class AgentSession implements AgentClient, Bridgeable {
201
205
  private async persist(): Promise<void> {
202
206
  if (!this.sessions) return;
203
207
  await this.sessions.record({
204
- id: this.id,
205
- agentSessionId: this._agentSessionId,
208
+ agentSessionId: this.agentSessionId,
209
+ internalSessionId: this._internalSessionId,
206
210
  adapter: this.adapterId,
207
211
  mode: this._mode,
208
212
  cwd: this._cwd,
package/src/controller.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { AcpClient, type AcpClientConfig, type ConnectResult, type SwitchSessionOptions } from "./client.ts";
2
+ import type * as schema from "@agentclientprotocol/sdk";
2
3
  import type { Adapter } from "./adapters.ts";
3
4
  import type { AgentClient, Bridgeable, BridgeOptions, InterruptOptions } from "./agent-client.ts";
4
5
  import type { Capabilities } from "./capabilities.ts";
@@ -33,6 +34,8 @@ export interface AgentControllerConfig {
33
34
  cwd?: string;
34
35
  /** Session id to load on start(). */
35
36
  sessionId?: string;
37
+ /** Default MCP servers bound at session creation/switch, unless a call overrides them. */
38
+ mcpServers?: schema.McpServer[];
36
39
  defaultPermission?: "approve" | "cancel";
37
40
  clientInfo?: AcpClientConfig["clientInfo"];
38
41
  logger?: Logger;
@@ -211,9 +214,11 @@ export class AgentController implements AgentClient {
211
214
  return this.requireAcp().authenticate(methodId, handlers);
212
215
  }
213
216
 
214
- /** Force a fresh session (e.g. after /login). */
215
- newSession(): Promise<ConnectResult> {
216
- return this.requireAcp().newSession();
217
+ /** Force a fresh session (e.g. after /login). mcpServers defaults to config.mcpServers. */
218
+ newSession(options: { mcpServers?: schema.McpServer[] } = {}): Promise<ConnectResult> {
219
+ return this.requireAcp().newSession({
220
+ mcpServers: options.mcpServers ?? this.config.mcpServers,
221
+ });
217
222
  }
218
223
 
219
224
  listSessions(): Promise<SessionListPage> {
@@ -221,15 +226,24 @@ export class AgentController implements AgentClient {
221
226
  }
222
227
 
223
228
  loadSession(sessionId: string, options: SwitchSessionOptions = {}): Promise<ConnectResult> {
224
- return this.requireAcp().loadSession(sessionId, options);
229
+ return this.requireAcp().loadSession(sessionId, {
230
+ ...options,
231
+ mcpServers: options.mcpServers ?? this.config.mcpServers,
232
+ });
225
233
  }
226
234
 
227
235
  resumeSession(sessionId: string, options: SwitchSessionOptions = {}): Promise<ConnectResult> {
228
- return this.requireAcp().resumeSession(sessionId, options);
236
+ return this.requireAcp().resumeSession(sessionId, {
237
+ ...options,
238
+ mcpServers: options.mcpServers ?? this.config.mcpServers,
239
+ });
229
240
  }
230
241
 
231
242
  forkSession(sessionId: string, options: SwitchSessionOptions = {}): Promise<ConnectResult> {
232
- return this.requireAcp().forkSession(sessionId, options);
243
+ return this.requireAcp().forkSession(sessionId, {
244
+ ...options,
245
+ mcpServers: options.mcpServers ?? this.config.mcpServers,
246
+ });
233
247
  }
234
248
 
235
249
  setConfig(configId: string, value: unknown): Promise<void> {
@@ -368,7 +382,7 @@ export class AgentController implements AgentClient {
368
382
  }
369
383
  let result: ConnectResult;
370
384
  try {
371
- result = await client.startSession();
385
+ result = await client.startSession({ mcpServers: this.config.mcpServers });
372
386
  } catch (err) {
373
387
  this.config.logger?.warn("session_start_failed", { error: String(err) });
374
388
  result = { sessionId: "", resumed: false };
@@ -29,8 +29,8 @@ import type { SessionManager, MergedSession } from "../session-manager.ts";
29
29
 
30
30
  /** The control-plane surface required by the reusable slash commands. */
31
31
  export interface InteractiveAgentClient extends AgentClient, Bridgeable {
32
- /** Optional durable catalog identity, when the client is session-backed. */
33
- readonly id?: string;
32
+ /** Optional durable AgentSession identity, when the client is session-backed. */
33
+ readonly agentSessionId?: string;
34
34
  readonly currentAdapterId: string;
35
35
  readonly currentMode: AgentMode;
36
36
  readonly currentSessionId: string | null;
@@ -96,12 +96,12 @@ export interface AgentCommands {
96
96
  export function createAgentCommands(deps: CommandHandlerDeps): AgentCommands {
97
97
  const { client, note, color = false, sessionManager, adapterId } = deps;
98
98
 
99
- const recordSession = async (agentSessionId: string | null): Promise<void> => {
100
- if (!sessionManager || !adapterId || !client.id) return;
99
+ const recordSession = async (internalSessionId: string | null): Promise<void> => {
100
+ if (!sessionManager || !adapterId || !client.agentSessionId) return;
101
101
  try {
102
102
  await sessionManager.record({
103
- id: client.id,
104
- agentSessionId,
103
+ agentSessionId: client.agentSessionId,
104
+ internalSessionId,
105
105
  adapter: adapterId,
106
106
  mode: client.currentMode,
107
107
  cwd: client.currentCwd,
@@ -253,7 +253,7 @@ export function createAgentCommands(deps: CommandHandlerDeps): AgentCommands {
253
253
  const merged = s as MergedSession;
254
254
  const plain = s as SessionListEntry;
255
255
  const source = isMerged ? ` [${merged.source}]` : "";
256
- note(`${isMerged ? merged.id : plain.sessionId} ${s.title ?? ""} ${s.updatedAt ?? ""}${source}`);
256
+ note(`${isMerged ? merged.agentSessionId : plain.sessionId} ${s.title ?? ""} ${s.updatedAt ?? ""}${source}`);
257
257
  }
258
258
  }
259
259
  } catch (e) {
@@ -10,9 +10,10 @@ import type { SessionListEntry } from "./types.ts";
10
10
 
11
11
  /** A row in the merged session view. `source` says where it was seen. */
12
12
  export interface MergedSession {
13
- /** Our catalog id when cataloged, else the agent's sessionId. */
14
- id: string;
15
- agentSessionId: string | null;
13
+ /** Our AgentSession id when cataloged, else the ACP/harness sessionId. */
14
+ agentSessionId: string;
15
+ /** The ACP/harness protocol sessionId used by lower-level session operations. */
16
+ internalSessionId: string | null;
16
17
  adapter: string;
17
18
  mode?: "normal" | "degraded";
18
19
  cwd?: string;
@@ -30,7 +31,7 @@ export class SessionManager {
30
31
  /** Upsert a record: preserves createdAt, bumps updatedAt. Returns the stored record. */
31
32
  async record(input: SessionInput): Promise<SessionRecord> {
32
33
  const now = new Date().toISOString();
33
- const existing = await this.store.get(input.id);
34
+ const existing = await this.store.get(input.agentSessionId);
34
35
  const record: SessionRecord = {
35
36
  ...input,
36
37
  createdAt: existing?.createdAt ?? now,
@@ -40,12 +41,12 @@ export class SessionManager {
40
41
  return record;
41
42
  }
42
43
 
43
- get(id: string): Promise<SessionRecord | null> {
44
- return this.store.get(id);
44
+ get(agentSessionId: string): Promise<SessionRecord | null> {
45
+ return this.store.get(agentSessionId);
45
46
  }
46
47
 
47
- delete(id: string): Promise<void> {
48
- return this.store.delete(id);
48
+ delete(agentSessionId: string): Promise<void> {
49
+ return this.store.delete(agentSessionId);
49
50
  }
50
51
 
51
52
  /** Catalog records, newest first. */
@@ -73,19 +74,19 @@ export function mergeSessions(
73
74
  agentEntries: SessionListEntry[],
74
75
  adapter: string,
75
76
  ): MergedSession[] {
76
- const byAgentId = new Map<string, SessionRecord>();
77
- for (const r of catalog) if (r.agentSessionId) byAgentId.set(r.agentSessionId, r);
77
+ const byInternalId = new Map<string, SessionRecord>();
78
+ for (const r of catalog) if (r.internalSessionId) byInternalId.set(r.internalSessionId, r);
78
79
 
79
80
  const out: MergedSession[] = [];
80
- const claimed = new Set<string>(); // catalog ids already emitted via the agent list
81
+ const claimed = new Set<string>(); // AgentSession ids already emitted via the agent list
81
82
 
82
83
  for (const e of agentEntries) {
83
- const rec = byAgentId.get(e.sessionId);
84
+ const rec = byInternalId.get(e.sessionId);
84
85
  if (rec) {
85
- claimed.add(rec.id);
86
+ claimed.add(rec.agentSessionId);
86
87
  out.push({
87
- id: rec.id,
88
- agentSessionId: e.sessionId,
88
+ agentSessionId: rec.agentSessionId,
89
+ internalSessionId: e.sessionId,
89
90
  adapter: rec.adapter,
90
91
  mode: rec.mode,
91
92
  cwd: e.cwd ?? rec.cwd,
@@ -96,8 +97,8 @@ export function mergeSessions(
96
97
  } else {
97
98
  // Reported by the agent but not in our catalog (made via native CLI/bridge).
98
99
  out.push({
99
- id: e.sessionId,
100
100
  agentSessionId: e.sessionId,
101
+ internalSessionId: e.sessionId,
101
102
  adapter,
102
103
  cwd: e.cwd,
103
104
  title: e.title,
@@ -108,10 +109,10 @@ export function mergeSessions(
108
109
  }
109
110
 
110
111
  for (const r of catalog) {
111
- if (claimed.has(r.id)) continue; // already emitted as "both"
112
+ if (claimed.has(r.agentSessionId)) continue; // already emitted as "both"
112
113
  out.push({
113
- id: r.id,
114
114
  agentSessionId: r.agentSessionId,
115
+ internalSessionId: r.internalSessionId,
115
116
  adapter: r.adapter,
116
117
  mode: r.mode,
117
118
  cwd: r.cwd,
@@ -1,6 +1,6 @@
1
1
  // Session catalog storage. The library is NOT the source of truth for conversation
2
2
  // CONTENT (the agent persists that; loadSession replays it) — this is a routing catalog:
3
- // which adapter/mode/cwd a session belongs to, keyed by OUR stable id. That's the part
3
+ // which adapter/mode/cwd a session belongs to, keyed by OUR stable agent session id. That's the part
4
4
  // the agent can't tell us, and it survives a broken agent `session/list` (e.g. kimi).
5
5
  //
6
6
  // `SessionStore` is an interface so consumers (the orchestration hub) can back it with
@@ -12,14 +12,14 @@ import { homedir } from "node:os";
12
12
  import { join } from "node:path";
13
13
 
14
14
  export interface SessionRecord {
15
- /** OUR stable catalog id (a uuid). Decoupled from the agent's sessionId on purpose. */
16
- id: string;
15
+ /** OUR stable AgentSession id (a uuid). Decoupled from the ACP sessionId on purpose. */
16
+ agentSessionId: string;
17
17
  /**
18
- * The agent's protocol sessionId. Null in degraded mode (no protocol session), and it
18
+ * The ACP/harness protocol sessionId. Null in degraded mode (no protocol session), and it
19
19
  * CHANGES on mode-swap (the pty session ≠ the ACP session). Kept as a mutable field so
20
20
  * a future portable conversation can span several agent sessions without re-keying.
21
21
  */
22
- agentSessionId: string | null;
22
+ internalSessionId: string | null;
23
23
  /** Which adapter this session belongs to — the routing the agent's own list can't give. */
24
24
  adapter: string;
25
25
  mode: "normal" | "degraded";
@@ -32,9 +32,9 @@ export interface SessionRecord {
32
32
 
33
33
  export interface SessionStore {
34
34
  save(record: SessionRecord): Promise<void>;
35
- get(id: string): Promise<SessionRecord | null>;
35
+ get(agentSessionId: string): Promise<SessionRecord | null>;
36
36
  list(): Promise<SessionRecord[]>;
37
- delete(id: string): Promise<void>;
37
+ delete(agentSessionId: string): Promise<void>;
38
38
  }
39
39
 
40
40
  /** Default location for the file store. Overridable; the hub injects its own store. */
@@ -52,12 +52,12 @@ export class FileSessionStore implements SessionStore {
52
52
 
53
53
  async save(record: SessionRecord): Promise<void> {
54
54
  await mkdir(this.dir, { recursive: true });
55
- await writeFile(this.path(record.id), JSON.stringify(record, null, 2), "utf8");
55
+ await writeFile(this.path(record.agentSessionId), JSON.stringify(record, null, 2), "utf8");
56
56
  }
57
57
 
58
- async get(id: string): Promise<SessionRecord | null> {
58
+ async get(agentSessionId: string): Promise<SessionRecord | null> {
59
59
  try {
60
- return JSON.parse(await readFile(this.path(id), "utf8")) as SessionRecord;
60
+ return JSON.parse(await readFile(this.path(agentSessionId), "utf8")) as SessionRecord;
61
61
  } catch {
62
62
  return null;
63
63
  }
@@ -82,15 +82,15 @@ export class FileSessionStore implements SessionStore {
82
82
  return records;
83
83
  }
84
84
 
85
- async delete(id: string): Promise<void> {
85
+ async delete(agentSessionId: string): Promise<void> {
86
86
  try {
87
- await unlink(this.path(id));
87
+ await unlink(this.path(agentSessionId));
88
88
  } catch {
89
89
  /* already gone */
90
90
  }
91
91
  }
92
92
 
93
- private path(id: string): string {
94
- return join(this.dir, `${encodeURIComponent(id)}.json`);
93
+ private path(agentSessionId: string): string {
94
+ return join(this.dir, `${encodeURIComponent(agentSessionId)}.json`);
95
95
  }
96
96
  }