@prisma-next/extension-pgvector 0.6.0-dev.6 → 0.6.0-dev.8

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 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 { vector, vectorColumn } from '@prisma-next/extension-pgvector/column-types';
72
+ import { vector } 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
 
@@ -90,7 +90,7 @@ export const contract = defineContract({
90
90
  });
91
91
  ```
92
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.
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. Every pgvector column must declare an explicit dimension via `vector(N)`; the runtime codec is constructed against `{ length: N }`, so an undimensioned form has no honest descriptor signature.
94
94
 
95
95
  ### Runtime Setup
96
96
 
@@ -14,8 +14,8 @@ type VectorParams = {
14
14
  readonly length: number;
15
15
  };
16
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);
17
+ readonly length: number;
18
+ constructor(descriptor: AnyCodecDescriptor, length: number);
19
19
  assertVector(value: unknown): asserts value is number[];
20
20
  encode(value: number[], _ctx: CodecCallContext): Promise<string>;
21
21
  decode(wire: string, _ctx: CodecCallContext): Promise<number[]>;
@@ -37,9 +37,6 @@ declare class PgVectorDescriptor extends CodecDescriptorImpl<VectorParams> {
37
37
  };
38
38
  readonly paramsSchema: StandardSchemaV1<VectorParams>;
39
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
40
  factory(params: VectorParams): (ctx: CodecInstanceContext) => PgVectorCodec;
44
41
  }
45
42
  declare const codecDescriptorMap: {
@@ -60,4 +57,4 @@ type Vector<N extends number = number> = number[] & {
60
57
  type CodecTypes = CodecTypes$1;
61
58
  //#endregion
62
59
  export { Vector as n, CodecTypes as t };
63
- //# sourceMappingURL=codec-types-CQubO6uQ.d.mts.map
60
+ //# sourceMappingURL=codec-types-D9jU-l1B.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"codec-types-CQubO6uQ.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,eAA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AAmB,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;EAGzB,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;;;;;;;;AAjHY;KCTlD,MAAA;EAAA,SAA0D,cAAA,GAAiB,CAAA;AAAA;AAAA,KAE3E,UAAA,GAAa,YAAA"}
@@ -1,2 +1,2 @@
1
- import { n as Vector, t as CodecTypes } from "./codec-types-CQubO6uQ.mjs";
1
+ import { n as Vector, t as CodecTypes } from "./codec-types-D9jU-l1B.mjs";
2
2
  export { type CodecTypes, type Vector };
@@ -1,13 +1,6 @@
1
1
  import { ColumnTypeDescriptor } from "@prisma-next/framework-components/codec";
2
2
 
3
3
  //#region src/exports/column-types.d.ts
4
- /**
5
- * Static vector column descriptor without dimension. Use `vector(N)` for dimensioned vectors that produce `vector(N)` DDL.
6
- */
7
- declare const vectorColumn: {
8
- readonly codecId: "pg/vector@1";
9
- readonly nativeType: "vector";
10
- };
11
4
  /**
12
5
  * Factory for creating dimensioned vector column descriptors.
13
6
  *
@@ -26,5 +19,5 @@ declare function vector<N extends number>(length: N): ColumnTypeDescriptor & {
26
19
  };
27
20
  };
28
21
  //#endregion
29
- export { vector, vectorColumn };
22
+ export { vector };
30
23
  //# sourceMappingURL=column-types.d.mts.map
@@ -1 +1 @@
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"}
1
+ {"version":3,"file":"column-types.d.mts","names":[],"sources":["../src/exports/column-types.ts"],"mappings":";;;;;;;;;;;;;;;iBAmBgB,MAAA,kBAAA,CACd,MAAA,EAAQ,CAAA,GACP,oBAAA;EAAA,SAAkC,UAAA;IAAA,SAAuB,MAAA,EAAQ,CAAA;EAAA;AAAA"}
@@ -1,13 +1,6 @@
1
1
  import { n as VECTOR_MAX_DIM, t as VECTOR_CODEC_ID } from "./constants-DX-00vYk.mjs";
2
2
  //#region src/exports/column-types.ts
3
3
  /**
4
- * Static vector column descriptor without dimension. Use `vector(N)` for dimensioned vectors that produce `vector(N)` DDL.
5
- */
6
- const vectorColumn = {
7
- codecId: VECTOR_CODEC_ID,
8
- nativeType: "vector"
9
- };
10
- /**
11
4
  * Factory for creating dimensioned vector column descriptors.
12
5
  *
13
6
  * @example
@@ -28,6 +21,6 @@ function vector(length) {
28
21
  };
29
22
  }
30
23
  //#endregion
31
- export { vector, vectorColumn };
24
+ export { vector };
32
25
 
33
26
  //# 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. 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"}
1
+ {"version":3,"file":"column-types.mjs","names":[],"sources":["../src/exports/column-types.ts"],"sourcesContent":["/**\n * Column type descriptor factory for pgvector extension. `vector(N)` is the canonical authoring surface; every pgvector column must declare a dimension via this factory. The dimension threads into the runtime codec through `paramsSchema.length` and into the DDL via the family-layer `expandNativeType` hook (e.g. `vector(1536)`).\n */\n\nimport type { ColumnTypeDescriptor } from '@prisma-next/framework-components/codec';\nimport { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from '../core/constants';\n\n/**\n * Factory for creating dimensioned vector column descriptors.\n *\n * @example\n * ```typescript\n * .column('embedding', { type: vector(1536), nullable: false })\n * // Produces: nativeType: 'vector', typeParams: { length: 1536 }\n * ```\n * @param length - The dimension of the vector (e.g., 1536 for OpenAI embeddings)\n * @returns A column type descriptor with `typeParams.length` set\n * @throws {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":";;;;;;;;;;;;;;AAmBA,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"}
package/dist/control.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { n as pgvectorQueryOperations, t as pgvectorPackMeta } from "./descriptor-meta-DEgJjLLi.mjs";
1
+ import { n as pgvectorQueryOperations, t as pgvectorPackMeta } from "./descriptor-meta-8UnGdpZ6.mjs";
2
2
  import { contractSpaceFromJson } from "@prisma-next/migration-tools/spaces";
3
3
  //#region migrations/20260601T0000_install_vector_extension/migration.json
4
4
  var migration_default = {
@@ -1,5 +1,5 @@
1
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";
2
+ import { buildOperation, codecOf, toExpr } from "@prisma-next/sql-relational-core/expression";
3
3
  import { buildCodecDescriptorRegistry } from "@prisma-next/sql-relational-core/codec-descriptor-registry";
4
4
  import { CodecDescriptorImpl, CodecImpl } from "@prisma-next/framework-components/codec";
5
5
  import { type } from "arktype";
@@ -40,7 +40,7 @@ var PgVectorCodec = class extends CodecImpl {
40
40
  assertVector(value) {
41
41
  if (!Array.isArray(value)) throw new Error("Vector value must be an array of numbers");
42
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}`);
43
+ if (value.length !== this.length) throw new Error(`Vector length mismatch: expected ${this.length}, got ${value.length}`);
44
44
  }
45
45
  async encode(value, _ctx) {
46
46
  this.assertVector(value);
@@ -76,11 +76,8 @@ var PgVectorDescriptor = class extends CodecDescriptorImpl {
76
76
  renderOutputType(params) {
77
77
  return `Vector<${params.length}>`;
78
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
79
  factory(params) {
83
- return () => new PgVectorCodec(this, params?.length);
80
+ return () => new PgVectorCodec(this, params.length);
84
81
  }
85
82
  };
86
83
  const codecDescriptorMap = { vector: new PgVectorDescriptor() };
@@ -100,10 +97,10 @@ function pgvectorQueryOperations() {
100
97
  cosineDistance: {
101
98
  self: { codecId: pgvectorTypeId },
102
99
  impl: (self, other) => {
103
- const selfRefs = refsOf(self);
100
+ const selfCodec = codecOf(self);
104
101
  return buildOperation({
105
102
  method: "cosineDistance",
106
- args: [toExpr(self, pgvectorTypeId, selfRefs), toExpr(other, pgvectorTypeId, selfRefs)],
103
+ args: [toExpr(self, selfCodec), toExpr(other, selfCodec)],
107
104
  returns: {
108
105
  codecId: "pg/float8@1",
109
106
  nullable: false
@@ -119,10 +116,10 @@ function pgvectorQueryOperations() {
119
116
  cosineSimilarity: {
120
117
  self: { codecId: pgvectorTypeId },
121
118
  impl: (self, other) => {
122
- const selfRefs = refsOf(self);
119
+ const selfCodec = codecOf(self);
123
120
  return buildOperation({
124
121
  method: "cosineSimilarity",
125
- args: [toExpr(self, pgvectorTypeId, selfRefs), toExpr(other, pgvectorTypeId, selfRefs)],
122
+ args: [toExpr(self, selfCodec), toExpr(other, selfCodec)],
126
123
  returns: {
127
124
  codecId: "pg/float8@1",
128
125
  nullable: false
@@ -159,6 +156,11 @@ const pgvectorPackMeta = {
159
156
  alias: "Vector"
160
157
  }]
161
158
  },
159
+ operationTypes: { import: {
160
+ package: "@prisma-next/extension-pgvector/operation-types",
161
+ named: "OperationTypes",
162
+ alias: "PgVectorOperationTypes"
163
+ } },
162
164
  queryOperationTypes: { import: {
163
165
  package: "@prisma-next/extension-pgvector/operation-types",
164
166
  named: "QueryOperationTypes",
@@ -175,4 +177,4 @@ const pgvectorPackMeta = {
175
177
  //#endregion
176
178
  export { pgvectorQueryOperations as n, pgvectorCodecRegistry as r, pgvectorPackMeta as t };
177
179
 
178
- //# sourceMappingURL=descriptor-meta-DEgJjLLi.mjs.map
180
+ //# sourceMappingURL=descriptor-meta-8UnGdpZ6.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"descriptor-meta-8UnGdpZ6.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;\n\n constructor(descriptor: AnyCodecDescriptor, length: number) {\n super(descriptor);\n this.length = length;\n }\n\n assertVector(value: unknown): asserts value is number[] {\n if (!Array.isArray(value)) {\n throw new Error('Vector value must be an array of numbers');\n }\n if (!value.every((v) => typeof v === 'number')) {\n throw new Error('Vector value must contain only numbers');\n }\n if (value.length !== this.length) {\n throw new Error(`Vector length mismatch: expected ${this.length}, got ${value.length}`);\n }\n }\n\n async encode(value: number[], _ctx: CodecCallContext): Promise<string> {\n this.assertVector(value);\n return `[${value.join(',')}]`;\n }\n\n async decode(wire: string, _ctx: CodecCallContext): Promise<number[]> {\n if (typeof wire !== 'string') {\n throw new Error('Vector wire value must be a string');\n }\n 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 override factory(params: VectorParams): (ctx: CodecInstanceContext) => PgVectorCodec {\n return () => new PgVectorCodec(this, params.length);\n }\n}\n\nexport const pgVectorDescriptor = new PgVectorDescriptor();\n\n/**\n * Per-codec column helper for `pg/vector@1`. Generic over `N extends number` so the column site preserves the dimension literal in `typeParams` (e.g. `pgVectorColumn(1536)` packs `typeParams: { length: 1536 }`).\n *\n * Passes the bare `nativeType: 'vector'`; the family-layer `expandNativeType` hook renders the parameterized form (`vector(1536)`) at emit/verify time from `nativeType` + `typeParams`.\n */\nexport const pgVectorColumn = <N extends number>(length: N) =>\n column(pgVectorDescriptor.factory({ length }), pgVectorDescriptor.codecId, { length }, 'vector');\n\npgVectorColumn satisfies ColumnHelperFor<PgVectorDescriptor>;\npgVectorColumn satisfies ColumnHelperForStrict<PgVectorDescriptor>;\n\nconst codecDescriptorMap = {\n vector: pgVectorDescriptor,\n} as const;\n\nexport type CodecTypes = ExtractCodecTypes<typeof codecDescriptorMap>;\n\nexport const codecDescriptors: readonly AnyCodecDescriptor[] = Object.values(codecDescriptorMap);\n","import { buildCodecDescriptorRegistry } from '@prisma-next/sql-relational-core/codec-descriptor-registry';\nimport type { CodecDescriptorRegistry } from '@prisma-next/sql-relational-core/query-lane-context';\nimport { codecDescriptors } from './codecs';\n\n/**\n * Registry of every codec descriptor shipped by `@prisma-next/extension-pgvector`.\n *\n * Public consumer surface for the pgvector codec set. Currently a single entry (`pg/vector@1`); the registry shape stays consistent with the other codec-shipping packages so consumers don't need to special-case extensions. See ADR 208.\n */\nexport const pgvectorCodecRegistry: CodecDescriptorRegistry =\n buildCodecDescriptorRegistry(codecDescriptors);\n","import {\n buildOperation,\n type CodecExpression,\n codecOf,\n type Expression,\n toExpr,\n} from '@prisma-next/sql-relational-core/expression';\nimport type { CodecTypes } from '../types/codec-types';\nimport type { QueryOperationTypes } from '../types/operation-types';\nimport { pgvectorAuthoringTypes } from './authoring';\nimport { pgvectorCodecRegistry } from './registry';\n\nconst pgvectorTypeId = 'pg/vector@1' as const;\n\ntype CodecTypesBase = Record<string, { readonly input: unknown; readonly output: unknown }>;\n\nexport function pgvectorQueryOperations<CT extends CodecTypesBase>(): QueryOperationTypes<CT> {\n return {\n cosineDistance: {\n self: { codecId: pgvectorTypeId },\n impl: (\n self: CodecExpression<'pg/vector@1', boolean, CT>,\n other: CodecExpression<'pg/vector@1', boolean, CT>,\n ): Expression<{ codecId: 'pg/float8@1'; nullable: false }> => {\n const selfCodec = codecOf(self);\n return buildOperation({\n method: 'cosineDistance',\n args: [toExpr(self, selfCodec), toExpr(other, selfCodec)],\n returns: { codecId: 'pg/float8@1', nullable: false },\n lowering: {\n targetFamily: 'sql',\n strategy: 'function',\n template: '{{self}} <=> {{arg0}}',\n },\n });\n },\n },\n cosineSimilarity: {\n self: { codecId: pgvectorTypeId },\n impl: (\n self: CodecExpression<'pg/vector@1', boolean, CT>,\n other: CodecExpression<'pg/vector@1', boolean, CT>,\n ): Expression<{ codecId: 'pg/float8@1'; nullable: false }> => {\n const selfCodec = codecOf(self);\n return buildOperation({\n method: 'cosineSimilarity',\n args: [toExpr(self, selfCodec), toExpr(other, selfCodec)],\n returns: { codecId: 'pg/float8@1', nullable: false },\n lowering: {\n targetFamily: 'sql',\n strategy: 'function',\n template: '1 - ({{self}} <=> {{arg0}})',\n },\n });\n },\n },\n };\n}\n\nconst pgvectorPackMetaBase = {\n kind: 'extension',\n id: 'pgvector',\n familyId: 'sql',\n targetId: 'postgres',\n version: '0.0.1',\n capabilities: {\n postgres: {\n 'pgvector.cosine': true,\n },\n },\n authoring: {\n type: pgvectorAuthoringTypes,\n },\n types: {\n codecTypes: {\n codecDescriptors: Array.from(pgvectorCodecRegistry.values()),\n import: {\n package: '@prisma-next/extension-pgvector/codec-types',\n named: 'CodecTypes',\n alias: 'PgVectorTypes',\n },\n typeImports: [\n {\n package: '@prisma-next/extension-pgvector/codec-types',\n named: 'Vector',\n alias: 'Vector',\n },\n ],\n },\n operationTypes: {\n import: {\n package: '@prisma-next/extension-pgvector/operation-types',\n named: 'OperationTypes',\n alias: 'PgVectorOperationTypes',\n },\n },\n queryOperationTypes: {\n import: {\n package: '@prisma-next/extension-pgvector/operation-types',\n named: 'QueryOperationTypes',\n alias: 'PgVectorQueryOperationTypes',\n },\n },\n storage: [\n { typeId: pgvectorTypeId, familyId: 'sql', targetId: 'postgres', nativeType: 'vector' },\n ],\n },\n} as const;\n\nexport const pgvectorPackMeta: typeof pgvectorPackMetaBase & {\n readonly __codecTypes?: CodecTypes;\n} = pgvectorPackMetaBase;\n"],"mappings":";;;;;;AAGA,MAAa,yBAAyB,EACpC,UAAU,EACR,QAAQ;CACN,MAAM;CACN,MAAM,CACJ;EAAE,MAAM;EAAU,MAAM;EAAU,SAAS;EAAM,SAAS;EAAG,SAAS;EAAgB,CACvF;CACD,QAAQ;EACN,SAAS;EACT,YAAY;EACZ,YAAY,EACV,QAAQ;GAAE,MAAM;GAAO,OAAO;GAAG,EAClC;EACF;CACF,EACF,EACF;;;ACWD,MAAM,qBAAqBA,KAAQ,EACjC,QAAQ,UACT,CAAC,CAAC,QAAQ,QAAQ,QAAQ;CACzB,MAAM,EAAE,WAAW;CACnB,IAAI,CAAC,OAAO,UAAU,OAAO,EAC3B,OAAO,IAAI,OAAO,aAAa;CAEjC,IAAI,SAAS,KAAK,SAAA,MAChB,OAAO,IAAI,OAAO,oBAAoB,eAAe,GAAG;CAE1D,OAAO;EACP;AAEF,MAAM,iBAAiB,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,UAAU,EAAE,EAAE,EAAE;AAE9E,IAAa,gBAAb,cAAmC,UAKjC;CACA;CAEA,YAAY,YAAgC,QAAgB;EAC1D,MAAM,WAAW;EACjB,KAAK,SAAS;;CAGhB,aAAa,OAA2C;EACtD,IAAI,CAAC,MAAM,QAAQ,MAAM,EACvB,MAAM,IAAI,MAAM,2CAA2C;EAE7D,IAAI,CAAC,MAAM,OAAO,MAAM,OAAO,MAAM,SAAS,EAC5C,MAAM,IAAI,MAAM,yCAAyC;EAE3D,IAAI,MAAM,WAAW,KAAK,QACxB,MAAM,IAAI,MAAM,oCAAoC,KAAK,OAAO,QAAQ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UAC4D;IAC5D,MAAM,YAAY,QAAQ,KAAK;IAC/B,OAAO,eAAe;KACpB,QAAQ;KACR,MAAM,CAAC,OAAO,MAAM,UAAU,EAAE,OAAO,OAAO,UAAU,CAAC;KACzD,SAAS;MAAE,SAAS;MAAe,UAAU;MAAO;KACpD,UAAU;MACR,cAAc;MACd,UAAU;MACV,UAAU;MACX;KACF,CAAC;;GAEL;EACD,kBAAkB;GAChB,MAAM,EAAE,SAAS,gBAAgB;GACjC,OACE,MACA,UAC4D;IAC5D,MAAM,YAAY,QAAQ,KAAK;IAC/B,OAAO,eAAe;KACpB,QAAQ;KACR,MAAM,CAAC,OAAO,MAAM,UAAU,EAAE,OAAO,OAAO,UAAU,CAAC;KACzD,SAAS;MAAE,SAAS;MAAe,UAAU;MAAO;KACpD,UAAU;MACR,cAAc;MACd,UAAU;MACV,UAAU;MACX;KACF,CAAC;;GAEL;EACF;;AAqDH,MAAa,mBAET;CAnDF,MAAM;CACN,IAAI;CACJ,UAAU;CACV,UAAU;CACV,SAAS;CACT,cAAc,EACZ,UAAU,EACR,mBAAmB,MACpB,EACF;CACD,WAAW,EACT,MAAM,wBACP;CACD,OAAO;EACL,YAAY;GACV,kBAAkB,MAAM,KAAK,sBAAsB,QAAQ,CAAC;GAC5D,QAAQ;IACN,SAAS;IACT,OAAO;IACP,OAAO;IACR;GACD,aAAa,CACX;IACE,SAAS;IACT,OAAO;IACP,OAAO;IACR,CACF;GACF;EACD,gBAAgB,EACd,QAAQ;GACN,SAAS;GACT,OAAO;GACP,OAAO;GACR,EACF;EACD,qBAAqB,EACnB,QAAQ;GACN,SAAS;GACT,OAAO;GACP,OAAO;GACR,EACF;EACD,SAAS,CACP;GAAE,QAAQ;GAAgB,UAAU;GAAO,UAAU;GAAY,YAAY;GAAU,CACxF;EACF;CAKC"}
package/dist/pack.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { t as CodecTypes } from "./codec-types-CQubO6uQ.mjs";
1
+ import { t as CodecTypes } from "./codec-types-D9jU-l1B.mjs";
2
2
  import * as _$_prisma_next_framework_components_codec0 from "@prisma-next/framework-components/codec";
3
3
 
4
4
  //#region src/core/descriptor-meta.d.ts
@@ -53,6 +53,13 @@ declare const pgvectorPackMetaBase: {
53
53
  readonly alias: "Vector";
54
54
  }];
55
55
  };
56
+ readonly operationTypes: {
57
+ readonly import: {
58
+ readonly package: "@prisma-next/extension-pgvector/operation-types";
59
+ readonly named: "OperationTypes";
60
+ readonly alias: "PgVectorOperationTypes";
61
+ };
62
+ };
56
63
  readonly queryOperationTypes: {
57
64
  readonly import: {
58
65
  readonly package: "@prisma-next/extension-pgvector/operation-types";
@@ -1 +1 @@
1
- {"version":3,"file":"pack.d.mts","names":[],"sources":["../src/core/descriptor-meta.ts"],"mappings":";;;;cA+CM,oBAAA;EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iCAyCI,0CAAA,CAAA,eAAA;MAAA;;;;;;;;;;;;;;;;;;;;;;;;;;cAEG,gBAAA,SAAyB,oBAAA;EAAA,SAC3B,YAAA,GAAe,UAAA;AAAA"}
1
+ {"version":3,"file":"pack.d.mts","names":[],"sources":["../src/core/descriptor-meta.ts"],"mappings":";;;;cA2DM,oBAAA;EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iCAgDI,0CAAA,CAAA,eAAA;MAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAEG,gBAAA,SAAyB,oBAAA;EAAA,SAC3B,YAAA,GAAe,UAAA;AAAA"}
package/dist/pack.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { t as pgvectorPackMeta } from "./descriptor-meta-DEgJjLLi.mjs";
1
+ import { t as pgvectorPackMeta } from "./descriptor-meta-8UnGdpZ6.mjs";
2
2
  export { pgvectorPackMeta as default };
package/dist/runtime.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { n as pgvectorQueryOperations, r as pgvectorCodecRegistry, t as pgvectorPackMeta } from "./descriptor-meta-DEgJjLLi.mjs";
1
+ import { n as pgvectorQueryOperations, r as pgvectorCodecRegistry, t as pgvectorPackMeta } from "./descriptor-meta-8UnGdpZ6.mjs";
2
2
  //#region src/exports/runtime.ts
3
3
  const pgvectorRuntimeDescriptor = {
4
4
  kind: "extension",
package/package.json CHANGED
@@ -1,38 +1,38 @@
1
1
  {
2
2
  "name": "@prisma-next/extension-pgvector",
3
- "version": "0.6.0-dev.6",
3
+ "version": "0.6.0-dev.8",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "dependencies": {
8
8
  "@standard-schema/spec": "^1.1.0",
9
9
  "arktype": "^2.1.29",
10
- "@prisma-next/contract-authoring": "0.6.0-dev.6",
11
- "@prisma-next/contract": "0.6.0-dev.6",
12
- "@prisma-next/framework-components": "0.6.0-dev.6",
13
- "@prisma-next/migration-tools": "0.6.0-dev.6",
14
- "@prisma-next/family-sql": "0.6.0-dev.6",
15
- "@prisma-next/sql-operations": "0.6.0-dev.6",
16
- "@prisma-next/sql-contract": "0.6.0-dev.6",
17
- "@prisma-next/sql-relational-core": "0.6.0-dev.6",
18
- "@prisma-next/sql-runtime": "0.6.0-dev.6",
19
- "@prisma-next/sql-schema-ir": "0.6.0-dev.6"
10
+ "@prisma-next/contract": "0.6.0-dev.8",
11
+ "@prisma-next/contract-authoring": "0.6.0-dev.8",
12
+ "@prisma-next/family-sql": "0.6.0-dev.8",
13
+ "@prisma-next/framework-components": "0.6.0-dev.8",
14
+ "@prisma-next/sql-contract": "0.6.0-dev.8",
15
+ "@prisma-next/migration-tools": "0.6.0-dev.8",
16
+ "@prisma-next/sql-operations": "0.6.0-dev.8",
17
+ "@prisma-next/sql-relational-core": "0.6.0-dev.8",
18
+ "@prisma-next/sql-runtime": "0.6.0-dev.8",
19
+ "@prisma-next/sql-schema-ir": "0.6.0-dev.8"
20
20
  },
21
21
  "devDependencies": {
22
22
  "tsdown": "0.22.0",
23
23
  "typescript": "5.9.3",
24
24
  "vitest": "4.1.5",
25
- "@prisma-next/adapter-postgres": "0.6.0-dev.6",
26
- "@prisma-next/operations": "0.6.0-dev.6",
27
- "@prisma-next/cli": "0.6.0-dev.6",
25
+ "@prisma-next/adapter-postgres": "0.6.0-dev.8",
26
+ "@prisma-next/cli": "0.6.0-dev.8",
27
+ "@prisma-next/operations": "0.6.0-dev.8",
28
+ "@prisma-next/sql-contract-ts": "0.6.0-dev.8",
29
+ "@prisma-next/target-postgres": "0.6.0-dev.8",
28
30
  "@prisma-next/test-utils": "0.0.1",
29
- "@prisma-next/target-postgres": "0.6.0-dev.6",
30
- "@prisma-next/sql-contract-ts": "0.6.0-dev.6",
31
31
  "@prisma-next/tsconfig": "0.0.0",
32
32
  "@prisma-next/tsdown": "0.0.0"
33
33
  },
34
34
  "peerDependencies": {
35
- "@prisma-next/adapter-postgres": "0.6.0-dev.6"
35
+ "@prisma-next/adapter-postgres": "0.6.0-dev.8"
36
36
  },
37
37
  "peerDependenciesMeta": {
38
38
  "@prisma-next/adapter-postgres": {
@@ -49,9 +49,9 @@ export class PgVectorCodec extends CodecImpl<
49
49
  string,
50
50
  number[]
51
51
  > {
52
- readonly length: number | undefined;
52
+ readonly length: number;
53
53
 
54
- constructor(descriptor: AnyCodecDescriptor, length: number | undefined) {
54
+ constructor(descriptor: AnyCodecDescriptor, length: number) {
55
55
  super(descriptor);
56
56
  this.length = length;
57
57
  }
@@ -63,7 +63,7 @@ export class PgVectorCodec extends CodecImpl<
63
63
  if (!value.every((v) => typeof v === 'number')) {
64
64
  throw new Error('Vector value must contain only numbers');
65
65
  }
66
- if (this.length !== undefined && value.length !== this.length) {
66
+ if (value.length !== this.length) {
67
67
  throw new Error(`Vector length mismatch: expected ${this.length}, got ${value.length}`);
68
68
  }
69
69
  }
@@ -115,11 +115,8 @@ export class PgVectorDescriptor extends CodecDescriptorImpl<VectorParams> {
115
115
  override renderOutputType(params: VectorParams): string {
116
116
  return `Vector<${params.length}>`;
117
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
118
  override factory(params: VectorParams): (ctx: CodecInstanceContext) => PgVectorCodec {
122
- return () => new PgVectorCodec(this, (params as VectorParams | undefined)?.length);
119
+ return () => new PgVectorCodec(this, params.length);
123
120
  }
124
121
  }
125
122
 
@@ -1,4 +1,10 @@
1
- import { buildOperation, refsOf, toExpr } from '@prisma-next/sql-relational-core/expression';
1
+ import {
2
+ buildOperation,
3
+ type CodecExpression,
4
+ codecOf,
5
+ type Expression,
6
+ toExpr,
7
+ } from '@prisma-next/sql-relational-core/expression';
2
8
  import type { CodecTypes } from '../types/codec-types';
3
9
  import type { QueryOperationTypes } from '../types/operation-types';
4
10
  import { pgvectorAuthoringTypes } from './authoring';
@@ -12,11 +18,14 @@ export function pgvectorQueryOperations<CT extends CodecTypesBase>(): QueryOpera
12
18
  return {
13
19
  cosineDistance: {
14
20
  self: { codecId: pgvectorTypeId },
15
- impl: (self, other) => {
16
- const selfRefs = refsOf(self);
21
+ impl: (
22
+ self: CodecExpression<'pg/vector@1', boolean, CT>,
23
+ other: CodecExpression<'pg/vector@1', boolean, CT>,
24
+ ): Expression<{ codecId: 'pg/float8@1'; nullable: false }> => {
25
+ const selfCodec = codecOf(self);
17
26
  return buildOperation({
18
27
  method: 'cosineDistance',
19
- args: [toExpr(self, pgvectorTypeId, selfRefs), toExpr(other, pgvectorTypeId, selfRefs)],
28
+ args: [toExpr(self, selfCodec), toExpr(other, selfCodec)],
20
29
  returns: { codecId: 'pg/float8@1', nullable: false },
21
30
  lowering: {
22
31
  targetFamily: 'sql',
@@ -28,11 +37,14 @@ export function pgvectorQueryOperations<CT extends CodecTypesBase>(): QueryOpera
28
37
  },
29
38
  cosineSimilarity: {
30
39
  self: { codecId: pgvectorTypeId },
31
- impl: (self, other) => {
32
- const selfRefs = refsOf(self);
40
+ impl: (
41
+ self: CodecExpression<'pg/vector@1', boolean, CT>,
42
+ other: CodecExpression<'pg/vector@1', boolean, CT>,
43
+ ): Expression<{ codecId: 'pg/float8@1'; nullable: false }> => {
44
+ const selfCodec = codecOf(self);
33
45
  return buildOperation({
34
46
  method: 'cosineSimilarity',
35
- args: [toExpr(self, pgvectorTypeId, selfRefs), toExpr(other, pgvectorTypeId, selfRefs)],
47
+ args: [toExpr(self, selfCodec), toExpr(other, selfCodec)],
36
48
  returns: { codecId: 'pg/float8@1', nullable: false },
37
49
  lowering: {
38
50
  targetFamily: 'sql',
@@ -75,6 +87,13 @@ const pgvectorPackMetaBase = {
75
87
  },
76
88
  ],
77
89
  },
90
+ operationTypes: {
91
+ import: {
92
+ package: '@prisma-next/extension-pgvector/operation-types',
93
+ named: 'OperationTypes',
94
+ alias: 'PgVectorOperationTypes',
95
+ },
96
+ },
78
97
  queryOperationTypes: {
79
98
  import: {
80
99
  package: '@prisma-next/extension-pgvector/operation-types',
@@ -1,20 +1,10 @@
1
1
  /**
2
- * Column type descriptors for pgvector extension.
3
- *
4
- * 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.
2
+ * Column type descriptor factory for pgvector extension. `vector(N)` is the canonical authoring surface; every pgvector column must declare a dimension via this factory. The dimension threads into the runtime codec through `paramsSchema.length` and into the DDL via the family-layer `expandNativeType` hook (e.g. `vector(1536)`).
5
3
  */
6
4
 
7
5
  import type { ColumnTypeDescriptor } from '@prisma-next/framework-components/codec';
8
6
  import { VECTOR_CODEC_ID, VECTOR_MAX_DIM } from '../core/constants';
9
7
 
10
- /**
11
- * Static vector column descriptor without dimension. Use `vector(N)` for dimensioned vectors that produce `vector(N)` DDL.
12
- */
13
- export const vectorColumn = {
14
- codecId: VECTOR_CODEC_ID,
15
- nativeType: 'vector',
16
- } as const satisfies ColumnTypeDescriptor;
17
-
18
8
  /**
19
9
  * Factory for creating dimensioned vector column descriptors.
20
10
  *
@@ -1 +0,0 @@
1
- {"version":3,"file":"descriptor-meta-DEgJjLLi.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 { buildOperation, refsOf, toExpr } from '@prisma-next/sql-relational-core/expression';\nimport type { CodecTypes } from '../types/codec-types';\nimport type { QueryOperationTypes } from '../types/operation-types';\nimport { pgvectorAuthoringTypes } from './authoring';\nimport { pgvectorCodecRegistry } from './registry';\n\nconst pgvectorTypeId = 'pg/vector@1' as const;\n\ntype CodecTypesBase = Record<string, { readonly input: unknown; readonly output: unknown }>;\n\nexport function pgvectorQueryOperations<CT extends CodecTypesBase>(): QueryOperationTypes<CT> {\n return {\n cosineDistance: {\n self: { codecId: pgvectorTypeId },\n impl: (self, other) => {\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 cosineSimilarity: {\n self: { codecId: pgvectorTypeId },\n impl: (self, other) => {\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 queryOperationTypes: {\n import: {\n package: '@prisma-next/extension-pgvector/operation-types',\n named: 'QueryOperationTypes',\n alias: 'PgVectorQueryOperationTypes',\n },\n },\n storage: [\n { typeId: pgvectorTypeId, familyId: 'sql', targetId: 'postgres', nativeType: 'vector' },\n ],\n },\n} as const;\n\nexport const pgvectorPackMeta: typeof pgvectorPackMetaBase & {\n readonly __codecTypes?: CodecTypes;\n} = pgvectorPackMetaBase;\n"],"mappings":";;;;;;AAGA,MAAa,yBAAyB,EACpC,UAAU,EACR,QAAQ;CACN,MAAM;CACN,MAAM,CACJ;EAAE,MAAM;EAAU,MAAM;EAAU,SAAS;EAAM,SAAS;EAAG,SAAS;EAAgB,CACvF;CACD,QAAQ;EACN,SAAS;EACT,YAAY;EACZ,YAAY,EACV,QAAQ;GAAE,MAAM;GAAO,OAAO;GAAG,EAClC;EACF;CACF,EACF,EACF;;;ACWD,MAAM,qBAAqBA,KAAQ,EACjC,QAAQ,UACT,CAAC,CAAC,QAAQ,QAAQ,QAAQ;CACzB,MAAM,EAAE,WAAW;CACnB,IAAI,CAAC,OAAO,UAAU,OAAO,EAC3B,OAAO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