2025-06-19 16:03:04 -07:00
import { Router , Request , Response , NextFunction , RequestHandler } from 'express' ;
2025-06-06 09:09:07 -07:00
import { BrainyData , NounType , VerbType } from '@soulcraft/brainy' ;
// Define a custom request type that includes the Brainy instance
interface BrainyRequest extends Request {
brainy : BrainyData ;
}
2025-06-19 16:03:04 -07:00
// Helper function to handle async route handlers
const asyncHandler = ( fn : ( req : BrainyRequest , res : Response , next : NextFunction ) = > Promise < any > ) = >
( req : Request , res : Response , next : NextFunction ) = > {
Promise . resolve ( fn ( req as BrainyRequest , res , next ) ) . catch ( next ) ;
} ;
2025-06-06 09:09:07 -07:00
export function setupRoutes() {
const router = Router ( ) ;
// Get database status
2025-06-19 16:03:04 -07:00
router . get ( '/status' , asyncHandler ( async ( req : BrainyRequest , res : Response , next : NextFunction ) = > {
2025-06-06 09:09:07 -07:00
try {
const status = await req . brainy . status ( ) ;
res . status ( 200 ) . json ( status ) ;
} catch ( error ) {
console . error ( 'Error getting status:' , error ) ;
res . status ( 500 ) . json ( { error : 'Failed to get database status' } ) ;
}
2025-06-19 16:03:04 -07:00
} ) ) ;
2025-06-06 09:09:07 -07:00
// Add a noun (entity)
2025-06-19 16:03:04 -07:00
router . post ( '/nouns' , asyncHandler ( async ( req : BrainyRequest , res : Response , next : NextFunction ) = > {
2025-06-06 09:09:07 -07:00
try {
const { text , metadata } = req . body ;
2025-06-19 16:03:04 -07:00
2025-06-06 09:09:07 -07:00
if ( ! text ) {
return res . status ( 400 ) . json ( { error : 'Text is required' } ) ;
}
const id = await req . brainy . add ( text , metadata || { } ) ;
res . status ( 201 ) . json ( { id } ) ;
} catch ( error ) {
console . error ( 'Error adding noun:' , error ) ;
res . status ( 500 ) . json ( { error : 'Failed to add noun' } ) ;
}
2025-06-19 16:03:04 -07:00
} ) ) ;
2025-06-06 09:09:07 -07:00
// Get a noun by ID
2025-06-19 16:03:04 -07:00
router . get ( '/nouns/:id' , asyncHandler ( async ( req : BrainyRequest , res : Response , next : NextFunction ) = > {
2025-06-06 09:09:07 -07:00
try {
const { id } = req . params ;
const noun = await req . brainy . get ( id ) ;
2025-06-19 16:03:04 -07:00
2025-06-06 09:09:07 -07:00
if ( ! noun ) {
return res . status ( 404 ) . json ( { error : 'Noun not found' } ) ;
}
2025-06-19 16:03:04 -07:00
2025-06-06 09:09:07 -07:00
res . status ( 200 ) . json ( noun ) ;
} catch ( error ) {
console . error ( 'Error getting noun:' , error ) ;
res . status ( 500 ) . json ( { error : 'Failed to get noun' } ) ;
}
2025-06-19 16:03:04 -07:00
} ) ) ;
2025-06-06 09:09:07 -07:00
// Update noun metadata
2025-06-19 16:03:04 -07:00
router . put ( '/nouns/:id' , asyncHandler ( async ( req : BrainyRequest , res : Response , next : NextFunction ) = > {
2025-06-06 09:09:07 -07:00
try {
const { id } = req . params ;
const { metadata } = req . body ;
2025-06-19 16:03:04 -07:00
2025-06-06 09:09:07 -07:00
if ( ! metadata ) {
return res . status ( 400 ) . json ( { error : 'Metadata is required' } ) ;
}
2025-06-19 16:03:04 -07:00
2025-06-06 09:09:07 -07:00
await req . brainy . updateMetadata ( id , metadata ) ;
res . status ( 200 ) . json ( { success : true } ) ;
} catch ( error ) {
console . error ( 'Error updating noun:' , error ) ;
res . status ( 500 ) . json ( { error : 'Failed to update noun' } ) ;
}
2025-06-19 16:03:04 -07:00
} ) ) ;
2025-06-06 09:09:07 -07:00
// Delete a noun
2025-06-19 16:03:04 -07:00
router . delete ( '/nouns/:id' , asyncHandler ( async ( req : BrainyRequest , res : Response , next : NextFunction ) = > {
2025-06-06 09:09:07 -07:00
try {
const { id } = req . params ;
await req . brainy . delete ( id ) ;
res . status ( 200 ) . json ( { success : true } ) ;
} catch ( error ) {
console . error ( 'Error deleting noun:' , error ) ;
res . status ( 500 ) . json ( { error : 'Failed to delete noun' } ) ;
}
2025-06-19 16:03:04 -07:00
} ) ) ;
2025-06-06 09:09:07 -07:00
// Search for similar nouns
2025-06-19 16:03:04 -07:00
router . post ( '/search' , asyncHandler ( async ( req : BrainyRequest , res : Response , next : NextFunction ) = > {
2025-06-06 09:09:07 -07:00
try {
const { query , limit = 10 } = req . body ;
2025-06-19 16:03:04 -07:00
2025-06-06 09:09:07 -07:00
if ( ! query ) {
return res . status ( 400 ) . json ( { error : 'Query is required' } ) ;
}
2025-06-19 16:03:04 -07:00
2025-06-06 09:09:07 -07:00
const results = await req . brainy . searchText ( query , limit ) ;
res . status ( 200 ) . json ( results ) ;
} catch ( error ) {
console . error ( 'Error searching:' , error ) ;
res . status ( 500 ) . json ( { error : 'Failed to search' } ) ;
}
2025-06-19 16:03:04 -07:00
} ) ) ;
2025-06-06 09:09:07 -07:00
// Add a verb (relationship)
2025-06-19 16:03:04 -07:00
router . post ( '/verbs' , asyncHandler ( async ( req : BrainyRequest , res : Response , next : NextFunction ) = > {
2025-06-06 09:09:07 -07:00
try {
const { sourceId , targetId , metadata } = req . body ;
2025-06-19 16:03:04 -07:00
2025-06-06 09:09:07 -07:00
if ( ! sourceId || ! targetId ) {
return res . status ( 400 ) . json ( { error : 'Source ID and Target ID are required' } ) ;
}
2025-06-19 16:03:04 -07:00
2025-06-06 09:09:07 -07:00
await req . brainy . addVerb ( sourceId , targetId , metadata || { } ) ;
res . status ( 201 ) . json ( { success : true } ) ;
} catch ( error ) {
console . error ( 'Error adding verb:' , error ) ;
res . status ( 500 ) . json ( { error : 'Failed to add verb' } ) ;
}
2025-06-19 16:03:04 -07:00
} ) ) ;
2025-06-06 09:09:07 -07:00
// Get all verbs
2025-06-19 16:03:04 -07:00
router . get ( '/verbs' , asyncHandler ( async ( req : BrainyRequest , res : Response , next : NextFunction ) = > {
2025-06-06 09:09:07 -07:00
try {
const verbs = await req . brainy . getAllVerbs ( ) ;
res . status ( 200 ) . json ( verbs ) ;
} catch ( error ) {
console . error ( 'Error getting verbs:' , error ) ;
res . status ( 500 ) . json ( { error : 'Failed to get verbs' } ) ;
}
2025-06-19 16:03:04 -07:00
} ) ) ;
2025-06-06 09:09:07 -07:00
// Get verbs by source
2025-06-19 16:03:04 -07:00
router . get ( '/verbs/source/:id' , asyncHandler ( async ( req : BrainyRequest , res : Response , next : NextFunction ) = > {
2025-06-06 09:09:07 -07:00
try {
const { id } = req . params ;
const verbs = await req . brainy . getVerbsBySource ( id ) ;
res . status ( 200 ) . json ( verbs ) ;
} catch ( error ) {
console . error ( 'Error getting verbs by source:' , error ) ;
res . status ( 500 ) . json ( { error : 'Failed to get verbs by source' } ) ;
}
2025-06-19 16:03:04 -07:00
} ) ) ;
2025-06-06 09:09:07 -07:00
// Get verbs by target
2025-06-19 16:03:04 -07:00
router . get ( '/verbs/target/:id' , asyncHandler ( async ( req : BrainyRequest , res : Response , next : NextFunction ) = > {
2025-06-06 09:09:07 -07:00
try {
const { id } = req . params ;
const verbs = await req . brainy . getVerbsByTarget ( id ) ;
res . status ( 200 ) . json ( verbs ) ;
} catch ( error ) {
console . error ( 'Error getting verbs by target:' , error ) ;
res . status ( 500 ) . json ( { error : 'Failed to get verbs by target' } ) ;
}
2025-06-19 16:03:04 -07:00
} ) ) ;
2025-06-06 09:09:07 -07:00
// Delete a verb
2025-06-19 16:03:04 -07:00
router . delete ( '/verbs/:id' , asyncHandler ( async ( req : BrainyRequest , res : Response , next : NextFunction ) = > {
2025-06-06 09:09:07 -07:00
try {
const { id } = req . params ;
await req . brainy . deleteVerb ( id ) ;
res . status ( 200 ) . json ( { success : true } ) ;
} catch ( error ) {
console . error ( 'Error deleting verb:' , error ) ;
res . status ( 500 ) . json ( { error : 'Failed to delete verb' } ) ;
}
2025-06-19 16:03:04 -07:00
} ) ) ;
2025-06-06 09:09:07 -07:00
// Clear all data
2025-06-19 16:03:04 -07:00
router . delete ( '/clear' , asyncHandler ( async ( req : BrainyRequest , res : Response , next : NextFunction ) = > {
2025-06-06 09:09:07 -07:00
try {
await req . brainy . clear ( ) ;
res . status ( 200 ) . json ( { success : true } ) ;
} catch ( error ) {
console . error ( 'Error clearing data:' , error ) ;
res . status ( 500 ) . json ( { error : 'Failed to clear data' } ) ;
}
2025-06-19 16:03:04 -07:00
} ) ) ;
2025-06-06 09:09:07 -07:00
return router ;
}