/**
 * Interactive task input mode
 *
 * Allows users to refine task requirements through conversation with AI
 * before executing the task. Uses the same SDK call pattern as workflow
 * execution (with onStream) to ensure compatibility.
 *
 * Commands:
 *   /go     - Confirm and execute the task
 *   /cancel - Cancel and exit
 */
import { getLabel, getLabelObject } from '../../shared/i18n/index.js';
import { loadTemplate } from '../../shared/prompts/index.js';
import { displayAndClearSessionState, runConversationLoop, } from './conversationLoop.js';
import { buildInteractivePolicyPrompt } from './policyPrompt.js';
import { initializeSession } from './sessionInitialization.js';
import { loadAssistantInitContext } from './assistantInitFiles.js';
import { formatStepPreviews, buildSummaryActionOptions, selectSummaryAction, } from './interactive-summary.js';
import { formatRunSessionForPrompt } from './runSessionReader.js';
/**
 * Format session state for display
 */
export function formatSessionStatus(state, lang) {
    const lines = [];
    // Status line
    if (state.status === 'success') {
        lines.push(getLabel('interactive.previousTask.success', lang));
    }
    else if (state.status === 'error') {
        lines.push(getLabel('interactive.previousTask.error', lang, {
            error: state.errorMessage,
        }));
    }
    else if (state.status === 'user_stopped') {
        lines.push(getLabel('interactive.previousTask.userStopped', lang));
    }
    // Workflow name
    lines.push(getLabel('interactive.previousTask.workflow', lang, {
        workflowName: state.workflowName,
    }));
    // Timestamp
    const timestamp = new Date(state.timestamp).toLocaleString(lang === 'ja' ? 'ja-JP' : 'en-US');
    lines.push(getLabel('interactive.previousTask.timestamp', lang, {
        timestamp,
    }));
    return lines.join('\n');
}
export function resolveLanguage(lang) {
    return lang === 'ja' ? 'ja' : 'en';
}
/** Default toolset for interactive mode */
export const DEFAULT_INTERACTIVE_TOOLS = ['Read', 'Glob', 'Grep', 'Bash', 'WebSearch', 'WebFetch'];
/**
 * Build the summary prompt (used as both system prompt and user message).
 */
export { buildSummaryPrompt, formatStepPreviews, } from './interactive-summary.js';
export async function interactiveMode(cwd, initialInput, workflowContext, sessionId, runSessionContext, options) {
    const baseCtx = initializeSession(cwd, 'interactive', {
        provider: options?.provider,
        model: options?.model,
    });
    const ctx = sessionId ? { ...baseCtx, sessionId } : baseCtx;
    displayAndClearSessionState(cwd, ctx.lang);
    const hasPreview = !!workflowContext?.stepPreviews?.length;
    const hasRunSession = !!runSessionContext;
    const runPromptVars = hasRunSession
        ? formatRunSessionForPrompt(runSessionContext)
        : { runTask: '', runWorkflow: '', runStatus: '', runStepLogs: '', runReports: '' };
    const systemPrompt = loadTemplate('score_interactive_system_prompt', ctx.lang, {
        hasWorkflowPreview: hasPreview,
        workflowStructure: workflowContext?.workflowStructure ?? '',
        stepDetails: hasPreview ? formatStepPreviews(workflowContext.stepPreviews, ctx.lang) : '',
        hasRunSession,
        ...runPromptVars,
    });
    const ui = getLabelObject('interactive.ui', ctx.lang);
    const assistantInitContext = loadAssistantInitContext(cwd);
    const excludeActions = options?.excludeActions;
    const selectAction = excludeActions?.length
        ? (task) => selectSummaryAction(task, ui.proposed, ui.actionPrompt, buildSummaryActionOptions({
            execute: ui.actions.execute,
            createIssue: ui.actions.createIssue,
            saveTask: ui.actions.saveTask,
            continue: ui.actions.continue,
        }, ['create_issue'], excludeActions))
        : undefined;
    return runConversationLoop(cwd, ctx, {
        systemPrompt,
        allowedTools: DEFAULT_INTERACTIVE_TOOLS,
        transformPrompt: (userMessage, sourceContext) => buildInteractivePolicyPrompt(ctx.lang, userMessage, sourceContext),
        introMessage: ui.intro,
        selectAction,
        initialPromptContext: assistantInitContext,
        summaryPromptContext: assistantInitContext,
    }, workflowContext, initialInput);
}
export { selectPostSummaryAction, buildSummaryActionOptions, selectSummaryAction, formatTaskHistorySummary, normalizeTaskHistorySummary, BASE_SUMMARY_ACTIONS, } from './interactive-summary.js';
//# sourceMappingURL=interactive.js.map