@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.
- package/LICENSE +202 -0
- package/README.md +870 -2
- package/_dist/access.d.ts +3 -0
- package/_dist/access.d.ts.map +1 -0
- package/_dist/app-view.d.ts +27 -0
- package/_dist/app-view.d.ts.map +1 -0
- package/_dist/args.d.ts +23 -0
- package/_dist/args.d.ts.map +1 -0
- package/_dist/check.d.ts +32 -0
- package/_dist/check.d.ts.map +1 -0
- package/_dist/declared.d.ts +66 -0
- package/_dist/declared.d.ts.map +1 -0
- package/_dist/display.d.ts +2 -0
- package/_dist/display.d.ts.map +1 -0
- package/_dist/errors.d.ts +46 -0
- package/_dist/errors.d.ts.map +1 -0
- package/_dist/fault.d.ts +4 -0
- package/_dist/fault.d.ts.map +1 -0
- package/_dist/invoke.d.ts +13 -0
- package/_dist/invoke.d.ts.map +1 -0
- package/_dist/mod.d.ts +31 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/outlast.d.ts +89 -0
- package/_dist/outlast.d.ts.map +1 -0
- package/_dist/persist.d.ts +5 -0
- package/_dist/persist.d.ts.map +1 -0
- package/_dist/read.d.ts +4 -0
- package/_dist/read.d.ts.map +1 -0
- package/_dist/readable.d.ts +59 -0
- package/_dist/readable.d.ts.map +1 -0
- package/_dist/registry.d.ts +53 -0
- package/_dist/registry.d.ts.map +1 -0
- package/_dist/result.d.ts +154 -0
- package/_dist/result.d.ts.map +1 -0
- package/_dist/routes.d.ts +11 -0
- package/_dist/routes.d.ts.map +1 -0
- package/_dist/runner.d.ts +161 -0
- package/_dist/runner.d.ts.map +1 -0
- package/_dist/said.d.ts +4 -0
- package/_dist/said.d.ts.map +1 -0
- package/_dist/schema.d.ts +76 -0
- package/_dist/schema.d.ts.map +1 -0
- package/_dist/state.d.ts +9 -0
- package/_dist/state.d.ts.map +1 -0
- package/_dist/title.d.ts +20 -0
- package/_dist/title.d.ts.map +1 -0
- package/_dist/value.d.ts +43 -0
- package/_dist/value.d.ts.map +1 -0
- package/_dist/views.d.ts +5 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +9 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/watch.d.ts +28 -0
- package/_dist/watch.d.ts.map +1 -0
- package/access.js +3 -0
- package/access.js.map +1 -0
- package/access.ts +11 -0
- package/app-view.js +26 -0
- package/app-view.js.map +1 -0
- package/app-view.ts +41 -0
- package/args.js +136 -0
- package/args.js.map +1 -0
- package/args.ts +187 -0
- package/body.js +14 -0
- package/body.js.map +1 -0
- package/body.ts +15 -0
- package/check.js +67 -0
- package/check.js.map +1 -0
- package/check.ts +92 -0
- package/declared.js +529 -0
- package/declared.js.map +1 -0
- package/declared.ts +719 -0
- package/deno.json +1 -0
- package/display.js +21 -0
- package/display.js.map +1 -0
- package/display.ts +39 -0
- package/errors.js +99 -0
- package/errors.js.map +1 -0
- package/errors.ts +127 -0
- package/fault.js +17 -0
- package/fault.js.map +1 -0
- package/fault.ts +24 -0
- package/invoke.js +207 -0
- package/invoke.js.map +1 -0
- package/invoke.ts +210 -0
- package/mod.js +31 -0
- package/mod.js.map +1 -0
- package/mod.ts +78 -0
- package/outlast.js +416 -0
- package/outlast.js.map +1 -0
- package/outlast.ts +551 -0
- package/package.json +109 -4
- package/persist.js +18 -0
- package/persist.js.map +1 -0
- package/persist.ts +26 -0
- package/read.js +50 -0
- package/read.js.map +1 -0
- package/read.ts +58 -0
- package/readable.js +118 -0
- package/readable.js.map +1 -0
- package/readable.ts +134 -0
- package/registry.js +162 -0
- package/registry.js.map +1 -0
- package/registry.ts +207 -0
- package/result.js +497 -0
- package/result.js.map +1 -0
- package/result.ts +540 -0
- package/routes.js +46 -0
- package/routes.js.map +1 -0
- package/routes.ts +50 -0
- package/runner.js +748 -0
- package/runner.js.map +1 -0
- package/runner.ts +910 -0
- package/said.js +15 -0
- package/said.js.map +1 -0
- package/said.ts +12 -0
- package/schema.js +137 -0
- package/schema.js.map +1 -0
- package/schema.ts +134 -0
- package/state.js +69 -0
- package/state.js.map +1 -0
- package/state.ts +108 -0
- package/title.js +222 -0
- package/title.js.map +1 -0
- package/title.ts +257 -0
- package/value.js +57 -0
- package/value.js.map +1 -0
- package/value.ts +74 -0
- package/views.js +131 -0
- package/views.js.map +1 -0
- package/views.ts +125 -0
- package/vocab.js +22 -0
- package/vocab.js.map +1 -0
- package/vocab.json +268 -0
- package/vocab.ts +22 -0
- package/watch.js +64 -0
- package/watch.js.map +1 -0
- package/watch.ts +74 -0
package/declared.ts
ADDED
|
@@ -0,0 +1,719 @@
|
|
|
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 type { Bundle, Graph, NamedTool } from 'jsr:@yaks/graph@0.2.36'
|
|
60
|
+
import { type Line, listed, whole } from '@yaks/query'
|
|
61
|
+
import { argsOf } from '@yaks/graph/call'
|
|
62
|
+
import {
|
|
63
|
+
publicToolSchema,
|
|
64
|
+
toolCheck,
|
|
65
|
+
type ToolDefinition,
|
|
66
|
+
toolGrammar,
|
|
67
|
+
} from '@yaks/vocab/tools'
|
|
68
|
+
import { CallError } from './args.js'
|
|
69
|
+
import type { Floor } from './access.js'
|
|
70
|
+
export { mayCall } from './access.js'
|
|
71
|
+
|
|
72
|
+
/** One argument's JSON Schema. */
|
|
73
|
+
export type Arg = Record<string, unknown>
|
|
74
|
+
|
|
75
|
+
// One declared tool, as its store keeps it. `apply` and `query` are the two
|
|
76
|
+
// acts; an entry names exactly one.
|
|
77
|
+
//
|
|
78
|
+
// `drop` is the one field a manifest cannot spell (see KEYS): it belongs to the
|
|
79
|
+
// tools a KIND is worth (workers/yak/kinds.ts), whose templates nobody wrote.
|
|
80
|
+
// It names the components that GO when the caller named none of their
|
|
81
|
+
// columns, since an empty component is a bare write and a bare `alias` is half
|
|
82
|
+
// a sentence, while an empty `recipe` is still what makes the row a recipe.
|
|
83
|
+
export type ToolDef = {
|
|
84
|
+
description: string
|
|
85
|
+
revision?: string
|
|
86
|
+
/** Offer this command as an app service, with its pricing terms. */
|
|
87
|
+
service?: { pricing: string }
|
|
88
|
+
input: Record<string, Arg>
|
|
89
|
+
required?: string[]
|
|
90
|
+
/** The declared command-line grammar; absent means flags only. */
|
|
91
|
+
positional?: ToolDefinition['positional']
|
|
92
|
+
/** Least caller authority for page and connector commands. */
|
|
93
|
+
floor?: Floor
|
|
94
|
+
/** Internal commands remain callable by scheduled graph work. */
|
|
95
|
+
discoverable?: boolean
|
|
96
|
+
/** A worker command that promises to leave the store unchanged. */
|
|
97
|
+
readOnly?: boolean
|
|
98
|
+
drop?: string[]
|
|
99
|
+
apply?: unknown
|
|
100
|
+
query?: string
|
|
101
|
+
worker?: string
|
|
102
|
+
view?: string
|
|
103
|
+
/** offered to the app's own models as a tool */
|
|
104
|
+
model?: boolean
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export type Tools = Record<string, ToolDef>
|
|
108
|
+
|
|
109
|
+
/** The declared caller floor; store and worker doors still check each act. */
|
|
110
|
+
/** Where a call runs, as the variables it binds that no caller sends:
|
|
111
|
+
* `session`, the transcript a model's turn is in. */
|
|
112
|
+
export type Context = { session?: string }
|
|
113
|
+
|
|
114
|
+
// The variables a model's call binds from where it runs.
|
|
115
|
+
let CONTEXT = ['session']
|
|
116
|
+
|
|
117
|
+
export let TOOLS_EXAMPLE = '{"$defs": {"log_run": {"tool": true, ' +
|
|
118
|
+
'"description": "Log a run", "input": {"miles": {"type": "number"}}, ' +
|
|
119
|
+
'"required": ["miles"], ' +
|
|
120
|
+
'"apply": {"entity": {"eid": "$run"}, "run": {"miles": "$miles"}}}}}'
|
|
121
|
+
|
|
122
|
+
// The keys an entry may carry. Unknown ones are refused rather than ignored,
|
|
123
|
+
// so a misspelling is a sentence at deploy and not a tool that quietly does
|
|
124
|
+
// half of what was meant.
|
|
125
|
+
let KEYS = [
|
|
126
|
+
'tool',
|
|
127
|
+
'description',
|
|
128
|
+
'title',
|
|
129
|
+
'revision',
|
|
130
|
+
'service',
|
|
131
|
+
'input',
|
|
132
|
+
'required',
|
|
133
|
+
'positional',
|
|
134
|
+
'apply',
|
|
135
|
+
'query',
|
|
136
|
+
'worker',
|
|
137
|
+
'view',
|
|
138
|
+
'model',
|
|
139
|
+
'floor',
|
|
140
|
+
'discoverable',
|
|
141
|
+
'readOnly',
|
|
142
|
+
]
|
|
143
|
+
|
|
144
|
+
// A `view` names a page in the app's OWN files (T-32687) — a relative path
|
|
145
|
+
// under the app's root, which the MCP door serves as `ui://<space>/<app>/
|
|
146
|
+
// <file>` for the host to render the answer in. HTML because that is the one
|
|
147
|
+
// type the MCP Apps spec renders; relative because the app's files are the
|
|
148
|
+
// only place a view may come from, and a path that climbs out of them would
|
|
149
|
+
// be a door onto somebody else's app.
|
|
150
|
+
let VIEW = /^(?!\/)(?!.*(?:^|\/)\.\.(?:\/|$))[\w./-]+\.html$/
|
|
151
|
+
|
|
152
|
+
// A command's name and an input's name: the same shape a tool name takes, so
|
|
153
|
+
// a command reads as a verb wherever it is said — `command` takes it whole,
|
|
154
|
+
// beside the app it belongs to.
|
|
155
|
+
let NAME = /^[a-z][a-z0-9_]{0,39}$/
|
|
156
|
+
|
|
157
|
+
// A variable, and the whole of a string that is nothing but one — the
|
|
158
|
+
// difference between a value passed through with its type and a value spliced
|
|
159
|
+
// into text. `$$` is a literal dollar and never a variable.
|
|
160
|
+
let VAR = /\$\$|\$([a-z][a-z0-9_]*)/g
|
|
161
|
+
let ONLY = /^\$([a-z][a-z0-9_]*)$/
|
|
162
|
+
|
|
163
|
+
let object = (v: unknown): v is Record<string, unknown> =>
|
|
164
|
+
!!v && typeof v == 'object' && !Array.isArray(v)
|
|
165
|
+
|
|
166
|
+
// The form before this one: `{{arg}}` was a hole where `$arg` is now a
|
|
167
|
+
// variable. A deploy that still writes one is refused, below, in the sentence
|
|
168
|
+
// that says what to write instead.
|
|
169
|
+
let HOLE = /\{\{[a-z][a-z0-9_]*\}\}/
|
|
170
|
+
|
|
171
|
+
// The wire's own keys beside the components: what an entity bundle may say
|
|
172
|
+
// that is not a component name (@yaks/graph `Bundle`).
|
|
173
|
+
let WIRE = ['entity', 'edges', 'tombstone', 'was']
|
|
174
|
+
|
|
175
|
+
// Every variable in a template, wherever the strings are.
|
|
176
|
+
let vars = (v: unknown, found: Set<string> = new Set()): Set<string> => {
|
|
177
|
+
if (typeof v == 'string') {
|
|
178
|
+
for (let m of v.matchAll(VAR)) if (m[1]) found.add(m[1])
|
|
179
|
+
} else if (Array.isArray(v)) { for (let one of v) vars(one, found) }
|
|
180
|
+
else if (object(v)) { for (let one of Object.values(v)) vars(one, found) }
|
|
181
|
+
return found
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// The variables this template MINTS: every `$name` standing alone as an
|
|
185
|
+
// entity's eid. Those are the aliases the store gives real ids to, so a
|
|
186
|
+
// `$name` pointing at one is a join and not a missing argument.
|
|
187
|
+
let minted = (v: unknown, found: Set<string> = new Set()): Set<string> => {
|
|
188
|
+
if (Array.isArray(v)) { for (let one of v) minted(one, found) }
|
|
189
|
+
else if (object(v)) {
|
|
190
|
+
let eid = object(v.entity) ? v.entity.eid : undefined
|
|
191
|
+
let only = typeof eid == 'string' ? ONLY.exec(eid) : null
|
|
192
|
+
if (only) found.add(only[1])
|
|
193
|
+
for (let one of Object.values(v)) minted(one, found)
|
|
194
|
+
}
|
|
195
|
+
return found
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// The components an `apply` template names, so a deploy can say which of them
|
|
199
|
+
// nobody declared. A bundle is an object (or a list of them) whose keys are
|
|
200
|
+
// the wire's own words and component names.
|
|
201
|
+
let named = (v: unknown, found: Set<string> = new Set()): Set<string> => {
|
|
202
|
+
if (Array.isArray(v)) { for (let one of v) named(one, found) }
|
|
203
|
+
else if (object(v)) {
|
|
204
|
+
for (let key of Object.keys(v)) if (!WIRE.includes(key)) found.add(key)
|
|
205
|
+
}
|
|
206
|
+
return found
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// The tool entries of a manifest: every `$defs` entry marked `"tool": true`.
|
|
210
|
+
// The rest of the document is its components (vocab.ts `appDoc`), and neither
|
|
211
|
+
// reading has to know about the other.
|
|
212
|
+
let entriesOf = (held: unknown): [string, Arg][] =>
|
|
213
|
+
object(held) && object(held.$defs)
|
|
214
|
+
? Object.entries(held.$defs).filter((e): e is [string, Arg] =>
|
|
215
|
+
object(e[1]) && e[1].tool === true
|
|
216
|
+
)
|
|
217
|
+
: []
|
|
218
|
+
|
|
219
|
+
// The manifest as written, its tools checked whole: every problem in one
|
|
220
|
+
// sentence, the way the components are refused (T-32628), because an agent
|
|
221
|
+
// that fixes one problem per deploy stops after the second. `words` are the
|
|
222
|
+
// components the app's store knows, which are the only ones a template may
|
|
223
|
+
// write. `file` is what the sentence calls the manifest.
|
|
224
|
+
export let parseTools = (
|
|
225
|
+
source: unknown,
|
|
226
|
+
words: string[] = [],
|
|
227
|
+
file = 'vocab.json',
|
|
228
|
+
): Tools => {
|
|
229
|
+
if (typeof source == 'string') {
|
|
230
|
+
if (!source.trim()) return {}
|
|
231
|
+
// JSON, and only JSON — the YAML door is not in this module's graph: a
|
|
232
|
+
// caller holding a `.yml` reads it first (tools.ts `released`).
|
|
233
|
+
try {
|
|
234
|
+
source = JSON.parse(source)
|
|
235
|
+
} catch {
|
|
236
|
+
throw new CallError(
|
|
237
|
+
'arguments',
|
|
238
|
+
`${file} is not JSON — ${TOOLS_EXAMPLE}`,
|
|
239
|
+
)
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
let wrong: string[] = []
|
|
243
|
+
let out: Tools = {}
|
|
244
|
+
for (let [name, entry] of entriesOf(source)) {
|
|
245
|
+
if (!NAME.test(name)) {
|
|
246
|
+
wrong.push(`${JSON.stringify(name)} is not a tool name (a-z, 0-9, _)`)
|
|
247
|
+
continue
|
|
248
|
+
}
|
|
249
|
+
let alien = Object.keys(entry).filter((k) => !KEYS.includes(k))
|
|
250
|
+
if (alien.length) {
|
|
251
|
+
wrong.push(
|
|
252
|
+
`${name}: ${alien.join(', ')} — a tool says ${KEYS.join(', ')}`,
|
|
253
|
+
)
|
|
254
|
+
}
|
|
255
|
+
let description = entry.description ?? entry.title
|
|
256
|
+
if (
|
|
257
|
+
entry.title !== undefined &&
|
|
258
|
+
(typeof entry.title != 'string' || !entry.title.trim())
|
|
259
|
+
) {
|
|
260
|
+
wrong.push(
|
|
261
|
+
`${name}.title must be text — use description to say what the tool does`,
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
if (typeof description != 'string' || !description) {
|
|
265
|
+
wrong.push(`${name}.description says what the tool does, in a sentence`)
|
|
266
|
+
}
|
|
267
|
+
let input: Record<string, Arg> = {}
|
|
268
|
+
if (entry.input != null) {
|
|
269
|
+
if (!object(entry.input)) {
|
|
270
|
+
wrong.push(
|
|
271
|
+
`${name}.input is an object of arguments — ${TOOLS_EXAMPLE}`,
|
|
272
|
+
)
|
|
273
|
+
} else {
|
|
274
|
+
for (let [arg, schema] of Object.entries(entry.input)) {
|
|
275
|
+
if (!NAME.test(arg)) {
|
|
276
|
+
wrong.push(
|
|
277
|
+
`${name}.input: ${JSON.stringify(arg)} is not an ` +
|
|
278
|
+
'argument name (a-z, 0-9, _)',
|
|
279
|
+
)
|
|
280
|
+
} else if (!object(schema)) {
|
|
281
|
+
wrong.push(
|
|
282
|
+
`${name}.input.${arg} is ${JSON.stringify(schema)} — an ` +
|
|
283
|
+
'argument is a JSON Schema, like {"type": "number"}',
|
|
284
|
+
)
|
|
285
|
+
} else input[arg] = schema
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
let required: string[] = []
|
|
290
|
+
if (entry.required != null) {
|
|
291
|
+
if (
|
|
292
|
+
!Array.isArray(entry.required) ||
|
|
293
|
+
!entry.required.every((r) => typeof r == 'string')
|
|
294
|
+
) {
|
|
295
|
+
wrong.push(`${name}.required is a list of its input's names`)
|
|
296
|
+
} else {
|
|
297
|
+
for (let arg of entry.required as string[]) {
|
|
298
|
+
if (arg in input) required.push(arg)
|
|
299
|
+
else wrong.push(`${name}.required: ${arg} is no input of ${name}`)
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
let positional: ToolDef['positional']
|
|
304
|
+
try {
|
|
305
|
+
positional = toolGrammar(input, entry.positional)
|
|
306
|
+
toolCheck({ type: 'object', properties: input })
|
|
307
|
+
} catch (error) {
|
|
308
|
+
wrong.push(`${name}.grammar: ${(error as Error).message}`)
|
|
309
|
+
}
|
|
310
|
+
if (
|
|
311
|
+
HOLE.test(
|
|
312
|
+
JSON.stringify(entry.apply ?? entry.query ?? entry.worker ?? ''),
|
|
313
|
+
)
|
|
314
|
+
) {
|
|
315
|
+
wrong.push(
|
|
316
|
+
`${name}: {{arg}} is not a hole any more — write $arg, the same ` +
|
|
317
|
+
'variable the wire and the query grammar already speak',
|
|
318
|
+
)
|
|
319
|
+
}
|
|
320
|
+
let acts = ['apply', 'query', 'worker'].filter((k) => entry[k] != null)
|
|
321
|
+
if (acts.length != 1) {
|
|
322
|
+
wrong.push(
|
|
323
|
+
`${name} does one thing: apply (a bundle to write), query (a ` +
|
|
324
|
+
'filter line to read), or worker (a path in its server code)',
|
|
325
|
+
)
|
|
326
|
+
}
|
|
327
|
+
if (entry.view != null) {
|
|
328
|
+
if (typeof entry.view != 'string' || !VIEW.test(entry.view)) {
|
|
329
|
+
wrong.push(
|
|
330
|
+
`${name}.view is a page in this app's files, like ` +
|
|
331
|
+
'"leaderboard.html"',
|
|
332
|
+
)
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if (entry.model != null && typeof entry.model != 'boolean') {
|
|
336
|
+
wrong.push(`${name}.model is true, to offer it to the app's models`)
|
|
337
|
+
}
|
|
338
|
+
if (
|
|
339
|
+
entry.service != null &&
|
|
340
|
+
(!object(entry.service) || typeof entry.service.pricing != 'string' ||
|
|
341
|
+
!entry.service.pricing.trim())
|
|
342
|
+
) {
|
|
343
|
+
wrong.push(
|
|
344
|
+
`${name}.service names its pricing terms as {pricing: string}`,
|
|
345
|
+
)
|
|
346
|
+
}
|
|
347
|
+
if (entry.service != null && entry.worker == null) {
|
|
348
|
+
wrong.push(
|
|
349
|
+
`${name}.service requires a worker command to authorize customer data`,
|
|
350
|
+
)
|
|
351
|
+
}
|
|
352
|
+
if (entry.revision != null && typeof entry.revision != 'string') {
|
|
353
|
+
wrong.push(`${name}.revision is a source version string`)
|
|
354
|
+
}
|
|
355
|
+
if (
|
|
356
|
+
entry.floor != null && entry.floor != 'person' &&
|
|
357
|
+
entry.floor != 'editor' && entry.floor != 'owner'
|
|
358
|
+
) {
|
|
359
|
+
wrong.push(`${name}.floor is person, editor, or owner`)
|
|
360
|
+
}
|
|
361
|
+
if (
|
|
362
|
+
entry.discoverable != null &&
|
|
363
|
+
typeof entry.discoverable != 'boolean'
|
|
364
|
+
) {
|
|
365
|
+
wrong.push(`${name}.discoverable is false to hide an internal command`)
|
|
366
|
+
}
|
|
367
|
+
if (entry.readOnly != null && entry.readOnly !== true) {
|
|
368
|
+
wrong.push(`${name}.readOnly is true for a worker that only reads`)
|
|
369
|
+
}
|
|
370
|
+
if (entry.readOnly === true && entry.worker == null) {
|
|
371
|
+
wrong.push(`${name}.readOnly is only for a worker command`)
|
|
372
|
+
}
|
|
373
|
+
if (entry.query != null && typeof entry.query != 'string') {
|
|
374
|
+
wrong.push(`${name}.query is a filter line, like ".run"`)
|
|
375
|
+
}
|
|
376
|
+
if (
|
|
377
|
+
entry.worker != null &&
|
|
378
|
+
(typeof entry.worker != 'string' ||
|
|
379
|
+
!/^\/[\w/-]+$/.test(entry.worker) || entry.worker.includes('..'))
|
|
380
|
+
) {
|
|
381
|
+
wrong.push(
|
|
382
|
+
`${name}.worker is an absolute path in this app, like "/tick"`,
|
|
383
|
+
)
|
|
384
|
+
}
|
|
385
|
+
if (
|
|
386
|
+
entry.apply != null && !object(entry.apply) && !Array.isArray(entry.apply)
|
|
387
|
+
) {
|
|
388
|
+
wrong.push(`${name}.apply is an entity bundle, or a list of them`)
|
|
389
|
+
}
|
|
390
|
+
// A variable that binds to nothing can never be filled, and a component
|
|
391
|
+
// nobody declared can never be written — both are the deploy's to catch,
|
|
392
|
+
// since the alternative is an agent calling the tool and reading `unknown
|
|
393
|
+
// component` from a store it cannot see. A `$name` an entity in this same
|
|
394
|
+
// template is minted at is neither: it is the join it looks like.
|
|
395
|
+
let mints = entry.apply == null ? new Set<string>() : minted(entry.apply)
|
|
396
|
+
for (let held of vars(entry.apply ?? entry.query ?? entry.worker)) {
|
|
397
|
+
if (held in input || mints.has(held)) continue
|
|
398
|
+
if (CONTEXT.includes(held)) {
|
|
399
|
+
if (entry.model !== true) {
|
|
400
|
+
wrong.push(
|
|
401
|
+
`${name}: $${held} is the transcript a model's turn runs in — ` +
|
|
402
|
+
`mark ${name} "model": true, or declare it in ${name}.input`,
|
|
403
|
+
)
|
|
404
|
+
}
|
|
405
|
+
continue
|
|
406
|
+
}
|
|
407
|
+
wrong.push(
|
|
408
|
+
`${name}: $${held} names no input and no entity here — declare it ` +
|
|
409
|
+
`in ${name}.input`,
|
|
410
|
+
)
|
|
411
|
+
}
|
|
412
|
+
for (let comp of named(entry.apply)) {
|
|
413
|
+
if (!words.includes(comp)) {
|
|
414
|
+
wrong.push(
|
|
415
|
+
`${name}.apply: ${comp} is not a component — declare it in ` +
|
|
416
|
+
`${file}, or use one the platform declares`,
|
|
417
|
+
)
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
let floor: Floor | undefined = entry.floor == 'person'
|
|
421
|
+
? 'person'
|
|
422
|
+
: entry.floor == 'editor'
|
|
423
|
+
? 'editor'
|
|
424
|
+
: entry.floor == 'owner'
|
|
425
|
+
? 'owner'
|
|
426
|
+
: undefined
|
|
427
|
+
out[name] = {
|
|
428
|
+
description: String(description ?? ''),
|
|
429
|
+
...(typeof entry.revision == 'string'
|
|
430
|
+
? { revision: entry.revision }
|
|
431
|
+
: {}),
|
|
432
|
+
...(object(entry.service) && typeof entry.service.pricing == 'string'
|
|
433
|
+
? { service: { pricing: entry.service.pricing } }
|
|
434
|
+
: {}),
|
|
435
|
+
input,
|
|
436
|
+
...(required.length ? { required } : {}),
|
|
437
|
+
...(positional ? { positional } : {}),
|
|
438
|
+
...(entry.apply != null ? { apply: entry.apply } : {}),
|
|
439
|
+
...(typeof entry.query == 'string' ? { query: entry.query } : {}),
|
|
440
|
+
...(typeof entry.worker == 'string' ? { worker: entry.worker } : {}),
|
|
441
|
+
...(typeof entry.view == 'string' && VIEW.test(entry.view)
|
|
442
|
+
? { view: entry.view }
|
|
443
|
+
: {}),
|
|
444
|
+
...(entry.model === true ? { model: true } : {}),
|
|
445
|
+
...(floor ? { floor } : {}),
|
|
446
|
+
...(entry.discoverable === false ? { discoverable: false } : {}),
|
|
447
|
+
...(entry.readOnly === true && typeof entry.worker == 'string'
|
|
448
|
+
? { readOnly: true }
|
|
449
|
+
: {}),
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
if (wrong.length) {
|
|
453
|
+
throw new CallError('arguments', `${file}: ${wrong.join('; ')}`)
|
|
454
|
+
}
|
|
455
|
+
return out
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// The pages a manifest's views name, read straight off the source so a deploy
|
|
459
|
+
// can check them against the app's own files before anything is planted. A
|
|
460
|
+
// manifest that will not parse yields none: {@link parseTools} says why.
|
|
461
|
+
export let viewsOf = (source: unknown): string[] => {
|
|
462
|
+
let seen = new Set<string>()
|
|
463
|
+
try {
|
|
464
|
+
// A manifest as written is a string here only when it is JSON — a `.yml`
|
|
465
|
+
// is read by whoever holds it and handed on as the value (see parseTools).
|
|
466
|
+
let held = typeof source == 'string' ? JSON.parse(source) : source
|
|
467
|
+
for (let [, entry] of entriesOf(held)) {
|
|
468
|
+
if (typeof entry.view == 'string') seen.add(entry.view)
|
|
469
|
+
}
|
|
470
|
+
} catch { /* parseTools says why */ }
|
|
471
|
+
return [...seen]
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// The arguments a caller must send; every other one may be left out.
|
|
475
|
+
let needed = (tool: ToolDef) => tool.required ?? []
|
|
476
|
+
|
|
477
|
+
// The tool's arguments as one JSON Schema object, which is what a host shows
|
|
478
|
+
// the model and what a store's runner checks a call against (@yaks/tools).
|
|
479
|
+
export let schemaOf = (
|
|
480
|
+
tool: ToolDef,
|
|
481
|
+
options: { short?: boolean } = {},
|
|
482
|
+
): { type: 'object'; properties: Record<string, Arg>; required: string[] } => {
|
|
483
|
+
let schema = {
|
|
484
|
+
type: 'object' as const,
|
|
485
|
+
properties: tool.input,
|
|
486
|
+
required: needed(tool),
|
|
487
|
+
}
|
|
488
|
+
return (options.short ? schema : publicToolSchema(schema)) as typeof schema
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// One argument, as its schema's type says to read it. A model sends what it
|
|
492
|
+
// sends — a number as a string, `"true"` for a flag — so the type it was
|
|
493
|
+
// declared under is what it becomes, and a value that cannot become that is
|
|
494
|
+
// refused by name. A schema naming no type takes the value as it came.
|
|
495
|
+
let typed = (arg: string, schema: Arg, v: unknown) => {
|
|
496
|
+
let no = (why: string) => new CallError('arguments', `${arg} ${why}`)
|
|
497
|
+
if (v == null) throw no('is required')
|
|
498
|
+
if (schema.type == 'number' || schema.type == 'integer') {
|
|
499
|
+
let n = typeof v == 'number' ? v : Number(String(v))
|
|
500
|
+
if (!Number.isFinite(n)) throw no('is a number')
|
|
501
|
+
return n
|
|
502
|
+
}
|
|
503
|
+
if (schema.type == 'boolean') {
|
|
504
|
+
if (typeof v == 'boolean') return v
|
|
505
|
+
if (v === 'true' || v === 'false') return v == 'true'
|
|
506
|
+
throw no('is true or false')
|
|
507
|
+
}
|
|
508
|
+
if (schema.type != 'string') return v
|
|
509
|
+
if (typeof v == 'object') throw no('is text')
|
|
510
|
+
return String(v)
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// The call's arguments, read under the declared inputs. An argument nobody
|
|
514
|
+
// declared is dropped: it can fill no hole, and refusing it would only teach
|
|
515
|
+
// the model to guess again. One the caller may leave out and did is dropped
|
|
516
|
+
// too, and what is missing from here is what makes a hole absent below.
|
|
517
|
+
let args = (tool: ToolDef, sent: Record<string, unknown>) =>
|
|
518
|
+
Object.fromEntries(
|
|
519
|
+
Object.entries(tool.input)
|
|
520
|
+
.filter(([arg]) => sent[arg] != null || needed(tool).includes(arg))
|
|
521
|
+
.map(([arg, schema]) => [arg, typed(arg, schema, sent[arg])]),
|
|
522
|
+
)
|
|
523
|
+
|
|
524
|
+
// A variable the caller left empty, travelling as a value. It takes the key
|
|
525
|
+
// that held it with it: a column nobody named is a column nobody writes, never
|
|
526
|
+
// the word `undefined` in the row.
|
|
527
|
+
let ABSENT = Symbol('absent')
|
|
528
|
+
|
|
529
|
+
// An argument the caller left out. A variable that is not an argument at all
|
|
530
|
+
// is not a gap — it is an alias, and it travels on untouched.
|
|
531
|
+
let gaps = (s: string, tool: ToolDef, vals: Record<string, unknown>) =>
|
|
532
|
+
[...s.matchAll(VAR)].some((m) =>
|
|
533
|
+
m[1] && m[1] in tool.input && !(m[1] in vals)
|
|
534
|
+
)
|
|
535
|
+
|
|
536
|
+
// One template string, bound. A string that is NOTHING but a bound variable
|
|
537
|
+
// becomes the value itself with its type — `"$miles"` writes the number 5, not
|
|
538
|
+
// "5" — and a variable inside a sentence is spliced in as text. `$$` is a
|
|
539
|
+
// dollar sign, and an unbound `$name` is the alias it was.
|
|
540
|
+
let fill = (
|
|
541
|
+
s: string,
|
|
542
|
+
tool: ToolDef,
|
|
543
|
+
vals: Record<string, unknown>,
|
|
544
|
+
encode: (v: unknown) => string,
|
|
545
|
+
) => {
|
|
546
|
+
if (gaps(s, tool, vals)) return ABSENT
|
|
547
|
+
let only = ONLY.exec(s)
|
|
548
|
+
if (only && only[1] in vals) return vals[only[1]]
|
|
549
|
+
return s.replace(
|
|
550
|
+
VAR,
|
|
551
|
+
(as, arg) => !arg ? '$' : arg in vals ? encode(vals[arg]) : as,
|
|
552
|
+
)
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
let filling = (
|
|
556
|
+
v: unknown,
|
|
557
|
+
tool: ToolDef,
|
|
558
|
+
vals: Record<string, unknown>,
|
|
559
|
+
encode: (v: unknown) => string,
|
|
560
|
+
): unknown =>
|
|
561
|
+
typeof v == 'string'
|
|
562
|
+
? fill(v, tool, vals, encode)
|
|
563
|
+
: Array.isArray(v)
|
|
564
|
+
? v.map((one) => filling(one, tool, vals, encode))
|
|
565
|
+
.filter((one) => one !== ABSENT)
|
|
566
|
+
: object(v)
|
|
567
|
+
? Object.fromEntries(
|
|
568
|
+
Object.entries(v)
|
|
569
|
+
.map(([k, one]) => [k, filling(one, tool, vals, encode)])
|
|
570
|
+
.filter(([, one]) => one !== ABSENT),
|
|
571
|
+
)
|
|
572
|
+
: v
|
|
573
|
+
|
|
574
|
+
// And the components a generated template lets go: one left with no columns is
|
|
575
|
+
// a bare write, which says the thing for `recipe` and is half a sentence for
|
|
576
|
+
// `alias{name}`. Nothing a person wrote names any, so nothing a person wrote
|
|
577
|
+
// loses one.
|
|
578
|
+
let dropped = (v: unknown, drop: string[]): unknown =>
|
|
579
|
+
Array.isArray(v)
|
|
580
|
+
? v.map((one) => dropped(one, drop))
|
|
581
|
+
: object(v)
|
|
582
|
+
? Object.fromEntries(
|
|
583
|
+
Object.entries(v).filter(([k, one]) =>
|
|
584
|
+
!(drop.includes(k) && object(one) && !Object.keys(one).length)
|
|
585
|
+
),
|
|
586
|
+
)
|
|
587
|
+
: v
|
|
588
|
+
|
|
589
|
+
// One `&`-clause of a filter line, or absent when the argument it asks about
|
|
590
|
+
// is. A value is percent-encoded — a filter line is a query string, and a
|
|
591
|
+
// title with an `&` in it would otherwise read as the next filter.
|
|
592
|
+
// A value fills its variable as one operand (@yaks/query `listed`), so an `&`
|
|
593
|
+
// or a `)` in what a caller sent is part of the value, not the line.
|
|
594
|
+
let clause = (s: string, tool: ToolDef, vals: Record<string, unknown>) =>
|
|
595
|
+
gaps(s, tool, vals) ? ABSENT : s.replace(
|
|
596
|
+
VAR,
|
|
597
|
+
(as, arg) => !arg ? '$' : arg in vals ? listed([String(vals[arg])]) : as,
|
|
598
|
+
)
|
|
599
|
+
|
|
600
|
+
// What the call binds from where it runs: each context variable the template
|
|
601
|
+
// names and no input declares, refused when the call runs nowhere that has
|
|
602
|
+
// one.
|
|
603
|
+
let bound = (tool: ToolDef, context: Context) =>
|
|
604
|
+
Object.fromEntries(
|
|
605
|
+
[...vars(tool.apply ?? tool.query ?? tool.worker)]
|
|
606
|
+
.filter((v) => CONTEXT.includes(v) && !(v in tool.input))
|
|
607
|
+
.map((v) => {
|
|
608
|
+
let held = context[v as keyof Context]
|
|
609
|
+
if (held == null) {
|
|
610
|
+
throw new CallError(
|
|
611
|
+
'arguments',
|
|
612
|
+
`$${v} is the transcript a model's turn runs in, and this call ` +
|
|
613
|
+
"runs in none: only the app's own models call this command",
|
|
614
|
+
)
|
|
615
|
+
}
|
|
616
|
+
return [v, held]
|
|
617
|
+
}),
|
|
618
|
+
)
|
|
619
|
+
|
|
620
|
+
// The act this call makes: bundles to write, a filter to read, or a worker
|
|
621
|
+
// path with its typed arguments. An optional query clause drops out when its
|
|
622
|
+
// argument was left out.
|
|
623
|
+
export let filled = (
|
|
624
|
+
tool: ToolDef,
|
|
625
|
+
sent: Record<string, unknown>,
|
|
626
|
+
context: Context = {},
|
|
627
|
+
): {
|
|
628
|
+
apply?: unknown
|
|
629
|
+
query?: Line
|
|
630
|
+
worker?: string
|
|
631
|
+
args?: Record<string, unknown>
|
|
632
|
+
} => {
|
|
633
|
+
let vals = { ...bound(tool, context), ...args(tool, sent) }
|
|
634
|
+
if (tool.worker != null) {
|
|
635
|
+
return { worker: tool.worker, args: vals }
|
|
636
|
+
}
|
|
637
|
+
if (tool.query != null) {
|
|
638
|
+
// A query that is nothing but one variable is a whole filter line passed
|
|
639
|
+
// through, so it keeps its `&`s rather than being read as one clause.
|
|
640
|
+
let only = ONLY.exec(tool.query)
|
|
641
|
+
if (only && only[1] in tool.input) {
|
|
642
|
+
return { query: whole(String(vals[only[1]] ?? '')) }
|
|
643
|
+
}
|
|
644
|
+
// The app's own line, its variables filled as operands.
|
|
645
|
+
return {
|
|
646
|
+
query: whole(
|
|
647
|
+
tool.query.split('&')
|
|
648
|
+
.map((one) => clause(one, tool, vals))
|
|
649
|
+
.filter((one) => one !== ABSENT && one !== '')
|
|
650
|
+
.join('&'),
|
|
651
|
+
),
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
return {
|
|
655
|
+
apply: dropped(
|
|
656
|
+
filling(tool.apply, tool, vals, (v) => String(v)),
|
|
657
|
+
tool.drop ?? [],
|
|
658
|
+
),
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/** Fill one declared command and hand its act to the caller's effect boundary. */
|
|
663
|
+
export let invoke = <T>(
|
|
664
|
+
tool: ToolDef,
|
|
665
|
+
args: Record<string, unknown>,
|
|
666
|
+
acts: {
|
|
667
|
+
apply: (bundles: Bundle[]) => T | Promise<T>
|
|
668
|
+
query: (line: Line) => T | Promise<T>
|
|
669
|
+
worker: (path: string, args: Record<string, unknown>) => T | Promise<T>
|
|
670
|
+
},
|
|
671
|
+
context: Context = {},
|
|
672
|
+
): T | Promise<T> => {
|
|
673
|
+
let act = filled(tool, args, context)
|
|
674
|
+
if (act.worker != null) return acts.worker(act.worker, act.args ?? {})
|
|
675
|
+
if (act.query != null) return acts.query(act.query)
|
|
676
|
+
return acts.apply(
|
|
677
|
+
(Array.isArray(act.apply) ? act.apply : [act.apply]) as Bundle[],
|
|
678
|
+
)
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* An app's declared commands as TOOLS a graph can run itself (T-37605): the
|
|
683
|
+
* declaration from the manifest, and a `run` that fills the template and hands
|
|
684
|
+
* back what it made. @yaks/tools does the rest — it writes the call, lands the
|
|
685
|
+
* answer beside it, and a call wearing a wake waits for its firing first.
|
|
686
|
+
*
|
|
687
|
+
* A tool ANSWERS bundles and never applies them: an `apply` template is the
|
|
688
|
+
* bundles it means, landed by the runner as the caller. A `query` template
|
|
689
|
+
* reads rows the runner keeps its hands off. A `worker` calls the app's own
|
|
690
|
+
* code through the host's scoped dispatch door.
|
|
691
|
+
*
|
|
692
|
+
* `invoke` also fills commands for a page or connector. Their hosts supply
|
|
693
|
+
* the acts that reach the store or the app's worker.
|
|
694
|
+
*/
|
|
695
|
+
export let commands = (
|
|
696
|
+
said: Tools,
|
|
697
|
+
worker?: (
|
|
698
|
+
path: string,
|
|
699
|
+
args: Record<string, unknown>,
|
|
700
|
+
call: Bundle,
|
|
701
|
+
) => Promise<Bundle[]>,
|
|
702
|
+
): NamedTool[] =>
|
|
703
|
+
Object.entries(said).map(([name, def]) => ({
|
|
704
|
+
name,
|
|
705
|
+
description: def.description,
|
|
706
|
+
revision: def.revision,
|
|
707
|
+
inputSchema: schemaOf(def, { short: true }),
|
|
708
|
+
...(def.positional ? { positional: def.positional } : {}),
|
|
709
|
+
readOnly: def.query != null || def.readOnly === true,
|
|
710
|
+
run: (call: Bundle, graph: Graph) =>
|
|
711
|
+
invoke(def, argsOf(call), {
|
|
712
|
+
worker: (path, args) => {
|
|
713
|
+
if (!worker) throw new Error('app worker is unavailable')
|
|
714
|
+
return worker(path, args, call)
|
|
715
|
+
},
|
|
716
|
+
query: (line) => graph.read(line),
|
|
717
|
+
apply: (bundles) => bundles,
|
|
718
|
+
}),
|
|
719
|
+
}))
|