Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(plan, rag) fix the bug in the word reader and plan notebook class. (#895)
DavdGao58a4858c20ab
diff --git a/src/agentscope/plan/_in_memory_storage.py b/src/agentscope/plan/_in_memory_storage.pyindex 627c91f3c..54def9693 100644--- a/src/agentscope/plan/_in_memory_storage.py+++ b/src/agentscope/plan/_in_memory_storage.py@@ -14,6 +14,15 @@ class InMemoryPlanStorage(PlanStorageBase):super().__init__()self.plans = OrderedDict()+ # Support historical plan serialization and deserialization+ self.register_state(+ "plans",+ lambda plans: {k: v.model_dump() for k, v in plans.items()},+ lambda json_data: OrderedDict(+ (k, Plan.model_validate(v)) for k, v in json_data.items()+ ),+ )+async def add_plan(self, plan: Plan, override: bool = True) -> None:"""Add a plan to the storage.diff --git a/src/agentscope/plan/_plan_model.py b/src/agentscope/plan/_plan_model.pyindex c262419c3..c91ab471f 100644--- a/src/agentscope/plan/_plan_model.py+++ b/src/agentscope/plan/_plan_model.py@@ -33,7 +33,6 @@ class SubTask(BaseModel):)outcome: str | None = Field(description="The actual outcome of the subtask.",- exclude=True,default=None,)state: Literal["todo", "in_progress", "done", "abandoned"] = Field(@@ -48,7 +47,6 @@ class SubTask(BaseModel):finished_at: str | None = Field(description="The time the subtask was finished.",default=None,- exclude=True,)def finish(self, outcome: str) -> None:@@ -106,7 +104,7 @@ class SubTask(BaseModel):class Plan(BaseModel):"""The plan model used in the plan module, contains a list of subtasks."""- id: str = Field(exclude=True, default_factory=shortuuid.uuid)+ id: str = Field(default_factory=shortuuid.uuid)name: str = Field(description=("The plan name, should be concise, descriptive and not exceed 10 "@@ -133,24 +131,41 @@ class Plan(BaseModel):created_at: str = Field(description="The time the plan was created.",default_factory=_get_timestamp,- exclude=True,)state: Literal["todo", "in_progress", "done", "abandoned"] = Field(description="The state of the plan.",default="todo",- exclude=True,)finished_at: str | None = Field(description="The time the plan was finished.",default=None,- exclude=True,)outcome: str | None = Field(description="The actual outcome of the plan.",default=None,- exclude=True,)+ def refresh_plan_state(self) -> str:+ """Refresh the plan state based on the states of its subtasks. This+ function only switches the plan state between "todo" and "in_progress".++ # TODO: Handle the plan state much more formally.+ """+ if self.state in ["done", "abandoned"]:+ return ""++ any_in_progress = any(_.state == "in_progress" for _ in self.subtasks)++ if any_in_progress and self.state == "todo":+ self.state = "in_progress"+ return "The plan state has been updated to 'in_progress'."++ elif not any_in_progress and self.state == "in_progress":+ self.state = "todo"+ return "The plan state has been updated to 'todo'."++ return ""+def finish(self,state: Literal["done", "abandoned"],diff --git a/src/agentscope/plan/_plan_notebook.py b/src/agentscope/plan/_plan_notebook.pyindex ff12f9365..b24706c91 100644--- a/src/agentscope/plan/_plan_notebook.py+++ b/src/agentscope/plan/_plan_notebook.py@@ -424,7 +424,7 @@ class PlanNotebook(StateModule):async def update_subtask_state(self,subtask_idx: int,- state: Literal["todo", "in_progress", "deprecated"],+ state: Literal["todo", "in_progress", "abandoned"],) -> ToolResponse:"""Update the state of a subtask by given index and state. Note if youwant to mark a subtask as done, you SHOULD call `finish_subtask`@@ -487,7 +487,7 @@ class PlanNotebook(StateModule):# Check all previous subtasks are done or deprecatedif idx < subtask_idx and subtask.state not in ["done",- "deprecated",+ "abandoned",]:return ToolResponse(content=[@@ -520,6 +520,10 @@ class PlanNotebook(StateModule):)self.current_plan.subtasks[subtask_idx].state = state++ # Update the plan state to in_progress if not already+ suffix = self.current_plan.refresh_plan_state()+await self._trigger_plan_change_hooks()return ToolResponse(content=[@@ -527,7 +531,7 @@ class PlanNotebook(StateModule):type="text",text=f"Subtask at index {subtask_idx}, named "f"'{self.current_plan.subtasks[subtask_idx].name}' "- f"is marked as '{state}' successfully.",+ f"is marked as '{state}' successfully. " + suffix,),],)@@ -585,7 +589,7 @@ class PlanNotebook(StateModule):for idx, subtask in enumerate(self.current_plan.subtasks[0:subtask_idx],):- if subtask.state not in ["done", "deprecated"]:+ if subtask.state not in ["done", "abandoned"]:return ToolResponse(content=[TextBlock(@@ -815,12 +819,15 @@ class PlanNotebook(StateModule):the agent."""return [+ # subtask related toolsself.view_subtasks,self.update_subtask_state,self.finish_subtask,+ # plan related toolsself.create_plan,self.revise_current_plan,self.finish_plan,+ # historical plan related toolsself.view_historical_plans,self.recover_historical_plan,]diff --git a/src/agentscope/rag/_reader/_word_reader.py b/src/agentscope/rag/_reader/_word_reader.pyindex d9475471b..a8c05f279 100644--- a/src/agentscope/rag/_reader/_word_reader.py+++ b/src/agentscope/rag/_reader/_word_reader.py@@ -4,12 +4,8 @@import base64import hashlibimport json-from typing import Literal+from typing import Literal, TYPE_CHECKING-from docx.oxml import CT_P, CT_Tbl-from docx.table import Table-from docx.text.paragraph import Paragraph-from docx.oxml.ns import qnfrom ._reader_base import ReaderBasefrom ._text_reader import TextReader@@ -17,20 +13,32 @@ from .._document import Document, DocMetadatafrom ..._logging import loggerfrom ...message import ImageBlock, Base64Source, TextBlock+if TYPE_CHECKING:+ from docx.table import Table as DocxTable+ from docx.text.paragraph import Paragraph as DocxParagraph+else:+ DocxTable = "docx.table.Table"+ DocxParagraph = "docx.text.paragraph.Paragraph"-def _extract_text_from_paragraph(para: Paragraph) -> str:++def _extract_text_from_paragraph(para: DocxParagraph) -> str:"""Extract text from a paragraph, including text in text boxes and shapes.Args:- para: Paragraph object+ para (`Paragraph`):+ The paragraph object from which to extract text.+Returns:- str: Extracted text+ `str`:+ Extracted text"""text = ""# Method 1: Extract all w:t elements directly from XML# (handles revisions, hyperlinks, etc.)+ from docx.oxml.ns import qn+for t_elem in para._element.findall(".//" + qn("w:t")):if t_elem.text:text += t_elem.text@@ -62,7 +70,7 @@ def _extract_text_from_paragraph(para: Paragraph) -> str:return text.strip()-def _extract_table_data(table: Table) -> list[list[str]]:+def _extract_table_data(table: DocxTable) -> list[list[str]]:"""Extract table data, handling merged cells and preserving line breakswithin cells.@@ -75,6 +83,8 @@ def _extract_table_data(table: Table) -> list[list[str]]:Table data represented as a 2D list."""+ from docx.oxml.ns import qn+table_data = []# Extract table cell elements directly from XMLfor tr in table._element.findall(qn("w:tr")):@@ -105,7 +115,7 @@ def _extract_table_data(table: Table) -> list[list[str]]:return table_data-def _extract_image_data(para: Paragraph) -> list[ImageBlock]:+def _extract_image_data(para: DocxParagraph) -> list[ImageBlock]:"""Extract image data from a paragraph.Args:@@ -118,6 +128,8 @@ def _extract_image_data(para: Paragraph) -> list[ImageBlock]:"""images = []+ from docx.oxml.ns import qn+# Method 1: Find all drawing elements (modern Word format)drawings = para._element.findall(".//" + qn("w:drawing"))@@ -334,6 +346,11 @@ class WordReader(ReaderBase):# Read the Word documenttry:from docx import Document as DocxDocument+ from docx.oxml import CT_P, CT_Tbl+ from docx.text.paragraph import Paragraph+ from docx.table import Table+ from docx.oxml.ns import qn+except ImportError as e:raise ImportError("Please install python-docx to use the Word reader. "diff --git a/tests/plan_test.py b/tests/plan_test.pyindex e9eeffad8..2baf7ad7d 100644--- a/tests/plan_test.py+++ b/tests/plan_test.py@@ -308,16 +308,21 @@ Subtask at index 2:self.assertIsNotNone(plan_notebook.current_plan)+ # Check the exported statestate = agent.state_dict()subtasks = plan_notebook.current_plan.subtasks+self.assertDictEqual(state,{"memory": {"content": []},"toolkit": {"active_groups": []},"plan_notebook": {- "storage": {},+ "storage": {+ "plans": {},+ },"current_plan": {+ "id": plan_notebook.current_plan.id,"name": "text","description": "abc","expected_outcome": "edf",@@ -326,17 +331,25 @@ Subtask at index 2:"name": "1","description": "1","expected_outcome": "1",+ "outcome": None,"state": "todo","created_at": subtasks[0].created_at,+ "finished_at": None,},{"name": "2","description": "2","expected_outcome": "2",+ "outcome": None,"state": "todo","created_at": subtasks[1].created_at,+ "finished_at": None,},],+ "created_at": plan_notebook.current_plan.created_at,+ "state": "todo",+ "finished_at": None,+ "outcome": None,},},"_reasoning_hint_msgs": {"content": []},@@ -344,6 +357,252 @@ Subtask at index 2:"_sys_prompt": "You are a helpful assistant named Friday. ",},)++ # Test finish the plan serialization+ res = await plan_notebook.update_subtask_state(+ 0,+ "in_progress",+ )+ self.assertEqual(+ res.content[0]["text"],+ "Subtask at index 0, named '1' is marked as 'in_progress' "+ "successfully. The plan state has been updated to 'in_progress'.",+ )++ self.assertEqual(+ plan_notebook.state_dict(),+ {+ "storage": {+ "plans": {},+ },+ "current_plan": {+ "id": plan_notebook.current_plan.id,+ "name": "text",+ "description": "abc",+ "expected_outcome": "edf",+ "subtasks": [+ {+ "name": "1",+ "description": "1",+ "expected_outcome": "1",+ "state": "in_progress",+ "created_at": subtasks[0].created_at,+ "outcome": None,+ "finished_at": None,+ },+ {+ "name": "2",+ "description": "2",+ "expected_outcome": "2",+ "state": "todo",+ "created_at": subtasks[1].created_at,+ "outcome": None,+ "finished_at": None,+ },+ ],+ "state": "in_progress",+ "created_at": plan_notebook.current_plan.created_at,+ "finished_at": None,+ "outcome": None,+ },+ },+ )++ # When finish a subtask+ await plan_notebook.finish_subtask(+ 0,+ subtask_outcome="abc",+ )+ self.assertDictEqual(+ plan_notebook.state_dict(),+ {+ "storage": {+ "plans": {},+ },+ "current_plan": {+ "id": plan_notebook.current_plan.id,+ "name": "text",+ "description": "abc",+ "expected_outcome": "edf",+ "created_at": plan_notebook.current_plan.created_at,+ "subtasks": [+ {+ "name": "1",+ "description": "1",+ "expected_outcome": "1",+ "state": "done",+ "created_at": subtasks[0].created_at,+ "finished_at": plan_notebook.current_plan.subtasks[+ 0+ ].finished_at,+ "outcome": "abc",+ },+ {+ "name": "2",+ "description": "2",+ "expected_outcome": "2",+ "state": "in_progress",+ "created_at": subtasks[1].created_at,+ "finished_at": None,+ "outcome": None,+ },+ ],+ "finished_at": None,+ "outcome": None,+ "state": "in_progress",+ },+ },+ )++ # Test deserialization+ await plan_notebook.finish_subtask(1, "def")+ self.assertDictEqual(+ plan_notebook.state_dict(),+ {+ "storage": {+ "plans": {},+ },+ "current_plan": {+ "id": plan_notebook.current_plan.id,+ "name": "text",+ "description": "abc",+ "expected_outcome": "edf",+ "created_at": plan_notebook.current_plan.created_at,+ "subtasks": [+ {+ "name": "1",+ "description": "1",+ "expected_outcome": "1",+ "state": "done",+ "created_at": subtasks[0].created_at,+ "finished_at": plan_notebook.current_plan.subtasks[+ 0+ ].finished_at,+ "outcome": "abc",+ },+ {+ "name": "2",+ "description": "2",+ "expected_outcome": "2",+ "state": "done",+ "created_at": subtasks[1].created_at,+ "finished_at": plan_notebook.current_plan.subtasks[+ 1+ ].finished_at,+ "outcome": "def",+ },+ ],+ "finished_at": None,+ "outcome": None,+ "state": "in_progress",+ },+ },+ )++ # Finish the plan+ await plan_notebook.finish_plan("done", "Overall outcome")+ self.assertIsNone(+ plan_notebook.current_plan,+ )++ # Check the finished plan+ plan = (await plan_notebook.storage.get_plans())[0]++ self.assertDictEqual(+ plan_notebook.state_dict(),+ {+ "storage": {+ "plans": {+ plan.id: {+ "id": plan.id,+ "name": "text",+ "description": "abc",+ "expected_outcome": "edf",+ "subtasks": [+ {+ "name": "1",+ "description": "1",+ "expected_outcome": "1",+ "outcome": "abc",+ "state": "done",+ "created_at": plan.subtasks[0].created_at,+ "finished_at": plan.subtasks[+ 0+ ].finished_at,+ },+ {+ "name": "2",+ "description": "2",+ "expected_outcome": "2",+ "outcome": "def",+ "state": "done",+ "created_at": plan.subtasks[1].created_at,+ "finished_at": plan.subtasks[+ 1+ ].finished_at,+ },+ ],+ "created_at": plan.created_at,+ "state": "done",+ "finished_at": plan.finished_at,+ "outcome": "Overall outcome",+ },+ },+ },+ "current_plan": None,+ },+ )++ # Load the state+ new_plan_notebook = PlanNotebook()+ new_plan_notebook.load_state_dict(+ plan_notebook.state_dict(),+ )+ self.assertDictEqual(+ new_plan_notebook.state_dict(),+ {+ "storage": {+ "plans": {+ plan.id: {+ "id": plan.id,+ "name": "text",+ "description": "abc",+ "expected_outcome": "edf",+ "subtasks": [+ {+ "name": "1",+ "description": "1",+ "expected_outcome": "1",+ "outcome": "abc",+ "state": "done",+ "created_at": plan.subtasks[0].created_at,+ "finished_at": plan.subtasks[+ 0+ ].finished_at,+ },+ {+ "name": "2",+ "description": "2",+ "expected_outcome": "2",+ "outcome": "def",+ "state": "done",+ "created_at": plan.subtasks[1].created_at,+ "finished_at": plan.subtasks[+ 1+ ].finished_at,+ },+ ],+ "created_at": plan.created_at,+ "state": "done",+ "finished_at": plan.finished_at,+ "outcome": "Overall outcome",+ },+ },+ },+ "current_plan": None,+ },+ )+plan_notebook.current_plan = Noneself.assertIsNone(agent.plan_notebook.current_plan,