"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mcpGetAiWritebackStatusStructuredOutputSchema = exports.mcpGetAiWritebackStatusArgsSchema = exports.MCP_TOOL_GET_AI_WRITEBACK_STATUS_DESCRIPTION = exports.mcpRunAiWritebackStructuredOutputSchema = exports.mcpRunAiWritebackArgsSchema = exports.MCP_TOOL_RUN_AI_WRITEBACK_DESCRIPTION = exports.isAiWritebackRunInProgress = exports.AI_WRITEBACK_RUN_TERMINAL_STATUSES = exports.AI_WRITEBACK_STAGES = void 0;
const zod_1 = require("zod");
exports.AI_WRITEBACK_STAGES = [
    'install',
    'sandbox',
    'clone',
    'agent',
    'commit',
    'push',
    'pull_request',
];
exports.AI_WRITEBACK_RUN_TERMINAL_STATUSES = ['ready', 'error'];
const isAiWritebackRunInProgress = (status) => !exports.AI_WRITEBACK_RUN_TERMINAL_STATUSES.includes(status);
exports.isAiWritebackRunInProgress = isAiWritebackRunInProgress;
exports.MCP_TOOL_RUN_AI_WRITEBACK_DESCRIPTION = `Tool: run_ai_writeback

Purpose:
Make a change to the dbt project that backs the active Lightdash project by describing it in natural language, then open a pull request with the result. The target GitHub repository and dbt sub-folder are resolved server-side from the active project's dbt connection — you never specify them.

How it works:
- This tool starts the run and returns immediately with an aiWritebackRunUuid — it does NOT wait for the run to finish.
- In the background: a sandbox is created, the project's GitHub repository is cloned, and the prompt is executed by the Claude Code CLI against the dbt project. If the agent changes any files, a branch is committed, pushed, and a pull request is opened.
- Call get_ai_writeback_status with the returned aiWritebackRunUuid to check progress and get the pull request URL once the run finishes. The run typically takes a few minutes (cloning, running the agent, opening the PR) — poll every 10-15 seconds rather than immediately looping.

Requirements:
- An active project must be set first via set_project (or the X-Lightdash-Project header).
- The project's dbt connection must be GitHub-backed, and the organization must have the GitHub App installed.
- The AI writeback feature must be enabled for the organization.

Important:
- This tool is NOT read-only and NOT idempotent — each call can start a run that opens a new pull request. Use it only when the user explicitly wants to change their dbt project.
- Some projects have more than one dbt source. If the prompt doesn't make clear which one to change, the run finishes with status "error" and an error message listing the sources by name and repository — call run_ai_writeback again naming the intended source in the prompt itself (e.g. "In jaffle-2, add ..."). You never pass an id.

Parameters:
- prompt: A clear, self-contained description of the change to make to the dbt project (e.g. "Add a 'total_revenue' metric to the orders model as the sum of amount"). When the project has more than one dbt source, name the intended source here (e.g. "In the marketing dbt project, ...").

Response shape (MCP CallToolResult):
- content: [{ type: "text", text: "<human-readable message telling you the run started and to poll get_ai_writeback_status>" }]
- structuredContent: {
    aiWritebackRunUuid: string   // pass this to get_ai_writeback_status
  }
`;
exports.mcpRunAiWritebackArgsSchema = zod_1.z.object({
    prompt: zod_1.z
        .string()
        .min(1)
        .describe('A clear, self-contained description of the change to make to the dbt project that backs the active Lightdash project. If the project has more than one dbt source, name the intended one here (e.g. "In jaffle-2, add ...") — a later get_ai_writeback_status call reports whether the run could tell which source you meant.'),
});
exports.mcpRunAiWritebackStructuredOutputSchema = zod_1.z.object({
    aiWritebackRunUuid: zod_1.z
        .string()
        .describe('Id of the writeback run that just started. Pass this to get_ai_writeback_status to check progress and get the pull request URL.'),
});
exports.MCP_TOOL_GET_AI_WRITEBACK_STATUS_DESCRIPTION = `Tool: get_ai_writeback_status

Purpose:
Check the status of a writeback run started by run_ai_writeback, and get the pull request URL once it's ready.

Important:
- Poll every 10-15 seconds rather than immediately looping — a run typically takes a few minutes.
- "status" is either "pending" (not yet picked up), an in-progress pipeline stage (e.g. "sandbox", "agent", "pull_request"), or a terminal value: "ready" (finished — check prUrl) or "error" (finished — check errorMessage; this also covers the "more than one dbt source" case described in run_ai_writeback).

Parameters:
- aiWritebackRunUuid: The id returned by run_ai_writeback.

Response shape (MCP CallToolResult):
- content: [{ type: "text", text: "<human-readable status summary>" }]
- structuredContent: {
    status:       string,        // "pending" | a pipeline stage | "ready" | "error"
    prUrl:        string | null, // set once status is "ready" and a PR was opened
    errorMessage: string | null  // set once status is "error"
  }
`;
exports.mcpGetAiWritebackStatusArgsSchema = zod_1.z.object({
    aiWritebackRunUuid: zod_1.z
        .string()
        .uuid()
        .describe('The id returned by run_ai_writeback.'),
});
exports.mcpGetAiWritebackStatusStructuredOutputSchema = zod_1.z.object({
    status: zod_1.z
        .string()
        .describe('"pending" | a pipeline stage (e.g. "sandbox", "agent", "pull_request") | "ready" | "error".'),
    prUrl: zod_1.z
        .string()
        .nullable()
        .describe('URL of the pull request opened from the agent changes, set once status is "ready"; null if the agent made no file changes or the run has not finished yet.'),
    errorMessage: zod_1.z
        .string()
        .nullable()
        .describe('Set once status is "error"; null otherwise.'),
});
//# sourceMappingURL=types.js.map