2025-08-07 19:33:03 -07:00
#!/usr/bin/env node
/ * *
2025-08-08 18:05:12 -07:00
* Brainy CLI - Redesigned for Better UX
* Direct commands + Augmentation system
2025-08-07 19:33:03 -07:00
* /
// @ts-ignore
import { program } from 'commander'
import { Cortex } from '../dist/cortex/cortex.js'
// @ts-ignore
import chalk from 'chalk'
import { readFileSync } from 'fs'
import { dirname , join } from 'path'
import { fileURLToPath } from 'url'
const _ _dirname = dirname ( fileURLToPath ( import . meta . url ) )
const packageJson = JSON . parse ( readFileSync ( join ( _ _dirname , '..' , 'package.json' ) , 'utf8' ) )
// Create Cortex instance
const cortex = new Cortex ( )
2025-08-08 18:05:12 -07:00
// Helper functions
2025-08-07 19:33:03 -07:00
const exitProcess = ( code = 0 ) => {
2025-08-08 18:05:12 -07:00
setTimeout ( ( ) => process . exit ( code ) , 100 )
2025-08-07 19:33:03 -07:00
}
const wrapAction = ( fn ) => {
return async ( ... args ) => {
try {
await fn ( ... args )
exitProcess ( 0 )
} catch ( error ) {
console . error ( chalk . red ( 'Error:' ) , error . message )
exitProcess ( 1 )
}
}
}
const wrapInteractive = ( fn ) => {
return async ( ... args ) => {
try {
await fn ( ... args )
exitProcess ( 0 )
} catch ( error ) {
console . error ( chalk . red ( 'Error:' ) , error . message )
exitProcess ( 1 )
}
}
}
2025-08-08 18:05:12 -07:00
// ========================================
// MAIN PROGRAM SETUP
// ========================================
2025-08-07 19:33:03 -07:00
program
2025-08-08 18:05:12 -07:00
. name ( 'brainy' )
. description ( '🧠 Brainy - Vector + Graph Database with AI Coordination' )
2025-08-07 19:33:03 -07:00
. version ( packageJson . version )
2025-08-08 18:05:12 -07:00
// ========================================
// CORE DATABASE COMMANDS (Direct Access)
// ========================================
2025-08-07 19:33:03 -07:00
program
. command ( 'init' )
2025-08-08 18:05:12 -07:00
. description ( '🚀 Initialize Brainy in your project' )
2025-08-07 19:33:03 -07:00
. option ( '-s, --storage <type>' , 'Storage type (filesystem, s3, r2, gcs, memory)' )
. option ( '-e, --encryption' , 'Enable encryption for secrets' )
. action ( wrapAction ( async ( options ) => {
await cortex . init ( options )
} ) )
program
. command ( 'add [data]' )
. description ( '📊 Add data to Brainy' )
. option ( '-m, --metadata <json>' , 'Metadata as JSON' )
. option ( '-i, --id <id>' , 'Custom ID' )
2025-08-08 18:05:12 -07:00
. action ( wrapAction ( async ( data , options ) => {
2025-08-07 19:33:03 -07:00
let metadata = { }
if ( options . metadata ) {
try {
metadata = JSON . parse ( options . metadata )
} catch {
console . error ( chalk . red ( 'Invalid JSON metadata' ) )
process . exit ( 1 )
}
}
if ( options . id ) {
metadata . id = options . id
}
await cortex . add ( data , metadata )
2025-08-08 18:05:12 -07:00
} ) )
2025-08-07 19:33:03 -07:00
program
. command ( 'search <query>' )
2025-08-08 18:05:12 -07:00
. description ( '🔍 Search your database' )
2025-08-07 19:33:03 -07:00
. option ( '-l, --limit <number>' , 'Number of results' , '10' )
. option ( '-f, --filter <json>' , 'MongoDB-style metadata filters' )
. option ( '-v, --verbs <types>' , 'Graph verb types to traverse (comma-separated)' )
. option ( '-d, --depth <number>' , 'Graph traversal depth' , '1' )
2025-08-08 18:05:12 -07:00
. action ( wrapAction ( async ( query , options ) => {
2025-08-07 19:33:03 -07:00
const searchOptions = { limit : parseInt ( options . limit ) }
if ( options . filter ) {
try {
searchOptions . filter = JSON . parse ( options . filter )
} catch {
console . error ( chalk . red ( 'Invalid filter JSON' ) )
process . exit ( 1 )
}
}
if ( options . verbs ) {
searchOptions . verbs = options . verbs . split ( ',' ) . map ( v => v . trim ( ) )
searchOptions . depth = parseInt ( options . depth )
}
await cortex . search ( query , searchOptions )
} ) )
program
2025-08-08 18:05:12 -07:00
. command ( 'chat [question]' )
. description ( '💬 Chat with your data (interactive mode if no question)' )
. option ( '-l, --llm <model>' , 'LLM model to use' )
. action ( wrapInteractive ( async ( question , options ) => {
await cortex . chat ( question )
2025-08-07 19:33:03 -07:00
} ) )
program
. command ( 'stats' )
. description ( '📊 Show database statistics' )
2025-08-08 18:05:12 -07:00
. option ( '-d, --detailed' , 'Show detailed statistics' )
2025-08-07 19:33:03 -07:00
. action ( wrapAction ( async ( options ) => {
await cortex . stats ( options . detailed )
} ) )
program
2025-08-08 18:05:12 -07:00
. command ( 'health' )
. description ( '🔋 Check system health' )
. option ( '--auto-fix' , 'Automatically apply safe repairs' )
. action ( wrapAction ( async ( options ) => {
await cortex . health ( options )
2025-08-07 19:33:03 -07:00
} ) )
program
2025-08-08 18:05:12 -07:00
. command ( 'find' )
. description ( '🔍 Interactive advanced search' )
. action ( wrapInteractive ( async ( ) => {
await cortex . advancedSearch ( )
2025-08-07 19:33:03 -07:00
} ) )
program
2025-08-08 18:05:12 -07:00
. command ( 'explore [nodeId]' )
. description ( '🗺️ Interactively explore graph connections' )
. action ( wrapInteractive ( async ( nodeId ) => {
await cortex . explore ( nodeId )
2025-08-07 19:33:03 -07:00
} ) )
program
. command ( 'backup' )
. description ( '💾 Create database backup' )
. option ( '-c, --compress' , 'Compress backup' )
. option ( '-o, --output <file>' , 'Output file' )
. action ( wrapAction ( async ( options ) => {
await cortex . backup ( options )
} ) )
program
. command ( 'restore <file>' )
. description ( '♻️ Restore from backup' )
. action ( wrapInteractive ( async ( file ) => {
await cortex . restore ( file )
} ) )
2025-08-08 18:05:12 -07:00
// ========================================
// AUGMENTATION MANAGEMENT (Direct Commands)
// ========================================
2025-08-07 19:33:03 -07:00
program
2025-08-08 18:05:12 -07:00
. command ( 'install <augmentation>' )
. description ( '📦 Install augmentation' )
. option ( '-m, --mode <type>' , 'Installation mode (free|premium)' , 'free' )
. option ( '-c, --config <json>' , 'Configuration as JSON' )
. action ( wrapAction ( async ( augmentation , options ) => {
if ( augmentation === 'brain-jar' ) {
await cortex . brainJarInstall ( options . mode )
} else {
// Generic augmentation install
let config = { }
if ( options . config ) {
try {
config = JSON . parse ( options . config )
} catch {
console . error ( chalk . red ( 'Invalid JSON configuration' ) )
process . exit ( 1 )
}
}
await cortex . addAugmentation ( augmentation , undefined , config )
}
2025-08-07 19:33:03 -07:00
} ) )
program
2025-08-08 18:05:12 -07:00
. command ( 'run <augmentation>' )
. description ( '⚡ Run augmentation' )
. option ( '-c, --config <json>' , 'Runtime configuration as JSON' )
. action ( wrapAction ( async ( augmentation , options ) => {
if ( augmentation === 'brain-jar' ) {
await cortex . brainJarStart ( options )
} else {
// Generic augmentation execution
const inputData = options . config ? JSON . parse ( options . config ) : { run : true }
await cortex . executePipelineStep ( augmentation , inputData )
}
2025-08-07 19:33:03 -07:00
} ) )
program
2025-08-08 18:05:12 -07:00
. command ( 'status [augmentation]' )
. description ( '📊 Show augmentation status' )
. action ( wrapAction ( async ( augmentation ) => {
if ( augmentation === 'brain-jar' ) {
await cortex . brainJarStatus ( )
} else if ( augmentation ) {
// Show specific augmentation status
await cortex . listAugmentations ( )
} else {
// Show all augmentation status
await cortex . listAugmentations ( )
}
2025-08-07 19:33:03 -07:00
} ) )
program
2025-08-08 18:05:12 -07:00
. command ( 'stop [augmentation]' )
. description ( '⏹️ Stop augmentation' )
. action ( wrapAction ( async ( augmentation ) => {
if ( augmentation === 'brain-jar' ) {
await cortex . brainJarStop ( )
} else {
console . log ( chalk . yellow ( 'Stop functionality for generic augmentations not yet implemented' ) )
}
2025-08-07 19:33:03 -07:00
} ) )
program
2025-08-08 18:05:12 -07:00
. command ( 'list' )
. description ( '📋 List installed augmentations' )
. option ( '-a, --available' , 'Show available augmentations' )
2025-08-07 19:33:03 -07:00
. action ( wrapAction ( async ( options ) => {
2025-08-08 18:05:12 -07:00
if ( options . available ) {
console . log ( chalk . cyan ( '🧩 Available Augmentations:' ) )
console . log ( ' • brain-jar - AI coordination and collaboration' )
console . log ( ' • encryption - Data encryption and security' )
console . log ( ' • neural-import - AI-powered data analysis' )
console . log ( ' • performance-monitor - System monitoring' )
console . log ( '' )
console . log ( chalk . dim ( 'Install: brainy install <augmentation>' ) )
} else {
await cortex . listAugmentations ( )
}
2025-08-07 19:33:03 -07:00
} ) )
2025-08-09 08:24:11 -07:00
// ========================================
// BRAIN CLOUD SUPER COMMAND (New!)
// ========================================
program
. command ( 'cloud' )
. description ( '☁️ Setup Brain Cloud - AI coordination across all devices' )
. option ( '-m, --mode <type>' , 'Setup mode (free|premium)' , 'interactive' )
. option ( '-k, --key <key>' , 'License key for premium features' )
. option ( '-s, --skip-install' , 'Skip Brain Jar installation' )
. action ( wrapInteractive ( async ( options ) => {
await cortex . setupBrainCloud ( options )
} ) )
2025-08-08 18:05:12 -07:00
// ========================================
// BRAIN JAR SPECIFIC COMMANDS (Rich UX)
// ========================================
2025-08-07 19:33:03 -07:00
2025-08-08 18:05:12 -07:00
const brainJar = program . command ( 'brain-jar' )
. description ( '🧠🫙 AI coordination and collaboration' )
2025-08-07 19:33:03 -07:00
2025-08-08 18:05:12 -07:00
brainJar
. command ( 'install' )
. description ( '📦 Install Brain Jar coordination' )
. option ( '-m, --mode <type>' , 'Installation mode (free|premium)' , 'free' )
. action ( wrapAction ( async ( options ) => {
await cortex . brainJarInstall ( options . mode )
2025-08-07 19:33:03 -07:00
} ) )
2025-08-08 18:05:12 -07:00
brainJar
. command ( 'start' )
. description ( '🚀 Start Brain Jar coordination' )
. option ( '-s, --server <url>' , 'Custom server URL' )
. option ( '-n, --name <name>' , 'Agent name' )
. option ( '-r, --role <role>' , 'Agent role' )
. action ( wrapAction ( async ( options ) => {
await cortex . brainJarStart ( options )
2025-08-07 19:33:03 -07:00
} ) )
2025-08-08 18:05:12 -07:00
brainJar
. command ( 'dashboard' )
. description ( '📊 Open Brain Jar dashboard' )
. option ( '-o, --open' , 'Auto-open in browser' , true )
. action ( wrapAction ( async ( options ) => {
await cortex . brainJarDashboard ( options . open )
2025-08-07 19:33:03 -07:00
} ) )
2025-08-08 18:05:12 -07:00
brainJar
. command ( 'status' )
. description ( '🔍 Show Brain Jar status' )
. action ( wrapAction ( async ( ) => {
await cortex . brainJarStatus ( )
2025-08-07 19:33:03 -07:00
} ) )
2025-08-08 18:05:12 -07:00
brainJar
. command ( 'agents' )
. description ( '👥 List connected agents' )
2025-08-07 19:33:03 -07:00
. action ( wrapAction ( async ( ) => {
2025-08-08 18:05:12 -07:00
await cortex . brainJarAgents ( )
2025-08-07 19:33:03 -07:00
} ) )
2025-08-08 18:05:12 -07:00
brainJar
. command ( 'message <text>' )
. description ( '📨 Send message to coordination channel' )
. action ( wrapAction ( async ( text ) => {
await cortex . brainJarMessage ( text )
2025-08-07 19:33:03 -07:00
} ) )
2025-08-08 18:05:12 -07:00
brainJar
. command ( 'search <query>' )
. description ( '🔍 Search coordination history' )
. option ( '-l, --limit <number>' , 'Number of results' , '10' )
. action ( wrapAction ( async ( query , options ) => {
await cortex . brainJarSearch ( query , parseInt ( options . limit ) )
2025-08-07 19:33:03 -07:00
} ) )
2025-08-08 18:05:12 -07:00
// ========================================
// CONFIGURATION COMMANDS
// ========================================
2025-08-07 19:33:03 -07:00
2025-08-08 18:05:12 -07:00
const config = program . command ( 'config' )
. description ( '⚙️ Manage configuration' )
2025-08-07 19:33:03 -07:00
2025-08-08 18:05:12 -07:00
config
. command ( 'set <key> <value>' )
. description ( 'Set configuration value' )
. option ( '-e, --encrypt' , 'Encrypt this value' )
. action ( wrapAction ( async ( key , value , options ) => {
await cortex . configSet ( key , value , options )
2025-08-07 19:33:03 -07:00
} ) )
2025-08-08 18:05:12 -07:00
config
. command ( 'get <key>' )
. description ( 'Get configuration value' )
. action ( wrapAction ( async ( key ) => {
const value = await cortex . configGet ( key )
if ( value ) {
console . log ( chalk . green ( ` ${ key } : ${ value } ` ) )
} else {
console . log ( chalk . yellow ( ` Key not found: ${ key } ` ) )
2025-08-07 19:33:03 -07:00
}
} ) )
2025-08-08 18:05:12 -07:00
config
. command ( 'list' )
. description ( 'List all configuration' )
2025-08-07 19:33:03 -07:00
. action ( wrapAction ( async ( ) => {
2025-08-08 18:05:12 -07:00
await cortex . configList ( )
2025-08-07 19:33:03 -07:00
} ) )
2025-08-08 18:05:12 -07:00
// ========================================
// LEGACY CORTEX COMMANDS (Backward Compatibility)
// ========================================
2025-08-07 19:33:03 -07:00
2025-08-08 18:05:12 -07:00
const cortexCmd = program . command ( 'cortex' )
. description ( '🔧 Legacy Cortex commands (deprecated - use direct commands)' )
2025-08-07 19:33:03 -07:00
2025-08-08 18:05:12 -07:00
cortexCmd
. command ( 'chat [question]' )
. description ( '💬 Chat with your data' )
. action ( wrapInteractive ( async ( question ) => {
console . log ( chalk . yellow ( '⚠️ Deprecated: Use "brainy chat" instead' ) )
await cortex . chat ( question )
2025-08-07 19:33:03 -07:00
} ) )
2025-08-08 18:05:12 -07:00
cortexCmd
. command ( 'add [data]' )
. description ( '📊 Add data' )
. action ( wrapAction ( async ( data ) => {
console . log ( chalk . yellow ( '⚠️ Deprecated: Use "brainy add" instead' ) )
await cortex . add ( data , { } )
2025-08-07 19:33:03 -07:00
} ) )
2025-08-08 18:05:12 -07:00
// ========================================
// INTERACTIVE SHELL
// ========================================
2025-08-07 19:33:03 -07:00
program
. command ( 'shell' )
2025-08-08 18:05:12 -07:00
. description ( '🐚 Interactive Brainy shell' )
. action ( wrapInteractive ( async ( ) => {
console . log ( chalk . cyan ( '🧠 Brainy Interactive Shell' ) )
2025-08-07 19:33:03 -07:00
console . log ( chalk . dim ( 'Type "help" for commands, "exit" to quit\n' ) )
await cortex . chat ( )
2025-08-08 18:05:12 -07:00
} ) )
// ========================================
// PARSE AND HANDLE
// ========================================
2025-08-07 19:33:03 -07:00
program . parse ( process . argv )
// Show help if no command
if ( ! process . argv . slice ( 2 ) . length ) {
2025-08-09 08:24:11 -07:00
console . log ( chalk . cyan ( '🧠☁️ Brainy - AI Coordination Service' ) )
console . log ( '' )
console . log ( chalk . bold ( 'One-Command Setup:' ) )
console . log ( chalk . green ( ' brainy cloud # Setup Brain Cloud (recommended!)' ) )
2025-08-08 18:05:12 -07:00
console . log ( '' )
console . log ( chalk . bold ( 'Quick Start:' ) )
console . log ( ' brainy init # Initialize project' )
console . log ( ' brainy add "some data" # Add data' )
console . log ( ' brainy search "query" # Search data' )
console . log ( ' brainy chat # Chat with data' )
console . log ( '' )
console . log ( chalk . bold ( 'AI Coordination:' ) )
console . log ( ' brainy install brain-jar # Install AI coordination' )
console . log ( ' brainy brain-jar start # Start coordination' )
console . log ( ' brainy brain-jar dashboard # View dashboard' )
console . log ( '' )
2025-08-07 19:33:03 -07:00
program . outputHelp ( )
}