rdev/internal/port/code_agent.go
jordan 39df51defd feat: Add multi-provider code agent interface with Claude Code and OpenCode adapters
Implements weeks 1-4 of the multi-provider architecture:

Week 1 - Foundation:
- Add domain models (AgentProvider, AgentRequest, AgentEvent, AgentResult)
- Define CodeAgent port interface with Execute, Cancel, Capabilities
- Create thread-safe provider registry with first-registered default

Week 2 - Claude Code Adapter:
- Extract kubectl exec logic into CodeAgent implementation
- Parse stream-json output format (init, message, tool_use, result)
- Support session continuation via --resume flag

Week 3 - OpenCode Adapter:
- HTTP/SSE client for opencode serve API
- Session management (create, send message, abort)
- Event streaming with documented buffer rationale

Week 4 - Quality & Polish:
- Fix race condition in OpenCode Cancel method
- Add AgentRequest.Validate() with ErrPromptRequired, ErrInvalidTimeout
- Document DefaultAvailabilityTimeout constants
- Add HTTP error context for debugging

Also includes:
- Work queue system with PostgreSQL adapter
- Credential store for infrastructure secrets
- Project templates with Woodpecker CI integration
- Comprehensive test coverage

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 09:25:51 -07:00

62 lines
2.2 KiB
Go

// Package port defines interface contracts for external adapters.
package port
import (
"context"
"github.com/orchard9/rdev/internal/domain"
)
// CodeAgent defines operations for executing AI coding agent commands.
// Implementations handle the specifics of different agent backends
// (Claude Code CLI, OpenCode HTTP API, etc.).
type CodeAgent interface {
// Name returns a human-readable name for this agent implementation.
Name() string
// Provider returns the agent provider identifier.
Provider() domain.AgentProvider
// Execute runs an agent command and streams events to the handler.
// The handler is called for each event during execution.
// Returns the final result when execution completes.
Execute(ctx context.Context, req *domain.AgentRequest, handler domain.AgentEventHandler) (*domain.AgentResult, error)
// Cancel attempts to cancel a running agent session.
// Returns nil if cancellation was successful or session not found.
Cancel(ctx context.Context, sessionID string) error
// Capabilities returns what this agent implementation supports.
Capabilities() domain.AgentCapabilities
// Available returns true if the agent is ready to accept requests.
// This may check connectivity to external services.
Available(ctx context.Context) bool
}
// CodeAgentRegistry manages registered code agent implementations.
// It allows looking up agents by provider and setting defaults.
type CodeAgentRegistry interface {
// Register adds an agent implementation for a provider.
// Overwrites any existing registration for the same provider.
Register(agent CodeAgent)
// Get returns the agent for a specific provider.
// Returns nil if no agent is registered for that provider.
Get(provider domain.AgentProvider) CodeAgent
// Default returns the default agent implementation.
// Returns nil if no agents are registered.
Default() CodeAgent
// SetDefault sets which provider should be used as the default.
// Returns error if the provider is not registered.
SetDefault(provider domain.AgentProvider) error
// Available returns all registered providers.
Available() []domain.AgentProvider
// AvailableAgents returns all registered agents that are currently available.
AvailableAgents(ctx context.Context) []CodeAgent
}