|
49 | 49 | _LOG_DIR, |
50 | 50 | f"log_{datetime.now().strftime('%y%m%d%H%M%S')}.md", |
51 | 51 | ) |
52 | | -os.makedirs(_LOG_DIR, exist_ok=True) |
53 | | -setup_logger(level="INFO", filepath=_LOG_PATH) |
54 | 52 |
|
55 | 53 |
|
56 | 54 | class SubTaskItem(BaseModel): |
@@ -188,6 +186,27 @@ def __init__( |
188 | 186 | self.toolkit.register_tool_function( |
189 | 187 | self.summarize_intermediate_results, |
190 | 188 | ) |
| 189 | + # set up a logger to record the researching process |
| 190 | + os.makedirs(_LOG_DIR, exist_ok=True) |
| 191 | + setup_logger(level="INFO", filepath=_LOG_PATH) |
| 192 | + |
| 193 | + def _extract_text_from_blocks( |
| 194 | + self, |
| 195 | + blocks: list, |
| 196 | + ) -> str: |
| 197 | + """Extract the text content from the output blocks |
| 198 | + returned by the model. |
| 199 | +
|
| 200 | + This method is designed to make deep research agents |
| 201 | + applicable to both thinking and non-thinking models. |
| 202 | + """ |
| 203 | + for block in blocks: |
| 204 | + if block.get("type") == "text": |
| 205 | + return block["text"] |
| 206 | + raise ValueError( |
| 207 | + f"No text block found in model output. " |
| 208 | + f"Received block types: {[b.get('type') for b in blocks]}", |
| 209 | + ) |
191 | 210 |
|
192 | 211 | async def _ensure_mcp_initialized(self) -> None: |
193 | 212 | """Ensure MCP client is properly initialized. |
@@ -770,9 +789,11 @@ async def summarize_intermediate_results(self) -> ToolResponse: |
770 | 789 | ], |
771 | 790 | stream=self.model.stream, |
772 | 791 | ) |
773 | | - self.current_subtask[-1].working_plan = blocks[0][ |
774 | | - "text" |
775 | | - ] # type: ignore[index] |
| 792 | + self.current_subtask[ |
| 793 | + -1 |
| 794 | + ].working_plan = self._extract_text_from_blocks( |
| 795 | + blocks, |
| 796 | + ) # type: ignore[index] |
776 | 797 | report_prefix = "#" * len(self.current_subtask) |
777 | 798 | summarize_sys_prompt = self.prompt_dict[ |
778 | 799 | "summarize_sys_prompt" |
@@ -810,7 +831,9 @@ async def summarize_intermediate_results(self) -> ToolResponse: |
810 | 831 | ], |
811 | 832 | stream=self.model.stream, |
812 | 833 | ) |
813 | | - intermediate_report = blocks[0]["text"] # type: ignore[index] |
| 834 | + intermediate_report = self._extract_text_from_blocks( |
| 835 | + blocks, |
| 836 | + ) # type: ignore[index] |
814 | 837 |
|
815 | 838 | # Write the intermediate report |
816 | 839 | intermediate_report_path = os.path.join( |
@@ -876,7 +899,7 @@ async def _generate_deepresearch_report( |
876 | 899 | The expected output items of the original task. |
877 | 900 | """ |
878 | 901 | reporting_sys_prompt = self.prompt_dict["reporting_sys_prompt"] |
879 | | - reporting_sys_prompt.format_map( |
| 902 | + reporting_sys_prompt = reporting_sys_prompt.format_map( |
880 | 903 | { |
881 | 904 | "original_task": self.user_query, |
882 | 905 | "checklist": checklist, |
@@ -928,7 +951,9 @@ async def _generate_deepresearch_report( |
928 | 951 | msgs=msgs, |
929 | 952 | stream=self.model.stream, |
930 | 953 | ) |
931 | | - final_report_content = blocks[0]["text"] # type: ignore[index] |
| 954 | + final_report_content = self._extract_text_from_blocks( |
| 955 | + blocks, |
| 956 | + ) # type: ignore[index] |
932 | 957 | logger.info( |
933 | 958 | "The final Report is generated: %s", |
934 | 959 | final_report_content, |
@@ -970,7 +995,7 @@ async def _summarizing(self) -> Msg: |
970 | 995 | ensure_ascii=False, |
971 | 996 | ), |
972 | 997 | ) |
973 | | - self.memory.add(summarize_result) |
| 998 | + await self.memory.add(summarize_result) |
974 | 999 | return summarize_result |
975 | 1000 |
|
976 | 1001 | async def reflect_failure(self) -> ToolResponse: |
|
0 commit comments