Coverage for app / agents / researcher.py: 100%
38 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
1"""
2ResearchAgent — information gathering and synthesis.
4Designed to work with tools (search, RAG retrieval, API calls).
5Subclass for domain-specific research: CodeResearchAgent, MarketResearchAgent, etc.
6"""
8from __future__ import annotations
10from typing import Any
12from pydantic import BaseModel, Field
14from app.agents.base import WorkerAgent, AgentCapability, reply_text
15from app.agents.enums import AgentRole
16from app import prompts
17from app.documents.spec import Confidence
18from app.util import vocab
20from app.util.logger import get_logger
22logger = get_logger(__name__)
25class ResearchResult(BaseModel):
26 """Structured output produced by a ResearchAgent."""
28 summary: str = Field(description="Synthesised summary of research findings")
29 findings: list[str] = Field(default_factory=list)
30 sources: list[str] = Field(
31 default_factory=list, description="Sources or tool names consulted"
32 )
33 gaps: list[str] = Field(
34 default_factory=list, description="Areas needing further investigation"
35 )
36 confidence: str = Field(
37 default="medium", description=f"Confidence level: {vocab.listing(Confidence)}"
38 )
39 raw_output: str = Field(default="")
42class ResearchAgent(WorkerAgent):
43 """
44 Base agent for research and information-gathering tasks.
46 When tools are registered (via add_tool), execute runs a proper
47 LLM-driven tool-calling loop (via run_with_tools) so the model decides
48 when and how to call each tool. Falls back to a plain LLM call when
49 no tools are configured.
50 """
52 DEFAULT_ROLE = AgentRole.RESEARCHER
53 DEFAULT_SYSTEM_PROMPT = prompts.RESEARCHER
54 DEFAULT_MAX_ITERATIONS = 15
56 def __init__(self, name: str = "researcher", **kwargs: Any) -> None:
57 super().__init__(name=name, **kwargs)
58 self.add_capability(
59 AgentCapability(
60 name="information_retrieval",
61 description="Retrieve relevant information via tools and knowledge bases",
62 required=True,
63 )
64 )
65 self.add_capability(
66 AgentCapability(
67 name="synthesis",
68 description="Synthesise information from multiple sources into coherent findings",
69 required=True,
70 )
71 )
73 async def execute(self, input_data: Any) -> ResearchResult:
74 # Sizes, never the words (``app/util/logger.py``).
75 logger.info("Executing ResearchAgent input_chars=%d", len(str(input_data)))
76 user_content = (
77 f"Research goal:\n{input_data}\n\n"
78 "Provide:\n"
79 "1. Summary of findings\n"
80 "2. Key findings (list)\n"
81 "3. Sources used\n"
82 "4. Knowledge gaps\n"
83 "5. Confidence level (high / medium / low)"
84 )
85 messages = self.build_messages(user_content)
87 logger.info(
88 "ResearchAgent starting execution loop with %d tools: %s",
89 len(self.tools),
90 [t.name for t in self.tools],
91 )
92 if self.tools:
93 # run_with_tools returns (content, new_messages); execute callers
94 # only need the content string.
95 output, _ = await self.run_with_tools(messages)
96 else:
97 llm = self.resolve_llm()
98 response = await llm.ainvoke(messages)
99 output = reply_text(llm, response, self.name)
101 sources = [t.name for t in self.tools]
102 logger.info("ResearchAgent completed chars=%d", len(output or ""))
103 return ResearchResult(
104 summary=output,
105 sources=sources,
106 raw_output=output,
107 )