|
| 1 | +// Package sessioncapture provides hawk's interface to trace. |
| 2 | +// |
| 3 | +// trace is a fully independent tool (own repo, own binary, works with any agent). |
| 4 | +// hawk can optionally manage it via slash commands: |
| 5 | +// /trace-enable — enable session capture |
| 6 | +// /trace-disable — disable session capture |
| 7 | +// /trace-status — show capture status |
| 8 | +// |
| 9 | +// hawk NEVER auto-enables trace. The user must explicitly opt in. |
| 10 | +package sessioncapture |
| 11 | + |
| 12 | +import ( |
| 13 | + "fmt" |
| 14 | + "os/exec" |
| 15 | + "path/filepath" |
| 16 | + "os" |
| 17 | + "strings" |
| 18 | +) |
| 19 | + |
| 20 | +// TraceControl lets hawk manage trace when the user asks. |
| 21 | +// trace remains independent — hawk just provides UI convenience. |
| 22 | +type TraceControl struct { |
| 23 | + ProjectDir string |
| 24 | +} |
| 25 | + |
| 26 | +// NewTraceControl creates a controller for the given project. |
| 27 | +func NewTraceControl(projectDir string) *TraceControl { |
| 28 | + return &TraceControl{ProjectDir: projectDir} |
| 29 | +} |
| 30 | + |
| 31 | +// IsInstalled checks if trace binary exists in PATH. |
| 32 | +func (tc *TraceControl) IsInstalled() bool { |
| 33 | + _, err := exec.LookPath("trace") |
| 34 | + return err == nil |
| 35 | +} |
| 36 | + |
| 37 | +// IsEnabled checks if trace is active in this project. |
| 38 | +func (tc *TraceControl) IsEnabled() bool { |
| 39 | + settingsPath := filepath.Join(tc.ProjectDir, ".trace", "settings.json") |
| 40 | + _, err := os.Stat(settingsPath) |
| 41 | + return err == nil |
| 42 | +} |
| 43 | + |
| 44 | +// Enable activates trace for this project. User must explicitly call this. |
| 45 | +func (tc *TraceControl) Enable() (string, error) { |
| 46 | + if !tc.IsInstalled() { |
| 47 | + return "", fmt.Errorf("trace is not installed. Install with: brew install GrayCodeAI/tap/trace") |
| 48 | + } |
| 49 | + if tc.IsEnabled() { |
| 50 | + return "Session capture is already enabled.", nil |
| 51 | + } |
| 52 | + |
| 53 | + cmd := exec.Command("trace", "enable", "--agent", "hawk") |
| 54 | + cmd.Dir = tc.ProjectDir |
| 55 | + output, err := cmd.CombinedOutput() |
| 56 | + if err != nil { |
| 57 | + return "", fmt.Errorf("failed to enable trace: %s", strings.TrimSpace(string(output))) |
| 58 | + } |
| 59 | + return "Session capture enabled. All future sessions will be recorded.", nil |
| 60 | +} |
| 61 | + |
| 62 | +// Disable deactivates trace for this project. User must explicitly call this. |
| 63 | +func (tc *TraceControl) Disable() (string, error) { |
| 64 | + if !tc.IsInstalled() { |
| 65 | + return "trace is not installed — nothing to disable.", nil |
| 66 | + } |
| 67 | + if !tc.IsEnabled() { |
| 68 | + return "Session capture is already disabled.", nil |
| 69 | + } |
| 70 | + |
| 71 | + cmd := exec.Command("trace", "disable") |
| 72 | + cmd.Dir = tc.ProjectDir |
| 73 | + output, err := cmd.CombinedOutput() |
| 74 | + if err != nil { |
| 75 | + return "", fmt.Errorf("failed to disable trace: %s", strings.TrimSpace(string(output))) |
| 76 | + } |
| 77 | + return "Session capture disabled. Sessions will no longer be recorded.", nil |
| 78 | +} |
| 79 | + |
| 80 | +// Status returns current trace state for display in hawk. |
| 81 | +func (tc *TraceControl) Status() string { |
| 82 | + if !tc.IsInstalled() { |
| 83 | + return "Session capture: not available (trace not installed)\n Install: brew install GrayCodeAI/tap/trace" |
| 84 | + } |
| 85 | + if !tc.IsEnabled() { |
| 86 | + return "Session capture: disabled\n Enable: /trace-enable" |
| 87 | + } |
| 88 | + |
| 89 | + // Get detailed status from trace |
| 90 | + cmd := exec.Command("trace", "status") |
| 91 | + cmd.Dir = tc.ProjectDir |
| 92 | + output, err := cmd.CombinedOutput() |
| 93 | + if err != nil { |
| 94 | + return "Session capture: enabled (running)" |
| 95 | + } |
| 96 | + return "Session capture: enabled\n" + strings.TrimSpace(string(output)) |
| 97 | +} |
0 commit comments