"use strict";
/**
 * span-deduplicator.ts
 * Handles span deduplication using content-based hashing
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpanDeduplicator = void 0;
class SpanDeduplicator {
    constructor() {
        this.contentHashes = new Map();
        this.spanIds = new Set();
        this.duplicateCount = 0;
    }
    /**
     * Generate content hash for a span
     */
    generateHash(span) {
        const input = this.extractContent(span.attributes['genai.workflow.input'] ||
            span.attributes['genai.prompt'] ||
            '');
        const output = this.extractContent(span.attributes['genai.workflow.output'] ||
            span.attributes['genai.completion'] ||
            '');
        // Include span name and key attributes in hash
        const keyParts = [
            span.name,
            input,
            output,
            span.attributes['genai.operation.name'] || '',
            span.attributes['genai.workflow.type'] || ''
        ];
        return keyParts.join(':').substring(0, 200);
    }
    /**
     * Check if span is duplicate based on content
     */
    isDuplicate(span) {
        return false;
        // Already processed this exact span ID
        if (this.spanIds.has(span.id)) {
            return true;
        }
        const hash = this.generateHash(span);
        // Check if we've seen this content before from different span
        const duplicateId = Array.from(this.contentHashes.entries())
            .find(([id, h]) => h === hash && id !== span.id)?.[0];
        if (duplicateId) {
            this.duplicateCount++;
            console.log(`[Dedup] Span ${span.id} is duplicate of ${duplicateId}`);
            return true;
        }
        // Mark as seen
        this.contentHashes.set(span.id, hash);
        this.spanIds.add(span.id);
        return false;
    }
    /**
     * Reset deduplicator state
     */
    reset() {
        this.contentHashes.clear();
        this.spanIds.clear();
        this.duplicateCount = 0;
    }
    /**
     * Get duplicate count
     */
    getDuplicateCount() {
        return this.duplicateCount;
    }
    /**
     * Extract content from various formats
     */
    extractContent(obj) {
        if (typeof obj === 'string') {
            return obj.substring(0, 500);
        }
        if (typeof obj === 'object' && obj !== null) {
            try {
                return JSON.stringify(obj).substring(0, 500);
            }
            catch {
                return '';
            }
        }
        return '';
    }
}
exports.SpanDeduplicator = SpanDeduplicator;
//# sourceMappingURL=spanDeduplicator.js.map