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,622 @@
1
+ /**
2
+ * The manifest, rendered as a tool list and a system prompt.
3
+ *
4
+ * **Nothing in this file knows the name of a single verb.** Each verb in the
5
+ * manifest becomes one tool, its `args` become the tool's `inputSchema`, and
6
+ * its `refuses` go into the description so a model can decline to make a call
7
+ * that was always going to be declined. The app's verb table is the only
8
+ * description of the app that exists, and it lives next to the code that
9
+ * implements it.
10
+ *
11
+ * This is the twin of `tools_for`/`verb_tool` in
12
+ * `python/src/driverkit/mcp_server.py`, and the notes below are the same
13
+ * strings. The Python half renders for an MCP client driving the app from
14
+ * outside; this half renders for a chatbot inside the page. A verb that is
15
+ * described one way through MCP and another way in the widget is a bug: the
16
+ * whole promise is that the app declares its capabilities once.
17
+ *
18
+ * Everything here is the STATIC half. It is a pure function of the manifest,
19
+ * which the core guarantees does not vary between two users or two minutes, so
20
+ * the output is byte-identical across every turn of every session of every
21
+ * person using the app — which is what makes it worth caching in the prompt
22
+ * prefix. Reaching into `driver.snapshot()` from this file would quietly undo
23
+ * that for everybody; it is not imported here, which is the cheapest way to
24
+ * make that impossible rather than merely discouraged.
25
+ */
26
+ /**
27
+ * The one tool that is not a verb.
28
+ *
29
+ * It is the protocol's own read, not an app feature, and no app should have to
30
+ * declare it. Verb names are kebab-case by the core's contract and this is
31
+ * snake_case so they cannot collide; `toolsFor` checks anyway rather than
32
+ * silently shadowing a verb.
33
+ *
34
+ * MCP's other hand-written tool, `list_tabs`, is deliberately absent. In-page
35
+ * there is one app and it is the one the person is looking at; a tool that
36
+ * lists tabs would invite a model to ask which tab it is driving, and the
37
+ * honest answer is "this one, there is no other".
38
+ */
39
+ export const READ_SCREEN = "read_screen";
40
+ const RESULT_NOTE = "Returns the screen as it is AFTER this runs, so you do not need to read it again.";
41
+ const REFUSAL_NOTE = "A refusal is a normal answer, not an error: read the reason and change what you do, " +
42
+ "rather than repeating the same call.";
43
+ /**
44
+ * `kind` folded into the description, because it is the difference between a
45
+ * call that is free to try and one that is irreversible, and nothing else in
46
+ * the tool definition can express that. Asked to "select the top 1000 and bulk
47
+ * accept", a model that cannot tell `select` from `accept` finds out by doing
48
+ * it, and the finding out costs a thousand accepted rows.
49
+ *
50
+ * Verbatim from the Python half, including the asterisks around the commit
51
+ * banner. It looks shouty in a source file and it is the one place in this
52
+ * library where shouting is the correct engineering decision.
53
+ *
54
+ * `read` says "changes no data and reports no count" and no longer says
55
+ * "changes nothing", because `go`, `open-contact` and `back` are reads and they
56
+ * move the person's screen out from under them. The two things the sentence
57
+ * must get right are the ones that decide whether the next call is safe; "the
58
+ * person will see it" is said as well, because they will.
59
+ */
60
+ const KIND_NOTE = {
61
+ read: "READ-ONLY: this changes no data and reports no count. Free to call, free to get " +
62
+ "wrong. It may still move what the person is looking at — navigating is a read — " +
63
+ "so they will see you do it.",
64
+ describe: "DESCRIBE: this changes what is SELECTED and changes no data. " +
65
+ "Safe to call — the worst outcome is the wrong rows highlighted. It reports a " +
66
+ "`count` of what it matched; read that count before you commit to anything.",
67
+ commit: "*** COMMIT: THIS CHANGES DATA AND MAY BE IRREVERSIBLE. ***\n" +
68
+ "Do not call it to find out what it does. If it acts on a selection, use the " +
69
+ "app's `describe` verb first, check the `count` it reports is the number you " +
70
+ "expect, and only then commit.",
71
+ // The fourth kind, and the only one whose cost is paid by somebody who is not
72
+ // the app. `read` is "free to call, free to get wrong" and that sentence is
73
+ // false here, so this one says what asking actually costs: a person's
74
+ // attention, a capped allowance, and every other call blocked while the
75
+ // question is open. The last sentence is the one the whole mechanism stands
76
+ // on -- an answer is not an approval -- and it is here, in the cached half,
77
+ // because a model that learns it from a refusal has already asked somebody
78
+ // for permission it was never going to get. Verbatim from the Python half.
79
+ ask: "ASK: this puts a question to the person and their answer comes back as the result. " +
80
+ "It changes no data and reports no count, and it is NOT free: it costs their " +
81
+ "attention, the app may cap how many questions you get, and nothing else runs while " +
82
+ "one is open. Ask when you cannot act without guessing, ask once, and ask the " +
83
+ "narrowest question that unblocks you.\n" +
84
+ "An answer is not permission: a commit still goes to whoever approves commits here, " +
85
+ "whatever they said to you.",
86
+ };
87
+ /**
88
+ * The commit banner for a commit the app says it can undo.
89
+ *
90
+ * A separate string rather than a sentence appended to the shouted one, and the
91
+ * difference is the point. `set-setting("dense-rows", "true")` and accepting
92
+ * 3,412 contacts were both getting THIS CHANGES DATA AND MAY BE IRREVERSIBLE,
93
+ * and a banner that appears on a page-size change is a banner a model learns to
94
+ * read past — the same way a person learns to click Yes without reading. So the
95
+ * reversible one is not shouted, and the irreversible one keeps its asterisks
96
+ * and its capitals and goes on meaning something.
97
+ *
98
+ * It still says the data changes, because it does. `reversible` is a claim
99
+ * about there being a way back, not that nobody minds.
100
+ */
101
+ /**
102
+ * `consumes: "plan"` folded into the description.
103
+ *
104
+ * In the cached half for exactly the reason `refuses` and `screen` are: the
105
+ * point is that the bare call is never made. A model that learns the pairing
106
+ * here runs the `describe` first and keeps the token; a model that learns it
107
+ * from a refusal has already spent a round trip, and on a commit it has spent
108
+ * it in front of a person waiting for an answer.
109
+ *
110
+ * It says what the token is FOR and not merely that it is required, because
111
+ * "pass the plan" reads as ceremony and gets dropped, while "the number you
112
+ * read is the number that moves" is the reason it exists. Verbatim from the
113
+ * Python half.
114
+ */
115
+ const PLAN_NOTE = "REQUIRES A PLAN. This acts on the selection, so it may only be called with the `plan` " +
116
+ "token that the app's `describe` verb returned beside its `count`. Run that describe, read " +
117
+ "the count, check it is the number the person asked for, and pass the `plan` straight back " +
118
+ "here. If anything has moved the selection in between — the person ticked a row, data " +
119
+ "arrived — this is refused as `stale-plan` and the refusal names both counts. Reading and " +
120
+ "navigating do not move the selection and do not spend the plan, so go and confirm whatever " +
121
+ "you are unsure of first. That is the point of the token: the number you read is the number " +
122
+ "that moves.";
123
+ const COMMIT_REVERSIBLE_NOTE = "COMMIT (reversible): this changes data, and the app says the change can be undone. " +
124
+ "Still a change the person will see, so do not call it to find out what it does; if it " +
125
+ "acts on a selection, read the `count` from the app's `describe` verb first.";
126
+ /**
127
+ * `screen` folded into the description.
128
+ *
129
+ * The tool list is the same on every screen — a verb list that changed with the
130
+ * screen would bust the prompt prefix on every navigation, which is the cost
131
+ * argument — so this sentence is how a model learns, in the cached half, that a
132
+ * verb it can see will not work where it is standing. Exactly the argument for
133
+ * putting `refuses` in here: the point is that the call is never made.
134
+ *
135
+ * A verb on several screens names them all, because naming one of three is
136
+ * worse than naming none: a model told `next-page` is "ONLY ON THE `contacts`
137
+ * SCREEN" will navigate away from the company list it was already standing on
138
+ * to do something it could have done where it was. The one-screen wording is
139
+ * byte-for-byte what it was before this could be a list, so no app's cached
140
+ * prefix moved for a feature it does not use.
141
+ */
142
+ function screenNote(screen) {
143
+ const screens = Array.isArray(screen) ? screen : [screen];
144
+ const quoted = screens.map((s) => `\`${s}\``);
145
+ const where = quoted.length > 1
146
+ ? `ONLY ON THE ${quoted.slice(0, -1).join(", ")} AND ${quoted[quoted.length - 1]} SCREENS.`
147
+ : `ONLY ON THE ${quoted[0]} SCREEN.`;
148
+ return (`${where} The screen you are on is the \`screen\` line of ` +
149
+ "every screen you read. Called from anywhere else this is refused as `wrong-screen`, " +
150
+ "and the refusal names where you are and where the verb lives.");
151
+ }
152
+ /**
153
+ * One `ArgSpec` as one JSON Schema property.
154
+ *
155
+ * `oneOf` becomes `enum`. See the note on `JsonSchema`: the two names look
156
+ * alike and the meanings diverge, and emitting JSON Schema's `oneOf` would
157
+ * produce a schema that validates anything.
158
+ */
159
+ export function argSchema(spec) {
160
+ if (spec.type !== "string" && spec.type !== "number" && spec.type !== "boolean") {
161
+ // Thrown, not skipped. The core's three types are a closed set, so an
162
+ // unknown one is a bug that should be loud here rather than become a
163
+ // property no client will validate.
164
+ throw new Error(`driverkit: unknown arg type ${JSON.stringify(spec.type)}`);
165
+ }
166
+ return {
167
+ type: spec.type,
168
+ description: spec.describe,
169
+ ...(spec.oneOf ? { enum: spec.oneOf } : {}),
170
+ };
171
+ }
172
+ /**
173
+ * One manifest verb as one tool.
174
+ *
175
+ * `optional` is expressed by omission from `required`, which is how JSON
176
+ * Schema says it; an `"optional": true` key would be ignored by every
177
+ * validator and the argument would silently become mandatory.
178
+ *
179
+ * `rev` is added as an extra optional property on every verb. It is not an app
180
+ * argument — it is `CallOptions.rev`, the staleness claim, hoisted into the
181
+ * schema because a model has nowhere else to put it. Passing the `rev` it last
182
+ * read is what turns "act on a row somebody already moved" into a clean
183
+ * `stale` refusal, and in-page that is not a rare case: the person is sitting
184
+ * right there, clicking, while the model thinks. A verb that declares its own
185
+ * `rev` argument keeps it and does not get the hoisted one, because the app's
186
+ * meaning wins over ours.
187
+ */
188
+ export function verbTool(verb) {
189
+ const args = verb.args ?? {};
190
+ const properties = {};
191
+ const required = [];
192
+ for (const [name, spec] of Object.entries(args)) {
193
+ properties[name] = argSchema(spec);
194
+ if (!spec.optional)
195
+ required.push(name);
196
+ }
197
+ // NOT on an `ask`, and this is the one exception to hoisting them onto
198
+ // everything.
199
+ //
200
+ // `rev` and `screen` are staleness claims about the screen a call acts on,
201
+ // and a question acts on no screen: it puts text in front of a person and
202
+ // returns what they typed. Offering them would be worse than merely wasteful.
203
+ // The system prompt tells a model to quote the `rev` it is acting on, so a
204
+ // model that has read its instructions WILL pass one here -- and then a
205
+ // question gets refused `stale` because the person clicked a row while it was
206
+ // being composed, which is the likeliest thing in the world when they are
207
+ // sitting right there. A question refused for a reason that has nothing to do
208
+ // with the question is the shape that teaches a model not to ask.
209
+ const asking = verb.kind === "ask";
210
+ if (!asking && !("rev" in properties)) {
211
+ properties.rev = {
212
+ type: "integer",
213
+ description: "Optional. The `rev` from the last screen you read. If the screen has " +
214
+ "moved since, the call is refused as `stale` instead of acting on " +
215
+ "something you did not see. Omit it to skip that check.",
216
+ };
217
+ }
218
+ // Hoisted exactly as `rev` is, and for a failure `rev` does not catch: a
219
+ // paging call sent with no rev once landed on a screen the agent had never
220
+ // seen, paged a different company's list and answered `ok`. "You are
221
+ // somewhere else" and "the number moved" are different mistakes with
222
+ // different fixes, so they are different claims and different refusals.
223
+ //
224
+ // A verb with its own `screen` argument keeps it and does not get this one —
225
+ // `go(screen)` is a real verb in a real app — because the app's meaning wins
226
+ // over ours. The same rule as `rev`.
227
+ if (!asking && !("screen" in properties)) {
228
+ properties.screen = {
229
+ type: "string",
230
+ description: "Optional. The `screen` from the last screen you read. If the app is on " +
231
+ "another screen now, the call is refused as `wrong-screen` instead of acting " +
232
+ "on a screen you never saw. Omit it to skip that check.",
233
+ };
234
+ }
235
+ // Hoisted like `rev` and `screen`, with one difference: ONLY onto a verb that
236
+ // declares `consumes`. `rev` and `screen` are claims any verb can make, so
237
+ // they go on everything; a plan token means nothing to a verb that does not
238
+ // consume one, and adding the property to every tool in every app would move
239
+ // every cached prompt prefix in the world for a feature those apps do not
240
+ // use. That prefix is the cost argument the library is built on.
241
+ //
242
+ // NOT in `required`, although the core will refuse the call without it, and
243
+ // that is deliberate. `required` moves the failure into the client's
244
+ // validator, where the refusal's hint — which names both counts and spells
245
+ // out the pairing — never reaches the model. This library's stance throughout
246
+ // is that an agent learns from a refusal it can act on, so the schema stays
247
+ // permissive and the core is strict. The description says it is required.
248
+ if (verb.consumes === "plan" && !("plan" in properties)) {
249
+ properties.plan = {
250
+ type: "string",
251
+ description: "Required. The `plan` token from the `describe` verb that counted this " +
252
+ "selection, passed back exactly as it was given. Without it the call is " +
253
+ "refused as `stale-plan`, and so is a token taken before the selection moved.",
254
+ };
255
+ }
256
+ const description = [verb.describe];
257
+ if (verb.kind === "commit" && verb.reversible) {
258
+ // Checked before the table, so a reversible commit reads differently from
259
+ // an irreversible one. `reversible` is read only here, on a `commit`: on a
260
+ // `read` or a `describe` it is a claim about nothing, and the app has
261
+ // already said no data moves.
262
+ description.push(COMMIT_REVERSIBLE_NOTE);
263
+ }
264
+ else if (verb.kind && KIND_NOTE[verb.kind]) {
265
+ // Second, straight after what the verb does and ahead of the mechanical
266
+ // notes, so a model skimming the first two lines of a description cannot
267
+ // miss that a verb commits. An unknown or absent kind adds nothing: this
268
+ // package does not manufacture a safety claim the app did not make, in
269
+ // either direction.
270
+ description.push(KIND_NOTE[verb.kind]);
271
+ }
272
+ // Third, above `screen`, because HOW a verb may be called outranks WHERE: a
273
+ // model that goes to the right screen and calls the commit bare has still
274
+ // lost the turn, and this is the sentence that stops it. Both still outrank
275
+ // the mechanics.
276
+ if (verb.consumes === "plan")
277
+ description.push(PLAN_NOTE);
278
+ if (verb.screen)
279
+ description.push(screenNote(verb.screen));
280
+ description.push(RESULT_NOTE);
281
+ if (verb.refuses && verb.refuses.length) {
282
+ // In the description rather than only in the error path, because this is
283
+ // the half that gets cached in the prompt prefix: the point of `refuses`
284
+ // is that a well-briefed agent never makes the call.
285
+ description.push("May refuse: " + verb.refuses.join(", ") + ". " + REFUSAL_NOTE);
286
+ }
287
+ return {
288
+ name: verb.name,
289
+ description: description.join("\n\n"),
290
+ inputSchema: {
291
+ type: "object",
292
+ properties,
293
+ required,
294
+ // The app named its own arguments; an extra one is a mistake worth
295
+ // catching at the client rather than sending to a verb that will ignore
296
+ // it.
297
+ additionalProperties: false,
298
+ },
299
+ };
300
+ }
301
+ /** The whole tool list: every verb, then the one fixed read. */
302
+ export function toolsFor(manifest) {
303
+ const tools = manifest.verbs.map(verbTool);
304
+ if (!tools.some((t) => t.name === READ_SCREEN)) {
305
+ tools.push({
306
+ name: READ_SCREEN,
307
+ description: `Read what is on screen in ${manifest.app || "the app"} right now: where the ` +
308
+ "person is, the rows visible to them, what they have selected, and the `rev` to " +
309
+ "quote back when you act.\n\n" +
310
+ "You see exactly what the person at the keyboard sees — same filters, same page — " +
311
+ "and nothing more. If a record you expect is missing, it is probably off screen " +
312
+ "rather than absent.\n\n" +
313
+ "You are given the screen at the start and after every verb, so you rarely need " +
314
+ "this. Use it when the person may have clicked something while you were thinking, " +
315
+ "or after anything unexpected: it is the cheapest tool here and the only way to " +
316
+ "settle what actually happened.",
317
+ inputSchema: { type: "object", properties: {}, required: [], additionalProperties: false },
318
+ });
319
+ }
320
+ return tools;
321
+ }
322
+ /**
323
+ * The app's map, rendered for the model.
324
+ *
325
+ * `Manifest.screens` is the static list of every screen the app has, so this
326
+ * belongs in the cached prefix beside the tool list rather than in any turn's
327
+ * tail. What an agent could not do before this existed is learn that a screen
328
+ * EXISTS without standing on it: `Snapshot.screen` says where it is and
329
+ * `Verb.screen` says where a verb works, and between them there was still no
330
+ * way to find out that a companies list is in there somewhere. The worked
331
+ * example closed that gap by pasting a prose screen-map into `instructions`,
332
+ * which is the same map the app's own route table already had, written a
333
+ * second time in English with nothing checking that the two agree.
334
+ *
335
+ * The sentence about it being the same for every user is not padding. It is
336
+ * the one thing a model cannot tell by looking: a list that might have been
337
+ * filtered for this person is a list whose absences mean nothing, and an agent
338
+ * that cannot trust the absences has to probe for screens anyway. See
339
+ * `ScreenSpec`, which forbids filtering it, and which puts "you may not open
340
+ * that" in the refusal where it can be read.
341
+ *
342
+ * Verbatim from the Python half.
343
+ */
344
+ export function screensNote(screens) {
345
+ const lines = [
346
+ "THE SCREENS THIS APP HAS. The same screens for every user, always — a screen listed " +
347
+ "here exists whether or not you have been to it. Only which one is LIVE changes, and " +
348
+ "that is the `screen` line of every screen you read. If you cannot get to one of " +
349
+ "these, the app will tell you why when you try; it is never missing from this list.",
350
+ ];
351
+ for (const screen of screens) {
352
+ const needs = Object.keys(screen.args ?? {});
353
+ lines.push(`- \`${screen.id}\`: ${screen.describe}` +
354
+ (screen.url ? ` Address: \`${screen.url}\`.` : "") +
355
+ (needs.length ? ` Needs: ${needs.map((n) => `\`${n}\``).join(", ")}.` : ""));
356
+ }
357
+ return lines.join("\n");
358
+ }
359
+ /**
360
+ * One flow, rendered as the recipe it is.
361
+ *
362
+ * The id first and the `describe` on the same line, because the `describe` is
363
+ * what a model picks a flow on — Microsoft's finding on selection across tool
364
+ * groups is that the choice is made "based on the plugin's description, not on
365
+ * any of the individual functions" — and the steps are what it follows once it
366
+ * has picked. A `section` is printed once where it changes rather than on
367
+ * every step, so a flow with phases reads as phases and a flow without one
368
+ * costs nothing.
369
+ *
370
+ * TWO MARKS GO IN PARENTHESES AFTER THE VERB, and both are there so that a
371
+ * model skimming five numbered lines can see the shape of the recipe without
372
+ * reading every hint:
373
+ *
374
+ * OPTIONAL — a step with a `skipWhen`. Which steps carry the job and which
375
+ * are there for the case where you do not already know the answer is
376
+ * structural, and before the field it was a clause buried mid-hint.
377
+ *
378
+ * the plan handoff — a step whose verb consumes a plan, pointing at the
379
+ * numbered step whose `describe` mints it. DERIVED, not declared: it is read
380
+ * off `Verb.kind` and `Verb.consumes`, which the manifest already carries, so
381
+ * there is nothing for an author to keep in step and nothing claimed here
382
+ * that `mount()` has not already refused to let through. It is the one value
383
+ * that crosses two steps in this library — see `FlowStep` for why there is no
384
+ * general way to say "pass step 2's output into step 3".
385
+ *
386
+ * Verbatim from the Python half.
387
+ */
388
+ function renderFlow(flow, verbs) {
389
+ const lines = [`\`${flow.id}\` — ${flow.describe}`];
390
+ let section;
391
+ // The numbered step whose `describe` verb last minted a plan token, so the
392
+ // mark can name it. Updated AFTER the current step is rendered: a describe
393
+ // does not consume its own plan.
394
+ let minted;
395
+ flow.steps.forEach((step, i) => {
396
+ if (step.section && step.section !== section)
397
+ lines.push(` (${step.section})`);
398
+ section = step.section;
399
+ const verb = verbs.find((v) => v.name === step.verb);
400
+ const marks = [];
401
+ if (step.skipWhen)
402
+ marks.push(`OPTIONAL — skip it when ${step.skipWhen}`);
403
+ if (verb?.consumes === "plan" && minted !== undefined) {
404
+ marks.push(`takes the \`plan\` token step ${minted} returned`);
405
+ }
406
+ const mark = marks.length ? ` (${marks.join("; ")})` : "";
407
+ lines.push(` ${i + 1}. \`${step.verb}\`${mark} — ${step.hint}`);
408
+ if (verb?.kind === "describe")
409
+ minted = i + 1;
410
+ });
411
+ return lines.join("\n");
412
+ }
413
+ /**
414
+ * The app's flows, rendered for the model.
415
+ *
416
+ * THE SENTENCE THAT MATTERS is the one saying there is no tool that runs a
417
+ * flow. A model handed a list of numbered steps will look for the button, and
418
+ * not finding one it may well decide the flow is unavailable; being told that
419
+ * following the steps IS the mechanism, and why, is what turns a list into an
420
+ * instruction. The why is honest and is the design: a flow that executed
421
+ * internally could not adapt when step three found the record already
422
+ * accepted, and the agent would get back an `ok` that told it nothing. Apple's
423
+ * app skills say the same thing about themselves — "Skills don't enforce
424
+ * deterministic behavior, and Siri doesn't run the listed intents in sequence."
425
+ *
426
+ * Static, so it rides in the prefix that was already being cached and costs
427
+ * nothing per turn. Verbatim from the Python half.
428
+ */
429
+ export function flowsNote(flows, verbs = []) {
430
+ return ("THE JOBS THIS APP SAYS ITS VERBS ARE USUALLY COMBINED INTO. Each one is a recipe for " +
431
+ "YOU to carry out with the verbs you already have. **There is no tool that runs a " +
432
+ "flow**, and that is deliberate: following the steps yourself means you see what each " +
433
+ "one actually returned and can change course, which something running them for you " +
434
+ "could not. Take the steps in order, read what each verb reports before the next, and " +
435
+ "depart from the recipe when the screen says something it did not expect — then say so." +
436
+ // The sentence about OPTIONAL steps is added only when some step actually
437
+ // is one. Everything in here rides in the cached prefix of every session of
438
+ // every user, and a paragraph about a construct the app does not use is the
439
+ // kind of cost that is real, paid once, and invisible -- which is the exact
440
+ // cost `SIZE_LIMITS` exists to make somebody weigh. An app whose flows mark
441
+ // nothing optional gets the note it got before the field existed.
442
+ (flows.some((f) => f.steps.some((s) => s.skipWhen))
443
+ ? " A step marked OPTIONAL may be skipped when the condition beside it holds, and " +
444
+ "that condition is a sentence for you to judge, not a rule something checks: when " +
445
+ "it is not clearly true, take the step."
446
+ : "") +
447
+ "\n\n" +
448
+ flows.map((f) => renderFlow(f, verbs)).join("\n\n"));
449
+ }
450
+ /**
451
+ * The system prompt. A pure function of the manifest — including the app's own
452
+ * `Manifest.instructions` — and the host's static `instructions`, and of
453
+ * nothing else.
454
+ *
455
+ * Mirrors the MCP server's `initialize` instructions, with the differences
456
+ * that are actually true in-page: there is no relay, no tab to pick, and the
457
+ * person whose screen this is can see every word of what happens next.
458
+ *
459
+ * The screen is NOT in here, and that is the rule this function exists to
460
+ * enforce. It is the uncached half; putting it in the system prompt would mean
461
+ * re-sending and re-paying for the entire prefix on every single turn, which
462
+ * is the exact cost the static/dynamic split was designed to avoid. It goes in
463
+ * the newest message instead — see `loop.ts`.
464
+ */
465
+ export function systemPrompt(manifest, instructions,
466
+ /**
467
+ * `DriveOptions.timing`, so the note about what a `took:` line means is paid
468
+ * for ONCE, in the half a provider caches, instead of once per call.
469
+ *
470
+ * Absent and `"off"` are the same thing and add nothing: a caller from before
471
+ * timing existed gets the byte-identical prompt it got before, which is what
472
+ * keeps every existing eval's cached prefix valid on upgrade.
473
+ *
474
+ * Still a pure function of its arguments, which is the property that matters
475
+ * here more than any wording: `drive` computes this once, before the loop,
476
+ * and `test/tools.test.ts` asserts it is byte-identical across turns.
477
+ */
478
+ timing = "off") {
479
+ const app = manifest.app || "this app";
480
+ const parts = [
481
+ `You are driving a live ${app} session: the page the person is looking at as you ` +
482
+ "read this. You act only through the verbs listed as your tools, which are this " +
483
+ "app's own. You cannot click, type, scroll or see pixels, and you do not need to.",
484
+ "Every verb returns the screen as it is AFTER it ran, so acting and looking are one " +
485
+ "step. Never follow a verb with a read to find out what it did — you were just " +
486
+ "told. Quote the `rev` from the screen you are acting on, so that if the person " +
487
+ "clicks something while you are thinking you get a `stale` refusal instead of " +
488
+ "changing a row you never saw.",
489
+ "Every verb says what it does to the world. READ-ONLY changes no data and reports " +
490
+ "no count — moving to another screen is a read, and the person watching will see " +
491
+ "it. DESCRIBE changes what is selected and reports a `count` of what it matched. " +
492
+ "COMMIT changes data; it may be irreversible, and a COMMIT that says it is " +
493
+ "reversible can be undone but is still a change they will see.\n\n" +
494
+ "Never commit on a selection whose count you have not read. The pairing is: run " +
495
+ "the app's `describe` verb with the predicate, read the `count` it reports, check " +
496
+ "that count is the number the person asked for, and only then run the `commit` " +
497
+ "verb — which takes no predicate and acts on the current selection. If the count " +
498
+ "is not what you expected, stop and say so in plain words. Asked to accept a " +
499
+ "thousand rows, accepting fifty thousand because a filter was wider than anybody " +
500
+ "thought is the single worst thing you can do here, and checking the count is how " +
501
+ "you avoid it.\n\n" +
502
+ "Some commit verbs say they REQUIRE A PLAN. Those return a `plan` token beside the " +
503
+ "`count` of the describe that counted them, and you pass it straight back. Do not " +
504
+ "hold one over for later and do not reuse one: a plan is good for one commit and " +
505
+ "only while nothing has moved the selection. Looking is free: reading a screen or " +
506
+ "navigating to another one leaves the plan good, so go back and confirm anything you " +
507
+ "are unsure of before an irreversible commit. `stale-plan` means the rows changed " +
508
+ "under you — it names the count you were shown and the count there is now — so run " +
509
+ "the describe again, read the new number, and say it out loud before you commit to " +
510
+ "it. That refusal is not an obstacle; it is the one that stops you from accepting a " +
511
+ "set the person never agreed to.",
512
+ "A refusal is a normal answer, not an error. Read the reason and do something " +
513
+ "different; repeating the same call unchanged will get the same answer. If you " +
514
+ "cannot find a way through, say so — the person is right there and can do it " +
515
+ "themselves or tell you what you are missing.",
516
+ "You can do exactly what that person could do, and nothing more. You also see " +
517
+ "exactly what they see — same filters, same sort, same page — so a record you " +
518
+ "expect and cannot find is probably off screen rather than absent; use the app's " +
519
+ "own paging verb the way a person would.",
520
+ "When you are finished, answer in prose and say what you did. A message with no " +
521
+ "verb call ends your turn. Keep it short: this is rendered in a small panel in " +
522
+ "the corner of the person's screen.",
523
+ ];
524
+ // The paragraph about asking, and only for an app that CAN ask.
525
+ //
526
+ // Conditional for the reason the screens note and the flows note are: this
527
+ // text rides in the cached prefix of every session of every user, and a
528
+ // paragraph about a tool the app has not got is a cost that is real, paid
529
+ // once per person, and invisible -- which is what `SIZE_LIMITS` exists to make
530
+ // somebody weigh. It is also worse than merely wasteful: a model told to ask
531
+ // rather than guess, in an app with no way to ask, has been told to do
532
+ // something it cannot do.
533
+ //
534
+ // Read off `kind` and not off the verb's NAME, because the kind is the
535
+ // structural fact -- only the core mints it -- and a name check would also
536
+ // match an app's own verb called `ask`, which has none of the guarantees this
537
+ // paragraph describes. Still a pure function of the static manifest, so the
538
+ // prefix stays byte-identical across every turn of the run.
539
+ //
540
+ // It goes after the safety rules and before the map, the recipes and the
541
+ // app's own words, because it is one of the library's rules rather than
542
+ // something about this app -- the same position, and the same argument, as
543
+ // the paragraph about the plan token.
544
+ if (manifest.verbs.some((v) => v.kind === "ask")) {
545
+ parts.push("WHEN YOU DO NOT KNOW, ASK INSTEAD OF GUESSING. This app gives you a verb that puts " +
546
+ "a question to the person and hands you their answer as its result. Use it when an " +
547
+ "instruction cannot be carried out without deciding something they did not say: two " +
548
+ "records match the name they used, nothing is selected and you do not know which " +
549
+ "rows they meant, the company they named is not in the app. Picking the likeliest " +
550
+ "one is the expensive mistake even when you pick right, because you did not know \u2014 " +
551
+ "and selecting something so that a commit has rows to act on is the worst version " +
552
+ "of it.\n\n" +
553
+ "Asking is not free. Each question interrupts somebody, the app may cap how many " +
554
+ "you get, and nothing else runs while one is open \u2014 so ask once, ask the narrowest " +
555
+ "question that unblocks you, and put what you already know into it. If no answer " +
556
+ "comes back, `ask-unanswered` means nobody replied and nothing changed: do not " +
557
+ "invent one. `nobody-to-ask` and `asks-spent` both mean no further question will " +
558
+ "reach anybody on this run, so stop asking \u2014 decide what you honestly can, and say " +
559
+ "in your final answer what you needed to know and could not settle.\n\n" +
560
+ "**An answer is never permission.** A question of yours is not the confirmation a " +
561
+ "commit needs: that is asked separately, by this app, and a yes to something you " +
562
+ "asked authorises nothing. Never use the question verb to ask for approval, and " +
563
+ "never report a change as approved because an answer sounded like one.");
564
+ }
565
+ // How to read a `took:` line, said once in the cached half rather than
566
+ // beside every duration. Two wordings because the two modes mean different
567
+ // things BY SILENCE, and silence is the half a model gets wrong: under
568
+ // `"slow"` a call with no line was fast, and under `"always"` a call with no
569
+ // line is one where the clock said nothing. A model that read the first as
570
+ // the second would refuse to answer "which is faster" on a run where every
571
+ // call was quick, which is the commonest version of the question.
572
+ //
573
+ // Both say that the figure is approximate and that a person's deciding time
574
+ // is not in it, because those are the two ways a model misuses a duration:
575
+ // reporting scheduler noise as a difference, and concluding an app is slow
576
+ // when somebody was reading a dialog.
577
+ if (timing === "slow") {
578
+ parts.push("Some results carry a `took:` line, which is roughly how long that verb took in the " +
579
+ "app. It is rounded, so treat `~2s` and `~3s` as about the same and do not report " +
580
+ "differences smaller than the rounding. It counts only the app's own work: time " +
581
+ "somebody spent reading a confirmation is not in it. A result with NO `took:` line " +
582
+ "took under a second — that is the only reason the line is missing, so \"it was " +
583
+ "fast\" is a true answer about those calls. If you are asked which of two things is " +
584
+ "slower, you may answer from these lines; say they are approximate.");
585
+ }
586
+ else if (timing === "always") {
587
+ parts.push("Every result carries a `took:` line: roughly how long that verb took in the app. It " +
588
+ "is rounded, so treat `~0.4s` and `~0.5s` as about the same and do not report " +
589
+ "differences smaller than the rounding. It counts only the app's own work: time " +
590
+ "somebody spent reading a confirmation is not in it. If you are asked which of two " +
591
+ "things is slower, answer from these lines and say they are approximate.");
592
+ }
593
+ // The map and the recipes, between the library's rules and the app's own
594
+ // words. Both are pure functions of the static manifest, so the prefix stays
595
+ // byte-identical across every turn of every session of every user -- which
596
+ // is the only reason either of them is affordable at all. They go AFTER the
597
+ // safety rules, because a recipe is a thing to do within those rules and not
598
+ // an exception to them, and BEFORE `instructions`, because whichever text is
599
+ // last is the one a model follows when two of them disagree and the app's
600
+ // own voice should outrank a list the core rendered.
601
+ if (manifest.screens && manifest.screens.length)
602
+ parts.push(screensNote(manifest.screens));
603
+ if (manifest.flows && manifest.flows.length) {
604
+ parts.push(flowsNote(manifest.flows, manifest.verbs));
605
+ }
606
+ // The app's own briefing, then the host's. Both last, not first: an
607
+ // app-specific note is an addition to these rules and not a replacement for
608
+ // them, and a model reading in order should meet the safety rules before the
609
+ // local colour. Both static — see `Manifest.instructions`, which is where an
610
+ // app says the thing it needs BOTH paths to hear, and `DriveOptions
611
+ // .instructions`, which is this host's own and reaches only this path.
612
+ //
613
+ // The app's goes first of the two because the host embedding the app is the
614
+ // more specific voice, and whichever is last is the one a model follows when
615
+ // they disagree.
616
+ if (manifest.instructions)
617
+ parts.push(manifest.instructions);
618
+ if (instructions)
619
+ parts.push(instructions);
620
+ return parts.join("\n\n");
621
+ }
622
+ //# sourceMappingURL=tools.js.map