import { z } from 'zod';
import { defineTool, } from '../toolDefinition.js';
import { AgentToolNames } from '../shared/agentToolNames.js';
import { planRunSummarySchema } from './GetPlanRunInfoDefinition.js';
/**
 * Aligned with Core API `failed_plan_run_sample_limit` (OpenAPI `planRuns.yaml`) and server `PlanRunServiceImpl`.
 */
export const WORKSPACE_PLAN_RUN_OVERVIEW_FAILED_SAMPLE_DEFAULT = 20;
export const WORKSPACE_PLAN_RUN_OVERVIEW_FAILED_SAMPLE_MAX = 150;
/**
 * Aggregated workspace plan-run stats for a fixed session scope (time range, optional env/app).
 * Implemented via Core API **server-side** Datastore count aggregations (fast), plus bounded fetches for failure samples.
 */
export const getWorkspacePlanRunsOverviewParamsSchema = z.object({
    failed_plan_run_sample_limit: z
        .number()
        .int()
        .min(0)
        .max(WORKSPACE_PLAN_RUN_OVERVIEW_FAILED_SAMPLE_MAX)
        .optional()
        .meta({
        description: `How many **newest** failing plan runs to include as full summaries (ids, names, URLs). Use 0 to skip samples and only return counts. Default: ${WORKSPACE_PLAN_RUN_OVERVIEW_FAILED_SAMPLE_DEFAULT}.`,
    }),
});
const statusCountSchema = z.object({
    passed: z.number(),
    failed: z.number(),
    running: z.number(),
    queued: z.number(),
    cancelled: z.number(),
    terminated: z.number(),
});
export const getWorkspacePlanRunsOverviewResponseSchema = z.object({
    total_plan_runs: z.number().meta({
        description: 'Total plan runs matching the session filters (server-side aggregate).',
    }),
    counts_by_status: statusCountSchema.meta({
        description: 'Breakdown by normalized plan-run status (every matching run counted once on the server).',
    }),
    scan_complete: z.boolean().meta({
        description: 'True when aggregate counts cover the full filter (always true for the aggregation endpoint).',
    }),
    stopped_early: z.boolean().meta({
        description: 'False for aggregation-based implementation.',
    }),
    newest_failed_plan_runs: z
        .array(planRunSummarySchema.extend({
        applicationId: z.string().optional(),
        environmentId: z.string().optional(),
    }))
        .meta({
        description: 'Up to failed_plan_run_sample_limit failing runs, newest first. Empty when limit is 0 or there were no failures.',
    }),
    top_plans_by_failed_runs: z
        .array(z.object({
        planId: z.string(),
        planName: z.string().optional(),
        failed_run_count: z.number(),
    }))
        .meta({
        description: 'Plans with the most failures among a **bounded sample** of newest failures; see top_plans_exhaustive.',
    }),
    failures_sampled_for_top_plans: z.number().meta({
        description: 'How many newest failed runs were scanned to build top_plans_by_failed_runs.',
    }),
    top_plans_exhaustive: z.boolean().meta({
        description: 'True when every failed run in range was included in that sample (so rankings are global within the window).',
    }),
    appliedFilters: z
        .object({
        workspaceId: z.string(),
        startTime: z.string(),
        endTime: z.string(),
        environmentId: z.string().optional(),
        applicationId: z.string().optional(),
    })
        .meta({ description: 'Session scope applied (same as listing tool).' }),
});
export const GetWorkspacePlanRunsOverviewToolDefinition = defineTool({
    name: AgentToolNames.GetWorkspacePlanRunsOverview,
    description: `Workspace: **exact** \`total_plan_runs\` / \`counts_by_status\` + sampled **top_plans_by_failed_runs** / optional **newest_failed_plan_runs** (\`top_plans_exhaustive\` may be false). Fast aggregate API.

Registered only when **workspace_results_context** has **no** **\`initial_workspace_summary\`** (server prefetch missed). If the summary exists, use it for counts/top plans — do not call this tool; use **query_workspace_plan_runs** for rows or failure payloads.

Row tables → **query_workspace_plan_runs**. Drill-down → **get_plan_run_info** / **get_plan_run_test_runs**.`,
    argumentsSchema: getWorkspacePlanRunsOverviewParamsSchema,
    responseSchema: getWorkspacePlanRunsOverviewResponseSchema,
});
//# sourceMappingURL=GetWorkspacePlanRunsOverviewDefinition.js.map