@mastra/upstash 0.1.0-alpha.2

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,101 @@
1
+ import type { Filter } from '@mastra/core/filter';
2
+ import { MastraVector } from '@mastra/core/vector';
3
+ import type { QueryResult } from '@mastra/core/vector';
4
+ import { Index } from '@upstash/vector';
5
+
6
+ import { UpstashFilterTranslator } from './filter';
7
+
8
+ export class UpstashVector extends MastraVector {
9
+ private client: Index;
10
+
11
+ constructor({ url, token }: { url: string; token: string }) {
12
+ super();
13
+ this.client = new Index({
14
+ url,
15
+ token,
16
+ });
17
+ }
18
+
19
+ async upsert(
20
+ indexName: string,
21
+ vectors: number[][],
22
+ metadata?: Record<string, any>[],
23
+ ids?: string[],
24
+ ): Promise<string[]> {
25
+ const generatedIds = ids || vectors.map(() => crypto.randomUUID());
26
+
27
+ const points = vectors.map((vector, index) => ({
28
+ id: generatedIds[index]!,
29
+ vector,
30
+ metadata: metadata?.[index],
31
+ }));
32
+
33
+ await this.client.upsert(points, {
34
+ namespace: indexName,
35
+ });
36
+ return generatedIds;
37
+ }
38
+
39
+ transformFilter(filter?: Filter) {
40
+ const translator = new UpstashFilterTranslator();
41
+ return translator.translate(filter);
42
+ }
43
+
44
+ async createIndex(
45
+ _indexName: string,
46
+ _dimension: number,
47
+ _metric: 'cosine' | 'euclidean' | 'dotproduct' = 'cosine',
48
+ ): Promise<void> {
49
+ console.log('No need to call createIndex for Upstash');
50
+ }
51
+
52
+ async query(
53
+ indexName: string,
54
+ queryVector: number[],
55
+ topK: number = 10,
56
+ filter?: Filter,
57
+ includeVector: boolean = false,
58
+ ): Promise<QueryResult[]> {
59
+ const ns = this.client.namespace(indexName);
60
+
61
+ const filterString = this.transformFilter(filter);
62
+ const results = await ns.query({
63
+ topK,
64
+ vector: queryVector,
65
+ includeVectors: includeVector,
66
+ includeMetadata: true,
67
+ ...(filterString ? { filter: filterString } : {}),
68
+ });
69
+
70
+ // Map the results to our expected format
71
+ return (results || []).map(result => ({
72
+ id: `${result.id}`,
73
+ score: result.score,
74
+ metadata: result.metadata,
75
+ ...(includeVector && { vector: result.vector || [] }),
76
+ }));
77
+ }
78
+
79
+ async listIndexes(): Promise<string[]> {
80
+ const indexes = await this.client.listNamespaces();
81
+ return indexes.filter(Boolean);
82
+ }
83
+
84
+ async describeIndex(indexName: string) {
85
+ const info = await this.client.info();
86
+
87
+ return {
88
+ dimension: info.dimension,
89
+ count: info.namespaces?.[indexName]?.vectorCount || 0,
90
+ metric: info?.similarityFunction?.toLowerCase() as 'cosine' | 'euclidean' | 'dotproduct',
91
+ };
92
+ }
93
+
94
+ async deleteIndex(indexName: string): Promise<void> {
95
+ try {
96
+ await this.client.deleteNamespace(indexName);
97
+ } catch (error) {
98
+ console.error('Failed to delete namespace:', error);
99
+ }
100
+ }
101
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "../../tsconfig.node.json",
3
+ "include": ["src/**/*"],
4
+ "exclude": ["node_modules", "**/*.test.ts"]
5
+ }
@@ -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
+ });