@johngaynor/ocnus 2.0.1-next.80 → 2.0.1-next.89

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.
@@ -0,0 +1,526 @@
1
+ // Needs you: the questions that wait on the human in the ocnus space that is
2
+ // showing, most urgent first, each answered with a click. It fills the right
3
+ // column of the vibe layout.
4
+ //
5
+ // What waits, and what answers it, are the ocnus command's to say:
6
+ // `ocnus task asks <space> --json` lists each question with its card, its
7
+ // kind, the step its agent is at, how long it has waited, its text and its
8
+ // options, and `ocnus task answer <space> <card> <question> <option>`
9
+ // answers one, with no one typing in the agent's tab. This mod draws the list
10
+ // and runs that command, and holds no rules about questions: which questions
11
+ // wait, what kind each is and whether an answer was taken all arrive from the
12
+ // command. The one thing it orders is the merges, which it moves after the
13
+ // rest. The cards' titles come from `ocnus task ls <space> --json`.
14
+ //
15
+ // A permission prompt, an approval and a merge show their options as buttons
16
+ // in a row, and a merge has an Open PR button for each of its open pull
17
+ // requests, which opens it in the browser and answers nothing. An agent's own
18
+ // list of options is a stack of numbered rows, the last of them Something
19
+ // else, which answers nothing either: it sets Herdr's shared value ocnus.open
20
+ // to the card, and the opened agent shows the card it names.
21
+ import { View, div, svg } from "gpui-kit";
22
+ import { v_flex, h_flex } from "gpui-base";
23
+ import { exec, spaces, panes, terminals, theme, window, checking, appHome, setShared } from "herdr";
24
+
25
+ // How often to see whether another space is showing, or Herdr's tabs in it
26
+ // have changed. A change in them is read at once, so a question shows within
27
+ // this and a read.
28
+ const LOOK_MS = 1000;
29
+ const ASKS_MS = 4000; // how often to read the questions again
30
+ // A look comes about every LOOK_MS, a little early or late, so the questions
31
+ // are read again at the fourth look after the last read began, counted from
32
+ // its start: a read that waits ASKS_MS from its end comes every 5 seconds.
33
+ const DUE_MS = ASKS_MS - LOOK_MS / 2;
34
+ const TITLES_MS = 60000; // how often to read the spaces and the cards' titles again
35
+ const TIMEOUT_MS = 10000;
36
+ const LIST_TIMEOUT_MS = 30000; // task ls waits up to 20s on Linear
37
+ // task answer waits up to 30s for an agent to end its turn and 15s for it to
38
+ // start on the answer, and a merge asks GitHub.
39
+ const ANSWER_TIMEOUT_MS = 120000;
40
+ // How this mod's own link into the repo that ships it ends.
41
+ const LINKED_AT = "/herdr/mods/needs-you";
42
+ const SPINNER = "mods/workspace-chrome/spinner.svg";
43
+
44
+ // The lines that stand in place of the questions.
45
+ const NOT_A_SPACE = "This space is not an ocnus space.";
46
+ const OLD_CLI = "This ocnus cannot list the questions. Run: ocnus update";
47
+ const NOTHING = "Nothing needs you.";
48
+
49
+ // The word each kind of question shows with.
50
+ const WORDS = new Map([["permission", "Permission"], ["approval", "Approve?"], ["choice", "Asking"], ["merge", "Merge?"]]);
51
+ // The kinds whose options are buttons. Any other kind's are rows.
52
+ const HAS_BUTTONS = new Set(["permission", "approval", "merge"]);
53
+ // What the options of a permission prompt and of a merge read on their
54
+ // buttons. Any other option of theirs has no button. An approval's options
55
+ // read as ocnus gives them, with a capital.
56
+ const BUTTONS = new Map([
57
+ ["permission", new Map([["allow", "Yes"], ["refuse", "No"], ["allow from now on", "Yes, and don't ask again"]])],
58
+ ["merge", new Map([["merge", "Merge"], ["a change you want made", "Ask for changes"]])],
59
+ ]);
60
+ // The last row of an agent's own options, which opens the agent.
61
+ const SOMETHING_ELSE = "Something else";
62
+
63
+ // `herdr-app check` draws the mod once, with no space showing, which would
64
+ // draw one line of text and none of the questions. It is given these to draw
65
+ // instead, so a mistake in an entry fails the check: a question of each kind,
66
+ // given out of order, with the merge first; a permission prompt with two
67
+ // options and one with three; a merge with two pull requests; a choice whose
68
+ // own last option is Something else; one being sent, one sent and one
69
+ // refused; and a card with no title. It draws each line under them too.
70
+ const SAMPLE = {
71
+ space: "demo",
72
+ questions: [
73
+ { id: "41", card: "ABC-1", kind: "merge", step: "pr", seconds: 7300,
74
+ text: "Merge https://github.com/acme/api/pull/7 and https://github.com/acme/web/pull/3. ocnus's watch sees the merge on GitHub and records it; tell me here if you want a change instead.",
75
+ summary: "Opened both pull requests and moved ABC-1 to In Review.",
76
+ options: ["merge", "a change you want made", "stop here"],
77
+ pulls: ["https://github.com/acme/api/pull/7", "https://github.com/acme/web/pull/3"] },
78
+ { id: "pa1b2c3d4", card: "ABC-2", kind: "permission", step: "review", seconds: 30,
79
+ text: "Allow Bash to run: npm test -- orders", summary: null, options: ["allow", "refuse", "allow from now on"] },
80
+ { id: "43", card: "ABC-3", kind: "approval", step: "breakdown", seconds: 200,
81
+ text: "Approve ABC-3 as specified, planned and broken down, and start its build phase?",
82
+ summary: "One piece: one mod and one command.",
83
+ options: ["approve", "change breakdown", "change plan", "change specification"] },
84
+ { id: "44", card: "ABC-4", kind: "choice", step: "plan", seconds: 360,
85
+ text: "Prices arrive in USD, GBP and EUR. How should the importer store them?", summary: null,
86
+ options: ["store each price in its own currency", "convert each to USD as it comes in",
87
+ "keep the price as it came and a USD column beside it, so that the charts stay simple and nothing is lost",
88
+ "something else"] },
89
+ { id: "pe5f6a7b8", card: "ABC-5", kind: "permission", step: null, seconds: 90000,
90
+ text: "Allow Edit on the file: /repo/app.ts", summary: null, options: ["allow", "refuse"] },
91
+ { id: "46", card: "ABC-6", kind: "choice", step: "pr", seconds: 600,
92
+ text: "https://github.com/acme/web/pull/8 is in conflict with main in app.ts. Keep both exports?",
93
+ summary: "The tests fail after the merge of main.", options: ["keep both exports", "keep main's", "stop here"] },
94
+ { id: "47", card: "ABC-7", kind: "approval", step: "breakdown", seconds: 4000,
95
+ text: "Approve ABC-7 as specified, planned and broken down, and start its build phase?", summary: null,
96
+ options: ["approve", "change breakdown", "change plan", "change specification"] },
97
+ { id: "p0c1d2e3f", card: "ABC-8", kind: "permission", step: "build", seconds: 15,
98
+ text: "Allow Bash to run: git push", summary: null, options: ["allow", "refuse"] },
99
+ ],
100
+ titles: {
101
+ "ABC-1": "Export orders as CSV",
102
+ "ABC-2": "Orders page filters",
103
+ "ABC-3": "Held cards show their blockers",
104
+ "ABC-4": "Coach price snapshot importer",
105
+ "ABC-6": "Retry a failed Linear sync",
106
+ "ABC-7": "Space colours in the sidebar",
107
+ "ABC-8": "Push the release notes",
108
+ },
109
+ answers: {
110
+ "demo/ABC-6/46": { state: "sending", label: "keep both exports" },
111
+ "demo/ABC-7/47": { state: "sent", label: "Approve" },
112
+ "demo/ABC-8/p0c1d2e3f": { state: "failed", label: "Yes",
113
+ said: "ABC-8's permission prompt p0c1d2e3f no longer waits: its terminal, t8, is no longer at a prompt, so it was answered in the tab. Nothing was sent." },
114
+ },
115
+ };
116
+
117
+ function parse(text) {
118
+ try {
119
+ return JSON.parse(text);
120
+ } catch {
121
+ return null;
122
+ }
123
+ }
124
+
125
+ // What a failed command said, without the name it starts its errors with.
126
+ const said = (r) => (r.stderr || r.stdout).trim().replace(/^ocnus: /, "");
127
+ // An ocnus from before `task asks` answers with its usage line, not JSON.
128
+ const failure = (r, what) => {
129
+ const text = said(r);
130
+ return text.startsWith("usage:") ? OLD_CLI : text || `${what} failed`;
131
+ };
132
+
133
+ // The shown space's folder, when that space is on this computer: the only
134
+ // place an ocnus space can be.
135
+ function shownFolder() {
136
+ const space = spaces().find((s) => s.active);
137
+ return space && !space.machine ? space.folder : null;
138
+ }
139
+
140
+ // What Herdr shows of the tabs in the spaces whose folder is `folder`, on
141
+ // this computer: each tab's id and name, and each of its terminals' status
142
+ // and app. An agent that asks, or is answered in its tab, changes these, so a
143
+ // change in them is read at once. A pane's status has no word for hc's error,
144
+ // so each terminal's own word, from terminals(), is in it too.
145
+ function herdrSees(folder) {
146
+ if (folder === null) return "";
147
+ const word = new Map(terminals().map((t) => [t.address, t.status ?? null]));
148
+ const tabs = new Set();
149
+ const seen = [];
150
+ for (const space of spaces()) {
151
+ if (space.machine || space.folder !== folder) continue;
152
+ for (const tab of space.tabs) {
153
+ tabs.add(tab.id);
154
+ seen.push([tab.id, tab.name]);
155
+ }
156
+ }
157
+ for (const p of panes()) {
158
+ if (p.terminal && tabs.has(p.tab)) seen.push([p.tab, p.terminal, p.status, p.app, word.get(p.terminal) ?? null]);
159
+ }
160
+ return JSON.stringify(seen);
161
+ }
162
+
163
+ // Most urgent first: the questions in ocnus's order, oldest first, with the
164
+ // merges moved after the rest. Nothing else is put out of ocnus's order.
165
+ const ordered = (questions) => [
166
+ ...questions.filter((q) => q.kind !== "merge"),
167
+ ...questions.filter((q) => q.kind === "merge"),
168
+ ];
169
+
170
+ // What became of a click is kept by the question: its space, its card and
171
+ // its id.
172
+ const keyOf = (space, q) => `${space}/${q.card}/${q.id}`;
173
+
174
+ // How long a question has waited, from its seconds: in whole minutes, then
175
+ // whole hours, then whole days.
176
+ function waited(seconds) {
177
+ const s = Math.max(0, Math.floor(Number(seconds) || 0));
178
+ if (s < 60) return "<1m";
179
+ if (s < 3600) return `${Math.floor(s / 60)}m`;
180
+ if (s < 86400) return `${Math.floor(s / 3600)}h`;
181
+ return `${Math.floor(s / 86400)}d`;
182
+ }
183
+
184
+ // A pull request by its repo and number, as acme/api#7.
185
+ function pullName(url) {
186
+ const m = url.match(/^https?:\/\/[^/]+\/(.+?)\/pull\/(\d+)/);
187
+ return m ? `${m[1]}#${m[2]}` : url;
188
+ }
189
+
190
+ // A question's options, as ocnus gives them.
191
+ const optionsOf = (q) => (Array.isArray(q.options) ? q.options.filter((o) => typeof o === "string") : []);
192
+
193
+ // The buttons of a permission prompt, an approval or a merge: [label, option]
194
+ // for each option that has one, in the order ocnus gives them.
195
+ function choices(q) {
196
+ const options = optionsOf(q);
197
+ if (q.kind === "approval") return options.map((o) => [o.charAt(0).toUpperCase() + o.slice(1), o]);
198
+ const names = BUTTONS.get(q.kind);
199
+ return names ? options.filter((o) => names.has(o)).map((o) => [names.get(o), o]) : [];
200
+ }
201
+
202
+ // An agent's own options, for its rows, in its order. An own last option
203
+ // that reads Something else, in any case, is left out: it is the last row.
204
+ function ownOptions(q) {
205
+ const options = optionsOf(q);
206
+ const last = options[options.length - 1];
207
+ return last?.trim().toLowerCase() === SOMETHING_ELSE.toLowerCase() ? options.slice(0, -1) : options;
208
+ }
209
+
210
+ export default class NeedsYou extends View {
211
+ init(_props, cx) {
212
+ this.cmd = "ocnus"; // the one on PATH, until that cannot start
213
+ this.listed = null; // the spaces ocnus listed last
214
+ this.listedAt = 0;
215
+ this.key = null; // the folder that the questions on hand were read for
216
+ this.space = null; // the ocnus space the shown Herdr space is, or null
217
+ this.questions = null; // the questions read last, in ocnus's order
218
+ this.error = null;
219
+ this.busy = false;
220
+ this.readAt = 0;
221
+ this.seen = null; // what Herdr showed of the space's tabs when they were read
222
+ this.again = false; // read again at the next look, as once an answer has ended
223
+ this.drawn = null; // what the last redraw showed, to redraw only on a change
224
+ // The titles of the open cards of the space titlesFor, by card ID.
225
+ this.titles = new Map();
226
+ this.titlesFor = null;
227
+ this.titlesAt = 0;
228
+ this.titlesBusy = false;
229
+ // What became of each click, by question (keyOf): sending, sent, or
230
+ // failed with what ocnus said. Kept until ocnus no longer lists the
231
+ // question.
232
+ this.answers = new Map();
233
+ if (checking()) return;
234
+ cx.timer.every(LOOK_MS, (cx) => this.look(cx));
235
+ this.look(cx);
236
+ }
237
+
238
+ // Runs the ocnus command. With `fallback`, a command that cannot run is
239
+ // tried again as the copy this mod is linked from (Herdr may have been
240
+ // started without the user's PATH). Only the readings ask for that: they
241
+ // only read, and they have run before there is a question to answer. An
242
+ // answer never does, because its failure may be a timeout with the first
243
+ // run still answering.
244
+ async ocnus(args, { fallback = false, timeout = TIMEOUT_MS } = {}) {
245
+ try {
246
+ return await exec(this.cmd, args, { timeout });
247
+ } catch (e) {
248
+ if (!fallback || this.cmd !== "ocnus") throw e;
249
+ const link = await exec("readlink", [`${appHome()}/mods/needs-you`]);
250
+ const at = link.stdout.trim();
251
+ if (link.code !== 0 || !at.endsWith(LINKED_AT)) throw e;
252
+ this.cmd = `${at.slice(0, -LINKED_AT.length)}/cli/ocnus`;
253
+ return exec(this.cmd, args, { timeout });
254
+ }
255
+ }
256
+
257
+ // Read the questions again when another space is showing, when Herdr's
258
+ // tabs in it have changed, once an answer has ended, and every 4 seconds:
259
+ // so is a read that failed, which is tried again. A Herdr space that is not
260
+ // an ocnus space is looked up again only as often as the spaces are
261
+ // listed. Nothing is read while nobody can see the window.
262
+ look(cx) {
263
+ if (window()?.visible === false) return;
264
+ this.lookTitles(cx);
265
+ if (this.busy) return;
266
+ const folder = shownFolder();
267
+ const key = folder ?? "";
268
+ const seen = herdrSees(folder);
269
+ const stale = this.space || this.error
270
+ ? this.again || Date.now() - this.readAt >= DUE_MS || seen !== this.seen
271
+ : Date.now() - this.readAt >= TITLES_MS;
272
+ if (key === this.key && !stale) return;
273
+ this.busy = true;
274
+ this.again = false;
275
+ const started = Date.now();
276
+ cx.spawn(async (cx) => {
277
+ let space = null;
278
+ let questions = null;
279
+ let error = null;
280
+ try {
281
+ if (folder !== null && (!this.listed || key !== this.key || Date.now() - this.listedAt >= TITLES_MS)) {
282
+ const r = await this.ocnus(["space", "ls", "--json"], { fallback: true });
283
+ const list = r.code === 0 ? parse(r.stdout)?.spaces : null;
284
+ if (Array.isArray(list)) {
285
+ this.listed = list;
286
+ this.listedAt = Date.now();
287
+ } else {
288
+ error = r.code === 0 ? OLD_CLI : failure(r, "ocnus space ls");
289
+ }
290
+ }
291
+ if (folder !== null && !error) space = (this.listed ?? []).find((s) => s.folder === folder)?.name ?? null;
292
+ if (space) {
293
+ const r = await this.ocnus(["task", "asks", space, "--json"], { fallback: true });
294
+ const got = r.code === 0 ? parse(r.stdout) : null;
295
+ if (Array.isArray(got?.questions)) questions = got.questions.filter((q) => q !== null && typeof q === "object");
296
+ else error = r.code === 0 ? OLD_CLI : failure(r, "ocnus task asks");
297
+ }
298
+ } catch (e) {
299
+ error = `Could not run ocnus: ${e?.message ?? e}`;
300
+ }
301
+ // A question ocnus no longer lists takes what became of its click with
302
+ // it. One still being sent keeps it until its answer has ended.
303
+ if (space && questions) {
304
+ const listed = new Set(questions.map((q) => keyOf(space, q)));
305
+ for (const [k, a] of this.answers) {
306
+ if (k.startsWith(`${space}/`) && a.state !== "sending" && !listed.has(k)) this.answers.delete(k);
307
+ }
308
+ }
309
+ this.key = key;
310
+ this.space = space;
311
+ this.questions = questions;
312
+ this.error = error;
313
+ this.readAt = started;
314
+ this.seen = seen;
315
+ this.busy = false;
316
+ this.redraw(cx);
317
+ this.lookTitles(cx);
318
+ // A space chosen, or an answer ended, while this was being read is
319
+ // read now, not at the next look.
320
+ if ((shownFolder() ?? "") !== key || this.again) this.look(cx);
321
+ });
322
+ }
323
+
324
+ // Read the cards' titles when another space shows and every minute, on
325
+ // their own: `task ls` asks Linear, which may be slow or out of reach, and
326
+ // the questions never wait for it. They are read only for the space that
327
+ // is showing, once its questions have been.
328
+ lookTitles(cx) {
329
+ const space = this.space;
330
+ if (this.titlesBusy || !space || (shownFolder() ?? "") !== this.key) return;
331
+ if (space === this.titlesFor && Date.now() - this.titlesAt < TITLES_MS) return;
332
+ this.titlesBusy = true;
333
+ cx.spawn(async (cx) => {
334
+ const titles = await this.readTitles(space);
335
+ // A read that failed leaves the titles the space's last read gave.
336
+ if (titles || space !== this.titlesFor) this.titles = titles ?? new Map();
337
+ this.titlesFor = space;
338
+ this.titlesAt = Date.now();
339
+ this.titlesBusy = false;
340
+ this.redraw(cx);
341
+ });
342
+ }
343
+
344
+ // The titles of the space's open cards, by ID, or null when `task ls`
345
+ // cannot list them.
346
+ async readTitles(space) {
347
+ try {
348
+ const r = await this.ocnus(["task", "ls", space, "--json"], { fallback: true, timeout: LIST_TIMEOUT_MS });
349
+ const statuses = r.code === 0 ? parse(r.stdout)?.statuses : null;
350
+ if (!Array.isArray(statuses)) return null;
351
+ const titles = new Map();
352
+ for (const s of statuses) {
353
+ for (const c of Array.isArray(s?.cards) ? s.cards : []) {
354
+ if (typeof c?.id === "string" && typeof c.title === "string") titles.set(c.id.toUpperCase(), c.title);
355
+ }
356
+ }
357
+ return titles;
358
+ } catch {
359
+ return null;
360
+ }
361
+ }
362
+
363
+ // A redraw of a mod redraws the whole window, so only when what it draws
364
+ // has changed. How long a question has waited is drawn in whole minutes.
365
+ redraw(cx) {
366
+ const titles = this.titlesFor === this.space ? this.titles : new Map();
367
+ const drawn = JSON.stringify([this.key, this.space, this.error, (this.questions ?? []).map((q) =>
368
+ [q.id, q.card, q.kind, q.step, waited(q.seconds), q.text, q.summary, q.options, q.pulls,
369
+ titles.get(String(q.card ?? "").toUpperCase()) ?? null])]);
370
+ if (drawn !== this.drawn) {
371
+ this.drawn = drawn;
372
+ cx.notify();
373
+ }
374
+ }
375
+
376
+ // Answers a question with one of its options, by `task answer`, with no
377
+ // fallback, as the Cards panel's Start runs `task new`: one that timed out
378
+ // may have answered. From the click the question shows the choice and takes
379
+ // no second answer. Exit 0 is ocnus saying the answer was taken, which
380
+ // shows as ✓ Sent; anything else puts the buttons or rows back, under what
381
+ // ocnus said. Each question has its own state, so one being sent holds no
382
+ // other. The questions are read again as soon as it has ended.
383
+ answer(space, q, option, label, cx) {
384
+ const key = keyOf(space, q);
385
+ const was = this.answers.get(key)?.state;
386
+ if (was === "sending" || was === "sent") return;
387
+ this.answers.set(key, { state: "sending", label });
388
+ cx.notify();
389
+ cx.spawn(async (cx) => {
390
+ let failed = null;
391
+ try {
392
+ const r = await this.ocnus(["task", "answer", space, q.card, q.id, option], { timeout: ANSWER_TIMEOUT_MS });
393
+ if (r.code !== 0) failed = said(r) || "ocnus task answer failed";
394
+ } catch (e) {
395
+ failed = `Could not run ocnus: ${e?.message ?? e}`;
396
+ }
397
+ this.answers.set(key, failed === null ? { state: "sent", label } : { state: "failed", label, said: failed });
398
+ this.again = true;
399
+ cx.notify();
400
+ this.look(cx);
401
+ });
402
+ }
403
+
404
+ // Something else: opens the card's agent on its conversation, where the
405
+ // human answers in their own words, by setting Herdr's shared value
406
+ // ocnus.open to the card, which the opened agent shows. It answers nothing
407
+ // and runs no command, and with no opened agent installed nothing reads it.
408
+ open(space, q) {
409
+ setShared("ocnus.open", { space, card: q.card });
410
+ }
411
+
412
+ render() {
413
+ const t = theme();
414
+ const sample = checking();
415
+ const space = sample ? SAMPLE.space : this.space;
416
+ // Until the space that is showing has been read, nothing is drawn for it.
417
+ const fresh = sample || (shownFolder() ?? "") === this.key;
418
+ const questions = sample ? SAMPLE.questions : fresh && !this.error ? this.questions : null;
419
+ const titles = sample ? new Map(Object.entries(SAMPLE.titles)) : this.titlesFor === space ? this.titles : new Map();
420
+ const answers = sample ? new Map(Object.entries(SAMPLE.answers)) : this.answers;
421
+ const entries = () => ordered(questions).map((q) => this.entry(space, q,
422
+ titles.get(String(q.card ?? "").toUpperCase()) ?? null, answers.get(keyOf(space, q)) ?? null, t));
423
+ // The one line that stands in place of the questions.
424
+ const line = (text, color) => div().py(4).text_size(12.5).text_color(color).whitespace_normal().child(text);
425
+ let items;
426
+ if (sample) items = [...entries(), line(NOT_A_SPACE, t.dim), line(OLD_CLI, t.blocked), line(NOTHING, t.dim)];
427
+ else if (!fresh) items = [line("Reading…", t.dim)];
428
+ else if (this.error) items = [line(this.error, t.blocked)];
429
+ else if (!space) items = [line(NOT_A_SPACE, t.dim)];
430
+ else if (!questions?.length) items = [line(NOTHING, t.dim)];
431
+ else items = entries();
432
+ const heading = space && Array.isArray(questions) ? `Needs you · ${questions.length}` : "Needs you";
433
+ return v_flex().size_full().min_h(0).bg(t.background).text_size(13).text_color(t.foreground)
434
+ .child(h_flex().h(46).flex_none().px(20).items_center().border_b(1).border_color(t.border)
435
+ .child(div().text_size(14).font_semibold().text_color(questions?.length ? t.blocked : t.muted).child(heading)))
436
+ .child(v_flex().id("needs-you-list").flex_1().min_h(0).px(20).py(16).gap(12)
437
+ .overflow_y_scrollbar().mode("always").children(items));
438
+ }
439
+
440
+ // A question: its card's ID and title, the step its agent is at, how long
441
+ // it has waited and its kind's word, then its text and any summary, then
442
+ // its buttons or rows, or what became of a click on them. `title` is null
443
+ // for a card `task ls` did not list. `st` is what became of a click, or
444
+ // null.
445
+ entry(space, q, title, st, t) {
446
+ const color = q.kind === "merge" ? t.done : t.blocked;
447
+ const taken = st?.state === "sending" || st?.state === "sent";
448
+ const when = [q.step, waited(q.seconds)].filter(Boolean).join(" · ");
449
+ return v_flex().id(`ask-${q.id}`).flex_none().gap(10).px(14).py(12).rounded(10)
450
+ .bg(t.surface).border_1().border_color(color + "55")
451
+ .child(h_flex().gap(8).items_center()
452
+ .child(div().flex_none().text_size(11.5).text_color(t.dim).child(String(q.card ?? "")))
453
+ .child(div().id(`ask-title-${q.id}`).flex_1().min_w(0).truncate().font_medium().tooltip(title ?? "")
454
+ .child(title ?? ""))
455
+ .child(div().flex_none().text_size(11.5).text_color(t.muted).child(when))
456
+ .child(div().flex_none().text_size(11.5).font_semibold().text_color(color)
457
+ .child(WORDS.get(q.kind) ?? String(q.kind ?? ""))))
458
+ .child(div().whitespace_normal().child(String(q.text ?? "")))
459
+ .when(Boolean(q.summary), (el) => el.child(div().text_size(12).text_color(t.muted).whitespace_normal()
460
+ .child(String(q.summary))))
461
+ .when(st?.state === "failed", (el) => el.child(div().text_size(12).text_color(t.blocked).whitespace_normal()
462
+ .child(st.said)))
463
+ .when(!taken, (el) => el.child(HAS_BUTTONS.has(q.kind) ? this.buttons(space, q, t) : this.rows(space, q, t)))
464
+ .when(taken, (el) => el.child(this.status(st, t)));
465
+ }
466
+
467
+ // A permission prompt's, an approval's or a merge's options as buttons in a
468
+ // row, the first standing out, then a merge's Open PR buttons.
469
+ buttons(space, q, t) {
470
+ const pulls = q.kind === "merge" && Array.isArray(q.pulls) ? q.pulls.filter((u) => typeof u === "string") : [];
471
+ return h_flex().flex_wrap().gap(8).items_center()
472
+ .children(choices(q).map(([label, option], i) => this.button(`ask-${q.id}-${i}`, label, i === 0, t,
473
+ (_e, cx) => this.answer(space, q, option, label, cx))))
474
+ .children(pulls.map((url, i) => this.pull(`ask-${q.id}-pull-${i}`, url, t)));
475
+ }
476
+
477
+ button(id, label, primary, t, action) {
478
+ return h_flex().id(id).h(32).flex_none().px(12).items_center().rounded(8).border_1()
479
+ .border_color(primary ? t.solid : t.input).bg(primary ? t.solid : t.raised)
480
+ .text_color(primary ? t.solid_foreground : t.foreground).text_size(12.5).font_medium()
481
+ .cursor_pointer().hover((s) => s.opacity(0.88)).on_click(action).child(label);
482
+ }
483
+
484
+ // An Open PR button, named by its pull request's repo and number. It opens
485
+ // the pull request in the browser, answers nothing and runs no command.
486
+ pull(id, url, t) {
487
+ return this.button(id, `Open ${pullName(url)} ↗`, false, t, (_e, cx) => {
488
+ try {
489
+ cx.open_url(url);
490
+ } catch {
491
+ // Not a link the system opens.
492
+ }
493
+ });
494
+ }
495
+
496
+ // An agent's own options as a stack of rows, in its order, each with its
497
+ // number and the option as the agent wrote it, on as many lines as it
498
+ // needs; then Something else, numbered after them, which opens the agent.
499
+ rows(space, q, t) {
500
+ const options = ownOptions(q);
501
+ return v_flex().gap(6)
502
+ .children(options.map((o, i) => this.row(`ask-${q.id}-${i}`, i + 1, o, t,
503
+ (_e, cx) => this.answer(space, q, o, o, cx))))
504
+ .child(this.row(`ask-${q.id}-else`, options.length + 1, SOMETHING_ELSE, t, () => this.open(space, q)));
505
+ }
506
+
507
+ row(id, n, text, t, action) {
508
+ return h_flex().id(id).w_full().min_h(40).px(10).py(8).gap(10).items_start().rounded(8)
509
+ .border_1().border_color(t.border).bg(t.background).cursor_pointer()
510
+ .hover((s) => s.bg(t.hover)).on_click(action)
511
+ .child(div().size(22).flex_none().rounded(6).bg(t.raised).flex().items_center().justify_center()
512
+ .text_size(11.5).text_color(t.muted).child(String(n)))
513
+ .child(div().flex_1().min_w(0).pt(2).whitespace_normal().child(text));
514
+ }
515
+
516
+ // What became of a click: the choice beside a spinner while it is sent,
517
+ // then ✓ Sent and the choice once ocnus says the answer was taken.
518
+ status(st, t) {
519
+ if (st.state === "sending") {
520
+ return h_flex().gap(8).items_center().text_size(12.5).text_color(t.muted)
521
+ .child(svg(SPINNER).size(12).flex_none().text_color(t.working).spin(900))
522
+ .child(div().flex_1().min_w(0).whitespace_normal().child(`Sending: ${st.label}`));
523
+ }
524
+ return div().text_size(12.5).text_color(t.done).whitespace_normal().child(`✓ Sent: ${st.label}`);
525
+ }
526
+ }
@@ -0,0 +1 @@
1
+ { "place": "none", "title": "Needs you" }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johngaynor/ocnus",
3
- "version": "2.0.1-next.80",
3
+ "version": "2.0.1-next.89",
4
4
  "description": "A software factory: agent skills and the Herdr setup to run them, installed with one command.",
5
5
  "license": "MIT",
6
6
  "bin": {