@agentshouse/kit 0.2.1-c3895fcff473b → 0.2.1-c39de3ed26c1b
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 +26 -26
- package/dist/acp.js +16 -93
- package/dist/agents.js +85 -35
- package/dist/api.js +34 -2
- package/dist/app-git.js +64 -0
- package/dist/bootstrap.json +4 -4
- package/dist/bridge.js +33 -51
- package/dist/chatgpt.js +25 -0
- package/dist/clis.js +117 -70
- package/dist/codex-main.js +58 -0
- package/dist/conversations.js +285 -236
- package/dist/copies.js +148 -115
- package/dist/end.js +22 -0
- package/dist/enrol-main.js +7 -1
- package/dist/events.js +521 -0
- package/dist/files.js +21 -1
- package/dist/git.js +11 -8
- package/dist/home.js +41 -3
- package/dist/house-main.js +151 -52
- package/dist/instructions.js +7 -5
- package/dist/kit-main.js +54 -6
- package/dist/login.js +95 -104
- package/dist/logout.js +16 -13
- package/dist/parts.js +82 -0
- package/dist/placement.js +48 -0
- package/dist/refusals.js +12 -0
- package/dist/relay.js +12 -2
- package/dist/resident.js +38 -27
- package/dist/scope.js +203 -0
- package/dist/secret-input.js +4 -1
- package/dist/service-main.js +9 -0
- package/dist/service.js +33 -0
- package/dist/shell.js +3 -0
- package/dist/sign-in.js +34 -9
- package/dist/stream.js +35 -5
- package/dist/transcription.js +54 -6
- package/dist/update.js +29 -6
- package/dist/wire.js +6 -0
- package/dist/work.js +348 -0
- package/package.json +2 -2
- package/dist/blocks.js +0 -51
package/dist/conversations.js
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
import { client, } from '@agentclientprotocol/sdk';
|
|
2
|
-
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
3
3
|
import { mkdir } from 'node:fs/promises';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
5
|
import { unlessAborted } from './abort.js';
|
|
6
|
-
import {
|
|
7
|
-
import { blocksOf, firstChange } from './blocks.js';
|
|
6
|
+
import { startAdapter } from './acp.js';
|
|
8
7
|
import { openBridge } from './bridge.js';
|
|
9
8
|
import { CLIS } from './clis.js';
|
|
10
|
-
import {
|
|
9
|
+
import { readerFor } from './events.js';
|
|
10
|
+
import { placeFiles, uploadBytes } from './files.js';
|
|
11
11
|
import { agentBase } from './home.js';
|
|
12
12
|
import { instructions } from './instructions.js';
|
|
13
|
+
import { Parts } from './parts.js';
|
|
14
|
+
import { killMarked, Scope } from './scope.js';
|
|
13
15
|
import { holdSecretInput } from './secret-input.js';
|
|
14
16
|
import { createHowWeWork } from './skills.js';
|
|
15
17
|
import { transcribe } from './transcription.js';
|
|
18
|
+
import { workFor } from './work.js';
|
|
16
19
|
const KILLED = 'the conversation was killed';
|
|
20
|
+
export const CONVERSATION_INPUTS = new Set(['open', 'message', 'interrupt', 'kill', 'option', 'answer']);
|
|
17
21
|
function causeOf(error) {
|
|
18
22
|
return error instanceof Error ? error.message : String(error);
|
|
19
23
|
}
|
|
@@ -23,33 +27,35 @@ function answerMessage(request, response) {
|
|
|
23
27
|
function logged(error) {
|
|
24
28
|
process.stderr.write(`kit: ${causeOf(error)}\n`);
|
|
25
29
|
}
|
|
26
|
-
function
|
|
27
|
-
|
|
30
|
+
function marker(bridge) {
|
|
31
|
+
return `HOUSE_BRIDGE=${bridge.env.HOUSE_BRIDGE}`;
|
|
28
32
|
}
|
|
29
33
|
class Turn {
|
|
30
34
|
id = randomUUID();
|
|
31
|
-
|
|
32
|
-
segment = null;
|
|
35
|
+
parts;
|
|
33
36
|
admitted = false;
|
|
34
|
-
notes = [];
|
|
35
|
-
sent = [];
|
|
36
|
-
unsentFrom = null;
|
|
37
|
-
draftSequence = 0;
|
|
38
|
-
planSequence = 0;
|
|
39
|
-
entries = null;
|
|
40
37
|
questions = 0;
|
|
41
38
|
secrets = 0;
|
|
42
|
-
|
|
39
|
+
prompted = false;
|
|
43
40
|
ended = false;
|
|
41
|
+
images = 0;
|
|
42
|
+
uploads = [];
|
|
43
|
+
constructor(emit) {
|
|
44
|
+
this.parts = new Parts(emit);
|
|
45
|
+
}
|
|
44
46
|
}
|
|
45
47
|
class Conversation {
|
|
46
48
|
id;
|
|
47
49
|
running = null;
|
|
50
|
+
session = null;
|
|
48
51
|
turn = null;
|
|
49
52
|
commands = null;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
+
context = null;
|
|
54
|
+
leftovers = new Set();
|
|
55
|
+
waiting = [];
|
|
56
|
+
sending = null;
|
|
57
|
+
subagents = new Map();
|
|
58
|
+
held = [];
|
|
53
59
|
opening = null;
|
|
54
60
|
kills = 0;
|
|
55
61
|
killed = new AbortController();
|
|
@@ -77,8 +83,11 @@ export class Conversations {
|
|
|
77
83
|
questions = new Map();
|
|
78
84
|
carrying = 0;
|
|
79
85
|
reporting = 0;
|
|
86
|
+
watched = false;
|
|
80
87
|
constructor(kit) {
|
|
81
88
|
this.kit = kit;
|
|
89
|
+
// A process or a durable schedule ends without a report, so the Kit looks at both again every two seconds.
|
|
90
|
+
setInterval(() => this.look(), 2_000);
|
|
82
91
|
}
|
|
83
92
|
input(input) {
|
|
84
93
|
this.carrying++;
|
|
@@ -88,14 +97,17 @@ export class Conversations {
|
|
|
88
97
|
if (input.kind === 'kill') {
|
|
89
98
|
conversation.kills++;
|
|
90
99
|
conversation.killed.abort(new Error(KILLED));
|
|
100
|
+
for (const waiting of conversation.waiting.splice(0))
|
|
101
|
+
waiting.settle({ refused: KILLED });
|
|
91
102
|
this.halt(conversation);
|
|
92
103
|
}
|
|
93
104
|
if (input.kind === 'interrupt') {
|
|
94
105
|
conversation.interrupts++;
|
|
95
106
|
conversation.interrupted.abort();
|
|
96
107
|
}
|
|
97
|
-
const
|
|
98
|
-
conversation.queue =
|
|
108
|
+
const handled = conversation.queue.then(() => this.carry(conversation, input));
|
|
109
|
+
conversation.queue = handled.catch(() => undefined);
|
|
110
|
+
const work = handled.then(({ ack }) => ack);
|
|
99
111
|
this.acks.set(input.input_id, work);
|
|
100
112
|
return work;
|
|
101
113
|
})();
|
|
@@ -113,15 +125,39 @@ export class Conversations {
|
|
|
113
125
|
for (const conversation of this.conversations.values()) {
|
|
114
126
|
const turn = conversation.turn;
|
|
115
127
|
const asking = turn !== null && turn.questions > 0 && turn.secrets === 0;
|
|
116
|
-
if (conversation.
|
|
128
|
+
if (conversation.open > (asking ? 1 : 0))
|
|
117
129
|
return false;
|
|
118
130
|
}
|
|
119
|
-
|
|
131
|
+
const conversations = [...this.conversations.values()];
|
|
132
|
+
if (conversations.some((conversation) => conversation.running?.work.busy()))
|
|
133
|
+
return false;
|
|
134
|
+
return !conversations.some((conversation) => conversation.running?.scope.working() || [...conversation.leftovers].some((scope) => scope.working()));
|
|
135
|
+
}
|
|
136
|
+
quiet() {
|
|
137
|
+
return this.idle() && [...this.conversations.values()].every((conversation) => conversation.open === 0);
|
|
138
|
+
}
|
|
139
|
+
look() {
|
|
140
|
+
for (const conversation of this.conversations.values()) {
|
|
141
|
+
for (const scope of conversation.leftovers) {
|
|
142
|
+
if (!scope.empty())
|
|
143
|
+
continue;
|
|
144
|
+
scope.close();
|
|
145
|
+
conversation.leftovers.delete(scope);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
this.kit.changed();
|
|
120
149
|
}
|
|
121
150
|
opened() {
|
|
151
|
+
this.watched = false;
|
|
152
|
+
}
|
|
153
|
+
watching(watched) {
|
|
154
|
+
const resend = watched && !this.watched;
|
|
155
|
+
this.watched = watched;
|
|
156
|
+
if (!resend)
|
|
157
|
+
return;
|
|
122
158
|
for (const conversation of this.conversations.values()) {
|
|
123
159
|
if (conversation.turn !== null)
|
|
124
|
-
conversation.turn
|
|
160
|
+
this.replay(conversation, conversation.turn);
|
|
125
161
|
}
|
|
126
162
|
}
|
|
127
163
|
conversation(id) {
|
|
@@ -134,7 +170,7 @@ export class Conversations {
|
|
|
134
170
|
}
|
|
135
171
|
async carry(conversation, input) {
|
|
136
172
|
if (input.kind === 'open')
|
|
137
|
-
return this.open(conversation, input);
|
|
173
|
+
return { ack: Promise.resolve(await this.open(conversation, input)) };
|
|
138
174
|
if (input.kind === 'message')
|
|
139
175
|
return this.message(conversation, input);
|
|
140
176
|
if (input.kind === 'interrupt') {
|
|
@@ -144,6 +180,7 @@ export class Conversations {
|
|
|
144
180
|
}
|
|
145
181
|
if (input.kind === 'kill') {
|
|
146
182
|
await this.kill(conversation);
|
|
183
|
+
await conversation.sending;
|
|
147
184
|
if (--conversation.kills === 0)
|
|
148
185
|
conversation.killed = new AbortController();
|
|
149
186
|
}
|
|
@@ -152,11 +189,12 @@ export class Conversations {
|
|
|
152
189
|
if (input.kind === 'answer') {
|
|
153
190
|
const question = this.questions.get(String(input.interaction_id));
|
|
154
191
|
if (question === undefined) {
|
|
155
|
-
|
|
192
|
+
const text = answerMessage(input.request, input.response);
|
|
193
|
+
return this.message(conversation, { ...input, text, files: [], first: false });
|
|
156
194
|
}
|
|
157
195
|
question.answer(input.response);
|
|
158
196
|
}
|
|
159
|
-
return {};
|
|
197
|
+
return { ack: Promise.resolve({}) };
|
|
160
198
|
}
|
|
161
199
|
async open(conversation, input) {
|
|
162
200
|
if (conversation.running !== null)
|
|
@@ -169,37 +207,69 @@ export class Conversations {
|
|
|
169
207
|
}
|
|
170
208
|
}
|
|
171
209
|
async message(conversation, input) {
|
|
172
|
-
|
|
173
|
-
|
|
210
|
+
const content = [{ type: 'text', text: String(input.text) }];
|
|
211
|
+
let route;
|
|
174
212
|
try {
|
|
175
|
-
|
|
213
|
+
route = await this.route(conversation, input.agent_id);
|
|
176
214
|
const files = input.files;
|
|
177
215
|
const paths = await placeFiles(this.kit.house, route.working_directory, files, conversation.killed.signal);
|
|
178
216
|
const lines = await this.transcribed(conversation, files, paths);
|
|
217
|
+
if (lines.length > 0)
|
|
218
|
+
content.push({ type: 'text', text: lines.join('\n') });
|
|
219
|
+
if (conversation.kills > 0)
|
|
220
|
+
throw new Error(KILLED);
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
return { ack: Promise.resolve({ refused: conversation.kills > 0 ? KILLED : causeOf(error) }) };
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
ack: new Promise((settle) => {
|
|
227
|
+
conversation.waiting.push({ input, route, content, settle });
|
|
228
|
+
this.next(conversation);
|
|
229
|
+
}),
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
next(conversation) {
|
|
233
|
+
if (conversation.sending !== null || conversation.open > 0)
|
|
234
|
+
return;
|
|
235
|
+
const waiting = conversation.waiting.shift();
|
|
236
|
+
if (waiting === undefined)
|
|
237
|
+
return;
|
|
238
|
+
conversation.sending = this.send(conversation, waiting).finally(() => {
|
|
239
|
+
conversation.sending = null;
|
|
240
|
+
this.next(conversation);
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
async send(conversation, waiting) {
|
|
244
|
+
const { input } = waiting;
|
|
245
|
+
let ack = {};
|
|
246
|
+
const prompt = [];
|
|
247
|
+
try {
|
|
179
248
|
if (conversation.running === null) {
|
|
180
|
-
const
|
|
181
|
-
|
|
249
|
+
const resumed = input.provider_session_id ?? conversation.session;
|
|
250
|
+
const opened = await this.start(conversation, waiting.route, resumed);
|
|
251
|
+
if (resumed === null)
|
|
182
252
|
ack = { provider_session_id: opened };
|
|
183
253
|
}
|
|
184
254
|
if (input.first === true) {
|
|
185
255
|
prompt.push({
|
|
186
256
|
type: 'text',
|
|
187
|
-
text: await instructions(conversation.running.bridge, route.base_instructions, await this.kit.copies.mapping()),
|
|
257
|
+
text: await instructions(conversation.running.bridge, waiting.route.base_instructions, await this.kit.copies.mapping()),
|
|
188
258
|
});
|
|
189
259
|
}
|
|
190
|
-
prompt.push({ type: 'text', text: String(input.text) });
|
|
191
|
-
if (lines.length > 0)
|
|
192
|
-
prompt.push({ type: 'text', text: lines.join('\n') });
|
|
193
260
|
if (conversation.kills > 0)
|
|
194
261
|
throw new Error(KILLED);
|
|
195
262
|
}
|
|
196
263
|
catch (error) {
|
|
197
|
-
if ('provider_session_id' in ack)
|
|
264
|
+
if ('provider_session_id' in ack) {
|
|
265
|
+
conversation.session = null;
|
|
198
266
|
await this.kill(conversation);
|
|
199
|
-
|
|
267
|
+
}
|
|
268
|
+
waiting.settle({ refused: conversation.kills > 0 ? KILLED : causeOf(error) });
|
|
269
|
+
return;
|
|
200
270
|
}
|
|
201
|
-
this.prompt(conversation, prompt);
|
|
202
|
-
|
|
271
|
+
this.prompt(conversation, [...prompt, ...waiting.content]);
|
|
272
|
+
waiting.settle(ack);
|
|
203
273
|
}
|
|
204
274
|
async transcribed(conversation, files, paths) {
|
|
205
275
|
const lines = [];
|
|
@@ -209,7 +279,7 @@ export class Conversations {
|
|
|
209
279
|
continue;
|
|
210
280
|
try {
|
|
211
281
|
const text = await transcribe(path, AbortSignal.any([conversation.killed.signal, conversation.interrupted.signal]));
|
|
212
|
-
if (text !== '')
|
|
282
|
+
if (text !== null && text !== '')
|
|
213
283
|
lines.push(`Transcript: ${text}`);
|
|
214
284
|
}
|
|
215
285
|
catch (error) {
|
|
@@ -229,11 +299,13 @@ export class Conversations {
|
|
|
229
299
|
}
|
|
230
300
|
halt(conversation) {
|
|
231
301
|
if (conversation.opening !== null)
|
|
232
|
-
|
|
302
|
+
killMarked(marker(conversation.opening));
|
|
303
|
+
for (const scope of conversation.leftovers)
|
|
304
|
+
scope.kill();
|
|
233
305
|
if (conversation.running === null)
|
|
234
306
|
return;
|
|
235
307
|
conversation.running.killed = true;
|
|
236
|
-
|
|
308
|
+
conversation.running.scope.kill();
|
|
237
309
|
}
|
|
238
310
|
async kill(conversation) {
|
|
239
311
|
const running = conversation.running;
|
|
@@ -245,6 +317,7 @@ export class Conversations {
|
|
|
245
317
|
if (running === null)
|
|
246
318
|
return;
|
|
247
319
|
try {
|
|
320
|
+
running.reader.requested(option, value);
|
|
248
321
|
const answered = await running.adapter.connection.agent.request('session/set_config_option', {
|
|
249
322
|
sessionId: running.sessionId,
|
|
250
323
|
configId: option,
|
|
@@ -253,6 +326,7 @@ export class Conversations {
|
|
|
253
326
|
this.options(conversation, answered.configOptions);
|
|
254
327
|
}
|
|
255
328
|
catch (error) {
|
|
329
|
+
running.reader.refused(option);
|
|
256
330
|
logged(error);
|
|
257
331
|
}
|
|
258
332
|
}
|
|
@@ -270,10 +344,9 @@ export class Conversations {
|
|
|
270
344
|
return route;
|
|
271
345
|
}
|
|
272
346
|
async start(conversation, route, sessionId) {
|
|
347
|
+
const reader = readerFor(route.kind);
|
|
348
|
+
reader.session = sessionId;
|
|
273
349
|
const app = client({ name: '@agentshouse/kit' })
|
|
274
|
-
.onNotification('session/update', ({ params }) => this.update(conversation, params))
|
|
275
|
-
.onNotification(TURN_STARTED, (params) => params, () => void this.started(conversation))
|
|
276
|
-
.onNotification(TURN_ENDED, (params) => params, () => this.ended(conversation))
|
|
277
350
|
.onRequest('session/request_permission', ({ params, signal }) => this.ask(conversation, 'session/request_permission', params, false, signal))
|
|
278
351
|
.onRequest('elicitation/create', ({ params, signal }) => this.ask(conversation, 'elicitation/create', params, asksSecret(params), signal));
|
|
279
352
|
const cli = await unlessAborted(this.kit.agents.cli(route.kind), conversation.killed.signal);
|
|
@@ -283,27 +356,32 @@ export class Conversations {
|
|
|
283
356
|
throw new Error(KILLED);
|
|
284
357
|
}
|
|
285
358
|
conversation.opening = bridge;
|
|
359
|
+
const work = workFor(route.kind, route.working_directory, () => this.kit.changed());
|
|
360
|
+
const scope = new Scope(marker(bridge), randomUUID(), cli);
|
|
286
361
|
try {
|
|
287
362
|
await unlessAborted(createHowWeWork(bridge), conversation.killed.signal);
|
|
288
|
-
await CLIS[route.kind].allowHouse();
|
|
289
|
-
const adapter = await startAdapter(route.kind, cli, route.working_directory, app, (
|
|
363
|
+
await CLIS[route.kind].allowHouse(bridge.env.PATH);
|
|
364
|
+
const adapter = await startAdapter(route.kind, cli, route.working_directory, app, (message) => {
|
|
365
|
+
work.read(message);
|
|
366
|
+
this.read(conversation, reader, message);
|
|
367
|
+
}, { ...bridge.env, ...(await work.open(cli)) }, undefined, scope);
|
|
290
368
|
void adapter.exited.then(() => bridge.close());
|
|
291
369
|
const agent = adapter.connection.agent;
|
|
292
370
|
const cwd = route.working_directory;
|
|
293
|
-
const _meta = CLIS[route.kind].sessionMeta;
|
|
371
|
+
const _meta = CLIS[route.kind].sessionMeta(bridge.env.HOUSE_BRIDGE, conversation.id);
|
|
294
372
|
const opened = sessionId === null
|
|
295
373
|
? await agent.request('session/new', { cwd, mcpServers: [], _meta })
|
|
296
374
|
: { ...(await agent.request('session/resume', { sessionId, cwd, mcpServers: [], _meta })), sessionId };
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
sessionId: opened.sessionId,
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
phase: CLIS[route.kind].phase,
|
|
304
|
-
};
|
|
375
|
+
reader.session = opened.sessionId;
|
|
376
|
+
if (sessionId === null && CLIS[route.kind].renames) {
|
|
377
|
+
const prompt = [{ type: 'text', text: `/rename ${conversation.id}` }];
|
|
378
|
+
await agent.request('session/prompt', { sessionId: opened.sessionId, prompt });
|
|
379
|
+
}
|
|
380
|
+
const running = { adapter, bridge, sessionId: opened.sessionId, killed: false, reader, work, scope };
|
|
305
381
|
const options = await this.launchSettings(running, route, opened.configOptions ?? []);
|
|
382
|
+
void work.settled().then(() => scope.record());
|
|
306
383
|
conversation.running = running;
|
|
384
|
+
conversation.session = running.sessionId;
|
|
307
385
|
this.kit.send({ type: 'process', conversation_id: conversation.id, running: true });
|
|
308
386
|
if (conversation.commands !== null)
|
|
309
387
|
this.commands(conversation, conversation.commands);
|
|
@@ -312,7 +390,9 @@ export class Conversations {
|
|
|
312
390
|
return running.sessionId;
|
|
313
391
|
}
|
|
314
392
|
catch (error) {
|
|
315
|
-
|
|
393
|
+
scope.kill();
|
|
394
|
+
work.close();
|
|
395
|
+
conversation.leftovers.add(scope);
|
|
316
396
|
bridge.close();
|
|
317
397
|
throw conversation.kills > 0 ? new Error(KILLED) : error;
|
|
318
398
|
}
|
|
@@ -331,6 +411,7 @@ export class Conversations {
|
|
|
331
411
|
if (value === null)
|
|
332
412
|
continue;
|
|
333
413
|
const option = options.find((candidate) => candidate.category === category);
|
|
414
|
+
running.reader.requested(option?.id ?? category, value);
|
|
334
415
|
const answered = await running.adapter.connection.agent.request('session/set_config_option', {
|
|
335
416
|
sessionId: running.sessionId,
|
|
336
417
|
configId: option?.id ?? category,
|
|
@@ -344,19 +425,23 @@ export class Conversations {
|
|
|
344
425
|
const running = conversation.running;
|
|
345
426
|
if (running?.adapter !== adapter)
|
|
346
427
|
return;
|
|
428
|
+
this.detach(conversation, running);
|
|
429
|
+
if (conversation.turn !== null) {
|
|
430
|
+
this.end(conversation, conversation.turn, { failed: running.killed ? KILLED : cause });
|
|
431
|
+
}
|
|
432
|
+
this.next(conversation);
|
|
433
|
+
this.kit.changed();
|
|
434
|
+
}
|
|
435
|
+
detach(conversation, running) {
|
|
436
|
+
running.work.close();
|
|
437
|
+
conversation.leftovers.add(running.scope);
|
|
347
438
|
conversation.running = null;
|
|
348
439
|
conversation.commands = null;
|
|
349
|
-
conversation.jobs.clear();
|
|
350
|
-
conversation.queued.length = 0;
|
|
351
440
|
for (const [id, question] of this.questions) {
|
|
352
441
|
if (question.conversation === conversation)
|
|
353
442
|
this.questions.delete(id);
|
|
354
443
|
}
|
|
355
444
|
this.kit.send({ type: 'process', conversation_id: conversation.id, running: false });
|
|
356
|
-
if (conversation.turn !== null) {
|
|
357
|
-
this.end(conversation, conversation.turn, { failed: running.killed ? KILLED : cause });
|
|
358
|
-
}
|
|
359
|
-
this.kit.changed();
|
|
360
445
|
}
|
|
361
446
|
commands(conversation, commands) {
|
|
362
447
|
conversation.commands = commands;
|
|
@@ -371,7 +456,7 @@ export class Conversations {
|
|
|
371
456
|
}
|
|
372
457
|
deliver(conversation, turn, event, body) {
|
|
373
458
|
this.reporting++;
|
|
374
|
-
const delivered = conversation.reports.then(() => this.kit.house.deliver(`/kit/conversations/${conversation.id}/turns/${turn.id}/${event}`, body));
|
|
459
|
+
const delivered = conversation.reports.then(async () => this.kit.house.deliver(`/kit/conversations/${conversation.id}/turns/${turn.id}/${event}`, await body));
|
|
375
460
|
conversation.reports = delivered
|
|
376
461
|
.catch(() => undefined)
|
|
377
462
|
.finally(() => {
|
|
@@ -380,119 +465,82 @@ export class Conversations {
|
|
|
380
465
|
});
|
|
381
466
|
return delivered;
|
|
382
467
|
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
.then(() => this.admitted(conversation, turn))
|
|
388
|
-
.catch(logged);
|
|
389
|
-
return turn;
|
|
468
|
+
write(conversation, turn, operation) {
|
|
469
|
+
if (!turn.admitted || turn.ended || !this.watched)
|
|
470
|
+
return;
|
|
471
|
+
this.kit.send({ type: 'turn', conversation_id: conversation.id, turn_id: turn.id, ...operation });
|
|
390
472
|
}
|
|
391
|
-
|
|
392
|
-
turn.
|
|
393
|
-
|
|
394
|
-
this.kit.send(note);
|
|
395
|
-
if (turn.text !== '') {
|
|
396
|
-
turn.unsentFrom = 0;
|
|
397
|
-
this.draft(conversation, turn);
|
|
398
|
-
}
|
|
399
|
-
if (turn.entries !== null)
|
|
400
|
-
this.plan(conversation, turn);
|
|
473
|
+
replay(conversation, turn) {
|
|
474
|
+
for (const operation of turn.parts.operations())
|
|
475
|
+
this.write(conversation, turn, operation);
|
|
401
476
|
}
|
|
402
477
|
begin(conversation) {
|
|
403
|
-
const turn = this.
|
|
478
|
+
const turn = new Turn((operation) => this.write(conversation, turn, operation));
|
|
479
|
+
conversation.open++;
|
|
480
|
+
void this.deliver(conversation, turn, 'started', {})
|
|
481
|
+
.then(() => {
|
|
482
|
+
turn.admitted = true;
|
|
483
|
+
this.replay(conversation, turn);
|
|
484
|
+
})
|
|
485
|
+
.catch(logged);
|
|
404
486
|
conversation.turn = turn;
|
|
487
|
+
if (conversation.context !== null)
|
|
488
|
+
turn.parts.measure(conversation.context);
|
|
489
|
+
for (const part of conversation.held.splice(0))
|
|
490
|
+
turn.parts.start(part);
|
|
405
491
|
this.kit.changed();
|
|
406
492
|
return turn;
|
|
407
493
|
}
|
|
408
|
-
|
|
409
|
-
if (turn.ended)
|
|
410
|
-
return;
|
|
411
|
-
const segment = turn.segment;
|
|
412
|
-
turn.segment = null;
|
|
413
|
-
if (segment?.phase === 'note')
|
|
414
|
-
this.note(conversation, turn, segment.text);
|
|
415
|
-
else if (segment !== null)
|
|
416
|
-
this.answer(conversation, turn, segment.text);
|
|
417
|
-
this.end(conversation, turn, { text: turn.text });
|
|
418
|
-
}
|
|
419
|
-
end(conversation, turn, outcome) {
|
|
494
|
+
end(conversation, turn, failed) {
|
|
420
495
|
if (turn.ended)
|
|
421
496
|
return;
|
|
497
|
+
if (conversation.turn === turn) {
|
|
498
|
+
for (const event of conversation.running?.reader.closeTurn() ?? [])
|
|
499
|
+
this.apply(conversation, turn, event);
|
|
500
|
+
}
|
|
422
501
|
turn.ended = true;
|
|
423
502
|
conversation.open--;
|
|
424
503
|
if (conversation.turn === turn)
|
|
425
504
|
conversation.turn = null;
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
505
|
+
const body = Promise.allSettled(turn.uploads).then(() => ({
|
|
506
|
+
parts: turn.parts.saved(),
|
|
507
|
+
context: turn.parts.context,
|
|
508
|
+
...failed,
|
|
509
|
+
}));
|
|
510
|
+
void this.deliver(conversation, turn, 'ended', body).catch(logged);
|
|
511
|
+
this.next(conversation);
|
|
430
512
|
this.kit.changed();
|
|
431
513
|
}
|
|
432
|
-
failed(conversation, cause) {
|
|
433
|
-
if (conversation.open > 0)
|
|
434
|
-
conversation.late.push(cause);
|
|
435
|
-
else
|
|
436
|
-
this.end(conversation, this.report(conversation), { failed: cause });
|
|
437
|
-
}
|
|
438
514
|
prompt(conversation, prompt) {
|
|
439
515
|
const running = conversation.running;
|
|
440
|
-
const
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
this.end(conversation,
|
|
516
|
+
const turn = this.begin(conversation);
|
|
517
|
+
turn.prompted = true;
|
|
518
|
+
running.adapter.connection.agent.request('session/prompt', { sessionId: running.sessionId, prompt }).then(() => this.end(conversation, turn, null), async (error) => {
|
|
519
|
+
if (running.adapter.connection.signal.aborted) {
|
|
520
|
+
this.end(conversation, turn, { failed: running.killed ? KILLED : await running.adapter.exited });
|
|
521
|
+
}
|
|
522
|
+
else {
|
|
523
|
+
if (conversation.open === 1 && conversation.running === running) {
|
|
524
|
+
running.scope.kill();
|
|
525
|
+
this.detach(conversation, running);
|
|
526
|
+
}
|
|
527
|
+
this.end(conversation, turn, { failed: causeOf(error) });
|
|
452
528
|
}
|
|
453
529
|
});
|
|
454
530
|
}
|
|
455
|
-
attach(turn, sent) {
|
|
456
|
-
turn.prompt = sent;
|
|
457
|
-
sent.turn = turn;
|
|
458
|
-
}
|
|
459
|
-
answered(conversation, sent, failed) {
|
|
460
|
-
const turn = sent.turn;
|
|
461
|
-
if (turn === null) {
|
|
462
|
-
const queued = conversation.queued.indexOf(sent);
|
|
463
|
-
if (queued < 0)
|
|
464
|
-
return;
|
|
465
|
-
conversation.queued.splice(queued, 1);
|
|
466
|
-
if (failed !== null)
|
|
467
|
-
this.failed(conversation, failed);
|
|
468
|
-
}
|
|
469
|
-
else if (failed !== null) {
|
|
470
|
-
if (turn.ended)
|
|
471
|
-
this.failed(conversation, failed);
|
|
472
|
-
else
|
|
473
|
-
this.end(conversation, turn, { failed });
|
|
474
|
-
}
|
|
475
|
-
else if (turn.prompt === sent) {
|
|
476
|
-
this.finish(conversation, turn);
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
531
|
started(conversation) {
|
|
480
|
-
|
|
481
|
-
return conversation.turn;
|
|
482
|
-
const turn = this.begin(conversation);
|
|
483
|
-
const queued = conversation.queued.shift();
|
|
484
|
-
if (queued !== undefined)
|
|
485
|
-
this.attach(turn, queued);
|
|
486
|
-
return turn;
|
|
532
|
+
return conversation.turn ?? this.begin(conversation);
|
|
487
533
|
}
|
|
488
534
|
ended(conversation) {
|
|
489
535
|
const turn = conversation.turn;
|
|
490
536
|
if (turn === null)
|
|
491
537
|
return;
|
|
492
|
-
if (turn.
|
|
493
|
-
this.
|
|
538
|
+
if (!turn.prompted) {
|
|
539
|
+
this.end(conversation, turn, null);
|
|
494
540
|
}
|
|
495
541
|
else {
|
|
542
|
+
for (const event of conversation.running?.reader.closeTurn() ?? [])
|
|
543
|
+
this.apply(conversation, turn, event);
|
|
496
544
|
conversation.turn = null;
|
|
497
545
|
this.kit.changed();
|
|
498
546
|
}
|
|
@@ -541,101 +589,102 @@ export class Conversations {
|
|
|
541
589
|
if (content !== null)
|
|
542
590
|
this.questions.get(interactionId)?.answer({ action: 'accept', content });
|
|
543
591
|
}
|
|
544
|
-
|
|
545
|
-
const
|
|
546
|
-
|
|
547
|
-
this.chunk(conversation, this.started(conversation), update, update.content.text);
|
|
548
|
-
}
|
|
549
|
-
else if (update.sessionUpdate === 'plan') {
|
|
550
|
-
const turn = this.started(conversation);
|
|
551
|
-
this.close(conversation, turn);
|
|
552
|
-
turn.entries = update.entries;
|
|
553
|
-
this.plan(conversation, turn);
|
|
554
|
-
}
|
|
555
|
-
else if (update.sessionUpdate === 'tool_call') {
|
|
556
|
-
this.close(conversation, this.started(conversation));
|
|
557
|
-
}
|
|
558
|
-
else if (update.sessionUpdate === 'agent_thought_chunk') {
|
|
559
|
-
const turn = this.started(conversation);
|
|
560
|
-
if (turn.segment?.messageId !== (update.messageId ?? null))
|
|
561
|
-
this.close(conversation, turn);
|
|
562
|
-
}
|
|
563
|
-
else if (update.sessionUpdate === 'available_commands_update') {
|
|
564
|
-
this.commands(conversation, update.availableCommands);
|
|
565
|
-
}
|
|
566
|
-
else if (update.sessionUpdate === 'config_option_update') {
|
|
567
|
-
this.options(conversation, update.configOptions);
|
|
568
|
-
}
|
|
592
|
+
read(conversation, reader, message) {
|
|
593
|
+
for (const event of reader.read(message))
|
|
594
|
+
this.event(conversation, event);
|
|
569
595
|
}
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
596
|
+
event(conversation, event) {
|
|
597
|
+
switch (event.kind) {
|
|
598
|
+
case 'started':
|
|
599
|
+
this.started(conversation);
|
|
600
|
+
return;
|
|
601
|
+
case 'ended':
|
|
602
|
+
this.ended(conversation);
|
|
603
|
+
return;
|
|
604
|
+
case 'commands':
|
|
605
|
+
this.commands(conversation, event.commands);
|
|
606
|
+
return;
|
|
607
|
+
case 'options':
|
|
608
|
+
this.options(conversation, event.options);
|
|
609
|
+
return;
|
|
610
|
+
case 'context':
|
|
611
|
+
conversation.context = { used: event.used, window: event.window };
|
|
612
|
+
conversation.turn?.parts.measure(conversation.context);
|
|
613
|
+
return;
|
|
614
|
+
case 'subagent-done':
|
|
615
|
+
this.subagentDone(conversation, event.id, event.result);
|
|
616
|
+
return;
|
|
617
|
+
case 'marker':
|
|
618
|
+
case 'image':
|
|
619
|
+
if (conversation.turn !== null)
|
|
620
|
+
break;
|
|
621
|
+
if (event.kind === 'marker' && event.marker === 'job')
|
|
622
|
+
conversation.held.push({ type: 'marker', marker: 'job', text: event.text });
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
this.apply(conversation, this.started(conversation), event);
|
|
576
626
|
}
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
627
|
+
apply(conversation, turn, event) {
|
|
628
|
+
switch (event.kind) {
|
|
629
|
+
case 'text':
|
|
630
|
+
case 'reasoning':
|
|
631
|
+
turn.parts.write(event.kind, event.id, event.text);
|
|
632
|
+
return;
|
|
633
|
+
case 'command':
|
|
634
|
+
turn.parts.command();
|
|
635
|
+
return;
|
|
636
|
+
case 'subagent': {
|
|
637
|
+
const index = turn.parts.start({ type: 'subagent', description: event.description, status: 'running', result: null });
|
|
638
|
+
conversation.subagents.set(event.id, { description: event.description, turn, index });
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
case 'message':
|
|
642
|
+
turn.parts.start({ type: 'message', command: event.command, target: event.target });
|
|
643
|
+
return;
|
|
644
|
+
case 'marker':
|
|
645
|
+
turn.parts.start({ type: 'marker', marker: event.marker, text: event.text });
|
|
646
|
+
return;
|
|
647
|
+
case 'plan':
|
|
648
|
+
turn.parts.plan(event.entries);
|
|
649
|
+
return;
|
|
650
|
+
case 'image':
|
|
651
|
+
turn.uploads.push(this.image(conversation, turn, turn.parts.reserve(), event.data, event.mediaType));
|
|
652
|
+
this.kit.changed();
|
|
653
|
+
return;
|
|
585
654
|
}
|
|
586
|
-
turn.segment ??= { messageId, phase, text: '' };
|
|
587
|
-
turn.segment.text += text;
|
|
588
655
|
}
|
|
589
|
-
|
|
590
|
-
const
|
|
591
|
-
if (
|
|
656
|
+
subagentDone(conversation, id, result) {
|
|
657
|
+
const subagent = conversation.subagents.get(id);
|
|
658
|
+
if (subagent === undefined)
|
|
592
659
|
return;
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
note(conversation, turn, text) {
|
|
597
|
-
if (text.trim() === '')
|
|
660
|
+
conversation.subagents.delete(id);
|
|
661
|
+
if (subagent.turn === conversation.turn) {
|
|
662
|
+
subagent.turn.parts.done(subagent.index, result);
|
|
598
663
|
return;
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
664
|
+
}
|
|
665
|
+
const part = { type: 'subagent', description: subagent.description, status: 'done', result };
|
|
666
|
+
if (conversation.turn === null)
|
|
667
|
+
conversation.held.push(part);
|
|
602
668
|
else
|
|
603
|
-
turn.
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
});
|
|
620
|
-
if (sent) {
|
|
621
|
-
turn.draftSequence++;
|
|
622
|
-
turn.sent = blocks;
|
|
623
|
-
turn.unsentFrom = null;
|
|
669
|
+
conversation.turn.parts.start(part);
|
|
670
|
+
}
|
|
671
|
+
async image(conversation, turn, reserved, data, mediaType) {
|
|
672
|
+
const bytes = Buffer.from(data, 'base64');
|
|
673
|
+
const name = `image-${++turn.images}.${mediaType.split('/')[1]}`;
|
|
674
|
+
try {
|
|
675
|
+
const declared = (await this.deliver(conversation, turn, 'images', {
|
|
676
|
+
name,
|
|
677
|
+
media_type: mediaType,
|
|
678
|
+
bytes: bytes.length,
|
|
679
|
+
sha256: createHash('sha256').update(bytes).digest('hex'),
|
|
680
|
+
}));
|
|
681
|
+
const saved = declared.upload === undefined ? declared : await uploadBytes(declared.upload, bytes);
|
|
682
|
+
if (saved.save.status !== 'saved')
|
|
683
|
+
throw new Error(saved.save.failure ?? saved.save.status);
|
|
684
|
+
turn.parts.arrive({ type: 'image', file: saved.version, name, media_type: mediaType }, reserved);
|
|
624
685
|
}
|
|
625
|
-
|
|
626
|
-
turn.
|
|
686
|
+
catch (error) {
|
|
687
|
+
turn.parts.arrive({ type: 'marker', marker: 'retry', text: `${name} was not saved: ${causeOf(error)}` }, reserved);
|
|
627
688
|
}
|
|
628
689
|
}
|
|
629
|
-
plan(conversation, turn) {
|
|
630
|
-
const sent = this.kit.send({
|
|
631
|
-
type: 'plan',
|
|
632
|
-
conversation_id: conversation.id,
|
|
633
|
-
turn_id: turn.id,
|
|
634
|
-
sequence: turn.planSequence + 1,
|
|
635
|
-
from: 0,
|
|
636
|
-
steps: turn.entries.map((entry) => ({ label: entry.content, priority: entry.priority, status: entry.status })),
|
|
637
|
-
});
|
|
638
|
-
if (sent)
|
|
639
|
-
turn.planSequence++;
|
|
640
|
-
}
|
|
641
690
|
}
|