@yaks/persona 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 (115) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +401 -2
  3. package/_dist/comp.d.ts +13 -0
  4. package/_dist/comp.d.ts.map +1 -0
  5. package/_dist/effects.d.ts +22 -0
  6. package/_dist/effects.d.ts.map +1 -0
  7. package/_dist/mod.d.ts +40 -0
  8. package/_dist/mod.d.ts.map +1 -0
  9. package/_dist/name.d.ts +49 -0
  10. package/_dist/name.d.ts.map +1 -0
  11. package/_dist/owed.d.ts +56 -0
  12. package/_dist/owed.d.ts.map +1 -0
  13. package/_dist/part.d.ts +4 -0
  14. package/_dist/part.d.ts.map +1 -0
  15. package/_dist/shipped.d.ts +30 -0
  16. package/_dist/shipped.d.ts.map +1 -0
  17. package/_dist/skill-checkout.d.ts +6 -0
  18. package/_dist/skill-checkout.d.ts.map +1 -0
  19. package/_dist/skill-effects.d.ts +25 -0
  20. package/_dist/skill-effects.d.ts.map +1 -0
  21. package/_dist/skill-mirror-io.d.ts +7 -0
  22. package/_dist/skill-mirror-io.d.ts.map +1 -0
  23. package/_dist/skill-mirror.d.ts +15 -0
  24. package/_dist/skill-mirror.d.ts.map +1 -0
  25. package/_dist/skill-text.d.ts +28 -0
  26. package/_dist/skill-text.d.ts.map +1 -0
  27. package/_dist/skills.d.ts +22 -0
  28. package/_dist/skills.d.ts.map +1 -0
  29. package/_dist/tools.d.ts +9 -0
  30. package/_dist/tools.d.ts.map +1 -0
  31. package/_dist/ui.d.ts +6 -0
  32. package/_dist/ui.d.ts.map +1 -0
  33. package/_dist/views.d.ts +19 -0
  34. package/_dist/views.d.ts.map +1 -0
  35. package/_dist/vocab.d.ts +7 -0
  36. package/_dist/vocab.d.ts.map +1 -0
  37. package/_dist/voice.d.ts +47 -0
  38. package/_dist/voice.d.ts.map +1 -0
  39. package/_dist/worn.d.ts +55 -0
  40. package/_dist/worn.d.ts.map +1 -0
  41. package/carried.js +201 -0
  42. package/carried.js.map +1 -0
  43. package/carried.ts +269 -0
  44. package/comp.js +31 -0
  45. package/comp.js.map +1 -0
  46. package/comp.ts +38 -0
  47. package/deno.json +1 -0
  48. package/desk.js +201 -0
  49. package/desk.js.map +1 -0
  50. package/desk.ts +301 -0
  51. package/effects.js +106 -0
  52. package/effects.js.map +1 -0
  53. package/effects.ts +120 -0
  54. package/files.js +305 -0
  55. package/files.js.map +1 -0
  56. package/files.ts +319 -0
  57. package/inspect.js +66 -0
  58. package/inspect.js.map +1 -0
  59. package/inspect.ts +30 -0
  60. package/mod.js +40 -0
  61. package/mod.js.map +1 -0
  62. package/mod.ts +61 -0
  63. package/name.js +161 -0
  64. package/name.js.map +1 -0
  65. package/name.ts +184 -0
  66. package/owed.js +196 -0
  67. package/owed.js.map +1 -0
  68. package/owed.ts +256 -0
  69. package/package.json +83 -4
  70. package/part.js +4 -0
  71. package/part.js.map +1 -0
  72. package/part.ts +4 -0
  73. package/shipped.js +130 -0
  74. package/shipped.js.map +1 -0
  75. package/shipped.ts +131 -0
  76. package/skill-checkout.js +92 -0
  77. package/skill-checkout.js.map +1 -0
  78. package/skill-checkout.ts +121 -0
  79. package/skill-effects.js +244 -0
  80. package/skill-effects.js.map +1 -0
  81. package/skill-effects.ts +274 -0
  82. package/skill-mirror-io.js +193 -0
  83. package/skill-mirror-io.js.map +1 -0
  84. package/skill-mirror-io.ts +212 -0
  85. package/skill-mirror.js +659 -0
  86. package/skill-mirror.js.map +1 -0
  87. package/skill-mirror.ts +640 -0
  88. package/skill-text.js +158 -0
  89. package/skill-text.js.map +1 -0
  90. package/skill-text.ts +183 -0
  91. package/skills.js +238 -0
  92. package/skills.js.map +1 -0
  93. package/skills.ts +383 -0
  94. package/text.js +59 -0
  95. package/text.js.map +1 -0
  96. package/text.ts +60 -0
  97. package/tools.js +76 -0
  98. package/tools.js.map +1 -0
  99. package/tools.ts +91 -0
  100. package/ui.js +19 -0
  101. package/ui.js.map +1 -0
  102. package/ui.ts +34 -0
  103. package/views.js +27 -0
  104. package/views.js.map +1 -0
  105. package/views.ts +29 -0
  106. package/vocab.js +14 -0
  107. package/vocab.js.map +1 -0
  108. package/vocab.json +270 -0
  109. package/vocab.ts +16 -0
  110. package/voice.js +105 -0
  111. package/voice.js.map +1 -0
  112. package/voice.ts +133 -0
  113. package/worn.js +328 -0
  114. package/worn.js.map +1 -0
  115. package/worn.ts +336 -0
package/worn.js ADDED
@@ -0,0 +1,328 @@
1
+ // What documents a persona is built from, read out of the graph: the
2
+ // `contains` and `reads` edges leaving it, resolved to the documents at their
3
+ // far ends, in the order they were authored in. {@link voice} renders what
4
+ // this function collects, and neither knows how the other does its job.
5
+ //
6
+ // Two relations, borrowed rather than invented. `contains` is @yaks/task's and
7
+ // `reads` is @yaks/kernel's, and both already mean what is needed here: this
8
+ // persona contains that document, this persona reads that one. A relation the
9
+ // composed vocabulary does not declare contributes nothing rather than
10
+ // throwing — a graph with no @yaks/task in it has no `contains`, so a persona
11
+ // there includes no documents, which is a fair reading of that graph and not a
12
+ // misconfiguration this package is entitled to refuse.
13
+ //
14
+ // It reads the EDGE entities rather than walking them (@yaks/edge `walk`
15
+ // returns the far ends, and a far end no longer carries the edge's `ord`) —
16
+ // the one property the authored order depends on.
17
+ //
18
+ // An included persona is folded in: its instruction text is included like any
19
+ // other document, and the documents it links to are added to the ones this
20
+ // persona links to, which is how a base persona reaches every persona built on
21
+ // top of it without anybody copying its text. A persona reached by `reads` is
22
+ // only listed: the edge records where it is, not that its text belongs here.
23
+ // A registered tool it contains is offered to whoever wears it rather than
24
+ // said, and an included persona's tools are offered with its own. A saved
25
+ // query it contains (@yaks/project's `board{query}`) is not said either: its
26
+ // rows are, when they are delivered (reference at authoring, resolve at
27
+ // delivery), and an included persona's queries are run with its own.
28
+ //
29
+ // A persona may be worn on a base (`persona.base`): the base's text and
30
+ // documents come first, and this persona's follow them, as {@link fold} says
31
+ // them. An edge stays within one store, and a base is often kept in another
32
+ // (a platform's, under each space's own), so the base is named by its eid and
33
+ // read from where the wearer says it is kept: reference at authoring, resolve
34
+ // at delivery.
35
+ // This walks level by level rather than recursing, so an embedded store
36
+ // answers the whole read synchronously and a remote one needs exactly one
37
+ // chain of calls (@yaks/edge's `reach` walks the same way).
38
+ //
39
+ // Two sets of what personas say combine the same way: {@link fold} says
40
+ // several as one, the way a sub-project's agents hear every common persona
41
+ // above them, and {@link beside} leaves out what another file already says.
42
+ import { and, eq, list, present } from "@yaks/query";
43
+ import { after, each, isPromise } from "@yaks/fp";
44
+ import { EDGE, linkable } from "@yaks/edge";
45
+ import { DOC } from "@yaks/doc";
46
+ import { PERSONA } from "./comp.js";
47
+ /** The relation whose far end is included in full. */ export let CONTAINS = 'contains';
48
+ /** The relation whose far end is only listed by id. */ export let READS = 'reads';
49
+ /** A registered tool (@yaks/tools): contained, it is offered, not said. */ export let TOOL = 'tool';
50
+ /** A saved query (@yaks/project): contained, its rows are delivered, not its
51
+ * text. */ export let QUERY = 'board';
52
+ // How deep personas may be nested. A base under a base under a base is a graph
53
+ // somebody drew by hand; deeper than this means a cycle, and a cycle is what
54
+ // the `seen` set already stops.
55
+ let DEPTH = 8;
56
+ let value = (eids)=>eids.length == 1 ? eids[0] : list(...eids);
57
+ // A document put forward for a decision (@yaks/kernel's `proposed`) is a
58
+ // suggestion until somebody decides it, and one declined stays a suggestion:
59
+ // linking it to a persona files it where it belongs, but the persona does not
60
+ // say it.
61
+ let undecided = (b)=>{
62
+ if (!b.proposed) return false;
63
+ let decided = b.decided;
64
+ return !decided || decided.verdict == 'declined';
65
+ };
66
+ let far = (b)=>{
67
+ let to = b[EDGE]?.to;
68
+ return typeof to == 'string' ? to : '';
69
+ };
70
+ let ordOf = (b)=>Number(b[EDGE]?.ord ?? Number.MAX_SAFE_INTEGER);
71
+ /**
72
+ * The order links are read in, the order an agent is given what they lead
73
+ * to: an edge's own `ord` where its author set one, then the entity it points
74
+ * at, so two unordered siblings never swap places between reads. A sort's
75
+ * comparator, for anything drawing them in that order.
76
+ */ /// let to = (to: string, ord?: number) => ({ entity: { eid: to }, edge: { from: 'p', to, ...ord == null ? {} : { ord } } })
77
+ /// [to('b'), to('a'), to('c', 1)].sort(served).map((b) => b.entity.eid) -> ['c', 'a', 'b']
78
+ export let served = (a, b)=>ordOf(a) - ordOf(b) || far(a).localeCompare(far(b));
79
+ // The links of one relation out of a whole level, in the order they are
80
+ // served.
81
+ let links = (storage, from, tag)=>!tag || !from.length ? [] : after(storage.read(and(eq(`${EDGE}.from`, value(from)), present(tag))), (found)=>found.filter(far).sort(served).map(far));
82
+ // Two reads at once, a promise only when either is one.
83
+ let both = (a, b)=>isPromise(a) || isPromise(b) ? Promise.all([
84
+ a,
85
+ b
86
+ ]) : [
87
+ a,
88
+ b
89
+ ];
90
+ /**
91
+ * The documents one persona is built from, read out of a storage. Returns
92
+ * `undefined` when the eid does not exist, or is not a persona — what to do
93
+ * about that is the caller's decision, since a tool call refuses while a
94
+ * report might not.
95
+ *
96
+ * ```ts ignore
97
+ * let worn = await wear(storage, vocab)('N-1')
98
+ * if (worn) console.log(voice(vocab)(worn))
99
+ * ```
100
+ *
101
+ * A persona worn on a base (`persona.base`) is said base first, the base read
102
+ * from `bases` (by default the same storage): a platform's persona, say, under
103
+ * each space's own.
104
+ *
105
+ * Synchronous in, synchronous out: over an embedded database this returns a
106
+ * value directly, and over a storage that returns promises it returns a
107
+ * promise.
108
+ */ export let wear = (storage, vocab, bases = storage)=>{
109
+ let layers = wearLayers(storage, vocab, bases);
110
+ return (eid)=>after(layers(eid), ([base, ...more])=>base && fold(base, ...more));
111
+ };
112
+ /** The same persona in base-first order, preserving the boundary between
113
+ * layers for consumers that send them at different cache lifetimes. */ export let wearLayers = (storage, vocab, bases = storage)=>{
114
+ let alone = gathered(storage, vocab);
115
+ let below = gathered(bases, vocab);
116
+ let on = (worn, seen)=>{
117
+ if (!worn) return [];
118
+ let base = worn.persona[PERSONA]?.base;
119
+ if (typeof base != 'string' || seen.has(base) || seen.size >= DEPTH) {
120
+ return [
121
+ worn
122
+ ];
123
+ }
124
+ return after(below(base), (under)=>after(on(under, new Set([
125
+ ...seen,
126
+ base
127
+ ])), (stack)=>[
128
+ ...stack,
129
+ worn
130
+ ]));
131
+ };
132
+ return (eid)=>after(alone(eid), (worn)=>on(worn, new Set([
133
+ eid
134
+ ])));
135
+ };
136
+ // One persona and what its edges reach, its base left aside.
137
+ let gathered = (storage, vocab)=>{
138
+ let tags = linkable(vocab);
139
+ // A level's links, both relations read at once.
140
+ let linked = (level)=>both(links(storage, level, tags[CONTAINS]), links(storage, level, tags[READS]));
141
+ // The persona and its own links are read together: a remote storage
142
+ // answers both in one round trip's time.
143
+ return (eid)=>after(both(storage.get([
144
+ eid
145
+ ]), linked([
146
+ eid
147
+ ])), ([found, first])=>{
148
+ let persona = found[0];
149
+ if (!persona?.[PERSONA]) return undefined;
150
+ let contained = new Map();
151
+ let names = new Map();
152
+ let tools = new Map();
153
+ let queries = new Map();
154
+ let seen = new Set([
155
+ eid
156
+ ]);
157
+ let level = [
158
+ eid
159
+ ];
160
+ return after(each(Array.from({
161
+ length: DEPTH
162
+ }, (_, i)=>i), null, (_, i)=>{
163
+ if (!level.length) return null;
164
+ return after(i ? linked(level) : first, ([held, said])=>after(storage.get([
165
+ ...new Set([
166
+ ...held,
167
+ ...said
168
+ ])
169
+ ]), (docs)=>{
170
+ let by = new Map(docs.map((b)=>[
171
+ b.entity.eid,
172
+ b
173
+ ]));
174
+ let next = [];
175
+ for (let id of held){
176
+ let b = by.get(id);
177
+ if (b?.[TOOL]) {
178
+ tools.set(id, b);
179
+ continue;
180
+ }
181
+ if (b?.[QUERY]) {
182
+ queries.set(id, b);
183
+ continue;
184
+ }
185
+ // `seen` holds the persona itself, so a cycle of
186
+ // personas never includes the text it started from
187
+ // twice.
188
+ if (!b || !(b[DOC] || b.content) || seen.has(id) || undecided(b)) {
189
+ continue;
190
+ }
191
+ seen.add(id);
192
+ contained.set(id, b);
193
+ // An included persona's text is included like any other
194
+ // document; the documents it links to are read on the
195
+ // next level.
196
+ if (b[PERSONA]) {
197
+ next.push(id);
198
+ }
199
+ }
200
+ for (let id of said){
201
+ let b = by.get(id);
202
+ if (b && (b[DOC] || b.content) && !names.has(id) && !undecided(b)) {
203
+ names.set(id, b);
204
+ }
205
+ }
206
+ level = next;
207
+ return null;
208
+ }));
209
+ }), ()=>{
210
+ // Inclusion wins: a document listed in one place and included in
211
+ // another is included once, and never appears twice. The persona
212
+ // itself is neither — its text is what the document opens with.
213
+ for (let id of contained.keys()){
214
+ names.delete(id);
215
+ }
216
+ names.delete(eid);
217
+ return {
218
+ persona,
219
+ contains: [
220
+ ...contained.values()
221
+ ],
222
+ names: [
223
+ ...names.values()
224
+ ],
225
+ tools: [
226
+ ...tools.values()
227
+ ],
228
+ queries: [
229
+ ...queries.values()
230
+ ]
231
+ };
232
+ });
233
+ });
234
+ };
235
+ let eidOf = (b)=>b.entity.eid;
236
+ /**
237
+ * Several personas said as one: the first leads, and each after it is
238
+ * included the way a persona it contained would be — its text as a document,
239
+ * then the documents it includes, its tools offered with the rest — with
240
+ * nothing said twice. How a sub-project's agents hear the common persona of
241
+ * every project above theirs.
242
+ */ export let fold = (head, ...more)=>{
243
+ let lead = eidOf(head.persona);
244
+ let contained = new Map(head.contains.map((b)=>[
245
+ eidOf(b),
246
+ b
247
+ ]));
248
+ let names = new Map(head.names.map((b)=>[
249
+ eidOf(b),
250
+ b
251
+ ]));
252
+ let tools = new Map(head.tools.map((b)=>[
253
+ eidOf(b),
254
+ b
255
+ ]));
256
+ let queries = new Map(head.queries.map((b)=>[
257
+ eidOf(b),
258
+ b
259
+ ]));
260
+ for (let w of more){
261
+ for (let b of [
262
+ w.persona,
263
+ ...w.contains
264
+ ]){
265
+ if (eidOf(b) != lead && !contained.has(eidOf(b))) {
266
+ contained.set(eidOf(b), b);
267
+ }
268
+ }
269
+ for (let b of w.names)if (!names.has(eidOf(b))) names.set(eidOf(b), b);
270
+ for (let b of w.tools)if (!tools.has(eidOf(b))) tools.set(eidOf(b), b);
271
+ for (let b of w.queries){
272
+ if (!queries.has(eidOf(b))) queries.set(eidOf(b), b);
273
+ }
274
+ }
275
+ for (let id of [
276
+ lead,
277
+ ...contained.keys()
278
+ ])names.delete(id);
279
+ return {
280
+ persona: head.persona,
281
+ contains: [
282
+ ...contained.values()
283
+ ],
284
+ names: [
285
+ ...names.values()
286
+ ],
287
+ tools: [
288
+ ...tools.values()
289
+ ],
290
+ queries: [
291
+ ...queries.values()
292
+ ]
293
+ };
294
+ };
295
+ /**
296
+ * What `w` says that `said` does not: a document `said` includes is dropped in
297
+ * both forms, and one it only names loses only its name, since including it in
298
+ * full still earns its place. `w` keeps its own lead and its tools, which are
299
+ * offered, not said, and its queries, whose rows are delivered. How a file
300
+ * read beside another avoids saying anything twice.
301
+ */ export let beside = (w, said)=>{
302
+ let included = new Set([
303
+ said.persona,
304
+ ...said.contains
305
+ ].map(eidOf));
306
+ let named = new Set([
307
+ ...included,
308
+ ...said.names.map(eidOf)
309
+ ]);
310
+ return {
311
+ persona: w.persona,
312
+ contains: w.contains.filter((b)=>!included.has(eidOf(b))),
313
+ names: w.names.filter((b)=>!named.has(eidOf(b))),
314
+ tools: w.tools,
315
+ queries: w.queries
316
+ };
317
+ };
318
+ /** The names of the tools a session wearing `w` is offered: nothing where it
319
+ * contains none, which leaves its runner's own set. */ export let offer = (w)=>{
320
+ let names = (w?.tools ?? []).flatMap((b)=>{
321
+ let name = b[TOOL].name;
322
+ return typeof name == 'string' && name ? [
323
+ name
324
+ ] : [];
325
+ });
326
+ return names.length ? names : undefined;
327
+ };
328
+ //# sourceMappingURL=worn.js.map
package/worn.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worn.js","sources":["./worn.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,8EAA8E;AAC9E,2EAA2E;AAC3E,wEAAwE;AACxE,EAAE;AACF,+EAA+E;AAC/E,6EAA6E;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,CAAE,MAAM,MAAM,CAAC;EACzD,OAAO;IACL,SAAS,KAAK,OAAO;IACrB,UAAU;SAAI,UAAU,MAAM;KAAG;IACjC,OAAO;SAAI,MAAM,MAAM;KAAG;IAC1B,OAAO;SAAI,MAAM,MAAM;KAAG;IAC1B,SAAS;SAAI,QAAQ,MAAM;KAAG;EAChC;AACF,EAAC;AAED;;;;;;CAMC,GACD,OAAO,IAAI,SAAS,CAAC,GAAS;EAC5B,IAAI,WAAW,IAAI,IAAI;IAAC,KAAK,OAAO;OAAK,KAAK,QAAQ;GAAC,CAAC,GAAG,CAAC;EAC5D,IAAI,QAAQ,IAAI,IAAI;OAAI;OAAa,KAAK,KAAK,CAAC,GAAG,CAAC;GAAO;EAC3D,OAAO;IACL,SAAS,EAAE,OAAO;IAClB,UAAU,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC,IAAM,CAAC,SAAS,GAAG,CAAC,MAAM;IACvD,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,IAAM,CAAC,MAAM,GAAG,CAAC,MAAM;IAC9C,OAAO,EAAE,KAAK;IACd,SAAS,EAAE,OAAO;EACpB;AACF,EAAC;AAED;qDACqD,GACrD,OAAO,IAAI,QAAQ,CAAC;EAClB,IAAI,QAAQ,CAAC,GAAG,SAAS,EAAE,EAAE,OAAO,CAAC,CAAC;IACpC,IAAI,OAAO,AAAC,CAAC,CAAC,KAAK,CAAwB,IAAI;IAC/C,OAAO,OAAO,QAAQ,YAAY,OAAO;MAAC;KAAK,GAAG,EAAE;EACtD;EACA,OAAO,MAAM,MAAM,GAAG,QAAQ;AAChC,EAAC"}
package/worn.ts ADDED
@@ -0,0 +1,336 @@
1
+ // What documents a persona is built from, read out of the graph: the
2
+ // `contains` and `reads` edges leaving it, resolved to the documents at their
3
+ // far ends, in the order they were authored in. {@link voice} renders what
4
+ // this function collects, and neither knows how the other does its job.
5
+ //
6
+ // Two relations, borrowed rather than invented. `contains` is @yaks/task's and
7
+ // `reads` is @yaks/kernel's, and both already mean what is needed here: this
8
+ // persona contains that document, this persona reads that one. A relation the
9
+ // composed vocabulary does not declare contributes nothing rather than
10
+ // throwing — a graph with no @yaks/task in it has no `contains`, so a persona
11
+ // there includes no documents, which is a fair reading of that graph and not a
12
+ // misconfiguration this package is entitled to refuse.
13
+ //
14
+ // It reads the EDGE entities rather than walking them (@yaks/edge `walk`
15
+ // returns the far ends, and a far end no longer carries the edge's `ord`) —
16
+ // the one property the authored order depends on.
17
+ //
18
+ // An included persona is folded in: its instruction text is included like any
19
+ // other document, and the documents it links to are added to the ones this
20
+ // persona links to, which is how a base persona reaches every persona built on
21
+ // top of it without anybody copying its text. A persona reached by `reads` is
22
+ // only listed: the edge records where it is, not that its text belongs here.
23
+ // A registered tool it contains is offered to whoever wears it rather than
24
+ // said, and an included persona's tools are offered with its own. A saved
25
+ // query it contains (@yaks/project's `board{query}`) is not said either: its
26
+ // rows are, when they are delivered (reference at authoring, resolve at
27
+ // delivery), and an included persona's queries are run with its own.
28
+ //
29
+ // A persona may be worn on a base (`persona.base`): the base's text and
30
+ // documents come first, and this persona's follow them, as {@link fold} says
31
+ // them. An edge stays within one store, and a base is often kept in another
32
+ // (a platform's, under each space's own), so the base is named by its eid and
33
+ // read from where the wearer says it is kept: reference at authoring, resolve
34
+ // at delivery.
35
+ // This walks level by level rather than recursing, so an embedded store
36
+ // answers the whole read synchronously and a remote one needs exactly one
37
+ // chain of calls (@yaks/edge's `reach` walks the same way).
38
+ //
39
+ // Two sets of what personas say combine the same way: {@link fold} says
40
+ // several as one, the way a sub-project's agents hear every common persona
41
+ // above them, and {@link beside} leaves out what another file already says.
42
+
43
+ import { and, eq, type Input, list, present } from '@yaks/query'
44
+ import { after, each, isPromise } from '@yaks/fp'
45
+ import type { Bundle, Eid, Storage } from 'jsr:@yaks/graph@0.2.36'
46
+ import { EDGE, linkable } from '@yaks/edge'
47
+ import { DOC } from '@yaks/doc'
48
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
49
+ import { PERSONA } from './comp.js'
50
+ import type { Worn } from './voice.ts'
51
+
52
+ /** The relation whose far end is included in full. */
53
+ export let CONTAINS = 'contains'
54
+
55
+ /** The relation whose far end is only listed by id. */
56
+ export let READS = 'reads'
57
+
58
+ /** A registered tool (@yaks/tools): contained, it is offered, not said. */
59
+ export let TOOL = 'tool'
60
+
61
+ /** A saved query (@yaks/project): contained, its rows are delivered, not its
62
+ * text. */
63
+ export let QUERY = 'board'
64
+
65
+ // How deep personas may be nested. A base under a base under a base is a graph
66
+ // somebody drew by hand; deeper than this means a cycle, and a cycle is what
67
+ // the `seen` set already stops.
68
+ let DEPTH = 8
69
+
70
+ /** What composing a persona reads of a storage: queries, and entities by
71
+ * eid. A page hands it a storage over what it holds (@yaks/inspect's
72
+ * `useReading`), so the page composes with this very code. */
73
+ export type Reads = Pick<Storage, 'read' | 'get'>
74
+
75
+ let value = (eids: Eid[]): Input => eids.length == 1 ? eids[0] : list(...eids)
76
+
77
+ // A document put forward for a decision (@yaks/kernel's `proposed`) is a
78
+ // suggestion until somebody decides it, and one declined stays a suggestion:
79
+ // linking it to a persona files it where it belongs, but the persona does not
80
+ // say it.
81
+ let undecided = (b: Bundle): boolean => {
82
+ if (!b.proposed) return false
83
+ let decided = b.decided as { verdict?: unknown } | undefined
84
+ return !decided || decided.verdict == 'declined'
85
+ }
86
+
87
+ let far = (b: Bundle): Eid => {
88
+ let to = (b[EDGE] as { to?: unknown } | undefined)?.to
89
+ return typeof to == 'string' ? to : ''
90
+ }
91
+
92
+ let ordOf = (b: Bundle): number =>
93
+ Number((b[EDGE] as { ord?: unknown })?.ord ?? Number.MAX_SAFE_INTEGER)
94
+
95
+ /**
96
+ * The order links are read in, the order an agent is given what they lead
97
+ * to: an edge's own `ord` where its author set one, then the entity it points
98
+ * at, so two unordered siblings never swap places between reads. A sort's
99
+ * comparator, for anything drawing them in that order.
100
+ */
101
+ /// let to = (to: string, ord?: number) => ({ entity: { eid: to }, edge: { from: 'p', to, ...ord == null ? {} : { ord } } })
102
+ /// [to('b'), to('a'), to('c', 1)].sort(served).map((b) => b.entity.eid) -> ['c', 'a', 'b']
103
+ export let served = (a: Bundle, b: Bundle): number =>
104
+ ordOf(a) - ordOf(b) || far(a).localeCompare(far(b))
105
+
106
+ // The links of one relation out of a whole level, in the order they are
107
+ // served.
108
+ let links = (storage: Reads, from: Eid[], tag: string | undefined) =>
109
+ !tag || !from.length ? [] : after(
110
+ storage.read(and(eq(`${EDGE}.from`, value(from)), present(tag))),
111
+ (found) => found.filter(far).sort(served).map(far),
112
+ )
113
+
114
+ // Two reads at once, a promise only when either is one.
115
+ let both = <A, B>(
116
+ a: A | Promise<A>,
117
+ b: B | Promise<B>,
118
+ ): [A, B] | Promise<[A, B]> =>
119
+ isPromise(a) || isPromise(b) ? Promise.all([a, b]) : [a, b]
120
+
121
+ /**
122
+ * The documents one persona is built from, read out of a storage. Returns
123
+ * `undefined` when the eid does not exist, or is not a persona — what to do
124
+ * about that is the caller's decision, since a tool call refuses while a
125
+ * report might not.
126
+ *
127
+ * ```ts ignore
128
+ * let worn = await wear(storage, vocab)('N-1')
129
+ * if (worn) console.log(voice(vocab)(worn))
130
+ * ```
131
+ *
132
+ * A persona worn on a base (`persona.base`) is said base first, the base read
133
+ * from `bases` (by default the same storage): a platform's persona, say, under
134
+ * each space's own.
135
+ *
136
+ * Synchronous in, synchronous out: over an embedded database this returns a
137
+ * value directly, and over a storage that returns promises it returns a
138
+ * promise.
139
+ */
140
+ export let wear = (
141
+ storage: Reads,
142
+ vocab: Vocab,
143
+ bases: Reads = storage,
144
+ ): (eid: Eid) => Worn | undefined | Promise<Worn | undefined> => {
145
+ let layers = wearLayers(storage, vocab, bases)
146
+ return (eid) =>
147
+ after(layers(eid), ([base, ...more]) => base && fold(base, ...more))
148
+ }
149
+
150
+ /** The same persona in base-first order, preserving the boundary between
151
+ * layers for consumers that send them at different cache lifetimes. */
152
+ export let wearLayers = (
153
+ storage: Reads,
154
+ vocab: Vocab,
155
+ bases: Reads = storage,
156
+ ): (eid: Eid) => Worn[] | Promise<Worn[]> => {
157
+ let alone = gathered(storage, vocab)
158
+ let below = gathered(bases, vocab)
159
+ let on = (
160
+ worn: Worn | undefined,
161
+ seen: Set<Eid>,
162
+ ): Worn[] | Promise<Worn[]> => {
163
+ if (!worn) return []
164
+ let base = (worn.persona[PERSONA] as { base?: unknown } | undefined)?.base
165
+ if (typeof base != 'string' || seen.has(base) || seen.size >= DEPTH) {
166
+ return [worn]
167
+ }
168
+ return after(
169
+ below(base),
170
+ (under) =>
171
+ after(on(under, new Set([...seen, base])), (stack) => [...stack, worn]),
172
+ )
173
+ }
174
+ return (eid: Eid) => after(alone(eid), (worn) => on(worn, new Set([eid])))
175
+ }
176
+
177
+ // One persona and what its edges reach, its base left aside.
178
+ let gathered = (
179
+ storage: Reads,
180
+ vocab: Vocab,
181
+ ): (eid: Eid) => Worn | undefined | Promise<Worn | undefined> => {
182
+ let tags = linkable(vocab)
183
+ // A level's links, both relations read at once.
184
+ let linked = (level: Eid[]) =>
185
+ both(
186
+ links(storage, level, tags[CONTAINS]),
187
+ links(storage, level, tags[READS]),
188
+ )
189
+ // The persona and its own links are read together: a remote storage
190
+ // answers both in one round trip's time.
191
+ return (eid) =>
192
+ after(both(storage.get([eid]), linked([eid])), ([found, first]) => {
193
+ let persona = found[0]
194
+ if (!persona?.[PERSONA]) return undefined
195
+ let contained = new Map<Eid, Bundle>()
196
+ let names = new Map<Eid, Bundle>()
197
+ let tools = new Map<Eid, Bundle>()
198
+ let queries = new Map<Eid, Bundle>()
199
+ let seen = new Set<Eid>([eid])
200
+ let level = [eid]
201
+ return after(
202
+ each(Array.from({ length: DEPTH }, (_, i) => i), null, (_, i) => {
203
+ if (!level.length) return null
204
+ return after(i ? linked(level) : first, ([held, said]) =>
205
+ after(
206
+ storage.get([...new Set([...held, ...said])]),
207
+ (docs) => {
208
+ let by = new Map(docs.map((b) => [b.entity.eid, b]))
209
+ let next: Eid[] = []
210
+ for (let id of held) {
211
+ let b = by.get(id)
212
+ if (b?.[TOOL]) {
213
+ tools.set(id, b)
214
+ continue
215
+ }
216
+ if (b?.[QUERY]) {
217
+ queries.set(id, b)
218
+ continue
219
+ }
220
+ // `seen` holds the persona itself, so a cycle of
221
+ // personas never includes the text it started from
222
+ // twice.
223
+ if (
224
+ !b || !(b[DOC] || b.content) || seen.has(id) ||
225
+ undecided(b)
226
+ ) {
227
+ continue
228
+ }
229
+ seen.add(id)
230
+ contained.set(id, b)
231
+ // An included persona's text is included like any other
232
+ // document; the documents it links to are read on the
233
+ // next level.
234
+ if (b[PERSONA]) {
235
+ next.push(id)
236
+ }
237
+ }
238
+ for (let id of said) {
239
+ let b = by.get(id)
240
+ if (
241
+ b && (b[DOC] || b.content) && !names.has(id) &&
242
+ !undecided(b)
243
+ ) {
244
+ names.set(id, b)
245
+ }
246
+ }
247
+ level = next
248
+ return null
249
+ },
250
+ ))
251
+ }),
252
+ () => {
253
+ // Inclusion wins: a document listed in one place and included in
254
+ // another is included once, and never appears twice. The persona
255
+ // itself is neither — its text is what the document opens with.
256
+ for (let id of contained.keys()) {
257
+ names.delete(id)
258
+ }
259
+ names.delete(eid)
260
+ return {
261
+ persona,
262
+ contains: [...contained.values()],
263
+ names: [...names.values()],
264
+ tools: [...tools.values()],
265
+ queries: [...queries.values()],
266
+ }
267
+ },
268
+ )
269
+ })
270
+ }
271
+
272
+ let eidOf = (b: Bundle): Eid => b.entity.eid
273
+
274
+ /**
275
+ * Several personas said as one: the first leads, and each after it is
276
+ * included the way a persona it contained would be — its text as a document,
277
+ * then the documents it includes, its tools offered with the rest — with
278
+ * nothing said twice. How a sub-project's agents hear the common persona of
279
+ * every project above theirs.
280
+ */
281
+ export let fold = (head: Worn, ...more: Worn[]): Worn => {
282
+ let lead = eidOf(head.persona)
283
+ let contained = new Map(head.contains.map((b) => [eidOf(b), b]))
284
+ let names = new Map(head.names.map((b) => [eidOf(b), b]))
285
+ let tools = new Map(head.tools.map((b) => [eidOf(b), b]))
286
+ let queries = new Map(head.queries.map((b) => [eidOf(b), b]))
287
+ for (let w of more) {
288
+ for (let b of [w.persona, ...w.contains]) {
289
+ if (eidOf(b) != lead && !contained.has(eidOf(b))) {
290
+ contained.set(eidOf(b), b)
291
+ }
292
+ }
293
+ for (let b of w.names) if (!names.has(eidOf(b))) names.set(eidOf(b), b)
294
+ for (let b of w.tools) if (!tools.has(eidOf(b))) tools.set(eidOf(b), b)
295
+ for (let b of w.queries) {
296
+ if (!queries.has(eidOf(b))) queries.set(eidOf(b), b)
297
+ }
298
+ }
299
+ for (let id of [lead, ...contained.keys()]) names.delete(id)
300
+ return {
301
+ persona: head.persona,
302
+ contains: [...contained.values()],
303
+ names: [...names.values()],
304
+ tools: [...tools.values()],
305
+ queries: [...queries.values()],
306
+ }
307
+ }
308
+
309
+ /**
310
+ * What `w` says that `said` does not: a document `said` includes is dropped in
311
+ * both forms, and one it only names loses only its name, since including it in
312
+ * full still earns its place. `w` keeps its own lead and its tools, which are
313
+ * offered, not said, and its queries, whose rows are delivered. How a file
314
+ * read beside another avoids saying anything twice.
315
+ */
316
+ export let beside = (w: Worn, said: Worn): Worn => {
317
+ let included = new Set([said.persona, ...said.contains].map(eidOf))
318
+ let named = new Set([...included, ...said.names.map(eidOf)])
319
+ return {
320
+ persona: w.persona,
321
+ contains: w.contains.filter((b) => !included.has(eidOf(b))),
322
+ names: w.names.filter((b) => !named.has(eidOf(b))),
323
+ tools: w.tools,
324
+ queries: w.queries,
325
+ }
326
+ }
327
+
328
+ /** The names of the tools a session wearing `w` is offered: nothing where it
329
+ * contains none, which leaves its runner's own set. */
330
+ export let offer = (w: Worn | undefined): string[] | undefined => {
331
+ let names = (w?.tools ?? []).flatMap((b) => {
332
+ let name = (b[TOOL] as { name?: unknown }).name
333
+ return typeof name == 'string' && name ? [name] : []
334
+ })
335
+ return names.length ? names : undefined
336
+ }