From 0c6f944e3034e219c54ad4ebf3e4ab4cbb5093b8 Mon Sep 17 00:00:00 2001 From: rcholic Date: Fri, 2 Jan 2026 17:31:38 -0800 Subject: [PATCH] fix trace name --- sentience/tracer_factory.py | 40 ++++++++++++++++++++++++++++++++++--- tests/test_cloud_tracing.py | 22 +++++++++++++++++++- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/sentience/tracer_factory.py b/sentience/tracer_factory.py index ecc96f0..f2e9f57 100644 --- a/sentience/tracer_factory.py +++ b/sentience/tracer_factory.py @@ -26,6 +26,10 @@ def create_tracer( api_url: str | None = None, logger: SentienceLogger | None = None, upload_trace: bool = False, + goal: str | None = None, + agent_type: str | None = None, + llm_model: str | None = None, + start_url: str | None = None, ) -> Tracer: """ Create tracer with automatic tier detection. @@ -44,13 +48,26 @@ def create_tracer( upload_trace: Enable cloud trace upload (default: False). When True and api_key is provided, traces will be uploaded to cloud. When False, traces are saved locally only. + goal: User's goal/objective for this trace run. This will be displayed as the + trace name in the frontend. Should be descriptive and action-oriented. + Example: "Add wireless headphones to cart on Amazon" + agent_type: Type of agent running (e.g., "SentienceAgent", "CustomAgent") + llm_model: LLM model used (e.g., "gpt-4-turbo", "claude-3-5-sonnet") + start_url: Starting URL of the agent run (e.g., "https://amazon.com") Returns: Tracer configured with appropriate sink Example: - >>> # Pro tier user - >>> tracer = create_tracer(api_key="sk_pro_xyz", run_id="demo") + >>> # Pro tier user with goal + >>> tracer = create_tracer( + ... api_key="sk_pro_xyz", + ... run_id="demo", + ... goal="Add headphones to cart", + ... agent_type="SentienceAgent", + ... llm_model="gpt-4-turbo", + ... start_url="https://amazon.com" + ... ) >>> # Returns: Tracer with CloudTraceSink >>> >>> # Free tier user @@ -75,11 +92,28 @@ def create_tracer( # 1. Try to initialize Cloud Sink (Pro/Enterprise tier) if upload enabled if api_key and upload_trace: try: + # Build metadata object for trace initialization + # Only include non-empty fields to avoid sending empty strings + metadata: dict[str, str] = {} + if goal and goal.strip(): + metadata["goal"] = goal.strip() + if agent_type and agent_type.strip(): + metadata["agent_type"] = agent_type.strip() + if llm_model and llm_model.strip(): + metadata["llm_model"] = llm_model.strip() + if start_url and start_url.strip(): + metadata["start_url"] = start_url.strip() + + # Build request payload + payload: dict[str, Any] = {"run_id": run_id} + if metadata: + payload["metadata"] = metadata + # Request pre-signed upload URL from backend response = requests.post( f"{api_url}/v1/traces/init", headers={"Authorization": f"Bearer {api_key}"}, - json={"run_id": run_id}, + json=payload, timeout=10, ) diff --git a/tests/test_cloud_tracing.py b/tests/test_cloud_tracing.py index 38c9bf2..a6dce1b 100644 --- a/tests/test_cloud_tracing.py +++ b/tests/test_cloud_tracing.py @@ -405,7 +405,15 @@ def test_create_tracer_pro_tier_success(self, capsys): mock_put.return_value = Mock(status_code=200) run_id = f"test-run-{uuid.uuid4().hex[:8]}" - tracer = create_tracer(api_key="sk_pro_test123", run_id=run_id, upload_trace=True) + tracer = create_tracer( + api_key="sk_pro_test123", + run_id=run_id, + upload_trace=True, + goal="Test goal for trace name", + agent_type="SentienceAgent", + llm_model="gpt-4-turbo", + start_url="https://example.com", + ) # Verify Pro tier message captured = capsys.readouterr() @@ -423,6 +431,18 @@ def test_create_tracer_pro_tier_success(self, capsys): assert mock_post.called assert mock_post.call_count == 1 + # Verify metadata was sent correctly + call_args = mock_post.call_args + request_payload = call_args[1]["json"] + assert "run_id" in request_payload + assert request_payload["run_id"] == run_id + assert "metadata" in request_payload + metadata = request_payload["metadata"] + assert metadata["goal"] == "Test goal for trace name" + assert metadata["agent_type"] == "SentienceAgent" + assert metadata["llm_model"] == "gpt-4-turbo" + assert metadata["start_url"] == "https://example.com" + # Cleanup - emit at least one event so file exists before close tracer.emit("test", {"v": 1, "seq": 1}) tracer.close()