// tools/configure-observability.ts — Handler for configure_observability tool (SPEC-165)
import { getOtelConfig, setOtelConfig } from '../engine/observability/config.js';
import { initTracer } from '../engine/observability/tracer.js';
/**
 * Configures the OpenTelemetry exporter.
 * Merges provided fields into the active config and reinitializes the tracer.
 * Idempotent: calling with the same values produces no observable side effects.
 */
export async function handleConfigureObservability(args) {
    const { enabled, endpoint, samplingRate, resourceAttributes, serviceName } = args;
    // Build the partial update — only include defined fields
    const partial = {};
    if (enabled !== undefined) {
        partial.enabled = enabled;
    }
    if (endpoint !== undefined) {
        partial.endpoint = endpoint;
    }
    if (samplingRate !== undefined) {
        partial.samplingRate = samplingRate;
    }
    if (resourceAttributes !== undefined) {
        partial.resourceAttributes = resourceAttributes;
    }
    if (serviceName !== undefined) {
        partial.serviceName = serviceName;
    }
    let newConfig;
    try {
        newConfig = setOtelConfig(partial);
    }
    catch (err) {
        const message = err instanceof Error ? err.message : String(err);
        return {
            content: [
                {
                    type: 'text',
                    text: `Failed to update observability config: ${message}. Ensure samplingRate is between 0 and 1.`,
                },
            ],
            isError: true,
        };
    }
    // Reinitialize the tracer with the new config
    await initTracer(newConfig);
    const status = newConfig.enabled
        ? `enabled — exporting to ${newConfig.endpoint ?? '(no endpoint set)'}`
        : 'disabled (no-op mode)';
    return {
        content: [
            {
                type: 'text',
                text: [
                    `Observability configured successfully.`,
                    ``,
                    `Status: ${status}`,
                    `Service: ${newConfig.serviceName}`,
                    `Sampling rate: ${newConfig.samplingRate}`,
                    newConfig.serviceVersion ? `Version: ${newConfig.serviceVersion}` : null,
                    newConfig.resourceAttributes && Object.keys(newConfig.resourceAttributes).length > 0
                        ? `Resource attributes: ${JSON.stringify(newConfig.resourceAttributes)}`
                        : null,
                    ``,
                    newConfig.enabled
                        ? `Traces will be exported via OTLP to ${newConfig.endpoint}. ` +
                            `Note: @opentelemetry/api must be installed as a peer dependency for actual export.`
                        : `To enable tracing, set enabled: true and provide an endpoint URL (e.g. http://localhost:4318).`,
                ]
                    .filter((line) => line !== null)
                    .join('\n'),
            },
        ],
    };
}
/**
 * Returns the current observability configuration as a ToolResult.
 * Used by a read-only status check.
 */
export function handleObservabilityStatus() {
    const config = getOtelConfig();
    return {
        content: [
            {
                type: 'text',
                text: [
                    `Observability status:`,
                    `  enabled: ${config.enabled}`,
                    `  endpoint: ${config.endpoint ?? '(none)'}`,
                    `  samplingRate: ${config.samplingRate}`,
                    `  serviceName: ${config.serviceName}`,
                ].join('\n'),
            },
        ],
    };
}
//# sourceMappingURL=configure-observability.js.map