@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/README.md
CHANGED
|
@@ -1,3 +1,871 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @yaks/tools
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Provides a shared tool registry and schemas, executes recorded graph tool calls,
|
|
4
|
+
and keeps their answers, duration, and execution state. It also parses
|
|
5
|
+
app-declared commands and provides their HTTP route and answer rendering.
|
|
6
|
+
|
|
7
|
+
## Tool registry
|
|
8
|
+
|
|
9
|
+
A **registry** is the runtime catalog shared by discovery and execution. A
|
|
10
|
+
**provider** supplies a complete tool snapshot under its own name. Tool names
|
|
11
|
+
and `toolEid(name)` identities stay independent of the provider name.
|
|
12
|
+
`registry.list()`, `get(nameOrEid)`, `search(words)` and `describe(nameOrEid)`
|
|
13
|
+
read the catalog; descriptions include input and output schemas, without the
|
|
14
|
+
implementation. `providers()` groups tools by source for adapters.
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { registry } from '@yaks/tools'
|
|
18
|
+
import { equal } from '@yaks/testing'
|
|
19
|
+
|
|
20
|
+
let catalog = registry([{
|
|
21
|
+
name: 'samples',
|
|
22
|
+
tools: [{ name: 'sample_read', description: 'Read a sample', run: () => [] }],
|
|
23
|
+
}])
|
|
24
|
+
equal(catalog.describe('sample_read')?.provider, 'samples')
|
|
25
|
+
equal(catalog.search('sample').map((t) => t.name), ['sample_read'])
|
|
26
|
+
catalog.replace('samples', [])
|
|
27
|
+
equal(catalog.list(), [])
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`replace(provider, tools)` publishes a complete snapshot; `null` removes the
|
|
31
|
+
provider. Duplicate tool names refuse the entire replacement. `subscribe(fn)`
|
|
32
|
+
notifies consumers after publication and returns an unsubscribe function. These
|
|
33
|
+
catalogs are runtime adapters; registration writes the existing `tool` rows
|
|
34
|
+
through `runner.ensure()`.
|
|
35
|
+
|
|
36
|
+
Both `runner` and `reader` accept a registry in `options.tools`, or an array for
|
|
37
|
+
a single local provider. Their `registry` property exposes the catalog.
|
|
38
|
+
`runner.admit(call)` captures one implementation and returns its `tool`,
|
|
39
|
+
`ensure()`, `call()` and `read()` methods. Replacement changes future calls; an
|
|
40
|
+
admitted call retains its implementation. `ensure()` advertises the current
|
|
41
|
+
catalog declaration at the same tool identity. Direct reads keep no call
|
|
42
|
+
history; recorded calls use the ordinary runner.
|
|
43
|
+
|
|
44
|
+
`@yaks/tools/schema` owns `inputSchemaOf`, `answerSchema` and `answerWith`.
|
|
45
|
+
`inputSchemaOf(tool)` returns its public JSON Schema, or one taking no arguments
|
|
46
|
+
for a tool that declares none. `answerSchema` describes the default structured
|
|
47
|
+
bundle answer, and `answerWith(words)` the same answer for rows that carry words
|
|
48
|
+
of their own beside their components, each word with its schema.
|
|
49
|
+
`@yaks/mcp/tools` re-exports these helpers.
|
|
50
|
+
|
|
51
|
+
## Calls and answers
|
|
52
|
+
|
|
53
|
+
A **call** is an [entity](../graph/README.md#data-model) recording a request to
|
|
54
|
+
run a [tool](../graph/README.md#tools):
|
|
55
|
+
`call: { to: toolEid('person_greet'), args: { name: 'Ada' } }`. A **runner**
|
|
56
|
+
validates calls, invokes its tools, and writes their answers and bookkeeping to
|
|
57
|
+
a [graph](../graph/README.md#data-model) (`Runner`). An **answer** is the array
|
|
58
|
+
of [bundles](../graph/README.md#data-model) a tool returns. **Content** is the
|
|
59
|
+
text attached to an entity, `content: { body: 'hello' }`. **Output** identifies
|
|
60
|
+
what produced an entity, its provider id, and its data:
|
|
61
|
+
`output: { source: '<call eid>', value: { total: 3 } }`. A **result** is the
|
|
62
|
+
entity recording completion, with `result: { call: '<call eid>', ms: 3 }` and
|
|
63
|
+
the answer's text in `content.body`. A **call claim** is the call's
|
|
64
|
+
`execution.by`, identifying the runner holding it. **Execution state** is
|
|
65
|
+
computed from that call claim, an interruption, and the call's explicitly named
|
|
66
|
+
answers: `running`, `done`, `failed`, or `interrupted`.
|
|
67
|
+
|
|
68
|
+
```sh
|
|
69
|
+
deno add jsr:@yaks/tools
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Run a tool
|
|
73
|
+
|
|
74
|
+
A tool returns bundles; the runner applies them as the caller. Load the
|
|
75
|
+
[vocabulary](../vocab/README.md#vocabulary), register the tools with `ensure()`,
|
|
76
|
+
and use `call()` for a direct invocation. `executionComputed` supplies execution
|
|
77
|
+
state to RAM; `executionDerived(vocab)` supplies it to
|
|
78
|
+
[SQLite](../sqlite/README.md).
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { argsOf, graph, type Tool } from '@yaks/graph'
|
|
82
|
+
import { ram } from '@yaks/ram'
|
|
83
|
+
import { loadVocab } from '@yaks/vocab'
|
|
84
|
+
import {
|
|
85
|
+
answerOf,
|
|
86
|
+
executionComputed,
|
|
87
|
+
faulted,
|
|
88
|
+
runner,
|
|
89
|
+
toolEid,
|
|
90
|
+
toolsDoc,
|
|
91
|
+
worded,
|
|
92
|
+
} from '@yaks/tools'
|
|
93
|
+
import { equal } from '@yaks/testing'
|
|
94
|
+
|
|
95
|
+
const greet: Tool = {
|
|
96
|
+
noun: 'person',
|
|
97
|
+
verb: 'greet',
|
|
98
|
+
description: 'Greet a person',
|
|
99
|
+
inputSchema: {
|
|
100
|
+
type: 'object',
|
|
101
|
+
required: ['name'],
|
|
102
|
+
properties: { name: { type: 'string' } },
|
|
103
|
+
},
|
|
104
|
+
run: (call) => [{
|
|
105
|
+
entity: { eid: '$greeting' },
|
|
106
|
+
content: { body: `hello ${argsOf(call).name}` },
|
|
107
|
+
output: { source: call.entity.eid },
|
|
108
|
+
}],
|
|
109
|
+
}
|
|
110
|
+
const vocab = loadVocab([toolsDoc])
|
|
111
|
+
const g = graph({
|
|
112
|
+
vocab,
|
|
113
|
+
storage: ram(vocab, { computed: executionComputed }),
|
|
114
|
+
})
|
|
115
|
+
const r = runner(g, { tools: [greet] })
|
|
116
|
+
await r.ensure(['person_greet'])
|
|
117
|
+
const bundles = await r.call({
|
|
118
|
+
entity: { eid: 'greet-ada' },
|
|
119
|
+
call: { to: toolEid('person_greet'), args: { name: 'Ada' } },
|
|
120
|
+
})
|
|
121
|
+
equal(faulted(bundles, 'greet-ada'), false)
|
|
122
|
+
equal(worded(answerOf(bundles, 'greet-ada')), 'hello Ada')
|
|
123
|
+
equal(
|
|
124
|
+
((await g.get(['greet-ada']))[0].execution as { state: string }).state,
|
|
125
|
+
'done',
|
|
126
|
+
)
|
|
127
|
+
equal((await g.read('.result.call=greet-ada')).length, 1)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
`ensure()` registers all the runner's tools; `ensure(names)` registers only
|
|
131
|
+
those named. `toolEid(name)` derives the eid from `tool.name`, the vocabulary's
|
|
132
|
+
[identity keyword](../vocab/README.md#identity-and-indexes). `toolRow(tool)`
|
|
133
|
+
builds the advertised `tool{name, description, revision}` bundle. `call()`
|
|
134
|
+
writes the call and call claim together before invoking the tool in this
|
|
135
|
+
process. A call whose eid is a [graph alias](../graph/README.md#ids-and-names)
|
|
136
|
+
is assigned a fresh eid first.
|
|
137
|
+
|
|
138
|
+
## Exports
|
|
139
|
+
|
|
140
|
+
| Import | Exports |
|
|
141
|
+
| ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
142
|
+
| `@yaks/tools` | `registry`, `Registry`, `Provider`, `Description`; `runner`, `Runner`, `Opts`, `Reply`, `Invocation`; `toolEid`, `toolRow`, `answerOf`, `worded`, `texts`, `structured`, `valueIn`, `display`, `faulted`, `parsed`; `reconcile`, `CallError`, `UnfinishedCall`, `Interrupted`; `executionState`, `executionComputed`, `executionDerived`; `RULES`, `READY`, `WOKEN`, `WORDS`, `MOST`; `CHECK`, `checks`, `checked`, `ailing`, `Finding`, `Level`; `toolsDoc`, `callDoc`, `toolDoc` |
|
|
143
|
+
| `@yaks/tools/registry` | `registry`, `registryOf`, `Registry`, `Provider`, `Description`: provider catalogs, lookup and discovery |
|
|
144
|
+
| `@yaks/tools/schema` | `inputSchemaOf`, `shapeOf`, `answerSchema`, `answerWith`: shared public schemas |
|
|
145
|
+
| `@yaks/tools/access` | `mayCall`, `Floor` |
|
|
146
|
+
| `@yaks/tools/app-view` | `VIEW_MIME`, `uiMeta`, `Csp`: how an MCP Apps view is served |
|
|
147
|
+
| `@yaks/tools/declared` | `parseTools`, `filled`, `invoke`, `commands`, `schemaOf`, `viewsOf`, `mayCall`, `TOOLS_EXAMPLE`; `Arg`, `ToolDef`, `Tools`, `Context` |
|
|
148
|
+
| `@yaks/tools/routes` | `routes` |
|
|
149
|
+
| `@yaks/tools/vocab` | `toolsDoc`, `callDoc`, `toolDoc`, `docs`, `description`, `derived` |
|
|
150
|
+
| `@yaks/tools/value` | `valueIn` |
|
|
151
|
+
| `@yaks/tools/readable` | `readable`, `drawing`, `Ids` |
|
|
152
|
+
| `@yaks/tools/views` | `views` |
|
|
153
|
+
| `@yaks/tools/watch` | `watched`, `Watch`: the query a reading tool's `watch` declaration answers for a call's arguments, which a subscription to the tool watches live |
|
|
154
|
+
|
|
155
|
+
## Subscribable reading tools
|
|
156
|
+
|
|
157
|
+
A reading tool whose answer is a store query's rows declares that query as
|
|
158
|
+
`watch` in its vocabulary: the query's parts, joined with `&`, each with
|
|
159
|
+
`%{name}` holes the call's arguments fill. A part whose hole is left out is
|
|
160
|
+
dropped, a part given as a list is its alternatives (the first that fills), and
|
|
161
|
+
a hole filled by a list is one copy of its part per item. `watched` renders it
|
|
162
|
+
for a call, and refuses an argument no part names, since the query could not
|
|
163
|
+
follow it. A subscription to the tool (the yaks.app Operator's
|
|
164
|
+
`subscription_set` with `tool` and `args`) watches that query live (@yaks/wake
|
|
165
|
+
`watch`).
|
|
166
|
+
|
|
167
|
+
"task_list": {
|
|
168
|
+
"watch": [".task", ["%{query}", ".task.status=open"], ".limit=%{limit}", "*"]
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
## Stored data
|
|
172
|
+
|
|
173
|
+
`toolsDoc` declares these [components](../graph/README.md#data-model):
|
|
174
|
+
|
|
175
|
+
| Component | Purpose |
|
|
176
|
+
| ------------------------------------- | --------------------------------------------------------------------------------------------------- |
|
|
177
|
+
| `tool{name, description, revision?}` | Identifies a registered tool; its eid is derived from `name`. |
|
|
178
|
+
| `call{to, args, id?, name?, source?}` | Names the tool, arguments, optional provider id, the name its caller wrote, and originating entity. |
|
|
179
|
+
| `execution{by, state}` | Holds the call claim and computed execution state. |
|
|
180
|
+
| `result{call, ms?}` | Records completion and duration when the runner observed the start. |
|
|
181
|
+
| `content{body}` | Carries content. |
|
|
182
|
+
| `output{source?, id?, value?}` | Records output. |
|
|
183
|
+
| `refusal{code}` | A **refusal** records a deliberate rejection with a reason code. |
|
|
184
|
+
| `exception{type?, value?}` | An **exception** keeps what was thrown in an answer or transcript entry. |
|
|
185
|
+
| `finding{level}` | Carries a [check](#health-checks)'s verdict. |
|
|
186
|
+
|
|
187
|
+
`callDoc` omits the `tool` declaration; `toolDoc` contains only that
|
|
188
|
+
declaration. `toolsDoc` contains both and the runner's effect rules. Importing
|
|
189
|
+
declarations from `@yaks/tools/vocab` avoids importing the runner and its input
|
|
190
|
+
validation.
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
import { callDoc, toolDoc, toolsDoc } from '@yaks/tools/vocab'
|
|
194
|
+
import { loadVocab } from '@yaks/vocab'
|
|
195
|
+
import { equal } from '@yaks/testing'
|
|
196
|
+
|
|
197
|
+
const split = loadVocab([callDoc, toolDoc])
|
|
198
|
+
const whole = loadVocab([toolsDoc])
|
|
199
|
+
equal(!!split.comp('call'), true)
|
|
200
|
+
equal(!!split.comp('tool'), true)
|
|
201
|
+
equal(!!whole.comp('result'), true)
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Calls remain recorded if a process stops before answering. The tool's answer and
|
|
205
|
+
runner bookkeeping commit together. A storage refusal marked `retryable: true`
|
|
206
|
+
keeps the completed answer in memory and retries persistence with backoff; it
|
|
207
|
+
does not invoke the tool again. The first refusal is reported through `report`.
|
|
208
|
+
Until persistence succeeds, a process crash can lose that in-memory answer.
|
|
209
|
+
|
|
210
|
+
## Reads, rehearsals, and failures
|
|
211
|
+
|
|
212
|
+
A `readOnly` tool returns bundles without writing them again; the runner still
|
|
213
|
+
writes its result and call claim. A **rehearsal** is a call with `check: true`
|
|
214
|
+
to a tool that writes: every write phase runs, the write rolls back, and the
|
|
215
|
+
proposed answer is returned. The runner still records completion.
|
|
216
|
+
|
|
217
|
+
Throw `CallError(code, message)` for an expected refusal. Other thrown values
|
|
218
|
+
produce an `exception` answer. Each is an entity of its own beside the result,
|
|
219
|
+
unless the runner is built with `onResult`: then a refusal is recorded on the
|
|
220
|
+
result itself, `refusal{code}` beside `result{call}`, as a transcript keeps it
|
|
221
|
+
(@yaks/session), and so is whatever a lone `$said` answer carries beside its
|
|
222
|
+
words and source (`ridden`). `report` receives the original thrown value once,
|
|
223
|
+
with its stack, whether or not recording the answer succeeds. Reporting skips
|
|
224
|
+
known provider transients through `actionable`. Invalid arguments, failed schema
|
|
225
|
+
validation, and refused graph writes also produce failed calls. The runner
|
|
226
|
+
resolves schema-declared [references](../vocab/README.md#routing-and-references)
|
|
227
|
+
through `g.address` before invocation. A tool that writes is refused if such an
|
|
228
|
+
argument names no entity of the declared component; a read-only tool can read
|
|
229
|
+
whatever the id names, including deleted history.
|
|
230
|
+
|
|
231
|
+
```ts
|
|
232
|
+
import { graph, type Tool } from '@yaks/graph'
|
|
233
|
+
import { ram } from '@yaks/ram'
|
|
234
|
+
import { loadVocab } from '@yaks/vocab'
|
|
235
|
+
import {
|
|
236
|
+
answerOf,
|
|
237
|
+
CallError,
|
|
238
|
+
executionComputed,
|
|
239
|
+
faulted,
|
|
240
|
+
runner,
|
|
241
|
+
toolEid,
|
|
242
|
+
toolsDoc,
|
|
243
|
+
} from '@yaks/tools'
|
|
244
|
+
import { equal } from '@yaks/testing'
|
|
245
|
+
|
|
246
|
+
const vocab = loadVocab([toolsDoc])
|
|
247
|
+
const g = graph({
|
|
248
|
+
vocab,
|
|
249
|
+
storage: ram(vocab, { computed: executionComputed }),
|
|
250
|
+
})
|
|
251
|
+
const write: Tool = {
|
|
252
|
+
name: 'write_text',
|
|
253
|
+
description: 'Write text',
|
|
254
|
+
inputSchema: { type: 'object', properties: { check: { type: 'boolean' } } },
|
|
255
|
+
run: (call) => [{
|
|
256
|
+
entity: { eid: 'text' },
|
|
257
|
+
content: { body: 'hello' },
|
|
258
|
+
output: { source: call.entity.eid },
|
|
259
|
+
}],
|
|
260
|
+
}
|
|
261
|
+
const read: Tool = {
|
|
262
|
+
name: 'read_text',
|
|
263
|
+
description: 'Read text',
|
|
264
|
+
readOnly: true,
|
|
265
|
+
run: () => [{ entity: { eid: 'read-only' }, content: { body: 'hello' } }],
|
|
266
|
+
}
|
|
267
|
+
const refuse: Tool = {
|
|
268
|
+
name: 'refuse',
|
|
269
|
+
description: 'Refuse a call',
|
|
270
|
+
run: () => {
|
|
271
|
+
throw new CallError('access', 'Permission required')
|
|
272
|
+
},
|
|
273
|
+
}
|
|
274
|
+
const r = runner(g, { tools: [write, read, refuse] })
|
|
275
|
+
await r.ensure()
|
|
276
|
+
const call = (id: string, name: string, args = {}) =>
|
|
277
|
+
r.call({
|
|
278
|
+
entity: { eid: id },
|
|
279
|
+
call: { to: toolEid(name), args },
|
|
280
|
+
})
|
|
281
|
+
const proposed = await call('preview', 'write_text', { check: true })
|
|
282
|
+
equal(answerOf(proposed, 'preview')[0].content, { body: 'hello' })
|
|
283
|
+
equal(await g.get(['text']), [])
|
|
284
|
+
await call('read', 'read_text')
|
|
285
|
+
equal(await g.get(['read-only']), [])
|
|
286
|
+
await call('write', 'write_text')
|
|
287
|
+
equal((await g.get(['text']))[0].content, { body: 'hello' })
|
|
288
|
+
const failed = await call('refused', 'refuse')
|
|
289
|
+
equal(faulted(failed, 'refused'), true)
|
|
290
|
+
equal(answerOf(failed, 'refused')[0].refusal, { code: 'access' })
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## Read an answer
|
|
294
|
+
|
|
295
|
+
`answerOf(bundles, callId)` removes this call and its result by identity. It
|
|
296
|
+
preserves other calls and results when a tool returns them as data.
|
|
297
|
+
`faulted(bundles, callId)` checks the named call's execution state, rather than
|
|
298
|
+
mistaking failure data in its answer for a failed invocation.
|
|
299
|
+
|
|
300
|
+
`worded(answer)` says what a call said as its `content.body`, search hits as one
|
|
301
|
+
line, and other bundles whole as JSON, each run of them one JSON array, so the
|
|
302
|
+
data in an answer parses with lines between. What a call said is words under a
|
|
303
|
+
name of the tool's own (`$said`) or aimed at the call (`output{source}`); a row
|
|
304
|
+
the answer read or wrote is data even when words are all it holds, since its
|
|
305
|
+
words alone would drop which entity it is. A component's prose, a string that
|
|
306
|
+
breaks lines or runs past 200 characters such as a `doc.body`, is said as plain
|
|
307
|
+
text: its JSON string holds a tag like `<doc.body t1>`, and a block opening with
|
|
308
|
+
that tag and closing with `</doc.body>` follows the bundle. A rehearsal
|
|
309
|
+
(`check: true`) ends with a line saying nothing was written. `texts(answer)` is
|
|
310
|
+
the same answer for a door that sends several texts, as MCP sends content
|
|
311
|
+
blocks: the data as JSON first, then its prose, then the lines beside it. It
|
|
312
|
+
limits text to `WORDS` characters by default and counts the bundles left unsaid.
|
|
313
|
+
`most` in runner options limits the answer's JSON size to `MOST` characters by
|
|
314
|
+
default; larger answers are refused with `too_large`.
|
|
315
|
+
|
|
316
|
+
`readable(vocab, ids)(answer)` from `@yaks/tools/readable` is the answer as its
|
|
317
|
+
reader reads it. Each `prose` property (@yaks/vocab) is drawn by
|
|
318
|
+
[@yaks/markdown](../markdown/README.md)'s `drawn`. An eid in it reads as the id
|
|
319
|
+
the answer itself gives that entity, whatever its kind, or as the id `ids` gives
|
|
320
|
+
(@yaks/id/graph `held`), else as its short handle. `drawing(vocab, held)` is
|
|
321
|
+
what a host applies before an answer is sent as data, printed or drawn
|
|
322
|
+
(@yaks/host `Host.readable`): it asks `held` about every eid the answer's prose
|
|
323
|
+
names and draws with what it answers, so a task a comment names reads as `T-12`.
|
|
324
|
+
The stored text is unchanged.
|
|
325
|
+
|
|
326
|
+
`structured(tool, answer)` returns `{ result: answer }`, or `output.value` for a
|
|
327
|
+
tool declaring `outputSchema`. `valueIn(answer)` reads that data directly.
|
|
328
|
+
`display(value)` renders structured data as Markdown fields.
|
|
329
|
+
|
|
330
|
+
```ts
|
|
331
|
+
import { answerOf, display, structured, worded } from '@yaks/tools'
|
|
332
|
+
import { valueIn } from '@yaks/tools/value'
|
|
333
|
+
import { equal } from '@yaks/testing'
|
|
334
|
+
|
|
335
|
+
const value = { total: 3 }
|
|
336
|
+
const answer = [{
|
|
337
|
+
entity: { eid: 'total' },
|
|
338
|
+
content: { body: 'Three items' },
|
|
339
|
+
output: { source: 'count', value },
|
|
340
|
+
}]
|
|
341
|
+
const bundles = [...answer, {
|
|
342
|
+
entity: { eid: 'result' },
|
|
343
|
+
result: { call: 'count', ms: 1 },
|
|
344
|
+
}]
|
|
345
|
+
equal(answerOf(bundles, 'count'), answer)
|
|
346
|
+
equal(worded(answer), 'Three items')
|
|
347
|
+
equal(valueIn(answer), value)
|
|
348
|
+
equal(structured({ outputSchema: { type: 'object' } }, answer), value)
|
|
349
|
+
equal(structured({}, answer), { result: answer })
|
|
350
|
+
equal(display(value).includes('**total**: `3`'), true)
|
|
351
|
+
equal(display([]), 'No rows.')
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
## Pending calls and scheduling
|
|
355
|
+
|
|
356
|
+
`run(callId)` executes a stored call, awaits an invocation already running in
|
|
357
|
+
this process, or recalls its recorded answer. A read-only answer is not stored
|
|
358
|
+
as bundles; recalling it returns its result with the answer's text.
|
|
359
|
+
`due(callId)` runs a call a rule selected if it still exists and is unclaimed.
|
|
360
|
+
`drive()` runs one pass over eligible calls. `reconcile(r)` calls
|
|
361
|
+
`drive({ redrive: true })` for startup recovery.
|
|
362
|
+
|
|
363
|
+
The runner's [rules](../graph/README.md#data-model) select an unclaimed call
|
|
364
|
+
without a result or wake (`call_ready`), or a call without a result when its
|
|
365
|
+
[@yaks/wake](../wake/README.md) writes `fired{at}` (`call_woken`). Their matches
|
|
366
|
+
derive result eids, so rerunning a call addresses the same result. Importing
|
|
367
|
+
this package starts no timer and registers no effects. A host can register each
|
|
368
|
+
rule with [@yaks/effects](../effects/README.md):
|
|
369
|
+
|
|
370
|
+
```ts
|
|
371
|
+
import { graph } from '@yaks/graph'
|
|
372
|
+
import { effects } from '@yaks/effects'
|
|
373
|
+
import { ram } from '@yaks/ram'
|
|
374
|
+
import { loadVocab } from '@yaks/vocab'
|
|
375
|
+
import { equal, until } from '@yaks/testing'
|
|
376
|
+
import { executionComputed, runner, toolEid, toolsDoc } from '@yaks/tools'
|
|
377
|
+
|
|
378
|
+
const vocab = loadVocab([toolsDoc])
|
|
379
|
+
const fx = effects(vocab, {
|
|
380
|
+
report: (error) => {
|
|
381
|
+
throw error
|
|
382
|
+
},
|
|
383
|
+
})
|
|
384
|
+
const g = graph({
|
|
385
|
+
vocab,
|
|
386
|
+
storage: ram(vocab, { computed: executionComputed }),
|
|
387
|
+
plugins: [fx],
|
|
388
|
+
})
|
|
389
|
+
const r = runner(g, {
|
|
390
|
+
tools: [{
|
|
391
|
+
name: 'echo',
|
|
392
|
+
description: 'Say hello',
|
|
393
|
+
run: (call) => [{
|
|
394
|
+
entity: { eid: '$hello' },
|
|
395
|
+
content: { body: 'hello' },
|
|
396
|
+
output: { source: call.entity.eid },
|
|
397
|
+
}],
|
|
398
|
+
}],
|
|
399
|
+
})
|
|
400
|
+
await r.ensure()
|
|
401
|
+
for (const rule of r.rules) {
|
|
402
|
+
fx.on(rule.plan, (event) => r.due(event.entity.eid))
|
|
403
|
+
}
|
|
404
|
+
await g.apply([{
|
|
405
|
+
entity: { eid: 'pending' },
|
|
406
|
+
call: { to: toolEid('echo'), args: {} },
|
|
407
|
+
}])
|
|
408
|
+
await until(async () => (await g.read('.result.call=pending')).length == 1)
|
|
409
|
+
equal((await g.read('.output.source=pending&*'))[0].content, { body: 'hello' })
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
A one-shot call with `wake.at` runs in place after firing. A recurring call with
|
|
413
|
+
`wake.every` or `wake.while` creates one call per firing, with `call.source`
|
|
414
|
+
pointing to the schedule. The generated eid is derived from the schedule eid and
|
|
415
|
+
`fired.at`, so repeating a firing does not create a second call. Keep one
|
|
416
|
+
scheduling owner per graph. [@yaks/session](../session/README.md) runs its
|
|
417
|
+
transcript's calls in order rather than registering these effects.
|
|
418
|
+
|
|
419
|
+
```ts
|
|
420
|
+
import { graph } from '@yaks/graph'
|
|
421
|
+
import { ram } from '@yaks/ram'
|
|
422
|
+
import { loadVocab } from '@yaks/vocab'
|
|
423
|
+
import { wakeDoc } from '@yaks/wake'
|
|
424
|
+
import { runner, toolEid, toolsDoc } from '@yaks/tools'
|
|
425
|
+
import { equal } from '@yaks/testing'
|
|
426
|
+
|
|
427
|
+
const vocab = loadVocab([toolsDoc, wakeDoc])
|
|
428
|
+
const g = graph({ vocab, storage: ram(vocab) })
|
|
429
|
+
let ran = 0
|
|
430
|
+
const r = runner(g, {
|
|
431
|
+
tools: [{
|
|
432
|
+
name: 'daily',
|
|
433
|
+
description: 'Do daily work',
|
|
434
|
+
run: () => {
|
|
435
|
+
ran++
|
|
436
|
+
return []
|
|
437
|
+
},
|
|
438
|
+
}],
|
|
439
|
+
})
|
|
440
|
+
await r.ensure()
|
|
441
|
+
await g.apply([{
|
|
442
|
+
entity: { eid: 'schedule' },
|
|
443
|
+
call: { to: toolEid('daily'), args: {} },
|
|
444
|
+
wake: { at: '2030-01-01T00:00:00.000Z', every: '1d' },
|
|
445
|
+
}])
|
|
446
|
+
await r.drive()
|
|
447
|
+
equal(ran, 0)
|
|
448
|
+
for (const at of ['2030-01-01T00:00:00.000Z', '2030-01-02T00:00:00.000Z']) {
|
|
449
|
+
await g.apply([{ entity: { eid: 'schedule' }, fired: { at } }])
|
|
450
|
+
await r.drive()
|
|
451
|
+
}
|
|
452
|
+
await r.drive()
|
|
453
|
+
equal(ran, 2)
|
|
454
|
+
equal((await g.read('.call.source=schedule')).length, 2)
|
|
455
|
+
equal(await g.read('.result.call=schedule'), [])
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
## Call claims, recovery, and interruption
|
|
459
|
+
|
|
460
|
+
A [precondition](../graph/README.md#writes-and-reads) on `execution.by` prevents
|
|
461
|
+
two runners from claiming the same call. Runners over the same graph object
|
|
462
|
+
share in-process invocations. The runner leaves call claims held by active
|
|
463
|
+
owners alone, including call claims held by its own owner in another runner. An
|
|
464
|
+
owner whose entity carries `exit` has abandoned its call claims, so another
|
|
465
|
+
runner can take them. Without an explicit `owner`, the runner generates an owner
|
|
466
|
+
eid.
|
|
467
|
+
|
|
468
|
+
`run()` throws `UnfinishedCall` for an unfinished, anonymously claimed call.
|
|
469
|
+
`reconcile()` may run it again. Recovery can repeat an external side effect if
|
|
470
|
+
the process stopped after the side effect and before persisting the result;
|
|
471
|
+
claiming does not guarantee exactly-once execution.
|
|
472
|
+
|
|
473
|
+
`interrupt(why)` ends calls this runner is running; `interrupt(why, holder)`
|
|
474
|
+
ends a holder's unanswered call claims. `interruptCall(callId, why, answer?)`
|
|
475
|
+
ends an unstarted or anonymously claimed call without invoking its tool. A named
|
|
476
|
+
call claim is refused with `UnfinishedCall`. An `Interrupted(message, code)`
|
|
477
|
+
thrown by a tool records interruption rather than a refusal or defect.
|
|
478
|
+
Interruption marks are supplied by [@yaks/kernel](../kernel/README.md).
|
|
479
|
+
|
|
480
|
+
```ts
|
|
481
|
+
import { graph } from '@yaks/graph'
|
|
482
|
+
import { kernelDoc, kernelKeywords } from '@yaks/kernel'
|
|
483
|
+
import { ram } from '@yaks/ram'
|
|
484
|
+
import { loadVocab } from '@yaks/vocab'
|
|
485
|
+
import {
|
|
486
|
+
executionComputed,
|
|
487
|
+
executionState,
|
|
488
|
+
faulted,
|
|
489
|
+
runner,
|
|
490
|
+
toolEid,
|
|
491
|
+
toolsDoc,
|
|
492
|
+
} from '@yaks/tools'
|
|
493
|
+
import { equal } from '@yaks/testing'
|
|
494
|
+
|
|
495
|
+
const vocab = loadVocab([toolsDoc, kernelDoc], [kernelKeywords])
|
|
496
|
+
const g = graph({
|
|
497
|
+
vocab,
|
|
498
|
+
storage: ram(vocab, { computed: executionComputed }),
|
|
499
|
+
})
|
|
500
|
+
let ran = 0
|
|
501
|
+
const r = runner(g, {
|
|
502
|
+
tools: [{
|
|
503
|
+
name: 'work',
|
|
504
|
+
description: 'Do work',
|
|
505
|
+
run: () => {
|
|
506
|
+
ran++
|
|
507
|
+
return []
|
|
508
|
+
},
|
|
509
|
+
}],
|
|
510
|
+
})
|
|
511
|
+
await r.ensure()
|
|
512
|
+
await g.apply([{
|
|
513
|
+
entity: { eid: 'cancelled' },
|
|
514
|
+
call: { to: toolEid('work'), args: {} },
|
|
515
|
+
}])
|
|
516
|
+
const interrupted = await r.interruptCall('cancelled', 'Caller cancelled')
|
|
517
|
+
equal(faulted(interrupted, 'cancelled'), true)
|
|
518
|
+
equal(executionState((await g.get(['cancelled']))[0]), 'interrupted')
|
|
519
|
+
await r.drive()
|
|
520
|
+
equal(ran, 0)
|
|
521
|
+
```
|
|
522
|
+
|
|
523
|
+
## Runner options
|
|
524
|
+
|
|
525
|
+
`host` is the graph passed to tool functions; it defaults to the graph storing
|
|
526
|
+
calls. `owner` identifies the call claim holder. `process` supplies
|
|
527
|
+
`{pid, command, cwd}` on the call handed to the tool and is never stored by the
|
|
528
|
+
runner. `otherwise` answers calls naming tools the runner lacks; without it,
|
|
529
|
+
those calls are left for another runner. `takes(call)` can restrict which calls
|
|
530
|
+
the runner accepts. `clock` (an @yaks/fp `Clock`) is what it keeps time by: its
|
|
531
|
+
`now` measures `result.ms`, and a write storage refused waits on its `after`
|
|
532
|
+
before trying again. The default is the runtime's timers with `performance.now`.
|
|
533
|
+
|
|
534
|
+
The runner uses `who(call)` when applying tool answers. **Call authority** is
|
|
535
|
+
the identity of whoever last decided its executable request. Changing the tool
|
|
536
|
+
or target, arguments, or source records that editor's entire `{by, via}` pair in
|
|
537
|
+
the server-owned `call.actor`. An empty pair means automation. Before an
|
|
538
|
+
executable edit, authority comes from `created` or an unstamped call's `$actor`.
|
|
539
|
+
No-op edits, provider ids, labels, claims, and wake firings keep call authority;
|
|
540
|
+
each recurring invocation inherits its schedule's call authority. Authorization
|
|
541
|
+
checks that identity's current standing when the call fires.
|
|
542
|
+
|
|
543
|
+
So a call has its writer's authority however it was written: through a tool
|
|
544
|
+
door, by a rule inside a write (whose output carries the batch's actor), or by
|
|
545
|
+
an effect after the commit that signs its write with the commit's actor
|
|
546
|
+
(@yaks/effects `Event.actor`). An effect's unsigned write is automation.
|
|
547
|
+
|
|
548
|
+
`report` receives caught failures, including tool defects, unrecorded reads,
|
|
549
|
+
completed-answer persistence and replies. Recording an `exception` answer keeps
|
|
550
|
+
the call’s history; it does not create another tracker occurrence.
|
|
551
|
+
|
|
552
|
+
A **reply** is extra answer bundles owed to a direct caller (`Reply`).
|
|
553
|
+
`reply(call, answer, wrote)` runs after `call()`, never after a deferred
|
|
554
|
+
invocation. `wrote` contains the tool's applied answer for a kept write and is
|
|
555
|
+
empty for reads, rehearsals, and refusals. Replies are appended and not written;
|
|
556
|
+
a failed reply is reported and the tool's answer still returns.
|
|
557
|
+
|
|
558
|
+
```ts
|
|
559
|
+
import { graph, type Tool } from '@yaks/graph'
|
|
560
|
+
import { ram } from '@yaks/ram'
|
|
561
|
+
import { loadVocab } from '@yaks/vocab'
|
|
562
|
+
import { answerOf, runner, toolEid, toolsDoc, worded } from '@yaks/tools'
|
|
563
|
+
import { equal } from '@yaks/testing'
|
|
564
|
+
|
|
565
|
+
const vocab = loadVocab([toolsDoc])
|
|
566
|
+
const records = graph({ vocab, storage: ram(vocab) })
|
|
567
|
+
const host = graph({ vocab, storage: ram(vocab) })
|
|
568
|
+
await records.apply([{
|
|
569
|
+
entity: { eid: 'server' },
|
|
570
|
+
content: { body: 'Runner' },
|
|
571
|
+
}])
|
|
572
|
+
await host.apply([{ entity: { eid: 'source' }, content: { body: 'hello' } }])
|
|
573
|
+
const read: Tool = {
|
|
574
|
+
name: 'read',
|
|
575
|
+
description: 'Read host text',
|
|
576
|
+
readOnly: true,
|
|
577
|
+
run: (_call, graph) => graph.get(['source']),
|
|
578
|
+
}
|
|
579
|
+
const r = runner(records, {
|
|
580
|
+
tools: [read],
|
|
581
|
+
host,
|
|
582
|
+
owner: 'server',
|
|
583
|
+
reply: async (_call, _answer, wrote) => {
|
|
584
|
+
equal(wrote, [])
|
|
585
|
+
return [{ entity: { eid: '$extra' }, content: { body: 'reply' } }]
|
|
586
|
+
},
|
|
587
|
+
})
|
|
588
|
+
await r.ensure()
|
|
589
|
+
const bundles = await r.call({
|
|
590
|
+
entity: { eid: 'read-host' },
|
|
591
|
+
call: { to: toolEid('read'), args: {} },
|
|
592
|
+
})
|
|
593
|
+
// The row read is data, said whole; the reply is words.
|
|
594
|
+
equal(
|
|
595
|
+
worded(answerOf(bundles, 'read-host')),
|
|
596
|
+
JSON.stringify(
|
|
597
|
+
[{ entity: { eid: 'source' }, content: { body: 'hello' } }],
|
|
598
|
+
null,
|
|
599
|
+
2,
|
|
600
|
+
) +
|
|
601
|
+
'\nreply',
|
|
602
|
+
)
|
|
603
|
+
equal(await records.get(['source', '$extra']), [])
|
|
604
|
+
```
|
|
605
|
+
|
|
606
|
+
## Health checks
|
|
607
|
+
|
|
608
|
+
A **check** is a tool whose `verb` is `check`; `checks(tools)` selects those
|
|
609
|
+
tools. A **finding** is `{ level, text }` describing something a check found
|
|
610
|
+
(`Finding`). A finding's **level** is `fail` for a measured contract violation
|
|
611
|
+
or `warn` for a leak or a verdict the check could not establish (`Level`).
|
|
612
|
+
|
|
613
|
+
`checked(callId, about, findings)` builds one answer bundle, even with no
|
|
614
|
+
findings. `finding.level` carries the most severe level. `ailing(tool, answer)`
|
|
615
|
+
tests a check's answer for `fail`, and the CLI exits 1 when it holds; reporting
|
|
616
|
+
a violation does not itself make the invocation fail.
|
|
617
|
+
|
|
618
|
+
```ts
|
|
619
|
+
import { ailing, checked, checks, worded } from '@yaks/tools'
|
|
620
|
+
import { equal } from '@yaks/testing'
|
|
621
|
+
|
|
622
|
+
const tools = [{ noun: 'queue', verb: 'check' }, {
|
|
623
|
+
noun: 'queue',
|
|
624
|
+
verb: 'list',
|
|
625
|
+
}]
|
|
626
|
+
equal(checks(tools), [tools[0]])
|
|
627
|
+
const clear = checked('check-queue', 'No calls are stuck', [])
|
|
628
|
+
equal(worded(clear), 'No calls are stuck — nothing to report')
|
|
629
|
+
equal(ailing(tools[0], clear), false)
|
|
630
|
+
const failed = checked('check-queue', 'No calls are stuck', [
|
|
631
|
+
{ level: 'warn', text: 'A holder cannot be inspected' },
|
|
632
|
+
{ level: 'fail', text: 'A call has no active holder or result' },
|
|
633
|
+
])
|
|
634
|
+
equal(failed[0].finding, { level: 'fail' })
|
|
635
|
+
equal(ailing(tools[0], failed), true)
|
|
636
|
+
equal(ailing(tools[1], failed), false)
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
## App-declared commands
|
|
640
|
+
|
|
641
|
+
A **command** is an app's `tool: true` declaration with exactly one act:
|
|
642
|
+
`apply`, `query`, or `worker` (`ToolDef`). A **command template** is that
|
|
643
|
+
command's `apply` bundles or `query` text with `$name` variables filled from
|
|
644
|
+
arguments. `parseTools(source, componentNames)` reads and checks these
|
|
645
|
+
declarations from a JSON string or object. `filled(command, args)` fills its
|
|
646
|
+
command template without performing the act; `invoke(command, args, acts)` hands
|
|
647
|
+
it to the supplied boundary. `commands(definitions, worker?)` converts
|
|
648
|
+
declarations to tools a runner can execute.
|
|
649
|
+
|
|
650
|
+
```ts
|
|
651
|
+
import { filled, invoke, parseTools, schemaOf } from '@yaks/tools/declared'
|
|
652
|
+
import { equal } from '@yaks/testing'
|
|
653
|
+
|
|
654
|
+
const definitions = parseTools({
|
|
655
|
+
$defs: {
|
|
656
|
+
log_run: {
|
|
657
|
+
tool: true,
|
|
658
|
+
description: 'Log a run',
|
|
659
|
+
input: { miles: { type: 'number' }, note: { type: 'string' } },
|
|
660
|
+
required: ['miles'],
|
|
661
|
+
apply: {
|
|
662
|
+
entity: { eid: '$run' },
|
|
663
|
+
run: { miles: '$miles', note: '$note' },
|
|
664
|
+
},
|
|
665
|
+
},
|
|
666
|
+
runs: {
|
|
667
|
+
tool: true,
|
|
668
|
+
description: 'Read runs',
|
|
669
|
+
input: { since: { type: 'number' } },
|
|
670
|
+
query: '.run&.run.miles>=$since',
|
|
671
|
+
},
|
|
672
|
+
tick: {
|
|
673
|
+
tool: true,
|
|
674
|
+
description: 'Advance a counter',
|
|
675
|
+
worker: '/tick',
|
|
676
|
+
input: { count: { type: 'integer' } },
|
|
677
|
+
required: ['count'],
|
|
678
|
+
},
|
|
679
|
+
},
|
|
680
|
+
}, ['run'])
|
|
681
|
+
equal(filled(definitions.log_run, { miles: '5' }), {
|
|
682
|
+
apply: { entity: { eid: '$run' }, run: { miles: 5 } },
|
|
683
|
+
})
|
|
684
|
+
equal(filled(definitions.runs, {}), { query: '.run' })
|
|
685
|
+
equal(filled(definitions.runs, { since: 5 }), { query: '.run&.run.miles>=5' })
|
|
686
|
+
equal(schemaOf(definitions.log_run).required, ['miles'])
|
|
687
|
+
const acts = {
|
|
688
|
+
apply: (bundles: unknown[]) => bundles,
|
|
689
|
+
query: (query: string) => query,
|
|
690
|
+
worker: (path: string, args: Record<string, unknown>) => ({ path, args }),
|
|
691
|
+
}
|
|
692
|
+
equal(await invoke(definitions.tick, { count: '2' }, acts), {
|
|
693
|
+
path: '/tick',
|
|
694
|
+
args: { count: 2 },
|
|
695
|
+
})
|
|
696
|
+
```
|
|
697
|
+
|
|
698
|
+
A whole `$name` retains the argument's type; within text it becomes text. `$$`
|
|
699
|
+
is a literal dollar sign. Omitted optional arguments remove their containing
|
|
700
|
+
keys or `&`-separated query clauses. Unbound eids in `apply` remain graph
|
|
701
|
+
aliases; unknown variables and undeclared component names are refused. Query
|
|
702
|
+
argument values are percent-encoded; an entire query supplied through one
|
|
703
|
+
argument is passed through. See [query syntax](../query/README.md#query-model).
|
|
704
|
+
|
|
705
|
+
`model: true` offers a command to an app's models. A command template's
|
|
706
|
+
`$session`, when not declared as an input, must come from the invocation's
|
|
707
|
+
`Context.session`. `view` names a relative HTML file in the app;
|
|
708
|
+
`viewsOf(source)` lists the files for the host to check. `discoverable: false`
|
|
709
|
+
is metadata for hosts to hide internal commands. A worker command can declare
|
|
710
|
+
`readOnly: true`; a query command is read-only by its act.
|
|
711
|
+
|
|
712
|
+
A **floor** is the least caller authority a command requires: `person`,
|
|
713
|
+
`editor`, or `owner` (`Floor`). `mayCall(floor, person, role)` checks this
|
|
714
|
+
floor; the host still authorizes each act.
|
|
715
|
+
|
|
716
|
+
```ts
|
|
717
|
+
import { mayCall } from '@yaks/tools/access'
|
|
718
|
+
import { commands, filled, parseTools, viewsOf } from '@yaks/tools/declared'
|
|
719
|
+
import { equal } from '@yaks/testing'
|
|
720
|
+
|
|
721
|
+
const source = {
|
|
722
|
+
$defs: {
|
|
723
|
+
own_calls: {
|
|
724
|
+
tool: true,
|
|
725
|
+
description: 'Read this session’s calls',
|
|
726
|
+
model: true,
|
|
727
|
+
input: {},
|
|
728
|
+
query: '.call.source=$session',
|
|
729
|
+
view: 'calls.html',
|
|
730
|
+
floor: 'person',
|
|
731
|
+
},
|
|
732
|
+
},
|
|
733
|
+
}
|
|
734
|
+
const definitions = parseTools(source)
|
|
735
|
+
equal(viewsOf(source), ['calls.html'])
|
|
736
|
+
equal(filled(definitions.own_calls, {}, { session: 's1' }), {
|
|
737
|
+
query: '.call.source=s1',
|
|
738
|
+
})
|
|
739
|
+
equal(commands(definitions)[0].readOnly, true)
|
|
740
|
+
equal(mayCall('editor', 'ada', 'editor'), true)
|
|
741
|
+
equal(mayCall('owner', 'ada', 'editor'), false)
|
|
742
|
+
equal(mayCall('person', null, null), false)
|
|
743
|
+
```
|
|
744
|
+
|
|
745
|
+
## HTTP route and views
|
|
746
|
+
|
|
747
|
+
`routes(host)` contributes `POST /command` if `host.command` exists. It accepts
|
|
748
|
+
`{name,args}`, returns the host's answer with `ok: true`, and converts expected
|
|
749
|
+
refusals to HTTP error responses. Unexpected errors propagate to the host. The
|
|
750
|
+
host resolves the app and caller and authorizes invocation;
|
|
751
|
+
[@yaks/api](../api/README.md) serves the route.
|
|
752
|
+
|
|
753
|
+
```ts
|
|
754
|
+
import { routes } from '@yaks/tools/routes'
|
|
755
|
+
import { CallError } from '@yaks/tools'
|
|
756
|
+
import { equal } from '@yaks/testing'
|
|
757
|
+
|
|
758
|
+
const [route] = routes({
|
|
759
|
+
command: async (name, args) => {
|
|
760
|
+
if (name != 'echo') throw new CallError('missing', 'Unknown command')
|
|
761
|
+
return { answer: args.text }
|
|
762
|
+
},
|
|
763
|
+
})
|
|
764
|
+
const request = (name: string) =>
|
|
765
|
+
new Request('https://example.test/command', {
|
|
766
|
+
method: 'POST',
|
|
767
|
+
headers: { 'Content-Type': 'application/json' },
|
|
768
|
+
body: JSON.stringify({ name, args: { text: 'hello' } }),
|
|
769
|
+
})
|
|
770
|
+
equal(await (await route.handle(request('echo'))).json(), {
|
|
771
|
+
answer: 'hello',
|
|
772
|
+
ok: true,
|
|
773
|
+
})
|
|
774
|
+
equal((await route.handle(request('unknown'))).status, 404)
|
|
775
|
+
equal(routes({}), [])
|
|
776
|
+
```
|
|
777
|
+
|
|
778
|
+
`refused(error)` gives a host the same JSON refusal and HTTP status. It throws
|
|
779
|
+
unexpected errors through to the host's failure boundary.
|
|
780
|
+
|
|
781
|
+
```ts
|
|
782
|
+
import { refused } from '@yaks/tools/errors'
|
|
783
|
+
import { CallError } from '@yaks/tools'
|
|
784
|
+
import { equal } from '@yaks/testing'
|
|
785
|
+
|
|
786
|
+
const answer = refused(new CallError('access', 'Not a writer'))
|
|
787
|
+
equal(answer.status, 403)
|
|
788
|
+
equal(await answer.json(), {
|
|
789
|
+
error: { code: 'access', message: 'Not a writer' },
|
|
790
|
+
})
|
|
791
|
+
```
|
|
792
|
+
|
|
793
|
+
`views` supplies `Tile` and `Page` [renderers](../render/README.md#use) for
|
|
794
|
+
`content.body`, preserving line breaks through the rendering backend. A text the
|
|
795
|
+
graph keeps (it carries `created`) is, as a `Tile`, a passage
|
|
796
|
+
([@yaks/render](../render/README.md#shared-views)'s `passage`): its title, who
|
|
797
|
+
wrote it, when, and the words around what was sought; its `Page` is still the
|
|
798
|
+
text whole.
|
|
799
|
+
|
|
800
|
+
```ts
|
|
801
|
+
import { views } from '@yaks/tools/views'
|
|
802
|
+
import { render } from '@yaks/text'
|
|
803
|
+
import { loadVocab } from '@yaks/vocab'
|
|
804
|
+
import { toolsDoc } from '@yaks/tools/vocab'
|
|
805
|
+
import { equal } from '@yaks/testing'
|
|
806
|
+
|
|
807
|
+
const vocab = loadVocab([toolsDoc])
|
|
808
|
+
const bundle = { entity: { eid: 'message' }, content: { body: 'one\ntwo' } }
|
|
809
|
+
equal(render(views, bundle, 'Tile', vocab).includes('one'), true)
|
|
810
|
+
equal(render(views, bundle, 'Page', vocab).includes('two'), true)
|
|
811
|
+
```
|
|
812
|
+
|
|
813
|
+
## Limits
|
|
814
|
+
|
|
815
|
+
The runner does not discover tools, start a polling loop, or fire wakes. Hosts
|
|
816
|
+
load tools through
|
|
817
|
+
[@yaks/graph/tools](../graph/README.md#built-in-tool-declarations), register
|
|
818
|
+
effects through [@yaks/effects](../effects/README.md), and drive time through
|
|
819
|
+
[@yaks/wake](../wake/README.md). Durable storage, stamping, and query
|
|
820
|
+
capabilities depend on the graph's installed packages and adapter.
|
|
821
|
+
|
|
822
|
+
## Direct read-only calls
|
|
823
|
+
|
|
824
|
+
`runner.read(call)` validates arguments and invokes a read-only tool without
|
|
825
|
+
recording tool rows, a call, a claim, a result, telemetry or reply bundles. It
|
|
826
|
+
returns the same answer and transient result shape as a recorded call, including
|
|
827
|
+
refusals; `runner.call(call)` retains recorded execution for stored work,
|
|
828
|
+
including deferred reads.
|
|
829
|
+
|
|
830
|
+
`@yaks/tools/read` exports `reader(graph, options)`, the same invocation
|
|
831
|
+
boundary without a recorded runner. Its `read` method validates, resolves
|
|
832
|
+
references, attributes answers and reports faults in the same way; methods for
|
|
833
|
+
recorded work refuse. `@yaks/tools/result` supplies the tool identities and
|
|
834
|
+
answer formatting shared by both runners. `@yaks/tools/persist` supplies
|
|
835
|
+
storage-certified write retries without loading either runner;
|
|
836
|
+
`persist(write, report, clock?)` waits out its backoff on the clock it is given.
|
|
837
|
+
`@yaks/tools/fault` classifies defects, and `@yaks/tools/errors` keeps shared
|
|
838
|
+
invocation error identities and `refused`, their HTTP answer.
|
|
839
|
+
|
|
840
|
+
```ts
|
|
841
|
+
import { graph, mint } from '@yaks/graph'
|
|
842
|
+
import { ram } from '@yaks/ram'
|
|
843
|
+
import { loadVocab } from '@yaks/vocab'
|
|
844
|
+
import { kernelDoc, kernelKeywords } from '@yaks/kernel'
|
|
845
|
+
import { callDoc, toolDoc } from '@yaks/tools/vocab'
|
|
846
|
+
import { reader } from '@yaks/tools/read'
|
|
847
|
+
import { answerOf, toolEid } from '@yaks/tools/result'
|
|
848
|
+
import { equal } from '@yaks/testing'
|
|
849
|
+
|
|
850
|
+
let vocab = loadVocab([kernelDoc, callDoc, toolDoc], [kernelKeywords])
|
|
851
|
+
let g = graph({ vocab, storage: ram(vocab) })
|
|
852
|
+
let r = reader(g, {
|
|
853
|
+
tools: [{
|
|
854
|
+
name: 'example_read',
|
|
855
|
+
description: 'Read without recording',
|
|
856
|
+
readOnly: true,
|
|
857
|
+
run: (call) => [{
|
|
858
|
+
entity: { eid: '$said' },
|
|
859
|
+
content: { body: 'read' },
|
|
860
|
+
output: { source: call.entity.eid },
|
|
861
|
+
}],
|
|
862
|
+
}],
|
|
863
|
+
})
|
|
864
|
+
let id = mint()
|
|
865
|
+
let answer = await r.read({
|
|
866
|
+
entity: { eid: id },
|
|
867
|
+
call: { to: toolEid('example_read'), args: {} },
|
|
868
|
+
})
|
|
869
|
+
equal(answerOf(answer, id)[0].content, { body: 'read' })
|
|
870
|
+
equal(await g.get([id]), [])
|
|
871
|
+
```
|