@agentshouse/kit 0.2.1-cd9cbd8b6428c → 0.2.1-ce0af1aee9a57
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 +46 -46
- package/dist/acp.js +102 -108
- package/dist/afk.js +17 -0
- package/dist/agents.js +218 -63
- package/dist/api.js +34 -2
- package/dist/app-git.js +48 -0
- package/dist/authority-main.js +3 -3
- package/dist/bridge.js +42 -62
- package/dist/building.js +50 -0
- package/dist/calls.js +71 -0
- package/dist/chunks.js +49 -0
- package/dist/clis.js +169 -95
- package/dist/codex-main.js +58 -0
- package/dist/connection.js +120 -0
- package/dist/content.js +63 -0
- package/dist/conversations.js +853 -333
- package/dist/copies.js +451 -251
- package/dist/end.js +33 -0
- package/dist/enrol-main.js +7 -1
- package/dist/events.js +1057 -0
- package/dist/files.js +139 -17
- package/dist/git.js +57 -21
- package/dist/grok.js +39 -0
- package/dist/home.js +89 -7
- package/dist/house-cli.js +299 -0
- package/dist/house-main.js +7 -159
- package/dist/instructions.js +7 -11
- package/dist/kit-cli.js +65 -0
- package/dist/kit-main.js +8 -19
- package/dist/login.js +117 -102
- package/dist/logout.js +14 -13
- package/dist/mcp.js +6 -2
- package/dist/placement.js +31 -0
- package/dist/refusals.js +12 -0
- package/dist/relay.js +33 -2
- package/dist/resident.js +138 -61
- package/dist/scope.js +214 -0
- package/dist/scripts.js +255 -0
- package/dist/secret-input.js +5 -2
- package/dist/service-main.js +9 -0
- package/dist/service.js +55 -0
- package/dist/shell.js +3 -0
- package/dist/sign-in.js +40 -32
- package/dist/silence.js +28 -0
- package/dist/skills.js +49 -35
- package/dist/transcription.js +131 -40
- package/dist/translate.js +19 -16
- package/dist/update.js +113 -103
- package/dist/wire.js +40 -0
- package/dist/work.js +353 -0
- package/package.json +22 -20
- package/dist/blocks.js +0 -51
- package/dist/bootstrap.json +0 -24
- package/dist/clis-main.js +0 -23
- package/dist/stream.js +0 -47
- package/how-we-work.md +0 -23
- package/skills/ask-matt/LICENSE +0 -21
- package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
- package/skills/ask-matt/SKILL.md +0 -91
- package/skills/ask-matt/agents/openai.yaml +0 -4
- 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 -116
- package/skills/codebase-design/agents/openai.yaml +0 -3
- package/skills/diagnosing-bugs/LICENSE +0 -21
- package/skills/diagnosing-bugs/SKILL.md +0 -140
- 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/CONTEXT-FORMAT.md +0 -60
- package/skills/domain-modeling/LICENSE +0 -21
- package/skills/domain-modeling/SKILL.md +0 -76
- package/skills/domain-modeling/agents/openai.yaml +0 -3
- package/skills/grill-me/LICENSE +0 -21
- package/skills/grill-me/SKILL.md +0 -8
- package/skills/grill-me/agents/openai.yaml +0 -4
- package/skills/grill-with-docs/LICENSE +0 -21
- package/skills/grill-with-docs/SKILL.md +0 -8
- package/skills/grill-with-docs/agents/openai.yaml +0 -4
- 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 -17
- package/skills/handoff/agents/openai.yaml +0 -4
- package/skills/implement/LICENSE +0 -21
- package/skills/implement/SKILL.md +0 -16
- package/skills/implement/agents/openai.yaml +0 -4
- 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 -72
- package/skills/improve-codebase-architecture/agents/openai.yaml +0 -4
- package/skills/prototype/LICENSE +0 -21
- package/skills/prototype/LOGIC.md +0 -67
- package/skills/prototype/SKILL.md +0 -28
- 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 -14
- package/skills/research/agents/openai.yaml +0 -3
- package/skills/resolving-merge-conflicts/LICENSE +0 -21
- package/skills/resolving-merge-conflicts/SKILL.md +0 -16
- package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
- package/skills/tdd/LICENSE +0 -21
- package/skills/tdd/SKILL.md +0 -40
- 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 -141
- package/skills/teach/agents/openai.yaml +0 -4
- package/skills/to-questionnaire/LICENSE +0 -21
- package/skills/to-questionnaire/SKILL.md +0 -55
- package/skills/to-questionnaire/agents/openai.yaml +0 -4
- package/skills/to-spec/LICENSE +0 -21
- package/skills/to-spec/SKILL.md +0 -76
- package/skills/to-spec/agents/openai.yaml +0 -4
- package/skills/to-tickets/LICENSE +0 -21
- package/skills/to-tickets/SKILL.md +0 -106
- package/skills/to-tickets/agents/openai.yaml +0 -4
- package/skills/wait-what/LICENSE +0 -21
- package/skills/wait-what/SKILL.md +0 -8
- package/skills/wait-what/agents/openai.yaml +0 -4
- package/skills/wayfinder/LICENSE +0 -21
- package/skills/wayfinder/SKILL.md +0 -129
- package/skills/wayfinder/agents/openai.yaml +0 -4
- 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 -83
- package/skills/writing-for-agents/agents/openai.yaml +0 -3
package/dist/conversations.js
CHANGED
|
@@ -1,63 +1,202 @@
|
|
|
1
1
|
import { client, } from '@agentclientprotocol/sdk';
|
|
2
|
-
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
3
3
|
import { mkdir } from 'node:fs/promises';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
5
|
import { unlessAborted } from './abort.js';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
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
|
-
import {
|
|
11
|
+
import { Content } from './content.js';
|
|
12
|
+
import { answeredFailure, readerFor } from './events.js';
|
|
13
|
+
import { inspectedFile, linkedPaths, storeRefusal, uploaded } from './files.js';
|
|
14
|
+
import { grokPlanAnswer, grokPlanForm, grokQuestionAnswer, grokQuestionForm } from './grok.js';
|
|
11
15
|
import { agentBase } from './home.js';
|
|
12
16
|
import { instructions } from './instructions.js';
|
|
17
|
+
import { refusalOf } from './refusals.js';
|
|
18
|
+
import { relayed } from './relay.js';
|
|
19
|
+
import { killMarked, Scope } from './scope.js';
|
|
13
20
|
import { holdSecretInput } from './secret-input.js';
|
|
14
|
-
import { createHowWeWork } from './skills.js';
|
|
15
21
|
import { transcribe } from './transcription.js';
|
|
22
|
+
import { workFor } from './work.js';
|
|
16
23
|
const KILLED = 'the conversation was killed';
|
|
24
|
+
const INTERRUPTED = 'the conversation was interrupted';
|
|
25
|
+
const REFUSED = 'the CLI refused the prompt';
|
|
26
|
+
const NO_CONNECTION = "the Agent's computer could not open this conversation; try again";
|
|
27
|
+
const GONE = 'House no longer holds this conversation; start a new one';
|
|
28
|
+
const RETIRED = "this conversation's Agent is retired, so it cannot start";
|
|
29
|
+
const ENDED = {
|
|
30
|
+
conversation_not_found: GONE,
|
|
31
|
+
conversation_historical: RETIRED,
|
|
32
|
+
};
|
|
33
|
+
const NO_START = "the Agent's CLI did not start";
|
|
34
|
+
const NO_SESSION = "the Agent's CLI did not open a session";
|
|
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";
|
|
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: [],
|
|
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;
|
|
59
|
+
class NotStarted extends Error {
|
|
60
|
+
}
|
|
61
|
+
class ResumeRefused extends NotStarted {
|
|
62
|
+
}
|
|
63
|
+
async function step(reason, work) {
|
|
64
|
+
try {
|
|
65
|
+
return await work;
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
throw new NotStarted(typeof reason === 'string' ? reason : reason(error), { cause: error });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function unopened(reason) {
|
|
72
|
+
return (error) => (signedOut(error) ? NOT_SIGNED_IN : reason);
|
|
73
|
+
}
|
|
74
|
+
// A start-up step is shown only once it has lasted a second, so a quick start shows none of them.
|
|
75
|
+
const STARTING_SHOWN_AFTER_MS = 1_000;
|
|
76
|
+
function unbridged(error) {
|
|
77
|
+
return (finalRefusal(error) ? ENDED[refusalOf(error.text)?.code ?? ''] : undefined) ?? NO_CONNECTION;
|
|
78
|
+
}
|
|
79
|
+
const GROK_PLAN = {
|
|
80
|
+
parse(value) {
|
|
81
|
+
return value;
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
const GROK_QUESTION = {
|
|
85
|
+
parse(value) {
|
|
86
|
+
return value;
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
export const CONVERSATION_METHODS = new Set(['open', 'message', 'take_out', 'interrupt', 'kill', 'done', 'option', 'answer']);
|
|
90
|
+
function addedTokens(sum, tokens) {
|
|
91
|
+
if (sum === null)
|
|
92
|
+
return tokens;
|
|
93
|
+
return {
|
|
94
|
+
input: sum.input + tokens.input,
|
|
95
|
+
output: sum.output + tokens.output,
|
|
96
|
+
cache_read: sum.cache_read + tokens.cache_read,
|
|
97
|
+
cache_write: sum.cache_write + tokens.cache_write,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function workRuns(part) {
|
|
101
|
+
return part.type === 'subagent' || part.type === 'background' ? part.status === 'running' : null;
|
|
102
|
+
}
|
|
103
|
+
function cliSaid(error) {
|
|
104
|
+
const details = error?.data?.details;
|
|
105
|
+
return typeof details === 'string' && details !== '' ? `${causeOf(error)}: ${details}` : causeOf(error);
|
|
106
|
+
}
|
|
17
107
|
function causeOf(error) {
|
|
18
108
|
return error instanceof Error ? error.message : String(error);
|
|
19
109
|
}
|
|
110
|
+
function answeredWith(error) {
|
|
111
|
+
return { failure: signedOut(error) ? 'signed_out' : 'error', cause: causeOf(error) };
|
|
112
|
+
}
|
|
113
|
+
function exitedWith(running, cause) {
|
|
114
|
+
if (running.killed)
|
|
115
|
+
return { failure: 'killed', cause: KILLED };
|
|
116
|
+
return { failure: running.adapter.silent() ? 'silent' : 'exited', cause };
|
|
117
|
+
}
|
|
20
118
|
function answerMessage(request, response) {
|
|
21
119
|
return `The User answered a question you asked earlier.\nQuestion: ${JSON.stringify(request)}\nAnswer: ${JSON.stringify(response)}`;
|
|
22
120
|
}
|
|
121
|
+
async function sentPart(url, bytes, headers) {
|
|
122
|
+
const answer = await fetch(relayed(url), { method: 'PUT', headers, body: bytes });
|
|
123
|
+
await answer.arrayBuffer();
|
|
124
|
+
if (!answer.ok)
|
|
125
|
+
throw storeRefusal('upload', answer.status);
|
|
126
|
+
}
|
|
23
127
|
function logged(error) {
|
|
24
128
|
process.stderr.write(`kit: ${causeOf(error)}\n`);
|
|
25
129
|
}
|
|
26
|
-
function
|
|
27
|
-
|
|
130
|
+
function refusal(conversation, error) {
|
|
131
|
+
if (conversation.kills > 0)
|
|
132
|
+
return KILLED;
|
|
133
|
+
const cause = error instanceof Error && error.cause !== undefined ? `${error.message}: ${causeOf(error.cause)}` : causeOf(error);
|
|
134
|
+
logged(new Error(`conversation ${conversation.id} did not start: ${cause}`));
|
|
135
|
+
return causeOf(error);
|
|
136
|
+
}
|
|
137
|
+
function audioOf(result) {
|
|
138
|
+
if (result.isError === true)
|
|
139
|
+
return null;
|
|
140
|
+
const file = inspectedFile(result.content.map((content) => content.text).join('\n'));
|
|
141
|
+
return file?.media_type?.startsWith('audio/') === true ? file : null;
|
|
142
|
+
}
|
|
143
|
+
function marker(bridge) {
|
|
144
|
+
return `HOUSE_BRIDGE=${bridge.env.HOUSE_BRIDGE}`;
|
|
145
|
+
}
|
|
146
|
+
function steers(adapter) {
|
|
147
|
+
return adapter.initialized._meta?.steering?.supported === true;
|
|
28
148
|
}
|
|
29
149
|
class Turn {
|
|
30
150
|
id = randomUUID();
|
|
31
|
-
|
|
32
|
-
segment = null;
|
|
33
|
-
admitted = false;
|
|
34
|
-
notes = [];
|
|
35
|
-
sent = [];
|
|
36
|
-
unsentFrom = null;
|
|
37
|
-
draftSequence = 0;
|
|
38
|
-
planSequence = 0;
|
|
39
|
-
entries = null;
|
|
151
|
+
content;
|
|
40
152
|
questions = 0;
|
|
41
153
|
secrets = 0;
|
|
42
|
-
|
|
154
|
+
prompted = false;
|
|
155
|
+
steered = false;
|
|
156
|
+
taken = null;
|
|
43
157
|
ended = false;
|
|
158
|
+
quietUntil = Infinity;
|
|
159
|
+
images = 0;
|
|
160
|
+
uploads = [];
|
|
161
|
+
constructor(emit) {
|
|
162
|
+
this.content = new Content(emit);
|
|
163
|
+
}
|
|
44
164
|
}
|
|
45
165
|
class Conversation {
|
|
46
166
|
id;
|
|
47
167
|
running = null;
|
|
168
|
+
session = null;
|
|
48
169
|
turn = null;
|
|
49
170
|
commands = null;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
171
|
+
options = null;
|
|
172
|
+
exitUntold = false;
|
|
173
|
+
afk = false;
|
|
174
|
+
reader = null;
|
|
175
|
+
state = QUIET;
|
|
176
|
+
sentState = null;
|
|
177
|
+
tokens = null;
|
|
178
|
+
leftovers = new Set();
|
|
179
|
+
waiting = [];
|
|
180
|
+
done = false;
|
|
181
|
+
sending = null;
|
|
182
|
+
work = new Map();
|
|
183
|
+
held = [];
|
|
53
184
|
opening = null;
|
|
185
|
+
bridge = null;
|
|
186
|
+
launch = null;
|
|
187
|
+
prompted = null;
|
|
54
188
|
kills = 0;
|
|
55
189
|
killed = new AbortController();
|
|
56
190
|
interrupts = 0;
|
|
57
191
|
interrupted = new AbortController();
|
|
58
|
-
|
|
192
|
+
turns = new Set();
|
|
59
193
|
queue = Promise.resolve();
|
|
60
194
|
reports = Promise.resolve();
|
|
195
|
+
starting = null;
|
|
196
|
+
preparing = null;
|
|
197
|
+
told = null;
|
|
198
|
+
untold = false;
|
|
199
|
+
shown = null;
|
|
61
200
|
constructor(id) {
|
|
62
201
|
this.id = id;
|
|
63
202
|
}
|
|
@@ -73,55 +212,92 @@ function asksSecret(request) {
|
|
|
73
212
|
export class Conversations {
|
|
74
213
|
kit;
|
|
75
214
|
conversations = new Map();
|
|
76
|
-
acks = new Map();
|
|
77
215
|
questions = new Map();
|
|
78
|
-
carrying = 0;
|
|
79
216
|
reporting = 0;
|
|
217
|
+
watched = false;
|
|
218
|
+
stopping = false;
|
|
80
219
|
constructor(kit) {
|
|
81
220
|
this.kit = kit;
|
|
221
|
+
// A process or a durable schedule ends without a report, so the Kit looks at both again every two seconds.
|
|
222
|
+
setInterval(() => this.look(), 2_000);
|
|
82
223
|
}
|
|
83
|
-
|
|
84
|
-
this.
|
|
85
|
-
|
|
86
|
-
(
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
})();
|
|
102
|
-
carried
|
|
103
|
-
.then((body) => this.kit.house.deliver(`/kit/inputs/${input.input_id}/ack`, body))
|
|
104
|
-
.catch(logged)
|
|
105
|
-
.finally(() => {
|
|
106
|
-
this.carrying--;
|
|
107
|
-
this.kit.changed();
|
|
224
|
+
call(call) {
|
|
225
|
+
void this.kit.calls.carry(call.input_id, () => {
|
|
226
|
+
const conversation = this.conversation(call.conversation_id);
|
|
227
|
+
if (call.kind === 'open' || call.kind === 'message')
|
|
228
|
+
conversation.afk = call.afk === true;
|
|
229
|
+
if (call.kind === 'kill') {
|
|
230
|
+
conversation.kills++;
|
|
231
|
+
conversation.killed.abort(new Error(KILLED));
|
|
232
|
+
for (const waiting of conversation.waiting.splice(0))
|
|
233
|
+
waiting.settle({ refused: KILLED });
|
|
234
|
+
this.halt(conversation);
|
|
235
|
+
}
|
|
236
|
+
if (call.kind === 'interrupt') {
|
|
237
|
+
conversation.interrupts++;
|
|
238
|
+
conversation.interrupted.abort();
|
|
239
|
+
}
|
|
240
|
+
const handled = conversation.queue.then(() => this.carry(conversation, call));
|
|
241
|
+
conversation.queue = handled.catch(() => undefined);
|
|
242
|
+
return handled.then(async ({ result }) => ({ result: await result }));
|
|
108
243
|
});
|
|
109
244
|
}
|
|
110
245
|
idle() {
|
|
111
|
-
if (this.
|
|
246
|
+
if (this.reporting > 0)
|
|
112
247
|
return false;
|
|
113
248
|
for (const conversation of this.conversations.values()) {
|
|
114
249
|
const turn = conversation.turn;
|
|
115
250
|
const asking = turn !== null && turn.questions > 0 && turn.secrets === 0;
|
|
116
|
-
if (conversation.
|
|
251
|
+
if (conversation.turns.size > (asking ? 1 : 0))
|
|
117
252
|
return false;
|
|
118
253
|
}
|
|
119
|
-
|
|
254
|
+
const conversations = [...this.conversations.values()];
|
|
255
|
+
if (conversations.some((conversation) => conversation.running?.work.busy()))
|
|
256
|
+
return false;
|
|
257
|
+
return !conversations.some((conversation) => conversation.running?.scope.working() || [...conversation.leftovers].some((scope) => scope.working()));
|
|
258
|
+
}
|
|
259
|
+
quiet() {
|
|
260
|
+
return this.idle() && [...this.conversations.values()].every((conversation) => conversation.turns.size === 0);
|
|
261
|
+
}
|
|
262
|
+
look() {
|
|
263
|
+
for (const conversation of this.conversations.values()) {
|
|
264
|
+
for (const scope of conversation.leftovers) {
|
|
265
|
+
if (!scope.empty())
|
|
266
|
+
continue;
|
|
267
|
+
scope.close();
|
|
268
|
+
conversation.leftovers.delete(scope);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
this.kit.changed();
|
|
272
|
+
}
|
|
273
|
+
stopped() {
|
|
274
|
+
this.stopping = true;
|
|
275
|
+
for (const conversation of this.conversations.values()) {
|
|
276
|
+
if (conversation.running !== null)
|
|
277
|
+
this.process(conversation, false);
|
|
278
|
+
}
|
|
120
279
|
}
|
|
121
280
|
opened() {
|
|
281
|
+
this.watched = false;
|
|
282
|
+
for (const conversation of this.conversations.values()) {
|
|
283
|
+
if (conversation.running !== null) {
|
|
284
|
+
this.facts(conversation);
|
|
285
|
+
this.stateChanged(conversation, true);
|
|
286
|
+
}
|
|
287
|
+
else if (conversation.exitUntold)
|
|
288
|
+
this.process(conversation, false);
|
|
289
|
+
if (conversation.untold || conversation.told !== null)
|
|
290
|
+
this.retell(conversation);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
watching(watched) {
|
|
294
|
+
const resend = watched && !this.watched;
|
|
295
|
+
this.watched = watched;
|
|
296
|
+
if (!resend)
|
|
297
|
+
return;
|
|
122
298
|
for (const conversation of this.conversations.values()) {
|
|
123
299
|
if (conversation.turn !== null)
|
|
124
|
-
conversation.turn
|
|
300
|
+
this.replay(conversation, conversation.turn);
|
|
125
301
|
}
|
|
126
302
|
}
|
|
127
303
|
conversation(id) {
|
|
@@ -132,96 +308,329 @@ export class Conversations {
|
|
|
132
308
|
}
|
|
133
309
|
return conversation;
|
|
134
310
|
}
|
|
135
|
-
async carry(conversation,
|
|
136
|
-
if (
|
|
137
|
-
return this.open(conversation,
|
|
138
|
-
if (
|
|
139
|
-
return this.message(conversation,
|
|
140
|
-
if (
|
|
311
|
+
async carry(conversation, call) {
|
|
312
|
+
if (call.kind === 'open')
|
|
313
|
+
return this.open(conversation, call);
|
|
314
|
+
if (call.kind === 'message')
|
|
315
|
+
return this.message(conversation, call);
|
|
316
|
+
if (call.kind === 'done') {
|
|
317
|
+
conversation.done = true;
|
|
318
|
+
this.next(conversation);
|
|
319
|
+
return { result: Promise.resolve({}) };
|
|
320
|
+
}
|
|
321
|
+
if (call.kind === 'take_out')
|
|
322
|
+
return { result: Promise.resolve(this.takeOut(conversation, String(call.message_id))) };
|
|
323
|
+
await this.launched(conversation);
|
|
324
|
+
if (call.kind === 'interrupt') {
|
|
141
325
|
if (--conversation.interrupts === 0)
|
|
142
326
|
conversation.interrupted = new AbortController();
|
|
143
|
-
await this.interrupt(conversation, String(
|
|
327
|
+
await this.interrupt(conversation, String(call.turn_id));
|
|
144
328
|
}
|
|
145
|
-
if (
|
|
329
|
+
if (call.kind === 'kill') {
|
|
146
330
|
await this.kill(conversation);
|
|
331
|
+
await conversation.sending;
|
|
147
332
|
if (--conversation.kills === 0)
|
|
148
333
|
conversation.killed = new AbortController();
|
|
149
334
|
}
|
|
150
|
-
if (
|
|
151
|
-
await this.option(conversation, String(
|
|
152
|
-
if (
|
|
153
|
-
const question = this.questions.get(String(
|
|
335
|
+
if (call.kind === 'option')
|
|
336
|
+
await this.option(conversation, String(call.option), call.value);
|
|
337
|
+
if (call.kind === 'answer') {
|
|
338
|
+
const question = this.questions.get(String(call.interaction_id));
|
|
154
339
|
if (question === undefined) {
|
|
155
|
-
|
|
340
|
+
const text = answerMessage(call.request, call.response);
|
|
341
|
+
return this.message(conversation, { ...call, text, first: false, message_id: null });
|
|
156
342
|
}
|
|
157
|
-
question.answer(
|
|
343
|
+
question.answer(call.response);
|
|
158
344
|
}
|
|
345
|
+
return { result: Promise.resolve({}) };
|
|
346
|
+
}
|
|
347
|
+
takeOut(conversation, messageId) {
|
|
348
|
+
const index = conversation.waiting.findIndex((waiting) => waiting.call.message_id === messageId);
|
|
349
|
+
if (index === -1)
|
|
350
|
+
return { started: true };
|
|
351
|
+
const [waiting] = conversation.waiting.splice(index, 1);
|
|
352
|
+
waiting.settle({ refused: TAKEN_OUT });
|
|
159
353
|
return {};
|
|
160
354
|
}
|
|
161
|
-
async open(conversation,
|
|
162
|
-
|
|
163
|
-
|
|
355
|
+
async open(conversation, call) {
|
|
356
|
+
await this.stop(conversation);
|
|
357
|
+
const launch = conversation.launch ?? this.launch(conversation, this.route(conversation, call.agent_id), null, true);
|
|
358
|
+
return {
|
|
359
|
+
result: launch.session.then((session) => ({ provider_session_id: session }), (error) => ({ refused: refusal(conversation, error) })),
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
async stop(conversation) {
|
|
363
|
+
const running = conversation.running;
|
|
364
|
+
running?.scope.kill();
|
|
365
|
+
await running?.adapter.exited;
|
|
366
|
+
}
|
|
367
|
+
launch(conversation, route, sessionId, empty) {
|
|
368
|
+
let opened;
|
|
369
|
+
let failed;
|
|
370
|
+
const bridge = new Promise((resolve, reject) => {
|
|
371
|
+
opened = resolve;
|
|
372
|
+
failed = reject;
|
|
373
|
+
});
|
|
374
|
+
bridge.catch(() => undefined);
|
|
375
|
+
const session = this.start(conversation, route, sessionId, empty, opened);
|
|
376
|
+
session.catch(failed);
|
|
377
|
+
const launch = { bridge, session };
|
|
378
|
+
conversation.launch = launch;
|
|
379
|
+
void session
|
|
380
|
+
.catch(() => undefined)
|
|
381
|
+
.then(() => {
|
|
382
|
+
if (conversation.launch !== launch)
|
|
383
|
+
return;
|
|
384
|
+
conversation.launch = null;
|
|
385
|
+
this.closeIdleBridge(conversation);
|
|
386
|
+
this.next(conversation);
|
|
387
|
+
});
|
|
388
|
+
this.next(conversation);
|
|
389
|
+
return launch;
|
|
390
|
+
}
|
|
391
|
+
async launched(conversation) {
|
|
392
|
+
while (conversation.launch !== null)
|
|
393
|
+
await conversation.launch.session.catch(() => undefined);
|
|
394
|
+
}
|
|
395
|
+
instructed(conversation, route) {
|
|
396
|
+
const bridge = conversation.running === null ? conversation.launch?.bridge : Promise.resolve(conversation.running.bridge);
|
|
397
|
+
if (bridge === undefined)
|
|
398
|
+
return null;
|
|
399
|
+
const read = bridge.then((opened) => instructions(opened, route.base_instructions));
|
|
400
|
+
read.catch(() => undefined);
|
|
401
|
+
return read;
|
|
402
|
+
}
|
|
403
|
+
async message(conversation, call) {
|
|
404
|
+
const content = [
|
|
405
|
+
...(typeof call.recent === 'string' ? [{ type: 'text', text: call.recent }] : []),
|
|
406
|
+
{ type: 'text', text: String(call.text) },
|
|
407
|
+
];
|
|
408
|
+
let route;
|
|
409
|
+
let instructing = null;
|
|
164
410
|
try {
|
|
165
|
-
|
|
411
|
+
route = await this.route(conversation, call.agent_id);
|
|
412
|
+
if (call.first === true)
|
|
413
|
+
instructing = this.instructed(conversation, route);
|
|
414
|
+
if (call.message_id !== null)
|
|
415
|
+
content.push(...(await this.transcribed(conversation, String(call.message_id), String(call.text))));
|
|
416
|
+
if (conversation.kills > 0)
|
|
417
|
+
throw new Error(KILLED);
|
|
166
418
|
}
|
|
167
419
|
catch (error) {
|
|
168
|
-
|
|
420
|
+
this.closeIdleBridge(conversation);
|
|
421
|
+
this.prepares(conversation, null);
|
|
422
|
+
return { result: Promise.resolve({ refused: refusal(conversation, error) }) };
|
|
423
|
+
}
|
|
424
|
+
conversation.preparing = null;
|
|
425
|
+
const { promise, resolve: settle } = Promise.withResolvers();
|
|
426
|
+
const waiting = { call, route, content, instructing, settle };
|
|
427
|
+
if (call.at_once === true && conversation.turns.size > 0)
|
|
428
|
+
await this.atOnce(conversation, waiting);
|
|
429
|
+
else
|
|
430
|
+
conversation.waiting.push(waiting);
|
|
431
|
+
this.next(conversation);
|
|
432
|
+
return { result: promise };
|
|
433
|
+
}
|
|
434
|
+
async atOnce(conversation, waiting) {
|
|
435
|
+
while (conversation.sending !== null)
|
|
436
|
+
await conversation.sending;
|
|
437
|
+
const running = conversation.running;
|
|
438
|
+
if (running !== null && conversation.turns.size > 0 && steers(running.adapter)) {
|
|
439
|
+
conversation.sending = this.steer(conversation, running, waiting);
|
|
440
|
+
await conversation.sending;
|
|
441
|
+
conversation.sending = null;
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
this.ahead(conversation, waiting);
|
|
445
|
+
if (conversation.turns.size > 0)
|
|
446
|
+
await this.cancel(conversation);
|
|
447
|
+
}
|
|
448
|
+
ahead(conversation, waiting) {
|
|
449
|
+
const behind = conversation.waiting.findIndex((held) => held.call.at_once !== true);
|
|
450
|
+
conversation.waiting.splice(behind === -1 ? conversation.waiting.length : behind, 0, waiting);
|
|
451
|
+
}
|
|
452
|
+
async steer(conversation, running, waiting) {
|
|
453
|
+
let outcome;
|
|
454
|
+
try {
|
|
455
|
+
({ outcome } = await running.adapter.connection.agent.request('_session/steering', {
|
|
456
|
+
sessionId: running.sessionId,
|
|
457
|
+
prompt: waiting.content,
|
|
458
|
+
_meta: { steering: { idleBehavior: 'promptRequired' } },
|
|
459
|
+
}));
|
|
460
|
+
}
|
|
461
|
+
catch (error) {
|
|
462
|
+
outcome = error;
|
|
463
|
+
}
|
|
464
|
+
if (outcome === 'promptRequired') {
|
|
465
|
+
this.ahead(conversation, waiting);
|
|
466
|
+
}
|
|
467
|
+
else if (outcome === 'injected' || outcome === 'startedNewTurn') {
|
|
468
|
+
if (outcome === 'injected') {
|
|
469
|
+
for (const turn of conversation.turns)
|
|
470
|
+
turn.steered ||= turn.prompted;
|
|
471
|
+
}
|
|
472
|
+
this.kit.calls.keepResult(String(waiting.call.input_id), {});
|
|
473
|
+
waiting.settle({});
|
|
474
|
+
}
|
|
475
|
+
else {
|
|
476
|
+
const cause = outcome instanceof Error ? outcome : new Error(`the CLI answered ${JSON.stringify(outcome)}`);
|
|
477
|
+
waiting.settle({ refused: refusal(conversation, new NotStarted(UNSTEERED, { cause })) });
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
next(conversation) {
|
|
481
|
+
if (this.stopping || conversation.sending !== null || conversation.turns.size > 0)
|
|
482
|
+
return;
|
|
483
|
+
const waiting = conversation.waiting.shift();
|
|
484
|
+
if (waiting === undefined) {
|
|
485
|
+
const step = conversation.preparing ?? (conversation.launch === null ? null : 'session');
|
|
486
|
+
if (conversation.starting !== step)
|
|
487
|
+
this.starts(conversation, step);
|
|
488
|
+
if (conversation.done && conversation.launch === null)
|
|
489
|
+
void this.stop(conversation).catch(logged);
|
|
490
|
+
return;
|
|
169
491
|
}
|
|
492
|
+
conversation.sending = this.send(conversation, waiting).finally(() => {
|
|
493
|
+
conversation.sending = null;
|
|
494
|
+
this.next(conversation);
|
|
495
|
+
});
|
|
170
496
|
}
|
|
171
|
-
async
|
|
172
|
-
|
|
497
|
+
async send(conversation, waiting) {
|
|
498
|
+
const { call } = waiting;
|
|
499
|
+
let result = {};
|
|
500
|
+
let started = false;
|
|
501
|
+
let instructing = waiting.instructing;
|
|
173
502
|
const prompt = [];
|
|
174
503
|
try {
|
|
175
|
-
const route = await this.route(conversation, input.agent_id);
|
|
176
|
-
const files = input.files;
|
|
177
|
-
const paths = await placeFiles(this.kit.house, route.working_directory, files, conversation.killed.signal);
|
|
178
|
-
const lines = await this.transcribed(conversation, files, paths);
|
|
179
504
|
if (conversation.running === null) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
505
|
+
this.starts(conversation, 'session');
|
|
506
|
+
await this.launched(conversation);
|
|
507
|
+
}
|
|
508
|
+
if (conversation.running === null) {
|
|
509
|
+
const stored = conversation.session ?? call.provider_session_id;
|
|
510
|
+
const empty = call.first === true && conversation.prompted !== stored;
|
|
511
|
+
const launch = this.launch(conversation, Promise.resolve(waiting.route), stored, empty);
|
|
512
|
+
if (call.first === true)
|
|
513
|
+
instructing = this.instructed(conversation, waiting.route);
|
|
514
|
+
started = (await launch.session) !== stored;
|
|
515
|
+
}
|
|
516
|
+
const session = conversation.running.sessionId;
|
|
517
|
+
if (started || (call.first === true && session !== call.provider_session_id)) {
|
|
518
|
+
result = { provider_session_id: session };
|
|
183
519
|
}
|
|
184
|
-
if (
|
|
185
|
-
prompt.push({
|
|
186
|
-
type: 'text',
|
|
187
|
-
text: await instructions(conversation.running.bridge, route.base_instructions, await this.kit.copies.mapping()),
|
|
188
|
-
});
|
|
520
|
+
if (call.first === true) {
|
|
521
|
+
prompt.push({ type: 'text', text: await (instructing ?? this.instructed(conversation, waiting.route)) });
|
|
189
522
|
}
|
|
190
|
-
prompt.push({ type: 'text', text: String(input.text) });
|
|
191
|
-
if (lines.length > 0)
|
|
192
|
-
prompt.push({ type: 'text', text: lines.join('\n') });
|
|
193
523
|
if (conversation.kills > 0)
|
|
194
524
|
throw new Error(KILLED);
|
|
195
525
|
}
|
|
196
526
|
catch (error) {
|
|
197
|
-
|
|
527
|
+
this.starts(conversation, null);
|
|
528
|
+
if (started) {
|
|
529
|
+
conversation.session = null;
|
|
198
530
|
await this.kill(conversation);
|
|
199
|
-
|
|
531
|
+
}
|
|
532
|
+
const refused = refusal(conversation, error);
|
|
533
|
+
const resumeRefused = call.kind === 'message' && conversation.kills === 0 && error instanceof ResumeRefused;
|
|
534
|
+
waiting.settle(resumeRefused ? { refused, resume_refused: true } : { refused });
|
|
535
|
+
return;
|
|
200
536
|
}
|
|
201
|
-
this.
|
|
202
|
-
|
|
537
|
+
this.starts(conversation, null);
|
|
538
|
+
this.prompt(conversation, [...prompt, ...waiting.content], () => this.kit.calls.keepResult(String(call.input_id), result));
|
|
539
|
+
waiting.settle(result);
|
|
203
540
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
541
|
+
starts(conversation, step) {
|
|
542
|
+
conversation.starting = step;
|
|
543
|
+
if (conversation.shown !== null)
|
|
544
|
+
clearTimeout(conversation.shown);
|
|
545
|
+
conversation.shown = null;
|
|
546
|
+
if (step === null || conversation.told !== null) {
|
|
547
|
+
this.tell(conversation, step);
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
conversation.shown = setTimeout(() => {
|
|
551
|
+
conversation.shown = null;
|
|
552
|
+
if (conversation.starting === step)
|
|
553
|
+
this.tell(conversation, step);
|
|
554
|
+
}, STARTING_SHOWN_AFTER_MS);
|
|
555
|
+
}
|
|
556
|
+
prepares(conversation, step) {
|
|
557
|
+
conversation.preparing = step;
|
|
558
|
+
this.next(conversation);
|
|
559
|
+
}
|
|
560
|
+
tell(conversation, step) {
|
|
561
|
+
if (conversation.told === step && !conversation.untold)
|
|
562
|
+
return;
|
|
563
|
+
conversation.told = step;
|
|
564
|
+
this.retell(conversation);
|
|
565
|
+
}
|
|
566
|
+
retell(conversation) {
|
|
567
|
+
conversation.untold = !this.kit.send({ type: 'starting', conversation_id: conversation.id, step: conversation.told });
|
|
568
|
+
}
|
|
569
|
+
async transcribed(conversation, messageId, text) {
|
|
570
|
+
const paths = linkedPaths(text);
|
|
571
|
+
if (paths.length === 0)
|
|
572
|
+
return [];
|
|
573
|
+
const killed = conversation.killed.signal;
|
|
574
|
+
const interrupted = conversation.interrupted.signal;
|
|
575
|
+
const transcribing = AbortSignal.any([killed, interrupted]);
|
|
576
|
+
let signal = killed;
|
|
577
|
+
const blocks = [];
|
|
578
|
+
try {
|
|
579
|
+
const bridge = await step(unbridged, unlessAborted(this.bridged(conversation), killed));
|
|
580
|
+
const read = async (path) => audioOf(await step(unbridged, unlessAborted(bridge.tool('inspect', { path }), signal)));
|
|
581
|
+
for (const path of paths) {
|
|
582
|
+
let answered = await read(path);
|
|
583
|
+
if (answered === null)
|
|
584
|
+
continue;
|
|
585
|
+
signal = transcribing;
|
|
586
|
+
this.prepares(conversation, 'transcription');
|
|
587
|
+
const said = await transcribe(async () => {
|
|
588
|
+
const address = answered ?? (await read(path));
|
|
589
|
+
answered = null;
|
|
590
|
+
if (address === null)
|
|
591
|
+
throw new Error(`${path} is no longer an audio file`);
|
|
592
|
+
return address;
|
|
593
|
+
}, signal);
|
|
594
|
+
await step(unbridged, unlessAborted(this.kit.house.deliver('/kit/transcripts/keep', { conversation: conversation.id, message: messageId, path, text: said }, () => !signal.aborted), signal));
|
|
595
|
+
blocks.push({ type: 'text', text: `${path}\nTranscript: ${said}` });
|
|
219
596
|
}
|
|
220
597
|
}
|
|
221
|
-
|
|
598
|
+
catch (error) {
|
|
599
|
+
if (signal === transcribing && interrupted.aborted && conversation.kills === 0)
|
|
600
|
+
throw new NotStarted(INTERRUPTED, { cause: error });
|
|
601
|
+
throw error;
|
|
602
|
+
}
|
|
603
|
+
return blocks;
|
|
604
|
+
}
|
|
605
|
+
bridged(conversation) {
|
|
606
|
+
if (conversation.bridge === null) {
|
|
607
|
+
const bridging = openBridge(this.kit.house, this.kit.copies, conversation.id, conversation.killed.signal);
|
|
608
|
+
conversation.bridge = bridging;
|
|
609
|
+
bridging.catch(() => {
|
|
610
|
+
if (conversation.bridge === bridging)
|
|
611
|
+
conversation.bridge = null;
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
return conversation.bridge;
|
|
615
|
+
}
|
|
616
|
+
closeBridge(conversation, bridging, bridge) {
|
|
617
|
+
bridge.close();
|
|
618
|
+
if (conversation.bridge === bridging)
|
|
619
|
+
conversation.bridge = null;
|
|
620
|
+
}
|
|
621
|
+
closeIdleBridge(conversation) {
|
|
622
|
+
const bridging = conversation.bridge;
|
|
623
|
+
if (bridging === null || conversation.running !== null || conversation.launch !== null)
|
|
624
|
+
return;
|
|
625
|
+
conversation.bridge = null;
|
|
626
|
+
void bridging.then((bridge) => bridge.close(), () => undefined);
|
|
222
627
|
}
|
|
223
628
|
async interrupt(conversation, turnId) {
|
|
224
|
-
if (conversation.
|
|
629
|
+
if ([...conversation.turns].some((turn) => turn.id === turnId))
|
|
630
|
+
await this.cancel(conversation);
|
|
631
|
+
}
|
|
632
|
+
async cancel(conversation) {
|
|
633
|
+
if (conversation.running === null)
|
|
225
634
|
return;
|
|
226
635
|
await conversation.running.adapter.connection.agent.notify('session/cancel', {
|
|
227
636
|
sessionId: conversation.running.sessionId,
|
|
@@ -229,11 +638,13 @@ export class Conversations {
|
|
|
229
638
|
}
|
|
230
639
|
halt(conversation) {
|
|
231
640
|
if (conversation.opening !== null)
|
|
232
|
-
|
|
641
|
+
killMarked(marker(conversation.opening));
|
|
642
|
+
for (const scope of conversation.leftovers)
|
|
643
|
+
scope.kill();
|
|
233
644
|
if (conversation.running === null)
|
|
234
645
|
return;
|
|
235
646
|
conversation.running.killed = true;
|
|
236
|
-
|
|
647
|
+
conversation.running.scope.kill();
|
|
237
648
|
}
|
|
238
649
|
async kill(conversation) {
|
|
239
650
|
const running = conversation.running;
|
|
@@ -245,6 +656,10 @@ export class Conversations {
|
|
|
245
656
|
if (running === null)
|
|
246
657
|
return;
|
|
247
658
|
try {
|
|
659
|
+
if (conversation.options?.find(({ id }) => id === option)?.category === 'mode') {
|
|
660
|
+
throw new Error(`conversation ${conversation.id} keeps the mode its Agent's launch settings name`);
|
|
661
|
+
}
|
|
662
|
+
running.reader.requested(option, value);
|
|
248
663
|
const answered = await running.adapter.connection.agent.request('session/set_config_option', {
|
|
249
664
|
sessionId: running.sessionId,
|
|
250
665
|
configId: option,
|
|
@@ -253,6 +668,7 @@ export class Conversations {
|
|
|
253
668
|
this.options(conversation, answered.configOptions);
|
|
254
669
|
}
|
|
255
670
|
catch (error) {
|
|
671
|
+
running.reader.refused(option);
|
|
256
672
|
logged(error);
|
|
257
673
|
}
|
|
258
674
|
}
|
|
@@ -269,58 +685,84 @@ export class Conversations {
|
|
|
269
685
|
}
|
|
270
686
|
return route;
|
|
271
687
|
}
|
|
272
|
-
async start(conversation,
|
|
688
|
+
async start(conversation, routed, sessionId, empty, bridged) {
|
|
689
|
+
const route = await routed;
|
|
690
|
+
const reader = readerFor(route.kind);
|
|
691
|
+
reader.session = sessionId;
|
|
692
|
+
conversation.reader = reader;
|
|
273
693
|
const app = client({ name: '@agentshouse/kit' })
|
|
274
|
-
.
|
|
275
|
-
.
|
|
276
|
-
.
|
|
277
|
-
.onRequest('
|
|
278
|
-
.onRequest('elicitation/create', ({ params, signal }) => this.ask(conversation, 'elicitation/create', params, asksSecret(params), signal));
|
|
694
|
+
.onRequest('session/request_permission', ({ params, signal }) => conversation.afk ? permissionAllowed(params) : this.ask(conversation, 'session/request_permission', params, false, signal))
|
|
695
|
+
.onRequest('elicitation/create', ({ params, signal }) => conversation.afk ? formAnswered(route.kind, params) : this.ask(conversation, 'elicitation/create', params, asksSecret(params), signal))
|
|
696
|
+
.onRequest('_x.ai/exit_plan_mode', GROK_PLAN, async ({ params, signal }) => conversation.afk ? PLAN_APPROVED : grokPlanAnswer(await this.askForm(conversation, grokPlanForm(params), signal)))
|
|
697
|
+
.onRequest('_x.ai/ask_user_question', GROK_QUESTION, async ({ params, signal }) => conversation.afk ? grokQuestionAnswered(params) : grokQuestionAnswer(await this.askForm(conversation, grokQuestionForm(params), signal)));
|
|
279
698
|
const cli = await unlessAborted(this.kit.agents.cli(route.kind), conversation.killed.signal);
|
|
280
|
-
const
|
|
699
|
+
const bridging = this.bridged(conversation);
|
|
700
|
+
const bridge = await step(unbridged, bridging);
|
|
281
701
|
if (conversation.kills > 0) {
|
|
282
|
-
|
|
702
|
+
this.closeBridge(conversation, bridging, bridge);
|
|
283
703
|
throw new Error(KILLED);
|
|
284
704
|
}
|
|
285
705
|
conversation.opening = bridge;
|
|
706
|
+
const work = workFor(route.kind, route.working_directory, () => this.kit.changed(), (message) => this.tapped(conversation, reader, message));
|
|
707
|
+
const scope = new Scope(marker(bridge), randomUUID(), cli);
|
|
708
|
+
let adapter = null;
|
|
286
709
|
try {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
710
|
+
bridged(bridge);
|
|
711
|
+
await CLIS[route.kind].allowHouse(bridge.env.PATH);
|
|
712
|
+
const streamed = await step(NO_CONNECTION, work.open(cli));
|
|
713
|
+
if (this.stopping)
|
|
714
|
+
throw new Error(KILLED);
|
|
715
|
+
adapter = await step(NO_START, startAdapter(route.kind, cli, route.working_directory, app, (message) => {
|
|
716
|
+
work.read(message);
|
|
717
|
+
this.read(conversation, reader, message);
|
|
718
|
+
}, { ...bridge.env, ...streamed }, undefined, scope));
|
|
719
|
+
void adapter.exited.then(() => this.closeBridge(conversation, bridging, bridge));
|
|
290
720
|
const agent = adapter.connection.agent;
|
|
291
721
|
const cwd = route.working_directory;
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
phase: CLIS[route.kind].phase,
|
|
302
|
-
};
|
|
303
|
-
const options = await this.launchSettings(running, route, opened.configOptions ?? []);
|
|
722
|
+
const _meta = CLIS[route.kind].sessionMeta(bridge.env.HOUSE_BRIDGE);
|
|
723
|
+
const resumed = sessionId === null ? null : await this.resume(conversation, route.kind, adapter, { sessionId, cwd, mcpServers: [], _meta }, empty);
|
|
724
|
+
const opened = resumed ?? (await step(unopened(NO_SESSION), agent.request('session/new', { cwd, mcpServers: [], _meta })));
|
|
725
|
+
reader.session = opened.sessionId;
|
|
726
|
+
if (opened.sessionId !== conversation.session)
|
|
727
|
+
conversation.state = QUIET;
|
|
728
|
+
const running = { adapter, bridging, bridge, sessionId: opened.sessionId, killed: false, reader, work, scope };
|
|
729
|
+
const options = await step(NO_SETTINGS, this.launchSettings(running, route, opened.configOptions ?? []));
|
|
730
|
+
void work.settled().then(() => scope.record());
|
|
304
731
|
conversation.running = running;
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
this.
|
|
309
|
-
void adapter.
|
|
732
|
+
adapter.turn(conversation.turns.size > 0);
|
|
733
|
+
conversation.session = running.sessionId;
|
|
734
|
+
conversation.options = options;
|
|
735
|
+
this.facts(conversation);
|
|
736
|
+
void adapter.lost.then((cause) => this.exited(conversation, running, cause));
|
|
310
737
|
return running.sessionId;
|
|
311
738
|
}
|
|
312
739
|
catch (error) {
|
|
313
|
-
|
|
314
|
-
|
|
740
|
+
adapter?.retire();
|
|
741
|
+
work.close();
|
|
742
|
+
conversation.leftovers.add(scope);
|
|
743
|
+
this.closeBridge(conversation, bridging, bridge);
|
|
315
744
|
throw conversation.kills > 0 ? new Error(KILLED) : error;
|
|
316
745
|
}
|
|
317
746
|
finally {
|
|
318
747
|
conversation.opening = null;
|
|
319
748
|
}
|
|
320
749
|
}
|
|
750
|
+
async resume(conversation, kind, adapter, request, empty) {
|
|
751
|
+
try {
|
|
752
|
+
return { ...(await adapter.connection.agent.request('session/resume', request)), sessionId: request.sessionId };
|
|
753
|
+
}
|
|
754
|
+
catch (error) {
|
|
755
|
+
if (!empty || conversation.kills > 0 || !CLIS[kind].unknownSession(error)) {
|
|
756
|
+
throw signedOut(error) ? new NotStarted(NOT_SIGNED_IN, { cause: error }) : new ResumeRefused(NO_RESUME, { cause: error });
|
|
757
|
+
}
|
|
758
|
+
logged(new Error(`conversation ${conversation.id} opens a new session: ${NO_RESUME}: ${causeOf(error)}`));
|
|
759
|
+
return null;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
321
762
|
async launchSettings(running, route, offered) {
|
|
322
763
|
let options = offered;
|
|
323
764
|
const settings = [
|
|
765
|
+
['mode', route.mode ?? CLIS[route.kind].fullAccess],
|
|
324
766
|
['model', route.model],
|
|
325
767
|
['thought_level', route.effort],
|
|
326
768
|
];
|
|
@@ -328,6 +770,7 @@ export class Conversations {
|
|
|
328
770
|
if (value === null)
|
|
329
771
|
continue;
|
|
330
772
|
const option = options.find((candidate) => candidate.category === category);
|
|
773
|
+
running.reader.requested(option?.id ?? category, value);
|
|
331
774
|
const answered = await running.adapter.connection.agent.request('session/set_config_option', {
|
|
332
775
|
sessionId: running.sessionId,
|
|
333
776
|
configId: option?.id ?? category,
|
|
@@ -337,23 +780,49 @@ export class Conversations {
|
|
|
337
780
|
}
|
|
338
781
|
return options;
|
|
339
782
|
}
|
|
340
|
-
exited(conversation,
|
|
341
|
-
|
|
342
|
-
if (
|
|
343
|
-
|
|
783
|
+
exited(conversation, running, cause) {
|
|
784
|
+
this.detach(conversation, running);
|
|
785
|
+
if (conversation.turn !== null) {
|
|
786
|
+
this.end(conversation, conversation.turn, exitedWith(running, cause));
|
|
787
|
+
}
|
|
788
|
+
this.next(conversation);
|
|
789
|
+
this.kit.changed();
|
|
790
|
+
}
|
|
791
|
+
detach(conversation, running) {
|
|
792
|
+
if (conversation.bridge === running.bridging)
|
|
793
|
+
conversation.bridge = null;
|
|
794
|
+
running.work.close();
|
|
795
|
+
conversation.leftovers.add(running.scope);
|
|
344
796
|
conversation.running = null;
|
|
345
797
|
conversation.commands = null;
|
|
346
|
-
conversation.
|
|
347
|
-
conversation.
|
|
798
|
+
conversation.options = null;
|
|
799
|
+
conversation.work.clear();
|
|
800
|
+
this.stated(conversation, { running: [] });
|
|
348
801
|
for (const [id, question] of this.questions) {
|
|
349
802
|
if (question.conversation === conversation)
|
|
350
803
|
this.questions.delete(id);
|
|
351
804
|
}
|
|
352
|
-
this.
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
}
|
|
356
|
-
|
|
805
|
+
this.process(conversation, false);
|
|
806
|
+
}
|
|
807
|
+
process(conversation, running) {
|
|
808
|
+
conversation.exitUntold = !this.kit.send({ type: 'process', conversation_id: conversation.id, running }) && !running;
|
|
809
|
+
}
|
|
810
|
+
facts(conversation) {
|
|
811
|
+
this.process(conversation, true);
|
|
812
|
+
if (conversation.commands !== null)
|
|
813
|
+
this.commands(conversation, conversation.commands);
|
|
814
|
+
if (conversation.options !== null)
|
|
815
|
+
this.options(conversation, conversation.options);
|
|
816
|
+
}
|
|
817
|
+
stated(conversation, change) {
|
|
818
|
+
conversation.state = { ...conversation.state, ...change };
|
|
819
|
+
this.stateChanged(conversation, false);
|
|
820
|
+
}
|
|
821
|
+
stateChanged(conversation, again) {
|
|
822
|
+
const state = JSON.stringify(conversation.state);
|
|
823
|
+
if (!again && state === conversation.sentState)
|
|
824
|
+
return;
|
|
825
|
+
conversation.sentState = this.kit.send({ type: 'state', conversation_id: conversation.id, state: conversation.state }) ? state : null;
|
|
357
826
|
}
|
|
358
827
|
commands(conversation, commands) {
|
|
359
828
|
conversation.commands = commands;
|
|
@@ -364,11 +833,17 @@ export class Conversations {
|
|
|
364
833
|
options(conversation, options) {
|
|
365
834
|
if (conversation.running === null)
|
|
366
835
|
return;
|
|
367
|
-
|
|
836
|
+
conversation.options = options;
|
|
837
|
+
this.kit.send({ type: 'options', conversation_id: conversation.id, options: options.filter((option) => option.category !== 'mode') });
|
|
838
|
+
const chosen = (category) => {
|
|
839
|
+
const value = options.find((option) => option.category === category)?.currentValue;
|
|
840
|
+
return value === undefined ? null : String(value);
|
|
841
|
+
};
|
|
842
|
+
this.stated(conversation, { model: chosen('model'), effort: chosen('thought_level'), mode: chosen('mode') });
|
|
368
843
|
}
|
|
369
|
-
deliver(conversation, turn,
|
|
844
|
+
deliver(conversation, turn, act, body, current) {
|
|
370
845
|
this.reporting++;
|
|
371
|
-
const delivered = conversation.reports.then(() => this.kit.house.deliver(`/kit
|
|
846
|
+
const delivered = conversation.reports.then(async () => this.kit.house.deliver(`/kit/${act}`, { conversation: conversation.id, turn: turn.id, ...(await body) }, current));
|
|
372
847
|
conversation.reports = delivered
|
|
373
848
|
.catch(() => undefined)
|
|
374
849
|
.finally(() => {
|
|
@@ -377,122 +852,117 @@ export class Conversations {
|
|
|
377
852
|
});
|
|
378
853
|
return delivered;
|
|
379
854
|
}
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
.then(() => this.admitted(conversation, turn))
|
|
385
|
-
.catch(logged);
|
|
386
|
-
return turn;
|
|
855
|
+
write(conversation, turn, operation) {
|
|
856
|
+
if (turn.ended || !this.watched)
|
|
857
|
+
return;
|
|
858
|
+
this.kit.send({ type: 'turn', conversation_id: conversation.id, turn_id: turn.id, operation });
|
|
387
859
|
}
|
|
388
|
-
|
|
389
|
-
turn.
|
|
390
|
-
|
|
391
|
-
this.kit.send(note);
|
|
392
|
-
if (turn.text !== '') {
|
|
393
|
-
turn.unsentFrom = 0;
|
|
394
|
-
this.draft(conversation, turn);
|
|
395
|
-
}
|
|
396
|
-
if (turn.entries !== null)
|
|
397
|
-
this.plan(conversation, turn);
|
|
860
|
+
replay(conversation, turn) {
|
|
861
|
+
for (const operation of turn.content.operations())
|
|
862
|
+
this.write(conversation, turn, operation);
|
|
398
863
|
}
|
|
399
864
|
begin(conversation) {
|
|
400
|
-
const turn = this.
|
|
865
|
+
const turn = new Turn((operation) => this.write(conversation, turn, operation));
|
|
866
|
+
conversation.turns.add(turn);
|
|
867
|
+
conversation.running?.adapter.turn(true);
|
|
868
|
+
void this.deliver(conversation, turn, 'turns/start', {})
|
|
869
|
+
.then((answer) => {
|
|
870
|
+
if (answer.run === true)
|
|
871
|
+
turn.quietUntil = Date.now() + ACTIVITY_REPORT_MS;
|
|
872
|
+
})
|
|
873
|
+
.catch(logged);
|
|
401
874
|
conversation.turn = turn;
|
|
875
|
+
this.stated(conversation, { suggestion: null });
|
|
876
|
+
for (const event of conversation.reader?.began() ?? [])
|
|
877
|
+
this.event(conversation, event);
|
|
878
|
+
for (const held of conversation.held.splice(0))
|
|
879
|
+
this.place(conversation, turn, held);
|
|
402
880
|
this.kit.changed();
|
|
403
881
|
return turn;
|
|
404
882
|
}
|
|
405
|
-
|
|
406
|
-
if (turn.ended)
|
|
407
|
-
return;
|
|
408
|
-
const segment = turn.segment;
|
|
409
|
-
turn.segment = null;
|
|
410
|
-
if (segment?.phase === 'note')
|
|
411
|
-
this.note(conversation, turn, segment.text);
|
|
412
|
-
else if (segment !== null)
|
|
413
|
-
this.answer(conversation, turn, segment.text);
|
|
414
|
-
this.end(conversation, turn, { text: turn.text });
|
|
415
|
-
}
|
|
416
|
-
end(conversation, turn, outcome) {
|
|
883
|
+
end(conversation, turn, failed, notice = null) {
|
|
417
884
|
if (turn.ended)
|
|
418
885
|
return;
|
|
886
|
+
if (failed !== null)
|
|
887
|
+
logged(new Error(`conversation ${conversation.id} turn ${turn.id} failed: ${failed.cause}`));
|
|
888
|
+
if (conversation.turn === turn) {
|
|
889
|
+
for (const event of conversation.reader?.closeTurn() ?? [])
|
|
890
|
+
this.event(conversation, event);
|
|
891
|
+
}
|
|
892
|
+
if (notice !== null)
|
|
893
|
+
turn.content.add(null, { type: 'notice', kind: 'error', text: notice });
|
|
419
894
|
turn.ended = true;
|
|
420
|
-
conversation.
|
|
895
|
+
conversation.turns.delete(turn);
|
|
896
|
+
conversation.running?.adapter.turn(conversation.turns.size > 0);
|
|
421
897
|
if (conversation.turn === turn)
|
|
422
898
|
conversation.turn = null;
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
if (
|
|
426
|
-
this.
|
|
899
|
+
const tokens = conversation.tokens;
|
|
900
|
+
conversation.tokens = null;
|
|
901
|
+
if (tokens !== null)
|
|
902
|
+
this.stated(conversation, { tokens: addedTokens(conversation.state.tokens, tokens) });
|
|
903
|
+
const body = Promise.allSettled(turn.uploads).then(() => ({
|
|
904
|
+
content: { parts: turn.content.parts, tokens, state: conversation.state },
|
|
905
|
+
...(failed === null ? {} : { failed: failed.failure }),
|
|
906
|
+
}));
|
|
907
|
+
void this.deliver(conversation, turn, 'turns/end', body).catch(logged);
|
|
908
|
+
this.next(conversation);
|
|
427
909
|
this.kit.changed();
|
|
428
910
|
}
|
|
429
|
-
|
|
430
|
-
if (conversation.open > 0)
|
|
431
|
-
conversation.late.push(cause);
|
|
432
|
-
else
|
|
433
|
-
this.end(conversation, this.report(conversation), { failed: cause });
|
|
434
|
-
}
|
|
435
|
-
prompt(conversation, prompt) {
|
|
911
|
+
prompt(conversation, prompt, taken) {
|
|
436
912
|
const running = conversation.running;
|
|
437
|
-
const
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
this.
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
else
|
|
448
|
-
this.end(conversation,
|
|
913
|
+
const turn = this.begin(conversation);
|
|
914
|
+
turn.prompted = true;
|
|
915
|
+
turn.taken = taken;
|
|
916
|
+
conversation.prompted = running.sessionId;
|
|
917
|
+
running.adapter.connection.agent.request('session/prompt', { sessionId: running.sessionId, prompt }).then((answer) => {
|
|
918
|
+
this.took(turn);
|
|
919
|
+
const notice = answeredFailure(answer);
|
|
920
|
+
if (notice !== null) {
|
|
921
|
+
this.end(conversation, turn, { failure: notice.limit ? 'limit' : 'error', cause: notice.title }, notice.title);
|
|
922
|
+
}
|
|
923
|
+
else {
|
|
924
|
+
this.end(conversation, turn, answer.stopReason === 'refusal' ? { failure: 'refused', cause: REFUSED } : null);
|
|
925
|
+
}
|
|
926
|
+
}, async (error) => {
|
|
927
|
+
if (running.adapter.connection.signal.aborted) {
|
|
928
|
+
this.end(conversation, turn, exitedWith(running, await running.adapter.exited));
|
|
929
|
+
}
|
|
930
|
+
else {
|
|
931
|
+
this.took(turn);
|
|
932
|
+
if (conversation.turns.size === 1 && conversation.turns.has(turn) && conversation.running === running) {
|
|
933
|
+
running.adapter.retire();
|
|
934
|
+
this.detach(conversation, running);
|
|
935
|
+
}
|
|
936
|
+
this.end(conversation, turn, answeredWith(error), cliSaid(error));
|
|
449
937
|
}
|
|
450
938
|
});
|
|
451
939
|
}
|
|
452
|
-
attach(turn, sent) {
|
|
453
|
-
turn.prompt = sent;
|
|
454
|
-
sent.turn = turn;
|
|
455
|
-
}
|
|
456
|
-
answered(conversation, sent, failed) {
|
|
457
|
-
const turn = sent.turn;
|
|
458
|
-
if (turn === null) {
|
|
459
|
-
const queued = conversation.queued.indexOf(sent);
|
|
460
|
-
if (queued < 0)
|
|
461
|
-
return;
|
|
462
|
-
conversation.queued.splice(queued, 1);
|
|
463
|
-
if (failed !== null)
|
|
464
|
-
this.failed(conversation, failed);
|
|
465
|
-
}
|
|
466
|
-
else if (failed !== null) {
|
|
467
|
-
if (turn.ended)
|
|
468
|
-
this.failed(conversation, failed);
|
|
469
|
-
else
|
|
470
|
-
this.end(conversation, turn, { failed });
|
|
471
|
-
}
|
|
472
|
-
else if (turn.prompt === sent) {
|
|
473
|
-
this.finish(conversation, turn);
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
940
|
started(conversation) {
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
const turn = this.begin(conversation);
|
|
480
|
-
const queued = conversation.queued.shift();
|
|
481
|
-
if (queued !== undefined)
|
|
482
|
-
this.attach(turn, queued);
|
|
941
|
+
const turn = conversation.turn ?? this.begin(conversation);
|
|
942
|
+
this.took(turn);
|
|
483
943
|
return turn;
|
|
484
944
|
}
|
|
485
|
-
ended(conversation) {
|
|
945
|
+
ended(conversation, stop) {
|
|
486
946
|
const turn = conversation.turn;
|
|
487
947
|
if (turn === null)
|
|
488
948
|
return;
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
this.
|
|
949
|
+
const held = stop !== null &&
|
|
950
|
+
!turn.steered &&
|
|
951
|
+
[...conversation.work.values()].some((work) => work.part.type === 'subagent' && turn.content.index(work.key) !== undefined);
|
|
952
|
+
if (!turn.prompted || held) {
|
|
953
|
+
this.took(turn);
|
|
954
|
+
this.end(conversation, turn, stop === 'refusal' ? { failure: 'refused', cause: REFUSED } : null);
|
|
955
|
+
return;
|
|
495
956
|
}
|
|
957
|
+
if (stop !== null)
|
|
958
|
+
return;
|
|
959
|
+
for (const event of conversation.reader?.closeTurn() ?? [])
|
|
960
|
+
this.event(conversation, event);
|
|
961
|
+
conversation.turn = null;
|
|
962
|
+
this.kit.changed();
|
|
963
|
+
}
|
|
964
|
+
askForm(conversation, form, signal) {
|
|
965
|
+
return this.ask(conversation, 'elicitation/create', form, false, signal);
|
|
496
966
|
}
|
|
497
967
|
async ask(conversation, method, params, secret, signal) {
|
|
498
968
|
const turn = this.started(conversation);
|
|
@@ -500,7 +970,7 @@ export class Conversations {
|
|
|
500
970
|
const answered = new Promise((answer, fail) => {
|
|
501
971
|
this.questions.set(interactionId, { conversation, answer: (response) => answer(response) });
|
|
502
972
|
signal.addEventListener('abort', () => answer(null), { once: true });
|
|
503
|
-
this.deliver(conversation, turn, '
|
|
973
|
+
this.deliver(conversation, turn, 'conversations/interaction/ask', {
|
|
504
974
|
interaction_id: interactionId,
|
|
505
975
|
request: { method, params },
|
|
506
976
|
secret,
|
|
@@ -538,101 +1008,151 @@ export class Conversations {
|
|
|
538
1008
|
if (content !== null)
|
|
539
1009
|
this.questions.get(interactionId)?.answer({ action: 'accept', content });
|
|
540
1010
|
}
|
|
541
|
-
|
|
542
|
-
const
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
}
|
|
546
|
-
else if (update.sessionUpdate === 'plan') {
|
|
547
|
-
const turn = this.started(conversation);
|
|
548
|
-
this.close(conversation, turn);
|
|
549
|
-
turn.entries = update.entries;
|
|
550
|
-
this.plan(conversation, turn);
|
|
551
|
-
}
|
|
552
|
-
else if (update.sessionUpdate === 'tool_call') {
|
|
553
|
-
this.close(conversation, this.started(conversation));
|
|
554
|
-
}
|
|
555
|
-
else if (update.sessionUpdate === 'agent_thought_chunk') {
|
|
556
|
-
const turn = this.started(conversation);
|
|
557
|
-
if (turn.segment?.messageId !== (update.messageId ?? null))
|
|
558
|
-
this.close(conversation, turn);
|
|
559
|
-
}
|
|
560
|
-
else if (update.sessionUpdate === 'available_commands_update') {
|
|
561
|
-
this.commands(conversation, update.availableCommands);
|
|
562
|
-
}
|
|
563
|
-
else if (update.sessionUpdate === 'config_option_update') {
|
|
564
|
-
this.options(conversation, update.configOptions);
|
|
565
|
-
}
|
|
1011
|
+
read(conversation, reader, message) {
|
|
1012
|
+
for (const event of reader.read(message))
|
|
1013
|
+
this.event(conversation, event);
|
|
1014
|
+
this.reportActivity(conversation);
|
|
566
1015
|
}
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
else
|
|
571
|
-
conversation.jobs.delete(job.id);
|
|
572
|
-
this.kit.changed();
|
|
1016
|
+
tapped(conversation, reader, message) {
|
|
1017
|
+
for (const event of reader.tap(message))
|
|
1018
|
+
this.event(conversation, event);
|
|
573
1019
|
}
|
|
574
|
-
|
|
575
|
-
const
|
|
576
|
-
|
|
577
|
-
if (turn.segment?.messageId !== messageId || turn.segment.phase !== phase)
|
|
578
|
-
this.close(conversation, turn);
|
|
579
|
-
if (phase === 'answer') {
|
|
580
|
-
this.answer(conversation, turn, text);
|
|
1020
|
+
reportActivity(conversation) {
|
|
1021
|
+
const turn = conversation.turn;
|
|
1022
|
+
if (turn === null || Date.now() < turn.quietUntil)
|
|
581
1023
|
return;
|
|
1024
|
+
turn.quietUntil = Infinity;
|
|
1025
|
+
void this.deliver(conversation, turn, 'turns/activity/report', {}, () => false)
|
|
1026
|
+
.catch(logged)
|
|
1027
|
+
.finally(() => {
|
|
1028
|
+
turn.quietUntil = Date.now() + ACTIVITY_REPORT_MS;
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
event(conversation, event) {
|
|
1032
|
+
switch (event.kind) {
|
|
1033
|
+
case 'started':
|
|
1034
|
+
this.started(conversation);
|
|
1035
|
+
return;
|
|
1036
|
+
case 'ended':
|
|
1037
|
+
this.ended(conversation, event.stop);
|
|
1038
|
+
return;
|
|
1039
|
+
case 'commands':
|
|
1040
|
+
this.commands(conversation, event.commands);
|
|
1041
|
+
return;
|
|
1042
|
+
case 'options':
|
|
1043
|
+
this.options(conversation, event.options);
|
|
1044
|
+
return;
|
|
1045
|
+
case 'state':
|
|
1046
|
+
this.stated(conversation, event.state);
|
|
1047
|
+
return;
|
|
1048
|
+
case 'tokens':
|
|
1049
|
+
conversation.tokens = addedTokens(conversation.tokens, event.tokens);
|
|
1050
|
+
return;
|
|
1051
|
+
case 'part':
|
|
1052
|
+
this.keyed(conversation, event);
|
|
1053
|
+
return;
|
|
1054
|
+
case 'notice':
|
|
1055
|
+
case 'image':
|
|
1056
|
+
if (conversation.turn === null)
|
|
1057
|
+
return;
|
|
1058
|
+
break;
|
|
1059
|
+
case 'text':
|
|
1060
|
+
case 'reasoning':
|
|
1061
|
+
if (event.parent !== null && conversation.turn === null)
|
|
1062
|
+
return;
|
|
1063
|
+
break;
|
|
582
1064
|
}
|
|
583
|
-
|
|
584
|
-
turn.segment.text += text;
|
|
1065
|
+
this.apply(conversation, this.started(conversation), event);
|
|
585
1066
|
}
|
|
586
|
-
|
|
587
|
-
const
|
|
588
|
-
|
|
1067
|
+
took(turn) {
|
|
1068
|
+
const taken = turn.taken;
|
|
1069
|
+
turn.taken = null;
|
|
1070
|
+
taken?.();
|
|
1071
|
+
}
|
|
1072
|
+
apply(conversation, turn, event) {
|
|
1073
|
+
switch (event.kind) {
|
|
1074
|
+
case 'text':
|
|
1075
|
+
case 'reasoning': {
|
|
1076
|
+
const parent = this.parentIn(conversation, turn, event.parent);
|
|
1077
|
+
if (parent !== undefined)
|
|
1078
|
+
turn.content.write(event.kind, event.id, parent, event.text);
|
|
1079
|
+
return;
|
|
1080
|
+
}
|
|
1081
|
+
case 'notice':
|
|
1082
|
+
turn.content.add(null, { type: 'notice', kind: event.notice, text: event.text });
|
|
1083
|
+
return;
|
|
1084
|
+
case 'plan':
|
|
1085
|
+
turn.content.plan(event.entries);
|
|
1086
|
+
return;
|
|
1087
|
+
case 'image':
|
|
1088
|
+
turn.uploads.push(this.image(conversation, turn, event.parent, event.data, event.mediaType));
|
|
1089
|
+
this.kit.changed();
|
|
1090
|
+
return;
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
keyed(conversation, keyed) {
|
|
1094
|
+
const running = workRuns(keyed.part);
|
|
1095
|
+
const earlier = running !== null && conversation.work.has(keyed.key) && conversation.turn?.content.index(keyed.key) === undefined;
|
|
1096
|
+
if (running === true)
|
|
1097
|
+
conversation.work.set(keyed.key, keyed);
|
|
1098
|
+
if (running === false)
|
|
1099
|
+
conversation.work.delete(keyed.key);
|
|
1100
|
+
const turn = conversation.turn;
|
|
1101
|
+
if (earlier) {
|
|
1102
|
+
if (running)
|
|
1103
|
+
return;
|
|
1104
|
+
if (turn === null)
|
|
1105
|
+
conversation.held.push(keyed);
|
|
1106
|
+
else
|
|
1107
|
+
this.place(conversation, turn, keyed);
|
|
589
1108
|
return;
|
|
590
|
-
|
|
591
|
-
|
|
1109
|
+
}
|
|
1110
|
+
if (keyed.parent !== null && turn === null)
|
|
1111
|
+
return;
|
|
1112
|
+
this.place(conversation, this.started(conversation), keyed);
|
|
592
1113
|
}
|
|
593
|
-
|
|
594
|
-
|
|
1114
|
+
place(conversation, turn, keyed) {
|
|
1115
|
+
const parent = this.parentIn(conversation, turn, keyed.parent);
|
|
1116
|
+
if (parent === undefined && workRuns(keyed.part) === null)
|
|
595
1117
|
return;
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
const
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
1118
|
+
turn.content.put(keyed.key, parent ?? null, keyed.part);
|
|
1119
|
+
}
|
|
1120
|
+
parentIn(conversation, turn, key) {
|
|
1121
|
+
if (key === null)
|
|
1122
|
+
return null;
|
|
1123
|
+
const index = turn.content.index(key);
|
|
1124
|
+
if (index !== undefined)
|
|
1125
|
+
return turn.content.parts[index].type === 'subagent' ? index : undefined;
|
|
1126
|
+
const running = conversation.work.get(key);
|
|
1127
|
+
if (running?.part.type !== 'subagent')
|
|
1128
|
+
return undefined;
|
|
1129
|
+
const parent = this.parentIn(conversation, turn, running.parent);
|
|
1130
|
+
return parent === undefined ? undefined : turn.content.put(key, parent, running.part);
|
|
1131
|
+
}
|
|
1132
|
+
async image(conversation, turn, parent, data, mediaType) {
|
|
1133
|
+
const bytes = Buffer.from(data, 'base64');
|
|
1134
|
+
const name = `image-${++turn.images}.${mediaType.split('/')[1]}`;
|
|
1135
|
+
const sha256 = createHash('sha256').update(bytes).digest('hex');
|
|
1136
|
+
try {
|
|
1137
|
+
const declared = (await this.deliver(conversation, turn, 'turns/images/declare', {
|
|
1138
|
+
name,
|
|
1139
|
+
media_type: mediaType,
|
|
1140
|
+
bytes: bytes.length,
|
|
1141
|
+
sha256,
|
|
1142
|
+
}));
|
|
1143
|
+
const saved = await uploaded(declared.upload, {
|
|
1144
|
+
name,
|
|
1145
|
+
bytes: bytes.length,
|
|
1146
|
+
sha256,
|
|
1147
|
+
read: (start, end) => [bytes.subarray(start, end)],
|
|
1148
|
+
send: (url, start, end, headers) => sentPart(url, bytes.subarray(start, end), headers),
|
|
1149
|
+
finish: (upload, partSha256) => this.kit.house.deliver('/kit/uploads/finish', { upload, part_sha256: partSha256 }),
|
|
1150
|
+
});
|
|
1151
|
+
turn.content.arrived(this.parentIn(conversation, turn, parent) ?? null, { type: 'image', file: saved.path, name, media_type: mediaType });
|
|
621
1152
|
}
|
|
622
|
-
|
|
623
|
-
|
|
1153
|
+
catch (error) {
|
|
1154
|
+
const cause = error instanceof HouseRefusal ? (refusalOf(error.text)?.text ?? causeOf(error)) : causeOf(error);
|
|
1155
|
+
turn.content.arrived(null, { type: 'notice', kind: 'error', text: `${name} was not saved: ${cause}` });
|
|
624
1156
|
}
|
|
625
1157
|
}
|
|
626
|
-
plan(conversation, turn) {
|
|
627
|
-
const sent = this.kit.send({
|
|
628
|
-
type: 'plan',
|
|
629
|
-
conversation_id: conversation.id,
|
|
630
|
-
turn_id: turn.id,
|
|
631
|
-
sequence: turn.planSequence + 1,
|
|
632
|
-
from: 0,
|
|
633
|
-
steps: turn.entries.map((entry) => ({ label: entry.content, priority: entry.priority, status: entry.status })),
|
|
634
|
-
});
|
|
635
|
-
if (sent)
|
|
636
|
-
turn.planSequence++;
|
|
637
|
-
}
|
|
638
1158
|
}
|