@mastra/vectorize 0.1.8-alpha.9 → 0.1.9-alpha.0

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