@prisma-next/extension-pgvector 0.16.0-dev.3 → 0.16.0-dev.31
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/README.md +5 -5
- package/dist/{codec-types-CEqpE2q2.d.mts → codec-types-eUx62rq4.d.mts} +3 -2
- package/dist/codec-types-eUx62rq4.d.mts.map +1 -0
- package/dist/codec-types.d.mts +1 -1
- package/dist/column-types.d.mts +1 -1
- package/dist/column-types.d.mts.map +1 -1
- package/dist/column-types.mjs +10 -3
- package/dist/column-types.mjs.map +1 -1
- package/dist/control.mjs +7 -7
- package/dist/{descriptor-meta-G5zBc8J8.mjs → descriptor-meta-DMPKzaNe.mjs} +37 -15
- package/dist/descriptor-meta-DMPKzaNe.mjs.map +1 -0
- package/dist/errors-BnZLl-sM.mjs +19 -0
- package/dist/errors-BnZLl-sM.mjs.map +1 -0
- package/dist/pack.d.mts +1 -1
- package/dist/pack.mjs +1 -1
- package/dist/runtime.mjs +1 -1
- package/package.json +22 -21
- package/src/contract.d.ts +3 -3
- package/src/contract.json +3 -3
- package/src/core/codecs.ts +42 -13
- package/src/core/errors.ts +15 -0
- package/src/exports/column-types.ts +12 -2
- package/dist/codec-types-CEqpE2q2.d.mts.map +0 -1
- package/dist/constants-D8Ccy-f4.mjs +0 -13
- package/dist/constants-D8Ccy-f4.mjs.map +0 -1
- package/dist/descriptor-meta-G5zBc8J8.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ pnpm add @prisma-next/extension-pgvector
|
|
|
30
30
|
|
|
31
31
|
## Database Setup
|
|
32
32
|
|
|
33
|
-
The pgvector extension ships an on-disk baseline migration in its contract space; applying that migration installs pgvector with `CREATE EXTENSION IF NOT EXISTS vector`. When the extension is composed into an application via `
|
|
33
|
+
The pgvector extension ships an on-disk baseline migration in its contract space; applying that migration installs pgvector with `CREATE EXTENSION IF NOT EXISTS vector`. When the extension is composed into an application via `extensions`, `prisma-next db init` and `prisma-next db update` apply the baseline (and any subsequent migrations) automatically.
|
|
34
34
|
|
|
35
35
|
For manual database setup, the equivalent DDL is:
|
|
36
36
|
|
|
@@ -55,7 +55,7 @@ export default defineConfig({
|
|
|
55
55
|
family: sql,
|
|
56
56
|
target: postgres,
|
|
57
57
|
adapter: postgresAdapter,
|
|
58
|
-
|
|
58
|
+
extensions: [pgvector],
|
|
59
59
|
});
|
|
60
60
|
```
|
|
61
61
|
|
|
@@ -76,7 +76,7 @@ import postgres from '@prisma-next/target-postgres/pack';
|
|
|
76
76
|
export const contract = defineContract({
|
|
77
77
|
family: sqlFamily,
|
|
78
78
|
target: postgres,
|
|
79
|
-
|
|
79
|
+
extensions: { pgvector },
|
|
80
80
|
models: {
|
|
81
81
|
Post: model('Post', {
|
|
82
82
|
fields: {
|
|
@@ -106,7 +106,7 @@ import pgvector from '@prisma-next/extension-pgvector/runtime';
|
|
|
106
106
|
const stack = createSqlExecutionStack({
|
|
107
107
|
target: postgresTarget,
|
|
108
108
|
adapter: postgresAdapter,
|
|
109
|
-
|
|
109
|
+
extensions: [pgvector],
|
|
110
110
|
});
|
|
111
111
|
const context = createExecutionContext({ contract, stack });
|
|
112
112
|
const stackInstance = instantiateExecutionStack(stack);
|
|
@@ -200,7 +200,7 @@ The extension declares the following capabilities:
|
|
|
200
200
|
|
|
201
201
|
## Authoring (maintainers)
|
|
202
202
|
|
|
203
|
-
After changing the contract source, run `pnpm migrations:regen` from the repo root to keep migration metadata, `refs/head.json`, and `
|
|
203
|
+
After changing the contract source, run `pnpm migrations:regen` from the repo root to keep migration metadata, `refs/head.json`, and the `migrations/snapshots/<hex>/contract.*` store entries consistent with the freshly-built `src/contract.json`; it is also wired into `pnpm fixtures:emit` automatically.
|
|
204
204
|
|
|
205
205
|
See [ADR 212 — Contract spaces](../../../docs/architecture%20docs/adrs/ADR%20212%20-%20Contract%20spaces.md) ("Contract-space package layout") for the layout and rationale.
|
|
206
206
|
|
|
@@ -9,13 +9,14 @@ import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
|
9
9
|
declare const VECTOR_CODEC_ID: "pg/vector@1";
|
|
10
10
|
//#endregion
|
|
11
11
|
//#region src/core/codecs.d.ts
|
|
12
|
+
type VectorConversionCode = 'RUNTIME.ENCODE_FAILED' | 'RUNTIME.DECODE_FAILED';
|
|
12
13
|
type VectorParams = {
|
|
13
14
|
readonly length: number;
|
|
14
15
|
};
|
|
15
16
|
declare class PgVectorCodec extends CodecImpl<typeof VECTOR_CODEC_ID, readonly ['equality'], string, number[]> {
|
|
16
17
|
readonly length: number;
|
|
17
18
|
constructor(descriptor: AnyCodecDescriptor, length: number);
|
|
18
|
-
assertVector(value: unknown): asserts value is number[];
|
|
19
|
+
assertVector(value: unknown, code: VectorConversionCode): asserts value is number[];
|
|
19
20
|
encode(value: number[], _ctx: CodecCallContext): Promise<string>;
|
|
20
21
|
decode(wire: string, _ctx: CodecCallContext): Promise<number[]>;
|
|
21
22
|
encodeJson(value: number[]): JsonValue;
|
|
@@ -56,4 +57,4 @@ type Vector<N extends number = number> = number[] & {
|
|
|
56
57
|
type CodecTypes = CodecTypes$1;
|
|
57
58
|
//#endregion
|
|
58
59
|
export { Vector as n, CodecTypes as t };
|
|
59
|
-
//# sourceMappingURL=codec-types-
|
|
60
|
+
//# sourceMappingURL=codec-types-eUx62rq4.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec-types-eUx62rq4.d.mts","names":[],"sources":["../src/core/constants.ts","../src/core/codecs.ts","../src/types/codec-types.ts"],"mappings":";;;;;;;;cAGa;;;KC0BR;KAEA;WAA0B;;cA+ClB,sBAAsB,iBAC1B;WAKE;cAEG,YAAY,oBAAoB;EAK5C,aAAa,gBAAgB,MAAM,+BAA+B;EAyB5D,OAAO,iBAAiB,MAAM,mBAAmB;EAKjD,OAAO,cAAc,MAAM,mBAAmB;EAWpD,WAAW,kBAAkB;EAK7B,WAAW,MAAM;;cAYN,2BAA2B,oBAAoB;WACxC;WACA;WACA;WACA;;;;;;;;;WACA,cAAc,iBAAiB;EACxC,iBAAiB,QAAQ;EAGzB,QAAQ,QAAQ,gBAAgB,KAAK,yBAAyB;;cAkBnE;mBAAA;;KAIM,eAAa,yBAAyB;;;;;;;;;KCnKtC,OAAO;WAAmD,iBAAiB;;KAE3E,aAAa"}
|
package/dist/codec-types.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as Vector, t as CodecTypes } from "./codec-types-
|
|
1
|
+
import { n as Vector, t as CodecTypes } from "./codec-types-eUx62rq4.mjs";
|
|
2
2
|
export type { CodecTypes, Vector };
|
package/dist/column-types.d.mts
CHANGED
|
@@ -10,7 +10,7 @@ import { ColumnTypeDescriptor } from "@prisma-next/framework-components/codec";
|
|
|
10
10
|
* ```
|
|
11
11
|
* @param length - The dimension of the vector (e.g., 1536 for OpenAI embeddings)
|
|
12
12
|
* @returns A column type descriptor with `typeParams.length` set
|
|
13
|
-
* @throws
|
|
13
|
+
* @throws `CONTRACT.ARGUMENT_INVALID` if length is not an integer in the range [1, VECTOR_MAX_DIM]
|
|
14
14
|
*/
|
|
15
15
|
declare function vector<N extends number>(length: N): ColumnTypeDescriptor & {
|
|
16
16
|
readonly typeParams: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"column-types.d.mts","names":[],"sources":["../src/exports/column-types.ts"],"mappings":";;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"column-types.d.mts","names":[],"sources":["../src/exports/column-types.ts"],"mappings":";;;;;;;;;;;;;;iBAoBgB,OAAO,kBACrB,QAAQ,IACP;WAAkC;aAAuB,QAAQ"}
|
package/dist/column-types.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as VECTOR_MAX_DIM, t as
|
|
1
|
+
import { n as VECTOR_CODEC_ID, r as VECTOR_MAX_DIM, t as pgVectorError } from "./errors-BnZLl-sM.mjs";
|
|
2
2
|
//#region src/exports/column-types.ts
|
|
3
3
|
/**
|
|
4
4
|
* Factory for creating dimensioned vector column descriptors.
|
|
@@ -10,10 +10,17 @@ import { n as VECTOR_MAX_DIM, t as VECTOR_CODEC_ID } from "./constants-D8Ccy-f4.
|
|
|
10
10
|
* ```
|
|
11
11
|
* @param length - The dimension of the vector (e.g., 1536 for OpenAI embeddings)
|
|
12
12
|
* @returns A column type descriptor with `typeParams.length` set
|
|
13
|
-
* @throws
|
|
13
|
+
* @throws `CONTRACT.ARGUMENT_INVALID` if length is not an integer in the range [1, VECTOR_MAX_DIM]
|
|
14
14
|
*/
|
|
15
15
|
function vector(length) {
|
|
16
|
-
if (!Number.isInteger(length) || length < 1 || length > 16e3) throw
|
|
16
|
+
if (!Number.isInteger(length) || length < 1 || length > 16e3) throw pgVectorError("CONTRACT.ARGUMENT_INVALID", `pgvector: dimension must be an integer in [1, ${VECTOR_MAX_DIM}], got ${length}`, {
|
|
17
|
+
fix: `Pass an integer dimension between 1 and ${VECTOR_MAX_DIM}, e.g. vector(1536).`,
|
|
18
|
+
meta: {
|
|
19
|
+
helperPath: "vector",
|
|
20
|
+
expected: `an integer in [1, ${VECTOR_MAX_DIM}]`,
|
|
21
|
+
received: length
|
|
22
|
+
}
|
|
23
|
+
});
|
|
17
24
|
return {
|
|
18
25
|
codecId: VECTOR_CODEC_ID,
|
|
19
26
|
nativeType: "vector",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"column-types.mjs","names":[],"sources":["../src/exports/column-types.ts"],"sourcesContent":["/**\n * Column type descriptor factory for pgvector extension. `vector(N)` is the canonical authoring surface; every pgvector column must declare a dimension via this factory. The dimension threads into the runtime codec through `paramsSchema.length` and into the DDL via the family-layer `expandNativeType` hook (e.g. `vector(1536)`).\n */\n\nimport type { ColumnTypeDescriptor } from '@prisma-next/framework-components/codec';\nimport { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from '../core/constants';\n\n/**\n * Factory for creating dimensioned vector column descriptors.\n *\n * @example\n * ```typescript\n * .column('embedding', { type: vector(1536), nullable: false })\n * // Produces: nativeType: 'vector', typeParams: { length: 1536 }\n * ```\n * @param length - The dimension of the vector (e.g., 1536 for OpenAI embeddings)\n * @returns A column type descriptor with `typeParams.length` set\n * @throws
|
|
1
|
+
{"version":3,"file":"column-types.mjs","names":[],"sources":["../src/exports/column-types.ts"],"sourcesContent":["/**\n * Column type descriptor factory for pgvector extension. `vector(N)` is the canonical authoring surface; every pgvector column must declare a dimension via this factory. The dimension threads into the runtime codec through `paramsSchema.length` and into the DDL via the family-layer `expandNativeType` hook (e.g. `vector(1536)`).\n */\n\nimport type { ColumnTypeDescriptor } from '@prisma-next/framework-components/codec';\nimport { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from '../core/constants';\nimport { pgVectorError } from '../core/errors';\n\n/**\n * Factory for creating dimensioned vector column descriptors.\n *\n * @example\n * ```typescript\n * .column('embedding', { type: vector(1536), nullable: false })\n * // Produces: nativeType: 'vector', typeParams: { length: 1536 }\n * ```\n * @param length - The dimension of the vector (e.g., 1536 for OpenAI embeddings)\n * @returns A column type descriptor with `typeParams.length` set\n * @throws `CONTRACT.ARGUMENT_INVALID` if length is not an integer in the range [1, VECTOR_MAX_DIM]\n */\nexport function vector<N extends number>(\n length: N,\n): ColumnTypeDescriptor & { readonly typeParams: { readonly length: N } } {\n if (!Number.isInteger(length) || length < 1 || length > VECTOR_MAX_DIM) {\n throw pgVectorError(\n 'CONTRACT.ARGUMENT_INVALID',\n `pgvector: dimension must be an integer in [1, ${VECTOR_MAX_DIM}], got ${length}`,\n {\n fix: `Pass an integer dimension between 1 and ${VECTOR_MAX_DIM}, e.g. vector(1536).`,\n meta: {\n helperPath: 'vector',\n expected: `an integer in [1, ${VECTOR_MAX_DIM}]`,\n received: length,\n },\n },\n );\n }\n return {\n codecId: VECTOR_CODEC_ID,\n nativeType: 'vector',\n typeParams: { length },\n } as const;\n}\n"],"mappings":";;;;;;;;;;;;;;AAoBA,SAAgB,OACd,QACwE;CACxE,IAAI,CAAC,OAAO,UAAU,MAAM,KAAK,SAAS,KAAK,SAAA,MAC7C,MAAM,cACJ,6BACA,iDAAiD,eAAe,SAAS,UACzE;EACE,KAAK,2CAA2C,eAAe;EAC/D,MAAM;GACJ,YAAY;GACZ,UAAU,qBAAqB,eAAe;GAC9C,UAAU;EACZ;CACF,CACF;CAEF,OAAO;EACL,SAAS;EACT,YAAY;EACZ,YAAY,EAAE,OAAO;CACvB;AACF"}
|
package/dist/control.mjs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { n as pgvectorQueryOperations, t as pgvectorPackMeta } from "./descriptor-meta-
|
|
1
|
+
import { n as pgvectorQueryOperations, t as pgvectorPackMeta } from "./descriptor-meta-DMPKzaNe.mjs";
|
|
2
2
|
import { contractSpaceFromJson } from "@prisma-next/migration-tools/spaces";
|
|
3
3
|
//#region migrations/20260601T0000_install_vector_extension/migration.json
|
|
4
4
|
var migration_default = {
|
|
5
5
|
from: null,
|
|
6
|
-
to: "
|
|
6
|
+
to: "3d2c56a2944685bd21b05bc8a8d73164397df51c014201902932fbe7e80ff1b8",
|
|
7
7
|
providedInvariants: ["pgvector:install-vector-v1"],
|
|
8
8
|
createdAt: "2026-06-01T00:00:00.000Z",
|
|
9
|
-
migrationHash: "
|
|
9
|
+
migrationHash: "12b0b77e65c4899ba04e88ed040b1cb3390b4b18a889460f8a83cb6047ff1fa5"
|
|
10
10
|
};
|
|
11
11
|
//#endregion
|
|
12
12
|
//#region migrations/20260601T0000_install_vector_extension/ops.json
|
|
@@ -41,7 +41,7 @@ var ops_default = [{
|
|
|
41
41
|
//#endregion
|
|
42
42
|
//#region migrations/refs/head.json
|
|
43
43
|
var head_default = {
|
|
44
|
-
hash: "
|
|
44
|
+
hash: "3d2c56a2944685bd21b05bc8a8d73164397df51c014201902932fbe7e80ff1b8",
|
|
45
45
|
invariants: ["pgvector:install-vector-v1"]
|
|
46
46
|
};
|
|
47
47
|
//#endregion
|
|
@@ -50,7 +50,7 @@ var contract_default = {
|
|
|
50
50
|
schemaVersion: "1",
|
|
51
51
|
targetFamily: "sql",
|
|
52
52
|
target: "postgres",
|
|
53
|
-
profileHash: "
|
|
53
|
+
profileHash: "3916f444a8a17ad749191acf9e08dad97d1a327b88c2f1d45d12f240296aa8b2",
|
|
54
54
|
roots: {},
|
|
55
55
|
domain: { "namespaces": { "public": { "models": {} } } },
|
|
56
56
|
storage: {
|
|
@@ -59,7 +59,7 @@ var contract_default = {
|
|
|
59
59
|
"id": "public",
|
|
60
60
|
"kind": "postgres-schema"
|
|
61
61
|
} },
|
|
62
|
-
"storageHash": "
|
|
62
|
+
"storageHash": "3d2c56a2944685bd21b05bc8a8d73164397df51c014201902932fbe7e80ff1b8",
|
|
63
63
|
"types": { "vector": {
|
|
64
64
|
"codecId": "pg/vector@1",
|
|
65
65
|
"kind": "codec-instance",
|
|
@@ -83,7 +83,7 @@ var contract_default = {
|
|
|
83
83
|
"scalarList": true
|
|
84
84
|
}
|
|
85
85
|
},
|
|
86
|
-
|
|
86
|
+
extensions: {},
|
|
87
87
|
meta: {},
|
|
88
88
|
_generated: {
|
|
89
89
|
"warning": "⚠️ GENERATED FILE - DO NOT EDIT",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as VECTOR_MAX_DIM, t as
|
|
1
|
+
import { n as VECTOR_CODEC_ID, r as VECTOR_MAX_DIM, t as pgVectorError } from "./errors-BnZLl-sM.mjs";
|
|
2
2
|
import { buildOperation, codecOf, toExpr } from "@prisma-next/sql-relational-core/expression";
|
|
3
3
|
import { buildCodecDescriptorRegistry } from "@prisma-next/sql-relational-core/codec-descriptor-registry";
|
|
4
4
|
import { CodecDescriptorImpl, CodecImpl } from "@prisma-next/framework-components/codec";
|
|
@@ -32,11 +32,23 @@ const vectorParamsSchema = type({ length: "number" }).narrow((params, ctx) => {
|
|
|
32
32
|
});
|
|
33
33
|
const PG_VECTOR_META = { db: { sql: { postgres: { nativeType: "vector" } } } };
|
|
34
34
|
function parseVector(value) {
|
|
35
|
-
if (!value.startsWith("[") || !value.endsWith("]")) throw
|
|
35
|
+
if (!value.startsWith("[") || !value.endsWith("]")) throw pgVectorError("RUNTIME.DECODE_FAILED", `Invalid vector format: expected "[...]", got "${value}"`, {
|
|
36
|
+
why: "The database returned a vector value that is not in the PostgreSQL \"[x,y,z]\" text format.",
|
|
37
|
+
meta: {
|
|
38
|
+
codecId: VECTOR_CODEC_ID,
|
|
39
|
+
wirePreview: value
|
|
40
|
+
}
|
|
41
|
+
});
|
|
36
42
|
const content = value.slice(1, -1).trim();
|
|
37
43
|
return content === "" ? [] : content.split(",").map((entry) => {
|
|
38
44
|
const number = Number.parseFloat(entry.trim());
|
|
39
|
-
if (Number.isNaN(number)) throw
|
|
45
|
+
if (Number.isNaN(number)) throw pgVectorError("RUNTIME.DECODE_FAILED", `Invalid vector value: "${entry}" is not a number`, {
|
|
46
|
+
why: "A vector entry returned by the database could not be parsed as a number.",
|
|
47
|
+
meta: {
|
|
48
|
+
codecId: VECTOR_CODEC_ID,
|
|
49
|
+
wirePreview: value
|
|
50
|
+
}
|
|
51
|
+
});
|
|
40
52
|
return number;
|
|
41
53
|
});
|
|
42
54
|
}
|
|
@@ -46,32 +58,42 @@ var PgVectorCodec = class extends CodecImpl {
|
|
|
46
58
|
super(descriptor);
|
|
47
59
|
this.length = length;
|
|
48
60
|
}
|
|
49
|
-
assertVector(value) {
|
|
50
|
-
|
|
61
|
+
assertVector(value, code) {
|
|
62
|
+
const meta = {
|
|
63
|
+
codecId: VECTOR_CODEC_ID,
|
|
64
|
+
expectedLength: this.length
|
|
65
|
+
};
|
|
66
|
+
if (!Array.isArray(value)) throw pgVectorError(code, "Vector value must be an array of numbers", { meta });
|
|
51
67
|
for (const element of value) {
|
|
52
|
-
if (typeof element !== "number") throw
|
|
53
|
-
if (!Number.isFinite(element)) throw
|
|
68
|
+
if (typeof element !== "number") throw pgVectorError(code, "Vector value must contain only numbers", { meta });
|
|
69
|
+
if (!Number.isFinite(element)) throw pgVectorError(code, "Vector value must contain only finite numbers", { meta });
|
|
54
70
|
}
|
|
55
|
-
if (value.length !== this.length) throw
|
|
71
|
+
if (value.length !== this.length) throw pgVectorError(code, `Vector length mismatch: expected ${this.length}, got ${value.length}`, {
|
|
72
|
+
why: `This column is declared as vector(${this.length}); every value must have exactly that many dimensions.`,
|
|
73
|
+
meta: {
|
|
74
|
+
...meta,
|
|
75
|
+
receivedLength: value.length
|
|
76
|
+
}
|
|
77
|
+
});
|
|
56
78
|
}
|
|
57
79
|
async encode(value, _ctx) {
|
|
58
|
-
this.assertVector(value);
|
|
80
|
+
this.assertVector(value, "RUNTIME.ENCODE_FAILED");
|
|
59
81
|
return `[${value.join(",")}]`;
|
|
60
82
|
}
|
|
61
83
|
async decode(wire, _ctx) {
|
|
62
|
-
if (typeof wire !== "string") throw
|
|
84
|
+
if (typeof wire !== "string") throw pgVectorError("RUNTIME.DECODE_FAILED", "Vector wire value must be a string", { meta: { codecId: VECTOR_CODEC_ID } });
|
|
63
85
|
const value = parseVector(wire);
|
|
64
|
-
this.assertVector(value);
|
|
86
|
+
this.assertVector(value, "RUNTIME.DECODE_FAILED");
|
|
65
87
|
return value;
|
|
66
88
|
}
|
|
67
89
|
encodeJson(value) {
|
|
68
|
-
this.assertVector(value);
|
|
90
|
+
this.assertVector(value, "RUNTIME.ENCODE_FAILED");
|
|
69
91
|
return `[${value.join(",")}]`;
|
|
70
92
|
}
|
|
71
93
|
decodeJson(json) {
|
|
72
|
-
if (typeof json !== "string") throw
|
|
94
|
+
if (typeof json !== "string") throw pgVectorError("RUNTIME.DECODE_FAILED", "Vector database JSON value must be a string", { meta: { codecId: VECTOR_CODEC_ID } });
|
|
73
95
|
const value = parseVector(json);
|
|
74
|
-
this.assertVector(value);
|
|
96
|
+
this.assertVector(value, "RUNTIME.DECODE_FAILED");
|
|
75
97
|
return value;
|
|
76
98
|
}
|
|
77
99
|
};
|
|
@@ -185,4 +207,4 @@ const pgvectorPackMeta = {
|
|
|
185
207
|
//#endregion
|
|
186
208
|
export { pgvectorQueryOperations as n, pgvectorCodecRegistry as r, pgvectorPackMeta as t };
|
|
187
209
|
|
|
188
|
-
//# sourceMappingURL=descriptor-meta-
|
|
210
|
+
//# sourceMappingURL=descriptor-meta-DMPKzaNe.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"descriptor-meta-DMPKzaNe.mjs","names":["arktype"],"sources":["../src/core/authoring.ts","../src/core/codecs.ts","../src/core/registry.ts","../src/core/descriptor-meta.ts"],"sourcesContent":["import type { AuthoringTypeNamespace } from '@prisma-next/framework-components/authoring';\nimport { VECTOR_MAX_DIM } from './constants';\n\nexport const pgvectorAuthoringTypes = {\n pgvector: {\n Vector: {\n kind: 'typeConstructor',\n args: [\n { kind: 'number', name: 'length', integer: true, minimum: 1, maximum: VECTOR_MAX_DIM },\n ],\n output: {\n codecId: 'pg/vector@1',\n nativeType: 'vector',\n typeParams: {\n length: { kind: 'arg', index: 0 },\n },\n },\n },\n },\n} as const satisfies AuthoringTypeNamespace;\n","/**\n * pgvector extension codec.\n *\n * Mirrors the patterns in `postgres/codecs-class.ts` and `sqlite/codecs-class.ts` for the single `pg/vector@1` codec. Three artifacts:\n *\n * 1. `PgVectorCodec` extends {@link CodecImpl} with the runtime encode/decode/encodeJson/decodeJson conversions inline. Conversions are simple enough (PostgreSQL `[1,2,3]` text format) that no shared helper module is warranted; the class body is the source of truth.\n * 2. `PgVectorDescriptor` extends {@link CodecDescriptorImpl} with the codec id, traits, target types, params schema (`{ length: number }`, validated against {@link VECTOR_MAX_DIM}), `meta` (postgres `nativeType: 'vector'`), and the emit-path `renderOutputType` producing `Vector<${length}>`.\n * 3. `pgVectorColumn(length)` per-codec column helper invoking `descriptor.factory({ length })` directly + passing the bare `nativeType: 'vector'`. The family-layer {@link expandNativeType} hook renders the parameterized form (`vector(1536)`) at emit/verify time from `nativeType` + `typeParams`.\n *\n * `length` threads into the runtime codec via the constructor so encode/decode/encodeJson/decodeJson enforce the declared dimension at every ingress path. Without this, `vector(3)` and `vector(1536)` would produce codecs with identical behaviour and a dimension-mismatched value would round-trip undetected.\n */\n\nimport type { JsonValue } from '@prisma-next/contract/types';\nimport {\n type AnyCodecDescriptor,\n type CodecCallContext,\n CodecDescriptorImpl,\n CodecImpl,\n type CodecInstanceContext,\n type ColumnHelperFor,\n type ColumnHelperForStrict,\n column,\n} from '@prisma-next/framework-components/codec';\nimport type { ExtractCodecTypes } from '@prisma-next/sql-relational-core/ast';\nimport type { StandardSchemaV1 } from '@standard-schema/spec';\nimport { type as arktype } from 'arktype';\nimport { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from './constants';\nimport { pgVectorError } from './errors';\n\ntype VectorConversionCode = 'RUNTIME.ENCODE_FAILED' | 'RUNTIME.DECODE_FAILED';\n\ntype VectorParams = { readonly length: number };\n\nconst vectorParamsSchema = arktype({\n length: 'number',\n}).narrow((params, ctx) => {\n const { length } = params;\n if (!Number.isInteger(length)) {\n return ctx.mustBe('an integer');\n }\n if (length < 1 || length > VECTOR_MAX_DIM) {\n return ctx.mustBe(`in the range [1, ${VECTOR_MAX_DIM}]`);\n }\n return true;\n}) satisfies StandardSchemaV1<VectorParams>;\n\nconst PG_VECTOR_META = { db: { sql: { postgres: { nativeType: 'vector' } } } } as const;\n\nfunction parseVector(value: string): number[] {\n if (!value.startsWith('[') || !value.endsWith(']')) {\n throw pgVectorError(\n 'RUNTIME.DECODE_FAILED',\n `Invalid vector format: expected \"[...]\", got \"${value}\"`,\n {\n why: 'The database returned a vector value that is not in the PostgreSQL \"[x,y,z]\" text format.',\n meta: { codecId: VECTOR_CODEC_ID, wirePreview: value },\n },\n );\n }\n const content = value.slice(1, -1).trim();\n return content === ''\n ? []\n : content.split(',').map((entry) => {\n const number = Number.parseFloat(entry.trim());\n if (Number.isNaN(number)) {\n throw pgVectorError(\n 'RUNTIME.DECODE_FAILED',\n `Invalid vector value: \"${entry}\" is not a number`,\n {\n why: 'A vector entry returned by the database could not be parsed as a number.',\n meta: { codecId: VECTOR_CODEC_ID, wirePreview: value },\n },\n );\n }\n return number;\n });\n}\n\nexport class PgVectorCodec extends CodecImpl<\n typeof VECTOR_CODEC_ID,\n readonly ['equality'],\n string,\n number[]\n> {\n readonly length: number;\n\n constructor(descriptor: AnyCodecDescriptor, length: number) {\n super(descriptor);\n this.length = length;\n }\n\n assertVector(value: unknown, code: VectorConversionCode): asserts value is number[] {\n const meta = { codecId: VECTOR_CODEC_ID, expectedLength: this.length };\n if (!Array.isArray(value)) {\n throw pgVectorError(code, 'Vector value must be an array of numbers', { meta });\n }\n for (const element of value) {\n if (typeof element !== 'number') {\n throw pgVectorError(code, 'Vector value must contain only numbers', { meta });\n }\n if (!Number.isFinite(element)) {\n throw pgVectorError(code, 'Vector value must contain only finite numbers', { meta });\n }\n }\n if (value.length !== this.length) {\n throw pgVectorError(\n code,\n `Vector length mismatch: expected ${this.length}, got ${value.length}`,\n {\n why: `This column is declared as vector(${this.length}); every value must have exactly that many dimensions.`,\n meta: { ...meta, receivedLength: value.length },\n },\n );\n }\n }\n\n async encode(value: number[], _ctx: CodecCallContext): Promise<string> {\n this.assertVector(value, 'RUNTIME.ENCODE_FAILED');\n return `[${value.join(',')}]`;\n }\n\n async decode(wire: string, _ctx: CodecCallContext): Promise<number[]> {\n if (typeof wire !== 'string') {\n throw pgVectorError('RUNTIME.DECODE_FAILED', 'Vector wire value must be a string', {\n meta: { codecId: VECTOR_CODEC_ID },\n });\n }\n const value = parseVector(wire);\n this.assertVector(value, 'RUNTIME.DECODE_FAILED');\n return value;\n }\n\n encodeJson(value: number[]): JsonValue {\n this.assertVector(value, 'RUNTIME.ENCODE_FAILED');\n return `[${value.join(',')}]`;\n }\n\n decodeJson(json: JsonValue): number[] {\n if (typeof json !== 'string') {\n throw pgVectorError('RUNTIME.DECODE_FAILED', 'Vector database JSON value must be a string', {\n meta: { codecId: VECTOR_CODEC_ID },\n });\n }\n const value = parseVector(json);\n this.assertVector(value, 'RUNTIME.DECODE_FAILED');\n return value;\n }\n}\n\nexport class PgVectorDescriptor extends CodecDescriptorImpl<VectorParams> {\n override readonly codecId = VECTOR_CODEC_ID;\n override readonly traits = ['equality'] as const;\n override readonly targetTypes = ['vector'] as const;\n override readonly meta = PG_VECTOR_META;\n override readonly paramsSchema: StandardSchemaV1<VectorParams> = vectorParamsSchema;\n override renderOutputType(params: VectorParams): string {\n return `Vector<${params.length}>`;\n }\n override factory(params: VectorParams): (ctx: CodecInstanceContext) => PgVectorCodec {\n return () => new PgVectorCodec(this, params.length);\n }\n}\n\nexport const pgVectorDescriptor = new PgVectorDescriptor();\n\n/**\n * Per-codec column helper for `pg/vector@1`. Generic over `N extends number` so the column site preserves the dimension literal in `typeParams` (e.g. `pgVectorColumn(1536)` packs `typeParams: { length: 1536 }`).\n *\n * Passes the bare `nativeType: 'vector'`; the family-layer `expandNativeType` hook renders the parameterized form (`vector(1536)`) at emit/verify time from `nativeType` + `typeParams`.\n */\nexport const pgVectorColumn = <N extends number>(length: N) =>\n column(pgVectorDescriptor.factory({ length }), pgVectorDescriptor.codecId, { length }, 'vector');\n\npgVectorColumn satisfies ColumnHelperFor<PgVectorDescriptor>;\npgVectorColumn satisfies ColumnHelperForStrict<PgVectorDescriptor>;\n\nconst codecDescriptorMap = {\n vector: pgVectorDescriptor,\n} as const;\n\nexport type CodecTypes = ExtractCodecTypes<typeof codecDescriptorMap>;\n\nexport const codecDescriptors: readonly AnyCodecDescriptor[] = Object.values(codecDescriptorMap);\n","import { buildCodecDescriptorRegistry } from '@prisma-next/sql-relational-core/codec-descriptor-registry';\nimport type { CodecDescriptorRegistry } from '@prisma-next/sql-relational-core/query-lane-context';\nimport { codecDescriptors } from './codecs';\n\n/**\n * Registry of every codec descriptor shipped by `@prisma-next/extension-pgvector`.\n *\n * Public consumer surface for the pgvector codec set. Currently a single entry (`pg/vector@1`); the registry shape stays consistent with the other codec-shipping packages so consumers don't need to special-case extensions. See ADR 208.\n */\nexport const pgvectorCodecRegistry: CodecDescriptorRegistry =\n buildCodecDescriptorRegistry(codecDescriptors);\n","import {\n buildOperation,\n type CodecExpression,\n codecOf,\n type Expression,\n toExpr,\n} from '@prisma-next/sql-relational-core/expression';\nimport type { CodecTypes } from '../types/codec-types';\nimport type { QueryOperationTypes } from '../types/operation-types';\nimport { pgvectorAuthoringTypes } from './authoring';\nimport { pgvectorCodecRegistry } from './registry';\n\nconst pgvectorTypeId = 'pg/vector@1' as const;\n\ntype CodecTypesBase = Record<string, { readonly input: unknown; readonly output: unknown }>;\n\nexport function pgvectorQueryOperations<CT extends CodecTypesBase>(): QueryOperationTypes<CT> {\n return {\n cosineDistance: {\n self: { codecId: pgvectorTypeId },\n impl: (\n self: CodecExpression<'pg/vector@1', boolean, CT>,\n other: CodecExpression<'pg/vector@1', boolean, CT>,\n ): Expression<{ codecId: 'pg/float8@1'; nullable: false }> => {\n const selfCodec = codecOf(self);\n return buildOperation({\n method: 'cosineDistance',\n args: [toExpr(self, selfCodec), toExpr(other, selfCodec)],\n returns: { codecId: 'pg/float8@1', nullable: false },\n lowering: {\n targetFamily: 'sql',\n strategy: 'function',\n template: '{{self}} <=> {{arg0}}',\n },\n });\n },\n },\n cosineSimilarity: {\n self: { codecId: pgvectorTypeId },\n impl: (\n self: CodecExpression<'pg/vector@1', boolean, CT>,\n other: CodecExpression<'pg/vector@1', boolean, CT>,\n ): Expression<{ codecId: 'pg/float8@1'; nullable: false }> => {\n const selfCodec = codecOf(self);\n return buildOperation({\n method: 'cosineSimilarity',\n args: [toExpr(self, selfCodec), toExpr(other, selfCodec)],\n returns: { codecId: 'pg/float8@1', nullable: false },\n lowering: {\n targetFamily: 'sql',\n strategy: 'function',\n template: '1 - ({{self}} <=> {{arg0}})',\n },\n });\n },\n },\n };\n}\n\nconst pgvectorPackMetaBase = {\n kind: 'extension',\n id: 'pgvector',\n familyId: 'sql',\n targetId: 'postgres',\n version: '0.0.1',\n capabilities: {\n postgres: {\n 'pgvector.cosine': true,\n },\n },\n authoring: {\n type: pgvectorAuthoringTypes,\n },\n types: {\n codecTypes: {\n codecDescriptors: Array.from(pgvectorCodecRegistry.values()),\n import: {\n package: '@prisma-next/extension-pgvector/codec-types',\n named: 'CodecTypes',\n alias: 'PgVectorTypes',\n },\n typeImports: [\n {\n package: '@prisma-next/extension-pgvector/codec-types',\n named: 'Vector',\n alias: 'Vector',\n },\n ],\n },\n operationTypes: {\n import: {\n package: '@prisma-next/extension-pgvector/operation-types',\n named: 'OperationTypes',\n alias: 'PgVectorOperationTypes',\n },\n },\n queryOperationTypes: {\n import: {\n package: '@prisma-next/extension-pgvector/operation-types',\n named: 'QueryOperationTypes',\n alias: 'PgVectorQueryOperationTypes',\n },\n },\n storage: [\n { typeId: pgvectorTypeId, familyId: 'sql', targetId: 'postgres', nativeType: 'vector' },\n ],\n },\n} as const;\n\nexport const pgvectorPackMeta: typeof pgvectorPackMetaBase & {\n readonly __codecTypes?: CodecTypes;\n} = pgvectorPackMetaBase;\n"],"mappings":";;;;;;AAGA,MAAa,yBAAyB,EACpC,UAAU,EACR,QAAQ;CACN,MAAM;CACN,MAAM,CACJ;EAAE,MAAM;EAAU,MAAM;EAAU,SAAS;EAAM,SAAS;EAAG,SAAS;CAAe,CACvF;CACA,QAAQ;EACN,SAAS;EACT,YAAY;EACZ,YAAY,EACV,QAAQ;GAAE,MAAM;GAAO,OAAO;EAAE,EAClC;CACF;AACF,EACF,EACF;;;ACcA,MAAM,qBAAqBA,KAAQ,EACjC,QAAQ,SACV,CAAC,CAAC,CAAC,QAAQ,QAAQ,QAAQ;CACzB,MAAM,EAAE,WAAW;CACnB,IAAI,CAAC,OAAO,UAAU,MAAM,GAC1B,OAAO,IAAI,OAAO,YAAY;CAEhC,IAAI,SAAS,KAAK,SAAA,MAChB,OAAO,IAAI,OAAO,oBAAoB,eAAe,EAAE;CAEzD,OAAO;AACT,CAAC;AAED,MAAM,iBAAiB,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,SAAS,EAAE,EAAE,EAAE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AO,MAAM,SAAS,GAAG,OAAO,OAAO,SAAS,CAAC;KACxD,SAAS;MAAE,SAAS;MAAe,UAAU;KAAM;KACnD,UAAU;MACR,cAAc;MACd,UAAU;MACV,UAAU;KACZ;IACF,CAAC;GACH;EACF;EACA,kBAAkB;GAChB,MAAM,EAAE,SAAS,eAAe;GAChC,OACE,MACA,UAC4D;IAC5D,MAAM,YAAY,QAAQ,IAAI;IAC9B,OAAO,eAAe;KACpB,QAAQ;KACR,MAAM,CAAC,OAAO,MAAM,SAAS,GAAG,OAAO,OAAO,SAAS,CAAC;KACxD,SAAS;MAAE,SAAS;MAAe,UAAU;KAAM;KACnD,UAAU;MACR,cAAc;MACd,UAAU;MACV,UAAU;KACZ;IACF,CAAC;GACH;EACF;CACF;AACF;AAoDA,MAAa,mBAET;CAnDF,MAAM;CACN,IAAI;CACJ,UAAU;CACV,UAAU;CACV,SAAS;CACT,cAAc,EACZ,UAAU,EACR,mBAAmB,KACrB,EACF;CACA,WAAW,EACT,MAAM,uBACR;CACA,OAAO;EACL,YAAY;GACV,kBAAkB,MAAM,KAAK,sBAAsB,OAAO,CAAC;GAC3D,QAAQ;IACN,SAAS;IACT,OAAO;IACP,OAAO;GACT;GACA,aAAa,CACX;IACE,SAAS;IACT,OAAO;IACP,OAAO;GACT,CACF;EACF;EACA,gBAAgB,EACd,QAAQ;GACN,SAAS;GACT,OAAO;GACP,OAAO;EACT,EACF;EACA,qBAAqB,EACnB,QAAQ;GACN,SAAS;GACT,OAAO;GACP,OAAO;EACT,EACF;EACA,SAAS,CACP;GAAE,QAAQ;GAAgB,UAAU;GAAO,UAAU;GAAY,YAAY;EAAS,CACxF;CACF;AAKE"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { structuredError } from "@prisma-next/utils/structured-error";
|
|
2
|
+
//#region src/core/constants.ts
|
|
3
|
+
/**
|
|
4
|
+
* Codec ID for pgvector's vector type.
|
|
5
|
+
*/
|
|
6
|
+
const VECTOR_CODEC_ID = "pg/vector@1";
|
|
7
|
+
/**
|
|
8
|
+
* Maximum dimension for pgvector vectors (VECTOR_MAX_DIM from pgvector).
|
|
9
|
+
*/
|
|
10
|
+
const VECTOR_MAX_DIM = 16e3;
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/core/errors.ts
|
|
13
|
+
function pgVectorError(code, message, options) {
|
|
14
|
+
return structuredError(code, message, options);
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { VECTOR_CODEC_ID as n, VECTOR_MAX_DIM as r, pgVectorError as t };
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=errors-BnZLl-sM.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors-BnZLl-sM.mjs","names":[],"sources":["../src/core/constants.ts","../src/core/errors.ts"],"sourcesContent":["/**\n * Codec ID for pgvector's vector type.\n */\nexport const VECTOR_CODEC_ID = 'pg/vector@1' as const;\n\n/**\n * Maximum dimension for pgvector vectors (VECTOR_MAX_DIM from pgvector).\n */\nexport const VECTOR_MAX_DIM = 16000;\n","import type { StructuredError, StructuredErrorOptions } from '@prisma-next/utils/structured-error';\nimport { structuredError } from '@prisma-next/utils/structured-error';\n\nexport type PgVectorErrorCode =\n | 'RUNTIME.ENCODE_FAILED'\n | 'RUNTIME.DECODE_FAILED'\n | 'CONTRACT.ARGUMENT_INVALID';\n\nexport function pgVectorError(\n code: PgVectorErrorCode,\n message: string,\n options?: StructuredErrorOptions,\n): StructuredError {\n return structuredError(code, message, options);\n}\n"],"mappings":";;;;;AAGA,MAAa,kBAAkB;;;;AAK/B,MAAa,iBAAiB;;;ACA9B,SAAgB,cACd,MACA,SACA,SACiB;CACjB,OAAO,gBAAgB,MAAM,SAAS,OAAO;AAC/C"}
|
package/dist/pack.d.mts
CHANGED
package/dist/pack.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as pgvectorPackMeta } from "./descriptor-meta-
|
|
1
|
+
import { t as pgvectorPackMeta } from "./descriptor-meta-DMPKzaNe.mjs";
|
|
2
2
|
export { pgvectorPackMeta as default };
|
package/dist/runtime.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as pgvectorQueryOperations, r as pgvectorCodecRegistry, t as pgvectorPackMeta } from "./descriptor-meta-
|
|
1
|
+
import { n as pgvectorQueryOperations, r as pgvectorCodecRegistry, t as pgvectorPackMeta } from "./descriptor-meta-DMPKzaNe.mjs";
|
|
2
2
|
//#region src/exports/runtime.ts
|
|
3
3
|
const pgvectorRuntimeDescriptor = {
|
|
4
4
|
kind: "extension",
|
package/package.json
CHANGED
|
@@ -1,39 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/extension-pgvector",
|
|
3
|
-
"version": "0.16.0-dev.
|
|
3
|
+
"version": "0.16.0-dev.31",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@prisma-next/contract": "0.16.0-dev.
|
|
9
|
-
"@prisma-next/contract-authoring": "0.16.0-dev.
|
|
10
|
-
"@prisma-next/family-sql": "0.16.0-dev.
|
|
11
|
-
"@prisma-next/framework-components": "0.16.0-dev.
|
|
12
|
-
"@prisma-next/migration-tools": "0.16.0-dev.
|
|
13
|
-
"@prisma-next/sql-contract": "0.16.0-dev.
|
|
14
|
-
"@prisma-next/sql-operations": "0.16.0-dev.
|
|
15
|
-
"@prisma-next/sql-relational-core": "0.16.0-dev.
|
|
16
|
-
"@prisma-next/sql-runtime": "0.16.0-dev.
|
|
17
|
-
"@prisma-next/sql-schema-ir": "0.16.0-dev.
|
|
8
|
+
"@prisma-next/contract": "0.16.0-dev.31",
|
|
9
|
+
"@prisma-next/contract-authoring": "0.16.0-dev.31",
|
|
10
|
+
"@prisma-next/family-sql": "0.16.0-dev.31",
|
|
11
|
+
"@prisma-next/framework-components": "0.16.0-dev.31",
|
|
12
|
+
"@prisma-next/migration-tools": "0.16.0-dev.31",
|
|
13
|
+
"@prisma-next/sql-contract": "0.16.0-dev.31",
|
|
14
|
+
"@prisma-next/sql-operations": "0.16.0-dev.31",
|
|
15
|
+
"@prisma-next/sql-relational-core": "0.16.0-dev.31",
|
|
16
|
+
"@prisma-next/sql-runtime": "0.16.0-dev.31",
|
|
17
|
+
"@prisma-next/sql-schema-ir": "0.16.0-dev.31",
|
|
18
|
+
"@prisma-next/utils": "0.16.0-dev.31",
|
|
18
19
|
"@standard-schema/spec": "^1.1.0",
|
|
19
20
|
"arktype": "^2.2.2"
|
|
20
21
|
},
|
|
21
22
|
"devDependencies": {
|
|
22
|
-
"@prisma-next/adapter-postgres": "0.16.0-dev.
|
|
23
|
-
"@prisma-next/cli": "0.16.0-dev.
|
|
24
|
-
"@prisma-next/operations": "0.16.0-dev.
|
|
25
|
-
"@prisma-next/postgres": "0.16.0-dev.
|
|
26
|
-
"@prisma-next/sql-contract-ts": "0.16.0-dev.
|
|
27
|
-
"@prisma-next/target-postgres": "0.16.0-dev.
|
|
28
|
-
"@prisma-next/test-utils": "0.16.0-dev.
|
|
29
|
-
"@prisma-next/tsconfig": "0.16.0-dev.
|
|
30
|
-
"@prisma-next/tsdown": "0.16.0-dev.
|
|
23
|
+
"@prisma-next/adapter-postgres": "0.16.0-dev.31",
|
|
24
|
+
"@prisma-next/cli": "0.16.0-dev.31",
|
|
25
|
+
"@prisma-next/operations": "0.16.0-dev.31",
|
|
26
|
+
"@prisma-next/postgres": "0.16.0-dev.31",
|
|
27
|
+
"@prisma-next/sql-contract-ts": "0.16.0-dev.31",
|
|
28
|
+
"@prisma-next/target-postgres": "0.16.0-dev.31",
|
|
29
|
+
"@prisma-next/test-utils": "0.16.0-dev.31",
|
|
30
|
+
"@prisma-next/tsconfig": "0.16.0-dev.31",
|
|
31
|
+
"@prisma-next/tsdown": "0.16.0-dev.31",
|
|
31
32
|
"tsdown": "0.22.8",
|
|
32
33
|
"typescript": "5.9.3",
|
|
33
34
|
"vitest": "4.1.10"
|
|
34
35
|
},
|
|
35
36
|
"peerDependencies": {
|
|
36
|
-
"@prisma-next/adapter-postgres": "0.16.0-dev.
|
|
37
|
+
"@prisma-next/adapter-postgres": "0.16.0-dev.31",
|
|
37
38
|
"typescript": ">=5.9"
|
|
38
39
|
},
|
|
39
40
|
"peerDependenciesMeta": {
|
package/src/contract.d.ts
CHANGED
|
@@ -30,10 +30,10 @@ import type {
|
|
|
30
30
|
} from '@prisma-next/contract/types';
|
|
31
31
|
|
|
32
32
|
export type StorageHash =
|
|
33
|
-
StorageHashBase<'
|
|
33
|
+
StorageHashBase<'3d2c56a2944685bd21b05bc8a8d73164397df51c014201902932fbe7e80ff1b8'>;
|
|
34
34
|
export type ExecutionHash = ExecutionHashBase<string>;
|
|
35
35
|
export type ProfileHash =
|
|
36
|
-
ProfileHashBase<'
|
|
36
|
+
ProfileHashBase<'3916f444a8a17ad749191acf9e08dad97d1a327b88c2f1d45d12f240296aa8b2'>;
|
|
37
37
|
|
|
38
38
|
export type CodecTypes = PgTypes;
|
|
39
39
|
export type LaneCodecTypes = CodecTypes;
|
|
@@ -103,7 +103,7 @@ type ContractBase = Omit<
|
|
|
103
103
|
readonly scalarList: true;
|
|
104
104
|
};
|
|
105
105
|
};
|
|
106
|
-
readonly
|
|
106
|
+
readonly extensions: {};
|
|
107
107
|
readonly meta: {};
|
|
108
108
|
|
|
109
109
|
readonly profileHash: ProfileHash;
|
package/src/contract.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"schemaVersion": "1",
|
|
3
3
|
"targetFamily": "sql",
|
|
4
4
|
"target": "postgres",
|
|
5
|
-
"profileHash": "
|
|
5
|
+
"profileHash": "3916f444a8a17ad749191acf9e08dad97d1a327b88c2f1d45d12f240296aa8b2",
|
|
6
6
|
"roots": {},
|
|
7
7
|
"domain": {
|
|
8
8
|
"namespaces": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"kind": "postgres-schema"
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
|
-
"storageHash": "
|
|
24
|
+
"storageHash": "3d2c56a2944685bd21b05bc8a8d73164397df51c014201902932fbe7e80ff1b8",
|
|
25
25
|
"types": {
|
|
26
26
|
"vector": {
|
|
27
27
|
"codecId": "pg/vector@1",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"scalarList": true
|
|
48
48
|
}
|
|
49
49
|
},
|
|
50
|
-
"
|
|
50
|
+
"extensions": {},
|
|
51
51
|
"meta": {},
|
|
52
52
|
"_generated": {
|
|
53
53
|
"warning": "⚠️ GENERATED FILE - DO NOT EDIT",
|
package/src/core/codecs.ts
CHANGED
|
@@ -25,6 +25,9 @@ import type { ExtractCodecTypes } from '@prisma-next/sql-relational-core/ast';
|
|
|
25
25
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
26
26
|
import { type as arktype } from 'arktype';
|
|
27
27
|
import { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from './constants';
|
|
28
|
+
import { pgVectorError } from './errors';
|
|
29
|
+
|
|
30
|
+
type VectorConversionCode = 'RUNTIME.ENCODE_FAILED' | 'RUNTIME.DECODE_FAILED';
|
|
28
31
|
|
|
29
32
|
type VectorParams = { readonly length: number };
|
|
30
33
|
|
|
@@ -45,7 +48,14 @@ const PG_VECTOR_META = { db: { sql: { postgres: { nativeType: 'vector' } } } } a
|
|
|
45
48
|
|
|
46
49
|
function parseVector(value: string): number[] {
|
|
47
50
|
if (!value.startsWith('[') || !value.endsWith(']')) {
|
|
48
|
-
throw
|
|
51
|
+
throw pgVectorError(
|
|
52
|
+
'RUNTIME.DECODE_FAILED',
|
|
53
|
+
`Invalid vector format: expected "[...]", got "${value}"`,
|
|
54
|
+
{
|
|
55
|
+
why: 'The database returned a vector value that is not in the PostgreSQL "[x,y,z]" text format.',
|
|
56
|
+
meta: { codecId: VECTOR_CODEC_ID, wirePreview: value },
|
|
57
|
+
},
|
|
58
|
+
);
|
|
49
59
|
}
|
|
50
60
|
const content = value.slice(1, -1).trim();
|
|
51
61
|
return content === ''
|
|
@@ -53,7 +63,14 @@ function parseVector(value: string): number[] {
|
|
|
53
63
|
: content.split(',').map((entry) => {
|
|
54
64
|
const number = Number.parseFloat(entry.trim());
|
|
55
65
|
if (Number.isNaN(number)) {
|
|
56
|
-
throw
|
|
66
|
+
throw pgVectorError(
|
|
67
|
+
'RUNTIME.DECODE_FAILED',
|
|
68
|
+
`Invalid vector value: "${entry}" is not a number`,
|
|
69
|
+
{
|
|
70
|
+
why: 'A vector entry returned by the database could not be parsed as a number.',
|
|
71
|
+
meta: { codecId: VECTOR_CODEC_ID, wirePreview: value },
|
|
72
|
+
},
|
|
73
|
+
);
|
|
57
74
|
}
|
|
58
75
|
return number;
|
|
59
76
|
});
|
|
@@ -72,48 +89,60 @@ export class PgVectorCodec extends CodecImpl<
|
|
|
72
89
|
this.length = length;
|
|
73
90
|
}
|
|
74
91
|
|
|
75
|
-
assertVector(value: unknown): asserts value is number[] {
|
|
92
|
+
assertVector(value: unknown, code: VectorConversionCode): asserts value is number[] {
|
|
93
|
+
const meta = { codecId: VECTOR_CODEC_ID, expectedLength: this.length };
|
|
76
94
|
if (!Array.isArray(value)) {
|
|
77
|
-
throw
|
|
95
|
+
throw pgVectorError(code, 'Vector value must be an array of numbers', { meta });
|
|
78
96
|
}
|
|
79
97
|
for (const element of value) {
|
|
80
98
|
if (typeof element !== 'number') {
|
|
81
|
-
throw
|
|
99
|
+
throw pgVectorError(code, 'Vector value must contain only numbers', { meta });
|
|
82
100
|
}
|
|
83
101
|
if (!Number.isFinite(element)) {
|
|
84
|
-
throw
|
|
102
|
+
throw pgVectorError(code, 'Vector value must contain only finite numbers', { meta });
|
|
85
103
|
}
|
|
86
104
|
}
|
|
87
105
|
if (value.length !== this.length) {
|
|
88
|
-
throw
|
|
106
|
+
throw pgVectorError(
|
|
107
|
+
code,
|
|
108
|
+
`Vector length mismatch: expected ${this.length}, got ${value.length}`,
|
|
109
|
+
{
|
|
110
|
+
why: `This column is declared as vector(${this.length}); every value must have exactly that many dimensions.`,
|
|
111
|
+
meta: { ...meta, receivedLength: value.length },
|
|
112
|
+
},
|
|
113
|
+
);
|
|
89
114
|
}
|
|
90
115
|
}
|
|
91
116
|
|
|
92
117
|
async encode(value: number[], _ctx: CodecCallContext): Promise<string> {
|
|
93
|
-
this.assertVector(value);
|
|
118
|
+
this.assertVector(value, 'RUNTIME.ENCODE_FAILED');
|
|
94
119
|
return `[${value.join(',')}]`;
|
|
95
120
|
}
|
|
96
121
|
|
|
97
122
|
async decode(wire: string, _ctx: CodecCallContext): Promise<number[]> {
|
|
98
123
|
if (typeof wire !== 'string') {
|
|
99
|
-
throw
|
|
124
|
+
throw pgVectorError('RUNTIME.DECODE_FAILED', 'Vector wire value must be a string', {
|
|
125
|
+
meta: { codecId: VECTOR_CODEC_ID },
|
|
126
|
+
});
|
|
100
127
|
}
|
|
101
128
|
const value = parseVector(wire);
|
|
102
|
-
this.assertVector(value);
|
|
129
|
+
this.assertVector(value, 'RUNTIME.DECODE_FAILED');
|
|
103
130
|
return value;
|
|
104
131
|
}
|
|
105
132
|
|
|
106
133
|
encodeJson(value: number[]): JsonValue {
|
|
107
|
-
this.assertVector(value);
|
|
134
|
+
this.assertVector(value, 'RUNTIME.ENCODE_FAILED');
|
|
108
135
|
return `[${value.join(',')}]`;
|
|
109
136
|
}
|
|
110
137
|
|
|
111
138
|
decodeJson(json: JsonValue): number[] {
|
|
112
139
|
if (typeof json !== 'string') {
|
|
113
|
-
throw
|
|
140
|
+
throw pgVectorError('RUNTIME.DECODE_FAILED', 'Vector database JSON value must be a string', {
|
|
141
|
+
meta: { codecId: VECTOR_CODEC_ID },
|
|
142
|
+
});
|
|
114
143
|
}
|
|
115
144
|
const value = parseVector(json);
|
|
116
|
-
this.assertVector(value);
|
|
145
|
+
this.assertVector(value, 'RUNTIME.DECODE_FAILED');
|
|
117
146
|
return value;
|
|
118
147
|
}
|
|
119
148
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { StructuredError, StructuredErrorOptions } from '@prisma-next/utils/structured-error';
|
|
2
|
+
import { structuredError } from '@prisma-next/utils/structured-error';
|
|
3
|
+
|
|
4
|
+
export type PgVectorErrorCode =
|
|
5
|
+
| 'RUNTIME.ENCODE_FAILED'
|
|
6
|
+
| 'RUNTIME.DECODE_FAILED'
|
|
7
|
+
| 'CONTRACT.ARGUMENT_INVALID';
|
|
8
|
+
|
|
9
|
+
export function pgVectorError(
|
|
10
|
+
code: PgVectorErrorCode,
|
|
11
|
+
message: string,
|
|
12
|
+
options?: StructuredErrorOptions,
|
|
13
|
+
): StructuredError {
|
|
14
|
+
return structuredError(code, message, options);
|
|
15
|
+
}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import type { ColumnTypeDescriptor } from '@prisma-next/framework-components/codec';
|
|
6
6
|
import { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from '../core/constants';
|
|
7
|
+
import { pgVectorError } from '../core/errors';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Factory for creating dimensioned vector column descriptors.
|
|
@@ -15,14 +16,23 @@ import { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from '../core/constants';
|
|
|
15
16
|
* ```
|
|
16
17
|
* @param length - The dimension of the vector (e.g., 1536 for OpenAI embeddings)
|
|
17
18
|
* @returns A column type descriptor with `typeParams.length` set
|
|
18
|
-
* @throws
|
|
19
|
+
* @throws `CONTRACT.ARGUMENT_INVALID` if length is not an integer in the range [1, VECTOR_MAX_DIM]
|
|
19
20
|
*/
|
|
20
21
|
export function vector<N extends number>(
|
|
21
22
|
length: N,
|
|
22
23
|
): ColumnTypeDescriptor & { readonly typeParams: { readonly length: N } } {
|
|
23
24
|
if (!Number.isInteger(length) || length < 1 || length > VECTOR_MAX_DIM) {
|
|
24
|
-
throw
|
|
25
|
+
throw pgVectorError(
|
|
26
|
+
'CONTRACT.ARGUMENT_INVALID',
|
|
25
27
|
`pgvector: dimension must be an integer in [1, ${VECTOR_MAX_DIM}], got ${length}`,
|
|
28
|
+
{
|
|
29
|
+
fix: `Pass an integer dimension between 1 and ${VECTOR_MAX_DIM}, e.g. vector(1536).`,
|
|
30
|
+
meta: {
|
|
31
|
+
helperPath: 'vector',
|
|
32
|
+
expected: `an integer in [1, ${VECTOR_MAX_DIM}]`,
|
|
33
|
+
received: length,
|
|
34
|
+
},
|
|
35
|
+
},
|
|
26
36
|
);
|
|
27
37
|
}
|
|
28
38
|
return {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"codec-types-CEqpE2q2.d.mts","names":[],"sources":["../src/core/constants.ts","../src/core/codecs.ts","../src/types/codec-types.ts"],"mappings":";;;;;;;;cAGa;;;KCyBR;WAA0B;;cAiClB,sBAAsB,iBAC1B;WAKE;cAEG,YAAY,oBAAoB;EAK5C,aAAa,yBAAyB;EAiBhC,OAAO,iBAAiB,MAAM,mBAAmB;EAKjD,OAAO,cAAc,MAAM,mBAAmB;EASpD,WAAW,kBAAkB;EAK7B,WAAW,MAAM;;cAUN,2BAA2B,oBAAoB;WACxC;WACA;WACA;WACA;;;;;;;;;WACA,cAAc,iBAAiB;EACxC,iBAAiB,QAAQ;EAGzB,QAAQ,QAAQ,gBAAgB,KAAK,yBAAyB;;cAkBnE;mBAAA;;KAIM,eAAa,yBAAyB;;;;;;;;;KCtItC,OAAO;WAAmD,iBAAiB;;KAE3E,aAAa"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
//#region src/core/constants.ts
|
|
2
|
-
/**
|
|
3
|
-
* Codec ID for pgvector's vector type.
|
|
4
|
-
*/
|
|
5
|
-
const VECTOR_CODEC_ID = "pg/vector@1";
|
|
6
|
-
/**
|
|
7
|
-
* Maximum dimension for pgvector vectors (VECTOR_MAX_DIM from pgvector).
|
|
8
|
-
*/
|
|
9
|
-
const VECTOR_MAX_DIM = 16e3;
|
|
10
|
-
//#endregion
|
|
11
|
-
export { VECTOR_MAX_DIM as n, VECTOR_CODEC_ID as t };
|
|
12
|
-
|
|
13
|
-
//# sourceMappingURL=constants-D8Ccy-f4.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants-D8Ccy-f4.mjs","names":[],"sources":["../src/core/constants.ts"],"sourcesContent":["/**\n * Codec ID for pgvector's vector type.\n */\nexport const VECTOR_CODEC_ID = 'pg/vector@1' as const;\n\n/**\n * Maximum dimension for pgvector vectors (VECTOR_MAX_DIM from pgvector).\n */\nexport const VECTOR_MAX_DIM = 16000;\n"],"mappings":";;;;AAGA,MAAa,kBAAkB;;;;AAK/B,MAAa,iBAAiB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"descriptor-meta-G5zBc8J8.mjs","names":["arktype"],"sources":["../src/core/authoring.ts","../src/core/codecs.ts","../src/core/registry.ts","../src/core/descriptor-meta.ts"],"sourcesContent":["import type { AuthoringTypeNamespace } from '@prisma-next/framework-components/authoring';\nimport { VECTOR_MAX_DIM } from './constants';\n\nexport const pgvectorAuthoringTypes = {\n pgvector: {\n Vector: {\n kind: 'typeConstructor',\n args: [\n { kind: 'number', name: 'length', integer: true, minimum: 1, maximum: VECTOR_MAX_DIM },\n ],\n output: {\n codecId: 'pg/vector@1',\n nativeType: 'vector',\n typeParams: {\n length: { kind: 'arg', index: 0 },\n },\n },\n },\n },\n} as const satisfies AuthoringTypeNamespace;\n","/**\n * pgvector extension codec.\n *\n * Mirrors the patterns in `postgres/codecs-class.ts` and `sqlite/codecs-class.ts` for the single `pg/vector@1` codec. Three artifacts:\n *\n * 1. `PgVectorCodec` extends {@link CodecImpl} with the runtime encode/decode/encodeJson/decodeJson conversions inline. Conversions are simple enough (PostgreSQL `[1,2,3]` text format) that no shared helper module is warranted; the class body is the source of truth.\n * 2. `PgVectorDescriptor` extends {@link CodecDescriptorImpl} with the codec id, traits, target types, params schema (`{ length: number }`, validated against {@link VECTOR_MAX_DIM}), `meta` (postgres `nativeType: 'vector'`), and the emit-path `renderOutputType` producing `Vector<${length}>`.\n * 3. `pgVectorColumn(length)` per-codec column helper invoking `descriptor.factory({ length })` directly + passing the bare `nativeType: 'vector'`. The family-layer {@link expandNativeType} hook renders the parameterized form (`vector(1536)`) at emit/verify time from `nativeType` + `typeParams`.\n *\n * `length` threads into the runtime codec via the constructor so encode/decode/encodeJson/decodeJson enforce the declared dimension at every ingress path. Without this, `vector(3)` and `vector(1536)` would produce codecs with identical behaviour and a dimension-mismatched value would round-trip undetected.\n */\n\nimport type { JsonValue } from '@prisma-next/contract/types';\nimport {\n type AnyCodecDescriptor,\n type CodecCallContext,\n CodecDescriptorImpl,\n CodecImpl,\n type CodecInstanceContext,\n type ColumnHelperFor,\n type ColumnHelperForStrict,\n column,\n} from '@prisma-next/framework-components/codec';\nimport type { ExtractCodecTypes } from '@prisma-next/sql-relational-core/ast';\nimport type { StandardSchemaV1 } from '@standard-schema/spec';\nimport { type as arktype } from 'arktype';\nimport { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from './constants';\n\ntype VectorParams = { readonly length: number };\n\nconst vectorParamsSchema = arktype({\n length: 'number',\n}).narrow((params, ctx) => {\n const { length } = params;\n if (!Number.isInteger(length)) {\n return ctx.mustBe('an integer');\n }\n if (length < 1 || length > VECTOR_MAX_DIM) {\n return ctx.mustBe(`in the range [1, ${VECTOR_MAX_DIM}]`);\n }\n return true;\n}) satisfies StandardSchemaV1<VectorParams>;\n\nconst PG_VECTOR_META = { db: { sql: { postgres: { nativeType: 'vector' } } } } as const;\n\nfunction parseVector(value: string): number[] {\n if (!value.startsWith('[') || !value.endsWith(']')) {\n throw new Error(`Invalid vector format: expected \"[...]\", got \"${value}\"`);\n }\n const content = value.slice(1, -1).trim();\n return content === ''\n ? []\n : content.split(',').map((entry) => {\n const number = Number.parseFloat(entry.trim());\n if (Number.isNaN(number)) {\n throw new Error(`Invalid vector value: \"${entry}\" is not a number`);\n }\n return number;\n });\n}\n\nexport class PgVectorCodec extends CodecImpl<\n typeof VECTOR_CODEC_ID,\n readonly ['equality'],\n string,\n number[]\n> {\n readonly length: number;\n\n constructor(descriptor: AnyCodecDescriptor, length: number) {\n super(descriptor);\n this.length = length;\n }\n\n assertVector(value: unknown): asserts value is number[] {\n if (!Array.isArray(value)) {\n throw new Error('Vector value must be an array of numbers');\n }\n for (const element of value) {\n if (typeof element !== 'number') {\n throw new Error('Vector value must contain only numbers');\n }\n if (!Number.isFinite(element)) {\n throw new Error('Vector value must contain only finite numbers');\n }\n }\n if (value.length !== this.length) {\n throw new Error(`Vector length mismatch: expected ${this.length}, got ${value.length}`);\n }\n }\n\n async encode(value: number[], _ctx: CodecCallContext): Promise<string> {\n this.assertVector(value);\n return `[${value.join(',')}]`;\n }\n\n async decode(wire: string, _ctx: CodecCallContext): Promise<number[]> {\n if (typeof wire !== 'string') {\n throw new Error('Vector wire value must be a string');\n }\n const value = parseVector(wire);\n this.assertVector(value);\n return value;\n }\n\n encodeJson(value: number[]): JsonValue {\n this.assertVector(value);\n return `[${value.join(',')}]`;\n }\n\n decodeJson(json: JsonValue): number[] {\n if (typeof json !== 'string') {\n throw new Error('Vector database JSON value must be a string');\n }\n const value = parseVector(json);\n this.assertVector(value);\n return value;\n }\n}\n\nexport class PgVectorDescriptor extends CodecDescriptorImpl<VectorParams> {\n override readonly codecId = VECTOR_CODEC_ID;\n override readonly traits = ['equality'] as const;\n override readonly targetTypes = ['vector'] as const;\n override readonly meta = PG_VECTOR_META;\n override readonly paramsSchema: StandardSchemaV1<VectorParams> = vectorParamsSchema;\n override renderOutputType(params: VectorParams): string {\n return `Vector<${params.length}>`;\n }\n override factory(params: VectorParams): (ctx: CodecInstanceContext) => PgVectorCodec {\n return () => new PgVectorCodec(this, params.length);\n }\n}\n\nexport const pgVectorDescriptor = new PgVectorDescriptor();\n\n/**\n * Per-codec column helper for `pg/vector@1`. Generic over `N extends number` so the column site preserves the dimension literal in `typeParams` (e.g. `pgVectorColumn(1536)` packs `typeParams: { length: 1536 }`).\n *\n * Passes the bare `nativeType: 'vector'`; the family-layer `expandNativeType` hook renders the parameterized form (`vector(1536)`) at emit/verify time from `nativeType` + `typeParams`.\n */\nexport const pgVectorColumn = <N extends number>(length: N) =>\n column(pgVectorDescriptor.factory({ length }), pgVectorDescriptor.codecId, { length }, 'vector');\n\npgVectorColumn satisfies ColumnHelperFor<PgVectorDescriptor>;\npgVectorColumn satisfies ColumnHelperForStrict<PgVectorDescriptor>;\n\nconst codecDescriptorMap = {\n vector: pgVectorDescriptor,\n} as const;\n\nexport type CodecTypes = ExtractCodecTypes<typeof codecDescriptorMap>;\n\nexport const codecDescriptors: readonly AnyCodecDescriptor[] = Object.values(codecDescriptorMap);\n","import { buildCodecDescriptorRegistry } from '@prisma-next/sql-relational-core/codec-descriptor-registry';\nimport type { CodecDescriptorRegistry } from '@prisma-next/sql-relational-core/query-lane-context';\nimport { codecDescriptors } from './codecs';\n\n/**\n * Registry of every codec descriptor shipped by `@prisma-next/extension-pgvector`.\n *\n * Public consumer surface for the pgvector codec set. Currently a single entry (`pg/vector@1`); the registry shape stays consistent with the other codec-shipping packages so consumers don't need to special-case extensions. See ADR 208.\n */\nexport const pgvectorCodecRegistry: CodecDescriptorRegistry =\n buildCodecDescriptorRegistry(codecDescriptors);\n","import {\n buildOperation,\n type CodecExpression,\n codecOf,\n type Expression,\n toExpr,\n} from '@prisma-next/sql-relational-core/expression';\nimport type { CodecTypes } from '../types/codec-types';\nimport type { QueryOperationTypes } from '../types/operation-types';\nimport { pgvectorAuthoringTypes } from './authoring';\nimport { pgvectorCodecRegistry } from './registry';\n\nconst pgvectorTypeId = 'pg/vector@1' as const;\n\ntype CodecTypesBase = Record<string, { readonly input: unknown; readonly output: unknown }>;\n\nexport function pgvectorQueryOperations<CT extends CodecTypesBase>(): QueryOperationTypes<CT> {\n return {\n cosineDistance: {\n self: { codecId: pgvectorTypeId },\n impl: (\n self: CodecExpression<'pg/vector@1', boolean, CT>,\n other: CodecExpression<'pg/vector@1', boolean, CT>,\n ): Expression<{ codecId: 'pg/float8@1'; nullable: false }> => {\n const selfCodec = codecOf(self);\n return buildOperation({\n method: 'cosineDistance',\n args: [toExpr(self, selfCodec), toExpr(other, selfCodec)],\n returns: { codecId: 'pg/float8@1', nullable: false },\n lowering: {\n targetFamily: 'sql',\n strategy: 'function',\n template: '{{self}} <=> {{arg0}}',\n },\n });\n },\n },\n cosineSimilarity: {\n self: { codecId: pgvectorTypeId },\n impl: (\n self: CodecExpression<'pg/vector@1', boolean, CT>,\n other: CodecExpression<'pg/vector@1', boolean, CT>,\n ): Expression<{ codecId: 'pg/float8@1'; nullable: false }> => {\n const selfCodec = codecOf(self);\n return buildOperation({\n method: 'cosineSimilarity',\n args: [toExpr(self, selfCodec), toExpr(other, selfCodec)],\n returns: { codecId: 'pg/float8@1', nullable: false },\n lowering: {\n targetFamily: 'sql',\n strategy: 'function',\n template: '1 - ({{self}} <=> {{arg0}})',\n },\n });\n },\n },\n };\n}\n\nconst pgvectorPackMetaBase = {\n kind: 'extension',\n id: 'pgvector',\n familyId: 'sql',\n targetId: 'postgres',\n version: '0.0.1',\n capabilities: {\n postgres: {\n 'pgvector.cosine': true,\n },\n },\n authoring: {\n type: pgvectorAuthoringTypes,\n },\n types: {\n codecTypes: {\n codecDescriptors: Array.from(pgvectorCodecRegistry.values()),\n import: {\n package: '@prisma-next/extension-pgvector/codec-types',\n named: 'CodecTypes',\n alias: 'PgVectorTypes',\n },\n typeImports: [\n {\n package: '@prisma-next/extension-pgvector/codec-types',\n named: 'Vector',\n alias: 'Vector',\n },\n ],\n },\n operationTypes: {\n import: {\n package: '@prisma-next/extension-pgvector/operation-types',\n named: 'OperationTypes',\n alias: 'PgVectorOperationTypes',\n },\n },\n queryOperationTypes: {\n import: {\n package: '@prisma-next/extension-pgvector/operation-types',\n named: 'QueryOperationTypes',\n alias: 'PgVectorQueryOperationTypes',\n },\n },\n storage: [\n { typeId: pgvectorTypeId, familyId: 'sql', targetId: 'postgres', nativeType: 'vector' },\n ],\n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