@yaks/topic 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.
- package/LICENSE +202 -0
- package/README.md +256 -2
- package/_dist/context.d.ts +74 -0
- package/_dist/context.d.ts.map +1 -0
- package/_dist/graph.d.ts +5 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/mod.d.ts +33 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/share.d.ts +61 -0
- package/_dist/share.d.ts.map +1 -0
- package/_dist/shelves.d.ts +24 -0
- package/_dist/shelves.d.ts.map +1 -0
- package/_dist/tools.d.ts +5 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/topic.d.ts +50 -0
- package/_dist/topic.d.ts.map +1 -0
- package/_dist/views.d.ts +23 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/catalogue.js +155 -0
- package/catalogue.js.map +1 -0
- package/catalogue.ts +218 -0
- package/context.js +289 -0
- package/context.js.map +1 -0
- package/context.ts +318 -0
- package/deno.json +1 -0
- package/graph.js +32 -0
- package/graph.js.map +1 -0
- package/graph.ts +26 -0
- package/inspect.js +48 -0
- package/inspect.js.map +1 -0
- package/inspect.ts +25 -0
- package/mod.js +33 -0
- package/mod.js.map +1 -0
- package/mod.ts +56 -0
- package/package.json +50 -4
- package/page.js +247 -0
- package/page.js.map +1 -0
- package/page.ts +297 -0
- package/share.js +104 -0
- package/share.js.map +1 -0
- package/share.ts +115 -0
- package/shelves.js +99 -0
- package/shelves.js.map +1 -0
- package/shelves.ts +93 -0
- package/tools.js +114 -0
- package/tools.js.map +1 -0
- package/tools.ts +122 -0
- package/topic.js +97 -0
- package/topic.js.map +1 -0
- package/topic.ts +128 -0
- package/tree.js +77 -0
- package/tree.js.map +1 -0
- package/tree.ts +93 -0
- package/views.js +30 -0
- package/views.js.map +1 -0
- package/views.ts +26 -0
- package/vocab.js +16 -0
- package/vocab.js.map +1 -0
- package/vocab.json +186 -0
- package/vocab.ts +17 -0
package/shelves.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shelves: every topic laid out as a catalogue's tree. A shelf is a topic
|
|
3
|
+
* and the shelves under it, the topics it holds (`contains`); the top shelves
|
|
4
|
+
* are the topics no topic holds. Each level runs by title. A topic held by two
|
|
5
|
+
* stands under both, and one met again on its own path stands there without
|
|
6
|
+
* what it holds, so a cycle ends; topics held only by each other are still
|
|
7
|
+
* shelved, at the top. Pure: the topics and their `contains` edges in, the
|
|
8
|
+
* tree out, so the views draw it and a test reads it without a graph.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
|
|
14
|
+
import { about, fits, title } from './topic.js'
|
|
15
|
+
|
|
16
|
+
/** A topic, and the shelves of the topics it holds. */
|
|
17
|
+
export type Shelf = { topic: Bundle; under: Shelf[] }
|
|
18
|
+
|
|
19
|
+
let by = (a: Bundle, b: Bundle) =>
|
|
20
|
+
title(a).localeCompare(title(b), undefined, { sensitivity: 'base' })
|
|
21
|
+
|
|
22
|
+
let eidOf = (b: Bundle) => b.entity.eid
|
|
23
|
+
|
|
24
|
+
let end = (e: Bundle, side: 'from' | 'to'): string => {
|
|
25
|
+
let v = (e.edge as Record<string, unknown> | undefined)?.[side]
|
|
26
|
+
return typeof v == 'string' ? v : ''
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The topics as shelves, from the topics and the `contains` edges among them;
|
|
31
|
+
* an edge to or from anything else is left out.
|
|
32
|
+
*/
|
|
33
|
+
/// let t = (eid: string, body = '') => ({ entity: { eid }, doc: { title: eid, body } })
|
|
34
|
+
/// let held = (from: string, to: string) => ({ entity: { eid: from + to }, edge: { from, to }, contains: {} })
|
|
35
|
+
/// let names = (s: Shelf[]): unknown[] => s.map((x) => x.under.length ? [x.topic.entity.eid, names(x.under)] : x.topic.entity.eid)
|
|
36
|
+
/// names(shelves([t('b'), t('a'), t('c')], [held('a', 'c')])) -> [['a', ['c']], 'b']
|
|
37
|
+
/// names(shelves([t('a'), t('b')], [held('a', 'b'), held('b', 'a')])) -> [['a', [['b', ['a']]]]]
|
|
38
|
+
/// names(shelves([t('a')], [held('a', 'x')])) -> ['a']
|
|
39
|
+
export let shelves = (topics: Bundle[], edges: Bundle[]): Shelf[] => {
|
|
40
|
+
let all = new Map(topics.map((t) => [eidOf(t), t]))
|
|
41
|
+
let inside = new Map<string, Bundle[]>()
|
|
42
|
+
for (let e of edges) {
|
|
43
|
+
let [from, to] = [all.get(end(e, 'from')), all.get(end(e, 'to'))]
|
|
44
|
+
if (from && to) {
|
|
45
|
+
inside.set(eidOf(from), [...inside.get(eidOf(from)) ?? [], to])
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
let kept = new Set([...inside.values()].flat().map(eidOf))
|
|
49
|
+
let seen = new Set<string>()
|
|
50
|
+
let shelf = (t: Bundle, path: string[]): Shelf => {
|
|
51
|
+
let eid = eidOf(t)
|
|
52
|
+
seen.add(eid)
|
|
53
|
+
return {
|
|
54
|
+
topic: t,
|
|
55
|
+
under: path.includes(eid)
|
|
56
|
+
? []
|
|
57
|
+
: [...new Set(inside.get(eid) ?? [])].sort(by)
|
|
58
|
+
.map((u) => shelf(u, [...path, eid])),
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
let top = [...all.values()].filter((t) => !kept.has(eidOf(t))).sort(by)
|
|
62
|
+
.map((t) => shelf(t, []))
|
|
63
|
+
// What only a cycle holds is shelved at the top, from its first by title.
|
|
64
|
+
for (let t of [...all.values()].sort(by)) {
|
|
65
|
+
if (!seen.has(eidOf(t))) top.push(shelf(t, []))
|
|
66
|
+
}
|
|
67
|
+
return top
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The shelves narrowed to what was said: each topic whose words fit it
|
|
72
|
+
* (`fits`), under the topics that hold it. Nothing said narrows nothing.
|
|
73
|
+
*/
|
|
74
|
+
/// let s = shelves([t('data', 'how data is shaped'), t('ids', 'what names a thing'), t('git', 'commits')], [held('data', 'ids')])
|
|
75
|
+
/// names(within(s, 'names')) -> [['data', ['ids']]]
|
|
76
|
+
/// names(within(s, 'commit')) -> ['git']
|
|
77
|
+
/// names(within(s, '')) -> [['data', ['ids']], 'git']
|
|
78
|
+
export let within = (all: Shelf[], said: string): Shelf[] =>
|
|
79
|
+
all.flatMap((s) => {
|
|
80
|
+
let under = within(s.under, said)
|
|
81
|
+
return under.length || fits(said, about(s.topic)) ? [{ ...s, under }] : []
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
/** Every topic on the shelves, top to bottom, as they are drawn. */
|
|
85
|
+
/// every(s).map((b) => b.entity.eid) -> ['data', 'ids', 'git']
|
|
86
|
+
export let every = (all: Shelf[]): Bundle[] =>
|
|
87
|
+
all.flatMap((s) => [s.topic, ...every(s.under)])
|
|
88
|
+
|
|
89
|
+
/** Whether a shelf holds a topic somewhere under it. */
|
|
90
|
+
/// holds(s[0], 'ids') -> true
|
|
91
|
+
/// holds(s[0], 'data') -> false
|
|
92
|
+
export let holds = (s: Shelf, eid: string): boolean =>
|
|
93
|
+
s.under.some((u) => eidOf(u.topic) == eid || holds(u, eid))
|
package/tools.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// What anybody may ask of the topics: the `@yaks/topic/tools` entry point, the
|
|
2
|
+
// implementations behind the `tool: true` declarations in ./vocab.json. Find
|
|
3
|
+
// before you make: `topic_find` answers the topics that exist, and `topic_new`
|
|
4
|
+
// makes one only under a name no topic holds; the name's unique index refuses
|
|
5
|
+
// the second of two builds that looked at once and found nothing. `topic_add`
|
|
6
|
+
// puts things under a topic, a `contains` edge each, so whoever reads the
|
|
7
|
+
// topic finds them, and `topic_read` answers what a session opened on a topic
|
|
8
|
+
// is given: its tree, compiled (./context.ts).
|
|
9
|
+
import { argsOf } from "@yaks/graph/call";
|
|
10
|
+
import { Refused } from "@yaks/graph/errors";
|
|
11
|
+
import { link, unlink } from "@yaks/edge";
|
|
12
|
+
import { compile, gather } from "./context.js";
|
|
13
|
+
import { about, FIND, fits, found, HOLDS, named, overlaps, TOPIC } from "./topic.js";
|
|
14
|
+
let str = (v)=>v == null ? '' : String(v);
|
|
15
|
+
// A topic as a refusal names it: its id, title and the first line of its
|
|
16
|
+
// cover, since a project's cover runs on for pages.
|
|
17
|
+
let brief = (t)=>{
|
|
18
|
+
let doc = t.doc ?? {};
|
|
19
|
+
return `${t.entity.eid} (${str(doc.title)}: ${str(doc.body).split('\n')[0]})`;
|
|
20
|
+
};
|
|
21
|
+
let nameOf = (t)=>str(t[TOPIC]?.name);
|
|
22
|
+
/** The implementations of `topic_new`, `topic_find`, `topic_add` and
|
|
23
|
+
* `topic_read`. A factory, like every such module, although these need
|
|
24
|
+
* nothing from the server. */ export let runs = ()=>({
|
|
25
|
+
topic_new: async (call, graph)=>{
|
|
26
|
+
let args = argsOf(call);
|
|
27
|
+
let title = str(args.name).trim().replace(/\s+/g, ' ');
|
|
28
|
+
let body = str(args.body).trim();
|
|
29
|
+
if (!title) throw new Refused('a topic needs a name');
|
|
30
|
+
if (!body) {
|
|
31
|
+
throw new Refused('a topic needs a brief saying what belongs under it');
|
|
32
|
+
}
|
|
33
|
+
// Every topic, by name: they are few and broad, and a word longer than
|
|
34
|
+
// theirs ("markets" for "market design") no prefix search finds.
|
|
35
|
+
let topics = await graph.read(found({}));
|
|
36
|
+
let held = topics.find((t)=>nameOf(t) == named(title));
|
|
37
|
+
if (held) {
|
|
38
|
+
throw new Refused(`the topic ${named(title)} is ${brief(held)} already; use it`);
|
|
39
|
+
}
|
|
40
|
+
let same = topics.filter((t)=>nameOf(t) && overlaps(title, nameOf(t)));
|
|
41
|
+
if (same.length) {
|
|
42
|
+
throw new Refused(`${named(title)} names the subject of ${same.map(brief).join(', ')}; use it, or a name that holds none of its words`);
|
|
43
|
+
}
|
|
44
|
+
// The graph mints its id, and the answer names it, so a model reading the
|
|
45
|
+
// answer learns the id to use.
|
|
46
|
+
return [
|
|
47
|
+
{
|
|
48
|
+
entity: {
|
|
49
|
+
eid: '$topic'
|
|
50
|
+
},
|
|
51
|
+
[TOPIC]: {
|
|
52
|
+
name: named(title)
|
|
53
|
+
},
|
|
54
|
+
doc: {
|
|
55
|
+
title,
|
|
56
|
+
body
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
];
|
|
60
|
+
},
|
|
61
|
+
// Every topic, its words matched here (`fits`), the ones that fit answered
|
|
62
|
+
// whole, in the order they were read.
|
|
63
|
+
topic_find: async (call, graph)=>{
|
|
64
|
+
let args = argsOf(call);
|
|
65
|
+
let hits = (await graph.read(found({
|
|
66
|
+
near: str(args.near)
|
|
67
|
+
}))).filter((t)=>fits(str(args.said), about(t))).slice(0, Number(args.limit ?? FIND)).map((t)=>t.entity.eid);
|
|
68
|
+
let whole = new Map((hits.length ? await graph.get(hits) : []).map((b)=>[
|
|
69
|
+
b.entity.eid,
|
|
70
|
+
b
|
|
71
|
+
]));
|
|
72
|
+
return hits.flatMap((eid)=>whole.get(eid) ?? []);
|
|
73
|
+
},
|
|
74
|
+
// A `contains` edge from the topic to each entity, or each taken away. The
|
|
75
|
+
// topic is read first, so a word that names something else is refused
|
|
76
|
+
// rather than holding things nobody browses.
|
|
77
|
+
topic_add: async (call, graph)=>{
|
|
78
|
+
let args = argsOf(call);
|
|
79
|
+
let topic = str(args.topic);
|
|
80
|
+
let ids = [
|
|
81
|
+
args.ids
|
|
82
|
+
].flat().map(str).filter(Boolean);
|
|
83
|
+
let [held] = await graph.get([
|
|
84
|
+
topic
|
|
85
|
+
]);
|
|
86
|
+
if (!held || held.tombstone || !held[TOPIC]) {
|
|
87
|
+
throw new Refused(`${topic} is not a topic: find one with topic_find, or make one ` + 'with topic_new');
|
|
88
|
+
}
|
|
89
|
+
if (ids.includes(topic)) throw new Refused('a topic never holds itself');
|
|
90
|
+
let edge = args.drop ? unlink : link;
|
|
91
|
+
return ids.map((id)=>edge(topic, HOLDS, id));
|
|
92
|
+
},
|
|
93
|
+
// Text, as @yaks/persona's `persona_read` answers: a `content{body}` entity
|
|
94
|
+
// naming the call it came from.
|
|
95
|
+
topic_read: async (call, graph)=>{
|
|
96
|
+
let said = str(argsOf(call).topic).trim();
|
|
97
|
+
let got = said ? await gather(graph)(said) : undefined;
|
|
98
|
+
if (!got) throw new Refused(`no topic called ${said}`);
|
|
99
|
+
return [
|
|
100
|
+
{
|
|
101
|
+
entity: {
|
|
102
|
+
eid: '$topic'
|
|
103
|
+
},
|
|
104
|
+
content: {
|
|
105
|
+
body: compile(graph.vocab)(got)
|
|
106
|
+
},
|
|
107
|
+
output: {
|
|
108
|
+
source: call.entity.eid
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
];
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
//# sourceMappingURL=tools.js.map
|
package/tools.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.js","sources":["./tools.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,6EAA6E;AAC7E,+EAA+E;AAC/E,8EAA8E;AAC9E,8EAA8E;AAC9E,0EAA0E;AAC1E,8EAA8E;AAC9E,+CAA+C;AAG/C,SAAS,MAAM,gCAAqC;AACpD,SAAS,OAAO,kCAAuC;AAEvD,SAAS,IAAI,EAAE,MAAM,0BAA+B;AACpD,SAAS,OAAO,EAAE,MAAM,uBAAsB;AAC9C,SACE,KAAK,EACL,IAAI,EACJ,IAAI,EACJ,KAAK,EACL,KAAK,EACL,KAAK,EACL,QAAQ,EACR,KAAK,qBACY;AAEnB,IAAI,MAAM,CAAC,IAAuB,KAAK,OAAO,KAAK,OAAO;AAE1D,yEAAyE;AACzE,oDAAoD;AACpD,IAAI,QAAQ,CAAC;EACX,IAAI,MAAO,EAAE,GAAG,I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Y,OAAO,MAAM;MACvB,IAAI,OAAO,IAAI,OAAO,MAAM,KAAK,EAAE,IAAI;MACvC,IAAI,MAAM,OAAO,MAAM,OAAO,OAAO,QAAQ;MAC7C,IAAI,CAAC,KAAK,MAAM,IAAI,QAAQ,CAAC,gBAAgB,EAAE,MAAM;MACrD,OAAO;QAAC;UACN,QAAQ;YAAE,KAAK;UAAS;UACxB,SAAS;YAAE,MAAM,QAAQ,MAAM,KAAK,EAAE;UAAK;UAC3C,QAAQ;YAAE,QAAQ,KAAK,MAAM,CAAC,GAAG;UAAC;QACpC;OAAE;IACJ;EACF,CAAC,EAAC"}
|
package/tools.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// What anybody may ask of the topics: the `@yaks/topic/tools` entry point, the
|
|
2
|
+
// implementations behind the `tool: true` declarations in ./vocab.json. Find
|
|
3
|
+
// before you make: `topic_find` answers the topics that exist, and `topic_new`
|
|
4
|
+
// makes one only under a name no topic holds; the name's unique index refuses
|
|
5
|
+
// the second of two builds that looked at once and found nothing. `topic_add`
|
|
6
|
+
// puts things under a topic, a `contains` edge each, so whoever reads the
|
|
7
|
+
// topic finds them, and `topic_read` answers what a session opened on a topic
|
|
8
|
+
// is given: its tree, compiled (./context.ts).
|
|
9
|
+
|
|
10
|
+
import type { Bundle, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
11
|
+
import { argsOf } from '@yaks/graph/call'
|
|
12
|
+
import { Refused } from '@yaks/graph/errors'
|
|
13
|
+
import type { Runs } from 'jsr:@yaks/graph@0.2.36/tools'
|
|
14
|
+
import { link, unlink } from '@yaks/edge'
|
|
15
|
+
import { compile, gather } from './context.js'
|
|
16
|
+
import {
|
|
17
|
+
about,
|
|
18
|
+
FIND,
|
|
19
|
+
fits,
|
|
20
|
+
found,
|
|
21
|
+
HOLDS,
|
|
22
|
+
named,
|
|
23
|
+
overlaps,
|
|
24
|
+
TOPIC,
|
|
25
|
+
} from './topic.js'
|
|
26
|
+
|
|
27
|
+
let str = (v: unknown): string => v == null ? '' : String(v)
|
|
28
|
+
|
|
29
|
+
// A topic as a refusal names it: its id, title and the first line of its
|
|
30
|
+
// cover, since a project's cover runs on for pages.
|
|
31
|
+
let brief = (t: Bundle): string => {
|
|
32
|
+
let doc = (t.doc ?? {}) as { title?: string; body?: string }
|
|
33
|
+
return `${t.entity.eid} (${str(doc.title)}: ${str(doc.body).split('\n')[0]})`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let nameOf = (t: Bundle): string =>
|
|
37
|
+
str((t[TOPIC] as { name?: unknown } | undefined)?.name)
|
|
38
|
+
|
|
39
|
+
/** The implementations of `topic_new`, `topic_find`, `topic_add` and
|
|
40
|
+
* `topic_read`. A factory, like every such module, although these need
|
|
41
|
+
* nothing from the server. */
|
|
42
|
+
export let runs = (): Runs => ({
|
|
43
|
+
topic_new: async (call, graph: Graph): Promise<Bundle[]> => {
|
|
44
|
+
let args = argsOf(call)
|
|
45
|
+
let title = str(args.name).trim().replace(/\s+/g, ' ')
|
|
46
|
+
let body = str(args.body).trim()
|
|
47
|
+
if (!title) throw new Refused('a topic needs a name')
|
|
48
|
+
if (!body) {
|
|
49
|
+
throw new Refused('a topic needs a brief saying what belongs under it')
|
|
50
|
+
}
|
|
51
|
+
// Every topic, by name: they are few and broad, and a word longer than
|
|
52
|
+
// theirs ("markets" for "market design") no prefix search finds.
|
|
53
|
+
let topics = await graph.read(found({}))
|
|
54
|
+
let held = topics.find((t) => nameOf(t) == named(title))
|
|
55
|
+
if (held) {
|
|
56
|
+
throw new Refused(
|
|
57
|
+
`the topic ${named(title)} is ${brief(held)} already; use it`,
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
let same = topics.filter((t) => nameOf(t) && overlaps(title, nameOf(t)))
|
|
61
|
+
if (same.length) {
|
|
62
|
+
throw new Refused(
|
|
63
|
+
`${named(title)} names the subject of ${
|
|
64
|
+
same.map(brief).join(', ')
|
|
65
|
+
}; use it, or a name that holds none of its words`,
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
// The graph mints its id, and the answer names it, so a model reading the
|
|
69
|
+
// answer learns the id to use.
|
|
70
|
+
return [{
|
|
71
|
+
entity: { eid: '$topic' },
|
|
72
|
+
[TOPIC]: { name: named(title) },
|
|
73
|
+
doc: { title, body },
|
|
74
|
+
}]
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
// Every topic, its words matched here (`fits`), the ones that fit answered
|
|
78
|
+
// whole, in the order they were read.
|
|
79
|
+
topic_find: async (call, graph: Graph): Promise<Bundle[]> => {
|
|
80
|
+
let args = argsOf(call)
|
|
81
|
+
let hits = (await graph.read(found({ near: str(args.near) })))
|
|
82
|
+
.filter((t) => fits(str(args.said), about(t)))
|
|
83
|
+
.slice(0, Number(args.limit ?? FIND))
|
|
84
|
+
.map((t) => t.entity.eid)
|
|
85
|
+
let whole = new Map(
|
|
86
|
+
(hits.length ? await graph.get(hits) : []).map((b) => [b.entity.eid, b]),
|
|
87
|
+
)
|
|
88
|
+
return hits.flatMap((eid) => whole.get(eid) ?? [])
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
// A `contains` edge from the topic to each entity, or each taken away. The
|
|
92
|
+
// topic is read first, so a word that names something else is refused
|
|
93
|
+
// rather than holding things nobody browses.
|
|
94
|
+
topic_add: async (call, graph: Graph): Promise<Bundle[]> => {
|
|
95
|
+
let args = argsOf(call)
|
|
96
|
+
let topic = str(args.topic)
|
|
97
|
+
let ids = [args.ids].flat().map(str).filter(Boolean)
|
|
98
|
+
let [held] = await graph.get([topic])
|
|
99
|
+
if (!held || held.tombstone || !held[TOPIC]) {
|
|
100
|
+
throw new Refused(
|
|
101
|
+
`${topic} is not a topic: find one with topic_find, or make one ` +
|
|
102
|
+
'with topic_new',
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
if (ids.includes(topic)) throw new Refused('a topic never holds itself')
|
|
106
|
+
let edge = args.drop ? unlink : link
|
|
107
|
+
return ids.map((id) => edge(topic, HOLDS, id))
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
// Text, as @yaks/persona's `persona_read` answers: a `content{body}` entity
|
|
111
|
+
// naming the call it came from.
|
|
112
|
+
topic_read: async (call, graph: Graph): Promise<Bundle[]> => {
|
|
113
|
+
let said = str(argsOf(call).topic).trim()
|
|
114
|
+
let got = said ? await gather(graph)(said) : undefined
|
|
115
|
+
if (!got) throw new Refused(`no topic called ${said}`)
|
|
116
|
+
return [{
|
|
117
|
+
entity: { eid: '$topic' },
|
|
118
|
+
content: { body: compile(graph.vocab)(got) },
|
|
119
|
+
output: { source: call.entity.eid },
|
|
120
|
+
}]
|
|
121
|
+
},
|
|
122
|
+
})
|
package/topic.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// A topic's name and the query that finds topics: the pure half of the
|
|
2
|
+
// package, which ./tools.ts reads and writes with.
|
|
3
|
+
//
|
|
4
|
+
// The one danger is synonyms. A model names a subject by what it means, not by
|
|
5
|
+
// its letters, so left alone it calls the same subject "testing" in one build,
|
|
6
|
+
// "tests" in the next and "test suite" in a third, and what is known about it
|
|
7
|
+
// scatters across three topics that each hold a third (M-12915 on why). Three
|
|
8
|
+
// things stand against that. A topic's name is unique (`topic.name`'s index),
|
|
9
|
+
// folded to lowercase with single spaces on the way in whoever writes it
|
|
10
|
+
// (./graph.ts), so a name said twice is one topic, and the second of two builds at once is refused; whoever
|
|
11
|
+
// makes one is shown the topics that exist, nearest its words by meaning,
|
|
12
|
+
// first, so it reuses a subject already named; and `topic_new` refuses a name
|
|
13
|
+
// that is taken, or whose words hold another topic's or are held by them
|
|
14
|
+
// ("economic market design" beside "market design"), and says which topic to
|
|
15
|
+
// use. The name is not the topic's identity: any entity may wear `topic`, a
|
|
16
|
+
// project to be its own topic, and keeps its eid when its name changes.
|
|
17
|
+
import { line } from "@yaks/query";
|
|
18
|
+
import { derivedEid } from "@yaks/graph/identity";
|
|
19
|
+
import { tokens } from "@yaks/match";
|
|
20
|
+
import { stemmer } from "stemmer";
|
|
21
|
+
/** The component this package is about. */ export let TOPIC = 'topic';
|
|
22
|
+
/** The relation from a topic to what it holds (@yaks/task's word). */ export let HOLDS = 'contains';
|
|
23
|
+
/** The oracle: the one persona a session opened on a topic wears where whoever
|
|
24
|
+
* opened it named none. Its eid is derived from a fixed sentence, as
|
|
25
|
+
* @yaks/harness's own row is, so every graph names it alike and no door looks
|
|
26
|
+
* it up; what it says is a row like any persona's. */ export let ORACLE = derivedEid('@yaks/topic oracle');
|
|
27
|
+
/** How many topics a find returns when the caller gave no limit. */ export let FIND = 12;
|
|
28
|
+
/** How many topics a reading of every one reads at most: they are few and
|
|
29
|
+
* broad, so a find reads them all and matches the words here. */ export let TOPICS = 100000;
|
|
30
|
+
/**
|
|
31
|
+
* A topic's name: its title in lowercase, the spaces single.
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* named(' UI Components ') // 'ui components'
|
|
35
|
+
* ```
|
|
36
|
+
*/ export let named = (title)=>title.trim().replace(/\s+/g, ' ').toLowerCase();
|
|
37
|
+
let prop = (t, comp, name)=>{
|
|
38
|
+
let v = t[comp]?.[name];
|
|
39
|
+
return typeof v == 'string' ? v : '';
|
|
40
|
+
};
|
|
41
|
+
/** What a topic is called: its cover's title, else its name. */ /// title({ entity: { eid: 'q' }, topic: { name: 'data model' } }) -> 'data model'
|
|
42
|
+
export let title = (t)=>prop(t, 'doc', 'title') || prop(t, TOPIC, 'name');
|
|
43
|
+
/** What a list of topics shows under its title: the first paragraph of its
|
|
44
|
+
* cover, since a project's cover is its whole description. */ /// cover({ entity: { eid: 'p' }, doc: { body: 'A game.\n\nIts engine is…' } }) -> 'A game.'
|
|
45
|
+
/// cover({ entity: { eid: 'q' } }) -> ''
|
|
46
|
+
export let cover = (t)=>prop(t, 'doc', 'body').trim().split(/\n\s*\n/)[0];
|
|
47
|
+
/**
|
|
48
|
+
* Whether two names name one subject by their words: every word of one starts
|
|
49
|
+
* a word of the other, or is started by one.
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* overlaps('economic market design', 'market design') // true
|
|
53
|
+
* overlaps('tests', 'testing and ci') // false
|
|
54
|
+
* overlaps('test', 'testing and ci') // true
|
|
55
|
+
* overlaps('ui design', 'cover design') // false
|
|
56
|
+
* ```
|
|
57
|
+
*/ export let overlaps = (a, b)=>{
|
|
58
|
+
let holds = (xs, ys)=>ys.every((y)=>xs.some((x)=>x.startsWith(y) || y.startsWith(x)));
|
|
59
|
+
return holds(tokens(a), tokens(b)) || holds(tokens(b), tokens(a));
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Whether what was said finds a topic's words: each word said starts one of
|
|
63
|
+
* them, as it is typed ("dat" finds "data"), or shares its stem ("data
|
|
64
|
+
* modeling" finds "data model", "tests" finds "testing"). Nothing said finds
|
|
65
|
+
* every topic.
|
|
66
|
+
*/ /// fits('data modeling', 'data model') -> true
|
|
67
|
+
/// fits('Tests', 'what a test checks') -> true
|
|
68
|
+
/// fits('dat mod', 'data model') -> true
|
|
69
|
+
/// fits('data storage', 'data model') -> false
|
|
70
|
+
/// fits('', 'anything') -> true
|
|
71
|
+
export let fits = (said, text)=>{
|
|
72
|
+
let have = tokens(text);
|
|
73
|
+
let stems = have.map((t)=>stemmer(t));
|
|
74
|
+
return tokens(said).every((w)=>{
|
|
75
|
+
let stem = stemmer(w);
|
|
76
|
+
return have.some((t, i)=>t.startsWith(w) || stems[i].startsWith(stem));
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
/** The words a topic is found by: its name, its title and its cover. */ /// about({ entity: { eid: 'q' }, topic: { name: 'data model' }, doc: { title: 'Data model', body: 'How data is shaped.' } })
|
|
80
|
+
/// -> 'data model Data model How data is shaped.'
|
|
81
|
+
export let about = (t)=>[
|
|
82
|
+
prop(t, TOPIC, 'name'),
|
|
83
|
+
prop(t, 'doc', 'title'),
|
|
84
|
+
prop(t, 'doc', 'body')
|
|
85
|
+
].filter(Boolean).join(' ');
|
|
86
|
+
/**
|
|
87
|
+
* The query string that reads every topic with its name and cover, ranked by
|
|
88
|
+
* meaning to `near`, or else in order of name. A find keeps those whose words
|
|
89
|
+
* fit what was said (`fits`).
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* found({}) // '.topic&?doc&.order=topic.name&.limit=100000'
|
|
93
|
+
* found({ near: 'T-9' })
|
|
94
|
+
* // '.near=T-9&.topic&?doc&.order=similar&.limit=100000'
|
|
95
|
+
* ```
|
|
96
|
+
*/ export let found = (o)=>o.near ? line`.near=${o.near}&.${TOPIC}&?doc&.order=similar&.limit=${TOPICS}` : line`.${TOPIC}&?doc&.order=${TOPIC}.name&.limit=${TOPICS}`;
|
|
97
|
+
//# sourceMappingURL=topic.js.map
|
package/topic.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"topic.js","sources":["./topic.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,mDAAmD;AACnD,EAAE;AACF,+EAA+E;AAC/E,+EAA+E;AAC/E,8EAA8E;AAC9E,8EAA8E;AAC9E,8EAA8E;AAC9E,yEAAyE;AACzE,4GAA4G;AAC5G,0EAA0E;AAC1E,8EAA8E;AAC9E,yEAAyE;AACzE,6EAA6E;AAC7E,4EAA4E;AAC5E,wEAAwE;AAGxE,SAAqB,IAAI,2BAAgC;AACzD,SAAS,UAAU,oCAAyC;AAC5D,SAAS,MAAM,2BAAgC;AAC/C,SAAS,OAAO,kBAA2B;AAE3C,yCAAyC,GACzC,OAAO,IAAI,QAAQ,QAAO;AAE1B,oEAAoE,GACpE,OAAO,IAAI,QAAQ,WAAU;AAE7B;;;oDAGoD,GACpD,OAAO,IAAI,SAAiB,WAAW,sBAAqB;AAE5D,kEAAkE,GAClE,OAAO,IAAI,OAAO,GAAE;AAEpB;+DAC+D,GAC/D,OAAO,IAAI,SAAS,OAAM;AAE1B;;;;;;CAMC,GACD,OAAO,IAAI,QAAQ,CAAC,QAClB,MAAM,IAAI,GA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AAS,KAAK,GAAG,OAAO;IAAU,KAAK,GAAG,OAAO;GAAQ,CACtE,MAAM,CAAC,SAAS,IAAI,CAAC,KAAI;AAE9B;;;;;;;;;;CAUC,GACD,OAAO,IAAI,QAAQ,CAAC,IAClB,EAAE,IAAI,GACF,IAAI,CAAC,MAAM,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,MAAM,4BAA4B,EAAE,OAAO,CAAC,GACpE,IAAI,CAAC,CAAC,EAAE,MAAM,aAAa,EAAE,MAAM,aAAa,EAAE,OAAO,CAAC,CAAA"}
|
package/topic.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// A topic's name and the query that finds topics: the pure half of the
|
|
2
|
+
// package, which ./tools.ts reads and writes with.
|
|
3
|
+
//
|
|
4
|
+
// The one danger is synonyms. A model names a subject by what it means, not by
|
|
5
|
+
// its letters, so left alone it calls the same subject "testing" in one build,
|
|
6
|
+
// "tests" in the next and "test suite" in a third, and what is known about it
|
|
7
|
+
// scatters across three topics that each hold a third (M-12915 on why). Three
|
|
8
|
+
// things stand against that. A topic's name is unique (`topic.name`'s index),
|
|
9
|
+
// folded to lowercase with single spaces on the way in whoever writes it
|
|
10
|
+
// (./graph.ts), so a name said twice is one topic, and the second of two builds at once is refused; whoever
|
|
11
|
+
// makes one is shown the topics that exist, nearest its words by meaning,
|
|
12
|
+
// first, so it reuses a subject already named; and `topic_new` refuses a name
|
|
13
|
+
// that is taken, or whose words hold another topic's or are held by them
|
|
14
|
+
// ("economic market design" beside "market design"), and says which topic to
|
|
15
|
+
// use. The name is not the topic's identity: any entity may wear `topic`, a
|
|
16
|
+
// project to be its own topic, and keeps its eid when its name changes.
|
|
17
|
+
|
|
18
|
+
import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
|
|
19
|
+
import { type Built, line } from '@yaks/query'
|
|
20
|
+
import { derivedEid } from '@yaks/graph/identity'
|
|
21
|
+
import { tokens } from '@yaks/match'
|
|
22
|
+
import { stemmer } from 'stemmer'
|
|
23
|
+
|
|
24
|
+
/** The component this package is about. */
|
|
25
|
+
export let TOPIC = 'topic'
|
|
26
|
+
|
|
27
|
+
/** The relation from a topic to what it holds (@yaks/task's word). */
|
|
28
|
+
export let HOLDS = 'contains'
|
|
29
|
+
|
|
30
|
+
/** The oracle: the one persona a session opened on a topic wears where whoever
|
|
31
|
+
* opened it named none. Its eid is derived from a fixed sentence, as
|
|
32
|
+
* @yaks/harness's own row is, so every graph names it alike and no door looks
|
|
33
|
+
* it up; what it says is a row like any persona's. */
|
|
34
|
+
export let ORACLE: string = derivedEid('@yaks/topic oracle')
|
|
35
|
+
|
|
36
|
+
/** How many topics a find returns when the caller gave no limit. */
|
|
37
|
+
export let FIND = 12
|
|
38
|
+
|
|
39
|
+
/** How many topics a reading of every one reads at most: they are few and
|
|
40
|
+
* broad, so a find reads them all and matches the words here. */
|
|
41
|
+
export let TOPICS = 100000
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A topic's name: its title in lowercase, the spaces single.
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* named(' UI Components ') // 'ui components'
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export let named = (title: string): string =>
|
|
51
|
+
title.trim().replace(/\s+/g, ' ').toLowerCase()
|
|
52
|
+
|
|
53
|
+
let prop = (t: Bundle, comp: string, name: string): string => {
|
|
54
|
+
let v = (t[comp] as Record<string, unknown> | undefined)?.[name]
|
|
55
|
+
return typeof v == 'string' ? v : ''
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** What a topic is called: its cover's title, else its name. */
|
|
59
|
+
/// title({ entity: { eid: 'q' }, topic: { name: 'data model' } }) -> 'data model'
|
|
60
|
+
export let title = (t: Bundle): string =>
|
|
61
|
+
prop(t, 'doc', 'title') || prop(t, TOPIC, 'name')
|
|
62
|
+
|
|
63
|
+
/** What a list of topics shows under its title: the first paragraph of its
|
|
64
|
+
* cover, since a project's cover is its whole description. */
|
|
65
|
+
/// cover({ entity: { eid: 'p' }, doc: { body: 'A game.\n\nIts engine is…' } }) -> 'A game.'
|
|
66
|
+
/// cover({ entity: { eid: 'q' } }) -> ''
|
|
67
|
+
export let cover = (t: Bundle): string =>
|
|
68
|
+
prop(t, 'doc', 'body').trim().split(/\n\s*\n/)[0]
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Whether two names name one subject by their words: every word of one starts
|
|
72
|
+
* a word of the other, or is started by one.
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* overlaps('economic market design', 'market design') // true
|
|
76
|
+
* overlaps('tests', 'testing and ci') // false
|
|
77
|
+
* overlaps('test', 'testing and ci') // true
|
|
78
|
+
* overlaps('ui design', 'cover design') // false
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export let overlaps = (a: string, b: string): boolean => {
|
|
82
|
+
let holds = (xs: string[], ys: string[]) =>
|
|
83
|
+
ys.every((y) => xs.some((x) => x.startsWith(y) || y.startsWith(x)))
|
|
84
|
+
return holds(tokens(a), tokens(b)) || holds(tokens(b), tokens(a))
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Whether what was said finds a topic's words: each word said starts one of
|
|
89
|
+
* them, as it is typed ("dat" finds "data"), or shares its stem ("data
|
|
90
|
+
* modeling" finds "data model", "tests" finds "testing"). Nothing said finds
|
|
91
|
+
* every topic.
|
|
92
|
+
*/
|
|
93
|
+
/// fits('data modeling', 'data model') -> true
|
|
94
|
+
/// fits('Tests', 'what a test checks') -> true
|
|
95
|
+
/// fits('dat mod', 'data model') -> true
|
|
96
|
+
/// fits('data storage', 'data model') -> false
|
|
97
|
+
/// fits('', 'anything') -> true
|
|
98
|
+
export let fits = (said: string, text: string): boolean => {
|
|
99
|
+
let have = tokens(text)
|
|
100
|
+
let stems = have.map((t) => stemmer(t))
|
|
101
|
+
return tokens(said).every((w) => {
|
|
102
|
+
let stem = stemmer(w)
|
|
103
|
+
return have.some((t, i) => t.startsWith(w) || stems[i].startsWith(stem))
|
|
104
|
+
})
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The words a topic is found by: its name, its title and its cover. */
|
|
108
|
+
/// about({ entity: { eid: 'q' }, topic: { name: 'data model' }, doc: { title: 'Data model', body: 'How data is shaped.' } })
|
|
109
|
+
/// -> 'data model Data model How data is shaped.'
|
|
110
|
+
export let about = (t: Bundle): string =>
|
|
111
|
+
[prop(t, TOPIC, 'name'), prop(t, 'doc', 'title'), prop(t, 'doc', 'body')]
|
|
112
|
+
.filter(Boolean).join(' ')
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The query string that reads every topic with its name and cover, ranked by
|
|
116
|
+
* meaning to `near`, or else in order of name. A find keeps those whose words
|
|
117
|
+
* fit what was said (`fits`).
|
|
118
|
+
*
|
|
119
|
+
* ```ts
|
|
120
|
+
* found({}) // '.topic&?doc&.order=topic.name&.limit=100000'
|
|
121
|
+
* found({ near: 'T-9' })
|
|
122
|
+
* // '.near=T-9&.topic&?doc&.order=similar&.limit=100000'
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export let found = (o: { near?: string }): Built =>
|
|
126
|
+
o.near
|
|
127
|
+
? line`.near=${o.near}&.${TOPIC}&?doc&.order=similar&.limit=${TOPICS}`
|
|
128
|
+
: line`.${TOPIC}&?doc&.order=${TOPIC}.name&.limit=${TOPICS}`
|
package/tree.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A topic's tree: everything it holds (`contains`), and everything each
|
|
3
|
+
* subtopic holds, all the way down, nested. Each level asks for its own
|
|
4
|
+
* children as it draws, so the tree reads only what is open on the screen.
|
|
5
|
+
* Only a topic is walked into: a project or a package a topic holds is a leaf
|
|
6
|
+
* here, since what it contains is its own tree. A topic met again on its own
|
|
7
|
+
* path is drawn and not walked, so a cycle ends; past `DEPTH` levels the
|
|
8
|
+
* branch links to its own tree.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/ /* @ts-types="npm:@types/react@19.3.0" */ import { createElement as h } from "react";
|
|
12
|
+
import * as ui from "@yaks/ui";
|
|
13
|
+
import { mention } from "@yaks/inspect";
|
|
14
|
+
import { EDGES } from "./page.js";
|
|
15
|
+
import { title, TOPIC } from "./topic.js";
|
|
16
|
+
/** How many levels a tree draws before a branch links to its own tree. */ export let DEPTH = 8;
|
|
17
|
+
let rows = (a)=>a?.rows ?? [];
|
|
18
|
+
let to = (b)=>String(b.edge?.to ?? '');
|
|
19
|
+
/** What a topic holds, each drawn by its own renderer, and below each
|
|
20
|
+
* subtopic what it holds. */ let Twig = ({ eid, io, path })=>{
|
|
21
|
+
let got = io.ask({
|
|
22
|
+
edges: `.contains&.edge.from=${eid}&?edge&.order=edge.ord&.limit=${EDGES}`
|
|
23
|
+
}).edges;
|
|
24
|
+
let ids = [
|
|
25
|
+
...new Set(rows(got).map(to).filter(Boolean))
|
|
26
|
+
];
|
|
27
|
+
let ends = io.ask(ids.length ? {
|
|
28
|
+
ends: `.entity.eid=${ids.join(',')}&*`
|
|
29
|
+
} : {}).ends;
|
|
30
|
+
let held = new Map(rows(ends).map((b)=>[
|
|
31
|
+
b.entity.eid,
|
|
32
|
+
b
|
|
33
|
+
]));
|
|
34
|
+
let root = path.length == 1;
|
|
35
|
+
if (got?.error) return /*#__PURE__*/ h(ui.Rows.More, {}, got.error);
|
|
36
|
+
if (!got?.ready && !ids.length) return root ? /*#__PURE__*/ h(ui.Loading, {}) : null;
|
|
37
|
+
if (!ids.length) {
|
|
38
|
+
return root ? /*#__PURE__*/ h(ui.Rows.More, {}, 'it holds nothing yet') : null;
|
|
39
|
+
}
|
|
40
|
+
return /*#__PURE__*/ h(ui.Rows, {
|
|
41
|
+
mod: root ? undefined : 'nested',
|
|
42
|
+
role: 'list'
|
|
43
|
+
}, ids.map((id)=>{
|
|
44
|
+
let b = held.get(id);
|
|
45
|
+
let walk = b?.[TOPIC] && !path.includes(id);
|
|
46
|
+
return /*#__PURE__*/ h(ui.Rows.Item, {
|
|
47
|
+
key: id,
|
|
48
|
+
role: 'listitem',
|
|
49
|
+
'data-held': id
|
|
50
|
+
}, b ? io.show(b, 'Topic.List.Tile', {}) : ends?.ready ? /*#__PURE__*/ h(ui.Rows.More, {}, mention(io, id), ' is gone') : /*#__PURE__*/ h(ui.Loading, {}), !walk ? null : path.length < DEPTH ? /*#__PURE__*/ h(Twig, {
|
|
51
|
+
eid: id,
|
|
52
|
+
io,
|
|
53
|
+
path: [
|
|
54
|
+
...path,
|
|
55
|
+
id
|
|
56
|
+
]
|
|
57
|
+
}) : /*#__PURE__*/ h(ui.Rows.More, {}, /*#__PURE__*/ h('a', {
|
|
58
|
+
href: io.link(id)
|
|
59
|
+
}, 'more below')));
|
|
60
|
+
}));
|
|
61
|
+
};
|
|
62
|
+
/** A topic's tree. */ export let TopicTree = ({ e, io })=>{
|
|
63
|
+
let eid = e.entity.eid;
|
|
64
|
+
let cover = String(e.doc?.body ?? '');
|
|
65
|
+
return /*#__PURE__*/ h('div', {
|
|
66
|
+
'data-tree': eid
|
|
67
|
+
}, /*#__PURE__*/ h(ui.Head, {}, /*#__PURE__*/ h(ui.Head.Title, {}, title(e), /*#__PURE__*/ h(ui.Head.Kind, {}, 'topic')), cover ? /*#__PURE__*/ h(ui.Head.Sub, {}, cover) : null), /*#__PURE__*/ h(ui.Section, {
|
|
68
|
+
'data-section': 'Tree'
|
|
69
|
+
}, /*#__PURE__*/ h(Twig, {
|
|
70
|
+
eid,
|
|
71
|
+
io,
|
|
72
|
+
path: [
|
|
73
|
+
eid
|
|
74
|
+
]
|
|
75
|
+
})));
|
|
76
|
+
};
|
|
77
|
+
//# sourceMappingURL=tree.js.map
|
package/tree.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tree.js","sources":["./tree.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;CAUC,GAED,uCAAuC,GAAG,SAAS,iBAAiB,CAAC,gBAA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