@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/fill.ts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// Reconciliation is a queue too: a schema or model change can owe the whole
|
|
2
|
+
// corpus. Persist source-table cursors so each pass seeks a bounded window,
|
|
3
|
+
// and a restart resumes it without one enormous INSERT SELECT.
|
|
4
|
+
import {
|
|
5
|
+
and,
|
|
6
|
+
as,
|
|
7
|
+
col,
|
|
8
|
+
each,
|
|
9
|
+
eq,
|
|
10
|
+
fn,
|
|
11
|
+
from,
|
|
12
|
+
gt,
|
|
13
|
+
type Insert,
|
|
14
|
+
le,
|
|
15
|
+
lit,
|
|
16
|
+
op,
|
|
17
|
+
select,
|
|
18
|
+
type Statements,
|
|
19
|
+
table,
|
|
20
|
+
val,
|
|
21
|
+
} from '@yaks/sql'
|
|
22
|
+
import { FILL, OWED, TABLE } from './ddl.js'
|
|
23
|
+
import { type Field, tablesIn, wearers } from './fields.js'
|
|
24
|
+
|
|
25
|
+
/** Queue owners, retaining the generation a concurrent write advances. */
|
|
26
|
+
export let queue = (rows: Pick<Insert, 'rows' | 'q'>): Insert => ({
|
|
27
|
+
t: 'insert',
|
|
28
|
+
into: OWED,
|
|
29
|
+
cols: ['owner', 'n'],
|
|
30
|
+
...rows,
|
|
31
|
+
upsert: [{ on: [col('owner')], set: { n: op('+', col('n'), lit(1)) } }],
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
let key = (name: string) => name == TABLE ? 'owner' : 'entity'
|
|
35
|
+
|
|
36
|
+
/** Schedule source tables through their current high key. New writes are
|
|
37
|
+
* already covered by the installed triggers. Rescheduling restarts a table,
|
|
38
|
+
* so another fields change cannot skip owners the earlier fields excluded. */
|
|
39
|
+
export let schedule = (db: Statements, names: Iterable<string>): void => {
|
|
40
|
+
for (let name of new Set(names)) {
|
|
41
|
+
let [r] = db.query(select({
|
|
42
|
+
cols: [as(fn('max', col(key(name))), 'last')],
|
|
43
|
+
from: table(name),
|
|
44
|
+
}))
|
|
45
|
+
if (r.last == null) continue
|
|
46
|
+
db.query({
|
|
47
|
+
t: 'insert',
|
|
48
|
+
into: FILL,
|
|
49
|
+
cols: ['name', 'after', 'last'],
|
|
50
|
+
rows: [[val(name), lit(0), val(Number(r.last))]],
|
|
51
|
+
upsert: [{
|
|
52
|
+
on: [col('name')],
|
|
53
|
+
set: { after: lit(0), last: col('last', 'excluded') },
|
|
54
|
+
}],
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Whether reconciliation still has source rows to visit: looked at once
|
|
60
|
+
* while its table's rows stand. */
|
|
61
|
+
let looked = new WeakMap<Statements, { at: number; filling: boolean }>()
|
|
62
|
+
export let filling = (db: Statements): boolean => {
|
|
63
|
+
let at = db.revision('data', FILL)
|
|
64
|
+
let held = looked.get(db)
|
|
65
|
+
if (held?.at !== at) {
|
|
66
|
+
let rows = db.query(
|
|
67
|
+
select({ cols: [lit(1)], from: table(FILL), limit: lit(1) }),
|
|
68
|
+
)
|
|
69
|
+
looked.set(db, held = { at, filling: rows.length > 0 })
|
|
70
|
+
}
|
|
71
|
+
return held.filling
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Queue at most `limit` candidate rows, including rows without eligible
|
|
75
|
+
* text. Applying the keyset window before eligibility also bounds a long
|
|
76
|
+
* excluded tail. Advancing the cursor and queueing its owners commit together. */
|
|
77
|
+
export let fill = (
|
|
78
|
+
db: Statements,
|
|
79
|
+
fields: Field[],
|
|
80
|
+
limit = 256,
|
|
81
|
+
): void | Promise<void> => {
|
|
82
|
+
if (!filling(db)) return
|
|
83
|
+
return db.atomic(() => filled(db, fields, limit))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** {@link fill} inside a unit the caller already holds. */
|
|
87
|
+
export let filled = (db: Statements, fields: Field[], limit = 256): void => {
|
|
88
|
+
let names = tablesIn(db)
|
|
89
|
+
while (limit > 0) {
|
|
90
|
+
let [job] = db.query(select({
|
|
91
|
+
cols: [col('name'), col('after'), col('last')],
|
|
92
|
+
from: table(FILL),
|
|
93
|
+
order: [col('name')],
|
|
94
|
+
limit: lit(1),
|
|
95
|
+
}))
|
|
96
|
+
if (!job) return
|
|
97
|
+
let name = String(job.name)
|
|
98
|
+
let where = eq(col('name'), val(name))
|
|
99
|
+
let rows = names.has(name)
|
|
100
|
+
? db.query(select({
|
|
101
|
+
cols: [as(col(key(name)), 'owner')],
|
|
102
|
+
from: table(name),
|
|
103
|
+
where: and(
|
|
104
|
+
gt(col(key(name)), val(Number(job.after))),
|
|
105
|
+
le(col(key(name)), val(Number(job.last))),
|
|
106
|
+
),
|
|
107
|
+
order: [col(key(name))],
|
|
108
|
+
limit: val(limit),
|
|
109
|
+
}))
|
|
110
|
+
: []
|
|
111
|
+
let owners = rows.map((r) => Number(r.owner))
|
|
112
|
+
if (owners.length) {
|
|
113
|
+
let candidates = select({
|
|
114
|
+
cols: [as(col('value'), 'owner')],
|
|
115
|
+
from: from(each(owners), 'ids'),
|
|
116
|
+
})
|
|
117
|
+
let vectors = select({
|
|
118
|
+
cols: [col('owner', 'v')],
|
|
119
|
+
from: from(candidates, 'o'),
|
|
120
|
+
joins: [{
|
|
121
|
+
how: 'cross',
|
|
122
|
+
src: table(TABLE, 'v'),
|
|
123
|
+
on: eq(col('owner', 'v'), col('owner', 'o')),
|
|
124
|
+
}],
|
|
125
|
+
})
|
|
126
|
+
for (let q of [...wearers(fields, db.arms, candidates), vectors]) {
|
|
127
|
+
db.query(queue({
|
|
128
|
+
q: select({ cols: [col('owner'), lit(1)], from: from(q, 'w') }),
|
|
129
|
+
}))
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (rows.length < limit || owners.at(-1) == Number(job.last)) {
|
|
133
|
+
db.query({ t: 'delete', from: FILL, where })
|
|
134
|
+
} else {
|
|
135
|
+
db.query({
|
|
136
|
+
t: 'update',
|
|
137
|
+
table: FILL,
|
|
138
|
+
set: { after: val(owners.at(-1)!) },
|
|
139
|
+
where,
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
limit -= rows.length
|
|
143
|
+
}
|
|
144
|
+
}
|
package/graph.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// Where the vectors are kept and read: the `graph` export
|
|
2
|
+
// (`@yaks/embedding/graph`) — the vector table, which a writing host adds to its
|
|
3
|
+
// own database, the `.near` compiler the read path consults, and the
|
|
4
|
+
// reply a tool call that created something is answered with (./neighbours.ts).
|
|
5
|
+
// "The server" here means whichever process opened the graph and loaded this
|
|
6
|
+
// package.
|
|
7
|
+
//
|
|
8
|
+
// `.near=<entity>&.order=similar` is a query, so it has to be answerable
|
|
9
|
+
// wherever a query is asked — `GET /query`, a subscription, an MCP tool, the
|
|
10
|
+
// CLI — and not only where somebody remembered to pass the compiler an
|
|
11
|
+
// extension. That is what this export is for: a server that composed this
|
|
12
|
+
// plugin can ask for a neighbourhood, and one that did not gets the compiler's
|
|
13
|
+
// own refusal. Nobody wires it up by hand.
|
|
14
|
+
//
|
|
15
|
+
// This package declares no component. No client ever writes a vector: it is
|
|
16
|
+
// derived from text another package's vocabulary declares, it is never sent to
|
|
17
|
+
// a client, and no patch creates one — which is why the table is created in SQL
|
|
18
|
+
// here rather than by the store. The one thing `./vocab` does declare is the
|
|
19
|
+
// index's check (./tools.ts), which is a tool and not a component.
|
|
20
|
+
import { semantic } from "./compile.js";
|
|
21
|
+
import { statements } from "@yaks/sql";
|
|
22
|
+
import { schema as tables } from "./ddl.js";
|
|
23
|
+
import { resolved } from "./fields.js";
|
|
24
|
+
import { chosen, ready, spaceOf } from "./options.js";
|
|
25
|
+
import { meaning as search } from "./search.js";
|
|
26
|
+
import { neighbours } from "./neighbours.js";
|
|
27
|
+
import { hold } from "./held.js";
|
|
28
|
+
import { context, during, peek, scope } from "@yaks/trace";
|
|
29
|
+
/** The vector tables, which a writing host adds with the
|
|
30
|
+
* rest of its composed schema. It contributes no rule to `apply()`: nothing a
|
|
31
|
+
* client writes is a vector, and what keeps the vectors in step with the text
|
|
32
|
+
* is the sweep (`./service`), off the write path. */ export let schema = (host)=>tables(host ? statements(host.storage).ownership : undefined);
|
|
33
|
+
/** Binding the index does not create or migrate its tables. */ export let plugins = ()=>[];
|
|
34
|
+
/** The `.near` and `.order=similar` compiler, over the vectors this server
|
|
35
|
+
* stores. One extension serves every query: it is told when a new one begins
|
|
36
|
+
* (@yaks/sql `Begin`), so a neighbourhood never outlives the query that
|
|
37
|
+
* resolved it.
|
|
38
|
+
*
|
|
39
|
+
* What it needs is the vector space, not the embedding function — compiling a
|
|
40
|
+
* query reads the vector an entity already has and never embeds anything — so
|
|
41
|
+
* a server still waiting for a key can still answer `.near` over whatever is
|
|
42
|
+
* stored. Only a config that names no embedder at all has no space to rank in;
|
|
43
|
+
* that one contributes no extension, and `.near` gets the compiler's own
|
|
44
|
+
* refusal. */ export let extend = (host, options = {})=>{
|
|
45
|
+
let db = statements(host.storage);
|
|
46
|
+
let model = spaceOf(options.embedder, host.embedders);
|
|
47
|
+
let text;
|
|
48
|
+
try {
|
|
49
|
+
text = host.vocab ? resolved(chosen(host.vocab, options), host.derived) : undefined;
|
|
50
|
+
} catch {
|
|
51
|
+
// An invalid text configuration waits, like the sweep's ready() result.
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
return model ? [
|
|
55
|
+
semantic(db, {
|
|
56
|
+
model
|
|
57
|
+
}, {
|
|
58
|
+
fields: text,
|
|
59
|
+
limit: options.neighbours,
|
|
60
|
+
floor: options.floor
|
|
61
|
+
})
|
|
62
|
+
] : [];
|
|
63
|
+
};
|
|
64
|
+
/** A phrase search over this graph's vectors, among what the `within` filter
|
|
65
|
+
* line admits where one is given (the storage's `screen` compiles it). Resolve
|
|
66
|
+
* options and the rows they name on every call: the service may have started
|
|
67
|
+
* embedding after a key or a provider arrived, without a new host or a new
|
|
68
|
+
* search function. */ export let meaning = (host, options = {})=>{
|
|
69
|
+
let db = statements(host.storage);
|
|
70
|
+
let among = (within)=>{
|
|
71
|
+
let screen = host.storage.screen;
|
|
72
|
+
if (!screen) throw new Error('this storage cannot narrow a search');
|
|
73
|
+
return ()=>screen(within);
|
|
74
|
+
};
|
|
75
|
+
let find = async (words, opts)=>{
|
|
76
|
+
let { within, ...rest } = opts;
|
|
77
|
+
let at = context();
|
|
78
|
+
let now = await ready(host.vocab, options, host.graph, host.embedders);
|
|
79
|
+
let embedder = now.embedder;
|
|
80
|
+
return embedder ? await scope(at, ()=>search(db, resolved(now.text, host.derived), embedder, words, {
|
|
81
|
+
...rest,
|
|
82
|
+
vocab: host.vocab,
|
|
83
|
+
floor: rest.floor ?? options.floor,
|
|
84
|
+
...within ? {
|
|
85
|
+
screen: among(within)
|
|
86
|
+
} : {}
|
|
87
|
+
})) : [];
|
|
88
|
+
};
|
|
89
|
+
// A span on the graph's channel, so the statements a search runs and its
|
|
90
|
+
// embedding request are seen where the graph's own reads are: a read that
|
|
91
|
+
// runs long names the step it is in (@yaks/cli read_activity.ts).
|
|
92
|
+
return (words, opts = {})=>{
|
|
93
|
+
let c = peek(host.graph);
|
|
94
|
+
if (!c) return find(words, opts);
|
|
95
|
+
return during(c.begin({
|
|
96
|
+
kind: 'query',
|
|
97
|
+
name: 'meaning',
|
|
98
|
+
package: '@yaks/embedding'
|
|
99
|
+
}), ()=>find(words, opts), 'ok', (hits)=>({
|
|
100
|
+
rows: hits.length
|
|
101
|
+
}));
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
/** Hold this graph's vectors in memory before the first search asks for them
|
|
105
|
+
* (./held.ts), so a process answering many searches reads its copy before it
|
|
106
|
+
* answers any, not during one. Nothing where no embedder names a space. */ export let preload = (host, options = {})=>()=>{
|
|
107
|
+
let model = spaceOf(options.embedder, host.embedders);
|
|
108
|
+
if (model) hold(statements(host.storage), model);
|
|
109
|
+
};
|
|
110
|
+
/** What a direct tool call's answer carries beside the tool's own: for each
|
|
111
|
+
* entity the call created, its nearest few of the same kind (./neighbours.ts),
|
|
112
|
+
* so a twin is seen while it is being written. */ export let reply = (host, options = {})=>neighbours(host, options);
|
|
113
|
+
//# sourceMappingURL=graph.js.map
|
package/graph.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph.js","sources":["./graph.ts"],"names":[],"mappings":"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"}
|
package/graph.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// Where the vectors are kept and read: the `graph` export
|
|
2
|
+
// (`@yaks/embedding/graph`) — the vector table, which a writing host adds to its
|
|
3
|
+
// own database, the `.near` compiler the read path consults, and the
|
|
4
|
+
// reply a tool call that created something is answered with (./neighbours.ts).
|
|
5
|
+
// "The server" here means whichever process opened the graph and loaded this
|
|
6
|
+
// package.
|
|
7
|
+
//
|
|
8
|
+
// `.near=<entity>&.order=similar` is a query, so it has to be answerable
|
|
9
|
+
// wherever a query is asked — `GET /query`, a subscription, an MCP tool, the
|
|
10
|
+
// CLI — and not only where somebody remembered to pass the compiler an
|
|
11
|
+
// extension. That is what this export is for: a server that composed this
|
|
12
|
+
// plugin can ask for a neighbourhood, and one that did not gets the compiler's
|
|
13
|
+
// own refusal. Nobody wires it up by hand.
|
|
14
|
+
//
|
|
15
|
+
// This package declares no component. No client ever writes a vector: it is
|
|
16
|
+
// derived from text another package's vocabulary declares, it is never sent to
|
|
17
|
+
// a client, and no patch creates one — which is why the table is created in SQL
|
|
18
|
+
// here rather than by the store. The one thing `./vocab` does declare is the
|
|
19
|
+
// index's check (./tools.ts), which is a tool and not a component.
|
|
20
|
+
|
|
21
|
+
import type { Graph, Plugin } from 'jsr:@yaks/graph@0.2.36'
|
|
22
|
+
import type { Reply } from 'jsr:@yaks/tools@0.2.36'
|
|
23
|
+
import type { Derived, Extension, Raw } from '@yaks/sql'
|
|
24
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
25
|
+
import { semantic } from './compile.js'
|
|
26
|
+
import { type Statements, statements, type Stmt } from '@yaks/sql'
|
|
27
|
+
import { schema as tables } from './ddl.js'
|
|
28
|
+
import { resolved } from './fields.js'
|
|
29
|
+
import { chosen, type Lent, type Options, ready, spaceOf } from './options.js'
|
|
30
|
+
import { type Hit, meaning as search, type MeaningOpts } from './search.js'
|
|
31
|
+
import { type Host, neighbours } from './neighbours.js'
|
|
32
|
+
import { hold } from './held.js'
|
|
33
|
+
import { context, during, peek, scope } from '@yaks/trace'
|
|
34
|
+
|
|
35
|
+
/** The vector tables, which a writing host adds with the
|
|
36
|
+
* rest of its composed schema. It contributes no rule to `apply()`: nothing a
|
|
37
|
+
* client writes is a vector, and what keeps the vectors in step with the text
|
|
38
|
+
* is the sweep (`./service`), off the write path. */
|
|
39
|
+
export let schema = (host?: { storage: { statements?: Statements } }): Stmt[] =>
|
|
40
|
+
tables(host ? statements(host.storage).ownership : undefined)
|
|
41
|
+
|
|
42
|
+
/** Binding the index does not create or migrate its tables. */
|
|
43
|
+
export let plugins = (): Plugin[] => []
|
|
44
|
+
|
|
45
|
+
/** The `.near` and `.order=similar` compiler, over the vectors this server
|
|
46
|
+
* stores. One extension serves every query: it is told when a new one begins
|
|
47
|
+
* (@yaks/sql `Begin`), so a neighbourhood never outlives the query that
|
|
48
|
+
* resolved it.
|
|
49
|
+
*
|
|
50
|
+
* What it needs is the vector space, not the embedding function — compiling a
|
|
51
|
+
* query reads the vector an entity already has and never embeds anything — so
|
|
52
|
+
* a server still waiting for a key can still answer `.near` over whatever is
|
|
53
|
+
* stored. Only a config that names no embedder at all has no space to rank in;
|
|
54
|
+
* that one contributes no extension, and `.near` gets the compiler's own
|
|
55
|
+
* refusal. */
|
|
56
|
+
export let extend = (
|
|
57
|
+
host: {
|
|
58
|
+
storage: { statements?: Statements }
|
|
59
|
+
vocab?: Vocab
|
|
60
|
+
derived?: Derived
|
|
61
|
+
embedders?: Lent
|
|
62
|
+
},
|
|
63
|
+
options: Options = {},
|
|
64
|
+
): Extension[] => {
|
|
65
|
+
let db = statements(host.storage)
|
|
66
|
+
let model = spaceOf(options.embedder, host.embedders)
|
|
67
|
+
let text
|
|
68
|
+
try {
|
|
69
|
+
text = host.vocab
|
|
70
|
+
? resolved(chosen(host.vocab, options), host.derived)
|
|
71
|
+
: undefined
|
|
72
|
+
} catch {
|
|
73
|
+
// An invalid text configuration waits, like the sweep's ready() result.
|
|
74
|
+
return []
|
|
75
|
+
}
|
|
76
|
+
return model
|
|
77
|
+
? [semantic(db, { model }, {
|
|
78
|
+
fields: text,
|
|
79
|
+
limit: options.neighbours,
|
|
80
|
+
floor: options.floor,
|
|
81
|
+
})]
|
|
82
|
+
: []
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** A phrase search over this graph's vectors, among what the `within` filter
|
|
86
|
+
* line admits where one is given (the storage's `screen` compiles it). Resolve
|
|
87
|
+
* options and the rows they name on every call: the service may have started
|
|
88
|
+
* embedding after a key or a provider arrived, without a new host or a new
|
|
89
|
+
* search function. */
|
|
90
|
+
export let meaning = (
|
|
91
|
+
host: {
|
|
92
|
+
storage: {
|
|
93
|
+
statements?: Statements
|
|
94
|
+
screen?: (query: string) => Raw | null
|
|
95
|
+
}
|
|
96
|
+
vocab: Vocab
|
|
97
|
+
graph: Pick<Graph, 'get'>
|
|
98
|
+
derived?: Derived
|
|
99
|
+
embedders?: Lent
|
|
100
|
+
},
|
|
101
|
+
options: Options = {},
|
|
102
|
+
): (
|
|
103
|
+
words: string,
|
|
104
|
+
opts?: Pick<MeaningOpts, 'limit' | 'floor'> & { within?: string },
|
|
105
|
+
) => Promise<Hit[]> => {
|
|
106
|
+
let db = statements(host.storage)
|
|
107
|
+
let among = (within: string) => {
|
|
108
|
+
let screen = host.storage.screen
|
|
109
|
+
if (!screen) throw new Error('this storage cannot narrow a search')
|
|
110
|
+
return () => screen(within)
|
|
111
|
+
}
|
|
112
|
+
let find = async (
|
|
113
|
+
words: string,
|
|
114
|
+
opts: Pick<MeaningOpts, 'limit' | 'floor'> & { within?: string },
|
|
115
|
+
) => {
|
|
116
|
+
let { within, ...rest } = opts
|
|
117
|
+
let at = context()
|
|
118
|
+
let now = await ready(host.vocab, options, host.graph, host.embedders)
|
|
119
|
+
let embedder = now.embedder
|
|
120
|
+
return embedder
|
|
121
|
+
? await scope(at, () =>
|
|
122
|
+
search(
|
|
123
|
+
db,
|
|
124
|
+
resolved(now.text, host.derived),
|
|
125
|
+
embedder,
|
|
126
|
+
words,
|
|
127
|
+
{
|
|
128
|
+
...rest,
|
|
129
|
+
vocab: host.vocab,
|
|
130
|
+
floor: rest.floor ?? options.floor,
|
|
131
|
+
...(within ? { screen: among(within) } : {}),
|
|
132
|
+
},
|
|
133
|
+
))
|
|
134
|
+
: []
|
|
135
|
+
}
|
|
136
|
+
// A span on the graph's channel, so the statements a search runs and its
|
|
137
|
+
// embedding request are seen where the graph's own reads are: a read that
|
|
138
|
+
// runs long names the step it is in (@yaks/cli read_activity.ts).
|
|
139
|
+
return (words, opts = {}) => {
|
|
140
|
+
let c = peek(host.graph)
|
|
141
|
+
if (!c) return find(words, opts)
|
|
142
|
+
return during(
|
|
143
|
+
c.begin({ kind: 'query', name: 'meaning', package: '@yaks/embedding' }),
|
|
144
|
+
() => find(words, opts),
|
|
145
|
+
'ok',
|
|
146
|
+
(hits) => ({ rows: hits.length }),
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Hold this graph's vectors in memory before the first search asks for them
|
|
152
|
+
* (./held.ts), so a process answering many searches reads its copy before it
|
|
153
|
+
* answers any, not during one. Nothing where no embedder names a space. */
|
|
154
|
+
export let preload = (
|
|
155
|
+
host: { storage: { statements?: Statements }; embedders?: Lent },
|
|
156
|
+
options: Options = {},
|
|
157
|
+
): () => void =>
|
|
158
|
+
() => {
|
|
159
|
+
let model = spaceOf(options.embedder, host.embedders)
|
|
160
|
+
if (model) hold(statements(host.storage), model)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** What a direct tool call's answer carries beside the tool's own: for each
|
|
164
|
+
* entity the call created, its nearest few of the same kind (./neighbours.ts),
|
|
165
|
+
* so a twin is seen while it is being written. */
|
|
166
|
+
export let reply = (host: Host, options: Options = {}): Reply =>
|
|
167
|
+
neighbours(host, options)
|