import { DEFAULT_OLLAMA_BASE_URL } from './constants';
export const isAbortError = (error) => {
    if (!error || typeof error !== 'object') {
        return false;
    }
    const maybeError = error;
    return maybeError.name === 'AbortError';
};
export const resolveBaseUrl = (agent) => {
    const candidates = [
        agent.options.baseUrl,
        agent.options.connection?.baseUrl
    ];
    for (const candidate of candidates) {
        if (typeof candidate === 'string' && candidate.trim().length > 0) {
            return candidate.trim().replace(/\/?$/, '');
        }
    }
    return DEFAULT_OLLAMA_BASE_URL;
};
export const sanitizeModelSettings = (settings) => {
    if (!settings || typeof settings !== 'object') {
        return undefined;
    }
    const sanitized = {};
    for (const [key, value] of Object.entries(settings)) {
        if (value === undefined || value === null) {
            continue;
        }
        if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
            sanitized[key] = value;
            continue;
        }
        if (Array.isArray(value)) {
            sanitized[key] = value;
            continue;
        }
        if (typeof value === 'object') {
            sanitized[key] = value;
        }
    }
    return Object.keys(sanitized).length > 0 ? sanitized : undefined;
};
export const coerceMessageContent = (message) => {
    if (typeof message.content !== 'string') {
        return undefined;
    }
    const trimmed = message.content.trim();
    return trimmed.length > 0 ? trimmed : undefined;
};
export const normalizeToolPermissions = (preferences) => {
    if (!preferences) {
        return [];
    }
    return Object.entries(preferences);
};
export const normalizeServerLabel = (id) => {
    const raw = typeof id === 'string' ? id.trim() : '';
    if (raw.length === 0) {
        return 'mcp_default';
    }
    const withoutPrefix = raw.replace(/^(mcp[_-])/, '');
    return `mcp_${withoutPrefix}`;
};
export const buildToolIdentifier = (serverId, toolName) => {
    const serverLabel = normalizeServerLabel(serverId);
    const normalizedTool = toolName.trim().replace(/\s+/g, '_');
    return `${serverLabel}__${normalizedTool}`;
};
export const cloneJson = (value) => JSON.parse(JSON.stringify(value));
