MCPcopy Create free account
hub / github.com/diillson/chatcli / handleCompactCommand

Method handleCompactCommand

cli/compact_command.go:19–87  ·  view source on GitHub ↗

handleCompactCommand handles /compact [instruction]. Without instruction: runs automatic compaction. With instruction: runs guided compaction preserving what the user specifies.

(ctx context.Context, userInput string)

Source from the content-addressed store, hash-verified

17// Without instruction: runs automatic compaction.
18// With instruction: runs guided compaction preserving what the user specifies.
19func (cli *ChatCLI) handleCompactCommand(ctx context.Context, userInput string) {
20 instruction := strings.TrimSpace(strings.TrimPrefix(userInput, "/compact"))
21
22 if cli.Client == nil {
23 fmt.Println(colorize(" "+i18n.T("compact.error.no_provider"), ColorYellow))
24 return
25 }
26
27 if len(cli.history) < 4 {
28 fmt.Println(colorize(" "+i18n.T("compact.error.history_too_short"), ColorGray))
29 return
30 }
31
32 ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
33 defer cancel()
34
35 if instruction == "" {
36 // Fire PreCompact hook
37 if cli.hookManager != nil {
38 wd, _ := os.Getwd()
39 cli.hookManager.Fire(ctx, hooks.HookEvent{
40 Type: hooks.EventPreCompact,
41 Timestamp: time.Now(),
42 SessionID: cli.currentSessionName,
43 WorkingDir: wd,
44 })
45 }
46
47 // Automatic compaction
48 cfg := DefaultCompactConfig(cli.Provider, cli.Model)
49 cfg.BudgetRatio = 0.50 // more aggressive for explicit /compact
50
51 // Live progress so the user knows what's happening, especially
52 // during the LLM-driven summarization (can take 30-90s).
53 cli.historyCompactor.SetStatusCallback(func(stage CompactStage, msg string) {
54 fmt.Printf(" %s %s\n", colorize("│", ColorCyan), colorize(msg, ColorGray))
55 })
56 compacted, err := cli.historyCompactor.Compact(ctx, cli.history, cli.Client, cfg)
57 cli.historyCompactor.SetStatusCallback(nil)
58 if err != nil {
59 fmt.Println(colorize(fmt.Sprintf(" %s", i18n.T("compact.error.failed", err)), ColorYellow))
60 return
61 }
62
63 before := len(cli.history)
64 cli.history = compacted
65 after := len(cli.history)
66 fmt.Printf(" %s %s\n",
67 colorize("📦", ""), i18n.T("compact.success", before, after))
68
69 // Fire PostCompact hook
70 if cli.hookManager != nil {
71 wd, _ := os.Getwd()
72 // Detached: the async hook must outlive this command (and the
73 // 60s timeout ctx that defer-cancels on return), so we strip
74 // cancellation but keep inherited values.
75 cli.hookManager.FireAsync(context.WithoutCancel(ctx), hooks.HookEvent{
76 Type: hooks.EventPostCompact,

Callers 1

buildRoutesMethod · 0.80

Calls 8

guidedCompactMethod · 0.95
TFunction · 0.92
colorizeFunction · 0.85
DefaultCompactConfigFunction · 0.85
FireMethod · 0.80
SetStatusCallbackMethod · 0.80
CompactMethod · 0.80
FireAsyncMethod · 0.80

Tested by

no test coverage detected