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