Skip to content

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/copilot): address PR review round 1

Zamil Majdycc29cf5e20c3
diff --git a/autogpt_platform/backend/backend/copilot/tools/ask_question.py b/autogpt_platform/backend/backend/copilot/tools/ask_question.py
index 181a77b22c..5171fd84a1 100644
--- a/autogpt_platform/backend/backend/copilot/tools/ask_question.py
+++ b/autogpt_platform/backend/backend/copilot/tools/ask_question.py
@@ -1,9 +1,12 @@
"""AskQuestionTool - Ask the user one or more clarifying questions."""
+import logging
from typing import Any
from backend.copilot.model import ChatSession
+logger = logging.getLogger(__name__)
+
from .base import BaseTool
from .models import ClarificationNeededResponse, ClarifyingQuestion, ToolResponseBase
@@ -92,11 +95,7 @@ class AskQuestionTool(BaseTool):
),
},
},
- "required": [],
- "anyOf": [
- {"required": ["question"]},
- {"required": ["questions"]},
- ],
+ "required": ["question"],
}
@property
@@ -115,7 +114,9 @@ class AskQuestionTool(BaseTool):
) -> ClarifyingQuestion:
"""Build a single ``ClarifyingQuestion`` from raw inputs."""
safe_options = (
- [str(o) for o in options if o] if isinstance(options, list) else []
+ [str(o) for o in options if o is not None]
+ if isinstance(options, list)
+ else []
)
return ClarifyingQuestion(
question=question,
@@ -156,7 +157,7 @@ class AskQuestionTool(BaseTool):
raw_options = kwargs.get("options", [])
if not isinstance(raw_options, list):
raw_options = []
- options: list[str] = [str(o) for o in raw_options if o]
+ options: list[str] = [str(o) for o in raw_options if o is not None]
raw_keyword = kwargs.get("keyword", "")
keyword: str = str(raw_keyword) if raw_keyword else ""
@@ -177,9 +178,14 @@ class AskQuestionTool(BaseTool):
clarifying_questions: list[ClarifyingQuestion] = []
for idx, item in enumerate(raw_questions):
if not isinstance(item, dict):
+ logger.warning("ask_question: skipping non-dict item at index %d", idx)
continue
q_text = item.get("question")
if not isinstance(q_text, str) or not q_text.strip():
+ logger.warning(
+ "ask_question: skipping item at index %d with missing/empty question",
+ idx,
+ )
continue
keyword = str(item.get("keyword", "")) or f"question-{idx}"
clarifying_questions.append(
@@ -195,7 +201,7 @@ class AskQuestionTool(BaseTool):
"ask_question requires at least one valid question in 'questions'"
)
- message = clarifying_questions[0].question
+ message = "; ".join(q.question for q in clarifying_questions)
return ClarificationNeededResponse(
message=message,
session_id=session_id,
diff --git a/autogpt_platform/backend/backend/copilot/tools/ask_question_test.py b/autogpt_platform/backend/backend/copilot/tools/ask_question_test.py
index b6e1c9bebc..6467f1cc1b 100644
--- a/autogpt_platform/backend/backend/copilot/tools/ask_question_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/ask_question_test.py
@@ -129,7 +129,7 @@ async def test_execute_multiple_questions(tool: AskQuestionTool, session: ChatSe
assert isinstance(result, ClarificationNeededResponse)
assert len(result.questions) == 3
- assert result.message == "Which channel?"
+ assert result.message == "Which channel?; How often?; Any extra notes?"
q0 = result.questions[0]
assert q0.question == "Which channel?"
@@ -151,7 +151,7 @@ async def test_execute_multiple_questions(tool: AskQuestionTool, session: ChatSe
async def test_execute_multiple_questions_skips_invalid_items(
tool: AskQuestionTool, session: ChatSession
):
- """Non-dict items and items without a question are silently skipped."""
+ """Non-dict items and items without a question are skipped with a warning."""
result = await tool._execute(
user_id=None,
session=session,
@@ -218,3 +218,77 @@ async def test_execute_empty_questions_falls_back_to_single(
assert isinstance(result, ClarificationNeededResponse)
assert len(result.questions) == 1
assert result.questions[0].question == "Fallback question?"
+
+
+# ── Edge cases (from review) ────────────────────────────────────────
+
+
+async def test_execute_with_none_session(tool: AskQuestionTool):
+ """session_id should be None when session has no session_id."""
+
+ class FakeSession:
+ session_id = None
+
+ result = await tool._execute(
+ user_id=None,
+ session=FakeSession(), # type: ignore[arg-type]
+ question="Does this work?",
+ )
+ assert isinstance(result, ClarificationNeededResponse)
+ assert result.session_id is None
+ assert result.questions[0].question == "Does this work?"
+
+
+async def test_execute_single_item_questions_array(
+ tool: AskQuestionTool, session: ChatSession
+):
+ """A questions array with a single item should work like the single path."""
+ result = await tool._execute(
+ user_id=None,
+ session=session,
+ questions=[{"question": "Only one?", "keyword": "solo"}],
+ )
+
+ assert isinstance(result, ClarificationNeededResponse)
+ assert len(result.questions) == 1
+ assert result.questions[0].question == "Only one?"
+ assert result.questions[0].keyword == "solo"
+ assert result.message == "Only one?"
+
+
+async def test_execute_duplicate_keywords_preserved(
+ tool: AskQuestionTool, session: ChatSession
+):
+ """Duplicate keywords from the LLM are passed through; the frontend
+ normalizes them via normalizeClarifyingQuestions()."""
+ result = await tool._execute(
+ user_id=None,
+ session=session,
+ questions=[
+ {"question": "First?", "keyword": "same"},
+ {"question": "Second?", "keyword": "same"},
+ ],
+ )
+
+ assert len(result.questions) == 2
+ assert result.questions[0].keyword == "same"
+ assert result.questions[1].keyword == "same"
+
+
+async def test_execute_options_with_falsy_values(
+ tool: AskQuestionTool, session: ChatSession
+):
+ """Falsy but valid option values like '0' should be preserved."""
+ result = await tool._execute(
+ user_id=None,
+ session=session,
+ question="Pick a number",
+ options=["0", "1", "2"],
+ keyword="number",
+ )
+
+ assert result.questions[0].example == "0, 1, 2"