-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.go
More file actions
164 lines (140 loc) · 3.71 KB
/
Copy pathagent.go
File metadata and controls
164 lines (140 loc) · 3.71 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package cmd
import (
"fmt"
"os"
"path/filepath"
"text/tabwriter"
"github.com/GrayCodeAI/hawk/internal/multiagent/agents"
"github.com/spf13/cobra"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
var agentCmd = &cobra.Command{
Use: "agent",
Short: "Manage custom agent personas",
Long: "Create, list, and manage custom agent personas stored in Hawk user state.",
}
var agentListCmd = &cobra.Command{
Use: "list",
Short: "List all available agents",
RunE: runAgentList,
}
var agentCreateCmd = &cobra.Command{
Use: "create <name>",
Short: "Create a new agent persona",
Args: cobra.ExactArgs(1),
RunE: runAgentCreate,
}
var agentShowCmd = &cobra.Command{
Use: "show <name>",
Short: "Show an agent's configuration",
Args: cobra.ExactArgs(1),
RunE: runAgentShow,
}
var agentRemoveCmd = &cobra.Command{
Use: "remove <name>",
Short: "Remove an agent persona",
Args: cobra.ExactArgs(1),
RunE: runAgentRemove,
}
var (
agentCreateDesc string
agentCreateModel string
)
func init() {
agentCreateCmd.Flags().StringVarP(&agentCreateDesc, "description", "d", "", "Agent description")
agentCreateCmd.Flags().StringVarP(&agentCreateModel, "model", "m", "", "Model to use (empty = inherit)")
agentCmd.AddCommand(agentListCmd)
agentCmd.AddCommand(agentCreateCmd)
agentCmd.AddCommand(agentShowCmd)
agentCmd.AddCommand(agentRemoveCmd)
}
func runAgentList(_ *cobra.Command, _ []string) error {
all, err := agents.ListAll()
if err != nil {
return err
}
if len(all) == 0 {
fmt.Printf("No agents found. Create one with: hawk agent create <name>\n")
fmt.Printf("Agent directory: %s\n", agents.DefaultDir())
return nil
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
_, _ = fmt.Fprintf(w, "NAME\tMODEL\tDESCRIPTION\n")
for _, a := range all {
model := a.Model
if model == "" {
model = "(inherit)"
}
desc := a.Description
if len(desc) > 50 {
desc = desc[:50] + "..."
}
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\n", a.Name, model, desc)
}
return w.Flush()
}
func runAgentCreate(_ *cobra.Command, args []string) error {
name := args[0]
dir := agents.DefaultDir()
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
path := filepath.Join(dir, name+".md")
if _, err := os.Stat(path); err == nil {
return fmt.Errorf("agent %q already exists at %s", name, path)
}
modelLine := "inherit"
if agentCreateModel != "" {
modelLine = agentCreateModel
}
desc := agentCreateDesc
if desc == "" {
desc = "Custom agent: " + name
}
content := fmt.Sprintf(`---
name: %s
description: %s
model: %s
---
# %s
You are a specialized agent. Complete tasks according to your expertise.
## Guidelines
- Be precise and focused
- Follow project conventions
- Report results clearly
`, name, desc, modelLine, cases.Title(language.English).String(name))
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
return err
}
fmt.Printf("Created agent %q at %s\n", name, path)
fmt.Printf("Edit the file to customize the system prompt.\n")
return nil
}
func runAgentShow(_ *cobra.Command, args []string) error {
a, err := agents.Get(args[0])
if err != nil {
return err
}
fmt.Printf("Name: %s\n", a.Name)
fmt.Printf("Description: %s\n", a.Description)
model := a.Model
if model == "" {
model = "(inherit from session)"
}
fmt.Printf("Model: %s\n", model)
fmt.Printf("File: %s\n", a.FilePath)
fmt.Printf("\n--- Prompt ---\n%s\n", a.Prompt)
return nil
}
func runAgentRemove(_ *cobra.Command, args []string) error {
a, err := agents.Get(args[0])
if err != nil {
return err
}
if err := os.Remove(a.FilePath); err != nil {
return fmt.Errorf("remove %s: %w", a.FilePath, err)
}
fmt.Printf("Removed agent %q (%s)\n", a.Name, a.FilePath)
return nil
}