blob: 1233bdcfbc8c290a51d925b055c16edf81a461ff [file] [log] [blame]
package agent
import (
"context"
"log"
"time"
"github.com/iomodo/staff/git"
"github.com/iomodo/staff/llm"
"github.com/iomodo/staff/tm"
"github.com/iomodo/staff/tm/git_tm"
)
// ExampleAgent demonstrates how to create and run an agent
func ExampleAgent() {
// Create git interface for task management
gitInterface := git.DefaultGit("./tasks-repo")
// Create task manager
taskManager := git_tm.NewGitTaskManager(gitInterface, "./tasks-repo")
// Create LLM configuration
llmConfig := llm.Config{
Provider: llm.ProviderOpenAI,
APIKey: "your-openai-api-key-here", // Replace with actual API key
BaseURL: "https://api.openai.com/v1",
Timeout: 30 * time.Second,
}
// Create agent configuration
config := AgentConfig{
Name: "backend-engineer-1",
Role: "Backend Engineer",
GitUsername: "backend-agent",
GitEmail: "backend-agent@company.com",
WorkingDir: "./workspace",
LLMProvider: llm.ProviderOpenAI,
LLMModel: "gpt-4",
LLMConfig: llmConfig,
SystemPrompt: `You are a skilled backend engineer. Your role is to:
1. Analyze tasks and provide technical solutions
2. Write clean, maintainable code
3. Consider performance, security, and scalability
4. Provide clear documentation for your solutions
5. Follow best practices and coding standards
When responding to tasks, provide:
- Detailed technical analysis
- Code examples where appropriate
- Implementation considerations
- Testing recommendations
- Documentation suggestions`,
TaskManager: taskManager,
GitRepoPath: "./code-repo",
GitRemote: "origin",
GitBranch: "main",
}
// Create agent
agent, err := NewAgent(config)
if err != nil {
log.Fatalf("Failed to create agent: %v", err)
}
// Create a sample task
ctx := context.Background()
task, err := taskManager.CreateTask(ctx, &tm.TaskCreateRequest{
Title: "Implement user authentication API",
Description: "Create a REST API endpoint for user authentication with JWT tokens. Include login, logout, and token refresh functionality.",
OwnerID: config.Name,
Priority: tm.PriorityHigh,
})
if err != nil {
log.Fatalf("Failed to create task: %v", err)
}
log.Printf("Created task: %s", task.ID)
// Run the agent (this will process tasks in an infinite loop)
go func() {
if err := agent.Run(); err != nil {
log.Printf("Agent stopped with error: %v", err)
}
}()
// Let the agent run for a while
time.Sleep(5 * time.Minute)
// Stop the agent
agent.Stop()
}
// ExampleMultipleAgents demonstrates how to create multiple agents with different roles
func ExampleMultipleAgents() {
// Create shared git interface for task management
gitInterface := git.DefaultGit("./tasks-repo")
taskManager := git_tm.NewGitTaskManager(gitInterface, "./tasks-repo")
// Create agents with different roles
agents := []AgentConfig{
{
Name: "backend-engineer-1",
Role: "Backend Engineer",
GitUsername: "backend-agent",
GitEmail: "backend-agent@company.com",
WorkingDir: "./workspace/backend",
LLMProvider: llm.ProviderOpenAI,
LLMModel: "gpt-4",
LLMConfig: llm.Config{
Provider: llm.ProviderOpenAI,
APIKey: "your-openai-api-key",
BaseURL: "https://api.openai.com/v1",
Timeout: 30 * time.Second,
},
SystemPrompt: `You are a backend engineer. Focus on:
- API design and implementation
- Database design and optimization
- Security best practices
- Performance optimization
- Code quality and testing`,
TaskManager: taskManager,
GitRepoPath: "./code-repo",
GitRemote: "origin",
GitBranch: "main",
},
{
Name: "frontend-engineer-1",
Role: "Frontend Engineer",
GitUsername: "frontend-agent",
GitEmail: "frontend-agent@company.com",
WorkingDir: "./workspace/frontend",
LLMProvider: llm.ProviderOpenAI,
LLMModel: "gpt-4",
LLMConfig: llm.Config{
Provider: llm.ProviderOpenAI,
APIKey: "your-openai-api-key",
BaseURL: "https://api.openai.com/v1",
Timeout: 30 * time.Second,
},
SystemPrompt: `You are a frontend engineer. Focus on:
- User interface design and implementation
- React/Vue/Angular development
- Responsive design and accessibility
- Performance optimization
- User experience best practices`,
TaskManager: taskManager,
GitRepoPath: "./code-repo",
GitRemote: "origin",
GitBranch: "main",
},
{
Name: "product-manager-1",
Role: "Product Manager",
GitUsername: "pm-agent",
GitEmail: "pm-agent@company.com",
WorkingDir: "./workspace/product",
LLMProvider: llm.ProviderOpenAI,
LLMModel: "gpt-4",
LLMConfig: llm.Config{
Provider: llm.ProviderOpenAI,
APIKey: "your-openai-api-key",
BaseURL: "https://api.openai.com/v1",
Timeout: 30 * time.Second,
},
SystemPrompt: `You are a product manager. Focus on:
- Product strategy and roadmap
- User research and requirements gathering
- Feature prioritization and planning
- Stakeholder communication
- Product documentation and specifications`,
TaskManager: taskManager,
GitRepoPath: "./docs-repo",
GitRemote: "origin",
GitBranch: "main",
},
}
// Create and start all agents
for _, config := range agents {
agent, err := NewAgent(config)
if err != nil {
log.Printf("Failed to create agent %s: %v", config.Name, err)
continue
}
go func(agent *Agent, name string) {
log.Printf("Starting agent: %s", name)
if err := agent.Run(); err != nil {
log.Printf("Agent %s stopped with error: %v", name, err)
}
}(agent, config.Name)
}
// Let agents run for a while
time.Sleep(10 * time.Minute)
}