@agentshouse/kit 0.2.1-cb997daca91ed → 0.2.1-cba216f2c559c
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 +14 -14
- package/dist/acp.js +54 -4
- package/dist/afk.js +17 -0
- package/dist/agents.js +58 -32
- package/dist/authority-main.js +1 -1
- package/dist/bridge.js +4 -4
- package/dist/building.js +7 -3
- package/dist/calls.js +12 -4
- package/dist/chunks.js +49 -0
- package/dist/clis.js +32 -50
- package/dist/connection.js +7 -45
- package/dist/conversations.js +193 -56
- package/dist/copies.js +275 -124
- package/dist/end.js +8 -2
- package/dist/events.js +66 -8
- package/dist/files.js +2 -2
- package/dist/git.js +44 -10
- package/dist/grok.js +39 -0
- package/dist/home.js +0 -7
- package/dist/login.js +2 -0
- package/dist/logout.js +1 -1
- package/dist/mcp.js +2 -0
- package/dist/parts.js +10 -0
- package/dist/placement.js +10 -18
- package/dist/resident.js +29 -27
- package/dist/scope.js +12 -4
- package/dist/scripts.js +255 -0
- package/dist/secret-input.js +1 -1
- package/dist/silence.js +28 -0
- package/dist/update.js +105 -154
- package/dist/wire.js +23 -5
- package/package.json +2 -2
- package/dist/clis-main.js +0 -23
- package/dist/web-wizard.js +0 -101
package/dist/conversations.js
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
|
-
import { client,
|
|
1
|
+
import { client, } from '@agentclientprotocol/sdk';
|
|
2
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 { startAdapter } from './acp.js';
|
|
6
|
+
import { signedOut, startAdapter } from './acp.js';
|
|
7
|
+
import { formAnswered, grokQuestionAnswered, permissionAllowed, PLAN_APPROVED } from './afk.js';
|
|
7
8
|
import { finalRefusal, HouseRefusal } from './api.js';
|
|
8
9
|
import { openBridge } from './bridge.js';
|
|
9
10
|
import { CLIS } from './clis.js';
|
|
10
11
|
import { answeredFailure, readerFor } from './events.js';
|
|
11
12
|
import { inspectedFile, linkedPaths, storeRefusal, uploaded } from './files.js';
|
|
13
|
+
import { grokPlanAnswer, grokPlanForm, grokQuestionAnswer, grokQuestionForm } from './grok.js';
|
|
12
14
|
import { agentBase } from './home.js';
|
|
13
15
|
import { instructions } from './instructions.js';
|
|
14
|
-
import { Parts } from './parts.js';
|
|
16
|
+
import { addedTokens, Parts } from './parts.js';
|
|
15
17
|
import { refusalOf } from './refusals.js';
|
|
16
18
|
import { relayed } from './relay.js';
|
|
17
19
|
import { killMarked, Scope } from './scope.js';
|
|
18
20
|
import { holdSecretInput } from './secret-input.js';
|
|
19
21
|
import { transcribe } from './transcription.js';
|
|
20
22
|
import { workFor } from './work.js';
|
|
21
|
-
const SIGNED_OUT = RequestError.authRequired().code;
|
|
22
23
|
const KILLED = 'the conversation was killed';
|
|
23
24
|
const INTERRUPTED = 'the conversation was interrupted';
|
|
24
25
|
const REFUSED = 'the CLI refused the prompt';
|
|
@@ -34,6 +35,9 @@ const NO_SESSION = "the Agent's CLI did not open a session";
|
|
|
34
35
|
const NO_RESUME = "the Agent's CLI could not resume this conversation";
|
|
35
36
|
const NOT_SIGNED_IN = "the Agent's CLI is not signed in; sign it in on the Agent's machine";
|
|
36
37
|
const NO_SETTINGS = "the Agent's CLI refused the Agent's model, mode or effort";
|
|
38
|
+
const UNSTEERED = "the Agent's CLI did not take the message into its running turn";
|
|
39
|
+
// A reader tells a working run from a stopped one by the minute, and House keeps no finer figure, so a run reports no oftener.
|
|
40
|
+
const ACTIVITY_REPORT_MS = 60_000;
|
|
37
41
|
class NotStarted extends Error {
|
|
38
42
|
}
|
|
39
43
|
class ResumeRefused extends NotStarted {
|
|
@@ -46,9 +50,6 @@ async function step(reason, work) {
|
|
|
46
50
|
throw new NotStarted(typeof reason === 'string' ? reason : reason(error), { cause: error });
|
|
47
51
|
}
|
|
48
52
|
}
|
|
49
|
-
function signedOut(error) {
|
|
50
|
-
return typeof error === 'object' && error !== null && error.code === SIGNED_OUT;
|
|
51
|
-
}
|
|
52
53
|
function unopened(reason) {
|
|
53
54
|
return (error) => (signedOut(error) ? NOT_SIGNED_IN : reason);
|
|
54
55
|
}
|
|
@@ -57,15 +58,17 @@ const STARTING_SHOWN_AFTER_MS = 1_000;
|
|
|
57
58
|
function unbridged(error) {
|
|
58
59
|
return (finalRefusal(error) ? ENDED[refusalOf(error.text)?.code ?? ''] : undefined) ?? NO_CONNECTION;
|
|
59
60
|
}
|
|
60
|
-
const
|
|
61
|
+
const GROK_PLAN = {
|
|
61
62
|
parse(value) {
|
|
62
|
-
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
63
|
-
throw new Error('_x.ai/exit_plan_mode needs an object');
|
|
64
|
-
}
|
|
65
63
|
return value;
|
|
66
64
|
},
|
|
67
65
|
};
|
|
68
|
-
|
|
66
|
+
const GROK_QUESTION = {
|
|
67
|
+
parse(value) {
|
|
68
|
+
return value;
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
export const CONVERSATION_METHODS = new Set(['open', 'message', 'interrupt', 'kill', 'done', 'option', 'answer']);
|
|
69
72
|
function causeOf(error) {
|
|
70
73
|
return error instanceof Error ? error.message : String(error);
|
|
71
74
|
}
|
|
@@ -73,7 +76,9 @@ function answeredWith(error) {
|
|
|
73
76
|
return { failure: signedOut(error) ? 'signed_out' : 'error', cause: causeOf(error) };
|
|
74
77
|
}
|
|
75
78
|
function exitedWith(running, cause) {
|
|
76
|
-
|
|
79
|
+
if (running.killed)
|
|
80
|
+
return { failure: 'killed', cause: KILLED };
|
|
81
|
+
return { failure: running.adapter.silent() ? 'silent' : 'exited', cause };
|
|
77
82
|
}
|
|
78
83
|
function answerMessage(request, response) {
|
|
79
84
|
return `The User answered a question you asked earlier.\nQuestion: ${JSON.stringify(request)}\nAnswer: ${JSON.stringify(response)}`;
|
|
@@ -103,14 +108,19 @@ function audioOf(result) {
|
|
|
103
108
|
function marker(bridge) {
|
|
104
109
|
return `HOUSE_BRIDGE=${bridge.env.HOUSE_BRIDGE}`;
|
|
105
110
|
}
|
|
111
|
+
function steers(adapter) {
|
|
112
|
+
return adapter.initialized._meta?.steering?.supported === true;
|
|
113
|
+
}
|
|
106
114
|
class Turn {
|
|
107
115
|
id = randomUUID();
|
|
108
116
|
parts;
|
|
109
117
|
questions = 0;
|
|
110
118
|
secrets = 0;
|
|
111
119
|
prompted = false;
|
|
120
|
+
steered = false;
|
|
112
121
|
taken = null;
|
|
113
122
|
ended = false;
|
|
123
|
+
quietUntil = Infinity;
|
|
114
124
|
images = 0;
|
|
115
125
|
uploads = [];
|
|
116
126
|
constructor(emit) {
|
|
@@ -125,9 +135,12 @@ class Conversation {
|
|
|
125
135
|
commands = null;
|
|
126
136
|
options = null;
|
|
127
137
|
exitUntold = false;
|
|
138
|
+
afk = false;
|
|
128
139
|
context = null;
|
|
140
|
+
tokens = null;
|
|
129
141
|
leftovers = new Set();
|
|
130
142
|
waiting = [];
|
|
143
|
+
done = false;
|
|
131
144
|
sending = null;
|
|
132
145
|
subagents = new Map();
|
|
133
146
|
held = [];
|
|
@@ -139,7 +152,7 @@ class Conversation {
|
|
|
139
152
|
killed = new AbortController();
|
|
140
153
|
interrupts = 0;
|
|
141
154
|
interrupted = new AbortController();
|
|
142
|
-
|
|
155
|
+
turns = new Set();
|
|
143
156
|
queue = Promise.resolve();
|
|
144
157
|
reports = Promise.resolve();
|
|
145
158
|
starting = null;
|
|
@@ -173,6 +186,8 @@ export class Conversations {
|
|
|
173
186
|
call(call) {
|
|
174
187
|
void this.kit.calls.carry(call.input_id, () => {
|
|
175
188
|
const conversation = this.conversation(call.conversation_id);
|
|
189
|
+
if (call.kind === 'open' || call.kind === 'message')
|
|
190
|
+
conversation.afk = call.afk === true;
|
|
176
191
|
if (call.kind === 'kill') {
|
|
177
192
|
conversation.kills++;
|
|
178
193
|
conversation.killed.abort(new Error(KILLED));
|
|
@@ -195,7 +210,7 @@ export class Conversations {
|
|
|
195
210
|
for (const conversation of this.conversations.values()) {
|
|
196
211
|
const turn = conversation.turn;
|
|
197
212
|
const asking = turn !== null && turn.questions > 0 && turn.secrets === 0;
|
|
198
|
-
if (conversation.
|
|
213
|
+
if (conversation.turns.size > (asking ? 1 : 0))
|
|
199
214
|
return false;
|
|
200
215
|
}
|
|
201
216
|
const conversations = [...this.conversations.values()];
|
|
@@ -204,7 +219,7 @@ export class Conversations {
|
|
|
204
219
|
return !conversations.some((conversation) => conversation.running?.scope.working() || [...conversation.leftovers].some((scope) => scope.working()));
|
|
205
220
|
}
|
|
206
221
|
quiet() {
|
|
207
|
-
return this.idle() && [...this.conversations.values()].every((conversation) => conversation.
|
|
222
|
+
return this.idle() && [...this.conversations.values()].every((conversation) => conversation.turns.size === 0);
|
|
208
223
|
}
|
|
209
224
|
look() {
|
|
210
225
|
for (const conversation of this.conversations.values()) {
|
|
@@ -220,8 +235,10 @@ export class Conversations {
|
|
|
220
235
|
opened() {
|
|
221
236
|
this.watched = false;
|
|
222
237
|
for (const conversation of this.conversations.values()) {
|
|
223
|
-
if (conversation.running !== null)
|
|
238
|
+
if (conversation.running !== null) {
|
|
224
239
|
this.facts(conversation);
|
|
240
|
+
this.subagentsChanged(conversation);
|
|
241
|
+
}
|
|
225
242
|
else if (conversation.exitUntold)
|
|
226
243
|
this.process(conversation, false);
|
|
227
244
|
if (conversation.untold || conversation.told !== null)
|
|
@@ -248,9 +265,14 @@ export class Conversations {
|
|
|
248
265
|
}
|
|
249
266
|
async carry(conversation, call) {
|
|
250
267
|
if (call.kind === 'open')
|
|
251
|
-
return
|
|
268
|
+
return this.open(conversation, call);
|
|
252
269
|
if (call.kind === 'message')
|
|
253
270
|
return this.message(conversation, call);
|
|
271
|
+
if (call.kind === 'done') {
|
|
272
|
+
conversation.done = true;
|
|
273
|
+
this.next(conversation);
|
|
274
|
+
return { result: Promise.resolve({}) };
|
|
275
|
+
}
|
|
254
276
|
await this.launched(conversation);
|
|
255
277
|
if (call.kind === 'interrupt') {
|
|
256
278
|
if (--conversation.interrupts === 0)
|
|
@@ -275,11 +297,17 @@ export class Conversations {
|
|
|
275
297
|
}
|
|
276
298
|
return { result: Promise.resolve({}) };
|
|
277
299
|
}
|
|
278
|
-
open(conversation, call) {
|
|
279
|
-
|
|
280
|
-
return Promise.resolve({});
|
|
300
|
+
async open(conversation, call) {
|
|
301
|
+
await this.stop(conversation);
|
|
281
302
|
const launch = conversation.launch ?? this.launch(conversation, this.route(conversation, call.agent_id), null, true);
|
|
282
|
-
return
|
|
303
|
+
return {
|
|
304
|
+
result: launch.session.then((session) => ({ provider_session_id: session }), (error) => ({ refused: refusal(conversation, error) })),
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
async stop(conversation) {
|
|
308
|
+
const running = conversation.running;
|
|
309
|
+
running?.scope.kill();
|
|
310
|
+
await running?.adapter.exited;
|
|
283
311
|
}
|
|
284
312
|
launch(conversation, route, sessionId, empty) {
|
|
285
313
|
let opened;
|
|
@@ -318,7 +346,10 @@ export class Conversations {
|
|
|
318
346
|
return read;
|
|
319
347
|
}
|
|
320
348
|
async message(conversation, call) {
|
|
321
|
-
const content = [
|
|
349
|
+
const content = [
|
|
350
|
+
...(typeof call.recent === 'string' ? [{ type: 'text', text: call.recent }] : []),
|
|
351
|
+
{ type: 'text', text: String(call.text) },
|
|
352
|
+
];
|
|
322
353
|
let route;
|
|
323
354
|
let instructing = null;
|
|
324
355
|
try {
|
|
@@ -336,21 +367,71 @@ export class Conversations {
|
|
|
336
367
|
return { result: Promise.resolve({ refused: refusal(conversation, error) }) };
|
|
337
368
|
}
|
|
338
369
|
conversation.preparing = null;
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
370
|
+
const { promise, resolve: settle } = Promise.withResolvers();
|
|
371
|
+
const waiting = { call, route, content, instructing, settle };
|
|
372
|
+
if (call.at_once === true && conversation.turns.size > 0)
|
|
373
|
+
await this.atOnce(conversation, waiting);
|
|
374
|
+
else
|
|
375
|
+
conversation.waiting.push(waiting);
|
|
376
|
+
this.next(conversation);
|
|
377
|
+
return { result: promise };
|
|
378
|
+
}
|
|
379
|
+
async atOnce(conversation, waiting) {
|
|
380
|
+
while (conversation.sending !== null)
|
|
381
|
+
await conversation.sending;
|
|
382
|
+
const running = conversation.running;
|
|
383
|
+
if (running !== null && conversation.turns.size > 0 && steers(running.adapter)) {
|
|
384
|
+
conversation.sending = this.steer(conversation, running, waiting);
|
|
385
|
+
await conversation.sending;
|
|
386
|
+
conversation.sending = null;
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
this.ahead(conversation, waiting);
|
|
390
|
+
if (conversation.turns.size > 0)
|
|
391
|
+
await this.cancel(conversation);
|
|
392
|
+
}
|
|
393
|
+
ahead(conversation, waiting) {
|
|
394
|
+
const behind = conversation.waiting.findIndex((held) => held.call.at_once !== true);
|
|
395
|
+
conversation.waiting.splice(behind === -1 ? conversation.waiting.length : behind, 0, waiting);
|
|
396
|
+
}
|
|
397
|
+
async steer(conversation, running, waiting) {
|
|
398
|
+
let outcome;
|
|
399
|
+
try {
|
|
400
|
+
({ outcome } = await running.adapter.connection.agent.request('_session/steering', {
|
|
401
|
+
sessionId: running.sessionId,
|
|
402
|
+
prompt: waiting.content,
|
|
403
|
+
_meta: { steering: { idleBehavior: 'promptRequired' } },
|
|
404
|
+
}));
|
|
405
|
+
}
|
|
406
|
+
catch (error) {
|
|
407
|
+
outcome = error;
|
|
408
|
+
}
|
|
409
|
+
if (outcome === 'promptRequired') {
|
|
410
|
+
this.ahead(conversation, waiting);
|
|
411
|
+
}
|
|
412
|
+
else if (outcome === 'injected' || outcome === 'startedNewTurn') {
|
|
413
|
+
if (outcome === 'injected') {
|
|
414
|
+
for (const turn of conversation.turns)
|
|
415
|
+
turn.steered ||= turn.prompted;
|
|
416
|
+
}
|
|
417
|
+
this.kit.calls.keepResult(String(waiting.call.input_id), {});
|
|
418
|
+
waiting.settle({});
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
const cause = outcome instanceof Error ? outcome : new Error(`the CLI answered ${JSON.stringify(outcome)}`);
|
|
422
|
+
waiting.settle({ refused: refusal(conversation, new NotStarted(UNSTEERED, { cause })) });
|
|
423
|
+
}
|
|
345
424
|
}
|
|
346
425
|
next(conversation) {
|
|
347
|
-
if (conversation.sending !== null || conversation.
|
|
426
|
+
if (conversation.sending !== null || conversation.turns.size > 0)
|
|
348
427
|
return;
|
|
349
428
|
const waiting = conversation.waiting.shift();
|
|
350
429
|
if (waiting === undefined) {
|
|
351
430
|
const step = conversation.preparing ?? (conversation.launch === null ? null : 'session');
|
|
352
431
|
if (conversation.starting !== step)
|
|
353
432
|
this.starts(conversation, step);
|
|
433
|
+
if (conversation.done && conversation.launch === null)
|
|
434
|
+
void this.stop(conversation).catch(logged);
|
|
354
435
|
return;
|
|
355
436
|
}
|
|
356
437
|
conversation.sending = this.send(conversation, waiting).finally(() => {
|
|
@@ -455,8 +536,7 @@ export class Conversations {
|
|
|
455
536
|
throw new Error(`${path} is no longer an audio file`);
|
|
456
537
|
return address;
|
|
457
538
|
}, signal);
|
|
458
|
-
|
|
459
|
-
await step(unbridged, unlessAborted(this.kit.house.deliver(transcript, { path, text: said }, () => !signal.aborted), signal));
|
|
539
|
+
await step(unbridged, unlessAborted(this.kit.house.deliver('/kit/transcripts/keep', { conversation: conversation.id, message: messageId, path, text: said }, () => !signal.aborted), signal));
|
|
460
540
|
blocks.push({ type: 'text', text: `${path}\nTranscript: ${said}` });
|
|
461
541
|
}
|
|
462
542
|
}
|
|
@@ -491,7 +571,11 @@ export class Conversations {
|
|
|
491
571
|
void bridging.then((bridge) => bridge.close(), () => undefined);
|
|
492
572
|
}
|
|
493
573
|
async interrupt(conversation, turnId) {
|
|
494
|
-
if (conversation.
|
|
574
|
+
if ([...conversation.turns].some((turn) => turn.id === turnId))
|
|
575
|
+
await this.cancel(conversation);
|
|
576
|
+
}
|
|
577
|
+
async cancel(conversation) {
|
|
578
|
+
if (conversation.running === null)
|
|
495
579
|
return;
|
|
496
580
|
await conversation.running.adapter.connection.agent.notify('session/cancel', {
|
|
497
581
|
sessionId: conversation.running.sessionId,
|
|
@@ -548,9 +632,10 @@ export class Conversations {
|
|
|
548
632
|
const reader = readerFor(route.kind);
|
|
549
633
|
reader.session = sessionId;
|
|
550
634
|
const app = client({ name: '@agentshouse/kit' })
|
|
551
|
-
.onRequest('session/request_permission', ({ params, signal }) => this.ask(conversation, 'session/request_permission', params, false, signal))
|
|
552
|
-
.onRequest('elicitation/create', ({ params, signal }) => this.ask(conversation, 'elicitation/create', params, asksSecret(params), signal))
|
|
553
|
-
.onRequest('_x.ai/exit_plan_mode',
|
|
635
|
+
.onRequest('session/request_permission', ({ params, signal }) => conversation.afk ? permissionAllowed(params) : this.ask(conversation, 'session/request_permission', params, false, signal))
|
|
636
|
+
.onRequest('elicitation/create', ({ params, signal }) => conversation.afk ? formAnswered(route.kind, params) : this.ask(conversation, 'elicitation/create', params, asksSecret(params), signal))
|
|
637
|
+
.onRequest('_x.ai/exit_plan_mode', GROK_PLAN, async ({ params, signal }) => conversation.afk ? PLAN_APPROVED : grokPlanAnswer(await this.askForm(conversation, grokPlanForm(params), signal)))
|
|
638
|
+
.onRequest('_x.ai/ask_user_question', GROK_QUESTION, async ({ params, signal }) => conversation.afk ? grokQuestionAnswered(params) : grokQuestionAnswer(await this.askForm(conversation, grokQuestionForm(params), signal)));
|
|
554
639
|
const cli = await unlessAborted(this.kit.agents.cli(route.kind), conversation.killed.signal);
|
|
555
640
|
const bridging = this.bridged(conversation);
|
|
556
641
|
const bridge = await step(unbridged, bridging);
|
|
@@ -585,6 +670,7 @@ export class Conversations {
|
|
|
585
670
|
const options = await step(NO_SETTINGS, this.launchSettings(running, route, opened.configOptions ?? []));
|
|
586
671
|
void work.settled().then(() => scope.record());
|
|
587
672
|
conversation.running = running;
|
|
673
|
+
adapter.turn(conversation.turns.size > 0);
|
|
588
674
|
conversation.session = running.sessionId;
|
|
589
675
|
conversation.options = options;
|
|
590
676
|
this.facts(conversation);
|
|
@@ -651,6 +737,10 @@ export class Conversations {
|
|
|
651
737
|
conversation.running = null;
|
|
652
738
|
conversation.commands = null;
|
|
653
739
|
conversation.options = null;
|
|
740
|
+
if (conversation.subagents.size > 0) {
|
|
741
|
+
conversation.subagents.clear();
|
|
742
|
+
this.subagentsChanged(conversation);
|
|
743
|
+
}
|
|
654
744
|
for (const [id, question] of this.questions) {
|
|
655
745
|
if (question.conversation === conversation)
|
|
656
746
|
this.questions.delete(id);
|
|
@@ -667,6 +757,10 @@ export class Conversations {
|
|
|
667
757
|
if (conversation.options !== null)
|
|
668
758
|
this.options(conversation, conversation.options);
|
|
669
759
|
}
|
|
760
|
+
subagentsChanged(conversation) {
|
|
761
|
+
const subagents = [...conversation.subagents.values()].map(({ description, activity }) => ({ description, activity }));
|
|
762
|
+
this.kit.send({ type: 'subagents', conversation_id: conversation.id, subagents });
|
|
763
|
+
}
|
|
670
764
|
commands(conversation, commands) {
|
|
671
765
|
conversation.commands = commands;
|
|
672
766
|
if (conversation.running === null)
|
|
@@ -679,9 +773,9 @@ export class Conversations {
|
|
|
679
773
|
conversation.options = options;
|
|
680
774
|
this.kit.send({ type: 'options', conversation_id: conversation.id, options });
|
|
681
775
|
}
|
|
682
|
-
deliver(conversation, turn,
|
|
776
|
+
deliver(conversation, turn, act, body, current) {
|
|
683
777
|
this.reporting++;
|
|
684
|
-
const delivered = conversation.reports.then(async () => this.kit.house.deliver(`/kit
|
|
778
|
+
const delivered = conversation.reports.then(async () => this.kit.house.deliver(`/kit/${act}`, { conversation: conversation.id, turn: turn.id, ...(await body) }, current));
|
|
685
779
|
conversation.reports = delivered
|
|
686
780
|
.catch(() => undefined)
|
|
687
781
|
.finally(() => {
|
|
@@ -701,8 +795,14 @@ export class Conversations {
|
|
|
701
795
|
}
|
|
702
796
|
begin(conversation) {
|
|
703
797
|
const turn = new Turn((operation) => this.write(conversation, turn, operation));
|
|
704
|
-
conversation.
|
|
705
|
-
|
|
798
|
+
conversation.turns.add(turn);
|
|
799
|
+
conversation.running?.adapter.turn(true);
|
|
800
|
+
void this.deliver(conversation, turn, 'turns/start', {})
|
|
801
|
+
.then((answer) => {
|
|
802
|
+
if (answer.run === true)
|
|
803
|
+
turn.quietUntil = Date.now() + ACTIVITY_REPORT_MS;
|
|
804
|
+
})
|
|
805
|
+
.catch(logged);
|
|
706
806
|
conversation.turn = turn;
|
|
707
807
|
if (conversation.context !== null)
|
|
708
808
|
turn.parts.measure(conversation.context);
|
|
@@ -723,15 +823,19 @@ export class Conversations {
|
|
|
723
823
|
if (notice !== null)
|
|
724
824
|
turn.parts.start({ type: 'marker', marker: 'retry', text: notice });
|
|
725
825
|
turn.ended = true;
|
|
726
|
-
conversation.
|
|
826
|
+
conversation.turns.delete(turn);
|
|
827
|
+
conversation.running?.adapter.turn(conversation.turns.size > 0);
|
|
727
828
|
if (conversation.turn === turn)
|
|
728
829
|
conversation.turn = null;
|
|
830
|
+
const tokens = conversation.tokens;
|
|
831
|
+
conversation.tokens = null;
|
|
729
832
|
const body = Promise.allSettled(turn.uploads).then(() => ({
|
|
730
833
|
parts: turn.parts.saved(),
|
|
731
834
|
context: turn.parts.context,
|
|
835
|
+
...(tokens === null ? {} : { tokens }),
|
|
732
836
|
...(failed === null ? {} : { failed: failed.failure }),
|
|
733
837
|
}));
|
|
734
|
-
void this.deliver(conversation, turn, '
|
|
838
|
+
void this.deliver(conversation, turn, 'turns/end', body).catch(logged);
|
|
735
839
|
this.next(conversation);
|
|
736
840
|
this.kit.changed();
|
|
737
841
|
}
|
|
@@ -756,7 +860,7 @@ export class Conversations {
|
|
|
756
860
|
}
|
|
757
861
|
else {
|
|
758
862
|
this.took(turn);
|
|
759
|
-
if (conversation.
|
|
863
|
+
if (conversation.turns.size === 1 && conversation.turns.has(turn) && conversation.running === running) {
|
|
760
864
|
running.adapter.retire();
|
|
761
865
|
this.detach(conversation, running);
|
|
762
866
|
}
|
|
@@ -769,19 +873,25 @@ export class Conversations {
|
|
|
769
873
|
this.took(turn);
|
|
770
874
|
return turn;
|
|
771
875
|
}
|
|
772
|
-
ended(conversation) {
|
|
876
|
+
ended(conversation, stop) {
|
|
773
877
|
const turn = conversation.turn;
|
|
774
878
|
if (turn === null)
|
|
775
879
|
return;
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
this.apply(conversation, turn, event);
|
|
782
|
-
conversation.turn = null;
|
|
783
|
-
this.kit.changed();
|
|
880
|
+
const held = stop !== null && !turn.steered && [...conversation.subagents.values()].some((subagent) => subagent.turn === turn);
|
|
881
|
+
if (!turn.prompted || held) {
|
|
882
|
+
this.took(turn);
|
|
883
|
+
this.end(conversation, turn, stop === 'refusal' ? { failure: 'refused', cause: REFUSED } : null);
|
|
884
|
+
return;
|
|
784
885
|
}
|
|
886
|
+
if (stop !== null)
|
|
887
|
+
return;
|
|
888
|
+
for (const event of conversation.running?.reader.closeTurn() ?? [])
|
|
889
|
+
this.apply(conversation, turn, event);
|
|
890
|
+
conversation.turn = null;
|
|
891
|
+
this.kit.changed();
|
|
892
|
+
}
|
|
893
|
+
askForm(conversation, form, signal) {
|
|
894
|
+
return this.ask(conversation, 'elicitation/create', form, false, signal);
|
|
785
895
|
}
|
|
786
896
|
async ask(conversation, method, params, secret, signal) {
|
|
787
897
|
const turn = this.started(conversation);
|
|
@@ -789,7 +899,7 @@ export class Conversations {
|
|
|
789
899
|
const answered = new Promise((answer, fail) => {
|
|
790
900
|
this.questions.set(interactionId, { conversation, answer: (response) => answer(response) });
|
|
791
901
|
signal.addEventListener('abort', () => answer(null), { once: true });
|
|
792
|
-
this.deliver(conversation, turn, '
|
|
902
|
+
this.deliver(conversation, turn, 'conversations/interaction/ask', {
|
|
793
903
|
interaction_id: interactionId,
|
|
794
904
|
request: { method, params },
|
|
795
905
|
secret,
|
|
@@ -830,6 +940,18 @@ export class Conversations {
|
|
|
830
940
|
read(conversation, reader, message) {
|
|
831
941
|
for (const event of reader.read(message))
|
|
832
942
|
this.event(conversation, event);
|
|
943
|
+
this.reportActivity(conversation);
|
|
944
|
+
}
|
|
945
|
+
reportActivity(conversation) {
|
|
946
|
+
const turn = conversation.turn;
|
|
947
|
+
if (turn === null || Date.now() < turn.quietUntil)
|
|
948
|
+
return;
|
|
949
|
+
turn.quietUntil = Infinity;
|
|
950
|
+
void this.deliver(conversation, turn, 'turns/activity/report', {}, () => false)
|
|
951
|
+
.catch(logged)
|
|
952
|
+
.finally(() => {
|
|
953
|
+
turn.quietUntil = Date.now() + ACTIVITY_REPORT_MS;
|
|
954
|
+
});
|
|
833
955
|
}
|
|
834
956
|
event(conversation, event) {
|
|
835
957
|
switch (event.kind) {
|
|
@@ -837,7 +959,7 @@ export class Conversations {
|
|
|
837
959
|
this.started(conversation);
|
|
838
960
|
return;
|
|
839
961
|
case 'ended':
|
|
840
|
-
this.ended(conversation);
|
|
962
|
+
this.ended(conversation, event.stop);
|
|
841
963
|
return;
|
|
842
964
|
case 'commands':
|
|
843
965
|
this.commands(conversation, event.commands);
|
|
@@ -849,9 +971,15 @@ export class Conversations {
|
|
|
849
971
|
conversation.context = { used: event.used, window: event.window };
|
|
850
972
|
conversation.turn?.parts.measure(conversation.context);
|
|
851
973
|
return;
|
|
974
|
+
case 'tokens':
|
|
975
|
+
conversation.tokens = addedTokens(conversation.tokens, event.tokens);
|
|
976
|
+
return;
|
|
852
977
|
case 'subagent-done':
|
|
853
978
|
this.subagentDone(conversation, event.id, event.result);
|
|
854
979
|
return;
|
|
980
|
+
case 'subagent-activity':
|
|
981
|
+
this.subagentActivity(conversation, event.id, event.activity);
|
|
982
|
+
return;
|
|
855
983
|
case 'marker':
|
|
856
984
|
case 'image':
|
|
857
985
|
if (conversation.turn !== null)
|
|
@@ -878,7 +1006,8 @@ export class Conversations {
|
|
|
878
1006
|
return;
|
|
879
1007
|
case 'subagent': {
|
|
880
1008
|
const index = turn.parts.start({ type: 'subagent', description: event.description, status: 'running', result: null });
|
|
881
|
-
conversation.subagents.set(event.id, { description: event.description, turn, index });
|
|
1009
|
+
conversation.subagents.set(event.id, { description: event.description, turn, index, activity: null });
|
|
1010
|
+
this.subagentsChanged(conversation);
|
|
882
1011
|
return;
|
|
883
1012
|
}
|
|
884
1013
|
case 'message':
|
|
@@ -901,6 +1030,7 @@ export class Conversations {
|
|
|
901
1030
|
if (subagent === undefined)
|
|
902
1031
|
return;
|
|
903
1032
|
conversation.subagents.delete(id);
|
|
1033
|
+
this.subagentsChanged(conversation);
|
|
904
1034
|
if (subagent.turn === conversation.turn) {
|
|
905
1035
|
subagent.turn.parts.done(subagent.index, result);
|
|
906
1036
|
return;
|
|
@@ -911,12 +1041,19 @@ export class Conversations {
|
|
|
911
1041
|
else
|
|
912
1042
|
conversation.turn.parts.start(part);
|
|
913
1043
|
}
|
|
1044
|
+
subagentActivity(conversation, id, activity) {
|
|
1045
|
+
const subagent = conversation.subagents.get(id);
|
|
1046
|
+
if (subagent === undefined || subagent.activity === activity)
|
|
1047
|
+
return;
|
|
1048
|
+
subagent.activity = activity;
|
|
1049
|
+
this.subagentsChanged(conversation);
|
|
1050
|
+
}
|
|
914
1051
|
async image(conversation, turn, reserved, data, mediaType) {
|
|
915
1052
|
const bytes = Buffer.from(data, 'base64');
|
|
916
1053
|
const name = `image-${++turn.images}.${mediaType.split('/')[1]}`;
|
|
917
1054
|
const sha256 = createHash('sha256').update(bytes).digest('hex');
|
|
918
1055
|
try {
|
|
919
|
-
const declared = (await this.deliver(conversation, turn, 'images', {
|
|
1056
|
+
const declared = (await this.deliver(conversation, turn, 'turns/images/declare', {
|
|
920
1057
|
name,
|
|
921
1058
|
media_type: mediaType,
|
|
922
1059
|
bytes: bytes.length,
|