Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies = [
"sentry-sdk >= 2.20.0",
"fastar >= 0.10.0",
"detect-installer>=0.1.0",
"agent-detector>=1.1.0",
]

[project.optional-dependencies]
Expand Down
12 changes: 11 additions & 1 deletion src/fastapi_cloud_cli/utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import httpx
import typer
from agent_detector import detect_agent
from pydantic import BaseModel, Field, TypeAdapter, ValidationError
from rich_toolkit.progress import Progress
from typing_extensions import ParamSpec
Expand Down Expand Up @@ -304,6 +305,15 @@ def get_http_error_hint(code: ErrorCode, *, auth_mode: AuthMode = "user") -> str
return None


def _get_user_agent() -> str:
user_agent = f"fastapi-cloud-cli/{__version__}"

if detection := detect_agent(minimum_confidence="high"):
user_agent = f"{user_agent} AI-Agent/{detection.agent}"

return user_agent


class APIClient(httpx.Client):
auth_mode: AuthMode

Expand All @@ -319,7 +329,7 @@ def __init__(self, use_deploy_token: bool = False) -> None:
token = identity.user_token
self.auth_mode = "user"

headers = {"User-Agent": f"fastapi-cloud-cli/{__version__}"}
headers = {"User-Agent": _get_user_agent()}
if token:
headers["Authorization"] = f"Bearer {token}"

Expand Down
26 changes: 26 additions & 0 deletions tests/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import httpx
import pytest
import respx
from agent_detector import DetectionResult
from httpx import Response

from fastapi_cloud_cli import __version__
from fastapi_cloud_cli.utils.api import (
STREAM_LOGS_MAX_RETRIES,
APIClient,
Expand All @@ -30,6 +32,30 @@ def deployment_id() -> str:
return "test-deployment-123"


def test_api_client_reports_high_confidence_agent() -> None:
detection = DetectionResult("codex", "high", "environment", "CODEX_THREAD_ID")

with (
patch(
"fastapi_cloud_cli.utils.api.detect_agent", return_value=detection
) as mock_detect_agent,
APIClient() as client,
):
assert client.headers["User-Agent"] == (
f"fastapi-cloud-cli/{__version__} AI-Agent/codex"
)

mock_detect_agent.assert_called_once_with(minimum_confidence="high")


def test_api_client_omits_unattributed_agent() -> None:
with (
patch("fastapi_cloud_cli.utils.api.detect_agent", return_value=None),
APIClient() as client,
):
assert client.headers["User-Agent"] == f"fastapi-cloud-cli/{__version__}"


@pytest.fixture
def logs_route(respx_mock: respx.MockRouter, deployment_id: str) -> respx.Route:
return respx_mock.get(f"/deployments/{deployment_id}/build-logs")
Expand Down
11 changes: 11 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading