@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/options.ts
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// What a config declares to this plugin, and the embedder it names. "The
|
|
2
|
+
// server" below means whichever process opened the graph and loaded this
|
|
3
|
+
// package.
|
|
4
|
+
//
|
|
5
|
+
// The vectors are this package's business; the model is not. So the config
|
|
6
|
+
// names one beside the plugin, the way a session names what serves it: a
|
|
7
|
+
// provider and a model, two choices. `./graph` needs the vector space the
|
|
8
|
+
// model names (a stored vector is only comparable with others in the same
|
|
9
|
+
// space), `./service` needs the embedding function itself:
|
|
10
|
+
//
|
|
11
|
+
// ```json
|
|
12
|
+
// { "use": "@yaks/embedding",
|
|
13
|
+
// "with": { "embedder": { "provider": "ollama",
|
|
14
|
+
// "model": "granite-embedding-30m-english" },
|
|
15
|
+
// "text": ["doc.title", "doc.body"] } }
|
|
16
|
+
// ```
|
|
17
|
+
//
|
|
18
|
+
// A provider reached over HTTP is a row in the graph, as it is for a chat
|
|
19
|
+
// (@yaks/model): `provider{name, base, api}` says where it answers and which
|
|
20
|
+
// API it speaks, and its `serves` edge to the `model` row says what it calls
|
|
21
|
+
// that model. Nothing here knows Ollama beyond the shape of its API
|
|
22
|
+
// (./remote.ts). The space is the model's own name, whoever serves it, so the
|
|
23
|
+
// same model moved to another provider keeps its vectors. A provider that runs
|
|
24
|
+
// in this process needs no row: `hash`, the offline embedder shipped here
|
|
25
|
+
// (instant, deterministic, and no model at all, which is what a development
|
|
26
|
+
// machine and a test want), or one a plugin beside this one lends its host by
|
|
27
|
+
// name (@yaks/model2vec lends `model2vec`), which this package knows nothing
|
|
28
|
+
// about beyond {@link Lent}.
|
|
29
|
+
//
|
|
30
|
+
// Missing config never prevents startup. A server that composes this plugin
|
|
31
|
+
// and has not been given a key, a provider row or a model yet is a server
|
|
32
|
+
// waiting for one: it starts, it stores no vectors, its check reports what it
|
|
33
|
+
// is waiting for, and the first pass after it arrives is the one that embeds.
|
|
34
|
+
// So nothing here throws: a config and the rows it names amount to a
|
|
35
|
+
// {@link Ready} value, read on every pass rather than once when the plugin is
|
|
36
|
+
// composed, which is what lets a key or a row written into the graph start the
|
|
37
|
+
// sweep without a restart.
|
|
38
|
+
|
|
39
|
+
import type { Bundle, Comp } from 'jsr:@yaks/graph@0.2.36'
|
|
40
|
+
import { dead } from '@yaks/graph/bundle'
|
|
41
|
+
import { identityEid } from '@yaks/graph/identity'
|
|
42
|
+
import { edgeEid } from '@yaks/edge'
|
|
43
|
+
import { MODEL, PROVIDER } from '@yaks/model'
|
|
44
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
45
|
+
import type { Embedder } from './embedder.js'
|
|
46
|
+
import { hashEmbedder } from './embedder.js'
|
|
47
|
+
import { type Field, fields, searched, unembedded } from './fields.js'
|
|
48
|
+
import { type Api, type Fetch, remote } from './remote.js'
|
|
49
|
+
|
|
50
|
+
/** An embedder, as a config names one: a provider and the model it serves. */
|
|
51
|
+
export type Named = {
|
|
52
|
+
/** `hash` or an embedder lent by name in this process, or the name of a
|
|
53
|
+
* `provider` row reached over HTTP */
|
|
54
|
+
provider: string
|
|
55
|
+
/** the `model` row's name, which names the vector space; for a lent
|
|
56
|
+
* embedder, the model it loads; none for `hash` */
|
|
57
|
+
model?: string
|
|
58
|
+
/** keep this many leading coordinates (Matryoshka); a second width of one
|
|
59
|
+
* model is a second space. For `hash`, how many buckets (default 64). */
|
|
60
|
+
dim?: number
|
|
61
|
+
/** a bearer token for a provider that wants one */
|
|
62
|
+
key?: string
|
|
63
|
+
/** the most characters sent for one vector (./remote.ts `CHARS`) */
|
|
64
|
+
chars?: number
|
|
65
|
+
/** how long to wait for one request, in milliseconds */
|
|
66
|
+
timeout?: number
|
|
67
|
+
/** the most inputs one request carries (./remote.ts `COUNT`) */
|
|
68
|
+
count?: number
|
|
69
|
+
/** the most characters one request carries (./remote.ts `LOAD`) */
|
|
70
|
+
load?: number
|
|
71
|
+
/** the fetch to call through: a test's */
|
|
72
|
+
fetch?: Fetch
|
|
73
|
+
/** the pauses before resending a failed call, in milliseconds */
|
|
74
|
+
waits?: readonly number[]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** What a config declares to `@yaks/embedding`. */
|
|
78
|
+
export type Options = {
|
|
79
|
+
/** which embedder the vectors are made with — required, and its model
|
|
80
|
+
* names the space every stored row is stamped with */
|
|
81
|
+
embedder?: Named
|
|
82
|
+
/** which text feeds a vector, as `comp.prop` pairs. The default is every
|
|
83
|
+
* property the vocabulary marks `search: true` (./fields.ts `searched`). */
|
|
84
|
+
text?: string[]
|
|
85
|
+
/** how many neighbours a `.near` selects (default 8) */
|
|
86
|
+
neighbours?: number
|
|
87
|
+
/** the similarity a neighbour must reach to be one at all (default 0) */
|
|
88
|
+
floor?: number
|
|
89
|
+
/** how many queued entities one sweep takes (default 64, ./sweep.ts
|
|
90
|
+
* `BATCH`); the next sweep follows at once while any are left */
|
|
91
|
+
batch?: number
|
|
92
|
+
/** how long an empty queue waits before the service looks again, in
|
|
93
|
+
* milliseconds (default 3000) — how soon a new text is embedded */
|
|
94
|
+
after?: number
|
|
95
|
+
/** the clock the service times a model's outage by (default `Date.now`):
|
|
96
|
+
* a test's */
|
|
97
|
+
now?: () => number
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Where the rows a config names are read: the graph's `get`, which leaves
|
|
101
|
+
* out an entity that does not exist. */
|
|
102
|
+
export type Rows = {
|
|
103
|
+
get: (eids: string[]) => Bundle[] | Promise<Bundle[]>
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** What the config amounts to: the embedder it names, or the single message
|
|
107
|
+
* explaining why there is none yet. */
|
|
108
|
+
export type Ready = {
|
|
109
|
+
/** the vector space — the model name stored on every row. It comes from the
|
|
110
|
+
* config alone, so a query can rank `.near` over what is already stored
|
|
111
|
+
* while the sweep is still waiting for a key. */
|
|
112
|
+
model?: string
|
|
113
|
+
/** the embedder itself: absent while the config is incomplete */
|
|
114
|
+
embedder?: Embedder
|
|
115
|
+
/** why there is none, for whoever asks — absent when there is one */
|
|
116
|
+
waiting?: string
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The embedders a host lends by name, each made from the `model` and `dim` a
|
|
120
|
+
* config names; the embedder's `model` names its space. */
|
|
121
|
+
export type Lent = Readonly<
|
|
122
|
+
Record<string, (said: { model: string; dim?: number }) => Embedder>
|
|
123
|
+
>
|
|
124
|
+
|
|
125
|
+
// The provider this package runs in its own process.
|
|
126
|
+
let HASH = 'hash'
|
|
127
|
+
|
|
128
|
+
// The APIs ./remote.ts speaks.
|
|
129
|
+
let APIS: Api[] = ['ollama', 'openai']
|
|
130
|
+
|
|
131
|
+
// The lent embedder a config names, made from its model and width.
|
|
132
|
+
let lentOf = (said: Named, lent?: Lent): Embedder | undefined =>
|
|
133
|
+
said.model && Object.hasOwn(lent ?? {}, said.provider)
|
|
134
|
+
? lent
|
|
135
|
+
: undefined
|
|
136
|
+
|
|
137
|
+
/** The space a config's vectors live in, from the config alone: the model,
|
|
138
|
+
* and its width when it is cut. `undefined` where the config names none. */
|
|
139
|
+
export let spaceOf = (said?: Named, lent?: Lent): string | undefined =>
|
|
140
|
+
!said?.provider
|
|
141
|
+
? undefined
|
|
142
|
+
: said.provider == HASH
|
|
143
|
+
? hashEmbedder(said.dim).model
|
|
144
|
+
: !said.model
|
|
145
|
+
? undefined
|
|
146
|
+
: lentOf(said, lent)?.model ??
|
|
147
|
+
(said.dim ? `${said.model}#${said.dim}` : said.model)
|
|
148
|
+
|
|
149
|
+
let live = (b: Bundle | undefined, comp: string): Comp | undefined =>
|
|
150
|
+
b && !dead(b) ? b[comp] as Comp | undefined : undefined
|
|
151
|
+
|
|
152
|
+
// A provider row, the model row and the edge between them, read as an
|
|
153
|
+
// embedder, or what is missing.
|
|
154
|
+
let served = async (said: Named, rows: Rows): Promise<Embedder | string> => {
|
|
155
|
+
let p = identityEid(PROVIDER, [said.provider])
|
|
156
|
+
let m = identityEid(MODEL, [said.model!])
|
|
157
|
+
let e = edgeEid(p, 'serves', m)
|
|
158
|
+
let got = new Map((await rows.get([p, m, e])).map((b) => [b.entity.eid, b]))
|
|
159
|
+
let provider = live(got.get(p), PROVIDER)
|
|
160
|
+
if (!provider) return `no provider called ${JSON.stringify(said.provider)}`
|
|
161
|
+
let api = provider.api as Api | undefined
|
|
162
|
+
if (!api || !APIS.includes(api)) {
|
|
163
|
+
return `${said.provider} names no API this package speaks (` +
|
|
164
|
+
`${APIS.join(', ')}) in \`provider.api\``
|
|
165
|
+
}
|
|
166
|
+
if (typeof provider.base != 'string' || !provider.base) {
|
|
167
|
+
return `${said.provider} says nowhere to reach it in \`provider.base\``
|
|
168
|
+
}
|
|
169
|
+
let serves = live(got.get(e), 'serves')
|
|
170
|
+
if (!live(got.get(m), MODEL) || !serves) {
|
|
171
|
+
return `${said.provider} serves no model called ${
|
|
172
|
+
JSON.stringify(said.model)
|
|
173
|
+
}`
|
|
174
|
+
}
|
|
175
|
+
return remote({
|
|
176
|
+
...said,
|
|
177
|
+
api,
|
|
178
|
+
base: provider.base,
|
|
179
|
+
model: typeof serves.name == 'string' ? serves.name : said.model!,
|
|
180
|
+
space: spaceOf(said),
|
|
181
|
+
})
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** The embedder a config and the rows it names amount to, or what it is
|
|
185
|
+
* waiting for. Missing config means waiting: the server starts with no
|
|
186
|
+
* vectors and begins embedding the moment what is missing appears. */
|
|
187
|
+
export let embedderOf = async (
|
|
188
|
+
options: Options,
|
|
189
|
+
rows: Rows,
|
|
190
|
+
lent?: Lent,
|
|
191
|
+
): Promise<Ready> => {
|
|
192
|
+
let said = options.embedder
|
|
193
|
+
let model = spaceOf(said, lent)
|
|
194
|
+
if (!said?.provider) {
|
|
195
|
+
return {
|
|
196
|
+
waiting:
|
|
197
|
+
'no `embedder` is named — `{"provider": "hash"}` is the offline one, and a provider and a model are named beside the plugin',
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (!model) {
|
|
201
|
+
return { waiting: `${said.provider} is named with no \`model\`` }
|
|
202
|
+
}
|
|
203
|
+
if (said.provider == HASH) return { model, embedder: hashEmbedder(said.dim) }
|
|
204
|
+
let own = lentOf(said, lent)
|
|
205
|
+
if (own) return { model, embedder: own }
|
|
206
|
+
// A config that names a key but has no value for it is waiting: the
|
|
207
|
+
// environment does not have one yet, and every request until it does would
|
|
208
|
+
// be a 401 paid for once per entity. A config that names no key never
|
|
209
|
+
// wanted one — a machine on your own network — and goes straight through.
|
|
210
|
+
if ('key' in said && said.key == null) {
|
|
211
|
+
return {
|
|
212
|
+
model,
|
|
213
|
+
waiting:
|
|
214
|
+
`waiting for a key: ${said.provider} is named with one the environment has not got`,
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
let made = await served(said, rows)
|
|
218
|
+
return typeof made == 'string'
|
|
219
|
+
? { model, waiting: made }
|
|
220
|
+
: { model, embedder: made }
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** The fields a config chose, or every searched one. A name the vocabulary
|
|
224
|
+
* does not declare is an error rather than a field that silently embeds
|
|
225
|
+
* nothing. */
|
|
226
|
+
export let chosen = (vocab: Vocab, options: Options): Field[] => {
|
|
227
|
+
if (!options.text) return fields(vocab, searched)
|
|
228
|
+
return options.text.map((said) => {
|
|
229
|
+
let [comp, prop, ...rest] = said.split('.')
|
|
230
|
+
if (!prop || rest.length || !vocab.prop(comp, prop)) {
|
|
231
|
+
throw new Error(
|
|
232
|
+
`@yaks/embedding: \`text\` names ${
|
|
233
|
+
JSON.stringify(said)
|
|
234
|
+
}, which is not a declared comp.prop`,
|
|
235
|
+
)
|
|
236
|
+
}
|
|
237
|
+
return { comp, prop, ...unembedded(vocab) }
|
|
238
|
+
})
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Everything a pass needs, as the config and the rows stand at this moment:
|
|
243
|
+
* the text a vector is made from, the embedder that makes it, and the message
|
|
244
|
+
* to report when there is none.
|
|
245
|
+
*
|
|
246
|
+
* Read it on every pass rather than once when the plugin is composed. That is
|
|
247
|
+
* what makes a key or a provider arriving late a server that starts embedding
|
|
248
|
+
* instead of one that has to be restarted: @yaks/cli resolves `{"secret": …}`
|
|
249
|
+
* each time it is asked for, so a key written into the graph is seen on the
|
|
250
|
+
* next pass, and the rows are read again with it.
|
|
251
|
+
*
|
|
252
|
+
* A `text` name the vocabulary does not declare is the one thing here that
|
|
253
|
+
* cannot wait: it is reported, and this plugin does nothing, which degrades the
|
|
254
|
+
* same way a key that has not arrived does.
|
|
255
|
+
*/
|
|
256
|
+
export let ready = async (
|
|
257
|
+
vocab: Vocab,
|
|
258
|
+
options: Options,
|
|
259
|
+
rows: Rows,
|
|
260
|
+
lent?: Lent,
|
|
261
|
+
): Promise<Ready & { text: Field[] }> => {
|
|
262
|
+
let text: Field[]
|
|
263
|
+
try {
|
|
264
|
+
text = chosen(vocab, options)
|
|
265
|
+
} catch (error) {
|
|
266
|
+
return { text: [], waiting: (error as Error).message }
|
|
267
|
+
}
|
|
268
|
+
return { text, ...await embedderOf(options, rows, lent) }
|
|
269
|
+
}
|
package/outage.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// When a model that does not answer is a failure.
|
|
2
|
+
//
|
|
3
|
+
// A provider out past its retries (./remote.ts `retrying`) throws
|
|
4
|
+
// `Unanswered`, and the texts it was asked for stay owed: the next drain
|
|
5
|
+
// embeds them once the model answers, so a host's drains ride an outage out
|
|
6
|
+
// on their own. What a person loses meanwhile is search by meaning: an owed
|
|
7
|
+
// text is found by its words, but not by `.near` or a recall. So an outage is
|
|
8
|
+
// a line in the log when it starts and when it ends, and a failure once the
|
|
9
|
+
// texts it holds up have waited {@link OVERDUE}: reported once, the outage as
|
|
10
|
+
// its cause. Anything else a drain throws is unexpected and reports at once,
|
|
11
|
+
// and once while drains keep failing by it: a box drains every few seconds,
|
|
12
|
+
// and one bug is one report. The drain that next answers logs that it did.
|
|
13
|
+
// A text the model refuses is the text's, not the drain's (./sweep.ts
|
|
14
|
+
// `refused`). A model `Withheld` (a host's spent budget) is no failure at
|
|
15
|
+
// all: its texts wait for the ceiling to lift, logged once, never reported.
|
|
16
|
+
//
|
|
17
|
+
// It is @yaks/mail's delayed letter (pull.ts) for vectors: what fails is the
|
|
18
|
+
// wait, not each try. The outage is @yaks/effects/outage's record, as it is for
|
|
19
|
+
// the mail pull, the tracker's passes and heal: a value the host keeps between
|
|
20
|
+
// drains, which a Durable Object keeps in its own storage, since it sleeps
|
|
21
|
+
// between the alarms that retry.
|
|
22
|
+
import { answered, failing } from "@yaks/effects/outage";
|
|
23
|
+
import { Unanswered, Withheld } from "./embedder.js";
|
|
24
|
+
/**
|
|
25
|
+
* How long owed texts may wait on a model that does not answer before the
|
|
26
|
+
* wait is a failure. A drain retries about once a minute, and the Workers AI
|
|
27
|
+
* outages the stores have met passed within a few minutes; a quarter of an
|
|
28
|
+
* hour is several times that, and as long as a person's searches by meaning
|
|
29
|
+
* should go without a new text before someone hears of it.
|
|
30
|
+
*/ export let OVERDUE = 15 * 60_000;
|
|
31
|
+
/** What a report of owed texts waiting past {@link OVERDUE} throws: a fixed
|
|
32
|
+
* message, since one outage is one failure however long it lasts or whatever
|
|
33
|
+
* the model said; what varies rides `context`, and the model's last failure
|
|
34
|
+
* is the `cause`. */ export class Overdue extends Error {
|
|
35
|
+
name = 'Overdue';
|
|
36
|
+
context;
|
|
37
|
+
constructor(waited, cause){
|
|
38
|
+
super('@yaks/embedding: owed texts waited too long for their vectors', {
|
|
39
|
+
cause
|
|
40
|
+
});
|
|
41
|
+
this.context = {
|
|
42
|
+
...cause.context,
|
|
43
|
+
waited: `${waited} ms`
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A drain that answered: an outage it rode out, or the failure it reported,
|
|
49
|
+
* is over.
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { equal } from '@yaks/testing'
|
|
53
|
+
* equal(drained(null, 0), { outage: null })
|
|
54
|
+
* equal(drained({ since: 0 }, 90_000),
|
|
55
|
+
* { outage: null, log: 'the outage ended after 90000 ms' })
|
|
56
|
+
* equal(drained({ since: 0, cause: 'Error: no table' }, 9_000), {
|
|
57
|
+
* outage: null,
|
|
58
|
+
* log: 'drains answer again after 9000 ms of Error: no table',
|
|
59
|
+
* })
|
|
60
|
+
* ```
|
|
61
|
+
*/ export let drained = (outage, now)=>{
|
|
62
|
+
let ms = answered(outage, now);
|
|
63
|
+
if (ms == null) return {
|
|
64
|
+
outage: null
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
outage: null,
|
|
68
|
+
log: outage?.cause ? `drains answer again after ${ms} ms of ${outage.cause}` : `the outage ended after ${ms} ms`
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* A drain that threw `error`. A model that did not answer starts an outage or
|
|
73
|
+
* rides on in one, until its texts have waited {@link OVERDUE}, which reports
|
|
74
|
+
* once; a model that withheld its answer waits without a report; anything
|
|
75
|
+
* else reports at once, and not again while drains keep failing by it.
|
|
76
|
+
*
|
|
77
|
+
* ```ts
|
|
78
|
+
* import { equal } from '@yaks/testing'
|
|
79
|
+
* import { Unanswered } from './embedder.ts'
|
|
80
|
+
* let down = new Unanswered('workers-ai',
|
|
81
|
+
* { model: '@cf/m', texts: '2', tries: '4' }, Error('4009'))
|
|
82
|
+
* equal(failed(down)(null, 0).outage, { since: 0 })
|
|
83
|
+
* equal(failed(down)({ since: 0 }, 60_000), { outage: { since: 0 } })
|
|
84
|
+
* let bug = failed(Error('no table'))
|
|
85
|
+
* equal(bug(null, 0).outage, { since: 0, cause: 'Error: no table' })
|
|
86
|
+
* equal(bug(bug(null, 0).outage, 3_000).report, undefined)
|
|
87
|
+
* ```
|
|
88
|
+
*/ export let failed = (error)=>(met, now)=>{
|
|
89
|
+
if (error instanceof Withheld) {
|
|
90
|
+
let { outage, fresh } = failing(met, now);
|
|
91
|
+
return {
|
|
92
|
+
outage: outage.told ? outage : {
|
|
93
|
+
...outage,
|
|
94
|
+
told: true
|
|
95
|
+
},
|
|
96
|
+
...fresh ? {
|
|
97
|
+
log: `${error.message}; its texts wait until it lifts`
|
|
98
|
+
} : {}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
if (!(error instanceof Unanswered)) {
|
|
102
|
+
let { outage, fresh } = failing(met, now, {
|
|
103
|
+
cause: String(error)
|
|
104
|
+
});
|
|
105
|
+
return fresh ? {
|
|
106
|
+
outage,
|
|
107
|
+
report: error
|
|
108
|
+
} : {
|
|
109
|
+
outage
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
let { outage, fresh, stayed } = failing(met, now, {
|
|
113
|
+
limit: OVERDUE
|
|
114
|
+
});
|
|
115
|
+
if (fresh) {
|
|
116
|
+
return {
|
|
117
|
+
outage,
|
|
118
|
+
log: `${error.message} (${error.cause}); its texts wait for the next drain`
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
return stayed == null ? {
|
|
122
|
+
outage
|
|
123
|
+
} : {
|
|
124
|
+
outage,
|
|
125
|
+
report: new Overdue(stayed, error)
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
//# sourceMappingURL=outage.js.map
|
package/outage.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"outage.js","sources":["./outage.ts"],"names":[],"mappings":"AAAA,kDAAkD;AAClD,EAAE;AACF,kEAAkE;AAClE,yEAAyE;AACzE,4EAA4E;AAC5E,4EAA4E;AAC5E,8EAA8E;AAC9E,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,4EAA4E;AAC5E,2EAA2E;AAC3E,sEAAsE;AACtE,0EAA0E;AAC1E,4EAA4E;AAC5E,EAAE;AACF,6EAA6E;AAC7E,gFAAgF;AAChF,+EAA+E;AAC/E,2EAA2E;AAC3E,iCAAiC;AAEjC,SAAS,QAAQ,EAAE,OAAO,oCAAsD;AAChF,SAAS,UAAU,EAAE,QAAQ,wBAAuB;AAEpD;;;;;;CAMC,GACD,OAAO,IAAI,UAAkB,KAAK,OAAM;AAexC;;;mBAGmB,GACnB,OAAO,MAAM,gBAAgB;EAClB,OAAO,UAAS;EACzB,QAAmD;EACnD,YAAY,MAAc,EAAE,KAAiB,CAAE;IAC7C,KAAK,CAAC,iEAAiE;MACrE;IACF;IACA,IAAI,CAAC,OAAO,GAAG;MAAE,GAAG,MAAM,OAAO;MAAE,QAAQ,GAAG,OAAO,GAAG,CAAC;IAAC;EAC5D;AACF;AAEA;;;;;;;;;;;;;;CAcC,GACD,OAAO,IAAI,UAAgB,CAAC,QAAQ;EAClC,IAAI,KAAK,SAAS,QAAQ;EAC1B,IAAI,MAAM,MAAM,OAAO;IAAE,QAAQ;EAAK;EACtC,OAAO;IACL,QAAQ;IACR,KAAK,QAAQ,QACT,CAAC,0BAA0B,EAAE,GAAG,OAAO,EAAE,OAAO,KAAK,EAAE,GACvD,CAAC,uBAAuB,EAAE,GAAG,GAAG,CAAC;EACvC;AACF,EAAC;AAED;;;;;;;;;;;;;;;;;CAiBC,GACD,OAAO,IAAI,SAAS,CAAC,QAAyB,CAAC,KAAK;IAClD,IAAI,iBAAiB,UAAU;MAC7B,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,QAAQ,KAAK;MACrC,OAAO;QACL,QAAQ,OAAO,IAAI,GAAG,SAAS;UAAE,GAAG,MAAM;UAAE,MAAM;QAAK;QACvD,GAAG,QACC;UAAE,KAAK,GAAG,MAAM,OAAO,CAAC,+BAA+B,CAAC;QAAC,IACzD,CAAC,CAAC;MACR;IACF;IACA,IAAI,CAAC,CAAC,iBAAiB,UAAU,GAAG;MAClC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,QAAQ,KAAK,KAAK;QAAE,OAAO,OAAO;MAAO;MACjE,OAAO,QAAQ;QAAE;QAAQ,QAAQ;MAAM,IAAI;QAAE;MAAO;IACtD;IACA,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,QAAQ,KAAK,KAAK;MAAE,OAAO;IAAQ;IACnE,IAAI,OAAO;MACT,OAAO;QACL;QACA,KACE,GAAG,MAAM,OAAO,CAAC,EAAE,EAAE,MAAM,KAAK,CAAC,oCAAoC,CAAC;MAC1E;IACF;IACA,OAAO,UAAU,OACb;MAAE;IAAO,IACT;MAAE;MAAQ,QAAQ,IAAI,QAAQ,QAAQ;IAAO;EACnD,EAAC"}
|
package/outage.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// When a model that does not answer is a failure.
|
|
2
|
+
//
|
|
3
|
+
// A provider out past its retries (./remote.ts `retrying`) throws
|
|
4
|
+
// `Unanswered`, and the texts it was asked for stay owed: the next drain
|
|
5
|
+
// embeds them once the model answers, so a host's drains ride an outage out
|
|
6
|
+
// on their own. What a person loses meanwhile is search by meaning: an owed
|
|
7
|
+
// text is found by its words, but not by `.near` or a recall. So an outage is
|
|
8
|
+
// a line in the log when it starts and when it ends, and a failure once the
|
|
9
|
+
// texts it holds up have waited {@link OVERDUE}: reported once, the outage as
|
|
10
|
+
// its cause. Anything else a drain throws is unexpected and reports at once,
|
|
11
|
+
// and once while drains keep failing by it: a box drains every few seconds,
|
|
12
|
+
// and one bug is one report. The drain that next answers logs that it did.
|
|
13
|
+
// A text the model refuses is the text's, not the drain's (./sweep.ts
|
|
14
|
+
// `refused`). A model `Withheld` (a host's spent budget) is no failure at
|
|
15
|
+
// all: its texts wait for the ceiling to lift, logged once, never reported.
|
|
16
|
+
//
|
|
17
|
+
// It is @yaks/mail's delayed letter (pull.ts) for vectors: what fails is the
|
|
18
|
+
// wait, not each try. The outage is @yaks/effects/outage's record, as it is for
|
|
19
|
+
// the mail pull, the tracker's passes and heal: a value the host keeps between
|
|
20
|
+
// drains, which a Durable Object keeps in its own storage, since it sleeps
|
|
21
|
+
// between the alarms that retry.
|
|
22
|
+
|
|
23
|
+
import { answered, failing, type Outage } from '@yaks/effects/outage'
|
|
24
|
+
import { Unanswered, Withheld } from './embedder.js'
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* How long owed texts may wait on a model that does not answer before the
|
|
28
|
+
* wait is a failure. A drain retries about once a minute, and the Workers AI
|
|
29
|
+
* outages the stores have met passed within a few minutes; a quarter of an
|
|
30
|
+
* hour is several times that, and as long as a person's searches by meaning
|
|
31
|
+
* should go without a new text before someone hears of it.
|
|
32
|
+
*/
|
|
33
|
+
export let OVERDUE: number = 15 * 60_000
|
|
34
|
+
|
|
35
|
+
/** An outage a host's drains are riding out (@yaks/effects/outage): since
|
|
36
|
+
* when no drain has answered, whether the wait has been reported or, withheld,
|
|
37
|
+
* needs no report, and as its `cause` the unexpected failure already
|
|
38
|
+
* reported, while drains keep failing by it. */
|
|
39
|
+
export type { Outage }
|
|
40
|
+
|
|
41
|
+
/** What one drain's outcome makes of the outage: the outage it leaves, the
|
|
42
|
+
* failure to report and the line to log, where there is one. */
|
|
43
|
+
export type Rode = { outage: Outage | null; report?: unknown; log?: string }
|
|
44
|
+
|
|
45
|
+
/** One drain's outcome, given the outage it met and when it ended. */
|
|
46
|
+
export type Ride = (outage: Outage | null, now: number) => Rode
|
|
47
|
+
|
|
48
|
+
/** What a report of owed texts waiting past {@link OVERDUE} throws: a fixed
|
|
49
|
+
* message, since one outage is one failure however long it lasts or whatever
|
|
50
|
+
* the model said; what varies rides `context`, and the model's last failure
|
|
51
|
+
* is the `cause`. */
|
|
52
|
+
export class Overdue extends Error {
|
|
53
|
+
override name = 'Overdue'
|
|
54
|
+
context: Unanswered['context'] & { waited: string }
|
|
55
|
+
constructor(waited: number, cause: Unanswered) {
|
|
56
|
+
super('@yaks/embedding: owed texts waited too long for their vectors', {
|
|
57
|
+
cause,
|
|
58
|
+
})
|
|
59
|
+
this.context = { ...cause.context, waited: `${waited} ms` }
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A drain that answered: an outage it rode out, or the failure it reported,
|
|
65
|
+
* is over.
|
|
66
|
+
*
|
|
67
|
+
* ```ts
|
|
68
|
+
* import { equal } from '@yaks/testing'
|
|
69
|
+
* equal(drained(null, 0), { outage: null })
|
|
70
|
+
* equal(drained({ since: 0 }, 90_000),
|
|
71
|
+
* { outage: null, log: 'the outage ended after 90000 ms' })
|
|
72
|
+
* equal(drained({ since: 0, cause: 'Error: no table' }, 9_000), {
|
|
73
|
+
* outage: null,
|
|
74
|
+
* log: 'drains answer again after 9000 ms of Error: no table',
|
|
75
|
+
* })
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export let drained: Ride = (outage, now) => {
|
|
79
|
+
let ms = answered(outage, now)
|
|
80
|
+
if (ms == null) return { outage: null }
|
|
81
|
+
return {
|
|
82
|
+
outage: null,
|
|
83
|
+
log: outage?.cause
|
|
84
|
+
? `drains answer again after ${ms} ms of ${outage.cause}`
|
|
85
|
+
: `the outage ended after ${ms} ms`,
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A drain that threw `error`. A model that did not answer starts an outage or
|
|
91
|
+
* rides on in one, until its texts have waited {@link OVERDUE}, which reports
|
|
92
|
+
* once; a model that withheld its answer waits without a report; anything
|
|
93
|
+
* else reports at once, and not again while drains keep failing by it.
|
|
94
|
+
*
|
|
95
|
+
* ```ts
|
|
96
|
+
* import { equal } from '@yaks/testing'
|
|
97
|
+
* import { Unanswered } from './embedder.ts'
|
|
98
|
+
* let down = new Unanswered('workers-ai',
|
|
99
|
+
* { model: '@cf/m', texts: '2', tries: '4' }, Error('4009'))
|
|
100
|
+
* equal(failed(down)(null, 0).outage, { since: 0 })
|
|
101
|
+
* equal(failed(down)({ since: 0 }, 60_000), { outage: { since: 0 } })
|
|
102
|
+
* let bug = failed(Error('no table'))
|
|
103
|
+
* equal(bug(null, 0).outage, { since: 0, cause: 'Error: no table' })
|
|
104
|
+
* equal(bug(bug(null, 0).outage, 3_000).report, undefined)
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export let failed = (error: unknown): Ride => (met, now) => {
|
|
108
|
+
if (error instanceof Withheld) {
|
|
109
|
+
let { outage, fresh } = failing(met, now)
|
|
110
|
+
return {
|
|
111
|
+
outage: outage.told ? outage : { ...outage, told: true },
|
|
112
|
+
...fresh
|
|
113
|
+
? { log: `${error.message}; its texts wait until it lifts` }
|
|
114
|
+
: {},
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (!(error instanceof Unanswered)) {
|
|
118
|
+
let { outage, fresh } = failing(met, now, { cause: String(error) })
|
|
119
|
+
return fresh ? { outage, report: error } : { outage }
|
|
120
|
+
}
|
|
121
|
+
let { outage, fresh, stayed } = failing(met, now, { limit: OVERDUE })
|
|
122
|
+
if (fresh) {
|
|
123
|
+
return {
|
|
124
|
+
outage,
|
|
125
|
+
log:
|
|
126
|
+
`${error.message} (${error.cause}); its texts wait for the next drain`,
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return stayed == null
|
|
130
|
+
? { outage }
|
|
131
|
+
: { outage, report: new Overdue(stayed, error) }
|
|
132
|
+
}
|