-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcc2.sh
More file actions
executable file
·661 lines (581 loc) · 18.6 KB
/
cc2.sh
File metadata and controls
executable file
·661 lines (581 loc) · 18.6 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
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
#!/usr/bin/env bash
# code-context-v2 CLI wrapper
# Usage: cc2 [command] [options]
#
# Commands:
# (none) Interactive menu (requires gum)
# list, -l List all indexed projects
# stats, -s Show global statistics
# index, -i Index a project (requires path)
# sync Sync project (reindex only changed files)
# check Check if project needs syncing (dry-run)
# delete, -d Delete a project (requires id)
# watch, -w Start watcher for a project
# watch-all Start global watcher (all projects, single process)
# watchers List active watchers
# sync-all Sync all indexed projects
# stop Stop watcher for a project
# stop-all Stop all watchers
# stop-global Stop the global watcher
# search Semantic search across project codebase
# search-file Search within a specific file
# search-lit Search indexed literature (books)
# memory Memory root init/index/search
# analyze Analyze search quality logs (JSONL)
# benchmark Run retrieval benchmark for a project
#
# Examples:
# cc2 # Interactive menu
# cc2 list # List projects
# cc2 index ~/projects/myapp # Index with auto-id
# cc2 index ~/projects/myapp -id myapp # Index with custom id
# cc2 search "auth middleware" -p myapp # Semantic codebase search
# cc2 search-file src/auth.ts "q" -p app # Search within a file
# cc2 search-lit "dependency injection" # Search literature
# cc2 memory init # Create .pi/memory/MEMORY.md
# cc2 memory index .pi/memory -p myapp # Index project memory
# cc2 memory search "auth flow" -p myapp # Search memory
# cc2 check myapp # Check if needs syncing
# cc2 sync myapp # Sync project (only changes)
# cc2 delete myapp # Delete project
# cc2 watch ~/projects/myapp # Start watcher
set -euo pipefail
BASE_DIR="${HOME}/.claude/mcp-servers/code-context-v2"
VENV_PYTHON="${BASE_DIR}/.venv/bin/python"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
NC='\033[0m' # No Color
print_success() { echo -e "${GREEN}✓${NC} $1"; }
print_error() { echo -e "${RED}✗${NC} $1"; }
print_info() { echo -e "${BLUE}→${NC} $1"; }
print_warn() { echo -e "${YELLOW}⚠${NC} $1"; }
check_deps() {
if [[ ! -f "${VENV_PYTHON}" ]]; then
print_error "Python venv not found at ${VENV_PYTHON}"
print_info "Run: cd ${BASE_DIR} && uv venv && uv pip install -e ."
exit 1
fi
}
run_cli() {
local caller_cwd="$PWD"
cd "${BASE_DIR}"
CC2_CALLER_CWD="${caller_cwd}" "${VENV_PYTHON}" -m cli.manage "$@"
}
run_memory_cli() {
local caller_cwd="$PWD"
cd "${BASE_DIR}"
CC2_CALLER_CWD="${caller_cwd}" "${VENV_PYTHON}" -m cli.memory "$@"
}
show_help() {
echo -e "${PURPLE}code-context-v2 CLI${NC}"
echo ""
echo "Usage: cc2 [command] [options]"
echo ""
echo "Commands:"
echo " (none) Interactive menu (requires gum)"
echo " list, -l List all indexed projects"
echo " stats, -s Show global statistics"
echo " books List indexed books"
echo " search QUERY -p ID Semantic codebase search"
echo " search-file FILE QUERY -p ID Search within a file"
echo " search-lit QUERY Search indexed literature"
echo " memory ... Memory init/index/search"
echo " index PATH [-id ID] Index a project"
echo " sync ID Sync project (reindex only changed files)"
echo " sync-all Sync all indexed projects"
echo " check ID Check sync status (dry-run, no changes)"
echo " delete ID Delete a project by ID"
echo " watch PATH Start background watcher (single project)"
echo " watch-all Start global watcher (all projects)"
echo " watchers List active watchers"
echo " stop PATH Stop watcher for project"
echo " stop-all Stop all watchers"
echo " stop-global Stop the global watcher"
echo ""
echo "Examples:"
echo " cc2 # Interactive menu"
echo " cc2 list # List projects"
echo " cc2 search 'auth middleware' -p app # Semantic search"
echo " cc2 search 'auth' -p app --intent usage # With intent"
echo " cc2 search-file src/auth.ts 'mid' -p app # File-scoped"
echo " cc2 search-lit 'dependency injection' # Literature search"
echo " cc2 search-lit 'DI' --book clean-code # Scoped to book"
echo " cc2 memory init # Create .pi/memory/MEMORY.md"
echo " cc2 memory index .pi/memory -p app # Index memory root"
echo " cc2 memory search 'auth flow' -p app # Search memory"
echo " cc2 index ~/myproject # Index (id = folder name)"
echo " cc2 index ~/myproject -id my-proj # Index with custom id"
echo " cc2 check my-proj # Check if needs syncing"
echo " cc2 sync my-proj # Sync (only changed files)"
echo " cc2 sync-all # Sync all projects"
echo " cc2 delete my-proj # Delete project"
echo " cc2 watch ~/myproject # Start watcher (single project)"
echo " cc2 watch-all # Start global watcher"
echo " cc2 analyze [--last N] [--since DATE] # Analyze search quality logs"
echo " cc2 benchmark PROJECT # Run retrieval benchmark"
echo " cc2 benchmark PROJECT --save v1 # Save baseline"
echo " cc2 benchmark PROJECT --compare v1 # Compare vs baseline"
echo ""
echo "Pipeline: voyage-4-large (index) → voyage-4-lite (query) → rerank-2.5"
}
show_memory_help() {
echo -e "${PURPLE}cc2 memory${NC}"
echo ""
echo "Usage: cc2 memory <init|index|search> [options]"
echo ""
echo "Commands:"
echo " init [PATH] Create a memory root with MEMORY.md (default: .pi/memory)"
echo " index [PATH] Index a memory root (default: .pi/memory)"
echo " search QUERY Search indexed memory (auto-scopes to nearest .pi/memory)"
echo ""
echo "Examples:"
echo " cc2 memory init"
echo " cc2 memory init .pi/memory --title 'Courtex'"
echo " cc2 memory index .pi/memory --project courtex"
echo " cc2 memory search 'refresh token rotation' --project courtex"
echo " # If no --root is passed, search uses the nearest .pi/memory above cwd"
echo " cc2 memory search 'oauth cookies' --project courtex --tags auth,security"
}
cmd_list() {
run_cli --list
}
cmd_stats() {
run_cli --stats
}
cmd_index() {
local path=""
local project_id=""
local force=""
while [[ $# -gt 0 ]]; do
case "$1" in
-id|--id)
project_id="$2"
shift 2
;;
-f|--force)
force="--force"
shift
;;
*)
if [[ -z "${path}" ]]; then
path="$1"
fi
shift
;;
esac
done
if [[ -z "${path}" ]]; then
print_error "Path required. Usage: cc2 index PATH [-id ID]"
exit 1
fi
# Expand path
path=$(realpath -m "${path/#\~/$HOME}")
if [[ ! -d "${path}" ]]; then
print_error "Not a directory: ${path}"
exit 1
fi
local args=(--index "${path}")
[[ -n "${project_id}" ]] && args+=(--id "${project_id}")
[[ -n "${force}" ]] && args+=(--force)
run_cli "${args[@]}"
}
cmd_sync() {
local project_id="$1"
if [[ -z "${project_id}" ]]; then
print_error "Project ID required. Usage: cc2 sync ID"
print_info "Use 'cc2 list' to see available projects"
exit 1
fi
run_cli --sync "${project_id}"
}
cmd_sync_all() {
run_cli --sync-all
}
cmd_check() {
local project_id="$1"
if [[ -z "${project_id}" ]]; then
print_error "Project ID required. Usage: cc2 check ID"
print_info "Use 'cc2 list' to see available projects"
exit 1
fi
run_cli --check "${project_id}"
}
cmd_delete() {
local project_id="$1"
if [[ -z "${project_id}" ]]; then
print_error "Project ID required. Usage: cc2 delete ID"
exit 1
fi
run_cli --delete "${project_id}"
}
cmd_watch() {
local path="$1"
if [[ -z "${path}" ]]; then
print_error "Path required. Usage: cc2 watch PATH"
exit 1
fi
path=$(realpath -m "${path/#\~/$HOME}")
run_cli --watch "${path}"
}
cmd_watchers() {
run_cli --watchers
}
cmd_stop() {
local path="$1"
if [[ -z "${path}" ]]; then
print_error "Path required. Usage: cc2 stop PATH"
exit 1
fi
path=$(realpath -m "${path/#\~/$HOME}")
run_cli --stop-watcher "${path}"
}
cmd_stop_all() {
run_cli --stop-all-watchers
}
cmd_watch_all() {
run_cli --watch-all
}
cmd_stop_global() {
run_cli --stop-global
}
cmd_search() {
local query=""
local args=()
# First positional arg is the query
if [[ $# -gt 0 && ! "$1" =~ ^- ]]; then
query="$1"
shift
fi
if [[ -z "${query}" ]]; then
print_error "Query required. Usage: cc2 search 'query' -p PROJECT [--intent TYPE] [--json]"
exit 1
fi
args=(--search "${query}")
while [[ $# -gt 0 ]]; do
case "$1" in
-p|--project) args+=(--project "$2"); shift 2 ;;
--intent) args+=(--intent "$2"); shift 2 ;;
--file-type) args+=(--file-type "$2"); shift 2 ;;
--dir|--directory) args+=(--directory "$2"); shift 2 ;;
--scope) args+=(--scope "$2"); shift 2 ;;
--language) args+=(--language "$2"); shift 2 ;;
--chunk-type) args+=(--chunk-type "$2"); shift 2 ;;
--max-tokens) args+=(--max-tokens "$2"); shift 2 ;;
--include-tests) args+=(--include-tests); shift ;;
--json) args+=(--json); shift ;;
*) shift ;;
esac
done
run_cli "${args[@]}"
}
cmd_search_file() {
local filepath=""
local query=""
local args=()
# First two positional args: filepath, query
if [[ $# -gt 0 && ! "$1" =~ ^- ]]; then
filepath="$1"
shift
fi
if [[ $# -gt 0 && ! "$1" =~ ^- ]]; then
query="$1"
shift
fi
if [[ -z "${filepath}" || -z "${query}" ]]; then
print_error "Usage: cc2 search-file FILEPATH 'query' -p PROJECT [--intent TYPE] [--json]"
exit 1
fi
args=(--search-file "${filepath}" --query "${query}")
while [[ $# -gt 0 ]]; do
case "$1" in
-p|--project) args+=(--project "$2"); shift 2 ;;
--intent) args+=(--intent "$2"); shift 2 ;;
--max-tokens) args+=(--max-tokens "$2"); shift 2 ;;
--include-tests) args+=(--include-tests); shift ;;
--json) args+=(--json); shift ;;
*) shift ;;
esac
done
run_cli "${args[@]}"
}
cmd_search_lit() {
local query=""
local args=()
if [[ $# -gt 0 && ! "$1" =~ ^- ]]; then
query="$1"
shift
fi
if [[ -z "${query}" ]]; then
print_error "Query required. Usage: cc2 search-lit 'query' [--book ID] [--top-k N] [--json]"
exit 1
fi
args=(--search-lit "${query}")
while [[ $# -gt 0 ]]; do
case "$1" in
--book) args+=(--book "$2"); shift 2 ;;
--top-k) args+=(--top-k "$2"); shift 2 ;;
--json) args+=(--json); shift ;;
*) shift ;;
esac
done
run_cli "${args[@]}"
}
cmd_memory() {
local subcmd="${1:-help}"
shift || true
case "${subcmd}" in
help|--help|-h)
show_memory_help
;;
init)
local path=".pi/memory"
local title=""
local force=""
if [[ $# -gt 0 && ! "$1" =~ ^- ]]; then
path="$1"
shift
fi
while [[ $# -gt 0 ]]; do
case "$1" in
--title)
title="$2"
shift 2
;;
-f|--force)
force="--force"
shift
;;
*)
shift
;;
esac
done
path=$(realpath -m "${path/#\~/$HOME}")
local args=(init "${path}")
[[ -n "${title}" ]] && args+=(--title "${title}")
[[ -n "${force}" ]] && args+=(--force)
run_memory_cli "${args[@]}"
;;
index)
local path=".pi/memory"
local project=""
local scope=""
local force=""
if [[ $# -gt 0 && ! "$1" =~ ^- ]]; then
path="$1"
shift
fi
while [[ $# -gt 0 ]]; do
case "$1" in
-p|--project)
project="$2"
shift 2
;;
--scope)
scope="$2"
shift 2
;;
-f|--force)
force="--force"
shift
;;
*)
shift
;;
esac
done
path=$(realpath -m "${path/#\~/$HOME}")
local args=(index "${path}")
[[ -n "${project}" ]] && args+=(--project "${project}")
[[ -n "${scope}" ]] && args+=(--scope "${scope}")
[[ -n "${force}" ]] && args+=(--force)
run_memory_cli "${args[@]}"
;;
search)
local query=""
local project=""
local root=""
local scope=""
local tags=""
local limit=""
local json_output=""
if [[ $# -gt 0 && ! "$1" =~ ^- ]]; then
query="$1"
shift
fi
if [[ -z "${query}" ]]; then
print_error "Query required. Usage: cc2 memory search 'query' [--project ID]"
exit 1
fi
while [[ $# -gt 0 ]]; do
case "$1" in
-p|--project)
project="$2"
shift 2
;;
--root)
root="$2"
shift 2
;;
--scope)
scope="$2"
shift 2
;;
--tags)
tags="$2"
shift 2
;;
--limit)
limit="$2"
shift 2
;;
--json)
json_output="--json"
shift
;;
*)
shift
;;
esac
done
local args=(search "${query}")
[[ -n "${project}" ]] && args+=(--project "${project}")
if [[ -n "${root}" ]]; then
root=$(realpath -m "${root/#\~/$HOME}")
args+=(--root "${root}")
fi
[[ -n "${scope}" ]] && args+=(--scope "${scope}")
[[ -n "${tags}" ]] && args+=(--tags "${tags}")
[[ -n "${limit}" ]] && args+=(--limit "${limit}")
[[ -n "${json_output}" ]] && args+=(--json)
run_memory_cli "${args[@]}"
;;
*)
print_error "Unknown memory command: ${subcmd}"
echo ""
show_memory_help
exit 1
;;
esac
}
cmd_analyze() {
run_cli --analyze "$@"
}
cmd_benchmark() {
local project_id="${1:-}"
shift 2>/dev/null || true
local args=()
if [[ -z "${project_id}" ]]; then
print_error "Project ID required. Usage: cc2 benchmark PROJECT [--save NAME] [--compare NAME]"
exit 1
fi
args+=(--benchmark "${project_id}")
while [[ $# -gt 0 ]]; do
case "$1" in
--save)
args+=(--save-baseline "$2")
shift 2
;;
--compare)
args+=(--compare-baseline "$2")
shift 2
;;
*)
shift
;;
esac
done
run_cli "${args[@]}"
}
cmd_interactive() {
if ! command -v gum >/dev/null 2>&1; then
print_warn "gum not installed. Using text mode."
print_info "Install gum for better experience: brew install gum"
fi
run_cli
}
main() {
check_deps
if [[ $# -eq 0 ]]; then
cmd_interactive
exit 0
fi
local cmd="$1"
shift
case "${cmd}" in
help|--help|-h)
show_help
;;
list|--list|-l)
cmd_list
;;
stats|--stats|-s)
cmd_stats
;;
books)
run_cli --list-books
;;
search)
cmd_search "$@"
;;
search-file)
cmd_search_file "$@"
;;
search-lit)
cmd_search_lit "$@"
;;
memory)
cmd_memory "$@"
;;
index|--index|-i)
cmd_index "$@"
;;
sync-all|--sync-all)
cmd_sync_all
;;
sync|--sync)
cmd_sync "${1:-}"
;;
check|--check)
cmd_check "${1:-}"
;;
delete|--delete|-d)
cmd_delete "${1:-}"
;;
watch-all|--watch-all)
cmd_watch_all
;;
watch|--watch|-w)
cmd_watch "${1:-}"
;;
watchers|--watchers)
cmd_watchers
;;
stop|--stop)
cmd_stop "${1:-}"
;;
stop-all|--stop-all)
cmd_stop_all
;;
stop-global|--stop-global)
cmd_stop_global
;;
analyze|--analyze)
cmd_analyze "$@"
;;
benchmark|--benchmark)
cmd_benchmark "$@"
;;
*)
print_error "Unknown command: ${cmd}"
echo ""
show_help
exit 1
;;
esac
}
main "$@"