import { z } from 'zod';
import { defineTool, } from '../toolDefinition.js';
import { AgentToolNames } from '../shared/agentToolNames.js';
/**
 * GetRelatedPlanRunsDefinition - Tool definition for fetching historical plan runs for comparison.
 */
export const getRelatedPlanRunsParamsSchema = z.object({
    plan_run_id: z.string().meta({
        description: 'Required. Plan run ID to find related runs for. When analyzing deployments, use plan_run_id from get_deployment_event_plan_runs results.',
    }),
    limit: z.number().optional().meta({
        description: 'Maximum number of plan runs to return. Default: 10, Max: 50',
    }),
    filter_status: z.enum(['all', 'passed', 'failed']).optional().meta({
        description: 'Filter plan runs by status. Default: all',
    }),
    include_current: z.boolean().optional().meta({
        description: 'Whether to include the current plan run in results. Default: false',
    }),
    include_ad_hoc: z.boolean().optional().meta({
        description: 'Whether to include ad-hoc runs. Default: true',
    }),
    time_range: z.enum(['recent', 'day', 'week', 'month']).optional().meta({
        description: 'Time range to filter runs. "recent" returns most recent regardless of time, "day" returns from last 24 hours, "week" from last 7 days, "month" from last 30 days. Default: recent',
    }),
});
// Schema for plan run info
export const planRunInfoSchema = z.object({
    planRunId: z.string().meta({ description: 'Plan run ID' }),
    status: z.enum(['passed', 'failed', 'running', 'cancelled', 'terminated']).meta({ description: 'Plan run status' }),
    startTime: z.string().optional().meta({ description: 'Start time as ISO string' }),
    endTime: z.string().optional().meta({ description: 'End time as ISO string' }),
    durationMs: z.number().optional().meta({ description: 'Duration in milliseconds' }),
    testCounts: z
        .object({
        total: z.number(),
        passed: z.number(),
        failed: z.number(),
    })
        .optional()
        .meta({ description: 'Test counts for this plan run' }),
    passRate: z.number().optional().meta({ description: 'Pass rate as a percentage (0-100)' }),
    triggerType: z.string().optional().meta({ description: 'How the plan run was triggered' }),
});
// Schema for insights
export const planRunInsightsSchema = z.object({
    totalRuns: z.number().meta({ description: 'Total number of runs analyzed' }),
    averagePassRate: z.number().optional().meta({ description: 'Average pass rate across all runs' }),
    averageDurationMs: z.number().optional().meta({ description: 'Average duration in milliseconds' }),
    minDurationMs: z.number().optional().meta({ description: 'Minimum duration in milliseconds' }),
    maxDurationMs: z.number().optional().meta({ description: 'Maximum duration in milliseconds' }),
    consecutivePasses: z.number().optional().meta({ description: 'Number of consecutive passing runs from current' }),
    consecutiveFailures: z.number().optional().meta({ description: 'Number of consecutive failing runs from current' }),
    lastPassedRun: planRunInfoSchema.optional().meta({ description: 'Most recent passing plan run' }),
    trend: z.enum(['improving', 'declining', 'stable', 'unknown']).optional().meta({
        description: 'Trend based on recent pass rates',
    }),
    isFlaky: z.boolean().optional().meta({ description: 'Whether the plan appears to be flaky' }),
    flakinessNote: z.string().optional().meta({ description: 'Explanation of flakiness detection result' }),
});
export const getRelatedPlanRunsResponseSchema = z.object({
    currentRun: planRunInfoSchema.meta({ description: 'The current plan run being analyzed' }),
    previousRuns: z.array(planRunInfoSchema).meta({ description: 'Historical plan runs for comparison (newest first)' }),
    insights: planRunInsightsSchema.meta({ description: 'Insights from analyzing plan run history' }),
});
export const GetRelatedPlanRunsToolDefinition = defineTool({
    name: AgentToolNames.GetRelatedPlanRuns,
    description: `Retrieves historical plan runs for comparison and trend analysis.

Use this tool to:
- Compare the current plan run with previous runs
- Identify trends (improving, declining, stable)
- Find when failures started occurring
- Calculate average pass rates and durations
- Detect if failures are new or recurring`,
    argumentsSchema: getRelatedPlanRunsParamsSchema,
    responseSchema: getRelatedPlanRunsResponseSchema,
});
//# sourceMappingURL=GetRelatedPlanRunsDefinition.js.map