driverkit 0.0.0-stage → 0.0.1

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 (160) hide show
  1. package/CONTROLS.md +578 -0
  2. package/REACT.md +477 -0
  3. package/README.md +104 -2
  4. package/dist/agent/backend.d.ts +69 -0
  5. package/dist/agent/backend.d.ts.map +1 -0
  6. package/dist/agent/backend.js +130 -0
  7. package/dist/agent/backend.js.map +1 -0
  8. package/dist/agent/control.d.ts +148 -0
  9. package/dist/agent/control.d.ts.map +1 -0
  10. package/dist/agent/control.js +342 -0
  11. package/dist/agent/control.js.map +1 -0
  12. package/dist/agent/index.d.ts +9 -0
  13. package/dist/agent/index.d.ts.map +1 -0
  14. package/dist/agent/index.js +6 -0
  15. package/dist/agent/index.js.map +1 -0
  16. package/dist/agent/loop.d.ts +136 -0
  17. package/dist/agent/loop.d.ts.map +1 -0
  18. package/dist/agent/loop.js +824 -0
  19. package/dist/agent/loop.js.map +1 -0
  20. package/dist/agent/render.d.ts +173 -0
  21. package/dist/agent/render.d.ts.map +1 -0
  22. package/dist/agent/render.js +425 -0
  23. package/dist/agent/render.js.map +1 -0
  24. package/dist/agent/tools.d.ts +138 -0
  25. package/dist/agent/tools.d.ts.map +1 -0
  26. package/dist/agent/tools.js +622 -0
  27. package/dist/agent/tools.js.map +1 -0
  28. package/dist/agent/types.d.ts +566 -0
  29. package/dist/agent/types.d.ts.map +1 -0
  30. package/dist/agent/types.js +32 -0
  31. package/dist/agent/types.js.map +1 -0
  32. package/dist/browser/budget.d.ts +92 -0
  33. package/dist/browser/budget.d.ts.map +1 -0
  34. package/dist/browser/budget.js +151 -0
  35. package/dist/browser/budget.js.map +1 -0
  36. package/dist/browser/connect.d.ts +368 -0
  37. package/dist/browser/connect.d.ts.map +1 -0
  38. package/dist/browser/connect.js +1038 -0
  39. package/dist/browser/connect.js.map +1 -0
  40. package/dist/browser/elsewhere.d.ts +89 -0
  41. package/dist/browser/elsewhere.d.ts.map +1 -0
  42. package/dist/browser/elsewhere.js +91 -0
  43. package/dist/browser/elsewhere.js.map +1 -0
  44. package/dist/browser/frames.d.ts +85 -0
  45. package/dist/browser/frames.d.ts.map +1 -0
  46. package/dist/browser/frames.js +11 -0
  47. package/dist/browser/frames.js.map +1 -0
  48. package/dist/browser/index.d.ts +8 -0
  49. package/dist/browser/index.d.ts.map +1 -0
  50. package/dist/browser/index.js +9 -0
  51. package/dist/browser/index.js.map +1 -0
  52. package/dist/check/check.d.ts +171 -0
  53. package/dist/check/check.d.ts.map +1 -0
  54. package/dist/check/check.js +666 -0
  55. package/dist/check/check.js.map +1 -0
  56. package/dist/check/index.d.ts +3 -0
  57. package/dist/check/index.d.ts.map +1 -0
  58. package/dist/check/index.js +2 -0
  59. package/dist/check/index.js.map +1 -0
  60. package/dist/check/types.d.ts +149 -0
  61. package/dist/check/types.d.ts.map +1 -0
  62. package/dist/check/types.js +29 -0
  63. package/dist/check/types.js.map +1 -0
  64. package/dist/controls/controls.d.ts +198 -0
  65. package/dist/controls/controls.d.ts.map +1 -0
  66. package/dist/controls/controls.js +742 -0
  67. package/dist/controls/controls.js.map +1 -0
  68. package/dist/controls/dom.d.ts +108 -0
  69. package/dist/controls/dom.d.ts.map +1 -0
  70. package/dist/controls/dom.js +56 -0
  71. package/dist/controls/dom.js.map +1 -0
  72. package/dist/controls/index.d.ts +26 -0
  73. package/dist/controls/index.d.ts.map +1 -0
  74. package/dist/controls/index.js +23 -0
  75. package/dist/controls/index.js.map +1 -0
  76. package/dist/controls/rows.d.ts +66 -0
  77. package/dist/controls/rows.d.ts.map +1 -0
  78. package/dist/controls/rows.js +127 -0
  79. package/dist/controls/rows.js.map +1 -0
  80. package/dist/core/index.d.ts +3 -0
  81. package/dist/core/index.d.ts.map +1 -0
  82. package/dist/core/index.js +15 -0
  83. package/dist/core/index.js.map +1 -0
  84. package/dist/core/registry.d.ts +192 -0
  85. package/dist/core/registry.d.ts.map +1 -0
  86. package/dist/core/registry.js +2799 -0
  87. package/dist/core/registry.js.map +1 -0
  88. package/dist/core/types.d.ts +2468 -0
  89. package/dist/core/types.d.ts.map +1 -0
  90. package/dist/core/types.js +19 -0
  91. package/dist/core/types.js.map +1 -0
  92. package/dist/otel/index.d.ts +122 -0
  93. package/dist/otel/index.d.ts.map +1 -0
  94. package/dist/otel/index.js +157 -0
  95. package/dist/otel/index.js.map +1 -0
  96. package/dist/react/index.d.ts +191 -0
  97. package/dist/react/index.d.ts.map +1 -0
  98. package/dist/react/index.js +547 -0
  99. package/dist/react/index.js.map +1 -0
  100. package/dist/react/runtime.d.ts +95 -0
  101. package/dist/react/runtime.d.ts.map +1 -0
  102. package/dist/react/runtime.js +54 -0
  103. package/dist/react/runtime.js.map +1 -0
  104. package/dist/react/store.d.ts +333 -0
  105. package/dist/react/store.d.ts.map +1 -0
  106. package/dist/react/store.js +695 -0
  107. package/dist/react/store.js.map +1 -0
  108. package/dist/relay/auth.d.ts +244 -0
  109. package/dist/relay/auth.d.ts.map +1 -0
  110. package/dist/relay/auth.js +391 -0
  111. package/dist/relay/auth.js.map +1 -0
  112. package/dist/relay/frames.d.ts +141 -0
  113. package/dist/relay/frames.d.ts.map +1 -0
  114. package/dist/relay/frames.js +20 -0
  115. package/dist/relay/frames.js.map +1 -0
  116. package/dist/relay/index.d.ts +8 -0
  117. package/dist/relay/index.d.ts.map +1 -0
  118. package/dist/relay/index.js +4 -0
  119. package/dist/relay/index.js.map +1 -0
  120. package/dist/relay/relay.d.ts +277 -0
  121. package/dist/relay/relay.d.ts.map +1 -0
  122. package/dist/relay/relay.js +1026 -0
  123. package/dist/relay/relay.js.map +1 -0
  124. package/dist/relay/serve.d.ts +33 -0
  125. package/dist/relay/serve.d.ts.map +1 -0
  126. package/dist/relay/serve.js +86 -0
  127. package/dist/relay/serve.js.map +1 -0
  128. package/dist/widget/chat.d.ts +290 -0
  129. package/dist/widget/chat.d.ts.map +1 -0
  130. package/dist/widget/chat.js +486 -0
  131. package/dist/widget/chat.js.map +1 -0
  132. package/dist/widget/css.d.ts +49 -0
  133. package/dist/widget/css.d.ts.map +1 -0
  134. package/dist/widget/css.js +532 -0
  135. package/dist/widget/css.js.map +1 -0
  136. package/dist/widget/dom.d.ts +143 -0
  137. package/dist/widget/dom.d.ts.map +1 -0
  138. package/dist/widget/dom.js +27 -0
  139. package/dist/widget/dom.js.map +1 -0
  140. package/dist/widget/highlight.d.ts +64 -0
  141. package/dist/widget/highlight.d.ts.map +1 -0
  142. package/dist/widget/highlight.js +136 -0
  143. package/dist/widget/highlight.js.map +1 -0
  144. package/dist/widget/index.d.ts +9 -0
  145. package/dist/widget/index.d.ts.map +1 -0
  146. package/dist/widget/index.js +20 -0
  147. package/dist/widget/index.js.map +1 -0
  148. package/dist/widget/position.d.ts +166 -0
  149. package/dist/widget/position.d.ts.map +1 -0
  150. package/dist/widget/position.js +497 -0
  151. package/dist/widget/position.js.map +1 -0
  152. package/dist/widget/settings.d.ts +116 -0
  153. package/dist/widget/settings.d.ts.map +1 -0
  154. package/dist/widget/settings.js +117 -0
  155. package/dist/widget/settings.js.map +1 -0
  156. package/dist/widget/widget.d.ts +442 -0
  157. package/dist/widget/widget.d.ts.map +1 -0
  158. package/dist/widget/widget.js +2005 -0
  159. package/dist/widget/widget.js.map +1 -0
  160. package/package.json +144 -4
@@ -0,0 +1,547 @@
1
+ /**
2
+ * driverkit/react — the same panel, for a team that already has React.
3
+ *
4
+ * WHY THIS EXISTS BESIDE `driverkit/widget` AND NOT INSTEAD OF IT.
5
+ * `mountWidget` is hand-written DOM in a shadow root with zero dependencies,
6
+ * and that is the right shape for a drop-in: it goes into a Thymeleaf
7
+ * template, a Rails view or a static page, and the host owns no version of
8
+ * anything it did not pick. It is the wrong shape for a team that has React, a
9
+ * design system and an opinion about what their product looks like — for them
10
+ * the shadow root is a wall around the panel and the hand-written markup is
11
+ * something to fight. So there are two paths. **The vanilla widget's
12
+ * dependency count stays zero and its behaviour is untouched by anything in
13
+ * here.**
14
+ *
15
+ * WHAT THE PUBLIC SURFACE IS, AND WHY IT IS THIS ONE. Three things, in
16
+ * decreasing order of how much they assume:
17
+ *
18
+ * `createDriverPanel(React)` returns everything. One call, because React
19
+ * itself is an argument rather than an import — see `runtime.ts`, which is
20
+ * the file to read before judging that.
21
+ *
22
+ * `useDriver(driver, options)` is the primitive, and it is the whole
23
+ * library: the conversation, the gate, the run controls, the budget and the
24
+ * receipts, as plain data and plain functions. A host that renders none of
25
+ * our components still gets all of the behaviour. This is deliberately the
26
+ * thing the documentation leads with, because a team adopting this has a
27
+ * design system and headless-with-defaults is the only shape that respects
28
+ * one.
29
+ *
30
+ * `<DriverPanel />` is one assembly of the components, for the team that
31
+ * wants a panel today and will restyle it on Thursday. Every part of it is
32
+ * replaceable one at a time through `components`, so "restyle it" never has
33
+ * to become "fork it".
34
+ *
35
+ * Both, rather than one, because the two audiences are real and a single
36
+ * `<DriverPanel />` would make the design-system team reimplement the gate —
37
+ * which is the one thing in here nobody should be reimplementing.
38
+ *
39
+ * WHAT IS DELIBERATELY ABSENT, with the vanilla widget's own reasoning
40
+ * re-examined rather than copied; `REACT.md` has the full list:
41
+ *
42
+ * **No shadow root.** The vanilla panel needs one because it is rendering
43
+ * into an app whose CSS it has never seen. A React app has a CSS isolation
44
+ * story of its own — modules, Tailwind, styled-components, whatever it
45
+ * picked — and a shadow root would break it: the host's own class names
46
+ * would not reach inside, which is the opposite of the point.
47
+ *
48
+ * **No launcher, no drag, no corner.** Those exist because the vanilla
49
+ * widget has to place itself in a page nobody told it about. Here the host
50
+ * renders `<DriverPanel />` where it wants it, in its own layout, and a
51
+ * library that positioned itself `fixed` would be overriding a decision the
52
+ * host is better placed to make.
53
+ *
54
+ * **No `aria-live` on the transcript**, which the vanilla panel has and
55
+ * which is a bug at a 600ms pace: a live region containing the whole
56
+ * conversation is re-read as each step row lands, so the person hears a
57
+ * growing transcript from the top instead of what just changed. State is
58
+ * announced instead, through small `role="status"` regions that say one
59
+ * thing — see `ReceiptStatus` and `RunAnnouncer`.
60
+ *
61
+ * **No `textContent` discipline, because React is the discipline.** The
62
+ * vanilla file keeps `innerHTML` out of its DOM wrapper on purpose; here the
63
+ * equivalent rule is that nothing in this area calls
64
+ * `dangerouslySetInnerHTML`, including at the markdown seam, where a host
65
+ * supplies a COMPONENT and never a string of HTML.
66
+ *
67
+ * The `era` counter is kept, and that one is worth saying out loud: React does
68
+ * not help with it. A `setState` from an async callback that outlived a Clear
69
+ * is exactly as wrong as the `appendChild` it replaced. See `store.ts`.
70
+ */
71
+ import { commas } from "../agent/index.js";
72
+ import { createPanelStore, receiptLine } from "./store.js";
73
+ export { COMMIT_ASK_ABOVE, createPanelStore, receiptLine, stepOutcome } from "./store.js";
74
+ /**
75
+ * Visually hidden and still read aloud.
76
+ *
77
+ * Inline rather than a stylesheet because this area ships no CSS at all — a
78
+ * headless library that injected a `<style>` would be making exactly the
79
+ * decision it claims to leave to the host. These five properties are the one
80
+ * exception, and they are not a look: a `role="status"` that is `display:
81
+ * none` is not announced, and one that is merely off-screen with `width: 0`
82
+ * is skipped by some screen readers, so the clip has to be right or the
83
+ * announcement does not happen.
84
+ */
85
+ const SR_ONLY = {
86
+ position: "absolute",
87
+ width: 1,
88
+ height: 1,
89
+ overflow: "hidden",
90
+ clip: "rect(0 0 0 0)",
91
+ whiteSpace: "nowrap",
92
+ };
93
+ /**
94
+ * Bind this area to the host's React and get the panel.
95
+ *
96
+ * ```tsx
97
+ * // driver-panel.tsx — one file you own
98
+ * import * as React from "react";
99
+ * import { createDriverPanel } from "driverkit/react";
100
+ *
101
+ * export const { DriverPanel, useDriver, components } = createDriverPanel(React);
102
+ * ```
103
+ *
104
+ * Everything after that line is ordinary React. See `runtime.ts` for why the
105
+ * line is there at all.
106
+ */
107
+ export function createDriverPanel(react) {
108
+ const h = react.createElement;
109
+ /**
110
+ * The whole library, as a hook.
111
+ *
112
+ * The store is built once per `driver` and the options are read then — which
113
+ * is a real constraint and so it is said plainly: changing `think` or
114
+ * `maxSteps` on a later render does not change a store that already exists.
115
+ * The things a person changes while watching a run (the pace, step mode,
116
+ * confirmations) are not options but state, with setters, for exactly that
117
+ * reason. A host that genuinely needs new options re-keys the component.
118
+ */
119
+ function useDriver(driver, options) {
120
+ // Forces one re-render, for the single case the effect below cannot repair
121
+ // on its own: a mount onto a store that has already been torn down.
122
+ const [, revive] = react.useState(0);
123
+ const held = react.useRef(null);
124
+ if (held.current === null || held.current.driver !== driver) {
125
+ // The outgoing store is NOT destroyed here. Render must not tear
126
+ // anything down: React may double-invoke this function, or throw the
127
+ // render away entirely, and `destroy` aborts a run and lapses an open
128
+ // question — the two things that must not happen speculatively. The
129
+ // effect below owns the teardown, and a `driver` change reaches it as a
130
+ // changed dependency.
131
+ held.current = { driver, store: createPanelStore(driver, options), holds: 0 };
132
+ }
133
+ const holding = held.current;
134
+ const store = holding.store;
135
+ // The run is aborted and any open question is answered `lapsed` when the
136
+ // last holder goes. Not `denied`: nobody refused it, and the record must
137
+ // not say somebody did. See `store.ts`.
138
+ //
139
+ // WHY THIS IS A RETAIN COUNT AND A DEFERRED TEARDOWN rather than the one
140
+ // line it looks like it should be. `destroy` is final — it clears the
141
+ // listeners and latches the store closed, so `ask` returns immediately
142
+ // afterwards. React mounts an effect, runs its cleanup and mounts it again
143
+ // in StrictMode, which is how every React app runs in development, and a
144
+ // plain `return () => store.destroy()` therefore kills the store during
145
+ // mount and leaves the adopter a panel whose composer does nothing. That
146
+ // was real: it is what this hook did, and it was invisible here until
147
+ // there was a React to render it with. So the cleanup gives up its hold
148
+ // and defers the decision past the synchronous remount; only a hold count
149
+ // that is still zero one microtask later is an unmount.
150
+ react.useEffect(() => {
151
+ if (holding.store.destroyed()) {
152
+ // A remount onto a torn-down store — a hide and a re-show far enough
153
+ // apart that the microtask already ran. Nothing can revive the store,
154
+ // so build another one; the conversation is gone either way, because
155
+ // the panel was not on screen.
156
+ held.current = null;
157
+ revive((n) => n + 1);
158
+ return;
159
+ }
160
+ holding.holds += 1;
161
+ return () => {
162
+ holding.holds -= 1;
163
+ queueMicrotask(() => {
164
+ if (holding.holds === 0)
165
+ holding.store.destroy();
166
+ });
167
+ };
168
+ }, [holding]);
169
+ const snapshot = react.useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);
170
+ return react.useMemo(() => ({
171
+ ...snapshot,
172
+ ask: store.ask,
173
+ stop: store.stop,
174
+ clear: store.clear,
175
+ pause: store.pause,
176
+ resume: store.resume,
177
+ step: store.step,
178
+ setPace: store.setPace,
179
+ setStepMode: store.setStepMode,
180
+ setConfirmCommits: store.setConfirmCommits,
181
+ decide: store.decide,
182
+ revoke: store.revoke,
183
+ approve: store.approve,
184
+ store,
185
+ }), [snapshot, store]);
186
+ }
187
+ /** The default prose renderer: the model's text, as text. The seam's
188
+ * identity function, so a host that plugs in markdown is replacing
189
+ * something rather than filling a hole. */
190
+ const Prose = ({ text }) => text;
191
+ const Said = ({ entry, Prose: Render }) => h("div", { className: `dk-${entry.kind}`, "data-dk": entry.kind }, entry.kind === "assistant" ? h(Render, { text: entry.text }) : entry.text);
192
+ const StepRow = ({ entry }) => h("div", {
193
+ className: `dk-step dk-step-${entry.status}${entry.commit ? " dk-step-commit" : ""}`,
194
+ "data-dk": "step",
195
+ "data-status": entry.status,
196
+ // The marker for a commit is on the row for the person as well as in
197
+ // the prompt for the model. This is the row somebody scrolls back to
198
+ // when they want to know what changed their data.
199
+ ...(entry.commit ? { title: "Changes data, and may be irreversible." } : {}),
200
+ },
201
+ // The row's text reads the same whether somebody looked at it while it
202
+ // was pending or a minute afterwards: the state is an attribute, never
203
+ // part of the sentence.
204
+ `${entry.verb}${entry.verbKind ? ` (${entry.verbKind})` : ""}${argSummary(entry.args)}`, entry.outcome === undefined ? null : ` · ${entry.outcome}`);
205
+ /**
206
+ * The receipt's status line: one region that stays, and a line inside it
207
+ * that is replaced on every phase change.
208
+ *
209
+ * THE STRUCTURE IS THE POINT, and the first version of it had it inside out,
210
+ * so the reasoning is written down rather than implied. The region itself —
211
+ * the `role="status"`, which is also the focus target — is mounted once,
212
+ * when the phase leaves `asking`, and then stays. Only the line inside it is
213
+ * keyed on the phase, so each new verdict replaces a node rather than
214
+ * mutating the text of one.
215
+ *
216
+ * Three things follow, and the earlier shape (the key on this component, so
217
+ * that the whole region remounted) got two of them wrong:
218
+ *
219
+ * **Focus moves once.** It moves here the first time the phase leaves
220
+ * `asking`, so somebody who pressed a button lands on the answer rather
221
+ * than on whatever followed the buttons that have just gone — and never
222
+ * again, because a receipt that stole focus when the app's verdict
223
+ * arrived a moment later would yank somebody out of the message they had
224
+ * started typing. A remounting region made that promise false twice over:
225
+ * the `moved` ref died with the old node so the move ran again on every
226
+ * phase, and had it not, the node holding focus would have been removed
227
+ * from the document and the focus dropped to `<body>`. Both were real and
228
+ * both are now tested — see `test/react-dom.test.ts`, which is the first
229
+ * thing in this repository to render this file with React.
230
+ *
231
+ * **The announcement still happens.** A live region is announced when its
232
+ * contents change while it is in the document; a region inserted together
233
+ * with its text is the case screen readers most often miss. Keeping the
234
+ * region and replacing the line inside it is therefore the more reliable
235
+ * of the two shapes, not a concession — and the `key` is still
236
+ * load-bearing, because it is what makes the line a new node instead of an
237
+ * updated one.
238
+ *
239
+ * **`data-phase` is on the region**, where a stylesheet and a test can
240
+ * find it without caring which node holds the words.
241
+ */
242
+ const ReceiptStatus = ({ entry, decided, }) => {
243
+ const node = react.useRef(null);
244
+ const moved = react.useRef(false);
245
+ react.useEffect(() => {
246
+ if (!decided || moved.current)
247
+ return;
248
+ moved.current = true;
249
+ node.current?.focus?.();
250
+ }, [decided]);
251
+ return h("div", {
252
+ ref: node,
253
+ // -1 so it can be focused programmatically and is not a tab stop. The
254
+ // receipt is a record, not a control.
255
+ tabIndex: -1,
256
+ role: "status",
257
+ className: "dk-ask-said",
258
+ "data-dk": "receipt",
259
+ "data-phase": entry.phase,
260
+ }, h("span", { key: entry.phase, className: "dk-ask-line" }, receiptLine(entry)));
261
+ };
262
+ /**
263
+ * The question, and then the receipt it becomes. **The same element either
264
+ * way.**
265
+ *
266
+ * The card does not unmount when it is answered: the buttons are replaced by
267
+ * the status line, in place, keeping the question's own words and its
268
+ * position in scrollback. The vanilla panel keeps the row too but tells the
269
+ * person only that they answered; this also says what the APP then did,
270
+ * which is the half that turns a decision into a record of a change.
271
+ */
272
+ const ApprovalCard = ({ entry, decide, notes }) => {
273
+ const [note, setNote] = react.useState("");
274
+ const asking = entry.phase === "asking";
275
+ const why = (entry.reversible
276
+ ? "This changes data, but the app says this can be undone." +
277
+ (entry.large
278
+ ? " You are being asked because of how many rows it affects: an undo over this" +
279
+ " many is a promise an app may not be able to keep."
280
+ : "")
281
+ : "This changes data and may be irreversible.") +
282
+ (entry.count === undefined
283
+ ? " No count was reported, so nobody can tell you how many rows it affects."
284
+ : "");
285
+ return h("div", {
286
+ className: `dk-ask dk-ask-${entry.phase}`,
287
+ "data-dk": "approval",
288
+ "data-phase": entry.phase,
289
+ }, h("div", { className: "dk-ask-q" }, entry.count === undefined
290
+ ? `${entry.describe}?`
291
+ : `${entry.describe} — ${commas(entry.count)} rows?`), h("div", { className: "dk-ask-why" }, why + argSummary(entry.args)), asking
292
+ ? h("div", { className: "dk-ask-actions" }, notes
293
+ ? h("label", { className: "dk-ask-note" }, [
294
+ h("span", { key: "l" }, "Why? (optional, sent to the model on a refusal)"),
295
+ h("textarea", {
296
+ key: "t",
297
+ value: note,
298
+ rows: 2,
299
+ onChange: (e) => setNote(e.target.value),
300
+ // Enter does not submit. Typing here is a comment and must
301
+ // never be the thing that authorizes a call; a field that
302
+ // answered the question on Enter would make the note the
303
+ // decision.
304
+ onKeyDown: (e) => {
305
+ if (e.key === "Enter")
306
+ e.stopPropagation();
307
+ },
308
+ }),
309
+ ])
310
+ : null,
311
+ // "No" first in the DOM, so it is the first of the two a keyboard
312
+ // reaches. Reaching "Yes" takes one more deliberate key, which is
313
+ // the right way round for a button that can accept a thousand rows.
314
+ h("button", {
315
+ key: "no",
316
+ type: "button",
317
+ className: "dk-no",
318
+ onClick: () => decide(entry.id, { allow: false, note }),
319
+ }, "No"), h("button", {
320
+ key: "yes",
321
+ type: "button",
322
+ className: "dk-yes",
323
+ onClick: () => decide(entry.id, { allow: true, note }),
324
+ }, "Yes, do it"),
325
+ // The standing rules, each labelled with the rule itself rather
326
+ // than with "always". The exact text that will be installed is on
327
+ // screen before the click, which is the only way somebody can be
328
+ // said to have agreed to it.
329
+ entry.grantOffer === undefined
330
+ ? null
331
+ : h("div", { key: "grants", className: "dk-ask-grants" }, h("button", {
332
+ key: "ar",
333
+ type: "button",
334
+ className: "dk-no-always",
335
+ onClick: () => decide(entry.id, { allow: false, always: true, note }),
336
+ }, grantLabel(entry.verb, false)), h("button", {
337
+ key: "aa",
338
+ type: "button",
339
+ className: "dk-yes-always",
340
+ onClick: () => decide(entry.id, { allow: true, always: true, note }),
341
+ }, grantLabel(entry.verb, true))))
342
+ : // NO `key` on this: the region must not remount when the phase
343
+ // moves on. The keyed node is the line inside it. See
344
+ // `ReceiptStatus`.
345
+ h(ReceiptStatus, { entry, decided: true }), entry.note === undefined || entry.note === ""
346
+ ? null
347
+ : h("div", { className: "dk-ask-noted" }, `You noted: ${entry.note}`));
348
+ };
349
+ const Transcript = ({ driving, components, notes }) => h("div", {
350
+ className: "dk-log",
351
+ "data-dk": "transcript",
352
+ // NO `aria-live`. The vanilla panel puts `aria-live="polite"` on this
353
+ // container, and at a 600ms pace that means the whole conversation is
354
+ // re-read as each step row lands — a live region nobody can use. The
355
+ // state is announced instead, in one-sentence `role="status"` regions:
356
+ // `RunAnnouncer` for the run, `ReceiptStatus` for a decision.
357
+ }, driving.entries.map((entry) => entry.kind === "step"
358
+ ? h(components.StepRow, { key: entry.id, entry })
359
+ : entry.kind === "approval"
360
+ ? h(components.ApprovalCard, {
361
+ key: entry.id,
362
+ entry,
363
+ decide: driving.decide,
364
+ notes,
365
+ })
366
+ : h(components.Said, { key: entry.id, entry, Prose: components.Prose })));
367
+ /**
368
+ * One short sentence about what the run is doing, for somebody who cannot see
369
+ * the buttons change.
370
+ *
371
+ * Rendered only while a run is in flight: a status region that said
372
+ * "running" to an idle panel would be announcing the absence of news.
373
+ *
374
+ * Same shape as `ReceiptStatus` and for the same reason: the region stays
375
+ * mounted and the sentence inside it is keyed, so each change replaces a
376
+ * node in a region that was already in the document. A live region inserted
377
+ * with its text already in it is the case screen readers most often miss.
378
+ */
379
+ const RunAnnouncer = ({ driving }) => {
380
+ const said = driving.runState === "paused"
381
+ ? "Run paused"
382
+ : driving.runState === "awaiting-approval"
383
+ ? "Waiting for you"
384
+ : "Run continuing";
385
+ return h("div", { role: "status", style: SR_ONLY }, h("span", { key: said }, driving.busy ? said : ""));
386
+ };
387
+ const RunControlsBar = ({ driving }) => {
388
+ if (!driving.busy)
389
+ return null;
390
+ const held = driving.runState === "paused";
391
+ return h("div", { className: "dk-run", "data-dk": "controls" }, h("button", {
392
+ key: "pause",
393
+ type: "button",
394
+ className: "dk-pause",
395
+ "aria-label": held ? "Resume the run" : "Pause the run",
396
+ onClick: () => (held ? driving.resume() : driving.pause()),
397
+ }, held ? "Resume" : "Pause"), h("button", { key: "step", type: "button", className: "dk-once", onClick: () => driving.step() }, "Step"),
398
+ // Stop is the abort signal and nothing else, which is also why it is not
399
+ // on `RunControls`: an abort outranks a pause, so a held run is still
400
+ // stoppable and never wedged.
401
+ h("button", { key: "stop", type: "button", className: "dk-stop", onClick: () => driving.stop() }, "Stop"),
402
+ // The pace, visibly and changeably, because the person watching their own
403
+ // screen move is the one who should decide how fast it moves. Named
404
+ // speeds rather than a free slider: a number of milliseconds is not a
405
+ // thing anybody has an opinion about, and a radio group of four labelled
406
+ // buttons is reachable with a keyboard without anybody being careful
407
+ // about it forever.
408
+ h("div", { key: "pace", className: "dk-speeds", role: "radiogroup", "aria-label": "Pace" }, SPEEDS.map((speed) => h("button", {
409
+ key: speed.id,
410
+ type: "button",
411
+ role: "radio",
412
+ "aria-checked": nearest(driving.paceMs).id === speed.id,
413
+ className: "dk-speed",
414
+ onClick: () => driving.setPace(speed.ms),
415
+ }, speed.label))), h("span", { key: "note", className: "dk-run-note" }, driving.runState === "paused"
416
+ ? "Paused"
417
+ : // Not "running": the run is waiting for the person, and a panel
418
+ // that said "running" there would be telling them to wait for the
419
+ // thing that is waiting for them.
420
+ driving.runState === "awaiting-approval"
421
+ ? "Waiting for you"
422
+ : `${nearest(driving.paceMs).label} · ${driving.paceMs} ms a step`), h(RunAnnouncer, { key: "live", driving }));
423
+ };
424
+ const Composer = ({ driving, placeholder }) => {
425
+ const [draft, setDraft] = react.useState("");
426
+ return h("form", {
427
+ className: "dk-form",
428
+ onSubmit: (e) => {
429
+ e.preventDefault();
430
+ const text = draft.trim();
431
+ if (text === "" || driving.busy)
432
+ return;
433
+ setDraft("");
434
+ void driving.ask(text);
435
+ },
436
+ }, h("input", {
437
+ key: "input",
438
+ type: "text",
439
+ className: "dk-input",
440
+ value: draft,
441
+ ...(placeholder === undefined ? {} : { placeholder }),
442
+ onChange: (e) => setDraft(e.target.value),
443
+ }), h("button", { key: "send", type: "submit", className: "dk-send", disabled: driving.busy }, "Send"));
444
+ };
445
+ /**
446
+ * The session's ceiling, where the app declared one.
447
+ *
448
+ * The one thing on the panel with no control beside it, and the reason
449
+ * `Driver.budget` exists at all: `MountOptions.budget` is set by whoever
450
+ * mounted the app, and a viewer must not be able to raise their own ceiling.
451
+ * Shown before the ceiling is reached rather than only after, because a run
452
+ * that stops mid-job and does not say why is read as a broken app.
453
+ *
454
+ * Absent when the app declared no ceiling. A line reading "no limit" would be
455
+ * one more thing to read; the counter is still on `Driver.budget` for anybody
456
+ * debugging.
457
+ */
458
+ const BudgetNotice = ({ driving }) => {
459
+ const spend = driving.budget;
460
+ if (!spend)
461
+ return null;
462
+ const parts = [];
463
+ if (spend.calls.limit !== undefined) {
464
+ parts.push(`${spend.calls.used} of ${spend.calls.limit} steps used`);
465
+ }
466
+ if (spend.commits.limit !== undefined) {
467
+ parts.push(`${spend.commits.used} of ${spend.commits.limit} changes to data used`);
468
+ }
469
+ if (parts.length === 0)
470
+ return null;
471
+ return h("div", { className: "dk-budget", "data-dk": "budget" }, h("span", { key: "l", className: "dk-budget-label" }, "This session's limit"), h("span", { key: "v", className: "dk-budget-value" }, parts.join(" · ")), h("span", { key: "w", className: "dk-budget-why" }, "Set by the app, not by this panel, so it cannot be changed from here."));
472
+ };
473
+ /** The standing rules, and a way to take one back. A grant nobody can see is
474
+ * a grant nobody can revoke. */
475
+ const GrantList = ({ driving }) => {
476
+ if (driving.grants.length === 0)
477
+ return null;
478
+ return h("div", { className: "dk-grants", "data-dk": "grants" }, h("span", { key: "l" }, "Standing rules for this session"), h("ul", { key: "u" }, driving.grants.map((grant) => h("li", { key: grant.verb }, grant.rule, h("button", {
479
+ key: "r",
480
+ type: "button",
481
+ onClick: () => driving.revoke(grant.verb),
482
+ }, "Revoke")))));
483
+ };
484
+ const components = {
485
+ Transcript,
486
+ Said,
487
+ StepRow,
488
+ ApprovalCard,
489
+ RunControlsBar,
490
+ Composer,
491
+ BudgetNotice,
492
+ GrantList,
493
+ Prose,
494
+ };
495
+ const DriverPanel = (props) => {
496
+ const driving = useDriver(props.driver, props.options);
497
+ const parts = {
498
+ ...components,
499
+ ...props.components,
500
+ ...(props.Prose === undefined ? {} : { Prose: props.Prose }),
501
+ };
502
+ return h("div", { className: "dk-panel", "data-dk": "panel" }, h(parts.Transcript, {
503
+ key: "log",
504
+ driving,
505
+ components: parts,
506
+ notes: props.options.notes === true,
507
+ }), h(parts.RunControlsBar, { key: "controls", driving }), h(parts.GrantList, { key: "grants", driving }), h(parts.BudgetNotice, { key: "budget", driving }), h(parts.Composer, {
508
+ key: "composer",
509
+ driving,
510
+ ...(props.placeholder === undefined ? {} : { placeholder: props.placeholder }),
511
+ }));
512
+ };
513
+ return { useDriver, DriverPanel, components };
514
+ }
515
+ /**
516
+ * The named speeds, re-declared for the same reason `COMMIT_ASK_ABOVE` is:
517
+ * `src/widget/settings.ts` is the vanilla panel's, and importing it would pull
518
+ * that area's `localStorage` access and its DOM types into a React bundle.
519
+ * `test/react.test.ts` pins that the two lists agree.
520
+ */
521
+ const SPEEDS = [
522
+ { id: "fast", label: "Fast", ms: 250 },
523
+ { id: "steady", label: "Steady", ms: 600 },
524
+ { id: "slow", label: "Slow", ms: 1500 },
525
+ { id: "crawl", label: "Crawl", ms: 3000 },
526
+ ];
527
+ function nearest(ms) {
528
+ let best = SPEEDS[0];
529
+ for (const speed of SPEEDS) {
530
+ if (Math.abs(speed.ms - ms) < Math.abs(best.ms - ms))
531
+ best = speed;
532
+ }
533
+ return best;
534
+ }
535
+ /** The arguments, short. Same shape and same clamp as the vanilla panel's, so
536
+ * one row does not read differently in the two. */
537
+ function argSummary(args) {
538
+ const parts = Object.entries(args).map(([k, v]) => `${k}=${String(v)}`);
539
+ if (parts.length === 0)
540
+ return "";
541
+ const text = parts.join(" ");
542
+ return ` ${text.length > 60 ? text.slice(0, 57) + "…" : text}`;
543
+ }
544
+ function grantLabel(verb, allow) {
545
+ return allow ? `Always allow “${verb}”` : `Always refuse “${verb}”`;
546
+ }
547
+ //# sourceMappingURL=index.js.map