@prisma-next/extension-pgvector 0.5.0-dev.9 → 0.5.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.
- package/README.md +5 -2
- package/dist/codec-types-yMSpEJJM.d.mts +63 -0
- package/dist/codec-types-yMSpEJJM.d.mts.map +1 -0
- package/dist/codec-types.d.mts +1 -1
- package/dist/codec-types.mjs +1 -1
- package/dist/column-types.d.mts +2 -5
- package/dist/column-types.d.mts.map +1 -1
- package/dist/column-types.mjs +4 -7
- package/dist/column-types.mjs.map +1 -1
- package/dist/{constants-Co5golCK.mjs → constants-DX-00vYk.mjs} +2 -2
- package/dist/{constants-Co5golCK.mjs.map → constants-DX-00vYk.mjs.map} +1 -1
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +177 -30
- package/dist/control.mjs.map +1 -1
- package/dist/descriptor-meta-DEgJjLLi.mjs +178 -0
- package/dist/descriptor-meta-DEgJjLLi.mjs.map +1 -0
- package/dist/operation-types-Bd-jkNN3.d.mts +38 -0
- package/dist/operation-types-Bd-jkNN3.d.mts.map +1 -0
- package/dist/operation-types.d.mts +2 -76
- package/dist/operation-types.mjs +1 -1
- package/dist/pack.d.mts +3 -10
- package/dist/pack.d.mts.map +1 -1
- package/dist/pack.mjs +2 -3
- package/dist/runtime.d.mts +10 -1
- package/dist/runtime.d.mts.map +1 -1
- package/dist/runtime.mjs +6 -25
- package/dist/runtime.mjs.map +1 -1
- package/package.json +20 -17
- package/src/core/codecs.ts +127 -59
- package/src/core/contract-space-constants.ts +30 -0
- package/src/core/contract.ts +74 -0
- package/src/core/descriptor-meta.ts +40 -36
- package/src/core/migrations.ts +125 -0
- package/src/core/registry.ts +11 -0
- package/src/exports/column-types.ts +3 -6
- package/src/exports/control.ts +35 -37
- package/src/exports/operation-types.ts +1 -1
- package/src/exports/runtime.ts +11 -41
- package/src/types/operation-types.ts +19 -36
- package/dist/codec-types-BifaP625.d.mts +0 -27
- package/dist/codec-types-BifaP625.d.mts.map +0 -1
- package/dist/descriptor-meta-BQbvJJxu.mjs +0 -148
- package/dist/descriptor-meta-BQbvJJxu.mjs.map +0 -1
- package/dist/operation-types.d.mts.map +0 -1
package/README.md
CHANGED
|
@@ -69,7 +69,7 @@ Add vector columns to your contract and enable the namespace via pack refs:
|
|
|
69
69
|
import { int4Column, textColumn } from '@prisma-next/adapter-postgres/column-types';
|
|
70
70
|
import sqlFamily from '@prisma-next/family-sql/pack';
|
|
71
71
|
import { defineContract, field, model } from '@prisma-next/sql-contract-ts/contract-builder';
|
|
72
|
-
import { vectorColumn } from '@prisma-next/extension-pgvector/column-types';
|
|
72
|
+
import { vector, vectorColumn } from '@prisma-next/extension-pgvector/column-types';
|
|
73
73
|
import pgvector from '@prisma-next/extension-pgvector/pack';
|
|
74
74
|
import postgres from '@prisma-next/target-postgres/pack';
|
|
75
75
|
|
|
@@ -82,13 +82,16 @@ export const contract = defineContract({
|
|
|
82
82
|
fields: {
|
|
83
83
|
id: field.column(int4Column).id(),
|
|
84
84
|
title: field.column(textColumn),
|
|
85
|
-
|
|
85
|
+
// Dimensioned vector — `field.embedding` resolves to `Vector<1536>`.
|
|
86
|
+
embedding: field.column(vector(1536)).optional(),
|
|
86
87
|
},
|
|
87
88
|
}).sql({ table: 'post' }),
|
|
88
89
|
},
|
|
89
90
|
});
|
|
90
91
|
```
|
|
91
92
|
|
|
93
|
+
The `vector(N)` factory is registered through the unified `CodecDescriptor<{ length: number }>` shape — `paramsSchema` validates the dimension at the contract boundary, `renderOutputType: ({ length }) => 'Vector<' + length + '>'` produces the column's TS type for `contract.d.ts`, and the curried `factory` materializes the runtime codec at context construction. See [ADR 208 — Higher-order codecs for parameterized types](../../../docs/architecture%20docs/adrs/ADR%20208%20-%20Higher-order%20codecs%20for%20parameterized%20types.md) for the descriptor model. For undimensioned vector columns (rare; typically only valid in legacy schemas where `vector` was declared without a dimension), use `vectorColumn` instead.
|
|
94
|
+
|
|
92
95
|
### Runtime Setup
|
|
93
96
|
|
|
94
97
|
Register the extension when creating your execution stack:
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { JsonValue } from "@prisma-next/contract/types";
|
|
2
|
+
import { AnyCodecDescriptor, CodecCallContext, CodecDescriptorImpl, CodecImpl, CodecInstanceContext } from "@prisma-next/framework-components/codec";
|
|
3
|
+
import { ExtractCodecTypes } from "@prisma-next/sql-relational-core/ast";
|
|
4
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
5
|
+
|
|
6
|
+
//#region src/core/constants.d.ts
|
|
7
|
+
/**
|
|
8
|
+
* Codec ID for pgvector's vector type.
|
|
9
|
+
*/
|
|
10
|
+
declare const VECTOR_CODEC_ID: "pg/vector@1";
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/core/codecs.d.ts
|
|
13
|
+
type VectorParams = {
|
|
14
|
+
readonly length: number;
|
|
15
|
+
};
|
|
16
|
+
declare class PgVectorCodec extends CodecImpl<typeof VECTOR_CODEC_ID, readonly ['equality'], string, number[]> {
|
|
17
|
+
readonly length: number | undefined;
|
|
18
|
+
constructor(descriptor: AnyCodecDescriptor, length: number | undefined);
|
|
19
|
+
assertVector(value: unknown): asserts value is number[];
|
|
20
|
+
encode(value: number[], _ctx: CodecCallContext): Promise<string>;
|
|
21
|
+
decode(wire: string, _ctx: CodecCallContext): Promise<number[]>;
|
|
22
|
+
encodeJson(value: number[]): JsonValue;
|
|
23
|
+
decodeJson(json: JsonValue): number[];
|
|
24
|
+
}
|
|
25
|
+
declare class PgVectorDescriptor extends CodecDescriptorImpl<VectorParams> {
|
|
26
|
+
readonly codecId: "pg/vector@1";
|
|
27
|
+
readonly traits: readonly ["equality"];
|
|
28
|
+
readonly targetTypes: readonly ["vector"];
|
|
29
|
+
readonly meta: {
|
|
30
|
+
readonly db: {
|
|
31
|
+
readonly sql: {
|
|
32
|
+
readonly postgres: {
|
|
33
|
+
readonly nativeType: "vector";
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
readonly paramsSchema: StandardSchemaV1<VectorParams>;
|
|
39
|
+
renderOutputType(params: VectorParams): string;
|
|
40
|
+
/**
|
|
41
|
+
* The runtime calls `factory(undefined)(ctx)` to materialize a representative codec for parameterized descriptors that ship a no-params column variant (here, `vectorColumn` vs `vector(N)`). The runtime cast widens `params` to `unknown`, so guarding with an optional read keeps the typed call site (`factory({ length })`) strict while still producing a length-agnostic codec for representative use. Encode/decode for an undimensioned column run through this representative; the wire format `[v1,v2,...]` is dimension-independent.
|
|
42
|
+
*/
|
|
43
|
+
factory(params: VectorParams): (ctx: CodecInstanceContext) => PgVectorCodec;
|
|
44
|
+
}
|
|
45
|
+
declare const codecDescriptorMap: {
|
|
46
|
+
readonly vector: PgVectorDescriptor;
|
|
47
|
+
};
|
|
48
|
+
type CodecTypes$1 = ExtractCodecTypes<typeof codecDescriptorMap>;
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/types/codec-types.d.ts
|
|
51
|
+
/**
|
|
52
|
+
* Type-level branded vector.
|
|
53
|
+
*
|
|
54
|
+
* The runtime values are plain number arrays, but parameterized column typing can
|
|
55
|
+
* carry the dimension at the type level (e.g. Vector<1536>).
|
|
56
|
+
*/
|
|
57
|
+
type Vector<N extends number = number> = number[] & {
|
|
58
|
+
readonly __vectorLength?: N;
|
|
59
|
+
};
|
|
60
|
+
type CodecTypes = CodecTypes$1;
|
|
61
|
+
//#endregion
|
|
62
|
+
export { Vector as n, CodecTypes as t };
|
|
63
|
+
//# sourceMappingURL=codec-types-yMSpEJJM.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec-types-yMSpEJJM.d.mts","names":[],"sources":["../src/core/constants.ts","../src/core/codecs.ts","../src/types/codec-types.ts"],"mappings":";;;;;;;;;cAGa,eAAA;;;KCyBR,YAAA;EAAA,SAA0B,MAAA;AAAA;AAAA,cAiBlB,aAAA,SAAsB,SAAA,QAC1B,eAAA;EAAA,SAKE,MAAA;cAEG,UAAA,EAAY,kBAAA,EAAoB,MAAA;EAK5C,YAAA,CAAa,KAAA,oBAAyB,KAAA;EAYhC,MAAA,CAAO,KAAA,YAAiB,IAAA,EAAM,gBAAA,GAAmB,OAAA;EAKjD,MAAA,CAAO,IAAA,UAAc,IAAA,EAAM,gBAAA,GAAmB,OAAA;EAsBpD,UAAA,CAAW,KAAA,aAAkB,SAAA;EAK7B,UAAA,CAAW,IAAA,EAAM,SAAA;AAAA;AAAA,cAMN,kBAAA,SAA2B,mBAAA,CAAoB,YAAA;EAAA,SACxC,OAAA;EAAA,SACA,MAAA;EAAA,SACA,WAAA;EAAA,SACA,IAAA;IAAA;;;;;;;;WACA,YAAA,EAAc,gBAAA,CAAiB,YAAA;EACxC,gBAAA,CAAiB,MAAA,EAAQ,YAAA;EA5C5B;;;EAkDG,OAAA,CAAQ,MAAA,EAAQ,YAAA,IAAgB,GAAA,EAAK,oBAAA,KAAyB,aAAA;AAAA;AAAA,cAkBnE,kBAAA;EAAA,iBAEI,kBAAA;AAAA;AAAA,KAEE,YAAA,GAAa,iBAAA,QAAyB,kBAAA;;;;;;;;AApHY;KCTlD,MAAA;EAAA,SAA0D,cAAA,GAAiB,CAAA;AAAA;AAAA,KAE3E,UAAA,GAAa,YAAA"}
|
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-yMSpEJJM.mjs";
|
|
2
2
|
export { type CodecTypes, type Vector };
|
package/dist/codec-types.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {};
|
package/dist/column-types.d.mts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { ColumnTypeDescriptor } from "@prisma-next/
|
|
1
|
+
import { ColumnTypeDescriptor } from "@prisma-next/framework-components/codec";
|
|
2
2
|
|
|
3
3
|
//#region src/exports/column-types.d.ts
|
|
4
|
-
|
|
5
4
|
/**
|
|
6
|
-
* Static vector column descriptor without dimension.
|
|
7
|
-
* Use `vector(N)` for dimensioned vectors that produce `vector(N)` DDL.
|
|
5
|
+
* Static vector column descriptor without dimension. Use `vector(N)` for dimensioned vectors that produce `vector(N)` DDL.
|
|
8
6
|
*/
|
|
9
7
|
declare const vectorColumn: {
|
|
10
8
|
readonly codecId: "pg/vector@1";
|
|
@@ -18,7 +16,6 @@ declare const vectorColumn: {
|
|
|
18
16
|
* .column('embedding', { type: vector(1536), nullable: false })
|
|
19
17
|
* // Produces: nativeType: 'vector', typeParams: { length: 1536 }
|
|
20
18
|
* ```
|
|
21
|
-
*
|
|
22
19
|
* @param length - The dimension of the vector (e.g., 1536 for OpenAI embeddings)
|
|
23
20
|
* @returns A column type descriptor with `typeParams.length` set
|
|
24
21
|
* @throws {RangeError} If length is not an integer in the range [1, VECTOR_MAX_DIM]
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"column-types.d.mts","names":[],"sources":["../src/exports/column-types.ts"],"
|
|
1
|
+
{"version":3,"file":"column-types.d.mts","names":[],"sources":["../src/exports/column-types.ts"],"mappings":";;;;;AA6BA;cAjBa,YAAA;EAAA,SAG4B,OAAA;EAAA,SAAA,UAAA;AAAA;;;;;;;;;;;;;iBAczB,MAAA,kBAAA,CACd,MAAA,EAAQ,CAAA,GACP,oBAAA;EAAA,SAAkC,UAAA;IAAA,SAAuB,MAAA,EAAQ,CAAA;EAAA;AAAA"}
|
package/dist/column-types.mjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import { n as VECTOR_MAX_DIM, t as VECTOR_CODEC_ID } from "./constants-
|
|
2
|
-
|
|
1
|
+
import { n as VECTOR_MAX_DIM, t as VECTOR_CODEC_ID } from "./constants-DX-00vYk.mjs";
|
|
3
2
|
//#region src/exports/column-types.ts
|
|
4
3
|
/**
|
|
5
|
-
* Static vector column descriptor without dimension.
|
|
6
|
-
* Use `vector(N)` for dimensioned vectors that produce `vector(N)` DDL.
|
|
4
|
+
* Static vector column descriptor without dimension. Use `vector(N)` for dimensioned vectors that produce `vector(N)` DDL.
|
|
7
5
|
*/
|
|
8
6
|
const vectorColumn = {
|
|
9
7
|
codecId: VECTOR_CODEC_ID,
|
|
@@ -17,20 +15,19 @@ const vectorColumn = {
|
|
|
17
15
|
* .column('embedding', { type: vector(1536), nullable: false })
|
|
18
16
|
* // Produces: nativeType: 'vector', typeParams: { length: 1536 }
|
|
19
17
|
* ```
|
|
20
|
-
*
|
|
21
18
|
* @param length - The dimension of the vector (e.g., 1536 for OpenAI embeddings)
|
|
22
19
|
* @returns A column type descriptor with `typeParams.length` set
|
|
23
20
|
* @throws {RangeError} If length is not an integer in the range [1, VECTOR_MAX_DIM]
|
|
24
21
|
*/
|
|
25
22
|
function vector(length) {
|
|
26
|
-
if (!Number.isInteger(length) || length < 1 || length >
|
|
23
|
+
if (!Number.isInteger(length) || length < 1 || length > 16e3) throw new RangeError(`pgvector: dimension must be an integer in [1, ${VECTOR_MAX_DIM}], got ${length}`);
|
|
27
24
|
return {
|
|
28
25
|
codecId: VECTOR_CODEC_ID,
|
|
29
26
|
nativeType: "vector",
|
|
30
27
|
typeParams: { length }
|
|
31
28
|
};
|
|
32
29
|
}
|
|
33
|
-
|
|
34
30
|
//#endregion
|
|
35
31
|
export { vector, vectorColumn };
|
|
32
|
+
|
|
36
33
|
//# sourceMappingURL=column-types.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"column-types.mjs","names":[],"sources":["../src/exports/column-types.ts"],"sourcesContent":["/**\n * Column type descriptors for pgvector extension.\n *\n * These descriptors provide both codecId and nativeType for use in contract authoring
|
|
1
|
+
{"version":3,"file":"column-types.mjs","names":[],"sources":["../src/exports/column-types.ts"],"sourcesContent":["/**\n * Column type descriptors for pgvector extension.\n *\n * These descriptors provide both codecId and nativeType for use in contract authoring. They are derived from the same source of truth as codec definitions and manifests.\n */\n\nimport type { ColumnTypeDescriptor } from '@prisma-next/framework-components/codec';\nimport { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from '../core/constants';\n\n/**\n * Static vector column descriptor without dimension. Use `vector(N)` for dimensioned vectors that produce `vector(N)` DDL.\n */\nexport const vectorColumn = {\n codecId: VECTOR_CODEC_ID,\n nativeType: 'vector',\n} as const satisfies ColumnTypeDescriptor;\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 {RangeError} 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 new RangeError(\n `pgvector: dimension must be an integer in [1, ${VECTOR_MAX_DIM}], got ${length}`,\n );\n }\n return {\n codecId: VECTOR_CODEC_ID,\n nativeType: 'vector',\n typeParams: { length },\n } as const;\n}\n"],"mappings":";;;;;AAYA,MAAa,eAAe;CAC1B,SAAS;CACT,YAAY;CACb;;;;;;;;;;;;;AAcD,SAAgB,OACd,QACwE;CACxE,IAAI,CAAC,OAAO,UAAU,OAAO,IAAI,SAAS,KAAK,SAAA,MAC7C,MAAM,IAAI,WACR,iDAAiD,eAAe,SAAS,SAC1E;CAEH,OAAO;EACL,SAAS;EACT,YAAY;EACZ,YAAY,EAAE,QAAQ;EACvB"}
|
|
@@ -7,7 +7,7 @@ const VECTOR_CODEC_ID = "pg/vector@1";
|
|
|
7
7
|
* Maximum dimension for pgvector vectors (VECTOR_MAX_DIM from pgvector).
|
|
8
8
|
*/
|
|
9
9
|
const VECTOR_MAX_DIM = 16e3;
|
|
10
|
-
|
|
11
10
|
//#endregion
|
|
12
11
|
export { VECTOR_MAX_DIM as n, VECTOR_CODEC_ID as t };
|
|
13
|
-
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=constants-DX-00vYk.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants-
|
|
1
|
+
{"version":3,"file":"constants-DX-00vYk.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"}
|
package/dist/control.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"control.d.mts","names":[],"sources":["../src/exports/control.ts"],"
|
|
1
|
+
{"version":3,"file":"control.d.mts","names":[],"sources":["../src/exports/control.ts"],"mappings":";;;cA+DM,2BAAA,EAA6B,6BAAA"}
|
package/dist/control.mjs
CHANGED
|
@@ -1,5 +1,170 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { t as VECTOR_CODEC_ID } from "./constants-DX-00vYk.mjs";
|
|
2
|
+
import { n as pgvectorQueryOperations, t as pgvectorPackMeta } from "./descriptor-meta-DEgJjLLi.mjs";
|
|
3
|
+
import { computeStorageHash } from "@prisma-next/contract/hashing";
|
|
4
|
+
import { coreHash, profileHash } from "@prisma-next/contract/types";
|
|
5
|
+
import { computeMigrationHash } from "@prisma-next/migration-tools/hash";
|
|
6
|
+
//#region src/core/contract-space-constants.ts
|
|
7
|
+
/**
|
|
8
|
+
* Static names and identifiers used across pgvector's contract space.
|
|
9
|
+
*
|
|
10
|
+
* Centralised here so the contract IR (`./contract`), the baseline
|
|
11
|
+
* migration ops (`./migrations`), the head ref, and the descriptor
|
|
12
|
+
* (`../exports/control`) all reference the same values without typos.
|
|
13
|
+
*
|
|
14
|
+
* The space identifier `'pgvector'` is what the framework writes to
|
|
15
|
+
* `migrations/pgvector/` in the user's repo and what the marker table's
|
|
16
|
+
* `space` column carries for pgvector-owned rows.
|
|
17
|
+
*
|
|
18
|
+
* The `pgvector:*` invariantId namespace is locked here — once
|
|
19
|
+
* published, an invariantId is immutable so downstream consumers can
|
|
20
|
+
* reference it by literal string match.
|
|
21
|
+
*/
|
|
22
|
+
const PGVECTOR_SPACE_ID = "pgvector";
|
|
23
|
+
const PGVECTOR_NATIVE_TYPE = "vector";
|
|
24
|
+
const PGVECTOR_BASELINE_MIGRATION_NAME = "20260601T0000_install_vector_extension";
|
|
25
|
+
/**
|
|
26
|
+
* `pgvector:*` invariantIds emitted by the baseline migration. Each id,
|
|
27
|
+
* once published, is immutable: downstream consumers (other extensions,
|
|
28
|
+
* the marker table) reference them by literal string match.
|
|
29
|
+
*/
|
|
30
|
+
const PGVECTOR_INVARIANTS = { installVector: "pgvector:install-vector-v1" };
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/core/contract.ts
|
|
33
|
+
/**
|
|
34
|
+
* pgvector contract space — declares the parameterised native type
|
|
35
|
+
* `vector(N)` that user columns can name as `nativeType`.
|
|
36
|
+
*
|
|
37
|
+
* Unlike CipherStash's typed objects (composite types / domains / enums
|
|
38
|
+
* — deferred behind `meta.cipherstashFutureIR` until the IR vocabulary
|
|
39
|
+
* gains first-class support), pgvector's `vector` is a parameterised
|
|
40
|
+
* native type and *is* representable in today's IR via
|
|
41
|
+
* {@link StorageTypeInstance}: `{ codecId, nativeType, typeParams }`.
|
|
42
|
+
* The contract registers a representative instance under
|
|
43
|
+
* `storage.types.vector` so the verifier sees the type as part of
|
|
44
|
+
* pgvector's space contribution and so the pinned `contract.json` on
|
|
45
|
+
* disk is materially distinct from an empty space.
|
|
46
|
+
*
|
|
47
|
+
* Per-column instances on the user's side carry concrete
|
|
48
|
+
* `typeParams.length` (e.g. `vector(1536)`); the registration here
|
|
49
|
+
* declares the parameterised shape — it is not consumed as a literal
|
|
50
|
+
* column type by any user table.
|
|
51
|
+
*/
|
|
52
|
+
const TARGET = "postgres";
|
|
53
|
+
const TARGET_FAMILY = "sql";
|
|
54
|
+
/**
|
|
55
|
+
* Storage body for the contract — pgvector ships no tables of its own;
|
|
56
|
+
* the `vector` parameterised native type is registered under
|
|
57
|
+
* `storage.types` so pgvector's IR contribution is non-empty and the
|
|
58
|
+
* pinned `contract.json` on disk differs materially from an empty
|
|
59
|
+
* extension space.
|
|
60
|
+
*
|
|
61
|
+
* Authored without `storageHash` here so {@link computeStorageHash} can
|
|
62
|
+
* digest the canonical body (the hashing pipeline panics if asked to
|
|
63
|
+
* hash an object that already carries its own output — see
|
|
64
|
+
* `assertDescriptorSelfConsistency`'s storage-hash strip).
|
|
65
|
+
*/
|
|
66
|
+
const storageBody = {
|
|
67
|
+
tables: {},
|
|
68
|
+
types: { [PGVECTOR_NATIVE_TYPE]: {
|
|
69
|
+
codecId: VECTOR_CODEC_ID,
|
|
70
|
+
nativeType: PGVECTOR_NATIVE_TYPE,
|
|
71
|
+
typeParams: {}
|
|
72
|
+
} }
|
|
73
|
+
};
|
|
74
|
+
/** Content-addressed hash of pgvector's storage IR. */
|
|
75
|
+
const PGVECTOR_STORAGE_HASH = computeStorageHash({
|
|
76
|
+
target: TARGET,
|
|
77
|
+
targetFamily: TARGET_FAMILY,
|
|
78
|
+
storage: storageBody
|
|
79
|
+
});
|
|
80
|
+
/** pgvector's contract value, exposed via the descriptor's `contractSpace.contractJson`. */
|
|
81
|
+
const pgvectorContract = {
|
|
82
|
+
target: TARGET,
|
|
83
|
+
targetFamily: TARGET_FAMILY,
|
|
84
|
+
roots: {},
|
|
85
|
+
models: {},
|
|
86
|
+
capabilities: {},
|
|
87
|
+
extensionPacks: {},
|
|
88
|
+
meta: {},
|
|
89
|
+
profileHash: profileHash("pgvector-extension-profile-v1"),
|
|
90
|
+
storage: {
|
|
91
|
+
...storageBody,
|
|
92
|
+
storageHash: coreHash(PGVECTOR_STORAGE_HASH)
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/core/migrations.ts
|
|
97
|
+
const pgvectorBaselineOps = [{
|
|
98
|
+
id: "pgvector.install-vector-extension",
|
|
99
|
+
label: "Enable extension \"vector\"",
|
|
100
|
+
operationClass: "additive",
|
|
101
|
+
invariantId: PGVECTOR_INVARIANTS.installVector,
|
|
102
|
+
target: {
|
|
103
|
+
id: "postgres",
|
|
104
|
+
details: {
|
|
105
|
+
schema: "public",
|
|
106
|
+
objectType: "extension",
|
|
107
|
+
name: "vector"
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
precheck: [{
|
|
111
|
+
description: "verify extension \"vector\" is not already enabled",
|
|
112
|
+
sql: "SELECT NOT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector')"
|
|
113
|
+
}],
|
|
114
|
+
execute: [{
|
|
115
|
+
description: "create extension \"vector\"",
|
|
116
|
+
sql: "CREATE EXTENSION IF NOT EXISTS vector"
|
|
117
|
+
}],
|
|
118
|
+
postcheck: [{
|
|
119
|
+
description: "confirm extension \"vector\" is enabled",
|
|
120
|
+
sql: "SELECT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector')"
|
|
121
|
+
}]
|
|
122
|
+
}];
|
|
123
|
+
/** Sorted list of invariantIds the baseline migration provides. */
|
|
124
|
+
const PGVECTOR_BASELINE_INVARIANTS = (() => {
|
|
125
|
+
const ids = pgvectorBaselineOps.map((op) => op.invariantId).filter((id) => typeof id === "string");
|
|
126
|
+
return [...new Set(ids)].sort();
|
|
127
|
+
})();
|
|
128
|
+
const baselineMetadataWithoutHash = {
|
|
129
|
+
from: null,
|
|
130
|
+
to: PGVECTOR_STORAGE_HASH,
|
|
131
|
+
fromContract: null,
|
|
132
|
+
toContract: pgvectorContract,
|
|
133
|
+
hints: {
|
|
134
|
+
used: [],
|
|
135
|
+
applied: [],
|
|
136
|
+
plannerVersion: "2.0.0"
|
|
137
|
+
},
|
|
138
|
+
labels: [],
|
|
139
|
+
providedInvariants: PGVECTOR_BASELINE_INVARIANTS,
|
|
140
|
+
createdAt: "2026-06-01T00:00:00.000Z"
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Baseline migration package the descriptor publishes via
|
|
144
|
+
* `contractSpace.migrations`. The framework's emitter writes this to
|
|
145
|
+
* `migrations/pgvector/<dirName>/{manifest,ops,contract}.json` in the
|
|
146
|
+
* user's repo at `migrate` time.
|
|
147
|
+
*/
|
|
148
|
+
const pgvectorBaselineMigration = {
|
|
149
|
+
dirName: PGVECTOR_BASELINE_MIGRATION_NAME,
|
|
150
|
+
metadata: {
|
|
151
|
+
...baselineMetadataWithoutHash,
|
|
152
|
+
migrationHash: computeMigrationHash(baselineMetadataWithoutHash, pgvectorBaselineOps)
|
|
153
|
+
},
|
|
154
|
+
ops: pgvectorBaselineOps
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Pinned head ref the descriptor publishes. The framework writes this
|
|
158
|
+
* verbatim to `migrations/pgvector/refs/head.json` (with `invariants`
|
|
159
|
+
* sorted alphabetically per the canonicalisation rules); the runner's
|
|
160
|
+
* `findPathWithDecision` step consults `head.json` to decide which
|
|
161
|
+
* migrations need to apply.
|
|
162
|
+
*/
|
|
163
|
+
const pgvectorHeadRef = {
|
|
164
|
+
hash: PGVECTOR_STORAGE_HASH,
|
|
165
|
+
invariants: PGVECTOR_BASELINE_INVARIANTS
|
|
166
|
+
};
|
|
167
|
+
//#endregion
|
|
3
168
|
//#region src/exports/control.ts
|
|
4
169
|
const PGVECTOR_CODEC_ID = "pg/vector@1";
|
|
5
170
|
function buildVectorIdentityValue(typeParams) {
|
|
@@ -15,31 +180,15 @@ const vectorControlPlaneHooks = {
|
|
|
15
180
|
},
|
|
16
181
|
resolveIdentityValue: ({ typeParams }) => buildVectorIdentityValue(typeParams)
|
|
17
182
|
};
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
label: "Enable extension \"vector\"",
|
|
24
|
-
summary: "Ensures the vector extension is available for pgvector operations",
|
|
25
|
-
operationClass: "additive",
|
|
26
|
-
target: { id: "postgres" },
|
|
27
|
-
precheck: [{
|
|
28
|
-
description: "verify extension \"vector\" is not already enabled",
|
|
29
|
-
sql: "SELECT NOT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector')"
|
|
30
|
-
}],
|
|
31
|
-
execute: [{
|
|
32
|
-
description: "create extension \"vector\"",
|
|
33
|
-
sql: "CREATE EXTENSION IF NOT EXISTS vector"
|
|
34
|
-
}],
|
|
35
|
-
postcheck: [{
|
|
36
|
-
description: "confirm extension \"vector\" is enabled",
|
|
37
|
-
sql: "SELECT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector')"
|
|
38
|
-
}]
|
|
39
|
-
}]
|
|
40
|
-
}] };
|
|
183
|
+
const pgvectorContractSpace = {
|
|
184
|
+
contractJson: pgvectorContract,
|
|
185
|
+
migrations: [pgvectorBaselineMigration],
|
|
186
|
+
headRef: pgvectorHeadRef
|
|
187
|
+
};
|
|
41
188
|
const pgvectorExtensionDescriptor = {
|
|
42
189
|
...pgvectorPackMeta,
|
|
190
|
+
id: PGVECTOR_SPACE_ID,
|
|
191
|
+
contractSpace: pgvectorContractSpace,
|
|
43
192
|
types: {
|
|
44
193
|
...pgvectorPackMeta.types,
|
|
45
194
|
codecTypes: {
|
|
@@ -47,15 +196,13 @@ const pgvectorExtensionDescriptor = {
|
|
|
47
196
|
controlPlaneHooks: { [PGVECTOR_CODEC_ID]: vectorControlPlaneHooks }
|
|
48
197
|
}
|
|
49
198
|
},
|
|
50
|
-
queryOperations: () => pgvectorQueryOperations,
|
|
51
|
-
databaseDependencies: pgvectorDatabaseDependencies,
|
|
199
|
+
queryOperations: () => pgvectorQueryOperations(),
|
|
52
200
|
create: () => ({
|
|
53
201
|
familyId: "sql",
|
|
54
202
|
targetId: "postgres"
|
|
55
203
|
})
|
|
56
204
|
};
|
|
57
|
-
var control_default = pgvectorExtensionDescriptor;
|
|
58
|
-
|
|
59
205
|
//#endregion
|
|
60
|
-
export {
|
|
206
|
+
export { pgvectorExtensionDescriptor as default, pgvectorExtensionDescriptor };
|
|
207
|
+
|
|
61
208
|
//# sourceMappingURL=control.mjs.map
|
package/dist/control.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"control.mjs","names":["vectorControlPlaneHooks: CodecControlHooks","pgvectorDatabaseDependencies: ComponentDatabaseDependencies<unknown>","pgvectorExtensionDescriptor: SqlControlExtensionDescriptor<'postgres'>"],"sources":["../src/exports/control.ts"],"sourcesContent":["import type {\n CodecControlHooks,\n ComponentDatabaseDependencies,\n SqlControlExtensionDescriptor,\n} from '@prisma-next/family-sql/control';\nimport { pgvectorPackMeta, pgvectorQueryOperations } from '../core/descriptor-meta';\n\nconst PGVECTOR_CODEC_ID = 'pg/vector@1' as const;\n\nfunction buildVectorIdentityValue(typeParams: Record<string, unknown> | undefined): string | null {\n const length = typeParams?.['length'];\n if (typeof length !== 'number' || !Number.isInteger(length) || length <= 0) {\n return null;\n }\n\n const zeroVector = `[${new Array(length).fill('0').join(',')}]`;\n return `'${zeroVector}'::vector`;\n}\n\nconst vectorControlPlaneHooks: CodecControlHooks = {\n expandNativeType: ({ nativeType, typeParams }) => {\n const length = typeParams?.['length'];\n if (typeof length === 'number' && Number.isInteger(length) && length > 0) {\n return `${nativeType}(${length})`;\n }\n return nativeType;\n },\n resolveIdentityValue: ({ typeParams }) => buildVectorIdentityValue(typeParams),\n};\n\nconst pgvectorDatabaseDependencies: ComponentDatabaseDependencies<unknown> = {\n init: [\n {\n id: 'postgres.extension.vector',\n label: 'Enable vector extension',\n install: [\n {\n id: 'extension.vector',\n label: 'Enable extension \"vector\"',\n summary: 'Ensures the vector extension is available for pgvector operations',\n operationClass: 'additive',\n target: { id: 'postgres' },\n precheck: [\n {\n description: 'verify extension \"vector\" is not already enabled',\n sql: \"SELECT NOT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector')\",\n },\n ],\n execute: [\n {\n description: 'create extension \"vector\"',\n sql: 'CREATE EXTENSION IF NOT EXISTS vector',\n },\n ],\n postcheck: [\n {\n description: 'confirm extension \"vector\" is enabled',\n sql: \"SELECT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector')\",\n },\n ],\n },\n ],\n },\n ],\n};\n\nconst pgvectorExtensionDescriptor: SqlControlExtensionDescriptor<'postgres'> = {\n ...pgvectorPackMeta,\n types: {\n ...pgvectorPackMeta.types,\n codecTypes: {\n ...pgvectorPackMeta.types.codecTypes,\n controlPlaneHooks: {\n [PGVECTOR_CODEC_ID]: vectorControlPlaneHooks,\n },\n },\n },\n queryOperations: () => pgvectorQueryOperations,\n databaseDependencies: pgvectorDatabaseDependencies,\n create: () => ({\n familyId: 'sql' as const,\n targetId: 'postgres' as const,\n }),\n};\n\nexport { pgvectorExtensionDescriptor };\nexport default pgvectorExtensionDescriptor;\n"],"mappings":";;;AAOA,MAAM,oBAAoB;AAE1B,SAAS,yBAAyB,YAAgE;CAChG,MAAM,SAAS,aAAa;AAC5B,KAAI,OAAO,WAAW,YAAY,CAAC,OAAO,UAAU,OAAO,IAAI,UAAU,EACvE,QAAO;AAIT,QAAO,IADY,IAAI,IAAI,MAAM,OAAO,CAAC,KAAK,IAAI,CAAC,KAAK,IAAI,CAAC,GACvC;;AAGxB,MAAMA,0BAA6C;CACjD,mBAAmB,EAAE,YAAY,iBAAiB;EAChD,MAAM,SAAS,aAAa;AAC5B,MAAI,OAAO,WAAW,YAAY,OAAO,UAAU,OAAO,IAAI,SAAS,EACrE,QAAO,GAAG,WAAW,GAAG,OAAO;AAEjC,SAAO;;CAET,uBAAuB,EAAE,iBAAiB,yBAAyB,WAAW;CAC/E;AAED,MAAMC,+BAAuE,EAC3E,MAAM,CACJ;CACE,IAAI;CACJ,OAAO;CACP,SAAS,CACP;EACE,IAAI;EACJ,OAAO;EACP,SAAS;EACT,gBAAgB;EAChB,QAAQ,EAAE,IAAI,YAAY;EAC1B,UAAU,CACR;GACE,aAAa;GACb,KAAK;GACN,CACF;EACD,SAAS,CACP;GACE,aAAa;GACb,KAAK;GACN,CACF;EACD,WAAW,CACT;GACE,aAAa;GACb,KAAK;GACN,CACF;EACF,CACF;CACF,CACF,EACF;AAED,MAAMC,8BAAyE;CAC7E,GAAG;CACH,OAAO;EACL,GAAG,iBAAiB;EACpB,YAAY;GACV,GAAG,iBAAiB,MAAM;GAC1B,mBAAmB,GAChB,oBAAoB,yBACtB;GACF;EACF;CACD,uBAAuB;CACvB,sBAAsB;CACtB,eAAe;EACb,UAAU;EACV,UAAU;EACX;CACF;AAGD,sBAAe"}
|
|
1
|
+
{"version":3,"file":"control.mjs","names":[],"sources":["../src/core/contract-space-constants.ts","../src/core/contract.ts","../src/core/migrations.ts","../src/exports/control.ts"],"sourcesContent":["/**\n * Static names and identifiers used across pgvector's contract space.\n *\n * Centralised here so the contract IR (`./contract`), the baseline\n * migration ops (`./migrations`), the head ref, and the descriptor\n * (`../exports/control`) all reference the same values without typos.\n *\n * The space identifier `'pgvector'` is what the framework writes to\n * `migrations/pgvector/` in the user's repo and what the marker table's\n * `space` column carries for pgvector-owned rows.\n *\n * The `pgvector:*` invariantId namespace is locked here — once\n * published, an invariantId is immutable so downstream consumers can\n * reference it by literal string match.\n */\n\nexport const PGVECTOR_SPACE_ID = 'pgvector' as const;\n\nexport const PGVECTOR_NATIVE_TYPE = 'vector' as const;\n\nexport const PGVECTOR_BASELINE_MIGRATION_NAME = '20260601T0000_install_vector_extension' as const;\n\n/**\n * `pgvector:*` invariantIds emitted by the baseline migration. Each id,\n * once published, is immutable: downstream consumers (other extensions,\n * the marker table) reference them by literal string match.\n */\nexport const PGVECTOR_INVARIANTS = {\n installVector: 'pgvector:install-vector-v1',\n} as const;\n","/**\n * pgvector contract space — declares the parameterised native type\n * `vector(N)` that user columns can name as `nativeType`.\n *\n * Unlike CipherStash's typed objects (composite types / domains / enums\n * — deferred behind `meta.cipherstashFutureIR` until the IR vocabulary\n * gains first-class support), pgvector's `vector` is a parameterised\n * native type and *is* representable in today's IR via\n * {@link StorageTypeInstance}: `{ codecId, nativeType, typeParams }`.\n * The contract registers a representative instance under\n * `storage.types.vector` so the verifier sees the type as part of\n * pgvector's space contribution and so the pinned `contract.json` on\n * disk is materially distinct from an empty space.\n *\n * Per-column instances on the user's side carry concrete\n * `typeParams.length` (e.g. `vector(1536)`); the registration here\n * declares the parameterised shape — it is not consumed as a literal\n * column type by any user table.\n */\n\nimport { computeStorageHash } from '@prisma-next/contract/hashing';\nimport { type Contract, coreHash, profileHash } from '@prisma-next/contract/types';\nimport type { SqlStorage } from '@prisma-next/sql-contract/types';\nimport { VECTOR_CODEC_ID } from './constants';\nimport { PGVECTOR_NATIVE_TYPE } from './contract-space-constants';\n\nconst TARGET = 'postgres' as const;\nconst TARGET_FAMILY = 'sql' as const;\n\n/**\n * Storage body for the contract — pgvector ships no tables of its own;\n * the `vector` parameterised native type is registered under\n * `storage.types` so pgvector's IR contribution is non-empty and the\n * pinned `contract.json` on disk differs materially from an empty\n * extension space.\n *\n * Authored without `storageHash` here so {@link computeStorageHash} can\n * digest the canonical body (the hashing pipeline panics if asked to\n * hash an object that already carries its own output — see\n * `assertDescriptorSelfConsistency`'s storage-hash strip).\n */\nconst storageBody = {\n tables: {},\n types: {\n [PGVECTOR_NATIVE_TYPE]: {\n codecId: VECTOR_CODEC_ID,\n nativeType: PGVECTOR_NATIVE_TYPE,\n typeParams: {},\n },\n },\n};\n\n/** Content-addressed hash of pgvector's storage IR. */\nexport const PGVECTOR_STORAGE_HASH = computeStorageHash({\n target: TARGET,\n targetFamily: TARGET_FAMILY,\n storage: storageBody,\n});\n\n/** pgvector's contract value, exposed via the descriptor's `contractSpace.contractJson`. */\nexport const pgvectorContract: Contract<SqlStorage> = {\n target: TARGET,\n targetFamily: TARGET_FAMILY,\n roots: {},\n models: {},\n capabilities: {},\n extensionPacks: {},\n meta: {},\n profileHash: profileHash('pgvector-extension-profile-v1'),\n storage: {\n ...storageBody,\n storageHash: coreHash(PGVECTOR_STORAGE_HASH),\n },\n};\n","/**\n * pgvector contract space — baseline migration package.\n *\n * An extension's `contractSpace.migrations` is a list of in-memory\n * `MigrationPackage` values whose `ops` carry framework-level\n * `MigrationPlanOperation`s. The SQL family runner reads the additional\n * runtime fields (`target`, `precheck`, `execute`, `postcheck`) at\n * apply time.\n *\n * Ships a single baseline migration whose only op is\n * {@link installVectorExtensionOp} — it carries the\n * `CREATE EXTENSION IF NOT EXISTS vector` DDL plus a postcondition that\n * confirms the extension landed. Mirrors the prior\n * `databaseDependencies.init[0]` shape (precheck / execute / postcheck)\n * but as a `MigrationPackage` op so the framework's per-space runner /\n * verifier can manage it the same way it manages an application's own\n * migrations.\n *\n * The op carries the stable `pgvector:install-vector-v1` invariantId —\n * once published it is immutable.\n */\n\nimport type { SqlMigrationPlanOperation } from '@prisma-next/family-sql/control';\nimport type {\n ContractSpaceHeadRef,\n MigrationPackage,\n MigrationPlanOperation,\n} from '@prisma-next/framework-components/control';\nimport { computeMigrationHash } from '@prisma-next/migration-tools/hash';\nimport { PGVECTOR_STORAGE_HASH, pgvectorContract } from './contract';\nimport { PGVECTOR_BASELINE_MIGRATION_NAME, PGVECTOR_INVARIANTS } from './contract-space-constants';\n\n/**\n * Postgres-style `target.details` shape the SQL runner consumes when\n * executing extension-space ops. pgvector targets only Postgres;\n * locking the target id here keeps the per-op `target` literal narrow\n * without coupling to the Postgres adapter package's\n * `PostgresPlanTargetDetails`.\n */\ntype PostgresTargetDetails = {\n readonly schema: string;\n readonly objectType: 'extension';\n readonly name: string;\n};\n\nconst installVectorExtensionOp: SqlMigrationPlanOperation<unknown> = {\n id: 'pgvector.install-vector-extension',\n label: 'Enable extension \"vector\"',\n operationClass: 'additive',\n invariantId: PGVECTOR_INVARIANTS.installVector,\n target: {\n id: 'postgres',\n details: {\n schema: 'public',\n objectType: 'extension',\n name: 'vector',\n } satisfies PostgresTargetDetails,\n },\n precheck: [\n {\n description: 'verify extension \"vector\" is not already enabled',\n sql: \"SELECT NOT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector')\",\n },\n ],\n execute: [\n {\n description: 'create extension \"vector\"',\n sql: 'CREATE EXTENSION IF NOT EXISTS vector',\n },\n ],\n postcheck: [\n {\n description: 'confirm extension \"vector\" is enabled',\n sql: \"SELECT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector')\",\n },\n ],\n};\n\nconst pgvectorBaselineOps: readonly MigrationPlanOperation[] = [installVectorExtensionOp];\n\n/** Sorted list of invariantIds the baseline migration provides. */\nexport const PGVECTOR_BASELINE_INVARIANTS: readonly string[] = (() => {\n const ids = pgvectorBaselineOps\n .map((op) => op.invariantId)\n .filter((id): id is string => typeof id === 'string');\n return [...new Set(ids)].sort();\n})();\n\nconst baselineMetadataWithoutHash: Omit<MigrationPackage['metadata'], 'migrationHash'> = {\n from: null,\n to: PGVECTOR_STORAGE_HASH,\n fromContract: null,\n toContract: pgvectorContract,\n hints: { used: [], applied: [], plannerVersion: '2.0.0' },\n labels: [],\n providedInvariants: PGVECTOR_BASELINE_INVARIANTS,\n createdAt: '2026-06-01T00:00:00.000Z',\n};\n\n/**\n * Baseline migration package the descriptor publishes via\n * `contractSpace.migrations`. The framework's emitter writes this to\n * `migrations/pgvector/<dirName>/{manifest,ops,contract}.json` in the\n * user's repo at `migrate` time.\n */\nexport const pgvectorBaselineMigration: MigrationPackage = {\n dirName: PGVECTOR_BASELINE_MIGRATION_NAME,\n metadata: {\n ...baselineMetadataWithoutHash,\n migrationHash: computeMigrationHash(baselineMetadataWithoutHash, pgvectorBaselineOps),\n },\n ops: pgvectorBaselineOps,\n};\n\n/**\n * Pinned head ref the descriptor publishes. The framework writes this\n * verbatim to `migrations/pgvector/refs/head.json` (with `invariants`\n * sorted alphabetically per the canonicalisation rules); the runner's\n * `findPathWithDecision` step consults `head.json` to decide which\n * migrations need to apply.\n */\nexport const pgvectorHeadRef: ContractSpaceHeadRef = {\n hash: PGVECTOR_STORAGE_HASH,\n invariants: PGVECTOR_BASELINE_INVARIANTS,\n};\n","/**\n * Control-plane descriptor for the pgvector extension.\n *\n * Exposes a `contractSpace` so the framework's per-space planner /\n * runner / verifier (project: extension-contract-spaces, M1+M2)\n * manages the pgvector extension's database scaffolding the same way\n * it manages an application's own schema. The descriptor is consumed\n * by the framework only at authoring time (`migrate`); apply / verify\n * paths read the user's repo (`migrations/pgvector/...`) instead — see\n * project spec NFR3 / FR2 / FR10.\n *\n * `databaseDependencies` is intentionally absent — pgvector was\n * migrated off the legacy `databaseDependencies.init` mechanism in M4\n * (project spec FR13). The `CREATE EXTENSION IF NOT EXISTS vector`\n * DDL the legacy entry carried now lives as the body of the\n * `installVectorExtension` op inside the baseline migration package\n * (`../core/migrations.ts`). Presence of `contractSpace` is the\n * shipping-strategy gate: the framework loads the contract space and\n * ignores any `databaseDependencies` block (project plan §\n * \"Shipping Strategy\"). M5 removes the field at the framework level.\n */\n\nimport type { Contract } from '@prisma-next/contract/types';\nimport type {\n CodecControlHooks,\n SqlControlExtensionDescriptor,\n} from '@prisma-next/family-sql/control';\nimport type { ContractSpace } from '@prisma-next/framework-components/control';\nimport type { SqlStorage } from '@prisma-next/sql-contract/types';\nimport { pgvectorContract } from '../core/contract';\nimport { PGVECTOR_SPACE_ID } from '../core/contract-space-constants';\nimport { pgvectorPackMeta, pgvectorQueryOperations } from '../core/descriptor-meta';\nimport { pgvectorBaselineMigration, pgvectorHeadRef } from '../core/migrations';\n\nconst PGVECTOR_CODEC_ID = 'pg/vector@1' as const;\n\nfunction buildVectorIdentityValue(typeParams: Record<string, unknown> | undefined): string | null {\n const length = typeParams?.['length'];\n if (typeof length !== 'number' || !Number.isInteger(length) || length <= 0) {\n return null;\n }\n\n const zeroVector = `[${new Array(length).fill('0').join(',')}]`;\n return `'${zeroVector}'::vector`;\n}\n\nconst vectorControlPlaneHooks: CodecControlHooks = {\n expandNativeType: ({ nativeType, typeParams }) => {\n const length = typeParams?.['length'];\n if (typeof length === 'number' && Number.isInteger(length) && length > 0) {\n return `${nativeType}(${length})`;\n }\n return nativeType;\n },\n resolveIdentityValue: ({ typeParams }) => buildVectorIdentityValue(typeParams),\n};\n\nconst pgvectorContractSpace: ContractSpace<Contract<SqlStorage>> = {\n contractJson: pgvectorContract,\n migrations: [pgvectorBaselineMigration],\n headRef: pgvectorHeadRef,\n};\n\nconst pgvectorExtensionDescriptor: SqlControlExtensionDescriptor<'postgres'> = {\n ...pgvectorPackMeta,\n id: PGVECTOR_SPACE_ID,\n contractSpace: pgvectorContractSpace,\n types: {\n ...pgvectorPackMeta.types,\n codecTypes: {\n ...pgvectorPackMeta.types.codecTypes,\n controlPlaneHooks: {\n [PGVECTOR_CODEC_ID]: vectorControlPlaneHooks,\n },\n },\n },\n queryOperations: () => pgvectorQueryOperations(),\n create: () => ({\n familyId: 'sql' as const,\n targetId: 'postgres' as const,\n }),\n};\n\nexport { pgvectorExtensionDescriptor };\nexport default pgvectorExtensionDescriptor;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAgBA,MAAa,oBAAoB;AAEjC,MAAa,uBAAuB;AAEpC,MAAa,mCAAmC;;;;;;AAOhD,MAAa,sBAAsB,EACjC,eAAe,8BAChB;;;;;;;;;;;;;;;;;;;;;;ACHD,MAAM,SAAS;AACf,MAAM,gBAAgB;;;;;;;;;;;;;AActB,MAAM,cAAc;CAClB,QAAQ,EAAE;CACV,OAAO,GACJ,uBAAuB;EACtB,SAAS;EACT,YAAY;EACZ,YAAY,EAAE;EACf,EACF;CACF;;AAGD,MAAa,wBAAwB,mBAAmB;CACtD,QAAQ;CACR,cAAc;CACd,SAAS;CACV,CAAC;;AAGF,MAAa,mBAAyC;CACpD,QAAQ;CACR,cAAc;CACd,OAAO,EAAE;CACT,QAAQ,EAAE;CACV,cAAc,EAAE;CAChB,gBAAgB,EAAE;CAClB,MAAM,EAAE;CACR,aAAa,YAAY,gCAAgC;CACzD,SAAS;EACP,GAAG;EACH,aAAa,SAAS,sBAAsB;EAC7C;CACF;;;ACKD,MAAM,sBAAyD,CAAC;CAhC9D,IAAI;CACJ,OAAO;CACP,gBAAgB;CAChB,aAAa,oBAAoB;CACjC,QAAQ;EACN,IAAI;EACJ,SAAS;GACP,QAAQ;GACR,YAAY;GACZ,MAAM;GACP;EACF;CACD,UAAU,CACR;EACE,aAAa;EACb,KAAK;EACN,CACF;CACD,SAAS,CACP;EACE,aAAa;EACb,KAAK;EACN,CACF;CACD,WAAW,CACT;EACE,aAAa;EACb,KAAK;EACN,CACF;CAGqF,CAAC;;AAGzF,MAAa,sCAAyD;CACpE,MAAM,MAAM,oBACT,KAAK,OAAO,GAAG,YAAY,CAC3B,QAAQ,OAAqB,OAAO,OAAO,SAAS;CACvD,OAAO,CAAC,GAAG,IAAI,IAAI,IAAI,CAAC,CAAC,MAAM;IAC7B;AAEJ,MAAM,8BAAmF;CACvF,MAAM;CACN,IAAI;CACJ,cAAc;CACd,YAAY;CACZ,OAAO;EAAE,MAAM,EAAE;EAAE,SAAS,EAAE;EAAE,gBAAgB;EAAS;CACzD,QAAQ,EAAE;CACV,oBAAoB;CACpB,WAAW;CACZ;;;;;;;AAQD,MAAa,4BAA8C;CACzD,SAAS;CACT,UAAU;EACR,GAAG;EACH,eAAe,qBAAqB,6BAA6B,oBAAoB;EACtF;CACD,KAAK;CACN;;;;;;;;AASD,MAAa,kBAAwC;CACnD,MAAM;CACN,YAAY;CACb;;;AC1FD,MAAM,oBAAoB;AAE1B,SAAS,yBAAyB,YAAgE;CAChG,MAAM,SAAS,aAAa;CAC5B,IAAI,OAAO,WAAW,YAAY,CAAC,OAAO,UAAU,OAAO,IAAI,UAAU,GACvE,OAAO;CAIT,OAAO,IAAI,IADY,IAAI,MAAM,OAAO,CAAC,KAAK,IAAI,CAAC,KAAK,IAAI,CAAC,GACvC;;AAGxB,MAAM,0BAA6C;CACjD,mBAAmB,EAAE,YAAY,iBAAiB;EAChD,MAAM,SAAS,aAAa;EAC5B,IAAI,OAAO,WAAW,YAAY,OAAO,UAAU,OAAO,IAAI,SAAS,GACrE,OAAO,GAAG,WAAW,GAAG,OAAO;EAEjC,OAAO;;CAET,uBAAuB,EAAE,iBAAiB,yBAAyB,WAAW;CAC/E;AAED,MAAM,wBAA6D;CACjE,cAAc;CACd,YAAY,CAAC,0BAA0B;CACvC,SAAS;CACV;AAED,MAAM,8BAAyE;CAC7E,GAAG;CACH,IAAI;CACJ,eAAe;CACf,OAAO;EACL,GAAG,iBAAiB;EACpB,YAAY;GACV,GAAG,iBAAiB,MAAM;GAC1B,mBAAmB,GAChB,oBAAoB,yBACtB;GACF;EACF;CACD,uBAAuB,yBAAyB;CAChD,eAAe;EACb,UAAU;EACV,UAAU;EACX;CACF"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { n as VECTOR_MAX_DIM, t as VECTOR_CODEC_ID } from "./constants-DX-00vYk.mjs";
|
|
2
|
+
import { buildOperation, refsOf, toExpr } from "@prisma-next/sql-relational-core/expression";
|
|
3
|
+
import { buildCodecDescriptorRegistry } from "@prisma-next/sql-relational-core/codec-descriptor-registry";
|
|
4
|
+
import { CodecDescriptorImpl, CodecImpl } from "@prisma-next/framework-components/codec";
|
|
5
|
+
import { type } from "arktype";
|
|
6
|
+
//#region src/core/authoring.ts
|
|
7
|
+
const pgvectorAuthoringTypes = { pgvector: { Vector: {
|
|
8
|
+
kind: "typeConstructor",
|
|
9
|
+
args: [{
|
|
10
|
+
kind: "number",
|
|
11
|
+
name: "length",
|
|
12
|
+
integer: true,
|
|
13
|
+
minimum: 1,
|
|
14
|
+
maximum: VECTOR_MAX_DIM
|
|
15
|
+
}],
|
|
16
|
+
output: {
|
|
17
|
+
codecId: "pg/vector@1",
|
|
18
|
+
nativeType: "vector",
|
|
19
|
+
typeParams: { length: {
|
|
20
|
+
kind: "arg",
|
|
21
|
+
index: 0
|
|
22
|
+
} }
|
|
23
|
+
}
|
|
24
|
+
} } };
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/core/codecs.ts
|
|
27
|
+
const vectorParamsSchema = type({ length: "number" }).narrow((params, ctx) => {
|
|
28
|
+
const { length } = params;
|
|
29
|
+
if (!Number.isInteger(length)) return ctx.mustBe("an integer");
|
|
30
|
+
if (length < 1 || length > 16e3) return ctx.mustBe(`in the range [1, ${VECTOR_MAX_DIM}]`);
|
|
31
|
+
return true;
|
|
32
|
+
});
|
|
33
|
+
const PG_VECTOR_META = { db: { sql: { postgres: { nativeType: "vector" } } } };
|
|
34
|
+
var PgVectorCodec = class extends CodecImpl {
|
|
35
|
+
length;
|
|
36
|
+
constructor(descriptor, length) {
|
|
37
|
+
super(descriptor);
|
|
38
|
+
this.length = length;
|
|
39
|
+
}
|
|
40
|
+
assertVector(value) {
|
|
41
|
+
if (!Array.isArray(value)) throw new Error("Vector value must be an array of numbers");
|
|
42
|
+
if (!value.every((v) => typeof v === "number")) throw new Error("Vector value must contain only numbers");
|
|
43
|
+
if (this.length !== void 0 && value.length !== this.length) throw new Error(`Vector length mismatch: expected ${this.length}, got ${value.length}`);
|
|
44
|
+
}
|
|
45
|
+
async encode(value, _ctx) {
|
|
46
|
+
this.assertVector(value);
|
|
47
|
+
return `[${value.join(",")}]`;
|
|
48
|
+
}
|
|
49
|
+
async decode(wire, _ctx) {
|
|
50
|
+
if (typeof wire !== "string") throw new Error("Vector wire value must be a string");
|
|
51
|
+
if (!wire.startsWith("[") || !wire.endsWith("]")) throw new Error(`Invalid vector format: expected "[...]", got "${wire}"`);
|
|
52
|
+
const content = wire.slice(1, -1).trim();
|
|
53
|
+
const parsed = content === "" ? [] : content.split(",").map((v) => {
|
|
54
|
+
const num = Number.parseFloat(v.trim());
|
|
55
|
+
if (Number.isNaN(num)) throw new Error(`Invalid vector value: "${v}" is not a number`);
|
|
56
|
+
return num;
|
|
57
|
+
});
|
|
58
|
+
this.assertVector(parsed);
|
|
59
|
+
return parsed;
|
|
60
|
+
}
|
|
61
|
+
encodeJson(value) {
|
|
62
|
+
this.assertVector(value);
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
decodeJson(json) {
|
|
66
|
+
this.assertVector(json);
|
|
67
|
+
return json;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var PgVectorDescriptor = class extends CodecDescriptorImpl {
|
|
71
|
+
codecId = VECTOR_CODEC_ID;
|
|
72
|
+
traits = ["equality"];
|
|
73
|
+
targetTypes = ["vector"];
|
|
74
|
+
meta = PG_VECTOR_META;
|
|
75
|
+
paramsSchema = vectorParamsSchema;
|
|
76
|
+
renderOutputType(params) {
|
|
77
|
+
return `Vector<${params.length}>`;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The runtime calls `factory(undefined)(ctx)` to materialize a representative codec for parameterized descriptors that ship a no-params column variant (here, `vectorColumn` vs `vector(N)`). The runtime cast widens `params` to `unknown`, so guarding with an optional read keeps the typed call site (`factory({ length })`) strict while still producing a length-agnostic codec for representative use. Encode/decode for an undimensioned column run through this representative; the wire format `[v1,v2,...]` is dimension-independent.
|
|
81
|
+
*/
|
|
82
|
+
factory(params) {
|
|
83
|
+
return () => new PgVectorCodec(this, params?.length);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
const codecDescriptorMap = { vector: new PgVectorDescriptor() };
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/core/registry.ts
|
|
89
|
+
/**
|
|
90
|
+
* Registry of every codec descriptor shipped by `@prisma-next/extension-pgvector`.
|
|
91
|
+
*
|
|
92
|
+
* 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.
|
|
93
|
+
*/
|
|
94
|
+
const pgvectorCodecRegistry = buildCodecDescriptorRegistry(Object.values(codecDescriptorMap));
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/core/descriptor-meta.ts
|
|
97
|
+
const pgvectorTypeId = "pg/vector@1";
|
|
98
|
+
function pgvectorQueryOperations() {
|
|
99
|
+
return {
|
|
100
|
+
cosineDistance: {
|
|
101
|
+
self: { codecId: pgvectorTypeId },
|
|
102
|
+
impl: (self, other) => {
|
|
103
|
+
const selfRefs = refsOf(self);
|
|
104
|
+
return buildOperation({
|
|
105
|
+
method: "cosineDistance",
|
|
106
|
+
args: [toExpr(self, pgvectorTypeId, selfRefs), toExpr(other, pgvectorTypeId, selfRefs)],
|
|
107
|
+
returns: {
|
|
108
|
+
codecId: "pg/float8@1",
|
|
109
|
+
nullable: false
|
|
110
|
+
},
|
|
111
|
+
lowering: {
|
|
112
|
+
targetFamily: "sql",
|
|
113
|
+
strategy: "function",
|
|
114
|
+
template: "{{self}} <=> {{arg0}}"
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
cosineSimilarity: {
|
|
120
|
+
self: { codecId: pgvectorTypeId },
|
|
121
|
+
impl: (self, other) => {
|
|
122
|
+
const selfRefs = refsOf(self);
|
|
123
|
+
return buildOperation({
|
|
124
|
+
method: "cosineSimilarity",
|
|
125
|
+
args: [toExpr(self, pgvectorTypeId, selfRefs), toExpr(other, pgvectorTypeId, selfRefs)],
|
|
126
|
+
returns: {
|
|
127
|
+
codecId: "pg/float8@1",
|
|
128
|
+
nullable: false
|
|
129
|
+
},
|
|
130
|
+
lowering: {
|
|
131
|
+
targetFamily: "sql",
|
|
132
|
+
strategy: "function",
|
|
133
|
+
template: "1 - ({{self}} <=> {{arg0}})"
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
const pgvectorPackMeta = {
|
|
141
|
+
kind: "extension",
|
|
142
|
+
id: "pgvector",
|
|
143
|
+
familyId: "sql",
|
|
144
|
+
targetId: "postgres",
|
|
145
|
+
version: "0.0.1",
|
|
146
|
+
capabilities: { postgres: { "pgvector.cosine": true } },
|
|
147
|
+
authoring: { type: pgvectorAuthoringTypes },
|
|
148
|
+
types: {
|
|
149
|
+
codecTypes: {
|
|
150
|
+
codecDescriptors: Array.from(pgvectorCodecRegistry.values()),
|
|
151
|
+
import: {
|
|
152
|
+
package: "@prisma-next/extension-pgvector/codec-types",
|
|
153
|
+
named: "CodecTypes",
|
|
154
|
+
alias: "PgVectorTypes"
|
|
155
|
+
},
|
|
156
|
+
typeImports: [{
|
|
157
|
+
package: "@prisma-next/extension-pgvector/codec-types",
|
|
158
|
+
named: "Vector",
|
|
159
|
+
alias: "Vector"
|
|
160
|
+
}]
|
|
161
|
+
},
|
|
162
|
+
queryOperationTypes: { import: {
|
|
163
|
+
package: "@prisma-next/extension-pgvector/operation-types",
|
|
164
|
+
named: "QueryOperationTypes",
|
|
165
|
+
alias: "PgVectorQueryOperationTypes"
|
|
166
|
+
} },
|
|
167
|
+
storage: [{
|
|
168
|
+
typeId: pgvectorTypeId,
|
|
169
|
+
familyId: "sql",
|
|
170
|
+
targetId: "postgres",
|
|
171
|
+
nativeType: "vector"
|
|
172
|
+
}]
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
//#endregion
|
|
176
|
+
export { pgvectorQueryOperations as n, pgvectorCodecRegistry as r, pgvectorPackMeta as t };
|
|
177
|
+
|
|
178
|
+
//# sourceMappingURL=descriptor-meta-DEgJjLLi.mjs.map
|