{"record":{"id":"4eb53d74192efb84","repo":"datawhalechina/hello-agents","slug":"task-type","errorCode":null,"errorMessage":"不支持的任务类型: {task_type}","messagePattern":"不支持的任务类型: (.+?)","errorType":"exception","errorClass":"AgentException","httpStatus":500,"severity":"error","filePath":"Co-creation-projects/Apricity-InnocoreAI/agents/coach.py","lineNumber":52,"sourceCode":"        \n        try:\n            user_id = input_data[\"user_id\"]\n            task_type = input_data[\"task_type\"]  # explain, polish, mimic, suggest\n            content = input_data[\"content\"]\n            context = input_data.get(\"context\", {})\n            \n            result = None\n            \n            if task_type == \"explain\":\n                result = await self._handle_explain_task(user_id, content, context)\n            elif task_type == \"polish\":\n                result = await self._handle_polish_task(user_id, content, context)\n            elif task_type == \"mimic\":\n                result = await self._handle_mimic_task(user_id, content, context)\n            elif task_type == \"suggest\":\n                result = await self._handle_suggest_task(user_id, content, context)\n            else:\n                raise AgentException(f\"不支持的任务类型: {task_type}\")\n            \n            self.set_state(\"completed\")\n            \n            return {\n                \"status\": \"success\",\n                \"task_type\": task_type,\n                \"user_id\": user_id,\n                \"result\": result,\n                \"timestamp\": datetime.now().isoformat()\n            }\n            \n        except Exception as e:\n            self.set_state(\"error\")\n            raise AgentException(f\"Coach Agent执行失败: {str(e)}\")\n    \n    def get_required_fields(self) -> List[str]:\n        \"\"\"获取必需的输入字段\"\"\"\n        return [\"user_id\", \"task_type\", \"content\"]","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/Apricity-InnocoreAI/agents/coach.py#L34-L70","documentation":"Dispatch error in CoachAgent.run: task_type is matched against the literal set {'explain','polish','mimic','suggest'}; anything else reaches the else-branch raise. Combined with the required-fields check, valid coach tasks must carry task_type in that exact lowercase set — 'Explain', 'summarize', or 'rewrite' all fail here.","triggerScenarios":"Calling coach.run({'task_type':'summarize',...}) (unsupported), passing uppercase 'EXPLAIN', a typo like 'polishing', or an LLM orchestrator choosing task types outside the advertised enum.","commonSituations":"Frontend dropdown options not synced with backend enum; adding a new coaching capability in the prompt but not in the if/elif chain; free-text task_type from users passed through unvalidated.","solutions":["Use one of: explain, polish, mimic, suggest (exact lowercase) for task_type.","Validate/normalize task_type at the API boundary (strip + lower + membership check) before coach.run.","Centralize the enum (Literal['explain','polish','mimic','suggest'] or an Enum) and drive both the dispatch and the API schema from it.","When adding a new capability, add both the handler method and the dispatch branch in the same commit."],"exampleFix":"# before\nif task_type == \"explain\": ...\nelif task_type == \"polish\": ...\nelse:\n    raise AgentException(f\"不支持的任务类型: {task_type}\")\n\n# after — enum-driven dispatch, impossible to desync\nfrom enum import Enum\nclass CoachTask(str, Enum):\n    EXPLAIN = \"explain\"; POLISH = \"polish\"; MIMIC = \"mimic\"; SUGGEST = \"suggest\"\n\ntry:\n    handler = getattr(self, f\"_handle_{CoachTask(task_type).value}_task\")\nexcept ValueError:\n    raise AgentException(f\"不支持的任务类型: {task_type}; 可选: {[t.value for t in CoachTask]}\")\nresult = await handler(user_id, content, context)","handlingStrategy":"validation","validationCode":"SUPPORTED = {\"explain\", \"polish\", \"mimic\", \"suggest\"}\ntask_type = (input_data.get(\"task_type\") or \"\").strip().lower()\nif task_type not in SUPPORTED:\n    raise ValueError(f\"task_type must be one of {sorted(SUPPORTED)}\")","typeGuard":"from typing import Literal\nCoachTaskType = Literal[\"explain\", \"polish\", \"mimic\", \"suggest\"]\n\ndef is_coach_task(v: str) -> bool:\n    return v in {\"explain\", \"polish\", \"mimic\", \"suggest\"}","tryCatchPattern":"try:\n    result = await coach.run(input_data)\nexcept AgentException as e:\n    if \"不支持的任务类型\" in str(e):\n        return 422, {\"error\": str(e), \"supported\": [...]}  # client fixable\n    raise","preventionTips":["Single source of truth (Enum/Literal) for task types shared by API schema, UI dropdown, and dispatch.","Normalize case/whitespace on input before dispatch.","Return the supported list in the error so callers self-correct."],"tags":["python","agent","dispatch","validation","enum"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}