@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/_dist/owed.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { type CreateTrigger } from "@yaks/sql";
|
|
2
|
+
import type { Statements } from "@yaks/sql";
|
|
3
|
+
import type { Bundle } from "@yaks/graph";
|
|
4
|
+
import { type Field } from "./fields.js";
|
|
5
|
+
/**
|
|
6
|
+
* The triggers that keep the queue: for each component a field lives on, its
|
|
7
|
+
* rows arriving, changing and leaving; for text an entity is found by through
|
|
8
|
+
* another component (`on`), that component arriving and leaving too, and the
|
|
9
|
+
* text counting only while it does; exclusions arriving and leaving; an entity
|
|
10
|
+
* with a vector being deleted or restored; and a vector deleted by anything
|
|
11
|
+
* but the sweep, which is owed back.
|
|
12
|
+
*/ export declare let triggers: (fields: Field[]) => CreateTrigger[];
|
|
13
|
+
/**
|
|
14
|
+
* Whether writing these bundles can have queued an entity: whether one names
|
|
15
|
+
* a component a trigger watches ({@link triggers}), or deletes or restores
|
|
16
|
+
* its entity. A host that drains the queue after a write asks the queue only
|
|
17
|
+
* then.
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { queues } from '@yaks/embedding'
|
|
21
|
+
*
|
|
22
|
+
* let fields = [{ comp: 'doc', prop: 'body' }]
|
|
23
|
+
* queues(fields, [{ entity: { eid: 'a' }, doc: { body: 'hi' } }]) // true
|
|
24
|
+
* queues(fields, [{ entity: { eid: 'a' }, $delete: true }]) // true
|
|
25
|
+
* queues(fields, [{ entity: { eid: 'a' }, place: { x: 1 } }]) // false
|
|
26
|
+
* ```
|
|
27
|
+
*/ export declare let queues: (fields: Field[], bundles: Bundle[]) => boolean;
|
|
28
|
+
/** A queue trigger as the database holds it: its name, the table it is on,
|
|
29
|
+
* and the statement it was made by. */ export type Standing = {
|
|
30
|
+
name: string;
|
|
31
|
+
on: string;
|
|
32
|
+
sql: string;
|
|
33
|
+
};
|
|
34
|
+
/** What making the database's triggers the wanted ones takes. */ export type Moves = {
|
|
35
|
+
/** the held triggers to drop: gone from the fields, or said otherwise */ drop: string[];
|
|
36
|
+
/** the wanted triggers to make */ make: CreateTrigger[];
|
|
37
|
+
/** whose wearers are owed a look: the tables a moved trigger is on, or
|
|
38
|
+
* everyone (`true`) when one on the vectors or the tombstones moved */ owed: Set<string> | true;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* What {@link watch} does about the triggers a database holds, given the
|
|
42
|
+
* ones the fields want. A trigger that stands as wanted owes nothing: it saw
|
|
43
|
+
* every write to its table. One made, dropped or changed missed or misread
|
|
44
|
+
* its table's writes, so the entities in that table are owed a look, and no
|
|
45
|
+
* others.
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { equal } from '@yaks/testing'
|
|
49
|
+
* import { render } from '@yaks/sql'
|
|
50
|
+
* let book = { comp: 'book', prop: 'title' }
|
|
51
|
+
* let want = triggers([book])
|
|
52
|
+
* let held = want.map((t) => ({ name: t.name, on: t.on, sql: render(t).sql }))
|
|
53
|
+
* equal(moves(held, want).owed, new Set())
|
|
54
|
+
* let more = triggers([book, { comp: 'review', prop: 'prose' }])
|
|
55
|
+
* equal(moves(held, more).owed, new Set(['review']))
|
|
56
|
+
* equal(moves([], want).owed, true)
|
|
57
|
+
* ```
|
|
58
|
+
*/ export declare let moves: (have: Standing[], want: CreateTrigger[], write?: (s: CreateTrigger) => {
|
|
59
|
+
sql: string;
|
|
60
|
+
}) => Moves;
|
|
61
|
+
/**
|
|
62
|
+
* Make the database's queue triggers the ones {@link triggers} says, and
|
|
63
|
+
* queue the entities whose writes a missing or changed trigger may have
|
|
64
|
+
* missed ({@link moves}). Returns whether it changed anything. A second call
|
|
65
|
+
* with the same fields and schema reads and writes nothing.
|
|
66
|
+
*/ export declare let watch: (db: Statements, wanted: Field[]) => boolean | Promise<boolean>;
|
|
67
|
+
/** Schedule every entity wearing a field or holding a vector — or, given
|
|
68
|
+
* `tables`, their owners only. Queue a bounded first window now; the sweep
|
|
69
|
+
* resumes the durable cursors for the rest. */ export declare let owe: (db: Statements, wanted: Field[], tables?: Set<string>) => void | Promise<void>;
|
|
70
|
+
/** One queued entity, and the count it was read at. */ export type Due = {
|
|
71
|
+
owner: number;
|
|
72
|
+
n: number;
|
|
73
|
+
};
|
|
74
|
+
/** The newest `limit` queued entities, newest first: what was just said is
|
|
75
|
+
* what someone is about to look for. */ export declare let due: (db: Statements, limit: number) => Due[];
|
|
76
|
+
/** Settle a queued entity, unless a write queued it again since it was read. */ export declare let paid: (db: Statements, d: Due) => void;
|
|
77
|
+
/** How many entities are queued, plus one while reconciliation has more. */ export declare let left: (db: Statements) => number;
|
|
78
|
+
/** Whether queued owners or a reconciliation cursor still need a sweep. */ export declare let owing: (db: Statements) => boolean;
|
|
79
|
+
//# sourceMappingURL=owed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"owed.d.ts","sources":["../owed.ts"],"names":[],"mappings":"AAwBA,SAKE,KAAK,aAAa,yBAaS;AAC7B,cAAc,UAAU,yBAA8B;AAEtD,cAAc,MAAM,2BAAgC;AACpD,SAAS,KAAK,KAAK,sBAA8B;AAqCjD;;;;;;;CAOC,GACD,OAAO,YAAI,WAAY,QAAQ,YAAU,gBAmDxC;AAID;;;;;;;;;;;;;;CAcC,GACD,OAAO,YAAI,SAAU,QAAQ,SAAS,SAAS,aAAW,OAAO,CAShE;AASD;qCACqC,GACrC,YAAY;EAAa,MAAM,MAAM;EAAE,IAAI,MAAM;EAAE,KAAK,MAAM;;AAE9D,+DAA+D,GAC/D,YAAY;EACV,uEAAuE,GACvE,MAAM,MAAM;EACZ,gCAAgC,GAChC,MAAM;EACN;uEACqE,GACrE,MAAM,IAAI,MAAM,IAAI,IAAI;;AAM1B;;;;;;;;;;;;;;;;;;CAkBC,GACD,OAAO,YAAI,QACT,MAAM,YACN,MAAM,iBACN,SAAQ,GAAG;EAAoB,KAAK,MAAM;MACzC,MAoBF;AAED;;;;;CAKC,GACD,OAAO,YAAI,QACT,IAAI,YACJ,QAAQ,YACP,OAAO,GAAG,QAAQ,OAAO,EA0C3B;AAED;;6CAE6C,GAC7C,OAAO,YAAI,MACT,IAAI,YACJ,QAAQ,SACR,SAAS,IAAI,MAAM,MAClB,IAAI,GAAG,QAAQ,IAAI,EAGrB;AAQD,qDAAqD,GACrD,YAAY;EAAQ,OAAO,MAAM;EAAE,GAAG,MAAM;;AAE5C;sCACsC,GACtC,OAAO,YAAI,MAAO,IAAI,YAAY,OAAO,MAAM,KAAG,MAQU;AAE5D,8EAA8E,GAC9E,OAAO,YAAI,OAAQ,IAAI,YAAY,GAAG,QAAM,IAAI,CAK5C;AAEJ,0EAA0E,GAC1E,OAAO,YAAI,OAAQ,IAAI,eAAa,MAAM,CAIrB;AAErB,yEAAyE,GACzE,OAAO,YAAI,QAAS,IAAI,eAAa,OAAO,CACN"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { type Embedder } from "./embedder.js";
|
|
2
|
+
/** The slice of `fetch` this file uses — the web one, and a Worker's. */ export type Fetch = (input: string, init?: {
|
|
3
|
+
method?: string;
|
|
4
|
+
headers?: Record<string, string>;
|
|
5
|
+
body?: string;
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
}) => Promise<{
|
|
8
|
+
ok: boolean;
|
|
9
|
+
status: number;
|
|
10
|
+
text: () => Promise<string>;
|
|
11
|
+
}>;
|
|
12
|
+
/** The APIs spoken here: Ollama's own, and OpenAI-compatible. */ export type Api = 'ollama' | 'openai';
|
|
13
|
+
/** A hosted or local embedding endpoint: a provider, and the model asked of
|
|
14
|
+
* it. */ export type Remote = {
|
|
15
|
+
/** which API the server speaks: Ollama's own, or OpenAI-compatible */ api: Api;
|
|
16
|
+
/** what the server calls the model, sent with every request */ model: string;
|
|
17
|
+
/** the space the vectors live in (default {@link space} of the model): the
|
|
18
|
+
* model's own name, whoever serves it, where the provider calls it
|
|
19
|
+
* something else */ space?: string;
|
|
20
|
+
/** the server's root, without a path (`https://ollama.example`) */ base: string;
|
|
21
|
+
/** a bearer token; a server on your own network may need none */ key?: string;
|
|
22
|
+
/** Matryoshka width: keep this many leading coordinates (see {@link cut}) */ dim?: number;
|
|
23
|
+
/** how long to wait for one request (default 30s) */ timeout?: number;
|
|
24
|
+
/** the fetch to call through (default: the global one) */ fetch?: Fetch;
|
|
25
|
+
/** pauses before resending a failed call, in milliseconds */ waits?: readonly number[];
|
|
26
|
+
} & Load;
|
|
27
|
+
/**
|
|
28
|
+
* How much of a text is sent by default. A model embeds a bounded context, and
|
|
29
|
+
* a server asked for more refuses the request rather than truncating it — which
|
|
30
|
+
* would stop a sweep at the same long document on every pass. So a long text
|
|
31
|
+
* is cut to its opening, which is what the model would have read of it anyway.
|
|
32
|
+
* qwen3-embedding refuses somewhere past 35,000 characters of markdown.
|
|
33
|
+
*/ export declare let CHARS: number;
|
|
34
|
+
/** The most inputs one request carries by default. */ export declare let COUNT: number;
|
|
35
|
+
/** The most characters one request carries by default: a batch of long texts
|
|
36
|
+
* is split before a server spends the whole timeout on it. */ export declare let LOAD: number;
|
|
37
|
+
/** The shortest text asked again after a refusal: one refused at this
|
|
38
|
+
* length is refused for more than its length. */ export declare let SHORTEST: number;
|
|
39
|
+
/** The pauses before a call that failed on the provider's side is sent again,
|
|
40
|
+
* one per resend: about eight seconds in all, which a passing failure rarely
|
|
41
|
+
* outlasts. */ export declare const retries: {
|
|
42
|
+
readonly waits: readonly number[];
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Texts split into runs of at most `count` of them and `load` characters,
|
|
46
|
+
* in order. A text longer than `load` rides alone.
|
|
47
|
+
*
|
|
48
|
+
* ```ts
|
|
49
|
+
* import { assertEquals } from '@std/assert'
|
|
50
|
+
* assertEquals(parts(['ab', 'cd', 'e', 'fghij'], 2, 4), [['ab', 'cd'], ['e'], ['fghij']])
|
|
51
|
+
* ```
|
|
52
|
+
*/ export declare let parts: (texts: string[], count: number, load: number, measure?: (text: string) => number) => string[][];
|
|
53
|
+
/**
|
|
54
|
+
* Matryoshka truncation: an MRL-trained model's leading coordinates are
|
|
55
|
+
* themselves a valid smaller embedding, so a corpus is kept at one width
|
|
56
|
+
* without a second model — and renormalized, so a cosine stays a cosine.
|
|
57
|
+
* A model that answers narrower than asked is a misconfiguration, not a
|
|
58
|
+
* vector to pad.
|
|
59
|
+
*/ export declare let cut: (v: Float32Array, dim: number) => Float32Array;
|
|
60
|
+
/** The space an embedder's vectors live in: the model, and its width when
|
|
61
|
+
* {@link cut}, since two widths of one model are two spaces. */ export declare let space: (said: {
|
|
62
|
+
model: string;
|
|
63
|
+
dim?: number;
|
|
64
|
+
}) => string;
|
|
65
|
+
/** How many texts ride one request, and how much of each is sent. */ export type Load = {
|
|
66
|
+
/** the most characters sent for one vector (default {@link CHARS}) */ chars?: number;
|
|
67
|
+
/** the most inputs one request carries (default {@link COUNT}) */ count?: number;
|
|
68
|
+
/** the most characters one request carries (default {@link LOAD}) */ load?: number;
|
|
69
|
+
/** measure of one input in the request, in the same units as `load` */ measure?: (text: string) => number;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* A provider's answer as vectors, one per input and in order, each cut to
|
|
73
|
+
* `dim` where one is asked for. An answer short of a vector is a provider
|
|
74
|
+
* that did not do what it says, and throws.
|
|
75
|
+
*/ export declare let answered: (who: string, input: string[], got: number[][] | undefined, dim?: number) => Float32Array[];
|
|
76
|
+
/** An {@link Embedder} whose every answer comes later: one that asks a
|
|
77
|
+
* provider, so a caller may treat each `embed()` as a promise. */ export type Batched = Omit<Embedder, 'embed'> & {
|
|
78
|
+
embed: (text: string) => Promise<Float32Array>;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* An {@link Embedder} over a provider that answers many texts at once. Every
|
|
82
|
+
* `embed()` made together, before the microtasks already queued have run,
|
|
83
|
+
* rides one call of `ask`, split by {@link parts} into calls no larger than
|
|
84
|
+
* `load` allows. A refused batch is retried one text at a time, so each
|
|
85
|
+
* caller gets its own vector or refusal. `ask` answers a vector per text,
|
|
86
|
+
* in order ({@link answered}).
|
|
87
|
+
*/ export declare let batched: (model: string, ask: Ask, load?: Load) => Batched;
|
|
88
|
+
/** What asks a provider: texts in, a vector each out, in order. */ export type Ask = (input: string[]) => Promise<Float32Array[]>;
|
|
89
|
+
/**
|
|
90
|
+
* `ask`, sent again after each of `waits` (default {@link retries}) while it
|
|
91
|
+
* fails other than {@link Refused}, since a refused input is refused every
|
|
92
|
+
* time. A failure that outlasts them throws once, as {@link Unanswered}.
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { assertEquals, assertRejects } from '@std/assert'
|
|
96
|
+
* import { Unanswered } from './embedder.ts'
|
|
97
|
+
* let sent = 0
|
|
98
|
+
* let flaky = retrying('workers-ai', '@cf/m', () =>
|
|
99
|
+
* ++sent < 3 ? Promise.reject(Error('3048: Unknown internal error'))
|
|
100
|
+
* : Promise.resolve([new Float32Array([1])]), [0, 0])
|
|
101
|
+
* assertEquals([[...(await flaky(['x']))[0]], sent], [[1], 3])
|
|
102
|
+
* let down = retrying('workers-ai', '@cf/m', () =>
|
|
103
|
+
* Promise.reject(Error('4009: An internal server error occured.')), [0])
|
|
104
|
+
* let error = await assertRejects(() => down(['x', 'y']), Unanswered,
|
|
105
|
+
* '@yaks/embedding: workers-ai did not answer')
|
|
106
|
+
* assertEquals((error as Unanswered).context,
|
|
107
|
+
* { model: '@cf/m', texts: '2', tries: '2' })
|
|
108
|
+
* ```
|
|
109
|
+
*/ export declare let retrying: (provider: string, model: string, ask: Ask, waits?: readonly number[], wait: (ms: number) => Promise<void>) => Ask;
|
|
110
|
+
/** `ask`, where one text asked alone and {@link Refused} is asked again as
|
|
111
|
+
* its opening half, up to `retries` times and down to `shortest`: a
|
|
112
|
+
* model that will not take a whole text still embeds what it can read of it. */ export declare let halving: (ask: Ask, _dts_1: {
|
|
113
|
+
}) => Ask;
|
|
114
|
+
/** An {@link Embedder} that asks a server for every vector, batching the
|
|
115
|
+
* calls made together. */ export declare let remote: (said: Remote) => Batched;
|
|
116
|
+
//# sourceMappingURL=remote.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remote.d.ts","sources":["../remote.ts"],"names":[],"mappings":"AAoCA,SAAS,KAAK,QAAQ,wBAAsD;AAG5E,uEAAuE,GACvE,YAAY,SACV,OAAO,MAAM,EACb;EACE,SAAS,MAAM;EACf,UAAU,OAAO,MAAM,EAAE,MAAM;EAC/B,OAAO,MAAM;EACb,SAAS;MAER;EAAU,IAAI,OAAO;EAAE,QAAQ,MAAM;EAAE,YAAY,QAAQ,MAAM;;AAEtE,+DAA+D,GAC/D,YAAY,MAAM,WAAW;AAE7B;OACO,GACP,YAAY;EACV,oEAAoE,GACpE,KAAK;EACL,6DAA6D,GAC7D,OAAO,MAAM;EACb;;oBAEkB,GAClB,QAAQ,MAAM;EACd,iEAAiE,GACjE,MAAM,MAAM;EACZ,+DAA+D,GAC/D,MAAM,MAAM;EACZ,2EAA2E,GAC3E,MAAM,MAAM;EACZ,mDAAmD,GACnD,UAAU,MAAM;EAChB,wDAAwD,GACxD,QAAQ;EACR,2DAA2D,GAC3D,iBAAiB,MAAM;IACrB;AAKJ;;;;;;CAMC,GACD,OAAO,YAAI,cAAc;AAEzB,oDAAoD,GACpD,OAAO,YAAI,cAAU;AAErB;4DAC4D,GAC5D,OAAO,YAAI,aAAc;AAEzB;+CAC+C,GAC/C,OAAO,YAAI,iBAAc;AAEzB;;aAEa,GACb,OAAO,cAAM;WAAoB,gBAAgB,MAAM;EAErD;AAgBF;;;;;;;;CAQC,GACD,OAAO,YAAI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|
|
5
|
+
export type Scored = {
|
|
6
|
+
owner: number;
|
|
7
|
+
similarity: number;
|
|
8
|
+
};
|
|
9
|
+
/** The nearest vectors, bounded by count and similarity; ties keep the lower owner. */ export declare let scored: (query: Float32Array, vectors: Iterable<Vector>, _dts_1: never) => Scored[];
|
|
10
|
+
//# sourceMappingURL=score.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"score.d.ts","sources":["../score.ts"],"names":[],"mappings":"AAGA,YAAY;EAAW,OAAO,MAAM;EAAE,QAAQ,eAAe;;AAC7D,YAAY;EAAW,OAAO,MAAM;EAAE,YAAY,MAAM;;AAmCxD,qFAAqF,GACrF,OAAO,YAAI,SACT,OAAO,cACP,SAAS,SAAS,SAClB,kBAKC,SAWF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Eid } from "@yaks/graph";
|
|
2
|
+
import { type BindOpts, type Screen, type Statements } from "@yaks/sql";
|
|
3
|
+
import type { Vocab } from "@yaks/vocab";
|
|
4
|
+
import type { Embedder } from "./embedder.js";
|
|
5
|
+
import { type Field } from "./fields.js";
|
|
6
|
+
/** One match by meaning. Its excerpt is unmarked: the words need not occur. */ export type Hit = {
|
|
7
|
+
entity: Eid;
|
|
8
|
+
similarity: number;
|
|
9
|
+
excerpt: string;
|
|
10
|
+
};
|
|
11
|
+
export type MeaningOpts = {
|
|
12
|
+
/** Vocabulary and visibility of the person asking, when the read is scoped. */ vocab?: Vocab;
|
|
13
|
+
visibility?: BindOpts['visibility'];
|
|
14
|
+
limit?: number;
|
|
15
|
+
floor?: number;
|
|
16
|
+
/** the entities a hit must be among, as a statement selecting their ids
|
|
17
|
+
* (@yaks/sql `Screen`): asked for once the words are embedded, so it is
|
|
18
|
+
* compiled against the rows the search then reads */ screen?: Screen;
|
|
19
|
+
};
|
|
20
|
+
/** A short piece of the text that made an entity's vector. */ export declare let excerpt: (text: string, size?: number) => string;
|
|
21
|
+
/** Search stored vectors with new words, then read only the hits' source text.
|
|
22
|
+
* `fields` must be the configured fields, resolved through any derived text
|
|
23
|
+
* expressions that fed the stored vectors. */ export declare let meaning: (db: Statements, fields: Field[], embedder: Embedder, words: string, opts?: MeaningOpts) => Promise<Hit[]>;
|
|
24
|
+
//# sourceMappingURL=search.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"search.d.ts","sources":["../search.ts"],"names":[],"mappings":"AAGA,cAAc,GAAG,2BAAgC;AACjD,SAEE,KAAK,QAAQ,EACb,KAAK,MAAM,EACX,KAAK,UAAU,yBACY;AAC7B,cAAc,KAAK,2BAAgC;AACnD,cAAc,QAAQ,wBAAuB;AAC7C,SAAS,KAAK,KAAK,sBAAwC;AAuB3D,6EAA6E,GAC7E,YAAY;EACV,QAAQ;EACR,YAAY,MAAM;EAClB,SAAS,MAAM;;AAGjB,YAAY;EACV,6EAA6E,GAC7E,QAAQ;EACR,aAAa,SAAS;EACtB,QAAQ,MAAM;EACd,QAAQ,MAAM;EACd;;qDAEmD,GACnD,SAAS;;AAGX,4DAA4D,GAC5D,OAAO,YAAI,UAAW,MAAM,MAAM,EAAE,kBAAa,MAAM,CAetD;AAED;;4CAE4C,GAC5C,OAAO,YAAI,UACT,IAAI,YACJ,QAAQ,SACR,UAAU,UACV,OAAO,MAAM,EACb,OAAM,gBACL,QAAQ,OAoCV"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Graph } from "@yaks/graph";
|
|
2
|
+
import type { Vocab } from "@yaks/vocab";
|
|
3
|
+
import { type Derived, type Statements } from "@yaks/sql";
|
|
4
|
+
import { type Lent, type Options } from "./options.js";
|
|
5
|
+
/** How long an empty queue waits before the loop looks again, by default. */ export declare let AFTER: number;
|
|
6
|
+
/** What the service reads: the vocabulary, the database, the graph the
|
|
7
|
+
* provider and model rows are read from, and the host's derived columns,
|
|
8
|
+
* through which a body @yaks/blob stores by address is read as its text. */ export type Host = {
|
|
9
|
+
vocab: Vocab;
|
|
10
|
+
storage: {
|
|
11
|
+
statements?: Statements;
|
|
12
|
+
};
|
|
13
|
+
graph: Pick<Graph, 'get'>;
|
|
14
|
+
derived?: Derived;
|
|
15
|
+
/** the embedders lent by name (@yaks/host `embedders`) */ embedders?: Lent;
|
|
16
|
+
/** where a failed pass is reported (@yaks/host `report`; default
|
|
17
|
+
* `console.error`) */ report?: (error: unknown, context: {
|
|
18
|
+
tags: Record<string, string>;
|
|
19
|
+
}) => void | Promise<void>;
|
|
20
|
+
};
|
|
21
|
+
/** Settle the queue for as long as `signal` lets it: one pass when it has
|
|
22
|
+
* already aborted, the way a one-shot command runs a service. */ export declare let service: (host: Host, options?: Options, signal?: AbortSignal) => Promise<void>;
|
|
23
|
+
//# sourceMappingURL=service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.d.ts","sources":["../service.ts"],"names":[],"mappings":"AAmCA,cAAc,KAAK,2BAAgC;AACnD,cAAc,KAAK,2BAAgC;AACnD,SAAS,KAAK,OAAO,EAAE,KAAK,UAAU,yBAA0C;AAEhF,SAAS,KAAK,IAAI,EAAE,KAAK,OAAO,uBAA6B;AAK7D,2EAA2E,GAC3E,OAAO,YAAI,cAAa;AAExB;;0EAE0E,GAC1E,YAAY;EACV,OAAO;EACP;IAAW,aAAa;;EACxB,OAAO,KAAK,OAAO;EACnB,UAAU;EACV,wDAAwD,GACxD,YAAY;EACZ;sBACoB,GACpB,UACE,OAAO,OAAO,EACd;IAAW,MAAM,OAAO,MAAM,EAAE,MAAM;QACnC,IAAI,GAAG,QAAQ,IAAI;;AAG1B;+DAC+D,GAC/D,OAAO,YAAI,UACT,MAAM,MACN,UAAS,SACT,SAAQ,gBACP,QAAQ,IAAI,EA6Dd"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Bundle } from "@yaks/graph";
|
|
2
|
+
import type { Statements } from "@yaks/sql";
|
|
3
|
+
import { type Clock } from "@yaks/fp";
|
|
4
|
+
import type { Embedder } from "./embedder.js";
|
|
5
|
+
import type { Field } from "./fields.js";
|
|
6
|
+
import { type Outage } from "./outage.js";
|
|
7
|
+
/** What a sleeping host lends its drains. */ export type Sleeping = {
|
|
8
|
+
/** its texts, or null while it can drain nothing: no schema standing yet,
|
|
9
|
+
* or asleep for good */ texts: () => {
|
|
10
|
+
statements: Statements;
|
|
11
|
+
fields: Field[];
|
|
12
|
+
} | null;
|
|
13
|
+
/** the embedder, or null where none is lent */ model: () => Embedder | null;
|
|
14
|
+
/** sets the host's alarm, for the rest of a drain or its retry */ arm: (at: number, why: string) => Promise<void>;
|
|
15
|
+
/** keeps the host up until the work is done (`waitUntil`) */ hold?: (work: Promise<unknown>) => void;
|
|
16
|
+
/** where the outage the drains are riding out is kept */ outage: {
|
|
17
|
+
get: () => Outage | null;
|
|
18
|
+
set: (o: Outage | null) => void;
|
|
19
|
+
};
|
|
20
|
+
/** where a failure goes, with what varies across one outage */ report: (error: unknown, context: Record<string, unknown>) => void;
|
|
21
|
+
/** a line for the log */ log?: (line: string, ...detail: unknown[]) => void;
|
|
22
|
+
clock?: Clock;
|
|
23
|
+
/** how long one drain may run, in ms */ deadline?: number;
|
|
24
|
+
/** how long a failed drain waits, in ms, given its error */ retry?: (error: unknown) => number;
|
|
25
|
+
/** when the alarm rings for the rest of a drain */ soon?: () => number;
|
|
26
|
+
};
|
|
27
|
+
/** A sleeping host's drains. */ export type Sleeper = {
|
|
28
|
+
/** what a committed write calls: a drain where it queued a text */ wrote: (bundles: Bundle[]) => Bundle[];
|
|
29
|
+
/** a drain, unless one runs or a failed one waits */ run: () => void;
|
|
30
|
+
/** whether a text is owed its vector: one queued row sought */ owes: () => boolean;
|
|
31
|
+
/** when a failed drain is retried, if one waits */ retry: () => number | null;
|
|
32
|
+
/** the drain in flight */ work: () => Promise<void> | null;
|
|
33
|
+
};
|
|
34
|
+
/** How long a drain runs before the alarm takes up the rest. */ export declare let DEADLINE: number;
|
|
35
|
+
/** How long a failed drain waits. */ export declare let RETRY: number;
|
|
36
|
+
/** The drains of a host that sleeps between requests. */ export declare let sleeper: (host: Sleeping) => Sleeper;
|
|
37
|
+
//# sourceMappingURL=sleeper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sleeper.d.ts","sources":["../sleeper.ts"],"names":[],"mappings":"AAkBA,cAAc,MAAM,2BAAgC;AACpD,cAAc,UAAU,yBAA8B;AACtD,SAAS,KAAK,KAAK,wBAAqC;AACxD,cAAc,QAAQ,wBAAuB;AAC7C,cAAc,KAAK,sBAAqB;AAGxC,SAA0B,KAAK,MAAM,sBAAyC;AAE9E,2CAA2C,GAC3C,YAAY;EACV;wBACsB,GACtB;IAAe,YAAY;IAAY,QAAQ;MAAY,IAAI;EAC/D,6CAA6C,GAC7C,aAAa,WAAW,IAAI;EAC5B,gEAAgE,GAChE,MAAM,IAAI,MAAM,EAAE,KAAK,MAAM,KAAK,QAAQ,IAAI;EAC9C,2DAA2D,GAC3D,QAAQ,MAAM,QAAQ,OAAO,MAAM,IAAI;EACvC,uDAAuD,GACvD;IAAU,WAAW,SAAS,IAAI;IAAE,MAAM,GAAG,SAAS,IAAI,KAAK,IAAI;;EACnE,6DAA6D,GAC7D,SAAS,OAAO,OAAO,EAAE,SAAS,OAAO,MAAM,EAAE,OAAO,MAAM,IAAI;EAClE,uBAAuB,GACvB,OAAO,MAAM,MAAM,EAAE,GAAG,QAAQ,OAAO,OAAO,IAAI;EAClD,QAAQ;EACR,sCAAsC,GACtC,WAAW,MAAM;EACjB,0DAA0D,GAC1D,SAAS,OAAO,OAAO,KAAK,MAAM;EAClC,iDAAiD,GACjD,aAAa,MAAM;;AAGrB,8BAA8B,GAC9B,YAAY;EACV,iEAAiE,GACjE,QAAQ,SAAS,aAAa;EAC9B,mDAAmD,GACnD,WAAW,IAAI;EACf,6DAA6D,GAC7D,YAAY,OAAO;EACnB,iDAAiD,GACjD,aAAa,MAAM,GAAG,IAAI;EAC1B,wBAAwB,GACxB,YAAY,QAAQ,IAAI,IAAI,IAAI;;AAGlC,8DAA8D,GAC9D,OAAO,YAAI,iBAAiB;AAE5B,mCAAmC,GACnC,OAAO,YAAI,cAAc;AAEzB,uDAAuD,GACvD,OAAO,YAAI,UAAW,MAAM,aAAW,QA2FtC"}
|
package/_dist/sweep.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { Eid } from "@yaks/graph";
|
|
2
|
+
import { type Clock } from "@yaks/fp";
|
|
3
|
+
import { type Statements } from "@yaks/sql";
|
|
4
|
+
import { type Embedder, Refused } from "./embedder.js";
|
|
5
|
+
import { type Field } from "./fields.js";
|
|
6
|
+
import { type Due } from "./owed.js";
|
|
7
|
+
/** How many queued entities one sweep takes by default. */ export declare let BATCH: number;
|
|
8
|
+
/**
|
|
9
|
+
* One entity's embeddable text: its integer owner id, its eid, the text of all
|
|
10
|
+
* its embedded fields joined together, and the hash of the vector already
|
|
11
|
+
* stored for it (null when it has none).
|
|
12
|
+
*/ export type Source = {
|
|
13
|
+
owner: number;
|
|
14
|
+
entity: Eid;
|
|
15
|
+
text: string;
|
|
16
|
+
had: string | null;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Every entity that still exists and has text to embed — or those of `owners`
|
|
20
|
+
* that do — its text assembled and its stored hash beside it. Deleted entities
|
|
21
|
+
* are left out, so an entity stops being a neighbour as soon as it is deleted.
|
|
22
|
+
*/ export declare let sources: (db: Statements, wanted: Field[], owners?: number[]) => Source[];
|
|
23
|
+
/**
|
|
24
|
+
* Store a source's vector, replacing whatever its owner had, and say whether
|
|
25
|
+
* it did. The vector was made from text read before the model was asked, and
|
|
26
|
+
* anything may run while it answers: the identity may be gone by then (the
|
|
27
|
+
* write that made it rolled back, the store was emptied) or its integer id
|
|
28
|
+
* may name another entity (an emptied store planted again). So the row is
|
|
29
|
+
* written only while the owner's id still names the source's entity.
|
|
30
|
+
*/ export declare let put: (db: Statements, source: Pick<Source, 'owner' | 'entity' | 'text'>, model: string, vec: Float32Array) => boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Owe every text its vector again when a stored one was made by another model,
|
|
33
|
+
* and say whether it did: what a host asks where its model may have changed (a
|
|
34
|
+
* deploy), since a sweep looks only once nothing else is owed.
|
|
35
|
+
*/ export declare let remodel: (db: Statements, fields: Field[], model: string) => boolean | Promise<boolean>;
|
|
36
|
+
/** What one sweep did. */ export type Swept = {
|
|
37
|
+
/** how many entities were (re)embedded */ fresh: number;
|
|
38
|
+
/** how many are queued, plus one while reconciliation still has more */ left: number;
|
|
39
|
+
/** the texts the embedder refused: their entities have no vector */ refused: {
|
|
40
|
+
entity: Eid;
|
|
41
|
+
error: Refused;
|
|
42
|
+
}[];
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* One pass: make the queue's triggers match the fields (./owed.ts `watch`),
|
|
46
|
+
* then settle the newest `limit` queued entities — embed what changed, delete
|
|
47
|
+
* the vectors of what has no text, and leave the rest as they are. An
|
|
48
|
+
* embedder that cannot be reached stops the pass: what it took stays queued
|
|
49
|
+
* for the next one, and the error reaches the caller rather than being
|
|
50
|
+
* swallowed into a quietly half-embedded corpus.
|
|
51
|
+
*/ export declare let sweep: (db: Statements, fields: Field[], embedder: Embedder, queued?: Due[]) => Promise<Swept>;
|
|
52
|
+
/** What one {@link drain} did: how many vectors it made, and the texts the
|
|
53
|
+
* embedder refused. */ export type Drained = {
|
|
54
|
+
fresh: number;
|
|
55
|
+
refused: Swept['refused'];
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Sweep pass after pass until nothing is owed, folding each pass into the
|
|
59
|
+
* vectors this process holds (./held.ts `absorb`), then code every vector that
|
|
60
|
+
* has no codes (./coded.ts), and stop. A
|
|
61
|
+
* host with nothing standing between requests (a Durable Object sleeps) runs
|
|
62
|
+
* this when a write asks; the service (./service.ts) runs it, then waits. An
|
|
63
|
+
* aborted `signal` stops it after the pass it is in. An embedder that cannot
|
|
64
|
+
* be reached throws, and what it took stays owed.
|
|
65
|
+
*/ export declare let drain: (db: Statements, fields: Field[], embedder: Embedder, opts?: {
|
|
66
|
+
batch?: number;
|
|
67
|
+
signal?: AbortSignal;
|
|
68
|
+
/** The first batch already read by the host, with its queue generations. */ queued?: Due[];
|
|
69
|
+
/** The clock used to yield between bounded passes. */ clock?: Clock;
|
|
70
|
+
}) => Promise<Drained>;
|
|
71
|
+
//# sourceMappingURL=sweep.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sweep.d.ts","sources":["../sweep.ts"],"names":[],"mappings":"AAaA,cAAc,GAAG,2BAAgC;AACjD,SAAgB,KAAK,KAAK,wBAAqC;AAC/D,SAkBE,KAAK,UAAU,yBAIY;AAC7B,SAAS,KAAK,QAAQ,EAAQ,OAAO,wBAAuB;AAC5D,SAAkB,KAAK,KAAK,sBAAiD;AAE7E,SAAS,KAAK,GAAG,oBAAgD;AAOjE,yDAAyD,GACzD,OAAO,YAAI,cAAU;AAErB;;;;CAIC,GACD,YAAY;EACV,OAAO,MAAM;EACb,QAAQ;EACR,MAAM,MAAM;EACZ,KAAK,MAAM,GAAG,IAAI;;AAoBpB;;;;CAIC,GACD,OAAO,YAAI,UACT,IAAI,YACJ,QAAQ,SACR,SAAS,MAAM,OACd,SA2IF;AAED;;;;;;;CAOC,GACD,OAAO,YAAI,MACT,IAAI,YACJ,QAAQ,KAAK,QAAQ,UAAU,WAAW,SAC1C,OAAO,MAAM,EACb,KAAK,iBACJ,OAAO,CAiCT;AAwBD;;;;CAIC,GACD,OAAO,YAAI,UACT,IAAI,YACJ,QAAQ,SACR,OAAO,MAAM,KACZ,OAAO,GAAG,QAAQ,OAAO,EAG3B;AAmBD,wBAAwB,GACxB,YAAY;EACV,wCAAwC,GACxC,OAAO,MAAM;EACb,sEAAsE,GACtE,MAAM,MAAM;EACZ,kEAAkE,GAClE;IAAW,QAAQ;IAAK,OAAO;;;AAGjC;;;;;;;CAOC,GACD,OAAO,YAAI,QACT,IAAI,YACJ,QAAQ,SACR,UAAU,UAEV,SAAS,UACR,QAAQ,OAyCV;AAKD;qBACqB,GACrB,YAAY;EAAY,OAAO,MAAM;EAAE,SAAS,MAAM;;AAEtD;;;;;;;;CAQC,GACD,OAAO,YAAI,QACT,IAAI,YACJ,QAAQ,SACR,UAAU,UACV;EACE,QAAQ,MAAM;EACd,SAAS;EACT,0EAA0E,GAC1E,SAAS;EACT,oDAAoD,GACpD,QAAQ;MAET,QAAQ,SAmBV"}
|
package/_dist/tools.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Graph } from "@yaks/graph";
|
|
2
|
+
import type { Runs } from "@yaks/graph/tools";
|
|
3
|
+
import { type Statements } from "@yaks/sql";
|
|
4
|
+
import { type Lent, type Options } from "./options.js";
|
|
5
|
+
export type { Options };
|
|
6
|
+
/** The implementation behind the tool ./vocab.json declares, over this
|
|
7
|
+
* server's own database connection — which is why this export is a factory. */ export declare let runs: (host: {
|
|
8
|
+
storage: {
|
|
9
|
+
statements?: Statements;
|
|
10
|
+
};
|
|
11
|
+
graph: Pick<Graph, 'get'>;
|
|
12
|
+
embedders?: Lent;
|
|
13
|
+
}, options?: Options) => Runs;
|
|
14
|
+
//# sourceMappingURL=tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sources":["../tools.ts"],"names":[],"mappings":"AACA,cAAc,KAAK,2BAAgC;AACnD,cAAc,IAAI,iCAAsC;AAExD,SAKE,KAAK,UAAU,yBAGY;AAG7B,SAAqB,KAAK,IAAI,EAAE,KAAK,OAAO,uBAAsB;AAElE,cAAc,OAAO,GAAE;AASvB;6EAC6E,GAC7E,OAAO,YAAI,OACT;EACE;IAAW,aAAa;;EACxB,OAAO,KAAK,OAAO;EACnB,YAAY;GAEd,UAAS,YACR,KAoDF"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** A vector's raw bytes, for storing it as a blob. */ export declare let pack: (v: Float32Array) => Uint8Array;
|
|
2
|
+
/**
|
|
3
|
+
* A vector read back from its blob. The copy is deliberate: a `Float32Array`
|
|
4
|
+
* needs a 4-byte-aligned buffer, and bytes handed back by a driver are aligned
|
|
5
|
+
* only by luck.
|
|
6
|
+
*/ export declare let unpack: (bytes: Uint8Array) => Float32Array;
|
|
7
|
+
/**
|
|
8
|
+
* A vector read back from its blob without a copy where it can be: a view of
|
|
9
|
+
* the bytes where they are 4-byte aligned, {@link unpack}'s copy where not.
|
|
10
|
+
* The view shares the bytes, so it is for reading.
|
|
11
|
+
*/ export declare let floats: (bytes: Uint8Array) => Float32Array;
|
|
12
|
+
/**
|
|
13
|
+
* The same direction at length 1. A zero vector has no direction, so it comes
|
|
14
|
+
* back unchanged — {@link cosine} answers 0 against it, which is what "unrelated
|
|
15
|
+
* to everything" should read as.
|
|
16
|
+
*/ export declare let unit: (v: Float32Array) => Float32Array;
|
|
17
|
+
/**
|
|
18
|
+
* Cosine similarity, 1 for the same direction and 0 for unrelated. Two vectors
|
|
19
|
+
* of different lengths are from different spaces and score 0 — a model change
|
|
20
|
+
* makes every old vector unrelated rather than subtly wrong.
|
|
21
|
+
*/ export declare let cosine: (a: Float32Array, b: Float32Array | Int8Array) => number;
|
|
22
|
+
/**
|
|
23
|
+
* Code `v` into `codes` from `at`: each coordinate over the largest, rounded
|
|
24
|
+
* to one of 255 steps from -127 to 127. Returns the scale that turns the
|
|
25
|
+
* codes' dot product with a query back into cosine times the query's length;
|
|
26
|
+
* 0 for a vector with no direction. The codes alone keep the direction, so
|
|
27
|
+
* their cosine is the vector's, give or take a step.
|
|
28
|
+
*/ export declare let code: (v: Float32Array, codes: Int8Array, at?: number) => number;
|
|
29
|
+
/** A vector's int8 codes as a blob, the shape a native int8 scan reads. */ export declare let coded: (v: Float32Array) => Uint8Array;
|
|
30
|
+
//# sourceMappingURL=vector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vector.d.ts","sources":["../vector.ts"],"names":[],"mappings":"AAYA,oDAAoD,GACpD,OAAO,YAAI,OAAQ,GAAG,iBAAe,WACsC;AAE3E;;;;CAIC,GACD,OAAO,YAAI,SAAU,OAAO,eAAa,aACD;AAExC;;;;CAIC,GACD,OAAO,YAAI,SAAU,OAAO,eAAa,aAGtB;AAEnB;;;;CAIC,GACD,OAAO,YAAI,OAAQ,GAAG,iBAAe,aAQpC;AAED;;;;CAIC,GACD,OAAO,YAAI,SAAU,GAAG,cAAc,GAAG,eAAe,cAAY,MAAM,CAUzE;AAED;;;;;;CAMC,GACD,OAAO,YAAI,OAAQ,GAAG,cAAc,OAAO,WAAW,gBAAS,MAAM,CAepE;AAED,yEAAyE,GACzE,OAAO,YAAI,QAAS,GAAG,iBAAe,WAIrC"}
|
package/_dist/vocab.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { VocabDoc } from "@yaks/vocab";
|
|
2
|
+
/** The tool declaration this plugin contributes. */ export declare let embeddingDoc: VocabDoc;
|
|
3
|
+
/** What this package is: its manifest's description, the one place it is
|
|
4
|
+
* written, which a host stamps on each document here. */ export declare let description: string;
|
|
5
|
+
/** Every document this plugin declares. */ export declare let docs: VocabDoc[];
|
|
6
|
+
//# sourceMappingURL=vocab.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vocab.d.ts","sources":["../vocab.ts"],"names":[],"mappings":"AASA,cAAc,QAAQ,2BAAgC;AAItD,kDAAkD,GAClD,OAAO,YAAI,cAAc,SAAc;AAEvC;uDACuD,GACvD,OAAO,YAAI,aAAa,MAAM,CAAuB;AAErD,yCAAyC,GACzC,OAAO,YAAI,MAAM,WAA2B"}
|
package/coded.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Each vector's int8 codes, kept beside it on shared storage, so a native scan
|
|
2
|
+
// reads 384 bytes a vector where its floats take 1,536 and a page holds two.
|
|
3
|
+
// A search that has to read the corpus from disk, the box's page cache having
|
|
4
|
+
// let it go, reads a fifth as much (./native.ts), and the nearest few are
|
|
5
|
+
// still scored from their floats (./near.ts).
|
|
6
|
+
//
|
|
7
|
+
// Triggers delete a vector's codes when the vector changes or goes, and keep
|
|
8
|
+
// the owners of vectors that have none (./ddl.ts), whoever wrote either table.
|
|
9
|
+
// So a scan trusts the codes once that set is empty, which is one row to ask
|
|
10
|
+
// however many vectors there are. {@link put} codes what the sweep stores;
|
|
11
|
+
// {@link heal} codes what anyone else stored (a migration, an older process,
|
|
12
|
+
// every vector stored before the table was made).
|
|
13
|
+
import { col, eq, lit, select, table, val } from "@yaks/sql";
|
|
14
|
+
import { CODE, TABLE, UNCODED } from "./ddl.js";
|
|
15
|
+
import { coded, floats } from "./vector.js";
|
|
16
|
+
// Whether this database keeps codes, asked again once its schema changes. The
|
|
17
|
+
// uncoded set is made after the codes, so it standing means both do.
|
|
18
|
+
let made = new WeakMap();
|
|
19
|
+
export let has = (db)=>{
|
|
20
|
+
let version = db.revision('schema');
|
|
21
|
+
let kept = made.get(db);
|
|
22
|
+
if (kept?.version == version) return kept.has;
|
|
23
|
+
let found = !!db.query(select({
|
|
24
|
+
cols: [
|
|
25
|
+
col('name')
|
|
26
|
+
],
|
|
27
|
+
from: table('sqlite_master'),
|
|
28
|
+
where: eq(col('name'), val(UNCODED))
|
|
29
|
+
}))[0];
|
|
30
|
+
made.set(db, {
|
|
31
|
+
version,
|
|
32
|
+
has: found
|
|
33
|
+
});
|
|
34
|
+
return found;
|
|
35
|
+
};
|
|
36
|
+
/** Whether every stored vector has its codes: whether any owner is noted
|
|
37
|
+
* uncoded, read off one row. */ export let complete = (db)=>has(db) && !db.query(select({
|
|
38
|
+
cols: [
|
|
39
|
+
lit(1)
|
|
40
|
+
],
|
|
41
|
+
from: table(UNCODED),
|
|
42
|
+
limit: lit(1)
|
|
43
|
+
})).length;
|
|
44
|
+
/** Store the codes of the vector an owner was just given. */ export let put = (db, owner, vec)=>{
|
|
45
|
+
if (!has(db)) return;
|
|
46
|
+
db.query({
|
|
47
|
+
t: 'insert',
|
|
48
|
+
or: 'replace',
|
|
49
|
+
into: CODE,
|
|
50
|
+
cols: [
|
|
51
|
+
'owner',
|
|
52
|
+
'code'
|
|
53
|
+
],
|
|
54
|
+
rows: [
|
|
55
|
+
[
|
|
56
|
+
val(owner),
|
|
57
|
+
val(coded(vec))
|
|
58
|
+
]
|
|
59
|
+
]
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
/** Code up to `limit` stored vectors that have no codes, in owner order, and
|
|
63
|
+
* say how many owners it settled. An owner noted with no vector standing is
|
|
64
|
+
* forgotten. Nothing to code costs one row's read. */ export let heal = (db, limit = 2048)=>{
|
|
65
|
+
if (!has(db)) return 0;
|
|
66
|
+
return db.atomic(()=>{
|
|
67
|
+
let rows = db.query(select({
|
|
68
|
+
cols: [
|
|
69
|
+
col('owner', 'u'),
|
|
70
|
+
col('vec', 'e')
|
|
71
|
+
],
|
|
72
|
+
from: table(UNCODED, 'u'),
|
|
73
|
+
joins: [
|
|
74
|
+
{
|
|
75
|
+
how: 'left',
|
|
76
|
+
src: table(TABLE, 'e'),
|
|
77
|
+
on: eq(col('owner', 'e'), col('owner', 'u'))
|
|
78
|
+
}
|
|
79
|
+
],
|
|
80
|
+
order: [
|
|
81
|
+
col('owner', 'u')
|
|
82
|
+
],
|
|
83
|
+
limit: val(limit)
|
|
84
|
+
}));
|
|
85
|
+
for (let r of rows){
|
|
86
|
+
let owner = Number(r.owner);
|
|
87
|
+
if (r.vec == null) {
|
|
88
|
+
db.query({
|
|
89
|
+
t: 'delete',
|
|
90
|
+
from: UNCODED,
|
|
91
|
+
where: eq(col('owner'), val(owner))
|
|
92
|
+
});
|
|
93
|
+
} else put(db, owner, floats(r.vec));
|
|
94
|
+
}
|
|
95
|
+
return rows.length;
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
//# sourceMappingURL=coded.js.map
|
package/coded.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coded.js","sources":["./coded.ts"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