@moxt-ai/mobius 0.0.125 → 0.0.127
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/acp/native-session-browsers.d.ts +15 -0
- package/dist/acp/native-session-browsers.d.ts.map +1 -0
- package/dist/acp/native-session-browsers.js +94 -0
- package/dist/acp/native-session-browsers.js.map +1 -0
- package/dist/acp/retained-sessions.d.ts +21 -0
- package/dist/acp/retained-sessions.d.ts.map +1 -0
- package/dist/acp/retained-sessions.js +107 -0
- package/dist/acp/retained-sessions.js.map +1 -0
- package/dist/acp/session-executions.d.ts +11 -0
- package/dist/acp/session-executions.d.ts.map +1 -0
- package/dist/acp/session-executions.js +76 -0
- package/dist/acp/session-executions.js.map +1 -0
- package/dist/acp/session-manager.d.ts.map +1 -1
- package/dist/acp/session-manager.js +155 -369
- package/dist/acp/session-manager.js.map +1 -1
- package/dist/cli.js +467 -414
- package/dist/cli.js.map +4 -4
- package/dist/index.js +490 -437
- package/dist/index.js.map +4 -4
- package/dist/runtime.js +466 -413
- package/dist/runtime.js.map +4 -4
- package/package.json +1 -1
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { NativeSessionBrowsers } from "./native-session-browsers.js";
|
|
1
2
|
import { AcpProcessSession } from "./process-session.js";
|
|
3
|
+
import { RetainedSessions } from "./retained-sessions.js";
|
|
4
|
+
import { SessionExecutions } from "./session-executions.js";
|
|
2
5
|
export { createWritableAcpStream } from "./process-endpoint.js";
|
|
3
6
|
import { unmeasuredOperationMetrics } from "../infra/operation-metrics.js";
|
|
4
7
|
import { isNativeChatSessionId } from "../native-sessions/chat-agent.js";
|
|
@@ -11,18 +14,11 @@ export { AcpAgentSettingsSnapshot, AcpPreparedSession, NullAcpSessionObserver, }
|
|
|
11
14
|
import { AcpAgentSettingsSnapshot, NullAcpSessionObserver, } from "./session-contracts.js";
|
|
12
15
|
export class AcpSessionManager {
|
|
13
16
|
#processes = new AcpProcessPool();
|
|
14
|
-
#
|
|
17
|
+
#sessions = new RetainedSessions({ touch: this.#processes.touch, changed: this.#processes.notifyChanged });
|
|
18
|
+
#browsers = new NativeSessionBrowsers(this.#processes);
|
|
19
|
+
#executions = new SessionExecutions();
|
|
15
20
|
#agentSettingsObserver = new NullAcpSessionObserver();
|
|
16
21
|
#nativeOperations = new Set();
|
|
17
|
-
#nativeBrowserObserver = new NullAcpSessionObserver();
|
|
18
|
-
#nativeBrowserPreparing = new Map();
|
|
19
|
-
#nativeBrowserSessions = new Map();
|
|
20
|
-
#nativeBrowserStore = new MemoryAcpSessionStore();
|
|
21
|
-
#nativeBrowserUseCounts = new Map();
|
|
22
|
-
#cancellationRequests = new Map();
|
|
23
|
-
#preparing = new Map();
|
|
24
|
-
#sessionUseCounts = new Map();
|
|
25
|
-
#sessions = new Map();
|
|
26
22
|
#store;
|
|
27
23
|
#nativeHistoryReader;
|
|
28
24
|
#sessionIdentity;
|
|
@@ -36,88 +32,21 @@ export class AcpSessionManager {
|
|
|
36
32
|
setObserver = (observer) => {
|
|
37
33
|
this.#observer = observer;
|
|
38
34
|
};
|
|
39
|
-
#touchSession(sessionId) {
|
|
40
|
-
const runtime = this.#sessions.get(sessionId);
|
|
41
|
-
if (runtime === undefined) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
this.#processes.touch(runtime);
|
|
45
|
-
}
|
|
46
|
-
#touchExpectedSession(sessionId, expected) {
|
|
47
|
-
if (this.#sessions.get(sessionId) !== expected) {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
this.#processes.touch(expected);
|
|
51
|
-
}
|
|
52
|
-
#touchNativeBrowserSession(sessionId) {
|
|
53
|
-
const runtime = this.#nativeBrowserSessions.get(sessionId);
|
|
54
|
-
if (runtime === undefined) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
this.#processes.touch(runtime);
|
|
58
|
-
}
|
|
59
|
-
#touchExpectedNativeBrowserSession(sessionId, expected) {
|
|
60
|
-
if (this.#nativeBrowserSessions.get(sessionId) !== expected) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
this.#processes.touch(expected);
|
|
64
|
-
}
|
|
65
|
-
#beginSessionUse(sessionId) {
|
|
66
|
-
this.#sessionUseCounts.set(sessionId, (this.#sessionUseCounts.get(sessionId) ?? 0) + 1);
|
|
67
|
-
this.#touchSession(sessionId);
|
|
68
|
-
}
|
|
69
|
-
#endSessionUse(sessionId) {
|
|
70
|
-
const count = this.#sessionUseCounts.get(sessionId) ?? 0;
|
|
71
|
-
if (count <= 1) {
|
|
72
|
-
this.#sessionUseCounts.delete(sessionId);
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
this.#sessionUseCounts.set(sessionId, count - 1);
|
|
76
|
-
}
|
|
77
|
-
this.#touchSession(sessionId);
|
|
78
|
-
this.#processes.notifyChanged();
|
|
79
|
-
}
|
|
80
|
-
#beginNativeBrowserUse(sessionId) {
|
|
81
|
-
this.#nativeBrowserUseCounts.set(sessionId, (this.#nativeBrowserUseCounts.get(sessionId) ?? 0) + 1);
|
|
82
|
-
this.#touchNativeBrowserSession(sessionId);
|
|
83
|
-
}
|
|
84
|
-
#endNativeBrowserUse(sessionId) {
|
|
85
|
-
const count = this.#nativeBrowserUseCounts.get(sessionId) ?? 0;
|
|
86
|
-
if (count <= 1) {
|
|
87
|
-
this.#nativeBrowserUseCounts.delete(sessionId);
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
this.#nativeBrowserUseCounts.set(sessionId, count - 1);
|
|
91
|
-
}
|
|
92
|
-
this.#touchNativeBrowserSession(sessionId);
|
|
93
|
-
this.#processes.notifyChanged();
|
|
94
|
-
}
|
|
95
35
|
#isNativeSessionOperationActive(sessionId) {
|
|
96
36
|
const stored = this.#store.read(sessionId);
|
|
97
37
|
return (stored instanceof FoundAcpStoredSession &&
|
|
98
38
|
this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`));
|
|
99
39
|
}
|
|
100
|
-
#retention(sessionId
|
|
101
|
-
if (kind === "execution") {
|
|
102
|
-
return {
|
|
103
|
-
isBusy: () => this.#activeCommandIds.has(sessionId) ||
|
|
104
|
-
this.#cancellationRequests.has(sessionId) ||
|
|
105
|
-
this.#preparing.has(sessionId) ||
|
|
106
|
-
(this.#sessionUseCounts.get(sessionId) ?? 0) > 0 ||
|
|
107
|
-
this.#isNativeSessionOperationActive(sessionId),
|
|
108
|
-
retire: (runtime) => this.#retireSessionProcess(sessionId, runtime, () => runtime.close()),
|
|
109
|
-
};
|
|
110
|
-
}
|
|
40
|
+
#retention(sessionId) {
|
|
111
41
|
return {
|
|
112
|
-
isBusy: () => this.#
|
|
113
|
-
|
|
42
|
+
isBusy: () => this.#executions.isBusy(sessionId) ||
|
|
43
|
+
this.#sessions.isBusy(sessionId) ||
|
|
44
|
+
this.#isNativeSessionOperationActive(sessionId),
|
|
45
|
+
retire: (runtime) => this.#retireSessionProcess(sessionId, runtime, () => runtime.close()),
|
|
114
46
|
};
|
|
115
47
|
}
|
|
116
48
|
async #retireSessionProcess(sessionId, runtime, close) {
|
|
117
|
-
const retained = this.#sessions.
|
|
118
|
-
if (retained) {
|
|
119
|
-
this.#sessions.delete(sessionId);
|
|
120
|
-
}
|
|
49
|
+
const retained = this.#sessions.detach(sessionId, runtime);
|
|
121
50
|
try {
|
|
122
51
|
await this.#processes.retire(runtime, close);
|
|
123
52
|
}
|
|
@@ -131,135 +60,71 @@ export class AcpSessionManager {
|
|
|
131
60
|
}
|
|
132
61
|
}
|
|
133
62
|
}
|
|
134
|
-
async #retireNativeBrowserProcess(sessionId, runtime, close) {
|
|
135
|
-
const retained = this.#nativeBrowserSessions.get(sessionId) === runtime;
|
|
136
|
-
if (retained) {
|
|
137
|
-
this.#nativeBrowserSessions.delete(sessionId);
|
|
138
|
-
this.#nativeBrowserStore.remove(sessionId);
|
|
139
|
-
}
|
|
140
|
-
await this.#processes.retire(runtime, close);
|
|
141
|
-
}
|
|
142
63
|
async #startRetainedSessionProcess(options, start) {
|
|
143
|
-
const runtime = await this.#processes.startRetained(options.signal, start, this.#retention(options.sessionId
|
|
144
|
-
this.#sessions.
|
|
145
|
-
return runtime;
|
|
146
|
-
}
|
|
147
|
-
async #startRetainedNativeBrowserProcess(options, start) {
|
|
148
|
-
const runtime = await this.#processes.startRetained(options.signal, start, this.#retention(options.sessionId, "browser"));
|
|
149
|
-
this.#nativeBrowserSessions.set(options.sessionId, runtime);
|
|
64
|
+
const runtime = await this.#processes.startRetained(options.signal, start, this.#retention(options.sessionId));
|
|
65
|
+
this.#sessions.retain(options.sessionId, runtime);
|
|
150
66
|
return runtime;
|
|
151
67
|
}
|
|
152
|
-
async #startNativeBrowserPreparation(options) {
|
|
153
|
-
const operation = (async () => {
|
|
154
|
-
try {
|
|
155
|
-
return await this.#startRetainedNativeBrowserProcess(options, async () => await AcpProcessSession.startNativeSessionBrowser(options, () => this.#nativeBrowserObserver, this.#nativeBrowserStore));
|
|
156
|
-
}
|
|
157
|
-
finally {
|
|
158
|
-
this.#nativeBrowserPreparing.delete(options.sessionId);
|
|
159
|
-
this.#processes.notifyChanged();
|
|
160
|
-
}
|
|
161
|
-
})();
|
|
162
|
-
this.#nativeBrowserPreparing.set(options.sessionId, operation);
|
|
163
|
-
return await operation;
|
|
164
|
-
}
|
|
165
|
-
async #replaceNativeBrowserPreparation(existing, options) {
|
|
166
|
-
const operation = (async () => {
|
|
167
|
-
try {
|
|
168
|
-
await this.#retireNativeBrowserProcess(options.sessionId, existing, async () => await existing.close());
|
|
169
|
-
return await this.#startRetainedNativeBrowserProcess(options, async () => await AcpProcessSession.startNativeSessionBrowser(options, () => this.#nativeBrowserObserver, this.#nativeBrowserStore));
|
|
170
|
-
}
|
|
171
|
-
finally {
|
|
172
|
-
this.#nativeBrowserPreparing.delete(options.sessionId);
|
|
173
|
-
this.#processes.notifyChanged();
|
|
174
|
-
}
|
|
175
|
-
})();
|
|
176
|
-
this.#nativeBrowserPreparing.set(options.sessionId, operation);
|
|
177
|
-
return await operation;
|
|
178
|
-
}
|
|
179
68
|
async #startPreparation(options, metrics = unmeasuredOperationMetrics) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
finally {
|
|
190
|
-
this.#preparing.delete(options.sessionId);
|
|
191
|
-
this.#processes.notifyChanged();
|
|
192
|
-
}
|
|
193
|
-
})();
|
|
194
|
-
this.#preparing.set(options.sessionId, operation);
|
|
195
|
-
return await operation;
|
|
69
|
+
return await this.#sessions.prepare(options.sessionId, async () => {
|
|
70
|
+
return await this.#startRetainedSessionProcess(options, async () => await AcpProcessSession.start(options.origin === undefined &&
|
|
71
|
+
this.#sessionIdentity === "native" &&
|
|
72
|
+
isNativeChatSessionId(options.agent.id, options.sessionId) &&
|
|
73
|
+
this.#store.read(options.sessionId) instanceof MissingAcpStoredSession
|
|
74
|
+
? { ...options, origin: { kind: "import", nativeSessionId: options.sessionId } }
|
|
75
|
+
: options, () => this.#observer, this.#store, metrics));
|
|
76
|
+
});
|
|
196
77
|
}
|
|
197
78
|
async #replacePreparation(existing, options, metrics) {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
finally {
|
|
205
|
-
this.#preparing.delete(options.sessionId);
|
|
206
|
-
this.#processes.notifyChanged();
|
|
207
|
-
}
|
|
208
|
-
})();
|
|
209
|
-
this.#preparing.set(options.sessionId, operation);
|
|
210
|
-
return await operation;
|
|
79
|
+
return await this.#sessions.prepare(options.sessionId, async () => {
|
|
80
|
+
await this.#retireSessionProcess(options.sessionId, existing, async () => await metrics.measure("runtime-replacement-close", async () => await existing.close()));
|
|
81
|
+
this.#store.remove(options.sessionId);
|
|
82
|
+
return await this.#startRetainedSessionProcess(options, async () => await AcpProcessSession.start(options, () => this.#observer, this.#store, metrics));
|
|
83
|
+
});
|
|
211
84
|
}
|
|
212
85
|
async #replaceExecutionPreparation(existing, options) {
|
|
213
86
|
const desiredConfigOptions = existing.prepared().configOptions;
|
|
214
|
-
|
|
87
|
+
return await this.#sessions.prepare(options.sessionId, async () => {
|
|
88
|
+
await this.#retireSessionProcess(options.sessionId, existing, async () => await existing.close());
|
|
89
|
+
const runtime = await this.#startRetainedSessionProcess(options, async () => await AcpProcessSession.start(options, () => this.#observer, this.#store));
|
|
215
90
|
try {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
if (desired.type === "boolean") {
|
|
225
|
-
if (current.booleanValue !== desired.booleanValue) {
|
|
226
|
-
await runtime.configure({
|
|
227
|
-
...options,
|
|
228
|
-
booleanValue: desired.booleanValue,
|
|
229
|
-
configId: desired.id,
|
|
230
|
-
requestId: `execution-prepare-${desired.id}`,
|
|
231
|
-
value: "",
|
|
232
|
-
valueType: "boolean",
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
continue;
|
|
236
|
-
}
|
|
237
|
-
if (current.currentValue !== desired.currentValue &&
|
|
238
|
-
current.values.some((value) => value.value === desired.currentValue)) {
|
|
91
|
+
for (const desired of desiredConfigOptions) {
|
|
92
|
+
const current = runtime.prepared().configOptions.find((option) => option.id === desired.id);
|
|
93
|
+
if (current === undefined || current.type !== desired.type) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (desired.type === "boolean") {
|
|
97
|
+
if (current.booleanValue !== desired.booleanValue) {
|
|
239
98
|
await runtime.configure({
|
|
240
99
|
...options,
|
|
241
|
-
booleanValue:
|
|
100
|
+
booleanValue: desired.booleanValue,
|
|
242
101
|
configId: desired.id,
|
|
243
102
|
requestId: `execution-prepare-${desired.id}`,
|
|
244
|
-
value:
|
|
245
|
-
valueType: "
|
|
103
|
+
value: "",
|
|
104
|
+
valueType: "boolean",
|
|
246
105
|
});
|
|
247
106
|
}
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (current.currentValue !== desired.currentValue &&
|
|
110
|
+
current.values.some((value) => value.value === desired.currentValue)) {
|
|
111
|
+
await runtime.configure({
|
|
112
|
+
...options,
|
|
113
|
+
booleanValue: false,
|
|
114
|
+
configId: desired.id,
|
|
115
|
+
requestId: `execution-prepare-${desired.id}`,
|
|
116
|
+
value: desired.currentValue,
|
|
117
|
+
valueType: "select",
|
|
118
|
+
});
|
|
248
119
|
}
|
|
249
120
|
}
|
|
250
|
-
catch (error) {
|
|
251
|
-
await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
|
|
252
|
-
throw error;
|
|
253
|
-
}
|
|
254
|
-
return runtime;
|
|
255
121
|
}
|
|
256
|
-
|
|
257
|
-
this.#
|
|
258
|
-
|
|
122
|
+
catch (error) {
|
|
123
|
+
await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
|
|
124
|
+
throw error;
|
|
259
125
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
return await operation;
|
|
126
|
+
return runtime;
|
|
127
|
+
});
|
|
263
128
|
}
|
|
264
129
|
prepareSession = async (options, metrics = unmeasuredOperationMetrics) => {
|
|
265
130
|
const stored = metrics.measureSync("session-state-read", () => this.#store.read(options.sessionId));
|
|
@@ -270,11 +135,11 @@ export class AcpSessionManager {
|
|
|
270
135
|
this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
|
|
271
136
|
throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
|
|
272
137
|
}
|
|
273
|
-
const pending = this.#
|
|
138
|
+
const pending = this.#sessions.pending(options.sessionId);
|
|
274
139
|
if (pending !== undefined) {
|
|
275
140
|
const runtime = await metrics.measure("session-preparation-wait", async () => await pending);
|
|
276
141
|
if (runtime.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
|
|
277
|
-
this.#
|
|
142
|
+
this.#sessions.touch(options.sessionId, runtime);
|
|
278
143
|
return runtime.prepared();
|
|
279
144
|
}
|
|
280
145
|
return await this.prepareSession(options, metrics);
|
|
@@ -282,7 +147,7 @@ export class AcpSessionManager {
|
|
|
282
147
|
const existing = this.#sessions.get(options.sessionId);
|
|
283
148
|
if (existing !== undefined) {
|
|
284
149
|
if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
|
|
285
|
-
this.#
|
|
150
|
+
this.#sessions.touch(options.sessionId, existing);
|
|
286
151
|
return existing.prepared();
|
|
287
152
|
}
|
|
288
153
|
if (!existing.canRebind()) {
|
|
@@ -323,11 +188,11 @@ export class AcpSessionManager {
|
|
|
323
188
|
* Use inspectAgentSettings for Agent-scoped settings discovery.
|
|
324
189
|
*/
|
|
325
190
|
inspectSession = async (options, metrics = unmeasuredOperationMetrics) => {
|
|
326
|
-
if (this.#
|
|
191
|
+
if (this.#sessions.pending(options.sessionId) !== undefined ||
|
|
192
|
+
this.#sessions.get(options.sessionId) !== undefined) {
|
|
327
193
|
throw new AcpExecutionError("The configuration inspection session is already in use", {}, "session_busy");
|
|
328
194
|
}
|
|
329
|
-
this.#
|
|
330
|
-
try {
|
|
195
|
+
return await this.#sessions.use(options.sessionId, async () => {
|
|
331
196
|
const runtime = await this.#startPreparation(options, metrics);
|
|
332
197
|
try {
|
|
333
198
|
return runtime.prepared();
|
|
@@ -340,13 +205,10 @@ export class AcpSessionManager {
|
|
|
340
205
|
this.#store.remove(options.sessionId);
|
|
341
206
|
}
|
|
342
207
|
}
|
|
343
|
-
}
|
|
344
|
-
finally {
|
|
345
|
-
this.#endSessionUse(options.sessionId);
|
|
346
|
-
}
|
|
208
|
+
});
|
|
347
209
|
};
|
|
348
210
|
prepareExecutionSession = async (options) => {
|
|
349
|
-
const pending = this.#
|
|
211
|
+
const pending = this.#sessions.pending(options.sessionId);
|
|
350
212
|
if (pending !== undefined) {
|
|
351
213
|
await pending;
|
|
352
214
|
return await this.prepareExecutionSession(options);
|
|
@@ -359,7 +221,7 @@ export class AcpSessionManager {
|
|
|
359
221
|
throw new AcpExecutionError("The agent or workspace cannot change after a conversation has started", {}, "session_identity_mismatch");
|
|
360
222
|
}
|
|
361
223
|
if (existing.usesEnvironment(options.agent.environment)) {
|
|
362
|
-
this.#
|
|
224
|
+
this.#sessions.touch(options.sessionId, existing);
|
|
363
225
|
return existing.prepared();
|
|
364
226
|
}
|
|
365
227
|
if (existing.hasRunningTurn()) {
|
|
@@ -367,73 +229,37 @@ export class AcpSessionManager {
|
|
|
367
229
|
}
|
|
368
230
|
return (await this.#replaceExecutionPreparation(existing, options)).prepared();
|
|
369
231
|
};
|
|
370
|
-
prepareNativeSessionBrowser =
|
|
371
|
-
const pending = this.#nativeBrowserPreparing.get(options.sessionId);
|
|
372
|
-
if (pending !== undefined) {
|
|
373
|
-
const runtime = await pending;
|
|
374
|
-
if (runtime.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
|
|
375
|
-
this.#touchExpectedNativeBrowserSession(options.sessionId, runtime);
|
|
376
|
-
return runtime.prepared();
|
|
377
|
-
}
|
|
378
|
-
return await this.prepareNativeSessionBrowser(options);
|
|
379
|
-
}
|
|
380
|
-
const existing = this.#nativeBrowserSessions.get(options.sessionId);
|
|
381
|
-
if (existing !== undefined) {
|
|
382
|
-
if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
|
|
383
|
-
this.#touchExpectedNativeBrowserSession(options.sessionId, existing);
|
|
384
|
-
return existing.prepared();
|
|
385
|
-
}
|
|
386
|
-
if (!existing.canRebind()) {
|
|
387
|
-
throw new AcpExecutionError("The agent or workspace cannot change after a conversation has started", {}, "session_identity_mismatch");
|
|
388
|
-
}
|
|
389
|
-
const runtime = await this.#replaceNativeBrowserPreparation(existing, options);
|
|
390
|
-
return runtime.prepared();
|
|
391
|
-
}
|
|
392
|
-
const runtime = await this.#startNativeBrowserPreparation(options);
|
|
393
|
-
return runtime.prepared();
|
|
394
|
-
};
|
|
232
|
+
prepareNativeSessionBrowser = (options) => this.#browsers.prepare(options);
|
|
395
233
|
configureSession = async (options) => {
|
|
396
|
-
this.#
|
|
397
|
-
try {
|
|
234
|
+
return await this.#sessions.use(options.sessionId, async () => {
|
|
398
235
|
const runtime = await this.ensureSession(options);
|
|
399
236
|
return await runtime.configure(options);
|
|
400
|
-
}
|
|
401
|
-
finally {
|
|
402
|
-
this.#endSessionUse(options.sessionId);
|
|
403
|
-
}
|
|
237
|
+
});
|
|
404
238
|
};
|
|
405
239
|
cancelPrompt = async (sessionId) => {
|
|
406
|
-
|
|
407
|
-
if (existing !== undefined) {
|
|
408
|
-
await existing;
|
|
409
|
-
return;
|
|
410
|
-
}
|
|
411
|
-
const cancellation = (async () => {
|
|
240
|
+
await this.#executions.cancel(sessionId, async () => {
|
|
412
241
|
const runtime = this.#sessions.get(sessionId);
|
|
413
242
|
if (runtime !== undefined) {
|
|
414
243
|
try {
|
|
415
244
|
await runtime.cancel();
|
|
416
245
|
}
|
|
417
246
|
finally {
|
|
418
|
-
if (this.#sessions.
|
|
419
|
-
this.#sessions.delete(sessionId);
|
|
247
|
+
if (this.#sessions.detach(sessionId, runtime)) {
|
|
420
248
|
this.#processes.releaseClosed(runtime);
|
|
421
249
|
}
|
|
422
250
|
}
|
|
423
251
|
}
|
|
424
|
-
})
|
|
425
|
-
this.#cancellationRequests.set(sessionId, cancellation);
|
|
426
|
-
await cancellation;
|
|
252
|
+
});
|
|
427
253
|
};
|
|
428
254
|
steerPrompt = async (options) => {
|
|
429
|
-
if (this.#
|
|
255
|
+
if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
|
|
430
256
|
throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
|
|
431
257
|
}
|
|
432
|
-
const pending = this.#
|
|
258
|
+
const pending = this.#sessions.pending(options.sessionId);
|
|
433
259
|
if (pending !== undefined) {
|
|
434
260
|
await pending;
|
|
435
261
|
}
|
|
436
|
-
if (this.#
|
|
262
|
+
if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
|
|
437
263
|
throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
|
|
438
264
|
}
|
|
439
265
|
const runtime = this.#sessions.get(options.sessionId);
|
|
@@ -465,28 +291,27 @@ export class AcpSessionManager {
|
|
|
465
291
|
this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
|
|
466
292
|
throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
|
|
467
293
|
}
|
|
468
|
-
|
|
469
|
-
throw new AcpExecutionError("Another prompt is already running in this conversation", {}, "session_busy");
|
|
470
|
-
}
|
|
471
|
-
this.#activeCommandIds.set(options.sessionId, options.commandId);
|
|
294
|
+
this.#executions.begin(options.sessionId, options.commandId);
|
|
472
295
|
try {
|
|
473
|
-
if (this.#
|
|
296
|
+
if (this.#executions.cancellation(options.sessionId) !== undefined) {
|
|
474
297
|
return { stopReason: "cancelled" };
|
|
475
298
|
}
|
|
476
299
|
const runtime = await this.ensureSession(options);
|
|
477
300
|
if (this.#nativeOperations.has(`${options.agent.id}:${runtime.nativeSessionId()}`)) {
|
|
478
301
|
throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
|
|
479
302
|
}
|
|
480
|
-
if (this.#
|
|
303
|
+
if (this.#executions.cancellation(options.sessionId) !== undefined) {
|
|
481
304
|
return { stopReason: "cancelled" };
|
|
482
305
|
}
|
|
483
306
|
try {
|
|
484
307
|
const result = await runtime.prompt(options);
|
|
485
|
-
return this.#
|
|
308
|
+
return this.#executions.cancellation(options.sessionId) !== undefined
|
|
309
|
+
? { stopReason: "cancelled" }
|
|
310
|
+
: result;
|
|
486
311
|
}
|
|
487
312
|
catch (error) {
|
|
488
|
-
if (this.#
|
|
489
|
-
await this.#
|
|
313
|
+
if (this.#executions.cancellation(options.sessionId) !== undefined) {
|
|
314
|
+
await this.#executions.cancellation(options.sessionId);
|
|
490
315
|
return { stopReason: "cancelled" };
|
|
491
316
|
}
|
|
492
317
|
throw error;
|
|
@@ -497,7 +322,7 @@ export class AcpSessionManager {
|
|
|
497
322
|
if (failedRuntime !== undefined) {
|
|
498
323
|
await this.#retireSessionProcess(options.sessionId, failedRuntime, async () => await failedRuntime.close());
|
|
499
324
|
}
|
|
500
|
-
if (this.#
|
|
325
|
+
if (this.#executions.cancellation(options.sessionId) !== undefined) {
|
|
501
326
|
return { stopReason: "cancelled" };
|
|
502
327
|
}
|
|
503
328
|
if (error instanceof AcpExecutionError) {
|
|
@@ -509,14 +334,11 @@ export class AcpSessionManager {
|
|
|
509
334
|
}
|
|
510
335
|
finally {
|
|
511
336
|
try {
|
|
512
|
-
await this.#
|
|
337
|
+
await this.#executions.cancellation(options.sessionId);
|
|
513
338
|
}
|
|
514
339
|
finally {
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
}
|
|
518
|
-
this.#cancellationRequests.delete(options.sessionId);
|
|
519
|
-
this.#touchSession(options.sessionId);
|
|
340
|
+
this.#executions.finish(options.sessionId, options.commandId);
|
|
341
|
+
this.#sessions.touch(options.sessionId);
|
|
520
342
|
this.#processes.notifyChanged();
|
|
521
343
|
}
|
|
522
344
|
}
|
|
@@ -537,10 +359,9 @@ export class AcpSessionManager {
|
|
|
537
359
|
};
|
|
538
360
|
close = async () => {
|
|
539
361
|
const sessions = [...this.#sessions.entries()];
|
|
540
|
-
const nativeBrowserSessions = [...this.#nativeBrowserSessions.entries()];
|
|
541
362
|
await Promise.all([
|
|
542
363
|
...sessions.map(async ([sessionId, runtime]) => await this.#retireSessionProcess(sessionId, runtime, async () => await runtime.close())),
|
|
543
|
-
|
|
364
|
+
this.#browsers.close(),
|
|
544
365
|
]);
|
|
545
366
|
};
|
|
546
367
|
findSessions = (sessionIds) => sessionIds.flatMap((sessionId) => {
|
|
@@ -552,34 +373,34 @@ export class AcpSessionManager {
|
|
|
552
373
|
createNativeSession = async (options) => {
|
|
553
374
|
const temporarySessionId = `new-${crypto.randomUUID()}`;
|
|
554
375
|
const temporaryOptions = { ...options, sessionId: temporarySessionId };
|
|
555
|
-
this.#
|
|
556
|
-
try {
|
|
557
|
-
const runtime = await this.#startPreparation(temporaryOptions);
|
|
558
|
-
let adopted = false;
|
|
376
|
+
return await this.#sessions.use(temporarySessionId, async () => {
|
|
559
377
|
try {
|
|
560
|
-
const
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
378
|
+
const runtime = await this.#startPreparation(temporaryOptions);
|
|
379
|
+
let adopted = false;
|
|
380
|
+
try {
|
|
381
|
+
const nativeSessionId = runtime.nativeSessionId();
|
|
382
|
+
if (!isNativeChatSessionId(options.agent.id, nativeSessionId) ||
|
|
383
|
+
this.#sessions.get(nativeSessionId) !== undefined ||
|
|
384
|
+
!(this.#store.read(nativeSessionId) instanceof MissingAcpStoredSession)) {
|
|
385
|
+
throw new AcpExecutionError("The Agent returned an invalid new Session ID", {}, "invalid_native_session_response");
|
|
386
|
+
}
|
|
387
|
+
runtime.adoptNativeSessionId(nativeSessionId);
|
|
388
|
+
this.#sessions.detach(temporarySessionId, runtime);
|
|
389
|
+
this.#sessions.retain(nativeSessionId, runtime);
|
|
390
|
+
this.#processes.retain(runtime, this.#retention(nativeSessionId));
|
|
391
|
+
adopted = true;
|
|
392
|
+
return nativeSessionId;
|
|
393
|
+
}
|
|
394
|
+
finally {
|
|
395
|
+
if (!adopted) {
|
|
396
|
+
await this.#retireSessionProcess(temporarySessionId, runtime, async () => await runtime.closeProcess());
|
|
397
|
+
}
|
|
565
398
|
}
|
|
566
|
-
runtime.adoptNativeSessionId(nativeSessionId);
|
|
567
|
-
this.#sessions.delete(temporarySessionId);
|
|
568
|
-
this.#sessions.set(nativeSessionId, runtime);
|
|
569
|
-
this.#processes.retain(runtime, this.#retention(nativeSessionId, "execution"));
|
|
570
|
-
adopted = true;
|
|
571
|
-
return nativeSessionId;
|
|
572
399
|
}
|
|
573
400
|
finally {
|
|
574
|
-
|
|
575
|
-
await this.#retireSessionProcess(temporarySessionId, runtime, async () => await runtime.closeProcess());
|
|
576
|
-
}
|
|
401
|
+
this.#store.remove(temporarySessionId);
|
|
577
402
|
}
|
|
578
|
-
}
|
|
579
|
-
finally {
|
|
580
|
-
this.#store.remove(temporarySessionId);
|
|
581
|
-
this.#endSessionUse(temporarySessionId);
|
|
582
|
-
}
|
|
403
|
+
});
|
|
583
404
|
};
|
|
584
405
|
ensureSession = async (options) => {
|
|
585
406
|
const existing = this.#sessions.get(options.sessionId);
|
|
@@ -587,7 +408,7 @@ export class AcpSessionManager {
|
|
|
587
408
|
if (!existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
|
|
588
409
|
throw new AcpExecutionError("The agent or workspace cannot change after a conversation has started", {}, "session_identity_mismatch");
|
|
589
410
|
}
|
|
590
|
-
this.#
|
|
411
|
+
this.#sessions.touch(options.sessionId, existing);
|
|
591
412
|
return existing;
|
|
592
413
|
}
|
|
593
414
|
await this.prepareSession(options);
|
|
@@ -597,52 +418,27 @@ export class AcpSessionManager {
|
|
|
597
418
|
}
|
|
598
419
|
return started;
|
|
599
420
|
};
|
|
600
|
-
async #ensureNativeSessionBrowser(options) {
|
|
601
|
-
const existing = this.#nativeBrowserSessions.get(options.sessionId);
|
|
602
|
-
if (existing !== undefined) {
|
|
603
|
-
if (!existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
|
|
604
|
-
throw new AcpExecutionError("The agent or workspace cannot change after a conversation has started", {}, "session_identity_mismatch");
|
|
605
|
-
}
|
|
606
|
-
this.#touchExpectedNativeBrowserSession(options.sessionId, existing);
|
|
607
|
-
return existing;
|
|
608
|
-
}
|
|
609
|
-
await this.prepareNativeSessionBrowser(options);
|
|
610
|
-
const started = this.#nativeBrowserSessions.get(options.sessionId);
|
|
611
|
-
if (started === undefined) {
|
|
612
|
-
throw new AcpExecutionError("ACP native session browser did not start");
|
|
613
|
-
}
|
|
614
|
-
return started;
|
|
615
|
-
}
|
|
616
421
|
listNativeSessions = async (options, cursor) => {
|
|
617
|
-
this.#
|
|
618
|
-
try {
|
|
619
|
-
return await (await this.#ensureNativeSessionBrowser(options)).listNativeSessions(cursor, options.signal);
|
|
620
|
-
}
|
|
621
|
-
finally {
|
|
622
|
-
this.#endNativeBrowserUse(options.sessionId);
|
|
623
|
-
}
|
|
422
|
+
return await this.#browsers.use(options, async (runtime) => await runtime.listNativeSessions(cursor, options.signal));
|
|
624
423
|
};
|
|
625
424
|
previewNativeSession = async (options, nativeSessionId) => {
|
|
626
425
|
const key = `${options.agent.id}:${nativeSessionId}`;
|
|
627
426
|
const linked = this.#store.findNative(options.agent.id, nativeSessionId);
|
|
628
427
|
if (this.#nativeOperations.has(key) ||
|
|
629
|
-
linked.some((entry) => this.#
|
|
428
|
+
linked.some((entry) => this.#executions.command(entry.sessionId) !== undefined ||
|
|
429
|
+
this.#sessions.pending(entry.sessionId) !== undefined)) {
|
|
630
430
|
throw new AcpExecutionError("Wait for the native session's running operation to finish", {}, "session_busy");
|
|
631
431
|
}
|
|
632
432
|
this.#nativeOperations.add(key);
|
|
633
433
|
try {
|
|
634
|
-
return await this.#
|
|
635
|
-
runtime.selectNativeSession(nativeSessionId);
|
|
636
|
-
return await runtime.loadHistory(options.signal);
|
|
637
|
-
}, async (runtime) => await runtime.closeProcess());
|
|
434
|
+
return await this.#browsers.preview(options, nativeSessionId);
|
|
638
435
|
}
|
|
639
436
|
finally {
|
|
640
437
|
this.#nativeOperations.delete(key);
|
|
641
438
|
}
|
|
642
439
|
};
|
|
643
440
|
loadSessionHistory = async (options) => {
|
|
644
|
-
this.#
|
|
645
|
-
try {
|
|
441
|
+
return await this.#sessions.use(options.sessionId, async () => {
|
|
646
442
|
const runtime = await this.ensureSession(options);
|
|
647
443
|
const stored = this.#store.read(options.sessionId);
|
|
648
444
|
if (stored instanceof FoundAcpStoredSession && this.#nativeHistoryReader !== null) {
|
|
@@ -652,10 +448,7 @@ export class AcpSessionManager {
|
|
|
652
448
|
}
|
|
653
449
|
}
|
|
654
450
|
return await runtime.loadHistory(options.signal);
|
|
655
|
-
}
|
|
656
|
-
finally {
|
|
657
|
-
this.#endSessionUse(options.sessionId);
|
|
658
|
-
}
|
|
451
|
+
});
|
|
659
452
|
};
|
|
660
453
|
openNativeSession = async (options, origin) => {
|
|
661
454
|
const key = `${options.agent.id}:${origin.nativeSessionId}`;
|
|
@@ -663,18 +456,18 @@ export class AcpSessionManager {
|
|
|
663
456
|
throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
|
|
664
457
|
}
|
|
665
458
|
const linked = this.#store.findNative(options.agent.id, origin.nativeSessionId);
|
|
666
|
-
if (linked.some((entry) => this.#
|
|
459
|
+
if (linked.some((entry) => this.#executions.command(entry.sessionId) !== undefined ||
|
|
460
|
+
this.#sessions.pending(entry.sessionId) !== undefined)) {
|
|
667
461
|
throw new AcpExecutionError("Wait for the native session's running operation to finish", {}, "session_busy");
|
|
668
462
|
}
|
|
669
463
|
const existing = linked[0];
|
|
670
464
|
if (origin.kind === "import" && existing !== undefined) {
|
|
671
|
-
this.#
|
|
672
|
-
try {
|
|
465
|
+
return await this.#sessions.use(existing.sessionId, async () => {
|
|
673
466
|
const runtime = await this.ensureSession({
|
|
674
467
|
...options,
|
|
675
468
|
sessionId: existing.sessionId,
|
|
676
469
|
});
|
|
677
|
-
if (this.#
|
|
470
|
+
if (this.#executions.command(existing.sessionId) !== undefined || this.#nativeOperations.has(key)) {
|
|
678
471
|
throw new AcpExecutionError("Wait for the native session's running operation to finish", {}, "session_busy");
|
|
679
472
|
}
|
|
680
473
|
this.#nativeOperations.add(key);
|
|
@@ -688,55 +481,51 @@ export class AcpSessionManager {
|
|
|
688
481
|
this.#nativeOperations.delete(key);
|
|
689
482
|
this.#processes.notifyChanged();
|
|
690
483
|
}
|
|
691
|
-
}
|
|
692
|
-
finally {
|
|
693
|
-
this.#endSessionUse(existing.sessionId);
|
|
694
|
-
}
|
|
484
|
+
});
|
|
695
485
|
}
|
|
696
|
-
if (this.#sessions.
|
|
697
|
-
this.#
|
|
486
|
+
if (this.#sessions.get(options.sessionId) !== undefined ||
|
|
487
|
+
this.#sessions.pending(options.sessionId) !== undefined ||
|
|
698
488
|
this.#store.read(options.sessionId) instanceof FoundAcpStoredSession ||
|
|
699
489
|
this.#store.read(options.sessionId) instanceof DeletedAcpStoredSession) {
|
|
700
490
|
throw new AcpExecutionError("The destination conversation already exists", {}, "session_identity_mismatch");
|
|
701
491
|
}
|
|
702
|
-
this.#
|
|
703
|
-
|
|
704
|
-
try {
|
|
705
|
-
const runtime = await this.#startPreparation({ ...options, origin });
|
|
706
|
-
const openedKey = `${options.agent.id}:${runtime.nativeSessionId()}`;
|
|
707
|
-
this.#nativeOperations.add(openedKey);
|
|
492
|
+
return await this.#sessions.use(options.sessionId, async () => {
|
|
493
|
+
this.#nativeOperations.add(key);
|
|
708
494
|
try {
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
495
|
+
const runtime = await this.#startPreparation({ ...options, origin });
|
|
496
|
+
const openedKey = `${options.agent.id}:${runtime.nativeSessionId()}`;
|
|
497
|
+
this.#nativeOperations.add(openedKey);
|
|
498
|
+
try {
|
|
499
|
+
return {
|
|
500
|
+
sessionId: options.sessionId,
|
|
501
|
+
prepared: runtime.prepared(),
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
finally {
|
|
505
|
+
this.#nativeOperations.delete(openedKey);
|
|
506
|
+
this.#processes.notifyChanged();
|
|
507
|
+
}
|
|
713
508
|
}
|
|
714
|
-
|
|
715
|
-
this.#
|
|
716
|
-
this.#
|
|
509
|
+
catch (error) {
|
|
510
|
+
this.#store.remove(options.sessionId);
|
|
511
|
+
const runtime = this.#sessions.get(options.sessionId);
|
|
512
|
+
if (runtime !== undefined) {
|
|
513
|
+
await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.closeProcess());
|
|
514
|
+
}
|
|
515
|
+
throw error;
|
|
717
516
|
}
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
this.#store.remove(options.sessionId);
|
|
721
|
-
const runtime = this.#sessions.get(options.sessionId);
|
|
722
|
-
if (runtime !== undefined) {
|
|
723
|
-
await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.closeProcess());
|
|
517
|
+
finally {
|
|
518
|
+
this.#nativeOperations.delete(key);
|
|
724
519
|
}
|
|
725
|
-
|
|
726
|
-
}
|
|
727
|
-
finally {
|
|
728
|
-
this.#nativeOperations.delete(key);
|
|
729
|
-
this.#endSessionUse(options.sessionId);
|
|
730
|
-
}
|
|
520
|
+
});
|
|
731
521
|
};
|
|
732
522
|
deleteNativeSession = async (options, nativeSessionId) => {
|
|
733
|
-
this.#
|
|
734
|
-
try {
|
|
735
|
-
const runtime = await this.#ensureNativeSessionBrowser(options);
|
|
523
|
+
return await this.#browsers.use(options, async (runtime) => {
|
|
736
524
|
const key = `${options.agent.id}:${nativeSessionId}`;
|
|
737
525
|
const linked = this.#store.findNative(options.agent.id, nativeSessionId);
|
|
738
526
|
if (this.#nativeOperations.has(key) ||
|
|
739
|
-
linked.some((entry) => this.#
|
|
527
|
+
linked.some((entry) => this.#executions.command(entry.sessionId) !== undefined ||
|
|
528
|
+
this.#sessions.pending(entry.sessionId) !== undefined)) {
|
|
740
529
|
throw new AcpExecutionError("Wait for the native session's running operation to finish", {}, "session_busy");
|
|
741
530
|
}
|
|
742
531
|
this.#nativeOperations.add(key);
|
|
@@ -751,7 +540,7 @@ export class AcpSessionManager {
|
|
|
751
540
|
await runtime.deleteNativeSession(nativeSessionId, options.signal);
|
|
752
541
|
const deleted = this.#store.markDeleted(options.agent.id, nativeSessionId);
|
|
753
542
|
if (runtime.nativeSessionId() === nativeSessionId) {
|
|
754
|
-
await this.#
|
|
543
|
+
await this.#browsers.retire(options.sessionId, runtime);
|
|
755
544
|
}
|
|
756
545
|
return deleted;
|
|
757
546
|
}
|
|
@@ -759,10 +548,7 @@ export class AcpSessionManager {
|
|
|
759
548
|
this.#nativeOperations.delete(key);
|
|
760
549
|
this.#processes.notifyChanged();
|
|
761
550
|
}
|
|
762
|
-
}
|
|
763
|
-
finally {
|
|
764
|
-
this.#endNativeBrowserUse(options.sessionId);
|
|
765
|
-
}
|
|
551
|
+
});
|
|
766
552
|
};
|
|
767
553
|
}
|
|
768
554
|
//# sourceMappingURL=session-manager.js.map
|