MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / DirectImporter

Class DirectImporter

apps/cli/src/sync/adapters.ts:52–147  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

50// ==================== DirectImporter ====================
51
52export class DirectImporter implements DataImporter {
53 private dbManager: DatabaseManager
54 private logger: SyncLogger
55
56 constructor(dbManager: DatabaseManager, logger?: SyncLogger) {
57 this.dbManager = dbManager
58 this.logger = logger ?? NOOP_LOGGER
59 }
60
61 sessionExists(sessionId: string): boolean {
62 const dbPath = this.dbManager.getDbPath(sessionId)
63 if (!fs.existsSync(dbPath)) return false
64 try {
65 const db = this.dbManager.openRawSessionDatabase(sessionId, { readonly: true })
66 const row = db
67 .prepare("SELECT COUNT(*) as cnt FROM sqlite_master WHERE type='table' AND name='message'")
68 .get() as { cnt: number }
69 db.close()
70 if (row.cnt === 0) {
71 this.logger.warn(`[DirectImporter] DB file exists but has no message table: ${sessionId}, removing`)
72 try {
73 fs.unlinkSync(dbPath)
74 } catch {
75 /* ignore */
76 }
77 return false
78 }
79 return true
80 } catch (error) {
81 if (error instanceof DataDirCompatibilityError) throw error
82
83 this.logger.warn(`[DirectImporter] Cannot validate DB file: ${sessionId}, removing`)
84 try {
85 fs.unlinkSync(dbPath)
86 } catch {
87 /* ignore */
88 }
89 return false
90 }
91 }
92
93 async importFile(tempFile: string, targetSessionId: string | undefined, externalId: string): Promise<ImportResult> {
94 if (targetSessionId && this.sessionExists(targetSessionId)) {
95 return this.incrementalImportFile(targetSessionId, tempFile)
96 }
97
98 if (targetSessionId) {
99 this.logger.info(`[DirectImporter] Session ${targetSessionId} not found locally, need full resync`)
100 return { success: false, newMessageCount: 0, needFullResync: true }
101 }
102
103 return this.fullImportFile(tempFile, externalId)
104 }
105
106 private async incrementalImportFile(sessionId: string, tempFile: string): Promise<ImportResult> {
107 try {
108 this.dbManager.close(sessionId)
109 const result = await incrementalImport(this.dbManager, sessionId, tempFile)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected