summaryrefslogtreecommitdiff
path: root/internal/executor/classifier.go
blob: 79ebc2781a909988c6ad27e6a3bdb1f5f806373f (plain)
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
package executor

import (
	"context"
	"encoding/json"
	"fmt"
	"os/exec"
	"strings"
)

type Classification struct {
	AgentType string `json:"agent_type"`
	Model     string `json:"model"`
	Reason    string `json:"reason"`
}

type SystemStatus struct {
	ActiveTasks map[string]int
	RateLimited map[string]bool
}

type Classifier struct {
	GeminiBinaryPath string
}

const classificationPrompt = `
You are a task classifier for Claudomator.
Given a task description and system status, select the best agent (claude or gemini) and model to use.

Agent Types:
- claude: Best for complex coding, reasoning, and tool use.
- gemini: Best for large context, fast reasoning, and multimodal tasks.

Available Models:
Claude:
- claude-3-5-sonnet-latest (balanced)
- claude-3-5-sonnet-20241022 (stable)
- claude-3-opus-20240229 (most powerful, expensive)
- claude-3-5-haiku-20241022 (fast, cheap)

Gemini:
- gemini-2.0-flash-lite (fastest, most efficient, best for simple tasks)
- gemini-2.0-flash (fast, multimodal)
- gemini-1.5-flash (fast, balanced)
- gemini-1.5-pro (more powerful, larger context)

Selection Criteria:
- Agent: Prefer the one with least running tasks and no active rate limit.
- Model: Select based on task complexity. Use powerful models (opus, pro) for complex reasoning/coding, flash-lite/flash/haiku for simple tasks.

Task:
Name: %s
Instructions: %s

System Status:
%s

Respond with ONLY a JSON object:
{
  "agent_type": "claude" | "gemini",
  "model": "model-name",
  "reason": "brief reason"
}
`

func (c *Classifier) Classify(ctx context.Context, taskName, instructions string, status SystemStatus) (*Classification, error) {
	statusStr := ""
	for agent, active := range status.ActiveTasks {
		statusStr += fmt.Sprintf("- Agent %s: %d active tasks, Rate Limited: %t\n", agent, active, status.RateLimited[agent])
	}

	prompt := fmt.Sprintf(classificationPrompt,
		taskName, instructions, statusStr,
	)

	binary := c.GeminiBinaryPath
	if binary == "" {
		binary = "gemini"
	}

	// Use a minimal model for classification to be fast and cheap.
	args := []string{
		"--prompt", prompt,
		"--model", "gemini-2.0-flash-lite",
		"--output-format", "json",
	}

	cmd := exec.CommandContext(ctx, binary, args...)
	out, err := cmd.Output()
	if err != nil {
		if exitErr, ok := err.(*exec.ExitError); ok {
			return nil, fmt.Errorf("classifier failed (%v): %s", err, string(exitErr.Stderr))
		}
		return nil, fmt.Errorf("classifier failed: %w", err)
	}

	var cls Classification
	// Gemini might wrap the JSON in markdown code blocks.
	cleanOut := strings.TrimSpace(string(out))
	cleanOut = strings.TrimPrefix(cleanOut, "```json")
	cleanOut = strings.TrimSuffix(cleanOut, "```")
	cleanOut = strings.TrimSpace(cleanOut)

	if err := json.Unmarshal([]byte(cleanOut), &cls); err != nil {
		return nil, fmt.Errorf("failed to parse classification JSON: %w\nOutput: %s", err, cleanOut)
	}

	return &cls, nil
}