import { displayAndClearSessionState, runConversationLoop, } from '../../interactive/conversationLoop.js';
import { initializeSession } from '../../interactive/sessionInitialization.js';
import { buildReplayHint, formatStepPreviews, } from '../../interactive/interactive-summary.js';
import { resolveLanguage } from '../../interactive/interactive.js';
import { buildInteractivePolicyPrompt } from '../../interactive/policyPrompt.js';
import { formatRunSessionForPrompt } from '../../interactive/runSessionReader.js';
import { resolveWorkflowConfigValues } from '../../../infra/config/index.js';
import { getLabelObject } from '../../../shared/i18n/index.js';
import { selectOption } from '../../../shared/prompt/index.js';
import { loadTemplate } from '../../../shared/prompts/index.js';
import { blankLine, info } from '../../../shared/ui/index.js';
const DIRECT_INSTRUCT_TOOLS = ['Read', 'Glob', 'Grep', 'Bash', 'WebSearch', 'WebFetch'];
function buildDirectInstructTemplateVars(options, lang) {
    const hasWorkflowPreview = options.workflowContext.stepPreviews !== undefined
        && options.workflowContext.stepPreviews.length > 0;
    const runPromptVars = formatRunSessionForPrompt(options.runSessionContext);
    return {
        runSlug: options.runSlug,
        taskContent: options.taskContent,
        hasWorkflowPreview,
        workflowStructure: options.workflowContext.workflowStructure,
        stepDetails: hasWorkflowPreview
            ? formatStepPreviews(options.workflowContext.stepPreviews, lang)
            : '',
        ...runPromptVars,
        hasOrderContent: options.previousOrderContent !== null,
        orderContent: options.previousOrderContent ?? '',
    };
}
function createDirectSelectAction(ui) {
    return async (task) => {
        blankLine();
        info(ui.proposed);
        info(task);
        return selectOption(ui.actionPrompt, [
            { label: ui.actions.execute, value: 'execute' },
            { label: ui.actions.continue, value: 'continue' },
        ]);
    };
}
export async function runDirectInstructMode(options) {
    const globalConfig = resolveWorkflowConfigValues(options.cwd, ['language', 'provider']);
    const lang = resolveLanguage(globalConfig.language);
    if (!globalConfig.provider) {
        throw new Error('Provider is not configured.');
    }
    const baseCtx = initializeSession(options.cwd, 'instruct');
    const ctx = { ...baseCtx, lang, personaName: 'instruct' };
    displayAndClearSessionState(options.cwd, ctx.lang);
    const ui = getLabelObject('instruct.ui', ctx.lang);
    const systemPrompt = loadTemplate('score_direct_instruct_system_prompt', ctx.lang, buildDirectInstructTemplateVars(options, ctx.lang));
    const replayHint = buildReplayHint(ctx.lang, options.previousOrderContent !== null);
    const strategy = {
        systemPrompt,
        allowedTools: DIRECT_INSTRUCT_TOOLS,
        transformPrompt: (userMessage, sourceContext) => buildInteractivePolicyPrompt(ctx.lang, userMessage, sourceContext),
        introMessage: `${ui.intro}${replayHint}`,
        selectAction: createDirectSelectAction(ui),
        previousOrderContent: options.previousOrderContent ?? undefined,
    };
    const result = await runConversationLoop(options.cwd, ctx, strategy, options.workflowContext, undefined);
    if (result.action === 'cancel') {
        return { action: 'cancel', task: '' };
    }
    return { action: 'execute', task: result.task };
}
//# sourceMappingURL=directInstructMode.js.map