| 12 | export type PlanFilePath = string | null; |
| 13 | |
| 14 | export class PlanMode { |
| 15 | protected _isActive = false; |
| 16 | protected _planId: null | string = null; |
| 17 | protected _planFilePath: PlanFilePath = null; |
| 18 | |
| 19 | constructor(protected readonly agent: Agent) {} |
| 20 | |
| 21 | createPlanId(): string { |
| 22 | return generateHeroSlug(randomUUID(), new Set()); |
| 23 | } |
| 24 | |
| 25 | async enter(id = this.createPlanId(), createFile = false, emitStatus = true): Promise<void> { |
| 26 | if (this._isActive) { |
| 27 | throw new Error('Already in plan mode'); |
| 28 | } |
| 29 | |
| 30 | this._isActive = true; |
| 31 | this._planId = id; |
| 32 | this._planFilePath = null; |
| 33 | |
| 34 | let enterRecorded = false; |
| 35 | try { |
| 36 | const planFilePath = this.planFilePathFor(id); |
| 37 | this._planFilePath = planFilePath; |
| 38 | await this.ensurePlanDirectory(planFilePath); |
| 39 | this.agent.records.logRecord({ type: 'plan_mode.enter', id }); |
| 40 | enterRecorded = true; |
| 41 | if (createFile) { |
| 42 | await this.writeEmptyPlanFile(planFilePath); |
| 43 | } |
| 44 | } catch (error) { |
| 45 | if (enterRecorded) { |
| 46 | this.cancel(id); |
| 47 | } else { |
| 48 | this._isActive = false; |
| 49 | this._planId = null; |
| 50 | this._planFilePath = null; |
| 51 | } |
| 52 | throw error; |
| 53 | } |
| 54 | |
| 55 | if (emitStatus) this.agent.emitStatusUpdated(); |
| 56 | } |
| 57 | |
| 58 | restoreEnter({ id }: { readonly id: string }): void { |
| 59 | this.agent.replayBuilder.push({ |
| 60 | type: 'plan_updated', |
| 61 | enabled: true, |
| 62 | }); |
| 63 | |
| 64 | this._isActive = true; |
| 65 | this._planId = id; |
| 66 | this._planFilePath = this.planFilePathFor(id); |
| 67 | } |
| 68 | |
| 69 | cancel(id?: string): void { |
| 70 | this.agent.records.logRecord({ type: 'plan_mode.cancel', id }); |
| 71 | this.agent.replayBuilder.push({ |
nothing calls this directly
no outgoing calls
no test coverage detected