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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! XORE CLI - 命令行入口
// 全局内存分配器配置 - 使用 mimalloc 提升性能
#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
use clap::{Parser, Subcommand};
use xore_core::{print_anyhow_error, LogConfig};
mod commands;
mod ui;
use commands::{abyss, agent, benchmark, config, find, peek, process, watch};
/// XORE - 搜索和数据处理一体化工具
#[derive(Parser)]
#[command(name = "xore")]
#[command(author = "XORE Team")]
#[command(version = env!("CARGO_PKG_VERSION"))]
#[command(about = "Explore the Abyss, Extract the Core", long_about = None)]
struct Cli {
/// 详细输出模式
#[arg(short, long, global = true)]
verbose: bool,
/// 静默模式
#[arg(short, long)]
quiet: bool,
/// 禁用彩色输出
#[arg(long)]
no_color: bool,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Agent 优化功能(Agent-Native)
#[command(alias = "a")]
Agent {
#[command(subcommand)]
subcommand: AgentCommands,
},
/// 文件监控守护进程管理
Watch {
#[command(subcommand)]
subcommand: WatchCommands,
},
/// 查找文件(搜索功能)
#[command(alias = "f")]
Find {
/// 查询字符串(可选,如不提供则只扫描文件)
query: Option<String>,
/// 搜索路径
#[arg(long, default_value = ".")]
path: String,
/// 文件类型过滤(csv, json, log, code, text, parquet, 或逗号分隔的扩展名)
#[arg(long, short = 't')]
r#type: Option<String>,
/// 文件大小过滤(例如:">1MB", "<500KB", "1MB-10MB")
#[arg(long, short = 's', allow_hyphen_values = true)]
size: Option<String>,
/// 修改时间过滤(例如:"-7d" 过去7天, "+30d" 超过30天, "2024-01-01")
#[arg(long, short = 'm', allow_hyphen_values = true)]
mtime: Option<String>,
/// 最大遍历深度
#[arg(long, short = 'd')]
max_depth: Option<usize>,
/// 包含隐藏文件
#[arg(long)]
hidden: bool,
/// 不遵守 .gitignore 规则
#[arg(long)]
no_ignore: bool,
/// 跟随符号链接
#[arg(long, short = 'L')]
follow_links: bool,
/// 并行线程数(默认自动检测)
#[arg(long, short = 'j')]
threads: Option<usize>,
/// 启用语义搜索
#[arg(long)]
semantic: bool,
/// 启用全文索引搜索模式
#[arg(long, short = 'i')]
index: bool,
/// 强制重建索引
#[arg(long)]
rebuild: bool,
/// 指定索引目录路径
#[arg(long)]
index_dir: Option<String>,
/// 启用文件监控模式(增量索引)
#[arg(long, short = 'w')]
watch: bool,
/// 以后台守护进程模式启动文件监控(P2.2)
#[arg(long)]
watch_daemon: bool,
/// 守护进程监控的包含模式(逗号分隔扩展名,例如 "*.rs,*.toml")
#[arg(long)]
watch_include: Option<String>,
/// 守护进程监控的排除模式(逗号分隔目录,例如 "target,node_modules")
#[arg(long)]
watch_exclude: Option<String>,
/// 显示搜索历史
#[arg(long)]
history: bool,
/// 显示智能推荐
#[arg(long)]
recommend: bool,
/// 清除搜索历史
#[arg(long)]
clear_history: bool,
/// 输出格式: raw (默认), json, agent-json, agent-md
#[arg(long, short = 'o')]
output: Option<String>,
/// 限制输出 token 数量
#[arg(long)]
max_tokens: Option<usize>,
},
/// 处理数据
#[command(alias = "p")]
Process {
/// 文件路径
file: String,
/// SQL查询
query: Option<String>,
/// 数据质量检测
#[arg(long)]
quality_check: bool,
/// 输出文件路径(支持 csv, json, parquet 格式)
#[arg(long, short = 'o')]
output: Option<String>,
/// 导出格式(如果不指定,从输出文件扩展名推断)
#[arg(long, short = 'f')]
format: Option<String>,
},
/// 性能基准测试
#[command(alias = "bench")]
Benchmark {
/// 测试套件 (all, scan, search, process, io, alloc)
#[arg(long, short = 's', default_value = "all")]
suite: benchmark::BenchmarkSuite,
/// 输出格式 (text, json, csv)
#[arg(long, short = 'o', default_value = "text")]
output: benchmark::OutputFormat,
/// 迭代次数
#[arg(long, short = 'n', default_value = "3")]
iterations: usize,
/// 测试数据路径
#[arg(long)]
data_path: Option<String>,
/// 预热次数
#[arg(long, default_value = "1")]
warmup: usize,
},
/// 管理全局配置
Config {
#[command(subcommand)]
subcommand: ConfigCommands,
},
}
#[derive(Subcommand)]
enum ConfigCommands {
/// 显示当前配置
Show,
/// 获取配置项的值
Get {
/// 配置项名称(例如:paths.index, search.max_file_size_mb)
key: String,
},
/// 设置配置项的值
Set {
/// 配置项名称
key: String,
/// 配置项新值
value: String,
},
/// 重置配置为默认值
Reset,
/// 编辑配置文件
Edit,
}
#[derive(Subcommand)]
enum AgentCommands {
/// 生成 Agent 初始化 Prompt
Init {
/// 目标模型 (gpt-4, claude, ollama, deepseek)
#[arg(long, default_value = "gpt-4")]
model: String,
/// 输出格式: mcp (默认), openai, langchain, openapi
#[arg(long, default_value = "mcp")]
format: String,
},
/// 获取数据结构(不读全文)
Schema {
/// 文件路径
file: String,
/// 显示分布直方图
#[arg(long)]
histogram: bool,
/// JSON 格式输出
#[arg(long)]
json: bool,
/// 压缩 JSON(无空格)
#[arg(long)]
minify: bool,
/// 注入当前会话上下文到输出
#[arg(long)]
with_context: bool,
},
/// 智能采样数据
Sample {
/// 文件路径
file: String,
/// 采样行数
#[arg(short = 'n', long, default_value = "5")]
rows: usize,
/// 采样策略 (random, head, tail, smart)
#[arg(long, default_value = "smart")]
strategy: String,
/// JSON 格式输出
#[arg(long)]
json: bool,
/// 注入当前会话上下文到输出
#[arg(long)]
with_context: bool,
},
/// 执行 SQL 查询
Query {
/// 文件路径
file: String,
/// SQL 查询语句
sql: String,
/// 输出格式 (json, csv, table)
#[arg(long, short = 'f', default_value = "json")]
format: String,
/// 压缩 JSON(无空格)
#[arg(long)]
minify: bool,
/// 返回行数限制
#[arg(long)]
limit: Option<usize>,
/// 注入当前会话上下文到输出
#[arg(long)]
with_context: bool,
},
/// SQL 错误分析
Explain {
/// SQL 语句
sql: String,
},
/// 会话上下文管理
Context {
#[command(subcommand)]
subcommand: ContextCommands,
},
/// 目录扫描与智能预览(P2.1)
Peek {
/// 目标目录
directory: String,
/// 读取指定文件
#[arg(long)]
file: Option<String>,
/// 禁用缓存(强制重新扫描)
#[arg(long)]
no_cache: bool,
/// 输出格式:json(默认)| tree | md
#[arg(long, short = 'o', default_value = "json")]
output: String,
/// 最大扫描深度(默认 5)
#[arg(long, short = 'd')]
max_depth: Option<usize>,
/// 包含模式(逗号分隔的扩展名,例如 "*.rs,*.toml")
#[arg(long)]
include: Option<String>,
/// 排除模式(逗号分隔,例如 "target,node_modules")
#[arg(long)]
exclude: Option<String>,
},
/// 全局文件监控守护进程(P2.3)
Abyss {
/// 启动全局监控
#[arg(long)]
start: bool,
/// 强制启动(跳过确认)
#[arg(long)]
force: bool,
/// 查看监控状态
#[arg(long)]
status: bool,
/// 查看监控日志
#[arg(long)]
logs: bool,
/// 日志显示行数(配合 --logs 使用)
#[arg(long, default_value = "50")]
lines: usize,
/// 停止全局监控
#[arg(long)]
stop: bool,
/// 显示或修改配置
#[arg(long)]
config: bool,
/// 排除的目录(逗号分隔,例如 "Downloads,Desktop")
#[arg(long)]
exclude: Option<String>,
/// 仅监控的扩展名(逗号分隔,例如 "rs,toml")
#[arg(long)]
include: Option<String>,
},
}
/// Watch 命令(顶级)
#[derive(Subcommand)]
enum WatchCommands {
/// 查看所有监控守护进程状态
Status,
/// 查看监控日志
Logs {
/// 显示行数
#[arg(long, short = 'n', default_value = "100")]
lines: usize,
},
/// 停止守护进程
Stop {
/// 指定要停止的监控路径(不指定则停止所有)
#[arg(long)]
path: Option<String>,
},
}
#[derive(Subcommand)]
enum ContextCommands {
/// 获取会话摘要
Get {
/// 摘要级别: short | detailed
#[arg(long, default_value = "short")]
level: String,
/// 会话 ID(默认 "default")
#[arg(long, default_value = "default")]
session_id: String,
},
/// 清空会话记录
Clear {
/// 会话 ID(默认 "default")
#[arg(long, default_value = "default")]
session_id: String,
},
/// 导出会话为 JSON
Export {
/// 会话 ID(默认 "default")
#[arg(long, default_value = "default")]
session_id: String,
},
/// 设置自定义上下文
Set {
/// 自定义上下文文本
#[arg(long)]
custom: String,
/// 会话 ID(默认 "default")
#[arg(long, default_value = "default")]
session_id: String,
},
}
/// 执行命令的内部函数
fn run_command(cli: &Cli) -> anyhow::Result<()> {
match &cli.command {
Commands::Agent { subcommand } => {
let agent_args = match subcommand {
AgentCommands::Init { model, format } => agent::AgentArgs {
subcommand: agent::AgentSubcommand::Init {
model: model.clone(),
format: format.clone(),
},
},
AgentCommands::Schema { file, histogram, json, minify, with_context } => {
agent::AgentArgs {
subcommand: agent::AgentSubcommand::Schema {
file: file.clone(),
histogram: *histogram,
json: *json,
minify: *minify,
with_context: *with_context,
},
}
}
AgentCommands::Sample { file, rows, strategy, json, with_context } => {
let strategy = strategy.parse().unwrap_or(agent::SampleStrategy::Smart);
agent::AgentArgs {
subcommand: agent::AgentSubcommand::Sample {
file: file.clone(),
n: *rows,
strategy,
json: *json,
with_context: *with_context,
},
}
}
AgentCommands::Query { file, sql, format, minify, limit, with_context } => {
agent::AgentArgs {
subcommand: agent::AgentSubcommand::Query {
file: file.clone(),
sql: sql.clone(),
format: format.clone(),
minify: *minify,
limit: *limit,
with_context: *with_context,
},
}
}
AgentCommands::Explain { sql } => agent::AgentArgs {
subcommand: agent::AgentSubcommand::Explain { sql: sql.clone() },
},
AgentCommands::Context { subcommand } => {
let ctx_sub = match subcommand {
ContextCommands::Get { level, session_id } => {
agent::ContextSubcommand::Get {
level: level.clone(),
session_id: session_id.clone(),
}
}
ContextCommands::Clear { session_id } => {
agent::ContextSubcommand::Clear { session_id: session_id.clone() }
}
ContextCommands::Export { session_id } => {
agent::ContextSubcommand::Export { session_id: session_id.clone() }
}
ContextCommands::Set { custom, session_id } => {
agent::ContextSubcommand::Set {
custom: custom.clone(),
session_id: session_id.clone(),
}
}
};
agent::AgentArgs {
subcommand: agent::AgentSubcommand::Context { subcommand: ctx_sub },
}
}
// P2.1: peek 命令
AgentCommands::Peek {
directory,
file,
no_cache,
output,
max_depth,
include,
exclude,
} => {
peek::execute(peek::PeekArgs {
directory: directory.clone(),
file: file.clone(),
use_cache: !no_cache,
output: output.clone(),
max_depth: *max_depth,
include: include.clone(),
exclude: exclude.clone(),
})?;
return Ok(());
}
// P2.3: abyss 命令
AgentCommands::Abyss {
start,
force,
status,
logs,
lines,
stop,
config,
exclude,
include,
} => {
let action = if *start {
abyss::AbyssAction::Start {
force: *force,
exclude: exclude.clone(),
include: include.clone(),
}
} else if *status {
abyss::AbyssAction::Status
} else if *logs {
abyss::AbyssAction::Logs { lines: *lines }
} else if *stop {
abyss::AbyssAction::Stop
} else if *config {
abyss::AbyssAction::Config {
exclude: exclude.clone(),
include: include.clone(),
}
} else {
// 默认显示状态
abyss::AbyssAction::Status
};
abyss::execute(abyss::AbyssArgs { action })?;
return Ok(());
}
};
agent::execute(agent_args)?;
}
Commands::Find {
query,
path,
r#type,
size,
mtime,
max_depth,
hidden,
no_ignore,
follow_links,
threads,
semantic,
index,
rebuild,
index_dir,
watch,
watch_daemon,
watch_include,
watch_exclude,
history,
recommend,
clear_history,
output,
max_tokens,
} => {
find::execute(find::FindArgs {
query: query.clone(),
path: path.clone(),
file_type: r#type.clone(),
size: size.clone(),
mtime: mtime.clone(),
max_depth: *max_depth,
hidden: *hidden,
no_ignore: *no_ignore,
follow_links: *follow_links,
threads: *threads,
semantic: *semantic,
index: *index,
rebuild: *rebuild,
index_dir: index_dir.clone(),
watch: *watch,
watch_daemon: *watch_daemon,
watch_include: watch_include.clone(),
watch_exclude: watch_exclude.clone(),
history: *history,
recommend: *recommend,
clear_history: *clear_history,
output: output.clone(),
max_tokens: *max_tokens,
})?;
}
// P2.2: Watch 守护进程管理
Commands::Watch { subcommand } => {
let watch_args = match subcommand {
WatchCommands::Status => {
watch::WatchArgs { subcommand: watch::WatchSubcommand::Status }
}
WatchCommands::Logs { lines } => {
watch::WatchArgs { subcommand: watch::WatchSubcommand::Logs { lines: *lines } }
}
WatchCommands::Stop { path } => watch::WatchArgs {
subcommand: watch::WatchSubcommand::Stop { path: path.clone() },
},
};
watch::execute(watch_args)?;
}
Commands::Process { file, query, quality_check, output, format } => {
process::execute(
file,
query.as_deref(),
*quality_check,
output.as_deref(),
format.as_deref(),
)?;
}
Commands::Benchmark { suite, output, iterations, data_path, warmup } => {
benchmark::execute(benchmark::BenchmarkArgs {
suite: *suite,
output: *output,
iterations: *iterations,
data_path: data_path.clone(),
warmup: *warmup,
})?;
}
Commands::Config { subcommand } => {
let config_args = match subcommand {
ConfigCommands::Show => {
config::ConfigArgs { subcommand: config::ConfigSubcommand::Show }
}
ConfigCommands::Get { key } => config::ConfigArgs {
subcommand: config::ConfigSubcommand::Get { key: key.clone() },
},
ConfigCommands::Set { key, value } => config::ConfigArgs {
subcommand: config::ConfigSubcommand::Set {
key: key.clone(),
value: value.clone(),
},
},
ConfigCommands::Reset => {
config::ConfigArgs { subcommand: config::ConfigSubcommand::Reset }
}
ConfigCommands::Edit => {
config::ConfigArgs { subcommand: config::ConfigSubcommand::Edit }
}
};
config::execute(config_args)?;
}
}
Ok(())
}
fn main() {
// 解析命令行参数
let cli = Cli::parse();
// 初始化日志系统
let log_config = LogConfig::from_args(cli.verbose, cli.quiet, cli.no_color);
if let Err(e) = log_config.init() {
print_anyhow_error(&e, cli.verbose, cli.no_color);
std::process::exit(1);
}
// 执行子命令
if let Err(e) = run_command(&cli) {
// BrokenPipe 是正常的管道截断(如 | head),静默退出
if let Some(io_err) = e.downcast_ref::<std::io::Error>() {
if io_err.kind() == std::io::ErrorKind::BrokenPipe {
std::process::exit(0);
}
}
print_anyhow_error(&e, cli.verbose, cli.no_color);
std::process::exit(1);
}
}