/**
 * update_session_settings — change model/thinking controls for subsequent turns.
 *
 * Lives behind the meta-tool wall (get_tool_info + execute_tool) and uses the
 * same session persistence path as `/model` and `/thinking`.
 */
import { Type } from '@sinclair/typebox';
import { isValidThinkingLevel } from '../../../core/defaults.js';
import { UPDATE_SESSION_SETTINGS } from '../../../tool-names.js';
import { formatModelRef, resolveExactModelRef } from '../../providers/model-resolver.js';
import { getAvailableThinkingLevels } from '../../session/session-models.js';
const TOOL_DESCRIPTION = 'Change the current session model and/or thinking level for subsequent turns only when the user explicitly requests a model or thinking-level change. This uses the same persistence path as /model and /thinking.';
const schema = Type.Object({
    model: Type.Optional(Type.String({
        description: 'User-requested model to use for subsequent turns. Use provider/model-id, or a bare exact model id if unambiguous.'
    })),
    thinking_level: Type.Optional(Type.String({
        description: 'User-requested thinking level for subsequent turns: off, minimal, low, medium, high, or xhigh.'
    }))
});
function textResult(text, isError = false) {
    return { content: [{ type: 'text', text }], details: undefined, isError };
}
export function createUpdateSessionSettingsTool(session) {
    return {
        name: UPDATE_SESSION_SETTINGS,
        label: 'Update session settings',
        isReadOnly: false,
        description: TOOL_DESCRIPTION,
        parameters: schema,
        async execute(_toolCallId, params) {
            const requestedModel = params.model?.trim();
            const requestedThinking = params.thinking_level?.trim();
            if (!requestedModel && !requestedThinking) {
                return textResult('Provide at least one of model or thinking_level.', true);
            }
            const currentSession = session();
            const availableModels = await currentSession.modelRegistry.getAvailable();
            let targetModel = currentSession.model;
            if (requestedModel) {
                const resolved = resolveExactModelRef(availableModels, requestedModel);
                if (resolved.error)
                    return textResult(resolved.error, true);
                targetModel = resolved.model ?? targetModel;
            }
            if (!targetModel) {
                return textResult('No active model is configured. Provide a model to set first.', true);
            }
            let targetThinking;
            if (requestedThinking !== undefined) {
                if (!isValidThinkingLevel(requestedThinking)) {
                    return textResult(`Invalid thinking_level "${requestedThinking}". Allowed: off, minimal, low, medium, high, xhigh.`, true);
                }
                const availableThinking = getAvailableThinkingLevels(targetModel);
                if (!availableThinking.includes(requestedThinking)) {
                    return textResult(`Thinking level "${requestedThinking}" is not available for ${formatModelRef(targetModel)}. Available: ${availableThinking.join(', ')}.`, true);
                }
                targetThinking = requestedThinking;
            }
            if (requestedModel) {
                await currentSession.setModel(targetModel);
            }
            if (targetThinking !== undefined) {
                session().setThinkingLevel(targetThinking);
            }
            const updatedSession = session();
            const updatedModel = updatedSession.model;
            const payload = {
                status: 'updated',
                current_model: updatedModel ? formatModelRef(updatedModel) : null,
                thinking_level: updatedSession.thinkingLevel
            };
            return textResult(JSON.stringify(payload, null, 2));
        }
    };
}
//# sourceMappingURL=update-session-settings.js.map