|
| 1 | +import type { OpenGoatPaths } from "../../domain/opengoat-paths.js"; |
| 2 | +import type { ArtifactService } from "../../artifacts/application/artifact.service.js"; |
| 3 | +import type { RunService } from "../../runs/application/run.service.js"; |
| 4 | +import type { RunRecord } from "../../runs/domain/run.js"; |
| 5 | +import type { PlaybookRegistryService } from "./playbook-registry.service.js"; |
| 6 | +import { matchArtifactsToExpected } from "./artifact-phase-matcher.js"; |
| 7 | + |
| 8 | +export interface StartPlaybookOptions { |
| 9 | + playbookId: string; |
| 10 | + projectId: string; |
| 11 | + objectiveId: string; |
| 12 | +} |
| 13 | + |
| 14 | +export interface PhaseProgressResult { |
| 15 | + advanced: boolean; |
| 16 | + completed: boolean; |
| 17 | + run: RunRecord; |
| 18 | +} |
| 19 | + |
| 20 | +export interface PhaseProgress { |
| 21 | + name: string; |
| 22 | + description: string; |
| 23 | + status: "completed" | "current" | "upcoming"; |
| 24 | + specialistId?: string; |
| 25 | + expectedArtifacts: string[]; |
| 26 | + matchedArtifacts: string[]; |
| 27 | + missingArtifacts: string[]; |
| 28 | +} |
| 29 | + |
| 30 | +export interface PlaybookProgress { |
| 31 | + runId: string; |
| 32 | + playbookId: string; |
| 33 | + playbookTitle: string; |
| 34 | + currentPhase: string; |
| 35 | + runStatus: string; |
| 36 | + phases: PhaseProgress[]; |
| 37 | +} |
| 38 | + |
| 39 | +interface PlaybookExecutionServiceDeps { |
| 40 | + runService: RunService; |
| 41 | + artifactService: ArtifactService; |
| 42 | + playbookRegistryService: PlaybookRegistryService; |
| 43 | +} |
| 44 | + |
| 45 | +export class PlaybookExecutionService { |
| 46 | + private readonly runService: RunService; |
| 47 | + private readonly artifactService: ArtifactService; |
| 48 | + private readonly playbookRegistryService: PlaybookRegistryService; |
| 49 | + |
| 50 | + constructor(deps: PlaybookExecutionServiceDeps) { |
| 51 | + this.runService = deps.runService; |
| 52 | + this.artifactService = deps.artifactService; |
| 53 | + this.playbookRegistryService = deps.playbookRegistryService; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Start a playbook: creates a run, sets it to running, and initializes the first phase. |
| 58 | + */ |
| 59 | + async startPlaybook( |
| 60 | + paths: OpenGoatPaths, |
| 61 | + options: StartPlaybookOptions, |
| 62 | + ): Promise<RunRecord> { |
| 63 | + const manifest = this.playbookRegistryService.getPlaybook(options.playbookId); |
| 64 | + const firstPhase = manifest.defaultPhases[0]; |
| 65 | + |
| 66 | + if (!firstPhase) { |
| 67 | + throw new Error(`Playbook "${options.playbookId}" has no phases`); |
| 68 | + } |
| 69 | + |
| 70 | + const run = await this.runService.createRun(paths, { |
| 71 | + projectId: options.projectId, |
| 72 | + objectiveId: options.objectiveId, |
| 73 | + playbookId: options.playbookId, |
| 74 | + title: manifest.title, |
| 75 | + startedFrom: "action", |
| 76 | + phase: firstPhase.name, |
| 77 | + phaseSummary: firstPhase.description, |
| 78 | + }); |
| 79 | + |
| 80 | + // Transition from draft → running |
| 81 | + return this.runService.updateRunStatus(paths, run.runId, "running"); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Check if the current phase's expected artifacts have been produced. |
| 86 | + * If so, advance to the next phase or complete the run. |
| 87 | + */ |
| 88 | + async checkPhaseProgress( |
| 89 | + paths: OpenGoatPaths, |
| 90 | + runId: string, |
| 91 | + ): Promise<PhaseProgressResult> { |
| 92 | + const run = await this.runService.getRun(paths, runId); |
| 93 | + |
| 94 | + if (!run.playbookId) { |
| 95 | + throw new Error("Run is not associated with a playbook"); |
| 96 | + } |
| 97 | + |
| 98 | + const manifest = this.playbookRegistryService.getPlaybook(run.playbookId); |
| 99 | + const currentPhaseIndex = manifest.defaultPhases.findIndex( |
| 100 | + (p) => p.name === run.phase, |
| 101 | + ); |
| 102 | + |
| 103 | + if (currentPhaseIndex === -1) { |
| 104 | + return { advanced: false, completed: false, run }; |
| 105 | + } |
| 106 | + |
| 107 | + const currentPhase = manifest.defaultPhases[currentPhaseIndex]!; |
| 108 | + const expectedArtifacts = currentPhase.expectedArtifacts ?? []; |
| 109 | + |
| 110 | + // If no expected artifacts, phase is automatically satisfied |
| 111 | + if (expectedArtifacts.length === 0) { |
| 112 | + return this.advanceOrComplete(paths, run, manifest, currentPhaseIndex); |
| 113 | + } |
| 114 | + |
| 115 | + // Get artifacts for this run |
| 116 | + const artifactPage = await this.artifactService.listArtifacts(paths, { |
| 117 | + runId: run.runId, |
| 118 | + }); |
| 119 | + |
| 120 | + const { missing } = matchArtifactsToExpected( |
| 121 | + artifactPage.items, |
| 122 | + expectedArtifacts, |
| 123 | + ); |
| 124 | + |
| 125 | + if (missing.length > 0) { |
| 126 | + return { advanced: false, completed: false, run }; |
| 127 | + } |
| 128 | + |
| 129 | + return this.advanceOrComplete(paths, run, manifest, currentPhaseIndex); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Get detailed progress for a playbook run. |
| 134 | + */ |
| 135 | + async getRunProgress( |
| 136 | + paths: OpenGoatPaths, |
| 137 | + runId: string, |
| 138 | + ): Promise<PlaybookProgress> { |
| 139 | + const run = await this.runService.getRun(paths, runId); |
| 140 | + |
| 141 | + if (!run.playbookId) { |
| 142 | + throw new Error("Run is not associated with a playbook"); |
| 143 | + } |
| 144 | + |
| 145 | + const manifest = this.playbookRegistryService.getPlaybook(run.playbookId); |
| 146 | + const currentPhaseIndex = manifest.defaultPhases.findIndex( |
| 147 | + (p) => p.name === run.phase, |
| 148 | + ); |
| 149 | + |
| 150 | + // Get all artifacts for the run |
| 151 | + const artifactPage = await this.artifactService.listArtifacts(paths, { |
| 152 | + runId: run.runId, |
| 153 | + }); |
| 154 | + |
| 155 | + const isCompleted = run.status === "completed"; |
| 156 | + |
| 157 | + const phases: PhaseProgress[] = manifest.defaultPhases.map((phase, index) => { |
| 158 | + const expectedArtifacts = phase.expectedArtifacts ?? []; |
| 159 | + const { matched, missing } = matchArtifactsToExpected( |
| 160 | + artifactPage.items, |
| 161 | + expectedArtifacts, |
| 162 | + ); |
| 163 | + |
| 164 | + let status: PhaseProgress["status"]; |
| 165 | + if (isCompleted || index < currentPhaseIndex) { |
| 166 | + status = "completed"; |
| 167 | + } else if (index === currentPhaseIndex) { |
| 168 | + status = isCompleted ? "completed" : "current"; |
| 169 | + } else { |
| 170 | + status = "upcoming"; |
| 171 | + } |
| 172 | + |
| 173 | + return { |
| 174 | + name: phase.name, |
| 175 | + description: phase.description, |
| 176 | + status, |
| 177 | + specialistId: phase.specialistId, |
| 178 | + expectedArtifacts, |
| 179 | + matchedArtifacts: Array.from(matched.keys()), |
| 180 | + missingArtifacts: missing, |
| 181 | + }; |
| 182 | + }); |
| 183 | + |
| 184 | + return { |
| 185 | + runId: run.runId, |
| 186 | + playbookId: run.playbookId, |
| 187 | + playbookTitle: manifest.title, |
| 188 | + currentPhase: run.phase, |
| 189 | + runStatus: run.status, |
| 190 | + phases, |
| 191 | + }; |
| 192 | + } |
| 193 | + |
| 194 | + private async advanceOrComplete( |
| 195 | + paths: OpenGoatPaths, |
| 196 | + run: RunRecord, |
| 197 | + manifest: { defaultPhases: Array<{ name: string; description: string }> }, |
| 198 | + currentPhaseIndex: number, |
| 199 | + ): Promise<PhaseProgressResult> { |
| 200 | + const isLastPhase = currentPhaseIndex === manifest.defaultPhases.length - 1; |
| 201 | + |
| 202 | + if (isLastPhase) { |
| 203 | + const completedRun = await this.runService.completeRun(paths, run.runId); |
| 204 | + return { advanced: false, completed: true, run: completedRun }; |
| 205 | + } |
| 206 | + |
| 207 | + const nextPhase = manifest.defaultPhases[currentPhaseIndex + 1]!; |
| 208 | + const advancedRun = await this.runService.advancePhase(paths, run.runId, { |
| 209 | + phase: nextPhase.name, |
| 210 | + phaseSummary: nextPhase.description, |
| 211 | + }); |
| 212 | + |
| 213 | + return { advanced: true, completed: false, run: advancedRun }; |
| 214 | + } |
| 215 | +} |
0 commit comments