@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/anatomy.ts ADDED
@@ -0,0 +1,439 @@
1
+ /** Per-composition evidence, separate from the shared vocabulary cache. */
2
+ import { groupBy } from '@yaks/fp'
3
+ import {
4
+ type Anatomy,
5
+ anatomy,
6
+ anatomyData,
7
+ anatomyDocuments,
8
+ type AnatomyObservation,
9
+ anatomyPlugin,
10
+ type AnatomySeed,
11
+ type AnatomySource,
12
+ } from '@yaks/code/anatomy'
13
+ import type { NamedTool, Plugin } from 'jsr:@yaks/graph@0.2.36'
14
+ import type { Vocab, VocabDoc } from 'jsr:@yaks/vocab@0.2.36'
15
+
16
+ // Options have not been revealed yet. Inspect data descriptors only: a getter
17
+ // on resolved options is a vault read, not a secret-name declaration.
18
+ export let secretNames = (
19
+ source: unknown,
20
+ seen: Set<object> = new Set<object>(),
21
+ ): string[] => {
22
+ if (!source || typeof source != 'object' || seen.has(source)) return []
23
+ seen.add(source)
24
+ let ds = Object.getOwnPropertyDescriptors(source)
25
+ let keys = Object.keys(ds).filter((k) => ds[k].enumerable)
26
+ let secret = ds.secret
27
+ if (
28
+ keys.length == 1 && keys[0] == 'secret' && secret && 'value' in secret &&
29
+ typeof secret.value == 'string'
30
+ ) return [secret.value]
31
+ return keys.flatMap((k) =>
32
+ 'value' in ds[k] ? secretNames(ds[k].value, seen) : []
33
+ )
34
+ }
35
+
36
+ type Facet = AnatomySeed<Anatomy['facets'][number]>
37
+ type Tool = AnatomySeed<Anatomy['tools'][number]>
38
+ let hints = [
39
+ 'title',
40
+ 'options',
41
+ 'readOnly',
42
+ 'destructive',
43
+ 'idempotent',
44
+ 'openWorld',
45
+ 'surfaces',
46
+ 'roles',
47
+ ]
48
+
49
+ /** Evidence gathered during one composition, projected only when read. */
50
+ export type Composition = {
51
+ read: () => Anatomy
52
+ observe: (observation: AnatomyObservation) => void
53
+ attempted: (owner: string, name: string) => void
54
+ loaded: (owner: string, name: string, present: boolean) => void
55
+ binding: (owner: string, name: string) => void
56
+ declarations: (docs: VocabDoc[], vocab: Vocab) => void
57
+ graph: (owner: string, plugins: Plugin[]) => void
58
+ graphed: () => void
59
+ tier: (tools: NamedTool[], owner?: string) => void
60
+ runs: (owner: string, names: string[], joined: string[]) => void
61
+ effects: (handlers: Map<string, string>, serving: boolean) => void
62
+ due: (names: string[]) => void
63
+ routes: (owner: string, routes: { method: string; path: string }[]) => void
64
+ }
65
+
66
+ /** Capture only already-made contributions; none of these methods runs code.
67
+ * What a composition observes is kept as it arrives and applied when its
68
+ * anatomy is read: projecting the vocabulary's declarations is tens of
69
+ * milliseconds of every command, and almost no command reads its anatomy.
70
+ * Package descriptors carry names and allowed facets, never resolved options.
71
+ * Repeated entries select the union of their facets. */
72
+ export let compositionAnatomy = (
73
+ packages: { name: string; facets?: readonly string[] }[],
74
+ roles: readonly string[],
75
+ common: Record<string, readonly string[]>,
76
+ secrets: { name: string; package: string }[] = [],
77
+ where: Pick<AnatomySource, 'host' | 'scope'> = {
78
+ host: 'native',
79
+ scope: 'native',
80
+ },
81
+ ): Composition => {
82
+ let byPackage = groupBy(packages, (p) => p.name)
83
+ let names = [...byPackage.keys()]
84
+ let source: AnatomySource = {
85
+ ...where,
86
+ observed: {
87
+ packages: true,
88
+ roles: true,
89
+ facets: true,
90
+ comps: true,
91
+ tools: true,
92
+ effects: true,
93
+ rules: true,
94
+ hooks: true,
95
+ routes: true,
96
+ secrets: true,
97
+ commands: false,
98
+ views: false,
99
+ inspectViews: false,
100
+ tui: false,
101
+ kits: false,
102
+ themes: false,
103
+ skills: false,
104
+ },
105
+ packages: names.map((name) => ({
106
+ name,
107
+ configured: true,
108
+ declared: true,
109
+ })),
110
+ roles: [...Object.keys(common), ...names].map((
111
+ name,
112
+ ) => ({
113
+ name,
114
+ ...common[name] ? {} : { package: name },
115
+ facets: [...(common[name] ?? ['service'])],
116
+ declared: true,
117
+ loaded: roles.includes(name),
118
+ bound: roles.includes(name),
119
+ })),
120
+ secrets: secrets.map((s) => ({ ...s, declared: true })),
121
+ }
122
+ let facets = new Map<string, Facet>()
123
+ let forFacet = (owner: string, name: string): Facet => {
124
+ let key = `${owner}/${name}`
125
+ let had = facets.get(key)
126
+ if (had) return had
127
+ let configured = byPackage.get(owner) ?? []
128
+ let allowed = !configured.length ||
129
+ configured.some((p) => p.facets === undefined || p.facets.includes(name))
130
+ let selected = allowed && (
131
+ Object.entries(common).some(([r, fs]) =>
132
+ roles.includes(r) && fs.includes(name)
133
+ ) ||
134
+ (name == 'service' && roles.includes(owner))
135
+ )
136
+ let made: Facet = { name, package: owner, selected, attempted: false }
137
+ facets.set(key, made)
138
+ return made
139
+ }
140
+ for (let owner of names) {
141
+ for (
142
+ let name of [
143
+ 'vocab',
144
+ 'graph',
145
+ 'tools',
146
+ 'effects',
147
+ 'routes',
148
+ 'ui',
149
+ 'service',
150
+ 'cli',
151
+ 'views',
152
+ 'tui',
153
+ ]
154
+ ) {
155
+ forFacet(owner, name)
156
+ }
157
+ }
158
+ let registered = new Set<string>()
159
+ let vocabulary = new Set<string>()
160
+ let ownerOf = (name: string) => {
161
+ let had = source.packages!.find((p) => p.name == name)
162
+ if (had) return had
163
+ let made = {
164
+ name,
165
+ configured: false,
166
+ declared: true,
167
+ loaded: false,
168
+ bound: false,
169
+ }
170
+ source.packages!.push(made)
171
+ return made
172
+ }
173
+ let binding = (owner: string, name: string) => {
174
+ let f = forFacet(owner, name)
175
+ f.declared = f.loaded = f.bound = true
176
+ let p = ownerOf(owner)
177
+ p.loaded = p.bound = true
178
+ }
179
+ let readValue = (input: unknown, name: string): unknown => {
180
+ if (!input || typeof input != 'object') return undefined
181
+ let d = Object.getOwnPropertyDescriptor(input, name)
182
+ return d && 'value' in d ? d.value : undefined
183
+ }
184
+ let entries = (input: unknown): [string, unknown][] => {
185
+ if (!input || typeof input != 'object') return []
186
+ return Object.entries(Object.getOwnPropertyDescriptors(input))
187
+ .filter(([, d]) => d.enumerable && 'value' in d)
188
+ .map(([k, d]) => [k, d.value])
189
+ }
190
+ let values = (input: unknown): unknown[] =>
191
+ Array.isArray(input) ? entries(input).map(([, v]) => v) : []
192
+ let observe = (o: AnatomyObservation) => {
193
+ let f = forFacet(o.package, o.facet)
194
+ f.attempted = true
195
+ f.loaded = o.loaded
196
+ if (!o.loaded) return
197
+ f.declared = true
198
+ ownerOf(o.package).loaded = true
199
+ if (o.bound) binding(o.package, o.facet)
200
+ let common = {
201
+ package: o.package,
202
+ facet: o.facet,
203
+ declared: true,
204
+ loaded: true,
205
+ bound: o.bound,
206
+ }
207
+ if (o.facet == 'ui') {
208
+ for (let group of ['kits', 'themes'] as const) {
209
+ source.observed![group] = true
210
+ source[group] = [
211
+ ...source[group]?.filter((p) => p.package != o.package) ?? [],
212
+ ...entries(readValue(o.value, group)).map(([name]) => ({
213
+ ...common,
214
+ name,
215
+ })),
216
+ ]
217
+ }
218
+ return
219
+ }
220
+ if (o.facet == 'cli') {
221
+ source.observed!.commands = true
222
+ source.commands = [
223
+ ...source.commands?.filter((p) => p.package != o.package) ?? [],
224
+ ...values(readValue(o.value, 'commands')).flatMap((c, i) => {
225
+ let name = readValue(c, 'name')
226
+ if (typeof name != 'string') return []
227
+ let description = readValue(c, 'description')
228
+ return [{
229
+ ...common,
230
+ name,
231
+ key: String(i),
232
+ schema: anatomyData(readValue(c, 'inputSchema')),
233
+ ...typeof description == 'string' ? { description } : {},
234
+ }]
235
+ }),
236
+ ]
237
+ return
238
+ }
239
+ for (
240
+ let group of o.facet == 'tui'
241
+ ? ['tui'] as const
242
+ : ['views', 'inspectViews'] as const
243
+ ) {
244
+ let raw = group == 'inspectViews'
245
+ ? readValue(o.value, 'inspectViews')
246
+ : readValue(readValue(o.value, 'views'), 'renderers')
247
+ // A module without inspectViews is not evidence about its page's own
248
+ // curated registry; an exported empty list is evidence of no additions.
249
+ if (raw === undefined || typeof raw == 'function') continue
250
+ source.observed![group] = true
251
+ source[group] = [
252
+ ...source[group]?.filter((p) => p.package != o.package) ?? [],
253
+ ...values(raw).flatMap((r, i) => {
254
+ let name = readValue(r, 'view')
255
+ if (typeof name != 'string') return []
256
+ return [{ ...common, name, key: String(i) }]
257
+ }),
258
+ ]
259
+ }
260
+ }
261
+ let declarations = (docs: VocabDoc[], vocab: Vocab) => {
262
+ let projected = anatomyDocuments(docs)
263
+ source.comps = projected.comps
264
+ source.tools = projected.tools
265
+ source.effects = projected.effects
266
+ source.rules = projected.rules
267
+ for (let doc of docs) {
268
+ if (!doc.label) continue
269
+ vocabulary.add(doc.label)
270
+ let f = forFacet(doc.label, 'vocab')
271
+ f.declared = f.loaded = true
272
+ ownerOf(doc.label).loaded = true
273
+ }
274
+ for (let c of source.comps ?? []) {
275
+ let info = vocab.comp(c.name)
276
+ if (info && !c.extends) c.rules = anatomyData(info)
277
+ for (let p of c.props) {
278
+ if (!c.extends) p.rules = anatomyData(vocab.prop(c.name, p.name))
279
+ }
280
+ c.refs = [
281
+ ...new Set(c.props.flatMap((p) => {
282
+ let ref = vocab.prop(c.name, p.name)?.ref
283
+ p.refs = ref ? [ref] : p.refs
284
+ return p.refs
285
+ })),
286
+ ]
287
+ if (!c.extends) {
288
+ c.indexes = vocab.indexes(c.name)
289
+ c.identity = vocab.identity(c.name)
290
+ c.assocs = vocab.assocs().filter(([, a]) => a.comp == c.name)
291
+ .map(([name, a]) => ({ name, prop: a.prop }))
292
+ }
293
+ }
294
+ }
295
+ let tool = (
296
+ owner: string,
297
+ t: Omit<NamedTool, 'run'>,
298
+ tier?: string,
299
+ ): Tool => {
300
+ // Do not traverse a tool object: even a discarded field could be its host.
301
+ let ds = Object.getOwnPropertyDescriptors(t)
302
+ let data = anatomyData(Object.fromEntries(
303
+ ['name', 'inputSchema', 'outputSchema', 'description', ...hints].flatMap(
304
+ (k) => {
305
+ let d = ds[k]
306
+ return d && 'value' in d ? [[k, d.value]] : []
307
+ },
308
+ ),
309
+ ))
310
+ let kept = Object.fromEntries(
311
+ hints.flatMap((k) => data[k] === undefined ? [] : [[k, data[k]]]),
312
+ )
313
+ return {
314
+ name: typeof data.name == 'string' ? data.name : '',
315
+ package: owner,
316
+ facet: 'tools',
317
+ inputSchema: anatomyData(data.inputSchema),
318
+ ...data.outputSchema
319
+ ? { outputSchema: anatomyData(data.outputSchema) }
320
+ : {},
321
+ ...typeof data.description == 'string'
322
+ ? { description: data.description }
323
+ : {},
324
+ hints: kept,
325
+ ...tier ? { tier } : {},
326
+ declared: true,
327
+ loaded: !!tier,
328
+ bound: !!tier,
329
+ }
330
+ }
331
+ // Applied in the order they were observed, the first time anybody reads.
332
+ let pending: (() => void)[] = []
333
+ let snapshot: Anatomy | undefined
334
+ let later =
335
+ <A extends unknown[]>(step: (...args: A) => void) => (...args: A) => {
336
+ pending.push(() => step(...args))
337
+ }
338
+ return {
339
+ read: (): Anatomy => {
340
+ if (!snapshot || pending.length) {
341
+ snapshot = undefined
342
+ for (let step of pending.splice(0)) step()
343
+ snapshot = anatomy({ ...source, facets: [...facets.values()] })
344
+ }
345
+ // A composition changes only when evidence arrives. Each reader receives
346
+ // an independent value, even while the projection remains current.
347
+ return structuredClone(snapshot)
348
+ },
349
+ observe: later(observe),
350
+ attempted: later((owner: string, name: string) => {
351
+ forFacet(owner, name).attempted = true
352
+ }),
353
+ loaded: later((owner: string, name: string, present: boolean) => {
354
+ let f = forFacet(owner, name)
355
+ f.loaded = f.declared = present
356
+ if (present) ownerOf(owner).loaded = true
357
+ }),
358
+ binding: later(binding),
359
+ declarations: later(declarations),
360
+ graph: later((owner: string, plugins: Plugin[]) => {
361
+ plugins.forEach((p, i) => {
362
+ let made = anatomyPlugin(owner, p, String(i))
363
+ source.rules = [
364
+ ...source.rules ?? [],
365
+ ...made.rules.map((r) => ({ ...r, bound: false })),
366
+ ]
367
+ source.hooks = [
368
+ ...source.hooks ?? [],
369
+ ...made.hooks.map((h) => ({ ...h, bound: false })),
370
+ ]
371
+ })
372
+ if (plugins.length) {
373
+ registered.add(owner)
374
+ ownerOf(owner).loaded = true
375
+ }
376
+ }),
377
+ graphed: later(() => {
378
+ for (let r of source.rules ?? []) r.bound = true
379
+ for (let h of source.hooks ?? []) h.bound = true
380
+ for (let c of source.comps ?? []) c.bound = true
381
+ for (let owner of vocabulary) binding(owner, 'vocab')
382
+ for (let owner of registered) binding(owner, 'graph')
383
+ }),
384
+ tier: later((tools: NamedTool[], owner = '@yaks/graph') => {
385
+ ownerOf(owner)
386
+ binding(owner, 'tools')
387
+ source.tools = [
388
+ ...source.tools ?? [],
389
+ ...tools.map((t) =>
390
+ tool(owner, t, owner == '@yaks/graph' ? 'graph' : 'tools')
391
+ ),
392
+ ]
393
+ }),
394
+ runs: later((owner: string, names: string[], joined: string[]) => {
395
+ if (joined.length) binding(owner, 'tools')
396
+ for (let t of source.tools ?? []) {
397
+ if (t.package != owner) continue
398
+ t.loaded = names.includes(t.name)
399
+ t.bound = joined.includes(t.name)
400
+ }
401
+ }),
402
+ effects: later((handlers: Map<string, string>, serving: boolean) => {
403
+ for (let e of source.effects ?? []) {
404
+ e.handler = handlers.get(e.name)
405
+ e.loaded = !!e.handler
406
+ e.bound = serving
407
+ e.noop = serving && !e.handler
408
+ if (e.handler) binding(e.handler, 'effects')
409
+ }
410
+ }),
411
+ due: later((names: string[]) => {
412
+ for (let e of source.effects ?? []) {
413
+ if (!names.includes(e.name)) continue
414
+ e.handler = '@yaks/tools'
415
+ e.noop = false
416
+ e.loaded = e.bound = true
417
+ }
418
+ if (names.length) binding('@yaks/tools', 'effects')
419
+ }),
420
+ routes: later(
421
+ (owner: string, routes: { method: string; path: string }[]) => {
422
+ source.routes = [
423
+ ...source.routes ?? [],
424
+ ...routes.map((r, i) => ({
425
+ name: `${r.method} ${r.path}`,
426
+ package: owner,
427
+ facet: 'routes',
428
+ key: String(i),
429
+ method: r.method,
430
+ path: r.path,
431
+ loaded: true,
432
+ bound: true,
433
+ })),
434
+ ]
435
+ binding(owner, 'routes')
436
+ },
437
+ ),
438
+ }
439
+ }