@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/words.ts ADDED
@@ -0,0 +1,210 @@
1
+ // Vocabulary assembly from already-imported facets, with no storage opened.
2
+ import type { NamedTool } from 'jsr:@yaks/graph@0.2.36'
3
+ import { toolName } from '@yaks/graph/call'
4
+ import { tier } from '@yaks/graph/tools'
5
+ import { derived as callsDerived } from '@yaks/tools/vocab'
6
+ import { toolsIn } from '@yaks/vocab/tools'
7
+ import {
8
+ type Keywords,
9
+ loadVocab,
10
+ type Vocab,
11
+ type VocabDoc,
12
+ } from '@yaks/vocab/runtime'
13
+ import { fields as searched } from '@yaks/fts/fields'
14
+ import { type Backings, backingsIn, type Derived, derivedIn } from '@yaks/sql'
15
+ import type { Configured, Options } from './config.ts'
16
+ import type { FacetName, Facets, Load, VocabFacet } from './facets.ts'
17
+ import { understood } from './keywords.js'
18
+ import { beneath, docsOf, selected } from './documents.js'
19
+ // One facet of every plugin a config names, each beside the options it was
20
+ // named with, the package it came from and its chosen words: what a process
21
+ // runs is one plugin's module handed one plugin's config. The package comes third, because a duty's
22
+ // lease is named after the package that owns the work — the lease `@yaks/wake`
23
+ // holds is the one every process reaching for that timer reaches for.
24
+ export type Taken<F extends FacetName> = [
25
+ Facets[F],
26
+ Options,
27
+ string,
28
+ string[]?,
29
+ FacetName[]?,
30
+ ][]
31
+
32
+ export let taking = async <F extends FacetName>(
33
+ plugins: Configured[],
34
+ name: F,
35
+ load: Load,
36
+ ): Promise<Taken<F>> =>
37
+ (await Promise.all(
38
+ plugins.filter(([, , , facets]) =>
39
+ facets === undefined || facets.includes(name)
40
+ ).map(async ([plugin, options, words, facets]) => {
41
+ let mod = await load(plugin, name)
42
+ if (!mod && name == 'vocab') selected([], words, plugin)
43
+ return [mod, options, plugin, words, facets] as const
44
+ }),
45
+ )).filter((
46
+ t,
47
+ ): t is [
48
+ Facets[F],
49
+ Options,
50
+ string,
51
+ string[] | undefined,
52
+ FacetName[] | undefined,
53
+ ] => !!t[0])
54
+
55
+ /** What a config's plugins declare, read without opening anything: their
56
+ * vocabulary documents (each written with the package that brought it), the
57
+ * vocabulary they load into, and the tools they declare. What a command line
58
+ * lists, and what a graph is then built over. */
59
+ export type Words = {
60
+ docs: VocabDoc[]
61
+ vocab: Vocab
62
+ /** the properties the store computes rather than stores */
63
+ derived: Derived
64
+ /** the rows the computed components are read from */
65
+ backed: Backings
66
+ /** the tools the graph these words describe offers, declared and not
67
+ * implemented: the generic tier where the vocabulary gives it one, then every
68
+ * plugin's */
69
+ tools: () => Declared[]
70
+ }
71
+
72
+ /** A tool as declared, without the code behind it. */
73
+ export type Declared = Omit<NamedTool, 'run'>
74
+
75
+ // The words of these facets, made once while every facet lives. A vocabulary
76
+ // is a value nobody changes once it is loaded, so each graph a process opens
77
+ // over the same plugins shares one, and with it all that is kept per
78
+ // vocabulary: a store's install plan, a read's rendered statements.
79
+ type Worded = {
80
+ next: WeakMap<VocabFacet, Map<string, Worded>>
81
+ choices?: WeakMap<
82
+ WordsOpts,
83
+ {
84
+ words?: Words
85
+ own?: WeakMap<VocabDoc[], Words>
86
+ sources?: { entries: Map<string, Words>; chars: number }
87
+ }
88
+ >
89
+ }
90
+ /** A concrete host may compose only the words it explicitly configured.
91
+ * Keywords supplied here likewise replace the plugins' keyword registrations. */
92
+ export type WordsOpts = { implicit?: boolean; keywords?: Keywords[] }
93
+ let defaults: WordsOpts = {}
94
+ let worded: Worded = { next: new WeakMap() }
95
+ export let wordsOf = (
96
+ vocabs: Taken<'vocab'>,
97
+ own?: VocabDoc[],
98
+ opts: WordsOpts = defaults,
99
+ ): Words => {
100
+ let at = vocabs.reduce((node, [v, , plugin, names]) => {
101
+ let named = node.next.get(v)
102
+ if (!named) node.next.set(v, named = new Map())
103
+ let key = JSON.stringify([plugin, names && [...new Set(names)].sort()])
104
+ let next = named.get(key)
105
+ if (!next) named.set(key, next = { next: new WeakMap() })
106
+ return next
107
+ }, worded)
108
+ let choices = at.choices ??= new WeakMap()
109
+ let choice = choices.get(opts)
110
+ if (!choice) choices.set(opts, choice = {})
111
+ if (!own?.length) return choice.words ??= spoken(vocabs, [], opts)
112
+ let held = choice.own ??= new WeakMap()
113
+ let words = held.get(own)
114
+ if (!words) {
115
+ // Hosts often decode the same small set of graph-owned documents afresh.
116
+ // Retain equivalent compilations across interleaved graphs, bounded by
117
+ // both their count and the source text they keep.
118
+ let source = JSON.stringify(own)
119
+ let sources = choice.sources ??= { entries: new Map(), chars: 0 }
120
+ words = sources.entries.get(source)
121
+ if (words) {
122
+ sources.entries.delete(source)
123
+ sources.entries.set(source, words)
124
+ } else {
125
+ words = spoken(vocabs, own, opts)
126
+ if (source.length <= 256 * 1024) {
127
+ sources.entries.set(source, words)
128
+ sources.chars += source.length
129
+ while (sources.entries.size > 16 || sources.chars > 256 * 1024) {
130
+ let first = sources.entries.keys().next().value!
131
+ sources.entries.delete(first)
132
+ sources.chars -= first.length
133
+ }
134
+ }
135
+ }
136
+ held.set(own, words)
137
+ }
138
+ return words
139
+ }
140
+
141
+ // The words, from the `./vocab` facets already imported.
142
+ let spoken = (
143
+ vocabs: Taken<'vocab'>,
144
+ own: VocabDoc[],
145
+ opts: WordsOpts,
146
+ ): Words => {
147
+ // The components a tool call is recorded in belong to the host, not to
148
+ // whichever plugin happened to declare them: what was asked of this host is
149
+ // its own record. A plugin that already declares them — a harness, whose
150
+ // transcripts are calls — keeps its own definitions, so only the components
151
+ // nobody supplied are added.
152
+ let docs: VocabDoc[] = vocabs.flatMap(([v, , plugin, names]) =>
153
+ selected(v.docs ?? [], names, plugin).map((d) => ({
154
+ ...d,
155
+ label: plugin,
156
+ description: v.description,
157
+ }))
158
+ )
159
+ if (opts.implicit !== false) docs = docsOf(docs)
160
+ let keywords = opts.keywords ??
161
+ (opts.implicit === false
162
+ ? vocabs.flatMap(([v]) => v.keywords ?? [])
163
+ : understood(vocabs.flatMap(([v]) => v.keywords ?? [])))
164
+ if (own.length) {
165
+ docs = [...beneath(docs, own), ...own]
166
+ }
167
+ let vocab = loadVocab(docs, keywords)
168
+ let by = vocabs.map(([v, , label]) => ({
169
+ facet: v,
170
+ local: vocab.in({ label }),
171
+ }))
172
+ let tools = vocab.in({ label: '@yaks/tools' })
173
+ let derived: Derived = Object.assign(
174
+ {},
175
+ derivedIn(callsDerived(tools), tools, vocab),
176
+ ...by.map(({ facet, local }) =>
177
+ facet.derived && derivedIn(facet.derived(local), local, vocab)
178
+ ),
179
+ )
180
+ return {
181
+ docs,
182
+ vocab,
183
+ derived,
184
+ backed: Object.assign(
185
+ {},
186
+ ...by.map(({ facet, local }) => {
187
+ if (!facet.backed) return {}
188
+ return backingsIn(
189
+ facet.backed(local, derivedIn(derived, vocab, local)),
190
+ local,
191
+ vocab,
192
+ )
193
+ }),
194
+ ),
195
+ // The generic tier lists `search` only where a property is indexed, so its
196
+ // declarations are read off the tier the graph would build. Read on asking,
197
+ // since checking every declaration costs what a graph opened to answer one
198
+ // query should not pay twice.
199
+ tools: () => [
200
+ ...tier({
201
+ keywords: vocab.keywords,
202
+ ...(searched(vocab).length ? { search: () => [] } : {}),
203
+ }).map(({ run: _, ...decl }) => decl),
204
+ ...toolsIn(docs).map((decl) => ({
205
+ ...decl,
206
+ name: decl.name ?? toolName(decl),
207
+ })),
208
+ ],
209
+ }
210
+ }