Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·547 lines (499 loc) · 18 KB
/
Copy pathcli.js
File metadata and controls
executable file
·547 lines (499 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#!/usr/bin/env node
const { Command } = require('commander');
const program = new Command();
program
.name('localllm')
.description('Local LLM infrastructure hub')
.version('1.0.0');
// Embeddings
program
.command('embed <text>')
.description('Generate embedding for text')
.option('-m, --model <model>', 'Embedding model', 'mxbai-embed-large')
.action(async (text, options) => {
const { embed } = require('./packages/embeddings');
try {
const embedding = await embed(text, options.model);
console.log(JSON.stringify(embedding));
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
program
.command('batch-embed <texts...>')
.description('Generate embeddings for multiple texts')
.option('-m, --model <model>', 'Embedding model', 'mxbai-embed-large')
.action(async (texts, options) => {
const { batchEmbed } = require('./packages/embeddings');
try {
const embeddings = await batchEmbed(texts, options.model);
console.log(JSON.stringify(embeddings));
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
program
.command('compare <textA> <textB>')
.description('Compare similarity between two texts')
.option('-m, --model <model>', 'Embedding model', 'mxbai-embed-large')
.action(async (textA, textB, options) => {
const { compare } = require('./packages/embeddings');
try {
const similarity = await compare(textA, textB, options.model);
console.log(`Similarity: ${similarity.toFixed(4)}`);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
// Classifier
program
.command('classify')
.description('Classify an email')
.option('--from <email>', 'From email address', '')
.option('--subject <subject>', 'Email subject', '')
.option('--body <body>', 'Email body', '')
.option('--labels <labels>', 'Comma-separated labels', '')
.action(async (options) => {
const { classify } = require('./packages/classifier');
try {
const email = {
from: options.from,
subject: options.subject,
body: options.body,
labels: options.labels ? options.labels.split(',') : []
};
const result = await classify(email);
console.log(JSON.stringify(result, null, 2));
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
// Triage
program
.command('triage <text>')
.description('Rate urgency of a message (1-5)')
.action(async (text) => {
const { rateUrgency } = require('./packages/triage');
try {
const result = await rateUrgency(text);
console.log(JSON.stringify(result, null, 2));
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
program
.command('route <text>')
.description('Route task to local or API')
.action(async (text) => {
const { routeTask } = require('./packages/triage');
try {
const result = await routeTask(text);
console.log(JSON.stringify(result, null, 2));
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
// Transcriber
program
.command('transcribe <file>')
.description('Transcribe audio file')
.option('-m, --model <model>', 'Whisper model', 'base')
.option('-l, --language <lang>', 'Language code', 'auto')
.option('-t, --threads <number>', 'Number of threads')
.action(async (file, options) => {
const { transcribe } = require('./packages/transcriber');
try {
const result = await transcribe(file, options);
console.log(result.text);
console.error(`\n[Transcribed in ${result.duration}ms]`);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
program
.command('transcribe-batch <directory>')
.description('Transcribe all audio files in directory')
.option('-m, --model <model>', 'Whisper model', 'base')
.option('-l, --language <lang>', 'Language code', 'auto')
.option('-o, --output <file>', 'Output JSON file')
.action(async (directory, options) => {
const { batchTranscribe } = require('./packages/transcriber');
try {
const results = await batchTranscribe(directory, options);
if (options.output) {
require('fs').writeFileSync(options.output, JSON.stringify(results, null, 2));
console.log(`Results saved to ${options.output}`);
} else {
console.log(JSON.stringify(results, null, 2));
}
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
// Search
program
.command('search <query>')
.description('Semantic search over indexed content')
.option('-d, --db <path>', 'Database path')
.option('-k, --top-k <number>', 'Number of results', '5')
.action(async (query, options) => {
const { search } = require('./packages/search');
const { homedir } = require('os');
const { join } = require('path');
const dbPath = options.db || join(homedir(), 'clawd/scripts/memory.db');
try {
const results = await search(query, dbPath, parseInt(options.topK));
console.log('\nResults:\n');
for (const result of results) {
console.log(`[${result.score.toFixed(3)}] ${result.file}:${result.startLine}-${result.endLine}`);
console.log(` ${result.text.slice(0, 200).replace(/\n/g, ' ')}...`);
console.log();
}
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
program
.command('reindex')
.description('Rebuild search index')
.option('-s, --source <path>', 'Source directory')
.option('-d, --db <path>', 'Database path')
.action(async (options) => {
const { indexDirectory } = require('./packages/search/indexer');
const config = require('./shared/config');
const { homedir } = require('os');
const { join } = require('path');
const source = options.source || config.paths.memoryDir;
const dbPath = options.db || join(homedir(), 'clawd/scripts/memory.db');
try {
await indexDirectory(source, dbPath);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
// Chat Ingest
const chat = program
.command('chat')
.description('Chat transcript ingestion and search');
chat
.command('ingest')
.description('Ingest all Clawdbot chat transcripts')
.option('-d, --db <path>', 'Database path')
.option('-s, --sessions <path>', 'Sessions directory')
.action(async (options) => {
const { ingestAll } = require('./packages/chat-ingest/ingest');
try {
const total = await ingestAll(options.db, options.sessions);
console.log(`\nDone: ${total} new chunks ingested.`);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
chat
.command('reindex')
.description('Clear existing chat index and re-ingest from scratch (applies new content filters)')
.option('-d, --db <path>', 'Database path')
.option('-s, --sessions <path>', 'Sessions directory')
.option('-y, --yes', 'Confirm deletion (REQUIRED)')
.action(async (options) => {
const { existsSync } = require('fs');
const Database = require('better-sqlite3');
const config = require('./shared/config');
const { ingestAll } = require('./packages/chat-ingest/ingest');
const dbPath = options.db || config.paths.chatDb;
if (!options.yes) {
console.error('\n⚠️ Chat Reindex requires --yes flag to confirm deletion.\n');
console.error('This will DELETE all existing chat chunks and re-ingest from scratch.');
console.error('New filters: assistant-only, length>100, no tool output, ANSI stripped\n');
console.error('Usage: node cli.js chat reindex --yes\n');
process.exit(1);
}
try {
if (existsSync(dbPath)) {
console.log('Clearing existing chat chunks...');
const db = new Database(dbPath);
const beforeCount = db.prepare('SELECT COUNT(*) as count FROM chat_chunks').get();
console.log(` Deleting ${beforeCount.count} existing chunks...`);
// Note: db.exec() is SQLite execution, not shell execution (safe)
db.prepare('DELETE FROM chat_chunks').run();
db.prepare('DELETE FROM ingest_progress').run();
db.close();
console.log('✓ Cleared');
}
console.log('\nRe-ingesting with Tier 2 content filters...');
console.log(' Filters: assistant-only, length>100 chars, no tools, ANSI stripped\n');
const total = await ingestAll(options.db, options.sessions);
console.log(`\n✓ Done: ${total} chunks ingested\n`);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
chat
.command('search <query>')
.description('Search chat history')
.option('-d, --db <path>', 'Database path')
.option('-k, --top-k <number>', 'Number of results', '5')
.action(async (query, options) => {
const { chatSearch } = require('./packages/chat-ingest/search');
try {
const results = await chatSearch(query, options.db, parseInt(options.topK));
console.log('\nChat search results:\n');
for (const r of results) {
const ts = r.startTs ? new Date(r.startTs).toLocaleString() : 'unknown';
console.log(`[${r.score.toFixed(3)}] session:${r.sessionId.slice(0, 8)} ${ts}`);
console.log(` ${r.text.slice(0, 200).replace(/\n/g, ' | ')}...`);
console.log();
}
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
chat
.command('watch')
.description('Watch for new chat messages and ingest continuously')
.option('-d, --db <path>', 'Database path')
.option('-s, --sessions <path>', 'Sessions directory')
.action((options) => {
const { startWatcher } = require('./packages/chat-ingest/watcher');
startWatcher(options.db, options.sessions);
});
chat
.command('search-all <query>')
.description('Unified search: memory + chat sessions + Telegram')
.option('-k, --top-k <number>', 'Number of results', '10')
.option('-s, --sources <list>', 'Comma-separated sources: memory,chat,telegram', 'memory,chat,telegram')
.action(async (query, options) => {
const { unifiedSearch } = require('./packages/chat-ingest/unified-search');
try {
const results = await unifiedSearch(query, {
topK: parseInt(options.topK),
sources: options.sources.split(','),
});
console.log(`\n🔍 Unified search: "${query}" (${results.length} results)\n`);
for (const r of results) {
const tag = r.source === 'memory' ? `📝 ${r.meta.file}:${r.meta.startLine}`
: r.source === 'chat' ? `💬 session:${r.meta.sessionId?.slice(0, 8)} ${r.meta.startTs ? new Date(r.meta.startTs).toLocaleString() : ''}`
: `📱 telegram ${r.meta.startTs ? new Date(r.meta.startTs).toLocaleString() : ''}`;
console.log(`[${r.score.toFixed(3)}] ${tag}`);
console.log(` ${r.text.slice(0, 200).replace(/\n/g, ' | ')}...`);
console.log();
}
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
chat
.command('status')
.description('Show ingestion stats')
.option('-d, --db <path>', 'Database path')
.action((options) => {
const { existsSync } = require('fs');
const Database = require('better-sqlite3');
const config = require('./shared/config');
const dbPath = options.db || config.paths.chatDb;
if (!existsSync(dbPath)) {
console.log('No database found. Run "chat ingest" first.');
return;
}
const db = new Database(dbPath);
const chunkCount = db.prepare('SELECT COUNT(*) as count FROM chat_chunks').get();
const fileCount = db.prepare('SELECT COUNT(*) as count FROM ingest_progress').get();
const lastUpdate = db.prepare('SELECT MAX(last_timestamp) as ts FROM ingest_progress').get();
const sessionCount = db.prepare('SELECT COUNT(DISTINCT session_id) as count FROM chat_chunks').get();
console.log('\n📊 Chat Ingest Status');
console.log(` Files indexed: ${fileCount.count}`);
console.log(` Sessions: ${sessionCount.count}`);
console.log(` Total chunks: ${chunkCount.count}`);
console.log(` Last update: ${lastUpdate.ts || 'never'}`);
console.log(` Database: ${dbPath}`);
console.log();
db.close();
});
// Librarian
program
.command('prefetch <query>')
.description('Pre-fetch context from memory + chat + telegram')
.option('-k, --top-k <number>', 'Results per source', '5')
.option('-s, --sources <list>', 'Comma-separated sources', 'memory,chat,telegram')
.option('--include-grep', 'Include grep results', false)
.option('--format <type>', 'Output format: json|markdown', 'json')
.action(async (query, options) => {
const { prefetchContext, formatAsMarkdown } = require('./packages/librarian');
try {
const result = await prefetchContext(query, {
topK: parseInt(options.topK),
sources: options.sources.split(','),
includeGrep: options.includeGrep,
});
if (options.format === 'markdown') {
console.log(formatAsMarkdown(result.blocks));
console.error(`\n[Fetched ${result.summary.totalResults} blocks in ${result.summary.queryTime}ms]`);
} else {
console.log(JSON.stringify(result, null, 2));
}
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
// Pipelines
const pipeline = program
.command('pipeline')
.description('Multi-step AI pipelines');
pipeline
.command('email-triage')
.description('Email triage pipeline: classify → urgency → route → notify')
.option('--from <email>', 'From email address', '')
.option('--subject <subject>', 'Email subject', '')
.option('--body <body>', 'Email body', '')
.option('--labels <labels>', 'Comma-separated labels', '')
.option('--threshold <number>', 'Urgency notification threshold (1-5)', '4')
.action(async (options) => {
const { handleEmailTriage } = require('./packages/pipelines/cli');
try {
await handleEmailTriage(options);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
pipeline
.command('voice-memo <file>')
.description('Voice memo ingestion pipeline: transcribe → embed → store')
.option('--context', 'Retrieve related context from memory', false)
.option('--top-k <number>', 'Number of context results', '3')
.action(async (file, options) => {
const { handleVoiceMemo } = require('./packages/pipelines/cli');
try {
await handleVoiceMemo(file, options);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
pipeline
.command('history')
.description('View pipeline run history')
.option('-p, --pipeline <name>', 'Filter by pipeline name')
.option('-l, --limit <number>', 'Max results', '50')
.action(async (options) => {
const { handleHistory } = require('./packages/pipelines/cli');
try {
await handleHistory(options);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
pipeline
.command('stats')
.description('View pipeline statistics')
.action(async () => {
const { handleStats } = require('./packages/pipelines/cli');
try {
await handleStats();
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
// Autonomous Agent
const agent = program
.command('agent')
.description('Autonomous agent control (DORMANT - requires explicit activation)');
agent
.command('status')
.description('Show agent status')
.action(async () => {
const { execFile } = require('child_process');
const { promisify } = require('util');
const execFileAsync = promisify(execFile);
const { join } = require('path');
try {
const { stdout } = await execFileAsync('node', [
join(__dirname, 'packages/autonomous-agent/src/index.js'),
'status'
]);
console.log(stdout);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
agent
.command('start')
.description('Start the autonomous agent')
.option('--live', 'Run in LIVE mode (default: dry-run)', false)
.action(async (options) => {
console.log('⚠️ AUTONOMOUS AGENT SYSTEM');
console.log(' This is DORMANT code - activation requires explicit user approval');
console.log(' Review DESIGN.md and safety controls before proceeding\n');
const mode = options.live ? 'LIVE' : 'DRY-RUN';
console.log(`Starting in ${mode} mode...\n`);
const { spawn } = require('child_process');
const { join } = require('path');
const args = ['packages/autonomous-agent/src/index.js'];
if (!options.live) {
args.push('--dry-run');
}
const child = spawn('node', args, {
cwd: __dirname,
stdio: 'inherit'
});
child.on('exit', (code) => {
process.exit(code);
});
});
agent
.command('health')
.description('Run health check')
.action(async () => {
const { execFile } = require('child_process');
const { promisify } = require('util');
const execFileAsync = promisify(execFile);
const { join } = require('path');
try {
const { stdout } = await execFileAsync('node', [
join(__dirname, 'packages/autonomous-agent/src/index.js'),
'health'
]);
console.log(stdout);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
});
// Dashboard
program
.command('dashboard')
.description('Start the monitoring dashboard')
.option('-p, --port <port>', 'Port number', '3847')
.action((options) => {
process.env.DASHBOARD_PORT = options.port;
const { start } = require('./packages/dashboard/server.cjs');
start();
});
// API Server
program
.command('api-server')
.description('Start the REST API server')
.option('-p, --port <port>', 'Port number', '3848')
.action((options) => {
const { start } = require('./packages/api-server');
start(parseInt(options.port));
});
program.parse();