@@ -73,20 +73,29 @@ class DatabaseService extends Base {
7373 * @returns {Promise<{message: string}> }
7474 */
7575 async exportDatabase ( { include } ) {
76- logger . log ( 'Starting agent memory export...' ) ;
77- let memoryCount = 0 , summaryCount = 0 ;
76+ try {
77+ logger . log ( 'Starting agent memory export...' ) ;
78+ let memoryCount = 0 , summaryCount = 0 ;
7879
79- if ( include . includes ( 'memories' ) ) {
80- const collection = await ChromaManager . getMemoryCollection ( ) ;
81- memoryCount = await this . #exportCollection( collection , aiConfig . memoryDb . backupPath , 'memory-backup' ) ;
82- }
80+ if ( include . includes ( 'memories' ) ) {
81+ const collection = await ChromaManager . getMemoryCollection ( ) ;
82+ memoryCount = await this . #exportCollection( collection , aiConfig . memoryDb . backupPath , 'memory-backup' ) ;
83+ }
8384
84- if ( include . includes ( 'summaries' ) ) {
85- const collection = await ChromaManager . getSummaryCollection ( ) ;
86- summaryCount = await this . #exportCollection( collection , aiConfig . sessionDb . backupPath , 'summaries-backup' ) ;
87- }
85+ if ( include . includes ( 'summaries' ) ) {
86+ const collection = await ChromaManager . getSummaryCollection ( ) ;
87+ summaryCount = await this . #exportCollection( collection , aiConfig . sessionDb . backupPath , 'summaries-backup' ) ;
88+ }
8889
89- return { message : `Export complete. Exported ${ memoryCount } memories and ${ summaryCount } summaries.` } ;
90+ return { message : `Export complete. Exported ${ memoryCount } memories and ${ summaryCount } summaries.` } ;
91+ } catch ( error ) {
92+ logger . error ( '[DatabaseService] Error exporting database:' , error ) ;
93+ return {
94+ error : 'Failed to export database' ,
95+ message : error . message ,
96+ code : 'DATABASE_EXPORT_ERROR'
97+ } ;
98+ }
9099 }
91100
92101 /**
@@ -97,57 +106,66 @@ class DatabaseService extends Base {
97106 * @returns {Promise<{imported: number, total: number, mode: string}> }
98107 */
99108 async importDatabase ( { file, mode } ) {
100- const filePath = file ; // Assuming file object contains path
101- logger . log ( `Starting agent memory import from: ${ filePath } ` ) ;
102-
103- if ( ! await fs . pathExists ( filePath ) ) {
104- throw new Error ( `Backup file not found at ${ filePath } ` ) ;
105- }
109+ try {
110+ const filePath = file ; // Assuming file object contains path
111+ logger . log ( `Starting agent memory import from: ${ filePath } ` ) ;
106112
107- // Determine which collection to import into based on filename
108- const isMemoryBackup = path . basename ( filePath ) . startsWith ( 'memory-backup' ) ;
109- let collection = isMemoryBackup
110- ? await ChromaManager . getMemoryCollection ( )
111- : await ChromaManager . getSummaryCollection ( ) ;
112-
113- if ( mode === 'replace' ) {
114- await ChromaManager . client . deleteCollection ( { name : collection . name } ) ;
115-
116- if ( isMemoryBackup ) {
117- ChromaManager . memoryCollection = null ;
118- collection = await ChromaManager . getMemoryCollection ( ) ;
119- } else {
120- ChromaManager . summaryCollection = null ;
121- collection = await ChromaManager . getSummaryCollection ( ) ;
113+ if ( ! await fs . pathExists ( filePath ) ) {
114+ throw new Error ( `Backup file not found at ${ filePath } ` ) ;
122115 }
123116
124- logger . log ( 'Replaced mode: existing collection cleared and recreated.' ) ;
125- }
117+ // Determine which collection to import into based on filename
118+ const isMemoryBackup = path . basename ( filePath ) . startsWith ( 'memory-backup' ) ;
119+ let collection = isMemoryBackup
120+ ? await ChromaManager . getMemoryCollection ( )
121+ : await ChromaManager . getSummaryCollection ( ) ;
126122
127- const fileStream = fs . createReadStream ( filePath ) ;
128- const rl = readline . createInterface ( { input : fileStream , crlfDelay : Infinity } ) ;
129- const records = [ ] ;
123+ if ( mode === 'replace' ) {
124+ await ChromaManager . client . deleteCollection ( { name : collection . name } ) ;
130125
131- for await ( const line of rl ) {
132- records . push ( JSON . parse ( line ) ) ;
133- }
126+ if ( isMemoryBackup ) {
127+ ChromaManager . memoryCollection = null ;
128+ collection = await ChromaManager . getMemoryCollection ( ) ;
129+ } else {
130+ ChromaManager . summaryCollection = null ;
131+ collection = await ChromaManager . getSummaryCollection ( ) ;
132+ }
134133
135- if ( records . length === 0 ) {
136- return { message : 'No records found in backup file to import.' } ;
137- }
134+ logger . log ( 'Replaced mode: existing collection cleared and recreated.' ) ;
135+ }
136+
137+ const fileStream = fs . createReadStream ( filePath ) ;
138+ const rl = readline . createInterface ( { input : fileStream , crlfDelay : Infinity } ) ;
139+ const records = [ ] ;
138140
139- logger . log ( `Importing ${ records . length } documents into ${ collection . name } ...` ) ;
141+ for await ( const line of rl ) {
142+ records . push ( JSON . parse ( line ) ) ;
143+ }
140144
141- await collection . upsert ( {
142- ids : records . map ( r => r . id ) ,
143- embeddings : records . map ( r => r . embedding ) ,
144- metadatas : records . map ( r => r . metadata ) ,
145- documents : records . map ( r => r . document )
146- } ) ;
145+ if ( records . length === 0 ) {
146+ return { message : 'No records found in backup file to import.' } ;
147+ }
147148
148- const count = await collection . count ( ) ;
149- logger . log ( `Import complete. Collection "${ collection . name } " now contains ${ count } documents.` ) ;
150- return { imported : records . length , total : count , mode } ;
149+ logger . log ( `Importing ${ records . length } documents into ${ collection . name } ...` ) ;
150+
151+ await collection . upsert ( {
152+ ids : records . map ( r => r . id ) ,
153+ embeddings : records . map ( r => r . embedding ) ,
154+ metadatas : records . map ( r => r . metadata ) ,
155+ documents : records . map ( r => r . document )
156+ } ) ;
157+
158+ const count = await collection . count ( ) ;
159+ logger . log ( `Import complete. Collection "${ collection . name } " now contains ${ count } documents.` ) ;
160+ return { imported : records . length , total : count , mode } ;
161+ } catch ( error ) {
162+ logger . error ( '[DatabaseService] Error importing database:' , error ) ;
163+ return {
164+ error : 'Failed to import database' ,
165+ message : error . message ,
166+ code : 'DATABASE_IMPORT_ERROR'
167+ } ;
168+ }
151169 }
152170}
153171
0 commit comments