| 23 | // ── Command Registration ─────────────────────────────────── |
| 24 | |
| 25 | export function registerDocCommand(program: Command) { |
| 26 | const doc = program.command('doc').description('Manage documents'); |
| 27 | |
| 28 | // ── list ────────────────────────────────────────────── |
| 29 | |
| 30 | doc |
| 31 | .command('list') |
| 32 | .description('List documents') |
| 33 | .option('-L, --limit <n>', 'Maximum number of items to fetch', '30') |
| 34 | .option('--file-type <type>', 'Filter by file type') |
| 35 | .option('--source-type <type>', 'Filter by source type (file, web, api, topic)') |
| 36 | .option('--json [fields]', 'Output JSON, optionally specify fields (comma-separated)') |
| 37 | .action( |
| 38 | async (options: { |
| 39 | fileType?: string; |
| 40 | json?: string | boolean; |
| 41 | limit?: string; |
| 42 | sourceType?: string; |
| 43 | }) => { |
| 44 | const client = await getTrpcClient(); |
| 45 | const pageSize = Number.parseInt(options.limit || '30', 10); |
| 46 | |
| 47 | const query: { fileTypes?: string[]; pageSize: number; sourceTypes?: string[] } = { |
| 48 | pageSize, |
| 49 | }; |
| 50 | if (options.fileType) query.fileTypes = [options.fileType]; |
| 51 | if (options.sourceType) query.sourceTypes = [options.sourceType]; |
| 52 | const result = await client.document.queryDocuments.query(query); |
| 53 | const docs = Array.isArray(result) ? result : ((result as any).items ?? []); |
| 54 | |
| 55 | if (options.json !== undefined) { |
| 56 | const fields = typeof options.json === 'string' ? options.json : undefined; |
| 57 | outputJson(docs, fields); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | if (docs.length === 0) { |
| 62 | console.log('No documents found.'); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | const rows = docs.map((d: any) => [ |
| 67 | d.id, |
| 68 | truncate(d.title || d.filename || 'Untitled', 120), |
| 69 | d.fileType || '', |
| 70 | d.updatedAt ? timeAgo(d.updatedAt) : '', |
| 71 | ]); |
| 72 | |
| 73 | printTable(rows, ['ID', 'TITLE', 'TYPE', 'UPDATED']); |
| 74 | }, |
| 75 | ); |
| 76 | |
| 77 | // ── view ────────────────────────────────────────────── |
| 78 | |
| 79 | doc |
| 80 | .command('view <id>') |
| 81 | .description('View a document') |
| 82 | .option('--json [fields]', 'Output JSON, optionally specify fields (comma-separated)') |