function truncateString(value, maxChars) {
    return value.length <= maxChars ? value : `${value.slice(0, maxChars)}...`;
}
function truncateStringValues(value, maxStringChars) {
    if (typeof value === 'string')
        return truncateString(value, maxStringChars);
    if (Array.isArray(value))
        return value.map((item) => truncateStringValues(item, maxStringChars));
    if (!value || typeof value !== 'object')
        return value;
    const result = {};
    for (const [key, child] of Object.entries(value)) {
        result[key] = truncateStringValues(child, maxStringChars);
    }
    return result;
}
/**
 * Return a JSON string no longer than `maxChars` without producing malformed JSON.
 *
 * Historical tool-call arguments are prompt context only. When they are too
 * large, preserve object shape with shortened string leaves before falling back
 * to a valid JSON preview wrapper.
 */
export function stringifyTruncatedJson(value, maxChars) {
    const full = JSON.stringify(value) ?? 'null';
    if (full.length <= maxChars)
        return full;
    for (const maxStringChars of [240, 160, 100, 60, 30]) {
        const candidate = JSON.stringify(truncateStringValues(value, maxStringChars));
        if (candidate.length <= maxChars)
            return candidate;
    }
    const preview = truncateString(full, Math.max(0, maxChars - 80));
    const wrappedPreview = JSON.stringify({ _truncated: true, preview });
    if (wrappedPreview.length <= maxChars)
        return wrappedPreview;
    return JSON.stringify({ _truncated: true });
}
//# sourceMappingURL=json-safe-truncate.js.map