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