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
package/REACT.md ADDED
@@ -0,0 +1,477 @@
1
+ # `driverkit/react`
2
+
3
+ A second, optional panel, for a team that already has React.
4
+
5
+ `mountWidget` — `driverkit/widget` — is hand-written DOM in a shadow root with
6
+ zero dependencies, and that is the right shape for a drop-in: it goes into a
7
+ Thymeleaf template, a Rails view or a static page, and the host ends up owning
8
+ no version of anything it did not pick. It is the wrong shape for a team that
9
+ has React, a design system and an opinion about what their product looks like.
10
+ For them the shadow root is a wall around the panel and the hand-written markup
11
+ is something to fight.
12
+
13
+ So there are two panels. **This one changes nothing about the other one: the
14
+ vanilla widget's dependency count is still zero and its behaviour is
15
+ untouched.** An app that imports neither pays for neither.
16
+
17
+ Pre-release, like the rest of the library: nothing is published.
18
+
19
+ This entry point **has** now been rendered by a real React — `react` and
20
+ `react-dom` are dev dependencies of the workspace and
21
+ `test/react-dom.test.ts` renders the panel into jsdom, clicks its buttons and
22
+ watches where focus goes. That is new, and it mattered: the first render under
23
+ `StrictMode` found a panel that rendered correctly and then ignored every
24
+ instruction typed into it, and the first real `ref` found the receipt stealing
25
+ focus on every phase change rather than once. Both are fixed and both are
26
+ pinned. What is still unproven is written down in *What has not been rendered*
27
+ at the bottom.
28
+
29
+ ---
30
+
31
+ ## Install
32
+
33
+ ```sh
34
+ npm install driverkit
35
+ ```
36
+
37
+ React is an **optional peer dependency**. If you do not import
38
+ `driverkit/react`, React is not installed, is not in your lockfile, and is not
39
+ referenced by any declaration file the package publishes. See *How the optional
40
+ peer is enforced* below, which is a shorter section than you might expect
41
+ because the mechanism is mostly subtraction.
42
+
43
+ ## Bind it to your React, once
44
+
45
+ ```tsx
46
+ // src/driver-panel.tsx — one file you own
47
+ import * as React from "react";
48
+ import { createDriverPanel } from "driverkit/react";
49
+
50
+ export const { DriverPanel, useDriver, components } = createDriverPanel(React);
51
+ ```
52
+
53
+ Everything after that line is ordinary React.
54
+
55
+ **Why that line exists**, since it is the first thing anybody will object to.
56
+ React's hook dispatcher is module-level mutable state, so a component built
57
+ against a different copy of `react` than the one rendering it throws "Invalid
58
+ hook call" — or, with a bundler that hoisted two versions, silently reads the
59
+ wrong dispatcher. A library that imports its own `react` can get that second
60
+ copy through a version mismatch, a nested `node_modules`, a monorepo hoist, or
61
+ an app that loads React from a CDN while the bundle resolves it from disk.
62
+ Taking the runtime as an argument makes it impossible: the only React that can
63
+ be passed is the one the host already has. It is the same argument
64
+ `driverkit/otel` makes about taking a `Tracer` instead of importing
65
+ `@opentelemetry/api`, and it is stronger here, because this failure is one the
66
+ adopter cannot debug from inside their own app.
67
+
68
+ The cost is real and it is one line. What is lost is `import { DriverPanel }
69
+ from "driverkit/react"` in forty files instead of re-exporting it from one.
70
+
71
+ That line also has to *typecheck*, which is a separate claim from compiling:
72
+ the React API is described structurally in `src/react/runtime.ts`, and a
73
+ structural description written against types nobody in this repository could
74
+ resolve is a description that can simply be wrong. It was. `ReactRuntime`
75
+ required a `useCallback` this area never calls, typed loosely enough that
76
+ React's own `<T extends Function>` signature did not satisfy it — so
77
+ `createDriverPanel(React)` was a type error in the adopter's own file, on the
78
+ first line of this document. `test/react-dom.test.ts` now assigns React's
79
+ module namespace to `ReactRuntime`, so `tsc` answers that question instead of
80
+ this paragraph.
81
+
82
+ ## Use it
83
+
84
+ ### The hook is the library
85
+
86
+ ```tsx
87
+ function Assistant({ driver }: { driver: Driver }) {
88
+ const d = useDriver(driver, { think: backendThink("/api/chat") });
89
+
90
+ return (
91
+ <YourPanel>
92
+ {d.entries.map((e) => <YourRow key={e.id} entry={e} />)}
93
+ {d.pending && <YourApprovalCard entry={d.pending} onDecide={d.decide} />}
94
+ <YourComposer onSubmit={d.ask} busy={d.busy} />
95
+ </YourPanel>
96
+ );
97
+ }
98
+ ```
99
+
100
+ `useDriver` is the whole thing: the conversation, the approval gate, the run
101
+ controls, the budget and the receipts, as plain data and plain functions. A host
102
+ that renders none of our components still gets all of the behaviour. This is
103
+ deliberately what the documentation leads with — a team adopting this has a
104
+ design system, and headless-with-defaults is the only shape that respects one.
105
+
106
+ ### The panel is one assembly of it
107
+
108
+ ```tsx
109
+ <DriverPanel
110
+ driver={driver}
111
+ options={{ think: backendThink("/api/chat") }}
112
+ placeholder="Ask about your contacts"
113
+ />
114
+ ```
115
+
116
+ Every part is replaceable, one at a time:
117
+
118
+ ```tsx
119
+ <DriverPanel
120
+ driver={driver}
121
+ options={{ think }}
122
+ components={{ ApprovalCard: OurApprovalCard }}
123
+ Prose={OurMarkdown}
124
+ />
125
+ ```
126
+
127
+ Both surfaces, rather than one, because the two audiences are real: a single
128
+ `<DriverPanel />` would make the design-system team reimplement the gate, which
129
+ is the one thing in here nobody should be reimplementing.
130
+
131
+ ### The gate, for a commit that did not come from this panel
132
+
133
+ An agent over the relay or MCP can call a `commit` too, and that call never
134
+ touches `drive`'s `onBeforeCommit`. Wire this panel's card up as the core's
135
+ approver and one question covers every path:
136
+
137
+ ```tsx
138
+ let panel: PanelStore;
139
+ const driver = mount({ …, approve: (req) => panel.approve(req) });
140
+ // and from the component, once it has rendered:
141
+ const d = useDriver(driver, { think });
142
+ panel = d.store;
143
+ ```
144
+
145
+ When the core is already asking — the app's own `approve` hook, or a dialog
146
+ adopted by something else on the page — the panel installs no gate of its own.
147
+ Two questions about one commit is how people learn to click Yes without reading,
148
+ and the next one they skip is the bulk accept.
149
+
150
+ ---
151
+
152
+ ## The approval receipt
153
+
154
+ This is the feature the area was built for, and it is the one place the panel
155
+ is better than the vanilla one.
156
+
157
+ **The card does not disappear when you decide. It becomes a receipt in place:
158
+ same element, same id, same position in scrollback, the question's own words
159
+ intact, and the button row replaced by a `role="status"` line.** The gate is the
160
+ only auditable feature in the library, and auditability happens in scrollback —
161
+ a card that unmounted on decision and left a loose outcome line somewhere below
162
+ would break the link between the question and its answer, which is exactly the
163
+ link somebody scrolling back for "what did I agree to" is following.
164
+
165
+ The phases, which are what a reader of that receipt needs distinguished:
166
+
167
+ | phase | what it says |
168
+ | --- | --- |
169
+ | `asking` | the buttons are live and nothing has happened |
170
+ | `allowed` | a person said yes and the verb is now running |
171
+ | `allowed-ok` | the verb ran and the app said ok |
172
+ | `allowed-refused` | the verb ran and **the app refused it anyway** |
173
+ | `denied` | a person said no |
174
+ | `stopped` | the run was stopped before the verb ran; nobody refused it |
175
+ | `lapsed` | the panel closed with the question open; nobody answered |
176
+
177
+ `allowed-refused` is the one worth pointing at. "You approved this" and "this
178
+ happened" are different facts, and a receipt recording only the first would be
179
+ recording the more flattering of the two. A stale rev, a spent budget or a plan
180
+ token that no longer matches all land there. `receiptLine(entry)` is exported so
181
+ a replacement card keeps the wording without re-deriving it from the phase.
182
+
183
+ `stopped` and `lapsed` are kept apart from `denied` for the reason the core
184
+ keeps `timed-out` apart from `declined`: a record that cannot tell "a person
185
+ refused" from "nobody was there" is the record this whole mechanism exists to
186
+ stop writing. The two are a question of ordering as well as of vocabulary, and
187
+ the store decides it deliberately: closing a panel with a question open lapses
188
+ the question *before* it aborts the run, because the abort would otherwise
189
+ answer it `stopped` and record the less true of the two available facts.
190
+
191
+ **Where focus goes, and once.** The status line is also a focus target —
192
+ `tabIndex={-1}`, so it can be focused programmatically and is not a tab stop —
193
+ and focus moves to it the first time the phase leaves `asking`, so somebody who
194
+ pressed a button lands on the answer rather than on whatever followed the
195
+ buttons that have just gone. It moves **once**: a receipt that took focus again
196
+ when the app's verdict arrived a moment later would pull somebody out of the
197
+ message they had started typing. The region itself is therefore mounted once
198
+ and stays, and only the line inside it is keyed on the phase. A replacement
199
+ card that remounts the region per phase gets both halves wrong — the focus
200
+ moves again, and the node holding focus leaves the document — which is not a
201
+ guess: it is what this one did until it was rendered with React.
202
+
203
+ ### Standing rules — off by default
204
+
205
+ With `grants: true` the card also offers "Always allow" / "Always refuse". Two
206
+ rules hold, and both are load-bearing:
207
+
208
+ - **The rule is shown in full before the click**, in the same string that is
209
+ then stored, so nobody installs a rule whose text they have not read.
210
+ - **Grants live for this panel and this page load.** Nothing is written to
211
+ storage, deliberately: a standing approval that came back after a reload, in a
212
+ session nobody remembers granting it in, is worse than asking again.
213
+ `PanelSnapshot.grants` is on the snapshot so a host can render the list, and
214
+ `revoke(verb)` takes one away — a grant nobody can see is a grant nobody can
215
+ revoke.
216
+
217
+ A commit answered by a rule is still posted as a receipt, naming the rule that
218
+ answered. A rule that silenced the question must not also silence the record.
219
+
220
+ On the core's `approve` path such a commit answers `{ asked: false }` rather
221
+ than `true`, because nobody *was* asked this time, and that is the answer the
222
+ core added for exactly this case. **An app that needs every allowed commit to
223
+ carry a person's yes in the audit record should leave `grants` off.** (On
224
+ `drive`'s `onBeforeCommit` path there is no `{ asked: false }` to return — it is
225
+ a `CommitDecision`, not an `Approval` — so a grant there can only say `true`,
226
+ and the richer record lives only in the panel. That asymmetry is the core's, not
227
+ ours.)
228
+
229
+ ### Notes — off by default
230
+
231
+ With `notes: true` the card offers a free-text field. On a *denial* the note
232
+ becomes the hint the model is told, which is a real feature of the gate rather
233
+ than decoration: `CommitDecision` takes a string, and "not until finance signs
234
+ off" is something a model can act on where "declined" is not.
235
+
236
+ **Typing in it never approves anything.** The note is read only when a decision
237
+ button is pressed, Enter in the field does not submit, and there is deliberately
238
+ no API that takes a note on its own. On a decision the approval itself is the
239
+ authorization; a gate that inferred consent from a typed explanation would let
240
+ the explanation authorize the call.
241
+
242
+ ---
243
+
244
+ ## The markdown seam
245
+
246
+ **Markdown rendering is not built here and must not be.** It is being built for
247
+ the vanilla widget. When it lands, this is where it plugs in:
248
+
249
+ ```tsx
250
+ const Prose: ProseRenderer = ({ text }) => /* nodes */;
251
+ <DriverPanel … Prose={Prose} />
252
+ ```
253
+
254
+ What the seam guarantees to whatever fills it:
255
+
256
+ - **`text` is the model's complete text for one turn.** `onSay` delivers a whole
257
+ turn at once — driverkit does not stream tokens — so a renderer never sees a
258
+ half-written fence, never has to parse incrementally, and needs none of the
259
+ streaming-markdown machinery the rest of the world needs.
260
+ - It is called during render, so it must be pure. Memoize an expensive parse in
261
+ the host.
262
+ - **It returns nodes, never HTML.** The prop is a component precisely so that
263
+ the escape hatch cannot be `dangerouslySetInnerHTML` over a model's output,
264
+ which is the least trusted thing on the screen. Nothing in this area injects
265
+ HTML and a test pins that.
266
+
267
+ The default renderer is the identity: the model's text, as text. Plugging
268
+ markdown in is replacing something rather than filling a hole.
269
+
270
+ ---
271
+
272
+ ## How the optional peer is enforced
273
+
274
+ Four things have to hold, and three of them are subtraction.
275
+
276
+ 1. **`peerDependencies: { react: ">=18" }` with
277
+ `peerDependenciesMeta: { react: { optional: true } }`.** This is what stops
278
+ npm installing React for somebody who does not want it, and stops it warning
279
+ about a peer they correctly do not have. `>=18` because
280
+ `useSyncExternalStore` is an 18 API and it is the one that makes this area
281
+ small.
282
+ 2. **React is in no `dependencies`.** The package's only dependency is
283
+ `@types/node`, which the relay's own declarations need.
284
+ 3. **No file in the package imports `react`, in any form, including
285
+ `import type`.** This is the one that peer-optional alone does not buy: an
286
+ `import type { ReactNode } from "react"` in a published `.d.ts` is a dangling
287
+ module reference for every consumer who did not install React. There is
288
+ nothing to dangle because the React API is typed structurally in
289
+ `src/react/runtime.ts` and passed in.
290
+ 4. **`./react` is reachable only by naming it.** Nothing in `core`, `agent`,
291
+ `widget`, `controls`, `browser`, `check`, `otel` or `relay` imports this
292
+ area, so the module graph of somebody importing only `driverkit` does not
293
+ contain it.
294
+
295
+ Proved in `test/packaging.test.ts`, in the style of the entry-point tests
296
+ already there:
297
+
298
+ - `./react` resolves under browser conditions like every other non-relay entry,
299
+ with `types` before `default`.
300
+ - the peer is declared, optional, and absent from `dependencies`.
301
+ - the emitted JavaScript for `dist/react` names **no** bare specifier — not even
302
+ `react` — so a page can load it from a plain URL with no import map.
303
+ - walking the emitted imports from `dist/core/index.js` transitively never
304
+ reaches `dist/react/`.
305
+
306
+ **A test-time renderer is not a shipped dependency.** `react`, `react-dom` and
307
+ `jsdom` are dev dependencies of the *workspace* — the repository root, not the
308
+ package — so they are in this repository's lockfile and in nobody's install.
309
+ All four assertions above are about `packages/driverkit/package.json` and the
310
+ files under `dist/`, and all four still hold with React on disk; the one that
311
+ would have noticed a slip is the probe that loads `dist/react/index.js` in a
312
+ child `node` and checks it names no bare specifier at all.
313
+
314
+ ## What is feature-equivalent, and what is deliberately not
315
+
316
+ Equivalent, and tested:
317
+
318
+ - the conversation: sending instructions, the model's replies, the stop notes
319
+ - **the approval gate**, under the same three options (`confirmCommits`,
320
+ `commitAskAbove`, `confirmEveryCommit`), the same `reversible` reading, the
321
+ same size check, the same `{ asked: false }` for a commit nobody was asked
322
+ about, and the same no-other-calls lock held while a question is open
323
+ - the run controls: pause, resume, single-step, and a visible, changeable pace
324
+ that reaches the run already in flight
325
+ - the session budget, where a ceiling is declared
326
+ - stop, and a clear that takes a dying run's events with it
327
+
328
+ Deliberately not:
329
+
330
+ - **No launcher, no drag, no snapping, no corner.** Those exist because the
331
+ vanilla widget has to place itself in a page nobody told it about. Here the
332
+ host renders the panel in its own layout.
333
+ - **No shadow root.** The vanilla panel needs one because it renders into an app
334
+ whose CSS it has never seen. A React app has a CSS isolation story of its own,
335
+ and a shadow root would break it — the host's own class names would not reach
336
+ inside, which is the opposite of the point.
337
+ - **No stylesheet at all.** Class names (`dk-step`, `dk-ask`, …) and
338
+ `data-dk` / `data-phase` / `data-status` attributes are emitted for you to
339
+ style. The one exception is the inline clip on the screen-reader-only status
340
+ regions, which is not a look: a `role="status"` that is `display: none` is not
341
+ announced.
342
+ - **No `localStorage`.** The vanilla panel remembers a viewer's pace and
343
+ position because it has nowhere else to put them. A React app has a user
344
+ object and a server, and a library writing to `localStorage` behind its back
345
+ is a surprise. Pace, step mode and confirmations are state with setters; the
346
+ host persists them if it wants to.
347
+ - **No element highlighting.** `at` is on every step entry and `onAim` fires
348
+ with it, which is the seam. The outline itself is DOM drawn outside React's
349
+ tree into a page whose stacking and transforms belong to the host, and a React
350
+ app that wants it can draw it with the tools it already has.
351
+ - **No settings view.** Four preferences and a budget row is a design-system
352
+ decision, and the state is all on the snapshot.
353
+
354
+ ## The vanilla design decisions, kept and dropped
355
+
356
+ | vanilla decision | here | why |
357
+ | --- | --- | --- |
358
+ | shadow root | **dropped** | a React app's CSS isolation is its own, and a shadow root breaks it |
359
+ | fixed-size launcher ("Ask", never the app's name) | **dropped** | there is no launcher; the host places the panel |
360
+ | `textContent` over `innerHTML` | **kept, restated** | React escapes by default; the rule becomes "nothing here calls `dangerouslySetInnerHTML`", including at the markdown seam |
361
+ | the `era` counter on Clear | **kept** | React does not help: a `setState` from an async callback that outlived a Clear is exactly as wrong as the `appendChild` it replaced |
362
+ | the commit gate's three options and the `reversible` reading | **kept, identically** | one of the two panels being laxer than the other would make "which panel is this app using" a security question |
363
+ | row text that reads the same pending or finished (state as a class, never as words) | **kept** | a step row is a record |
364
+ | "No" focused / reached first, "Yes" one key further | **kept** | a person pressing Enter on a card that appeared while they were typing must not thereby accept a thousand rows |
365
+ | the step row appearing on `onBeforeCall`, before the pace wait | **kept** | the only way "you can see it coming and stop it" is true rather than aspirational |
366
+ | named speeds rather than a free slider | **kept** | milliseconds are not a thing anybody has an opinion about, and four labelled radios are keyboard-reachable without anybody being careful forever |
367
+ | `aria-live="polite"` on the whole transcript | **dropped as a bug** | at a 600ms pace the region is re-read as each step row lands, so the person hears a growing conversation from the top instead of what just changed. State is announced instead, in one-sentence `role="status"` regions that stay in the document, each with a keyed line inside it so a change replaces a node rather than editing one |
368
+ | the approval row keeping its outcome line | **kept and extended** | the vanilla row survives and records that *you* answered; this also records what the *app* then did, which is the half that turns a decision into a record of a change |
369
+
370
+ ## What a React adopter can get wrong that the vanilla widget made impossible
371
+
372
+ Worth reading once; each of these is a shape the DOM version had no way to
373
+ express.
374
+
375
+ - **Rendering the transcript inside a live region.** The vanilla panel owns its
376
+ markup, so its (wrong) `aria-live` was at least one wrong thing in one place.
377
+ A host that wraps `<DriverPanel />` in its own `aria-live="polite"` container,
378
+ or in a design-system `<Toast>` that is one, reproduces the bug from outside
379
+ and nothing in here can stop it.
380
+ - **Replacing `ApprovalCard` with one that unmounts on decision.** The receipt
381
+ is the product. A card that returns `null` once `phase !== "asking"`, or that
382
+ renders decided approvals into a separate "history" list, throws away the
383
+ question/answer link in scrollback. If you replace the card, keep the entry
384
+ rendered in place.
385
+ - **Treating `pending` as a modal.** `d.pending` is convenient and it is also
386
+ the entry that is already in `entries`. Rendering it a second time in a dialog
387
+ gives two live "Yes" buttons for one commit.
388
+ - **Keying transcript entries by index.** Entries are appended and patched in
389
+ place; `entry.id` is stable and an index is not, so an index key will make a
390
+ receipt inherit another entry's state mid-run.
391
+ - **Calling `useDriver` twice for one driver.** Two stores means two gates, and
392
+ the second one's `onBeforeCommit` is a second question about one commit. One
393
+ store per driver; lift it if two components need it. (One hook rendered
394
+ twice under `StrictMode` is *not* this: that builds one store, and a test
395
+ pins it.)
396
+ - **Expecting an options change to take effect.** The store is built once per
397
+ `driver` and the options are read then. The things a person changes while
398
+ watching a run — pace, step mode, confirmations — are state with setters for
399
+ exactly that reason. Re-key the component if you genuinely need new options.
400
+ - **Persisting grants.** If you add storage for `PanelSnapshot.grants`, you have
401
+ changed what a standing approval means. See above.
402
+
403
+ ## StrictMode, and what the hook does about it
404
+
405
+ Worth one section because it is the configuration every React app runs in
406
+ development, and because this area got it wrong.
407
+
408
+ `StrictMode` double-invokes a component's body and, since React 18, also runs
409
+ each mount effect's **cleanup and then the effect again**. The second half is
410
+ the one that bites a library holding a resource: the obvious
411
+ `useEffect(() => () => store.destroy(), [store])` therefore destroys the store
412
+ during mount, and `destroy` is final — it clears the listeners and latches the
413
+ store closed, so `ask` returns immediately and the panel renders perfectly
414
+ while doing nothing at all. That is what this hook did in `70ca8aa`, and no
415
+ framework-free test could see it, because nothing in one runs an effect twice.
416
+
417
+ What it does now:
418
+
419
+ - **The store is built lazily in render and kept in a ref**, so the
420
+ double-invoked render finds the one it already made. `useMemo` would not do:
421
+ React may double-invoke a memo factory, and two stores for one driver means
422
+ two gates.
423
+ - **Render tears nothing down.** A `driver` change no longer destroys the
424
+ outgoing store from inside render — a render may be discarded, and destroying
425
+ a live store speculatively would abort a run that is still on screen. The
426
+ effect owns every teardown.
427
+ - **The teardown is a hold count, decided one microtask late.** The cleanup
428
+ gives up its hold; only a hold count still at zero on the next microtask is a
429
+ real unmount. StrictMode's remount re-takes the hold first, so the store
430
+ survives with its conversation intact.
431
+ - **A mount onto a store somebody already destroyed builds a new one**, through
432
+ `PanelStore.destroyed`. That is the hidden-then-re-shown case — `<Activity>`
433
+ far enough apart that the microtask already ran — where nothing can revive
434
+ the store. The conversation is gone in that case; the panel is not.
435
+
436
+ ## What has not been rendered
437
+
438
+ Honest list, since the top of this file now claims rather a lot more than it
439
+ used to.
440
+
441
+ - **jsdom is not a browser.** It lays nothing out and has no screen reader, so
442
+ "the status region is announced" is not asserted anywhere and cannot be. What
443
+ is asserted is the structure that makes it possible. The vanilla widget's
444
+ equivalent claims meet a real Chrome in `e2e/`; this panel's do not, because
445
+ the demo in `examples/` is the vanilla one.
446
+ - **No concurrent-rendering stress.** `useSyncExternalStore` is what makes the
447
+ panel tear-free under a transition, and that is React's guarantee rather than
448
+ a tested one here. What was removed was the thing that would break it: render
449
+ no longer has side effects.
450
+ - **One React.** The tests run against React 19. The peer range is `>=18`
451
+ because `useSyncExternalStore` is an 18 API; nothing here has been rendered by
452
+ an 18.
453
+ - **No React Server Components.** `useDriver` is a client hook over a live
454
+ driver and a browser event stream; the panel has never been near an RSC
455
+ boundary, and `"use client"` is the host's to write.
456
+
457
+ ## Reference
458
+
459
+ ```ts
460
+ createDriverPanel(react): { useDriver, DriverPanel, components }
461
+
462
+ useDriver(driver, options): Driving
463
+ // entries, busy, runState, paceMs, permits, stepMode, confirmCommits,
464
+ // budget?, grants, pending?, era
465
+ // ask, stop, clear, pause, resume, step, setPace, setStepMode,
466
+ // setConfirmCommits, decide, revoke, approve, store
467
+
468
+ createPanelStore(driver, options): PanelStore // the same thing, without React
469
+ receiptLine(entry): string // what a receipt says
470
+ stepOutcome(ok, refused?, count?): string // what a step row says
471
+ COMMIT_ASK_ABOVE // 25, as in the vanilla widget
472
+ ```
473
+
474
+ `PanelOptions`: `think`, `instructions`, `maxSteps`, `refusalLimit`, `greeting`,
475
+ `confirmCommits`, `commitAskAbove`, `confirmEveryCommit`, `onBeforeCommit`,
476
+ `grants`, `notes`, `pace`, `stepMode`, `controls`, `clock`, `onAim`, `onError`,
477
+ `AbortController`.
package/README.md CHANGED
@@ -1,3 +1,105 @@
1
- # Temporary Holding Version
1
+ # driverkit
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Make a web app drivable by an agent. No pixels, no selectors, no model.
4
+
5
+ Your app declares what it can do and what is on screen. An agent calls those
6
+ verbs by name. The app's own functions run, and the UI moves the way it would
7
+ have if somebody had clicked.
8
+
9
+ ```sh
10
+ npm i driverkit
11
+ ```
12
+
13
+ ```ts
14
+ import { mount } from "driverkit";
15
+
16
+ const driver = mount({
17
+ app: "Contacts",
18
+ verbs: [
19
+ { name: "search", kind: "read", describe: "filter the list",
20
+ args: { q: { type: "string", describe: "what to search for" } },
21
+ run: ({ q }) => store.search(q) },
22
+
23
+ { name: "select-where", kind: "describe", describe: "select every contact at a company",
24
+ args: { company: { type: "string", describe: "company name" } },
25
+ run: ({ company }) => ({ count: store.select(company) }) },
26
+
27
+ { name: "accept-selected", kind: "commit", describe: "accept the current selection",
28
+ refuses: ["nothing-selected"],
29
+ run: (_, ctx) => store.selected.size ? store.accept() : ctx.refuse("nothing-selected") },
30
+ ],
31
+ visible: () => ({
32
+ where: "contacts",
33
+ rev: store.rev,
34
+ rows: store.onScreen(),
35
+ page: { index: store.page, size: 20, total: store.total },
36
+ selected: store.visibleSelection(),
37
+ selectedCount: store.selected.size,
38
+ }),
39
+ });
40
+ ```
41
+
42
+ That is the whole integration: one new file in your app, one import.
43
+
44
+ ## Entry points
45
+
46
+ One package, one version, one changelog. Tree-shaking keeps a page free of
47
+ what it does not import.
48
+
49
+ | import | what |
50
+ |---|---|
51
+ | `driverkit` | the registry: verbs, the screen, one `call` |
52
+ | `driverkit/widget` | a chat panel in the corner of your own app |
53
+ | `driverkit/agent` | the loop behind it, provider-neutral |
54
+ | `driverkit/controls` | three generic verbs over elements you marked `data-dk-control` — see [CONTROLS.md](CONTROLS.md) |
55
+ | `driverkit/browser` | the page side of the relay: one SSE stream, held open |
56
+ | `driverkit/relay` | the relay — **Node only** |
57
+
58
+ `driverkit/relay` imports `node:http`, so it is published under a `"node"`
59
+ export condition and no other. A browser-targeted build that asks for it fails
60
+ at build time with the import named, rather than pulling Node builtins into a
61
+ page bundle. There is no browser shim on purpose: a `createRelay` that
62
+ resolved to an empty module would silently do nothing, which is worse than a
63
+ build error.
64
+
65
+ Pre-1.0, so one version covers everything — a widget fix bumps this number
66
+ too. That is deliberate: there is one number that says which driverkit you are
67
+ running, and no matrix of which widget works with which core.
68
+
69
+ No runtime dependencies. `@types/node` is a types-only dependency, there
70
+ because the relay's own `.d.ts` files re-export `node:http` types in their
71
+ public surface; nothing is emitted into a page bundle by it.
72
+
73
+ ## The two things it drives
74
+
75
+ **A chat panel in the corner of your app.** The page holds the driver, so
76
+ there is no transport at all — a loop asks a model what to do and calls the
77
+ verb. Your API key stays on your backend behind one endpoint.
78
+
79
+ ```ts
80
+ import { mountWidget } from "driverkit/widget";
81
+ import { backendThink } from "driverkit/agent";
82
+
83
+ mountWidget(driver, { think: backendThink("/api/chat") });
84
+ ```
85
+
86
+ **An agent somewhere else** — another process, another machine, a voice
87
+ assistant. Nothing can call *into* a browser tab, so the tab dials out to a
88
+ relay and holds the line open. The agent talks to the relay over MCP.
89
+
90
+ ```
91
+ agent ──► MCP ──► relay ──► your tab ──► your app's function
92
+ ```
93
+
94
+ The relay ships twice, once here and once on PyPI (`pip install driverkit`,
95
+ which also carries the MCP server). The two implementations are tested against
96
+ each other, frame for frame.
97
+
98
+ ## Status
99
+
100
+ Early. Treat the APIs as unstable until there is a tagged release.
101
+
102
+ The full README, the wire protocol, the design arguments and a 3,412-row demo
103
+ that is drivable both ways live in the repository.
104
+
105
+ MIT.
@@ -0,0 +1,69 @@
1
+ /**
2
+ * The default deployment story, in thirty lines.
3
+ *
4
+ * A browser cannot hold an API key. So the shape that actually ships is: the
5
+ * page posts the conversation to the app's own backend, the backend — which
6
+ * already authenticates this person and already holds the key — calls whatever
7
+ * model it likes and answers with prose or tool calls. This is that post.
8
+ *
9
+ * It is a convenience and not a layer. `Think` is a function; if your backend
10
+ * speaks a different shape, write the fetch yourself and do not use this. The
11
+ * reason it is here at all is that the first thing anybody asks of a package
12
+ * with no model client in it is "so what do I pass", and the answer should be
13
+ * code rather than a paragraph.
14
+ *
15
+ * **What the backend should do with the two static fields.** `system` and
16
+ * `tools` are byte-identical on every request within a run, and across runs
17
+ * and across users for as long as the app's verbs do not change. A backend
18
+ * that forwards them with its provider's caching marker on (Anthropic's
19
+ * `cache_control`, or whatever the equivalent is wherever you are sending it)
20
+ * pays for the app's whole capability list once instead of once per turn. A
21
+ * backend that appends its own per-request string to `system` before
22
+ * forwarding — a user id, a clock — gives that saving back and will not notice,
23
+ * because nothing breaks. It just costs more.
24
+ *
25
+ * **What the backend must not do.** Trust the verb names. The page tells the
26
+ * model what the app can do, and the model's reply comes back through here and
27
+ * is executed against the driver mounted in *this* page, under *this* person's
28
+ * session — which is the same authority model the relay has and PROTOCOL.md's
29
+ * "Who is allowed to do this" applies unchanged. But a backend that stores or
30
+ * replays these messages is storing the app's row data, because the snapshots
31
+ * are in them.
32
+ */
33
+ import type { Think } from "./types.ts";
34
+ /** The slice of `fetch` this needs. Narrow so a test can satisfy it and so
35
+ * this file does not depend on DOM lib types. */
36
+ export interface FetchLike {
37
+ (url: string, init: {
38
+ method: string;
39
+ headers: Record<string, string>;
40
+ body: string;
41
+ signal?: AbortSignal;
42
+ }): Promise<ResponseLike>;
43
+ }
44
+ /** The slice of `Response` this reads. `text` is optional so a hand-written
45
+ * fake that only answers `json()` still satisfies the type; see `detailOf`. */
46
+ export interface ResponseLike {
47
+ ok: boolean;
48
+ status: number;
49
+ json(): Promise<unknown>;
50
+ text?(): Promise<string>;
51
+ }
52
+ export interface BackendThinkOptions {
53
+ /** Extra headers — a CSRF token, usually. */
54
+ headers?: Record<string, string>;
55
+ /** Injected, defaulting to the global. Same reason as everywhere else in
56
+ * this repository: a module that can only run in a browser is a module
57
+ * whose error path is tested by hand. */
58
+ fetch?: FetchLike;
59
+ }
60
+ /**
61
+ * `Think` over one HTTP endpoint.
62
+ *
63
+ * Posts `{ system, tools, messages }` and expects `{ text?, calls? }` back.
64
+ * Credentials ride on the request the way the rest of the app's requests do —
65
+ * `same-origin` is the browser default for a same-origin URL, so a session
66
+ * cookie is already attached and there is nothing to configure.
67
+ */
68
+ export declare function backendThink(url: string, options?: BackendThinkOptions): Think;
69
+ //# sourceMappingURL=backend.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"backend.d.ts","sourceRoot":"","sources":["../../src/agent/backend.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAyB,MAAM,YAAY,CAAC;AAE/D;kDACkD;AAClD,MAAM,WAAW,SAAS;IACxB,CACE,GAAG,EAAE,MAAM,EACX,IAAI,EAAE;QACJ,MAAM,EAAE,MAAM,CAAC;QACf,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAChC,IAAI,EAAE,MAAM,CAAC;QACb,MAAM,CAAC,EAAE,WAAW,CAAC;KACtB,GACA,OAAO,CAAC,YAAY,CAAC,CAAC;CAC1B;AAED;gFACgF;AAChF,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IACzB,IAAI,CAAC,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;CAC1B;AAED,MAAM,WAAW,mBAAmB;IAClC,6CAA6C;IAC7C,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC;;8CAE0C;IAC1C,KAAK,CAAC,EAAE,SAAS,CAAC;CACnB;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,mBAAwB,GAAG,KAAK,CAsClF"}