/**
 * Instruct mode for branch-based tasks.
 *
 * Provides conversation loop for additional instructions on existing branches,
 * similar to interactive mode but with branch context and limited actions.
 */
import { displayAndClearSessionState, runConversationLoop, } from '../../interactive/conversationLoop.js';
import { initializeSession } from '../../interactive/sessionInitialization.js';
import { resolveLanguage, formatStepPreviews, } from '../../interactive/interactive.js';
import { buildInteractivePolicyPrompt } from '../../interactive/policyPrompt.js';
import { createSelectActionWithoutExecute, buildReplayHint } from '../../interactive/interactive-summary.js';
import { formatRunSessionForPrompt } from '../../interactive/runSessionReader.js';
import { loadTemplate } from '../../../shared/prompts/index.js';
import { getLabelObject } from '../../../shared/i18n/index.js';
import { resolveWorkflowConfigValues } from '../../../infra/config/index.js';
const INSTRUCT_TOOLS = ['Read', 'Glob', 'Grep', 'Bash', 'WebSearch', 'WebFetch'];
function toInstructModeResult(result) {
    if (result.action === 'cancel') {
        return {
            action: 'cancel',
            task: '',
            ...(result.attachments ? { attachments: result.attachments } : {}),
        };
    }
    return {
        action: result.action,
        task: result.task,
        ...(result.attachments ? { attachments: result.attachments } : {}),
    };
}
function buildInstructTemplateVars(branchContext, branchName, taskName, taskContent, retryNote, lang, workflowContext, runSessionContext, previousOrderContent) {
    const hasWorkflowPreview = !!workflowContext?.stepPreviews?.length;
    const stepDetails = hasWorkflowPreview
        ? formatStepPreviews(workflowContext.stepPreviews, lang)
        : '';
    const hasRunSession = !!runSessionContext;
    const runPromptVars = hasRunSession
        ? formatRunSessionForPrompt(runSessionContext)
        : { runTask: '', runWorkflow: '', runStatus: '', runStepLogs: '', runReports: '' };
    return {
        taskName,
        taskContent,
        branchName,
        branchContext,
        retryNote,
        hasWorkflowPreview,
        workflowStructure: workflowContext?.workflowStructure ?? '',
        stepDetails,
        hasRunSession,
        ...runPromptVars,
        hasOrderContent: !!previousOrderContent,
        orderContent: previousOrderContent ?? '',
    };
}
export async function runInstructMode(cwd, branchContext, branchName, taskName, taskContent, retryNote, workflowContext, runSessionContext, previousOrderContent) {
    const globalConfig = resolveWorkflowConfigValues(cwd, ['language', 'provider']);
    const lang = resolveLanguage(globalConfig.language);
    if (!globalConfig.provider) {
        throw new Error('Provider is not configured.');
    }
    const baseCtx = initializeSession(cwd, 'instruct');
    const ctx = { ...baseCtx, lang, personaName: 'instruct' };
    displayAndClearSessionState(cwd, ctx.lang);
    const ui = getLabelObject('instruct.ui', ctx.lang);
    const templateVars = buildInstructTemplateVars(branchContext, branchName, taskName, taskContent, retryNote, lang, workflowContext, runSessionContext, previousOrderContent);
    const systemPrompt = loadTemplate('score_instruct_system_prompt', ctx.lang, templateVars);
    const replayHint = buildReplayHint(ctx.lang, !!previousOrderContent);
    const strategy = {
        systemPrompt,
        allowedTools: INSTRUCT_TOOLS,
        transformPrompt: (userMessage, sourceContext) => buildInteractivePolicyPrompt(ctx.lang, userMessage, sourceContext),
        introMessage: `${ui.intro}${replayHint}`,
        selectAction: createSelectActionWithoutExecute(ui),
        previousOrderContent: previousOrderContent ?? undefined,
    };
    const result = await runConversationLoop(cwd, ctx, strategy, workflowContext, undefined);
    return toInstructModeResult(result);
}
//# sourceMappingURL=instructMode.js.map