🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
enhance: Workflow better filenames and custom session id (#3285)
Hesam Sheikh328bec84cf84
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.pyindex 6ef16aecd..077a7c11c 100644--- a/camel/agents/chat_agent.py+++ b/camel/agents/chat_agent.py@@ -1247,11 +1247,29 @@ class ChatAgent(BaseAgent):result["status"] = status_messagereturn result- base_filename = (- filename- if filename- else f"context_summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}" # noqa: E501- )+ # handle structured output if response_format was provided+ structured_output = None+ if response_format and response.msgs[-1].parsed:+ structured_output = response.msgs[-1].parsed++ # determine filename: use provided filename, or extract from+ # structured output, or generate timestamp+ if filename:+ base_filename = filename+ elif structured_output and hasattr(+ structured_output, 'task_title'+ ):+ # use task_title from structured output for filename+ task_title = structured_output.task_title+ clean_title = ContextUtility.sanitize_workflow_filename(+ task_title+ )+ base_filename = (+ f"{clean_title}_workflow" if clean_title else "workflow"+ )+ else:+ base_filename = f"context_summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}" # noqa: E501+base_filename = Path(base_filename).with_suffix("").namemetadata = context_util.get_session_metadata()@@ -1262,11 +1280,9 @@ class ChatAgent(BaseAgent):})- # Handle structured output if response_format was provided- structured_output = None- if response_format and response.msgs[-1].parsed:- structured_output = response.msgs[-1].parsed- # Convert structured output to custom markdown+ # convert structured output to custom markdown if present+ if structured_output:+ # convert structured output to custom markdownsummary_content = context_util.structured_output_to_markdown(structured_data=structured_output, metadata=metadata)diff --git a/camel/societies/workforce/single_agent_worker.py b/camel/societies/workforce/single_agent_worker.pyindex f79073613..327a4d17d 100644--- a/camel/societies/workforce/single_agent_worker.py+++ b/camel/societies/workforce/single_agent_worker.py@@ -80,6 +80,7 @@ class AgentPool:self._in_use_agents: set = set()self._agent_last_used: dict = {}self._lock = asyncio.Lock()+ self._condition = asyncio.Condition(self._lock)# Statisticsself._total_borrows = 0@@ -105,36 +106,31 @@ class AgentPool:async def get_agent(self) -> ChatAgent:r"""Get an agent from the pool, creating one if necessary."""- async with self._lock:+ async with self._condition:self._total_borrows += 1- if self._available_agents:- agent = self._available_agents.popleft()- self._in_use_agents.add(id(agent))- self._pool_hits += 1- return agent-- # Check if we can create a new agent- if len(self._in_use_agents) < self.max_size or self.auto_scale:- agent = self._create_fresh_agent()- self._in_use_agents.add(id(agent))- return agent-- # Wait for available agent- while True:- async with self._lock:+ # Try to get available agent or create new one+ while True:if self._available_agents:agent = self._available_agents.popleft()self._in_use_agents.add(id(agent))self._pool_hits += 1return agent- await asyncio.sleep(0.05)++ # Check if we can create a new agent+ if len(self._in_use_agents) < self.max_size or self.auto_scale:+ agent = self._create_fresh_agent()+ self._in_use_agents.add(id(agent))+ return agent++ # Wait for an agent to be returned+ await self._condition.wait()async def return_agent(self, agent: ChatAgent) -> None:r"""Return an agent to the pool."""agent_id = id(agent)- async with self._lock:+ async with self._condition:if agent_id not in self._in_use_agents:return@@ -145,6 +141,8 @@ class AgentPool:agent.reset()self._agent_last_used[agent_id] = time.time()self._available_agents.append(agent)+ # Notify one waiting coroutine that an agent is available+ self._condition.notify()else:# Remove tracking for agents not returned to poolself._agent_last_used.pop(agent_id, None)@@ -154,7 +152,7 @@ class AgentPool:if not self.auto_scale:return- async with self._lock:+ async with self._condition:if not self._available_agents:return@@ -428,6 +426,7 @@ class SingleAgentWorker(Worker):"usage") or final_response.info.get("token_usage")else:+ final_response = responseusage_info = response.info.get("usage") or response.info.get("token_usage")@@ -562,10 +561,11 @@ class SingleAgentWorker(Worker):while True:try:# Fixed interval cleanup- await asyncio.sleep(self.agent_pool.cleanup_interval)-if self.agent_pool:+ await asyncio.sleep(self.agent_pool.cleanup_interval)await self.agent_pool.cleanup_idle_agents()+ else:+ breakexcept asyncio.CancelledError:breakexcept Exception as e:@@ -583,7 +583,8 @@ class SingleAgentWorker(Worker):This method generates a workflow summary from the worker agent'sconversation history and saves it to a markdown file. The filename- is based on the worker's description for easy loading later.+ is based on either the worker's explicit role_name or the generated+ task_title from the summary.Returns:Dict[str, Any]: Result dictionary with keys:@@ -603,13 +604,31 @@ class SingleAgentWorker(Worker):self.worker.set_context_utility(context_util)# prepare workflow summarization components- filename = self._generate_workflow_filename()structured_prompt = self._prepare_workflow_prompt()agent_to_summarize = self._select_agent_for_summarization(context_util)+ # check if we should use role_name or let summarize extract+ # task_title+ role_name = getattr(self.worker, 'role_name', 'assistant')+ use_role_name_for_filename = role_name.lower() not in {+ 'assistant',+ 'agent',+ 'user',+ 'system',+ }+# generate and save workflow summary+ # if role_name is explicit, use it for filename+ # if role_name is generic, pass none to let summarize use+ # task_title+ filename = (+ self._generate_workflow_filename()+ if use_role_name_for_filename+ else None+ )+result = agent_to_summarize.summarize(filename=filename,summary_prompt=structured_prompt,@@ -716,12 +735,23 @@ class SingleAgentWorker(Worker):)return []- # generate filename-safe search pattern from worker description+ # generate filename-safe search pattern from worker role nameif pattern is None:- # sanitize description: spaces to underscores, remove special chars- clean_desc = self.description.lower().replace(" ", "_")- clean_desc = re.sub(r'[^a-z0-9_]', '', clean_desc)- pattern = f"{clean_desc}_workflow*.md"+ from camel.utils.context_utils import ContextUtility++ # get role_name (always available, defaults to "assistant")+ role_name = getattr(self.worker, 'role_name', 'assistant')+ clean_name = ContextUtility.sanitize_workflow_filename(role_name)++ # check if role_name is generic+ generic_names = {'assistant', 'agent', 'user', 'system'}+ if clean_name in generic_names:+ # for generic role names, search for all workflow files+ # since filename is based on task_title+ pattern = "*_workflow*.md"+ else:+ # for explicit role names, search for role-specific files+ pattern = f"{clean_name}_workflow*.md"# Get the base workforce_workflows directorycamel_workdir = os.environ.get("CAMEL_WORKDIR")@@ -816,15 +846,21 @@ class SingleAgentWorker(Worker):return Nonedef _generate_workflow_filename(self) -> str:- r"""Generate a filename for the workflow based on worker description.+ r"""Generate a filename for the workflow based on worker role name.++ Uses the worker's explicit role_name when available.Returns:- str: Sanitized filename without timestamp (session already has- timestamp).+ str: Sanitized filename without timestamp and without .md+ extension. Format: {role_name}_workflow"""- clean_desc = self.description.lower().replace(" ", "_")- clean_desc = re.sub(r'[^a-z0-9_]', '', clean_desc)- return f"{clean_desc}_workflow"+ from camel.utils.context_utils import ContextUtility++ # get role_name (always available, defaults to "assistant"/"Assistant")+ role_name = getattr(self.worker, 'role_name', 'assistant')+ clean_name = ContextUtility.sanitize_workflow_filename(role_name)++ return f"{clean_name}_workflow"def _prepare_workflow_prompt(self) -> str:r"""Prepare the structured prompt for workflow summarization.diff --git a/camel/societies/workforce/workforce.py b/camel/societies/workforce/workforce.pyindex ccfd7dff8..431166079 100644--- a/camel/societies/workforce/workforce.py+++ b/camel/societies/workforce/workforce.py@@ -463,20 +463,27 @@ class Workforce(BaseNode):# Helper for propagating pause control to externally supplied agents# ------------------------------------------------------------------- def _get_or_create_shared_context_utility(self) -> "ContextUtility":+ def _get_or_create_shared_context_utility(+ self,+ session_id: Optional[str] = None,+ ) -> "ContextUtility":r"""Get or create the shared context utility for workflow management.This method creates the context utility only when needed, avoidingunnecessary session folder creation during initialization.+ Args:+ session_id (Optional[str]): Custom session ID to use. If None,+ auto-generates a timestamped session ID. (default: :obj:`None`)+Returns:ContextUtility: The shared context utility instance."""if self._shared_context_utility is None:from camel.utils.context_utils import ContextUtility- self._shared_context_utility = (- ContextUtility.get_workforce_shared()+ self._shared_context_utility = ContextUtility.get_workforce_shared(+ session_id=session_id)return self._shared_context_utility@@ -2132,7 +2139,10 @@ class Workforce(BaseNode):else:self.metrics_logger = WorkforceLogger(workforce_id=self.node_id)- def save_workflow_memories(self) -> Dict[str, str]:+ def save_workflow_memories(+ self,+ session_id: Optional[str] = None,+ ) -> Dict[str, str]:r"""Save workflow memories for all SingleAgentWorker instances in theworkforce.@@ -2142,6 +2152,12 @@ class Workforce(BaseNode):method.Other worker types are skipped.+ Args:+ session_id (Optional[str]): Custom session ID to use for saving+ workflows. If None, auto-generates a timestamped session ID.+ Useful for organizing workflows by project or context.+ (default: :obj:`None`)+Returns:Dict[str, str]: Dictionary mapping worker node IDs to save results.Values are either file paths (success) or error messages@@ -2150,15 +2166,22 @@ class Workforce(BaseNode):Example:>>> workforce = Workforce("My Team")>>> # ... add workers and process tasks ...- >>> results = workforce.save_workflows()+ >>> # save with auto-generated session id+ >>> results = workforce.save_workflow_memories()>>> print(results)- {'worker_123': '/path/to/data_analyst_workflow_20250122.md',+ {'worker_123': '/path/to/developer_agent_workflow.md','worker_456': 'error: No conversation context available'}+ >>> # save with custom project id+ >>> results = workforce.save_workflow_memories(+ ... session_id="project_123"+ ... )"""results = {}# Get or create shared context utility for this save operation- shared_context_utility = self._get_or_create_shared_context_utility()+ shared_context_utility = self._get_or_create_shared_context_utility(+ session_id=session_id+ )for child in self._children:if isinstance(child, SingleAgentWorker):diff --git a/camel/utils/context_utils.py b/camel/utils/context_utils.pyindex a3b70c6b2..3c1bb5ca5 100644--- a/camel/utils/context_utils.py+++ b/camel/utils/context_utils.py@@ -13,6 +13,7 @@# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========import os+import refrom datetime import datetimefrom pathlib import Pathfrom typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional@@ -131,6 +132,10 @@ class ContextUtility:- Shared session management for workforce workflows"""+ # maximum filename length for workflow files (chosen for filesystem+ # compatibility and readability)+ MAX_WORKFLOW_FILENAME_LENGTH: ClassVar[int] = 50+# Class variables for shared session management_shared_sessions: ClassVar[Dict[str, 'ContextUtility']] = {}_default_workforce_session: ClassVar[Optional['ContextUtility']] = None@@ -191,6 +196,54 @@ class ContextUtility:timestamp = datetime.now().strftime('%Y%m%d_%H%M%S_%f')return f"session_{timestamp}"+ @staticmethod+ def sanitize_workflow_filename(+ name: str,+ max_length: Optional[int] = None,+ ) -> str:+ r"""Sanitize a name string for use as a workflow filename.++ Converts the input string to a safe filename by:+ - converting to lowercase+ - replacing spaces with underscores+ - removing special characters (keeping only alphanumeric and+ underscores)+ - truncating to maximum length if specified++ Args:+ name (str): The name string to sanitize (e.g., role_name or+ task_title).+ max_length (Optional[int]): Maximum length for the sanitized+ filename. If None, uses MAX_WORKFLOW_FILENAME_LENGTH.+ (default: :obj:`None`)++ Returns:+ str: Sanitized filename string suitable for filesystem use.+ Returns "agent" if sanitization results in empty string.++ Example:+ >>> ContextUtility.sanitize_workflow_filename("Data Analyst!")+ 'data_analyst'+ >>> ContextUtility.sanitize_workflow_filename("Test@123", 5)+ 'test1'+ """+ if max_length is None:+ max_length = ContextUtility.MAX_WORKFLOW_FILENAME_LENGTH++ # sanitize: lowercase, spaces to underscores, remove special chars+ clean_name = name.lower().replace(" ", "_")+ clean_name = re.sub(r'[^a-z0-9_]', '', clean_name)++ # truncate if too long+ if len(clean_name) > max_length:+ clean_name = clean_name[:max_length]++ # ensure it's not empty after sanitization+ if not clean_name:+ clean_name = "agent"++ return clean_name+# ========= GENERIC FILE MANAGEMENT METHODS =========def _ensure_directory_exists(self) -> None:diff --git a/test/workforce/test_workflow_memory.py b/test/workforce/test_workflow_memory.pyindex fb68be418..bf5cb6086 100644--- a/test/workforce/test_workflow_memory.py+++ b/test/workforce/test_workflow_memory.py@@ -459,9 +459,11 @@ class TestWorkforceWorkflowMemoryMethods:mock_summarize.assert_called_once()call_kwargs = mock_summarize.call_args[1]- # Verify filename generation includes worker description+ # Verify filename generation includes worker role_name+ # (not description, as that would be too long)assert 'filename' in call_kwargs- assert 'data_analyst_workflow' in call_kwargs['filename']+ # MockSingleAgentWorker uses "Test Worker" as role_name+ assert 'test_worker_workflow' in call_kwargs['filename']# Verify structured output format is set (WorkflowSummary)assert 'response_format' in call_kwargs@@ -615,6 +617,155 @@ class TestWorkflowIntegration:result = worker.save_workflow_memories()assert result["status"] == "success"+ def test_long_description_filename_generation(self, temp_context_dir):+ """Test that very long descriptions don't create unwieldy filenames.++ This test addresses issue #3277 where long worker descriptions+ caused filesystem errors due to excessive filename lengths.+ """+ # create worker with extremely long description (like in eigent.py)+ long_desc = (+ "Developer Agent: A master-level coding assistant with a "+ "powerful terminal. It can write and execute code, manage "+ "files, automate desktop tasks, and deploy web applications "+ "to solve complex technical challenges."+ )++ # create a test worker with long description but short role_name+ sys_msg = BaseMessage.make_assistant_message(+ role_name="Developer Agent",+ content="You are a developer agent.",+ )+ agent = ChatAgent(sys_msg)+ worker = SingleAgentWorker(+ description=long_desc,+ worker=agent,+ enable_workflow_memory=True,+ )++ # mock the summarize method+ mock_result = {+ "status": "success",+ "summary": "Test workflow summary",+ "file_path": f"{temp_context_dir}/developer_agent_workflow.md",+ }++ with patch.object(agent, 'summarize', return_value=mock_result):+ result = worker.save_workflow_memories()++ assert result["status"] == "success"++ # verify filename is reasonable length (< 100 chars including path)+ filename = os.path.basename(result["file_path"])+ assert (+ len(filename) < 100+ ), f"Filename too long: {len(filename)} chars - {filename}"++ # verify filename uses role_name, not full description+ assert "developer_agent_workflow" in filename+ assert "master_level_coding_assistant" not in filename++ def test_filename_generation_with_generic_role_name(+ self, temp_context_dir+ ):+ """Test filename generation with generic role_name uses task_title.++ When agent has a generic role_name like "assistant", the filename+ should be based on the task_title from the generated workflow summary.+ """+ # create worker with generic role_name+ sys_msg = BaseMessage.make_assistant_message(+ role_name="Assistant", # generic role name+ content="You are a helpful assistant.",+ )+ agent = ChatAgent(sys_msg)++ worker = SingleAgentWorker(+ description="Data analyst worker",+ worker=agent,+ enable_workflow_memory=True,+ )++ # verify role_name is generic+ assert agent.role_name.lower() in {+ 'assistant',+ 'agent',+ 'user',+ 'system',+ }++ # mock the summarize method to return a workflow with task_title+ mock_result = {+ "status": "success",+ "summary": "Test workflow summary",+ "file_path": f"{temp_context_dir}/analyze_sales_data_workflow.md",+ "structured_summary": type(+ 'obj', (object,), {'task_title': 'Analyze sales data'}+ )(),+ }++ with patch.object(agent, 'summarize', return_value=mock_result):+ # note: in real execution, file would be renamed to use task_title+ # the test shows that with generic role_name, we rely on task_title+ result = worker.save_workflow_memories()++ assert result["status"] == "success"+ # filename should be based on task_title, not role_name+ assert (+ "sales" in result["file_path"]+ or "analyze" in result["file_path"]+ )++ def test_custom_session_id_integration(self, temp_context_dir):+ """Test end-to-end workflow with custom session ID.++ This test addresses issue #3277 request for custom session IDs.+ """+ # create workforce with custom session id+ workforce = Workforce("Test Team")+ worker = MockSingleAgentWorker("test_analyst")+ workforce._children = [worker]++ custom_session = "project_abc123"++ # simulate conversation+ user_msg = BaseMessage.make_user_message(+ role_name="User", content="Analyze data"+ )+ assistant_msg = BaseMessage.make_assistant_message(+ role_name="Assistant", content="Analysis complete"+ )+ worker.worker.record_message(user_msg)+ worker.worker.record_message(assistant_msg)++ # mock the summarize method+ expected_path = (+ f"{temp_context_dir}/workforce_workflows/"+ f"{custom_session}/test_worker_workflow.md"+ )+ mock_result = {+ "status": "success",+ "summary": "Test workflow",+ "file_path": expected_path,+ }++ with patch.object(ChatAgent, 'summarize', return_value=mock_result):+ # save with custom session id+ results = workforce.save_workflow_memories(+ session_id=custom_session+ )++ # verify results contain the custom session path+ assert worker.node_id in results+ assert custom_session in results[worker.node_id]++ # verify the shared context utility was created with custom+ # session+ assert workforce._shared_context_utility is not None+ assert (+ workforce._shared_context_utility.session_id == custom_session+ )+class TestSharedContextUtility:"""Test shared context utility functionality."""