|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/aoagents/agent-orchestrator/backend/internal/domain" |
| 8 | + "github.com/aoagents/agent-orchestrator/backend/internal/ports" |
| 9 | + sessionmanager "github.com/aoagents/agent-orchestrator/backend/internal/session_manager" |
| 10 | +) |
| 11 | + |
| 12 | +// SessionStore is the persistence surface needed to assemble controller-facing session read models. |
| 13 | +type SessionStore interface { |
| 14 | + sessionmanager.Store |
| 15 | + GetDisplayPRFactsForSession(ctx context.Context, id domain.SessionID) (domain.PRFacts, bool, error) |
| 16 | +} |
| 17 | + |
| 18 | +// Session is the controller-facing session service. It delegates command-side |
| 19 | +// session operations to the internal sessionmanager.Manager and owns read-model |
| 20 | +// assembly, including user-facing display status derivation. |
| 21 | +type Session struct { |
| 22 | + manager *sessionmanager.Manager |
| 23 | + store SessionStore |
| 24 | +} |
| 25 | + |
| 26 | +// NewSession wires a controller-facing session service over an internal session Manager. |
| 27 | +func NewSession(manager *sessionmanager.Manager, store SessionStore) *Session { |
| 28 | + return &Session{manager: manager, store: store} |
| 29 | +} |
| 30 | + |
| 31 | +// Spawn creates a session and returns the API-facing read model. |
| 32 | +func (s *Session) Spawn(ctx context.Context, cfg ports.SpawnConfig) (domain.Session, error) { |
| 33 | + rec, err := s.manager.Spawn(ctx, cfg) |
| 34 | + if err != nil { |
| 35 | + return domain.Session{}, err |
| 36 | + } |
| 37 | + return s.toSession(ctx, rec) |
| 38 | +} |
| 39 | + |
| 40 | +// Restore relaunches a terminated session and returns the API-facing read model. |
| 41 | +func (s *Session) Restore(ctx context.Context, id domain.SessionID) (domain.Session, error) { |
| 42 | + rec, err := s.manager.Restore(ctx, id) |
| 43 | + if err != nil { |
| 44 | + return domain.Session{}, err |
| 45 | + } |
| 46 | + return s.toSession(ctx, rec) |
| 47 | +} |
| 48 | + |
| 49 | +// Kill delegates terminal intent and teardown to the internal manager. |
| 50 | +func (s *Session) Kill(ctx context.Context, id domain.SessionID) (bool, error) { |
| 51 | + return s.manager.Kill(ctx, id) |
| 52 | +} |
| 53 | + |
| 54 | +// Send delegates agent messaging to the internal manager. |
| 55 | +func (s *Session) Send(ctx context.Context, id domain.SessionID, message string) error { |
| 56 | + return s.manager.Send(ctx, id, message) |
| 57 | +} |
| 58 | + |
| 59 | +// Cleanup delegates terminal workspace cleanup to the internal manager. |
| 60 | +func (s *Session) Cleanup(ctx context.Context, project domain.ProjectID) ([]domain.SessionID, error) { |
| 61 | + return s.manager.Cleanup(ctx, project) |
| 62 | +} |
| 63 | + |
| 64 | +// List returns the project's sessions as enriched display models. |
| 65 | +func (s *Session) List(ctx context.Context, project domain.ProjectID) ([]domain.Session, error) { |
| 66 | + recs, err := s.store.ListSessions(ctx, project) |
| 67 | + if err != nil { |
| 68 | + return nil, fmt.Errorf("list %s: %w", project, err) |
| 69 | + } |
| 70 | + out := make([]domain.Session, 0, len(recs)) |
| 71 | + for _, rec := range recs { |
| 72 | + sess, err := s.toSession(ctx, rec) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + out = append(out, sess) |
| 77 | + } |
| 78 | + return out, nil |
| 79 | +} |
| 80 | + |
| 81 | +// Get returns one session as an enriched display model, or sessionmanager.ErrNotFound if it is absent. |
| 82 | +func (s *Session) Get(ctx context.Context, id domain.SessionID) (domain.Session, error) { |
| 83 | + rec, ok, err := s.store.GetSession(ctx, id) |
| 84 | + if err != nil { |
| 85 | + return domain.Session{}, fmt.Errorf("get %s: %w", id, err) |
| 86 | + } |
| 87 | + if !ok { |
| 88 | + return domain.Session{}, fmt.Errorf("get %s: %w", id, sessionmanager.ErrNotFound) |
| 89 | + } |
| 90 | + return s.toSession(ctx, rec) |
| 91 | +} |
| 92 | + |
| 93 | +func (s *Session) toSession(ctx context.Context, rec domain.SessionRecord) (domain.Session, error) { |
| 94 | + pr, ok, err := s.store.GetDisplayPRFactsForSession(ctx, rec.ID) |
| 95 | + if err != nil { |
| 96 | + return domain.Session{}, fmt.Errorf("pr facts %s: %w", rec.ID, err) |
| 97 | + } |
| 98 | + if !ok { |
| 99 | + return domain.Session{SessionRecord: rec, Status: deriveStatus(rec, nil)}, nil |
| 100 | + } |
| 101 | + return domain.Session{SessionRecord: rec, Status: deriveStatus(rec, &pr)}, nil |
| 102 | +} |
0 commit comments