Skip to content

Commit 832d8a7

Browse files
committed
feat: update configuration and initialization for MongoDB and PostgreSQL stores
- Removed optional WorkOS configuration from .env.example. - Initialized MongoDB store and created a vector index for embeddings in mongodb.ts. - Initialized PostgreSQL store and logged index details in pg-storage.ts. - Updated memory configuration to use HNSW index type in mongodb.ts. - Adjusted import statement for ExtractParams to use type import in upstashMemory.ts. - Added CORS configuration to mastra index for better API access control. - Added authentication logic using Supabase in auth.ts.
1 parent 8b7b374 commit 832d8a7

6 files changed

Lines changed: 46 additions & 7 deletions

File tree

.env.example

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,3 @@ NEXT_PUBLIC_MASTRA_API_URL='http://localhost:4111'
6767
# Example placeholders for local testing
6868
LOCAL_DEV='true'
6969

70-
# WorkOS Configuration (optional, for authentication)
71-
WORKOS_API_KEY=sk_live_...
72-
WORKOS_CLIENT_ID=client_...

lib/auth.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { createClient } from "@supabase/supabase-js";
2+
3+
const supabase = createClient("<supabase-url>", "<supabase-key>");
4+
5+
const authTokenResponse = await supabase.auth.signInWithPassword({
6+
email: "<user's email>",
7+
password: "<user's password>",
8+
});
9+
10+
const accessToken = authTokenResponse.data?.session?.access_token;

src/mastra/config/mongodb.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ const mongoStore = new MongoDBStore({
3030
url: process.env.MONGODB_URI ?? 'mongodb://localhost:27017',
3131
dbName: process.env.MONGODB_DATABASE ?? 'AgentStack',
3232
});
33+
34+
await mongoStore.init();
35+
//const allIndexes = await mongoStore.listIndexes();
36+
//console.log(allIndexes);
37+
//log.info('MongoDB Store initialized with MongoDBVector support, all indexes:', {
38+
// indexCount: allIndexes.length,
39+
// indexes: allIndexes,
40+
//});
41+
42+
3343
/**
3444
* Initialize MongoDB Vector store with proper configuration
3545
*/
@@ -38,6 +48,11 @@ const mongoVector = new MongoDBVector({
3848
dbName: MONGODB_CONFIG.dbName,
3949
});
4050

51+
// Create a vector index for embeddings
52+
await mongoVector.createIndex({
53+
indexName: "ultra",
54+
dimension: 1536,
55+
});
4156

4257
/**
4358
* * Memory configuration using MongoDB Vector & Storage
@@ -64,9 +79,12 @@ export const mongoMemory = new Memory({
6479
scope: 'resource', // 'resource' | 'thread'
6580
// HNSW index configuration to support high-dimensional embeddings (>2000 dimensions)
6681
indexConfig: {
67-
type: 'flat', // flat index type (supports dimensions > 4000, unlike HNSW limit of 2000)
82+
type: 'hnsw', // flat index type (supports dimensions > 4000, unlike HNSW limit of 2000)
6883
metric: 'cosine', // Distance metric for normalized embeddings
69-
ivf: {lists: 4000},
84+
hnsw: {
85+
m: 16, // Number of bi-directional links created for every new element
86+
efConstruction: 200, // Size of the dynamic candidate list for construction
87+
},
7088
}
7189
},
7290
// Enhanced working memory with supported template

src/mastra/config/pg-storage.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
import { Memory } from '@mastra/memory'
23
import { PgVector, PostgresStore } from '@mastra/pg'
34
import { createVectorQueryTool, createGraphRAGTool } from '@mastra/rag'
@@ -11,7 +12,6 @@ import { google } from '@ai-sdk/google'
1112
//import type { CoreMessage } from '@mastra/core';
1213
import { maskStreamTags } from '@mastra/core';
1314
import { z } from 'zod'
14-
import { MDocument } from "@mastra/rag";
1515

1616
// Use the proper CoreMessage type from @mastra/core
1717
// This replaces the custom extension that was causing type conflicts
@@ -68,6 +68,14 @@ export const pgStore = new PostgresStore({
6868
keepAliveInitialDelayMillis: 0,
6969
})
7070

71+
await pgStore.init();
72+
const allIndexes = await pgStore.listIndexes();
73+
console.log(allIndexes);
74+
log.info('PostgreSQL Store initialized with PgVector support, all indexes:', {
75+
indexCount: allIndexes.length,
76+
indexes: allIndexes,
77+
});
78+
7179
// PgVector configuration for 1568 dimension embeddings (gemini-embedding-002)
7280
export const pgVector = new PgVector({
7381
connectionString:

src/mastra/config/upstashMemory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { google } from '@ai-sdk/google'
1010
import { AISpanType } from '@mastra/core/ai-tracing';
1111
import type { TracingContext } from '@mastra/core/ai-tracing';
1212
import { log } from './logger';
13-
import { ExtractParams } from '@mastra/rag';
13+
import type { ExtractParams } from '@mastra/rag';
1414

1515
/**
1616
* Redefine CoreMessage to include a metadata property for custom data.

src/mastra/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,12 @@ export const mastra = new Mastra({
637637
}
638638
}),
639639
],
640+
cors: {
641+
origin: ["*"], // Allow specific origins or '*' for all
642+
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
643+
allowHeaders: ["Content-Type", "Authorization"],
644+
credentials: false,
645+
},
640646
middleware: [
641647
async (c, next) => {
642648
const runtimeContext = c.get("runtimeContext");

0 commit comments

Comments
 (0)