@yaks/memory 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 +309 -2
- package/_dist/comp.d.ts +9 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/earlier.d.ts +108 -0
- package/_dist/earlier.d.ts.map +1 -0
- package/_dist/graph.d.ts +13 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/inspect.d.ts +28 -0
- package/_dist/inspect.d.ts.map +1 -0
- package/_dist/mod.d.ts +54 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/passage.d.ts +19 -0
- package/_dist/passage.d.ts.map +1 -0
- package/_dist/recall.d.ts +67 -0
- package/_dist/recall.d.ts.map +1 -0
- package/_dist/save.d.ts +45 -0
- package/_dist/save.d.ts.map +1 -0
- package/_dist/tools.d.ts +12 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/views.d.ts +17 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +7 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/around.js +141 -0
- package/around.js.map +1 -0
- package/around.ts +152 -0
- package/comp.js +42 -0
- package/comp.js.map +1 -0
- package/comp.ts +45 -0
- package/deno.json +1 -0
- package/earlier.js +386 -0
- package/earlier.js.map +1 -0
- package/earlier.ts +494 -0
- package/graph.js +138 -0
- package/graph.js.map +1 -0
- package/graph.ts +133 -0
- package/inspect.js +168 -0
- package/inspect.js.map +1 -0
- package/inspect.ts +169 -0
- package/mod.js +54 -0
- package/mod.js.map +1 -0
- package/mod.ts +55 -0
- package/package.json +51 -4
- package/passage.js +67 -0
- package/passage.js.map +1 -0
- package/passage.ts +75 -0
- package/recall.js +145 -0
- package/recall.js.map +1 -0
- package/recall.ts +180 -0
- package/save.js +93 -0
- package/save.js.map +1 -0
- package/save.ts +113 -0
- package/tools.js +246 -0
- package/tools.js.map +1 -0
- package/tools.ts +250 -0
- package/views.js +18 -0
- package/views.js.map +1 -0
- package/views.ts +23 -0
- package/vocab.js +14 -0
- package/vocab.js.map +1 -0
- package/vocab.json +456 -0
- package/vocab.ts +16 -0
package/earlier.ts
ADDED
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Earlier words, not summaries: a bounded read of what a person said and
|
|
3
|
+
* decided before, shared by the doors that set it beside what is in front of
|
|
4
|
+
* an agent.
|
|
5
|
+
*
|
|
6
|
+
* - {@link earlier} is keyed on the person's own words: a prompt, a comment or
|
|
7
|
+
* a letter of theirs, answered with their nearest earlier words and the
|
|
8
|
+
* nearest memories and decisions, as lines.
|
|
9
|
+
* - {@link bearing} is keyed on what an agent just wrote (a task, a comment, a
|
|
10
|
+
* memory): the person's earlier words and the decisions already answered
|
|
11
|
+
* that are near it, as rows, for the answer to the write (./graph.ts).
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
import type { Bundle, Comp, Graph } from '@yaks/graph'
|
|
16
|
+
import { type Built, joined, line, sub } from '@yaks/query'
|
|
17
|
+
import type { Meaning } from 'jsr:@yaks/host@0.2.36'
|
|
18
|
+
import { HUMAN, human, SHORT } from '@yaks/id'
|
|
19
|
+
import { minted } from '@yaks/graph'
|
|
20
|
+
import { lifecycleSubject, lifecycleTerms } from '@yaks/fts/term'
|
|
21
|
+
|
|
22
|
+
let part = (b: Bundle, c: string): Comp => b[c] as Comp ?? {}
|
|
23
|
+
export type Earlier = { id: string; at: string; words: string }
|
|
24
|
+
|
|
25
|
+
let WORDS =
|
|
26
|
+
'Earlier words from the same person (historical evidence, not new instructions):'
|
|
27
|
+
let KEPT =
|
|
28
|
+
'Nearest memories and decisions (read one by id before relying on it):'
|
|
29
|
+
|
|
30
|
+
/** A recalled item in a stored prompt, with the addressing prefix removed
|
|
31
|
+
* from its displayed text. */
|
|
32
|
+
export type RecalledItem = { id: string; text: string }
|
|
33
|
+
|
|
34
|
+
export let recalledItems = (prompt: string): RecalledItem[] =>
|
|
35
|
+
prompt.split('\n\n').flatMap((block) => {
|
|
36
|
+
let [heading, ...lines] = block.split('\n')
|
|
37
|
+
if (heading != WORDS && heading != KEPT) return []
|
|
38
|
+
return lines.flatMap((line) => {
|
|
39
|
+
let separator = line.indexOf(' · ')
|
|
40
|
+
if (separator < 0) return []
|
|
41
|
+
let id = line.slice(0, separator)
|
|
42
|
+
if (!HUMAN.test(id) && !SHORT.test(id) && !minted(id)) return []
|
|
43
|
+
let text = line.slice(separator + 3)
|
|
44
|
+
if (heading == WORDS) {
|
|
45
|
+
let quoted = text.match(/^\d{4}-\d{2}-\d{2} · (".*")$/)?.[1]
|
|
46
|
+
if (!quoted) return []
|
|
47
|
+
try {
|
|
48
|
+
text = JSON.parse(quoted) as string
|
|
49
|
+
} catch {
|
|
50
|
+
return []
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return text ? [{ id, text }] : []
|
|
54
|
+
})
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
// What an entry wears when it is not words somebody typed.
|
|
58
|
+
let UNSAID = [
|
|
59
|
+
'output',
|
|
60
|
+
'result',
|
|
61
|
+
'call',
|
|
62
|
+
'ask',
|
|
63
|
+
'notice',
|
|
64
|
+
'prompt',
|
|
65
|
+
'reasoning',
|
|
66
|
+
'meta',
|
|
67
|
+
'cancel',
|
|
68
|
+
'checkpoint',
|
|
69
|
+
'tool_progress',
|
|
70
|
+
'exception',
|
|
71
|
+
'refusal',
|
|
72
|
+
]
|
|
73
|
+
|
|
74
|
+
/** Only an authored input, a comment the person wrote, or an explicitly
|
|
75
|
+
* attributed verbatim quotation. Text outside the quotation is never evidence
|
|
76
|
+
* of what the person said. */
|
|
77
|
+
export function ownWords(b: Bundle, person: string, name: string): Earlier[] {
|
|
78
|
+
let at = String(part(b, 'created').at ?? '')
|
|
79
|
+
let id = b.comment && b.entity.num ? `C-${b.entity.num}` : b.entity.eid
|
|
80
|
+
if (!Number.isFinite(Date.parse(at))) return []
|
|
81
|
+
let theirs = part(b, 'created').by == person
|
|
82
|
+
if (b.entry && theirs && b.content && !UNSAID.some((c) => b[c])) {
|
|
83
|
+
return [{ id, at, words: String(part(b, 'content').body ?? '') }]
|
|
84
|
+
}
|
|
85
|
+
if (!b.comment) return []
|
|
86
|
+
let body = String(part(b, 'doc').body ?? '')
|
|
87
|
+
if (theirs) return [{ id, at, words: body }]
|
|
88
|
+
if (!name) return []
|
|
89
|
+
let escaped = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
90
|
+
// A quotation occupies a whole attributed line; fail closed on prose around it.
|
|
91
|
+
let re = new RegExp(
|
|
92
|
+
`^${escaped}, (\\d{4}-\\d{2}-\\d{2}), verbatim: ["“]([\\s\\S]+)["”]$`,
|
|
93
|
+
)
|
|
94
|
+
let m = re.exec(body.trim())
|
|
95
|
+
return m ? [{ id, at: m[1], words: m[2] }] : []
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// A recall's answer, or `none` once `wait` milliseconds pass.
|
|
99
|
+
let within = <T>(
|
|
100
|
+
found: Promise<T>,
|
|
101
|
+
wait: number | null | undefined,
|
|
102
|
+
none: T,
|
|
103
|
+
) => {
|
|
104
|
+
if (wait == null) return found
|
|
105
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
106
|
+
return Promise.race([
|
|
107
|
+
found,
|
|
108
|
+
new Promise<T>((done) => timer = setTimeout(done, wait, none)),
|
|
109
|
+
]).finally(() => clearTimeout(timer))
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The similarities a recall keeps to, in its meaning's model's space. */
|
|
113
|
+
export type Floors = { floor?: number; quoted?: number }
|
|
114
|
+
|
|
115
|
+
type Asked = {
|
|
116
|
+
person?: string
|
|
117
|
+
name?: string
|
|
118
|
+
words: string
|
|
119
|
+
before: string
|
|
120
|
+
meaning?: Meaning
|
|
121
|
+
wait?: number | null
|
|
122
|
+
/** a filter line the person's own inputs are kept to, such as those the
|
|
123
|
+
* agent answering does not already have in front of it */
|
|
124
|
+
among?: Built
|
|
125
|
+
/** the similarity a memory or decision must reach to be listed (default
|
|
126
|
+
* {@link FLOOR}) */
|
|
127
|
+
floor?: number
|
|
128
|
+
/** the similarity one of the person's earlier words must reach to be
|
|
129
|
+
* quoted (default {@link QUOTED}) */
|
|
130
|
+
quoted?: number
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// The person's name, as their quotations are attributed.
|
|
134
|
+
let named = async (g: Graph, person: string, name?: string) => {
|
|
135
|
+
if (name) return name
|
|
136
|
+
let [p] = await g.get([person], ['doc'])
|
|
137
|
+
return String(part(p ?? { entity: { eid: person } }, 'doc').title ?? '')
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// The filter line the person's own words are among: what they typed, the
|
|
141
|
+
// comments they wrote, and the comments that quote them by name, each where
|
|
142
|
+
// the vocabulary has the kind.
|
|
143
|
+
let theirs = (
|
|
144
|
+
g: Graph,
|
|
145
|
+
{ person, name, before, among }: {
|
|
146
|
+
person: string
|
|
147
|
+
name: string
|
|
148
|
+
before: string
|
|
149
|
+
among?: Built
|
|
150
|
+
},
|
|
151
|
+
): Built => {
|
|
152
|
+
let has = (c: string) => !!g.vocab.comp(c)
|
|
153
|
+
let by = line`.created.by=${person}`
|
|
154
|
+
let unsaid = UNSAID.filter(has).reduce<Built>(
|
|
155
|
+
(l, c) => line`${sub(l)} !${c}`,
|
|
156
|
+
line``,
|
|
157
|
+
)
|
|
158
|
+
let typed = line`.entry ${sub(by)}${sub(unsaid)}${
|
|
159
|
+
sub(among ? line` (${sub(among)})` : line``)
|
|
160
|
+
}`
|
|
161
|
+
let kinds = [
|
|
162
|
+
...has('entry') ? [typed] : [],
|
|
163
|
+
...has('comment') ? [line`.comment ${sub(by)}`] : [],
|
|
164
|
+
// Only a verbatim quotation is their words (ownWords), and asking for
|
|
165
|
+
// both strings halves the bodies read: "Jeff, " alone is in 4,751 docs.
|
|
166
|
+
...has('comment') && name
|
|
167
|
+
? [line`.comment .doc.body~=${name + ', '} .doc.body~=${', verbatim: '}`]
|
|
168
|
+
: [],
|
|
169
|
+
]
|
|
170
|
+
return line`(.created.at<${before}) (${sub(joined(kinds, '|'))})`
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// The rows among `line` nearest `words` in meaning and at least `floor`
|
|
174
|
+
// similar, nearest first; undefined where meaning answered nothing at all. A
|
|
175
|
+
// meaning that failed fails the recall, for its caller to report.
|
|
176
|
+
let nearest = async (
|
|
177
|
+
g: Graph,
|
|
178
|
+
meaning: Meaning | undefined,
|
|
179
|
+
words: string,
|
|
180
|
+
line: Built,
|
|
181
|
+
floor: number,
|
|
182
|
+
): Promise<Bundle[] | undefined> => {
|
|
183
|
+
let hits = meaning ? await meaning(words, { within: line, limit: 24 }) : []
|
|
184
|
+
if (!hits.length) return
|
|
185
|
+
let ids = hits.filter((h) => h.similarity >= floor).map((h) => h.entity)
|
|
186
|
+
if (!ids.length) return []
|
|
187
|
+
let rows = await g.get(
|
|
188
|
+
ids,
|
|
189
|
+
['entry', 'comment', 'content', 'doc', 'created', ...UNSAID]
|
|
190
|
+
.filter((c) => g.vocab.comp(c)),
|
|
191
|
+
)
|
|
192
|
+
return ids.flatMap((eid) => rows.filter((b) => b.entity.eid == eid))
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** One thing the person said before, and the row that holds it. */
|
|
196
|
+
export type Said = Earlier & { from: Bundle }
|
|
197
|
+
|
|
198
|
+
type Quoting = { person: string; name: string; words: string; before: string }
|
|
199
|
+
|
|
200
|
+
// The first `most` different things these rows hold that the person said
|
|
201
|
+
// before `before`, other than `words` itself.
|
|
202
|
+
let quotes = (
|
|
203
|
+
g: Graph,
|
|
204
|
+
rows: Bundle[],
|
|
205
|
+
{ person, name, words, before }: Quoting,
|
|
206
|
+
most: number,
|
|
207
|
+
): Said[] => {
|
|
208
|
+
let id = human(g.vocab)
|
|
209
|
+
let seen = new Set<string>()
|
|
210
|
+
let out: Said[] = []
|
|
211
|
+
for (let b of rows) {
|
|
212
|
+
for (let q of ownWords(b, person, name)) {
|
|
213
|
+
if (
|
|
214
|
+
Date.parse(String(part(b, 'created').at)) >= Date.parse(before) ||
|
|
215
|
+
Date.parse(q.at) >= Date.parse(before) || q.words == words ||
|
|
216
|
+
!q.words.trim() || seen.has(q.words)
|
|
217
|
+
) continue
|
|
218
|
+
seen.add(q.words)
|
|
219
|
+
out.push({ ...q, id: id(b), from: b })
|
|
220
|
+
if (out.length == most) return out
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return out
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
let keywords = (words: string): string[] => {
|
|
227
|
+
let stop = new Set(
|
|
228
|
+
'that this with have your from what would could should just like here there then them they been will into when about some only also really actually feel myself'
|
|
229
|
+
.split(' '),
|
|
230
|
+
)
|
|
231
|
+
return [
|
|
232
|
+
...new Set(words.toLowerCase().match(/[\p{L}\p{N}_-]{4,}/gu) ?? []),
|
|
233
|
+
].filter((w) => !stop.has(w)).slice(0, 24)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// The fallback ranks one term at a time, avoiding a broad OR over the
|
|
237
|
+
// transcript. Two terms must occur in the words we can actually quote.
|
|
238
|
+
let lexical = async (
|
|
239
|
+
g: Graph,
|
|
240
|
+
own: Built,
|
|
241
|
+
tokens: string[],
|
|
242
|
+
person: string,
|
|
243
|
+
name: string,
|
|
244
|
+
): Promise<Bundle[]> => {
|
|
245
|
+
if (tokens.length < 2) return []
|
|
246
|
+
let candidates = new Map<string, Bundle>()
|
|
247
|
+
for (let word of tokens) {
|
|
248
|
+
for (
|
|
249
|
+
let row of await g.read(
|
|
250
|
+
line`${word} (${sub(own)}) * .order=search .limit=24`,
|
|
251
|
+
)
|
|
252
|
+
) {
|
|
253
|
+
candidates.set(row.entity.eid, row)
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
let relevance = (b: Bundle) => {
|
|
257
|
+
let text = ownWords(b, person, name).map((q) => q.words).join(' ')
|
|
258
|
+
.toLowerCase()
|
|
259
|
+
return tokens.filter((w) => text.includes(w)).length
|
|
260
|
+
}
|
|
261
|
+
return [...candidates.values()]
|
|
262
|
+
.filter((b) => relevance(b) >= 2)
|
|
263
|
+
.sort((a, b) => relevance(b) - relevance(a))
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// A premise can carry a different subject than the rest of the request.
|
|
267
|
+
// Require both its name and one of the lifecycle terms before ranking.
|
|
268
|
+
let premise = async (
|
|
269
|
+
g: Graph,
|
|
270
|
+
own: Built,
|
|
271
|
+
subject: string,
|
|
272
|
+
source: Quoting,
|
|
273
|
+
): Promise<Bundle[]> => {
|
|
274
|
+
for (let term of lifecycleTerms) {
|
|
275
|
+
let rows = await g.read(
|
|
276
|
+
line`${subject} ${term} (${sub(own)}) * .limit=4`,
|
|
277
|
+
)
|
|
278
|
+
let [found] = quotes(g, rows, source, 1)
|
|
279
|
+
if (found) return [found.from]
|
|
280
|
+
}
|
|
281
|
+
return []
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** What a person said, kept and decided before on the subject of `words`:
|
|
285
|
+
* a few lines by id for the agent answering them. A person the graph has no
|
|
286
|
+
* name for has no attributed quotations, only their own inputs. With `wait`,
|
|
287
|
+
* only the reads completed by the deadline are included. A meaning read that
|
|
288
|
+
* fails rejects it, for its caller to report. */
|
|
289
|
+
export async function earlier(g: Graph, options: Asked): Promise<string> {
|
|
290
|
+
let { person, words, before, meaning, wait } = options
|
|
291
|
+
if (!person || !words.trim() || !Number.isFinite(Date.parse(before))) {
|
|
292
|
+
return ''
|
|
293
|
+
}
|
|
294
|
+
let tokens = keywords(words)
|
|
295
|
+
let subject = lifecycleSubject(words)
|
|
296
|
+
if (tokens.length < 2 && !subject) return ''
|
|
297
|
+
let source = { person, name: '', words, before, among: options.among }
|
|
298
|
+
let held: Bundle[] = [], related: Bundle[] = [], grounded: Bundle[] = []
|
|
299
|
+
// Start the authored premise read before the broader ranking brings in
|
|
300
|
+
// attributed quotations, memories and decisions.
|
|
301
|
+
let focused =
|
|
302
|
+
(subject
|
|
303
|
+
? premise(g, theirs(g, source), subject, source)
|
|
304
|
+
: Promise.resolve([])).then((rows) => grounded = rows)
|
|
305
|
+
let broad = async () => {
|
|
306
|
+
let name = source.name = await named(g, person, options.name)
|
|
307
|
+
let own = theirs(g, source)
|
|
308
|
+
await Promise.all([
|
|
309
|
+
decided(g, words, before, meaning, options.floor).then((rows) => {
|
|
310
|
+
held = rows
|
|
311
|
+
}),
|
|
312
|
+
nearest(g, meaning, words, own, options.quoted ?? QUOTED)
|
|
313
|
+
.then(async (rows) =>
|
|
314
|
+
rows ??
|
|
315
|
+
((await focused).length
|
|
316
|
+
? []
|
|
317
|
+
: lexical(g, own, tokens, person, name))
|
|
318
|
+
)
|
|
319
|
+
.then((rows) => {
|
|
320
|
+
related = rows
|
|
321
|
+
}),
|
|
322
|
+
])
|
|
323
|
+
}
|
|
324
|
+
await within(Promise.all([focused, broad()]), wait, undefined)
|
|
325
|
+
let said = quotes(g, [...grounded, ...related], source, 3)
|
|
326
|
+
let id = human(g.vocab)
|
|
327
|
+
// A memory can be one of the person's own inputs, already quoted above.
|
|
328
|
+
let shown = new Set(said.map((q) => q.id))
|
|
329
|
+
let kept = held.filter((b) => !shown.has(id(b))).slice(0, 4)
|
|
330
|
+
return [
|
|
331
|
+
said.length
|
|
332
|
+
? WORDS + '\n' +
|
|
333
|
+
said.map((q) =>
|
|
334
|
+
`${q.id} · ${q.at.slice(0, 10)} · ${JSON.stringify(clip(q.words))}`
|
|
335
|
+
).join('\n')
|
|
336
|
+
: '',
|
|
337
|
+
kept.length
|
|
338
|
+
? KEPT + '\n' +
|
|
339
|
+
kept.map((b) => `${id(b)} · ${titleOf(b)}`).join('\n')
|
|
340
|
+
: '',
|
|
341
|
+
].filter(Boolean).join('\n\n')
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// A quotation is shown verbatim up to a line's worth, and says when it stops.
|
|
345
|
+
let CLIP = 280
|
|
346
|
+
/** Words cut to a line's worth, saying where they stop. */
|
|
347
|
+
export let clip = (words: string): string => {
|
|
348
|
+
let flat = words.replace(/\s+/g, ' ').trim()
|
|
349
|
+
return flat.length > CLIP
|
|
350
|
+
? flat.slice(0, CLIP).replace(/\s+\S*$/, '') + '…'
|
|
351
|
+
: flat
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** What a memory or a decision is called in a line: its title, or its words
|
|
355
|
+
* where it has none, and the choice a decision was answered with. */
|
|
356
|
+
export let titleOf = (b: Bundle): string => {
|
|
357
|
+
let doc = part(b, 'doc')
|
|
358
|
+
let title = String(
|
|
359
|
+
doc.title || doc.body || part(b, 'content').body || '',
|
|
360
|
+
)
|
|
361
|
+
let choice = part(b, 'decided').choice
|
|
362
|
+
return clip(choice ? `${title} → ${choice}` : title)
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** How near in meaning a memory or decision must be to a person's words to be
|
|
366
|
+
* listed beside them. In granite-embedding-30m-english, over the 19 inputs of
|
|
367
|
+
* Jeff's in the windows of T-155224's cases, the memories on the input's
|
|
368
|
+
* subject began at about .75 and the ones at .70 to .75 were on another: .65
|
|
369
|
+
* listed 69 memories and 13 decisions, .75 lists 28 and none (T-161252). No
|
|
370
|
+
* floor from .55 to .70 put a case's needed fact in front. */
|
|
371
|
+
export let FLOOR = .75
|
|
372
|
+
|
|
373
|
+
/** How near in meaning one of the person's earlier words must be to their
|
|
374
|
+
* words now to be quoted beside them. In granite-embedding-30m-english,
|
|
375
|
+
* unrelated corpus pairs reached .642; the attachment paraphrase reached .669
|
|
376
|
+
* and inspect's two earlier asks .664/.677. Like {@link FLOOR}, it is a number
|
|
377
|
+
* in one model's space: a host whose vectors another model makes passes its
|
|
378
|
+
* own. */
|
|
379
|
+
export let QUOTED = .65
|
|
380
|
+
|
|
381
|
+
/** How near in meaning one of the person's earlier words, or an answered
|
|
382
|
+
* decision, must be to what an agent wrote to be set beside it: nothing is,
|
|
383
|
+
* so the door brings nothing until a variant scored with `yak trial score
|
|
384
|
+
* --arrive said --floor` earns it. At .8, the third-nearest of Jeff's words
|
|
385
|
+
* to the 73 things T-155224's agents wrote was at .74 in the median, and the
|
|
386
|
+
* door reached the needed row in 1 of 17 cases (C-161507), not worth a list
|
|
387
|
+
* beside every write. */
|
|
388
|
+
export let SAID = Infinity
|
|
389
|
+
|
|
390
|
+
/** The memories and answered decisions nearest the words in meaning: what was
|
|
391
|
+
* already kept or settled on the subject, at least `floor` similar. A
|
|
392
|
+
* quarantined memory is not offered; without a meaning index there is nothing
|
|
393
|
+
* to rank by, so nothing. `memories: false` answers the decisions alone. */
|
|
394
|
+
export async function decided(
|
|
395
|
+
g: Graph,
|
|
396
|
+
words: string,
|
|
397
|
+
before: string,
|
|
398
|
+
meaning?: Meaning,
|
|
399
|
+
floor = FLOOR,
|
|
400
|
+
memories = true,
|
|
401
|
+
): Promise<Bundle[]> {
|
|
402
|
+
if (!meaning || !words.trim()) return []
|
|
403
|
+
let at = line`.created.at<${before}`
|
|
404
|
+
let ask = (within: Built, limit: number) => meaning(words, { within, limit })
|
|
405
|
+
let [kept, decisions] = await Promise.all([
|
|
406
|
+
memories && g.vocab.comp('memory')
|
|
407
|
+
? ask(
|
|
408
|
+
line`(${sub(at)}) .memory${
|
|
409
|
+
sub(g.vocab.comp('quarantined') ? line` !quarantined` : line``)
|
|
410
|
+
}`,
|
|
411
|
+
6,
|
|
412
|
+
)
|
|
413
|
+
: [],
|
|
414
|
+
g.vocab.comp('decision') && g.vocab.comp('decided')
|
|
415
|
+
? ask(line`(${sub(at)}) .decision .decided`, 2)
|
|
416
|
+
: [],
|
|
417
|
+
])
|
|
418
|
+
let ids = [...kept, ...decisions].filter((h) => h.similarity >= floor)
|
|
419
|
+
.map((h) => h.entity)
|
|
420
|
+
if (!ids.length) return []
|
|
421
|
+
let rows = await g.get(ids, [
|
|
422
|
+
'doc',
|
|
423
|
+
'content',
|
|
424
|
+
'created',
|
|
425
|
+
'memory',
|
|
426
|
+
'decision',
|
|
427
|
+
'decided',
|
|
428
|
+
])
|
|
429
|
+
return ids.flatMap((eid) => rows.filter((b) => b.entity.eid == eid))
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/** What the agent that wrote something already holds: what its session wrote
|
|
433
|
+
* or was told `since` (its start, or its latest compaction). */
|
|
434
|
+
export type Held = { via: string; since?: string }
|
|
435
|
+
|
|
436
|
+
let holds = (held: Held | undefined) => (b: Bundle): boolean => {
|
|
437
|
+
if (!held) return false
|
|
438
|
+
let { via, at } = part(b, 'created')
|
|
439
|
+
let its = via == held.via || part(b, 'entry').session == held.via
|
|
440
|
+
return its && String(at ?? '') >= (held.since ?? '')
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** What bears on something an agent wrote: the person's earlier words near
|
|
444
|
+
* it, and the decisions answered before it. */
|
|
445
|
+
export type Bearing = { said: Said[]; decided: Bundle[] }
|
|
446
|
+
|
|
447
|
+
/** How many of the person's words, and how many decisions, one written thing
|
|
448
|
+
* is answered with. */
|
|
449
|
+
export let FEW = { said: 3, decided: 2 }
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* What a person said and decided before that bears on `words`, the text of
|
|
453
|
+
* something an agent just wrote: their nearest earlier words at least `floor`
|
|
454
|
+
* similar (default {@link SAID}), and the nearest answered decisions at least
|
|
455
|
+
* that and {@link FLOOR} similar. What the writing session already `held` is left out.
|
|
456
|
+
* There is no ranking by words here: without a meaning index, or past `wait`
|
|
457
|
+
* milliseconds, nothing bears.
|
|
458
|
+
*/
|
|
459
|
+
export function bearing(
|
|
460
|
+
g: Graph,
|
|
461
|
+
options: Asked & { held?: Held },
|
|
462
|
+
): Promise<Bearing> {
|
|
463
|
+
let none: Bearing = { said: [], decided: [] }
|
|
464
|
+
let { person, words, before, meaning } = options
|
|
465
|
+
let floor = options.floor ?? SAID
|
|
466
|
+
if (
|
|
467
|
+
!person || !meaning || !words.trim() ||
|
|
468
|
+
!Number.isFinite(Date.parse(before))
|
|
469
|
+
) return Promise.resolve(none)
|
|
470
|
+
let fresh = (b: Bundle) => !holds(options.held)(b)
|
|
471
|
+
let found = async (): Promise<Bearing> => {
|
|
472
|
+
let name = await named(g, person, options.name)
|
|
473
|
+
let [rows, settled] = await Promise.all([
|
|
474
|
+
nearest(
|
|
475
|
+
g,
|
|
476
|
+
meaning,
|
|
477
|
+
words,
|
|
478
|
+
theirs(g, { person, name, before }),
|
|
479
|
+
floor,
|
|
480
|
+
),
|
|
481
|
+
decided(g, words, before, meaning, Math.max(floor, FLOOR), false),
|
|
482
|
+
])
|
|
483
|
+
return {
|
|
484
|
+
said: quotes(
|
|
485
|
+
g,
|
|
486
|
+
(rows ?? []).filter(fresh),
|
|
487
|
+
{ person, name, words, before },
|
|
488
|
+
FEW.said,
|
|
489
|
+
),
|
|
490
|
+
decided: settled.filter(fresh).slice(0, FEW.decided),
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return within(found(), options.wait, none)
|
|
494
|
+
}
|
package/graph.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// What a write is answered with from memory: the `reply` the graph facet
|
|
2
|
+
// gives the host, beside the neighbours @yaks/embedding answers with.
|
|
3
|
+
//
|
|
4
|
+
// An agent that writes a task, a comment or a memory is told what the graph's
|
|
5
|
+
// person said and decided before that bears on it: their few nearest earlier
|
|
6
|
+
// words, and the nearest decisions already answered (./earlier.ts `bearing`).
|
|
7
|
+
// A prompt recalls by the person's message; this recalls by what the agent is
|
|
8
|
+
// doing about it, which is where a correction it never saw would have helped.
|
|
9
|
+
// Each is a `hit` naming, under `said`, the entity it bears on and, under
|
|
10
|
+
// `by`, who said it. What the calling session wrote or was told since its
|
|
11
|
+
// latest compaction is left out: it holds that already. The person's own
|
|
12
|
+
// writes are answered with nothing.
|
|
13
|
+
//
|
|
14
|
+
// Nothing here fails or holds a write: it committed before a reply is asked
|
|
15
|
+
// for, and a recall slower than WAIT answers nothing.
|
|
16
|
+
import { who } from "@yaks/graph";
|
|
17
|
+
import { called } from "@yaks/graph/hit";
|
|
18
|
+
import { and, eq, present } from "@yaks/query";
|
|
19
|
+
import { became } from "@yaks/tools";
|
|
20
|
+
import { bearing, clip, titleOf } from "./earlier.js";
|
|
21
|
+
/** How long a write's answer waits for what was said before, in
|
|
22
|
+
* milliseconds: as long as it waits for its neighbours (@yaks/embedding
|
|
23
|
+
* `WAIT`), which are asked at the same time. */ export let WAIT = 1000;
|
|
24
|
+
/** How many of one call's new entities are looked at. */ export let MOST = 3;
|
|
25
|
+
/** How much of a written entity's text the recall is keyed on. */ export let TEXT = 4000;
|
|
26
|
+
let part = (b, c)=>b?.[c] ?? {};
|
|
27
|
+
let str = (v)=>typeof v == 'string' ? v : '';
|
|
28
|
+
/** The text of something written, as a recall is keyed on it: its title and
|
|
29
|
+
* its words. */ export let textOf = (b)=>[
|
|
30
|
+
part(b, 'doc').title,
|
|
31
|
+
part(b, 'doc').body,
|
|
32
|
+
part(b, 'content').body
|
|
33
|
+
].map(str).filter((s)=>s.trim()).join('\n').slice(0, TEXT);
|
|
34
|
+
// What the session behind a call already holds: everything since its latest
|
|
35
|
+
// compaction, or since it began.
|
|
36
|
+
let held = async (host, via)=>{
|
|
37
|
+
if (!via) return;
|
|
38
|
+
let cuts = host.vocab.comp('checkpoint') && host.vocab.comp('entry') ? await host.graph.read(and(eq('entry.session', via), present('checkpoint'))) : [];
|
|
39
|
+
let since = cuts.map((b)=>str(part(b, 'created').at)).sort().at(-1);
|
|
40
|
+
return {
|
|
41
|
+
via,
|
|
42
|
+
since
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
/** The reply: for each entity the call's write made, the person's nearest
|
|
46
|
+
* earlier words and answered decisions, as `hit` bundles. */ export let reply = (host)=>async (call, _answer, wrote)=>{
|
|
47
|
+
let configured = host.config.person;
|
|
48
|
+
if (!configured || !host.meaning) return [];
|
|
49
|
+
let born = wrote.filter((b)=>(became(host.vocab, b) ?? 'entity') != 'entity').map((b)=>b.entity.eid);
|
|
50
|
+
if (!born.length) return [];
|
|
51
|
+
let person = (await host.graph.address([
|
|
52
|
+
configured
|
|
53
|
+
])).get(configured) ?? configured;
|
|
54
|
+
let actor = who(call);
|
|
55
|
+
if (actor?.by == person) return [];
|
|
56
|
+
let [p] = await host.graph.get([
|
|
57
|
+
person
|
|
58
|
+
], [
|
|
59
|
+
'doc'
|
|
60
|
+
]);
|
|
61
|
+
let name = str(part(p, 'doc').title);
|
|
62
|
+
let made = (await host.graph.get(born.slice(0, MOST), [
|
|
63
|
+
'doc',
|
|
64
|
+
'content'
|
|
65
|
+
])).filter((b)=>textOf(b).trim());
|
|
66
|
+
if (!made.length) return [];
|
|
67
|
+
let holds = await held(host, actor?.via);
|
|
68
|
+
let before = new Date().toISOString();
|
|
69
|
+
let id = called(host.vocab);
|
|
70
|
+
let shown = new Set();
|
|
71
|
+
let out = [];
|
|
72
|
+
let found = await Promise.all(made.map((b)=>bearing(host.graph, {
|
|
73
|
+
person,
|
|
74
|
+
name,
|
|
75
|
+
words: textOf(b),
|
|
76
|
+
before,
|
|
77
|
+
meaning: host.meaning,
|
|
78
|
+
held: holds,
|
|
79
|
+
wait: WAIT
|
|
80
|
+
})));
|
|
81
|
+
// Each row whole, for the kind its components make it and its status, and
|
|
82
|
+
// the ids the comments among them are on: a comment's words alone do not
|
|
83
|
+
// say which task they are about.
|
|
84
|
+
let rows = found.flatMap((f)=>[
|
|
85
|
+
...f.said.map((q)=>q.from),
|
|
86
|
+
...f.decided
|
|
87
|
+
]);
|
|
88
|
+
let targets = rows.map((b)=>str(part(b, 'comment').target)).filter(Boolean);
|
|
89
|
+
let whole = new Map((await host.graph.get([
|
|
90
|
+
...new Set([
|
|
91
|
+
...rows.map((b)=>b.entity.eid),
|
|
92
|
+
...targets
|
|
93
|
+
])
|
|
94
|
+
])).map((b)=>[
|
|
95
|
+
b.entity.eid,
|
|
96
|
+
b
|
|
97
|
+
]));
|
|
98
|
+
let status = (b)=>Object.values(b).map((c)=>str(c?.status)).find(Boolean);
|
|
99
|
+
made.forEach((b, i)=>{
|
|
100
|
+
let hit = (found, rest)=>{
|
|
101
|
+
let from = whole.get(found.entity.eid) ?? found;
|
|
102
|
+
if (shown.has(from.entity.eid)) return;
|
|
103
|
+
shown.add(from.entity.eid);
|
|
104
|
+
let state = status(from);
|
|
105
|
+
out.push({
|
|
106
|
+
entity: from.entity,
|
|
107
|
+
hit: {
|
|
108
|
+
id: id(from),
|
|
109
|
+
kind: host.vocab.kindOf(from),
|
|
110
|
+
source: 'meaning',
|
|
111
|
+
said: b.entity.eid,
|
|
112
|
+
...name ? {
|
|
113
|
+
by: name
|
|
114
|
+
} : {},
|
|
115
|
+
...state ? {
|
|
116
|
+
status: state
|
|
117
|
+
} : {},
|
|
118
|
+
...rest
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
for (let q of found[i].said){
|
|
123
|
+
let target = whole.get(str(part(q.from, 'comment').target));
|
|
124
|
+
hit(q.from, {
|
|
125
|
+
...target ? {
|
|
126
|
+
target: id(target)
|
|
127
|
+
} : {},
|
|
128
|
+
snippet: `${q.at.slice(0, 10)} ${JSON.stringify(clip(q.words))}`
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
for (let d of found[i].decided)hit(d, {
|
|
132
|
+
title: titleOf(d),
|
|
133
|
+
snippet: ''
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
return out;
|
|
137
|
+
};
|
|
138
|
+
//# sourceMappingURL=graph.js.map
|
package/graph.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph.js","sources":["./graph.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,sEAAsE;AACtE,EAAE;AACF,8EAA8E;AAC9E,6EAA6E;AAC7E,8EAA8E;AAC9E,8EAA8E;AAC9E,8EAA8E;AAC9E,0EAA0E;AAC1E,0EAA0E;AAC1E,yEAAyE;AACzE,oCAAoC;AACpC,EAAE;A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