@yaks/tools 0.0.0-stage → 0.2.36

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 (138) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +870 -2
  3. package/_dist/access.d.ts +3 -0
  4. package/_dist/access.d.ts.map +1 -0
  5. package/_dist/app-view.d.ts +27 -0
  6. package/_dist/app-view.d.ts.map +1 -0
  7. package/_dist/args.d.ts +23 -0
  8. package/_dist/args.d.ts.map +1 -0
  9. package/_dist/check.d.ts +32 -0
  10. package/_dist/check.d.ts.map +1 -0
  11. package/_dist/declared.d.ts +66 -0
  12. package/_dist/declared.d.ts.map +1 -0
  13. package/_dist/display.d.ts +2 -0
  14. package/_dist/display.d.ts.map +1 -0
  15. package/_dist/errors.d.ts +46 -0
  16. package/_dist/errors.d.ts.map +1 -0
  17. package/_dist/fault.d.ts +4 -0
  18. package/_dist/fault.d.ts.map +1 -0
  19. package/_dist/invoke.d.ts +13 -0
  20. package/_dist/invoke.d.ts.map +1 -0
  21. package/_dist/mod.d.ts +31 -0
  22. package/_dist/mod.d.ts.map +1 -0
  23. package/_dist/outlast.d.ts +89 -0
  24. package/_dist/outlast.d.ts.map +1 -0
  25. package/_dist/persist.d.ts +5 -0
  26. package/_dist/persist.d.ts.map +1 -0
  27. package/_dist/read.d.ts +4 -0
  28. package/_dist/read.d.ts.map +1 -0
  29. package/_dist/readable.d.ts +59 -0
  30. package/_dist/readable.d.ts.map +1 -0
  31. package/_dist/registry.d.ts +53 -0
  32. package/_dist/registry.d.ts.map +1 -0
  33. package/_dist/result.d.ts +154 -0
  34. package/_dist/result.d.ts.map +1 -0
  35. package/_dist/routes.d.ts +11 -0
  36. package/_dist/routes.d.ts.map +1 -0
  37. package/_dist/runner.d.ts +161 -0
  38. package/_dist/runner.d.ts.map +1 -0
  39. package/_dist/said.d.ts +4 -0
  40. package/_dist/said.d.ts.map +1 -0
  41. package/_dist/schema.d.ts +76 -0
  42. package/_dist/schema.d.ts.map +1 -0
  43. package/_dist/state.d.ts +9 -0
  44. package/_dist/state.d.ts.map +1 -0
  45. package/_dist/title.d.ts +20 -0
  46. package/_dist/title.d.ts.map +1 -0
  47. package/_dist/value.d.ts +43 -0
  48. package/_dist/value.d.ts.map +1 -0
  49. package/_dist/views.d.ts +5 -0
  50. package/_dist/views.d.ts.map +1 -0
  51. package/_dist/vocab.d.ts +9 -0
  52. package/_dist/vocab.d.ts.map +1 -0
  53. package/_dist/watch.d.ts +28 -0
  54. package/_dist/watch.d.ts.map +1 -0
  55. package/access.js +3 -0
  56. package/access.js.map +1 -0
  57. package/access.ts +11 -0
  58. package/app-view.js +26 -0
  59. package/app-view.js.map +1 -0
  60. package/app-view.ts +41 -0
  61. package/args.js +136 -0
  62. package/args.js.map +1 -0
  63. package/args.ts +187 -0
  64. package/body.js +14 -0
  65. package/body.js.map +1 -0
  66. package/body.ts +15 -0
  67. package/check.js +67 -0
  68. package/check.js.map +1 -0
  69. package/check.ts +92 -0
  70. package/declared.js +529 -0
  71. package/declared.js.map +1 -0
  72. package/declared.ts +719 -0
  73. package/deno.json +1 -0
  74. package/display.js +21 -0
  75. package/display.js.map +1 -0
  76. package/display.ts +39 -0
  77. package/errors.js +99 -0
  78. package/errors.js.map +1 -0
  79. package/errors.ts +127 -0
  80. package/fault.js +17 -0
  81. package/fault.js.map +1 -0
  82. package/fault.ts +24 -0
  83. package/invoke.js +207 -0
  84. package/invoke.js.map +1 -0
  85. package/invoke.ts +210 -0
  86. package/mod.js +31 -0
  87. package/mod.js.map +1 -0
  88. package/mod.ts +78 -0
  89. package/outlast.js +416 -0
  90. package/outlast.js.map +1 -0
  91. package/outlast.ts +551 -0
  92. package/package.json +109 -4
  93. package/persist.js +18 -0
  94. package/persist.js.map +1 -0
  95. package/persist.ts +26 -0
  96. package/read.js +50 -0
  97. package/read.js.map +1 -0
  98. package/read.ts +58 -0
  99. package/readable.js +118 -0
  100. package/readable.js.map +1 -0
  101. package/readable.ts +134 -0
  102. package/registry.js +162 -0
  103. package/registry.js.map +1 -0
  104. package/registry.ts +207 -0
  105. package/result.js +497 -0
  106. package/result.js.map +1 -0
  107. package/result.ts +540 -0
  108. package/routes.js +46 -0
  109. package/routes.js.map +1 -0
  110. package/routes.ts +50 -0
  111. package/runner.js +748 -0
  112. package/runner.js.map +1 -0
  113. package/runner.ts +910 -0
  114. package/said.js +15 -0
  115. package/said.js.map +1 -0
  116. package/said.ts +12 -0
  117. package/schema.js +137 -0
  118. package/schema.js.map +1 -0
  119. package/schema.ts +134 -0
  120. package/state.js +69 -0
  121. package/state.js.map +1 -0
  122. package/state.ts +108 -0
  123. package/title.js +222 -0
  124. package/title.js.map +1 -0
  125. package/title.ts +257 -0
  126. package/value.js +57 -0
  127. package/value.js.map +1 -0
  128. package/value.ts +74 -0
  129. package/views.js +131 -0
  130. package/views.js.map +1 -0
  131. package/views.ts +125 -0
  132. package/vocab.js +22 -0
  133. package/vocab.js.map +1 -0
  134. package/vocab.json +268 -0
  135. package/vocab.ts +22 -0
  136. package/watch.js +64 -0
  137. package/watch.js.map +1 -0
  138. package/watch.ts +74 -0
package/declared.js ADDED
@@ -0,0 +1,529 @@
1
+ // An app's OWN commands (T-32685, T-34541, T-38021): the `$defs` entries of an
2
+ // app's `vocab.json` marked `"tool": true`, the same form a package declares
3
+ // its tools in (@yaks/vocab `toolsSaid`, D-37544). app_deploy reads them beside
4
+ // the components and keeps them in that app's store (graph.ts `/tools`).
5
+ // Everyone who can reach the app reads them with the platform's `commands`
6
+ // tool and runs one with `command` — they are not MCP tools themselves,
7
+ // because that list is snapshotted by a directory and must be the same for
8
+ // everybody (workers/yak/declared.ts). One entry is a sentence, its arguments,
9
+ // and one act:
10
+ //
11
+ // { "$defs": {
12
+ // "log_run": {
13
+ // "tool": true,
14
+ // "description": "Log a run for the club leaderboard",
15
+ // "input": { "who": { "type": "string" },
16
+ // "miles": { "type": "number" } },
17
+ // "required": ["miles"],
18
+ // "apply": { "entity": { "eid": "$run" },
19
+ // "run": { "who": "$who", "miles": "$miles" } } },
20
+ // "leaderboard": {
21
+ // "tool": true,
22
+ // "description": "This month's runs",
23
+ // "input": { "since": { "type": "string" } },
24
+ // "query": ".run&.created.at>=$since" } } }
25
+ //
26
+ // `input` is one JSON Schema per argument and `required` names the ones a
27
+ // caller must send, as a package writes them. The act is what an app adds: a
28
+ // TEMPLATE over the app's own store. `apply` is the wire's entity bundle (or a
29
+ // list of them) and `query` is a filter line, and both are written in the ONE
30
+ // variable language the wire and the query grammar already speak. `$name` is a
31
+ // variable: BOUND by an argument of that name, it is that argument's value,
32
+ // typed by its schema; left unbound, it is what it has always been — an alias
33
+ // the store mints an entity at, so the same `$run` names the row in the bundle
34
+ // that made it. A `$name` that is neither an argument nor an entity this
35
+ // template writes is refused at deploy, because a tool that cannot be filled
36
+ // is one an agent calls once and gives up on. `$$` is a literal dollar sign.
37
+ //
38
+ // An argument a caller left out takes its variable with it: the key holding
39
+ // it is not written, and the `&`-clause naming it drops out of the filter
40
+ // line.
41
+ //
42
+ // An entry may also name a `view`: a page in the app's own files that the
43
+ // person's agent renders the answer in (T-32687), served at the MCP door as
44
+ // `ui://<space>/<app>/<file>` and named beside the command in what `commands`
45
+ // answers. The app writes that page the way it writes any other.
46
+ //
47
+ // And `"model": true` offers the command to the app's own models (D-40545): a
48
+ // transcript in the app's store may call it as a tool, as the person who asked
49
+ // for the turn. A command not marked is never a model's to call. In one that
50
+ // is, `$session` is the transcript whose turn called it: bound by where the
51
+ // call runs, never by what the model sends, so a model cannot be talked into
52
+ // naming another. Called any other way, such a command is refused.
53
+ //
54
+ // Nothing here reaches the store: filling a template makes the same body a
55
+ // page's own `apply` and `query` send, and the call goes through the app's
56
+ // ordinary doors with the caller's identity and the app's access rule
57
+ // (workers/yak/declared.ts). So a tool can do exactly what the person
58
+ // calling it could do on the page, and never more.
59
+ import { listed, whole } from "@yaks/query";
60
+ import { argsOf } from "@yaks/graph/call";
61
+ import { publicToolSchema, toolCheck, toolGrammar } from "@yaks/vocab/tools";
62
+ import { CallError } from "./args.js";
63
+ export { mayCall } from "./access.js";
64
+ // The variables a model's call binds from where it runs.
65
+ let CONTEXT = [
66
+ 'session'
67
+ ];
68
+ export let TOOLS_EXAMPLE = '{"$defs": {"log_run": {"tool": true, ' + '"description": "Log a run", "input": {"miles": {"type": "number"}}, ' + '"required": ["miles"], ' + '"apply": {"entity": {"eid": "$run"}, "run": {"miles": "$miles"}}}}}';
69
+ // The keys an entry may carry. Unknown ones are refused rather than ignored,
70
+ // so a misspelling is a sentence at deploy and not a tool that quietly does
71
+ // half of what was meant.
72
+ let KEYS = [
73
+ 'tool',
74
+ 'description',
75
+ 'title',
76
+ 'revision',
77
+ 'service',
78
+ 'input',
79
+ 'required',
80
+ 'positional',
81
+ 'apply',
82
+ 'query',
83
+ 'worker',
84
+ 'view',
85
+ 'model',
86
+ 'floor',
87
+ 'discoverable',
88
+ 'readOnly'
89
+ ];
90
+ // A `view` names a page in the app's OWN files (T-32687) — a relative path
91
+ // under the app's root, which the MCP door serves as `ui://<space>/<app>/
92
+ // <file>` for the host to render the answer in. HTML because that is the one
93
+ // type the MCP Apps spec renders; relative because the app's files are the
94
+ // only place a view may come from, and a path that climbs out of them would
95
+ // be a door onto somebody else's app.
96
+ let VIEW = /^(?!\/)(?!.*(?:^|\/)\.\.(?:\/|$))[\w./-]+\.html$/;
97
+ // A command's name and an input's name: the same shape a tool name takes, so
98
+ // a command reads as a verb wherever it is said — `command` takes it whole,
99
+ // beside the app it belongs to.
100
+ let NAME = /^[a-z][a-z0-9_]{0,39}$/;
101
+ // A variable, and the whole of a string that is nothing but one — the
102
+ // difference between a value passed through with its type and a value spliced
103
+ // into text. `$$` is a literal dollar and never a variable.
104
+ let VAR = /\$\$|\$([a-z][a-z0-9_]*)/g;
105
+ let ONLY = /^\$([a-z][a-z0-9_]*)$/;
106
+ let object = (v)=>!!v && typeof v == 'object' && !Array.isArray(v);
107
+ // The form before this one: `{{arg}}` was a hole where `$arg` is now a
108
+ // variable. A deploy that still writes one is refused, below, in the sentence
109
+ // that says what to write instead.
110
+ let HOLE = /\{\{[a-z][a-z0-9_]*\}\}/;
111
+ // The wire's own keys beside the components: what an entity bundle may say
112
+ // that is not a component name (@yaks/graph `Bundle`).
113
+ let WIRE = [
114
+ 'entity',
115
+ 'edges',
116
+ 'tombstone',
117
+ 'was'
118
+ ];
119
+ // Every variable in a template, wherever the strings are.
120
+ let vars = (v, found = new Set())=>{
121
+ if (typeof v == 'string') {
122
+ for (let m of v.matchAll(VAR))if (m[1]) found.add(m[1]);
123
+ } else if (Array.isArray(v)) {
124
+ for (let one of v)vars(one, found);
125
+ } else if (object(v)) {
126
+ for (let one of Object.values(v))vars(one, found);
127
+ }
128
+ return found;
129
+ };
130
+ // The variables this template MINTS: every `$name` standing alone as an
131
+ // entity's eid. Those are the aliases the store gives real ids to, so a
132
+ // `$name` pointing at one is a join and not a missing argument.
133
+ let minted = (v, found = new Set())=>{
134
+ if (Array.isArray(v)) {
135
+ for (let one of v)minted(one, found);
136
+ } else if (object(v)) {
137
+ let eid = object(v.entity) ? v.entity.eid : undefined;
138
+ let only = typeof eid == 'string' ? ONLY.exec(eid) : null;
139
+ if (only) found.add(only[1]);
140
+ for (let one of Object.values(v))minted(one, found);
141
+ }
142
+ return found;
143
+ };
144
+ // The components an `apply` template names, so a deploy can say which of them
145
+ // nobody declared. A bundle is an object (or a list of them) whose keys are
146
+ // the wire's own words and component names.
147
+ let named = (v, found = new Set())=>{
148
+ if (Array.isArray(v)) {
149
+ for (let one of v)named(one, found);
150
+ } else if (object(v)) {
151
+ for (let key of Object.keys(v))if (!WIRE.includes(key)) found.add(key);
152
+ }
153
+ return found;
154
+ };
155
+ // The tool entries of a manifest: every `$defs` entry marked `"tool": true`.
156
+ // The rest of the document is its components (vocab.ts `appDoc`), and neither
157
+ // reading has to know about the other.
158
+ let entriesOf = (held)=>object(held) && object(held.$defs) ? Object.entries(held.$defs).filter((e)=>object(e[1]) && e[1].tool === true) : [];
159
+ // The manifest as written, its tools checked whole: every problem in one
160
+ // sentence, the way the components are refused (T-32628), because an agent
161
+ // that fixes one problem per deploy stops after the second. `words` are the
162
+ // components the app's store knows, which are the only ones a template may
163
+ // write. `file` is what the sentence calls the manifest.
164
+ export let parseTools = (source, words = [], file = 'vocab.json')=>{
165
+ if (typeof source == 'string') {
166
+ if (!source.trim()) return {};
167
+ // JSON, and only JSON — the YAML door is not in this module's graph: a
168
+ // caller holding a `.yml` reads it first (tools.ts `released`).
169
+ try {
170
+ source = JSON.parse(source);
171
+ } catch {
172
+ throw new CallError('arguments', `${file} is not JSON — ${TOOLS_EXAMPLE}`);
173
+ }
174
+ }
175
+ let wrong = [];
176
+ let out = {};
177
+ for (let [name, entry] of entriesOf(source)){
178
+ if (!NAME.test(name)) {
179
+ wrong.push(`${JSON.stringify(name)} is not a tool name (a-z, 0-9, _)`);
180
+ continue;
181
+ }
182
+ let alien = Object.keys(entry).filter((k)=>!KEYS.includes(k));
183
+ if (alien.length) {
184
+ wrong.push(`${name}: ${alien.join(', ')} — a tool says ${KEYS.join(', ')}`);
185
+ }
186
+ let description = entry.description ?? entry.title;
187
+ if (entry.title !== undefined && (typeof entry.title != 'string' || !entry.title.trim())) {
188
+ wrong.push(`${name}.title must be text — use description to say what the tool does`);
189
+ }
190
+ if (typeof description != 'string' || !description) {
191
+ wrong.push(`${name}.description says what the tool does, in a sentence`);
192
+ }
193
+ let input = {};
194
+ if (entry.input != null) {
195
+ if (!object(entry.input)) {
196
+ wrong.push(`${name}.input is an object of arguments — ${TOOLS_EXAMPLE}`);
197
+ } else {
198
+ for (let [arg, schema] of Object.entries(entry.input)){
199
+ if (!NAME.test(arg)) {
200
+ wrong.push(`${name}.input: ${JSON.stringify(arg)} is not an ` + 'argument name (a-z, 0-9, _)');
201
+ } else if (!object(schema)) {
202
+ wrong.push(`${name}.input.${arg} is ${JSON.stringify(schema)} — an ` + 'argument is a JSON Schema, like {"type": "number"}');
203
+ } else input[arg] = schema;
204
+ }
205
+ }
206
+ }
207
+ let required = [];
208
+ if (entry.required != null) {
209
+ if (!Array.isArray(entry.required) || !entry.required.every((r)=>typeof r == 'string')) {
210
+ wrong.push(`${name}.required is a list of its input's names`);
211
+ } else {
212
+ for (let arg of entry.required){
213
+ if (arg in input) required.push(arg);
214
+ else wrong.push(`${name}.required: ${arg} is no input of ${name}`);
215
+ }
216
+ }
217
+ }
218
+ let positional;
219
+ try {
220
+ positional = toolGrammar(input, entry.positional);
221
+ toolCheck({
222
+ type: 'object',
223
+ properties: input
224
+ });
225
+ } catch (error) {
226
+ wrong.push(`${name}.grammar: ${error.message}`);
227
+ }
228
+ if (HOLE.test(JSON.stringify(entry.apply ?? entry.query ?? entry.worker ?? ''))) {
229
+ wrong.push(`${name}: {{arg}} is not a hole any more — write $arg, the same ` + 'variable the wire and the query grammar already speak');
230
+ }
231
+ let acts = [
232
+ 'apply',
233
+ 'query',
234
+ 'worker'
235
+ ].filter((k)=>entry[k] != null);
236
+ if (acts.length != 1) {
237
+ wrong.push(`${name} does one thing: apply (a bundle to write), query (a ` + 'filter line to read), or worker (a path in its server code)');
238
+ }
239
+ if (entry.view != null) {
240
+ if (typeof entry.view != 'string' || !VIEW.test(entry.view)) {
241
+ wrong.push(`${name}.view is a page in this app's files, like ` + '"leaderboard.html"');
242
+ }
243
+ }
244
+ if (entry.model != null && typeof entry.model != 'boolean') {
245
+ wrong.push(`${name}.model is true, to offer it to the app's models`);
246
+ }
247
+ if (entry.service != null && (!object(entry.service) || typeof entry.service.pricing != 'string' || !entry.service.pricing.trim())) {
248
+ wrong.push(`${name}.service names its pricing terms as {pricing: string}`);
249
+ }
250
+ if (entry.service != null && entry.worker == null) {
251
+ wrong.push(`${name}.service requires a worker command to authorize customer data`);
252
+ }
253
+ if (entry.revision != null && typeof entry.revision != 'string') {
254
+ wrong.push(`${name}.revision is a source version string`);
255
+ }
256
+ if (entry.floor != null && entry.floor != 'person' && entry.floor != 'editor' && entry.floor != 'owner') {
257
+ wrong.push(`${name}.floor is person, editor, or owner`);
258
+ }
259
+ if (entry.discoverable != null && typeof entry.discoverable != 'boolean') {
260
+ wrong.push(`${name}.discoverable is false to hide an internal command`);
261
+ }
262
+ if (entry.readOnly != null && entry.readOnly !== true) {
263
+ wrong.push(`${name}.readOnly is true for a worker that only reads`);
264
+ }
265
+ if (entry.readOnly === true && entry.worker == null) {
266
+ wrong.push(`${name}.readOnly is only for a worker command`);
267
+ }
268
+ if (entry.query != null && typeof entry.query != 'string') {
269
+ wrong.push(`${name}.query is a filter line, like ".run"`);
270
+ }
271
+ if (entry.worker != null && (typeof entry.worker != 'string' || !/^\/[\w/-]+$/.test(entry.worker) || entry.worker.includes('..'))) {
272
+ wrong.push(`${name}.worker is an absolute path in this app, like "/tick"`);
273
+ }
274
+ if (entry.apply != null && !object(entry.apply) && !Array.isArray(entry.apply)) {
275
+ wrong.push(`${name}.apply is an entity bundle, or a list of them`);
276
+ }
277
+ // A variable that binds to nothing can never be filled, and a component
278
+ // nobody declared can never be written — both are the deploy's to catch,
279
+ // since the alternative is an agent calling the tool and reading `unknown
280
+ // component` from a store it cannot see. A `$name` an entity in this same
281
+ // template is minted at is neither: it is the join it looks like.
282
+ let mints = entry.apply == null ? new Set() : minted(entry.apply);
283
+ for (let held of vars(entry.apply ?? entry.query ?? entry.worker)){
284
+ if (held in input || mints.has(held)) continue;
285
+ if (CONTEXT.includes(held)) {
286
+ if (entry.model !== true) {
287
+ wrong.push(`${name}: $${held} is the transcript a model's turn runs in — ` + `mark ${name} "model": true, or declare it in ${name}.input`);
288
+ }
289
+ continue;
290
+ }
291
+ wrong.push(`${name}: $${held} names no input and no entity here — declare it ` + `in ${name}.input`);
292
+ }
293
+ for (let comp of named(entry.apply)){
294
+ if (!words.includes(comp)) {
295
+ wrong.push(`${name}.apply: ${comp} is not a component — declare it in ` + `${file}, or use one the platform declares`);
296
+ }
297
+ }
298
+ let floor = entry.floor == 'person' ? 'person' : entry.floor == 'editor' ? 'editor' : entry.floor == 'owner' ? 'owner' : undefined;
299
+ out[name] = {
300
+ description: String(description ?? ''),
301
+ ...typeof entry.revision == 'string' ? {
302
+ revision: entry.revision
303
+ } : {},
304
+ ...object(entry.service) && typeof entry.service.pricing == 'string' ? {
305
+ service: {
306
+ pricing: entry.service.pricing
307
+ }
308
+ } : {},
309
+ input,
310
+ ...required.length ? {
311
+ required
312
+ } : {},
313
+ ...positional ? {
314
+ positional
315
+ } : {},
316
+ ...entry.apply != null ? {
317
+ apply: entry.apply
318
+ } : {},
319
+ ...typeof entry.query == 'string' ? {
320
+ query: entry.query
321
+ } : {},
322
+ ...typeof entry.worker == 'string' ? {
323
+ worker: entry.worker
324
+ } : {},
325
+ ...typeof entry.view == 'string' && VIEW.test(entry.view) ? {
326
+ view: entry.view
327
+ } : {},
328
+ ...entry.model === true ? {
329
+ model: true
330
+ } : {},
331
+ ...floor ? {
332
+ floor
333
+ } : {},
334
+ ...entry.discoverable === false ? {
335
+ discoverable: false
336
+ } : {},
337
+ ...entry.readOnly === true && typeof entry.worker == 'string' ? {
338
+ readOnly: true
339
+ } : {}
340
+ };
341
+ }
342
+ if (wrong.length) {
343
+ throw new CallError('arguments', `${file}: ${wrong.join('; ')}`);
344
+ }
345
+ return out;
346
+ };
347
+ // The pages a manifest's views name, read straight off the source so a deploy
348
+ // can check them against the app's own files before anything is planted. A
349
+ // manifest that will not parse yields none: {@link parseTools} says why.
350
+ export let viewsOf = (source)=>{
351
+ let seen = new Set();
352
+ try {
353
+ // A manifest as written is a string here only when it is JSON — a `.yml`
354
+ // is read by whoever holds it and handed on as the value (see parseTools).
355
+ let held = typeof source == 'string' ? JSON.parse(source) : source;
356
+ for (let [, entry] of entriesOf(held)){
357
+ if (typeof entry.view == 'string') seen.add(entry.view);
358
+ }
359
+ } catch {}
360
+ return [
361
+ ...seen
362
+ ];
363
+ };
364
+ // The arguments a caller must send; every other one may be left out.
365
+ let needed = (tool)=>tool.required ?? [];
366
+ // The tool's arguments as one JSON Schema object, which is what a host shows
367
+ // the model and what a store's runner checks a call against (@yaks/tools).
368
+ export let schemaOf = (tool, options = {})=>{
369
+ let schema = {
370
+ type: 'object',
371
+ properties: tool.input,
372
+ required: needed(tool)
373
+ };
374
+ return options.short ? schema : publicToolSchema(schema);
375
+ };
376
+ // One argument, as its schema's type says to read it. A model sends what it
377
+ // sends — a number as a string, `"true"` for a flag — so the type it was
378
+ // declared under is what it becomes, and a value that cannot become that is
379
+ // refused by name. A schema naming no type takes the value as it came.
380
+ let typed = (arg, schema, v)=>{
381
+ let no = (why)=>new CallError('arguments', `${arg} ${why}`);
382
+ if (v == null) throw no('is required');
383
+ if (schema.type == 'number' || schema.type == 'integer') {
384
+ let n = typeof v == 'number' ? v : Number(String(v));
385
+ if (!Number.isFinite(n)) throw no('is a number');
386
+ return n;
387
+ }
388
+ if (schema.type == 'boolean') {
389
+ if (typeof v == 'boolean') return v;
390
+ if (v === 'true' || v === 'false') return v == 'true';
391
+ throw no('is true or false');
392
+ }
393
+ if (schema.type != 'string') return v;
394
+ if (typeof v == 'object') throw no('is text');
395
+ return String(v);
396
+ };
397
+ // The call's arguments, read under the declared inputs. An argument nobody
398
+ // declared is dropped: it can fill no hole, and refusing it would only teach
399
+ // the model to guess again. One the caller may leave out and did is dropped
400
+ // too, and what is missing from here is what makes a hole absent below.
401
+ let args = (tool, sent)=>Object.fromEntries(Object.entries(tool.input).filter(([arg])=>sent[arg] != null || needed(tool).includes(arg)).map(([arg, schema])=>[
402
+ arg,
403
+ typed(arg, schema, sent[arg])
404
+ ]));
405
+ // A variable the caller left empty, travelling as a value. It takes the key
406
+ // that held it with it: a column nobody named is a column nobody writes, never
407
+ // the word `undefined` in the row.
408
+ let ABSENT = Symbol('absent');
409
+ // An argument the caller left out. A variable that is not an argument at all
410
+ // is not a gap — it is an alias, and it travels on untouched.
411
+ let gaps = (s, tool, vals)=>[
412
+ ...s.matchAll(VAR)
413
+ ].some((m)=>m[1] && m[1] in tool.input && !(m[1] in vals));
414
+ // One template string, bound. A string that is NOTHING but a bound variable
415
+ // becomes the value itself with its type — `"$miles"` writes the number 5, not
416
+ // "5" — and a variable inside a sentence is spliced in as text. `$$` is a
417
+ // dollar sign, and an unbound `$name` is the alias it was.
418
+ let fill = (s, tool, vals, encode)=>{
419
+ if (gaps(s, tool, vals)) return ABSENT;
420
+ let only = ONLY.exec(s);
421
+ if (only && only[1] in vals) return vals[only[1]];
422
+ return s.replace(VAR, (as, arg)=>!arg ? '$' : arg in vals ? encode(vals[arg]) : as);
423
+ };
424
+ let filling = (v, tool, vals, encode)=>typeof v == 'string' ? fill(v, tool, vals, encode) : Array.isArray(v) ? v.map((one)=>filling(one, tool, vals, encode)).filter((one)=>one !== ABSENT) : object(v) ? Object.fromEntries(Object.entries(v).map(([k, one])=>[
425
+ k,
426
+ filling(one, tool, vals, encode)
427
+ ]).filter(([, one])=>one !== ABSENT)) : v;
428
+ // And the components a generated template lets go: one left with no columns is
429
+ // a bare write, which says the thing for `recipe` and is half a sentence for
430
+ // `alias{name}`. Nothing a person wrote names any, so nothing a person wrote
431
+ // loses one.
432
+ let dropped = (v, drop)=>Array.isArray(v) ? v.map((one)=>dropped(one, drop)) : object(v) ? Object.fromEntries(Object.entries(v).filter(([k, one])=>!(drop.includes(k) && object(one) && !Object.keys(one).length))) : v;
433
+ // One `&`-clause of a filter line, or absent when the argument it asks about
434
+ // is. A value is percent-encoded — a filter line is a query string, and a
435
+ // title with an `&` in it would otherwise read as the next filter.
436
+ // A value fills its variable as one operand (@yaks/query `listed`), so an `&`
437
+ // or a `)` in what a caller sent is part of the value, not the line.
438
+ let clause = (s, tool, vals)=>gaps(s, tool, vals) ? ABSENT : s.replace(VAR, (as, arg)=>!arg ? '$' : arg in vals ? listed([
439
+ String(vals[arg])
440
+ ]) : as);
441
+ // What the call binds from where it runs: each context variable the template
442
+ // names and no input declares, refused when the call runs nowhere that has
443
+ // one.
444
+ let bound = (tool, context)=>Object.fromEntries([
445
+ ...vars(tool.apply ?? tool.query ?? tool.worker)
446
+ ].filter((v)=>CONTEXT.includes(v) && !(v in tool.input)).map((v)=>{
447
+ let held = context[v];
448
+ if (held == null) {
449
+ throw new CallError('arguments', `$${v} is the transcript a model's turn runs in, and this call ` + "runs in none: only the app's own models call this command");
450
+ }
451
+ return [
452
+ v,
453
+ held
454
+ ];
455
+ }));
456
+ // The act this call makes: bundles to write, a filter to read, or a worker
457
+ // path with its typed arguments. An optional query clause drops out when its
458
+ // argument was left out.
459
+ export let filled = (tool, sent, context = {})=>{
460
+ let vals = {
461
+ ...bound(tool, context),
462
+ ...args(tool, sent)
463
+ };
464
+ if (tool.worker != null) {
465
+ return {
466
+ worker: tool.worker,
467
+ args: vals
468
+ };
469
+ }
470
+ if (tool.query != null) {
471
+ // A query that is nothing but one variable is a whole filter line passed
472
+ // through, so it keeps its `&`s rather than being read as one clause.
473
+ let only = ONLY.exec(tool.query);
474
+ if (only && only[1] in tool.input) {
475
+ return {
476
+ query: whole(String(vals[only[1]] ?? ''))
477
+ };
478
+ }
479
+ // The app's own line, its variables filled as operands.
480
+ return {
481
+ query: whole(tool.query.split('&').map((one)=>clause(one, tool, vals)).filter((one)=>one !== ABSENT && one !== '').join('&'))
482
+ };
483
+ }
484
+ return {
485
+ apply: dropped(filling(tool.apply, tool, vals, (v)=>String(v)), tool.drop ?? [])
486
+ };
487
+ };
488
+ /** Fill one declared command and hand its act to the caller's effect boundary. */ export let invoke = (tool, args, acts, context = {})=>{
489
+ let act = filled(tool, args, context);
490
+ if (act.worker != null) return acts.worker(act.worker, act.args ?? {});
491
+ if (act.query != null) return acts.query(act.query);
492
+ return acts.apply(Array.isArray(act.apply) ? act.apply : [
493
+ act.apply
494
+ ]);
495
+ };
496
+ /**
497
+ * An app's declared commands as TOOLS a graph can run itself (T-37605): the
498
+ * declaration from the manifest, and a `run` that fills the template and hands
499
+ * back what it made. @yaks/tools does the rest — it writes the call, lands the
500
+ * answer beside it, and a call wearing a wake waits for its firing first.
501
+ *
502
+ * A tool ANSWERS bundles and never applies them: an `apply` template is the
503
+ * bundles it means, landed by the runner as the caller. A `query` template
504
+ * reads rows the runner keeps its hands off. A `worker` calls the app's own
505
+ * code through the host's scoped dispatch door.
506
+ *
507
+ * `invoke` also fills commands for a page or connector. Their hosts supply
508
+ * the acts that reach the store or the app's worker.
509
+ */ export let commands = (said, worker)=>Object.entries(said).map(([name, def])=>({
510
+ name,
511
+ description: def.description,
512
+ revision: def.revision,
513
+ inputSchema: schemaOf(def, {
514
+ short: true
515
+ }),
516
+ ...def.positional ? {
517
+ positional: def.positional
518
+ } : {},
519
+ readOnly: def.query != null || def.readOnly === true,
520
+ run: (call, graph)=>invoke(def, argsOf(call), {
521
+ worker: (path, args)=>{
522
+ if (!worker) throw new Error('app worker is unavailable');
523
+ return worker(path, args, call);
524
+ },
525
+ query: (line)=>graph.read(line),
526
+ apply: (bundles)=>bundles
527
+ })
528
+ }));
529
+ //# sourceMappingURL=declared.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"declared.js","sources":["./declared.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,6EAA6E;AAC7E,gFAAgF;AAChF,yEAAyE;AACzE,2EAA2E;AAC3E,wEAAwE;AACxE,2EAA2E;AAC3E,+EAA+E;AAC/E,eAAe;AACf,EAAE;AACF,iBAAiB;AACjB,qBAAqB;AACrB,wBAAwB;AACxB,+DAA+D;AAC/D,kDAAkD;AAClD,sDAAsD;AACtD,iCAAiC;AACjC,kDAAkD;AAClD,sEAAsE;AACtE,yBAAyB;AACzB,wBAAwB;AACxB,8CAA8C;AAC9C,sDAAsD;AACtD,oDAAoD;AACpD,EAAE;AACF,0EAA0E;AAC1E,6EAA6E;AAC7E,+EAA+E;AAC/E,8EAA8E;AAC9E,+EAA+E;AAC/E,4EAA4E;AAC5E,8EAA8E;AAC9E,+EAA+E;AAC/E,yEAAyE;AACzE,6EAA6E;AAC7E,6EAA6E;AAC7E,EAAE;AACF,4EAA4E;AAC5E,0EAA0E;AAC1E,QAAQ;AACR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