@midsummer-ai/sdk 0.1.0-next.901 → 0.1.0-staging.3106
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/auth-1W-SzpD3.mjs +441 -0
- package/build-DQo-JrlN.d.mts +120 -0
- package/bytes-C2pcyl9u.mjs +280 -0
- package/{connection-CDk7H1FL.mjs → connection-Cu2GSOaR.mjs} +171 -31
- package/database/build.d.mts +2 -2
- package/database/build.mjs +1404 -117
- package/database/client.d.mts +85 -9
- package/database/client.mjs +550 -32
- package/database/contract.d.mts +92 -2
- package/database/contract.mjs +2179 -3
- package/database/errors.d.mts +1 -22
- package/database/react.d.mts +133 -0
- package/database/react.mjs +488 -0
- package/database/server.d.mts +2 -3
- package/database/server.mjs +3 -320
- package/database/stand-in.d.mts +95 -9
- package/database/stand-in.mjs +1436 -116
- package/errors-x2vXKi0u.d.mts +25 -0
- package/index-DNNXNny7.d.mts +410 -0
- package/key-BrRdFY-n.mjs +11 -0
- package/package.json +114 -1
- package/schema-CTHQMRsW.mjs +217 -0
- package/server-B5HK2Ou9.d.mts +574 -0
- package/server-EDRbF2u-.mjs +1893 -0
- package/{store-BzI9at5x.mjs → store-lIZ5GxVv.mjs} +1 -1
- package/{tabs-D-P8_ci_.mjs → tabs-zCh8kceo.mjs} +1 -1
- package/vector/contract.d.mts +1 -1
- package/vector/contract.mjs +773 -1
- package/vector/index.d.mts +2 -2
- package/vector/index.mjs +71 -3
- package/vector/stand-in.d.mts +1 -1
- package/vector/stand-in.mjs +326 -23
- package/vector/wire.d.mts +11 -2
- package/vector/wire.mjs +179 -14
- package/build-SQ44jwSa.d.mts +0 -41
- package/index-DdoK0y7D.d.mts +0 -192
- package/schema-BF-Y1EVu.mjs +0 -142
- package/schema-DS8x1Dn8.mjs +0 -115
- package/schema-f7E9uzUn.d.mts +0 -74
- package/server-C7O7CICU.d.mts +0 -182
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
//#region src/vector/errors.ts
|
|
3
|
+
const vectorErrorCodes = [
|
|
4
|
+
"invalid",
|
|
5
|
+
"not_found",
|
|
6
|
+
"conflict",
|
|
7
|
+
"forbidden",
|
|
8
|
+
"unavailable"
|
|
9
|
+
];
|
|
10
|
+
var VectorError = class extends Error {
|
|
11
|
+
code;
|
|
12
|
+
constructor(code, why) {
|
|
13
|
+
super(why);
|
|
14
|
+
this.code = code;
|
|
15
|
+
this.name = "VectorError";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/vector/schema.ts
|
|
20
|
+
/**
|
|
21
|
+
* What Vector DB accepts, as zod schemas: the client checks a call with them before it leaves the
|
|
22
|
+
* caller, and `serve` checks every frame with them again, because a frame comes from the network.
|
|
23
|
+
* A limit lives here once, so both edges hold the same one.
|
|
24
|
+
*/
|
|
25
|
+
/** Most numbers a Vector holds. */
|
|
26
|
+
const MAX_DIMENSION = 4096;
|
|
27
|
+
/** Most Vectors one upsert takes, and most ids one fetch or delete names. */
|
|
28
|
+
const MAX_BATCH = 1e3;
|
|
29
|
+
/** The most matches one query returns. */
|
|
30
|
+
const MAX_TOP_K = 100;
|
|
31
|
+
/** Most characters a text to embed has. */
|
|
32
|
+
const MAX_TEXT = 3e4;
|
|
33
|
+
/** Most bytes a Vector's metadata takes, as JSON. */
|
|
34
|
+
const MAX_METADATA_BYTES = 10240;
|
|
35
|
+
/** Most searches one `multiQuery` asks. */
|
|
36
|
+
const MAX_QUERIES = 16;
|
|
37
|
+
/** Most metadata fields a namespace keeps full text. */
|
|
38
|
+
const MAX_FULL_TEXT = 16;
|
|
39
|
+
/** 1 to 63 lowercase letters, digits and hyphens, starting with a letter or digit. */
|
|
40
|
+
const NamespaceName = z.string().regex(/^[a-z0-9][a-z0-9-]{0,62}$/, "a Vector namespace's name is 1 to 63 lowercase letters, digits and hyphens, starting with a letter or digit");
|
|
41
|
+
/** How closeness is measured, fixed when a Vector namespace is made. */
|
|
42
|
+
const Metric = z.enum([
|
|
43
|
+
"cosine",
|
|
44
|
+
"euclidean",
|
|
45
|
+
"dot"
|
|
46
|
+
]);
|
|
47
|
+
const Dimension = z.number().int().min(1).max(MAX_DIMENSION);
|
|
48
|
+
const VectorId = z.string().min(1).max(512);
|
|
49
|
+
const Finite = z.number().refine(Number.isFinite, "a number in metadata is finite");
|
|
50
|
+
const Scalar = z.union([
|
|
51
|
+
z.string().max(2048),
|
|
52
|
+
Finite,
|
|
53
|
+
z.boolean()
|
|
54
|
+
]);
|
|
55
|
+
const FieldName = z.string().min(1).max(64).refine((name) => !name.startsWith("$") && !name.includes("."), "a metadata field's name does not start with $ or hold a dot");
|
|
56
|
+
/** An array value: up to 100 strings and numbers. */
|
|
57
|
+
const List_ = z.array(z.union([z.string().max(2048), Finite])).max(100);
|
|
58
|
+
const Metadata = z.record(FieldName, z.union([Scalar, List_])).refine((metadata) => new TextEncoder().encode(JSON.stringify(metadata)).length <= MAX_METADATA_BYTES, `metadata is at most ${MAX_METADATA_BYTES} bytes as JSON`);
|
|
59
|
+
/**
|
|
60
|
+
* The largest value a Vector holds. Engines keep values and scores as 32-bit floats, and within it
|
|
61
|
+
* every score of 4,096 values is one, finite: a squared distance is at most 4,096 · (2e17)², about
|
|
62
|
+
* 1.6e38, under a 32-bit float's 3.4e38. Scores are exact to a 32-bit float's precision for values
|
|
63
|
+
* from 1e-17 up.
|
|
64
|
+
*/
|
|
65
|
+
const MAX_VALUE = 0x16345785d8a0000;
|
|
66
|
+
const Values = z.array(z.number().refine((value) => Number.isFinite(value) && Math.abs(value) <= 0x16345785d8a0000, `a Vector's values are finite numbers of at most ${MAX_VALUE} in size`)).min(1).max(MAX_DIMENSION);
|
|
67
|
+
const Vector = z.object({
|
|
68
|
+
id: VectorId,
|
|
69
|
+
values: Values,
|
|
70
|
+
metadata: Metadata.optional()
|
|
71
|
+
}).strict();
|
|
72
|
+
const Text = z.string().max(MAX_TEXT).refine((text) => text.trim().length > 0, "a text to embed is not empty or all spaces");
|
|
73
|
+
const TextVector = z.object({
|
|
74
|
+
id: VectorId,
|
|
75
|
+
text: Text,
|
|
76
|
+
metadata: Metadata.optional()
|
|
77
|
+
}).strict();
|
|
78
|
+
const Vectors = z.array(z.union([Vector, TextVector])).min(1).max(MAX_BATCH).refine((vectors) => new Set(vectors.map((v) => v.id)).size === vectors.length, "each id is upserted once a call");
|
|
79
|
+
const Ids = z.array(VectorId).min(1).max(MAX_BATCH);
|
|
80
|
+
const Bound = z.union([z.string().max(2048), z.number().refine(Number.isFinite)]);
|
|
81
|
+
const Item = z.union([z.string().max(2048), z.number().refine(Number.isFinite)]);
|
|
82
|
+
const Pattern = z.string().max(2048);
|
|
83
|
+
const Nullable = z.union([Scalar, z.null()]);
|
|
84
|
+
const Condition = z.union([Scalar, z.object({
|
|
85
|
+
$eq: Nullable.optional(),
|
|
86
|
+
$ne: Nullable.optional(),
|
|
87
|
+
$in: z.array(Nullable).min(1).max(100).optional(),
|
|
88
|
+
$nin: z.array(Nullable).min(1).max(100).optional(),
|
|
89
|
+
$gt: Bound.optional(),
|
|
90
|
+
$gte: Bound.optional(),
|
|
91
|
+
$lt: Bound.optional(),
|
|
92
|
+
$lte: Bound.optional(),
|
|
93
|
+
$contains: Item.optional(),
|
|
94
|
+
$notContains: Item.optional(),
|
|
95
|
+
$containsAny: z.array(Item).min(1).max(100).optional(),
|
|
96
|
+
$notContainsAny: z.array(Item).min(1).max(100).optional(),
|
|
97
|
+
$anyGt: Bound.optional(),
|
|
98
|
+
$anyGte: Bound.optional(),
|
|
99
|
+
$anyLt: Bound.optional(),
|
|
100
|
+
$anyLte: Bound.optional(),
|
|
101
|
+
$glob: Pattern.optional(),
|
|
102
|
+
$notGlob: Pattern.optional(),
|
|
103
|
+
$iglob: Pattern.optional(),
|
|
104
|
+
$notIGlob: Pattern.optional(),
|
|
105
|
+
$containsAllTokens: Pattern.optional(),
|
|
106
|
+
$containsAnyToken: z.union([Pattern, z.object({
|
|
107
|
+
tokens: Pattern,
|
|
108
|
+
lastAsPrefix: z.boolean().optional()
|
|
109
|
+
}).strict()]).optional(),
|
|
110
|
+
$containsTokenSequence: Pattern.optional()
|
|
111
|
+
}).strict().refine((condition) => Object.keys(condition).length > 0, "a condition names an operator")]);
|
|
112
|
+
/** A filter's field: a metadata field, or `id`. */
|
|
113
|
+
const FilterField = z.union([z.literal("id"), FieldName]);
|
|
114
|
+
const Fields = z.record(FilterField, Condition).refine((filter) => Object.keys(filter).length <= 20, "a filter names at most 20 fields");
|
|
115
|
+
/** How deep `$and`, `$or` and `$not` nest. */
|
|
116
|
+
const DEPTH = 4;
|
|
117
|
+
function filterOf(depth) {
|
|
118
|
+
if (depth === 0) return Fields;
|
|
119
|
+
const inner = z.lazy(() => filterOf(depth - 1));
|
|
120
|
+
return z.union([
|
|
121
|
+
z.object({ $and: z.array(inner).min(1).max(20) }).strict(),
|
|
122
|
+
z.object({ $or: z.array(inner).min(1).max(20) }).strict(),
|
|
123
|
+
z.object({ $not: inner }).strict(),
|
|
124
|
+
Fields
|
|
125
|
+
]);
|
|
126
|
+
}
|
|
127
|
+
/** Each field's condition, all of which must hold (a bare value is `$eq`); or `$and`, `$or` or `$not` of filters, 4 deep at most. */
|
|
128
|
+
const Filter = filterOf(DEPTH);
|
|
129
|
+
const QueryOptions = {
|
|
130
|
+
topK: z.number().int().min(1).max(100),
|
|
131
|
+
filter: Filter.optional(),
|
|
132
|
+
includeValues: z.boolean(),
|
|
133
|
+
includeMetadata: z.boolean(),
|
|
134
|
+
consistency: z.enum(["strong", "eventual"]).optional()
|
|
135
|
+
};
|
|
136
|
+
const Query = z.union([z.object({
|
|
137
|
+
vector: Values,
|
|
138
|
+
...QueryOptions
|
|
139
|
+
}).strict(), z.object({
|
|
140
|
+
text: Text,
|
|
141
|
+
...QueryOptions
|
|
142
|
+
}).strict()]);
|
|
143
|
+
const Include = {
|
|
144
|
+
includeValues: z.boolean(),
|
|
145
|
+
includeMetadata: z.boolean()
|
|
146
|
+
};
|
|
147
|
+
const TopK = z.number().int().min(1).max(100);
|
|
148
|
+
const OrderField = z.union([z.literal("id"), FieldName]);
|
|
149
|
+
const List = z.object({
|
|
150
|
+
orderBy: z.array(z.tuple([OrderField, z.enum(["asc", "desc"])])).min(1).max(8),
|
|
151
|
+
filter: Filter.optional(),
|
|
152
|
+
topK: TopK,
|
|
153
|
+
...Include
|
|
154
|
+
}).strict();
|
|
155
|
+
const Words = z.string().max(MAX_TEXT).refine((words) => /[\p{L}\p{N}]/u.test(words), "a text search has a word");
|
|
156
|
+
const TextSearch = z.object({
|
|
157
|
+
words: Words,
|
|
158
|
+
field: FieldName,
|
|
159
|
+
filter: Filter.optional(),
|
|
160
|
+
topK: TopK,
|
|
161
|
+
...Include
|
|
162
|
+
}).strict();
|
|
163
|
+
const PartOptions = {
|
|
164
|
+
filter: Filter.optional(),
|
|
165
|
+
topK: TopK
|
|
166
|
+
};
|
|
167
|
+
const Part = z.union([
|
|
168
|
+
z.object({
|
|
169
|
+
vector: Values,
|
|
170
|
+
...PartOptions
|
|
171
|
+
}).strict(),
|
|
172
|
+
z.object({
|
|
173
|
+
text: Text,
|
|
174
|
+
...PartOptions
|
|
175
|
+
}).strict(),
|
|
176
|
+
z.object({
|
|
177
|
+
textSearch: Words,
|
|
178
|
+
field: FieldName,
|
|
179
|
+
...PartOptions
|
|
180
|
+
}).strict()
|
|
181
|
+
]);
|
|
182
|
+
const MultiQuery = z.object({
|
|
183
|
+
queries: z.array(Part).min(1).max(16),
|
|
184
|
+
fuse: z.object({
|
|
185
|
+
k: z.number().min(0).max(1e6),
|
|
186
|
+
weights: z.array(z.number().min(0).max(1e6))
|
|
187
|
+
}).strict().optional(),
|
|
188
|
+
topK: TopK,
|
|
189
|
+
includeMetadata: z.boolean()
|
|
190
|
+
}).strict().refine((multi) => !multi.fuse || multi.fuse.weights.length === multi.queries.length, "fuse has one weight a search");
|
|
191
|
+
const AggregateOf = z.union([z.tuple([z.literal("count")]), z.tuple([z.literal("sum"), FieldName])]);
|
|
192
|
+
const Aggregation = z.object({
|
|
193
|
+
aggregates: z.record(z.string().min(1).max(64), AggregateOf).refine((a) => Object.keys(a).length >= 1 && Object.keys(a).length <= 16, "1 to 16 aggregates"),
|
|
194
|
+
groupBy: z.array(FieldName).max(8),
|
|
195
|
+
filter: Filter.optional(),
|
|
196
|
+
limit: z.number().int().min(1).max(1e4)
|
|
197
|
+
}).strict();
|
|
198
|
+
/** An embedding model's name, as the platform offers it. */
|
|
199
|
+
const EmbeddingModel = z.string().min(1).max(100);
|
|
200
|
+
const FullText = z.array(FieldName).max(16).refine((fields) => new Set(fields).size === fields.length, "each full-text field once");
|
|
201
|
+
const Settings = z.object({
|
|
202
|
+
dimension: Dimension,
|
|
203
|
+
metric: Metric,
|
|
204
|
+
embed: EmbeddingModel.optional(),
|
|
205
|
+
fullText: FullText.optional(),
|
|
206
|
+
vectorType: z.enum(["f32", "f16"]).optional()
|
|
207
|
+
}).strict();
|
|
208
|
+
const Update = z.object({ addFullText: FullText.min(1) }).strict();
|
|
209
|
+
/** `value` as `schema` reads it, or `invalid` with why not. */
|
|
210
|
+
function checked(schema, value) {
|
|
211
|
+
const parsed = schema.safeParse(value);
|
|
212
|
+
if (parsed.success) return parsed.data;
|
|
213
|
+
const issue = parsed.error.issues[0];
|
|
214
|
+
throw new VectorError("invalid", `${issue && issue.path.length > 0 ? `${issue.path.join(".")}: ` : ""}${issue?.message ?? "not valid"}`);
|
|
215
|
+
}
|
|
216
|
+
//#endregion
|
|
217
|
+
export { Update as _, MAX_DIMENSION as a, VectorError as b, MAX_QUERIES as c, MAX_VALUE as d, MultiQuery as f, TextSearch as g, Settings as h, MAX_BATCH as i, MAX_TEXT as l, Query as m, Ids as n, MAX_FULL_TEXT as o, NamespaceName as p, List as r, MAX_METADATA_BYTES as s, Aggregation as t, MAX_TOP_K as u, Vectors as v, vectorErrorCodes as x, checked as y };
|