angelia-gateway 0.4.3 → 0.4.4

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.
@@ -198,7 +198,12 @@ export function permissionDialog(pane) {
198
198
  const body = raw.slice(top >= 0 ? top + 1 : Math.max(0, at - 6), at).map(clean).filter((l) => l && !/^[\s╌─━┄┈]+$/.test(l));
199
199
  if (!body.length)
200
200
  return { tool: 'tool', preview: 'a permission prompt is open' };
201
- return { tool: body[0].slice(0, 40), preview: (body.slice(1).join(' · ') || body[0]).slice(0, 300) };
201
+ // A teammate's request names it after the title: "Bash command · from the restart-relay agent"
202
+ // (claude 2.1.332, seen in a chat's pane 2026-11-25).
203
+ // Only that shape: a title with a "·" of its own says nothing of who asks.
204
+ const asker = /^(.+?)\s*·\s*from\s+((?:the\s+)?[A-Za-z0-9_.-]{1,64}\s+agent)$/.exec(body[0]);
205
+ const tool = (asker ? asker[1] : body[0]).slice(0, 40);
206
+ return { tool, preview: (body.slice(1).join(' · ') || body[0]).slice(0, 300), ...(asker ? { from: asker[2].slice(0, 60) } : {}) };
202
207
  }
203
208
  /** A directory Claude Code has not been run in before opens a trust dialog before the first
204
209
  * prompt, with "No, exit" preselected. Print mode never shows it. */
@@ -9,8 +9,12 @@ import { permissionPreview } from './brain.js';
9
9
  * A dialog on the screen is text a command can shape (a line of `─` in a command reads as the dialog's
10
10
  * top edge), and a key press answers whichever dialog is open by then; neither is used to approve.
11
11
  *
12
- * The hook relays only while a turn from the chat is being read, which `beat` says on every poll.
13
- * Otherwise it steps aside at once, and Claude opens its own dialog in the pane and the Claude app.
12
+ * The hook relays while a turn from the chat is being read, which `beat` says on every poll, and
13
+ * between the chat's turns while the daemon holds the pane (`beat` with `idle`): then the hook adds
14
+ * what started the turn that asks (turnOrigin, as the Stop hook reads it), and the daemon either
15
+ * takes the request (a turn the agent started by itself: the profile's `permissions` decide) or tells
16
+ * the hook to step aside (`pass`: a turn a person typed in the Claude app or the pane). With no
17
+ * heartbeat at all it steps aside at once, and Claude opens its own dialog in the pane and the app.
14
18
  */
15
19
  /** A heartbeat older than this means nobody reads the chat. The hook has the same number. Only a
16
20
  * daemon that died or stopped for a restart leaves one to go stale (every other end of a turn removes
@@ -40,8 +44,9 @@ export class PermissionRelay {
40
44
  this.open.clear();
41
45
  this.seen.clear();
42
46
  }
43
- /** While a turn is read from the chat, every RELAY_BEAT_MS. */
44
- beat(now = Date.now()) { this.put('relay.json', { at: now }); }
47
+ /** While a turn is read from the chat, every RELAY_BEAT_MS. `idle`: between the chat's turns, while
48
+ * the daemon holds the pane: the hook then says what started the turn that asks. */
49
+ beat(now = Date.now(), idle = false) { this.put('relay.json', idle ? { at: now, idle: true } : { at: now }); }
45
50
  /** Nobody reads the chat now: a hook waiting, or asking next, lets Claude open its own dialog. */
46
51
  quiet() { rmSync(join(this.dir, 'relay.json'), { force: true }); }
47
52
  /** Requests the hook wrote since the last call, oldest first. */
@@ -71,9 +76,10 @@ export class PermissionRelay {
71
76
  }
72
77
  this.seen.add(id);
73
78
  this.open.add(id);
74
- out.push({ id, tool: typeof row.tool_name === 'string' && row.tool_name ? row.tool_name : '?', input: row.tool_input, at: Number(row.at) || 0 });
79
+ const origin = originOf(row.origin);
80
+ out.push({ id, tool: typeof row.tool_name === 'string' && row.tool_name ? row.tool_name : '?', input: row.tool_input, at: Number(row.at) || 0, ...(origin ? { origin } : {}) });
75
81
  }
76
- return out.sort((a, b) => a.at - b.at).map(({ id, tool, input, at }) => ({ id, tool, input, ...(at ? { at } : {}) }));
82
+ return out.sort((a, b) => a.at - b.at).map(({ at, ...r }) => ({ ...r, ...(at ? { at } : {}) }));
77
83
  }
78
84
  /** A hook waits on a request now, taken or not: Claude Code draws its own dialog while the hook
79
85
  * runs (measured on claude 2.1.332), so Enter must not be pressed for a message into the turn. */
@@ -107,6 +113,17 @@ export class PermissionRelay {
107
113
  rmSync(join(this.dir, `${id}.answer.json`), { force: true });
108
114
  return false;
109
115
  }
116
+ /** Not the chat's to answer (a turn a person typed, between the chat's turns): the hook steps aside
117
+ * and Claude asks in its own dialog, as with no daemon. False when no hook waits for it any more. */
118
+ pass(id) {
119
+ if (!this.open.delete(id))
120
+ return false;
121
+ const request = join(this.dir, `${id}.request.json`);
122
+ if (!existsSync(request))
123
+ return false;
124
+ this.put(`${id}.answer.json`, { pass: true });
125
+ return true;
126
+ }
110
127
  put(name, value) {
111
128
  mkdirSync(this.dir, { recursive: true, mode: 0o700 });
112
129
  const tmp = join(this.dir, `${name}.tmp`);
@@ -114,6 +131,46 @@ export class PermissionRelay {
114
131
  renameSync(tmp, join(this.dir, name));
115
132
  }
116
133
  }
134
+ /** The hook's `origin` field, kept only in its own shape: plain words, keys, a time, a count. */
135
+ function originOf(v) {
136
+ if (!v || typeof v !== 'object')
137
+ return undefined;
138
+ const o = v;
139
+ const keys = Array.isArray(o.prompt_keys) && o.prompt_keys.length <= 20 && o.prompt_keys.every((k) => typeof k === 'string' && /^[0-9a-f]{1,128}$/.test(k)) ? o.prompt_keys : undefined;
140
+ if (typeof o.origin !== 'string' || !/^[a-z]{1,20}$/.test(o.origin))
141
+ return undefined;
142
+ return {
143
+ origin: o.origin,
144
+ ...(typeof o.origin_why === 'string' ? { why: o.origin_why.replace(/[^\w <>(),-]/g, '').slice(0, 120) } : {}),
145
+ ...(typeof o.prompt_key === 'string' && o.prompt_key.length <= 128 ? { promptKey: o.prompt_key } : {}),
146
+ ...(typeof o.prompts === 'number' && Number.isInteger(o.prompts) ? { prompts: o.prompts } : {}),
147
+ ...(keys ? { promptKeys: keys } : {}),
148
+ ...(typeof o.prompt_at === 'number' && Number.isFinite(o.prompt_at) ? { promptAt: o.prompt_at } : {}),
149
+ ...(typeof o.opened_by === 'string' && /^[a-z]{1,20}$/.test(o.opened_by) ? { openedBy: o.opened_by } : {}),
150
+ ...(words(o.origin_ids) ? { ids: words(o.origin_ids) } : {}),
151
+ ...(words(o.origin_teammates) ? { teammates: words(o.origin_teammates) } : {}),
152
+ ...(typeof o.joins === 'number' && Number.isInteger(o.joins) && o.joins >= 0 ? { joins: o.joins } : {}),
153
+ ...(Array.isArray(o.joined) && o.joined.length <= 20 ? { joined: o.joined.map(joinedOf) } : {}),
154
+ };
155
+ }
156
+ /** One entry of the hook's `joined`, in its own shape; anything else reads `other` (ties nothing). */
157
+ function joinedOf(v) {
158
+ const j = v && typeof v === 'object' ? v : {};
159
+ const ids = words(j.ids), mates = words(j.teammates);
160
+ if (typeof j.kind !== 'string' || !/^[a-z]{1,20}$/.test(j.kind))
161
+ return { kind: 'other' };
162
+ // A list that is there but not in its shape: the row is not known for anyone's work.
163
+ if ((j.ids !== undefined && !ids) || (j.teammates !== undefined && !mates))
164
+ return { kind: 'other' };
165
+ return { kind: j.kind, ...(ids ? { ids } : {}), ...(mates ? { teammates: mates } : {}) };
166
+ }
167
+ /** A list of ids or names as the hook writes them (turn-origin.mjs: TOOL_USE_ID, TEAMMATE_ID): plain
168
+ * words, at most 20. Anything else is no list, and ties the request to nothing. */
169
+ function words(v) {
170
+ if (!Array.isArray(v) || !v.length || v.length > 20)
171
+ return undefined;
172
+ return v.every((x) => typeof x === 'string' && /^[A-Za-z0-9_.-]{1,100}$/.test(x)) ? v : undefined;
173
+ }
117
174
  /** What Claude Code writes as the result of a tool use a person refused in its dialog (No or Esc).
118
175
  * Seen on 2.1.287: `toolUseResult` is this string, and the tool_result has `is_error: true`. */
119
176
  export const REJECTED = 'User rejected tool use';