"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetWorkspacePlanRunsOverviewToolDefinition = exports.getWorkspacePlanRunsOverviewResponseSchema = exports.getWorkspacePlanRunsOverviewParamsSchema = exports.WORKSPACE_PLAN_RUN_OVERVIEW_FAILED_SAMPLE_MAX = exports.WORKSPACE_PLAN_RUN_OVERVIEW_FAILED_SAMPLE_DEFAULT = void 0;
var zod_1 = require("zod");
var toolDefinition_js_1 = require("../toolDefinition.js");
var agentToolNames_js_1 = require("../shared/agentToolNames.js");
var GetPlanRunInfoDefinition_js_1 = require("./GetPlanRunInfoDefinition.js");
/**
 * Aligned with Core API `failed_plan_run_sample_limit` (OpenAPI `planRuns.yaml`) and server `PlanRunServiceImpl`.
 */
exports.WORKSPACE_PLAN_RUN_OVERVIEW_FAILED_SAMPLE_DEFAULT = 20;
exports.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.
 */
exports.getWorkspacePlanRunsOverviewParamsSchema = zod_1.z.object({
    failed_plan_run_sample_limit: zod_1.z
        .number()
        .int()
        .min(0)
        .max(exports.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: ".concat(exports.WORKSPACE_PLAN_RUN_OVERVIEW_FAILED_SAMPLE_DEFAULT, "."),
    }),
});
var statusCountSchema = zod_1.z.object({
    passed: zod_1.z.number(),
    failed: zod_1.z.number(),
    running: zod_1.z.number(),
    queued: zod_1.z.number(),
    cancelled: zod_1.z.number(),
    terminated: zod_1.z.number(),
});
exports.getWorkspacePlanRunsOverviewResponseSchema = zod_1.z.object({
    total_plan_runs: zod_1.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: zod_1.z.boolean().meta({
        description: 'True when aggregate counts cover the full filter (always true for the aggregation endpoint).',
    }),
    stopped_early: zod_1.z.boolean().meta({
        description: 'False for aggregation-based implementation.',
    }),
    newest_failed_plan_runs: zod_1.z
        .array(GetPlanRunInfoDefinition_js_1.planRunSummarySchema.extend({
        applicationId: zod_1.z.string().optional(),
        environmentId: zod_1.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: zod_1.z
        .array(zod_1.z.object({
        planId: zod_1.z.string(),
        planName: zod_1.z.string().optional(),
        failed_run_count: zod_1.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: zod_1.z.number().meta({
        description: 'How many newest failed runs were scanned to build top_plans_by_failed_runs.',
    }),
    top_plans_exhaustive: zod_1.z.boolean().meta({
        description: 'True when every failed run in range was included in that sample (so rankings are global within the window).',
    }),
    appliedFilters: zod_1.z
        .object({
        workspaceId: zod_1.z.string(),
        startTime: zod_1.z.string(),
        endTime: zod_1.z.string(),
        environmentId: zod_1.z.string().optional(),
        applicationId: zod_1.z.string().optional(),
    })
        .meta({ description: 'Session scope applied (same as listing tool).' }),
});
exports.GetWorkspacePlanRunsOverviewToolDefinition = (0, toolDefinition_js_1.defineTool)({
    name: agentToolNames_js_1.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.\n\nRegistered only when **workspace_results_context** has **no** **`initial_workspace_summary`** (server prefetch missed). If the summary exists, use it for counts/top plans \u2014 do not call this tool; use **query_workspace_plan_runs** for rows or failure payloads.\n\nRow tables \u2192 **query_workspace_plan_runs**. Drill-down \u2192 **get_plan_run_info** / **get_plan_run_test_runs**.",
    argumentsSchema: exports.getWorkspacePlanRunsOverviewParamsSchema,
    responseSchema: exports.getWorkspacePlanRunsOverviewResponseSchema,
});
//# sourceMappingURL=GetWorkspacePlanRunsOverviewDefinition.js.map