@prisma-next/extension-pgvector 0.5.0-dev.8 → 0.5.0-dev.81

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.
Files changed (41) hide show
  1. package/README.md +5 -2
  2. package/dist/codec-types-yMSpEJJM.d.mts +63 -0
  3. package/dist/codec-types-yMSpEJJM.d.mts.map +1 -0
  4. package/dist/codec-types.d.mts +1 -1
  5. package/dist/codec-types.mjs +1 -1
  6. package/dist/column-types.d.mts +2 -5
  7. package/dist/column-types.d.mts.map +1 -1
  8. package/dist/column-types.mjs +4 -7
  9. package/dist/column-types.mjs.map +1 -1
  10. package/dist/{constants-Co5golCK.mjs → constants-DX-00vYk.mjs} +2 -2
  11. package/dist/{constants-Co5golCK.mjs.map → constants-DX-00vYk.mjs.map} +1 -1
  12. package/dist/control.d.mts.map +1 -1
  13. package/dist/control.mjs +177 -30
  14. package/dist/control.mjs.map +1 -1
  15. package/dist/descriptor-meta-CBnWOxms.mjs +182 -0
  16. package/dist/descriptor-meta-CBnWOxms.mjs.map +1 -0
  17. package/dist/operation-types.d.mts +18 -42
  18. package/dist/operation-types.d.mts.map +1 -1
  19. package/dist/operation-types.mjs +1 -1
  20. package/dist/pack.d.mts +3 -4
  21. package/dist/pack.d.mts.map +1 -1
  22. package/dist/pack.mjs +2 -3
  23. package/dist/runtime.d.mts +10 -1
  24. package/dist/runtime.d.mts.map +1 -1
  25. package/dist/runtime.mjs +6 -25
  26. package/dist/runtime.mjs.map +1 -1
  27. package/package.json +20 -17
  28. package/src/core/codecs.ts +127 -59
  29. package/src/core/contract-space-constants.ts +30 -0
  30. package/src/core/contract.ts +74 -0
  31. package/src/core/descriptor-meta.ts +55 -28
  32. package/src/core/migrations.ts +125 -0
  33. package/src/core/registry.ts +11 -0
  34. package/src/exports/column-types.ts +3 -6
  35. package/src/exports/control.ts +35 -37
  36. package/src/exports/runtime.ts +11 -41
  37. package/src/types/operation-types.ts +24 -30
  38. package/dist/codec-types-BifaP625.d.mts +0 -27
  39. package/dist/codec-types-BifaP625.d.mts.map +0 -1
  40. package/dist/descriptor-meta-BQbvJJxu.mjs +0 -148
  41. package/dist/descriptor-meta-BQbvJJxu.mjs.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"descriptor-meta-CBnWOxms.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\nexport class PgVectorCodec extends CodecImpl<\n typeof VECTOR_CODEC_ID,\n readonly ['equality'],\n string,\n number[]\n> {\n readonly length: number | undefined;\n\n constructor(descriptor: AnyCodecDescriptor, length: number | undefined) {\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 if (!value.every((v) => typeof v === 'number')) {\n throw new Error('Vector value must contain only numbers');\n }\n if (this.length !== undefined && 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 if (!wire.startsWith('[') || !wire.endsWith(']')) {\n throw new Error(`Invalid vector format: expected \"[...]\", got \"${wire}\"`);\n }\n const content = wire.slice(1, -1).trim();\n const parsed =\n content === ''\n ? []\n : content.split(',').map((v) => {\n const num = Number.parseFloat(v.trim());\n if (Number.isNaN(num)) {\n throw new Error(`Invalid vector value: \"${v}\" is not a number`);\n }\n return num;\n });\n this.assertVector(parsed);\n return parsed;\n }\n\n encodeJson(value: number[]): JsonValue {\n this.assertVector(value);\n return value;\n }\n\n decodeJson(json: JsonValue): number[] {\n this.assertVector(json);\n return json;\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 /**\n * 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.\n */\n override factory(params: VectorParams): (ctx: CodecInstanceContext) => PgVectorCodec {\n return () => new PgVectorCodec(this, (params as VectorParams | undefined)?.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 type { SqlOperationDescriptor } from '@prisma-next/sql-operations';\nimport {\n buildOperation,\n type CodecExpression,\n type Expression,\n refsOf,\n toExpr,\n} from '@prisma-next/sql-relational-core/expression';\nimport type { CodecTypes } from '../types/codec-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<\n CT extends CodecTypesBase,\n>(): readonly SqlOperationDescriptor[] {\n return [\n {\n method: '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 selfRefs = refsOf(self);\n return buildOperation({\n method: 'cosineDistance',\n args: [toExpr(self, pgvectorTypeId, selfRefs), toExpr(other, pgvectorTypeId, selfRefs)],\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 {\n method: '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 selfRefs = refsOf(self);\n return buildOperation({\n method: 'cosineSimilarity',\n args: [toExpr(self, pgvectorTypeId, selfRefs), toExpr(other, pgvectorTypeId, selfRefs)],\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":"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"}
@@ -1,7 +1,11 @@
1
+ import { CodecExpression, Expression } from "@prisma-next/sql-relational-core/expression";
1
2
  import { SqlQueryOperationTypes } from "@prisma-next/sql-contract/types";
2
3
 
3
4
  //#region src/types/operation-types.d.ts
4
-
5
+ type CodecTypesBase = Record<string, {
6
+ readonly input: unknown;
7
+ readonly output: unknown;
8
+ }>;
5
9
  /**
6
10
  * Operation type definitions for pgvector extension.
7
11
  *
@@ -11,64 +15,36 @@ import { SqlQueryOperationTypes } from "@prisma-next/sql-contract/types";
11
15
  type OperationTypes = {
12
16
  readonly 'pg/vector@1': {
13
17
  readonly cosineDistance: {
14
- readonly args: readonly [{
18
+ readonly self: {
15
19
  readonly codecId: 'pg/vector@1';
16
- readonly nullable: false;
17
- }];
18
- readonly returns: {
19
- readonly codecId: 'pg/float8@1';
20
- readonly nullable: false;
21
- };
22
- readonly lowering: {
23
- readonly targetFamily: 'sql';
24
- readonly strategy: 'function';
25
- readonly template: string;
26
20
  };
27
21
  };
28
22
  readonly cosineSimilarity: {
29
- readonly args: readonly [{
23
+ readonly self: {
30
24
  readonly codecId: 'pg/vector@1';
31
- readonly nullable: false;
32
- }];
33
- readonly returns: {
34
- readonly codecId: 'pg/float8@1';
35
- readonly nullable: false;
36
- };
37
- readonly lowering: {
38
- readonly targetFamily: 'sql';
39
- readonly strategy: 'function';
40
- readonly template: string;
41
25
  };
42
26
  };
43
27
  };
44
28
  };
45
29
  /** Flat operation signatures for the query builder. */
46
- type QueryOperationTypes = SqlQueryOperationTypes<{
30
+ type QueryOperationTypes<CT extends CodecTypesBase> = SqlQueryOperationTypes<CT, {
47
31
  readonly cosineDistance: {
48
- readonly args: readonly [{
49
- readonly codecId: 'pg/vector@1';
50
- readonly nullable: boolean;
51
- }, {
32
+ readonly self: {
52
33
  readonly codecId: 'pg/vector@1';
53
- readonly nullable: boolean;
54
- }];
55
- readonly returns: {
56
- readonly codecId: 'pg/float8@1';
57
- readonly nullable: false;
58
34
  };
35
+ readonly impl: (self: CodecExpression<'pg/vector@1', boolean, CT>, other: CodecExpression<'pg/vector@1', boolean, CT>) => Expression<{
36
+ codecId: 'pg/float8@1';
37
+ nullable: false;
38
+ }>;
59
39
  };
60
40
  readonly cosineSimilarity: {
61
- readonly args: readonly [{
62
- readonly codecId: 'pg/vector@1';
63
- readonly nullable: boolean;
64
- }, {
41
+ readonly self: {
65
42
  readonly codecId: 'pg/vector@1';
66
- readonly nullable: boolean;
67
- }];
68
- readonly returns: {
69
- readonly codecId: 'pg/float8@1';
70
- readonly nullable: false;
71
43
  };
44
+ readonly impl: (self: CodecExpression<'pg/vector@1', boolean, CT>, other: CodecExpression<'pg/vector@1', boolean, CT>) => Expression<{
45
+ codecId: 'pg/float8@1';
46
+ nullable: false;
47
+ }>;
72
48
  };
73
49
  }>;
74
50
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"operation-types.d.mts","names":[],"sources":["../src/types/operation-types.ts"],"sourcesContent":[],"mappings":";;;;;;AASA;AAwBA;;;KAxBY,cAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAwBA,mBAAA,GAAsB"}
1
+ {"version":3,"file":"operation-types.d.mts","names":[],"sources":["../src/types/operation-types.ts"],"mappings":";;;;KAGK,cAAA,GAAiB,MAAA;EAAA,SAA0B,KAAA;EAAA,SAAyB,MAAA;AAAA;;;;;;;KAS7D,cAAA;EAAA,SACD,aAAA;IAAA,SACE,cAAA;MAAA,SACE,IAAA;QAAA,SAAiB,OAAA;MAAA;IAAA;IAAA,SAEnB,gBAAA;MAAA,SACE,IAAA;QAAA,SAAiB,OAAA;MAAA;IAAA;EAAA;AAAA;AAMhC;AAAA,KAAY,mBAAA,YAA+B,cAAA,IAAkB,sBAAA,CAC3D,EAAA;EAAA,SAEW,cAAA;IAAA,SACE,IAAA;MAAA,SAAiB,OAAA;IAAA;IAAA,SACjB,IAAA,GACP,IAAA,EAAM,eAAA,yBAAwC,EAAA,GAC9C,KAAA,EAAO,eAAA,yBAAwC,EAAA,MAC5C,UAAA;MAAa,OAAA;MAAwB,QAAA;IAAA;EAAA;EAAA,SAEnC,gBAAA;IAAA,SACE,IAAA;MAAA,SAAiB,OAAA;IAAA;IAAA,SACjB,IAAA,GACP,IAAA,EAAM,eAAA,yBAAwC,EAAA,GAC9C,KAAA,EAAO,eAAA,yBAAwC,EAAA,MAC5C,UAAA;MAAa,OAAA;MAAwB,QAAA;IAAA;EAAA;AAAA"}
@@ -1 +1 @@
1
- export { };
1
+ export {};
package/dist/pack.d.mts CHANGED
@@ -1,6 +1,5 @@
1
- import { t as CodecTypes } from "./codec-types-BifaP625.mjs";
2
- import * as _prisma_next_sql_relational_core_ast0 from "@prisma-next/sql-relational-core/ast";
3
-
1
+ import { t as CodecTypes } from "./codec-types-yMSpEJJM.mjs";
2
+ import * as _$_prisma_next_framework_components_codec0 from "@prisma-next/framework-components/codec";
4
3
  //#region src/core/descriptor-meta.d.ts
5
4
  declare const pgvectorPackMetaBase: {
6
5
  readonly kind: "extension";
@@ -41,7 +40,7 @@ declare const pgvectorPackMetaBase: {
41
40
  };
42
41
  readonly types: {
43
42
  readonly codecTypes: {
44
- readonly codecInstances: _prisma_next_sql_relational_core_ast0.Codec<"pg/vector@1", readonly ["equality"], string, number[], Record<string, unknown>, unknown>[];
43
+ readonly codecDescriptors: _$_prisma_next_framework_components_codec0.CodecDescriptor<unknown>[];
45
44
  readonly import: {
46
45
  readonly package: "@prisma-next/extension-pgvector/codec-types";
47
46
  readonly named: "CodecTypes";
@@ -1 +1 @@
1
- {"version":3,"file":"pack.d.mts","names":[],"sources":["../src/core/descriptor-meta.ts"],"sourcesContent":[],"mappings":";;;;cAoCM;EAAA,SAAA,IAAA,EAAA,WAgDI;EAEG,SAAA,EAAA,EAAA,UAEW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;+BAJd,qCAAA,CAAA,8DAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAEG,yBAAyB;0BACZ"}
1
+ {"version":3,"file":"pack.d.mts","names":[],"sources":["../src/core/descriptor-meta.ts"],"mappings":";;;cA+DM,oBAAA;EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iCAgDI,0CAAA,CAAA,eAAA;MAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAEG,gBAAA,SAAyB,oBAAA;EAAA,SAC3B,YAAA,GAAe,UAAA;AAAA"}
package/dist/pack.mjs CHANGED
@@ -1,3 +1,2 @@
1
- import { t as pgvectorPackMeta } from "./descriptor-meta-BQbvJJxu.mjs";
2
-
3
- export { pgvectorPackMeta as default };
1
+ import { t as pgvectorPackMeta } from "./descriptor-meta-CBnWOxms.mjs";
2
+ export { pgvectorPackMeta as default };
@@ -1,7 +1,16 @@
1
1
  import { SqlRuntimeExtensionDescriptor } from "@prisma-next/sql-runtime";
2
+ import { CodecDescriptorRegistry } from "@prisma-next/sql-relational-core/query-lane-context";
2
3
 
4
+ //#region src/core/registry.d.ts
5
+ /**
6
+ * Registry of every codec descriptor shipped by `@prisma-next/extension-pgvector`.
7
+ *
8
+ * 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.
9
+ */
10
+ declare const pgvectorCodecRegistry: CodecDescriptorRegistry;
11
+ //#endregion
3
12
  //#region src/exports/runtime.d.ts
4
13
  declare const pgvectorRuntimeDescriptor: SqlRuntimeExtensionDescriptor<'postgres'>;
5
14
  //#endregion
6
- export { pgvectorRuntimeDescriptor as default };
15
+ export { pgvectorRuntimeDescriptor as default, pgvectorCodecRegistry };
7
16
  //# sourceMappingURL=runtime.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/exports/runtime.ts"],"sourcesContent":[],"mappings":";;;cAwCM,2BAA2B"}
1
+ {"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/core/registry.ts","../src/exports/runtime.ts"],"mappings":";;;;;;;AASA;;cAAa,qBAAA,EAAuB,uBAAA;;;cCL9B,yBAAA,EAA2B,6BAAA"}
package/dist/runtime.mjs CHANGED
@@ -1,32 +1,14 @@
1
- import { n as VECTOR_MAX_DIM, t as VECTOR_CODEC_ID } from "./constants-Co5golCK.mjs";
2
- import { n as pgvectorQueryOperations, r as codecDefinitions, t as pgvectorPackMeta } from "./descriptor-meta-BQbvJJxu.mjs";
3
- import { createCodecRegistry } from "@prisma-next/sql-relational-core/ast";
4
- import { type } from "arktype";
5
-
1
+ import { n as pgvectorQueryOperations, r as pgvectorCodecRegistry, t as pgvectorPackMeta } from "./descriptor-meta-CBnWOxms.mjs";
6
2
  //#region src/exports/runtime.ts
7
- const parameterizedCodecDescriptors = [{
8
- codecId: VECTOR_CODEC_ID,
9
- paramsSchema: type({ length: "number" }).narrow((params, ctx) => {
10
- const { length } = params;
11
- if (!Number.isInteger(length)) return ctx.mustBe("an integer");
12
- if (length < 1 || length > VECTOR_MAX_DIM) return ctx.mustBe(`in the range [1, ${VECTOR_MAX_DIM}]`);
13
- return true;
14
- })
15
- }];
16
- function createPgvectorCodecRegistry() {
17
- const registry = createCodecRegistry();
18
- for (const def of Object.values(codecDefinitions)) registry.register(def.codec);
19
- return registry;
20
- }
21
3
  const pgvectorRuntimeDescriptor = {
22
4
  kind: "extension",
23
5
  id: pgvectorPackMeta.id,
24
6
  version: pgvectorPackMeta.version,
25
7
  familyId: "sql",
26
8
  targetId: "postgres",
27
- codecs: createPgvectorCodecRegistry,
28
- queryOperations: () => pgvectorQueryOperations,
29
- parameterizedCodecs: () => parameterizedCodecDescriptors,
9
+ types: { codecTypes: { codecDescriptors: Array.from(pgvectorCodecRegistry.values()) } },
10
+ codecs: () => Array.from(pgvectorCodecRegistry.values()),
11
+ queryOperations: () => pgvectorQueryOperations(),
30
12
  create() {
31
13
  return {
32
14
  familyId: "sql",
@@ -34,8 +16,7 @@ const pgvectorRuntimeDescriptor = {
34
16
  };
35
17
  }
36
18
  };
37
- var runtime_default = pgvectorRuntimeDescriptor;
38
-
39
19
  //#endregion
40
- export { runtime_default as default };
20
+ export { pgvectorRuntimeDescriptor as default, pgvectorCodecRegistry };
21
+
41
22
  //# sourceMappingURL=runtime.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.mjs","names":["arktype","pgvectorRuntimeDescriptor: SqlRuntimeExtensionDescriptor<'postgres'>"],"sources":["../src/exports/runtime.ts"],"sourcesContent":["import { createCodecRegistry } from '@prisma-next/sql-relational-core/ast';\nimport type {\n RuntimeParameterizedCodecDescriptor,\n SqlRuntimeExtensionDescriptor,\n} from '@prisma-next/sql-runtime';\nimport { type as arktype } from 'arktype';\nimport { codecDefinitions } from '../core/codecs';\nimport { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from '../core/constants';\nimport { pgvectorPackMeta, pgvectorQueryOperations } from '../core/descriptor-meta';\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});\n\nconst parameterizedCodecDescriptors = [\n {\n codecId: VECTOR_CODEC_ID,\n paramsSchema: vectorParamsSchema,\n },\n] as const satisfies ReadonlyArray<\n RuntimeParameterizedCodecDescriptor<{ readonly length: number }>\n>;\n\nfunction createPgvectorCodecRegistry() {\n const registry = createCodecRegistry();\n for (const def of Object.values(codecDefinitions)) {\n registry.register(def.codec);\n }\n return registry;\n}\n\nconst pgvectorRuntimeDescriptor: SqlRuntimeExtensionDescriptor<'postgres'> = {\n kind: 'extension' as const,\n id: pgvectorPackMeta.id,\n version: pgvectorPackMeta.version,\n familyId: 'sql' as const,\n targetId: 'postgres' as const,\n codecs: createPgvectorCodecRegistry,\n queryOperations: () => pgvectorQueryOperations,\n parameterizedCodecs: () => parameterizedCodecDescriptors,\n create() {\n return {\n familyId: 'sql' as const,\n targetId: 'postgres' as const,\n };\n },\n};\n\nexport default pgvectorRuntimeDescriptor;\n"],"mappings":";;;;;;AAuBA,MAAM,gCAAgC,CACpC;CACE,SAAS;CACT,cAhBuBA,KAAQ,EACjC,QAAQ,UACT,CAAC,CAAC,QAAQ,QAAQ,QAAQ;EACzB,MAAM,EAAE,WAAW;AACnB,MAAI,CAAC,OAAO,UAAU,OAAO,CAC3B,QAAO,IAAI,OAAO,aAAa;AAEjC,MAAI,SAAS,KAAK,SAAS,eACzB,QAAO,IAAI,OAAO,oBAAoB,eAAe,GAAG;AAE1D,SAAO;GACP;CAMC,CACF;AAID,SAAS,8BAA8B;CACrC,MAAM,WAAW,qBAAqB;AACtC,MAAK,MAAM,OAAO,OAAO,OAAO,iBAAiB,CAC/C,UAAS,SAAS,IAAI,MAAM;AAE9B,QAAO;;AAGT,MAAMC,4BAAuE;CAC3E,MAAM;CACN,IAAI,iBAAiB;CACrB,SAAS,iBAAiB;CAC1B,UAAU;CACV,UAAU;CACV,QAAQ;CACR,uBAAuB;CACvB,2BAA2B;CAC3B,SAAS;AACP,SAAO;GACL,UAAU;GACV,UAAU;GACX;;CAEJ;AAED,sBAAe"}
1
+ {"version":3,"file":"runtime.mjs","names":[],"sources":["../src/exports/runtime.ts"],"sourcesContent":["import type { SqlRuntimeExtensionDescriptor } from '@prisma-next/sql-runtime';\nimport { pgvectorPackMeta, pgvectorQueryOperations } from '../core/descriptor-meta';\nimport { pgvectorCodecRegistry } from '../core/registry';\n\nconst pgvectorRuntimeDescriptor: SqlRuntimeExtensionDescriptor<'postgres'> = {\n kind: 'extension' as const,\n id: pgvectorPackMeta.id,\n version: pgvectorPackMeta.version,\n familyId: 'sql' as const,\n targetId: 'postgres' as const,\n // Expose the unified descriptor list so `extractCodecLookup` reads `targetTypes` / `meta` / `renderOutputType` directly off the descriptors and materializes the representative `Codec` for the SQL renderer's cast-policy lookup.\n types: {\n codecTypes: {\n codecDescriptors: Array.from(pgvectorCodecRegistry.values()),\n },\n },\n codecs: () => Array.from(pgvectorCodecRegistry.values()),\n queryOperations: () => pgvectorQueryOperations(),\n create() {\n return {\n familyId: 'sql' as const,\n targetId: 'postgres' as const,\n };\n },\n};\n\nexport { pgvectorCodecRegistry };\nexport default pgvectorRuntimeDescriptor;\n"],"mappings":";;AAIA,MAAM,4BAAuE;CAC3E,MAAM;CACN,IAAI,iBAAiB;CACrB,SAAS,iBAAiB;CAC1B,UAAU;CACV,UAAU;CAEV,OAAO,EACL,YAAY,EACV,kBAAkB,MAAM,KAAK,sBAAsB,QAAQ,CAAC,EAC7D,EACF;CACD,cAAc,MAAM,KAAK,sBAAsB,QAAQ,CAAC;CACxD,uBAAuB,yBAAyB;CAChD,SAAS;EACP,OAAO;GACL,UAAU;GACV,UAAU;GACX;;CAEJ"}
package/package.json CHANGED
@@ -1,29 +1,32 @@
1
1
  {
2
2
  "name": "@prisma-next/extension-pgvector",
3
- "version": "0.5.0-dev.8",
3
+ "version": "0.5.0-dev.81",
4
+ "license": "Apache-2.0",
4
5
  "type": "module",
5
6
  "sideEffects": false,
6
7
  "dependencies": {
7
- "arktype": "^2.0.0",
8
- "@prisma-next/contract-authoring": "0.5.0-dev.8",
9
- "@prisma-next/contract": "0.5.0-dev.8",
10
- "@prisma-next/family-sql": "0.5.0-dev.8",
11
- "@prisma-next/framework-components": "0.5.0-dev.8",
12
- "@prisma-next/sql-relational-core": "0.5.0-dev.8",
13
- "@prisma-next/sql-runtime": "0.5.0-dev.8",
14
- "@prisma-next/sql-operations": "0.5.0-dev.8",
15
- "@prisma-next/sql-schema-ir": "0.5.0-dev.8"
8
+ "@standard-schema/spec": "^1.1.0",
9
+ "arktype": "^2.1.29",
10
+ "@prisma-next/contract": "0.5.0-dev.81",
11
+ "@prisma-next/family-sql": "0.5.0-dev.81",
12
+ "@prisma-next/contract-authoring": "0.5.0-dev.81",
13
+ "@prisma-next/framework-components": "0.5.0-dev.81",
14
+ "@prisma-next/migration-tools": "0.5.0-dev.81",
15
+ "@prisma-next/sql-operations": "0.5.0-dev.81",
16
+ "@prisma-next/sql-runtime": "0.5.0-dev.81",
17
+ "@prisma-next/sql-contract": "0.5.0-dev.81",
18
+ "@prisma-next/sql-schema-ir": "0.5.0-dev.81",
19
+ "@prisma-next/sql-relational-core": "0.5.0-dev.81"
16
20
  },
17
21
  "devDependencies": {
18
- "tsdown": "0.18.4",
22
+ "tsdown": "0.22.0",
19
23
  "typescript": "5.9.3",
20
- "vitest": "4.0.17",
21
- "@prisma-next/operations": "0.5.0-dev.8",
22
- "@prisma-next/sql-contract": "0.5.0-dev.8",
23
- "@prisma-next/sql-contract-ts": "0.5.0-dev.8",
24
- "@prisma-next/tsdown": "0.0.0",
24
+ "vitest": "4.1.5",
25
+ "@prisma-next/sql-contract-ts": "0.5.0-dev.81",
26
+ "@prisma-next/test-utils": "0.0.1",
27
+ "@prisma-next/operations": "0.5.0-dev.81",
25
28
  "@prisma-next/tsconfig": "0.0.0",
26
- "@prisma-next/test-utils": "0.0.1"
29
+ "@prisma-next/tsdown": "0.0.0"
27
30
  },
28
31
  "files": [
29
32
  "dist",
@@ -1,77 +1,145 @@
1
1
  /**
2
- * Vector codec implementation for pgvector extension.
2
+ * pgvector extension codec.
3
3
  *
4
- * Provides encoding/decoding for the `vector` PostgreSQL type.
5
- * Wire format is a string like `[1,2,3]` (PostgreSQL vector text format).
4
+ * Mirrors the patterns in `postgres/codecs-class.ts` and `sqlite/codecs-class.ts` for the single `pg/vector@1` codec. Three artifacts:
5
+ *
6
+ * 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.
7
+ * 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}>`.
8
+ * 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`.
9
+ *
10
+ * `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.
6
11
  */
7
12
 
8
- import { codec, defineCodecs } from '@prisma-next/sql-relational-core/ast';
9
-
10
- const pgVectorCodec = codec({
11
- typeId: 'pg/vector@1',
12
- targetTypes: ['vector'],
13
- traits: ['equality'],
14
- renderOutputType: (typeParams) => {
15
- const length = typeParams['length'];
16
- if (length === undefined) return undefined;
17
- if (typeof length !== 'number' || !Number.isFinite(length) || !Number.isInteger(length)) {
18
- throw new Error(
19
- `renderOutputType: expected positive integer "length" in typeParams for Vector, got ${String(length)}`,
20
- );
21
- }
22
- return `Vector<${length}>`;
23
- },
24
- encode: (value: number[]): string => {
25
- // Validate that value is an array of numbers
13
+ import type { JsonValue } from '@prisma-next/contract/types';
14
+ import {
15
+ type AnyCodecDescriptor,
16
+ type CodecCallContext,
17
+ CodecDescriptorImpl,
18
+ CodecImpl,
19
+ type CodecInstanceContext,
20
+ type ColumnHelperFor,
21
+ type ColumnHelperForStrict,
22
+ column,
23
+ } from '@prisma-next/framework-components/codec';
24
+ import type { ExtractCodecTypes } from '@prisma-next/sql-relational-core/ast';
25
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
26
+ import { type as arktype } from 'arktype';
27
+ import { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from './constants';
28
+
29
+ type VectorParams = { readonly length: number };
30
+
31
+ const vectorParamsSchema = arktype({
32
+ length: 'number',
33
+ }).narrow((params, ctx) => {
34
+ const { length } = params;
35
+ if (!Number.isInteger(length)) {
36
+ return ctx.mustBe('an integer');
37
+ }
38
+ if (length < 1 || length > VECTOR_MAX_DIM) {
39
+ return ctx.mustBe(`in the range [1, ${VECTOR_MAX_DIM}]`);
40
+ }
41
+ return true;
42
+ }) satisfies StandardSchemaV1<VectorParams>;
43
+
44
+ const PG_VECTOR_META = { db: { sql: { postgres: { nativeType: 'vector' } } } } as const;
45
+
46
+ export class PgVectorCodec extends CodecImpl<
47
+ typeof VECTOR_CODEC_ID,
48
+ readonly ['equality'],
49
+ string,
50
+ number[]
51
+ > {
52
+ readonly length: number | undefined;
53
+
54
+ constructor(descriptor: AnyCodecDescriptor, length: number | undefined) {
55
+ super(descriptor);
56
+ this.length = length;
57
+ }
58
+
59
+ assertVector(value: unknown): asserts value is number[] {
26
60
  if (!Array.isArray(value)) {
27
61
  throw new Error('Vector value must be an array of numbers');
28
62
  }
29
63
  if (!value.every((v) => typeof v === 'number')) {
30
64
  throw new Error('Vector value must contain only numbers');
31
65
  }
32
- // Format as PostgreSQL vector text format: [1,2,3]
33
- // PostgreSQL's pg library requires the vector format string
66
+ if (this.length !== undefined && value.length !== this.length) {
67
+ throw new Error(`Vector length mismatch: expected ${this.length}, got ${value.length}`);
68
+ }
69
+ }
70
+
71
+ async encode(value: number[], _ctx: CodecCallContext): Promise<string> {
72
+ this.assertVector(value);
34
73
  return `[${value.join(',')}]`;
35
- },
36
- decode: (wire: string): number[] => {
37
- // Handle string format from PostgreSQL: [1,2,3]
74
+ }
75
+
76
+ async decode(wire: string, _ctx: CodecCallContext): Promise<number[]> {
38
77
  if (typeof wire !== 'string') {
39
78
  throw new Error('Vector wire value must be a string');
40
79
  }
41
- // Parse PostgreSQL vector format: [1,2,3]
42
80
  if (!wire.startsWith('[') || !wire.endsWith(']')) {
43
81
  throw new Error(`Invalid vector format: expected "[...]", got "${wire}"`);
44
82
  }
45
83
  const content = wire.slice(1, -1).trim();
46
- if (content === '') {
47
- return [];
48
- }
49
- const values = content.split(',').map((v) => {
50
- const num = Number.parseFloat(v.trim());
51
- if (Number.isNaN(num)) {
52
- throw new Error(`Invalid vector value: "${v}" is not a number`);
53
- }
54
- return num;
55
- });
56
- return values;
57
- },
58
- meta: {
59
- db: {
60
- sql: {
61
- postgres: {
62
- nativeType: 'vector',
63
- },
64
- },
65
- },
66
- },
67
- });
68
-
69
- // Build codec definitions using the builder DSL
70
- const codecs = defineCodecs().add('vector', pgVectorCodec);
71
-
72
- // Export derived structures directly from codecs builder
73
- export const codecDefinitions = codecs.codecDefinitions;
74
- export const dataTypes = codecs.dataTypes;
75
-
76
- // Export types derived from codecs builder
77
- export type CodecTypes = typeof codecs.CodecTypes;
84
+ const parsed =
85
+ content === ''
86
+ ? []
87
+ : content.split(',').map((v) => {
88
+ const num = Number.parseFloat(v.trim());
89
+ if (Number.isNaN(num)) {
90
+ throw new Error(`Invalid vector value: "${v}" is not a number`);
91
+ }
92
+ return num;
93
+ });
94
+ this.assertVector(parsed);
95
+ return parsed;
96
+ }
97
+
98
+ encodeJson(value: number[]): JsonValue {
99
+ this.assertVector(value);
100
+ return value;
101
+ }
102
+
103
+ decodeJson(json: JsonValue): number[] {
104
+ this.assertVector(json);
105
+ return json;
106
+ }
107
+ }
108
+
109
+ export class PgVectorDescriptor extends CodecDescriptorImpl<VectorParams> {
110
+ override readonly codecId = VECTOR_CODEC_ID;
111
+ override readonly traits = ['equality'] as const;
112
+ override readonly targetTypes = ['vector'] as const;
113
+ override readonly meta = PG_VECTOR_META;
114
+ override readonly paramsSchema: StandardSchemaV1<VectorParams> = vectorParamsSchema;
115
+ override renderOutputType(params: VectorParams): string {
116
+ return `Vector<${params.length}>`;
117
+ }
118
+ /**
119
+ * 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.
120
+ */
121
+ override factory(params: VectorParams): (ctx: CodecInstanceContext) => PgVectorCodec {
122
+ return () => new PgVectorCodec(this, (params as VectorParams | undefined)?.length);
123
+ }
124
+ }
125
+
126
+ export const pgVectorDescriptor = new PgVectorDescriptor();
127
+
128
+ /**
129
+ * 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 }`).
130
+ *
131
+ * Passes the bare `nativeType: 'vector'`; the family-layer `expandNativeType` hook renders the parameterized form (`vector(1536)`) at emit/verify time from `nativeType` + `typeParams`.
132
+ */
133
+ export const pgVectorColumn = <N extends number>(length: N) =>
134
+ column(pgVectorDescriptor.factory({ length }), pgVectorDescriptor.codecId, { length }, 'vector');
135
+
136
+ pgVectorColumn satisfies ColumnHelperFor<PgVectorDescriptor>;
137
+ pgVectorColumn satisfies ColumnHelperForStrict<PgVectorDescriptor>;
138
+
139
+ const codecDescriptorMap = {
140
+ vector: pgVectorDescriptor,
141
+ } as const;
142
+
143
+ export type CodecTypes = ExtractCodecTypes<typeof codecDescriptorMap>;
144
+
145
+ export const codecDescriptors: readonly AnyCodecDescriptor[] = Object.values(codecDescriptorMap);
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Static names and identifiers used across pgvector's contract space.
3
+ *
4
+ * Centralised here so the contract IR (`./contract`), the baseline
5
+ * migration ops (`./migrations`), the head ref, and the descriptor
6
+ * (`../exports/control`) all reference the same values without typos.
7
+ *
8
+ * The space identifier `'pgvector'` is what the framework writes to
9
+ * `migrations/pgvector/` in the user's repo and what the marker table's
10
+ * `space` column carries for pgvector-owned rows.
11
+ *
12
+ * The `pgvector:*` invariantId namespace is locked here — once
13
+ * published, an invariantId is immutable so downstream consumers can
14
+ * reference it by literal string match.
15
+ */
16
+
17
+ export const PGVECTOR_SPACE_ID = 'pgvector' as const;
18
+
19
+ export const PGVECTOR_NATIVE_TYPE = 'vector' as const;
20
+
21
+ export const PGVECTOR_BASELINE_MIGRATION_NAME = '20260601T0000_install_vector_extension' as const;
22
+
23
+ /**
24
+ * `pgvector:*` invariantIds emitted by the baseline migration. Each id,
25
+ * once published, is immutable: downstream consumers (other extensions,
26
+ * the marker table) reference them by literal string match.
27
+ */
28
+ export const PGVECTOR_INVARIANTS = {
29
+ installVector: 'pgvector:install-vector-v1',
30
+ } as const;
@@ -0,0 +1,74 @@
1
+ /**
2
+ * pgvector contract space — declares the parameterised native type
3
+ * `vector(N)` that user columns can name as `nativeType`.
4
+ *
5
+ * Unlike CipherStash's typed objects (composite types / domains / enums
6
+ * — deferred behind `meta.cipherstashFutureIR` until the IR vocabulary
7
+ * gains first-class support), pgvector's `vector` is a parameterised
8
+ * native type and *is* representable in today's IR via
9
+ * {@link StorageTypeInstance}: `{ codecId, nativeType, typeParams }`.
10
+ * The contract registers a representative instance under
11
+ * `storage.types.vector` so the verifier sees the type as part of
12
+ * pgvector's space contribution and so the pinned `contract.json` on
13
+ * disk is materially distinct from an empty space.
14
+ *
15
+ * Per-column instances on the user's side carry concrete
16
+ * `typeParams.length` (e.g. `vector(1536)`); the registration here
17
+ * declares the parameterised shape — it is not consumed as a literal
18
+ * column type by any user table.
19
+ */
20
+
21
+ import { computeStorageHash } from '@prisma-next/contract/hashing';
22
+ import { type Contract, coreHash, profileHash } from '@prisma-next/contract/types';
23
+ import type { SqlStorage } from '@prisma-next/sql-contract/types';
24
+ import { VECTOR_CODEC_ID } from './constants';
25
+ import { PGVECTOR_NATIVE_TYPE } from './contract-space-constants';
26
+
27
+ const TARGET = 'postgres' as const;
28
+ const TARGET_FAMILY = 'sql' as const;
29
+
30
+ /**
31
+ * Storage body for the contract — pgvector ships no tables of its own;
32
+ * the `vector` parameterised native type is registered under
33
+ * `storage.types` so pgvector's IR contribution is non-empty and the
34
+ * pinned `contract.json` on disk differs materially from an empty
35
+ * extension space.
36
+ *
37
+ * Authored without `storageHash` here so {@link computeStorageHash} can
38
+ * digest the canonical body (the hashing pipeline panics if asked to
39
+ * hash an object that already carries its own output — see
40
+ * `assertDescriptorSelfConsistency`'s storage-hash strip).
41
+ */
42
+ const storageBody = {
43
+ tables: {},
44
+ types: {
45
+ [PGVECTOR_NATIVE_TYPE]: {
46
+ codecId: VECTOR_CODEC_ID,
47
+ nativeType: PGVECTOR_NATIVE_TYPE,
48
+ typeParams: {},
49
+ },
50
+ },
51
+ };
52
+
53
+ /** Content-addressed hash of pgvector's storage IR. */
54
+ export const PGVECTOR_STORAGE_HASH = computeStorageHash({
55
+ target: TARGET,
56
+ targetFamily: TARGET_FAMILY,
57
+ storage: storageBody,
58
+ });
59
+
60
+ /** pgvector's contract value, exposed via the descriptor's `contractSpace.contractJson`. */
61
+ export const pgvectorContract: Contract<SqlStorage> = {
62
+ target: TARGET,
63
+ targetFamily: TARGET_FAMILY,
64
+ roots: {},
65
+ models: {},
66
+ capabilities: {},
67
+ extensionPacks: {},
68
+ meta: {},
69
+ profileHash: profileHash('pgvector-extension-profile-v1'),
70
+ storage: {
71
+ ...storageBody,
72
+ storageHash: coreHash(PGVECTOR_STORAGE_HASH),
73
+ },
74
+ };