import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Box, Text } from 'ink';
import React from 'react';
import ToolMessage from '../components/tool-message.js';
import { ThemeContext } from '../hooks/useTheme.js';
import { jsonSchema, tool } from '../types/core.js';
import { signalQuestion } from '../utils/question-queue.js';
import { ensureString, toOptionString } from '../utils/type-helpers.js';
const executeAskQuestion = async (args) => {
    const { allowFreeform = true } = args;
    const question = ensureString(args.question);
    const options = (Array.isArray(args.options) ? args.options : []).map(toOptionString);
    if (options.length < 2 || options.length > 4) {
        return 'Error: options must contain 2-4 items.';
    }
    const answer = await signalQuestion({
        question,
        options,
        allowFreeform,
    });
    return answer;
};
const askQuestionCoreTool = tool({
    description: 'Ask the user a question with selectable options. Use when you need clarification, a decision between approaches, or user preference. The user sees the question with clickable options and can optionally type a custom answer. Returns the selected answer as a string. IMPORTANT: Never re-ask a question the user has already answered. Accept their response and proceed.',
    inputSchema: jsonSchema({
        type: 'object',
        properties: {
            question: {
                type: 'string',
                description: 'The question to ask the user.',
            },
            options: {
                type: 'array',
                // Models like MiniMax M3 emit options as {label, value} objects
                // despite the description below. The handler coerces them via
                // toOptionString, so accept objects here too rather than failing
                // validation before that coercion can run.
                items: { type: ['string', 'object'] },
                description: '2-4 selectable answer options for the user to choose from. Each option MUST be a plain string — the exact text shown to the user and returned verbatim when selected. Do not wrap options in objects or use {label, value} pairs.',
            },
            allowFreeform: {
                type: 'boolean',
                description: 'If true (default), adds a "Type custom answer..." option so the user can provide their own response.',
            },
        },
        required: ['question', 'options'],
    }),
    execute: async (args, _options) => {
        return await executeAskQuestion(args);
    },
});
const AskQuestionFormatter = React.memo(({ args, result }) => {
    const themeContext = React.useContext(ThemeContext);
    if (!themeContext) {
        throw new Error('ThemeContext not found');
    }
    const { colors } = themeContext;
    const messageContent = (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: colors.tool, children: "\u2692 ask_user" }), _jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Text, { color: colors.secondary, children: "Question:" }), _jsx(Box, { marginLeft: 2, children: _jsx(Text, { color: colors.text, children: ensureString(args.question) }) })] }), result && (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: colors.secondary, children: "Answer:" }), _jsx(Box, { marginLeft: 2, children: _jsx(Text, { color: colors.text, children: result }) })] }))] }));
    return _jsx(ToolMessage, { message: messageContent, hideBox: true });
});
const askQuestionFormatter = (args, result) => {
    if (result && result.startsWith('Error:')) {
        return _jsx(_Fragment, {});
    }
    return _jsx(AskQuestionFormatter, { args: args, result: result });
};
export const askQuestionTool = {
    name: 'ask_user',
    tool: askQuestionCoreTool,
    formatter: askQuestionFormatter,
    // Asking the user a question is itself the interaction - never gated.
    approval: false,
};
//# sourceMappingURL=ask-question.js.map