blob: a7faea943d8545498f565decce95029c2c824e87 [file] [log] [blame]
iomodo76f9a2d2025-07-26 12:14:40 +04001package agent
2
3import (
4 "context"
iomodo0c203b12025-07-26 19:44:57 +04005 "log/slog"
6 "os"
iomodo76f9a2d2025-07-26 12:14:40 +04007 "time"
8
9 "github.com/iomodo/staff/git"
10 "github.com/iomodo/staff/llm"
11 "github.com/iomodo/staff/tm"
12 "github.com/iomodo/staff/tm/git_tm"
13)
14
15// ExampleAgent demonstrates how to create and run an agent
16func ExampleAgent() {
iomodo0c203b12025-07-26 19:44:57 +040017 // Create logger
18 logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
19
iomodo76f9a2d2025-07-26 12:14:40 +040020 // Create git interface for task management
21 gitInterface := git.DefaultGit("./tasks-repo")
22
23 // Create task manager
iomodo0c203b12025-07-26 19:44:57 +040024 taskManager := git_tm.NewGitTaskManagerWithLogger(gitInterface, "./tasks-repo", logger)
iomodo76f9a2d2025-07-26 12:14:40 +040025
26 // Create LLM configuration
27 llmConfig := llm.Config{
28 Provider: llm.ProviderOpenAI,
29 APIKey: "your-openai-api-key-here", // Replace with actual API key
30 BaseURL: "https://api.openai.com/v1",
31 Timeout: 30 * time.Second,
32 }
33
34 // Create agent configuration
35 config := AgentConfig{
36 Name: "backend-engineer-1",
37 Role: "Backend Engineer",
38 GitUsername: "backend-agent",
39 GitEmail: "backend-agent@company.com",
40 WorkingDir: "./workspace",
41 LLMProvider: llm.ProviderOpenAI,
42 LLMModel: "gpt-4",
43 LLMConfig: llmConfig,
44 SystemPrompt: `You are a skilled backend engineer. Your role is to:
451. Analyze tasks and provide technical solutions
462. Write clean, maintainable code
473. Consider performance, security, and scalability
484. Provide clear documentation for your solutions
495. Follow best practices and coding standards
50
51When responding to tasks, provide:
52- Detailed technical analysis
53- Code examples where appropriate
54- Implementation considerations
55- Testing recommendations
56- Documentation suggestions`,
57 TaskManager: taskManager,
58 GitRepoPath: "./code-repo",
59 GitRemote: "origin",
60 GitBranch: "main",
61 }
62
63 // Create agent
iomodo0c203b12025-07-26 19:44:57 +040064 agent, err := NewAgent(config, logger)
iomodo76f9a2d2025-07-26 12:14:40 +040065 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +040066 logger.Error("Failed to create agent", slog.String("error", err.Error()))
67 os.Exit(1)
iomodo76f9a2d2025-07-26 12:14:40 +040068 }
69
70 // Create a sample task
71 ctx := context.Background()
72 task, err := taskManager.CreateTask(ctx, &tm.TaskCreateRequest{
73 Title: "Implement user authentication API",
74 Description: "Create a REST API endpoint for user authentication with JWT tokens. Include login, logout, and token refresh functionality.",
75 OwnerID: config.Name,
76 Priority: tm.PriorityHigh,
77 })
78 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +040079 logger.Error("Failed to create task", slog.String("error", err.Error()))
80 os.Exit(1)
iomodo76f9a2d2025-07-26 12:14:40 +040081 }
82
iomodo0c203b12025-07-26 19:44:57 +040083 logger.Info("Created task", slog.String("id", task.ID))
iomodo76f9a2d2025-07-26 12:14:40 +040084
85 // Run the agent (this will process tasks in an infinite loop)
86 go func() {
87 if err := agent.Run(); err != nil {
iomodo0c203b12025-07-26 19:44:57 +040088 logger.Error("Agent stopped with error", slog.String("error", err.Error()))
iomodo76f9a2d2025-07-26 12:14:40 +040089 }
90 }()
91
92 // Let the agent run for a while
93 time.Sleep(5 * time.Minute)
94
95 // Stop the agent
96 agent.Stop()
97}
98
99// ExampleMultipleAgents demonstrates how to create multiple agents with different roles
100func ExampleMultipleAgents() {
iomodo0c203b12025-07-26 19:44:57 +0400101 // Create logger
102 logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
103
iomodo76f9a2d2025-07-26 12:14:40 +0400104 // Create shared git interface for task management
105 gitInterface := git.DefaultGit("./tasks-repo")
iomodo0c203b12025-07-26 19:44:57 +0400106 taskManager := git_tm.NewGitTaskManagerWithLogger(gitInterface, "./tasks-repo", logger)
iomodo76f9a2d2025-07-26 12:14:40 +0400107
108 // Create agents with different roles
109 agents := []AgentConfig{
110 {
111 Name: "backend-engineer-1",
112 Role: "Backend Engineer",
113 GitUsername: "backend-agent",
114 GitEmail: "backend-agent@company.com",
115 WorkingDir: "./workspace/backend",
116 LLMProvider: llm.ProviderOpenAI,
117 LLMModel: "gpt-4",
118 LLMConfig: llm.Config{
119 Provider: llm.ProviderOpenAI,
120 APIKey: "your-openai-api-key",
121 BaseURL: "https://api.openai.com/v1",
122 Timeout: 30 * time.Second,
123 },
124 SystemPrompt: `You are a backend engineer. Focus on:
125- API design and implementation
126- Database design and optimization
127- Security best practices
128- Performance optimization
129- Code quality and testing`,
130 TaskManager: taskManager,
131 GitRepoPath: "./code-repo",
132 GitRemote: "origin",
133 GitBranch: "main",
134 },
135 {
136 Name: "frontend-engineer-1",
137 Role: "Frontend Engineer",
138 GitUsername: "frontend-agent",
139 GitEmail: "frontend-agent@company.com",
140 WorkingDir: "./workspace/frontend",
141 LLMProvider: llm.ProviderOpenAI,
142 LLMModel: "gpt-4",
143 LLMConfig: llm.Config{
144 Provider: llm.ProviderOpenAI,
145 APIKey: "your-openai-api-key",
146 BaseURL: "https://api.openai.com/v1",
147 Timeout: 30 * time.Second,
148 },
149 SystemPrompt: `You are a frontend engineer. Focus on:
150- User interface design and implementation
151- React/Vue/Angular development
152- Responsive design and accessibility
153- Performance optimization
154- User experience best practices`,
155 TaskManager: taskManager,
156 GitRepoPath: "./code-repo",
157 GitRemote: "origin",
158 GitBranch: "main",
159 },
160 {
161 Name: "product-manager-1",
162 Role: "Product Manager",
163 GitUsername: "pm-agent",
164 GitEmail: "pm-agent@company.com",
165 WorkingDir: "./workspace/product",
166 LLMProvider: llm.ProviderOpenAI,
167 LLMModel: "gpt-4",
168 LLMConfig: llm.Config{
169 Provider: llm.ProviderOpenAI,
170 APIKey: "your-openai-api-key",
171 BaseURL: "https://api.openai.com/v1",
172 Timeout: 30 * time.Second,
173 },
174 SystemPrompt: `You are a product manager. Focus on:
175- Product strategy and roadmap
176- User research and requirements gathering
177- Feature prioritization and planning
178- Stakeholder communication
179- Product documentation and specifications`,
180 TaskManager: taskManager,
181 GitRepoPath: "./docs-repo",
182 GitRemote: "origin",
183 GitBranch: "main",
184 },
185 }
186
187 // Create and start all agents
188 for _, config := range agents {
iomodo0c203b12025-07-26 19:44:57 +0400189 agent, err := NewAgent(config, logger)
iomodo76f9a2d2025-07-26 12:14:40 +0400190 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +0400191 logger.Error("Failed to create agent", slog.String("name", config.Name), slog.String("error", err.Error()))
iomodo76f9a2d2025-07-26 12:14:40 +0400192 continue
193 }
194
195 go func(agent *Agent, name string) {
iomodo0c203b12025-07-26 19:44:57 +0400196 logger.Info("Starting agent", slog.String("name", name))
iomodo76f9a2d2025-07-26 12:14:40 +0400197 if err := agent.Run(); err != nil {
iomodo0c203b12025-07-26 19:44:57 +0400198 logger.Error("Agent stopped with error", slog.String("name", name), slog.String("error", err.Error()))
iomodo76f9a2d2025-07-26 12:14:40 +0400199 }
200 }(agent, config.Name)
201 }
202
203 // Let agents run for a while
204 time.Sleep(10 * time.Minute)
205}