|
| 1 | +import { join } from "node:path"; |
| 2 | + |
| 3 | +import type { DatasetContext } from "./populate.js"; |
| 4 | +import { |
| 5 | + DefaultPopulateRecipeAuthor, |
| 6 | + FileSystemPopulateRecipeStore, |
| 7 | + MastraPopulateRecipeRuntime, |
| 8 | + SelfHealingPopulateRecipeService, |
| 9 | + type PopulateRecipeAuthor, |
| 10 | + type PopulateRecipeRunResult, |
| 11 | + type PopulateRecipeRuntime, |
| 12 | + type PopulateRecipeStore, |
| 13 | + type SelfHealingPopulateTickResult, |
| 14 | +} from "./populate-self-healing.js"; |
| 15 | + |
| 16 | +export interface PopulateDatasetRowWriter { |
| 17 | + replaceRows(input: { |
| 18 | + datasetId: string; |
| 19 | + rows: PopulateRecipeRunResult["rows"]; |
| 20 | + }): Promise<PopulateDatasetWriteResult>; |
| 21 | +} |
| 22 | + |
| 23 | +export interface PopulateDatasetWriteResult { |
| 24 | + clearedRowCount?: number; |
| 25 | + insertedRowCount: number; |
| 26 | +} |
| 27 | + |
| 28 | +export interface RunSelfHealingPopulateInput { |
| 29 | + context: DatasetContext; |
| 30 | + store?: PopulateRecipeStore; |
| 31 | + runtime?: PopulateRecipeRuntime; |
| 32 | + author?: PopulateRecipeAuthor; |
| 33 | + rowWriter?: PopulateDatasetRowWriter; |
| 34 | + shouldCommitRows?: boolean; |
| 35 | + recipeStoreDirectory?: string; |
| 36 | +} |
| 37 | + |
| 38 | +export interface RunSelfHealingPopulateResult { |
| 39 | + success: boolean; |
| 40 | + action: SelfHealingPopulateTickResult["action"]; |
| 41 | + datasetId: string; |
| 42 | + selectedRun?: PopulateRecipeRunResult; |
| 43 | + diagnosticRun?: PopulateRecipeRunResult; |
| 44 | + committedRows?: PopulateDatasetWriteResult; |
| 45 | + rejectionReasons: string[]; |
| 46 | + validationIssues: string[]; |
| 47 | + tick: SelfHealingPopulateTickResult; |
| 48 | +} |
| 49 | + |
| 50 | +export async function runSelfHealingPopulate( |
| 51 | + input: RunSelfHealingPopulateInput |
| 52 | +): Promise<RunSelfHealingPopulateResult> { |
| 53 | + if (input.shouldCommitRows && !input.rowWriter) { |
| 54 | + throw new Error("rowWriter is required when shouldCommitRows is true."); |
| 55 | + } |
| 56 | + const rowWriter = input.rowWriter; |
| 57 | + |
| 58 | + const store = input.store ?? new FileSystemPopulateRecipeStore( |
| 59 | + input.recipeStoreDirectory ?? defaultPopulateRecipeStoreDirectory() |
| 60 | + ); |
| 61 | + const service = new SelfHealingPopulateRecipeService({ |
| 62 | + store, |
| 63 | + runtime: input.runtime ?? new MastraPopulateRecipeRuntime(), |
| 64 | + author: input.author ?? new DefaultPopulateRecipeAuthor(), |
| 65 | + }); |
| 66 | + const tick = await service.tick({ |
| 67 | + datasetId: input.context.datasetId, |
| 68 | + context: input.context, |
| 69 | + }); |
| 70 | + const selectedRun = successfulRunForTick(tick); |
| 71 | + const diagnosticRun = diagnosticRunForTick(tick); |
| 72 | + let committedRows: PopulateDatasetWriteResult | undefined; |
| 73 | + |
| 74 | + if (input.shouldCommitRows && selectedRun && rowWriter) { |
| 75 | + committedRows = await rowWriter.replaceRows({ |
| 76 | + datasetId: input.context.datasetId, |
| 77 | + rows: selectedRun.rows, |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + return { |
| 82 | + success: Boolean(selectedRun), |
| 83 | + action: tick.action, |
| 84 | + datasetId: input.context.datasetId, |
| 85 | + selectedRun, |
| 86 | + diagnosticRun, |
| 87 | + committedRows, |
| 88 | + rejectionReasons: tick.rejectionReasons, |
| 89 | + validationIssues: validationIssuesForSelfHealingTick(tick), |
| 90 | + tick, |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +export function successfulRunForTick( |
| 95 | + tick: SelfHealingPopulateTickResult |
| 96 | +): PopulateRecipeRunResult | undefined { |
| 97 | + if (tick.action === "active_rerun_succeeded") { |
| 98 | + return tick.activeRun; |
| 99 | + } |
| 100 | + if ( |
| 101 | + tick.action === "generated_initial_recipe" || |
| 102 | + tick.action === "repaired_active_recipe" |
| 103 | + ) { |
| 104 | + return tick.candidateRun; |
| 105 | + } |
| 106 | + return undefined; |
| 107 | +} |
| 108 | + |
| 109 | +export function diagnosticRunForTick( |
| 110 | + tick: SelfHealingPopulateTickResult |
| 111 | +): PopulateRecipeRunResult | undefined { |
| 112 | + return successfulRunForTick(tick) ?? tick.candidateRun ?? tick.activeRun; |
| 113 | +} |
| 114 | + |
| 115 | +export function validationIssuesForSelfHealingTick( |
| 116 | + tick: SelfHealingPopulateTickResult |
| 117 | +): string[] { |
| 118 | + const run = diagnosticRunForTick(tick); |
| 119 | + return Array.from(new Set([ |
| 120 | + ...(run?.validationIssues ?? []), |
| 121 | + ...(run?.productionValidation.criticalIssues ?? []), |
| 122 | + ...tick.rejectionReasons, |
| 123 | + ])); |
| 124 | +} |
| 125 | + |
| 126 | +function defaultPopulateRecipeStoreDirectory(): string { |
| 127 | + return join(process.cwd(), ".bigset", "populate-recipes"); |
| 128 | +} |
0 commit comments