import { z } from "zod";
import { sendInteractiveMessageChannel } from "../../../state-machine/helpers/peer.js";
import { createToolFactory, PermissionLevel, ToolCategory } from "../types.js";
/**
 * Ask Multi-Choice - presents multiple choice options to the user.
 */
export const askMultiChoiceToolFactory = createToolFactory("askMultiChoice", ({ clark, services, }) => {
    const { chatSessionStore } = services;
    return {
        description: `
This tool presents the user with a question and multiple choice options.
Even for seemingly open-ended questions, you should come up with 2-3 suggested choices to make it easier for the user.

For open-ended questions:
- Come up with 2-3 reasonable suggestions based on common patterns
- If there are many possible options, present the 3 most common/relevant ones
- You may use more than 3 choices (up to 6) ONLY when all options are genuinely distinct and necessary
- If you need to present more choices, add "More choices" as the last option, which allows you to ask the same question again with a different set of choices
- The user can also type their own response directly instead of selecting from the choices

When NOT to use this tool:
- If the user points out a mistake or bug, don't ask them how to fix it; instead, if in BUILD mode, fix it directly. If in PLAN mode, call exitPlanMode with a (very) brief plan to address it.
- Don't ask the user for input on low-level implementation details.
- Do not use this for remembering application knowledge. Use the rememberKnowledge tool instead.

IMPORTANT: When asking about datasources/integrations:
- FIRST use the searchIntegrations tool to discover what datasources are available
- THEN present the available options to the user in your multi-choice question
- DON'T ask the user about datasources without first checking what's available
- DON'T ask the user about metadata/schemas without first checking what's available (use readIntegrationMetadata tool)
- NEVER put a "custom" or "provide your own" option; this is redundant since the user can always type their own response

After the user makes a selection:
- ALWAYS acknowledge their selection in your next message (e.g., "Got it, I'll use PostgreSQL" or "Understood, I'll create a dashboard layout")
- Then proceed with the task based on their choice

The user will select one (or more) of the choices, and their selection will be sent back to you as a message.
`,
        category: ToolCategory.GENERAL,
        defaultPermission: PermissionLevel.ALLOW,
        readOnly: true,
        inputSchema: z.object({
            question: z
                .string()
                .describe("The question to ask the user. Supports markdown formatting."),
            choices: z
                .array(z.string())
                .min(2)
                .max(6)
                .describe('An array of choice strings, e.g. ["Option A", "Option B", "Option C"]. STRONGLY prefer 3 choices when possible. Use more than 3 only when all options are genuinely necessary and cannot be grouped or simplified. Each element should be a clear, concise option without markdown formatting. If there are more options to present, include "More choices" as the last element to allow presenting additional choices in a follow-up question.'),
            selectionType: z
                .enum(["single", "multiple"])
                .default("single")
                .optional()
                .describe('Selection mode: "single" (user picks one option) or "multiple" (user can select multiple options). Default is "single".'),
        }),
        execute: async ({ question, choices, selectionType }) => {
            // Use sendUserMessageChannel to ensure ID is generated and shared properly
            const sendInteractiveMessage = sendInteractiveMessageChannel(clark, chatSessionStore);
            const result = await sendInteractiveMessage({
                role: "assistant",
                type: "multi_choice",
                content: question,
                group: "clark",
                choices,
                selectionType,
            });
            if (!result.success) {
                return {
                    success: false,
                    message: "Cannot ask question because there is already an active message that requires user input.",
                };
            }
            return {
                success: true,
                message: `Presented ${choices?.length ?? 0} choices to user. Awaiting their selection.`,
            };
        },
        getSummary: (input, output) => {
            const choiceCount = input.choices?.length ?? 0;
            const preview = input.question.length > 40
                ? input.question.substring(0, 40) + "..."
                : input.question;
            return {
                input: `"${preview}" (${choiceCount} choices)`,
                output: output.message ?? "Awaiting user selection",
            };
        },
    };
});
//# sourceMappingURL=ask-multi-choice.js.map