basant307/AI_Governance_Project
048
1/**2 * @license3 * Copyright 2025 Google LLC4 * SPDX-License-Identifier: Apache-2.05 */6 7import { createHash } from 'node:crypto';8 9/**10 * Derive a deterministic 32-char hex traceId from a session ID.11 * Uses SHA-256 truncated to 128 bits to match the OTel trace ID format.12 * Shared by LogToSpanProcessor and debugLogger for consistent correlation.13 */14export function deriveTraceId(sessionId: string): string {15 return createHash('sha256').update(sessionId).digest('hex').slice(0, 32);16}17 18export function randomSpanId(): string {19 const bytes = new Uint8Array(8);20 crypto.getRandomValues(bytes);21 return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');22}23 24export function randomHexString(length: number): string {25 const bytes = new Uint8Array(Math.ceil(length / 2));26 crypto.getRandomValues(bytes);27 return Array.from(bytes, (b) => b.toString(16).padStart(2, '0'))28 .join('')29 .slice(0, length);30}31 