/**
 * Workbench GOA orchestrator context (VIGTHORIA_DECK_MODE / comfort prompt auto-route).
 * Emitted as: @vigthoria-goa:{json}
 */
const GOA_PREFIX = '@vigthoria-goa:';
export function parseGoaLine(line) {
    const trimmed = line.trim();
    if (!trimmed.startsWith(GOA_PREFIX))
        return null;
    try {
        return JSON.parse(trimmed.slice(GOA_PREFIX.length));
    }
    catch {
        return null;
    }
}
export function stripGoaLines(text) {
    return text
        .split('\n')
        .filter((line) => !line.trim().startsWith(GOA_PREFIX))
        .join('\n');
}
/** Split Workbench-injected GOA prefix from the user prompt. */
export function splitGoaContextFromInput(input) {
    const lines = input.split('\n');
    let goaContext = null;
    const rest = [];
    for (const line of lines) {
        const parsed = parseGoaLine(line);
        if (parsed && !goaContext) {
            goaContext = parsed;
            continue;
        }
        rest.push(line);
    }
    return { goaContext, userPrompt: rest.join('\n').trim() };
}
export function formatGoaSystemGrounding(context) {
    const parts = [
        'VIGTHORIA GOA ORCHESTRATOR CONTEXT (pre-executed by Workbench — do not re-scaffold greenfield templates):',
        `- workspace_type: ${context.workspace_type || 'unknown'}`,
    ];
    if (context.executed === true) {
        parts.push('- Workbench already executed the GOA payload (template write / AST pre-scan / stabilize).');
        parts.push('- Continue from the current workspace state; prefer surgical edits over full rewrites.');
    }
    if (context.analysis && typeof context.analysis === 'object') {
        const a = context.analysis;
        if (typeof a.detected_framework === 'string')
            parts.push(`- framework: ${a.detected_framework}`);
        if (Array.isArray(a.target_files) && a.target_files.length) {
            parts.push(`- target_files: ${a.target_files.slice(0, 6).join(', ')}`);
        }
        if (typeof a.anchor_hint === 'string')
            parts.push(`- anchor: ${a.anchor_hint}`);
        if (typeof a.template_match === 'string')
            parts.push(`- template_match: ${a.template_match}`);
    }
    if (typeof context.instruction_count === 'number') {
        parts.push(`- instructions_planned: ${context.instruction_count}`);
    }
    return parts.join('\n');
}
