AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): add autopilot title fallback (#13141)
Abhimanyu Yadavd4ca8f97cf12
diff --git a/autogpt_platform/backend/backend/copilot/service.py b/autogpt_platform/backend/backend/copilot/service.pyindex 00f544b472..c7c66d262c 100644--- a/autogpt_platform/backend/backend/copilot/service.py+++ b/autogpt_platform/backend/backend/copilot/service.py@@ -43,6 +43,11 @@ logger = logging.getLogger(__name__)config = ChatConfig()settings = Settings()+_TITLE_MAX_WORDS = 6+_TITLE_MAX_CHARS = 50+_TITLE_ELLIPSIS = "..."+_TITLE_TRUNCATED_MAX_CHARS = _TITLE_MAX_CHARS - len(_TITLE_ELLIPSIS)+def resolve_chat_model(tier: CopilotLlmModel | None) -> str:"""Return the configured SDK model for the given tier.@@ -706,7 +711,7 @@ async def _generate_session_title(message: str,user_id: str | None = None,session_id: str | None = None,-) -> tuple[str | None, ChatCompletion | None]:+) -> tuple[str, ChatCompletion | None]:"""Generate a concise title for a chat session based on the first message.Returns ``(title, response)``. The caller is responsible for@@ -721,9 +726,13 @@ async def _generate_session_title(session_id: Session ID for OpenRouter tracing (optional)Returns:- ``(title, response)`` on success; ``(None, None)`` if the LLM- call raised. ``response`` is returned even when ``title`` is- empty so the caller can still record the (paid-for) cost.+ ``(title, response)``. ``title`` falls back to the user's first+ message when the LLM call raises or returns an empty title.+ ``response`` is returned (non-None) ONLY when the create call+ succeeded — empty-content path still carries it so the caller+ can record the (paid-for) cost. The exception path returns+ ``response=None`` and the caller skips cost-recording: a raised+ ``create`` did not bill, so there is no cost to record."""try:# Build extra_body for OpenRouter tracing and PostHog analytics.@@ -772,22 +781,39 @@ async def _generate_session_title()except Exception as e:logger.warning(f"Failed to generate session title: {e}")- return None, None+ return _fallback_title_from_message(message), None# Robust against an empty ``choices`` list OR a choice whose# ``message`` is missing ``content`` (shouldn't happen on the OpenAI# SDK typing, but belt-and-suspenders — the background task would# otherwise die on ``IndexError`` and lose the (paid-for) cost# recording we're about to do below).- title: str | None = None+ title = ""if response.choices:msg = response.choices[0].message- title = msg.content if msg is not None else None- if title:- title = title.strip().strip("\"'")- if len(title) > 50:- title = title[:47] + "..."- return title, response+ if msg is not None and msg.content:+ title = msg.content.strip().strip("\"'")+ if len(title) > _TITLE_MAX_CHARS:+ title = title[:_TITLE_TRUNCATED_MAX_CHARS] + _TITLE_ELLIPSIS+ return title or _fallback_title_from_message(message), response+++def _fallback_title_from_message(message: str) -> str:+ # ``maxsplit=_TITLE_MAX_WORDS`` caps the per-call allocation for huge+ # messages — we only need the first N words plus a "has more" signal.+ parts = strip_injected_context_for_display(message).split(maxsplit=_TITLE_MAX_WORDS)+ if not parts:+ return "New chat"++ is_shortened = len(parts) > _TITLE_MAX_WORDS+ title = " ".join(parts[:_TITLE_MAX_WORDS])+ if len(title) > _TITLE_MAX_CHARS or (+ is_shortened and len(title) > _TITLE_TRUNCATED_MAX_CHARS+ ):+ return title[:_TITLE_TRUNCATED_MAX_CHARS] + _TITLE_ELLIPSIS+ if is_shortened:+ return title + _TITLE_ELLIPSIS+ return titledef _title_usage_from_response(@@ -945,7 +971,7 @@ async def _update_title_async("""title, response = await _generate_session_title(message, user_id, session_id)- if title and user_id:+ if user_id:try:await update_session_title(session_id, user_id, title, only_if_empty=True)logger.debug("Generated title for session %s", session_id)diff --git a/autogpt_platform/backend/backend/copilot/service_unit_test.py b/autogpt_platform/backend/backend/copilot/service_unit_test.pyindex eed7401b97..8eb8d48879 100644--- a/autogpt_platform/backend/backend/copilot/service_unit_test.py+++ b/autogpt_platform/backend/backend/copilot/service_unit_test.py@@ -22,6 +22,7 @@ from openai.types.chat.chat_completion_message import ChatCompletionMessagefrom openai.types.completion_usage import CompletionUsagefrom backend.copilot.service import (+ _fallback_title_from_message,_generate_session_title,_record_title_generation_cost,_title_usage_from_response,@@ -64,6 +65,48 @@ def _usage_with_cost(cost: object | None) -> CompletionUsage:return CompletionUsage.model_validate(payload)+class TestFallbackTitleFromMessage:+ def test_short_message_is_unchanged(self):+ title = _fallback_title_from_message("Build an invoice tool")+ assert title == "Build an invoice tool"++ def test_whitespace_and_newlines_are_collapsed(self):+ title = _fallback_title_from_message(" Build\n\nan\tinvoice tool ")+ assert title == "Build an invoice tool"++ def test_injected_context_prefixes_are_stripped(self):+ title = _fallback_title_from_message(+ "<memory_context>\nfacts\n</memory_context>\n\n"+ "<budget_context>\nlimit: $1\n</budget_context>\n\n"+ "Build an invoice tool"+ )+ assert title == "Build an invoice tool"++ def test_empty_message_returns_new_chat(self):+ title = _fallback_title_from_message(" \n\t ")+ assert title == "New chat"++ def test_more_than_six_words_uses_first_six_with_ellipsis(self):+ title = _fallback_title_from_message(+ "Build me a competitor research dashboard today please"+ )+ assert title == "Build me a competitor research dashboard..."++ def test_long_message_is_truncated_to_fifty_chars(self):+ title = _fallback_title_from_message(+ "aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff"+ )+ assert len(title) == 50+ assert title.endswith("...")++ def test_shortened_title_near_limit_stays_within_fifty_chars(self):+ title = _fallback_title_from_message(+ "aaaaaaaa bbbbbbbb cccccccc dddddddd eeeeeeee ffffffff extra"+ )+ assert len(title) == 50+ assert title.endswith("...")++class TestTitleUsageFromResponse:"""``_title_usage_from_response`` returns sensible zeros/Nones whenoptional fields are absent or of unexpected shape."""@@ -416,9 +459,9 @@ class TestUpdateTitleAsync:@pytest.mark.asyncioasync def test_generation_returns_none_response_skips_cost(self):- """``_generate_session_title`` swallows exceptions and returns- ``(None, None)`` — no response means no cost to record."""- gen = AsyncMock(return_value=(None, None))+ """``_generate_session_title`` swallows exceptions and returns a+ fallback title with no response, so no cost is recorded."""+ gen = AsyncMock(return_value=("msg", None))update = AsyncMock()record = AsyncMock()with (@@ -437,15 +480,14 @@ class TestUpdateTitleAsync:):await _update_title_async("sess-5", "msg", user_id="u")- update.assert_not_awaited()+ update.assert_awaited_once_with("sess-5", "u", "msg", only_if_empty=True)record.assert_not_awaited()@pytest.mark.asyncio- async def test_empty_title_with_response_still_records_cost(self):- """Title came back empty but we still paid for the LLM call —- cost recording runs even though the title write is skipped."""+ async def test_fallback_title_with_response_persists_and_records_cost(self):+ """Fallback title is persisted while cost recording still runs."""resp = _build_completion(usage=_usage_with_cost(0.0001))- gen = AsyncMock(return_value=(None, resp))+ gen = AsyncMock(return_value=("fallback title", resp))update = AsyncMock()record = AsyncMock()with (@@ -464,7 +506,9 @@ class TestUpdateTitleAsync:):await _update_title_async("sess-6", "msg", user_id="u")- update.assert_not_awaited()+ update.assert_awaited_once_with(+ "sess-6", "u", "fallback title", only_if_empty=True+ )record.assert_awaited_once()@@ -508,10 +552,10 @@ class TestGenerateSessionTitle:assert title.endswith("...")@pytest.mark.asyncio- async def test_empty_choices_returns_none_title_with_response(self):+ async def test_empty_choices_returns_fallback_title_with_response(self):"""No ``choices`` on the response (shouldn't happen per SDK- typing) must not raise IndexError — response is preserved so the- caller can still record the paid-for cost."""+ typing) must not raise IndexError. The response is preserved so+ the caller can still record the paid-for cost."""resp = _build_completion(choices=[])client = MagicMock()client.chat.completions.create = AsyncMock(return_value=resp)@@ -519,13 +563,13 @@ class TestGenerateSessionTitle:"backend.copilot.service._get_aux_client",return_value=client,):- title, response = await _generate_session_title("x")- assert title is None+ title, response = await _generate_session_title("write a sales email")+ assert title == "write a sales email"assert response is resp@pytest.mark.asyncio- async def test_missing_message_returns_none_title(self):- """A choice whose ``.message`` is absent produces a None title+ async def test_missing_message_returns_fallback_title(self):+ """A choice whose ``.message`` is absent produces a fallback titlebut the response still lands on the caller."""fake_choice = SimpleNamespace(message=None)fake_response = SimpleNamespace(choices=[fake_choice])@@ -535,15 +579,15 @@ class TestGenerateSessionTitle:"backend.copilot.service._get_aux_client",return_value=client,):- title, response = await _generate_session_title("x")- assert title is None+ title, response = await _generate_session_title("summarize this document")+ assert title == "summarize this document"assert response is fake_response@pytest.mark.asyncio- async def test_llm_call_raises_returns_none_none(self):- """Network / API errors on the create call are swallowed;- ``(None, None)`` ensures the caller skips both title and cost- without crashing the background task."""+ async def test_llm_call_raises_returns_fallback_title_and_no_response(self):+ """Network / API errors on the create call are swallowed and+ fallback to the first message without crashing the background+ task."""client = MagicMock()client.chat.completions.create = AsyncMock(side_effect=RuntimeError("connection reset")@@ -552,10 +596,33 @@ class TestGenerateSessionTitle:"backend.copilot.service._get_aux_client",return_value=client,):- title, response = await _generate_session_title("x")- assert title is None+ title, response = await _generate_session_title("build me an invoice tool")+ assert title == "build me an invoice tool"assert response is None+ @pytest.mark.asyncio+ async def test_aux_client_unavailable_returns_fallback_title_and_no_response(self):+ with patch(+ "backend.copilot.service._get_aux_client",+ side_effect=RuntimeError("missing aux API key"),+ ):+ title, response = await _generate_session_title("research CRM options")+ assert title == "research CRM options"+ assert response is None++ @pytest.mark.asyncio+ async def test_empty_content_returns_fallback_title_with_response(self):+ resp = _build_completion(content=" ")+ client = MagicMock()+ client.chat.completions.create = AsyncMock(return_value=resp)+ with patch(+ "backend.copilot.service._get_aux_client",+ return_value=client,+ ):+ title, response = await _generate_session_title("plan next sprint")+ assert title == "plan next sprint"+ assert response is resp+@pytest.mark.asyncioasync def test_create_receives_usage_include_extra_body(self):"""PR adds ``usage: {'include': True}`` so OpenRouter embeds the