@midsummer-ai/sdk 0.1.0-staging.3123 → 0.1.0-staging.3129
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/ai/contract.mjs +39 -0
- package/ai/index.d.mts +70 -12
- package/ai/index.mjs +74 -5
- package/ai/stand-in.mjs +26 -4
- package/client.d.mts +1 -1
- package/client.mjs +1 -105
- package/{connection-Cu2GSOaR.mjs → connection-C_JSWUrt.mjs} +7 -6
- package/contract.d.mts +1 -1
- package/contract.mjs +1 -1
- package/database/client.d.mts +20 -1
- package/database/client.mjs +31 -8
- package/database/stand-in.d.mts +2 -2
- package/database/stand-in.mjs +1 -1
- package/email/client.mjs +1 -130
- package/email/contract.mjs +1 -1
- package/email/index.d.mts +9 -7
- package/email/index.mjs +1 -58
- package/email/smtp.mjs +1 -2
- package/email/stand-in.mjs +1 -1
- package/email-DXtpr7pT.mjs +214 -0
- package/here-CeqGJW21.mjs +28 -0
- package/{index-DNNXNny7.d.mts → index-B9zs0wIp.d.mts} +6 -1
- package/{index-sgdyoA2P.d.mts → index-Ci6mGXqz.d.mts} +7 -1
- package/index.d.mts +2 -2
- package/index.mjs +3 -137
- package/package.json +1 -1
- package/queues/index.d.mts +17 -5
- package/queues/index.mjs +71 -1
- package/s3.d.mts +1 -1
- package/s3.mjs +1 -1
- package/socket.d.mts +1 -1
- package/socket.mjs +1 -262
- package/src-BKrQAqJN.mjs +519 -0
- package/stand-in.d.mts +1 -1
- package/{store-lIZ5GxVv.mjs → store-CsBhnabU.mjs} +1 -1
- package/{tabs-zCh8kceo.mjs → tabs-gHJHkhm2.mjs} +1 -1
- package/vector/contract.d.mts +1 -1
- package/vector/contract.mjs +1 -1
- package/vector/index.d.mts +2 -2
- package/vector/index.mjs +18 -7
- package/vector/stand-in.d.mts +1 -1
- package/vector/stand-in.mjs +1 -1
- package/vector/wire.d.mts +1 -1
- package/vector/wire.mjs +1 -339
- package/wire-BdNDzlIN.mjs +554 -0
- package/wire.d.mts +1 -1
- package/schema-CTHQMRsW.mjs +0 -217
package/vector/wire.mjs
CHANGED
|
@@ -1,340 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
//#region src/vector/wire.ts
|
|
4
|
-
/**
|
|
5
|
-
* Vector DB's wire: one call is one frame, `{ op, namespace, … }`, and its answer is one frame, `{ ok }`
|
|
6
|
-
* or `{ error, why }` with a `VectorErrorCode`. A frame is a u32 (little-endian) length, that much
|
|
7
|
-
* UTF-8 JSON, then its vectors: each array at a place its call names for one (`places`: an upsert's
|
|
8
|
-
* Vectors' `values`, a query's `vector`, an answer's matches' `values`; never a filter, metadata or
|
|
9
|
-
* an aggregate's key, whatever its fields are named) is `{"$v": n}` in the JSON and section n after
|
|
10
|
-
* it, a u32 count and that many little-endian 32-bit floats, so a value crosses the wire bit for bit
|
|
11
|
-
* (-0 included), never as a JSON number. A transport
|
|
12
|
-
* only carries frames; a Site's own handle is one (`{ send }`), handed to it by the platform for its
|
|
13
|
-
* Environment.
|
|
14
|
-
*
|
|
15
|
-
* const docs = vectorDb(remote(context.vector)).namespace("docs") // a Site
|
|
16
|
-
* const answer = await serve(scoped(engine, (name) => vector.siteNamespace(repositoryId, environment, name)))(frame) // the platform
|
|
17
|
-
*
|
|
18
|
-
* `scoped` is how the platform serves a Site: names mapped to the ids the engine knows them by, and
|
|
19
|
-
* making, changing or dropping a namespace refused.
|
|
20
|
-
*/
|
|
21
|
-
const call = z.discriminatedUnion("op", [
|
|
22
|
-
z.object({
|
|
23
|
-
op: z.literal("describe"),
|
|
24
|
-
namespace: z.string()
|
|
25
|
-
}),
|
|
26
|
-
z.object({
|
|
27
|
-
op: z.literal("upsert"),
|
|
28
|
-
namespace: z.string(),
|
|
29
|
-
vectors: z.unknown()
|
|
30
|
-
}),
|
|
31
|
-
z.object({
|
|
32
|
-
op: z.literal("query"),
|
|
33
|
-
namespace: z.string(),
|
|
34
|
-
query: z.unknown()
|
|
35
|
-
}),
|
|
36
|
-
z.object({
|
|
37
|
-
op: z.literal("fetch"),
|
|
38
|
-
namespace: z.string(),
|
|
39
|
-
ids: z.unknown()
|
|
40
|
-
}),
|
|
41
|
-
z.object({
|
|
42
|
-
op: z.literal("delete"),
|
|
43
|
-
namespace: z.string(),
|
|
44
|
-
ids: z.unknown()
|
|
45
|
-
}),
|
|
46
|
-
z.object({
|
|
47
|
-
op: z.literal("list"),
|
|
48
|
-
namespace: z.string(),
|
|
49
|
-
list: z.unknown()
|
|
50
|
-
}),
|
|
51
|
-
z.object({
|
|
52
|
-
op: z.literal("textSearch"),
|
|
53
|
-
namespace: z.string(),
|
|
54
|
-
search: z.unknown()
|
|
55
|
-
}),
|
|
56
|
-
z.object({
|
|
57
|
-
op: z.literal("multiQuery"),
|
|
58
|
-
namespace: z.string(),
|
|
59
|
-
multi: z.unknown()
|
|
60
|
-
}),
|
|
61
|
-
z.object({
|
|
62
|
-
op: z.literal("aggregate"),
|
|
63
|
-
namespace: z.string(),
|
|
64
|
-
aggregate: z.unknown()
|
|
65
|
-
}),
|
|
66
|
-
z.object({
|
|
67
|
-
op: z.literal("create"),
|
|
68
|
-
namespace: z.string(),
|
|
69
|
-
settings: z.unknown()
|
|
70
|
-
}),
|
|
71
|
-
z.object({
|
|
72
|
-
op: z.literal("update"),
|
|
73
|
-
namespace: z.string(),
|
|
74
|
-
change: z.unknown()
|
|
75
|
-
}),
|
|
76
|
-
z.object({
|
|
77
|
-
op: z.literal("drop"),
|
|
78
|
-
namespace: z.string()
|
|
79
|
-
})
|
|
80
|
-
]);
|
|
81
|
-
const answer = z.union([z.object({
|
|
82
|
-
error: z.enum(vectorErrorCodes),
|
|
83
|
-
why: z.string()
|
|
84
|
-
}), z.object({ ok: z.unknown() })]);
|
|
85
|
-
/** The places of a call's vectors (`side` "call") or of its answer's (`side` "answer"), by its op. */
|
|
86
|
-
function places(op, side) {
|
|
87
|
-
if (side === "call") {
|
|
88
|
-
if (op === "upsert") return [[
|
|
89
|
-
"vectors",
|
|
90
|
-
"*",
|
|
91
|
-
"values"
|
|
92
|
-
]];
|
|
93
|
-
if (op === "query") return [["query", "vector"]];
|
|
94
|
-
if (op === "multiQuery") return [[
|
|
95
|
-
"multi",
|
|
96
|
-
"queries",
|
|
97
|
-
"*",
|
|
98
|
-
"vector"
|
|
99
|
-
]];
|
|
100
|
-
return [];
|
|
101
|
-
}
|
|
102
|
-
if (op === "query" || op === "fetch" || op === "list" || op === "textSearch") return [[
|
|
103
|
-
"ok",
|
|
104
|
-
"*",
|
|
105
|
-
"values"
|
|
106
|
-
]];
|
|
107
|
-
if (op === "multiQuery") return [[
|
|
108
|
-
"ok",
|
|
109
|
-
"results",
|
|
110
|
-
"*",
|
|
111
|
-
"*",
|
|
112
|
-
"values"
|
|
113
|
-
], [
|
|
114
|
-
"ok",
|
|
115
|
-
"fused",
|
|
116
|
-
"*",
|
|
117
|
-
"values"
|
|
118
|
-
]];
|
|
119
|
-
return [];
|
|
120
|
-
}
|
|
121
|
-
/** `value` with each thing at one of `at` (`Places`) given to `f`, the rest as it is. */
|
|
122
|
-
function atPlaces(value, at, f) {
|
|
123
|
-
const walk = (v, paths) => {
|
|
124
|
-
if (paths.some((p) => p.length === 0)) return f(v);
|
|
125
|
-
if (paths.length === 0 || v === null || typeof v !== "object") return v;
|
|
126
|
-
if (Array.isArray(v)) {
|
|
127
|
-
const next = paths.filter((p) => p[0] === "*").map((p) => p.slice(1));
|
|
128
|
-
return next.length === 0 ? v : v.map((item) => walk(item, next));
|
|
129
|
-
}
|
|
130
|
-
return Object.fromEntries(Object.entries(v).map(([k, x]) => {
|
|
131
|
-
const next = paths.filter((p) => p[0] === k).map((p) => p.slice(1));
|
|
132
|
-
return [k, next.length === 0 ? x : walk(x, next)];
|
|
133
|
-
}));
|
|
134
|
-
};
|
|
135
|
-
return walk(value, at);
|
|
136
|
-
}
|
|
137
|
-
/** `value` as a frame: the arrays of numbers at `where` (`places`) moved into sections (`{"$v": n}`), the JSON first. */
|
|
138
|
-
function encode(value, where = []) {
|
|
139
|
-
const sections = [];
|
|
140
|
-
const lifted = atPlaces(value, where, (v) => Array.isArray(v) && v.every((x) => typeof x === "number") ? { $v: sections.push(v) - 1 } : v);
|
|
141
|
-
const json = new TextEncoder().encode(JSON.stringify(lifted));
|
|
142
|
-
const out = new Uint8Array(4 + json.length + sections.reduce((n, v) => n + 4 + 4 * v.length, 0));
|
|
143
|
-
const view = new DataView(out.buffer);
|
|
144
|
-
view.setUint32(0, json.length, true);
|
|
145
|
-
out.set(json, 4);
|
|
146
|
-
let at = 4 + json.length;
|
|
147
|
-
for (const v of sections) {
|
|
148
|
-
view.setUint32(at, v.length, true);
|
|
149
|
-
at += 4;
|
|
150
|
-
for (const x of v) {
|
|
151
|
-
view.setFloat32(at, x, true);
|
|
152
|
-
at += 4;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return out;
|
|
156
|
-
}
|
|
157
|
-
/** A frame's value (`encode`): its sections back in place of their `{"$v": n}` at `where` (`places`; or, for a call
|
|
158
|
-
* read before its op is known, the places of the op its JSON names). Throws where it does not read. */
|
|
159
|
-
function decode(frame, where = []) {
|
|
160
|
-
const view = new DataView(frame.buffer, frame.byteOffset, frame.byteLength);
|
|
161
|
-
const length = view.getUint32(0, true);
|
|
162
|
-
if (4 + length > frame.length) throw new Error("a frame shorter than its JSON");
|
|
163
|
-
const json = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(frame.subarray(4, 4 + length)));
|
|
164
|
-
const sections = [];
|
|
165
|
-
for (let at = 4 + length; at < frame.length;) {
|
|
166
|
-
const count = view.getUint32(at, true);
|
|
167
|
-
at += 4;
|
|
168
|
-
if (at + 4 * count > frame.length) throw new Error("a frame shorter than its vectors");
|
|
169
|
-
sections.push(Array.from({ length: count }, (_, k) => view.getFloat32(at + 4 * k, true)));
|
|
170
|
-
at += 4 * count;
|
|
171
|
-
}
|
|
172
|
-
const place = (v) => {
|
|
173
|
-
if (v === null || typeof v !== "object" || Array.isArray(v) || !("$v" in v)) return v;
|
|
174
|
-
const n = v.$v;
|
|
175
|
-
if (typeof n !== "number" || !sections[n]) throw new Error("a vector section that is not there");
|
|
176
|
-
return sections[n];
|
|
177
|
-
};
|
|
178
|
-
return atPlaces(json, typeof where === "function" ? where(json) : where, place);
|
|
179
|
-
}
|
|
180
|
-
/** The places of the call `json` names the op of (`places`). */
|
|
181
|
-
function callPlaces(json) {
|
|
182
|
-
const op = json !== null && typeof json === "object" ? json.op : void 0;
|
|
183
|
-
return typeof op === "string" ? places(op, "call") : [];
|
|
184
|
-
}
|
|
185
|
-
/** An engine that sends each call over `transport`. A transport that fails is `unavailable`. */
|
|
186
|
-
function remote(transport) {
|
|
187
|
-
async function ask(request) {
|
|
188
|
-
let frame;
|
|
189
|
-
try {
|
|
190
|
-
frame = await transport.send(encode(request, places(request.op, "call")));
|
|
191
|
-
} catch {
|
|
192
|
-
throw new VectorError("unavailable", "Vector DB could not be reached. Try again.");
|
|
193
|
-
}
|
|
194
|
-
let parsed;
|
|
195
|
-
try {
|
|
196
|
-
parsed = answer.parse(decode(frame, places(request.op, "answer")));
|
|
197
|
-
} catch {
|
|
198
|
-
throw new VectorError("unavailable", "Vector DB answered something that is not a Vector DB frame");
|
|
199
|
-
}
|
|
200
|
-
if ("error" in parsed) throw new VectorError(parsed.error, parsed.why);
|
|
201
|
-
return parsed.ok;
|
|
202
|
-
}
|
|
203
|
-
return {
|
|
204
|
-
describe: (namespace) => ask({
|
|
205
|
-
op: "describe",
|
|
206
|
-
namespace
|
|
207
|
-
}),
|
|
208
|
-
upsert: (namespace, vectors) => ask({
|
|
209
|
-
op: "upsert",
|
|
210
|
-
namespace,
|
|
211
|
-
vectors
|
|
212
|
-
}),
|
|
213
|
-
query: (namespace, query) => ask({
|
|
214
|
-
op: "query",
|
|
215
|
-
namespace,
|
|
216
|
-
query
|
|
217
|
-
}),
|
|
218
|
-
fetch: (namespace, ids) => ask({
|
|
219
|
-
op: "fetch",
|
|
220
|
-
namespace,
|
|
221
|
-
ids
|
|
222
|
-
}),
|
|
223
|
-
delete: async (namespace, ids) => void await ask({
|
|
224
|
-
op: "delete",
|
|
225
|
-
namespace,
|
|
226
|
-
ids
|
|
227
|
-
}),
|
|
228
|
-
list: (namespace, list) => ask({
|
|
229
|
-
op: "list",
|
|
230
|
-
namespace,
|
|
231
|
-
list
|
|
232
|
-
}),
|
|
233
|
-
textSearch: (namespace, search) => ask({
|
|
234
|
-
op: "textSearch",
|
|
235
|
-
namespace,
|
|
236
|
-
search
|
|
237
|
-
}),
|
|
238
|
-
multiQuery: (namespace, multi) => ask({
|
|
239
|
-
op: "multiQuery",
|
|
240
|
-
namespace,
|
|
241
|
-
multi
|
|
242
|
-
}),
|
|
243
|
-
aggregate: (namespace, aggregate) => ask({
|
|
244
|
-
op: "aggregate",
|
|
245
|
-
namespace,
|
|
246
|
-
aggregate
|
|
247
|
-
}),
|
|
248
|
-
create: async (namespace, settings) => void await ask({
|
|
249
|
-
op: "create",
|
|
250
|
-
namespace,
|
|
251
|
-
settings
|
|
252
|
-
}),
|
|
253
|
-
update: async (namespace, change) => void await ask({
|
|
254
|
-
op: "update",
|
|
255
|
-
namespace,
|
|
256
|
-
change
|
|
257
|
-
}),
|
|
258
|
-
drop: async (namespace) => void await ask({
|
|
259
|
-
op: "drop",
|
|
260
|
-
namespace
|
|
261
|
-
})
|
|
262
|
-
};
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* Serves request frames from `engine`. A frame comes from the network, so it is checked here as the
|
|
266
|
-
* client would check it; a refusal is returned as `invalid`. Making and dropping namespaces is
|
|
267
|
-
* served only by an engine that has them, else `forbidden`. A failure that is not a `VectorError`
|
|
268
|
-
* is the engine's own fault and is thrown to the transport, never returned.
|
|
269
|
-
*/
|
|
270
|
-
function serve(engine) {
|
|
271
|
-
return async (frame) => {
|
|
272
|
-
try {
|
|
273
|
-
let json;
|
|
274
|
-
try {
|
|
275
|
-
json = decode(frame, callPlaces);
|
|
276
|
-
} catch {
|
|
277
|
-
throw new VectorError("invalid", "the request frame is not a Vector DB frame");
|
|
278
|
-
}
|
|
279
|
-
const parsed = call.safeParse(json);
|
|
280
|
-
if (!parsed.success) throw new VectorError("invalid", "the request frame is not a Vector DB call");
|
|
281
|
-
const request = parsed.data;
|
|
282
|
-
return encode({ ok: await answerTo(engine, checked(NamespaceName, request.namespace), request) ?? null }, places(request.op, "answer"));
|
|
283
|
-
} catch (error) {
|
|
284
|
-
if (error instanceof VectorError) return encode({
|
|
285
|
-
error: error.code,
|
|
286
|
-
why: error.message
|
|
287
|
-
});
|
|
288
|
-
throw error;
|
|
289
|
-
}
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
|
-
async function answerTo(engine, namespace, request) {
|
|
293
|
-
switch (request.op) {
|
|
294
|
-
case "describe": return engine.describe(namespace);
|
|
295
|
-
case "upsert": return engine.upsert(namespace, checked(Vectors, request.vectors));
|
|
296
|
-
case "query": return engine.query(namespace, checked(Query, request.query));
|
|
297
|
-
case "fetch": return engine.fetch(namespace, checked(Ids, request.ids));
|
|
298
|
-
case "delete": return engine.delete(namespace, checked(Ids, request.ids));
|
|
299
|
-
case "list": return engine.list(namespace, checked(List, request.list));
|
|
300
|
-
case "textSearch": return engine.textSearch(namespace, checked(TextSearch, request.search));
|
|
301
|
-
case "multiQuery": return engine.multiQuery(namespace, checked(MultiQuery, request.multi));
|
|
302
|
-
case "aggregate": return engine.aggregate(namespace, checked(Aggregation, request.aggregate));
|
|
303
|
-
case "create":
|
|
304
|
-
if (!engine.create) throw new VectorError("forbidden", "Vector namespaces are made through the platform");
|
|
305
|
-
return engine.create(namespace, checked(Settings, request.settings));
|
|
306
|
-
case "update":
|
|
307
|
-
if (!engine.update) throw new VectorError("forbidden", "Vector namespaces are changed through the platform");
|
|
308
|
-
return engine.update(namespace, checked(Update, request.change));
|
|
309
|
-
case "drop":
|
|
310
|
-
if (!engine.drop) throw new VectorError("forbidden", "Vector namespaces are dropped through the platform");
|
|
311
|
-
return engine.drop(namespace);
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* `engine` as a Site of one Environment reaches it: `namespaceId` returns, for the one name a call
|
|
316
|
-
* names, the id the engine knows that Environment's Vector namespace by, so a call costs the lookup
|
|
317
|
-
* of its own namespace and never a listing of the others. A name with no id is `not_found`, as a
|
|
318
|
-
* namespace that does not exist is, so a Site learns nothing of other Environments. It cannot make
|
|
319
|
-
* or drop one.
|
|
320
|
-
*/
|
|
321
|
-
function scoped(engine, namespaceId) {
|
|
322
|
-
const id = async (name) => {
|
|
323
|
-
const found = await namespaceId(name);
|
|
324
|
-
if (found === void 0) throw new VectorError("not_found", `There is no Vector namespace named ${name}.`);
|
|
325
|
-
return found;
|
|
326
|
-
};
|
|
327
|
-
return {
|
|
328
|
-
describe: async (namespace) => engine.describe(await id(namespace)),
|
|
329
|
-
upsert: async (namespace, vectors) => engine.upsert(await id(namespace), vectors),
|
|
330
|
-
query: async (namespace, query) => engine.query(await id(namespace), query),
|
|
331
|
-
fetch: async (namespace, ids) => engine.fetch(await id(namespace), ids),
|
|
332
|
-
delete: async (namespace, ids) => engine.delete(await id(namespace), ids),
|
|
333
|
-
list: async (namespace, list) => engine.list(await id(namespace), list),
|
|
334
|
-
textSearch: async (namespace, search) => engine.textSearch(await id(namespace), search),
|
|
335
|
-
multiQuery: async (namespace, multi) => engine.multiQuery(await id(namespace), multi),
|
|
336
|
-
aggregate: async (namespace, aggregate) => engine.aggregate(await id(namespace), aggregate)
|
|
337
|
-
};
|
|
338
|
-
}
|
|
339
|
-
//#endregion
|
|
1
|
+
import { a as scoped, i as remote, n as encode, o as serve, r as places, t as decode } from "../wire-BdNDzlIN.mjs";
|
|
340
2
|
export { decode, encode, places, remote, scoped, serve };
|