@agentshouse/kit 0.2.1-cf268dff1869b → 0.2.1-cf2c570b41f24

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 (54) hide show
  1. package/README.md +18 -25
  2. package/dist/acp.js +12 -9
  3. package/dist/afk.js +17 -0
  4. package/dist/agents.js +72 -26
  5. package/dist/api.js +18 -4
  6. package/dist/binaries.js +60 -0
  7. package/dist/bridge.js +99 -44
  8. package/dist/calls.js +18 -1
  9. package/dist/chunks.js +54 -0
  10. package/dist/clis.js +33 -15
  11. package/dist/connect.js +121 -0
  12. package/dist/connection.js +25 -10
  13. package/dist/content.js +106 -0
  14. package/dist/conversations.js +251 -158
  15. package/dist/copies.js +1260 -599
  16. package/dist/earlier.js +273 -0
  17. package/dist/end.js +6 -9
  18. package/dist/events.js +544 -297
  19. package/dist/files.js +77 -11
  20. package/dist/git.js +0 -70
  21. package/dist/grok.js +39 -0
  22. package/dist/home.js +86 -22
  23. package/dist/house-cli.js +145 -162
  24. package/dist/instructions.js +2 -4
  25. package/dist/keys.js +238 -0
  26. package/dist/kit-cli.js +14 -4
  27. package/dist/login.js +34 -20
  28. package/dist/logout.js +2 -2
  29. package/dist/manifest.js +75 -0
  30. package/dist/mcp.js +43 -7
  31. package/dist/owner.js +46 -0
  32. package/dist/placement.js +0 -6
  33. package/dist/refusals.js +50 -23
  34. package/dist/resident.js +117 -76
  35. package/dist/scripts.js +79 -15
  36. package/dist/secret-input.js +1 -1
  37. package/dist/settings.js +247 -0
  38. package/dist/sign-in.js +49 -4
  39. package/dist/supervisor.js +163 -0
  40. package/dist/transcription.js +5 -2
  41. package/dist/update.js +31 -22
  42. package/dist/wire.js +2 -10
  43. package/dist/work.js +27 -2
  44. package/dist/workspace.js +113 -0
  45. package/package.json +12 -8
  46. package/dist/authority-main.js +0 -22
  47. package/dist/chatgpt.js +0 -32
  48. package/dist/configure-main.js +0 -6
  49. package/dist/enrol-main.js +0 -10
  50. package/dist/git-main.js +0 -153
  51. package/dist/parts.js +0 -82
  52. package/dist/service-main.js +0 -9
  53. package/dist/service.js +0 -55
  54. package/dist/translate.js +0 -45
@@ -1,17 +1,20 @@
1
+ import { KIT_CONNECTION_PATH, KIT_CONNECTION_PROTOCOL_PREFIX, KIT_WIRE, } from '@agentshouse/kit-wire';
1
2
  import { subscribe } from 'node:diagnostics_channel';
3
+ import { setTimeout as delay } from 'node:timers/promises';
2
4
  import { HouseRefusal, retryDelay } from './api.js';
3
5
  import { end, REPLACED, signInAgain } from './end.js';
4
6
  import { readEnrolment } from './home.js';
5
7
  import { relayed } from './relay.js';
6
8
  import { Silence } from './silence.js';
7
- import { endUnserved, KIT_WIRE, unserved } from './wire.js';
8
- const PATH = '/kit/stream';
9
- const SUBPROTOCOL = `house.kit.stream.${KIT_WIRE}`;
9
+ import { endUnserved, unserved } from './wire.js';
10
+ const SUBPROTOCOL = `${KIT_CONNECTION_PROTOCOL_PREFIX}${KIT_WIRE}`;
10
11
  const REFUSAL_HEADER = 'x-house-refusal';
11
12
  // House beats each Kit connection every thirty seconds, so two silent beats mean this Kit process cannot hear House.
12
13
  export const CONNECTION_SILENCE_MS = 60_000;
14
+ // A stopping Kit waits up to two seconds for House to answer its close, so what it sent last reaches House and a House that does not answer holds no stop.
15
+ const CLOSE_ANSWER_MS = 2_000;
13
16
  function connectionUrl(house) {
14
- const url = new URL(PATH, house);
17
+ const url = new URL(KIT_CONNECTION_PATH, house);
15
18
  url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
16
19
  return url;
17
20
  }
@@ -25,17 +28,18 @@ function header(headers, name) {
25
28
  let refused = null;
26
29
  subscribe('undici:request:headers', (message) => {
27
30
  const { request, response } = message;
28
- if (!request.path.endsWith(PATH))
31
+ if (!request.path.endsWith(KIT_CONNECTION_PATH))
29
32
  return;
30
- refused = new HouseRefusal(PATH, response.statusCode, header(response.headers, REFUSAL_HEADER) || response.statusText);
33
+ refused = new HouseRefusal(KIT_CONNECTION_PATH, response.statusCode, header(response.headers, REFUSAL_HEADER) || response.statusText);
31
34
  });
32
35
  export function holdConnection(handlers) {
33
36
  let connection = null;
34
37
  let socket = null;
35
38
  let failures = 0;
39
+ let stopping = false;
36
40
  const connect = async () => {
37
41
  const { house, credential } = (await readEnrolment());
38
- if (socket !== null || unserved())
42
+ if (socket !== null || unserved() || stopping)
39
43
  return;
40
44
  refused = null;
41
45
  const opening = new WebSocket(relayed(connectionUrl(house)), {
@@ -54,6 +58,8 @@ export function holdConnection(handlers) {
54
58
  connection = null;
55
59
  handlers.closed();
56
60
  process.stderr.write(`kit: connection closed ${event.code} ${event.reason}\n`);
61
+ if (stopping)
62
+ return;
57
63
  if (event.reason === 'replaced')
58
64
  end(REPLACED);
59
65
  if (event.reason === 'credential_rejected' || refused?.status === 401) {
@@ -88,18 +94,27 @@ export function holdConnection(handlers) {
88
94
  if (arrived.type === 'input')
89
95
  handlers.call(arrived);
90
96
  else if (arrived.type !== 'beat')
91
- handlers.notification(arrived);
97
+ handlers.oneWayCall(arrived);
92
98
  };
93
99
  opening.onerror = () => undefined;
94
100
  opening.onclose = closed;
95
101
  };
96
102
  void connect();
97
103
  return {
98
- send(notification) {
104
+ send(oneWayCall) {
99
105
  if (connection === null || connection.readyState !== WebSocket.OPEN)
100
106
  return false;
101
- connection.send(JSON.stringify(notification));
107
+ connection.send(JSON.stringify(oneWayCall));
102
108
  return true;
103
109
  },
110
+ close() {
111
+ stopping = true;
112
+ const open = socket;
113
+ if (open === null)
114
+ return Promise.resolve();
115
+ const answered = new Promise((resolve) => open.addEventListener('close', () => resolve(), { once: true }));
116
+ open.close();
117
+ return Promise.race([answered, delay(CLOSE_ANSWER_MS)]);
118
+ },
104
119
  };
105
120
  }
@@ -0,0 +1,106 @@
1
+ import { applied } from '@agentshouse/session-content';
2
+ function filesOf(files) {
3
+ const held = new Map();
4
+ for (const { path, added, removed } of files) {
5
+ const before = held.get(path);
6
+ held.set(path, { added: (before?.added ?? 0) + added, removed: (before?.removed ?? 0) + removed });
7
+ }
8
+ return held;
9
+ }
10
+ function none() {
11
+ return { type: 'counts', commands: 0, edited: 0, other: 0, added: 0, removed: 0, failed: 0 };
12
+ }
13
+ export class Content {
14
+ parts = [];
15
+ emit;
16
+ calls = new Map();
17
+ writing = null;
18
+ stretch = null;
19
+ constructor(emit) {
20
+ this.emit = emit;
21
+ }
22
+ holds(key) {
23
+ return this.calls.has(key);
24
+ }
25
+ write(id, text) {
26
+ if (text === '')
27
+ return;
28
+ if (this.writing !== null && this.writing.id === id) {
29
+ this.apply({ op: 'append', index: this.writing.index, text });
30
+ return;
31
+ }
32
+ this.stretch = null;
33
+ this.writing = { id, index: this.started({ type: 'text', text }) };
34
+ }
35
+ add(part) {
36
+ this.writing = null;
37
+ this.started(part);
38
+ }
39
+ arrived(part) {
40
+ this.started(part);
41
+ }
42
+ call(key, parent, called) {
43
+ const held = this.calls.get(key);
44
+ if (held !== undefined) {
45
+ held.files = filesOf(called.files);
46
+ held.failed = called.failed;
47
+ this.settle(this.tally());
48
+ return;
49
+ }
50
+ if (parent !== null && !this.calls.has(parent))
51
+ return;
52
+ if (parent === null)
53
+ this.writing = null;
54
+ const stretch = parent === null ? this.stretch : this.calls.get(parent).stretch;
55
+ const at = stretch ?? this.parts.length;
56
+ this.calls.set(key, { stretch: at, kind: called.kind, files: filesOf(called.files), failed: called.failed });
57
+ const tallied = this.tally();
58
+ if (stretch === null)
59
+ this.stretch = this.started(tallied.get(at));
60
+ this.settle(tallied);
61
+ }
62
+ operations() {
63
+ return this.parts.map((part, index) => ({ op: 'start', index, part }));
64
+ }
65
+ tally() {
66
+ const stretches = new Map();
67
+ const first = new Set();
68
+ for (const call of this.calls.values()) {
69
+ const counts = stretches.get(call.stretch) ?? none();
70
+ stretches.set(call.stretch, counts);
71
+ if (call.kind === 'command')
72
+ counts.commands += 1;
73
+ if (call.kind === 'other')
74
+ counts.other += 1;
75
+ if (call.failed)
76
+ counts.failed += 1;
77
+ if (call.kind !== 'edit')
78
+ continue;
79
+ if (call.files.size === 0)
80
+ counts.edited += 1;
81
+ for (const [path, lines] of call.files) {
82
+ if (!first.has(path))
83
+ counts.edited += 1;
84
+ first.add(path);
85
+ counts.added += lines.added;
86
+ counts.removed += lines.removed;
87
+ }
88
+ }
89
+ return stretches;
90
+ }
91
+ settle(tallied) {
92
+ for (const [index, counts] of tallied) {
93
+ if (JSON.stringify(counts) !== JSON.stringify(this.parts[index]))
94
+ this.apply({ op: 'set', index, part: counts });
95
+ }
96
+ }
97
+ started(part) {
98
+ const index = this.parts.length;
99
+ this.apply({ op: 'start', index, part });
100
+ return index;
101
+ }
102
+ apply(operation) {
103
+ this.parts = applied(this.parts, operation);
104
+ this.emit(operation);
105
+ }
106
+ }