@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/context.js
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
// What a session opened on a topic is given: the topic tree compiled from that
|
|
2
|
+
// topic, the way a persona is compiled from what it contains (@yaks/persona's
|
|
3
|
+
// `wear` and `voice`). {@link gather} reads it out of the graph and
|
|
4
|
+
// {@link compile} writes it as one markdown document, a pure function of what
|
|
5
|
+
// was read.
|
|
6
|
+
//
|
|
7
|
+
// In order: where the topic sits (the topics that contain it, up to the roots,
|
|
8
|
+
// each by its cover), the topic itself (its cover, its contents, and the
|
|
9
|
+
// `$was` token of the contents, so a session rewrites them without reading
|
|
10
|
+
// them again), everything it holds in full, the topics below it as an outline,
|
|
11
|
+
// and what to understand first, what is related and its sources, a line each.
|
|
12
|
+
//
|
|
13
|
+
// What it holds is said in full and what lies below is only named, as a
|
|
14
|
+
// persona includes what it contains and names what it reads: a root topic's
|
|
15
|
+
// session would otherwise carry the whole graph. A body too long to carry is
|
|
16
|
+
// cut at a paragraph and says how to read the rest, and what is past the
|
|
17
|
+
// budget is listed by id. What does not fit is listed, never dropped.
|
|
18
|
+
//
|
|
19
|
+
// The ids are the human ones (Q-12, M-3) a session passes back to graph_show;
|
|
20
|
+
// every title goes through @yaks/text's `safe`, and a body does not, since
|
|
21
|
+
// `safe` would strip the line breaks that make it a document.
|
|
22
|
+
import { line } from "@yaks/query";
|
|
23
|
+
import { token } from "@yaks/graph/sha256";
|
|
24
|
+
import { human } from "@yaks/id";
|
|
25
|
+
import { safe } from "@yaks/text";
|
|
26
|
+
import { cover, HOLDS, title, TOPIC } from "./topic.js";
|
|
27
|
+
/** How many characters of one held entity's body are carried; the rest is
|
|
28
|
+
* named, to be read with graph_show. */ export let CUT = 4000;
|
|
29
|
+
/** How many characters of held bodies a topic carries in all; past it, what
|
|
30
|
+
* it holds is listed by id and title. */ export let BUDGET = 100000;
|
|
31
|
+
/** How many levels below the topic its outline goes. */ export let LEVELS = 4;
|
|
32
|
+
/** How many lines the outline below a topic holds at most. */ export let LINES = 400;
|
|
33
|
+
/** How many characters of its first line name an entity that has no title. */ export let GIST = 80;
|
|
34
|
+
// How many edges are read from one topic, or from one level of the tree.
|
|
35
|
+
let EDGES = 500;
|
|
36
|
+
// How far up the topics containing it are followed: deeper means a cycle,
|
|
37
|
+
// which `seen` already stops.
|
|
38
|
+
let DEPTH = 16;
|
|
39
|
+
let str = (b, comp, prop)=>{
|
|
40
|
+
let v = b?.[comp]?.[prop];
|
|
41
|
+
return typeof v == 'string' ? v : '';
|
|
42
|
+
};
|
|
43
|
+
let eidOf = (b)=>b.entity.eid;
|
|
44
|
+
let end = (b, side)=>str(b, 'edge', side);
|
|
45
|
+
let unique = (ids)=>[
|
|
46
|
+
...new Set(ids.filter(Boolean))
|
|
47
|
+
];
|
|
48
|
+
let byTitle = (a, b)=>title(a).localeCompare(title(b), undefined, {
|
|
49
|
+
sensitivity: 'base'
|
|
50
|
+
});
|
|
51
|
+
// The live rows of some eids, by eid.
|
|
52
|
+
let rows = async (g, ids)=>new Map((ids.length ? await g.get(ids) : []).filter((b)=>!b.tombstone).map((b)=>[
|
|
53
|
+
eidOf(b),
|
|
54
|
+
b
|
|
55
|
+
]));
|
|
56
|
+
// The topics containing `eid`, a level at a time, root first.
|
|
57
|
+
let above = async (g, eid)=>{
|
|
58
|
+
let seen = new Set([
|
|
59
|
+
eid
|
|
60
|
+
]);
|
|
61
|
+
let levels = [];
|
|
62
|
+
let level = [
|
|
63
|
+
eid
|
|
64
|
+
];
|
|
65
|
+
for(let i = 0; i < DEPTH && level.length; i++){
|
|
66
|
+
let edges = await g.read(line`.${HOLDS}&.edge.to=${level}&?edge&.limit=${EDGES}`);
|
|
67
|
+
let ids = unique(edges.map((b)=>end(b, 'from'))).filter((id)=>!seen.has(id));
|
|
68
|
+
let tops = [
|
|
69
|
+
...(await rows(g, ids)).values()
|
|
70
|
+
].filter((b)=>b[TOPIC]).toSorted(byTitle);
|
|
71
|
+
for (let b of tops)seen.add(eidOf(b));
|
|
72
|
+
levels.push(tops);
|
|
73
|
+
level = tops.map(eidOf);
|
|
74
|
+
}
|
|
75
|
+
return levels.reverse().flat();
|
|
76
|
+
};
|
|
77
|
+
// The topics below some subtopics, a level at a time: each one's holdings by
|
|
78
|
+
// title and the topics it contains, {@link LEVELS} deep and {@link LINES}
|
|
79
|
+
// lines long. A topic met twice is drawn once.
|
|
80
|
+
let outline = async (g, tops, seen)=>{
|
|
81
|
+
let branches = tops.map((topic)=>({
|
|
82
|
+
topic,
|
|
83
|
+
holds: [],
|
|
84
|
+
below: []
|
|
85
|
+
}));
|
|
86
|
+
for (let b of tops)seen.add(eidOf(b));
|
|
87
|
+
let level = branches;
|
|
88
|
+
let lines = branches.length;
|
|
89
|
+
for(let depth = 1; depth < LEVELS && level.length; depth++){
|
|
90
|
+
let at = new Map(level.map((b)=>[
|
|
91
|
+
eidOf(b.topic),
|
|
92
|
+
b
|
|
93
|
+
]));
|
|
94
|
+
let edges = await g.read(line`.${HOLDS}&.edge.from=${[
|
|
95
|
+
...at.keys()
|
|
96
|
+
]}&?edge&.order=edge.ord&.limit=${EDGES}`);
|
|
97
|
+
let ends = await rows(g, unique(edges.map((b)=>end(b, 'to'))));
|
|
98
|
+
let next = [];
|
|
99
|
+
for (let e of edges){
|
|
100
|
+
let parent = at.get(end(e, 'from'));
|
|
101
|
+
let held = ends.get(end(e, 'to'));
|
|
102
|
+
if (!parent || !held || lines >= LINES) continue;
|
|
103
|
+
if (held[TOPIC]) {
|
|
104
|
+
if (seen.has(eidOf(held))) continue;
|
|
105
|
+
seen.add(eidOf(held));
|
|
106
|
+
let branch = {
|
|
107
|
+
topic: held,
|
|
108
|
+
holds: [],
|
|
109
|
+
below: []
|
|
110
|
+
};
|
|
111
|
+
parent.below.push(branch);
|
|
112
|
+
next.push(branch);
|
|
113
|
+
} else parent.holds.push(held);
|
|
114
|
+
lines++;
|
|
115
|
+
}
|
|
116
|
+
level = next;
|
|
117
|
+
}
|
|
118
|
+
return branches;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* A topic's tree, read out of a graph: where it sits, what it holds, what
|
|
122
|
+
* lies below it, and what it requires, relates to and cites. `undefined` when
|
|
123
|
+
* the eid names no topic, which is the caller's to refuse.
|
|
124
|
+
*
|
|
125
|
+
* ```ts ignore
|
|
126
|
+
* let got = await gather(g)('Q-59416')
|
|
127
|
+
* if (got) console.log(compile(g.vocab)(got))
|
|
128
|
+
* ```
|
|
129
|
+
*/ export let gather = (g)=>async (eid)=>{
|
|
130
|
+
let [topic] = await g.get([
|
|
131
|
+
eid
|
|
132
|
+
]);
|
|
133
|
+
if (!topic?.[TOPIC] || topic.tombstone) return undefined;
|
|
134
|
+
let out = await g.read(line`.edge.from=${eid}&*&.order=edge.ord&.limit=${EDGES}`);
|
|
135
|
+
let ends = await rows(g, unique(out.map((b)=>end(b, 'to'))));
|
|
136
|
+
let along = (rels)=>unique(out.filter((b)=>rels.some((r)=>b[r])).map((b)=>end(b, 'to'))).flatMap((id)=>ends.get(id) ?? []);
|
|
137
|
+
let held = along([
|
|
138
|
+
HOLDS
|
|
139
|
+
]);
|
|
140
|
+
return {
|
|
141
|
+
topic,
|
|
142
|
+
path: await above(g, eid),
|
|
143
|
+
holds: held.filter((b)=>!b[TOPIC]),
|
|
144
|
+
below: await outline(g, held.filter((b)=>b[TOPIC]), new Set([
|
|
145
|
+
eid
|
|
146
|
+
])),
|
|
147
|
+
needs: along([
|
|
148
|
+
'requires'
|
|
149
|
+
]),
|
|
150
|
+
related: along([
|
|
151
|
+
'reads',
|
|
152
|
+
'references'
|
|
153
|
+
]),
|
|
154
|
+
sources: along([
|
|
155
|
+
'cites'
|
|
156
|
+
])
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
// A body as a session reads it: a document's own, or else what its content
|
|
160
|
+
// says (a transcript entry, a memory's marked words).
|
|
161
|
+
let body = (b)=>(str(b, 'doc', 'body') || str(b, 'content', 'body')).trim();
|
|
162
|
+
/** A body cut to at most `at` characters, at the last paragraph (else line)
|
|
163
|
+
* break past half of it, and how many characters were left off. */ /// cut('one\n\ntwo', 100) -> { text: 'one\n\ntwo', left: 0 }
|
|
164
|
+
/// cut('one two\n\nthree four', 12) -> { text: 'one two', left: 12 }
|
|
165
|
+
export let cut = (text, at)=>{
|
|
166
|
+
if (text.length <= at) return {
|
|
167
|
+
text,
|
|
168
|
+
left: 0
|
|
169
|
+
};
|
|
170
|
+
let head = text.slice(0, at);
|
|
171
|
+
let par = head.lastIndexOf('\n\n');
|
|
172
|
+
let line = head.lastIndexOf('\n');
|
|
173
|
+
let stop = par > at / 2 ? par : line > at / 2 ? line : at;
|
|
174
|
+
let kept = text.slice(0, stop).trimEnd();
|
|
175
|
+
return {
|
|
176
|
+
text: kept,
|
|
177
|
+
left: text.length - kept.length
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* A topic's tree as one markdown document, the context a session opened on it
|
|
182
|
+
* starts from. Takes the loaded vocabulary first, for the ids it names:
|
|
183
|
+
* `compile(vocab)(gathered)`.
|
|
184
|
+
*
|
|
185
|
+
* ```md
|
|
186
|
+
* # Q-1 Data model
|
|
187
|
+
*
|
|
188
|
+
* How data is shaped in the graph.
|
|
189
|
+
*
|
|
190
|
+
* ## Within
|
|
191
|
+
*
|
|
192
|
+
* - Q-2 Architecture: how the system is built
|
|
193
|
+
*
|
|
194
|
+
* ## Contents
|
|
195
|
+
*
|
|
196
|
+
* What is understood about it.
|
|
197
|
+
*
|
|
198
|
+
* $was of its contents: {"content": {"body": "9f86…"}}
|
|
199
|
+
*
|
|
200
|
+
* ---
|
|
201
|
+
*
|
|
202
|
+
* # M-3 Stored state properties are an anti-pattern
|
|
203
|
+
*
|
|
204
|
+
* a held entity's body, in full
|
|
205
|
+
*
|
|
206
|
+
* ---
|
|
207
|
+
*
|
|
208
|
+
* ## Below
|
|
209
|
+
*
|
|
210
|
+
* - Q-4 Identity: ids, names and keys
|
|
211
|
+
* - M-5 Names in any system must always be changeable
|
|
212
|
+
* ```
|
|
213
|
+
*/ export let compile = (vocab)=>{
|
|
214
|
+
let id = human(vocab);
|
|
215
|
+
let called = (b)=>b[TOPIC] ? title(b) : str(b, 'doc', 'title');
|
|
216
|
+
// Its id and title: a part's heading, whose body follows.
|
|
217
|
+
let headed = (b)=>safe(`${id(b)} ${called(b)}`.trim());
|
|
218
|
+
// Its id and title, or the start of what it says where it has no title (his
|
|
219
|
+
// words in a comment or an entry): a line read with nothing under it.
|
|
220
|
+
let named = (b)=>{
|
|
221
|
+
let first = body(b).split('\n').find((l)=>l.trim())?.trim() ?? '';
|
|
222
|
+
let gist = first.length > GIST ? `${first.slice(0, GIST - 1).trimEnd()}…` : first;
|
|
223
|
+
return safe(`${id(b)} ${called(b) || gist}`.trim());
|
|
224
|
+
};
|
|
225
|
+
// Its id, title and cover's first paragraph: a topic as a list names it.
|
|
226
|
+
let covered = (b)=>{
|
|
227
|
+
let said = safe(cover(b).replace(/\s+/g, ' '));
|
|
228
|
+
return said ? `${named(b)}: ${said}` : named(b);
|
|
229
|
+
};
|
|
230
|
+
let list = (heading, items)=>items.length ? [
|
|
231
|
+
`## ${heading}\n\n${items.join('\n')}`
|
|
232
|
+
] : [];
|
|
233
|
+
let tree = (bs, indent = '')=>bs.flatMap((b)=>[
|
|
234
|
+
`${indent}- ${covered(b.topic)}`,
|
|
235
|
+
...b.holds.map((h)=>`${indent} - ${named(h)}`),
|
|
236
|
+
...tree(b.below, indent + ' ')
|
|
237
|
+
]);
|
|
238
|
+
return (got)=>{
|
|
239
|
+
let t = got.topic;
|
|
240
|
+
let stored = str(t, 'content', 'body');
|
|
241
|
+
let contents = stored.trim();
|
|
242
|
+
let lead = [
|
|
243
|
+
`# ${headed(t)}`,
|
|
244
|
+
str(t, 'doc', 'body').trim(),
|
|
245
|
+
...list('Within', got.path.map((b)=>`- ${covered(b)}`)),
|
|
246
|
+
...contents ? [
|
|
247
|
+
`## Contents\n\n${contents}`,
|
|
248
|
+
`$was of its contents: ${JSON.stringify({
|
|
249
|
+
content: {
|
|
250
|
+
body: token(stored)
|
|
251
|
+
}
|
|
252
|
+
})}`
|
|
253
|
+
] : [
|
|
254
|
+
'## Contents\n\nNothing is written here yet.'
|
|
255
|
+
]
|
|
256
|
+
].filter(Boolean).join('\n\n');
|
|
257
|
+
let spent = 0;
|
|
258
|
+
let carried = [];
|
|
259
|
+
let past = [];
|
|
260
|
+
for (let b of got.holds){
|
|
261
|
+
let { text, left } = cut(body(b), CUT);
|
|
262
|
+
if (spent + text.length > BUDGET) {
|
|
263
|
+
past.push(b);
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
spent += text.length;
|
|
267
|
+
carried.push([
|
|
268
|
+
`# ${headed(b)}`,
|
|
269
|
+
text,
|
|
270
|
+
left ? `… ${left} more characters: graph_show ${id(b)}.` : ''
|
|
271
|
+
].filter(Boolean).join('\n\n'));
|
|
272
|
+
}
|
|
273
|
+
let tail = [
|
|
274
|
+
...list('Below', tree(got.below)),
|
|
275
|
+
...list('Also held', past.map((b)=>`- ${named(b)}`)),
|
|
276
|
+
...list('Understand first', got.needs.map((b)=>`- ${covered(b)}`)),
|
|
277
|
+
...list('Related', got.related.map((b)=>`- ${named(b)}`)),
|
|
278
|
+
...list('Sources', got.sources.map((b)=>`- ${named(b)}`))
|
|
279
|
+
].join('\n\n');
|
|
280
|
+
return [
|
|
281
|
+
lead,
|
|
282
|
+
...carried,
|
|
283
|
+
...tail ? [
|
|
284
|
+
tail
|
|
285
|
+
] : []
|
|
286
|
+
].join('\n\n---\n\n') + '\n';
|
|
287
|
+
};
|
|
288
|
+
};
|
|
289
|
+
//# sourceMappingURL=context.js.map
|
package/context.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sources":["./context.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,8EAA8E;AAC9E,oEAAoE;AACpE,8EAA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EAAE,EAAE,MAAM,IAAI;SACtD,KAAK,WAAW,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC,IAAM,CAAC,EAAE,EAAE,MAAM,IAAI;KAC1D,CAAC,IAAI,CAAC;IACP,OAAO;MAAC;SAAS;SAAY,OAAO;QAAC;OAAK,GAAG,EAAE;KAAC,CAAC,IAAI,CAAC,iBACpD;EACJ;AACF,EAAC"}
|
package/context.ts
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
// What a session opened on a topic is given: the topic tree compiled from that
|
|
2
|
+
// topic, the way a persona is compiled from what it contains (@yaks/persona's
|
|
3
|
+
// `wear` and `voice`). {@link gather} reads it out of the graph and
|
|
4
|
+
// {@link compile} writes it as one markdown document, a pure function of what
|
|
5
|
+
// was read.
|
|
6
|
+
//
|
|
7
|
+
// In order: where the topic sits (the topics that contain it, up to the roots,
|
|
8
|
+
// each by its cover), the topic itself (its cover, its contents, and the
|
|
9
|
+
// `$was` token of the contents, so a session rewrites them without reading
|
|
10
|
+
// them again), everything it holds in full, the topics below it as an outline,
|
|
11
|
+
// and what to understand first, what is related and its sources, a line each.
|
|
12
|
+
//
|
|
13
|
+
// What it holds is said in full and what lies below is only named, as a
|
|
14
|
+
// persona includes what it contains and names what it reads: a root topic's
|
|
15
|
+
// session would otherwise carry the whole graph. A body too long to carry is
|
|
16
|
+
// cut at a paragraph and says how to read the rest, and what is past the
|
|
17
|
+
// budget is listed by id. What does not fit is listed, never dropped.
|
|
18
|
+
//
|
|
19
|
+
// The ids are the human ones (Q-12, M-3) a session passes back to graph_show;
|
|
20
|
+
// every title goes through @yaks/text's `safe`, and a body does not, since
|
|
21
|
+
// `safe` would strip the line breaks that make it a document.
|
|
22
|
+
|
|
23
|
+
import type { Bundle, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
24
|
+
import { line } from '@yaks/query'
|
|
25
|
+
import { token } from '@yaks/graph/sha256'
|
|
26
|
+
import { human } from '@yaks/id'
|
|
27
|
+
import { safe } from '@yaks/text'
|
|
28
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
29
|
+
import { cover, HOLDS, title, TOPIC } from './topic.js'
|
|
30
|
+
|
|
31
|
+
/** How many characters of one held entity's body are carried; the rest is
|
|
32
|
+
* named, to be read with graph_show. */
|
|
33
|
+
export let CUT = 4000
|
|
34
|
+
|
|
35
|
+
/** How many characters of held bodies a topic carries in all; past it, what
|
|
36
|
+
* it holds is listed by id and title. */
|
|
37
|
+
export let BUDGET = 100000
|
|
38
|
+
|
|
39
|
+
/** How many levels below the topic its outline goes. */
|
|
40
|
+
export let LEVELS = 4
|
|
41
|
+
|
|
42
|
+
/** How many lines the outline below a topic holds at most. */
|
|
43
|
+
export let LINES = 400
|
|
44
|
+
|
|
45
|
+
/** How many characters of its first line name an entity that has no title. */
|
|
46
|
+
export let GIST = 80
|
|
47
|
+
|
|
48
|
+
// How many edges are read from one topic, or from one level of the tree.
|
|
49
|
+
let EDGES = 500
|
|
50
|
+
|
|
51
|
+
// How far up the topics containing it are followed: deeper means a cycle,
|
|
52
|
+
// which `seen` already stops.
|
|
53
|
+
let DEPTH = 16
|
|
54
|
+
|
|
55
|
+
/** A topic below the one compiled: what it holds, by title, and the topics
|
|
56
|
+
* below it in turn. */
|
|
57
|
+
export type Branch = { topic: Bundle; holds: Bundle[]; below: Branch[] }
|
|
58
|
+
|
|
59
|
+
/** One topic's tree, as read. */
|
|
60
|
+
export type Gathered = {
|
|
61
|
+
/** the topic */
|
|
62
|
+
topic: Bundle
|
|
63
|
+
/** the topics containing it, up to the roots, root first */
|
|
64
|
+
path: Bundle[]
|
|
65
|
+
/** what it contains that is not a topic, in edge order */
|
|
66
|
+
holds: Bundle[]
|
|
67
|
+
/** the topics it contains, each with what lies below it */
|
|
68
|
+
below: Branch[]
|
|
69
|
+
/** what to understand first (`requires`) */
|
|
70
|
+
needs: Bundle[]
|
|
71
|
+
/** what is related (`reads`, `references`) */
|
|
72
|
+
related: Bundle[]
|
|
73
|
+
/** its sources (`cites`) */
|
|
74
|
+
sources: Bundle[]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type Comp = Record<string, unknown>
|
|
78
|
+
let str = (b: Bundle | undefined, comp: string, prop: string): string => {
|
|
79
|
+
let v = (b?.[comp] as Comp | undefined)?.[prop]
|
|
80
|
+
return typeof v == 'string' ? v : ''
|
|
81
|
+
}
|
|
82
|
+
let eidOf = (b: Bundle): Eid => b.entity.eid
|
|
83
|
+
let end = (b: Bundle, side: 'from' | 'to'): Eid => str(b, 'edge', side)
|
|
84
|
+
let unique = (ids: Eid[]): Eid[] => [...new Set(ids.filter(Boolean))]
|
|
85
|
+
let byTitle = (a: Bundle, b: Bundle) =>
|
|
86
|
+
title(a).localeCompare(title(b), undefined, { sensitivity: 'base' })
|
|
87
|
+
|
|
88
|
+
// The live rows of some eids, by eid.
|
|
89
|
+
let rows = async (g: Graph, ids: Eid[]): Promise<Map<Eid, Bundle>> =>
|
|
90
|
+
new Map(
|
|
91
|
+
(ids.length ? await g.get(ids) : [])
|
|
92
|
+
.filter((b) => !b.tombstone)
|
|
93
|
+
.map((b) => [eidOf(b), b]),
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
// The topics containing `eid`, a level at a time, root first.
|
|
97
|
+
let above = async (g: Graph, eid: Eid): Promise<Bundle[]> => {
|
|
98
|
+
let seen = new Set([eid])
|
|
99
|
+
let levels: Bundle[][] = []
|
|
100
|
+
let level = [eid]
|
|
101
|
+
for (let i = 0; i < DEPTH && level.length; i++) {
|
|
102
|
+
let edges = await g.read(
|
|
103
|
+
line`.${HOLDS}&.edge.to=${level}&?edge&.limit=${EDGES}`,
|
|
104
|
+
)
|
|
105
|
+
let ids = unique(edges.map((b) => end(b, 'from')))
|
|
106
|
+
.filter((id) => !seen.has(id))
|
|
107
|
+
let tops = [...(await rows(g, ids)).values()]
|
|
108
|
+
.filter((b) => b[TOPIC]).toSorted(byTitle)
|
|
109
|
+
for (let b of tops) seen.add(eidOf(b))
|
|
110
|
+
levels.push(tops)
|
|
111
|
+
level = tops.map(eidOf)
|
|
112
|
+
}
|
|
113
|
+
return levels.reverse().flat()
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// The topics below some subtopics, a level at a time: each one's holdings by
|
|
117
|
+
// title and the topics it contains, {@link LEVELS} deep and {@link LINES}
|
|
118
|
+
// lines long. A topic met twice is drawn once.
|
|
119
|
+
let outline = async (
|
|
120
|
+
g: Graph,
|
|
121
|
+
tops: Bundle[],
|
|
122
|
+
seen: Set<Eid>,
|
|
123
|
+
): Promise<Branch[]> => {
|
|
124
|
+
let branches: Branch[] = tops.map((topic) => ({
|
|
125
|
+
topic,
|
|
126
|
+
holds: [],
|
|
127
|
+
below: [],
|
|
128
|
+
}))
|
|
129
|
+
for (let b of tops) seen.add(eidOf(b))
|
|
130
|
+
let level = branches
|
|
131
|
+
let lines = branches.length
|
|
132
|
+
for (let depth = 1; depth < LEVELS && level.length; depth++) {
|
|
133
|
+
let at = new Map(level.map((b) => [eidOf(b.topic), b]))
|
|
134
|
+
let edges = await g.read(
|
|
135
|
+
line`.${HOLDS}&.edge.from=${[
|
|
136
|
+
...at.keys(),
|
|
137
|
+
]}&?edge&.order=edge.ord&.limit=${EDGES}`,
|
|
138
|
+
)
|
|
139
|
+
let ends = await rows(g, unique(edges.map((b) => end(b, 'to'))))
|
|
140
|
+
let next: Branch[] = []
|
|
141
|
+
for (let e of edges) {
|
|
142
|
+
let parent = at.get(end(e, 'from'))
|
|
143
|
+
let held = ends.get(end(e, 'to'))
|
|
144
|
+
if (!parent || !held || lines >= LINES) continue
|
|
145
|
+
if (held[TOPIC]) {
|
|
146
|
+
if (seen.has(eidOf(held))) continue
|
|
147
|
+
seen.add(eidOf(held))
|
|
148
|
+
let branch = { topic: held, holds: [], below: [] }
|
|
149
|
+
parent.below.push(branch)
|
|
150
|
+
next.push(branch)
|
|
151
|
+
} else parent.holds.push(held)
|
|
152
|
+
lines++
|
|
153
|
+
}
|
|
154
|
+
level = next
|
|
155
|
+
}
|
|
156
|
+
return branches
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* A topic's tree, read out of a graph: where it sits, what it holds, what
|
|
161
|
+
* lies below it, and what it requires, relates to and cites. `undefined` when
|
|
162
|
+
* the eid names no topic, which is the caller's to refuse.
|
|
163
|
+
*
|
|
164
|
+
* ```ts ignore
|
|
165
|
+
* let got = await gather(g)('Q-59416')
|
|
166
|
+
* if (got) console.log(compile(g.vocab)(got))
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
export let gather =
|
|
170
|
+
(g: Graph) => async (eid: Eid): Promise<Gathered | undefined> => {
|
|
171
|
+
let [topic] = await g.get([eid])
|
|
172
|
+
if (!topic?.[TOPIC] || topic.tombstone) return undefined
|
|
173
|
+
let out = await g.read(
|
|
174
|
+
line`.edge.from=${eid}&*&.order=edge.ord&.limit=${EDGES}`,
|
|
175
|
+
)
|
|
176
|
+
let ends = await rows(g, unique(out.map((b) => end(b, 'to'))))
|
|
177
|
+
let along = (rels: string[]): Bundle[] =>
|
|
178
|
+
unique(out.filter((b) => rels.some((r) => b[r])).map((b) => end(b, 'to')))
|
|
179
|
+
.flatMap((id) => ends.get(id) ?? [])
|
|
180
|
+
let held = along([HOLDS])
|
|
181
|
+
return {
|
|
182
|
+
topic,
|
|
183
|
+
path: await above(g, eid),
|
|
184
|
+
holds: held.filter((b) => !b[TOPIC]),
|
|
185
|
+
below: await outline(g, held.filter((b) => b[TOPIC]), new Set([eid])),
|
|
186
|
+
needs: along(['requires']),
|
|
187
|
+
related: along(['reads', 'references']),
|
|
188
|
+
sources: along(['cites']),
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// A body as a session reads it: a document's own, or else what its content
|
|
193
|
+
// says (a transcript entry, a memory's marked words).
|
|
194
|
+
let body = (b: Bundle): string =>
|
|
195
|
+
(str(b, 'doc', 'body') || str(b, 'content', 'body')).trim()
|
|
196
|
+
|
|
197
|
+
/** A body cut to at most `at` characters, at the last paragraph (else line)
|
|
198
|
+
* break past half of it, and how many characters were left off. */
|
|
199
|
+
/// cut('one\n\ntwo', 100) -> { text: 'one\n\ntwo', left: 0 }
|
|
200
|
+
/// cut('one two\n\nthree four', 12) -> { text: 'one two', left: 12 }
|
|
201
|
+
export let cut = (text: string, at: number): { text: string; left: number } => {
|
|
202
|
+
if (text.length <= at) return { text, left: 0 }
|
|
203
|
+
let head = text.slice(0, at)
|
|
204
|
+
let par = head.lastIndexOf('\n\n')
|
|
205
|
+
let line = head.lastIndexOf('\n')
|
|
206
|
+
let stop = par > at / 2 ? par : line > at / 2 ? line : at
|
|
207
|
+
let kept = text.slice(0, stop).trimEnd()
|
|
208
|
+
return { text: kept, left: text.length - kept.length }
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* A topic's tree as one markdown document, the context a session opened on it
|
|
213
|
+
* starts from. Takes the loaded vocabulary first, for the ids it names:
|
|
214
|
+
* `compile(vocab)(gathered)`.
|
|
215
|
+
*
|
|
216
|
+
* ```md
|
|
217
|
+
* # Q-1 Data model
|
|
218
|
+
*
|
|
219
|
+
* How data is shaped in the graph.
|
|
220
|
+
*
|
|
221
|
+
* ## Within
|
|
222
|
+
*
|
|
223
|
+
* - Q-2 Architecture: how the system is built
|
|
224
|
+
*
|
|
225
|
+
* ## Contents
|
|
226
|
+
*
|
|
227
|
+
* What is understood about it.
|
|
228
|
+
*
|
|
229
|
+
* $was of its contents: {"content": {"body": "9f86…"}}
|
|
230
|
+
*
|
|
231
|
+
* ---
|
|
232
|
+
*
|
|
233
|
+
* # M-3 Stored state properties are an anti-pattern
|
|
234
|
+
*
|
|
235
|
+
* a held entity's body, in full
|
|
236
|
+
*
|
|
237
|
+
* ---
|
|
238
|
+
*
|
|
239
|
+
* ## Below
|
|
240
|
+
*
|
|
241
|
+
* - Q-4 Identity: ids, names and keys
|
|
242
|
+
* - M-5 Names in any system must always be changeable
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
export let compile = (vocab: Vocab): (got: Gathered) => string => {
|
|
246
|
+
let id = human(vocab)
|
|
247
|
+
let called = (b: Bundle): string =>
|
|
248
|
+
b[TOPIC] ? title(b) : str(b, 'doc', 'title')
|
|
249
|
+
// Its id and title: a part's heading, whose body follows.
|
|
250
|
+
let headed = (b: Bundle): string => safe(`${id(b)} ${called(b)}`.trim())
|
|
251
|
+
// Its id and title, or the start of what it says where it has no title (his
|
|
252
|
+
// words in a comment or an entry): a line read with nothing under it.
|
|
253
|
+
let named = (b: Bundle): string => {
|
|
254
|
+
let first = body(b).split('\n').find((l) => l.trim())?.trim() ?? ''
|
|
255
|
+
let gist = first.length > GIST
|
|
256
|
+
? `${first.slice(0, GIST - 1).trimEnd()}…`
|
|
257
|
+
: first
|
|
258
|
+
return safe(`${id(b)} ${called(b) || gist}`.trim())
|
|
259
|
+
}
|
|
260
|
+
// Its id, title and cover's first paragraph: a topic as a list names it.
|
|
261
|
+
let covered = (b: Bundle): string => {
|
|
262
|
+
let said = safe(cover(b).replace(/\s+/g, ' '))
|
|
263
|
+
return said ? `${named(b)}: ${said}` : named(b)
|
|
264
|
+
}
|
|
265
|
+
let list = (heading: string, items: string[]): string[] =>
|
|
266
|
+
items.length ? [`## ${heading}\n\n${items.join('\n')}`] : []
|
|
267
|
+
let tree = (bs: Branch[], indent = ''): string[] =>
|
|
268
|
+
bs.flatMap((b) => [
|
|
269
|
+
`${indent}- ${covered(b.topic)}`,
|
|
270
|
+
...b.holds.map((h) => `${indent} - ${named(h)}`),
|
|
271
|
+
...tree(b.below, indent + ' '),
|
|
272
|
+
])
|
|
273
|
+
return (got) => {
|
|
274
|
+
let t = got.topic
|
|
275
|
+
let stored = str(t, 'content', 'body')
|
|
276
|
+
let contents = stored.trim()
|
|
277
|
+
let lead = [
|
|
278
|
+
`# ${headed(t)}`,
|
|
279
|
+
str(t, 'doc', 'body').trim(),
|
|
280
|
+
...list('Within', got.path.map((b) => `- ${covered(b)}`)),
|
|
281
|
+
...contents
|
|
282
|
+
? [
|
|
283
|
+
`## Contents\n\n${contents}`,
|
|
284
|
+
`$was of its contents: ${
|
|
285
|
+
JSON.stringify({ content: { body: token(stored) } })
|
|
286
|
+
}`,
|
|
287
|
+
]
|
|
288
|
+
: ['## Contents\n\nNothing is written here yet.'],
|
|
289
|
+
].filter(Boolean).join('\n\n')
|
|
290
|
+
let spent = 0
|
|
291
|
+
let carried: string[] = []
|
|
292
|
+
let past: Bundle[] = []
|
|
293
|
+
for (let b of got.holds) {
|
|
294
|
+
let { text, left } = cut(body(b), CUT)
|
|
295
|
+
if (spent + text.length > BUDGET) {
|
|
296
|
+
past.push(b)
|
|
297
|
+
continue
|
|
298
|
+
}
|
|
299
|
+
spent += text.length
|
|
300
|
+
carried.push(
|
|
301
|
+
[
|
|
302
|
+
`# ${headed(b)}`,
|
|
303
|
+
text,
|
|
304
|
+
left ? `… ${left} more characters: graph_show ${id(b)}.` : '',
|
|
305
|
+
].filter(Boolean).join('\n\n'),
|
|
306
|
+
)
|
|
307
|
+
}
|
|
308
|
+
let tail = [
|
|
309
|
+
...list('Below', tree(got.below)),
|
|
310
|
+
...list('Also held', past.map((b) => `- ${named(b)}`)),
|
|
311
|
+
...list('Understand first', got.needs.map((b) => `- ${covered(b)}`)),
|
|
312
|
+
...list('Related', got.related.map((b) => `- ${named(b)}`)),
|
|
313
|
+
...list('Sources', got.sources.map((b) => `- ${named(b)}`)),
|
|
314
|
+
].join('\n\n')
|
|
315
|
+
return [lead, ...carried, ...tail ? [tail] : []].join('\n\n---\n\n') +
|
|
316
|
+
'\n'
|
|
317
|
+
}
|
|
318
|
+
}
|
package/deno.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"publish":{"exclude":["**/*_test.ts"]},"name":"@yaks/topic","version":"0.2.36","license":"Apache-2.0","description":"A knowledge graph of topics: each a cover, contents and edges to what it holds, needs, reads and cites, found and made under one name per subject.","exports":{".":"./mod.ts","./vocab":"./vocab.ts","./graph":"./graph.ts","./share":"./share.ts","./tools":"./tools.ts","./views":"./views.ts"},"imports":{"@yaks/topic":"./mod.ts","@yaks/topic/vocab":"./vocab.ts","@yaks/topic/graph":"./graph.ts","@yaks/topic/share":"./share.ts","@yaks/topic/tools":"./tools.ts","@yaks/topic/views":"./views.ts"}}
|
package/graph.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// The write-time half: the `@yaks/topic/graph` entry point. A topic's name is
|
|
2
|
+
// folded by `named()` in the `normalize` phase, the earliest there is, so
|
|
3
|
+
// every door that writes one (topic_new, graph_apply, a seed file, a page's
|
|
4
|
+
// form) stores it in one form, and "Breads" and "breads" meet on
|
|
5
|
+
// `topic.name`'s unique index as one topic.
|
|
6
|
+
import { hook } from "@yaks/graph";
|
|
7
|
+
import { named, TOPIC } from "./topic.js";
|
|
8
|
+
/** A bundle with its topic's name folded, or as it came. */ /// fold({ entity: { eid: 'q' }, topic: { name: ' Breads ' } }) -> { entity: { eid: 'q' }, topic: { name: 'breads' } }
|
|
9
|
+
/// fold({ entity: { eid: 'q' }, topic: null }) -> { entity: { eid: 'q' }, topic: null }
|
|
10
|
+
export let fold = (b)=>{
|
|
11
|
+
let t = b[TOPIC];
|
|
12
|
+
if (typeof t?.name != 'string' || named(t.name) == t.name) return b;
|
|
13
|
+
return {
|
|
14
|
+
...b,
|
|
15
|
+
[TOPIC]: {
|
|
16
|
+
...t,
|
|
17
|
+
name: named(t.name)
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
/** The topic plugin: a name folded on the way in. */ export let topics = ()=>({
|
|
22
|
+
name: '@yaks/topic',
|
|
23
|
+
hooks: {
|
|
24
|
+
normalize: hook([
|
|
25
|
+
TOPIC
|
|
26
|
+
], (bundles)=>bundles.map(fold))
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
/** The graph plugins this package contributes. */ export let plugins = ()=>[
|
|
30
|
+
topics()
|
|
31
|
+
];
|
|
32
|
+
//# sourceMappingURL=graph.js.map
|
package/graph.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph.js","sources":["./graph.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,0EAA0E;AAC1E,4EAA4E;AAC5E,iEAAiE;AACjE,4CAA4C;AAE5C,SAAiC,IAAI,2BAA6C;AAClF,SAAS,KAAK,EAAE,KAAK,qBAAoB;AAEzC,0DAA0D,GAC1D,sHAAsH;AACtH,wFAAwF;AACxF,OAAO,IAAI,OAAO,CAAC;EACjB,IAAI,IAAI,CAAC,CAAC,MAAM;EAChB,IAAI,OAAO,GAAG,QAAQ,YAAY,MAAM,EAAE,IAAI,KAAK,EAAE,IAAI,EAAE,OAAO;EAClE,OAAO;IAAE,GAAG,CAAC;IAAE,CAAC,MAAM,EAAE;MAAE,GAAG,CAAC;MAAE,MAAM,MAAM,EAAE,IAAI;IAAE;EAAE;AACxD,EAAC;AAED,mDAAmD,GACnD,OAAO,IAAI,SAAS,IAAc,CAAC;IACjC,MAAM;IACN,OAAO;MAAE,WAAW,KAAK;QAAC;OAAM,EAAE,CAAC,UAAY,QAAQ,GAAG,CAAC;IAAO;EACpE,CAAC,EAAC;AAEF,gDAAgD,GAChD,OAAO,IAAI,UAAU,IAAgB;IAAC;GAAS,CAAA"}
|