2026-03-18 12:17:45 -07:00
|
|
|
"""LangSmith trace URL utilities."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
2026-04-22 13:10:43 -07:00
|
|
|
def _compose_langsmith_project_url() -> str:
|
|
|
|
|
"""Build the LangSmith project URL base from environment variables."""
|
2026-03-18 12:17:45 -07:00
|
|
|
host_url = os.environ.get("LANGSMITH_URL_PROD", "https://smith.langchain.com")
|
|
|
|
|
tenant_id = os.environ.get("LANGSMITH_TENANT_ID_PROD")
|
|
|
|
|
project_id = os.environ.get("LANGSMITH_TRACING_PROJECT_ID_PROD")
|
|
|
|
|
if not tenant_id or not project_id:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"LANGSMITH_TENANT_ID_PROD and LANGSMITH_TRACING_PROJECT_ID_PROD must be set"
|
|
|
|
|
)
|
2026-04-22 13:10:43 -07:00
|
|
|
return f"{host_url}/o/{tenant_id}/projects/p/{project_id}"
|
2026-03-18 12:17:45 -07:00
|
|
|
|
|
|
|
|
|
2026-04-23 13:46:28 -07:00
|
|
|
def get_langsmith_trace_url(thread_id: str) -> str | None:
|
|
|
|
|
"""Build the LangSmith thread URL for a given thread ID."""
|
2026-03-18 12:17:45 -07:00
|
|
|
try:
|
2026-04-23 13:46:28 -07:00
|
|
|
project_url = _compose_langsmith_project_url()
|
|
|
|
|
return f"{project_url}/t/{thread_id}"
|
2026-03-18 12:17:45 -07:00
|
|
|
except Exception: # noqa: BLE001
|
2026-04-23 13:46:28 -07:00
|
|
|
logger.warning(
|
|
|
|
|
"Failed to build LangSmith trace URL for thread %s", thread_id, exc_info=True
|
|
|
|
|
)
|
2026-03-18 12:17:45 -07:00
|
|
|
return None
|