@agentshouse/kit 0.2.1-cc719fffeec71 → 0.2.1-cc813e0274de8
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 +28 -28
- package/dist/acp.js +59 -8
- package/dist/afk.js +17 -0
- package/dist/agents.js +59 -33
- package/dist/api.js +5 -5
- package/dist/authority-main.js +3 -3
- package/dist/bridge.js +5 -5
- package/dist/building.js +50 -0
- package/dist/calls.js +71 -0
- package/dist/chunks.js +49 -0
- package/dist/clis.js +46 -40
- package/dist/{stream.js → connection.js} +31 -49
- package/dist/conversations.js +471 -150
- package/dist/copies.js +290 -153
- package/dist/end.js +14 -4
- package/dist/events.js +76 -8
- package/dist/files.js +139 -37
- package/dist/git.js +47 -13
- package/dist/grok.js +39 -0
- package/dist/home.js +0 -7
- package/dist/house-cli.js +95 -87
- package/dist/house-main.js +3 -3
- package/dist/instructions.js +29 -9
- package/dist/kit-cli.js +4 -4
- package/dist/kit-main.js +3 -3
- package/dist/login.js +4 -4
- package/dist/logout.js +1 -1
- package/dist/mcp.js +6 -2
- package/dist/parts.js +10 -0
- package/dist/placement.js +21 -20
- package/dist/relay.js +21 -0
- package/dist/resident.js +51 -40
- package/dist/scope.js +15 -4
- package/dist/scripts.js +255 -0
- package/dist/secret-input.js +1 -1
- package/dist/service.js +1 -1
- package/dist/sign-in.js +22 -21
- package/dist/silence.js +28 -0
- package/dist/skills.js +42 -97
- package/dist/transcription.js +88 -44
- package/dist/translate.js +2 -8
- package/dist/update.js +113 -124
- package/dist/wire.js +37 -3
- package/package.json +20 -21
- package/dist/bootstrap.json +0 -24
- package/dist/clis-main.js +0 -23
- package/dist/inputs.js +0 -37
- package/how-we-work.md +0 -33
- package/skills/ask-matt/LICENSE +0 -21
- package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
- package/skills/ask-matt/SKILL.md +0 -97
- package/skills/ask-matt/agents/openai.yaml +0 -5
- package/skills/code-review/LICENSE +0 -21
- package/skills/code-review/SKILL.md +0 -89
- package/skills/code-review/agents/openai.yaml +0 -3
- package/skills/codebase-design/DEEPENING.md +0 -37
- package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
- package/skills/codebase-design/LICENSE +0 -21
- package/skills/codebase-design/SKILL.md +0 -114
- package/skills/codebase-design/agents/openai.yaml +0 -3
- package/skills/diagnosing-bugs/LICENSE +0 -21
- package/skills/diagnosing-bugs/SKILL.md +0 -138
- package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
- package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
- package/skills/domain-modeling/ADR-FORMAT.md +0 -47
- package/skills/domain-modeling/GLOSSARY-FORMAT.md +0 -60
- package/skills/domain-modeling/LICENSE +0 -21
- package/skills/domain-modeling/SKILL.md +0 -74
- package/skills/domain-modeling/agents/openai.yaml +0 -3
- package/skills/grill-me/LICENSE +0 -21
- package/skills/grill-me/SKILL.md +0 -7
- package/skills/grill-me/agents/openai.yaml +0 -5
- package/skills/grill-with-docs/LICENSE +0 -21
- package/skills/grill-with-docs/SKILL.md +0 -7
- package/skills/grill-with-docs/agents/openai.yaml +0 -5
- package/skills/grilling/LICENSE +0 -21
- package/skills/grilling/SKILL.md +0 -30
- package/skills/grilling/agents/openai.yaml +0 -3
- package/skills/handoff/LICENSE +0 -21
- package/skills/handoff/SKILL.md +0 -16
- package/skills/handoff/agents/openai.yaml +0 -5
- package/skills/implement/LICENSE +0 -21
- package/skills/implement/SKILL.md +0 -17
- package/skills/implement/agents/openai.yaml +0 -5
- package/skills/implement-spec/LICENSE +0 -21
- package/skills/implement-spec/SKILL.md +0 -40
- package/skills/implement-spec/agents/openai.yaml +0 -5
- package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
- package/skills/improve-codebase-architecture/LICENSE +0 -21
- package/skills/improve-codebase-architecture/SKILL.md +0 -71
- package/skills/improve-codebase-architecture/agents/openai.yaml +0 -5
- package/skills/pr/CREDITS.md +0 -3
- package/skills/pr/LICENSE +0 -21
- package/skills/pr/SKILL.md +0 -170
- package/skills/pr/agents/openai.yaml +0 -3
- package/skills/prototype/LICENSE +0 -21
- package/skills/prototype/LOGIC.md +0 -67
- package/skills/prototype/SKILL.md +0 -26
- package/skills/prototype/UI.md +0 -112
- package/skills/prototype/agents/openai.yaml +0 -3
- package/skills/research/LICENSE +0 -21
- package/skills/research/SKILL.md +0 -12
- package/skills/research/agents/openai.yaml +0 -3
- package/skills/retro/LICENSE +0 -21
- package/skills/retro/SKILL.md +0 -44
- package/skills/retro/agents/openai.yaml +0 -5
- package/skills/tdd/LICENSE +0 -21
- package/skills/tdd/SKILL.md +0 -38
- package/skills/tdd/agents/openai.yaml +0 -3
- package/skills/tdd/mocking.md +0 -59
- package/skills/tdd/tests.md +0 -77
- package/skills/teach/GLOSSARY-FORMAT.md +0 -35
- package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
- package/skills/teach/LICENSE +0 -21
- package/skills/teach/MISSION-FORMAT.md +0 -31
- package/skills/teach/RESOURCES-FORMAT.md +0 -32
- package/skills/teach/SKILL.md +0 -140
- package/skills/teach/agents/openai.yaml +0 -5
- package/skills/to-questionnaire/LICENSE +0 -21
- package/skills/to-questionnaire/SKILL.md +0 -54
- package/skills/to-questionnaire/agents/openai.yaml +0 -5
- package/skills/to-spec/LICENSE +0 -21
- package/skills/to-spec/SKILL.md +0 -75
- package/skills/to-spec/agents/openai.yaml +0 -5
- package/skills/to-tickets/LICENSE +0 -21
- package/skills/to-tickets/SKILL.md +0 -105
- package/skills/to-tickets/agents/openai.yaml +0 -5
- package/skills/triage/AGENT-BRIEF.md +0 -207
- package/skills/triage/LICENSE +0 -21
- package/skills/triage/OUT-OF-SCOPE.md +0 -105
- package/skills/triage/SKILL.md +0 -112
- package/skills/triage/agents/openai.yaml +0 -5
- package/skills/wait-what/LICENSE +0 -21
- package/skills/wait-what/SKILL.md +0 -7
- package/skills/wait-what/agents/openai.yaml +0 -5
- package/skills/wayfinder/LICENSE +0 -21
- package/skills/wayfinder/SKILL.md +0 -128
- package/skills/wayfinder/agents/openai.yaml +0 -5
- package/skills/wizard/LICENSE +0 -21
- package/skills/wizard/SKILL.md +0 -44
- package/skills/wizard/agents/openai.yaml +0 -3
- package/skills/wizard/template.sh +0 -223
- package/skills/writing-for-agents/LICENSE +0 -21
- package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
- package/skills/writing-for-agents/SKILL.md +0 -81
- package/skills/writing-for-agents/agents/openai.yaml +0 -3
package/dist/conversations.js
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
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';
|
|
7
|
-
import {
|
|
6
|
+
import { signedOut, startAdapter } from './acp.js';
|
|
7
|
+
import { formAnswered, grokQuestionAnswered, permissionAllowed, PLAN_APPROVED } from './afk.js';
|
|
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
|
-
import {
|
|
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';
|
|
18
|
+
import { relayed } from './relay.js';
|
|
16
19
|
import { killMarked, Scope } from './scope.js';
|
|
17
20
|
import { holdSecretInput } from './secret-input.js';
|
|
18
|
-
import { createHowWeWork } from './skills.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';
|
|
24
|
+
const INTERRUPTED = 'the conversation was interrupted';
|
|
23
25
|
const REFUSED = 'the CLI refused the prompt';
|
|
24
26
|
const NO_CONNECTION = "the Agent's computer could not open this conversation; try again";
|
|
25
27
|
const GONE = 'House no longer holds this conversation; start a new one';
|
|
@@ -31,9 +33,15 @@ const ENDED = {
|
|
|
31
33
|
const NO_START = "the Agent's CLI did not start";
|
|
32
34
|
const NO_SESSION = "the Agent's CLI did not open a session";
|
|
33
35
|
const NO_RESUME = "the Agent's CLI could not resume this conversation";
|
|
36
|
+
const NOT_SIGNED_IN = "the Agent's CLI is not signed in; sign it in on the Agent's machine";
|
|
34
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;
|
|
35
41
|
class NotStarted extends Error {
|
|
36
42
|
}
|
|
43
|
+
class ResumeRefused extends NotStarted {
|
|
44
|
+
}
|
|
37
45
|
async function step(reason, work) {
|
|
38
46
|
try {
|
|
39
47
|
return await work;
|
|
@@ -42,52 +50,77 @@ async function step(reason, work) {
|
|
|
42
50
|
throw new NotStarted(typeof reason === 'string' ? reason : reason(error), { cause: error });
|
|
43
51
|
}
|
|
44
52
|
}
|
|
53
|
+
function unopened(reason) {
|
|
54
|
+
return (error) => (signedOut(error) ? NOT_SIGNED_IN : reason);
|
|
55
|
+
}
|
|
56
|
+
// A start-up step is shown only once it has lasted a second, so a quick start shows none of them.
|
|
57
|
+
const STARTING_SHOWN_AFTER_MS = 1_000;
|
|
45
58
|
function unbridged(error) {
|
|
46
59
|
return (finalRefusal(error) ? ENDED[refusalOf(error.text)?.code ?? ''] : undefined) ?? NO_CONNECTION;
|
|
47
60
|
}
|
|
48
|
-
const
|
|
61
|
+
const GROK_PLAN = {
|
|
62
|
+
parse(value) {
|
|
63
|
+
return value;
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
const GROK_QUESTION = {
|
|
49
67
|
parse(value) {
|
|
50
|
-
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
51
|
-
throw new Error('_x.ai/exit_plan_mode needs an object');
|
|
52
|
-
}
|
|
53
68
|
return value;
|
|
54
69
|
},
|
|
55
70
|
};
|
|
56
|
-
export const
|
|
71
|
+
export const CONVERSATION_METHODS = new Set(['open', 'message', 'interrupt', 'kill', 'done', 'option', 'answer']);
|
|
57
72
|
function causeOf(error) {
|
|
58
73
|
return error instanceof Error ? error.message : String(error);
|
|
59
74
|
}
|
|
60
75
|
function answeredWith(error) {
|
|
61
|
-
|
|
62
|
-
return { failure: signedOut ? 'signed_out' : 'error', cause: causeOf(error) };
|
|
76
|
+
return { failure: signedOut(error) ? 'signed_out' : 'error', cause: causeOf(error) };
|
|
63
77
|
}
|
|
64
78
|
function exitedWith(running, cause) {
|
|
65
|
-
|
|
79
|
+
if (running.killed)
|
|
80
|
+
return { failure: 'killed', cause: KILLED };
|
|
81
|
+
return { failure: running.adapter.silent() ? 'silent' : 'exited', cause };
|
|
66
82
|
}
|
|
67
83
|
function answerMessage(request, response) {
|
|
68
84
|
return `The User answered a question you asked earlier.\nQuestion: ${JSON.stringify(request)}\nAnswer: ${JSON.stringify(response)}`;
|
|
69
85
|
}
|
|
86
|
+
async function sentPart(url, bytes, headers) {
|
|
87
|
+
const answer = await fetch(relayed(url), { method: 'PUT', headers, body: bytes });
|
|
88
|
+
await answer.arrayBuffer();
|
|
89
|
+
if (!answer.ok)
|
|
90
|
+
throw storeRefusal('upload', answer.status);
|
|
91
|
+
}
|
|
70
92
|
function logged(error) {
|
|
71
93
|
process.stderr.write(`kit: ${causeOf(error)}\n`);
|
|
72
94
|
}
|
|
73
95
|
function refusal(conversation, error) {
|
|
74
96
|
if (conversation.kills > 0)
|
|
75
97
|
return KILLED;
|
|
76
|
-
const cause = error instanceof
|
|
98
|
+
const cause = error instanceof Error && error.cause !== undefined ? `${error.message}: ${causeOf(error.cause)}` : causeOf(error);
|
|
77
99
|
logged(new Error(`conversation ${conversation.id} did not start: ${cause}`));
|
|
78
100
|
return causeOf(error);
|
|
79
101
|
}
|
|
102
|
+
function audioOf(result) {
|
|
103
|
+
if (result.isError === true)
|
|
104
|
+
return null;
|
|
105
|
+
const file = inspectedFile(result.content.map((content) => content.text).join('\n'));
|
|
106
|
+
return file?.media_type?.startsWith('audio/') === true ? file : null;
|
|
107
|
+
}
|
|
80
108
|
function marker(bridge) {
|
|
81
109
|
return `HOUSE_BRIDGE=${bridge.env.HOUSE_BRIDGE}`;
|
|
82
110
|
}
|
|
111
|
+
function steers(adapter) {
|
|
112
|
+
return adapter.initialized._meta?.steering?.supported === true;
|
|
113
|
+
}
|
|
83
114
|
class Turn {
|
|
84
115
|
id = randomUUID();
|
|
85
116
|
parts;
|
|
86
|
-
admitted = false;
|
|
87
117
|
questions = 0;
|
|
88
118
|
secrets = 0;
|
|
89
119
|
prompted = false;
|
|
120
|
+
steered = false;
|
|
121
|
+
taken = null;
|
|
90
122
|
ended = false;
|
|
123
|
+
quietUntil = Infinity;
|
|
91
124
|
images = 0;
|
|
92
125
|
uploads = [];
|
|
93
126
|
constructor(emit) {
|
|
@@ -100,21 +133,33 @@ class Conversation {
|
|
|
100
133
|
session = null;
|
|
101
134
|
turn = null;
|
|
102
135
|
commands = null;
|
|
136
|
+
options = null;
|
|
137
|
+
exitUntold = false;
|
|
138
|
+
afk = false;
|
|
103
139
|
context = null;
|
|
140
|
+
tokens = null;
|
|
104
141
|
leftovers = new Set();
|
|
105
142
|
waiting = [];
|
|
143
|
+
done = false;
|
|
106
144
|
sending = null;
|
|
107
145
|
subagents = new Map();
|
|
108
146
|
held = [];
|
|
109
147
|
opening = null;
|
|
148
|
+
bridge = null;
|
|
149
|
+
launch = null;
|
|
110
150
|
prompted = null;
|
|
111
151
|
kills = 0;
|
|
112
152
|
killed = new AbortController();
|
|
113
153
|
interrupts = 0;
|
|
114
154
|
interrupted = new AbortController();
|
|
115
|
-
|
|
155
|
+
turns = new Set();
|
|
116
156
|
queue = Promise.resolve();
|
|
117
157
|
reports = Promise.resolve();
|
|
158
|
+
starting = null;
|
|
159
|
+
preparing = null;
|
|
160
|
+
told = null;
|
|
161
|
+
untold = false;
|
|
162
|
+
shown = null;
|
|
118
163
|
constructor(id) {
|
|
119
164
|
this.id = id;
|
|
120
165
|
}
|
|
@@ -138,23 +183,25 @@ export class Conversations {
|
|
|
138
183
|
// A process or a durable schedule ends without a report, so the Kit looks at both again every two seconds.
|
|
139
184
|
setInterval(() => this.look(), 2_000);
|
|
140
185
|
}
|
|
141
|
-
|
|
142
|
-
void this.kit.
|
|
143
|
-
const conversation = this.conversation(
|
|
144
|
-
if (
|
|
186
|
+
call(call) {
|
|
187
|
+
void this.kit.calls.carry(call.input_id, () => {
|
|
188
|
+
const conversation = this.conversation(call.conversation_id);
|
|
189
|
+
if (call.kind === 'open' || call.kind === 'message')
|
|
190
|
+
conversation.afk = call.afk === true;
|
|
191
|
+
if (call.kind === 'kill') {
|
|
145
192
|
conversation.kills++;
|
|
146
193
|
conversation.killed.abort(new Error(KILLED));
|
|
147
194
|
for (const waiting of conversation.waiting.splice(0))
|
|
148
195
|
waiting.settle({ refused: KILLED });
|
|
149
196
|
this.halt(conversation);
|
|
150
197
|
}
|
|
151
|
-
if (
|
|
198
|
+
if (call.kind === 'interrupt') {
|
|
152
199
|
conversation.interrupts++;
|
|
153
200
|
conversation.interrupted.abort();
|
|
154
201
|
}
|
|
155
|
-
const handled = conversation.queue.then(() => this.carry(conversation,
|
|
202
|
+
const handled = conversation.queue.then(() => this.carry(conversation, call));
|
|
156
203
|
conversation.queue = handled.catch(() => undefined);
|
|
157
|
-
return handled.then(async ({
|
|
204
|
+
return handled.then(async ({ result }) => ({ result: await result }));
|
|
158
205
|
});
|
|
159
206
|
}
|
|
160
207
|
idle() {
|
|
@@ -163,7 +210,7 @@ export class Conversations {
|
|
|
163
210
|
for (const conversation of this.conversations.values()) {
|
|
164
211
|
const turn = conversation.turn;
|
|
165
212
|
const asking = turn !== null && turn.questions > 0 && turn.secrets === 0;
|
|
166
|
-
if (conversation.
|
|
213
|
+
if (conversation.turns.size > (asking ? 1 : 0))
|
|
167
214
|
return false;
|
|
168
215
|
}
|
|
169
216
|
const conversations = [...this.conversations.values()];
|
|
@@ -172,7 +219,7 @@ export class Conversations {
|
|
|
172
219
|
return !conversations.some((conversation) => conversation.running?.scope.working() || [...conversation.leftovers].some((scope) => scope.working()));
|
|
173
220
|
}
|
|
174
221
|
quiet() {
|
|
175
|
-
return this.idle() && [...this.conversations.values()].every((conversation) => conversation.
|
|
222
|
+
return this.idle() && [...this.conversations.values()].every((conversation) => conversation.turns.size === 0);
|
|
176
223
|
}
|
|
177
224
|
look() {
|
|
178
225
|
for (const conversation of this.conversations.values()) {
|
|
@@ -187,6 +234,16 @@ export class Conversations {
|
|
|
187
234
|
}
|
|
188
235
|
opened() {
|
|
189
236
|
this.watched = false;
|
|
237
|
+
for (const conversation of this.conversations.values()) {
|
|
238
|
+
if (conversation.running !== null) {
|
|
239
|
+
this.facts(conversation);
|
|
240
|
+
this.subagentsChanged(conversation);
|
|
241
|
+
}
|
|
242
|
+
else if (conversation.exitUntold)
|
|
243
|
+
this.process(conversation, false);
|
|
244
|
+
if (conversation.untold || conversation.told !== null)
|
|
245
|
+
this.retell(conversation);
|
|
246
|
+
}
|
|
190
247
|
}
|
|
191
248
|
watching(watched) {
|
|
192
249
|
const resend = watched && !this.watched;
|
|
@@ -206,134 +263,319 @@ export class Conversations {
|
|
|
206
263
|
}
|
|
207
264
|
return conversation;
|
|
208
265
|
}
|
|
209
|
-
async carry(conversation,
|
|
210
|
-
if (
|
|
211
|
-
return
|
|
212
|
-
if (
|
|
213
|
-
return this.message(conversation,
|
|
214
|
-
if (
|
|
266
|
+
async carry(conversation, call) {
|
|
267
|
+
if (call.kind === 'open')
|
|
268
|
+
return this.open(conversation, call);
|
|
269
|
+
if (call.kind === 'message')
|
|
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
|
+
}
|
|
276
|
+
await this.launched(conversation);
|
|
277
|
+
if (call.kind === 'interrupt') {
|
|
215
278
|
if (--conversation.interrupts === 0)
|
|
216
279
|
conversation.interrupted = new AbortController();
|
|
217
|
-
await this.interrupt(conversation, String(
|
|
280
|
+
await this.interrupt(conversation, String(call.turn_id));
|
|
218
281
|
}
|
|
219
|
-
if (
|
|
282
|
+
if (call.kind === 'kill') {
|
|
220
283
|
await this.kill(conversation);
|
|
221
284
|
await conversation.sending;
|
|
222
285
|
if (--conversation.kills === 0)
|
|
223
286
|
conversation.killed = new AbortController();
|
|
224
287
|
}
|
|
225
|
-
if (
|
|
226
|
-
await this.option(conversation, String(
|
|
227
|
-
if (
|
|
228
|
-
const question = this.questions.get(String(
|
|
288
|
+
if (call.kind === 'option')
|
|
289
|
+
await this.option(conversation, String(call.option), call.value);
|
|
290
|
+
if (call.kind === 'answer') {
|
|
291
|
+
const question = this.questions.get(String(call.interaction_id));
|
|
229
292
|
if (question === undefined) {
|
|
230
|
-
const text = answerMessage(
|
|
231
|
-
return this.message(conversation, { ...
|
|
293
|
+
const text = answerMessage(call.request, call.response);
|
|
294
|
+
return this.message(conversation, { ...call, text, first: false, message_id: null });
|
|
232
295
|
}
|
|
233
|
-
question.answer(
|
|
296
|
+
question.answer(call.response);
|
|
234
297
|
}
|
|
235
|
-
return {
|
|
298
|
+
return { result: Promise.resolve({}) };
|
|
299
|
+
}
|
|
300
|
+
async open(conversation, call) {
|
|
301
|
+
await this.stop(conversation);
|
|
302
|
+
const launch = conversation.launch ?? this.launch(conversation, this.route(conversation, call.agent_id), null, true);
|
|
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;
|
|
311
|
+
}
|
|
312
|
+
launch(conversation, route, sessionId, empty) {
|
|
313
|
+
let opened;
|
|
314
|
+
let failed;
|
|
315
|
+
const bridge = new Promise((resolve, reject) => {
|
|
316
|
+
opened = resolve;
|
|
317
|
+
failed = reject;
|
|
318
|
+
});
|
|
319
|
+
bridge.catch(() => undefined);
|
|
320
|
+
const session = this.start(conversation, route, sessionId, empty, opened);
|
|
321
|
+
session.catch(failed);
|
|
322
|
+
const launch = { bridge, session };
|
|
323
|
+
conversation.launch = launch;
|
|
324
|
+
void session
|
|
325
|
+
.catch(() => undefined)
|
|
326
|
+
.then(() => {
|
|
327
|
+
if (conversation.launch !== launch)
|
|
328
|
+
return;
|
|
329
|
+
conversation.launch = null;
|
|
330
|
+
this.closeIdleBridge(conversation);
|
|
331
|
+
this.next(conversation);
|
|
332
|
+
});
|
|
333
|
+
this.next(conversation);
|
|
334
|
+
return launch;
|
|
236
335
|
}
|
|
237
|
-
async
|
|
238
|
-
|
|
239
|
-
|
|
336
|
+
async launched(conversation) {
|
|
337
|
+
while (conversation.launch !== null)
|
|
338
|
+
await conversation.launch.session.catch(() => undefined);
|
|
339
|
+
}
|
|
340
|
+
instructed(conversation, route) {
|
|
341
|
+
const bridge = conversation.running === null ? conversation.launch?.bridge : Promise.resolve(conversation.running.bridge);
|
|
342
|
+
if (bridge === undefined)
|
|
343
|
+
return null;
|
|
344
|
+
const read = Promise.all([bridge, this.kit.copies.mapping()]).then(([opened, copies]) => instructions(opened, route.base_instructions, copies));
|
|
345
|
+
read.catch(() => undefined);
|
|
346
|
+
return read;
|
|
347
|
+
}
|
|
348
|
+
async message(conversation, call) {
|
|
349
|
+
const content = [
|
|
350
|
+
...(typeof call.recent === 'string' ? [{ type: 'text', text: call.recent }] : []),
|
|
351
|
+
{ type: 'text', text: String(call.text) },
|
|
352
|
+
];
|
|
353
|
+
let route;
|
|
354
|
+
let instructing = null;
|
|
240
355
|
try {
|
|
241
|
-
|
|
356
|
+
route = await this.route(conversation, call.agent_id);
|
|
357
|
+
if (call.first === true)
|
|
358
|
+
instructing = this.instructed(conversation, route);
|
|
359
|
+
if (call.message_id !== null)
|
|
360
|
+
content.push(...(await this.transcribed(conversation, String(call.message_id), String(call.text))));
|
|
361
|
+
if (conversation.kills > 0)
|
|
362
|
+
throw new Error(KILLED);
|
|
242
363
|
}
|
|
243
364
|
catch (error) {
|
|
244
|
-
|
|
365
|
+
this.closeIdleBridge(conversation);
|
|
366
|
+
this.prepares(conversation, null);
|
|
367
|
+
return { result: Promise.resolve({ refused: refusal(conversation, error) }) };
|
|
245
368
|
}
|
|
369
|
+
conversation.preparing = null;
|
|
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 };
|
|
246
378
|
}
|
|
247
|
-
async
|
|
248
|
-
|
|
249
|
-
|
|
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;
|
|
250
399
|
try {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
content.push({ type: 'text', text: lines.join('\n') });
|
|
257
|
-
if (conversation.kills > 0)
|
|
258
|
-
throw new Error(KILLED);
|
|
400
|
+
({ outcome } = await running.adapter.connection.agent.request('_session/steering', {
|
|
401
|
+
sessionId: running.sessionId,
|
|
402
|
+
prompt: waiting.content,
|
|
403
|
+
_meta: { steering: { idleBehavior: 'promptRequired' } },
|
|
404
|
+
}));
|
|
259
405
|
}
|
|
260
406
|
catch (error) {
|
|
261
|
-
|
|
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 })) });
|
|
262
423
|
}
|
|
263
|
-
return {
|
|
264
|
-
ack: new Promise((settle) => {
|
|
265
|
-
conversation.waiting.push({ input, route, content, settle });
|
|
266
|
-
this.next(conversation);
|
|
267
|
-
}),
|
|
268
|
-
};
|
|
269
424
|
}
|
|
270
425
|
next(conversation) {
|
|
271
|
-
if (conversation.sending !== null || conversation.
|
|
426
|
+
if (conversation.sending !== null || conversation.turns.size > 0)
|
|
272
427
|
return;
|
|
273
428
|
const waiting = conversation.waiting.shift();
|
|
274
|
-
if (waiting === undefined)
|
|
429
|
+
if (waiting === undefined) {
|
|
430
|
+
const step = conversation.preparing ?? (conversation.launch === null ? null : 'session');
|
|
431
|
+
if (conversation.starting !== step)
|
|
432
|
+
this.starts(conversation, step);
|
|
433
|
+
if (conversation.done && conversation.launch === null)
|
|
434
|
+
void this.stop(conversation).catch(logged);
|
|
275
435
|
return;
|
|
436
|
+
}
|
|
276
437
|
conversation.sending = this.send(conversation, waiting).finally(() => {
|
|
277
438
|
conversation.sending = null;
|
|
278
439
|
this.next(conversation);
|
|
279
440
|
});
|
|
280
441
|
}
|
|
281
442
|
async send(conversation, waiting) {
|
|
282
|
-
const {
|
|
283
|
-
let
|
|
443
|
+
const { call } = waiting;
|
|
444
|
+
let result = {};
|
|
284
445
|
let started = false;
|
|
446
|
+
let instructing = waiting.instructing;
|
|
285
447
|
const prompt = [];
|
|
286
448
|
try {
|
|
287
449
|
if (conversation.running === null) {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
450
|
+
this.starts(conversation, 'session');
|
|
451
|
+
await this.launched(conversation);
|
|
452
|
+
}
|
|
453
|
+
if (conversation.running === null) {
|
|
454
|
+
const stored = conversation.session ?? call.provider_session_id;
|
|
455
|
+
const empty = call.first === true && conversation.prompted !== stored;
|
|
456
|
+
const launch = this.launch(conversation, Promise.resolve(waiting.route), stored, empty);
|
|
457
|
+
if (call.first === true)
|
|
458
|
+
instructing = this.instructed(conversation, waiting.route);
|
|
459
|
+
started = (await launch.session) !== stored;
|
|
291
460
|
}
|
|
292
461
|
const session = conversation.running.sessionId;
|
|
293
|
-
if (started || (
|
|
294
|
-
|
|
462
|
+
if (started || (call.first === true && session !== call.provider_session_id)) {
|
|
463
|
+
result = { provider_session_id: session };
|
|
295
464
|
}
|
|
296
|
-
if (
|
|
297
|
-
prompt.push({
|
|
298
|
-
type: 'text',
|
|
299
|
-
text: await instructions(conversation.running.bridge, waiting.route.base_instructions, await this.kit.copies.mapping()),
|
|
300
|
-
});
|
|
465
|
+
if (call.first === true) {
|
|
466
|
+
prompt.push({ type: 'text', text: await (instructing ?? this.instructed(conversation, waiting.route)) });
|
|
301
467
|
}
|
|
302
468
|
if (conversation.kills > 0)
|
|
303
469
|
throw new Error(KILLED);
|
|
304
470
|
}
|
|
305
471
|
catch (error) {
|
|
472
|
+
this.starts(conversation, null);
|
|
306
473
|
if (started) {
|
|
307
474
|
conversation.session = null;
|
|
308
475
|
await this.kill(conversation);
|
|
309
476
|
}
|
|
310
|
-
|
|
477
|
+
const refused = refusal(conversation, error);
|
|
478
|
+
const resumeRefused = call.kind === 'message' && conversation.kills === 0 && error instanceof ResumeRefused;
|
|
479
|
+
waiting.settle(resumeRefused ? { refused, resume_refused: true } : { refused });
|
|
311
480
|
return;
|
|
312
481
|
}
|
|
313
|
-
this.
|
|
314
|
-
waiting.
|
|
482
|
+
this.starts(conversation, null);
|
|
483
|
+
this.prompt(conversation, [...prompt, ...waiting.content], () => this.kit.calls.keepResult(String(call.input_id), result));
|
|
484
|
+
waiting.settle(result);
|
|
315
485
|
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
486
|
+
starts(conversation, step) {
|
|
487
|
+
conversation.starting = step;
|
|
488
|
+
if (conversation.shown !== null)
|
|
489
|
+
clearTimeout(conversation.shown);
|
|
490
|
+
conversation.shown = null;
|
|
491
|
+
if (step === null || conversation.told !== null) {
|
|
492
|
+
this.tell(conversation, step);
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
conversation.shown = setTimeout(() => {
|
|
496
|
+
conversation.shown = null;
|
|
497
|
+
if (conversation.starting === step)
|
|
498
|
+
this.tell(conversation, step);
|
|
499
|
+
}, STARTING_SHOWN_AFTER_MS);
|
|
500
|
+
}
|
|
501
|
+
prepares(conversation, step) {
|
|
502
|
+
conversation.preparing = step;
|
|
503
|
+
this.next(conversation);
|
|
504
|
+
}
|
|
505
|
+
tell(conversation, step) {
|
|
506
|
+
if (conversation.told === step && !conversation.untold)
|
|
507
|
+
return;
|
|
508
|
+
conversation.told = step;
|
|
509
|
+
this.retell(conversation);
|
|
510
|
+
}
|
|
511
|
+
retell(conversation) {
|
|
512
|
+
conversation.untold = !this.kit.send({ type: 'starting', conversation_id: conversation.id, step: conversation.told });
|
|
513
|
+
}
|
|
514
|
+
async transcribed(conversation, messageId, text) {
|
|
515
|
+
const paths = linkedPaths(text);
|
|
516
|
+
if (paths.length === 0)
|
|
517
|
+
return [];
|
|
518
|
+
const killed = conversation.killed.signal;
|
|
519
|
+
const interrupted = conversation.interrupted.signal;
|
|
520
|
+
const transcribing = AbortSignal.any([killed, interrupted]);
|
|
521
|
+
let signal = killed;
|
|
522
|
+
const blocks = [];
|
|
523
|
+
try {
|
|
524
|
+
const bridge = await step(unbridged, unlessAborted(this.bridged(conversation), killed));
|
|
525
|
+
const read = async (path) => audioOf(await step(unbridged, unlessAborted(bridge.tool('inspect', { path }), signal)));
|
|
526
|
+
for (const path of paths) {
|
|
527
|
+
let answered = await read(path);
|
|
528
|
+
if (answered === null)
|
|
529
|
+
continue;
|
|
530
|
+
signal = transcribing;
|
|
531
|
+
this.prepares(conversation, 'transcription');
|
|
532
|
+
const said = await transcribe(async () => {
|
|
533
|
+
const address = answered ?? (await read(path));
|
|
534
|
+
answered = null;
|
|
535
|
+
if (address === null)
|
|
536
|
+
throw new Error(`${path} is no longer an audio file`);
|
|
537
|
+
return address;
|
|
538
|
+
}, signal);
|
|
539
|
+
await step(unbridged, unlessAborted(this.kit.house.deliver('/kit/transcripts/keep', { conversation: conversation.id, message: messageId, path, text: said }, () => !signal.aborted), signal));
|
|
540
|
+
blocks.push({ type: 'text', text: `${path}\nTranscript: ${said}` });
|
|
331
541
|
}
|
|
332
542
|
}
|
|
333
|
-
|
|
543
|
+
catch (error) {
|
|
544
|
+
if (signal === transcribing && interrupted.aborted && conversation.kills === 0)
|
|
545
|
+
throw new NotStarted(INTERRUPTED, { cause: error });
|
|
546
|
+
throw error;
|
|
547
|
+
}
|
|
548
|
+
return blocks;
|
|
549
|
+
}
|
|
550
|
+
bridged(conversation) {
|
|
551
|
+
if (conversation.bridge === null) {
|
|
552
|
+
const bridging = openBridge(this.kit.house, this.kit.copies, conversation.id, conversation.killed.signal);
|
|
553
|
+
conversation.bridge = bridging;
|
|
554
|
+
bridging.catch(() => {
|
|
555
|
+
if (conversation.bridge === bridging)
|
|
556
|
+
conversation.bridge = null;
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
return conversation.bridge;
|
|
560
|
+
}
|
|
561
|
+
closeBridge(conversation, bridging, bridge) {
|
|
562
|
+
bridge.close();
|
|
563
|
+
if (conversation.bridge === bridging)
|
|
564
|
+
conversation.bridge = null;
|
|
565
|
+
}
|
|
566
|
+
closeIdleBridge(conversation) {
|
|
567
|
+
const bridging = conversation.bridge;
|
|
568
|
+
if (bridging === null || conversation.running !== null || conversation.launch !== null)
|
|
569
|
+
return;
|
|
570
|
+
conversation.bridge = null;
|
|
571
|
+
void bridging.then((bridge) => bridge.close(), () => undefined);
|
|
334
572
|
}
|
|
335
573
|
async interrupt(conversation, turnId) {
|
|
336
|
-
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)
|
|
337
579
|
return;
|
|
338
580
|
await conversation.running.adapter.connection.agent.notify('session/cancel', {
|
|
339
581
|
sessionId: conversation.running.sessionId,
|
|
@@ -385,17 +627,20 @@ export class Conversations {
|
|
|
385
627
|
}
|
|
386
628
|
return route;
|
|
387
629
|
}
|
|
388
|
-
async start(conversation,
|
|
630
|
+
async start(conversation, routed, sessionId, empty, bridged) {
|
|
631
|
+
const route = await routed;
|
|
389
632
|
const reader = readerFor(route.kind);
|
|
390
633
|
reader.session = sessionId;
|
|
391
634
|
const app = client({ name: '@agentshouse/kit' })
|
|
392
|
-
.onRequest('session/request_permission', ({ params, signal }) => this.ask(conversation, 'session/request_permission', params, false, signal))
|
|
393
|
-
.onRequest('elicitation/create', ({ params, signal }) => this.ask(conversation, 'elicitation/create', params, asksSecret(params), signal))
|
|
394
|
-
.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)));
|
|
395
639
|
const cli = await unlessAborted(this.kit.agents.cli(route.kind), conversation.killed.signal);
|
|
396
|
-
const
|
|
640
|
+
const bridging = this.bridged(conversation);
|
|
641
|
+
const bridge = await step(unbridged, bridging);
|
|
397
642
|
if (conversation.kills > 0) {
|
|
398
|
-
|
|
643
|
+
this.closeBridge(conversation, bridging, bridge);
|
|
399
644
|
throw new Error(KILLED);
|
|
400
645
|
}
|
|
401
646
|
conversation.opening = bridge;
|
|
@@ -403,33 +648,32 @@ export class Conversations {
|
|
|
403
648
|
const scope = new Scope(marker(bridge), randomUUID(), cli);
|
|
404
649
|
let adapter = null;
|
|
405
650
|
try {
|
|
406
|
-
|
|
651
|
+
bridged(bridge);
|
|
407
652
|
await CLIS[route.kind].allowHouse(bridge.env.PATH);
|
|
408
653
|
const streamed = await step(NO_CONNECTION, work.open(cli));
|
|
409
654
|
adapter = await step(NO_START, startAdapter(route.kind, cli, route.working_directory, app, (message) => {
|
|
410
655
|
work.read(message);
|
|
411
656
|
this.read(conversation, reader, message);
|
|
412
657
|
}, { ...bridge.env, ...streamed }, undefined, scope));
|
|
413
|
-
void adapter.exited.then(() =>
|
|
658
|
+
void adapter.exited.then(() => this.closeBridge(conversation, bridging, bridge));
|
|
414
659
|
const agent = adapter.connection.agent;
|
|
415
660
|
const cwd = route.working_directory;
|
|
416
661
|
const _meta = CLIS[route.kind].sessionMeta(bridge.env.HOUSE_BRIDGE, conversation.id);
|
|
417
662
|
const resumed = sessionId === null ? null : await this.resume(conversation, route.kind, adapter, { sessionId, cwd, mcpServers: [], _meta }, empty);
|
|
418
|
-
const opened = resumed ?? (await step(NO_SESSION, agent.request('session/new', { cwd, mcpServers: [], _meta })));
|
|
663
|
+
const opened = resumed ?? (await step(unopened(NO_SESSION), agent.request('session/new', { cwd, mcpServers: [], _meta })));
|
|
419
664
|
reader.session = opened.sessionId;
|
|
420
665
|
if (opened.sessionId !== sessionId && CLIS[route.kind].renames) {
|
|
421
666
|
const prompt = [{ type: 'text', text: `/rename ${conversation.id}` }];
|
|
422
|
-
await step(NO_SESSION, agent.request('session/prompt', { sessionId: opened.sessionId, prompt }));
|
|
667
|
+
await step(unopened(NO_SESSION), agent.request('session/prompt', { sessionId: opened.sessionId, prompt }));
|
|
423
668
|
}
|
|
424
|
-
const running = { adapter, bridge, sessionId: opened.sessionId, killed: false, reader, work, scope };
|
|
669
|
+
const running = { adapter, bridging, bridge, sessionId: opened.sessionId, killed: false, reader, work, scope };
|
|
425
670
|
const options = await step(NO_SETTINGS, this.launchSettings(running, route, opened.configOptions ?? []));
|
|
426
671
|
void work.settled().then(() => scope.record());
|
|
427
672
|
conversation.running = running;
|
|
673
|
+
adapter.turn(conversation.turns.size > 0);
|
|
428
674
|
conversation.session = running.sessionId;
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
this.commands(conversation, conversation.commands);
|
|
432
|
-
this.options(conversation, options);
|
|
675
|
+
conversation.options = options;
|
|
676
|
+
this.facts(conversation);
|
|
433
677
|
void adapter.lost.then((cause) => this.exited(conversation, running, cause));
|
|
434
678
|
return running.sessionId;
|
|
435
679
|
}
|
|
@@ -437,7 +681,7 @@ export class Conversations {
|
|
|
437
681
|
adapter?.retire();
|
|
438
682
|
work.close();
|
|
439
683
|
conversation.leftovers.add(scope);
|
|
440
|
-
|
|
684
|
+
this.closeBridge(conversation, bridging, bridge);
|
|
441
685
|
throw conversation.kills > 0 ? new Error(KILLED) : error;
|
|
442
686
|
}
|
|
443
687
|
finally {
|
|
@@ -450,7 +694,7 @@ export class Conversations {
|
|
|
450
694
|
}
|
|
451
695
|
catch (error) {
|
|
452
696
|
if (!empty || conversation.kills > 0 || !CLIS[kind].unknownSession(error)) {
|
|
453
|
-
throw new NotStarted(NO_RESUME, { cause: error });
|
|
697
|
+
throw signedOut(error) ? new NotStarted(NOT_SIGNED_IN, { cause: error }) : new ResumeRefused(NO_RESUME, { cause: error });
|
|
454
698
|
}
|
|
455
699
|
logged(new Error(`conversation ${conversation.id} opens a new session: ${NO_RESUME}: ${causeOf(error)}`));
|
|
456
700
|
return null;
|
|
@@ -486,15 +730,36 @@ export class Conversations {
|
|
|
486
730
|
this.kit.changed();
|
|
487
731
|
}
|
|
488
732
|
detach(conversation, running) {
|
|
733
|
+
if (conversation.bridge === running.bridging)
|
|
734
|
+
conversation.bridge = null;
|
|
489
735
|
running.work.close();
|
|
490
736
|
conversation.leftovers.add(running.scope);
|
|
491
737
|
conversation.running = null;
|
|
492
738
|
conversation.commands = null;
|
|
739
|
+
conversation.options = null;
|
|
740
|
+
if (conversation.subagents.size > 0) {
|
|
741
|
+
conversation.subagents.clear();
|
|
742
|
+
this.subagentsChanged(conversation);
|
|
743
|
+
}
|
|
493
744
|
for (const [id, question] of this.questions) {
|
|
494
745
|
if (question.conversation === conversation)
|
|
495
746
|
this.questions.delete(id);
|
|
496
747
|
}
|
|
497
|
-
this.
|
|
748
|
+
this.process(conversation, false);
|
|
749
|
+
}
|
|
750
|
+
process(conversation, running) {
|
|
751
|
+
conversation.exitUntold = !this.kit.send({ type: 'process', conversation_id: conversation.id, running }) && !running;
|
|
752
|
+
}
|
|
753
|
+
facts(conversation) {
|
|
754
|
+
this.process(conversation, true);
|
|
755
|
+
if (conversation.commands !== null)
|
|
756
|
+
this.commands(conversation, conversation.commands);
|
|
757
|
+
if (conversation.options !== null)
|
|
758
|
+
this.options(conversation, conversation.options);
|
|
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 });
|
|
498
763
|
}
|
|
499
764
|
commands(conversation, commands) {
|
|
500
765
|
conversation.commands = commands;
|
|
@@ -505,11 +770,12 @@ export class Conversations {
|
|
|
505
770
|
options(conversation, options) {
|
|
506
771
|
if (conversation.running === null)
|
|
507
772
|
return;
|
|
773
|
+
conversation.options = options;
|
|
508
774
|
this.kit.send({ type: 'options', conversation_id: conversation.id, options });
|
|
509
775
|
}
|
|
510
|
-
deliver(conversation, turn,
|
|
776
|
+
deliver(conversation, turn, act, body, current) {
|
|
511
777
|
this.reporting++;
|
|
512
|
-
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));
|
|
513
779
|
conversation.reports = delivered
|
|
514
780
|
.catch(() => undefined)
|
|
515
781
|
.finally(() => {
|
|
@@ -519,7 +785,7 @@ export class Conversations {
|
|
|
519
785
|
return delivered;
|
|
520
786
|
}
|
|
521
787
|
write(conversation, turn, operation) {
|
|
522
|
-
if (
|
|
788
|
+
if (turn.ended || !this.watched)
|
|
523
789
|
return;
|
|
524
790
|
this.kit.send({ type: 'turn', conversation_id: conversation.id, turn_id: turn.id, ...operation });
|
|
525
791
|
}
|
|
@@ -529,11 +795,12 @@ export class Conversations {
|
|
|
529
795
|
}
|
|
530
796
|
begin(conversation) {
|
|
531
797
|
const turn = new Turn((operation) => this.write(conversation, turn, operation));
|
|
532
|
-
conversation.
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
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;
|
|
537
804
|
})
|
|
538
805
|
.catch(logged);
|
|
539
806
|
conversation.turn = turn;
|
|
@@ -556,24 +823,30 @@ export class Conversations {
|
|
|
556
823
|
if (notice !== null)
|
|
557
824
|
turn.parts.start({ type: 'marker', marker: 'retry', text: notice });
|
|
558
825
|
turn.ended = true;
|
|
559
|
-
conversation.
|
|
826
|
+
conversation.turns.delete(turn);
|
|
827
|
+
conversation.running?.adapter.turn(conversation.turns.size > 0);
|
|
560
828
|
if (conversation.turn === turn)
|
|
561
829
|
conversation.turn = null;
|
|
830
|
+
const tokens = conversation.tokens;
|
|
831
|
+
conversation.tokens = null;
|
|
562
832
|
const body = Promise.allSettled(turn.uploads).then(() => ({
|
|
563
833
|
parts: turn.parts.saved(),
|
|
564
834
|
context: turn.parts.context,
|
|
835
|
+
...(tokens === null ? {} : { tokens }),
|
|
565
836
|
...(failed === null ? {} : { failed: failed.failure }),
|
|
566
837
|
}));
|
|
567
|
-
void this.deliver(conversation, turn, '
|
|
838
|
+
void this.deliver(conversation, turn, 'turns/end', body).catch(logged);
|
|
568
839
|
this.next(conversation);
|
|
569
840
|
this.kit.changed();
|
|
570
841
|
}
|
|
571
|
-
prompt(conversation, prompt) {
|
|
842
|
+
prompt(conversation, prompt, taken) {
|
|
572
843
|
const running = conversation.running;
|
|
573
844
|
const turn = this.begin(conversation);
|
|
574
845
|
turn.prompted = true;
|
|
846
|
+
turn.taken = taken;
|
|
575
847
|
conversation.prompted = running.sessionId;
|
|
576
848
|
running.adapter.connection.agent.request('session/prompt', { sessionId: running.sessionId, prompt }).then((answer) => {
|
|
849
|
+
this.took(turn);
|
|
577
850
|
const notice = answeredFailure(answer);
|
|
578
851
|
if (notice !== null) {
|
|
579
852
|
this.end(conversation, turn, { failure: notice.limit ? 'limit' : 'error', cause: notice.title }, notice.title);
|
|
@@ -586,7 +859,8 @@ export class Conversations {
|
|
|
586
859
|
this.end(conversation, turn, exitedWith(running, await running.adapter.exited));
|
|
587
860
|
}
|
|
588
861
|
else {
|
|
589
|
-
|
|
862
|
+
this.took(turn);
|
|
863
|
+
if (conversation.turns.size === 1 && conversation.turns.has(turn) && conversation.running === running) {
|
|
590
864
|
running.adapter.retire();
|
|
591
865
|
this.detach(conversation, running);
|
|
592
866
|
}
|
|
@@ -595,21 +869,29 @@ export class Conversations {
|
|
|
595
869
|
});
|
|
596
870
|
}
|
|
597
871
|
started(conversation) {
|
|
598
|
-
|
|
872
|
+
const turn = conversation.turn ?? this.begin(conversation);
|
|
873
|
+
this.took(turn);
|
|
874
|
+
return turn;
|
|
599
875
|
}
|
|
600
|
-
ended(conversation) {
|
|
876
|
+
ended(conversation, stop) {
|
|
601
877
|
const turn = conversation.turn;
|
|
602
878
|
if (turn === null)
|
|
603
879
|
return;
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
this.apply(conversation, turn, event);
|
|
610
|
-
conversation.turn = null;
|
|
611
|
-
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;
|
|
612
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);
|
|
613
895
|
}
|
|
614
896
|
async ask(conversation, method, params, secret, signal) {
|
|
615
897
|
const turn = this.started(conversation);
|
|
@@ -617,7 +899,7 @@ export class Conversations {
|
|
|
617
899
|
const answered = new Promise((answer, fail) => {
|
|
618
900
|
this.questions.set(interactionId, { conversation, answer: (response) => answer(response) });
|
|
619
901
|
signal.addEventListener('abort', () => answer(null), { once: true });
|
|
620
|
-
this.deliver(conversation, turn, '
|
|
902
|
+
this.deliver(conversation, turn, 'conversations/interaction/ask', {
|
|
621
903
|
interaction_id: interactionId,
|
|
622
904
|
request: { method, params },
|
|
623
905
|
secret,
|
|
@@ -658,6 +940,18 @@ export class Conversations {
|
|
|
658
940
|
read(conversation, reader, message) {
|
|
659
941
|
for (const event of reader.read(message))
|
|
660
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
|
+
});
|
|
661
955
|
}
|
|
662
956
|
event(conversation, event) {
|
|
663
957
|
switch (event.kind) {
|
|
@@ -665,7 +959,7 @@ export class Conversations {
|
|
|
665
959
|
this.started(conversation);
|
|
666
960
|
return;
|
|
667
961
|
case 'ended':
|
|
668
|
-
this.ended(conversation);
|
|
962
|
+
this.ended(conversation, event.stop);
|
|
669
963
|
return;
|
|
670
964
|
case 'commands':
|
|
671
965
|
this.commands(conversation, event.commands);
|
|
@@ -677,9 +971,15 @@ export class Conversations {
|
|
|
677
971
|
conversation.context = { used: event.used, window: event.window };
|
|
678
972
|
conversation.turn?.parts.measure(conversation.context);
|
|
679
973
|
return;
|
|
974
|
+
case 'tokens':
|
|
975
|
+
conversation.tokens = addedTokens(conversation.tokens, event.tokens);
|
|
976
|
+
return;
|
|
680
977
|
case 'subagent-done':
|
|
681
978
|
this.subagentDone(conversation, event.id, event.result);
|
|
682
979
|
return;
|
|
980
|
+
case 'subagent-activity':
|
|
981
|
+
this.subagentActivity(conversation, event.id, event.activity);
|
|
982
|
+
return;
|
|
683
983
|
case 'marker':
|
|
684
984
|
case 'image':
|
|
685
985
|
if (conversation.turn !== null)
|
|
@@ -690,6 +990,11 @@ export class Conversations {
|
|
|
690
990
|
}
|
|
691
991
|
this.apply(conversation, this.started(conversation), event);
|
|
692
992
|
}
|
|
993
|
+
took(turn) {
|
|
994
|
+
const taken = turn.taken;
|
|
995
|
+
turn.taken = null;
|
|
996
|
+
taken?.();
|
|
997
|
+
}
|
|
693
998
|
apply(conversation, turn, event) {
|
|
694
999
|
switch (event.kind) {
|
|
695
1000
|
case 'text':
|
|
@@ -701,7 +1006,8 @@ export class Conversations {
|
|
|
701
1006
|
return;
|
|
702
1007
|
case 'subagent': {
|
|
703
1008
|
const index = turn.parts.start({ type: 'subagent', description: event.description, status: 'running', result: null });
|
|
704
|
-
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);
|
|
705
1011
|
return;
|
|
706
1012
|
}
|
|
707
1013
|
case 'message':
|
|
@@ -724,6 +1030,7 @@ export class Conversations {
|
|
|
724
1030
|
if (subagent === undefined)
|
|
725
1031
|
return;
|
|
726
1032
|
conversation.subagents.delete(id);
|
|
1033
|
+
this.subagentsChanged(conversation);
|
|
727
1034
|
if (subagent.turn === conversation.turn) {
|
|
728
1035
|
subagent.turn.parts.done(subagent.index, result);
|
|
729
1036
|
return;
|
|
@@ -734,23 +1041,37 @@ export class Conversations {
|
|
|
734
1041
|
else
|
|
735
1042
|
conversation.turn.parts.start(part);
|
|
736
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
|
+
}
|
|
737
1051
|
async image(conversation, turn, reserved, data, mediaType) {
|
|
738
1052
|
const bytes = Buffer.from(data, 'base64');
|
|
739
1053
|
const name = `image-${++turn.images}.${mediaType.split('/')[1]}`;
|
|
1054
|
+
const sha256 = createHash('sha256').update(bytes).digest('hex');
|
|
740
1055
|
try {
|
|
741
|
-
const declared = (await this.deliver(conversation, turn, 'images', {
|
|
1056
|
+
const declared = (await this.deliver(conversation, turn, 'turns/images/declare', {
|
|
742
1057
|
name,
|
|
743
1058
|
media_type: mediaType,
|
|
744
1059
|
bytes: bytes.length,
|
|
745
|
-
sha256
|
|
1060
|
+
sha256,
|
|
746
1061
|
}));
|
|
747
|
-
const saved =
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
1062
|
+
const saved = await uploaded(declared.upload, {
|
|
1063
|
+
name,
|
|
1064
|
+
bytes: bytes.length,
|
|
1065
|
+
sha256,
|
|
1066
|
+
read: (start, end) => [bytes.subarray(start, end)],
|
|
1067
|
+
send: (url, start, end, headers) => sentPart(url, bytes.subarray(start, end), headers),
|
|
1068
|
+
finish: (upload, partSha256) => this.kit.house.deliver('/kit/uploads/finish', { upload, part_sha256: partSha256 }),
|
|
1069
|
+
});
|
|
1070
|
+
turn.parts.arrive({ type: 'image', file: saved.path, name, media_type: mediaType }, reserved);
|
|
751
1071
|
}
|
|
752
1072
|
catch (error) {
|
|
753
|
-
|
|
1073
|
+
const cause = error instanceof HouseRefusal ? (refusalOf(error.text)?.text ?? causeOf(error)) : causeOf(error);
|
|
1074
|
+
turn.parts.arrive({ type: 'marker', marker: 'retry', text: `${name} was not saved: ${cause}` }, reserved);
|
|
754
1075
|
}
|
|
755
1076
|
}
|
|
756
1077
|
}
|