@sentry/junior-plugin-api 0.310.0 → 0.312.0

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.
@@ -1,171 +0,0 @@
1
- /**
2
- * Shared parts of the derived embedding indexes. Each owner keeps its own
3
- * table, so a foreign key still deletes a vector with its source row. These
4
- * helpers keep the column types, the distance SQL, the batch checks, and the
5
- * backfill loop the same in every table.
6
- */
7
- import { v as PluginEmbedder, y as PluginEmbeddingInputType } from "./context-Buwf8ZyW.js";
8
- import { SQL } from "drizzle-orm";
9
- type PgvectorType = "vector" | "halfvec";
10
- /**
11
- * pgvector `vector` column without a fixed size, so a new embedding model
12
- * needs no migration. Each table records the size of each row in its own
13
- * column.
14
- */
15
- export declare const embeddingVector: {
16
- (): import("drizzle-orm/pg-core").PgCustomColumnBuilder<{
17
- name: "";
18
- dataType: 'custom';
19
- columnType: 'PgCustomColumn';
20
- data: number[];
21
- driverParam: string;
22
- enumValues: undefined;
23
- }>;
24
- <TConfig extends Record<string, any> & unknown>(fieldConfig?: TConfig | undefined): import("drizzle-orm/pg-core").PgCustomColumnBuilder<{
25
- name: "";
26
- dataType: 'custom';
27
- columnType: 'PgCustomColumn';
28
- data: number[];
29
- driverParam: string;
30
- enumValues: undefined;
31
- }>;
32
- <TName extends string>(dbName: TName, fieldConfig?: unknown): import("drizzle-orm/pg-core").PgCustomColumnBuilder<{
33
- name: TName;
34
- dataType: 'custom';
35
- columnType: 'PgCustomColumn';
36
- data: number[];
37
- driverParam: string;
38
- enumValues: undefined;
39
- }>;
40
- };
41
- /**
42
- * Half-precision `embeddingVector`: 2 bytes per value instead of 4, for
43
- * tables that hold many vectors.
44
- */
45
- export declare const embeddingHalfvec: {
46
- (): import("drizzle-orm/pg-core").PgCustomColumnBuilder<{
47
- name: "";
48
- dataType: 'custom';
49
- columnType: 'PgCustomColumn';
50
- data: number[];
51
- driverParam: string;
52
- enumValues: undefined;
53
- }>;
54
- <TConfig extends Record<string, any> & unknown>(fieldConfig?: TConfig | undefined): import("drizzle-orm/pg-core").PgCustomColumnBuilder<{
55
- name: "";
56
- dataType: 'custom';
57
- columnType: 'PgCustomColumn';
58
- data: number[];
59
- driverParam: string;
60
- enumValues: undefined;
61
- }>;
62
- <TName extends string>(dbName: TName, fieldConfig?: unknown): import("drizzle-orm/pg-core").PgCustomColumnBuilder<{
63
- name: TName;
64
- dataType: 'custom';
65
- columnType: 'PgCustomColumn';
66
- data: number[];
67
- driverParam: string;
68
- enumValues: undefined;
69
- }>;
70
- };
71
- /** One embedding vector and the model that made it. */
72
- export interface EmbeddingVector {
73
- model: string;
74
- provider: string;
75
- vector: number[];
76
- }
77
- /** Vectors of one embedder call, all of one size. */
78
- export interface EmbeddingBatch {
79
- dimensions: number;
80
- model: string;
81
- provider: string;
82
- vectors: number[][];
83
- }
84
- /**
85
- * Validate an embedder result: one vector per text, all of the reported
86
- * size. With `dimensions`, the size must be one that the table accepts. With
87
- * `expected`, the result must come from that provider and model, because a
88
- * backfill that finds rows by model would see a row of another model as
89
- * missing on every run.
90
- */
91
- export declare function parseEmbeddingBatch(raw: unknown, args: {
92
- dimensions?: readonly number[];
93
- expected?: {
94
- model: string;
95
- provider: string;
96
- };
97
- textCount: number;
98
- }): EmbeddingBatch;
99
- /** Embed one text with the embedder's model and return its vector. */
100
- export declare function embedText(embedder: PluginEmbedder, input: {
101
- inputType: PluginEmbeddingInputType;
102
- text: string;
103
- }): Promise<EmbeddingVector>;
104
- /**
105
- * Cosine distance from a stored vector to a query vector. Both sides are cast
106
- * to the query size, the form that an HNSW expression index on that size
107
- * matches. The cast fails on a stored vector of another size, so filter on the
108
- * size first, as the index predicate does, or use `guardedCosineDistance`.
109
- * A caller with another copy of drizzle-orm embeds the result in its own
110
- * `sql` template to get its own `SQL` type.
111
- */
112
- export declare function cosineDistance(args: {
113
- /**
114
- * Vector column of the caller's table. Not typed as a drizzle column: a
115
- * plugin can load another copy of drizzle-orm, whose column types do not
116
- * match this copy. Drizzle finds columns of any copy at run time.
117
- */
118
- column: unknown;
119
- query: readonly number[];
120
- type: PgvectorType;
121
- }): SQL<number>;
122
- /**
123
- * `cosineDistance`, or null for a stored vector of another size. Postgres
124
- * does not promise to check a WHERE term before it computes an ORDER BY or
125
- * SELECT expression, and pgvector fails on vectors of two sizes. CASE fixes
126
- * the order.
127
- */
128
- export declare function guardedCosineDistance(args: {
129
- /** Vector column, as in `cosineDistance`. */
130
- column: unknown;
131
- /** Column that stores the size of each vector. */
132
- dimensions: unknown;
133
- query: readonly number[];
134
- type: PgvectorType;
135
- }): SQL<number | null>;
136
- /** Outcome of one `refreshEmbeddings` run. */
137
- export interface EmbeddingRefresh {
138
- /** No missing item was left. False when the deadline or `maxItems` stopped the run. */
139
- done: boolean;
140
- embedded: number;
141
- }
142
- /**
143
- * Embed items that have no current vector, one batch at a time, and store
144
- * each batch before the next starts. `listMissing` returns the next items in
145
- * the caller's order, after the last item of the previous page, so each run
146
- * ends even when a write leaves an item missing. Text that trims to nothing
147
- * is skipped, because the embedder rejects it. A provider failure stops the
148
- * run and keeps the batches stored before it, so a later run continues.
149
- */
150
- export declare function refreshEmbeddings<TItem extends {
151
- text: string;
152
- }>(args: {
153
- batchSize: number;
154
- /** Do not start a batch at or after this time. */
155
- deadlineMs?: number | undefined;
156
- /** Vector sizes that the table accepts. */
157
- dimensions?: readonly number[];
158
- embedder: PluginEmbedder;
159
- listMissing(page: {
160
- after: TItem | undefined;
161
- limit: number;
162
- }): Promise<TItem[]>;
163
- /** Most items to embed in one run. */
164
- maxItems?: number;
165
- now?: () => number;
166
- write(rows: Array<{
167
- item: TItem;
168
- vector: number[];
169
- }>, batch: EmbeddingBatch): Promise<void>;
170
- }): Promise<EmbeddingRefresh>;
171
- export {};
@@ -1,147 +0,0 @@
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 };