diff --git a/internal/skills/spec/handlers.go b/internal/skills/spec/handlers.go index 2c3dc2d..5e4378e 100644 --- a/internal/skills/spec/handlers.go +++ b/internal/skills/spec/handlers.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "fmt" + "time" iexec "github.com/mathiasbq/supervisor/internal/exec" "github.com/mathiasbq/supervisor/internal/session" @@ -48,11 +49,12 @@ func (s *Skill) Handle(ctx context.Context, tool string, args json.RawMessage) ( "phase: spec\nproject_root: %s\nrequirements: %s\noutput_path: %s\ncontext: %s\nmodel: %s", a.ProjectRoot, a.Requirements, outputPath, a.Context, model, ) - task = s.prependHistory(a.SessionID, "spec", task) + task = session.PrependHistory(s.cfg.SessionsDir, a.SessionID, "spec", task) if s.cfg.ExecutorFn == nil { return nil, fmt.Errorf("no executor configured") } + t0 := time.Now() result, err := s.cfg.ExecutorFn(ctx, iexec.Request{ SkillPrompt: s.cfg.SkillPrompt, TaskPrompt: task, @@ -62,24 +64,26 @@ func (s *Skill) Handle(ctx context.Context, tool string, args json.RawMessage) ( if err != nil { return nil, err } + + if a.SessionID != "" && s.cfg.SessionsDir != "" { + _ = session.Append(s.cfg.SessionsDir, a.SessionID, session.Entry{ + SessionID: a.SessionID, + Timestamp: time.Now(), + Skill: "spec", + Phase: "spec", + ProjectRoot: a.ProjectRoot, + Attempts: session.AttemptsFrom(result.Attempts), + FinalStatus: result.Status, + FilePath: result.FilePath, + ModelUsed: result.ModelUsed, + DurationMs: time.Since(t0).Milliseconds(), + Message: result.Message, + }) + } + b, err := json.Marshal(result) if err != nil { return nil, fmt.Errorf("marshal result: %w", err) } return b, nil } - -func (s *Skill) prependHistory(sessionID, currentPhase, task string) string { - if sessionID == "" || s.cfg.SessionsDir == "" { - return task - } - entries, err := session.Read(s.cfg.SessionsDir, sessionID) - if err != nil || len(entries) == 0 { - return task - } - history := session.FormatHistory(entries, currentPhase) - if history == "" { - return task - } - return history + "\n---\n\n" + task -}