/**
 * refresh_context tool — discard current context and start fresh.
 *
 * Used by the /autonomous skill to reset context between phases.
 * The tool sets a pending refresh on the session and returns.
 * After the current agent loop ends, the interactive mode picks up
 * the pending refresh, creates a new session, and prompts with the query.
 */
import { Type } from '@sinclair/typebox';
import { VALID_THINKING_LEVELS } from '../../core/defaults.js';
import { SHORTCUT_MODEL_REF } from '../../model-ids.js';
import { REFRESH_CONTEXT } from '../../tool-names.js';
import { resolvePendingRefreshOverride } from '../session/pending-refresh-overrides.js';
const thinkingLevelSchema = Type.Union(VALID_THINKING_LEVELS.map((level) => Type.Literal(level)), { description: 'Optional thinking level override for the next session.' });
const schema = Type.Object({
    query: Type.String({
        description: 'The prompt for the next agent session. Should be self-contained — the new agent has no memory of the current conversation. Reference the progress folder for continuity.'
    }),
    progressFolder: Type.Optional(Type.String({
        description: 'Progress folder for autonomous continuation. When autonomous mode is active, pass this explicitly so the runtime can inject it into the next-session prompt.'
    })),
    model: Type.Optional(Type.String({
        description: `Optional model override for the next session. Accepts the same model patterns as CLI model selection, e.g. "${SHORTCUT_MODEL_REF.ClaudeOpus48}" or "opus".`
    })),
    thinkingLevel: Type.Optional(thinkingLevelSchema)
});
/** Build the `refresh_context` tool that schedules a fresh session for autonomous mode. */
export function createRefreshContextTool(session) {
    return {
        name: REFRESH_CONTEXT,
        label: 'Refresh Context',
        isReadOnly: false,
        description: 'Discard the current conversation context and start a fresh session with the given query. ' +
            'Use this when you have completed a phase of autonomous work and need a clean context to continue. ' +
            'After calling this tool, do NOT make any more tool calls — respond with a brief summary of what you accomplished, then stop. ' +
            'Do NOT ask the user what to do next; the refreshed session will continue automatically from the query.',
        parameters: schema,
        async execute(_toolCallId, params) {
            const currentSession = session();
            const resolvedOverride = await resolvePendingRefreshOverride(params, currentSession.modelRegistry);
            if (resolvedOverride.error) {
                return {
                    content: [
                        {
                            type: 'text',
                            text: `Error: ${resolvedOverride.error}`
                        }
                    ],
                    details: null
                };
            }
            if (currentSession.autonomousState &&
                !params.progressFolder &&
                !currentSession.autonomousState.progressFolder) {
                return {
                    content: [
                        {
                            type: 'text',
                            text: 'Error: autonomous refresh requires a progressFolder. Pass it explicitly when calling refresh_context so the runtime can continue reliably.'
                        }
                    ],
                    details: null
                };
            }
            currentSession.setPendingRefresh({
                query: params.query,
                progressFolder: params.progressFolder,
                model: params.model,
                thinkingLevel: params.thinkingLevel
            });
            return {
                content: [
                    {
                        type: 'text',
                        text: 'Context refresh scheduled. Summarize what you accomplished this phase and stop. Do not make any more tool calls. Do not ask the user what to do next.'
                    }
                ],
                details: null
            };
        }
    };
}
//# sourceMappingURL=refresh-context.js.map