fix: make FTS5 optional for template search (fixes Claude Desktop compatibility)
- Added runtime FTS5 detection in database adapters - Removed FTS5 from required schema to prevent "no such module" errors - FTS5 tables/triggers created conditionally only if supported - Template search automatically falls back to LIKE when FTS5 unavailable - Works in ALL SQLite environments (Claude Desktop, restricted envs, etc.) This ensures search_templates() works correctly regardless of SQLite build, while still providing optimal performance when FTS5 is available. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -23,9 +23,57 @@ export interface StoredTemplate {
|
||||
|
||||
export class TemplateRepository {
|
||||
private sanitizer: TemplateSanitizer;
|
||||
private hasFTS5Support: boolean = false;
|
||||
|
||||
constructor(private db: DatabaseAdapter) {
|
||||
this.sanitizer = new TemplateSanitizer();
|
||||
this.initializeFTS5();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize FTS5 tables if supported
|
||||
*/
|
||||
private initializeFTS5(): void {
|
||||
this.hasFTS5Support = this.db.checkFTS5Support();
|
||||
|
||||
if (this.hasFTS5Support) {
|
||||
try {
|
||||
// Create FTS5 virtual table
|
||||
this.db.exec(`
|
||||
CREATE VIRTUAL TABLE IF NOT EXISTS templates_fts USING fts5(
|
||||
name, description, content=templates
|
||||
);
|
||||
`);
|
||||
|
||||
// Create triggers to keep FTS5 in sync
|
||||
this.db.exec(`
|
||||
CREATE TRIGGER IF NOT EXISTS templates_ai AFTER INSERT ON templates BEGIN
|
||||
INSERT INTO templates_fts(rowid, name, description)
|
||||
VALUES (new.id, new.name, new.description);
|
||||
END;
|
||||
`);
|
||||
|
||||
this.db.exec(`
|
||||
CREATE TRIGGER IF NOT EXISTS templates_au AFTER UPDATE ON templates BEGIN
|
||||
UPDATE templates_fts SET name = new.name, description = new.description
|
||||
WHERE rowid = new.id;
|
||||
END;
|
||||
`);
|
||||
|
||||
this.db.exec(`
|
||||
CREATE TRIGGER IF NOT EXISTS templates_ad AFTER DELETE ON templates BEGIN
|
||||
DELETE FROM templates_fts WHERE rowid = old.id;
|
||||
END;
|
||||
`);
|
||||
|
||||
logger.info('FTS5 support enabled for template search');
|
||||
} catch (error) {
|
||||
logger.warn('Failed to initialize FTS5 for templates:', error);
|
||||
this.hasFTS5Support = false;
|
||||
}
|
||||
} else {
|
||||
logger.info('FTS5 not available, using LIKE search for templates');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,16 +158,41 @@ export class TemplateRepository {
|
||||
* Search templates by name or description
|
||||
*/
|
||||
searchTemplates(query: string, limit: number = 20): StoredTemplate[] {
|
||||
// Use FTS for search
|
||||
const ftsQuery = query.split(' ').map(term => `"${term}"`).join(' OR ');
|
||||
// If FTS5 is not supported, go straight to LIKE search
|
||||
if (!this.hasFTS5Support) {
|
||||
return this.searchTemplatesLIKE(query, limit);
|
||||
}
|
||||
|
||||
try {
|
||||
// Use FTS for search
|
||||
const ftsQuery = query.split(' ').map(term => `"${term}"`).join(' OR ');
|
||||
|
||||
return this.db.prepare(`
|
||||
SELECT t.* FROM templates t
|
||||
JOIN templates_fts ON t.id = templates_fts.rowid
|
||||
WHERE templates_fts MATCH ?
|
||||
ORDER BY rank, t.views DESC
|
||||
LIMIT ?
|
||||
`).all(ftsQuery, limit) as StoredTemplate[];
|
||||
} catch (error: any) {
|
||||
// If FTS5 query fails, fallback to LIKE search
|
||||
logger.warn('FTS5 template search failed, using LIKE fallback:', error.message);
|
||||
return this.searchTemplatesLIKE(query, limit);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fallback search using LIKE when FTS5 is not available
|
||||
*/
|
||||
private searchTemplatesLIKE(query: string, limit: number = 20): StoredTemplate[] {
|
||||
const likeQuery = `%${query}%`;
|
||||
|
||||
return this.db.prepare(`
|
||||
SELECT t.* FROM templates t
|
||||
JOIN templates_fts ON t.id = templates_fts.rowid
|
||||
WHERE templates_fts MATCH ?
|
||||
ORDER BY rank, t.views DESC
|
||||
SELECT * FROM templates
|
||||
WHERE name LIKE ? OR description LIKE ?
|
||||
ORDER BY views DESC, created_at DESC
|
||||
LIMIT ?
|
||||
`).all(ftsQuery, limit) as StoredTemplate[];
|
||||
`).all(likeQuery, likeQuery, limit) as StoredTemplate[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -208,4 +281,32 @@ export class TemplateRepository {
|
||||
this.db.exec('DELETE FROM templates');
|
||||
logger.info('Cleared all templates from database');
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebuild the FTS5 index for all templates
|
||||
* This is needed when templates are bulk imported or when FTS5 gets out of sync
|
||||
*/
|
||||
rebuildTemplateFTS(): void {
|
||||
// Skip if FTS5 is not supported
|
||||
if (!this.hasFTS5Support) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Clear existing FTS data
|
||||
this.db.exec('DELETE FROM templates_fts');
|
||||
|
||||
// Repopulate from templates table
|
||||
this.db.exec(`
|
||||
INSERT INTO templates_fts(rowid, name, description)
|
||||
SELECT id, name, description FROM templates
|
||||
`);
|
||||
|
||||
const count = this.getTemplateCount();
|
||||
logger.info(`Rebuilt FTS5 index for ${count} templates`);
|
||||
} catch (error) {
|
||||
logger.warn('Failed to rebuild template FTS5 index:', error);
|
||||
// Non-critical error - search will fallback to LIKE
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -132,6 +132,11 @@ export class TemplateService {
|
||||
}
|
||||
|
||||
logger.info(`Successfully saved ${saved} templates to database`);
|
||||
|
||||
// Rebuild FTS5 index after bulk import
|
||||
logger.info('Rebuilding FTS5 index for templates');
|
||||
this.repository.rebuildTemplateFTS();
|
||||
|
||||
progressCallback?.('Complete', saved, saved);
|
||||
} catch (error) {
|
||||
logger.error('Error fetching templates:', error);
|
||||
|
||||
Reference in New Issue
Block a user