// internal/skills/debug/handlers.go package debug import ( "context" "encoding/json" "fmt" "time" "github.com/mathiasbq/supervisor/internal/brain" "github.com/mathiasbq/supervisor/internal/session" ) type debugArgs struct { ProjectRoot string `json:"project_root"` Error string `json:"error"` Context string `json:"context"` Model string `json:"model"` SessionID string `json:"session_id"` } // Handle dispatches the MCP tool call to the appropriate handler. func (s *Skill) Handle(ctx context.Context, tool string, args json.RawMessage) (json.RawMessage, error) { if tool != "debug" { return nil, fmt.Errorf("unknown tool: %s", tool) } var a debugArgs if err := json.Unmarshal(args, &a); err != nil { return nil, fmt.Errorf("parse args: %w", err) } if a.ProjectRoot == "" { return nil, fmt.Errorf("project_root is required") } if a.Error == "" { return nil, fmt.Errorf("error is required") } model := a.Model if model == "" { model = s.cfg.DefaultModel } brainCtx, _ := brain.Query(ctx, s.cfg.IngestBaseURL, a.Error+" "+a.Context, 3) task := fmt.Sprintf( "phase: debug\nproject_root: %s\nerror: %s\ncontext: %s\nmodel: %s", a.ProjectRoot, a.Error, a.Context, model, ) task = session.PrependHistory(s.cfg.SessionsDir, a.SessionID, "debug", task) if brainCtx != "" { task = brainCtx + "\n---\n\n" + task } if s.cfg.CompleteFunc == nil { return nil, fmt.Errorf("no executor configured") } t0 := time.Now() text, dur, err := s.cfg.CompleteFunc(ctx, model, s.cfg.SkillPrompt, task) if err != nil { return nil, err } if a.SessionID != "" && s.cfg.SessionsDir != "" { msg := text if len(msg) > 200 { msg = msg[:200] } _ = session.Append(s.cfg.SessionsDir, a.SessionID, session.Entry{ SessionID: a.SessionID, Timestamp: time.Now(), Skill: "debug", Phase: "debug", ProjectRoot: a.ProjectRoot, FinalStatus: "ok", ModelUsed: model, DurationMs: time.Since(t0).Milliseconds(), Message: msg, }) } return json.Marshal(map[string]any{"text": text, "model": model, "duration_ms": dur}) }