"""AI Research Agent API(Phase 5)。 POST /api/agent/chat {message} → {reply, actions:[{tool,args,output}]} 未配置 LLM Key 时返回 400 引导(不会崩溃)。 """ from __future__ import annotations from typing import Annotated from fastapi import APIRouter, Depends, HTTPException from pydantic import BaseModel, Field from app.agent.llm import LLMClient, build_llm_from_settings from app.agent.service import AgentService router = APIRouter(prefix="/agent", tags=["agent"]) class AgentChatRequest(BaseModel): message: str = Field(min_length=1, max_length=2000) def _llm_or_raise() -> LLMClient: llm = build_llm_from_settings() if llm is None: raise HTTPException( status_code=400, detail="未配置 LLM:请在根目录 .env 中设置 LLM_API_KEY(可选 LLM_BASE_URL / LLM_MODEL)," "参考 .env.example 与 AGENT.md §33", ) return llm @router.post("/chat", summary="与 AI 研究助手对话(受控工具)") def agent_chat( body: AgentChatRequest, llm: Annotated[LLMClient, Depends(_llm_or_raise)], ) -> dict: return AgentService(llm).chat(body.message)