@mastra/pinecone 0.0.0-commonjs-20250227130920

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,136 @@
1
+ import type { Filter } from '@mastra/core/filter';
2
+ import { MastraVector } from '@mastra/core/vector';
3
+ import type { QueryResult, IndexStats } from '@mastra/core/vector';
4
+ import { Pinecone } from '@pinecone-database/pinecone';
5
+
6
+ import { PineconeFilterTranslator } from './filter';
7
+
8
+ export class PineconeVector extends MastraVector {
9
+ private client: Pinecone;
10
+
11
+ constructor(apiKey: string, environment?: string) {
12
+ super();
13
+
14
+ const opts: { apiKey: string; controllerHostUrl?: string } = { apiKey };
15
+
16
+ if (environment) {
17
+ opts['controllerHostUrl'] = environment;
18
+ }
19
+
20
+ const baseClient = new Pinecone(opts);
21
+ const telemetry = this.__getTelemetry();
22
+ this.client =
23
+ telemetry?.traceClass(baseClient, {
24
+ spanNamePrefix: 'pinecone-vector',
25
+ attributes: {
26
+ 'vector.type': 'pinecone',
27
+ },
28
+ }) ?? baseClient;
29
+ }
30
+
31
+ async createIndex(
32
+ indexName: string,
33
+ dimension: number,
34
+ metric: 'cosine' | 'euclidean' | 'dotproduct' = 'cosine',
35
+ ): Promise<void> {
36
+ if (!Number.isInteger(dimension) || dimension <= 0) {
37
+ throw new Error('Dimension must be a positive integer');
38
+ }
39
+ await this.client.createIndex({
40
+ name: indexName,
41
+ dimension: dimension,
42
+ metric: metric,
43
+ spec: {
44
+ serverless: {
45
+ cloud: 'aws',
46
+ region: 'us-east-1',
47
+ },
48
+ },
49
+ });
50
+ }
51
+
52
+ async upsert(
53
+ indexName: string,
54
+ vectors: number[][],
55
+ metadata?: Record<string, any>[],
56
+ ids?: string[],
57
+ ): Promise<string[]> {
58
+ const index = this.client.Index(indexName);
59
+
60
+ // Generate IDs if not provided
61
+ const vectorIds = ids || vectors.map(() => crypto.randomUUID());
62
+
63
+ const records = vectors.map((vector, i) => ({
64
+ id: vectorIds[i]!,
65
+ values: vector,
66
+ metadata: metadata?.[i] || {},
67
+ }));
68
+
69
+ // Pinecone has a limit of 100 vectors per upsert request
70
+ const batchSize = 100;
71
+ for (let i = 0; i < records.length; i += batchSize) {
72
+ const batch = records.slice(i, i + batchSize);
73
+ await index.upsert(batch);
74
+ }
75
+
76
+ return vectorIds;
77
+ }
78
+
79
+ transformFilter(filter?: Filter) {
80
+ const pineconeFilter = new PineconeFilterTranslator();
81
+ const translatedFilter = pineconeFilter.translate(filter);
82
+ return translatedFilter;
83
+ }
84
+
85
+ async query(
86
+ indexName: string,
87
+ queryVector: number[],
88
+ topK: number = 10,
89
+ filter?: Filter,
90
+ includeVector: boolean = false,
91
+ ): Promise<QueryResult[]> {
92
+ const index = this.client.Index(indexName);
93
+
94
+ const translatedFilter = this.transformFilter(filter);
95
+
96
+ const results = await index.query({
97
+ vector: queryVector,
98
+ topK,
99
+ filter: translatedFilter,
100
+ includeMetadata: true,
101
+ includeValues: includeVector,
102
+ });
103
+
104
+ return results.matches.map(match => ({
105
+ id: match.id,
106
+ score: match.score || 0,
107
+ metadata: match.metadata as Record<string, any>,
108
+ ...(includeVector && { vector: match.values || [] }),
109
+ }));
110
+ }
111
+
112
+ async listIndexes(): Promise<string[]> {
113
+ const indexesResult = await this.client.listIndexes();
114
+ return indexesResult?.indexes?.map(index => index.name) || [];
115
+ }
116
+
117
+ async describeIndex(indexName: string): Promise<IndexStats> {
118
+ const index = this.client.Index(indexName);
119
+ const stats = await index.describeIndexStats();
120
+ const description = await this.client.describeIndex(indexName);
121
+
122
+ return {
123
+ dimension: description.dimension,
124
+ count: stats.totalRecordCount || 0,
125
+ metric: description.metric as 'cosine' | 'euclidean' | 'dotproduct',
126
+ };
127
+ }
128
+
129
+ async deleteIndex(indexName: string): Promise<void> {
130
+ try {
131
+ await this.client.deleteIndex(indexName);
132
+ } catch (error: any) {
133
+ throw new Error(`Failed to delete Pinecone index: ${error.message}`);
134
+ }
135
+ }
136
+ }
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
+ });