| iomodo | 76f9a2d | 2025-07-26 12:14:40 +0400 | [diff] [blame] | 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| iomodo | 0c203b1 | 2025-07-26 19:44:57 +0400 | [diff] [blame] | 5 | "log/slog" |
| 6 | "os" |
| iomodo | 76f9a2d | 2025-07-26 12:14:40 +0400 | [diff] [blame] | 7 | "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 |
| 16 | func ExampleAgent() { |
| iomodo | 0c203b1 | 2025-07-26 19:44:57 +0400 | [diff] [blame] | 17 | // Create logger |
| 18 | logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})) |
| 19 | |
| iomodo | 76f9a2d | 2025-07-26 12:14:40 +0400 | [diff] [blame] | 20 | // Create git interface for task management |
| 21 | gitInterface := git.DefaultGit("./tasks-repo") |
| 22 | |
| 23 | // Create task manager |
| iomodo | 0c203b1 | 2025-07-26 19:44:57 +0400 | [diff] [blame] | 24 | taskManager := git_tm.NewGitTaskManagerWithLogger(gitInterface, "./tasks-repo", logger) |
| iomodo | 76f9a2d | 2025-07-26 12:14:40 +0400 | [diff] [blame] | 25 | |
| 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: |
| 45 | 1. Analyze tasks and provide technical solutions |
| 46 | 2. Write clean, maintainable code |
| 47 | 3. Consider performance, security, and scalability |
| 48 | 4. Provide clear documentation for your solutions |
| 49 | 5. Follow best practices and coding standards |
| 50 | |
| 51 | When 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 |
| iomodo | 0c203b1 | 2025-07-26 19:44:57 +0400 | [diff] [blame] | 64 | agent, err := NewAgent(config, logger) |
| iomodo | 76f9a2d | 2025-07-26 12:14:40 +0400 | [diff] [blame] | 65 | if err != nil { |
| iomodo | 0c203b1 | 2025-07-26 19:44:57 +0400 | [diff] [blame] | 66 | logger.Error("Failed to create agent", slog.String("error", err.Error())) |
| 67 | os.Exit(1) |
| iomodo | 76f9a2d | 2025-07-26 12:14:40 +0400 | [diff] [blame] | 68 | } |
| 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 { |
| iomodo | 0c203b1 | 2025-07-26 19:44:57 +0400 | [diff] [blame] | 79 | logger.Error("Failed to create task", slog.String("error", err.Error())) |
| 80 | os.Exit(1) |
| iomodo | 76f9a2d | 2025-07-26 12:14:40 +0400 | [diff] [blame] | 81 | } |
| 82 | |
| iomodo | 0c203b1 | 2025-07-26 19:44:57 +0400 | [diff] [blame] | 83 | logger.Info("Created task", slog.String("id", task.ID)) |
| iomodo | 76f9a2d | 2025-07-26 12:14:40 +0400 | [diff] [blame] | 84 | |
| 85 | // Run the agent (this will process tasks in an infinite loop) |
| 86 | go func() { |
| 87 | if err := agent.Run(); err != nil { |
| iomodo | 0c203b1 | 2025-07-26 19:44:57 +0400 | [diff] [blame] | 88 | logger.Error("Agent stopped with error", slog.String("error", err.Error())) |
| iomodo | 76f9a2d | 2025-07-26 12:14:40 +0400 | [diff] [blame] | 89 | } |
| 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 |
| 100 | func ExampleMultipleAgents() { |
| iomodo | 0c203b1 | 2025-07-26 19:44:57 +0400 | [diff] [blame] | 101 | // Create logger |
| 102 | logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})) |
| 103 | |
| iomodo | 76f9a2d | 2025-07-26 12:14:40 +0400 | [diff] [blame] | 104 | // Create shared git interface for task management |
| 105 | gitInterface := git.DefaultGit("./tasks-repo") |
| iomodo | 0c203b1 | 2025-07-26 19:44:57 +0400 | [diff] [blame] | 106 | taskManager := git_tm.NewGitTaskManagerWithLogger(gitInterface, "./tasks-repo", logger) |
| iomodo | 76f9a2d | 2025-07-26 12:14:40 +0400 | [diff] [blame] | 107 | |
| 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 { |
| iomodo | 0c203b1 | 2025-07-26 19:44:57 +0400 | [diff] [blame] | 189 | agent, err := NewAgent(config, logger) |
| iomodo | 76f9a2d | 2025-07-26 12:14:40 +0400 | [diff] [blame] | 190 | if err != nil { |
| iomodo | 0c203b1 | 2025-07-26 19:44:57 +0400 | [diff] [blame] | 191 | logger.Error("Failed to create agent", slog.String("name", config.Name), slog.String("error", err.Error())) |
| iomodo | 76f9a2d | 2025-07-26 12:14:40 +0400 | [diff] [blame] | 192 | continue |
| 193 | } |
| 194 | |
| 195 | go func(agent *Agent, name string) { |
| iomodo | 0c203b1 | 2025-07-26 19:44:57 +0400 | [diff] [blame] | 196 | logger.Info("Starting agent", slog.String("name", name)) |
| iomodo | 76f9a2d | 2025-07-26 12:14:40 +0400 | [diff] [blame] | 197 | if err := agent.Run(); err != nil { |
| iomodo | 0c203b1 | 2025-07-26 19:44:57 +0400 | [diff] [blame] | 198 | logger.Error("Agent stopped with error", slog.String("name", name), slog.String("error", err.Error())) |
| iomodo | 76f9a2d | 2025-07-26 12:14:40 +0400 | [diff] [blame] | 199 | } |
| 200 | }(agent, config.Name) |
| 201 | } |
| 202 | |
| 203 | // Let agents run for a while |
| 204 | time.Sleep(10 * time.Minute) |
| 205 | } |