@mastra/vectorize 0.1.0-alpha.29

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.
@@ -0,0 +1,150 @@
1
+ import { Filter } from '@mastra/core/filter';
2
+ import { MastraVector, QueryResult } from '@mastra/core/vector';
3
+ import Cloudflare from 'cloudflare';
4
+
5
+ import { VectorizeFilterTranslator } from './filter';
6
+
7
+ export class CloudflareVector extends MastraVector {
8
+ client: Cloudflare;
9
+ accountId: string;
10
+
11
+ constructor({ accountId, apiToken }: { accountId: string; apiToken: string }) {
12
+ super();
13
+ this.accountId = accountId;
14
+
15
+ this.client = new Cloudflare({
16
+ apiKey: apiToken,
17
+ });
18
+ }
19
+
20
+ async upsert(
21
+ indexName: string,
22
+ vectors: number[][],
23
+ metadata?: Record<string, any>[],
24
+ ids?: string[],
25
+ ): Promise<string[]> {
26
+ const generatedIds = ids || vectors.map(() => crypto.randomUUID());
27
+
28
+ // Create NDJSON string - each line is a JSON object
29
+ const ndjson = vectors
30
+ .map((vector, index) => ({
31
+ id: generatedIds[index]!,
32
+ values: vector,
33
+ metadata: metadata?.[index],
34
+ }))
35
+ .map(record => JSON.stringify(record))
36
+ .join('\n');
37
+
38
+ await this.client.vectorize.indexes.upsert(indexName, {
39
+ account_id: this.accountId,
40
+ body: ndjson as any,
41
+ });
42
+
43
+ return generatedIds;
44
+ }
45
+
46
+ transformFilter(filter?: Filter) {
47
+ const translator = new VectorizeFilterTranslator();
48
+ const translatedFilter = translator.translate(filter);
49
+ return translatedFilter;
50
+ }
51
+
52
+ async createIndex(
53
+ indexName: string,
54
+ dimension: number,
55
+ metric: 'cosine' | 'euclidean' | 'dotproduct' = 'cosine',
56
+ ): Promise<void> {
57
+ await this.client.vectorize.indexes.create({
58
+ account_id: this.accountId,
59
+ config: {
60
+ dimensions: dimension,
61
+ metric: metric === 'dotproduct' ? 'dot-product' : metric,
62
+ },
63
+ name: indexName,
64
+ });
65
+ }
66
+
67
+ async query(
68
+ indexName: string,
69
+ queryVector: number[],
70
+ topK: number = 10,
71
+ filter?: Filter,
72
+ includeVector: boolean = false,
73
+ ): Promise<QueryResult[]> {
74
+ const translatedFilter = this.transformFilter(filter);
75
+ const response = await this.client.vectorize.indexes.query(indexName, {
76
+ account_id: this.accountId,
77
+ vector: queryVector,
78
+ returnValues: includeVector,
79
+ returnMetadata: 'all',
80
+ topK,
81
+ filter: translatedFilter,
82
+ });
83
+
84
+ return (
85
+ response?.matches?.map((match: any) => {
86
+ return {
87
+ id: match.id,
88
+ metadata: match.metadata,
89
+ score: match.score,
90
+ vector: match.values,
91
+ };
92
+ }) || []
93
+ );
94
+ }
95
+
96
+ async listIndexes(): Promise<string[]> {
97
+ const res = await this.client.vectorize.indexes.list({
98
+ account_id: this.accountId,
99
+ });
100
+
101
+ return res?.result?.map(index => index.name!) || [];
102
+ }
103
+
104
+ async describeIndex(indexName: string) {
105
+ const index = await this.client.vectorize.indexes.get(indexName, {
106
+ account_id: this.accountId,
107
+ });
108
+
109
+ const described = await this.client.vectorize.indexes.info(indexName, {
110
+ account_id: this.accountId,
111
+ });
112
+
113
+ return {
114
+ dimension: described?.dimensions!,
115
+ // Since vector_count is not available in the response,
116
+ // we might need a separate API call to get the count if needed
117
+ count: described?.vectorCount || 0,
118
+ metric: index?.config?.metric as 'cosine' | 'euclidean' | 'dotproduct',
119
+ };
120
+ }
121
+
122
+ async deleteIndex(indexName: string): Promise<void> {
123
+ await this.client.vectorize.indexes.delete(indexName, {
124
+ account_id: this.accountId,
125
+ });
126
+ }
127
+
128
+ async createMetadataIndex(indexName: string, propertyName: string, indexType: 'string' | 'number' | 'boolean') {
129
+ await this.client.vectorize.indexes.metadataIndex.create(indexName, {
130
+ account_id: this.accountId,
131
+ propertyName,
132
+ indexType,
133
+ });
134
+ }
135
+
136
+ async deleteMetadataIndex(indexName: string, propertyName: string) {
137
+ await this.client.vectorize.indexes.metadataIndex.delete(indexName, {
138
+ account_id: this.accountId,
139
+ propertyName,
140
+ });
141
+ }
142
+
143
+ async listMetadataIndexes(indexName: string) {
144
+ const res = await this.client.vectorize.indexes.metadataIndex.list(indexName, {
145
+ account_id: this.accountId,
146
+ });
147
+
148
+ return res?.metadataIndexes ?? [];
149
+ }
150
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "moduleResolution": "bundler",
5
+ "outDir": "./dist",
6
+ "rootDir": "./src"
7
+ },
8
+ "include": ["src/**/*"],
9
+ "exclude": ["node_modules", "**/*.test.ts"]
10
+ }
@@ -0,0 +1,11 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ environment: 'node',
6
+ include: ['src/**/*.test.ts'],
7
+ coverage: {
8
+ reporter: ['text', 'json', 'html'],
9
+ },
10
+ },
11
+ });