chainlesschain 0.45.64 → 0.45.66
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/package.json +1 -1
- package/src/gateways/ws/session-protocol.js +162 -105
- package/src/gateways/ws/worktree-protocol.js +134 -141
- package/src/lib/agent-core.js +173 -13
- package/src/lib/interaction-adapter.js +45 -0
- package/src/lib/plan-mode.js +3 -1
- package/src/lib/session-manager.js +45 -8
- package/src/lib/web-ui-envelope.js +94 -0
- package/src/lib/web-ui-server.js +13 -1
- package/src/lib/ws-agent-handler.js +8 -1
- package/src/lib/ws-session-manager.js +388 -20
- package/src/runtime/agent-runtime.js +140 -37
- package/src/runtime/coding-agent-contract.js +294 -0
- package/src/runtime/coding-agent-events.cjs +372 -0
- package/src/runtime/coding-agent-managed-tool-policy.cjs +294 -0
- package/src/runtime/coding-agent-policy.cjs +354 -0
- package/src/runtime/coding-agent-shell-policy.cjs +233 -0
- package/src/runtime/contracts/session-record.js +13 -0
- package/src/runtime/index.js +14 -0
- package/src/runtime/runtime-events.js +27 -0
- package/src/tools/index.js +12 -0
- package/src/tools/legacy-agent-tools.js +12 -157
package/package.json
CHANGED
|
@@ -1,9 +1,34 @@
|
|
|
1
1
|
import {
|
|
2
2
|
RUNTIME_EVENTS,
|
|
3
3
|
createRuntimeEvent,
|
|
4
|
+
createCodingAgentEvent,
|
|
5
|
+
CODING_AGENT_EVENT_TYPES,
|
|
4
6
|
} from "../../runtime/runtime-events.js";
|
|
5
7
|
import { createSessionRecord } from "../../runtime/contracts/session-record.js";
|
|
6
8
|
|
|
9
|
+
// Build a unified envelope for a solicited WS response. The bridge correlates
|
|
10
|
+
// by `requestId` (the inbound request's id) and unwraps `payload` for callers
|
|
11
|
+
// so existing flat-shape consumers keep working unchanged.
|
|
12
|
+
function envelopeResponse(type, id, payload, sessionId) {
|
|
13
|
+
return createCodingAgentEvent(type, payload || {}, {
|
|
14
|
+
requestId: id,
|
|
15
|
+
sessionId: sessionId || null,
|
|
16
|
+
source: "cli-runtime",
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function envelopeError(id, code, message, sessionId) {
|
|
21
|
+
return createCodingAgentEvent(
|
|
22
|
+
CODING_AGENT_EVENT_TYPES.ERROR,
|
|
23
|
+
{ code, message },
|
|
24
|
+
{
|
|
25
|
+
requestId: id,
|
|
26
|
+
sessionId: sessionId || null,
|
|
27
|
+
source: "cli-runtime",
|
|
28
|
+
},
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
7
32
|
async function ensureSessionHandler(server, ws, session) {
|
|
8
33
|
if (server.sessionHandlers.has(session.id)) {
|
|
9
34
|
return server.sessionHandlers.get(session.id);
|
|
@@ -35,12 +60,14 @@ async function ensureSessionHandler(server, ws, session) {
|
|
|
35
60
|
|
|
36
61
|
export async function handleSessionCreate(server, id, ws, message) {
|
|
37
62
|
if (!server.sessionManager) {
|
|
38
|
-
server._send(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
63
|
+
server._send(
|
|
64
|
+
ws,
|
|
65
|
+
envelopeError(
|
|
66
|
+
id,
|
|
67
|
+
"NO_SESSION_SUPPORT",
|
|
68
|
+
"Session support not configured on this server",
|
|
69
|
+
),
|
|
70
|
+
);
|
|
44
71
|
return;
|
|
45
72
|
}
|
|
46
73
|
|
|
@@ -51,6 +78,7 @@ export async function handleSessionCreate(server, id, ws, message) {
|
|
|
51
78
|
apiKey,
|
|
52
79
|
baseUrl,
|
|
53
80
|
projectRoot,
|
|
81
|
+
enabledToolNames,
|
|
54
82
|
hostManagedToolPolicy,
|
|
55
83
|
worktreeIsolation,
|
|
56
84
|
} = message;
|
|
@@ -63,6 +91,7 @@ export async function handleSessionCreate(server, id, ws, message) {
|
|
|
63
91
|
apiKey,
|
|
64
92
|
baseUrl,
|
|
65
93
|
projectRoot,
|
|
94
|
+
enabledToolNames,
|
|
66
95
|
hostManagedToolPolicy,
|
|
67
96
|
worktreeIsolation,
|
|
68
97
|
});
|
|
@@ -103,31 +132,30 @@ export async function handleSessionCreate(server, id, ws, message) {
|
|
|
103
132
|
),
|
|
104
133
|
);
|
|
105
134
|
|
|
106
|
-
server._send(
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
135
|
+
server._send(
|
|
136
|
+
ws,
|
|
137
|
+
envelopeResponse(
|
|
138
|
+
CODING_AGENT_EVENT_TYPES.SESSION_STARTED,
|
|
139
|
+
id,
|
|
140
|
+
{
|
|
141
|
+
sessionId,
|
|
142
|
+
sessionType: sessionType || "agent",
|
|
143
|
+
record,
|
|
144
|
+
},
|
|
145
|
+
sessionId,
|
|
146
|
+
),
|
|
147
|
+
);
|
|
113
148
|
} catch (err) {
|
|
114
|
-
server._send(ws,
|
|
115
|
-
id,
|
|
116
|
-
type: "error",
|
|
117
|
-
code: "SESSION_CREATE_FAILED",
|
|
118
|
-
message: err.message,
|
|
119
|
-
});
|
|
149
|
+
server._send(ws, envelopeError(id, "SESSION_CREATE_FAILED", err.message));
|
|
120
150
|
}
|
|
121
151
|
}
|
|
122
152
|
|
|
123
153
|
export async function handleSessionResume(server, id, ws, message) {
|
|
124
154
|
if (!server.sessionManager) {
|
|
125
|
-
server._send(
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
message: "Session support not configured",
|
|
130
|
-
});
|
|
155
|
+
server._send(
|
|
156
|
+
ws,
|
|
157
|
+
envelopeError(id, "NO_SESSION_SUPPORT", "Session support not configured"),
|
|
158
|
+
);
|
|
131
159
|
return;
|
|
132
160
|
}
|
|
133
161
|
|
|
@@ -135,12 +163,15 @@ export async function handleSessionResume(server, id, ws, message) {
|
|
|
135
163
|
const session = server.sessionManager.resumeSession(sessionId);
|
|
136
164
|
|
|
137
165
|
if (!session) {
|
|
138
|
-
server._send(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
166
|
+
server._send(
|
|
167
|
+
ws,
|
|
168
|
+
envelopeError(
|
|
169
|
+
id,
|
|
170
|
+
"SESSION_NOT_FOUND",
|
|
171
|
+
`Session not found: ${sessionId}`,
|
|
172
|
+
sessionId,
|
|
173
|
+
),
|
|
174
|
+
);
|
|
144
175
|
return;
|
|
145
176
|
}
|
|
146
177
|
|
|
@@ -173,13 +204,19 @@ export async function handleSessionResume(server, id, ws, message) {
|
|
|
173
204
|
),
|
|
174
205
|
);
|
|
175
206
|
|
|
176
|
-
server._send(
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
207
|
+
server._send(
|
|
208
|
+
ws,
|
|
209
|
+
envelopeResponse(
|
|
210
|
+
CODING_AGENT_EVENT_TYPES.SESSION_RESUMED,
|
|
211
|
+
id,
|
|
212
|
+
{
|
|
213
|
+
sessionId: session.id,
|
|
214
|
+
history,
|
|
215
|
+
record,
|
|
216
|
+
},
|
|
217
|
+
session.id,
|
|
218
|
+
),
|
|
219
|
+
);
|
|
183
220
|
}
|
|
184
221
|
|
|
185
222
|
export function handleSessionMessage(server, id, ws, message) {
|
|
@@ -187,12 +224,15 @@ export function handleSessionMessage(server, id, ws, message) {
|
|
|
187
224
|
const handler = server.sessionHandlers.get(sessionId);
|
|
188
225
|
|
|
189
226
|
if (!handler) {
|
|
190
|
-
server._send(
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
227
|
+
server._send(
|
|
228
|
+
ws,
|
|
229
|
+
envelopeError(
|
|
230
|
+
id,
|
|
231
|
+
"SESSION_NOT_FOUND",
|
|
232
|
+
`No active session handler for: ${sessionId}`,
|
|
233
|
+
sessionId,
|
|
234
|
+
),
|
|
235
|
+
);
|
|
196
236
|
return;
|
|
197
237
|
}
|
|
198
238
|
|
|
@@ -221,12 +261,10 @@ export function handleSessionMessage(server, id, ws, message) {
|
|
|
221
261
|
}
|
|
222
262
|
})
|
|
223
263
|
.catch((err) => {
|
|
224
|
-
server._send(
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
message: err.message,
|
|
229
|
-
});
|
|
264
|
+
server._send(
|
|
265
|
+
ws,
|
|
266
|
+
envelopeError(id, "MESSAGE_FAILED", err.message, sessionId),
|
|
267
|
+
);
|
|
230
268
|
});
|
|
231
269
|
}
|
|
232
270
|
|
|
@@ -234,12 +272,10 @@ export function handleSessionPolicyUpdate(server, id, ws, message) {
|
|
|
234
272
|
const { sessionId, hostManagedToolPolicy } = message;
|
|
235
273
|
|
|
236
274
|
if (!server.sessionManager) {
|
|
237
|
-
server._send(
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
message: "Session support not configured",
|
|
242
|
-
});
|
|
275
|
+
server._send(
|
|
276
|
+
ws,
|
|
277
|
+
envelopeError(id, "NO_SESSION_SUPPORT", "Session support not configured"),
|
|
278
|
+
);
|
|
243
279
|
return;
|
|
244
280
|
}
|
|
245
281
|
|
|
@@ -251,31 +287,35 @@ export function handleSessionPolicyUpdate(server, id, ws, message) {
|
|
|
251
287
|
: null;
|
|
252
288
|
|
|
253
289
|
if (!session) {
|
|
254
|
-
server._send(
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
290
|
+
server._send(
|
|
291
|
+
ws,
|
|
292
|
+
envelopeError(
|
|
293
|
+
id,
|
|
294
|
+
"SESSION_NOT_FOUND",
|
|
295
|
+
`Session not found: ${sessionId}`,
|
|
296
|
+
sessionId,
|
|
297
|
+
),
|
|
298
|
+
);
|
|
260
299
|
return;
|
|
261
300
|
}
|
|
262
301
|
|
|
263
|
-
server._send(
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
302
|
+
server._send(
|
|
303
|
+
ws,
|
|
304
|
+
envelopeResponse(
|
|
305
|
+
CODING_AGENT_EVENT_TYPES.COMMAND_RESPONSE,
|
|
306
|
+
id,
|
|
307
|
+
{ success: true, sessionId },
|
|
308
|
+
sessionId,
|
|
309
|
+
),
|
|
310
|
+
);
|
|
269
311
|
}
|
|
270
312
|
|
|
271
313
|
export function handleSessionList(server, id, ws) {
|
|
272
314
|
if (!server.sessionManager) {
|
|
273
|
-
server._send(
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
message: "Session support not configured",
|
|
278
|
-
});
|
|
315
|
+
server._send(
|
|
316
|
+
ws,
|
|
317
|
+
envelopeError(id, "NO_SESSION_SUPPORT", "Session support not configured"),
|
|
318
|
+
);
|
|
279
319
|
return;
|
|
280
320
|
}
|
|
281
321
|
|
|
@@ -290,11 +330,10 @@ export function handleSessionList(server, id, ws) {
|
|
|
290
330
|
: [],
|
|
291
331
|
}),
|
|
292
332
|
}));
|
|
293
|
-
server._send(
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
});
|
|
333
|
+
server._send(
|
|
334
|
+
ws,
|
|
335
|
+
envelopeResponse(CODING_AGENT_EVENT_TYPES.SESSION_LIST, id, { sessions }),
|
|
336
|
+
);
|
|
298
337
|
}
|
|
299
338
|
|
|
300
339
|
export function handleSessionClose(server, id, ws, message) {
|
|
@@ -330,24 +369,25 @@ export function handleSessionClose(server, id, ws, message) {
|
|
|
330
369
|
),
|
|
331
370
|
);
|
|
332
371
|
|
|
333
|
-
server._send(
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
372
|
+
server._send(
|
|
373
|
+
ws,
|
|
374
|
+
envelopeResponse(
|
|
375
|
+
CODING_AGENT_EVENT_TYPES.COMMAND_RESPONSE,
|
|
376
|
+
id,
|
|
377
|
+
{ success: true, sessionId },
|
|
378
|
+
sessionId,
|
|
379
|
+
),
|
|
380
|
+
);
|
|
339
381
|
}
|
|
340
382
|
|
|
341
383
|
export function handleSessionAnswer(server, id, ws, message) {
|
|
342
384
|
const { sessionId, requestId, answer } = message;
|
|
343
385
|
|
|
344
386
|
if (!server.sessionManager) {
|
|
345
|
-
server._send(
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
message: "Session support not configured",
|
|
350
|
-
});
|
|
387
|
+
server._send(
|
|
388
|
+
ws,
|
|
389
|
+
envelopeError(id, "NO_SESSION_SUPPORT", "Session support not configured"),
|
|
390
|
+
);
|
|
351
391
|
return;
|
|
352
392
|
}
|
|
353
393
|
|
|
@@ -356,30 +396,39 @@ export function handleSessionAnswer(server, id, ws, message) {
|
|
|
356
396
|
session.interaction.resolveAnswer(requestId, answer);
|
|
357
397
|
}
|
|
358
398
|
|
|
359
|
-
server._send(
|
|
399
|
+
server._send(
|
|
400
|
+
ws,
|
|
401
|
+
envelopeResponse(
|
|
402
|
+
CODING_AGENT_EVENT_TYPES.COMMAND_RESPONSE,
|
|
403
|
+
id,
|
|
404
|
+
{ success: true },
|
|
405
|
+
sessionId,
|
|
406
|
+
),
|
|
407
|
+
);
|
|
360
408
|
}
|
|
361
409
|
|
|
362
410
|
export function handleHostToolResult(server, id, ws, message) {
|
|
363
411
|
const { sessionId, requestId, success, result, error, toolName } = message;
|
|
364
412
|
|
|
365
413
|
if (!server.sessionManager) {
|
|
366
|
-
server._send(
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
message: "Session support not configured",
|
|
371
|
-
});
|
|
414
|
+
server._send(
|
|
415
|
+
ws,
|
|
416
|
+
envelopeError(id, "NO_SESSION_SUPPORT", "Session support not configured"),
|
|
417
|
+
);
|
|
372
418
|
return;
|
|
373
419
|
}
|
|
374
420
|
|
|
375
421
|
const session = server.sessionManager.getSession(sessionId);
|
|
376
422
|
if (!session || !session.interaction) {
|
|
377
|
-
server._send(
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
423
|
+
server._send(
|
|
424
|
+
ws,
|
|
425
|
+
envelopeError(
|
|
426
|
+
id,
|
|
427
|
+
"SESSION_NOT_FOUND",
|
|
428
|
+
`Session not found: ${sessionId}`,
|
|
429
|
+
sessionId,
|
|
430
|
+
),
|
|
431
|
+
);
|
|
383
432
|
return;
|
|
384
433
|
}
|
|
385
434
|
|
|
@@ -392,5 +441,13 @@ export function handleHostToolResult(server, id, ws, message) {
|
|
|
392
441
|
});
|
|
393
442
|
}
|
|
394
443
|
|
|
395
|
-
server._send(
|
|
444
|
+
server._send(
|
|
445
|
+
ws,
|
|
446
|
+
envelopeResponse(
|
|
447
|
+
CODING_AGENT_EVENT_TYPES.COMMAND_RESPONSE,
|
|
448
|
+
id,
|
|
449
|
+
{ success: true },
|
|
450
|
+
sessionId,
|
|
451
|
+
),
|
|
452
|
+
);
|
|
396
453
|
}
|