import { parseSkillBlock } from '../resources/skill-block.js';
export const AUTONOMOUS_STATE_CUSTOM_TYPE = 'autonomous_state';
function trimNonEmpty(value) {
    if (typeof value !== 'string')
        return undefined;
    const trimmed = value.trim();
    return trimmed.length > 0 ? trimmed : undefined;
}
function extractMessageText(message) {
    if (message.role !== 'user')
        return undefined;
    if (typeof message.content === 'string') {
        return trimNonEmpty(message.content);
    }
    const text = message.content
        .filter((part) => part.type === 'text')
        .map((part) => part.text)
        .join('\n')
        .trim();
    return text.length > 0 ? text : undefined;
}
function parseAutonomousSelectedSkill(text) {
    const parsed = parseSkillBlock(text);
    if (!parsed || parsed.name !== 'autonomous')
        return null;
    return parsed;
}
export function extractProgressFolder(query) {
    const match = query.match(/^\s*Progress folder:\s*(.+)$/im);
    return trimNonEmpty(match?.[1]);
}
export function inferAutonomousStateFromEntries(entries) {
    for (let i = entries.length - 1; i >= 0; i--) {
        const entry = entries[i];
        if (entry.type !== 'custom')
            continue;
        const customEntry = entry;
        if (customEntry.customType !== AUTONOMOUS_STATE_CUSTOM_TYPE)
            continue;
        const originalUserTask = trimNonEmpty(customEntry.data?.originalUserTask);
        if (customEntry.data?.enabled !== true || !originalUserTask) {
            return null;
        }
        return {
            enabled: true,
            originalUserTask,
            progressFolder: trimNonEmpty(customEntry.data?.progressFolder)
        };
    }
    for (const entry of entries) {
        if (entry.type !== 'message')
            continue;
        const messageEntry = entry;
        const text = extractMessageText(messageEntry.message);
        if (!text)
            continue;
        const parsed = parseAutonomousSelectedSkill(text);
        if (!parsed?.userMessage)
            continue;
        return {
            enabled: true,
            originalUserTask: parsed.userMessage
        };
    }
    return null;
}
export function buildAutonomousRefreshPrompt(state, refresh) {
    const parts = ['/skill:autonomous', '', 'Original user task:', state.originalUserTask];
    const progressFolder = trimNonEmpty(refresh.progressFolder) ?? trimNonEmpty(state.progressFolder);
    if (progressFolder) {
        parts.push('', 'Progress folder:', progressFolder);
    }
    parts.push('', 'You are continuing an existing autonomous-mode task. See the autonomous skill for guidelines. Read plan.md and status.md in the progress folder first, treat them as the source of truth, and continue autonomously from there. Do not ask the user what to do next. Stop only if the task is complete, blocked, or a real approval boundary is hit.');
    const note = trimNonEmpty(refresh.query);
    if (note) {
        parts.push('', 'Continuation note:', note);
    }
    return parts.join('\n');
}
//# sourceMappingURL=autonomous-workflow.js.map