feat: add WebSocket API, REST API routes, and server setup for Brainy
Implemented a WebSocket API for real-time communication and REST API routes for CRUD operations on nouns and verbs. Added a server initialization script with WebSocket server integration and improved application structure with modularized routing and services.
This commit is contained in:
parent
6feab06f34
commit
e00f1a92ce
9 changed files with 1533 additions and 85 deletions
80
cloud-wrapper/src/services/brainyService.ts
Normal file
80
cloud-wrapper/src/services/brainyService.ts
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
import { BrainyData } from '@soulcraft/brainy';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
// Load environment variables
|
||||
dotenv.config();
|
||||
|
||||
/**
|
||||
* Initialize the Brainy instance with appropriate configuration
|
||||
* based on environment variables
|
||||
*/
|
||||
export async function initializeBrainy(): Promise<BrainyData> {
|
||||
// Get storage configuration from environment variables
|
||||
const storageType = process.env.STORAGE_TYPE || 'filesystem';
|
||||
|
||||
// Create configuration object
|
||||
const config: any = {
|
||||
storage: {}
|
||||
};
|
||||
|
||||
// Configure storage based on type
|
||||
switch (storageType) {
|
||||
case 's3':
|
||||
// Configure S3 storage
|
||||
config.storage.s3Storage = {
|
||||
bucketName: process.env.S3_BUCKET_NAME,
|
||||
accessKeyId: process.env.S3_ACCESS_KEY_ID,
|
||||
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
|
||||
region: process.env.S3_REGION || 'us-east-1',
|
||||
endpoint: process.env.S3_ENDPOINT // Optional for custom S3-compatible services
|
||||
};
|
||||
break;
|
||||
|
||||
case 'filesystem':
|
||||
// Configure filesystem storage
|
||||
config.storage.rootDirectory = process.env.STORAGE_ROOT_DIR || './data';
|
||||
break;
|
||||
|
||||
case 'memory':
|
||||
// No additional configuration needed for memory storage
|
||||
break;
|
||||
|
||||
default:
|
||||
// Default to filesystem storage
|
||||
config.storage.rootDirectory = process.env.STORAGE_ROOT_DIR || './data';
|
||||
break;
|
||||
}
|
||||
|
||||
// Configure embedding options
|
||||
if (process.env.USE_SIMPLE_EMBEDDING === 'true') {
|
||||
// Use simple embedding (faster but less accurate)
|
||||
config.useSimpleEmbedding = true;
|
||||
}
|
||||
|
||||
// Configure HNSW index parameters if provided
|
||||
if (process.env.HNSW_M) {
|
||||
config.hnsw = {
|
||||
M: parseInt(process.env.HNSW_M, 10),
|
||||
efConstruction: process.env.HNSW_EF_CONSTRUCTION
|
||||
? parseInt(process.env.HNSW_EF_CONSTRUCTION, 10)
|
||||
: 200,
|
||||
efSearch: process.env.HNSW_EF_SEARCH
|
||||
? parseInt(process.env.HNSW_EF_SEARCH, 10)
|
||||
: 50
|
||||
};
|
||||
}
|
||||
|
||||
// Create and initialize the Brainy instance
|
||||
const brainy = new BrainyData(config);
|
||||
await brainy.init();
|
||||
|
||||
return brainy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current storage type being used by Brainy
|
||||
*/
|
||||
export async function getStorageType(brainy: BrainyData): Promise<string> {
|
||||
const status = await brainy.status();
|
||||
return status.storageType || 'unknown';
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue