const SHELL_COMMAND_DESCRIPTION = 'Shell command argv tokens. Use ["bash","-lc","<cmd>"] form.';
const isRecord = (value) => typeof value === 'object' && value !== null;
const ensureStringArray = (value) => {
    if (Array.isArray(value) && value.every(item => typeof item === 'string')) {
        return [...value];
    }
    return [];
};
const sanitizeShellCommandProperty = (properties) => {
    const candidate = properties.command;
    if (isRecord(candidate)) {
        const command = { ...candidate };
        delete command.oneOf;
        command.type = 'array';
        command.items = { type: 'string' };
        if (typeof command.description !== 'string' || command.description.length === 0) {
            command.description = SHELL_COMMAND_DESCRIPTION;
        }
        properties.command = command;
        return;
    }
    properties.command = {
        description: SHELL_COMMAND_DESCRIPTION,
        type: 'array',
        items: { type: 'string' }
    };
};
const sanitizeToolFunction = (toolFn, isShell) => {
    const fn = { ...toolFn };
    if ('strict' in fn) {
        delete fn.strict;
    }
    if (!fn.parameters || !isRecord(fn.parameters)) {
        return fn;
    }
    const params = { ...fn.parameters };
    if (isShell) {
        if (params.properties && isRecord(params.properties)) {
            params.properties = { ...params.properties };
        }
        else {
            params.properties = {};
        }
        sanitizeShellCommandProperty(params.properties);
        const required = ensureStringArray(params.required);
        if (!required.includes('command')) {
            required.push('command');
        }
        params.required = required;
        params.type = typeof params.type === 'string' ? params.type : 'object';
        params.additionalProperties = typeof params.additionalProperties === 'boolean'
            ? params.additionalProperties
            : false;
    }
    fn.parameters = params;
    return fn;
};
const sanitizeToolDefinition = (tool) => {
    const sanitized = { ...tool };
    if (!sanitized.function || !isRecord(sanitized.function)) {
        return sanitized;
    }
    const fn = sanitizeToolFunction(sanitized.function, sanitized.function.name === 'shell');
    sanitized.function = fn;
    return sanitized;
};
export const sanitizeGLMToolsSchema = (data) => {
    const toolsValue = data.tools;
    if (!Array.isArray(toolsValue)) {
        return data;
    }
    const sanitizedTools = toolsValue.map(tool => (isRecord(tool) ? sanitizeToolDefinition(tool) : tool));
    return {
        ...data,
        tools: sanitizedTools
    };
};
export const sanitizeGLMToolsSchemaInPlace = (data) => {
    const toolsValue = data.tools;
    if (!Array.isArray(toolsValue)) {
        return;
    }
    const sanitized = sanitizeGLMToolsSchema(data);
    data.tools = sanitized.tools;
};
export function sanitizeToolSchema(payload, mode = 'glm_shell') {
    if (mode === 'glm_shell') {
        return sanitizeGLMToolsSchema(payload);
    }
    return payload;
}
