@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/invoke.ts ADDED
@@ -0,0 +1,210 @@
1
+ // A tool's validation, caller identity, answer and fault, independent of persistence.
2
+ import type { Bundle, Comp, Graph, NamedTool } from 'jsr:@yaks/graph@0.2.36'
3
+ import { who } from '@yaks/graph/call'
4
+ import { signed } from '@yaks/graph/provenance'
5
+ import { Refused, status, unheard } from '@yaks/graph/errors'
6
+ import { QuerySyntax } from '@yaks/query/core'
7
+ import { asking } from '@yaks/graph/asker'
8
+ import type { Opts, Refusal } from './runner.ts'
9
+ import { CallError, parsed, resolved, validated } from './args.js'
10
+ import { Busy, Interrupted } from './errors.js'
11
+ import { actionable, exceptionOf } from './fault.js'
12
+ import { MOST } from './result.js'
13
+ import { said } from './said.js'
14
+
15
+ /** What a call cut off before its arguments were finished (`cut`) is
16
+ * answered: nothing ran, and how its writer, a model, fits it in a reply. */
17
+ export let CUT = 'This call was cut off: your reply reached its output limit ' +
18
+ 'while writing it, so its arguments were incomplete and nothing ran. Make ' +
19
+ 'it again with less in one reply: think less before it, and split a long ' +
20
+ 'script or file across several smaller calls.'
21
+
22
+ /** What a rehearsal says after the batch it answers. */
23
+ let CHECKED = 'Checked: nothing was written. Its eids were made for ' +
24
+ 'this check alone; the same call without check writes it, under eids of ' +
25
+ 'its own.'
26
+
27
+ export type Land = (
28
+ made: Bundle[],
29
+ state: string,
30
+ keeps?: boolean,
31
+ code?: string,
32
+ ) => Bundle[] | Promise<Bundle[]>
33
+ // An answer too long to send whole is refused, and so never reaches a door
34
+ // that would build its JSON in one string. It is measured one bundle at a
35
+ // time, stopping once past `most`, so measuring any answer costs at most that.
36
+ let measured = (answer: Bundle[], most: number) => {
37
+ let size = 0
38
+ for (let b of answer) {
39
+ size += JSON.stringify(b).length
40
+ if (size > most) {
41
+ throw new CallError(
42
+ 'too_large',
43
+ `the answer is ${answer.length} entities, past the ${most} ` +
44
+ 'characters of JSON a call answers in: ask for fewer',
45
+ )
46
+ }
47
+ }
48
+ }
49
+
50
+ /** What an error refuses, when it is a refusal: a `CallError` with its code,
51
+ * or an error the graph's `status` puts below 500 (a `Refused` write, an
52
+ * `Unknown` name) with its name, each in its own words. Anything else is a
53
+ * defect, and refuses nothing. */
54
+ export let refusalOf = (error: unknown): Refusal | undefined => {
55
+ let code = error instanceof CallError
56
+ ? error.code
57
+ : status(error) < 500
58
+ ? (error as Error).name
59
+ : undefined
60
+ if (code == undefined) return undefined
61
+ let said = (error as Error).message || String(error)
62
+ return error instanceof QuerySyntax ||
63
+ error instanceof Refused && error.summary !== undefined ||
64
+ error instanceof CallError && error.detail
65
+ ? { code, said, summary: error.summary }
66
+ : { code, said }
67
+ }
68
+
69
+ /** Tell whoever hears refusals of one, reporting a hearer that fails rather
70
+ * than letting it change the call's answer. */
71
+ export let heard = async (
72
+ opts: Pick<Opts, 'report' | 'refused'>,
73
+ refusal: Refusal,
74
+ call: Bundle,
75
+ tool?: string,
76
+ ): Promise<void> => {
77
+ try {
78
+ await opts.refused?.(refusal, call, tool)
79
+ } catch (e) {
80
+ await opts.report?.(e, call, tool)
81
+ }
82
+ }
83
+
84
+ /** Invoke once, letting the owner retain the answer or return it in memory. */
85
+ export let invoke = async (
86
+ call: Bundle,
87
+ tool: NamedTool,
88
+ g: Graph,
89
+ opts: Pick<Opts, 'host' | 'process' | 'report' | 'refused' | 'most'>,
90
+ land: Land,
91
+ defer = false,
92
+ ): Promise<Bundle[]> => {
93
+ let id = call.entity.eid
94
+ let host = opts.host ?? g
95
+ let c = call.call as Comp
96
+ // The tool the call asked for: the name its caller wrote, where it kept
97
+ // one, since a tool answering a name nobody serves (`otherwise`) is not it.
98
+ let asked = typeof c.name == 'string' && c.name ? c.name : tool.name
99
+ // What a thrown error becomes: the fault as its own entity, recording
100
+ // which call it came from. An expected refusal ({@link refusalOf}) gets
101
+ // `refusal{code}` and is told to whoever hears refusals. Anything else is
102
+ // a defect, reported with its original thrown value.
103
+ let faulted = async (error: unknown): Promise<Bundle[]> => {
104
+ if (error instanceof Interrupted) {
105
+ return [{
106
+ entity: { eid: '$fault' },
107
+ content: { body: String(error) },
108
+ output: { source: id },
109
+ ...(g.vocab.comp('notice') ? { notice: {} } : {}),
110
+ }]
111
+ }
112
+ // A service that did not answer for now is said plainly, as
113
+ // `unavailable`: neither a defect nor a refusal of the tool's, so it is
114
+ // neither reported nor told to whoever hears refusals.
115
+ if (unheard(error)) {
116
+ return [{
117
+ entity: { eid: '$fault' },
118
+ content: { body: (error as Error).message },
119
+ output: { source: id },
120
+ refusal: { code: 'unavailable' },
121
+ }]
122
+ }
123
+ let refusal = refusalOf(error)
124
+ if (!refusal && actionable(String(exceptionOf(error).value))) {
125
+ await opts.report?.(error, call, asked)
126
+ }
127
+ if (refusal) await heard(opts, refusal, call, asked)
128
+ // A refusal is said in its own words, which its code already classes;
129
+ // a defect keeps the name of what was thrown.
130
+ return [{
131
+ entity: { eid: '$fault' },
132
+ content: { body: refusal?.said ?? String(error) },
133
+ output: { source: id },
134
+ ...refusal
135
+ ? { refusal: { code: refusal.code } }
136
+ : { exception: exceptionOf(error) },
137
+ }]
138
+ }
139
+ let answered: Bundle[]
140
+ try {
141
+ // An unfinished call's arguments are not what its writer meant: whatever
142
+ // they spell, it is refused for what it is.
143
+ if (call.cut) throw new CallError('cut', CUT)
144
+ let args = await resolved(tool, validated(tool, parsed(c.args)), host)
145
+ let actor = who(call)
146
+ let called: Bundle = {
147
+ ...call,
148
+ call: { ...c, args },
149
+ ...!call.created && actor ? { created: { ...actor } } : {},
150
+ ...opts.process ? { process: opts.process } : {},
151
+ }
152
+ // What the tool starts answers it, and the session that called it.
153
+ let made = await asking(
154
+ { door: tool.name, ...actor?.via && { session: actor.via } },
155
+ () => tool.run(called, host),
156
+ )
157
+ if (!tool.applied) made = signed(made, actor)
158
+ measured(made, opts.most ?? MOST)
159
+ // A rehearsal: `check: true` to a tool that writes runs the write's
160
+ // every phase and rolls it back, so the answer is the batch as a kept
161
+ // write would have returned it, or the refusal it would have met. It
162
+ // says so after the batch, since otherwise it reads as the write itself,
163
+ // eids and stamps and all: a caller that took one for the other wrote
164
+ // twice, or never. A tool that answers only its own words about the call
165
+ // has those words to say instead: it handled `check` itself (a session's
166
+ // tool, whose graph call ran a rehearsal of its own and said so).
167
+ // A write on a branch lands there, through the branch's graph, and the
168
+ // call's record stays on main with the rest of its caller's: the answer
169
+ // is the batch as the branch applied it, kept nowhere else.
170
+ let branch = typeof args.branch == 'number' && !tool.readOnly &&
171
+ !tool.applied
172
+ ? args.branch
173
+ : undefined
174
+ if (branch !== undefined) {
175
+ let check = args.check === true
176
+ let wrote = await host.apply(made, { branch, ...check ? { check } : {} })
177
+ answered = await land(
178
+ check ? [...wrote, said(call, CHECKED)] : wrote,
179
+ 'done',
180
+ false,
181
+ )
182
+ } else if (!tool.readOnly && !tool.applied && args.check === true) {
183
+ let rehearsed = await g.apply(made, { check: true })
184
+ let reported = made.length > 0 &&
185
+ made.every((b) =>
186
+ b.entity.eid[0] == '$' && (b.output as Comp | undefined)?.source == id
187
+ )
188
+ answered = await land(
189
+ reported ? rehearsed : [...rehearsed, said(call, CHECKED)],
190
+ 'done',
191
+ false,
192
+ )
193
+ } else {
194
+ answered = await land(made, 'done')
195
+ }
196
+ } catch (error) {
197
+ if (defer && error instanceof Busy) throw error
198
+ // This catches both the tool's own throw and a rejection of what it
199
+ // returned: a transaction the graph refuses is this call's failure,
200
+ // rather than a call left claimed with nothing recorded about it.
201
+ let made = await faulted(error)
202
+ answered = await land(
203
+ made,
204
+ error instanceof Interrupted ? 'interrupted' : 'failed',
205
+ undefined,
206
+ error instanceof Interrupted ? error.code : undefined,
207
+ )
208
+ }
209
+ return answered
210
+ }
package/mod.js ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * @yaks/tools — a tool is a function that takes entity patches and returns
3
+ * entity patches; a `call` entity is the stored record of having asked for
4
+ * one. This package runs the function and keeps that record.
5
+ *
6
+ * A caller invokes a tool directly: `call()` writes the `call` entity, runs
7
+ * the function named by `call.to` with the call's bundles and a context
8
+ * carrying the caller's identity, and applies what the function returned
9
+ * together with a `result{call, ms}` entity whose id is derived from the rule
10
+ * that emits it. Nothing polls the graph — a call another process wrote, or
11
+ * one deferred by a `wake` component, is picked up by registering those same
12
+ * rules as effects (@yaks/effects `on`). See ./runner.ts for the claim, crash
13
+ * recovery, and scheduling.
14
+ *
15
+ * @module
16
+ */ export { callDoc, toolDoc, toolsDoc } from "./vocab.js";
17
+ export { ailing, CHECK, checked, checks } from "./check.js";
18
+ export { CallError, parsed, unreferenced } from "./args.js";
19
+ export { said } from "./said.js";
20
+ export { persist } from "./persist.js";
21
+ export { answerOf, faulted, MOST, pending, READY, RULES, structured, texts, toolEid, toolRow, WOKEN, worded, WORDS } from "./result.js";
22
+ export { became, keeps, reconcile, runner, UnfinishedCall } from "./runner.js";
23
+ export { valueIn } from "./value.js";
24
+ export { actionable, exceptionOf } from "./fault.js";
25
+ export { display } from "./display.js";
26
+ export { watched } from "./watch.js";
27
+ export { did, doing, stepOf, titleOf } from "./title.js";
28
+ export { executionComputed, executionDerived, executionState, Interrupted } from "./state.js";
29
+ export { registry } from "./registry.js";
30
+ export { CUT, heard, invoke, refusalOf } from "./invoke.js";
31
+ //# sourceMappingURL=mod.js.map
package/mod.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAAS,OAAO,EAAE,OAAO,EAAE,QAAQ,qBAAoB;AACvD,SACE,MAAM,EACN,KAAK,EACL,OAAO,EACP,MAAM,qBAGW;AACnB,SAAS,SAAS,EAAE,MAAM,EAAE,YAAY,oBAAmB;AAC3D,SAAS,IAAI,oBAAmB;AAChC,SAAS,OAAO,uBAAsB;AACtC,SACE,QAAQ,EACR,OAAO,EACP,IAAI,EACJ,OAAO,EACP,KAAK,EACL,KAAK,EACL,UAAU,EACV,KAAK,EACL,OAAO,EACP,OAAO,EACP,KAAK,EACL,MAAM,EACN,KAAK,sBACa;AACpB,SACE,MAAM,EAEN,KAAK,EAEL,SAAS,EAKT,MAAM,EACN,cAAc,sBACI;AACpB,SAAS,OAAO,qBAAoB;AACpC,SAAS,UAAU,EAAE,WAAW,qBAAoB;AACpD,SAAS,OAAO,uBAAsB;AACtC,SAAqB,OAAO,qBAAoB;AAChD,SAAS,GAAG,EAAE,KAAK,EAAa,MAAM,EAAc,OAAO,qBAAoB;AAE/E,SACE,iBAAiB,EACjB,gBAAgB,EAChB,cAAc,EACd,WAAW,qBACM;AAEnB,SAIE,QAAQ,wBACY;AAEtB,SAAS,GAAG,EAAE,KAAK,EAAE,MAAM,EAAa,SAAS,sBAAqB"}
package/mod.ts ADDED
@@ -0,0 +1,78 @@
1
+ /**
2
+ * @yaks/tools — a tool is a function that takes entity patches and returns
3
+ * entity patches; a `call` entity is the stored record of having asked for
4
+ * one. This package runs the function and keeps that record.
5
+ *
6
+ * A caller invokes a tool directly: `call()` writes the `call` entity, runs
7
+ * the function named by `call.to` with the call's bundles and a context
8
+ * carrying the caller's identity, and applies what the function returned
9
+ * together with a `result{call, ms}` entity whose id is derived from the rule
10
+ * that emits it. Nothing polls the graph — a call another process wrote, or
11
+ * one deferred by a `wake` component, is picked up by registering those same
12
+ * rules as effects (@yaks/effects `on`). See ./runner.ts for the claim, crash
13
+ * recovery, and scheduling.
14
+ *
15
+ * @module
16
+ */
17
+
18
+ export { callDoc, toolDoc, toolsDoc } from './vocab.js'
19
+ export {
20
+ ailing,
21
+ CHECK,
22
+ checked,
23
+ checks,
24
+ type Finding,
25
+ type Level,
26
+ } from './check.js'
27
+ export { CallError, parsed, unreferenced } from './args.js'
28
+ export { said } from './said.js'
29
+ export { persist } from './persist.js'
30
+ export {
31
+ answerOf,
32
+ faulted,
33
+ MOST,
34
+ pending,
35
+ READY,
36
+ RULES,
37
+ structured,
38
+ texts,
39
+ toolEid,
40
+ toolRow,
41
+ WOKEN,
42
+ worded,
43
+ WORDS,
44
+ } from './result.js'
45
+ export {
46
+ became,
47
+ type Invocation,
48
+ keeps,
49
+ type Opts,
50
+ reconcile,
51
+ type Refusal,
52
+ type Refused,
53
+ type Reply,
54
+ type Runner,
55
+ runner,
56
+ UnfinishedCall,
57
+ } from './runner.js'
58
+ export { valueIn } from './value.js'
59
+ export { actionable, exceptionOf } from './fault.js'
60
+ export { display } from './display.js'
61
+ export { type Watch, watched } from './watch.js'
62
+ export { did, doing, type Step, stepOf, type Steps, titleOf } from './title.js'
63
+
64
+ export {
65
+ executionComputed,
66
+ executionDerived,
67
+ executionState,
68
+ Interrupted,
69
+ } from './state.js'
70
+
71
+ export {
72
+ type Description,
73
+ type Provider,
74
+ type Registry,
75
+ registry,
76
+ } from './registry.js'
77
+
78
+ export { CUT, heard, invoke, type Land, refusalOf } from './invoke.js'