@agentshouse/kit 0.2.1-ce3d2bb67177c → 0.2.1-ce46b2b55463f
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 +31 -35
- package/dist/acp.js +56 -6
- package/dist/afk.js +17 -0
- package/dist/agents.js +120 -51
- package/dist/api.js +11 -6
- package/dist/binaries.js +79 -0
- package/dist/bridge.js +30 -26
- package/dist/building.js +50 -0
- package/dist/calls.js +29 -4
- package/dist/chunks.js +49 -0
- package/dist/clis.js +61 -49
- package/dist/connect.js +108 -0
- package/dist/connection.js +33 -38
- package/dist/content.js +106 -0
- package/dist/conversations.js +325 -141
- package/dist/copies.js +720 -247
- package/dist/earlier.js +263 -0
- package/dist/end.js +4 -5
- package/dist/events.js +549 -281
- package/dist/files.js +110 -28
- package/dist/git-main.js +24 -29
- package/dist/git.js +89 -14
- package/dist/grok.js +39 -0
- package/dist/home.js +65 -29
- package/dist/house-cli.js +114 -159
- package/dist/house-main.js +3 -3
- package/dist/instructions.js +29 -11
- package/dist/keys.js +238 -0
- package/dist/kit-cli.js +16 -6
- package/dist/kit-main.js +3 -3
- package/dist/login.js +36 -20
- package/dist/logout.js +2 -2
- package/dist/mcp.js +38 -1
- package/dist/owner.js +46 -0
- package/dist/placement.js +9 -23
- package/dist/refusals.js +37 -21
- package/dist/relay.js +21 -0
- package/dist/resident.js +109 -65
- package/dist/scope.js +12 -4
- package/dist/scripts.js +303 -0
- package/dist/secret-input.js +1 -1
- package/dist/settings.js +188 -0
- package/dist/sign-in.js +49 -3
- package/dist/silence.js +28 -0
- package/dist/skills.js +41 -96
- package/dist/supervisor.js +163 -0
- package/dist/transcription.js +37 -25
- package/dist/translate.js +18 -9
- package/dist/update.js +115 -117
- package/dist/wire.js +19 -9
- package/dist/work.js +9 -2
- package/dist/workspace.js +103 -0
- package/package.json +25 -21
- package/dist/authority-main.js +0 -22
- package/dist/bootstrap.json +0 -24
- package/dist/chatgpt.js +0 -32
- package/dist/clis-main.js +0 -23
- package/dist/configure-main.js +0 -6
- package/dist/enrol-main.js +0 -10
- package/dist/parts.js +0 -82
- package/dist/service-main.js +0 -9
- package/dist/service.js +0 -55
- 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,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';
|
|
11
|
+
import { Content } from './content.js';
|
|
10
12
|
import { answeredFailure, readerFor } from './events.js';
|
|
11
|
-
import {
|
|
13
|
+
import { fileInfo, linkedPaths, storeRefusal, uploaded } from './files.js';
|
|
14
|
+
import { grokPlanAnswer, grokPlanForm, grokQuestionAnswer, grokQuestionForm } from './grok.js';
|
|
12
15
|
import { agentBase } from './home.js';
|
|
13
16
|
import { instructions } from './instructions.js';
|
|
14
|
-
import { 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
|
-
import { createHowWeWork } from './skills.js';
|
|
20
21
|
import { transcribe } from './transcription.js';
|
|
21
22
|
import { workFor } from './work.js';
|
|
22
|
-
const SIGNED_OUT = RequestError.authRequired().code;
|
|
23
23
|
const KILLED = 'the conversation was killed';
|
|
24
24
|
const INTERRUPTED = 'the conversation was interrupted';
|
|
25
25
|
const REFUSED = 'the CLI refused the prompt';
|
|
@@ -35,6 +35,27 @@ const NO_SESSION = "the Agent's CLI did not open a session";
|
|
|
35
35
|
const NO_RESUME = "the Agent's CLI could not resume this conversation";
|
|
36
36
|
const NOT_SIGNED_IN = "the Agent's CLI is not signed in; sign it in on the Agent's machine";
|
|
37
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
|
+
const TAKEN_OUT = 'the message was taken out before it started';
|
|
40
|
+
const QUIET = {
|
|
41
|
+
title: null,
|
|
42
|
+
context: null,
|
|
43
|
+
limits: [],
|
|
44
|
+
cost: null,
|
|
45
|
+
tokens: null,
|
|
46
|
+
duration: null,
|
|
47
|
+
files: { count: 0, added: 0, removed: 0 },
|
|
48
|
+
branch: null,
|
|
49
|
+
worktree: null,
|
|
50
|
+
pull: null,
|
|
51
|
+
model: null,
|
|
52
|
+
effort: null,
|
|
53
|
+
mode: null,
|
|
54
|
+
suggestion: null,
|
|
55
|
+
running: [],
|
|
56
|
+
};
|
|
57
|
+
// 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.
|
|
58
|
+
const ACTIVITY_REPORT_MS = 60_000;
|
|
38
59
|
class NotStarted extends Error {
|
|
39
60
|
}
|
|
40
61
|
class ResumeRefused extends NotStarted {
|
|
@@ -47,26 +68,41 @@ async function step(reason, work) {
|
|
|
47
68
|
throw new NotStarted(typeof reason === 'string' ? reason : reason(error), { cause: error });
|
|
48
69
|
}
|
|
49
70
|
}
|
|
50
|
-
function signedOut(error) {
|
|
51
|
-
return typeof error === 'object' && error !== null && error.code === SIGNED_OUT;
|
|
52
|
-
}
|
|
53
71
|
function unopened(reason) {
|
|
54
72
|
return (error) => (signedOut(error) ? NOT_SIGNED_IN : reason);
|
|
55
73
|
}
|
|
56
74
|
// A start-up step is shown only once it has lasted a second, so a quick start shows none of them.
|
|
57
75
|
const STARTING_SHOWN_AFTER_MS = 1_000;
|
|
76
|
+
function turnOf(conversation, turn) {
|
|
77
|
+
return { conversation: conversation.id, turn: turn.id };
|
|
78
|
+
}
|
|
58
79
|
function unbridged(error) {
|
|
59
80
|
return (finalRefusal(error) ? ENDED[refusalOf(error.text)?.code ?? ''] : undefined) ?? NO_CONNECTION;
|
|
60
81
|
}
|
|
61
|
-
const
|
|
82
|
+
const GROK_PLAN = {
|
|
83
|
+
parse(value) {
|
|
84
|
+
return value;
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
const GROK_QUESTION = {
|
|
62
88
|
parse(value) {
|
|
63
|
-
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
64
|
-
throw new Error('_x.ai/exit_plan_mode needs an object');
|
|
65
|
-
}
|
|
66
89
|
return value;
|
|
67
90
|
},
|
|
68
91
|
};
|
|
69
|
-
|
|
92
|
+
function addedTokens(sum, tokens) {
|
|
93
|
+
if (sum === null)
|
|
94
|
+
return tokens;
|
|
95
|
+
return {
|
|
96
|
+
input: sum.input + tokens.input,
|
|
97
|
+
output: sum.output + tokens.output,
|
|
98
|
+
cache_read: sum.cache_read + tokens.cache_read,
|
|
99
|
+
cache_write: sum.cache_write + tokens.cache_write,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function cliSaid(error) {
|
|
103
|
+
const details = error?.data?.details;
|
|
104
|
+
return typeof details === 'string' && details !== '' ? `${causeOf(error)}: ${details}` : causeOf(error);
|
|
105
|
+
}
|
|
70
106
|
function causeOf(error) {
|
|
71
107
|
return error instanceof Error ? error.message : String(error);
|
|
72
108
|
}
|
|
@@ -74,13 +110,15 @@ function answeredWith(error) {
|
|
|
74
110
|
return { failure: signedOut(error) ? 'signed_out' : 'error', cause: causeOf(error) };
|
|
75
111
|
}
|
|
76
112
|
function exitedWith(running, cause) {
|
|
77
|
-
|
|
113
|
+
if (running.killed)
|
|
114
|
+
return { failure: 'killed', cause: KILLED };
|
|
115
|
+
return { failure: running.adapter.silent() ? 'silent' : 'exited', cause };
|
|
78
116
|
}
|
|
79
117
|
function answerMessage(request, response) {
|
|
80
118
|
return `The User answered a question you asked earlier.\nQuestion: ${JSON.stringify(request)}\nAnswer: ${JSON.stringify(response)}`;
|
|
81
119
|
}
|
|
82
|
-
async function sentPart(url, bytes) {
|
|
83
|
-
const answer = await fetch(relayed(url), { method: 'PUT', body: bytes });
|
|
120
|
+
async function sentPart(url, bytes, headers) {
|
|
121
|
+
const answer = await fetch(relayed(url), { method: 'PUT', headers, body: bytes });
|
|
84
122
|
await answer.arrayBuffer();
|
|
85
123
|
if (!answer.ok)
|
|
86
124
|
throw storeRefusal('upload', answer.status);
|
|
@@ -98,24 +136,29 @@ function refusal(conversation, error) {
|
|
|
98
136
|
function audioOf(result) {
|
|
99
137
|
if (result.isError === true)
|
|
100
138
|
return null;
|
|
101
|
-
const file =
|
|
139
|
+
const file = fileInfo(result.content.map((content) => content.text).join('\n'));
|
|
102
140
|
return file?.media_type?.startsWith('audio/') === true ? file : null;
|
|
103
141
|
}
|
|
104
142
|
function marker(bridge) {
|
|
105
143
|
return `HOUSE_BRIDGE=${bridge.env.HOUSE_BRIDGE}`;
|
|
106
144
|
}
|
|
145
|
+
function steers(adapter) {
|
|
146
|
+
return adapter.initialized._meta?.steering?.supported === true;
|
|
147
|
+
}
|
|
107
148
|
class Turn {
|
|
108
149
|
id = randomUUID();
|
|
109
|
-
|
|
150
|
+
content;
|
|
110
151
|
questions = 0;
|
|
111
152
|
secrets = 0;
|
|
112
153
|
prompted = false;
|
|
154
|
+
steered = false;
|
|
113
155
|
taken = null;
|
|
114
156
|
ended = false;
|
|
157
|
+
quietUntil = Infinity;
|
|
115
158
|
images = 0;
|
|
116
159
|
uploads = [];
|
|
117
160
|
constructor(emit) {
|
|
118
|
-
this.
|
|
161
|
+
this.content = new Content(emit);
|
|
119
162
|
}
|
|
120
163
|
}
|
|
121
164
|
class Conversation {
|
|
@@ -126,12 +169,16 @@ class Conversation {
|
|
|
126
169
|
commands = null;
|
|
127
170
|
options = null;
|
|
128
171
|
exitUntold = false;
|
|
129
|
-
|
|
172
|
+
afk = false;
|
|
173
|
+
reader = null;
|
|
174
|
+
state = QUIET;
|
|
175
|
+
sentState = null;
|
|
176
|
+
tokens = null;
|
|
130
177
|
leftovers = new Set();
|
|
131
178
|
waiting = [];
|
|
179
|
+
done = false;
|
|
132
180
|
sending = null;
|
|
133
|
-
subagents = new
|
|
134
|
-
held = [];
|
|
181
|
+
subagents = new Set();
|
|
135
182
|
opening = null;
|
|
136
183
|
bridge = null;
|
|
137
184
|
launch = null;
|
|
@@ -140,7 +187,7 @@ class Conversation {
|
|
|
140
187
|
killed = new AbortController();
|
|
141
188
|
interrupts = 0;
|
|
142
189
|
interrupted = new AbortController();
|
|
143
|
-
|
|
190
|
+
turns = new Set();
|
|
144
191
|
queue = Promise.resolve();
|
|
145
192
|
reports = Promise.resolve();
|
|
146
193
|
starting = null;
|
|
@@ -166,6 +213,7 @@ export class Conversations {
|
|
|
166
213
|
questions = new Map();
|
|
167
214
|
reporting = 0;
|
|
168
215
|
watched = false;
|
|
216
|
+
stopping = false;
|
|
169
217
|
constructor(kit) {
|
|
170
218
|
this.kit = kit;
|
|
171
219
|
// A process or a durable schedule ends without a report, so the Kit looks at both again every two seconds.
|
|
@@ -174,6 +222,8 @@ export class Conversations {
|
|
|
174
222
|
call(call) {
|
|
175
223
|
void this.kit.calls.carry(call.input_id, () => {
|
|
176
224
|
const conversation = this.conversation(call.conversation_id);
|
|
225
|
+
if (call.kind === 'open' || call.kind === 'message')
|
|
226
|
+
conversation.afk = call.afk === true;
|
|
177
227
|
if (call.kind === 'kill') {
|
|
178
228
|
conversation.kills++;
|
|
179
229
|
conversation.killed.abort(new Error(KILLED));
|
|
@@ -196,7 +246,7 @@ export class Conversations {
|
|
|
196
246
|
for (const conversation of this.conversations.values()) {
|
|
197
247
|
const turn = conversation.turn;
|
|
198
248
|
const asking = turn !== null && turn.questions > 0 && turn.secrets === 0;
|
|
199
|
-
if (conversation.
|
|
249
|
+
if (conversation.turns.size > (asking ? 1 : 0))
|
|
200
250
|
return false;
|
|
201
251
|
}
|
|
202
252
|
const conversations = [...this.conversations.values()];
|
|
@@ -205,7 +255,10 @@ export class Conversations {
|
|
|
205
255
|
return !conversations.some((conversation) => conversation.running?.scope.working() || [...conversation.leftovers].some((scope) => scope.working()));
|
|
206
256
|
}
|
|
207
257
|
quiet() {
|
|
208
|
-
return this.idle() &&
|
|
258
|
+
return this.idle() && !this.turnOpen();
|
|
259
|
+
}
|
|
260
|
+
turnOpen() {
|
|
261
|
+
return [...this.conversations.values()].some((conversation) => conversation.turns.size > 0);
|
|
209
262
|
}
|
|
210
263
|
look() {
|
|
211
264
|
for (const conversation of this.conversations.values()) {
|
|
@@ -218,11 +271,20 @@ export class Conversations {
|
|
|
218
271
|
}
|
|
219
272
|
this.kit.changed();
|
|
220
273
|
}
|
|
274
|
+
stopped() {
|
|
275
|
+
this.stopping = true;
|
|
276
|
+
for (const conversation of this.conversations.values()) {
|
|
277
|
+
if (conversation.running !== null)
|
|
278
|
+
this.process(conversation, false);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
221
281
|
opened() {
|
|
222
282
|
this.watched = false;
|
|
223
283
|
for (const conversation of this.conversations.values()) {
|
|
224
|
-
if (conversation.running !== null)
|
|
284
|
+
if (conversation.running !== null) {
|
|
225
285
|
this.facts(conversation);
|
|
286
|
+
this.stateChanged(conversation, true);
|
|
287
|
+
}
|
|
226
288
|
else if (conversation.exitUntold)
|
|
227
289
|
this.process(conversation, false);
|
|
228
290
|
if (conversation.untold || conversation.told !== null)
|
|
@@ -249,14 +311,21 @@ export class Conversations {
|
|
|
249
311
|
}
|
|
250
312
|
async carry(conversation, call) {
|
|
251
313
|
if (call.kind === 'open')
|
|
252
|
-
return
|
|
314
|
+
return this.open(conversation, call);
|
|
253
315
|
if (call.kind === 'message')
|
|
254
316
|
return this.message(conversation, call);
|
|
317
|
+
if (call.kind === 'done') {
|
|
318
|
+
conversation.done = true;
|
|
319
|
+
this.next(conversation);
|
|
320
|
+
return { result: Promise.resolve({}) };
|
|
321
|
+
}
|
|
322
|
+
if (call.kind === 'take_out')
|
|
323
|
+
return { result: Promise.resolve(this.takeOut(conversation, call.message_id)) };
|
|
255
324
|
await this.launched(conversation);
|
|
256
325
|
if (call.kind === 'interrupt') {
|
|
257
326
|
if (--conversation.interrupts === 0)
|
|
258
327
|
conversation.interrupted = new AbortController();
|
|
259
|
-
await this.interrupt(conversation,
|
|
328
|
+
await this.interrupt(conversation, call.turn_id);
|
|
260
329
|
}
|
|
261
330
|
if (call.kind === 'kill') {
|
|
262
331
|
await this.kill(conversation);
|
|
@@ -265,9 +334,9 @@ export class Conversations {
|
|
|
265
334
|
conversation.killed = new AbortController();
|
|
266
335
|
}
|
|
267
336
|
if (call.kind === 'option')
|
|
268
|
-
await this.option(conversation,
|
|
337
|
+
await this.option(conversation, call.option, call.value);
|
|
269
338
|
if (call.kind === 'answer') {
|
|
270
|
-
const question = this.questions.get(
|
|
339
|
+
const question = this.questions.get(call.interaction_id);
|
|
271
340
|
if (question === undefined) {
|
|
272
341
|
const text = answerMessage(call.request, call.response);
|
|
273
342
|
return this.message(conversation, { ...call, text, first: false, message_id: null });
|
|
@@ -276,11 +345,25 @@ export class Conversations {
|
|
|
276
345
|
}
|
|
277
346
|
return { result: Promise.resolve({}) };
|
|
278
347
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
348
|
+
takeOut(conversation, messageId) {
|
|
349
|
+
const index = conversation.waiting.findIndex((waiting) => waiting.call.message_id === messageId);
|
|
350
|
+
if (index === -1)
|
|
351
|
+
return { started: true };
|
|
352
|
+
const [waiting] = conversation.waiting.splice(index, 1);
|
|
353
|
+
waiting.settle({ refused: TAKEN_OUT });
|
|
354
|
+
return {};
|
|
355
|
+
}
|
|
356
|
+
async open(conversation, call) {
|
|
357
|
+
await this.stop(conversation);
|
|
282
358
|
const launch = conversation.launch ?? this.launch(conversation, this.route(conversation, call.agent_id), null, true);
|
|
283
|
-
return
|
|
359
|
+
return {
|
|
360
|
+
result: launch.session.then((session) => ({ provider_session_id: session }), (error) => ({ refused: refusal(conversation, error) })),
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
async stop(conversation) {
|
|
364
|
+
const running = conversation.running;
|
|
365
|
+
running?.scope.kill();
|
|
366
|
+
await running?.adapter.exited;
|
|
284
367
|
}
|
|
285
368
|
launch(conversation, route, sessionId, empty) {
|
|
286
369
|
let opened;
|
|
@@ -314,12 +397,15 @@ export class Conversations {
|
|
|
314
397
|
const bridge = conversation.running === null ? conversation.launch?.bridge : Promise.resolve(conversation.running.bridge);
|
|
315
398
|
if (bridge === undefined)
|
|
316
399
|
return null;
|
|
317
|
-
const read =
|
|
400
|
+
const read = bridge.then((opened) => instructions(opened, route.base_instructions));
|
|
318
401
|
read.catch(() => undefined);
|
|
319
402
|
return read;
|
|
320
403
|
}
|
|
321
404
|
async message(conversation, call) {
|
|
322
|
-
const content = [
|
|
405
|
+
const content = [
|
|
406
|
+
...(call.recent === undefined ? [] : [{ type: 'text', text: call.recent }]),
|
|
407
|
+
{ type: 'text', text: call.text },
|
|
408
|
+
];
|
|
323
409
|
let route;
|
|
324
410
|
let instructing = null;
|
|
325
411
|
try {
|
|
@@ -327,7 +413,7 @@ export class Conversations {
|
|
|
327
413
|
if (call.first === true)
|
|
328
414
|
instructing = this.instructed(conversation, route);
|
|
329
415
|
if (call.message_id !== null)
|
|
330
|
-
content.push(...(await this.transcribed(conversation,
|
|
416
|
+
content.push(...(await this.transcribed(conversation, call.message_id, call.text)));
|
|
331
417
|
if (conversation.kills > 0)
|
|
332
418
|
throw new Error(KILLED);
|
|
333
419
|
}
|
|
@@ -337,21 +423,71 @@ export class Conversations {
|
|
|
337
423
|
return { result: Promise.resolve({ refused: refusal(conversation, error) }) };
|
|
338
424
|
}
|
|
339
425
|
conversation.preparing = null;
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
426
|
+
const { promise, resolve: settle } = Promise.withResolvers();
|
|
427
|
+
const waiting = { call, route, content, instructing, settle };
|
|
428
|
+
if (call.at_once === true && conversation.turns.size > 0)
|
|
429
|
+
await this.atOnce(conversation, waiting);
|
|
430
|
+
else
|
|
431
|
+
conversation.waiting.push(waiting);
|
|
432
|
+
this.next(conversation);
|
|
433
|
+
return { result: promise };
|
|
434
|
+
}
|
|
435
|
+
async atOnce(conversation, waiting) {
|
|
436
|
+
while (conversation.sending !== null)
|
|
437
|
+
await conversation.sending;
|
|
438
|
+
const running = conversation.running;
|
|
439
|
+
if (running !== null && conversation.turns.size > 0 && steers(running.adapter)) {
|
|
440
|
+
conversation.sending = this.steer(conversation, running, waiting);
|
|
441
|
+
await conversation.sending;
|
|
442
|
+
conversation.sending = null;
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
this.ahead(conversation, waiting);
|
|
446
|
+
if (conversation.turns.size > 0)
|
|
447
|
+
await this.cancel(conversation);
|
|
448
|
+
}
|
|
449
|
+
ahead(conversation, waiting) {
|
|
450
|
+
const behind = conversation.waiting.findIndex((held) => held.call.at_once !== true);
|
|
451
|
+
conversation.waiting.splice(behind === -1 ? conversation.waiting.length : behind, 0, waiting);
|
|
452
|
+
}
|
|
453
|
+
async steer(conversation, running, waiting) {
|
|
454
|
+
let outcome;
|
|
455
|
+
try {
|
|
456
|
+
({ outcome } = await running.adapter.connection.agent.request('_session/steering', {
|
|
457
|
+
sessionId: running.sessionId,
|
|
458
|
+
prompt: waiting.content,
|
|
459
|
+
_meta: { steering: { idleBehavior: 'promptRequired' } },
|
|
460
|
+
}));
|
|
461
|
+
}
|
|
462
|
+
catch (error) {
|
|
463
|
+
outcome = error;
|
|
464
|
+
}
|
|
465
|
+
if (outcome === 'promptRequired') {
|
|
466
|
+
this.ahead(conversation, waiting);
|
|
467
|
+
}
|
|
468
|
+
else if (outcome === 'injected' || outcome === 'startedNewTurn') {
|
|
469
|
+
if (outcome === 'injected') {
|
|
470
|
+
for (const turn of conversation.turns)
|
|
471
|
+
turn.steered ||= turn.prompted;
|
|
472
|
+
}
|
|
473
|
+
this.kit.calls.keepResult(waiting.call.input_id, {});
|
|
474
|
+
waiting.settle({});
|
|
475
|
+
}
|
|
476
|
+
else {
|
|
477
|
+
const cause = outcome instanceof Error ? outcome : new Error(`the CLI answered ${JSON.stringify(outcome)}`);
|
|
478
|
+
waiting.settle({ refused: refusal(conversation, new NotStarted(UNSTEERED, { cause })) });
|
|
479
|
+
}
|
|
346
480
|
}
|
|
347
481
|
next(conversation) {
|
|
348
|
-
if (conversation.sending !== null || conversation.
|
|
482
|
+
if (this.stopping || conversation.sending !== null || conversation.turns.size > 0)
|
|
349
483
|
return;
|
|
350
484
|
const waiting = conversation.waiting.shift();
|
|
351
485
|
if (waiting === undefined) {
|
|
352
486
|
const step = conversation.preparing ?? (conversation.launch === null ? null : 'session');
|
|
353
487
|
if (conversation.starting !== step)
|
|
354
488
|
this.starts(conversation, step);
|
|
489
|
+
if (conversation.done && conversation.launch === null)
|
|
490
|
+
void this.stop(conversation).catch(logged);
|
|
355
491
|
return;
|
|
356
492
|
}
|
|
357
493
|
conversation.sending = this.send(conversation, waiting).finally(() => {
|
|
@@ -400,7 +536,7 @@ export class Conversations {
|
|
|
400
536
|
return;
|
|
401
537
|
}
|
|
402
538
|
this.starts(conversation, null);
|
|
403
|
-
this.prompt(conversation, [...prompt, ...waiting.content], () => this.kit.calls.keepResult(
|
|
539
|
+
this.prompt(conversation, [...prompt, ...waiting.content], () => this.kit.calls.keepResult(call.input_id, result));
|
|
404
540
|
waiting.settle(result);
|
|
405
541
|
}
|
|
406
542
|
starts(conversation, step) {
|
|
@@ -442,7 +578,7 @@ export class Conversations {
|
|
|
442
578
|
const blocks = [];
|
|
443
579
|
try {
|
|
444
580
|
const bridge = await step(unbridged, unlessAborted(this.bridged(conversation), killed));
|
|
445
|
-
const read = async (path) => audioOf(await step(unbridged, unlessAborted(bridge.tool('
|
|
581
|
+
const read = async (path) => audioOf(await step(unbridged, unlessAborted(bridge.tool('info', { path }), signal)));
|
|
446
582
|
for (const path of paths) {
|
|
447
583
|
let answered = await read(path);
|
|
448
584
|
if (answered === null)
|
|
@@ -456,8 +592,7 @@ export class Conversations {
|
|
|
456
592
|
throw new Error(`${path} is no longer an audio file`);
|
|
457
593
|
return address;
|
|
458
594
|
}, signal);
|
|
459
|
-
|
|
460
|
-
await step(unbridged, unlessAborted(this.kit.house.deliver(transcript, { path, text: said }, () => !signal.aborted), signal));
|
|
595
|
+
await step(unbridged, unlessAborted(this.kit.house.deliver('transcripts/keep', { conversation: conversation.id, message: messageId, path, text: said }, () => !signal.aborted), signal));
|
|
461
596
|
blocks.push({ type: 'text', text: `${path}\nTranscript: ${said}` });
|
|
462
597
|
}
|
|
463
598
|
}
|
|
@@ -492,7 +627,11 @@ export class Conversations {
|
|
|
492
627
|
void bridging.then((bridge) => bridge.close(), () => undefined);
|
|
493
628
|
}
|
|
494
629
|
async interrupt(conversation, turnId) {
|
|
495
|
-
if (conversation.
|
|
630
|
+
if ([...conversation.turns].some((turn) => turn.id === turnId))
|
|
631
|
+
await this.cancel(conversation);
|
|
632
|
+
}
|
|
633
|
+
async cancel(conversation) {
|
|
634
|
+
if (conversation.running === null)
|
|
496
635
|
return;
|
|
497
636
|
await conversation.running.adapter.connection.agent.notify('session/cancel', {
|
|
498
637
|
sessionId: conversation.running.sessionId,
|
|
@@ -518,6 +657,9 @@ export class Conversations {
|
|
|
518
657
|
if (running === null)
|
|
519
658
|
return;
|
|
520
659
|
try {
|
|
660
|
+
if (conversation.options?.find(({ id }) => id === option)?.category === 'mode') {
|
|
661
|
+
throw new Error(`conversation ${conversation.id} keeps the mode its Agent's launch settings name`);
|
|
662
|
+
}
|
|
521
663
|
running.reader.requested(option, value);
|
|
522
664
|
const answered = await running.adapter.connection.agent.request('session/set_config_option', {
|
|
523
665
|
sessionId: running.sessionId,
|
|
@@ -548,10 +690,12 @@ export class Conversations {
|
|
|
548
690
|
const route = await routed;
|
|
549
691
|
const reader = readerFor(route.kind);
|
|
550
692
|
reader.session = sessionId;
|
|
693
|
+
conversation.reader = reader;
|
|
551
694
|
const app = client({ name: '@agentshouse/kit' })
|
|
552
|
-
.onRequest('session/request_permission', ({ params, signal }) => this.ask(conversation, 'session/request_permission', params, false, signal))
|
|
553
|
-
.onRequest('elicitation/create', ({ params, signal }) => this.ask(conversation, 'elicitation/create', params, asksSecret(params), signal))
|
|
554
|
-
.onRequest('_x.ai/exit_plan_mode',
|
|
695
|
+
.onRequest('session/request_permission', ({ params, signal }) => conversation.afk ? permissionAllowed(params) : this.ask(conversation, 'session/request_permission', params, false, signal))
|
|
696
|
+
.onRequest('elicitation/create', ({ params, signal }) => conversation.afk ? formAnswered(route.kind, params) : this.ask(conversation, 'elicitation/create', params, asksSecret(params), signal))
|
|
697
|
+
.onRequest('_x.ai/exit_plan_mode', GROK_PLAN, async ({ params, signal }) => conversation.afk ? PLAN_APPROVED : grokPlanAnswer(await this.askForm(conversation, grokPlanForm(params), signal)))
|
|
698
|
+
.onRequest('_x.ai/ask_user_question', GROK_QUESTION, async ({ params, signal }) => conversation.afk ? grokQuestionAnswered(params) : grokQuestionAnswer(await this.askForm(conversation, grokQuestionForm(params), signal)));
|
|
555
699
|
const cli = await unlessAborted(this.kit.agents.cli(route.kind), conversation.killed.signal);
|
|
556
700
|
const bridging = this.bridged(conversation);
|
|
557
701
|
const bridge = await step(unbridged, bridging);
|
|
@@ -560,14 +704,15 @@ export class Conversations {
|
|
|
560
704
|
throw new Error(KILLED);
|
|
561
705
|
}
|
|
562
706
|
conversation.opening = bridge;
|
|
563
|
-
const work = workFor(route.kind, route.working_directory, () => this.kit.changed());
|
|
707
|
+
const work = workFor(route.kind, route.working_directory, () => this.kit.changed(), (message) => this.tapped(conversation, reader, message));
|
|
564
708
|
const scope = new Scope(marker(bridge), randomUUID(), cli);
|
|
565
709
|
let adapter = null;
|
|
566
710
|
try {
|
|
567
|
-
await unlessAborted(createHowWeWork(bridge), conversation.killed.signal);
|
|
568
711
|
bridged(bridge);
|
|
569
712
|
await CLIS[route.kind].allowHouse(bridge.env.PATH);
|
|
570
713
|
const streamed = await step(NO_CONNECTION, work.open(cli));
|
|
714
|
+
if (this.stopping)
|
|
715
|
+
throw new Error(KILLED);
|
|
571
716
|
adapter = await step(NO_START, startAdapter(route.kind, cli, route.working_directory, app, (message) => {
|
|
572
717
|
work.read(message);
|
|
573
718
|
this.read(conversation, reader, message);
|
|
@@ -575,18 +720,17 @@ export class Conversations {
|
|
|
575
720
|
void adapter.exited.then(() => this.closeBridge(conversation, bridging, bridge));
|
|
576
721
|
const agent = adapter.connection.agent;
|
|
577
722
|
const cwd = route.working_directory;
|
|
578
|
-
const _meta = CLIS[route.kind].sessionMeta(bridge.env.HOUSE_BRIDGE
|
|
723
|
+
const _meta = CLIS[route.kind].sessionMeta(bridge.env.HOUSE_BRIDGE);
|
|
579
724
|
const resumed = sessionId === null ? null : await this.resume(conversation, route.kind, adapter, { sessionId, cwd, mcpServers: [], _meta }, empty);
|
|
580
725
|
const opened = resumed ?? (await step(unopened(NO_SESSION), agent.request('session/new', { cwd, mcpServers: [], _meta })));
|
|
581
726
|
reader.session = opened.sessionId;
|
|
582
|
-
if (opened.sessionId !==
|
|
583
|
-
|
|
584
|
-
await step(unopened(NO_SESSION), agent.request('session/prompt', { sessionId: opened.sessionId, prompt }));
|
|
585
|
-
}
|
|
727
|
+
if (opened.sessionId !== conversation.session)
|
|
728
|
+
conversation.state = QUIET;
|
|
586
729
|
const running = { adapter, bridging, bridge, sessionId: opened.sessionId, killed: false, reader, work, scope };
|
|
587
730
|
const options = await step(NO_SETTINGS, this.launchSettings(running, route, opened.configOptions ?? []));
|
|
588
731
|
void work.settled().then(() => scope.record());
|
|
589
732
|
conversation.running = running;
|
|
733
|
+
adapter.turn(conversation.turns.size > 0);
|
|
590
734
|
conversation.session = running.sessionId;
|
|
591
735
|
conversation.options = options;
|
|
592
736
|
this.facts(conversation);
|
|
@@ -653,6 +797,8 @@ export class Conversations {
|
|
|
653
797
|
conversation.running = null;
|
|
654
798
|
conversation.commands = null;
|
|
655
799
|
conversation.options = null;
|
|
800
|
+
conversation.subagents.clear();
|
|
801
|
+
this.stated(conversation, { running: [] });
|
|
656
802
|
for (const [id, question] of this.questions) {
|
|
657
803
|
if (question.conversation === conversation)
|
|
658
804
|
this.questions.delete(id);
|
|
@@ -669,6 +815,16 @@ export class Conversations {
|
|
|
669
815
|
if (conversation.options !== null)
|
|
670
816
|
this.options(conversation, conversation.options);
|
|
671
817
|
}
|
|
818
|
+
stated(conversation, change) {
|
|
819
|
+
conversation.state = { ...conversation.state, ...change };
|
|
820
|
+
this.stateChanged(conversation, false);
|
|
821
|
+
}
|
|
822
|
+
stateChanged(conversation, again) {
|
|
823
|
+
const state = JSON.stringify(conversation.state);
|
|
824
|
+
if (!again && state === conversation.sentState)
|
|
825
|
+
return;
|
|
826
|
+
conversation.sentState = this.kit.send({ type: 'state', conversation_id: conversation.id, state: conversation.state }) ? state : null;
|
|
827
|
+
}
|
|
672
828
|
commands(conversation, commands) {
|
|
673
829
|
conversation.commands = commands;
|
|
674
830
|
if (conversation.running === null)
|
|
@@ -679,11 +835,16 @@ export class Conversations {
|
|
|
679
835
|
if (conversation.running === null)
|
|
680
836
|
return;
|
|
681
837
|
conversation.options = options;
|
|
682
|
-
this.kit.send({ type: 'options', conversation_id: conversation.id, options });
|
|
838
|
+
this.kit.send({ type: 'options', conversation_id: conversation.id, options: options.filter((option) => option.category !== 'mode') });
|
|
839
|
+
const chosen = (category) => {
|
|
840
|
+
const value = options.find((option) => option.category === category)?.currentValue;
|
|
841
|
+
return value === undefined ? null : String(value);
|
|
842
|
+
};
|
|
843
|
+
this.stated(conversation, { model: chosen('model'), effort: chosen('thought_level'), mode: chosen('mode') });
|
|
683
844
|
}
|
|
684
|
-
deliver(conversation,
|
|
845
|
+
deliver(conversation, act, request, current) {
|
|
685
846
|
this.reporting++;
|
|
686
|
-
const delivered = conversation.reports.then(async () => this.kit.house.deliver(
|
|
847
|
+
const delivered = conversation.reports.then(async () => this.kit.house.deliver(act, await request, current));
|
|
687
848
|
conversation.reports = delivered
|
|
688
849
|
.catch(() => undefined)
|
|
689
850
|
.finally(() => {
|
|
@@ -695,21 +856,26 @@ export class Conversations {
|
|
|
695
856
|
write(conversation, turn, operation) {
|
|
696
857
|
if (turn.ended || !this.watched)
|
|
697
858
|
return;
|
|
698
|
-
this.kit.send({ type: 'turn', conversation_id: conversation.id, turn_id: turn.id,
|
|
859
|
+
this.kit.send({ type: 'turn', conversation_id: conversation.id, turn_id: turn.id, operation });
|
|
699
860
|
}
|
|
700
861
|
replay(conversation, turn) {
|
|
701
|
-
for (const operation of turn.
|
|
862
|
+
for (const operation of turn.content.operations())
|
|
702
863
|
this.write(conversation, turn, operation);
|
|
703
864
|
}
|
|
704
865
|
begin(conversation) {
|
|
705
866
|
const turn = new Turn((operation) => this.write(conversation, turn, operation));
|
|
706
|
-
conversation.
|
|
707
|
-
|
|
867
|
+
conversation.turns.add(turn);
|
|
868
|
+
conversation.running?.adapter.turn(true);
|
|
869
|
+
void this.deliver(conversation, 'turns/start', turnOf(conversation, turn))
|
|
870
|
+
.then((answer) => {
|
|
871
|
+
if (answer.run === true)
|
|
872
|
+
turn.quietUntil = Date.now() + ACTIVITY_REPORT_MS;
|
|
873
|
+
})
|
|
874
|
+
.catch(logged);
|
|
708
875
|
conversation.turn = turn;
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
turn.parts.start(part);
|
|
876
|
+
this.stated(conversation, { suggestion: null });
|
|
877
|
+
for (const event of conversation.reader?.began() ?? [])
|
|
878
|
+
this.event(conversation, event);
|
|
713
879
|
this.kit.changed();
|
|
714
880
|
return turn;
|
|
715
881
|
}
|
|
@@ -718,22 +884,25 @@ export class Conversations {
|
|
|
718
884
|
return;
|
|
719
885
|
if (failed !== null)
|
|
720
886
|
logged(new Error(`conversation ${conversation.id} turn ${turn.id} failed: ${failed.cause}`));
|
|
721
|
-
if (conversation.turn === turn)
|
|
722
|
-
|
|
723
|
-
this.apply(conversation, turn, event);
|
|
724
|
-
}
|
|
887
|
+
if (conversation.turn === turn)
|
|
888
|
+
conversation.reader?.closeTurn();
|
|
725
889
|
if (notice !== null)
|
|
726
|
-
turn.
|
|
890
|
+
turn.content.add({ type: 'notice', kind: 'error', text: notice });
|
|
727
891
|
turn.ended = true;
|
|
728
|
-
conversation.
|
|
892
|
+
conversation.turns.delete(turn);
|
|
893
|
+
conversation.running?.adapter.turn(conversation.turns.size > 0);
|
|
729
894
|
if (conversation.turn === turn)
|
|
730
895
|
conversation.turn = null;
|
|
896
|
+
const tokens = conversation.tokens;
|
|
897
|
+
conversation.tokens = null;
|
|
898
|
+
if (tokens !== null)
|
|
899
|
+
this.stated(conversation, { tokens: addedTokens(conversation.state.tokens, tokens) });
|
|
731
900
|
const body = Promise.allSettled(turn.uploads).then(() => ({
|
|
732
|
-
|
|
733
|
-
|
|
901
|
+
...turnOf(conversation, turn),
|
|
902
|
+
content: { parts: turn.content.parts, tokens, state: conversation.state },
|
|
734
903
|
...(failed === null ? {} : { failed: failed.failure }),
|
|
735
904
|
}));
|
|
736
|
-
void this.deliver(conversation,
|
|
905
|
+
void this.deliver(conversation, 'turns/end', body).catch(logged);
|
|
737
906
|
this.next(conversation);
|
|
738
907
|
this.kit.changed();
|
|
739
908
|
}
|
|
@@ -758,11 +927,11 @@ export class Conversations {
|
|
|
758
927
|
}
|
|
759
928
|
else {
|
|
760
929
|
this.took(turn);
|
|
761
|
-
if (conversation.
|
|
930
|
+
if (conversation.turns.size === 1 && conversation.turns.has(turn) && conversation.running === running) {
|
|
762
931
|
running.adapter.retire();
|
|
763
932
|
this.detach(conversation, running);
|
|
764
933
|
}
|
|
765
|
-
this.end(conversation, turn, answeredWith(error));
|
|
934
|
+
this.end(conversation, turn, answeredWith(error), cliSaid(error));
|
|
766
935
|
}
|
|
767
936
|
});
|
|
768
937
|
}
|
|
@@ -771,19 +940,24 @@ export class Conversations {
|
|
|
771
940
|
this.took(turn);
|
|
772
941
|
return turn;
|
|
773
942
|
}
|
|
774
|
-
ended(conversation) {
|
|
943
|
+
ended(conversation, stop) {
|
|
775
944
|
const turn = conversation.turn;
|
|
776
945
|
if (turn === null)
|
|
777
946
|
return;
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
this.apply(conversation, turn, event);
|
|
784
|
-
conversation.turn = null;
|
|
785
|
-
this.kit.changed();
|
|
947
|
+
const held = stop !== null && !turn.steered && [...conversation.subagents].some((key) => turn.content.holds(key));
|
|
948
|
+
if (!turn.prompted || held) {
|
|
949
|
+
this.took(turn);
|
|
950
|
+
this.end(conversation, turn, stop === 'refusal' ? { failure: 'refused', cause: REFUSED } : null);
|
|
951
|
+
return;
|
|
786
952
|
}
|
|
953
|
+
if (stop !== null)
|
|
954
|
+
return;
|
|
955
|
+
conversation.reader?.closeTurn();
|
|
956
|
+
conversation.turn = null;
|
|
957
|
+
this.kit.changed();
|
|
958
|
+
}
|
|
959
|
+
askForm(conversation, form, signal) {
|
|
960
|
+
return this.ask(conversation, 'elicitation/create', form, false, signal);
|
|
787
961
|
}
|
|
788
962
|
async ask(conversation, method, params, secret, signal) {
|
|
789
963
|
const turn = this.started(conversation);
|
|
@@ -791,7 +965,8 @@ export class Conversations {
|
|
|
791
965
|
const answered = new Promise((answer, fail) => {
|
|
792
966
|
this.questions.set(interactionId, { conversation, answer: (response) => answer(response) });
|
|
793
967
|
signal.addEventListener('abort', () => answer(null), { once: true });
|
|
794
|
-
this.deliver(conversation,
|
|
968
|
+
this.deliver(conversation, 'conversations/interaction/ask', {
|
|
969
|
+
...turnOf(conversation, turn),
|
|
795
970
|
interaction_id: interactionId,
|
|
796
971
|
request: { method, params },
|
|
797
972
|
secret,
|
|
@@ -832,6 +1007,22 @@ export class Conversations {
|
|
|
832
1007
|
read(conversation, reader, message) {
|
|
833
1008
|
for (const event of reader.read(message))
|
|
834
1009
|
this.event(conversation, event);
|
|
1010
|
+
this.reportActivity(conversation);
|
|
1011
|
+
}
|
|
1012
|
+
tapped(conversation, reader, message) {
|
|
1013
|
+
for (const event of reader.tap(message))
|
|
1014
|
+
this.event(conversation, event);
|
|
1015
|
+
}
|
|
1016
|
+
reportActivity(conversation) {
|
|
1017
|
+
const turn = conversation.turn;
|
|
1018
|
+
if (turn === null || Date.now() < turn.quietUntil)
|
|
1019
|
+
return;
|
|
1020
|
+
turn.quietUntil = Infinity;
|
|
1021
|
+
void this.deliver(conversation, 'turns/activity/report', turnOf(conversation, turn), () => false)
|
|
1022
|
+
.catch(logged)
|
|
1023
|
+
.finally(() => {
|
|
1024
|
+
turn.quietUntil = Date.now() + ACTIVITY_REPORT_MS;
|
|
1025
|
+
});
|
|
835
1026
|
}
|
|
836
1027
|
event(conversation, event) {
|
|
837
1028
|
switch (event.kind) {
|
|
@@ -839,7 +1030,7 @@ export class Conversations {
|
|
|
839
1030
|
this.started(conversation);
|
|
840
1031
|
return;
|
|
841
1032
|
case 'ended':
|
|
842
|
-
this.ended(conversation);
|
|
1033
|
+
this.ended(conversation, event.stop);
|
|
843
1034
|
return;
|
|
844
1035
|
case 'commands':
|
|
845
1036
|
this.commands(conversation, event.commands);
|
|
@@ -847,20 +1038,20 @@ export class Conversations {
|
|
|
847
1038
|
case 'options':
|
|
848
1039
|
this.options(conversation, event.options);
|
|
849
1040
|
return;
|
|
850
|
-
case '
|
|
851
|
-
conversation
|
|
852
|
-
conversation.turn?.parts.measure(conversation.context);
|
|
1041
|
+
case 'state':
|
|
1042
|
+
this.stated(conversation, event.state);
|
|
853
1043
|
return;
|
|
854
|
-
case '
|
|
855
|
-
|
|
1044
|
+
case 'tokens':
|
|
1045
|
+
conversation.tokens = addedTokens(conversation.tokens, event.tokens);
|
|
856
1046
|
return;
|
|
857
|
-
case '
|
|
858
|
-
|
|
859
|
-
if (conversation.turn !== null)
|
|
860
|
-
break;
|
|
861
|
-
if (event.kind === 'marker' && event.marker === 'job')
|
|
862
|
-
conversation.held.push({ type: 'marker', marker: 'job', text: event.text });
|
|
1047
|
+
case 'call':
|
|
1048
|
+
this.called(conversation, event);
|
|
863
1049
|
return;
|
|
1050
|
+
case 'notice':
|
|
1051
|
+
case 'image':
|
|
1052
|
+
if (conversation.turn === null)
|
|
1053
|
+
return;
|
|
1054
|
+
break;
|
|
864
1055
|
}
|
|
865
1056
|
this.apply(conversation, this.started(conversation), event);
|
|
866
1057
|
}
|
|
@@ -872,67 +1063,60 @@ export class Conversations {
|
|
|
872
1063
|
apply(conversation, turn, event) {
|
|
873
1064
|
switch (event.kind) {
|
|
874
1065
|
case 'text':
|
|
875
|
-
|
|
876
|
-
turn.parts.write(event.kind, event.id, event.text);
|
|
877
|
-
return;
|
|
878
|
-
case 'command':
|
|
879
|
-
turn.parts.command();
|
|
880
|
-
return;
|
|
881
|
-
case 'subagent': {
|
|
882
|
-
const index = turn.parts.start({ type: 'subagent', description: event.description, status: 'running', result: null });
|
|
883
|
-
conversation.subagents.set(event.id, { description: event.description, turn, index });
|
|
884
|
-
return;
|
|
885
|
-
}
|
|
886
|
-
case 'message':
|
|
887
|
-
turn.parts.start({ type: 'message', command: event.command, target: event.target });
|
|
1066
|
+
turn.content.write(event.id, event.text);
|
|
888
1067
|
return;
|
|
889
|
-
case '
|
|
890
|
-
turn.
|
|
891
|
-
return;
|
|
892
|
-
case 'plan':
|
|
893
|
-
turn.parts.plan(event.entries);
|
|
1068
|
+
case 'notice':
|
|
1069
|
+
turn.content.add({ type: 'notice', kind: event.notice, text: event.text });
|
|
894
1070
|
return;
|
|
895
1071
|
case 'image':
|
|
896
|
-
turn.uploads.push(this.image(conversation, turn,
|
|
1072
|
+
turn.uploads.push(this.image(conversation, turn, event.data, event.mediaType));
|
|
897
1073
|
this.kit.changed();
|
|
898
1074
|
return;
|
|
899
1075
|
}
|
|
900
1076
|
}
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
if (
|
|
1077
|
+
// A sub-agent's calls count only in the turn that holds the call that started it, and a call's later changes only in the turn that counted it.
|
|
1078
|
+
called(conversation, event) {
|
|
1079
|
+
if (event.running === true)
|
|
1080
|
+
conversation.subagents.add(event.key);
|
|
1081
|
+
if (event.running === false)
|
|
1082
|
+
conversation.subagents.delete(event.key);
|
|
1083
|
+
const turn = conversation.turn;
|
|
1084
|
+
if (!event.first) {
|
|
1085
|
+
if (turn?.content.holds(event.key))
|
|
1086
|
+
turn.content.call(event.key, event.parent, event.call);
|
|
904
1087
|
return;
|
|
905
|
-
|
|
906
|
-
if (
|
|
907
|
-
|
|
1088
|
+
}
|
|
1089
|
+
if (event.parent !== null) {
|
|
1090
|
+
turn?.content.call(event.key, event.parent, event.call);
|
|
908
1091
|
return;
|
|
909
1092
|
}
|
|
910
|
-
|
|
911
|
-
if (conversation.turn === null)
|
|
912
|
-
conversation.held.push(part);
|
|
913
|
-
else
|
|
914
|
-
conversation.turn.parts.start(part);
|
|
1093
|
+
this.started(conversation).content.call(event.key, null, event.call);
|
|
915
1094
|
}
|
|
916
|
-
async image(conversation, turn,
|
|
1095
|
+
async image(conversation, turn, data, mediaType) {
|
|
917
1096
|
const bytes = Buffer.from(data, 'base64');
|
|
918
1097
|
const name = `image-${++turn.images}.${mediaType.split('/')[1]}`;
|
|
1098
|
+
const sha256 = createHash('sha256').update(bytes).digest('hex');
|
|
919
1099
|
try {
|
|
920
|
-
const declared = (await this.deliver(conversation,
|
|
1100
|
+
const declared = (await this.deliver(conversation, 'turns/images/declare', {
|
|
1101
|
+
...turnOf(conversation, turn),
|
|
921
1102
|
name,
|
|
922
1103
|
media_type: mediaType,
|
|
923
1104
|
bytes: bytes.length,
|
|
924
|
-
sha256
|
|
1105
|
+
sha256,
|
|
925
1106
|
}));
|
|
926
1107
|
const saved = await uploaded(declared.upload, {
|
|
1108
|
+
name,
|
|
927
1109
|
bytes: bytes.length,
|
|
928
|
-
|
|
929
|
-
|
|
1110
|
+
sha256,
|
|
1111
|
+
read: (start, end) => [bytes.subarray(start, end)],
|
|
1112
|
+
send: (url, start, end, headers) => sentPart(url, bytes.subarray(start, end), headers),
|
|
1113
|
+
finish: (upload, partSha256) => this.kit.house.deliver('uploads/finish', { upload, part_sha256: partSha256 }),
|
|
930
1114
|
});
|
|
931
|
-
turn.
|
|
1115
|
+
turn.content.arrived({ type: 'image', file: saved.path, name, media_type: mediaType });
|
|
932
1116
|
}
|
|
933
1117
|
catch (error) {
|
|
934
1118
|
const cause = error instanceof HouseRefusal ? (refusalOf(error.text)?.text ?? causeOf(error)) : causeOf(error);
|
|
935
|
-
turn.
|
|
1119
|
+
turn.content.arrived({ type: 'notice', kind: 'error', text: `${name} was not saved: ${cause}` });
|
|
936
1120
|
}
|
|
937
1121
|
}
|
|
938
1122
|
}
|