|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + |
| 7 | + "intern/internal/config" |
| 8 | + "intern/internal/errors" |
| 9 | + "intern/internal/orchestrator" |
| 10 | + "intern/internal/provider" |
| 11 | + "intern/internal/repository" |
| 12 | + "intern/internal/repository/github" |
| 13 | + "intern/internal/ticketing" |
| 14 | + jiraraw "intern/internal/ticketing/jira-raw" |
| 15 | + |
| 16 | + logger "github.com/jenish-jain/logger" |
| 17 | +) |
| 18 | + |
| 19 | +// Dependencies holds all initialized dependencies for the application |
| 20 | +type Dependencies struct { |
| 21 | + Config *config.Config |
| 22 | + JiraClient interface{} // JIRA ticketing client |
| 23 | + TicketingSvc *ticketing.Service |
| 24 | + GitHubClient repository.RepositoryClient |
| 25 | + RepoSvc *repository.RepositoryService |
| 26 | + State *orchestrator.State |
| 27 | + Agent interface{} // AI agent (provider-agnostic) |
| 28 | + Coordinator *orchestrator.Coordinator |
| 29 | + RepoPaths *repository.RepositoryPath |
| 30 | +} |
| 31 | + |
| 32 | +// State represents the agent state (wrapper for orchestrator.State) |
| 33 | +type State struct { |
| 34 | + *orchestrator.State |
| 35 | +} |
| 36 | + |
| 37 | +// InitDependencies initializes all dependencies for the agent |
| 38 | +func InitDependencies(ctx context.Context) (*Dependencies, error) { |
| 39 | + // Load config |
| 40 | + cfg, err := config.LoadConfig() |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + |
| 45 | + // Initialize JIRA client |
| 46 | + jiraClient, err := jiraraw.NewRawClient(cfg.JiraURL, cfg.JiraEmail, cfg.JiraAPIToken) |
| 47 | + if err != nil { |
| 48 | + logger.Error("Failed to init JIRA client: %v", err) |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + |
| 52 | + // Health check for JIRA |
| 53 | + if err := jiraClient.HealthCheck(context.Background()); err != nil { |
| 54 | + logger.Error("JIRA health check failed: %v", err) |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + // Create ticketing service |
| 59 | + ticketingSvc := ticketing.NewService(jiraClient) |
| 60 | + |
| 61 | + // Create repository path manager |
| 62 | + workingDir := cfg.WorkingDir |
| 63 | + if workingDir == "" { |
| 64 | + workingDir = "./workspace" |
| 65 | + } |
| 66 | + |
| 67 | + repoPaths, err := repository.NewRepositoryPath(workingDir, cfg.GitHubRepo) |
| 68 | + if err != nil { |
| 69 | + logger.Error("Failed to create repository path manager", "error", err) |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + // Initialize GitHub client and repository service |
| 74 | + githubClient := github.NewClient(cfg.GitHubToken, cfg.GitHubOwner, cfg.GitHubRepo, repoPaths) |
| 75 | + repoSvc := repository.NewRepositoryService(githubClient) |
| 76 | + |
| 77 | + // Load state |
| 78 | + stateFile := "agent_state.jsonc" |
| 79 | + state := orchestrator.NewState(stateFile) |
| 80 | + |
| 81 | + // Load existing state if available |
| 82 | + // Only ignore "file not found" error (expected on first run) |
| 83 | + // Fail on other errors (permission denied, corrupted file, etc.) |
| 84 | + if err := state.Load(); err != nil && !os.IsNotExist(err) { |
| 85 | + stateErr := errors.NewStateLoadError(err, stateFile) |
| 86 | + logger.Error("Failed to load state file", stateErr.LogFields()) |
| 87 | + logger.Info("State file may be corrupted. Delete %s to reset state.", stateFile) |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + // Initialize AI agent based on configured provider |
| 92 | + agent, err := provider.NewAgent(cfg) |
| 93 | + if err != nil { |
| 94 | + logger.Error("Failed to initialize AI agent: %v", err) |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + logger.Info("Initialized AI provider", "provider", cfg.AIProvider) |
| 98 | + |
| 99 | + // Create coordinator |
| 100 | + coordinator := orchestrator.NewCoordinator(ticketingSvc, repoSvc, agent, cfg, state, repoPaths) |
| 101 | + |
| 102 | + return &Dependencies{ |
| 103 | + Config: cfg, |
| 104 | + JiraClient: jiraClient, |
| 105 | + TicketingSvc: ticketingSvc, |
| 106 | + GitHubClient: githubClient, |
| 107 | + RepoSvc: repoSvc, |
| 108 | + State: state, |
| 109 | + Agent: agent, |
| 110 | + Coordinator: coordinator, |
| 111 | + RepoPaths: repoPaths, |
| 112 | + }, nil |
| 113 | +} |
| 114 | + |
| 115 | +// InitPartialDependencies initializes only config and repository paths for lightweight commands |
| 116 | +func InitPartialDependencies() (*config.Config, *repository.RepositoryPath, error) { |
| 117 | + cfg, err := config.LoadConfig() |
| 118 | + if err != nil { |
| 119 | + return nil, nil, err |
| 120 | + } |
| 121 | + |
| 122 | + workingDir := cfg.WorkingDir |
| 123 | + if workingDir == "" { |
| 124 | + workingDir = "./workspace" |
| 125 | + } |
| 126 | + |
| 127 | + repoPaths, err := repository.NewRepositoryPath(workingDir, cfg.GitHubRepo) |
| 128 | + if err != nil { |
| 129 | + logger.Error("Failed to create repository path manager", "error", err) |
| 130 | + return nil, nil, err |
| 131 | + } |
| 132 | + |
| 133 | + return cfg, repoPaths, nil |
| 134 | +} |
| 135 | + |
| 136 | +// NewState creates a new state instance |
| 137 | +func NewState(filePath string) *State { |
| 138 | + return &State{ |
| 139 | + State: orchestrator.NewState(filePath), |
| 140 | + } |
| 141 | +} |
0 commit comments