import type { PersistedRunState, RunId, RunSummary, WorkflowExecutionListingRepository, WorkflowExecutionRepository, } from "@codemation/core"; import { CoreTokens, inject, injectable } from "@codemation/core"; import type { WorkflowRunRepository as WorkflowRunRepositoryContract } from "../../domain/runs/WorkflowRunRepository"; @injectable() export class WorkflowRunRepository implements WorkflowRunRepositoryContract { constructor( @inject(CoreTokens.WorkflowExecutionRepository) private readonly workflowExecutionRepository: WorkflowExecutionRepository, ) {} async load(runId: string): Promise { return (await this.workflowExecutionRepository.load(decodeURIComponent(runId))) as PersistedRunState | undefined; } async save(state: PersistedRunState): Promise { await this.workflowExecutionRepository.save(state); } async listRuns(args: Readonly<{ workflowId?: string; limit?: number }>): Promise> { const listingStore = this.workflowExecutionRepository as unknown as Partial; if (!listingStore.listRuns) { return []; } return (await listingStore.listRuns({ workflowId: args.workflowId ? decodeURIComponent(args.workflowId) : undefined, limit: args.limit, })) as ReadonlyArray; } async deleteRun(runId: RunId): Promise { const id = decodeURIComponent(runId) as RunId; const deletable = this.workflowExecutionRepository as WorkflowExecutionRepository & { deleteRun?: (rid: RunId) => Promise; }; if (deletable.deleteRun) { await deletable.deleteRun(id); } } }