jazz-tools 0.18.25 → 0.18.26

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 (40) hide show
  1. package/.turbo/turbo-build.log +44 -44
  2. package/CHANGELOG.md +10 -0
  3. package/dist/{chunk-DOCEAUVD.js → chunk-ZIAN4UY5.js} +338 -9
  4. package/dist/chunk-ZIAN4UY5.js.map +1 -0
  5. package/dist/index.js +1 -1
  6. package/dist/testing.js +1 -1
  7. package/dist/tools/coValues/coVector.d.ts +127 -0
  8. package/dist/tools/coValues/coVector.d.ts.map +1 -0
  9. package/dist/tools/implementation/zodSchema/coExport.d.ts +2 -1
  10. package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
  11. package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts.map +1 -1
  12. package/dist/tools/implementation/zodSchema/schemaTypes/CoVectorSchema.d.ts +47 -0
  13. package/dist/tools/implementation/zodSchema/schemaTypes/CoVectorSchema.d.ts.map +1 -0
  14. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +2 -2
  15. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  16. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +2 -2
  17. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
  18. package/dist/tools/implementation/zodSchema/zodCo.d.ts +2 -1
  19. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  20. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +3 -2
  21. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  22. package/dist/tools/internal.d.ts +2 -0
  23. package/dist/tools/internal.d.ts.map +1 -1
  24. package/dist/tools/tests/coVector.test-d.d.ts +2 -0
  25. package/dist/tools/tests/coVector.test-d.d.ts.map +1 -0
  26. package/dist/tools/tests/coVector.test.d.ts +2 -0
  27. package/dist/tools/tests/coVector.test.d.ts.map +1 -0
  28. package/package.json +4 -4
  29. package/src/tools/coValues/coVector.ts +432 -0
  30. package/src/tools/implementation/zodSchema/coExport.ts +2 -0
  31. package/src/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.ts +13 -0
  32. package/src/tools/implementation/zodSchema/schemaTypes/CoVectorSchema.ts +105 -0
  33. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +9 -5
  34. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +15 -9
  35. package/src/tools/implementation/zodSchema/zodCo.ts +15 -0
  36. package/src/tools/implementation/zodSchema/zodSchema.ts +15 -6
  37. package/src/tools/internal.ts +2 -0
  38. package/src/tools/tests/coVector.test-d.ts +40 -0
  39. package/src/tools/tests/coVector.test.ts +891 -0
  40. package/dist/chunk-DOCEAUVD.js.map +0 -1
@@ -0,0 +1,432 @@
1
+ import type { RawBinaryCoStream } from "cojson";
2
+ import { cojsonInternals } from "cojson";
3
+ import {
4
+ AnonymousJazzAgent,
5
+ CoValue,
6
+ CoValueClass,
7
+ getCoValueOwner,
8
+ Group,
9
+ ID,
10
+ Resolved,
11
+ SubscribeListenerOptions,
12
+ SubscribeRestArgs,
13
+ TypeSym,
14
+ } from "../internal.js";
15
+ import {
16
+ Account,
17
+ CoValueJazzApi,
18
+ inspect,
19
+ loadCoValueWithoutMe,
20
+ parseCoValueCreateOptions,
21
+ parseSubscribeRestArgs,
22
+ subscribeToCoValueWithoutMe,
23
+ subscribeToExistingCoValue,
24
+ } from "../internal.js";
25
+
26
+ /**
27
+ * CoVectors are collaborative storages of vectors (floating point arrays).
28
+ *
29
+ * @category CoValues
30
+ */
31
+ export class CoVector
32
+ extends Float32Array
33
+ implements Readonly<Float32Array>, CoValue
34
+ {
35
+ declare $jazz: CoVectorJazzApi<this>;
36
+
37
+ /** @category Type Helpers */
38
+ declare [TypeSym]: "BinaryCoStream";
39
+
40
+ static get [Symbol.species]() {
41
+ return Float32Array;
42
+ }
43
+
44
+ protected static requiredDimensionsCount: number | undefined = undefined;
45
+ private declare _isVectorLoaded: boolean;
46
+ private declare _requiredDimensionsCount: number;
47
+
48
+ constructor(
49
+ options:
50
+ | {
51
+ owner: Account | Group;
52
+ }
53
+ | {
54
+ fromRaw: RawBinaryCoStream;
55
+ },
56
+ ) {
57
+ const dimensionsCount = (new.target as typeof CoVector)
58
+ .requiredDimensionsCount;
59
+
60
+ if (dimensionsCount === undefined) {
61
+ throw new Error(
62
+ "Instantiating CoVector without a dimensions count is not allowed. Use co.vector(...).create() instead.",
63
+ );
64
+ }
65
+
66
+ // Initialize empty Float32Array buffer with the expected vector length
67
+ // to be filled with the vector data later
68
+ super(dimensionsCount);
69
+
70
+ const isFromRaw = "fromRaw" in options;
71
+
72
+ const raw: RawBinaryCoStream = isFromRaw
73
+ ? options.fromRaw
74
+ : options.owner.$jazz.raw.createBinaryStream();
75
+
76
+ Object.defineProperties(this, {
77
+ [TypeSym]: { value: "BinaryCoStream", enumerable: false },
78
+ $jazz: {
79
+ value: new CoVectorJazzApi(this, raw),
80
+ enumerable: false,
81
+ },
82
+ _isVectorLoaded: { value: false, enumerable: false, writable: true },
83
+ _requiredDimensionsCount: {
84
+ value: dimensionsCount,
85
+ enumerable: false,
86
+ writable: false,
87
+ },
88
+ });
89
+
90
+ if (isFromRaw) {
91
+ this.loadVectorData();
92
+ }
93
+ }
94
+
95
+ /** @category Internals */
96
+ static fromRaw<V extends CoVector>(
97
+ this: CoValueClass<V> & typeof CoVector,
98
+ raw: RawBinaryCoStream,
99
+ ) {
100
+ return new this({ fromRaw: raw });
101
+ }
102
+
103
+ /**
104
+ * Create a new `CoVector` instance with the given vector.
105
+ *
106
+ * @category Creation
107
+ * @deprecated Use `co.vector(...).create` instead.
108
+ */
109
+ static create<S extends CoVector>(
110
+ this: CoValueClass<S> & typeof CoVector,
111
+ vector: number[] | Float32Array,
112
+ options?: { owner?: Account | Group } | Account | Group,
113
+ ) {
114
+ const vectorAsFloat32Array =
115
+ vector instanceof Float32Array ? vector : new Float32Array(vector);
116
+
117
+ const givenVectorDimensions =
118
+ vectorAsFloat32Array.byteLength / vectorAsFloat32Array.BYTES_PER_ELEMENT;
119
+
120
+ if (
121
+ this.requiredDimensionsCount !== undefined &&
122
+ givenVectorDimensions !== this.requiredDimensionsCount
123
+ ) {
124
+ throw new Error(
125
+ `Vector dimension mismatch! Expected ${this.requiredDimensionsCount} dimensions, got ${
126
+ givenVectorDimensions
127
+ }`,
128
+ );
129
+ }
130
+
131
+ const coVector = new this(parseCoValueCreateOptions(options));
132
+ coVector.setVectorData(vectorAsFloat32Array);
133
+
134
+ const byteArray = CoVector.toByteArray(vectorAsFloat32Array);
135
+
136
+ coVector.$jazz.raw.startBinaryStream({
137
+ mimeType: "application/vector+octet-stream",
138
+ totalSizeBytes: byteArray.byteLength,
139
+ });
140
+
141
+ const chunkSize =
142
+ cojsonInternals.TRANSACTION_CONFIG.MAX_RECOMMENDED_TX_SIZE;
143
+
144
+ // Although most embedding vectors are small
145
+ // (3072-dimensional vector is only 12,288 bytes),
146
+ // we should still chunk the data to avoid transaction size limits
147
+ for (let idx = 0; idx < byteArray.length; idx += chunkSize) {
148
+ coVector.$jazz.raw.pushBinaryStreamChunk(
149
+ byteArray.slice(idx, idx + chunkSize),
150
+ );
151
+ }
152
+ coVector.$jazz.raw.endBinaryStream();
153
+
154
+ return coVector;
155
+ }
156
+
157
+ private static toByteArray(vector: Float32Array): Uint8Array {
158
+ // zero copy view of the vector bytes
159
+ return new Uint8Array(vector.buffer, vector.byteOffset, vector.byteLength);
160
+ }
161
+
162
+ private static fromByteArray(bytesChunks: Uint8Array[]): Float32Array {
163
+ const total = bytesChunks.reduce((acc, c) => acc + c.byteLength, 0);
164
+
165
+ if (total % 4 !== 0)
166
+ throw new Error("[INTERNAL] Total byte length must be multiple of 4");
167
+
168
+ const u8 = new Uint8Array(total);
169
+ let off = 0;
170
+
171
+ for (const c of bytesChunks) {
172
+ u8.set(c, off);
173
+ off += c.byteLength;
174
+ }
175
+
176
+ return new Float32Array(u8.buffer, u8.byteOffset, total / 4);
177
+ }
178
+
179
+ private loadVectorData(): void {
180
+ if (this._isVectorLoaded === true) {
181
+ return;
182
+ }
183
+
184
+ const chunks = this.$jazz.raw.getBinaryChunks();
185
+
186
+ if (!chunks) {
187
+ // This should never happen
188
+ throw new Error(`CoVector '${this.$jazz.raw.id}' is not loaded`);
189
+ }
190
+
191
+ const vector = CoVector.fromByteArray(chunks.chunks);
192
+
193
+ if (vector.length !== this._requiredDimensionsCount) {
194
+ throw new Error(
195
+ `Vector dimension mismatch! CoVector '${this.$jazz.raw.id}' loaded with ${vector.length} dimensions, but the schema requires ${this._requiredDimensionsCount} dimensions`,
196
+ );
197
+ }
198
+
199
+ this.setVectorData(vector);
200
+
201
+ return;
202
+ }
203
+
204
+ private setVectorData(vector: Float32Array): void {
205
+ super.set(vector, 0);
206
+ this._isVectorLoaded = true;
207
+ }
208
+
209
+ /**
210
+ * Get a JSON representation of the `CoVector`
211
+ * @category Content
212
+ */
213
+ toJSON(): Array<number> {
214
+ return Array.from(this);
215
+ }
216
+
217
+ /** @internal */
218
+ [inspect]() {
219
+ return this.toJSON();
220
+ }
221
+
222
+ /**
223
+ * Load a `CoVector`
224
+ *
225
+ * @category Subscription & Loading
226
+ * @deprecated Use `co.vector(...).load` instead.
227
+ */
228
+ static async load<C extends CoVector>(
229
+ this: CoValueClass<C>,
230
+ id: ID<C>,
231
+ options?: {
232
+ loadAs?: Account | AnonymousJazzAgent;
233
+ },
234
+ ): Promise<C | null> {
235
+ const coVector = await loadCoValueWithoutMe(this, id, options);
236
+
237
+ /**
238
+ * We are only interested in the entire vector. Since most vectors are small (<15kB),
239
+ * we can wait for the stream to be complete before returning the vector
240
+ */
241
+ if (!coVector?.$jazz.raw.isBinaryStreamEnded()) {
242
+ return new Promise<C | null>((resolve) => {
243
+ subscribeToCoValueWithoutMe(
244
+ this,
245
+ id,
246
+ options || {},
247
+ (value, unsubscribe) => {
248
+ if (value.$jazz.raw.isBinaryStreamEnded()) {
249
+ unsubscribe();
250
+ resolve(value);
251
+ }
252
+ },
253
+ );
254
+ });
255
+ }
256
+
257
+ coVector.loadVectorData();
258
+ return coVector;
259
+ }
260
+
261
+ /**
262
+ * Subscribe to a `CoVector`, when you have an ID but don't have a `CoVector` instance yet
263
+ * @category Subscription & Loading
264
+ * @deprecated Use `co.vector(...).subscribe` instead.
265
+ */
266
+ static subscribe<V extends CoVector>(
267
+ this: CoValueClass<V>,
268
+ id: ID<V>,
269
+ listener: (value: Resolved<V, true>, unsubscribe: () => void) => void,
270
+ ): () => void;
271
+ static subscribe<V extends CoVector>(
272
+ this: CoValueClass<V>,
273
+ id: ID<V>,
274
+ options: SubscribeListenerOptions<V, true>,
275
+ listener: (value: Resolved<V, true>, unsubscribe: () => void) => void,
276
+ ): () => void;
277
+ static subscribe<V extends CoVector>(
278
+ this: CoValueClass<V>,
279
+ id: ID<V>,
280
+ ...args: SubscribeRestArgs<V, true>
281
+ ): () => void {
282
+ const { options, listener } = parseSubscribeRestArgs(args);
283
+ return subscribeToCoValueWithoutMe<V, true>(this, id, options, listener);
284
+ }
285
+
286
+ // CoVector mutation method overrides, as CoVectors aren't meant to be mutated
287
+ /**
288
+ * Calling `copyWithin` on a CoVector is forbidden. CoVectors are immutable.
289
+ * @deprecated If you want to change the vector, replace the former instance of CoVector with a new one.
290
+ */
291
+ override copyWithin(target: number, start: number, end?: number): never {
292
+ throw new Error("Cannot mutate a CoVector using `copyWithin`");
293
+ }
294
+ /**
295
+ * Calling `fill` on a CoVector is forbidden. CoVectors are immutable.
296
+ * @deprecated If you want to change the vector, replace the former instance of CoVector with a new one.
297
+ */
298
+ override fill(value: number, start?: number, end?: number): never {
299
+ throw new Error("Cannot mutate a CoVector using `fill`");
300
+ }
301
+ /**
302
+ * Calling `reverse` on a CoVector is forbidden. CoVectors are immutable.
303
+ * @deprecated If you want to change the vector, replace the former instance of CoVector with a new one.
304
+ */
305
+ override reverse(): never {
306
+ throw new Error("Cannot mutate a CoVector using `reverse`");
307
+ }
308
+ /**
309
+ * Calling `set` on a CoVector is forbidden. CoVectors are immutable.
310
+ * @deprecated If you want to change the vector, replace the former instance of CoVector with a new one.
311
+ // */
312
+ override set(array: ArrayLike<number>, offset?: number): never {
313
+ throw new Error("Cannot mutate a CoVector using `set`");
314
+ }
315
+ /**
316
+ * Calling `sort` on a CoVector is forbidden. CoVectors are immutable.
317
+ * @deprecated If you want to change the vector, replace the former instance of CoVector with a new one.
318
+ */
319
+ override sort(compareFn?: (a: number, b: number) => number): never {
320
+ throw new Error("Cannot mutate a CoVector using `sort`");
321
+ }
322
+ }
323
+
324
+ export class CoVectorJazzApi<V extends CoVector> extends CoValueJazzApi<V> {
325
+ constructor(
326
+ private coVector: V,
327
+ public raw: RawBinaryCoStream,
328
+ ) {
329
+ super(coVector);
330
+ }
331
+
332
+ get owner(): Group {
333
+ return getCoValueOwner(this.coVector);
334
+ }
335
+
336
+ /**
337
+ * An instance method to subscribe to an existing `CoVector`
338
+ * @category Subscription & Loading
339
+ */
340
+ subscribe<B extends CoVector>(
341
+ this: CoVectorJazzApi<B>,
342
+ listener: (value: Resolved<B, true>) => void,
343
+ ): () => void {
344
+ return subscribeToExistingCoValue(this.coVector, {}, listener);
345
+ }
346
+
347
+ /**
348
+ * Wait for the `CoVector` to be uploaded to the other peers.
349
+ *
350
+ * @category Subscription & Loading
351
+ */
352
+ waitForSync(options?: { timeout?: number }) {
353
+ return this.raw.core.waitForSync(options);
354
+ }
355
+
356
+ // Vector operations
357
+ /**
358
+ * Calculate the magnitude of this vector.
359
+ */
360
+ magnitude(): number {
361
+ return VectorCalculation.magnitude(this.coVector);
362
+ }
363
+
364
+ /**
365
+ * Normalize this vector.
366
+ * @returns A new instance of a normalized vector.
367
+ */
368
+ normalize(): Float32Array {
369
+ return VectorCalculation.normalize(this.coVector);
370
+ }
371
+
372
+ /**
373
+ * Calculate the dot product of this vector and another vector.
374
+ */
375
+ dotProduct(otherVector: CoVector | Float32Array | number[]): number {
376
+ return VectorCalculation.dotProduct(this.coVector, otherVector);
377
+ }
378
+
379
+ /**
380
+ * Calculate the cosine similarity between this vector and another vector.
381
+ *
382
+ * @returns A value between `-1` and `1`:
383
+ * - `1` means the vectors are identical
384
+ * - `0` means the vectors are orthogonal (i.e. no similarity)
385
+ * - `-1` means the vectors are opposite direction (perfectly dissimilar)
386
+ */
387
+ cosineSimilarity(otherVector: CoVector | Float32Array | number[]): number {
388
+ return VectorCalculation.cosineSimilarity(this.coVector, otherVector);
389
+ }
390
+ }
391
+
392
+ const VectorCalculation = {
393
+ magnitude: (vector: Float32Array | number[]) => {
394
+ let sum = 0;
395
+ for (const v of vector) {
396
+ sum += v * v;
397
+ }
398
+ return Math.sqrt(sum);
399
+ },
400
+ normalize: (vector: Float32Array) => {
401
+ const mag = VectorCalculation.magnitude(vector);
402
+
403
+ if (mag === 0) {
404
+ return new Float32Array(vector.length).fill(0);
405
+ }
406
+
407
+ return vector.map((v) => v / mag);
408
+ },
409
+ dotProduct: (vectorA: Float32Array, vectorB: Float32Array | number[]) => {
410
+ if (vectorA.length !== vectorB.length) {
411
+ throw new Error(
412
+ `Vector dimensions don't match: ${vectorA.length} vs ${vectorB.length}`,
413
+ );
414
+ }
415
+
416
+ return vectorA.reduce((sum, a, i) => sum + a * vectorB[i]!, 0);
417
+ },
418
+ cosineSimilarity: (
419
+ vectorA: Float32Array,
420
+ vectorB: Float32Array | number[],
421
+ ) => {
422
+ const magnitudeA = VectorCalculation.magnitude(vectorA);
423
+ const magnitudeB = VectorCalculation.magnitude(vectorB);
424
+
425
+ if (magnitudeA === 0 || magnitudeB === 0) {
426
+ return 0;
427
+ }
428
+
429
+ const dotProductAB = VectorCalculation.dotProduct(vectorA, vectorB);
430
+ return dotProductAB / (magnitudeA * magnitudeB);
431
+ },
432
+ };
@@ -7,6 +7,7 @@ export { CoFeedSchema as Feed } from "./schemaTypes/CoFeedSchema.js";
7
7
  export { PlainTextSchema as PlainText } from "./schemaTypes/PlainTextSchema.js";
8
8
  export { RichTextSchema as RichText } from "./schemaTypes/RichTextSchema.js";
9
9
  export { FileStreamSchema as FileStream } from "./schemaTypes/FileStreamSchema.js";
10
+ export { CoVectorSchema as Vector } from "./schemaTypes/CoVectorSchema.js";
10
11
  export { CoInput as input } from "./typeConverters/CoFieldSchemaInit.js";
11
12
  export {
12
13
  AccountSchema as Account,
@@ -23,6 +24,7 @@ export {
23
24
  coPlainTextDefiner as plainText,
24
25
  coRichTextDefiner as richText,
25
26
  coFileStreamDefiner as fileStream,
27
+ coVectorDefiner as vector,
26
28
  coImageDefiner as image,
27
29
  coAccountDefiner as account,
28
30
  coGroupDefiner as group,
@@ -12,12 +12,14 @@ import {
12
12
  CoValueClass,
13
13
  FileStream,
14
14
  FileStreamSchema,
15
+ CoVectorSchema,
15
16
  PlainTextSchema,
16
17
  SchemaUnion,
17
18
  enrichAccountSchema,
18
19
  enrichCoMapSchema,
19
20
  isCoValueClass,
20
21
  Group,
22
+ CoVector,
21
23
  } from "../../../internal.js";
22
24
  import { coField } from "../../schema.js";
23
25
 
@@ -123,6 +125,17 @@ export function hydrateCoreCoValueSchema<S extends AnyCoreCoValueSchema>(
123
125
  } else if (schema.builtin === "FileStream") {
124
126
  const coValueClass = FileStream;
125
127
  return new FileStreamSchema(coValueClass) as CoValueSchemaFromCoreSchema<S>;
128
+ } else if (schema.builtin === "CoVector") {
129
+ const dimensions = schema.dimensions;
130
+
131
+ const coValueClass = class CoVectorWithDimensions extends CoVector {
132
+ protected static requiredDimensionsCount = dimensions;
133
+ };
134
+
135
+ return new CoVectorSchema(
136
+ dimensions,
137
+ coValueClass,
138
+ ) as CoValueSchemaFromCoreSchema<S>;
126
139
  } else if (schema.builtin === "CoPlainText") {
127
140
  const coValueClass = CoPlainText;
128
141
  return new PlainTextSchema(coValueClass) as CoValueSchemaFromCoreSchema<S>;
@@ -0,0 +1,105 @@
1
+ import {
2
+ Account,
3
+ AnonymousJazzAgent,
4
+ CoVector,
5
+ Group,
6
+ InstanceOrPrimitiveOfSchema,
7
+ InstanceOrPrimitiveOfSchemaCoValuesNullable,
8
+ coOptionalDefiner,
9
+ } from "../../../internal.js";
10
+ import { CoOptionalSchema } from "./CoOptionalSchema.js";
11
+ import { CoreCoValueSchema } from "./CoValueSchema.js";
12
+
13
+ export interface CoreCoVectorSchema extends CoreCoValueSchema {
14
+ builtin: "CoVector";
15
+ dimensions: number;
16
+ }
17
+
18
+ export function createCoreCoVectorSchema(
19
+ dimensions: number,
20
+ ): CoreCoVectorSchema {
21
+ return {
22
+ collaborative: true as const,
23
+ builtin: "CoVector" as const,
24
+ dimensions,
25
+ };
26
+ }
27
+
28
+ export class CoVectorSchema implements CoreCoVectorSchema {
29
+ readonly collaborative = true as const;
30
+ readonly builtin = "CoVector" as const;
31
+
32
+ constructor(
33
+ public dimensions: number,
34
+ private coValueClass: typeof CoVector,
35
+ ) {}
36
+
37
+ /**
38
+ * Create a `CoVector` from a given vector.
39
+ */
40
+ create(
41
+ vector: number[] | Float32Array,
42
+ options?: { owner: Group } | Group,
43
+ ): CoVectorInstance;
44
+ /**
45
+ * Create a `CoVector` from a given vector.
46
+ *
47
+ * @deprecated Creating CoValues with an Account as owner is deprecated. Use a Group instead.
48
+ */
49
+ create(
50
+ vector: number[] | Float32Array,
51
+ options?: { owner: Account | Group } | Account | Group,
52
+ ): CoVectorInstance;
53
+ create(
54
+ vector: number[] | Float32Array,
55
+ options?: { owner: Account | Group } | Account | Group,
56
+ ): CoVectorInstance {
57
+ return this.coValueClass.create(vector, options);
58
+ }
59
+
60
+ /**
61
+ * Load a `CoVector` with a given ID.
62
+ */
63
+ load(
64
+ id: string,
65
+ options?: { loadAs: Account | AnonymousJazzAgent },
66
+ ): Promise<CoVectorInstanceNullable> {
67
+ return this.coValueClass.load(id, options);
68
+ }
69
+
70
+ /**
71
+ * Subscribe to a `CoVector`, when you have an ID but don't have a `CoVector` instance yet
72
+ */
73
+ subscribe(
74
+ id: string,
75
+ options: { loadAs: Account | AnonymousJazzAgent },
76
+ listener: (
77
+ value: CoVectorInstanceNullable,
78
+ unsubscribe: () => void,
79
+ ) => void,
80
+ ): () => void;
81
+ subscribe(
82
+ id: string,
83
+ listener: (
84
+ value: CoVectorInstanceNullable,
85
+ unsubscribe: () => void,
86
+ ) => void,
87
+ ): () => void;
88
+ subscribe(...args: [any, ...any[]]) {
89
+ // @ts-expect-error
90
+ return this.coValueClass.subscribe(...args);
91
+ }
92
+
93
+ getCoValueClass(): typeof CoVector {
94
+ return this.coValueClass;
95
+ }
96
+
97
+ optional(): CoOptionalSchema<this> {
98
+ return coOptionalDefiner(this);
99
+ }
100
+ }
101
+
102
+ export type CoVectorInstance = InstanceOrPrimitiveOfSchema<CoVectorSchema>;
103
+
104
+ export type CoVectorInstanceNullable =
105
+ InstanceOrPrimitiveOfSchemaCoValuesNullable<CoVectorSchema>;
@@ -7,9 +7,11 @@ import {
7
7
  CoMap,
8
8
  CoPlainText,
9
9
  CoRichText,
10
+ CoVector,
10
11
  CoValueClass,
11
12
  CoreAccountSchema,
12
13
  CoreCoRecordSchema,
14
+ CoreCoVectorSchema,
13
15
  FileStream,
14
16
  Group,
15
17
  } from "../../../internal.js";
@@ -63,11 +65,13 @@ export type InstanceOfSchema<S extends CoValueClass | AnyZodOrCoValueSchema> =
63
65
  ? CoRichText
64
66
  : S extends CoreFileStreamSchema
65
67
  ? FileStream
66
- : S extends CoreCoOptionalSchema<infer T>
67
- ? InstanceOrPrimitiveOfSchema<T> | undefined
68
- : S extends CoDiscriminatedUnionSchema<infer Members>
69
- ? InstanceOrPrimitiveOfSchema<Members[number]>
70
- : never
68
+ : S extends CoreCoVectorSchema
69
+ ? Readonly<CoVector>
70
+ : S extends CoreCoOptionalSchema<infer T>
71
+ ? InstanceOrPrimitiveOfSchema<T> | undefined
72
+ : S extends CoDiscriminatedUnionSchema<infer Members>
73
+ ? InstanceOrPrimitiveOfSchema<Members[number]>
74
+ : never
71
75
  : S extends CoValueClass
72
76
  ? InstanceType<S>
73
77
  : never;
@@ -13,6 +13,8 @@ import {
13
13
  CoreCoListSchema,
14
14
  CoreCoMapSchema,
15
15
  CoreCoRecordSchema,
16
+ CoreCoVectorSchema,
17
+ CoVector,
16
18
  FileStream,
17
19
  Group,
18
20
  } from "../../../internal.js";
@@ -70,15 +72,19 @@ export type InstanceOfSchemaCoValuesNullable<
70
72
  ? CoRichText | null
71
73
  : S extends CoreFileStreamSchema
72
74
  ? FileStream | null
73
- : S extends CoreCoOptionalSchema<infer Inner>
74
- ?
75
- | InstanceOrPrimitiveOfSchemaCoValuesNullable<Inner>
76
- | undefined
77
- : S extends CoreCoDiscriminatedUnionSchema<infer Members>
78
- ? InstanceOrPrimitiveOfSchemaCoValuesNullable<
79
- Members[number]
80
- >
81
- : never
75
+ : S extends CoreCoVectorSchema
76
+ ? Readonly<CoVector> | null
77
+ : S extends CoreCoOptionalSchema<infer Inner>
78
+ ?
79
+ | InstanceOrPrimitiveOfSchemaCoValuesNullable<Inner>
80
+ | undefined
81
+ : S extends CoreCoDiscriminatedUnionSchema<
82
+ infer Members
83
+ >
84
+ ? InstanceOrPrimitiveOfSchemaCoValuesNullable<
85
+ Members[number]
86
+ >
87
+ : never
82
88
  : S extends CoValueClass
83
89
  ? InstanceType<S> | null
84
90
  : never;
@@ -9,6 +9,7 @@ import {
9
9
  type CoRecordSchema,
10
10
  type DefaultProfileShape,
11
11
  type FileStreamSchema,
12
+ type CoVectorSchema,
12
13
  ImageDefinition,
13
14
  type PlainTextSchema,
14
15
  type Simplify,
@@ -18,6 +19,7 @@ import {
18
19
  createCoreCoMapSchema,
19
20
  createCoreCoPlainTextSchema,
20
21
  createCoreFileStreamSchema,
22
+ createCoreCoVectorSchema,
21
23
  hydrateCoreCoValueSchema,
22
24
  isAnyCoValueSchema,
23
25
  isCoValueClass,
@@ -193,6 +195,19 @@ export const coFileStreamDefiner = (): FileStreamSchema => {
193
195
  return hydrateCoreCoValueSchema(coreSchema);
194
196
  };
195
197
 
198
+ export const coVectorDefiner = (dimensions: number): CoVectorSchema => {
199
+ const isPositiveInteger = Number.isInteger(dimensions) && dimensions > 0;
200
+
201
+ if (!isPositiveInteger) {
202
+ throw new Error(
203
+ "co.vector() expects the vector dimensions count to be a positive integer",
204
+ );
205
+ }
206
+
207
+ const coreSchema = createCoreCoVectorSchema(dimensions);
208
+ return hydrateCoreCoValueSchema(coreSchema);
209
+ };
210
+
196
211
  export const coPlainTextDefiner = (): PlainTextSchema => {
197
212
  const coreSchema = createCoreCoPlainTextSchema();
198
213
  return hydrateCoreCoValueSchema(coreSchema);