@mastra/vectorize 0.1.0-alpha.37 → 0.1.0-alpha.38

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @mastra/vectorize
2
2
 
3
+ ## 0.1.0-alpha.38
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [a9345f9]
8
+ - @mastra/core@0.2.0-alpha.102
9
+
3
10
  ## 0.1.0-alpha.37
4
11
 
5
12
  ### Patch Changes
@@ -0,0 +1,39 @@
1
+ import { BaseFilterTranslator } from '@mastra/core/filter';
2
+ import Cloudflare from 'cloudflare';
3
+ import { Filter } from '@mastra/core/filter';
4
+ import { MastraVector } from '@mastra/core/vector';
5
+ import { OperatorSupport } from '@mastra/core/filter';
6
+ import { QueryResult } from '@mastra/core/vector';
7
+
8
+ declare class CloudflareVector extends MastraVector {
9
+ client: Cloudflare;
10
+ accountId: string;
11
+ constructor({ accountId, apiToken }: {
12
+ accountId: string;
13
+ apiToken: string;
14
+ });
15
+ upsert(indexName: string, vectors: number[][], metadata?: Record<string, any>[], ids?: string[]): Promise<string[]>;
16
+ transformFilter(filter?: Filter): Filter | undefined;
17
+ createIndex(indexName: string, dimension: number, metric?: 'cosine' | 'euclidean' | 'dotproduct'): Promise<void>;
18
+ query(indexName: string, queryVector: number[], topK?: number, filter?: Filter, includeVector?: boolean): Promise<QueryResult[]>;
19
+ listIndexes(): Promise<string[]>;
20
+ describeIndex(indexName: string): Promise<{
21
+ dimension: number;
22
+ count: number;
23
+ metric: "cosine" | "euclidean" | "dotproduct";
24
+ }>;
25
+ deleteIndex(indexName: string): Promise<void>;
26
+ createMetadataIndex(indexName: string, propertyName: string, indexType: 'string' | 'number' | 'boolean'): Promise<void>;
27
+ deleteMetadataIndex(indexName: string, propertyName: string): Promise<void>;
28
+ listMetadataIndexes(indexName: string): Promise<Cloudflare.Vectorize.Indexes.MetadataIndex.MetadataIndexListResponse.MetadataIndex[]>;
29
+ }
30
+ export { CloudflareVector }
31
+ export { CloudflareVector as CloudflareVector_alias_1 }
32
+
33
+ export declare class VectorizeFilterTranslator extends BaseFilterTranslator {
34
+ protected getSupportedOperators(): OperatorSupport;
35
+ translate(filter?: Filter): Filter | undefined;
36
+ private translateNode;
37
+ }
38
+
39
+ export { }
@@ -0,0 +1 @@
1
+ export { CloudflareVector } from './_tsup-dts-rollup.js';
package/dist/index.js ADDED
@@ -0,0 +1,165 @@
1
+ import { BaseFilterTranslator } from '@mastra/core/filter';
2
+ import { MastraVector } from '@mastra/core/vector';
3
+ import Cloudflare from 'cloudflare';
4
+
5
+ // src/vector/index.ts
6
+ var VectorizeFilterTranslator = class extends BaseFilterTranslator {
7
+ getSupportedOperators() {
8
+ return {
9
+ ...BaseFilterTranslator.DEFAULT_OPERATORS,
10
+ logical: [],
11
+ array: ["$in", "$nin"],
12
+ element: [],
13
+ regex: [],
14
+ custom: []
15
+ };
16
+ }
17
+ translate(filter) {
18
+ if (this.isEmpty(filter)) return filter;
19
+ this.validateFilter(filter);
20
+ return this.translateNode(filter);
21
+ }
22
+ translateNode(node, currentPath = "") {
23
+ if (this.isRegex(node)) {
24
+ throw new Error("Regex is not supported in Vectorize");
25
+ }
26
+ if (this.isPrimitive(node)) return { $eq: this.normalizeComparisonValue(node) };
27
+ if (Array.isArray(node)) return { $in: this.normalizeArrayValues(node) };
28
+ const entries = Object.entries(node);
29
+ const firstEntry = entries[0];
30
+ if (entries.length === 1 && firstEntry && this.isOperator(firstEntry[0])) {
31
+ const [operator, value] = firstEntry;
32
+ return { [operator]: this.normalizeComparisonValue(value) };
33
+ }
34
+ const result = {};
35
+ for (const [key, value] of entries) {
36
+ const newPath = currentPath ? `${currentPath}.${key}` : key;
37
+ if (this.isOperator(key)) {
38
+ result[key] = this.normalizeComparisonValue(value);
39
+ continue;
40
+ }
41
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
42
+ if (Object.keys(value).length === 0) {
43
+ result[newPath] = this.translateNode(value);
44
+ continue;
45
+ }
46
+ const hasOperators = Object.keys(value).some((k) => this.isOperator(k));
47
+ if (hasOperators) {
48
+ result[newPath] = this.translateNode(value);
49
+ } else {
50
+ Object.assign(result, this.translateNode(value, newPath));
51
+ }
52
+ } else {
53
+ result[newPath] = this.translateNode(value);
54
+ }
55
+ }
56
+ return result;
57
+ }
58
+ };
59
+
60
+ // src/vector/index.ts
61
+ var CloudflareVector = class extends MastraVector {
62
+ client;
63
+ accountId;
64
+ constructor({ accountId, apiToken }) {
65
+ super();
66
+ this.accountId = accountId;
67
+ this.client = new Cloudflare({
68
+ apiKey: apiToken
69
+ });
70
+ }
71
+ async upsert(indexName, vectors, metadata, ids) {
72
+ const generatedIds = ids || vectors.map(() => crypto.randomUUID());
73
+ const ndjson = vectors.map((vector, index) => ({
74
+ id: generatedIds[index],
75
+ values: vector,
76
+ metadata: metadata?.[index]
77
+ })).map((record) => JSON.stringify(record)).join("\n");
78
+ await this.client.vectorize.indexes.upsert(indexName, {
79
+ account_id: this.accountId,
80
+ body: ndjson
81
+ });
82
+ return generatedIds;
83
+ }
84
+ transformFilter(filter) {
85
+ const translator = new VectorizeFilterTranslator();
86
+ const translatedFilter = translator.translate(filter);
87
+ return translatedFilter;
88
+ }
89
+ async createIndex(indexName, dimension, metric = "cosine") {
90
+ await this.client.vectorize.indexes.create({
91
+ account_id: this.accountId,
92
+ config: {
93
+ dimensions: dimension,
94
+ metric: metric === "dotproduct" ? "dot-product" : metric
95
+ },
96
+ name: indexName
97
+ });
98
+ }
99
+ async query(indexName, queryVector, topK = 10, filter, includeVector = false) {
100
+ const translatedFilter = this.transformFilter(filter);
101
+ const response = await this.client.vectorize.indexes.query(indexName, {
102
+ account_id: this.accountId,
103
+ vector: queryVector,
104
+ returnValues: includeVector,
105
+ returnMetadata: "all",
106
+ topK,
107
+ filter: translatedFilter
108
+ });
109
+ return response?.matches?.map((match) => {
110
+ return {
111
+ id: match.id,
112
+ metadata: match.metadata,
113
+ score: match.score,
114
+ vector: match.values
115
+ };
116
+ }) || [];
117
+ }
118
+ async listIndexes() {
119
+ const res = await this.client.vectorize.indexes.list({
120
+ account_id: this.accountId
121
+ });
122
+ return res?.result?.map((index) => index.name) || [];
123
+ }
124
+ async describeIndex(indexName) {
125
+ const index = await this.client.vectorize.indexes.get(indexName, {
126
+ account_id: this.accountId
127
+ });
128
+ const described = await this.client.vectorize.indexes.info(indexName, {
129
+ account_id: this.accountId
130
+ });
131
+ return {
132
+ dimension: described?.dimensions,
133
+ // Since vector_count is not available in the response,
134
+ // we might need a separate API call to get the count if needed
135
+ count: described?.vectorCount || 0,
136
+ metric: index?.config?.metric
137
+ };
138
+ }
139
+ async deleteIndex(indexName) {
140
+ await this.client.vectorize.indexes.delete(indexName, {
141
+ account_id: this.accountId
142
+ });
143
+ }
144
+ async createMetadataIndex(indexName, propertyName, indexType) {
145
+ await this.client.vectorize.indexes.metadataIndex.create(indexName, {
146
+ account_id: this.accountId,
147
+ propertyName,
148
+ indexType
149
+ });
150
+ }
151
+ async deleteMetadataIndex(indexName, propertyName) {
152
+ await this.client.vectorize.indexes.metadataIndex.delete(indexName, {
153
+ account_id: this.accountId,
154
+ propertyName
155
+ });
156
+ }
157
+ async listMetadataIndexes(indexName) {
158
+ const res = await this.client.vectorize.indexes.metadataIndex.list(indexName, {
159
+ account_id: this.accountId
160
+ });
161
+ return res?.metadataIndexes ?? [];
162
+ }
163
+ };
164
+
165
+ export { CloudflareVector };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/vectorize",
3
- "version": "0.1.0-alpha.37",
3
+ "version": "0.1.0-alpha.38",
4
4
  "description": "Cloudflare Vectorize store provider for Mastra",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "cloudflare": "^4.0.0",
19
- "@mastra/core": "^0.2.0-alpha.101"
19
+ "@mastra/core": "^0.2.0-alpha.102"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@microsoft/api-extractor": "^7.49.2",