@mastra/pinecone 0.1.0-alpha.35 → 0.1.0-alpha.37
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 +16 -0
- package/package.json +6 -5
- package/src/vector/filter.ts +7 -1
- package/src/vector/index.ts +2 -2
- package/tsconfig.json +1 -6
- package/dist/index.d.ts +0 -16
- package/dist/index.js +0 -176
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @mastra/pinecone
|
|
2
2
|
|
|
3
|
+
## 0.1.0-alpha.37
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 4f1d1a1: Enforce types ann cleanup package.json
|
|
8
|
+
- Updated dependencies [66a03ec]
|
|
9
|
+
- Updated dependencies [4f1d1a1]
|
|
10
|
+
- @mastra/core@0.2.0-alpha.101
|
|
11
|
+
|
|
12
|
+
## 0.1.0-alpha.36
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- Updated dependencies [9d1796d]
|
|
17
|
+
- @mastra/core@0.2.0-alpha.100
|
|
18
|
+
|
|
3
19
|
## 0.1.0-alpha.35
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/pinecone",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.37",
|
|
4
4
|
"description": "Pinecone vector store provider for Mastra",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -16,16 +16,17 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@pinecone-database/pinecone": "^3.0.3",
|
|
19
|
-
"@mastra/core": "^0.2.0-alpha.
|
|
19
|
+
"@mastra/core": "^0.2.0-alpha.101"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@
|
|
23
|
-
"@types/node": "^22.
|
|
22
|
+
"@microsoft/api-extractor": "^7.49.2",
|
|
23
|
+
"@types/node": "^22.13.1",
|
|
24
|
+
"dotenv": "^16.4.7",
|
|
24
25
|
"tsup": "^8.0.1",
|
|
25
26
|
"vitest": "^3.0.4"
|
|
26
27
|
},
|
|
27
28
|
"scripts": {
|
|
28
|
-
"build": "tsup src/index.ts --format esm --dts --clean --treeshake",
|
|
29
|
+
"build": "tsup src/index.ts --format esm --experimental-dts --clean --treeshake",
|
|
29
30
|
"build:watch": "pnpm build --watch",
|
|
30
31
|
"test": "vitest run"
|
|
31
32
|
}
|
package/src/vector/filter.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
BaseFilterTranslator,
|
|
3
|
+
type FieldCondition,
|
|
4
|
+
type Filter,
|
|
5
|
+
type OperatorSupport,
|
|
6
|
+
type QueryOperator,
|
|
7
|
+
} from '@mastra/core/filter';
|
|
2
8
|
|
|
3
9
|
export class PineconeFilterTranslator extends BaseFilterTranslator {
|
|
4
10
|
protected override getSupportedOperators(): OperatorSupport {
|
package/src/vector/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Filter } from '@mastra/core/filter';
|
|
2
|
-
import { MastraVector, QueryResult, IndexStats } from '@mastra/core/vector';
|
|
1
|
+
import { type Filter } from '@mastra/core/filter';
|
|
2
|
+
import { MastraVector, type QueryResult, type IndexStats } from '@mastra/core/vector';
|
|
3
3
|
import { Pinecone } from '@pinecone-database/pinecone';
|
|
4
4
|
|
|
5
5
|
import { PineconeFilterTranslator } from './filter';
|
package/tsconfig.json
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"extends": "../../tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"moduleResolution": "bundler",
|
|
5
|
-
"outDir": "./dist",
|
|
6
|
-
"rootDir": "./src"
|
|
7
|
-
},
|
|
2
|
+
"extends": "../../tsconfig.node.json",
|
|
8
3
|
"include": ["src/**/*"],
|
|
9
4
|
"exclude": ["node_modules", "**/*.test.ts"]
|
|
10
5
|
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Filter } from '@mastra/core/filter';
|
|
2
|
-
import { MastraVector, QueryResult, IndexStats } from '@mastra/core/vector';
|
|
3
|
-
|
|
4
|
-
declare class PineconeVector extends MastraVector {
|
|
5
|
-
private client;
|
|
6
|
-
constructor(apiKey: string, environment?: string);
|
|
7
|
-
createIndex(indexName: string, dimension: number, metric?: 'cosine' | 'euclidean' | 'dotproduct'): Promise<void>;
|
|
8
|
-
upsert(indexName: string, vectors: number[][], metadata?: Record<string, any>[], ids?: string[]): Promise<string[]>;
|
|
9
|
-
transformFilter(filter?: Filter): Filter | undefined;
|
|
10
|
-
query(indexName: string, queryVector: number[], topK?: number, filter?: Filter, includeVector?: boolean): Promise<QueryResult[]>;
|
|
11
|
-
listIndexes(): Promise<string[]>;
|
|
12
|
-
describeIndex(indexName: string): Promise<IndexStats>;
|
|
13
|
-
deleteIndex(indexName: string): Promise<void>;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export { PineconeVector };
|
package/dist/index.js
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
import { MastraVector } from '@mastra/core/vector';
|
|
2
|
-
import { Pinecone } from '@pinecone-database/pinecone';
|
|
3
|
-
import { BaseFilterTranslator } from '@mastra/core/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
|
-
constructor(apiKey, environment) {
|
|
86
|
-
super();
|
|
87
|
-
const opts = { apiKey };
|
|
88
|
-
if (environment) {
|
|
89
|
-
opts["controllerHostUrl"] = environment;
|
|
90
|
-
}
|
|
91
|
-
const baseClient = new Pinecone(opts);
|
|
92
|
-
const telemetry = this.__getTelemetry();
|
|
93
|
-
this.client = telemetry?.traceClass(baseClient, {
|
|
94
|
-
spanNamePrefix: "pinecone-vector",
|
|
95
|
-
attributes: {
|
|
96
|
-
"vector.type": "pinecone"
|
|
97
|
-
}
|
|
98
|
-
}) ?? baseClient;
|
|
99
|
-
}
|
|
100
|
-
async createIndex(indexName, dimension, metric = "cosine") {
|
|
101
|
-
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
102
|
-
throw new Error("Dimension must be a positive integer");
|
|
103
|
-
}
|
|
104
|
-
await this.client.createIndex({
|
|
105
|
-
name: indexName,
|
|
106
|
-
dimension,
|
|
107
|
-
metric,
|
|
108
|
-
spec: {
|
|
109
|
-
serverless: {
|
|
110
|
-
cloud: "aws",
|
|
111
|
-
region: "us-east-1"
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
async upsert(indexName, vectors, metadata, ids) {
|
|
117
|
-
const index = this.client.Index(indexName);
|
|
118
|
-
const vectorIds = ids || vectors.map(() => crypto.randomUUID());
|
|
119
|
-
const records = vectors.map((vector, i) => ({
|
|
120
|
-
id: vectorIds[i],
|
|
121
|
-
values: vector,
|
|
122
|
-
metadata: metadata?.[i] || {}
|
|
123
|
-
}));
|
|
124
|
-
const batchSize = 100;
|
|
125
|
-
for (let i = 0; i < records.length; i += batchSize) {
|
|
126
|
-
const batch = records.slice(i, i + batchSize);
|
|
127
|
-
await index.upsert(batch);
|
|
128
|
-
}
|
|
129
|
-
return vectorIds;
|
|
130
|
-
}
|
|
131
|
-
transformFilter(filter) {
|
|
132
|
-
const pineconeFilter = new PineconeFilterTranslator();
|
|
133
|
-
const translatedFilter = pineconeFilter.translate(filter);
|
|
134
|
-
return translatedFilter;
|
|
135
|
-
}
|
|
136
|
-
async query(indexName, queryVector, topK = 10, filter, includeVector = false) {
|
|
137
|
-
const index = this.client.Index(indexName);
|
|
138
|
-
const translatedFilter = this.transformFilter(filter);
|
|
139
|
-
const results = await index.query({
|
|
140
|
-
vector: queryVector,
|
|
141
|
-
topK,
|
|
142
|
-
filter: translatedFilter,
|
|
143
|
-
includeMetadata: true,
|
|
144
|
-
includeValues: includeVector
|
|
145
|
-
});
|
|
146
|
-
return results.matches.map((match) => ({
|
|
147
|
-
id: match.id,
|
|
148
|
-
score: match.score || 0,
|
|
149
|
-
metadata: match.metadata,
|
|
150
|
-
...includeVector && { vector: match.values || [] }
|
|
151
|
-
}));
|
|
152
|
-
}
|
|
153
|
-
async listIndexes() {
|
|
154
|
-
const indexesResult = await this.client.listIndexes();
|
|
155
|
-
return indexesResult?.indexes?.map((index) => index.name) || [];
|
|
156
|
-
}
|
|
157
|
-
async describeIndex(indexName) {
|
|
158
|
-
const index = this.client.Index(indexName);
|
|
159
|
-
const stats = await index.describeIndexStats();
|
|
160
|
-
const description = await this.client.describeIndex(indexName);
|
|
161
|
-
return {
|
|
162
|
-
dimension: description.dimension,
|
|
163
|
-
count: stats.totalRecordCount || 0,
|
|
164
|
-
metric: description.metric
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
async deleteIndex(indexName) {
|
|
168
|
-
try {
|
|
169
|
-
await this.client.deleteIndex(indexName);
|
|
170
|
-
} catch (error) {
|
|
171
|
-
throw new Error(`Failed to delete Pinecone index: ${error.message}`);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
export { PineconeVector };
|