@sentry/junior-plugin-api 0.306.0 → 0.307.1
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/dist/context-Buwf8ZyW.d.ts +523 -0
- package/dist/embeddings.d.ts +171 -0
- package/dist/embeddings.js +147 -0
- package/dist/index.d.ts +87 -521
- package/dist/index.js +322 -114
- package/package.json +6 -1
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { sql } from "drizzle-orm";
|
|
3
|
+
import { customType } from "drizzle-orm/pg-core";
|
|
4
|
+
// src/embeddings.ts
|
|
5
|
+
/**
|
|
6
|
+
* Shared parts of the derived embedding indexes. Each owner keeps its own
|
|
7
|
+
* table, so a foreign key still deletes a vector with its source row. These
|
|
8
|
+
* helpers keep the column types, the distance SQL, the batch checks, and the
|
|
9
|
+
* backfill loop the same in every table.
|
|
10
|
+
*/
|
|
11
|
+
function pgvectorColumn(dataType) {
|
|
12
|
+
return customType({
|
|
13
|
+
dataType() {
|
|
14
|
+
return dataType;
|
|
15
|
+
},
|
|
16
|
+
toDriver(value) {
|
|
17
|
+
return JSON.stringify(value);
|
|
18
|
+
},
|
|
19
|
+
fromDriver(value) {
|
|
20
|
+
return value.slice(1, -1).split(",").map(Number);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* pgvector `vector` column without a fixed size, so a new embedding model
|
|
26
|
+
* needs no migration. Each table records the size of each row in its own
|
|
27
|
+
* column.
|
|
28
|
+
*/
|
|
29
|
+
const embeddingVector = pgvectorColumn("vector");
|
|
30
|
+
/**
|
|
31
|
+
* Half-precision `embeddingVector`: 2 bytes per value instead of 4, for
|
|
32
|
+
* tables that hold many vectors.
|
|
33
|
+
*/
|
|
34
|
+
const embeddingHalfvec = pgvectorColumn("halfvec");
|
|
35
|
+
const embeddingResultSchema = z.object({
|
|
36
|
+
costUsd: z.number().finite().nonnegative().optional(),
|
|
37
|
+
dimensions: z.number().int().positive(),
|
|
38
|
+
model: z.string().min(1),
|
|
39
|
+
provider: z.string().min(1),
|
|
40
|
+
vectors: z.array(z.array(z.number().finite()))
|
|
41
|
+
}).strict();
|
|
42
|
+
/**
|
|
43
|
+
* Validate an embedder result: one vector per text, all of the reported
|
|
44
|
+
* size. With `dimensions`, the size must be one that the table accepts. With
|
|
45
|
+
* `expected`, the result must come from that provider and model, because a
|
|
46
|
+
* backfill that finds rows by model would see a row of another model as
|
|
47
|
+
* missing on every run.
|
|
48
|
+
*/
|
|
49
|
+
function parseEmbeddingBatch(raw, args) {
|
|
50
|
+
const result = embeddingResultSchema.parse(raw);
|
|
51
|
+
if (args.dimensions && !args.dimensions.includes(result.dimensions)) throw new Error("Unsupported embedding vector size.");
|
|
52
|
+
if (result.vectors.length !== args.textCount) throw new Error("Embedding provider returned an unexpected vector count.");
|
|
53
|
+
if (result.vectors.some((vector) => vector.length !== result.dimensions)) throw new Error("Embedding provider returned an unexpected vector size.");
|
|
54
|
+
if (args.expected && (result.provider !== args.expected.provider || result.model !== args.expected.model)) throw new Error(`Embedding provider used model ${result.provider}/${result.model}, not ${args.expected.provider}/${args.expected.model}.`);
|
|
55
|
+
return {
|
|
56
|
+
dimensions: result.dimensions,
|
|
57
|
+
model: result.model,
|
|
58
|
+
provider: result.provider,
|
|
59
|
+
vectors: result.vectors
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** Embed one text with the embedder's model and return its vector. */
|
|
63
|
+
async function embedText(embedder, input) {
|
|
64
|
+
const batch = parseEmbeddingBatch(await embedder.embedTexts({
|
|
65
|
+
inputType: input.inputType,
|
|
66
|
+
texts: [input.text]
|
|
67
|
+
}), {
|
|
68
|
+
expected: embedder,
|
|
69
|
+
textCount: 1
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
model: batch.model,
|
|
73
|
+
provider: batch.provider,
|
|
74
|
+
vector: batch.vectors[0]
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function vectorSize(query) {
|
|
78
|
+
if (query.length === 0) throw new Error("Embedding vector is empty.");
|
|
79
|
+
return sql.raw(String(query.length));
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Cosine distance from a stored vector to a query vector. Both sides are cast
|
|
83
|
+
* to the query size, the form that an HNSW expression index on that size
|
|
84
|
+
* matches. The cast fails on a stored vector of another size, so filter on the
|
|
85
|
+
* size first, as the index predicate does, or use `guardedCosineDistance`.
|
|
86
|
+
* A caller with another copy of drizzle-orm embeds the result in its own
|
|
87
|
+
* `sql` template to get its own `SQL` type.
|
|
88
|
+
*/
|
|
89
|
+
function cosineDistance(args) {
|
|
90
|
+
const size = vectorSize(args.query);
|
|
91
|
+
const type = sql.raw(args.type);
|
|
92
|
+
return sql`(${args.column}::${type}(${size})) <=> ${JSON.stringify(args.query)}::${type}(${size})`;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* `cosineDistance`, or null for a stored vector of another size. Postgres
|
|
96
|
+
* does not promise to check a WHERE term before it computes an ORDER BY or
|
|
97
|
+
* SELECT expression, and pgvector fails on vectors of two sizes. CASE fixes
|
|
98
|
+
* the order.
|
|
99
|
+
*/
|
|
100
|
+
function guardedCosineDistance(args) {
|
|
101
|
+
return sql`CASE WHEN ${args.dimensions} = ${vectorSize(args.query)} THEN ${cosineDistance(args)} END`;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Embed items that have no current vector, one batch at a time, and store
|
|
105
|
+
* each batch before the next starts. `listMissing` returns the next items in
|
|
106
|
+
* the caller's order, after the last item of the previous page, so each run
|
|
107
|
+
* ends even when a write leaves an item missing. Text that trims to nothing
|
|
108
|
+
* is skipped, because the embedder rejects it. A provider failure stops the
|
|
109
|
+
* run and keeps the batches stored before it, so a later run continues.
|
|
110
|
+
*/
|
|
111
|
+
async function refreshEmbeddings(args) {
|
|
112
|
+
const now = args.now ?? Date.now;
|
|
113
|
+
let after;
|
|
114
|
+
let embedded = 0;
|
|
115
|
+
for (;;) {
|
|
116
|
+
const remaining = (args.maxItems ?? 1e400) - embedded;
|
|
117
|
+
if (remaining <= 0 || args.deadlineMs !== void 0 && now() >= args.deadlineMs) return {
|
|
118
|
+
done: false,
|
|
119
|
+
embedded
|
|
120
|
+
};
|
|
121
|
+
const page = await args.listMissing({
|
|
122
|
+
after,
|
|
123
|
+
limit: Math.min(args.batchSize, remaining)
|
|
124
|
+
});
|
|
125
|
+
after = page.at(-1);
|
|
126
|
+
if (!after) return {
|
|
127
|
+
done: true,
|
|
128
|
+
embedded
|
|
129
|
+
};
|
|
130
|
+
const items = page.filter((item) => item.text.trim() !== "");
|
|
131
|
+
if (items.length === 0) continue;
|
|
132
|
+
const batch = parseEmbeddingBatch(await args.embedder.embedTexts({
|
|
133
|
+
inputType: "document",
|
|
134
|
+
texts: items.map((item) => item.text)
|
|
135
|
+
}), {
|
|
136
|
+
...args.dimensions ? { dimensions: args.dimensions } : void 0,
|
|
137
|
+
expected: args.embedder,
|
|
138
|
+
textCount: items.length
|
|
139
|
+
});
|
|
140
|
+
await args.write(items.map((item, index) => ({
|
|
141
|
+
item,
|
|
142
|
+
vector: batch.vectors[index]
|
|
143
|
+
})), batch);
|
|
144
|
+
embedded += items.length;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
export { cosineDistance, embedText, embeddingHalfvec, embeddingVector, guardedCosineDistance, parseEmbeddingBatch, refreshEmbeddings };
|