@yaks/embedding 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 +549 -2
- package/_dist/compile.d.ts +34 -0
- package/_dist/compile.d.ts.map +1 -0
- package/_dist/ddl.d.ts +31 -0
- package/_dist/ddl.d.ts.map +1 -0
- package/_dist/embedder.d.ts +55 -0
- package/_dist/embedder.d.ts.map +1 -0
- package/_dist/fields.d.ts +70 -0
- package/_dist/fields.d.ts.map +1 -0
- package/_dist/graph.d.ts +63 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/held.d.ts +16 -0
- package/_dist/held.d.ts.map +1 -0
- package/_dist/image.d.ts +20 -0
- package/_dist/image.d.ts.map +1 -0
- package/_dist/mod.d.ts +105 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/native.d.ts +20 -0
- package/_dist/native.d.ts.map +1 -0
- package/_dist/near.d.ts +35 -0
- package/_dist/near.d.ts.map +1 -0
- package/_dist/neighbours.d.ts +29 -0
- package/_dist/neighbours.d.ts.map +1 -0
- package/_dist/options.d.ts +37 -0
- package/_dist/options.d.ts.map +1 -0
- package/_dist/outage.d.ts +64 -0
- package/_dist/outage.d.ts.map +1 -0
- package/_dist/owed.d.ts +79 -0
- package/_dist/owed.d.ts.map +1 -0
- package/_dist/remote.d.ts +116 -0
- package/_dist/remote.d.ts.map +1 -0
- package/_dist/score.d.ts +10 -0
- package/_dist/score.d.ts.map +1 -0
- package/_dist/search.d.ts +24 -0
- package/_dist/search.d.ts.map +1 -0
- package/_dist/service.d.ts +23 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/sleeper.d.ts +37 -0
- package/_dist/sleeper.d.ts.map +1 -0
- package/_dist/sweep.d.ts +71 -0
- package/_dist/sweep.d.ts.map +1 -0
- package/_dist/tools.d.ts +14 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/vector.d.ts +30 -0
- package/_dist/vector.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/coded.js +98 -0
- package/coded.js.map +1 -0
- package/coded.ts +85 -0
- package/compile.js +145 -0
- package/compile.js.map +1 -0
- package/compile.ts +206 -0
- package/ddl.js +476 -0
- package/ddl.js.map +1 -0
- package/ddl.ts +346 -0
- package/deno.json +1 -0
- package/embedder.js +85 -0
- package/embedder.js.map +1 -0
- package/embedder.ts +113 -0
- package/fields.js +178 -0
- package/fields.js.map +1 -0
- package/fields.ts +259 -0
- package/fill.js +177 -0
- package/fill.js.map +1 -0
- package/fill.ts +144 -0
- package/graph.js +113 -0
- package/graph.js.map +1 -0
- package/graph.ts +167 -0
- package/held.js +680 -0
- package/held.js.map +1 -0
- package/held.ts +726 -0
- package/image.js +202 -0
- package/image.js.map +1 -0
- package/image.ts +185 -0
- package/mod.js +105 -0
- package/mod.js.map +1 -0
- package/mod.ts +113 -0
- package/native.js +236 -0
- package/native.js.map +1 -0
- package/native.ts +288 -0
- package/near.js +123 -0
- package/near.js.map +1 -0
- package/near.ts +192 -0
- package/neighbours.js +223 -0
- package/neighbours.js.map +1 -0
- package/neighbours.ts +303 -0
- package/options.js +187 -0
- package/options.js.map +1 -0
- package/options.ts +269 -0
- package/outage.js +128 -0
- package/outage.js.map +1 -0
- package/outage.ts +132 -0
- package/owed.js +287 -0
- package/owed.js.map +1 -0
- package/owed.ts +346 -0
- package/package.json +45 -4
- package/remote.js +264 -0
- package/remote.js.map +1 -0
- package/remote.ts +391 -0
- package/score.js +46 -0
- package/score.js.map +1 -0
- package/score.ts +60 -0
- package/search.js +65 -0
- package/search.js.map +1 -0
- package/search.ts +118 -0
- package/service.js +107 -0
- package/service.js.map +1 -0
- package/service.ts +133 -0
- package/sleeper.js +107 -0
- package/sleeper.js.map +1 -0
- package/sleeper.ts +166 -0
- package/sparse.js +36 -0
- package/sparse.js.map +1 -0
- package/sparse.ts +49 -0
- package/sweep.js +374 -0
- package/sweep.js.map +1 -0
- package/sweep.ts +446 -0
- package/tools.js +57 -0
- package/tools.js.map +1 -0
- package/tools.ts +88 -0
- package/vector.js +78 -0
- package/vector.js.map +1 -0
- package/vector.ts +96 -0
- package/visibility.js +83 -0
- package/visibility.js.map +1 -0
- package/visibility.ts +111 -0
- package/vocab.js +22 -0
- package/vocab.js.map +1 -0
- package/vocab.json +19 -0
- package/vocab.ts +22 -0
package/near.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// Cosine ranking over canonical vectors. A copy held in memory (./held.ts)
|
|
2
|
+
// selects the candidates where there is one; the native engine, on a shared
|
|
3
|
+
// file whose copy is not held yet. Every candidate is scored from its current
|
|
4
|
+
// float vector before the final cut.
|
|
5
|
+
//
|
|
6
|
+
// A {@link Screen} is the other half of "nearest": nearest among what. The
|
|
7
|
+
// eight nearest entities of any kind are the wrong eight for `.near=X&.memory`
|
|
8
|
+
// — intersecting them with "and a memory" usually leaves nothing — so the rest
|
|
9
|
+
// of the query comes in as a statement selecting the ids it admits, only those
|
|
10
|
+
// vectors are candidates, and the cut to `limit` happens after. Filter, then
|
|
11
|
+
// rank, then cut.
|
|
12
|
+
//
|
|
13
|
+
// A neighbour carries its integer owner id beside its eid for SQL ranking.
|
|
14
|
+
|
|
15
|
+
import type { Eid } from 'jsr:@yaks/graph@0.2.36'
|
|
16
|
+
import {
|
|
17
|
+
among,
|
|
18
|
+
and,
|
|
19
|
+
as,
|
|
20
|
+
at,
|
|
21
|
+
col,
|
|
22
|
+
eq,
|
|
23
|
+
exists,
|
|
24
|
+
join,
|
|
25
|
+
lit,
|
|
26
|
+
not,
|
|
27
|
+
oneOf,
|
|
28
|
+
select,
|
|
29
|
+
type Selection,
|
|
30
|
+
type Statements,
|
|
31
|
+
table,
|
|
32
|
+
val,
|
|
33
|
+
} from '@yaks/sql'
|
|
34
|
+
import { TABLE } from './ddl.js'
|
|
35
|
+
import { hold, ranked } from './held.js'
|
|
36
|
+
import { admitted, candidates } from './native.js'
|
|
37
|
+
import { floats, unpack } from './vector.js'
|
|
38
|
+
import { scored, type Vector } from './score.js'
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* One semantic neighbour: the entity, the integer id its rows key on, and how
|
|
42
|
+
* similar it is (1 identical, 0 unrelated).
|
|
43
|
+
*/
|
|
44
|
+
export type Near = { entity: Eid; owner: number; similarity: number }
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A statement selecting, as `id`, the integer ids of the entities a neighbour
|
|
48
|
+
* must be among — what @yaks/sql's `screen` compiled for the rest of the query,
|
|
49
|
+
* passed straight in (the same interface @yaks/fts's `find` takes).
|
|
50
|
+
*/
|
|
51
|
+
export type Screen = Selection
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A ranking: the nearest `limit` entities to a query vector, most similar
|
|
55
|
+
* first, among the entities `within` allows. {@link nearest} is the exact one;
|
|
56
|
+
* an approximate index has the same type, and one that cannot honour `within`
|
|
57
|
+
* returns the wrong neighbourhood for every query that also filters.
|
|
58
|
+
*/
|
|
59
|
+
export type Rank = (
|
|
60
|
+
query: Float32Array,
|
|
61
|
+
limit: number,
|
|
62
|
+
within?: Screen,
|
|
63
|
+
) => Near[]
|
|
64
|
+
|
|
65
|
+
// The vectors in one model's space whose entity still exists, among what the
|
|
66
|
+
// screen admits, and only the `pool` where the index narrowed it. Deleted
|
|
67
|
+
// entities are excluded here as well as pruned by the sweep: a delete between
|
|
68
|
+
// two sweeps must not leave a neighbour that no longer exists.
|
|
69
|
+
let e = at('e')
|
|
70
|
+
let o = at('o')
|
|
71
|
+
let vectors = (
|
|
72
|
+
db: Statements,
|
|
73
|
+
model: string,
|
|
74
|
+
within?: Screen,
|
|
75
|
+
pool?: number[],
|
|
76
|
+
) =>
|
|
77
|
+
db.query(select({
|
|
78
|
+
cols: [as(e('owner'), 'owner'), as(e('vec'), 'vec')],
|
|
79
|
+
from: table(TABLE, 'e'),
|
|
80
|
+
where: and(
|
|
81
|
+
...(pool ? [oneOf(e('owner'), pool)] : []),
|
|
82
|
+
eq(e('model'), val(model)),
|
|
83
|
+
not(exists(select({
|
|
84
|
+
cols: [lit(1)],
|
|
85
|
+
from: table('tombstone', 't'),
|
|
86
|
+
where: eq(col('entity', 't'), e('owner')),
|
|
87
|
+
}))),
|
|
88
|
+
// The pool was drawn from what the screen admits already. Without one,
|
|
89
|
+
// every vector is read, so the screen is read whole once.
|
|
90
|
+
...(within && !pool ? [among(e('owner'), within)] : []),
|
|
91
|
+
),
|
|
92
|
+
}))
|
|
93
|
+
|
|
94
|
+
let vectorRows = function* (
|
|
95
|
+
rows: ReturnType<typeof vectors>,
|
|
96
|
+
): Generator<Vector> {
|
|
97
|
+
for (let row of rows) {
|
|
98
|
+
yield { owner: Number(row.owner), vector: floats(row.vec as Uint8Array) }
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The vector stored for an entity under a model, or null when it has none —
|
|
104
|
+
* because it holds no text, because the sweep has not reached it, or because
|
|
105
|
+
* the model moved and its row belongs to the old space.
|
|
106
|
+
*/
|
|
107
|
+
export let vectorOf = (
|
|
108
|
+
db: Statements,
|
|
109
|
+
entity: Eid,
|
|
110
|
+
model: string,
|
|
111
|
+
within?: Screen,
|
|
112
|
+
): Float32Array | null => {
|
|
113
|
+
let row = db.query(select({
|
|
114
|
+
cols: [as(e('vec'), 'vec')],
|
|
115
|
+
from: table(TABLE, 'e'),
|
|
116
|
+
joins: [join(table('entity', 'o'), eq(o('id'), e('owner')))],
|
|
117
|
+
where: and(
|
|
118
|
+
eq(o('eid'), val(entity)),
|
|
119
|
+
eq(e('model'), val(model)),
|
|
120
|
+
...within ? [admitted(within, e('owner'))] : [],
|
|
121
|
+
),
|
|
122
|
+
}))[0]
|
|
123
|
+
return row ? unpack(row.vec as Uint8Array) : null
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** How a search is narrowed beyond "the nearest few". */
|
|
127
|
+
export type NearOpts = {
|
|
128
|
+
/** the model whose space to search — the one the vectors were stored under */
|
|
129
|
+
model: string
|
|
130
|
+
/** how many neighbours at most (default 8) */
|
|
131
|
+
limit?: number
|
|
132
|
+
/** the similarity a neighbour must reach to count at all (default 0) */
|
|
133
|
+
floor?: number
|
|
134
|
+
/** an entity to leave out — nothing is its own neighbour */
|
|
135
|
+
without?: Eid
|
|
136
|
+
/** the ids a neighbour must be among — what the rest of the query selects */
|
|
137
|
+
within?: Screen
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** The entities nearest a query vector, most similar first. */
|
|
141
|
+
export let nearest = (
|
|
142
|
+
db: Statements,
|
|
143
|
+
query: Float32Array,
|
|
144
|
+
opts: NearOpts,
|
|
145
|
+
): Near[] => {
|
|
146
|
+
let limit = opts.limit ?? 8
|
|
147
|
+
if (limit <= 0) return []
|
|
148
|
+
let floor = opts.floor ?? 0
|
|
149
|
+
let without = opts.without && db.query(select({
|
|
150
|
+
cols: [col('id')],
|
|
151
|
+
from: table('entity'),
|
|
152
|
+
where: eq(col('eid'), val(opts.without)),
|
|
153
|
+
}))[0]?.id
|
|
154
|
+
let held = hold(db, opts.model)
|
|
155
|
+
if (held) {
|
|
156
|
+
return ranked(db, held, query, {
|
|
157
|
+
limit,
|
|
158
|
+
floor,
|
|
159
|
+
without: without == null ? undefined : Number(without),
|
|
160
|
+
within: opts.within,
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
let pool = candidates(db, query, {
|
|
164
|
+
model: opts.model,
|
|
165
|
+
need: limit + (without == null ? 0 : 1),
|
|
166
|
+
within: opts.within,
|
|
167
|
+
}) ?? undefined
|
|
168
|
+
if (pool && !pool.length) return []
|
|
169
|
+
let hits = scored(
|
|
170
|
+
query,
|
|
171
|
+
vectorRows(vectors(db, opts.model, opts.within, pool)),
|
|
172
|
+
{
|
|
173
|
+
limit,
|
|
174
|
+
floor,
|
|
175
|
+
without: without == null ? undefined : Number(without),
|
|
176
|
+
},
|
|
177
|
+
)
|
|
178
|
+
let owners = hits.map((h) => h.owner)
|
|
179
|
+
if (!owners.length) return []
|
|
180
|
+
let eids = new Map(
|
|
181
|
+
db.query(select({
|
|
182
|
+
cols: [col('id'), col('eid')],
|
|
183
|
+
from: table('entity'),
|
|
184
|
+
where: among(col('id'), owners.map((id) => val(id))),
|
|
185
|
+
})).map((r) => [Number(r.id), String(r.eid)]),
|
|
186
|
+
)
|
|
187
|
+
return hits.map(({ owner, similarity }) => ({
|
|
188
|
+
entity: eids.get(owner)!,
|
|
189
|
+
owner,
|
|
190
|
+
similarity,
|
|
191
|
+
}))
|
|
192
|
+
}
|
package/neighbours.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// What a call just made is near: the `reply` the rules export gives the host
|
|
2
|
+
// (./graph.ts), which a direct tool call's answer carries beside the tool's
|
|
3
|
+
// own (@yaks/tools `Opts.reply`).
|
|
4
|
+
//
|
|
5
|
+
// An entity a call made — a task, a memory, a comment — comes back with two
|
|
6
|
+
// short lists. The first is the few existing entities of its kind nearest to
|
|
7
|
+
// it (`hit.near`), so whoever wrote it sees at once whether the graph already
|
|
8
|
+
// holds it, and keeps the one that stands instead of two. The second is the
|
|
9
|
+
// few nearest of any kind that is a resource (`hit.related`): the memories,
|
|
10
|
+
// tasks, designs and topics it bears on, and the words a person wrote. A
|
|
11
|
+
// one-off record (wearing a component the vocabulary marks `notice`: a knock,
|
|
12
|
+
// a transcript entry) is in neither list unless a person wrote it, even where
|
|
13
|
+
// it also wears the kind: a run's receipt is a comment and a knock at once.
|
|
14
|
+
// Nor is anything the calling session wrote itself, which it already holds:
|
|
15
|
+
// a helper commenting twice is not told of its first comment. A comment is
|
|
16
|
+
// shown with the id of what it is on (`hit.target`), since a comment's words
|
|
17
|
+
// alone do not say which task they are about. A call that made nothing is
|
|
18
|
+
// answered as it was.
|
|
19
|
+
//
|
|
20
|
+
// Made means created, or marked as a kind for the first time: memory_save
|
|
21
|
+
// keeps a person's words where they were said, by marking the transcript entry
|
|
22
|
+
// or comment that holds them a `memory`, and that memory is as new as a
|
|
23
|
+
// created one.
|
|
24
|
+
//
|
|
25
|
+
// A new entity has no vector yet: the sweep (./sweep.ts) makes them off the
|
|
26
|
+
// write path, seconds later. So this makes the vector now, for the few
|
|
27
|
+
// entities a caller is waiting on, and stores it as the sweep would, which
|
|
28
|
+
// then finds it made and calls no model. The neighbours are then a query,
|
|
29
|
+
// `.near` among entities of the same kind, answered like any other. A model
|
|
30
|
+
// that does not answer within WAIT, or a config naming none yet, leaves the
|
|
31
|
+
// entity with no vector, and the fallback is its first line as words
|
|
32
|
+
// (`.order=search`): a twin that says the same thing in the same words.
|
|
33
|
+
//
|
|
34
|
+
// Nothing here fails a write. The write committed before a reply is asked
|
|
35
|
+
// for, and a model out of reach only means fewer neighbours.
|
|
36
|
+
import { who } from "@yaks/graph";
|
|
37
|
+
import { called } from "@yaks/graph/hit";
|
|
38
|
+
import { became } from "@yaks/tools";
|
|
39
|
+
import { absent, and, every, limit, ne, near, or, order, present, text } from "@yaks/query";
|
|
40
|
+
import { as, col, oneOf, select, statements, table } from "@yaks/sql";
|
|
41
|
+
import { hash } from "./embedder.js";
|
|
42
|
+
import { fields, resolved, searched } from "./fields.js";
|
|
43
|
+
import { ready } from "./options.js";
|
|
44
|
+
import { excerpt } from "./search.js";
|
|
45
|
+
import { put, sources } from "./sweep.js";
|
|
46
|
+
import { SIMILAR } from "./compile.js";
|
|
47
|
+
// The ordering @yaks/fts gives a text search: closest match first.
|
|
48
|
+
let SEARCH = 'search';
|
|
49
|
+
/** How many neighbours of each list a new entity is answered with. */ export let FEW = 3;
|
|
50
|
+
/** How many of one call's new entities are looked at: a plan of twenty
|
|
51
|
+
* tasks is answered about its first few, not with sixty lines. */ export let MOST = 5;
|
|
52
|
+
/** How long an answer waits for the model, in milliseconds, before it falls
|
|
53
|
+
* back to words. */ export let WAIT = 1000;
|
|
54
|
+
let comps = (b)=>Object.entries(b).flatMap(([k, v])=>k != 'entity' && v && typeof v == 'object' ? [
|
|
55
|
+
v
|
|
56
|
+
] : []);
|
|
57
|
+
// The first string a bundle's components hold under `name`: a task's
|
|
58
|
+
// `doc.title`, its computed `task.status`.
|
|
59
|
+
let said = (b, name)=>comps(b).map((c)=>c[name]).find((v)=>typeof v == 'string');
|
|
60
|
+
// An entity's text, read off its bundle the way ./fields.ts reads it off the
|
|
61
|
+
// tables: each embedded field in order, joined.
|
|
62
|
+
let textOf = (b, prose)=>prose.map((f)=>b[f.comp]?.[f.prop]).filter((v)=>typeof v == 'string' && !!v.trim()).join('\n');
|
|
63
|
+
// One neighbour, as a search answers one (@yaks/cli, @yaks/tools `worded`): a
|
|
64
|
+
// `hit` naming the id it is called by (@yaks/graph/hit `called`), its kind,
|
|
65
|
+
// its title and its status where it has them, the id of what a comment is on,
|
|
66
|
+
// whether meaning or words found it, and, under its list's name, the new
|
|
67
|
+
// entity it was found near. An entity with no title is shown by an excerpt of
|
|
68
|
+
// its text instead.
|
|
69
|
+
let hit = (vocab, b, of, list, source, prose, on)=>{
|
|
70
|
+
let title = said(b, 'title');
|
|
71
|
+
let status = said(b, 'status');
|
|
72
|
+
return {
|
|
73
|
+
entity: b.entity,
|
|
74
|
+
hit: {
|
|
75
|
+
id: called(vocab)(b),
|
|
76
|
+
kind: vocab.kindOf(b),
|
|
77
|
+
...title ? {
|
|
78
|
+
title
|
|
79
|
+
} : {},
|
|
80
|
+
...on ? {
|
|
81
|
+
target: on
|
|
82
|
+
} : {},
|
|
83
|
+
snippet: title ? '' : excerpt(textOf(b, prose)),
|
|
84
|
+
source,
|
|
85
|
+
[list]: of,
|
|
86
|
+
...status ? {
|
|
87
|
+
status
|
|
88
|
+
} : {}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
// The ids the comments among these are on, by the comment's eid, each as a
|
|
93
|
+
// line names it (its human id where it has one).
|
|
94
|
+
let targets = async (host, found)=>{
|
|
95
|
+
let on = new Map();
|
|
96
|
+
for (let b of found){
|
|
97
|
+
let target = b.comment?.target;
|
|
98
|
+
if (typeof target == 'string') on.set(b.entity.eid, target);
|
|
99
|
+
}
|
|
100
|
+
if (!on.size) return new Map();
|
|
101
|
+
let rows = await host.graph.get([
|
|
102
|
+
...new Set(on.values())
|
|
103
|
+
], host.vocab.kinds);
|
|
104
|
+
let name = called(host.vocab);
|
|
105
|
+
let named = new Map(rows.map((r)=>[
|
|
106
|
+
r.entity.eid,
|
|
107
|
+
name(r)
|
|
108
|
+
]));
|
|
109
|
+
return new Map([
|
|
110
|
+
...on
|
|
111
|
+
].map(([c, t])=>[
|
|
112
|
+
c,
|
|
113
|
+
named.get(t) ?? t
|
|
114
|
+
]));
|
|
115
|
+
};
|
|
116
|
+
// The integer ids these eids' rows are keyed by, for the ones this database
|
|
117
|
+
// holds: a rehearsed create (`check: true`) holds none.
|
|
118
|
+
let owners = (db, eids)=>db.query(select({
|
|
119
|
+
cols: [
|
|
120
|
+
as(col('id'), 'id')
|
|
121
|
+
],
|
|
122
|
+
from: table('entity'),
|
|
123
|
+
where: oneOf(col('eid'), eids)
|
|
124
|
+
})).map((r)=>Number(r.id));
|
|
125
|
+
// A promise's value, or undefined once WAIT passes or it fails: an answer
|
|
126
|
+
// waits this long for a model and never longer.
|
|
127
|
+
let within = (p)=>new Promise((done)=>{
|
|
128
|
+
let t = setTimeout(()=>done(undefined), WAIT);
|
|
129
|
+
p.then((v)=>(clearTimeout(t), done(v)), ()=>(clearTimeout(t), done(undefined)));
|
|
130
|
+
});
|
|
131
|
+
// The sources that have a vector for their text: the ones the sweep already
|
|
132
|
+
// made, and the rest made now and stored as the sweep stores them, where the
|
|
133
|
+
// model answers in time.
|
|
134
|
+
let vectored = async (db, embedder, made)=>{
|
|
135
|
+
let owed = made.filter((s)=>s.had != hash(embedder.model, s.text));
|
|
136
|
+
let got = await Promise.all(owed.map((s)=>within(Promise.resolve().then(()=>embedder.embed(s.text)))));
|
|
137
|
+
let have = new Set(made.filter((s)=>!owed.includes(s)).map((s)=>s.entity));
|
|
138
|
+
await db.atomic(()=>{
|
|
139
|
+
owed.forEach((s, i)=>{
|
|
140
|
+
let v = got[i];
|
|
141
|
+
if (v && put(db, s, embedder.model, v)) have.add(s.entity);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
return have;
|
|
145
|
+
};
|
|
146
|
+
// The words a twin would share: the first line's words (a document's title),
|
|
147
|
+
// the short ones left out, since every word must match.
|
|
148
|
+
let words = (s)=>(s.split('\n')[0].match(/[\p{L}\p{N}]{3,}/gu) ?? []).slice(0, 8);
|
|
149
|
+
/** What a neighbour in either list is: wearing no notice, or written by a
|
|
150
|
+
* person, where the vocabulary knows people. */ export let resources = (vocab)=>{
|
|
151
|
+
let notices = vocab.all.filter((c)=>vocab.comp(c)?.notice);
|
|
152
|
+
if (!notices.length) return [];
|
|
153
|
+
let none = and(...notices.map(absent));
|
|
154
|
+
return [
|
|
155
|
+
vocab.comp('person') ? or(none, present('created.by.person')) : none
|
|
156
|
+
];
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* The reply: for each entity the call's write made, its nearest few existing
|
|
160
|
+
* entities of the same kind, then its nearest few resources of any kind, as
|
|
161
|
+
* `hit` bundles.
|
|
162
|
+
*/ export let neighbours = (host, options = {})=>{
|
|
163
|
+
let db = statements(host.storage);
|
|
164
|
+
// Words are a query only where the vocabulary indexes words at all.
|
|
165
|
+
let indexed = fields(host.vocab, searched).length > 0;
|
|
166
|
+
let kept = resources(host.vocab);
|
|
167
|
+
return async (call, _answer, wrote)=>{
|
|
168
|
+
let kinds = new Map();
|
|
169
|
+
for (let b of wrote){
|
|
170
|
+
let kind = became(host.vocab, b);
|
|
171
|
+
if (kind) kinds.set(b.entity.eid, kind);
|
|
172
|
+
}
|
|
173
|
+
let fresh = new Set(kinds.keys());
|
|
174
|
+
let born = [
|
|
175
|
+
...kinds
|
|
176
|
+
].filter(([, k])=>k != 'entity').map(([e])=>e);
|
|
177
|
+
if (!born.length) return [];
|
|
178
|
+
let now = await ready(host.vocab, options, host.graph, host.embedders);
|
|
179
|
+
let prose = resolved(now.text, host.derived);
|
|
180
|
+
// The first few that have text: a plan's links come back beside its tasks.
|
|
181
|
+
let made = sources(db, prose, owners(db, born)).slice(0, MOST);
|
|
182
|
+
if (!made.length) return [];
|
|
183
|
+
let meant = now.embedder ? await vectored(db, now.embedder, made) : new Set();
|
|
184
|
+
// What the calling session wrote is its own, and no news to it. `!=`
|
|
185
|
+
// holds where `created.via` is absent too (@yaks/sql `ne`), and spelling
|
|
186
|
+
// that out as an OR made the screen a union over every `created` row:
|
|
187
|
+
// over two minutes on the box's graph.
|
|
188
|
+
let via = who(call)?.via;
|
|
189
|
+
let others = via ? [
|
|
190
|
+
ne('created.via', via)
|
|
191
|
+
] : [];
|
|
192
|
+
let out = [];
|
|
193
|
+
for (let s of made){
|
|
194
|
+
let meaning = meant.has(s.entity);
|
|
195
|
+
let terms = words(s.text);
|
|
196
|
+
if (!meaning && !(indexed && terms.length)) continue;
|
|
197
|
+
let source = meaning ? 'meaning' : 'text';
|
|
198
|
+
let shown = new Set(fresh);
|
|
199
|
+
// The nearest few matching `by`, past the ones already shown: room for
|
|
200
|
+
// them, which are not neighbours yet.
|
|
201
|
+
let nearest = async (list, by)=>{
|
|
202
|
+
let room = limit(FEW + shown.size);
|
|
203
|
+
let ranked = meaning ? [
|
|
204
|
+
near(s.entity),
|
|
205
|
+
order(SIMILAR)
|
|
206
|
+
] : [
|
|
207
|
+
...terms.map(text),
|
|
208
|
+
order(SEARCH)
|
|
209
|
+
];
|
|
210
|
+
let found = (await host.graph.read(and(...by, ...kept, ...others, ...ranked, room, every()))).filter((b)=>!shown.has(b.entity.eid)).slice(0, FEW);
|
|
211
|
+
found.forEach((b)=>shown.add(b.entity.eid));
|
|
212
|
+
let on = await targets(host, found);
|
|
213
|
+
out.push(...found.map((b)=>hit(host.vocab, b, s.entity, list, source, prose, on.get(b.entity.eid))));
|
|
214
|
+
};
|
|
215
|
+
await nearest('near', [
|
|
216
|
+
present(kinds.get(s.entity))
|
|
217
|
+
]);
|
|
218
|
+
await nearest('related', []);
|
|
219
|
+
}
|
|
220
|
+
return out;
|
|
221
|
+
};
|
|
222
|
+
};
|
|
223
|
+
//# sourceMappingURL=neighbours.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"neighbours.js","sources":["./neighbours.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,4EAA4E;AAC5E,kCAAkC;AAClC,EAAE;AACF,4EAA4E;AAC5E,6EAA6E;AAC7E,8EAA8E;AAC9E,4EAA4E;AAC5E,4EAA4E;AAC5E,yEAAyE;AACzE,8EAA8E;AAC9E,8EAA8E;AAC9E,4EAA4E;AAC5E,4EAA4E;AAC5E,2EAA2E;AAC3E,6EAA6E;AAC7E,0EAA0E;AAC1E,sBAAsB;AACtB,EAAE;AACF,0EAA0E;AAC1E,+EAA+E;AAC/E,wEAAwE;AACxE,eAAe;AACf,EAAE;AACF,4EAA4E;AAC5E,uEAAuE;AACvE,2EAA2E;AAC3E,0EAA0E;AAC1E,4EAA4E;AAC5E,4EAA4E;AAC5E,qEAAqE;AACrE,wEAAwE;AACxE,EAAE;AACF,0EAA0E;AAC1E,6DAA6D;AAE7D,SAAuD,GAAG,2BAAgC;AAE1F,SAAS,MAAM,+BAAoC;AACnD,SAAS,MAAM,2BAA4C;AAC3D,SACE,MAAM,EACN,GAAG,EAEH,KAAK,EACL,KAAK,EACL,EAAE,EACF,IAAI,EACJ,EAAE,EACF,KAAK,EACL,OAAO,EACP,IAAI,2BACyB;AAC/B,SACE,EAAE,EACF,GAAG,EAEH,KAAK,EACL,MAAM,EAEN,UAAU,EACV,KAAK,yBACsB;AAE7B,SAAwB,IAAI,wBAAuB;AACnD,SAAqB,MAAM,EAAE,QAAQ,EAAE,QAAQ,sBAAqB;AACpE,SAAkC,KAAK,uBAAsB;AAC7D,SAAS,OAAO,sBAAqB;AACrC,SAAS,GAAG,EAAe,OAAO,qBAAoB;AACtD,S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