/**
 * Agent-loop-owned refresh_context tool for embedded ShortcutXL.
 *
 * Schedules a fresh LLM context continuation in the same visible UI thread.
 * The composition root drains the scheduled refresh after the current run ends.
 */
import { Type } from '@sinclair/typebox';
import { trimNonEmpty } from '../../../utils/string.js';
import { REFRESH_CONTEXT } from '../tool-names.js';
export const REFRESH_CONTEXT_TOOL_SUMMARY = 'Continue an autonomous task in a fresh LLM context while keeping the same visible thread.';
export const REFRESH_CONTEXT_TOOL_DESCRIPTION = `Discard the current LLM conversation context and continue in a fresh context while preserving the same visible UI thread.

Use this after updating progress markdown files for a complex autonomous task. After calling this tool, do not make more tool calls; briefly summarize the phase and stop.`;
const REFRESH_CONTEXT_LABEL = 'Refresh Context';
const REFRESH_CONTEXT_QUERY_DESCRIPTION = 'Self-contained continuation prompt for the next fresh context. Mention the progress folder and what to do next.';
const REFRESH_CONTEXT_PROGRESS_FOLDER_DESCRIPTION = 'Progress folder path.';
const REFRESH_CONTEXT_MODEL_DESCRIPTION = 'Optional model id override for the next context.';
const REFRESH_CONTEXT_EMPTY_QUERY_MESSAGE = 'Error: refresh_context requires a non-empty query.';
const REFRESH_CONTEXT_UNWIRED_MESSAGE = 'Error: refresh_context is not wired for this runtime.';
const REFRESH_CONTEXT_SCHEDULED_MESSAGE = 'Context refresh scheduled. Summarize what you accomplished in this phase and stop. Do not make more tool calls.';
const refreshContextLlmSchema = Type.Object({
    query: Type.String({ description: REFRESH_CONTEXT_QUERY_DESCRIPTION }),
    progressFolder: Type.Optional(Type.String({ description: REFRESH_CONTEXT_PROGRESS_FOLDER_DESCRIPTION })),
    model: Type.Optional(Type.String({ description: REFRESH_CONTEXT_MODEL_DESCRIPTION }))
});
function createTextResult(text, isError = false) {
    return {
        content: [{ type: 'text', text }],
        details: null,
        ...(isError ? { isError } : {})
    };
}
function createDetails(params) {
    return {
        query: params.query,
        ...(params.progressFolder ? { progress_folder: params.progressFolder } : {}),
        ...(params.model ? { model: params.model } : {})
    };
}
function createLlmTool(options) {
    return {
        name: REFRESH_CONTEXT,
        label: REFRESH_CONTEXT_LABEL,
        isReadOnly: false,
        description: REFRESH_CONTEXT_TOOL_DESCRIPTION,
        parameters: refreshContextLlmSchema,
        async execute(_toolCallId, llmArgs) {
            const args = llmArgs;
            const query = trimNonEmpty(args.query);
            if (!query) {
                return createTextResult(REFRESH_CONTEXT_EMPTY_QUERY_MESSAGE, true);
            }
            if (!options.scheduleRefresh) {
                return createTextResult(REFRESH_CONTEXT_UNWIRED_MESSAGE, true);
            }
            const progressFolder = trimNonEmpty(args.progressFolder);
            const model = trimNonEmpty(args.model);
            const params = {
                query,
                ...(progressFolder ? { progressFolder } : {}),
                ...(model ? { model } : {})
            };
            options.scheduleRefresh(params);
            return {
                content: [{ type: 'text', text: REFRESH_CONTEXT_SCHEDULED_MESSAGE }],
                details: createDetails(params)
            };
        }
    };
}
export const refreshContextContract = {
    createLlmTool
};
//# sourceMappingURL=contract.js.map