@agentshouse/kit 0.2.1-cb8dc89b78e07 → 0.2.1-cba216f2c559c

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