@yaks/host 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 (69) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +306 -2
  3. package/_dist/anatomy.d.ts +35 -0
  4. package/_dist/anatomy.d.ts.map +1 -0
  5. package/_dist/assembly.d.ts +94 -0
  6. package/_dist/assembly.d.ts.map +1 -0
  7. package/_dist/config.d.ts +150 -0
  8. package/_dist/config.d.ts.map +1 -0
  9. package/_dist/documents.d.ts +9 -0
  10. package/_dist/documents.d.ts.map +1 -0
  11. package/_dist/facets.d.ts +164 -0
  12. package/_dist/facets.d.ts.map +1 -0
  13. package/_dist/keepers.d.ts +15 -0
  14. package/_dist/keepers.d.ts.map +1 -0
  15. package/_dist/keywords.d.ts +3 -0
  16. package/_dist/keywords.d.ts.map +1 -0
  17. package/_dist/lending.d.ts +34 -0
  18. package/_dist/lending.d.ts.map +1 -0
  19. package/_dist/mod.d.ts +260 -0
  20. package/_dist/mod.d.ts.map +1 -0
  21. package/_dist/payments.d.ts +501 -0
  22. package/_dist/payments.d.ts.map +1 -0
  23. package/_dist/roles.d.ts +10 -0
  24. package/_dist/roles.d.ts.map +1 -0
  25. package/_dist/senders.d.ts +180 -0
  26. package/_dist/senders.d.ts.map +1 -0
  27. package/_dist/words.d.ts +27 -0
  28. package/_dist/words.d.ts.map +1 -0
  29. package/anatomy.js +432 -0
  30. package/anatomy.js.map +1 -0
  31. package/anatomy.ts +439 -0
  32. package/assembly.js +737 -0
  33. package/assembly.js.map +1 -0
  34. package/assembly.ts +930 -0
  35. package/config.js +149 -0
  36. package/config.js.map +1 -0
  37. package/config.ts +287 -0
  38. package/deno.json +1 -0
  39. package/documents.js +80 -0
  40. package/documents.js.map +1 -0
  41. package/documents.ts +105 -0
  42. package/facets.js +2 -0
  43. package/facets.js.map +1 -0
  44. package/facets.ts +236 -0
  45. package/keepers.js +7 -0
  46. package/keepers.js.map +1 -0
  47. package/keepers.ts +17 -0
  48. package/keywords.js +24 -0
  49. package/keywords.js.map +1 -0
  50. package/keywords.ts +25 -0
  51. package/lending.js +46 -0
  52. package/lending.js.map +1 -0
  53. package/lending.ts +77 -0
  54. package/mod.js +5 -0
  55. package/mod.js.map +1 -0
  56. package/mod.ts +356 -0
  57. package/package.json +79 -4
  58. package/payments.js +66 -0
  59. package/payments.js.map +1 -0
  60. package/payments.ts +665 -0
  61. package/roles.js +22 -0
  62. package/roles.js.map +1 -0
  63. package/roles.ts +18 -0
  64. package/senders.js +58 -0
  65. package/senders.js.map +1 -0
  66. package/senders.ts +272 -0
  67. package/words.js +134 -0
  68. package/words.js.map +1 -0
  69. package/words.ts +210 -0
package/config.js ADDED
@@ -0,0 +1,149 @@
1
+ // Composition configuration. A concrete host interprets its storage locator.
2
+ import { CallError } from "@yaks/tools/errors";
3
+ import { prefixes } from "@yaks/id";
4
+ /** How a config's store numbers its entities (@yaks/sqlite `Numbering`):
5
+ * not at all, or in the series of each letter a component declares (@yaks/id).
6
+ *
7
+ * ```ts
8
+ * import { numbering } from '@yaks/host/config'
9
+ * import { idKeywords } from '@yaks/id'
10
+ * import { loadVocab } from '@yaks/vocab'
11
+ * import { equal } from '@yaks/testing'
12
+ *
13
+ * const vocab = loadVocab({
14
+ * $defs: {
15
+ * task: { component: true, prefix: 'T', properties: {} },
16
+ * edge: { component: true, properties: {} },
17
+ * },
18
+ * }, [idKeywords])
19
+ * equal(numbering(vocab, undefined), false)
20
+ * equal(numbering(vocab, true), { task: 'T' })
21
+ * ```
22
+ */ export let numbering = (vocab, numbers)=>numbers ? prefixes(vocab) : false;
23
+ /** The package a plugin entry names. */ export let used = (plug)=>typeof plug == 'string' ? plug : plug.use;
24
+ /** The options passed to its factories. */ export let given = (plug)=>typeof plug == 'string' ? {} : plug.with ?? {};
25
+ /** The definitions selected from its vocabulary, or all when omitted. */ export let chosen = (plug)=>typeof plug == 'string' ? undefined : plug.words;
26
+ /** The facets selected for this host, or all when omitted. */ export let facetsOf = (plug)=>typeof plug == 'string' ? undefined : plug.facets;
27
+ // Only data is looked into: an instance a host passed in keeps its prototype.
28
+ let plain = (value)=>!!value && typeof value == 'object' && [
29
+ Object.prototype,
30
+ Array.prototype,
31
+ null
32
+ ].includes(Object.getPrototypeOf(value));
33
+ // The kind and name of a reference, `{"secret": "NAME"}`, when the object
34
+ // holds nothing else and the host reads that kind.
35
+ let reference = (value, reveal)=>{
36
+ if (!plain(value) || Array.isArray(value)) return;
37
+ let [entry, ...rest] = Object.entries(value);
38
+ if (!entry || rest.length || typeof entry[1] != 'string') return;
39
+ let read = reveal[entry[0]];
40
+ if (read) return ()=>read(entry[1]);
41
+ };
42
+ /**
43
+ * Options with each reference in them read by `reveal` at the moment it is
44
+ * accessed, so a value that arrives after the host started is read the next
45
+ * time. A reference whose kind the host does not read stays as written.
46
+ *
47
+ * ```ts
48
+ * import { revealed } from '@yaks/host/config'
49
+ * import { equal } from '@yaks/testing'
50
+ *
51
+ * let env: Record<string, unknown> = { TOKEN: 't' }
52
+ * let options = revealed(
53
+ * { token: { secret: 'TOKEN' }, mail: { binding: 'MAIL' }, base: 'x' },
54
+ * { secret: (name) => env[name] },
55
+ * ) as Record<string, unknown>
56
+ * equal(options, { token: 't', mail: { binding: 'MAIL' }, base: 'x' })
57
+ * env.TOKEN = 'u'
58
+ * equal(options.token, 'u')
59
+ * ```
60
+ */ export let revealed = (value, reveal)=>{
61
+ // A whole value written as a reference has no parent object to define a
62
+ // getter on, so it is read here, once.
63
+ let read = reference(value, reveal);
64
+ if (read) return read();
65
+ if (!plain(value)) return value;
66
+ let out = Array.isArray(value) ? [] : {};
67
+ for (let [k, v] of Object.entries(value)){
68
+ let later = reference(v, reveal);
69
+ if (later) {
70
+ Object.defineProperty(out, k, {
71
+ get: later,
72
+ enumerable: true,
73
+ configurable: true
74
+ });
75
+ } else out[k] = revealed(v, reveal);
76
+ }
77
+ return out;
78
+ };
79
+ /** A {@link Reveal} that reads every kind from one record by name: a Worker's
80
+ * env, which holds its secrets and its bindings alike. */ export let holding = (held)=>{
81
+ let read = (name)=>held[name];
82
+ return {
83
+ secret: read,
84
+ binding: read
85
+ };
86
+ };
87
+ /** Live bindings with a second source taking precedence for its own keys. */ export let overlaid = (base, extra)=>{
88
+ let source = (key)=>{
89
+ let top = extra();
90
+ return Object.hasOwn(top, key) ? top : base;
91
+ };
92
+ return new Proxy({}, {
93
+ get: (_, key)=>Reflect.get(source(key), key),
94
+ set: (_, key, value)=>Reflect.set(base, key, value),
95
+ has: (_, key)=>Reflect.has(source(key), key),
96
+ ownKeys: ()=>[
97
+ ...new Set([
98
+ ...Reflect.ownKeys(base),
99
+ ...Reflect.ownKeys(extra())
100
+ ])
101
+ ],
102
+ getOwnPropertyDescriptor: (_, key)=>{
103
+ let own = Reflect.getOwnPropertyDescriptor(source(key), key);
104
+ return own && {
105
+ ...own,
106
+ configurable: true
107
+ };
108
+ }
109
+ });
110
+ };
111
+ /** Each plugin a config names, with its options, each reference among them
112
+ * read by `reveal` when accessed. */ export let configured = (config, reveal = {})=>(config.plugins ?? []).map((plug)=>[
113
+ used(plug),
114
+ Object.keys(reveal).length ? revealed(given(plug), reveal) : given(plug),
115
+ chosen(plug),
116
+ facetsOf(plug)
117
+ ]);
118
+ /**
119
+ * Refuse a store whose data requires a plugin this config does not compose:
120
+ * one it does not list, or lists with its graph facet left out, which is where
121
+ * a plugin's writes are shaped. `requirements` is what the store recorded
122
+ * (@yaks/sqlite `requirements`), and the error names every one missing.
123
+ *
124
+ * ```ts
125
+ * import { meet } from '@yaks/host/config'
126
+ * import { throws } from '@yaks/testing'
127
+ *
128
+ * meet({ plugins: ['@yaks/blob'] }, ['@yaks/blob'])
129
+ * await throws(
130
+ * () => meet({ plugins: ['@yaks/doc'] }, ['@yaks/blob', '@yaks/journal']),
131
+ * 'requires @yaks/blob and @yaks/journal,',
132
+ * )
133
+ * ```
134
+ */ export let meet = (config, requirements)=>{
135
+ let composed = new Set((config.plugins ?? []).filter((p)=>facetsOf(p)?.includes('graph') ?? true).map(used));
136
+ let missing = requirements.filter((p)=>!composed.has(p));
137
+ if (missing.length) throw new Unmet(missing);
138
+ };
139
+ // `a`, `a and b`, `a, b and c`.
140
+ let listed = (names)=>names.length == 1 ? names[0] : `${names.slice(0, -1).join(', ')} and ${names.at(-1)}`;
141
+ /** A store requires plugins its opener's config does not compose. */ export class Unmet extends CallError {
142
+ plugins;
143
+ constructor(plugins){
144
+ let one = plugins.length == 1;
145
+ super('config', `this store requires ${listed(plugins)}, which this config does not ` + `compose: list ${one ? 'it' : 'them'} in plugins or, if ` + `${one ? 'it was' : 'they were'} removed on purpose, drop the ` + `store's ${one ? 'requirement' : 'requirements'} (@yaks/sqlite ` + 'REQUIRED)'), this.plugins = plugins;
146
+ this.name = 'Unmet';
147
+ }
148
+ }
149
+ //# sourceMappingURL=config.js.map
package/config.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sources":["./config.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAE7E,SAAS,SAAS,kCAAuC;AACzD,SAAS,QAAQ,wBAA6B;AA+F9C;;;;;;;;;;;;;;;;;;CAkBC,GACD,OAAO,IAAI,YAAY,CACrB,OACA,UACmC,UAAU,SAAS,SAAS,MAAK;AAEtE,sCAAsC,GACtC,OAAO,IAAI,OAAO,CAAC,OACjB,OAAO,QAAQ,WAAW,OAAO,KAAK,GAAG,CAAA;AAC3C,yCAAyC,GACzC,OAAO,IAAI,QAAQ,CAAC,OAClB,OAAO,QAAQ,WAAW,CAAC,IAAI,KAAK,IAAI,IAAI,CAAC,EAAC;AAChD,uEAAuE,GACvE,OAAO,IAAI,SAAS,CAAC,OACnB,OAAO,QAAQ,WAAW,YAAY,KAAK,KAAK,CAAA;AAElD,4DAA4D,GAC5D,OAAO,IAAI,WAAW,CAAC,OACrB,OAAO,QAAQ,WAAW,YAAY,KAAK,MAAM,CAAA;AAQnD,8EAA8E;AAC9E,IAAI,QAAQ,CAAC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,KAAK,CAAC,EAAE,GACR,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,MAAM,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC,IAAI;AAE5D,mEAAmE,GACnE,OAAO,MAAM,cAAc;;EACzB,YAAY,AAAO,OAAiB,CAAE;IACpC,IAAI,MAAM,QAAQ,MAAM,IAAI;IAC5B,KAAK,CACH,UACA,CAAC,oBAAoB,EAAE,OAAO,SAAS,6BAA6B,CAAC,GACnE,CAAC,cAAc,EAAE,MAAM,OAAO,OAAO,mBAAmB,CAAC,GACzD,GAAG,MAAM,WAAW,YAAY,8BAA8B,CAAC,GAC/D,CAAC,QAAQ,EAAE,MAAM,gBAAgB,eAAe,eAAe,CAAC,GAChE,mBARa,UAAA;IAUjB,IAAI,CAAC,IAAI,GAAG;EACd;AACF"}
package/config.ts ADDED
@@ -0,0 +1,287 @@
1
+ // Composition configuration. A concrete host interprets its storage locator.
2
+ import type { FacetName } from './facets.ts'
3
+ import { CallError } from '@yaks/tools/errors'
4
+ import { prefixes } from '@yaks/id'
5
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
6
+ /** The options a config passes to one plugin, given to each of that plugin's
7
+ * exported factories as the second argument, after the host. A value written
8
+ * `{"secret": "NAME"}` is that secret's value, read every time it is accessed
9
+ * ({@link revealed}), so a config names a secret without holding one — and
10
+ * a factory that re-reads its options picks up a secret written after the host
11
+ * started. A runtime that holds bindings lends one written `{"binding":
12
+ * "NAME"}` the same way. */
13
+ export type Options = Record<string, unknown>
14
+
15
+ /** A plugin a config names: a bare specifier, or one with options and chosen words. */
16
+ export type Plug = string | {
17
+ use: string
18
+ with?: Options
19
+ words?: string[]
20
+ /** The facets this host composes; omitted takes all exported facets. */
21
+ facets?: FacetName[]
22
+ }
23
+
24
+ /** A resolved plugin, its options and optional word and facet selections. */
25
+ export type Configured = [string, Options, string[]?, FacetName[]?]
26
+
27
+ /** A config file: where the graph lives, and what reads and writes it. */
28
+ export type Config = {
29
+ /** Systemd unit templates rolled by `yak restart`. A worker selects its
30
+ * entry with `yak work --process <unit>`; omitted roles takes all duties
31
+ * except those explicitly excluded. Optional entries roll only if active. */
32
+ processes?: {
33
+ unit: string
34
+ roles?: string[]
35
+ except?: string[]
36
+ optional?: boolean
37
+ }[]
38
+ /** Explicit connector bindings for file-and-command workspace targets. */
39
+ workspaces?: {
40
+ connectors: Record<string, { use: string; with?: Record<string, unknown> }>
41
+ receipts: string
42
+ }
43
+ /** Sandbox adapters, separate from computer connection transports. */
44
+ sandboxes?: import('jsr:@yaks/sandbox@0.2.36/lifecycle').Configuration & {
45
+ defaultProvider?: string
46
+ }
47
+ /** the SQLite file, or `:memory:` for a graph that lasts as long as the
48
+ * process. Required — a host never guesses a database. */
49
+ db?: string
50
+ /** Report to this durable spool without opening the tracker graph. */
51
+ tracker?: { spool: string; commit?: string }
52
+ /** the plugin packages, by import specifier. A relative specifier is
53
+ * resolved against the config file itself; `{use, with, words, facets}` names one
54
+ * with options and an optional list of its vocabulary definitions. Omitted
55
+ * `words` takes its whole vocabulary; `[]` takes none of its definitions.
56
+ * `facets` restricts which subpaths are imported; omitted takes all. */
57
+ plugins?: Plug[]
58
+ /** the kinds of store one host serves, each by its own config: a Worker
59
+ * whose one object class opens a recipe app's store and the platform's
60
+ * directory alike gives each kind its own plugins. A store reads its
61
+ * kind's; the host's own `plugins` are what serves them. */
62
+ stores?: Record<string, Config>
63
+ /** what `yak serve` listens on (default @yaks/api's `PORT`) */
64
+ port?: number
65
+ /** which interface it binds (default @yaks/api's `HOSTNAME`, this machine
66
+ * alone; `0.0.0.0` offers it to every network the machine is on) */
67
+ hostname?: string
68
+ /** whether the store mints a short human-readable number beside each entity
69
+ * id, in the series of the letter its component declares (@yaks/id). Opt-in:
70
+ * left out, no entity gets one, because a number exists for a person to type
71
+ * and most hosts have nobody typing. */
72
+ numbers?: boolean
73
+ /** what the MCP server calls itself (default `yak`) */
74
+ name?: string
75
+ /** the person who works at this machine, as any id the graph resolves (an
76
+ * eid, a human id, a name). What they type here is signed with them: a
77
+ * prompt their harness's transcript marks as typed (@yaks/session
78
+ * `service`), a line typed into the harness's terminal (@yaks/harness), and
79
+ * a command line run at a terminal that names no session (./local.ts
80
+ * `signer`). Left out, nothing is signed as a person: a machine never
81
+ * guesses who is at its keyboard (./host.ts `person`). */
82
+ person?: string
83
+ /** how long this process's lease on a duty, or its claim on an effect
84
+ * run, stands before another process may take it over, in milliseconds
85
+ * (default 30_000 for a duty, 60_000 for a run). A holder still doing the
86
+ * work renews it on a timer; one that was killed leaves a lease that
87
+ * expires, which is how a second long-running process takes over without
88
+ * anybody having to reap the first. */
89
+ lease?: number
90
+ /** whether this process runs its duties: the effect pool, each plugin's
91
+ * `./service`, and the start-up passes a plugin holds a lease for (default
92
+ * true). `false` takes no lease, works no effects and runs none of them, so
93
+ * the process answers what it is asked and nothing else — what its writes
94
+ * owe is left written down for a process that does — what `yak --no-duties`
95
+ * sets. */
96
+ duties?: boolean
97
+ }
98
+
99
+ /** How a config's store numbers its entities (@yaks/sqlite `Numbering`):
100
+ * not at all, or in the series of each letter a component declares (@yaks/id).
101
+ *
102
+ * ```ts
103
+ * import { numbering } from '@yaks/host/config'
104
+ * import { idKeywords } from '@yaks/id'
105
+ * import { loadVocab } from '@yaks/vocab'
106
+ * import { equal } from '@yaks/testing'
107
+ *
108
+ * const vocab = loadVocab({
109
+ * $defs: {
110
+ * task: { component: true, prefix: 'T', properties: {} },
111
+ * edge: { component: true, properties: {} },
112
+ * },
113
+ * }, [idKeywords])
114
+ * equal(numbering(vocab, undefined), false)
115
+ * equal(numbering(vocab, true), { task: 'T' })
116
+ * ```
117
+ */
118
+ export let numbering = (
119
+ vocab: Vocab,
120
+ numbers: Config['numbers'],
121
+ ): false | Record<string, string> => numbers ? prefixes(vocab) : false
122
+
123
+ /** The package a plugin entry names. */
124
+ export let used = (plug: Plug): string =>
125
+ typeof plug == 'string' ? plug : plug.use
126
+ /** The options passed to its factories. */
127
+ export let given = (plug: Plug): Options =>
128
+ typeof plug == 'string' ? {} : plug.with ?? {}
129
+ /** The definitions selected from its vocabulary, or all when omitted. */
130
+ export let chosen = (plug: Plug): string[] | undefined =>
131
+ typeof plug == 'string' ? undefined : plug.words
132
+
133
+ /** The facets selected for this host, or all when omitted. */
134
+ export let facetsOf = (plug: Plug): FacetName[] | undefined =>
135
+ typeof plug == 'string' ? undefined : plug.facets
136
+
137
+ /** How a host reads what a config names without holding: each kind of
138
+ * reference (`secret`, `binding`) it can read, by the name written in it. */
139
+ export type Reveal = Partial<
140
+ Record<'secret' | 'binding', (name: string) => unknown>
141
+ >
142
+
143
+ // Only data is looked into: an instance a host passed in keeps its prototype.
144
+ let plain = (value: unknown): value is object =>
145
+ !!value && typeof value == 'object' &&
146
+ [Object.prototype, Array.prototype, null].includes(
147
+ Object.getPrototypeOf(value),
148
+ )
149
+
150
+ // The kind and name of a reference, `{"secret": "NAME"}`, when the object
151
+ // holds nothing else and the host reads that kind.
152
+ let reference = (value: unknown, reveal: Reveal) => {
153
+ if (!plain(value) || Array.isArray(value)) return
154
+ let [entry, ...rest] = Object.entries(value)
155
+ if (!entry || rest.length || typeof entry[1] != 'string') return
156
+ let read = reveal[entry[0] as keyof Reveal]
157
+ if (read) return () => read(entry[1] as string)
158
+ }
159
+
160
+ /**
161
+ * Options with each reference in them read by `reveal` at the moment it is
162
+ * accessed, so a value that arrives after the host started is read the next
163
+ * time. A reference whose kind the host does not read stays as written.
164
+ *
165
+ * ```ts
166
+ * import { revealed } from '@yaks/host/config'
167
+ * import { equal } from '@yaks/testing'
168
+ *
169
+ * let env: Record<string, unknown> = { TOKEN: 't' }
170
+ * let options = revealed(
171
+ * { token: { secret: 'TOKEN' }, mail: { binding: 'MAIL' }, base: 'x' },
172
+ * { secret: (name) => env[name] },
173
+ * ) as Record<string, unknown>
174
+ * equal(options, { token: 't', mail: { binding: 'MAIL' }, base: 'x' })
175
+ * env.TOKEN = 'u'
176
+ * equal(options.token, 'u')
177
+ * ```
178
+ */
179
+ export let revealed = (value: unknown, reveal: Reveal): unknown => {
180
+ // A whole value written as a reference has no parent object to define a
181
+ // getter on, so it is read here, once.
182
+ let read = reference(value, reveal)
183
+ if (read) return read()
184
+ if (!plain(value)) return value
185
+ let out = (Array.isArray(value) ? [] : {}) as Record<string, unknown>
186
+ for (let [k, v] of Object.entries(value)) {
187
+ let later = reference(v, reveal)
188
+ if (later) {
189
+ Object.defineProperty(out, k, {
190
+ get: later,
191
+ enumerable: true,
192
+ configurable: true,
193
+ })
194
+ } else out[k] = revealed(v, reveal)
195
+ }
196
+ return out
197
+ }
198
+
199
+ /** A {@link Reveal} that reads every kind from one record by name: a Worker's
200
+ * env, which holds its secrets and its bindings alike. */
201
+ export let holding = (held: object): Reveal => {
202
+ let read = (name: string) => (held as Record<string, unknown>)[name]
203
+ return { secret: read, binding: read }
204
+ }
205
+
206
+ /** Live bindings with a second source taking precedence for its own keys. */
207
+ export let overlaid = <Base extends object, Extra extends object>(
208
+ base: Base,
209
+ extra: () => Extra,
210
+ ): Base & Extra => {
211
+ let source = (key: PropertyKey) => {
212
+ let top = extra()
213
+ return Object.hasOwn(top, key) ? top : base
214
+ }
215
+ return new Proxy({} as Base & Extra, {
216
+ get: (_, key) => Reflect.get(source(key), key),
217
+ set: (_, key, value) => Reflect.set(base, key, value),
218
+ has: (_, key) => Reflect.has(source(key), key),
219
+ ownKeys: () => [
220
+ ...new Set([...Reflect.ownKeys(base), ...Reflect.ownKeys(extra())]),
221
+ ],
222
+ getOwnPropertyDescriptor: (_, key) => {
223
+ let own = Reflect.getOwnPropertyDescriptor(source(key), key)
224
+ return own && { ...own, configurable: true }
225
+ },
226
+ })
227
+ }
228
+
229
+ /** Each plugin a config names, with its options, each reference among them
230
+ * read by `reveal` when accessed. */
231
+ export let configured = (config: Config, reveal: Reveal = {}): Configured[] =>
232
+ (config.plugins ?? []).map((plug) => [
233
+ used(plug),
234
+ (Object.keys(reveal).length
235
+ ? revealed(given(plug), reveal)
236
+ : given(plug)) as Options,
237
+ chosen(plug),
238
+ facetsOf(plug),
239
+ ])
240
+
241
+ /**
242
+ * Refuse a store whose data requires a plugin this config does not compose:
243
+ * one it does not list, or lists with its graph facet left out, which is where
244
+ * a plugin's writes are shaped. `requirements` is what the store recorded
245
+ * (@yaks/sqlite `requirements`), and the error names every one missing.
246
+ *
247
+ * ```ts
248
+ * import { meet } from '@yaks/host/config'
249
+ * import { throws } from '@yaks/testing'
250
+ *
251
+ * meet({ plugins: ['@yaks/blob'] }, ['@yaks/blob'])
252
+ * await throws(
253
+ * () => meet({ plugins: ['@yaks/doc'] }, ['@yaks/blob', '@yaks/journal']),
254
+ * 'requires @yaks/blob and @yaks/journal,',
255
+ * )
256
+ * ```
257
+ */
258
+ export let meet = (config: Config, requirements: readonly string[]): void => {
259
+ let composed = new Set(
260
+ (config.plugins ?? []).filter((p) => facetsOf(p)?.includes('graph') ?? true)
261
+ .map(used),
262
+ )
263
+ let missing = requirements.filter((p) => !composed.has(p))
264
+ if (missing.length) throw new Unmet(missing)
265
+ }
266
+
267
+ // `a`, `a and b`, `a, b and c`.
268
+ let listed = (names: string[]) =>
269
+ names.length == 1
270
+ ? names[0]
271
+ : `${names.slice(0, -1).join(', ')} and ${names.at(-1)}`
272
+
273
+ /** A store requires plugins its opener's config does not compose. */
274
+ export class Unmet extends CallError {
275
+ constructor(public plugins: string[]) {
276
+ let one = plugins.length == 1
277
+ super(
278
+ 'config',
279
+ `this store requires ${listed(plugins)}, which this config does not ` +
280
+ `compose: list ${one ? 'it' : 'them'} in plugins or, if ` +
281
+ `${one ? 'it was' : 'they were'} removed on purpose, drop the ` +
282
+ `store's ${one ? 'requirement' : 'requirements'} (@yaks/sqlite ` +
283
+ 'REQUIRED)',
284
+ )
285
+ this.name = 'Unmet'
286
+ }
287
+ }
package/deno.json ADDED
@@ -0,0 +1 @@
1
+ {"publish":{"exclude":["**/*_test.ts"]},"name":"@yaks/host","version":"0.2.36","license":"Apache-2.0","description":"The portable capabilities a host lends the plugins it composes.","exports":{".":"./mod.ts","./assembly":"./assembly.ts","./roles":"./roles.ts","./senders":"./senders.ts","./keepers":"./keepers.ts","./words":"./words.ts","./config":"./config.ts","./documents":"./documents.ts","./keywords":"./keywords.ts","./anatomy":"./anatomy.ts","./lending":"./lending.ts","./payments":"./payments.ts"},"imports":{"@yaks/host":"./mod.ts","@yaks/host/assembly":"./assembly.ts","@yaks/host/roles":"./roles.ts","@yaks/host/senders":"./senders.ts","@yaks/host/keepers":"./keepers.ts","@yaks/host/words":"./words.ts","@yaks/host/config":"./config.ts","@yaks/host/documents":"./documents.ts","@yaks/host/keywords":"./keywords.ts","@yaks/host/anatomy":"./anatomy.ts","@yaks/host/lending":"./lending.ts","@yaks/host/payments":"./payments.ts"}}
package/documents.js ADDED
@@ -0,0 +1,80 @@
1
+ // The plugins' declarations, plus the components a tool call is recorded in
2
+ // where no plugin declared them. Declaring one component twice is an error
3
+ // (@yaks/vocab), and rightly — two definitions of one component is not
4
+ // something to guess about — so what is added here is only the difference,
5
+ // never a second copy.
6
+ import { description as toolsAbout, toolsDoc } from "@yaks/tools/vocab";
7
+ import { docs as machineDocs } from "@yaks/machine/vocab";
8
+ import { docs as fileDocs } from "@yaks/file/vocab";
9
+ import { gitDoc } from "@yaks/git/vocab";
10
+ import { idOf } from "@yaks/vocab";
11
+ import { narrow } from "@yaks/vocab/runtime";
12
+ /** Select definitions without changing their schemas or document metadata.
13
+ * Every name must exist: a misspelled selection must not silently lose words.
14
+ * A kind a document declares of another document's component is kept with it
15
+ * (@yaks/vocab `narrow`), since no pick of this plugin's words names it. */ export let selected = (docs, names, plugin)=>{
16
+ if (!names) return docs;
17
+ let known = new Set(docs.flatMap((d)=>Object.keys(d.$defs ?? {})));
18
+ for (let name of names){
19
+ if (!known.has(name)) {
20
+ throw new Error(`${plugin} declares no word '${name}'`);
21
+ }
22
+ }
23
+ let wanted = new Set(names);
24
+ return docs.map((d)=>narrow(d, (name)=>wanted.has(name))).filter((d)=>Object.keys(d.$defs).length || d.kinds);
25
+ };
26
+ /** A graph's own component declarations replace the plugins' declarations
27
+ * and extensions of those components. Its extensions still add to plugins. */ export let beneath = (docs, own)=>{
28
+ let mine = new Map(own.flatMap((d)=>Object.entries(d.$defs ?? {}).filter(([, s])=>s.component === true && s.extends !== true).map(([name, schema])=>[
29
+ name,
30
+ {
31
+ doc: d,
32
+ schema
33
+ }
34
+ ])));
35
+ let replaced = new Set();
36
+ return docs.map((d)=>narrow(d, (name)=>{
37
+ let s = d.$defs[name];
38
+ let local = mine.get(name);
39
+ if (!local || s.component !== true) return true;
40
+ let here = idOf(s.$id), there = idOf(local.schema.$id);
41
+ if (here && there && here !== there && d.label && local.doc.label && d.label !== local.doc.label) return true;
42
+ // A shadow cannot conceal two plugins declaring the same component.
43
+ // The vocabulary loader checks every declaration we retain.
44
+ if (s.extends !== true) {
45
+ if (replaced.has(name)) {
46
+ throw new Error(`component '${name}' is declared twice`);
47
+ }
48
+ replaced.add(name);
49
+ }
50
+ return false;
51
+ })).filter((d)=>Object.keys(d.$defs).length);
52
+ };
53
+ export let docsOf = (docs)=>{
54
+ let supplied = new Set(docs.flatMap((d)=>Object.keys(d.$defs ?? {})));
55
+ // Hosts lend machines and the files on them; their durable records and commit
56
+ // references are host words, even when no plugin separately contributes
57
+ // these vocabulary facets.
58
+ let effects = new Set(docs.flatMap((doc)=>Object.keys(doc.$defs ?? {}).filter((name)=>doc.$defs[name].effect === true)));
59
+ let intrinsic = [
60
+ ...machineDocs,
61
+ ...fileDocs,
62
+ gitDoc
63
+ ].map((doc)=>narrow(doc, (name)=>doc.$defs[name].effect === true ? !effects.has(name) : !supplied.has(name)));
64
+ docs = [
65
+ ...intrinsic.filter((doc)=>Object.keys(doc.$defs).length),
66
+ ...docs
67
+ ];
68
+ let taken = new Set(docs.flatMap((d)=>Object.keys(d.$defs ?? {})));
69
+ let $defs = Object.fromEntries(Object.entries(toolsDoc.$defs ?? {}).filter(([name])=>!taken.has(name)));
70
+ return Object.keys($defs).length ? [
71
+ {
72
+ title: 'invocation',
73
+ label: '@yaks/tools',
74
+ description: toolsAbout,
75
+ $defs
76
+ },
77
+ ...docs
78
+ ] : docs;
79
+ };
80
+ //# sourceMappingURL=documents.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"documents.js","sources":["./documents.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,2EAA2E;AAC3E,uEAAuE;AACvE,2EAA2E;AAC3E,uBAAuB;AACvB,SAAS,eAAe,UAAU,EAAE,QAAQ,iCAAsC;AAClF,SAAS,QAAQ,WAAW,mCAAwC;AACpE,SAAS,QAAQ,QAAQ,gCAAqC;AAC9D,SAAS,MAAM,+BAAoC;AACnD,SAAS,IAAI,2BAAgC;AAC7C,SAAS,MAAM,mCAAuD;AAEtE;;;0EAG0E,GAC1E,OAAO,IAAI,WAAW,CACpB,MACA,OACA;EAEA,IAAI,CAAC,OAAO,OAAO;EACnB,IAAI,QAAQ,IAAI,IAAI,KAAK,OAAO,CAAC,CAAC,IAAM,OAAO,IAAI,CAAC,EAAE,KAAK,IAAI,CAAC;EAChE,KAAK,IAAI,QAAQ,MAAO;IACtB,IAAI,CAAC,MAAM,GAAG,CAAC,OAAO;MACpB,MAAM,IAAI,MAAM,GAAG,OAAO,mBAAmB,EAAE,KAAK,CAAC,CAAC;IACxD;EACF;EACA,IAAI,SAAS,IAAI,IAAI;EACrB,OAAO,KAAK,GAAG,CAAC,CAAC,IAAM,OAAO,GAAG,CAAC,OAAS,OAAO,GAAG,CAAC,QACnD,MAAM,CAAC,CAAC,IAAM,OAAO,IAAI,CAAC,EAAE,KAAK,EAAG,MAAM,IAAI,EAAE,KAAK;AAC1D,EAAC;AAED;4EAC4E,GAC5E,OAAO,IAAI,UAAU,CAAC,MAAkB;EACtC,IAAI,OAAO,IAAI,IACb,IAAI,OAAO,CAAC,CAAC,IACX,OAAO,OAAO,CAAC,EAAE,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,E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AAM,KAAK;EAIlC,IAAI,YAAY;OAAI;OAAgB;IAAU;GAAO,CAAC,GAAG,CAAC,CAAC,MACzD,OACE,KACA,CAAC,OACC,IAAI,KAAK,AAAC,CAAC,KAAK,CAAC,MAAM,KAAK,OACxB,CAAC,QAAQ,GAAG,CAAC,QACb,CAAC,SAAS,GAAG,CAAC;EAGxB,OAAO;OACF,UAAU,MAAM,CAAC,CAAC,MAAQ,OAAO,IAAI,CAAC,IAAI,KAAK,EAAG,MAAM;OACxD;GACJ;EACD,IAAI,QAAQ,IAAI,IAAI,KAAK,OAAO,CAAC,CAAC,IAAM,OAAO,IAAI,CAAC,EAAE,KAAK,IAAI,CAAC;EAChE,IAAI,QAAQ,OAAO,WAAW,CAC5B,OAAO,OAAO,CAAC,SAAS,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,GAAK,CAAC,MAAM,GAAG,CAAC;EAErE,OAAO,OAAO,IAAI,CAAC,OAAO,MAAM,GAC5B;IAAC;MACD,OAAO;MACP,OAAO;MACP,aAAa;MACb;IACF;OAAM;GAAK,GACT;AACN,EAAC"}
package/documents.ts ADDED
@@ -0,0 +1,105 @@
1
+ // The plugins' declarations, plus the components a tool call is recorded in
2
+ // where no plugin declared them. Declaring one component twice is an error
3
+ // (@yaks/vocab), and rightly — two definitions of one component is not
4
+ // something to guess about — so what is added here is only the difference,
5
+ // never a second copy.
6
+ import { description as toolsAbout, toolsDoc } from '@yaks/tools/vocab'
7
+ import { docs as machineDocs } from '@yaks/machine/vocab'
8
+ import { docs as fileDocs } from '@yaks/file/vocab'
9
+ import { gitDoc } from '@yaks/git/vocab'
10
+ import { idOf } from '@yaks/vocab'
11
+ import { narrow, type VocabDoc } from '@yaks/vocab/runtime'
12
+
13
+ /** Select definitions without changing their schemas or document metadata.
14
+ * Every name must exist: a misspelled selection must not silently lose words.
15
+ * A kind a document declares of another document's component is kept with it
16
+ * (@yaks/vocab `narrow`), since no pick of this plugin's words names it. */
17
+ export let selected = (
18
+ docs: VocabDoc[],
19
+ names: string[] | undefined,
20
+ plugin: string,
21
+ ): VocabDoc[] => {
22
+ if (!names) return docs
23
+ let known = new Set(docs.flatMap((d) => Object.keys(d.$defs ?? {})))
24
+ for (let name of names) {
25
+ if (!known.has(name)) {
26
+ throw new Error(`${plugin} declares no word '${name}'`)
27
+ }
28
+ }
29
+ let wanted = new Set(names)
30
+ return docs.map((d) => narrow(d, (name) => wanted.has(name)))
31
+ .filter((d) => Object.keys(d.$defs!).length || d.kinds)
32
+ }
33
+
34
+ /** A graph's own component declarations replace the plugins' declarations
35
+ * and extensions of those components. Its extensions still add to plugins. */
36
+ export let beneath = (docs: VocabDoc[], own: VocabDoc[]): VocabDoc[] => {
37
+ let mine = new Map(
38
+ own.flatMap((d) =>
39
+ Object.entries(d.$defs ?? {}).filter(([, s]) =>
40
+ s.component === true && s.extends !== true
41
+ ).map(([name, schema]) => [name, { doc: d, schema }] as const)
42
+ ),
43
+ )
44
+ let replaced = new Set<string>()
45
+ return docs.map((d) =>
46
+ narrow(d, (name) => {
47
+ let s = d.$defs![name]
48
+ let local = mine.get(name)
49
+ if (!local || s.component !== true) return true
50
+ let here = idOf(s.$id), there = idOf(local.schema.$id)
51
+ if (
52
+ here && there && here !== there && d.label && local.doc.label &&
53
+ d.label !== local.doc.label
54
+ ) return true
55
+ // A shadow cannot conceal two plugins declaring the same component.
56
+ // The vocabulary loader checks every declaration we retain.
57
+ if (s.extends !== true) {
58
+ if (replaced.has(name)) {
59
+ throw new Error(`component '${name}' is declared twice`)
60
+ }
61
+ replaced.add(name)
62
+ }
63
+ return false
64
+ })
65
+ ).filter((d) => Object.keys(d.$defs!).length)
66
+ }
67
+
68
+ export let docsOf = (docs: VocabDoc[]): VocabDoc[] => {
69
+ let supplied = new Set(docs.flatMap((d) => Object.keys(d.$defs ?? {})))
70
+ // Hosts lend machines and the files on them; their durable records and commit
71
+ // references are host words, even when no plugin separately contributes
72
+ // these vocabulary facets.
73
+ let effects = new Set(
74
+ docs.flatMap((doc) =>
75
+ Object.keys(doc.$defs ?? {}).filter((name) =>
76
+ doc.$defs![name].effect === true
77
+ )
78
+ ),
79
+ )
80
+ let intrinsic = [...machineDocs, ...fileDocs, gitDoc].map((doc) =>
81
+ narrow(
82
+ doc,
83
+ (name) =>
84
+ doc.$defs![name].effect === true
85
+ ? !effects.has(name)
86
+ : !supplied.has(name),
87
+ )
88
+ )
89
+ docs = [
90
+ ...intrinsic.filter((doc) => Object.keys(doc.$defs!).length),
91
+ ...docs,
92
+ ]
93
+ let taken = new Set(docs.flatMap((d) => Object.keys(d.$defs ?? {})))
94
+ let $defs = Object.fromEntries(
95
+ Object.entries(toolsDoc.$defs ?? {}).filter(([name]) => !taken.has(name)),
96
+ )
97
+ return Object.keys($defs).length
98
+ ? [{
99
+ title: 'invocation',
100
+ label: '@yaks/tools',
101
+ description: toolsAbout,
102
+ $defs,
103
+ }, ...docs]
104
+ : docs
105
+ }
package/facets.js ADDED
@@ -0,0 +1,2 @@
1
+ /** The subpaths a plugin contributes to a composed graph. */
2
+ //# sourceMappingURL=facets.js.map
package/facets.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"facets.js","sources":["./facets.ts"],"names":[],"mappings":"AAAA,2DAA2D,GAC3D"}