@qlairoslabs/acp-client 0.1.1 → 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 +1 -1
- package/package.json +1 -1
- package/src/agent-session.ts +20 -20
- package/src/repl/commands.ts +7 -7
- package/src/session-manager.ts +19 -18
- package/src/session-store.ts +14 -14
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.
|
|
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
package/src/agent-session.ts
CHANGED
|
@@ -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 `
|
|
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
|
//
|
|
@@ -43,33 +43,33 @@ export interface AgentSessionConfig {
|
|
|
43
43
|
logger?: Logger;
|
|
44
44
|
/** Optional catalog. When present, the session records itself here. */
|
|
45
45
|
sessions?: SessionManager;
|
|
46
|
-
/** Our stable
|
|
47
|
-
id?: string;
|
|
48
|
-
/** 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. */
|
|
49
47
|
agentSessionId?: string;
|
|
48
|
+
/** ACP/harness session to load on start (resume an existing provider conversation). */
|
|
49
|
+
internalSessionId?: string;
|
|
50
50
|
title?: string;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
export class AgentSession implements AgentClient, Bridgeable {
|
|
54
|
-
/** Our stable
|
|
55
|
-
readonly
|
|
54
|
+
/** Our stable AgentSession id — decoupled from the ACP/harness session id. */
|
|
55
|
+
readonly agentSessionId: string;
|
|
56
56
|
readonly adapterId: string;
|
|
57
57
|
|
|
58
58
|
private readonly controller: AgentController;
|
|
59
59
|
private readonly sessions?: SessionManager;
|
|
60
|
-
private
|
|
60
|
+
private _internalSessionId: string | null;
|
|
61
61
|
private _mode: AgentMode;
|
|
62
62
|
private _cwd?: string;
|
|
63
63
|
private _title?: string;
|
|
64
64
|
|
|
65
65
|
private constructor(config: AgentSessionConfig) {
|
|
66
|
-
this.
|
|
66
|
+
this.agentSessionId = config.agentSessionId ?? crypto.randomUUID();
|
|
67
67
|
this.adapterId = config.adapterId;
|
|
68
68
|
this.sessions = config.sessions;
|
|
69
69
|
this._mode = config.mode ?? "normal";
|
|
70
70
|
this._cwd = config.cwd;
|
|
71
71
|
this._title = config.title;
|
|
72
|
-
this.
|
|
72
|
+
this._internalSessionId = config.internalSessionId ?? null;
|
|
73
73
|
this.controller = new AgentController({
|
|
74
74
|
adapter: config.adapter,
|
|
75
75
|
adapterId: config.adapterId,
|
|
@@ -77,7 +77,7 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
77
77
|
execPrefix: config.execPrefix,
|
|
78
78
|
env: config.env,
|
|
79
79
|
cwd: config.cwd,
|
|
80
|
-
sessionId: config.
|
|
80
|
+
sessionId: config.internalSessionId,
|
|
81
81
|
mcpServers: config.mcpServers,
|
|
82
82
|
defaultPermission: config.defaultPermission,
|
|
83
83
|
clientInfo: config.clientInfo,
|
|
@@ -86,8 +86,8 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
/**
|
|
89
|
-
* Start a conversation. A FRESH one if no `
|
|
90
|
-
* `
|
|
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
|
|
91
91
|
* caller resolves the record's adapter and passes the record's fields here.
|
|
92
92
|
*/
|
|
93
93
|
static async create(config: AgentSessionConfig): Promise<AgentSession> {
|
|
@@ -98,13 +98,13 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
98
98
|
|
|
99
99
|
private async start(): Promise<void> {
|
|
100
100
|
const result = await this.controller.start();
|
|
101
|
-
this.
|
|
101
|
+
this._internalSessionId = result.sessionId || this._internalSessionId;
|
|
102
102
|
await this.persist();
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
// --- identity ----------------------------------------------------------------
|
|
106
|
-
get
|
|
107
|
-
return this.
|
|
106
|
+
get internalSessionId(): string | null {
|
|
107
|
+
return this._internalSessionId;
|
|
108
108
|
}
|
|
109
109
|
get mode(): AgentMode {
|
|
110
110
|
return this._mode;
|
|
@@ -131,7 +131,7 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
131
131
|
this._title = text.trim().replace(/\s+/g, " ").slice(0, 60);
|
|
132
132
|
}
|
|
133
133
|
// A session may have been created lazily on first prompt (degraded has none).
|
|
134
|
-
this.
|
|
134
|
+
this._internalSessionId = this.controller.currentSessionId ?? this._internalSessionId;
|
|
135
135
|
await this.persist();
|
|
136
136
|
return res;
|
|
137
137
|
}
|
|
@@ -148,7 +148,7 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
148
148
|
async setMode(mode: AgentMode): Promise<ConnectResult> {
|
|
149
149
|
const result = await this.controller.setMode(mode);
|
|
150
150
|
this._mode = this.controller.currentMode;
|
|
151
|
-
this.
|
|
151
|
+
this._internalSessionId = result.sessionId || null; // the ACP/harness session changes on swap
|
|
152
152
|
await this.persist();
|
|
153
153
|
return result;
|
|
154
154
|
}
|
|
@@ -192,8 +192,8 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
192
192
|
}
|
|
193
193
|
if (this.sessions) return this.sessions.listMerged(agentEntries, this.adapterId);
|
|
194
194
|
return agentEntries.map((e) => ({
|
|
195
|
-
id: e.sessionId,
|
|
196
195
|
agentSessionId: e.sessionId,
|
|
196
|
+
internalSessionId: e.sessionId,
|
|
197
197
|
adapter: this.adapterId,
|
|
198
198
|
cwd: e.cwd,
|
|
199
199
|
title: e.title,
|
|
@@ -205,8 +205,8 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
205
205
|
private async persist(): Promise<void> {
|
|
206
206
|
if (!this.sessions) return;
|
|
207
207
|
await this.sessions.record({
|
|
208
|
-
|
|
209
|
-
|
|
208
|
+
agentSessionId: this.agentSessionId,
|
|
209
|
+
internalSessionId: this._internalSessionId,
|
|
210
210
|
adapter: this.adapterId,
|
|
211
211
|
mode: this._mode,
|
|
212
212
|
cwd: this._cwd,
|
package/src/repl/commands.ts
CHANGED
|
@@ -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
|
|
33
|
-
readonly
|
|
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 (
|
|
100
|
-
if (!sessionManager || !adapterId || !client.
|
|
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
|
-
|
|
104
|
-
|
|
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.
|
|
256
|
+
note(`${isMerged ? merged.agentSessionId : plain.sessionId} ${s.title ?? ""} ${s.updatedAt ?? ""}${source}`);
|
|
257
257
|
}
|
|
258
258
|
}
|
|
259
259
|
} catch (e) {
|
package/src/session-manager.ts
CHANGED
|
@@ -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
|
|
14
|
-
|
|
15
|
-
|
|
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.
|
|
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(
|
|
44
|
-
return this.store.get(
|
|
44
|
+
get(agentSessionId: string): Promise<SessionRecord | null> {
|
|
45
|
+
return this.store.get(agentSessionId);
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
delete(
|
|
48
|
-
return this.store.delete(
|
|
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
|
|
77
|
-
for (const r of catalog) if (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>(); //
|
|
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 =
|
|
84
|
+
const rec = byInternalId.get(e.sessionId);
|
|
84
85
|
if (rec) {
|
|
85
|
-
claimed.add(rec.
|
|
86
|
+
claimed.add(rec.agentSessionId);
|
|
86
87
|
out.push({
|
|
87
|
-
|
|
88
|
-
|
|
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.
|
|
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,
|
package/src/session-store.ts
CHANGED
|
@@ -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
|
|
16
|
-
|
|
15
|
+
/** OUR stable AgentSession id (a uuid). Decoupled from the ACP sessionId on purpose. */
|
|
16
|
+
agentSessionId: string;
|
|
17
17
|
/**
|
|
18
|
-
* The
|
|
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
|
-
|
|
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(
|
|
35
|
+
get(agentSessionId: string): Promise<SessionRecord | null>;
|
|
36
36
|
list(): Promise<SessionRecord[]>;
|
|
37
|
-
delete(
|
|
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.
|
|
55
|
+
await writeFile(this.path(record.agentSessionId), JSON.stringify(record, null, 2), "utf8");
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
async get(
|
|
58
|
+
async get(agentSessionId: string): Promise<SessionRecord | null> {
|
|
59
59
|
try {
|
|
60
|
-
return JSON.parse(await readFile(this.path(
|
|
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(
|
|
85
|
+
async delete(agentSessionId: string): Promise<void> {
|
|
86
86
|
try {
|
|
87
|
-
await unlink(this.path(
|
|
87
|
+
await unlink(this.path(agentSessionId));
|
|
88
88
|
} catch {
|
|
89
89
|
/* already gone */
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
private path(
|
|
94
|
-
return join(this.dir, `${encodeURIComponent(
|
|
93
|
+
private path(agentSessionId: string): string {
|
|
94
|
+
return join(this.dir, `${encodeURIComponent(agentSessionId)}.json`);
|
|
95
95
|
}
|
|
96
96
|
}
|