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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
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-sonnet-4-6 (default, balanced, best for most coding tasks)
- claude-opus-4-6 (most powerful, expensive, use for hardest tasks only)
- claude-haiku-4-5-20251001 (fast, cheap, use for simple tasks)
Gemini:
- gemini-2.5-flash-lite (fastest, most efficient, best for simple tasks)
- gemini-2.5-flash (fast, balanced)
- gemini-2.5-pro (most powerful Gemini, larger context)
Selection Criteria:
- Agent: CRITICAL: You MUST select an agent where "Rate Limited: false". DO NOT select an agent where "Rate Limited: true" if any other agent is available and NOT rate limited.
Check the "System Status" section below. If it says "- Agent claude: ... Rate Limited: true", you MUST NOT select claude. Use gemini instead.
- Model: Select based on task complexity. Use powerful models (opus, pro, pro-preview) 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.5-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)
}
// 1. Parse the JSON envelope from the gemini CLI.
var cliOut struct {
Response string `json:"response"`
}
if err := json.Unmarshal(out, &cliOut); err != nil {
// If it's not JSON, it might be raw text (though we requested JSON).
// This can happen if the CLI prints "Loaded cached credentials" or other info.
cliOut.Response = string(out)
}
// 2. Extract the model response from the "response" field if present.
// If it was already raw text, cliOut.Response will have it.
cleanOut := strings.TrimSpace(cliOut.Response)
// 3. Clean up "Loaded cached credentials" or other noise that might be in the string
// if we fell back to string(out).
if strings.Contains(cleanOut, "Loaded cached credentials.") {
lines := strings.Split(cleanOut, "\n")
var modelLines []string
for _, line := range lines {
if !strings.Contains(line, "Loaded cached credentials.") {
modelLines = append(modelLines, line)
}
}
cleanOut = strings.TrimSpace(strings.Join(modelLines, "\n"))
}
// 4. Gemini might wrap the JSON in markdown code blocks.
cleanOut = strings.TrimPrefix(cleanOut, "```json")
cleanOut = strings.TrimPrefix(cleanOut, "```") // fallback
cleanOut = strings.TrimSuffix(cleanOut, "```")
cleanOut = strings.TrimSpace(cleanOut)
var cls Classification
if err := json.Unmarshal([]byte(cleanOut), &cls); err != nil {
return nil, fmt.Errorf("failed to parse classification JSON: %w\nOriginal Output: %s\nCleaned Output: %s", err, string(out), cleanOut)
}
return &cls, nil
}
|