// SPDX-FileCopyrightText: 2025 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0

// LiveKit custom attributes
export const ATTR_SPEECH_ID = 'lk.speech_id';
export const ATTR_AGENT_LABEL = 'lk.agent_label';
export const ATTR_START_TIME = 'lk.start_time';
export const ATTR_END_TIME = 'lk.end_time';
export const ATTR_RETRY_COUNT = 'lk.retry_count';

/**
 * Provider-known correlation ids associated with this span (string[]).
 *
 * Populated by STT/TTS plugins when the id is either sent to the provider
 * (e.g. WS context_id) or returned by it (e.g. response request_id /
 * session_id), so it can be cross-referenced with the provider's logs for
 * debugging.
 */
export const ATTR_PROVIDER_REQUEST_IDS = 'lk.provider_request_ids';

export const ATTR_PARTICIPANT_ID = 'lk.participant_id';
export const ATTR_PARTICIPANT_IDENTITY = 'lk.participant_identity';
export const ATTR_PARTICIPANT_KIND = 'lk.participant_kind';

// session start
export const ATTR_JOB_ID = 'lk.job_id';
export const ATTR_AGENT_NAME = 'lk.agent_name';
export const ATTR_ROOM_NAME = 'lk.room_name';
export const ATTR_SESSION_OPTIONS = 'lk.session_options';

// assistant turn
export const ATTR_AGENT_TURN_ID = 'lk.generation_id';
export const ATTR_AGENT_PARENT_TURN_ID = 'lk.parent_generation_id';
export const ATTR_USER_INPUT = 'lk.user_input';
export const ATTR_INSTRUCTIONS = 'lk.instructions';
export const ATTR_SPEECH_INTERRUPTED = 'lk.interrupted';

// llm node
export const ATTR_CHAT_CTX = 'lk.chat_ctx';
export const ATTR_FUNCTION_TOOLS = 'lk.function_tools';
export const ATTR_PROVIDER_TOOLS = 'lk.provider_tools';
export const ATTR_TOOL_SETS = 'lk.tool_sets';
export const ATTR_RESPONSE_TEXT = 'lk.response.text';
export const ATTR_RESPONSE_FUNCTION_CALLS = 'lk.response.function_calls';
/** Time to first token in seconds. */
export const ATTR_RESPONSE_TTFT = 'lk.response.ttft';

// function tool
export const ATTR_FUNCTION_TOOL_ID = 'lk.function_tool.id';
export const ATTR_FUNCTION_TOOL_NAME = 'lk.function_tool.name';
export const ATTR_FUNCTION_TOOL_ARGS = 'lk.function_tool.arguments';
export const ATTR_FUNCTION_TOOL_IS_ERROR = 'lk.function_tool.is_error';
export const ATTR_FUNCTION_TOOL_OUTPUT = 'lk.function_tool.output';

// tts node
export const ATTR_TTS_INPUT_TEXT = 'lk.input_text';
export const ATTR_TTS_STREAMING = 'lk.tts.streaming';
export const ATTR_TTS_LABEL = 'lk.tts.label';
/** Time to first byte in seconds. */
export const ATTR_RESPONSE_TTFB = 'lk.response.ttfb';

// eou detection
export const ATTR_EOU_PROBABILITY = 'lk.eou.probability';
export const ATTR_EOU_UNLIKELY_THRESHOLD = 'lk.eou.unlikely_threshold';
export const ATTR_EOU_DELAY = 'lk.eou.endpointing_delay';
export const ATTR_EOU_LANGUAGE = 'lk.eou.language';
/** Which signal triggered the EOU detection: 'vad' | 'stt' | 'manual'. */
export const ATTR_EOU_SOURCE = 'lk.eou.source';
/** True when the audio EOT detector resolved this prediction from its
 * inference-window cache instead of running a fresh predict. */
export const ATTR_EOU_FROM_CACHE = 'lk.eou.from_cache';
/** Latest input-audio creation time → prediction receive time (ms). */
export const ATTR_EOU_DETECTION_DELAY = 'lk.eou.detection_delay';
export const ATTR_USER_TRANSCRIPT = 'lk.user_transcript';
export const ATTR_TRANSCRIPT_CONFIDENCE = 'lk.transcript_confidence';
export const ATTR_TRANSCRIPTION_DELAY = 'lk.transcription_delay';
export const ATTR_END_OF_TURN_DELAY = 'lk.end_of_turn_delay';

// answering machine detection
export const ATTR_AMD_CATEGORY = 'lk.amd.category';
export const ATTR_AMD_REASON = 'lk.amd.reason';
export const ATTR_AMD_IS_MACHINE = 'lk.amd.is_machine';
export const ATTR_AMD_INTERRUPT_ON_MACHINE = 'lk.amd.interrupt_on_machine';
/** Total user-speech duration captured before the AMD verdict (milliseconds). */
export const ATTR_AMD_SPEECH_DURATION = 'lk.amd.speech_duration';
/** Time between speech end and the AMD verdict emission (milliseconds). */
export const ATTR_AMD_DELAY = 'lk.amd.delay';
export const ATTR_AMD_TRANSCRIPT = 'lk.amd.transcript';

// Adaptive Interruption attributes
export const ATTR_IS_INTERRUPTION = 'lk.is_interruption';
export const ATTR_INTERRUPTION_PROBABILITY = 'lk.interruption.probability';
export const ATTR_INTERRUPTION_TOTAL_DURATION = 'lk.interruption.total_duration';
export const ATTR_INTERRUPTION_PREDICTION_DURATION = 'lk.interruption.prediction_duration';
export const ATTR_INTERRUPTION_DETECTION_DELAY = 'lk.interruption.detection_delay';

// metrics
export const ATTR_LLM_METRICS = 'lk.llm_metrics';
export const ATTR_TTS_METRICS = 'lk.tts_metrics';
export const ATTR_REALTIME_MODEL_METRICS = 'lk.realtime_model_metrics';

/** End-to-end latency in seconds. */
export const ATTR_E2E_LATENCY = 'lk.e2e_latency';

// OpenTelemetry GenAI attributes
// OpenTelemetry specification: https://opentelemetry.io/docs/specs/semconv/registry/attributes/gen-ai/
export const ATTR_GEN_AI_OPERATION_NAME = 'gen_ai.operation.name';
export const ATTR_GEN_AI_REQUEST_MODEL = 'gen_ai.request.model';
/** The provider name (e.g., 'openai', 'anthropic'). */
export const ATTR_GEN_AI_PROVIDER_NAME = 'gen_ai.provider.name';
export const ATTR_GEN_AI_USAGE_INPUT_TOKENS = 'gen_ai.usage.input_tokens';
export const ATTR_GEN_AI_USAGE_OUTPUT_TOKENS = 'gen_ai.usage.output_tokens';

// Unofficial OpenTelemetry GenAI attributes, recognized by LangFuse
// https://langfuse.com/integrations/native/opentelemetry#usage
// but not yet in the official OpenTelemetry specification.
export const ATTR_GEN_AI_USAGE_INPUT_TEXT_TOKENS = 'gen_ai.usage.input_text_tokens';
export const ATTR_GEN_AI_USAGE_INPUT_AUDIO_TOKENS = 'gen_ai.usage.input_audio_tokens';
export const ATTR_GEN_AI_USAGE_INPUT_CACHED_TOKENS = 'gen_ai.usage.input_cached_tokens';
export const ATTR_GEN_AI_USAGE_OUTPUT_TEXT_TOKENS = 'gen_ai.usage.output_text_tokens';
export const ATTR_GEN_AI_USAGE_OUTPUT_AUDIO_TOKENS = 'gen_ai.usage.output_audio_tokens';

// OpenTelemetry GenAI event names (for structured logging)
export const EVENT_GEN_AI_SYSTEM_MESSAGE = 'gen_ai.system.message';
export const EVENT_GEN_AI_USER_MESSAGE = 'gen_ai.user.message';
export const EVENT_GEN_AI_ASSISTANT_MESSAGE = 'gen_ai.assistant.message';
export const EVENT_GEN_AI_TOOL_MESSAGE = 'gen_ai.tool.message';
export const EVENT_GEN_AI_CHOICE = 'gen_ai.choice';

// Exception attributes
export const ATTR_EXCEPTION_TRACE = 'exception.stacktrace';
export const ATTR_EXCEPTION_TYPE = 'exception.type';
export const ATTR_EXCEPTION_MESSAGE = 'exception.message';

// Platform-specific attributes
export const ATTR_LANGFUSE_COMPLETION_START_TIME = 'langfuse.observation.completion_start_time';
