/** * brainbank docs — Index document collections * brainbank dsearch — Search documents only */ import { c, args, getFlag, stripFlags, findDocsPlugin } from '@/cli/utils.ts'; import { createBrain } from '@/cli/factory/index.ts'; export async function cmdDocs(): Promise { const collection = getFlag('collection'); const brain = await createBrain(); console.log(c.bold('\n━━━ BrainBank Docs Index ━━━\n')); const opts: { collections?: string[]; onProgress?: (collection: string, file: string, current: number, total: number) => void } = {}; if (collection) opts.collections = [collection]; opts.onProgress = (col: string, file: string, cur: number, total: number) => { process.stdout.write(`\r ${c.cyan(col)} [${cur}/${total}] ${file} `); }; const docsPlugin = findDocsPlugin(brain); if (!docsPlugin) { console.log(c.red(' Docs plugin not loaded. Install @brainbank/docs.')); process.exit(1); } const results = await docsPlugin.indexDocs(opts); console.log('\n'); for (const [name, stat] of Object.entries(results) as [string, { indexed: number; skipped: number; removed: number; chunks: number }][]) { const removedStr = stat.removed > 0 ? `, ${c.red(String(stat.removed) + ' removed')}` : ''; console.log(` ${c.green(name)}: ${stat.indexed} indexed, ${stat.skipped} skipped${removedStr}, ${stat.chunks} chunks`); } brain.close(); } export async function cmdDocSearch(): Promise { const query = stripFlags(args).slice(1).join(' '); if (!query) { console.log(c.red('Usage: brainbank dsearch ')); process.exit(1); } const brain = await createBrain(); const collection = getFlag('collection'); const k = parseInt(getFlag('k') || '8', 10); console.log(c.bold(`\n━━━ BrainBank Doc Search: "${query}" ━━━\n`)); const docsPlugin = findDocsPlugin(brain); if (!docsPlugin) { console.log(c.red('Docs plugin not loaded. Install @brainbank/docs.')); process.exit(1); } const results = await docsPlugin.search(query, { collection: collection ?? undefined, k }); if (results.length === 0) { console.log(c.yellow(' No results found.')); brain.close(); return; } for (const r of results) { const score = Math.round(r.score * 100); const ctx = r.context ? ` — ${c.dim(r.context)}` : ''; console.log(`${c.magenta(`[DOC ${score}%]`)} ${c.bold(r.filePath!)} [${r.type === 'document' ? r.metadata.collection ?? '' : ''}]${ctx}`); const preview = r.content.split('\n').slice(0, 4).join('\n'); console.log(c.dim(preview)); console.log(''); } brain.close(); }