@mastra/pinecone 0.0.0-a2a-20250421213654
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 +1426 -0
- package/LICENSE.md +7 -0
- package/README.md +72 -0
- package/dist/_tsup-dts-rollup.d.cts +60 -0
- package/dist/_tsup-dts-rollup.d.ts +60 -0
- package/dist/index.cjs +214 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +212 -0
- package/eslint.config.js +6 -0
- package/package.json +42 -0
- package/src/index.ts +1 -0
- package/src/vector/filter.test.ts +457 -0
- package/src/vector/filter.ts +102 -0
- package/src/vector/index.test.ts +1548 -0
- package/src/vector/index.ts +213 -0
- package/tsconfig.json +5 -0
- package/vitest.config.ts +11 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2025 Mastra AI, Inc.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# @mastra/pinecone
|
|
2
|
+
|
|
3
|
+
Vector store implementation for Pinecone, using the official @pinecone-database/pinecone SDK with added telemetry support.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @mastra/pinecone
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { PineconeVector } from '@mastra/pinecone';
|
|
15
|
+
|
|
16
|
+
const vectorStore = new PineconeVector(
|
|
17
|
+
'your-api-key',
|
|
18
|
+
'optional-environment-url'
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
// Create a new index
|
|
22
|
+
await vectorStore.createIndex({ indexName: 'my-index', dimension: 1536, metric: 'cosine' });
|
|
23
|
+
|
|
24
|
+
// Add vectors
|
|
25
|
+
const vectors = [[0.1, 0.2, ...], [0.3, 0.4, ...]];
|
|
26
|
+
const metadata = [{ text: 'doc1' }, { text: 'doc2' }];
|
|
27
|
+
const ids = await vectorStore.upsert({ indexName: 'my-index', vectors, metadata });
|
|
28
|
+
|
|
29
|
+
// Query vectors
|
|
30
|
+
const results = await vectorStore.query({
|
|
31
|
+
indexName: 'my-index',
|
|
32
|
+
queryVector: [0.1, 0.2, ...],
|
|
33
|
+
topK: 10, // topK
|
|
34
|
+
filter: { text: { $eq: 'doc1' } }, // optional filter
|
|
35
|
+
includeVector: false, // includeValues
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Configuration
|
|
40
|
+
|
|
41
|
+
Required:
|
|
42
|
+
|
|
43
|
+
- `apiKey`: Your Pinecone API key
|
|
44
|
+
|
|
45
|
+
Optional:
|
|
46
|
+
|
|
47
|
+
- `environment`: Your Pinecone environment URL (controller host URL)
|
|
48
|
+
|
|
49
|
+
## Features
|
|
50
|
+
|
|
51
|
+
- Serverless deployment on AWS (us-east-1)
|
|
52
|
+
- Vector similarity search with cosine, euclidean, and dot product metrics
|
|
53
|
+
- Automatic batching for large upserts (100 vectors per request)
|
|
54
|
+
- Built-in telemetry support
|
|
55
|
+
- Metadata filtering
|
|
56
|
+
- Optional vector inclusion in query results
|
|
57
|
+
- Automatic UUID generation for vectors
|
|
58
|
+
- Built on top of @pinecone-database/pinecone SDK
|
|
59
|
+
|
|
60
|
+
## Methods
|
|
61
|
+
|
|
62
|
+
- `createIndex({indexName, dimension, metric?})`: Create a new index
|
|
63
|
+
- `upsert({indexName, vectors, metadata?, ids?})`: Add or update vectors
|
|
64
|
+
- `query({indexName, queryVector, topK?, filter?, includeVector?})`: Search for similar vectors
|
|
65
|
+
- `listIndexes()`: List all indexes
|
|
66
|
+
- `describeIndex(indexName)`: Get index statistics
|
|
67
|
+
- `deleteIndex(indexName)`: Delete an index
|
|
68
|
+
|
|
69
|
+
## Related Links
|
|
70
|
+
|
|
71
|
+
- [Pinecone Documentation](https://docs.pinecone.io/)
|
|
72
|
+
- [Pinecone Node.js SDK](https://github.com/pinecone-io/pinecone-ts-client)
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
2
|
+
import type { CreateIndexParams } from '@mastra/core/vector';
|
|
3
|
+
import type { IndexStats } from '@mastra/core/vector';
|
|
4
|
+
import type { IndexStatsDescription } from '@pinecone-database/pinecone';
|
|
5
|
+
import { MastraVector } from '@mastra/core/vector';
|
|
6
|
+
import type { OperatorSupport } from '@mastra/core/vector/filter';
|
|
7
|
+
import type { ParamsToArgs } from '@mastra/core/vector';
|
|
8
|
+
import type { QueryResult } from '@mastra/core/vector';
|
|
9
|
+
import type { QueryVectorArgs } from '@mastra/core/vector';
|
|
10
|
+
import type { QueryVectorParams } from '@mastra/core/vector';
|
|
11
|
+
import type { RecordSparseValues } from '@pinecone-database/pinecone';
|
|
12
|
+
import type { UpsertVectorArgs } from '@mastra/core/vector';
|
|
13
|
+
import type { UpsertVectorParams } from '@mastra/core/vector';
|
|
14
|
+
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
15
|
+
|
|
16
|
+
export declare class PineconeFilterTranslator extends BaseFilterTranslator {
|
|
17
|
+
protected getSupportedOperators(): OperatorSupport;
|
|
18
|
+
translate(filter?: VectorFilter): VectorFilter;
|
|
19
|
+
private translateNode;
|
|
20
|
+
private translateOperator;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
declare interface PineconeIndexStats extends IndexStats {
|
|
24
|
+
namespaces?: IndexStatsDescription['namespaces'];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
declare type PineconeQueryVectorArgs = [...QueryVectorArgs, string?, RecordSparseValues?];
|
|
28
|
+
|
|
29
|
+
declare interface PineconeQueryVectorParams extends QueryVectorParams {
|
|
30
|
+
namespace?: string;
|
|
31
|
+
sparseVector?: RecordSparseValues;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
declare type PineconeUpsertVectorArgs = [...UpsertVectorArgs, string?, RecordSparseValues[]?];
|
|
35
|
+
|
|
36
|
+
declare interface PineconeUpsertVectorParams extends UpsertVectorParams {
|
|
37
|
+
namespace?: string;
|
|
38
|
+
sparseVectors?: RecordSparseValues[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
declare class PineconeVector extends MastraVector {
|
|
42
|
+
private client;
|
|
43
|
+
constructor(apiKey: string, environment?: string);
|
|
44
|
+
createIndex(...args: ParamsToArgs<CreateIndexParams>): Promise<void>;
|
|
45
|
+
upsert(...args: ParamsToArgs<PineconeUpsertVectorParams> | PineconeUpsertVectorArgs): Promise<string[]>;
|
|
46
|
+
transformFilter(filter?: VectorFilter): VectorFilter;
|
|
47
|
+
query(...args: ParamsToArgs<PineconeQueryVectorParams> | PineconeQueryVectorArgs): Promise<QueryResult[]>;
|
|
48
|
+
listIndexes(): Promise<string[]>;
|
|
49
|
+
describeIndex(indexName: string): Promise<PineconeIndexStats>;
|
|
50
|
+
deleteIndex(indexName: string): Promise<void>;
|
|
51
|
+
updateIndexById(indexName: string, id: string, update: {
|
|
52
|
+
vector?: number[];
|
|
53
|
+
metadata?: Record<string, any>;
|
|
54
|
+
}, namespace?: string): Promise<void>;
|
|
55
|
+
deleteIndexById(indexName: string, id: string, namespace?: string): Promise<void>;
|
|
56
|
+
}
|
|
57
|
+
export { PineconeVector }
|
|
58
|
+
export { PineconeVector as PineconeVector_alias_1 }
|
|
59
|
+
|
|
60
|
+
export { }
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
2
|
+
import type { CreateIndexParams } from '@mastra/core/vector';
|
|
3
|
+
import type { IndexStats } from '@mastra/core/vector';
|
|
4
|
+
import type { IndexStatsDescription } from '@pinecone-database/pinecone';
|
|
5
|
+
import { MastraVector } from '@mastra/core/vector';
|
|
6
|
+
import type { OperatorSupport } from '@mastra/core/vector/filter';
|
|
7
|
+
import type { ParamsToArgs } from '@mastra/core/vector';
|
|
8
|
+
import type { QueryResult } from '@mastra/core/vector';
|
|
9
|
+
import type { QueryVectorArgs } from '@mastra/core/vector';
|
|
10
|
+
import type { QueryVectorParams } from '@mastra/core/vector';
|
|
11
|
+
import type { RecordSparseValues } from '@pinecone-database/pinecone';
|
|
12
|
+
import type { UpsertVectorArgs } from '@mastra/core/vector';
|
|
13
|
+
import type { UpsertVectorParams } from '@mastra/core/vector';
|
|
14
|
+
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
15
|
+
|
|
16
|
+
export declare class PineconeFilterTranslator extends BaseFilterTranslator {
|
|
17
|
+
protected getSupportedOperators(): OperatorSupport;
|
|
18
|
+
translate(filter?: VectorFilter): VectorFilter;
|
|
19
|
+
private translateNode;
|
|
20
|
+
private translateOperator;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
declare interface PineconeIndexStats extends IndexStats {
|
|
24
|
+
namespaces?: IndexStatsDescription['namespaces'];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
declare type PineconeQueryVectorArgs = [...QueryVectorArgs, string?, RecordSparseValues?];
|
|
28
|
+
|
|
29
|
+
declare interface PineconeQueryVectorParams extends QueryVectorParams {
|
|
30
|
+
namespace?: string;
|
|
31
|
+
sparseVector?: RecordSparseValues;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
declare type PineconeUpsertVectorArgs = [...UpsertVectorArgs, string?, RecordSparseValues[]?];
|
|
35
|
+
|
|
36
|
+
declare interface PineconeUpsertVectorParams extends UpsertVectorParams {
|
|
37
|
+
namespace?: string;
|
|
38
|
+
sparseVectors?: RecordSparseValues[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
declare class PineconeVector extends MastraVector {
|
|
42
|
+
private client;
|
|
43
|
+
constructor(apiKey: string, environment?: string);
|
|
44
|
+
createIndex(...args: ParamsToArgs<CreateIndexParams>): Promise<void>;
|
|
45
|
+
upsert(...args: ParamsToArgs<PineconeUpsertVectorParams> | PineconeUpsertVectorArgs): Promise<string[]>;
|
|
46
|
+
transformFilter(filter?: VectorFilter): VectorFilter;
|
|
47
|
+
query(...args: ParamsToArgs<PineconeQueryVectorParams> | PineconeQueryVectorArgs): Promise<QueryResult[]>;
|
|
48
|
+
listIndexes(): Promise<string[]>;
|
|
49
|
+
describeIndex(indexName: string): Promise<PineconeIndexStats>;
|
|
50
|
+
deleteIndex(indexName: string): Promise<void>;
|
|
51
|
+
updateIndexById(indexName: string, id: string, update: {
|
|
52
|
+
vector?: number[];
|
|
53
|
+
metadata?: Record<string, any>;
|
|
54
|
+
}, namespace?: string): Promise<void>;
|
|
55
|
+
deleteIndexById(indexName: string, id: string, namespace?: string): Promise<void>;
|
|
56
|
+
}
|
|
57
|
+
export { PineconeVector }
|
|
58
|
+
export { PineconeVector as PineconeVector_alias_1 }
|
|
59
|
+
|
|
60
|
+
export { }
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var vector = require('@mastra/core/vector');
|
|
4
|
+
var pinecone = require('@pinecone-database/pinecone');
|
|
5
|
+
var filter = require('@mastra/core/vector/filter');
|
|
6
|
+
|
|
7
|
+
// src/vector/index.ts
|
|
8
|
+
var PineconeFilterTranslator = class extends filter.BaseFilterTranslator {
|
|
9
|
+
getSupportedOperators() {
|
|
10
|
+
return {
|
|
11
|
+
...filter.BaseFilterTranslator.DEFAULT_OPERATORS,
|
|
12
|
+
logical: ["$and", "$or"],
|
|
13
|
+
array: ["$in", "$all", "$nin"],
|
|
14
|
+
element: ["$exists"],
|
|
15
|
+
regex: [],
|
|
16
|
+
custom: []
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
translate(filter) {
|
|
20
|
+
if (this.isEmpty(filter)) return filter;
|
|
21
|
+
this.validateFilter(filter);
|
|
22
|
+
return this.translateNode(filter);
|
|
23
|
+
}
|
|
24
|
+
translateNode(node, currentPath = "") {
|
|
25
|
+
if (this.isRegex(node)) {
|
|
26
|
+
throw new Error("Regex is not supported in Pinecone");
|
|
27
|
+
}
|
|
28
|
+
if (this.isPrimitive(node)) return this.normalizeComparisonValue(node);
|
|
29
|
+
if (Array.isArray(node)) return { $in: this.normalizeArrayValues(node) };
|
|
30
|
+
const entries = Object.entries(node);
|
|
31
|
+
const firstEntry = entries[0];
|
|
32
|
+
if (entries.length === 1 && firstEntry && this.isOperator(firstEntry[0])) {
|
|
33
|
+
const [operator, value] = firstEntry;
|
|
34
|
+
const translated = this.translateOperator(operator, value, currentPath);
|
|
35
|
+
return this.isLogicalOperator(operator) ? { [operator]: translated } : translated;
|
|
36
|
+
}
|
|
37
|
+
const result = {};
|
|
38
|
+
for (const [key, value] of entries) {
|
|
39
|
+
const newPath = currentPath ? `${currentPath}.${key}` : key;
|
|
40
|
+
if (this.isOperator(key)) {
|
|
41
|
+
result[key] = this.translateOperator(key, value, currentPath);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
45
|
+
if (Object.keys(value).length === 1 && "$all" in value) {
|
|
46
|
+
const translated = this.translateNode(value, key);
|
|
47
|
+
if (translated.$and) {
|
|
48
|
+
return translated;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (Object.keys(value).length === 0) {
|
|
52
|
+
result[newPath] = this.translateNode(value);
|
|
53
|
+
} else {
|
|
54
|
+
const hasOperators = Object.keys(value).some((k) => this.isOperator(k));
|
|
55
|
+
if (hasOperators) {
|
|
56
|
+
const normalizedValue = {};
|
|
57
|
+
for (const [op, opValue] of Object.entries(value)) {
|
|
58
|
+
normalizedValue[op] = this.isOperator(op) ? this.translateOperator(op, opValue) : opValue;
|
|
59
|
+
}
|
|
60
|
+
result[newPath] = normalizedValue;
|
|
61
|
+
} else {
|
|
62
|
+
Object.assign(result, this.translateNode(value, newPath));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
result[newPath] = this.translateNode(value);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
translateOperator(operator, value, currentPath = "") {
|
|
72
|
+
if (operator === "$all") {
|
|
73
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
74
|
+
throw new Error("A non-empty array is required for the $all operator");
|
|
75
|
+
}
|
|
76
|
+
return this.simulateAllOperator(currentPath, value);
|
|
77
|
+
}
|
|
78
|
+
if (this.isLogicalOperator(operator)) {
|
|
79
|
+
return Array.isArray(value) ? value.map((item) => this.translateNode(item)) : this.translateNode(value);
|
|
80
|
+
}
|
|
81
|
+
return this.normalizeComparisonValue(value);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// src/vector/index.ts
|
|
86
|
+
var PineconeVector = class extends vector.MastraVector {
|
|
87
|
+
client;
|
|
88
|
+
constructor(apiKey, environment) {
|
|
89
|
+
super();
|
|
90
|
+
const opts = { apiKey };
|
|
91
|
+
if (environment) {
|
|
92
|
+
opts["controllerHostUrl"] = environment;
|
|
93
|
+
}
|
|
94
|
+
const baseClient = new pinecone.Pinecone(opts);
|
|
95
|
+
const telemetry = this.__getTelemetry();
|
|
96
|
+
this.client = telemetry?.traceClass(baseClient, {
|
|
97
|
+
spanNamePrefix: "pinecone-vector",
|
|
98
|
+
attributes: {
|
|
99
|
+
"vector.type": "pinecone"
|
|
100
|
+
}
|
|
101
|
+
}) ?? baseClient;
|
|
102
|
+
}
|
|
103
|
+
async createIndex(...args) {
|
|
104
|
+
const params = this.normalizeArgs("createIndex", args);
|
|
105
|
+
const { indexName, dimension, metric = "cosine" } = params;
|
|
106
|
+
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
107
|
+
throw new Error("Dimension must be a positive integer");
|
|
108
|
+
}
|
|
109
|
+
await this.client.createIndex({
|
|
110
|
+
name: indexName,
|
|
111
|
+
dimension,
|
|
112
|
+
metric,
|
|
113
|
+
spec: {
|
|
114
|
+
serverless: {
|
|
115
|
+
cloud: "aws",
|
|
116
|
+
region: "us-east-1"
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
async upsert(...args) {
|
|
122
|
+
const params = this.normalizeArgs("upsert", args, [
|
|
123
|
+
"namespace",
|
|
124
|
+
"sparseVectors"
|
|
125
|
+
]);
|
|
126
|
+
const { indexName, vectors, metadata, ids, namespace, sparseVectors } = params;
|
|
127
|
+
const index = this.client.Index(indexName).namespace(namespace || "");
|
|
128
|
+
const vectorIds = ids || vectors.map(() => crypto.randomUUID());
|
|
129
|
+
const records = vectors.map((vector, i) => ({
|
|
130
|
+
id: vectorIds[i],
|
|
131
|
+
values: vector,
|
|
132
|
+
...sparseVectors?.[i] && { sparseValues: sparseVectors?.[i] },
|
|
133
|
+
metadata: metadata?.[i] || {}
|
|
134
|
+
}));
|
|
135
|
+
const batchSize = 100;
|
|
136
|
+
for (let i = 0; i < records.length; i += batchSize) {
|
|
137
|
+
const batch = records.slice(i, i + batchSize);
|
|
138
|
+
await index.upsert(batch);
|
|
139
|
+
}
|
|
140
|
+
return vectorIds;
|
|
141
|
+
}
|
|
142
|
+
transformFilter(filter) {
|
|
143
|
+
const translator = new PineconeFilterTranslator();
|
|
144
|
+
return translator.translate(filter);
|
|
145
|
+
}
|
|
146
|
+
async query(...args) {
|
|
147
|
+
const params = this.normalizeArgs("query", args, [
|
|
148
|
+
"namespace",
|
|
149
|
+
"sparseVector"
|
|
150
|
+
]);
|
|
151
|
+
const { indexName, queryVector, topK = 10, filter, includeVector = false, namespace, sparseVector } = params;
|
|
152
|
+
const index = this.client.Index(indexName).namespace(namespace || "");
|
|
153
|
+
const translatedFilter = this.transformFilter(filter) ?? void 0;
|
|
154
|
+
const queryParams = {
|
|
155
|
+
vector: queryVector,
|
|
156
|
+
topK,
|
|
157
|
+
includeMetadata: true,
|
|
158
|
+
includeValues: includeVector,
|
|
159
|
+
filter: translatedFilter
|
|
160
|
+
};
|
|
161
|
+
if (sparseVector) {
|
|
162
|
+
queryParams.sparseVector = sparseVector;
|
|
163
|
+
}
|
|
164
|
+
const results = await index.query(queryParams);
|
|
165
|
+
return results.matches.map((match) => ({
|
|
166
|
+
id: match.id,
|
|
167
|
+
score: match.score || 0,
|
|
168
|
+
metadata: match.metadata,
|
|
169
|
+
...includeVector && { vector: match.values || [] }
|
|
170
|
+
}));
|
|
171
|
+
}
|
|
172
|
+
async listIndexes() {
|
|
173
|
+
const indexesResult = await this.client.listIndexes();
|
|
174
|
+
return indexesResult?.indexes?.map((index) => index.name) || [];
|
|
175
|
+
}
|
|
176
|
+
async describeIndex(indexName) {
|
|
177
|
+
const index = this.client.Index(indexName);
|
|
178
|
+
const stats = await index.describeIndexStats();
|
|
179
|
+
const description = await this.client.describeIndex(indexName);
|
|
180
|
+
return {
|
|
181
|
+
dimension: description.dimension,
|
|
182
|
+
count: stats.totalRecordCount || 0,
|
|
183
|
+
metric: description.metric,
|
|
184
|
+
namespaces: stats.namespaces
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
async deleteIndex(indexName) {
|
|
188
|
+
try {
|
|
189
|
+
await this.client.deleteIndex(indexName);
|
|
190
|
+
} catch (error) {
|
|
191
|
+
throw new Error(`Failed to delete Pinecone index: ${error.message}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
async updateIndexById(indexName, id, update, namespace) {
|
|
195
|
+
if (!update.vector && !update.metadata) {
|
|
196
|
+
throw new Error("No updates provided");
|
|
197
|
+
}
|
|
198
|
+
const index = this.client.Index(indexName).namespace(namespace || "");
|
|
199
|
+
const updateObj = { id };
|
|
200
|
+
if (update.vector) {
|
|
201
|
+
updateObj.values = update.vector;
|
|
202
|
+
}
|
|
203
|
+
if (update.metadata) {
|
|
204
|
+
updateObj.metadata = update.metadata;
|
|
205
|
+
}
|
|
206
|
+
await index.update(updateObj);
|
|
207
|
+
}
|
|
208
|
+
async deleteIndexById(indexName, id, namespace) {
|
|
209
|
+
const index = this.client.Index(indexName).namespace(namespace || "");
|
|
210
|
+
await index.deleteOne(id);
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
exports.PineconeVector = PineconeVector;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { PineconeVector } from './_tsup-dts-rollup.cjs';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { PineconeVector } from './_tsup-dts-rollup.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { MastraVector } from '@mastra/core/vector';
|
|
2
|
+
import { Pinecone } from '@pinecone-database/pinecone';
|
|
3
|
+
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
4
|
+
|
|
5
|
+
// src/vector/index.ts
|
|
6
|
+
var PineconeFilterTranslator = class extends BaseFilterTranslator {
|
|
7
|
+
getSupportedOperators() {
|
|
8
|
+
return {
|
|
9
|
+
...BaseFilterTranslator.DEFAULT_OPERATORS,
|
|
10
|
+
logical: ["$and", "$or"],
|
|
11
|
+
array: ["$in", "$all", "$nin"],
|
|
12
|
+
element: ["$exists"],
|
|
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 Pinecone");
|
|
25
|
+
}
|
|
26
|
+
if (this.isPrimitive(node)) return 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
|
+
const translated = this.translateOperator(operator, value, currentPath);
|
|
33
|
+
return this.isLogicalOperator(operator) ? { [operator]: translated } : translated;
|
|
34
|
+
}
|
|
35
|
+
const result = {};
|
|
36
|
+
for (const [key, value] of entries) {
|
|
37
|
+
const newPath = currentPath ? `${currentPath}.${key}` : key;
|
|
38
|
+
if (this.isOperator(key)) {
|
|
39
|
+
result[key] = this.translateOperator(key, value, currentPath);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
43
|
+
if (Object.keys(value).length === 1 && "$all" in value) {
|
|
44
|
+
const translated = this.translateNode(value, key);
|
|
45
|
+
if (translated.$and) {
|
|
46
|
+
return translated;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (Object.keys(value).length === 0) {
|
|
50
|
+
result[newPath] = this.translateNode(value);
|
|
51
|
+
} else {
|
|
52
|
+
const hasOperators = Object.keys(value).some((k) => this.isOperator(k));
|
|
53
|
+
if (hasOperators) {
|
|
54
|
+
const normalizedValue = {};
|
|
55
|
+
for (const [op, opValue] of Object.entries(value)) {
|
|
56
|
+
normalizedValue[op] = this.isOperator(op) ? this.translateOperator(op, opValue) : opValue;
|
|
57
|
+
}
|
|
58
|
+
result[newPath] = normalizedValue;
|
|
59
|
+
} else {
|
|
60
|
+
Object.assign(result, this.translateNode(value, newPath));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
result[newPath] = this.translateNode(value);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
translateOperator(operator, value, currentPath = "") {
|
|
70
|
+
if (operator === "$all") {
|
|
71
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
72
|
+
throw new Error("A non-empty array is required for the $all operator");
|
|
73
|
+
}
|
|
74
|
+
return this.simulateAllOperator(currentPath, value);
|
|
75
|
+
}
|
|
76
|
+
if (this.isLogicalOperator(operator)) {
|
|
77
|
+
return Array.isArray(value) ? value.map((item) => this.translateNode(item)) : this.translateNode(value);
|
|
78
|
+
}
|
|
79
|
+
return this.normalizeComparisonValue(value);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// src/vector/index.ts
|
|
84
|
+
var PineconeVector = class extends MastraVector {
|
|
85
|
+
client;
|
|
86
|
+
constructor(apiKey, environment) {
|
|
87
|
+
super();
|
|
88
|
+
const opts = { apiKey };
|
|
89
|
+
if (environment) {
|
|
90
|
+
opts["controllerHostUrl"] = environment;
|
|
91
|
+
}
|
|
92
|
+
const baseClient = new Pinecone(opts);
|
|
93
|
+
const telemetry = this.__getTelemetry();
|
|
94
|
+
this.client = telemetry?.traceClass(baseClient, {
|
|
95
|
+
spanNamePrefix: "pinecone-vector",
|
|
96
|
+
attributes: {
|
|
97
|
+
"vector.type": "pinecone"
|
|
98
|
+
}
|
|
99
|
+
}) ?? baseClient;
|
|
100
|
+
}
|
|
101
|
+
async createIndex(...args) {
|
|
102
|
+
const params = this.normalizeArgs("createIndex", args);
|
|
103
|
+
const { indexName, dimension, metric = "cosine" } = params;
|
|
104
|
+
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
105
|
+
throw new Error("Dimension must be a positive integer");
|
|
106
|
+
}
|
|
107
|
+
await this.client.createIndex({
|
|
108
|
+
name: indexName,
|
|
109
|
+
dimension,
|
|
110
|
+
metric,
|
|
111
|
+
spec: {
|
|
112
|
+
serverless: {
|
|
113
|
+
cloud: "aws",
|
|
114
|
+
region: "us-east-1"
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
async upsert(...args) {
|
|
120
|
+
const params = this.normalizeArgs("upsert", args, [
|
|
121
|
+
"namespace",
|
|
122
|
+
"sparseVectors"
|
|
123
|
+
]);
|
|
124
|
+
const { indexName, vectors, metadata, ids, namespace, sparseVectors } = params;
|
|
125
|
+
const index = this.client.Index(indexName).namespace(namespace || "");
|
|
126
|
+
const vectorIds = ids || vectors.map(() => crypto.randomUUID());
|
|
127
|
+
const records = vectors.map((vector, i) => ({
|
|
128
|
+
id: vectorIds[i],
|
|
129
|
+
values: vector,
|
|
130
|
+
...sparseVectors?.[i] && { sparseValues: sparseVectors?.[i] },
|
|
131
|
+
metadata: metadata?.[i] || {}
|
|
132
|
+
}));
|
|
133
|
+
const batchSize = 100;
|
|
134
|
+
for (let i = 0; i < records.length; i += batchSize) {
|
|
135
|
+
const batch = records.slice(i, i + batchSize);
|
|
136
|
+
await index.upsert(batch);
|
|
137
|
+
}
|
|
138
|
+
return vectorIds;
|
|
139
|
+
}
|
|
140
|
+
transformFilter(filter) {
|
|
141
|
+
const translator = new PineconeFilterTranslator();
|
|
142
|
+
return translator.translate(filter);
|
|
143
|
+
}
|
|
144
|
+
async query(...args) {
|
|
145
|
+
const params = this.normalizeArgs("query", args, [
|
|
146
|
+
"namespace",
|
|
147
|
+
"sparseVector"
|
|
148
|
+
]);
|
|
149
|
+
const { indexName, queryVector, topK = 10, filter, includeVector = false, namespace, sparseVector } = params;
|
|
150
|
+
const index = this.client.Index(indexName).namespace(namespace || "");
|
|
151
|
+
const translatedFilter = this.transformFilter(filter) ?? void 0;
|
|
152
|
+
const queryParams = {
|
|
153
|
+
vector: queryVector,
|
|
154
|
+
topK,
|
|
155
|
+
includeMetadata: true,
|
|
156
|
+
includeValues: includeVector,
|
|
157
|
+
filter: translatedFilter
|
|
158
|
+
};
|
|
159
|
+
if (sparseVector) {
|
|
160
|
+
queryParams.sparseVector = sparseVector;
|
|
161
|
+
}
|
|
162
|
+
const results = await index.query(queryParams);
|
|
163
|
+
return results.matches.map((match) => ({
|
|
164
|
+
id: match.id,
|
|
165
|
+
score: match.score || 0,
|
|
166
|
+
metadata: match.metadata,
|
|
167
|
+
...includeVector && { vector: match.values || [] }
|
|
168
|
+
}));
|
|
169
|
+
}
|
|
170
|
+
async listIndexes() {
|
|
171
|
+
const indexesResult = await this.client.listIndexes();
|
|
172
|
+
return indexesResult?.indexes?.map((index) => index.name) || [];
|
|
173
|
+
}
|
|
174
|
+
async describeIndex(indexName) {
|
|
175
|
+
const index = this.client.Index(indexName);
|
|
176
|
+
const stats = await index.describeIndexStats();
|
|
177
|
+
const description = await this.client.describeIndex(indexName);
|
|
178
|
+
return {
|
|
179
|
+
dimension: description.dimension,
|
|
180
|
+
count: stats.totalRecordCount || 0,
|
|
181
|
+
metric: description.metric,
|
|
182
|
+
namespaces: stats.namespaces
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
async deleteIndex(indexName) {
|
|
186
|
+
try {
|
|
187
|
+
await this.client.deleteIndex(indexName);
|
|
188
|
+
} catch (error) {
|
|
189
|
+
throw new Error(`Failed to delete Pinecone index: ${error.message}`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
async updateIndexById(indexName, id, update, namespace) {
|
|
193
|
+
if (!update.vector && !update.metadata) {
|
|
194
|
+
throw new Error("No updates provided");
|
|
195
|
+
}
|
|
196
|
+
const index = this.client.Index(indexName).namespace(namespace || "");
|
|
197
|
+
const updateObj = { id };
|
|
198
|
+
if (update.vector) {
|
|
199
|
+
updateObj.values = update.vector;
|
|
200
|
+
}
|
|
201
|
+
if (update.metadata) {
|
|
202
|
+
updateObj.metadata = update.metadata;
|
|
203
|
+
}
|
|
204
|
+
await index.update(updateObj);
|
|
205
|
+
}
|
|
206
|
+
async deleteIndexById(indexName, id, namespace) {
|
|
207
|
+
const index = this.client.Index(indexName).namespace(namespace || "");
|
|
208
|
+
await index.deleteOne(id);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
export { PineconeVector };
|
package/eslint.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mastra/pinecone",
|
|
3
|
+
"version": "0.0.0-a2a-20250421213654",
|
|
4
|
+
"description": "Pinecone vector store provider for Mastra",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./dist/index.d.cts",
|
|
16
|
+
"default": "./dist/index.cjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@pinecone-database/pinecone": "^3.0.3",
|
|
24
|
+
"@mastra/core": "0.0.0-a2a-20250421213654"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@microsoft/api-extractor": "^7.52.1",
|
|
28
|
+
"@types/node": "^20.17.27",
|
|
29
|
+
"dotenv": "^16.4.7",
|
|
30
|
+
"eslint": "^9.23.0",
|
|
31
|
+
"tsup": "^8.4.0",
|
|
32
|
+
"typescript": "^5.8.2",
|
|
33
|
+
"vitest": "^3.0.9",
|
|
34
|
+
"@internal/lint": "0.0.2"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
|
|
38
|
+
"build:watch": "pnpm build --watch",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"lint": "eslint ."
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './vector/index';
|