Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: Workforce Workflow (#3180)

Hesam Sheikhfe58156f0502
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index b15e5387d..6ef16aecd 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -14,15 +14,22 @@
from __future__ import annotations
import asyncio
+import atexit
+import base64
import concurrent.futures
+import hashlib
+import inspect
import json
-import logging
-import queue
+import math
+import os
import random
+import re
+import tempfile
import textwrap
import threading
import time
import uuid
+import warnings
from datetime import datetime
from pathlib import Path
from typing import (
@@ -101,10 +108,24 @@ if TYPE_CHECKING:
logger = get_logger(__name__)
+# Cleanup temp files on exit
+_temp_files: Set[str] = set()
+_temp_files_lock = threading.Lock()
+
+
+def _cleanup_temp_files():
+ with _temp_files_lock:
+ for path in _temp_files:
+ try:
+ os.unlink(path)
+ except Exception:
+ pass
+
+
+atexit.register(_cleanup_temp_files)
+
# AgentOps decorator setting
try:
- import os
-
if os.getenv("AGENTOPS_API_KEY") is not None:
from agentops import track_agent
else:
@@ -194,13 +215,10 @@ class StreamingChatAgentResponse:
def _ensure_latest_response(self):
r"""Ensure we have the latest response by consuming the generator."""
if not self._consumed:
- try:
- for response in self._generator:
- self._responses.append(response)
- self._current_response = response
- self._consumed = True
- except StopIteration:
- self._consumed = True
+ for response in self._generator:
+ self._responses.append(response)
+ self._current_response = response
+ self._consumed = True
@property
def msgs(self) -> List[BaseMessage]:
@@ -241,14 +259,11 @@ class StreamingChatAgentResponse:
yield from self._responses
else:
# If not consumed, consume and yield
- try:
- for response in self._generator:
- self._responses.append(response)
- self._current_response = response
- yield response
- self._consumed = True
- except StopIteration:
- self._consumed = True
+ for response in self._generator:
+ self._responses.append(response)
+ self._current_response = response
+ yield response
+ self._consumed = True
def __getattr__(self, name):
r"""Forward any other attribute access to the latest response."""
@@ -279,13 +294,10 @@ class AsyncStreamingChatAgentResponse:
async def _ensure_latest_response(self):
r"""Ensure the latest response by consuming the async generator."""
if not self._consumed:
- try:
- async for response in self._async_generator:
- self._responses.append(response)
- self._current_response = response
- self._consumed = True
- except StopAsyncIteration:
- self._consumed = True
+ async for response in self._async_generator:
+ self._responses.append(response)
+ self._current_response = response
+ self._consumed = True
async def _get_final_response(self) -> ChatAgentResponse:
r"""Get the final response after consuming the entire stream."""
@@ -311,14 +323,11 @@ class AsyncStreamingChatAgentResponse:
else:
# If not consumed, consume and yield
async def _consume_and_yield():
- try:
- async for response in self._async_generator:
- self._responses.append(response)
- self._current_response = response
- yield response
- self._consumed = True
- except StopAsyncIteration:
- self._consumed = True
+ async for response in self._async_generator:
+ self._responses.append(response)
+ self._current_response = response
+ yield response
+ self._consumed = True
return _consume_and_yield()
@@ -386,9 +395,10 @@ class ChatAgent(BaseAgent):
for individual tool execution. If None, wait indefinitely.
mask_tool_output (Optional[bool]): Whether to return a sanitized
placeholder instead of the raw tool output. (default: :obj:`False`)
- pause_event (Optional[asyncio.Event]): Event to signal pause of the
- agent's operation. When clear, the agent will pause its execution.
- (default: :obj:`None`)
+ pause_event (Optional[Union[threading.Event, asyncio.Event]]): Event to
+ signal pause of the agent's operation. When clear, the agent will
+ pause its execution. Use threading.Event for sync operations or
+ asyncio.Event for async operations. (default: :obj:`None`)
prune_tool_calls_from_memory (bool): Whether to clean tool
call messages from memory after response generation to save token
usage. When enabled, removes FUNCTION/TOOL role messages and
@@ -443,7 +453,7 @@ class ChatAgent(BaseAgent):
stop_event: Optional[threading.Event] = None,
tool_execution_timeout: Optional[float] = None,
mask_tool_output: bool = False,
- pause_event: Optional[asyncio.Event] = None,
+ pause_event: Optional[Union[threading.Event, asyncio.Event]] = None,
prune_tool_calls_from_memory: bool = False,
retry_attempts: int = 3,
retry_delay: float = 1.0,
@@ -532,6 +542,7 @@ class ChatAgent(BaseAgent):
self.tool_execution_timeout = tool_execution_timeout
self.mask_tool_output = mask_tool_output
self._secure_result_store: Dict[str, Any] = {}
+ self._secure_result_store_lock = threading.Lock()
self.pause_event = pause_event
self.prune_tool_calls_from_memory = prune_tool_calls_from_memory
self.retry_attempts = max(1, retry_attempts)
@@ -728,6 +739,20 @@ class ChatAgent(BaseAgent):
# Ensure the new memory has the system message
self.init_messages()
+ def set_context_utility(
+ self, context_utility: Optional[ContextUtility]
+ ) -> None:
+ r"""Set the context utility for the agent.
+
+ This allows external components (like SingleAgentWorker) to provide
+ a shared context utility instance for workflow management.
+
+ Args:
+ context_utility (ContextUtility, optional): The context utility
+ to use. If None, the agent will create its own when needed.
+ """
+ self._context_utility = context_utility
+
def _get_full_tool_schemas(self) -> List[Dict[str, Any]]:
r"""Returns a list of tool schemas of all tools, including internal
and external tools.
@@ -817,9 +842,6 @@ class ChatAgent(BaseAgent):
(default: :obj:`None`)
(default: obj:`None`)
"""
- import math
- import time
- import uuid as _uuid
# 1. Helper to write a record to memory
def _write_single_record(
@@ -854,7 +876,6 @@ class ChatAgent(BaseAgent):
current_tokens = token_counter.count_tokens_from_messages(
[message.to_openai_message(role)]
)
- import warnings
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=EmptyMemoryWarning)
@@ -928,7 +949,7 @@ class ChatAgent(BaseAgent):
# 4. Calculate how many chunks we will need with this body size.
num_chunks = math.ceil(len(all_token_ids) / chunk_body_limit)
- group_id = str(_uuid.uuid4())
+ group_id = str(uuid.uuid4())
for i in range(num_chunks):
start_idx = i * chunk_body_limit
@@ -1050,6 +1071,7 @@ class ChatAgent(BaseAgent):
self,
filename: Optional[str] = None,
summary_prompt: Optional[str] = None,
+ response_format: Optional[Type[BaseModel]] = None,
working_directory: Optional[Union[str, Path]] = None,
) -> Dict[str, Any]:
r"""Summarize the agent's current conversation context and persist it
@@ -1062,13 +1084,18 @@ class ChatAgent(BaseAgent):
summary_prompt (Optional[str]): Custom prompt for the summarizer.
When omitted, a default prompt highlighting key decisions,
action items, and open questions is used.
+ response_format (Optional[Type[BaseModel]]): A Pydantic model
+ defining the expected structure of the response. If provided,
+ the summary will be generated as structured output and included
+ in the result.
working_directory (Optional[str|Path]): Optional directory to save
the markdown summary file. If provided, overrides the default
directory used by ContextUtility.
Returns:
Dict[str, Any]: A dictionary containing the summary text, file
- path, and status message.
+ path, status message, and optionally structured_summary if
+ response_format was provided.
"""
result: Dict[str, Any] = {
@@ -1078,6 +1105,7 @@ class ChatAgent(BaseAgent):
}
try:
+ # Use external context if set, otherwise create local one
if self._context_utility is None:
if working_directory is not None:
self._context_utility = ContextUtility(
@@ -1085,6 +1113,7 @@ class ChatAgent(BaseAgent):
)
else:
self._context_utility = ContextUtility()
+ context_util = self._context_utility
# Get conversation directly from agent's memory
messages, _ = self.memory.get_context()
@@ -1101,7 +1130,58 @@ class ChatAgent(BaseAgent):
for message in messages:
role = message.get('role', 'unknown')
content = message.get('content', '')
- if content:
+
+ # Handle tool call messages (assistant calling tools)
+ tool_calls = message.get('tool_calls')
+ if tool_calls and isinstance(tool_calls, (list, tuple)):
+ for tool_call in tool_calls:
+ # Handle both dict and object formats
+ if isinstance(tool_call, dict):
+ func_name = tool_call.get('function', {}).get(
+ 'name', 'unknown_tool'
+ )
+ func_args_str = tool_call.get('function', {}).get(
+ 'arguments', '{}'
+ )
+ else:
+ # Handle object format (Pydantic or similar)
+ func_name = getattr(
+ getattr(tool_call, 'function', None),
+ 'name',
+ 'unknown_tool',
+ )
+ func_args_str = getattr(
+ getattr(tool_call, 'function', None),
+ 'arguments',
+ '{}',
+ )
+
+ # Parse and format arguments for readability
+ try:
+ import json
+
+ args_dict = json.loads(func_args_str)
+ args_formatted = ', '.join(
+ f"{k}={v}" for k, v in args_dict.items()
+ )
+ except (json.JSONDecodeError, ValueError, TypeError):
+ args_formatted = func_args_str
+
+ conversation_lines.append(
+ f"[TOOL CALL] {func_name}({args_formatted})"
+ )
+
+ # Handle tool response messages
+ elif role == 'tool':
+ tool_name = message.get('name', 'unknown_tool')
+ if not content:
+ content = str(message.get('content', ''))
+ conversation_lines.append(
+ f"[TOOL RESULT] {tool_name} → {content}"
+ )
+
+ # Handle regular content messages (user/assistant/system)
+ elif content:
conversation_lines.append(f"{role}: {content}")
conversation_text = "\n".join(conversation_lines).strip()
@@ -1117,7 +1197,7 @@ class ChatAgent(BaseAgent):
self._context_summary_agent = ChatAgent(
system_message=(
"You are a helpful assistant that summarizes "
- "conversations into concise markdown bullet lists."
+ "conversations"
),
model=self.model_backend,
agent_id=f"{self.agent_id}_context_summarizer",
@@ -1128,7 +1208,8 @@ class ChatAgent(BaseAgent):
if summary_prompt:
prompt_text = (
f"{summary_prompt.rstrip()}\n\n"
- f"Context information:\n{conversation_text}"
+ f"AGENT CONVERSATION TO BE SUMMARIZED:\n"
+ f"{conversation_text}"
)
else:
prompt_text = (
@@ -1138,7 +1219,13 @@ class ChatAgent(BaseAgent):
)
try:
- response = self._context_summary_agent.step(prompt_text)
+ # Use structured output if response_format is provided
+ if response_format:
+ response = self._context_summary_agent.step(
+ prompt_text, response_format=response_format
+ )
+ else:
+ response = self._context_summary_agent.step(prompt_text)
except Exception as step_exc:
error_message = (
f"Failed to generate summary using model: {step_exc}"
@@ -1167,7 +1254,7 @@ class ChatAgent(BaseAgent):
)
base_filename = Path(base_filename).with_suffix("").name
- metadata = self._context_utility.get_session_metadata()
+ metadata = context_util.get_session_metadata()
metadata.update(
{
"agent_id": self.agent_id,
@@ -1175,25 +1262,38 @@ class ChatAgent(BaseAgent):
}
)
- save_status = self._context_utility.save_markdown_file(
+ # 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
+ summary_content = context_util.structured_output_to_markdown(
+ structured_data=structured_output, metadata=metadata
+ )
+
+ # Save the markdown (either custom structured or default)
+ save_status = context_util.save_markdown_file(
base_filename,
summary_content,
- title="Conversation Summary",
- metadata=metadata,
+ title="Conversation Summary"
+ if not structured_output
+ else None,
+ metadata=metadata if not structured_output else None,
)
file_path = (
- self._context_utility.get_working_directory()
- / f"{base_filename}.md"
+ context_util.get_working_directory() / f"{base_filename}.md"
)
- result.update(
- {
- "summary": summary_content,
- "file_path": str(file_path),
- "status": save_status,
- }
- )
+ # Prepare result dictionary
+ result_dict = {
+ "summary": summary_content,
+ "file_path": str(file_path),
+ "status": save_status,
+ "structured_summary": structured_output,
+ }
+
+ result.update(result_dict)
logger.info("Conversation summary saved to %s", file_path)
return result
@@ -1246,8 +1346,6 @@ class ChatAgent(BaseAgent):
r"""Initializes the stored messages list with the current system
message.
"""
- import time
-
self.memory.clear()
# avoid UserWarning: The `ChatHistoryMemory` is empty.
if self.system_message is not None:
@@ -1260,6 +1358,17 @@ class ChatAgent(BaseAgent):
)
)
+ def reset_to_original_system_message(self) -> None:
+ r"""Reset system message to original, removing any appended context.
+
+ This method reverts the agent's system message back to its original
+ state, removing any workflow context or other modifications that may
+ have been appended. Useful for resetting agent state in multi-turn
+ scenarios.
+ """
+ self._system_message = self._original_system_message
+ self.init_messages()
+
def record_message(self, message: BaseMessage) -> None:
r"""Records the externally provided message into the agent memory as if
it were an answer of the :obj:`ChatAgent` from the backend. Currently,
@@ -1321,7 +1430,7 @@ class ChatAgent(BaseAgent):
# Create a prompt based on the schema
format_instruction = (
- "\n\nPlease respond in the following JSON format:\n" "{\n"
+ "\n\nPlease respond in the following JSON format:\n{\n"
)
properties = schema.get("properties", {})
@@ -1420,8 +1529,6 @@ class ChatAgent(BaseAgent):
Returns:
bool: True if called from a RegisteredAgentToolkit, False otherwise
"""
- import inspect
-
from camel.toolkits.base import RegisteredAgentToolkit
try:
@@ -1453,7 +1560,6 @@ class ChatAgent(BaseAgent):
try:
# Try to extract JSON from the response content
import json
- import re
from pydantic import ValidationError
@@ -1492,8 +1598,7 @@ class ChatAgent(BaseAgent):
if not message.parsed:
logger.warning(
- f"Failed to parse JSON from response: "
- f"{content}"
+ f"Failed to parse JSON from response: {content}"
)
except Exception as e:
@@ -1665,8 +1770,13 @@ class ChatAgent(BaseAgent):
while True:
if self.pause_event is not None and not self.pause_event.is_set():
- while not self.pause_event.is_set():
- time.sleep(0.001)
+ # Use efficient blocking wait for threading.Event
+ if isinstance(self.pause_event, threading.Event):
+ self.pause_event.wait()
+ else:
+ # Fallback for asyncio.Event in sync context
+ while not self.pause_event.is_set():
+ time.sleep(0.001)
try:
openai_messages, num_tokens = self.memory.get_context()
@@ -1698,7 +1808,7 @@ class ChatAgent(BaseAgent):
if self.stop_event and self.stop_event.is_set():
# Use the _step_terminate to terminate the agent with reason
logger.info(
- f"Termination triggered at iteration " f"{iteration_count}"
+ f"Termination triggered at iteration {iteration_count}"
)
return self._step_terminate(
accumulated_context_tokens,
@@ -1721,8 +1831,11 @@ class ChatAgent(BaseAgent):
self.pause_event is not None
and not self.pause_event.is_set()
):
- while not self.pause_event.is_set():
- time.sleep(0.001)
+ if isinstance(self.pause_event, threading.Event):
+ self.pause_event.wait()
+ else:
+ while not self.pause_event.is_set():
+ time.sleep(0.001)
result = self._execute_tool(tool_call_request)
tool_call_records.append(result)
@@ -1879,7 +1992,12 @@ class ChatAgent(BaseAgent):
prev_num_openai_messages: int = 0
while True:
if self.pause_event is not None and not self.pause_event.is_set():
- await self.pause_event.wait()
+ if isinstance(self.pause_event, asyncio.Event):
+ await self.pause_event.wait()
+ elif isinstance(self.pause_event, threading.Event):
+ # For threading.Event in async context, run in executor
+ loop = asyncio.get_event_loop()
+ await loop.run_in_executor(None, self.pause_event.wait)
try:
openai_messages, num_tokens = self.memory.get_context()
accumulated_context_tokens += num_tokens
@@ -1909,7 +2027,7 @@ class ChatAgent(BaseAgent):
if self.stop_event and self.stop_event.is_set():
# Use the _step_terminate to terminate the agent with reason
logger.info(
- f"Termination triggered at iteration " f"{iteration_count}"
+ f"Termination triggered at iteration {iteration_count}"
)
return self._step_terminate(
accumulated_context_tokens,
@@ -1932,7 +2050,13 @@ class ChatAgent(BaseAgent):
self.pause_event is not None
and not self.pause_event.is_set()
):
- await self.pause_event.wait()
+ if isinstance(self.pause_event, asyncio.Event):
+ await self.pause_event.wait()
+ elif isinstance(self.pause_event, threading.Event):
+ loop = asyncio.get_event_loop()
+ await loop.run_in_executor(
+ None, self.pause_event.wait
+ )
tool_call_record = await self._aexecute_tool(
tool_call_request
)
@@ -2185,11 +2309,6 @@ class ChatAgent(BaseAgent):
Returns:
List[OpenAIMessage]: The sanitized OpenAI messages.
"""
- import hashlib
- import os
- import re
- import tempfile
-
# Create a copy of messages for logging to avoid modifying the
# original messages
sanitized_messages = []
@@ -2230,7 +2349,14 @@ class ChatAgent(BaseAgent):
# Save image to temp directory for viewing
try:
- import base64
+ # Sanitize img_format to prevent path
+ # traversal
+ safe_format = re.sub(
+ r'[^a-zA-Z0-9]', '', img_format
+ )[:10]
+ img_filename = (
+ f"image_{img_hash}.{safe_format}"
+ )
temp_dir = tempfile.gettempdir()
img_path = os.path.join(
@@ -2245,6 +2371,9 @@ class ChatAgent(BaseAgent):
base64_data
)
)
+ # Register for cleanup
+ with _temp_files_lock:
+ _temp_files.add(img_path)
# Create a file:// URL that can be
# opened
@@ -2497,7 +2626,8 @@ class ChatAgent(BaseAgent):
try:
raw_result = tool(**args)
if self.mask_tool_output:
- self._secure_result_store[tool_call_id] = raw_result
+ with self._secure_result_store_lock:
+ self._secure_result_store[tool_call_id] = raw_result
result = (
"[The tool has been executed successfully, but the output"
" from the tool is masked. You can move forward]"
@@ -2555,7 +2685,7 @@ class ChatAgent(BaseAgent):
# Capture the error message to prevent framework crash
error_msg = f"Error executing async tool '{func_name}': {e!s}"
result = f"Tool execution failed: {error_msg}"
- logging.warning(error_msg)
+ logger.warning(error_msg)
return self._record_tool_calling(func_name, args, result, tool_call_id)
def _record_tool_calling(
@@ -2606,8 +2736,6 @@ class ChatAgent(BaseAgent):
# This ensures the assistant message (tool call) always appears before
# the function message (tool result) in the conversation context
# Use time.time_ns() for nanosecond precision to avoid collisions
- import time
-
current_time_ns = time.time_ns()
base_timestamp = current_time_ns / 1_000_000_000 # Convert to seconds
@@ -2698,7 +2826,7 @@ class ChatAgent(BaseAgent):
# Check termination condition
if self.stop_event and self.stop_event.is_set():
logger.info(
- f"Termination triggered at iteration " f"{iteration_count}"
+ f"Termination triggered at iteration {iteration_count}"
)
yield self._step_terminate(
num_tokens, tool_call_records, "termination_triggered"
@@ -3067,72 +3195,70 @@ class ChatAgent(BaseAgent):
accumulated_tool_calls: Dict[str, Any],
tool_call_records: List[ToolCallingRecord],
) -> Generator[ChatAgentResponse, None, None]:
- r"""Execute multiple tools synchronously with
- proper content accumulation, using threads+queue for
- non-blocking status streaming."""
-
- def tool_worker(result_queue, tool_call_data):
- try:
- tool_call_record = self._execute_tool_from_stream_data(
- tool_call_data
- )
- result_queue.put(tool_call_record)
- except Exception as e:
- logger.error(f"Error in threaded tool execution: {e}")
- result_queue.put(None)
+ r"""Execute multiple tools synchronously with proper content
+ accumulation, using ThreadPoolExecutor for better timeout handling."""
tool_calls_to_execute = []
for _tool_call_index, tool_call_data in accumulated_tool_calls.items():
if tool_call_data.get('complete', False):
tool_calls_to_execute.append(tool_call_data)
- # Phase 2: Execute tools in threads and yield status while waiting
- for tool_call_data in tool_calls_to_execute:
- function_name = tool_call_data['function']['name']
- try:
- args = json.loads(tool_call_data['function']['arguments'])
- except json.JSONDecodeError:
- args = tool_call_data['function']['arguments']
- result_queue: queue.Queue[Optional[ToolCallingRecord]] = (
- queue.Queue()
- )
- thread = threading.Thread(
- target=tool_worker,
- args=(result_queue, tool_call_data),
- )
- thread.start()
-
- # Log debug info instead of adding to content
- logger.info(
- f"Calling function: {function_name} with arguments: {args}"
- )
-
- # wait for tool thread to finish with optional timeout
- thread.join(self.tool_execution_timeout)
+ if not tool_calls_to_execute:
+ # No tools to execute, return immediately
+ return
+ yield # Make this a generator
+
+ # Execute tools using ThreadPoolExecutor for proper timeout handling
+ # Use max_workers=len() for parallel execution, with min of 1
+ with concurrent.futures.ThreadPoolExecutor(
+ max_workers=max(1, len(tool_calls_to_execute))
+ ) as executor:
+ # Submit all tools first (parallel execution)
+ futures_map = {}
+ for tool_call_data in tool_calls_to_execute:
+ function_name = tool_call_data['function']['name']
+ try:
+ args = json.loads(tool_call_data['function']['arguments'])
+ except json.JSONDecodeError:
+ args = tool_call_data['function']['arguments']
- # If timeout occurred, mark as error and continue
- if thread.is_alive():
- # Log timeout info instead of adding to content
- logger.warning(
- f"Function '{function_name}' timed out after "
- f"{self.tool_execution_timeout} seconds"
+ # Log debug info
+ logger.info(
+ f"Calling function: {function_name} with arguments: {args}"
)
- # Detach thread (it may still finish later). Skip recording.
- continue
-
- # Tool finished, get result
- tool_call_record = result_queue.get()
- if tool_call_record:
- tool_call_records.append(tool_call_record)
- raw_result = tool_call_record.result
- result_str = str(raw_result)
+ # Submit tool execution (non-blocking)
+ future = executor.submit(
+ self._execute_tool_from_stream_data, tool_call_data
+ )
+ futures_map[future] = (function_name, tool_call_data)
+
+ # Wait for all futures to complete (or timeout)
+ for future in concurrent.futures.as_completed(
+ futures_map.keys(),
+ timeout=self.tool_execution_timeout
+ if self.tool_execution_timeout
+ else None,
+ ):
+ function_name, tool_call_data = futures_map[future]
- # Log debug info instead of adding to content
- logger.info(f"Function output: {result_str}")
- else:
- # Error already logged
- continue
+ try:
+ tool_call_record = future.result()
+ if tool_call_record:
+ tool_call_records.append(tool_call_record)
+ logger.info(
+ f"Function output: {tool_call_record.result}"
+ )
+ except concurrent.futures.TimeoutError:
+ logger.warning(
+ f"Function '{function_name}' timed out after "
+ f"{self.tool_execution_timeout} seconds"
+ )
+ future.cancel()
+ except Exception as e:
+ logger.error(
+ f"Error executing tool '{function_name}': {e}"
+ )
# Ensure this function remains a generator (required by type signature)
return
@@ -3177,8 +3303,6 @@ class ChatAgent(BaseAgent):
# Record both messages with precise timestamps to ensure
# correct ordering
- import time
-
current_time_ns = time.time_ns()
base_timestamp = (
current_time_ns / 1_000_000_000
@@ -3207,7 +3331,7 @@ class ChatAgent(BaseAgent):
f"Error executing tool '{function_name}': {e!s}"
)
result = {"error": error_msg}
- logging.warning(error_msg)
+ logger.warning(error_msg)
# Record error response
func_msg = FunctionCallingMessage(
@@ -3302,8 +3426,6 @@ class ChatAgent(BaseAgent):
# Record both messages with precise timestamps to ensure
# correct ordering
- import time
-
current_time_ns = time.time_ns()
base_timestamp = (
current_time_ns / 1_000_000_000
@@ -3332,7 +3454,7 @@ class ChatAgent(BaseAgent):
f"Error executing async tool '{function_name}': {e!s}"
)
result = {"error": error_msg}
- logging.warning(error_msg)
+ logger.warning(error_msg)
# Record error response
func_msg = FunctionCallingMessage(
@@ -3442,7 +3564,7 @@ class ChatAgent(BaseAgent):
# Check termination condition
if self.stop_event and self.stop_event.is_set():
logger.info(
- f"Termination triggered at iteration " f"{iteration_count}"
+ f"Termination triggered at iteration {iteration_count}"
)
yield self._step_terminate(
num_tokens, tool_call_records, "termination_triggered"
@@ -3954,10 +4076,12 @@ class ChatAgent(BaseAgent):
configuration.
"""
# Create a new instance with the same configuration
- # If with_memory is True, set system_message to None
- # If with_memory is False, use the original system message
+ # If with_memory is True, set system_message to None (it will be
+ # copied from memory below, including any workflow context)
+ # If with_memory is False, use the current system message
+ # (which may include appended workflow context)
# To avoid duplicated system memory.
- system_message = None if with_memory else self._original_system_message
+ system_message = None if with_memory else self._system_message
# Clone tools and collect toolkits that need registration
cloned_tools, toolkits_to_register = self._clone_tools()
diff --git a/camel/societies/workforce/single_agent_worker.py b/camel/societies/workforce/single_agent_worker.py
index cc9ef30d5..f79073613 100644
--- a/camel/societies/workforce/single_agent_worker.py
+++ b/camel/societies/workforce/single_agent_worker.py
@@ -15,14 +15,19 @@ from __future__ import annotations
import asyncio
import datetime
+import glob
+import os
+import re
import time
from collections import deque
-from typing import Any, List, Optional
+from pathlib import Path
+from typing import Any, Dict, List, Optional
from colorama import Fore
from camel.agents import ChatAgent
from camel.agents.chat_agent import AsyncStreamingChatAgentResponse
+from camel.logger import get_logger
from camel.societies.workforce.prompts import PROCESS_TASK_PROMPT
from camel.societies.workforce.structured_output_handler import (
StructuredOutputHandler,
@@ -30,6 +35,9 @@ from camel.societies.workforce.structured_output_handler import (
from camel.societies.workforce.utils import TaskResult
from camel.societies.workforce.worker import Worker
from camel.tasks.task import Task, TaskState, is_task_result_insufficient
+from camel.utils.context_utils import ContextUtility, WorkflowSummary
+
+logger = get_logger(__name__)
class AgentPool:
@@ -201,6 +209,16 @@ class SingleAgentWorker(Worker):
support native structured output. When disabled, the workforce
uses the native response_format parameter.
(default: :obj:`True`)
+ context_utility (ContextUtility, optional): Shared context utility
+ instance for workflow management. If provided, all workflow
+ operations will use this shared instance instead of creating
+ a new one. This ensures multiple workers share the same session
+ directory. (default: :obj:`None`)
+ enable_workflow_memory (bool, optional): Whether to enable workflow
+ memory accumulation during task execution. When enabled,
+ conversations from all task executions are accumulated for
+ potential workflow saving. Set to True if you plan to call
+ save_workflow_memories(). (default: :obj:`False`)
"""
def __init__(
@@ -212,6 +230,8 @@ class SingleAgentWorker(Worker):
pool_max_size: int = 10,
auto_scale_pool: bool = True,
use_structured_output_handler: bool = True,
+ context_utility: Optional[ContextUtility] = None,
+ enable_workflow_memory: bool = False,
) -> None:
node_id = worker.agent_id
super().__init__(
@@ -226,6 +246,18 @@ class SingleAgentWorker(Worker):
)
self.worker = worker
self.use_agent_pool = use_agent_pool
+ self.enable_workflow_memory = enable_workflow_memory
+ self._shared_context_utility = context_utility
+ self._context_utility: Optional[ContextUtility] = (
+ None # Will be initialized when needed
+ )
+
+ # accumulator agent for collecting conversations
+ # from all task processing
+ self._conversation_accumulator: Optional[ChatAgent] = None
+
+ # note: context utility is set on the worker agent during save/load
+ # operations to avoid creating session folders during initialization
self.agent_pool: Optional[AgentPool] = None
self._cleanup_task: Optional[asyncio.Task] = None
@@ -268,6 +300,24 @@ class SingleAgentWorker(Worker):
await self.agent_pool.return_agent(agent)
# If not using pool, agent will be garbage collected
+ def _get_context_utility(self) -> ContextUtility:
+ r"""Get context utility with lazy initialization."""
+ if self._context_utility is None:
+ self._context_utility = (
+ self._shared_context_utility
+ or ContextUtility.get_workforce_shared()
+ )
+ return self._context_utility
+
+ def _get_conversation_accumulator(self) -> ChatAgent:
+ r"""Get or create the conversation accumulator agent."""
+ if self._conversation_accumulator is None:
+ # create a clone of the original worker to serve as accumulator
+ self._conversation_accumulator = self.worker.clone(
+ with_memory=False
+ )
+ return self._conversation_accumulator
+
async def _process_task(
self, task: Task, dependencies: List[Task]
) -> TaskState:
@@ -385,10 +435,36 @@ class SingleAgentWorker(Worker):
usage_info.get("total_tokens", 0) if usage_info else 0
)
+ # collect conversation from working agent to
+ # accumulator for workflow memory
+ # Only transfer memory if workflow memory is enabled
+ if self.enable_workflow_memory:
+ accumulator = self._get_conversation_accumulator()
+
+ # transfer all memory records from working agent to accumulator
+ try:
+ # retrieve all context records from the working agent
+ work_records = worker_agent.memory.retrieve()
+
+ # write these records to the accumulator's memory
+ memory_records = [
+ record.memory_record for record in work_records
+ ]
+ accumulator.memory.write_records(memory_records)
+
+ logger.debug(
+ f"Transferred {len(memory_records)} memory records to "
+ f"accumulator"
+ )
+
+ except Exception as e:
+ logger.warning(
+ f"Failed to transfer conversation to accumulator: {e}"
+ )
+
except Exception as e:
- print(
- f"{Fore.RED}Error processing task {task.id}: "
- f"{type(e).__name__}: {e}{Fore.RESET}"
+ logger.error(
+ f"Error processing task {task.id}: {type(e).__name__}: {e}"
)
# Store error information in task result
task.result = f"{type(e).__name__}: {e!s}"
@@ -433,13 +509,13 @@ class SingleAgentWorker(Worker):
task.additional_info["token_usage"] = {"total_tokens": total_tokens}
print(f"======\n{Fore.GREEN}Response from {self}:{Fore.RESET}")
+ logger.info(f"Response from {self}:")
if not self.use_structured_output_handler:
# Handle native structured output parsing
if task_result is None:
- print(
- f"{Fore.RED}Error in worker step execution: Invalid "
- f"task result{Fore.RESET}"
+ logger.error(
+ "Error in worker step execution: Invalid task result"
)
task_result = TaskResult(
content="Failed to generate valid task result.",
@@ -450,6 +526,10 @@ class SingleAgentWorker(Worker):
print(
f"\n{color}{task_result.content}{Fore.RESET}\n======", # type: ignore[union-attr]
)
+ if task_result.failed: # type: ignore[union-attr]
+ logger.error(f"{task_result.content}") # type: ignore[union-attr]
+ else:
+ logger.info(f"{task_result.content}") # type: ignore[union-attr]
task.result = task_result.content # type: ignore[union-attr]
@@ -457,9 +537,9 @@ class SingleAgentWorker(Worker):
return TaskState.FAILED
if is_task_result_insufficient(task):
- print(
- f"{Fore.RED}Task {task.id}: Content validation failed - "
- f"task marked as failed{Fore.RESET}"
+ logger.warning(
+ f"Task {task.id}: Content validation failed - "
+ f"task marked as failed"
)
return TaskState.FAILED
return TaskState.DONE
@@ -489,10 +569,309 @@ class SingleAgentWorker(Worker):
except asyncio.CancelledError:
break
except Exception as e:
- print(f"Error in pool cleanup: {e}")
+ logger.warning(f"Error in pool cleanup: {e}")
def get_pool_stats(self) -> Optional[dict]:
r"""Get agent pool statistics if pool is enabled."""
if self.use_agent_pool and self.agent_pool:
return self.agent_pool.get_stats()
return None
+
+ def save_workflow_memories(self) -> Dict[str, Any]:
+ r"""Save the worker's current workflow memories using agent
+ summarization.
+
+ This method generates a workflow summary from the worker agent's
+ conversation history and saves it to a markdown file. The filename
+ is based on the worker's description for easy loading later.
+
+ Returns:
+ Dict[str, Any]: Result dictionary with keys:
+ - status (str): "success" or "error"
+ - summary (str): Generated workflow summary
+ - file_path (str): Path to saved file
+ - worker_description (str): Worker description used
+ """
+ try:
+ # validate requirements
+ validation_error = self._validate_workflow_save_requirements()
+ if validation_error:
+ return validation_error
+
+ # setup context utility and agent
+ context_util = self._get_context_utility()
+ 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
+ )
+
+ # generate and save workflow summary
+ result = agent_to_summarize.summarize(
+ filename=filename,
+ summary_prompt=structured_prompt,
+ response_format=WorkflowSummary,
+ )
+
+ # add worker metadata and cleanup
+ result["worker_description"] = self.description
+ if self._conversation_accumulator is not None:
+ logger.info(
+ "Cleaning up conversation accumulator after workflow "
+ "summarization"
+ )
+ self._conversation_accumulator = None
+
+ return result
+
+ except Exception as e:
+ return {
+ "status": "error",
+ "summary": "",
+ "file_path": None,
+ "worker_description": self.description,
+ "message": f"Failed to save workflow memories: {e!s}",
+ }
+
+ def load_workflow_memories(
+ self,
+ pattern: Optional[str] = None,
+ max_files_to_load: int = 3,
+ session_id: Optional[str] = None,
+ ) -> bool:
+ r"""Load workflow memories matching worker description
+ from saved files.
+
+ This method searches for workflow memory files that match the worker's
+ description and loads them into the agent's memory using
+ ContextUtility.
+
+ Args:
+ pattern (Optional[str]): Custom search pattern for workflow
+ memory files.
+ If None, uses worker description to generate pattern.
+ max_files_to_load (int): Maximum number of workflow files to load.
+ (default: :obj:`3`)
+ session_id (Optional[str]): Specific workforce session ID to load
+ from. If None, searches across all sessions.
+ (default: :obj:`None`)
+
+ Returns:
+ bool: True if workflow memories were successfully loaded, False
+ otherwise.
+ """
+ try:
+ # reset system message to original state before loading
+ # this prevents duplicate workflow context on multiple calls
+ if isinstance(self.worker, ChatAgent):
+ self.worker.reset_to_original_system_message()
+
+ # Find workflow memory files matching the pattern
+ workflow_files = self._find_workflow_files(pattern, session_id)
+ if not workflow_files:
+ return False
+
+ # Load the workflow memory files
+ loaded_count = self._load_workflow_files(
+ workflow_files, max_files_to_load
+ )
+
+ # Report results
+ logger.info(
+ f"Successfully loaded {loaded_count} workflow file(s) for "
+ f"{self.description}"
+ )
+ return loaded_count > 0
+
+ except Exception as e:
+ logger.warning(
+ f"Error loading workflow memories for {self.description}: "
+ f"{e!s}"
+ )
+ return False
+
+ def _find_workflow_files(
+ self, pattern: Optional[str], session_id: Optional[str] = None
+ ) -> List[str]:
+ r"""Find and return sorted workflow files matching the pattern.
+
+ Args:
+ pattern (Optional[str]): Custom search pattern for workflow files.
+ If None, uses worker description to generate pattern.
+ session_id (Optional[str]): Specific session ID to search in.
+ If None, searches across all sessions.
+
+ Returns:
+ List[str]: Sorted list of workflow file paths (empty if
+ validation fails).
+ """
+ # Ensure we have a ChatAgent worker
+ if not isinstance(self.worker, ChatAgent):
+ logger.warning(
+ f"Cannot load workflow: {self.description} worker is not "
+ "a ChatAgent"
+ )
+ return []
+
+ # generate filename-safe search pattern from worker description
+ if 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"
+
+ # Get the base workforce_workflows directory
+ camel_workdir = os.environ.get("CAMEL_WORKDIR")
+ if camel_workdir:
+ base_dir = os.path.join(camel_workdir, "workforce_workflows")
+ else:
+ base_dir = "workforce_workflows"
+
+ # search for workflow files in specified or all session directories
+ if session_id:
+ search_path = str(Path(base_dir) / session_id / pattern)
+ else:
+ # search across all session directories using wildcard pattern
+ search_path = str(Path(base_dir) / "*" / pattern)
+ workflow_files = glob.glob(search_path)
+
+ if not workflow_files:
+ logger.info(f"No workflow files found for pattern: {pattern}")
+ return []
+
+ # prioritize most recent sessions by session timestamp in
+ # directory name
+ def extract_session_timestamp(filepath: str) -> str:
+ match = re.search(r'session_(\d{8}_\d{6}_\d{6})', filepath)
+ return match.group(1) if match else ""
+
+ workflow_files.sort(key=extract_session_timestamp, reverse=True)
+ return workflow_files
+
+ def _load_workflow_files(
+ self, workflow_files: List[str], max_files_to_load: int
+ ) -> int:
+ r"""Load workflow files and return count of successful loads.
+
+ Args:
+ workflow_files (List[str]): List of workflow file paths to load.
+
+ Returns:
+ int: Number of successfully loaded workflow files.
+ """
+ loaded_count = 0
+ # limit loading to prevent context overflow
+ for file_path in workflow_files[:max_files_to_load]:
+ try:
+ # extract file and session info from full path
+ filename = os.path.basename(file_path).replace('.md', '')
+ session_dir = os.path.dirname(file_path)
+ session_id = os.path.basename(session_dir)
+
+ # create context utility for the specific session
+ # where file exists
+ temp_utility = ContextUtility.get_workforce_shared(session_id)
+
+ status = temp_utility.load_markdown_context_to_memory(
+ self.worker, filename
+ )
+
+ if "Context appended" in status:
+ loaded_count += 1
+ logger.info(f"Loaded workflow: {filename}")
+ else:
+ logger.warning(
+ f"Failed to load workflow {filename}: {status}"
+ )
+
+ except Exception as e:
+ logger.warning(
+ f"Failed to load workflow file {file_path}: {e!s}"
+ )
+ continue
+
+ return loaded_count
+
+ def _validate_workflow_save_requirements(self) -> Optional[Dict[str, Any]]:
+ r"""Validate requirements for workflow saving.
+
+ Returns:
+ Optional[Dict[str, Any]]: Error result dict if validation fails,
+ None if validation passes.
+ """
+ if not isinstance(self.worker, ChatAgent):
+ return {
+ "status": "error",
+ "summary": "",
+ "file_path": None,
+ "worker_description": self.description,
+ "message": (
+ "Worker must be a ChatAgent instance to save workflow "
+ "memories"
+ ),
+ }
+ return None
+
+ def _generate_workflow_filename(self) -> str:
+ r"""Generate a filename for the workflow based on worker description.
+
+ Returns:
+ str: Sanitized filename without timestamp (session already has
+ timestamp).
+ """
+ clean_desc = self.description.lower().replace(" ", "_")
+ clean_desc = re.sub(r'[^a-z0-9_]', '', clean_desc)
+ return f"{clean_desc}_workflow"
+
+ def _prepare_workflow_prompt(self) -> str:
+ r"""Prepare the structured prompt for workflow summarization.
+
+ Returns:
+ str: Structured prompt for workflow summary.
+ """
+ workflow_prompt = WorkflowSummary.get_instruction_prompt()
+ return StructuredOutputHandler.generate_structured_prompt(
+ base_prompt=workflow_prompt, schema=WorkflowSummary
+ )
+
+ def _select_agent_for_summarization(
+ self, context_util: ContextUtility
+ ) -> ChatAgent:
+ r"""Select the best agent for workflow summarization.
+
+ Args:
+ context_util: Context utility to set on selected agent.
+
+ Returns:
+ ChatAgent: Agent to use for summarization.
+ """
+ agent_to_summarize = self.worker
+
+ if self._conversation_accumulator is not None:
+ accumulator_messages, _ = (
+ self._conversation_accumulator.memory.get_context()
+ )
+ if accumulator_messages:
+ self._conversation_accumulator.set_context_utility(
+ context_util
+ )
+ agent_to_summarize = self._conversation_accumulator
+ logger.info(
+ f"Using conversation accumulator with "
+ f"{len(accumulator_messages)} messages for workflow "
+ f"summary"
+ )
+ else:
+ logger.info(
+ "Using original worker for workflow summary (no "
+ "accumulated conversations)"
+ )
+ else:
+ logger.info(
+ "Using original worker for workflow summary (no accumulator)"
+ )
+
+ return agent_to_summarize
diff --git a/camel/societies/workforce/workforce.py b/camel/societies/workforce/workforce.py
index f0b9ffc5c..ba9a872e5 100644
--- a/camel/societies/workforce/workforce.py
+++ b/camel/societies/workforce/workforce.py
@@ -22,6 +22,7 @@ import uuid
from collections import deque
from enum import Enum
from typing import (
+ TYPE_CHECKING,
Any,
Callable,
Coroutine,
@@ -36,6 +37,9 @@ from typing import (
cast,
)
+if TYPE_CHECKING:
+ from camel.utils.context_utils import ContextUtility
+
from colorama import Fore
from camel.agents import ChatAgent
@@ -50,7 +54,9 @@ from camel.societies.workforce.prompts import (
TASK_DECOMPOSE_PROMPT,
)
from camel.societies.workforce.role_playing_worker import RolePlayingWorker
-from camel.societies.workforce.single_agent_worker import SingleAgentWorker
+from camel.societies.workforce.single_agent_worker import (
+ SingleAgentWorker,
+)
from camel.societies.workforce.structured_output_handler import (
StructuredOutputHandler,
)
@@ -324,8 +330,7 @@ class Workforce(BaseNode):
if coordinator_agent.system_message is not None:
user_sys_msg_content = coordinator_agent.system_message.content
combined_content = (
- f"{user_sys_msg_content}\n\n"
- f"{coord_agent_sys_msg.content}"
+ f"{user_sys_msg_content}\n\n{coord_agent_sys_msg.content}"
)
combined_sys_msg = BaseMessage.make_assistant_message(
role_name=coordinator_agent.system_message.role_name,
@@ -387,8 +392,7 @@ class Workforce(BaseNode):
if task_agent.system_message is not None:
user_task_sys_msg_content = task_agent.system_message.content
combined_task_content = (
- f"{user_task_sys_msg_content}\n\n"
- f"{task_sys_msg.content}"
+ f"{user_task_sys_msg_content}\n\n{task_sys_msg.content}"
)
combined_task_sys_msg = BaseMessage.make_assistant_message(
role_name=task_agent.system_message.role_name,
@@ -450,6 +454,30 @@ class Workforce(BaseNode):
"better context continuity during task handoffs."
)
+ # Shared context utility for workflow management (created lazily)
+ self._shared_context_utility: Optional["ContextUtility"] = None
+
+ # ------------------------------------------------------------------
+ # Helper for propagating pause control to externally supplied agents
+ # ------------------------------------------------------------------
+
+ def _get_or_create_shared_context_utility(self) -> "ContextUtility":
+ r"""Get or create the shared context utility for workflow management.
+
+ This method creates the context utility only when needed, avoiding
+ unnecessary session folder creation during initialization.
+
+ 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()
+ )
+ return self._shared_context_utility
+
def _validate_agent_compatibility(
self, agent: ChatAgent, agent_context: str = "agent"
) -> None:
@@ -1660,6 +1688,7 @@ class Workforce(BaseNode):
description: str,
worker: ChatAgent,
pool_max_size: int = DEFAULT_WORKER_POOL_SIZE,
+ enable_workflow_memory: bool = False,
) -> Workforce:
r"""Add a worker node to the workforce that uses a single agent.
Can be called when workforce is paused to dynamically add workers.
@@ -1669,6 +1698,9 @@ class Workforce(BaseNode):
worker (ChatAgent): The agent to be added.
pool_max_size (int): Maximum size of the agent pool.
(default: :obj:`10`)
+ enable_workflow_memory (bool): Whether to enable workflow memory
+ accumulation. Set to True if you plan to call
+ save_workflow_memories(). (default: :obj:`False`)
Returns:
Workforce: The workforce node itself.
@@ -1695,6 +1727,8 @@ class Workforce(BaseNode):
worker=worker,
pool_max_size=pool_max_size,
use_structured_output_handler=self.use_structured_output_handler,
+ context_utility=None, # Will be set during save/load operations
+ enable_workflow_memory=enable_workflow_memory,
)
self._children.append(worker_node)
@@ -1871,6 +1905,237 @@ class Workforce(BaseNode):
else:
self.metrics_logger = WorkforceLogger(workforce_id=self.node_id)
+ def save_workflow_memories(self) -> Dict[str, str]:
+ r"""Save workflow memories for all SingleAgentWorker instances in the
+ workforce.
+
+ This method iterates through all child workers and triggers workflow
+ saving for SingleAgentWorker instances using their
+ save_workflow_memories()
+ method.
+ Other worker types are skipped.
+
+ Returns:
+ Dict[str, str]: Dictionary mapping worker node IDs to save results.
+ Values are either file paths (success) or error messages
+ (failure).
+
+ Example:
+ >>> workforce = Workforce("My Team")
+ >>> # ... add workers and process tasks ...
+ >>> results = workforce.save_workflows()
+ >>> print(results)
+ {'worker_123': '/path/to/data_analyst_workflow_20250122.md',
+ 'worker_456': 'error: No conversation context available'}
+ """
+ results = {}
+
+ # Get or create shared context utility for this save operation
+ shared_context_utility = self._get_or_create_shared_context_utility()
+
+ for child in self._children:
+ if isinstance(child, SingleAgentWorker):
+ try:
+ # Set the shared context utility for this operation
+ child._shared_context_utility = shared_context_utility
+ child.worker.set_context_utility(shared_context_utility)
+
+ result = child.save_workflow_memories()
+ if result.get("status") == "success":
+ results[child.node_id] = result.get(
+ "file_path", "unknown_path"
+ )
+ else:
+ # Error: check if there's a separate message field,
+ # otherwise use the status itself
+ error_msg = result.get(
+ "message", result.get("status", "Unknown error")
+ )
+ results[child.node_id] = f"error: {error_msg}"
+
+ except Exception as e:
+ results[child.node_id] = f"error: {e!s}"
+ else:
+ # Skip non-SingleAgentWorker types
+ results[child.node_id] = (
+ f"skipped: {type(child).__name__} not supported"
+ )
+
+ logger.info(f"Workflow save completed for {len(results)} workers")
+ return results
+
+ def load_workflow_memories(
+ self,
+ max_files_to_load: int = 3,
+ session_id: Optional[str] = None,
+ ) -> Dict[str, bool]:
+ r"""Load workflow memories for all SingleAgentWorker instances in the
+ workforce.
+
+ This method iterates through all child workers and loads relevant
+ workflow files for SingleAgentWorker instances using their
+ load_workflow_memories()
+ method. Workers match files based on their description names.
+
+ Args:
+ max_files_to_load (int): Maximum number of workflow files to load
+ per worker. (default: :obj:`3`)
+ session_id (Optional[str]): Specific workforce session ID to load
+ from. If None, searches across all sessions.
+ (default: :obj:`None`)
+
+ Returns:
+ Dict[str, bool]: Dictionary mapping worker node IDs to load
+ success status.
+ True indicates successful loading, False indicates failure.
+
+ Example:
+ >>> workforce = Workforce("My Team")
+ >>> workforce.add_single_agent_worker(
+ ... "data_analyst", analyst_agent
+ ... )
+ >>> success_status = workforce.load_workflows()
+ >>> print(success_status)
+ {'worker_123': True} # Successfully loaded workflows for
+ # data_analyst
+ """
+ results = {}
+
+ # For loading, we don't create a new session - instead we search
+ # existing ones
+ # Each worker will search independently across all existing sessions
+
+ # First, load workflows for SingleAgentWorker instances
+ for child in self._children:
+ if isinstance(child, SingleAgentWorker):
+ try:
+ # For loading, don't set shared context utility
+ # Let each worker search across existing sessions
+ success = child.load_workflow_memories(
+ max_files_to_load=max_files_to_load,
+ session_id=session_id,
+ )
+ results[child.node_id] = success
+
+ except Exception as e:
+ logger.error(
+ f"Failed to load workflow for {child.node_id}: {e!s}"
+ )
+ results[child.node_id] = False
+ else:
+ # Skip non-SingleAgentWorker types
+ results[child.node_id] = False
+
+ # Load aggregated workflow summaries for coordinator and task agents
+ self._load_management_agent_workflows(max_files_to_load, session_id)
+
+ logger.info(f"Workflow load completed for {len(results)} workers")
+ return results
+
+ def _load_management_agent_workflows(
+ self, max_files_to_load: int, session_id: Optional[str] = None
+ ) -> None:
+ r"""Load workflow summaries for coordinator and task planning agents.
+
+ This method loads aggregated workflow summaries to help:
+ - Coordinator agent: understand task assignment patterns and worker
+ capabilities
+ - Task agent: understand task decomposition patterns and
+ successful strategies
+
+ Args:
+ max_files_to_load (int): Maximum number of workflow files to load.
+ session_id (Optional[str]): Specific session ID to load from.
+ If None, searches across all sessions.
+ """
+ try:
+ import glob
+ import os
+ from pathlib import Path
+
+ from camel.utils.context_utils import ContextUtility
+
+ # For loading management workflows, search across all sessions
+ camel_workdir = os.environ.get("CAMEL_WORKDIR")
+ if camel_workdir:
+ base_dir = os.path.join(camel_workdir, "workforce_workflows")
+ else:
+ base_dir = "workforce_workflows"
+
+ # Search for workflow files in specified or all session directories
+ if session_id:
+ search_path = str(
+ Path(base_dir) / session_id / "*_workflow*.md"
+ )
+ else:
+ search_path = str(Path(base_dir) / "*" / "*_workflow*.md")
+ workflow_files = glob.glob(search_path)
+
+ if not workflow_files:
+ logger.info(
+ "No workflow files found for management agent context"
+ )
+ return
+
+ # Sort by modification time (most recent first)
+ workflow_files.sort(
+ key=lambda x: os.path.getmtime(x), reverse=True
+ )
+
+ # Load workflows for coordinator agent (up to 5 most recent)
+ coordinator_loaded = 0
+ for file_path in workflow_files[:max_files_to_load]:
+ try:
+ filename = os.path.basename(file_path).replace('.md', '')
+ session_dir = os.path.dirname(file_path)
+ session_id = os.path.basename(session_dir)
+
+ # Use shared context utility with specific session
+ temp_utility = ContextUtility.get_workforce_shared(
+ session_id
+ )
+
+ status = temp_utility.load_markdown_context_to_memory(
+ self.coordinator_agent, filename
+ )
+ if "Context appended" in status:
+ coordinator_loaded += 1
+ except Exception as e:
+ logger.warning(
+ f"Failed to load coordinator workflow {file_path}: {e}"
+ )
+
+ # Load workflows for task agent (up to 3 most recent)
+ task_agent_loaded = 0
+ for file_path in workflow_files[:max_files_to_load]:
+ try:
+ filename = os.path.basename(file_path).replace('.md', '')
+ session_dir = os.path.dirname(file_path)
+ session_id = os.path.basename(session_dir)
+
+ # Use shared context utility with specific session
+ temp_utility = ContextUtility.get_workforce_shared(
+ session_id
+ )
+
+ status = temp_utility.load_markdown_context_to_memory(
+ self.task_agent, filename
+ )
+ if "Context appended" in status:
+ task_agent_loaded += 1
+ except Exception as e:
+ logger.warning(
+ f"Failed to load task agent workflow {file_path}: {e}"
+ )
+
+ logger.info(
+ f"Loaded {coordinator_loaded} workflows for coordinator, "
+ f"{task_agent_loaded} workflows for task agent"
+ )
+
+ except Exception as e:
+ logger.error(f"Error loading management agent workflows: {e}")
+
@check_if_running(False)
def set_channel(self, channel: TaskChannel) -> None:
r"""Set the channel for the node and all the child nodes under it."""
@@ -2380,8 +2645,7 @@ class Workforce(BaseNode):
"worker creation"
)
new_node_conf = WorkerConf(
- description=f"Fallback worker for task: "
- f"{task.content}",
+ description=f"Fallback worker for task: {task.content}",
role="General Assistant",
sys_msg="You are a general assistant that can help "
"with various tasks.",
@@ -2391,7 +2655,7 @@ class Workforce(BaseNode):
response.msg.content,
schema=WorkerConf,
fallback_values={
- "description": f"Worker for task: " f"{task.content}",
+ "description": f"Worker for task: {task.content}",
"role": "Task Specialist",
"sys_msg": f"You are a specialist for: {task.content}",
},
@@ -2419,8 +2683,7 @@ class Workforce(BaseNode):
)
# Create a fallback worker configuration
new_node_conf = WorkerConf(
- description=f"Fallback worker for "
- f"task: {task.content}",
+ description=f"Fallback worker for task: {task.content}",
role="General Assistant",
sys_msg="You are a general assistant that can help "
"with various tasks.",
diff --git a/camel/toolkits/context_summarizer_toolkit.py b/camel/toolkits/context_summarizer_toolkit.py
index ac27d7561..9b52f1c11 100644
--- a/camel/toolkits/context_summarizer_toolkit.py
+++ b/camel/toolkits/context_summarizer_toolkit.py
@@ -191,7 +191,7 @@ class ContextSummarizerToolkit(BaseToolkit):
summary (str): The summary text to save.
Returns:
- str: Success message or error message.
+ str: "success" or error message starting with "Error:".
"""
try:
# prepare metadata for unified markdown saving
@@ -221,7 +221,7 @@ class ContextSummarizerToolkit(BaseToolkit):
to save.
Returns:
- str: Success message or error message.
+ str: "success" or error message starting with "Error:".
"""
try:
# prepare metadata for markdown saving
diff --git a/camel/types/enums.py b/camel/types/enums.py
index e407d1d00..89394c4ff 100644
--- a/camel/types/enums.py
+++ b/camel/types/enums.py
@@ -30,7 +30,7 @@ class RoleType(Enum):
class ModelType(UnifiedModelType, Enum):
- DEFAULT = os.getenv("DEFAULT_MODEL_TYPE", "gpt-5-mini")
+ DEFAULT = os.getenv("DEFAULT_MODEL_TYPE", "gpt-4.1-mini-2025-04-14")
GPT_3_5_TURBO = "gpt-3.5-turbo"
GPT_4 = "gpt-4"
diff --git a/camel/utils/context_utils.py b/camel/utils/context_utils.py
index 34e6fa0c4..a3b70c6b2 100644
--- a/camel/utils/context_utils.py
+++ b/camel/utils/context_utils.py
@@ -15,7 +15,9 @@
import os
from datetime import datetime
from pathlib import Path
-from typing import TYPE_CHECKING, Any, Dict, List, Optional
+from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional
+
+from pydantic import BaseModel, Field
from camel.logger import get_logger
@@ -26,6 +28,93 @@ if TYPE_CHECKING:
logger = get_logger(__name__)
+class WorkflowSummary(BaseModel):
+ r"""Pydantic model for structured workflow summaries.
+
+ This model defines the schema for workflow memories that can be reused
+ by future agents for similar tasks.
+ """
+
+ task_title: str = Field(
+ description="A short, generic title of the main task (≤ 10 words). "
+ "Avoid product- or case-specific names. "
+ "Example: 'List GitHub stargazers', "
+ "'Remind weekly meetings on Slack', "
+ "'Find best leads and turn them into a table on Notion'."
+ )
+ task_description: str = Field(
+ description="One-paragraph summary of what the user asked for "
+ "(≤ 80 words). "
+ "No implementation details; just the outcome the user wants. "
+ "Example: Find academic professors who might be interested in the "
+ "upcoming research paper on Graph-based Agentic Memory, extract "
+ "their email addresses, affiliations, and research interests, "
+ "and create a table on Notion with this information."
+ )
+ tools: List[str] = Field(
+ description="Bullet list of tool calls or functions calls used. "
+ "For each: name → what it did → why it was useful (one line each). "
+ "This field is explicitly for tool call messages or the MCP "
+ "servers used."
+ "Example: - ArxivToolkit: get authors from a paper title, "
+ "it helped find academic professors who authored a particular "
+ "paper, and then get their email addresses, affiliations, and "
+ "research interests.",
+ default_factory=list,
+ )
+ steps: List[str] = Field(
+ description="Numbered, ordered actions the agent took to complete "
+ "the task. Each step starts with a verb and is generic "
+ "enough to be repeatable. "
+ "Example: 1. Find the upcoming meetings on Google Calendar "
+ " today. 2. Send participants a reminder on Slack...",
+ default_factory=list,
+ )
+ failure_and_recovery_strategies: List[str] = Field(
+ description="[Optional] Bullet each incident with symptom, "
+ " cause (if known), fix/workaround, verification of "
+ "recovery. Leave empty if no failures. "
+ "failures. Example: Running the script for consumer data "
+ "analysis failed since Pandas package was not installed. "
+ "Fixed by running 'pip install pandas'.",
+ default_factory=list,
+ )
+ notes_and_observations: str = Field(
+ description="[Optional] Anything not covered in previous fields "
+ "that is critical to know for future executions of the task. "
+ "Leave empty if no notes. Do not repeat any information, or "
+ "mention trivial details. Only what is essential. "
+ "Example: The user likes to be in the "
+ "loop of the task execution, make sure to check with them the "
+ "plan before starting to work, and ask them for approval "
+ "mid-task by using the HumanToolkit.",
+ default="",
+ )
+
+ @classmethod
+ def get_instruction_prompt(cls) -> str:
+ r"""Get the instruction prompt for this model.
+
+ Returns:
+ str: The instruction prompt that guides agents to produce
+ structured output matching this schema.
+ """
+ return (
+ 'You are writing a compact "workflow memory" so future agents '
+ 'can reuse what you just did for future tasks. '
+ 'Be concise, precise, and action-oriented. Analyze the '
+ 'conversation and extract the key workflow information '
+ 'following the provided schema structure. If a field has no '
+ 'content, still include it per the schema, but keep it empty. '
+ 'The length of your workflow must be proportional to the '
+ 'complexity of the task. Example: If the task is simply '
+ 'about a simple math problem, the workflow must be short, '
+ 'e.g. <60 words. By contrast, if the task is complex and '
+ 'multi-step, such as finding particular job applications based '
+ 'on user CV, the workflow must be longer, e.g. about 120 words.'
+ )
+
+
class ContextUtility:
r"""Utility class for context management and file operations.
@@ -39,23 +128,46 @@ class ContextUtility:
- Text-based search through files
- File metadata handling
- Agent memory record retrieval
+ - Shared session management for workforce workflows
"""
- def __init__(self, working_directory: Optional[str] = None):
+ # Class variables for shared session management
+ _shared_sessions: ClassVar[Dict[str, 'ContextUtility']] = {}
+ _default_workforce_session: ClassVar[Optional['ContextUtility']] = None
+
+ def __init__(
+ self,
+ working_directory: Optional[str] = None,
+ session_id: Optional[str] = None,
+ create_folder: bool = True,
+ ):
r"""Initialize the ContextUtility.
Args:
working_directory (str, optional): The directory path where files
will be stored. If not provided, a default directory will be
used.
+ session_id (str, optional): The session ID to use. If provided,
+ this instance will use the same session folder as other
+ instances with the same session_id. If not provided, a new
+ session ID will be generated.
+ create_folder (bool): Whether to create the session folder
+ immediately. If False, the folder will be created only when
+ needed (e.g., when saving files). Default is True for
+ backward compatibility.
"""
self.working_directory_param = working_directory
- self._setup_storage(working_directory)
+ self._setup_storage(working_directory, session_id, create_folder)
- def _setup_storage(self, working_directory: Optional[str]) -> None:
- r"""Initialize session-specific storage paths and create directory
- structure for context file management."""
- self.session_id = self._generate_session_id()
+ def _setup_storage(
+ self,
+ working_directory: Optional[str],
+ session_id: Optional[str] = None,
+ create_folder: bool = True,
+ ) -> None:
+ r"""Initialize session-specific storage paths and optionally create
+ directory structure for context file management."""
+ self.session_id = session_id or self._generate_session_id()
if working_directory:
self.working_directory = Path(working_directory).resolve()
@@ -68,7 +180,10 @@ class ContextUtility:
# Create session-specific directory
self.working_directory = self.working_directory / self.session_id
- self.working_directory.mkdir(parents=True, exist_ok=True)
+
+ # Only create directory if requested
+ if create_folder:
+ self.working_directory.mkdir(parents=True, exist_ok=True)
def _generate_session_id(self) -> str:
r"""Create timestamp-based unique identifier for isolating
@@ -78,6 +193,10 @@ class ContextUtility:
# ========= GENERIC FILE MANAGEMENT METHODS =========
+ def _ensure_directory_exists(self) -> None:
+ r"""Ensure the working directory exists, creating it if necessary."""
+ self.working_directory.mkdir(parents=True, exist_ok=True)
+
def _create_or_update_note(self, note_name: str, content: str) -> str:
r"""Write content to markdown file, creating new file or
overwriting existing one with UTF-8 encoding.
@@ -90,6 +209,8 @@ class ContextUtility:
str: Success message.
"""
try:
+ # Ensure directory exists before writing
+ self._ensure_directory_exists()
file_path = self.working_directory / f"{note_name}.md"
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
@@ -114,7 +235,8 @@ class ContextUtility:
metadata (Dict, optional): Additional metadata to include.
Returns:
- str: Success message or error message.
+ str: "success" on success, error message starting with "Error:"
+ on failure.
"""
try:
markdown_content = ""
@@ -135,14 +257,99 @@ class ContextUtility:
self._create_or_update_note(filename, markdown_content)
logger.info(
- f"Markdown file saved to "
+ f"Markdown file '{filename}.md' saved successfully to "
f"{self.working_directory / f'{filename}.md'}"
)
- return f"Markdown file '{filename}.md' saved successfully"
+ return "success"
except Exception as e:
logger.error(f"Error saving markdown file {filename}: {e}")
- return f"Error saving markdown file: {e}"
+ return f"Error: {e}"
+
+ def structured_output_to_markdown(
+ self,
+ structured_data: BaseModel,
+ metadata: Optional[Dict[str, Any]] = None,
+ title: Optional[str] = None,
+ field_mappings: Optional[Dict[str, str]] = None,
+ ) -> str:
+ r"""Convert any Pydantic BaseModel instance to markdown format.
+
+ Args:
+ structured_data: Any Pydantic BaseModel instance
+ metadata: Optional metadata to include in the markdown
+ title: Optional custom title, defaults to model class name
+ field_mappings: Optional mapping of field names to custom
+ section titles
+
+ Returns:
+ str: Markdown formatted content
+ """
+ markdown_content = []
+
+ # Add metadata if provided
+ if metadata:
+ markdown_content.append("## Metadata\n")
+ for key, value in metadata.items():
+ markdown_content.append(f"- {key}: {value}")
+ markdown_content.append("")
+
+ # Add title
+ if title:
+ markdown_content.extend([f"## {title}", ""])
+ else:
+ model_name = structured_data.__class__.__name__
+ markdown_content.extend([f"## {model_name}", ""])
+
+ # Get model fields and values
+ model_dict = structured_data.model_dump()
+
+ for field_name, field_value in model_dict.items():
+ # Use custom mapping or convert field name to title case
+ if field_mappings and field_name in field_mappings:
+ section_title = field_mappings[field_name]
+ else:
+ # Convert snake_case to Title Case
+ section_title = field_name.replace('_', ' ').title()
+
+ markdown_content.append(f"### {section_title}")
+
+ # Handle different data types
+ if isinstance(field_value, list):
+ if field_value:
+ for i, item in enumerate(field_value):
+ if isinstance(item, str):
+ # Check if it looks like a numbered item already
+ if item.strip() and not item.strip()[0].isdigit():
+ # For steps or numbered lists, add numbers
+ if 'step' in field_name.lower():
+ markdown_content.append(f"{i + 1}. {item}")
+ else:
+ markdown_content.append(f"- {item}")
+ else:
+ markdown_content.append(f"- {item}")
+ else:
+ markdown_content.append(f"- {item!s}")
+ else:
+ markdown_content.append(
+ f"(No {section_title.lower()} recorded)"
+ )
+ elif isinstance(field_value, str):
+ if field_value.strip():
+ markdown_content.append(field_value)
+ else:
+ markdown_content.append(
+ f"(No {section_title.lower()} provided)"
+ )
+ elif isinstance(field_value, dict):
+ for k, v in field_value.items():
+ markdown_content.append(f"- **{k}**: {v}")
+ else:
+ markdown_content.append(str(field_value))
+
+ markdown_content.append("")
+
+ return "\n".join(markdown_content)
def load_markdown_file(self, filename: str) -> str:
r"""Generic method to load any markdown file.
@@ -394,15 +601,40 @@ class ContextUtility:
"""
return self.session_id
+ def set_session_id(self, session_id: str) -> None:
+ r"""Set a new session ID and update the working directory accordingly.
+
+ This allows sharing session directories between multiple ContextUtility
+ instances by using the same session_id.
+
+ Args:
+ session_id (str): The session ID to use.
+ """
+ self.session_id = session_id
+
+ # Update working directory with new session_id
+ if self.working_directory_param:
+ base_dir = Path(self.working_directory_param).resolve()
+ else:
+ camel_workdir = os.environ.get("CAMEL_WORKDIR")
+ if camel_workdir:
+ base_dir = Path(camel_workdir) / "context_files"
+ else:
+ base_dir = Path("context_files")
+
+ self.working_directory = base_dir / self.session_id
+ self.working_directory.mkdir(parents=True, exist_ok=True)
+
def load_markdown_context_to_memory(
- self, agent: "ChatAgent", filename: str
+ self, agent: "ChatAgent", filename: str, include_metadata: bool = False
) -> str:
r"""Load context from a markdown file and append it to agent memory.
- Preserves existing conversation history without wiping it.
Args:
agent (ChatAgent): The agent to append context to.
filename (str): Name of the markdown file (without .md extension).
+ include_metadata (bool): Whether to include metadata section in the
+ loaded content. Defaults to False.
Returns:
str: Status message indicating success or failure with details.
@@ -413,24 +645,52 @@ class ContextUtility:
if not content.strip():
return f"Context file not found or empty: {filename}"
- from camel.messages import BaseMessage
+ # Filter out metadata section if not requested
+ if not include_metadata:
+ content = self._filter_metadata_from_content(content)
+
from camel.types import OpenAIBackendRole
prefix_prompt = (
"The following is the context from a previous "
- "session or workflow. This information might help you "
+ "session or workflow which might be useful for "
+ "to the current task. This information might help you "
"understand the background, choose which tools to use, "
"and plan your next steps."
)
- # TODO: change to system message once multi-system-message
- # is supported
- context_message = BaseMessage.make_assistant_message(
- role_name="Assistant",
- content=f"{prefix_prompt}\n\n{content}",
+ # Append workflow content to the agent's system message
+ # This ensures the context persists when agents are cloned
+ workflow_content = (
+ f"\n\n--- Workflow Memory ---\n{prefix_prompt}\n\n{content}"
)
- agent.update_memory(context_message, OpenAIBackendRole.USER)
+ # Update the original system message to include workflow
+ if agent._original_system_message is None:
+ logger.error(
+ f"Agent {agent.agent_id} has no system message. "
+ "Cannot append workflow memory to system message."
+ )
+ return (
+ "Error: Agent has no system message to append workflow to"
+ )
+
+ # Update the current system message
+ current_system_message = agent._system_message
+ if current_system_message is not None:
+ new_sys_content = (
+ current_system_message.content + workflow_content
+ )
+ agent._system_message = (
+ current_system_message.create_new_instance(new_sys_content)
+ )
+
+ # Replace the system message in memory
+ # Clear and re-initialize with updated system message
+ agent.memory.clear()
+ agent.update_memory(
+ agent._system_message, OpenAIBackendRole.SYSTEM
+ )
char_count = len(content)
log_msg = (
@@ -445,3 +705,100 @@ class ContextUtility:
error_msg = f"Failed to load markdown context to memory: {e}"
logger.error(error_msg)
return error_msg
+
+ def _filter_metadata_from_content(self, content: str) -> str:
+ r"""Filter out metadata section from markdown content.
+
+ Args:
+ content (str): The full markdown content including metadata.
+
+ Returns:
+ str: Content with metadata section removed.
+ """
+ lines = content.split('\n')
+ filtered_lines = []
+ skip_metadata = False
+
+ for line in lines:
+ # Check if we're starting a metadata section
+ if line.strip() == "## Metadata":
+ skip_metadata = True
+ continue
+
+ # Check if we're starting a new section after metadata
+ if (
+ skip_metadata
+ and line.startswith("## ")
+ and "Metadata" not in line
+ ):
+ skip_metadata = False
+
+ # Add line if we're not in metadata section
+ if not skip_metadata:
+ filtered_lines.append(line)
+
+ # Clean up any extra whitespace at the beginning
+ result = '\n'.join(filtered_lines).strip()
+ return result
+
+ # ========= SHARED SESSION MANAGEMENT METHODS =========
+
+ @classmethod
+ def get_workforce_shared(
+ cls, session_id: Optional[str] = None
+ ) -> 'ContextUtility':
+ r"""Get or create shared workforce context utility with lazy init.
+
+ This method provides a centralized way to access shared context
+ utilities for workforce workflows, ensuring all workforce components
+ use the same session directory.
+
+ Args:
+ session_id (str, optional): Custom session ID. If None, uses the
+ default workforce session.
+
+ Returns:
+ ContextUtility: Shared context utility instance for workforce.
+ """
+ if session_id is None:
+ # Use default workforce session
+ if cls._default_workforce_session is None:
+ camel_workdir = os.environ.get("CAMEL_WORKDIR")
+ if camel_workdir:
+ base_path = os.path.join(
+ camel_workdir, "workforce_workflows"
+ )
+ else:
+ base_path = "workforce_workflows"
+
+ cls._default_workforce_session = cls(
+ working_directory=base_path,
+ create_folder=False, # Don't create folder until needed
+ )
+ return cls._default_workforce_session
+
+ # Use specific session
+ if session_id not in cls._shared_sessions:
+ camel_workdir = os.environ.get("CAMEL_WORKDIR")
+ if camel_workdir:
+ base_path = os.path.join(camel_workdir, "workforce_workflows")
+ else:
+ base_path = "workforce_workflows"
+
+ cls._shared_sessions[session_id] = cls(
+ working_directory=base_path,
+ session_id=session_id,
+ create_folder=False, # Don't create folder until needed
+ )
+ return cls._shared_sessions[session_id]
+
+ @classmethod
+ def reset_shared_sessions(cls) -> None:
+ r"""Reset shared sessions (useful for testing).
+
+ This method clears all shared session instances, forcing new ones
+ to be created on next access. Primarily used for testing to ensure
+ clean state between tests.
+ """
+ cls._shared_sessions.clear()
+ cls._default_workforce_session = None
diff --git a/examples/workforce/workforce_workflow_memory_example.py b/examples/workforce/workforce_workflow_memory_example.py
new file mode 100644
index 000000000..ef7051b34
--- /dev/null
+++ b/examples/workforce/workforce_workflow_memory_example.py
@@ -0,0 +1,317 @@
+#!/usr/bin/env python3
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+
+
+import os
+
+from dotenv import load_dotenv
+
+from camel.agents import ChatAgent
+from camel.configs import ChatGPTConfig
+from camel.logger import get_logger
+from camel.messages import BaseMessage
+from camel.models import ModelFactory
+from camel.societies.workforce import Workforce
+from camel.tasks.task import Task
+from camel.toolkits import MathToolkit
+from camel.types import ModelPlatformType, ModelType
+
+load_dotenv()
+
+logger = get_logger(__name__)
+
+
+def create_math_agent() -> ChatAgent:
+ r"""Create a math agent with math tools.
+
+ Returns:
+ ChatAgent: A configured math expert agent with MathToolkit tools.
+ """
+ math_msg = BaseMessage.make_assistant_message(
+ role_name="Math Expert",
+ content=(
+ "You are a math expert specialized in solving "
+ "mathematical problems.You can perform calculations, "
+ "solve equations, and work with various "
+ "mathematical concepts. Use the math tools available to you."
+ ),
+ )
+ model = ModelFactory.create(
+ model_platform=ModelPlatformType.OPENAI,
+ model_type=ModelType.DEFAULT,
+ model_config_dict=ChatGPTConfig().as_dict(),
+ )
+ return ChatAgent(
+ system_message=math_msg,
+ model=model,
+ tools=MathToolkit().get_tools(),
+ )
+
+
+def create_writer_agent() -> ChatAgent:
+ r"""Create a writer agent without tools.
+
+ Returns:
+ ChatAgent: A configured content writer agent without additional tools.
+ """
+ writer_msg = BaseMessage.make_assistant_message(
+ role_name="Content Writer",
+ content=(
+ "You are a skilled content writer who specializes in creating "
+ "clear, engaging, and well-structured written content. You excel "
+ "at storytelling, technical writing, and adapting your tone to "
+ "different audiences."
+ ),
+ )
+ model = ModelFactory.create(
+ model_platform=ModelPlatformType.DEFAULT,
+ model_type=ModelType.DEFAULT,
+ model_config_dict=ChatGPTConfig().as_dict(),
+ )
+ return ChatAgent(system_message=writer_msg, model=model)
+
+
+def demonstrate_first_session():
+ r"""Demonstrate first workforce session with workflow saving.
+
+ Creates a workforce with math and writer agents, processes tasks,
+ and saves the resulting workflows for future use.
+
+ Returns:
+ Dict[str, str]: Results of the workflow saving operation.
+ """
+ # Create workforce with two specialized agents
+ workforce = Workforce("Simple Demo Team")
+
+ # Add math agent with math tools
+ math_agent = create_math_agent()
+ workforce.add_single_agent_worker(
+ description="math_expert",
+ worker=math_agent,
+ enable_workflow_memory=True, # Enable to save workflows later
+ )
+
+ # Add writer agent without tools
+ writer_agent = create_writer_agent()
+ workforce.add_single_agent_worker(
+ description="content_writer",
+ worker=writer_agent,
+ enable_workflow_memory=True, # Enable to save workflows later
+ )
+
+ # Assign one task to each agent
+ tasks = [
+ Task(
+ content="Calculate the compound interest on $1000 invested at 5% "
+ "annual rate for 3 years",
+ id="math_task",
+ ),
+ Task(
+ content="Write a one-paragraph creative story about a robot "
+ "learning to paint",
+ id="writing_task",
+ ),
+ ]
+
+ for task in tasks:
+ try:
+ workforce.process_task(task)
+ except Exception as e:
+ logger.warning(f"Failed to process task {task.id}: {e}")
+
+ # Save workflows after completing tasks
+ saved_workflows = workforce.save_workflow_memories()
+
+ return saved_workflows
+
+
+def demonstrate_second_session():
+ r"""Demonstrate second workforce session with workflow loading.
+
+ Creates a new workforce instance, loads previously saved workflows,
+ and processes new tasks with the loaded workflow context.
+
+ Returns:
+ Dict[str, bool]: Results of the workflow loading operation.
+ """
+ # Create new workforce (simulating new session/process)
+ workforce = Workforce("Simple Demo Team - Session 2")
+
+ # Add workers with same descriptive names as before
+ math_agent = create_math_agent()
+ workforce.add_single_agent_worker(
+ description="math_expert", # Same description = loads matching
+ # workflow
+ worker=math_agent,
+ enable_workflow_memory=False, # Not saving in this session
+ )
+
+ writer_agent = create_writer_agent()
+ workforce.add_single_agent_worker(
+ description="content_writer", # Same description = loads
+ # matching workflow
+ worker=writer_agent,
+ enable_workflow_memory=False, # Not saving in this session
+ )
+
+ # Load previous workflows
+ loaded_workflows = workforce.load_workflow_memories()
+
+ # Process new tasks with loaded workflow context
+ new_tasks = [
+ Task(
+ content="Calculate the area of a circle with radius 7.5 meters",
+ id="new_math_task",
+ ),
+ Task(
+ content="Write a brief technical explanation of machine "
+ "learning for beginners",
+ id="new_writing_task",
+ ),
+ ]
+
+ for task in new_tasks:
+ try:
+ workforce.process_task(task)
+ except Exception as e:
+ logger.warning(f"Failed to process task {task.id}: {e}")
+
+ return loaded_workflows
+
+
+def demonstrate_workflow_file_management() -> None:
+ r"""Demonstrate workflow file management and inspection.
+
+ Shows information about where workflow files are stored and how
+ to inspect the saved workflow data.
+ """
+
+ # Get the base directory for workforce workflows
+ camel_workdir = os.environ.get("CAMEL_WORKDIR")
+ if camel_workdir:
+ base_dir = os.path.join(camel_workdir, "workforce_workflows")
+ else:
+ base_dir = "workforce_workflows"
+
+ logger.info(f"Workforce workflows are stored in: {base_dir}")
+
+ # Show how to check for existing workflow files
+ if os.path.exists(base_dir):
+ session_dirs = [
+ d
+ for d in os.listdir(base_dir)
+ if os.path.isdir(os.path.join(base_dir, d))
+ ]
+ logger.info(f"Found {len(session_dirs)} session directories")
+
+ for session_dir in session_dirs[:3]: # Show first 3 sessions
+ session_path = os.path.join(base_dir, session_dir)
+ workflow_files = [
+ f for f in os.listdir(session_path) if f.endswith('.md')
+ ]
+ logger.info(
+ f"Session {session_dir}: {len(workflow_files)} workflow files"
+ )
+ else:
+ logger.info("No workflow directory found yet")
+
+
+def main() -> None:
+ try:
+ # First session to execute tasks and save workflows
+ saved_results = demonstrate_first_session()
+ print(f"Workflow save results: {saved_results}")
+
+ # Second session to load workflows and execute new tasks
+ loaded_results = demonstrate_second_session()
+ print(f"Workflow load results: {loaded_results}")
+
+ except Exception as e:
+ logger.error(f"Demonstration failed: {e}")
+ raise
+
+
+if __name__ == "__main__":
+ main()
+
+"""
+===============================================================================
+Workflows saved:
+workforce_workflows/session_20250925_150330_302341/content_writer_workflow.md
+workforce_workflows/session_20250925_150330_302341/math_expert_workflow.md
+
+Saved workflow in math_expert_workflow.md
+<MARKDOWN CONTENT>
+## Metadata
+
+- session_id: session_20250925_150330_302341
+- working_directory: .../camel/workforce_workflows/
+ session_20250925_150330_302341
+- created_at: ...
+- base_directory: .../Codebase/camel/workforce_workflows
+- agent_id: 687b2e57-11ce-4138-9990-4d5ab58d2acc
+- message_count: 3
+
+## WorkflowSummary
+
+### Task Title
+Calculate compound interest
+
+### Task Description
+Calculate the future value of $1000 at 5% annual interest compounded
+once per year for 3 years, and produce a plain-text result containing:
+the compound interest formula with symbol definitions and numeric
+substitution, balances at end of Years 1-3, the final amount,
+total interest earned (all rounded to 2 decimals), and a final
+one-line JSON object with numeric values to two decimals.
+
+
+### Tools
+[Bullet point list of tools used]
+
+### Steps
+1. Identify the required formula: A = P*(1 + r)^n.
+2. Substitute given values into the formula: P = 1000, r = 0.05, n = 3.
+3. Compute the sum inside parentheses: calculate 1 + r = 1.05.
+4. Compute the exponentiation: calculate 1.05^3 = 1.157625.
+5. Multiply by principal: compute A = 1000 * 1.157625 = 1157.625 (unrounded).
+6. Compute total interest unrounded: Interest = A - P =
+ 1157.625 - 1000 = 157.625.
+7. Round results to 2 decimals and format as USD: A → $1157.63;
+ Interest → $157.63.
+8. Return deliverables: formula, each arithmetic step with numbers substituted,
+ unrounded A, rounded A in USD, and rounded interest in USD.
+
+### Failure And Recovery Strategies
+(No failure and recovery strategies recorded)
+
+### Notes And Observations
+No errors encountered.
+</MARKDOWN CONTENT>
+
+System message of math agent after loading workflow:
+You are a math expert specialized in solving mathematical problems.
+You can perform calculations, solve equations, and work with various
+mathematical concepts.
+Use the math tools available to you.
+
+--- Workflow Memory ---
+The following is the context from a previous session or workflow which might be
+useful for to the current task. This information might help you understand the
+background, choose which tools to use, and plan your next steps.
+
+## WorkflowSummary
+[The Workflow Summary from the above]
+"""
diff --git a/test/agents/test_chat_agent.py b/test/agents/test_chat_agent.py
index 36b645112..ffc8a06da 100644
--- a/test/agents/test_chat_agent.py
+++ b/test/agents/test_chat_agent.py
@@ -83,7 +83,7 @@ parametrize = pytest.mark.parametrize(
[
ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
- model_type=ModelType.GPT_5_MINI,
+ model_type=ModelType.DEFAULT,
),
pytest.param(None, marks=pytest.mark.model_backend),
],
@@ -103,11 +103,11 @@ def test_chat_agent(model, step_call_count=3):
assistant_without_sys_msg = ChatAgent(model=model)
assert str(assistant_with_sys_msg) == (
- "ChatAgent(doctor, " f"RoleType.ASSISTANT, {ModelType.GPT_5_MINI})"
+ "ChatAgent(doctor, " f"RoleType.ASSISTANT, {ModelType.DEFAULT})"
)
assert str(assistant_without_sys_msg) == (
"ChatAgent(assistant, "
- f"RoleType.ASSISTANT, {UnifiedModelType(ModelType.GPT_5_MINI)})"
+ f"RoleType.ASSISTANT, {UnifiedModelType(ModelType.DEFAULT)})"
)
for assistant in [assistant_with_sys_msg, assistant_without_sys_msg]:
diff --git a/test/agents/test_role_playing.py b/test/agents/test_role_playing.py
index af025fe29..d7b2c1989 100644
--- a/test/agents/test_role_playing.py
+++ b/test/agents/test_role_playing.py
@@ -34,7 +34,7 @@ from camel.types import (
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
- model_type=ModelType.GPT_5_MINI,
+ model_type=ModelType.DEFAULT,
)
model_backend_rsp = ChatCompletion(
@@ -105,11 +105,11 @@ def test_role_playing_init(model, critic_role_name, with_critic_in_the_loop):
assert isinstance(assistant_agent, ChatAgent)
assert isinstance(user_agent, ChatAgent)
if model is None:
- assert assistant_agent.model_backend.model_type == ModelType.GPT_5_MINI
- assert user_agent.model_backend.model_type == ModelType.GPT_5_MINI
+ assert assistant_agent.model_backend.model_type == ModelType.DEFAULT
+ assert user_agent.model_backend.model_type == ModelType.DEFAULT
else:
- assert assistant_agent.model_backend.model_type == ModelType.GPT_5_MINI
- assert user_agent.model_backend.model_type == ModelType.GPT_5_MINI
+ assert assistant_agent.model_backend.model_type == ModelType.DEFAULT
+ assert user_agent.model_backend.model_type == ModelType.DEFAULT
if not with_critic_in_the_loop:
assert critic is None
@@ -121,9 +121,9 @@ def test_role_playing_init(model, critic_role_name, with_critic_in_the_loop):
assert isinstance(critic, CriticAgent)
assert role_playing.critic_sys_msg is not None
if model is None:
- assert critic.model_backend.model_type == ModelType.GPT_5_MINI
+ assert critic.model_backend.model_type == ModelType.DEFAULT
else:
- assert critic.model_backend.model_type == ModelType.GPT_5_MINI
+ assert critic.model_backend.model_type == ModelType.DEFAULT
@pytest.mark.model_backend
diff --git a/test/toolkits/test_context_summarizer_toolkit.py b/test/toolkits/test_context_summarizer_toolkit.py
index 775599754..8b12162b1 100644
--- a/test/toolkits/test_context_summarizer_toolkit.py
+++ b/test/toolkits/test_context_summarizer_toolkit.py
@@ -244,7 +244,10 @@ def test_conversation_history_removal_and_summary_push(
backend_role = call_args[0][1] # Second argument (role)
# Verify the summary message content
- assert "[Context Summary from Previous Conversation]" in summary_message.content
+ assert (
+ "[Context Summary from Previous Conversation]"
+ in summary_message.content
+ )
assert summary_text in summary_message.content
assert backend_role == OpenAIBackendRole.USER
diff --git a/test/utils/test_context_utils.py b/test/utils/test_context_utils.py
index ceb805b4b..c29670541 100644
--- a/test/utils/test_context_utils.py
+++ b/test/utils/test_context_utils.py
@@ -33,6 +33,9 @@ def temp_directory():
@pytest.fixture(scope="function")
def mock_agent():
r"""Create a mock ChatAgent for testing."""
+ from camel.messages import BaseMessage
+ from camel.types import RoleType
+
agent = MagicMock(spec=ChatAgent)
agent.agent_id = "test_agent_001"
@@ -40,6 +43,38 @@ def mock_agent():
mock_model = MagicMock(spec=BaseModelBackend)
agent.model_backend = mock_model
+ # Create mock system message
+ mock_system_message = MagicMock(spec=BaseMessage)
+ mock_system_message.content = "You are a helpful assistant."
+ mock_system_message.role_name = "Assistant"
+ mock_system_message.role_type = RoleType.ASSISTANT
+ mock_system_message.meta_dict = {}
+
+ def create_new_instance_side_effect(new_content):
+ new_mock = MagicMock(spec=BaseMessage)
+ new_mock.content = new_content
+ new_mock.role_name = "Assistant"
+ new_mock.role_type = RoleType.ASSISTANT
+ new_mock.meta_dict = {}
+ new_mock.create_new_instance = MagicMock(
+ side_effect=create_new_instance_side_effect
+ )
+ return new_mock
+
+ mock_system_message.create_new_instance = MagicMock(
+ side_effect=create_new_instance_side_effect
+ )
+
+ # Set up required attributes
+ agent._original_system_message = mock_system_message
+ agent._system_message = mock_system_message
+ agent.system_message = mock_system_message
+
+ # Create mock memory with clear method
+ mock_memory = MagicMock()
+ mock_memory.clear = MagicMock()
+ agent.memory = mock_memory
+
agent.update_memory = MagicMock()
agent.clear_memory = MagicMock()
return agent
@@ -101,7 +136,7 @@ Data analysis of customer sales using pandas and matplotlib.
)
assert "Data analysis of customer sales" in context_message.content
assert "Sales increased 15%" in context_message.content
- assert backend_role.value == "user"
+ assert backend_role.value == "system"
def test_load_markdown_context_to_memory_file_not_found(
@@ -214,7 +249,7 @@ def test_save_and_load_markdown_file(context_utility_fixture):
filename="test_file", content=test_content, title="Test Title"
)
- assert "saved successfully" in result
+ assert result == "success"
# Load file
loaded_content = context_util.load_markdown_file("test_file")
diff --git a/test/workforce/test_workflow_memory.py b/test/workforce/test_workflow_memory.py
new file mode 100644
index 000000000..fb68be418
--- /dev/null
+++ b/test/workforce/test_workflow_memory.py
@@ -0,0 +1,710 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+
+import os
+import tempfile
+from typing import List
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+from camel.agents.chat_agent import ChatAgent
+from camel.messages.base import BaseMessage
+from camel.societies.workforce import Workforce
+from camel.societies.workforce.single_agent_worker import SingleAgentWorker
+from camel.tasks.task import Task, TaskState
+
+
+class MockSingleAgentWorker(SingleAgentWorker):
+ """A mock worker for testing workflow functionality."""
+
+ def __init__(self, description: str):
+ # Create a mock agent with conversation history
+ sys_msg = BaseMessage.make_assistant_message(
+ role_name="Test Worker",
+ content=f"You are a {description} for testing.",
+ )
+ agent = ChatAgent(sys_msg)
+ super().__init__(description, agent)
+
+ async def _process_task(
+ self, task: Task, dependencies: List[Task]
+ ) -> TaskState:
+ """Mock task processing with simulated conversation."""
+ # Simulate some conversation history
+ user_msg = BaseMessage.make_user_message(
+ role_name="User", content=f"Please process: {task.content}"
+ )
+ assistant_msg = BaseMessage.make_assistant_message(
+ role_name="Assistant",
+ content=f"Processed task: {task.content} successfully",
+ )
+
+ # Record conversation to create some history for summarization
+ self.worker.record_message(user_msg)
+ self.worker.record_message(assistant_msg)
+
+ task.result = f"Successfully processed: {task.content}"
+ task.state = TaskState.DONE
+ return TaskState.DONE
+
+
+def temp_context_dir():
+ """Create a temporary directory for context files."""
+ with tempfile.TemporaryDirectory() as temp_dir:
+ with patch.dict(os.environ, {"CAMEL_WORKDIR": temp_dir}):
+ yield temp_dir
+
+
+def mock_workforce():
+ """Create a workforce with mock SingleAgentWorker instances."""
+ workforce = Workforce("Test Workforce")
+
+ # Add mock workers with different descriptions
+ analyst_worker = MockSingleAgentWorker("data_analyst")
+ developer_worker = MockSingleAgentWorker("python_developer")
+
+ workforce._children = [analyst_worker, developer_worker]
+ return workforce
+
+
+class TestSingleAgentWorkerWorkflow:
+ """Test workflow functionality for SingleAgentWorker."""
+
+ def test_save_workflow_success(self, temp_context_dir):
+ """Test successful workflow saving."""
+ worker = MockSingleAgentWorker("test_analyst")
+
+ # Mock the summarize method to return success
+ mock_result = {
+ "status": "success",
+ "summary": "Test workflow summary",
+ "file_path": (
+ f"{temp_context_dir}/test_analyst_workflow_20250122.md"
+ ),
+ }
+
+ with patch.object(
+ worker.worker, 'summarize', return_value=mock_result
+ ):
+ result = worker.save_workflow_memories()
+
+ assert result["status"] == "success"
+ assert result["worker_description"] == "test_analyst"
+ assert "test_analyst_workflow_" in result["file_path"]
+
+ def test_save_workflow_non_chat_agent(self):
+ """Test workflow saving with non-ChatAgent worker."""
+ # Create worker and then replace worker with non-ChatAgent
+ sys_msg = BaseMessage.make_assistant_message(
+ role_name="Test Worker", content="Test"
+ )
+ worker = SingleAgentWorker("test", ChatAgent(sys_msg))
+
+ # Replace the worker with a non-ChatAgent object
+ worker.worker = "not_a_chat_agent"
+
+ result = worker.save_workflow_memories()
+
+ assert result["status"] == "error"
+ assert "Worker must be a ChatAgent" in result["message"]
+ assert result["worker_description"] == "test"
+
+ def test_save_workflow_exception(self, temp_context_dir):
+ """Test workflow saving when exception occurs."""
+ worker = MockSingleAgentWorker("test_analyst")
+
+ # Mock summarize to raise exception
+ with patch.object(
+ worker.worker, 'summarize', side_effect=Exception("Test error")
+ ):
+ result = worker.save_workflow_memories()
+
+ assert result["status"] == "error"
+ assert (
+ "Failed to save workflow memories: Test error"
+ in result["message"]
+ )
+
+ @patch('glob.glob')
+ @patch('os.path.getmtime')
+ def test_load_workflow_success(
+ self, mock_getmtime, mock_glob, temp_context_dir
+ ):
+ """Test successful workflow loading."""
+ worker = MockSingleAgentWorker("data_analyst")
+
+ # Mock file discovery
+ mock_files = [
+ f"{temp_context_dir}/session_123/data_analyst_workflow_20250122.md"
+ ]
+ mock_glob.return_value = mock_files
+ mock_getmtime.return_value = 1234567890
+
+ # Mock the shared context utility method
+ mock_context_utility = MagicMock()
+ mock_context_utility.load_markdown_context_to_memory.return_value = (
+ "Context appended successfully"
+ )
+
+ with patch(
+ 'camel.utils.context_utils.ContextUtility.get_workforce_shared',
+ return_value=mock_context_utility,
+ ):
+ result = worker.load_workflow_memories()
+
+ assert result is True
+ mock_context_utility.load_markdown_context_to_memory.assert_called()
+
+ def test_load_workflow_no_files(self, temp_context_dir):
+ """Test workflow loading when no files found."""
+ worker = MockSingleAgentWorker("data_analyst")
+
+ with patch('glob.glob', return_value=[]):
+ result = worker.load_workflow_memories()
+
+ assert result is False
+
+ def test_load_workflow_non_chat_agent(self):
+ """Test workflow loading with non-ChatAgent worker."""
+ # Create worker and then replace worker with non-ChatAgent
+ sys_msg = BaseMessage.make_assistant_message(
+ role_name="Test Worker", content="Test"
+ )
+ worker = SingleAgentWorker("test", ChatAgent(sys_msg))
+
+ # Replace the worker with a non-ChatAgent object
+ worker.worker = "not_a_chat_agent"
+
+ result = worker.load_workflow_memories()
+
+ assert result is False
+
+ @patch('glob.glob')
+ def test_load_workflow_prioritizes_newest_session(
+ self, mock_glob, temp_context_dir
+ ):
+ """Test that workflow loading prioritizes files from newest sessions.
+
+ This test verifies that when multiple workflow files exist across
+ different sessions, files from the newest session (by session
+ timestamp) are loaded first, regardless of file modification times.
+ """
+ worker = MockSingleAgentWorker("data_analyst")
+
+ # Simulate realistic scenario: multiple workforce sessions over time
+ # Each session has workflows from different agents
+ # Session 1 (older): 17:23:56
+ # Session 2 (newer): 17:46:50
+ mock_files = [
+ # Newer session files (data_analyst and other agents)
+ f"{temp_context_dir}/workforce_workflows/session_20251002_174650_470517/data_analyst_workflow.md",
+ f"{temp_context_dir}/workforce_workflows/session_20251002_174650_470517/developer_workflow.md",
+ # Older session files (data_analyst and other agents)
+ f"{temp_context_dir}/workforce_workflows/session_20251002_172356_365242/data_analyst_workflow.md",
+ f"{temp_context_dir}/workforce_workflows/session_20251002_172356_365242/researcher_workflow.md",
+ ]
+ mock_glob.return_value = mock_files
+
+ # Mock the shared context utility method
+ mock_context_utility = MagicMock()
+ mock_context_utility.load_markdown_context_to_memory.return_value = (
+ "Context appended successfully"
+ )
+
+ with patch(
+ 'camel.utils.context_utils.ContextUtility.get_workforce_shared',
+ return_value=mock_context_utility,
+ ):
+ result = worker.load_workflow_memories(max_files_to_load=1)
+
+ assert result is True
+
+ # Verify load was called once
+ assert (
+ mock_context_utility.load_markdown_context_to_memory.call_count
+ == 1
+ )
+
+ # Get the filename that was loaded (second argument)
+ loaded_filename = (
+ mock_context_utility.load_markdown_context_to_memory.call_args[
+ 0
+ ][1]
+ )
+
+ # The loaded file should be 'data_analyst_workflow' (without .md)
+ # from the newer session (verified by it being loaded first)
+ assert loaded_filename == "data_analyst_workflow"
+
+ @patch('glob.glob')
+ def test_load_workflow_memories_resets_system_message(
+ self, mock_glob, temp_context_dir
+ ):
+ """Test that multiple calls to load_workflow_memories reset system
+ message.
+
+ This test verifies that calling load_workflow_memories multiple times
+ doesn't accumulate workflow context but instead resets to original
+ system message each time.
+ """
+ worker = MockSingleAgentWorker("data_analyst")
+
+ # get original system message content
+ original_content = worker.worker._original_system_message.content
+
+ # mock workflow files
+ mock_files = [
+ f"{temp_context_dir}/workforce_workflows/session_1/data_analyst_workflow.md"
+ ]
+ mock_glob.return_value = mock_files
+
+ # mock the shared context utility method
+ mock_context_utility = MagicMock()
+ mock_context_utility.load_markdown_context_to_memory.return_value = (
+ "Context appended successfully"
+ )
+
+ with patch(
+ 'camel.utils.context_utils.ContextUtility.get_workforce_shared',
+ return_value=mock_context_utility,
+ ):
+ # first call to load_workflow_memories
+ worker.load_workflow_memories(max_files_to_load=1)
+
+ # verify reset was called (system message matches original)
+ first_call_system_content = worker.worker._system_message.content
+
+ # second call to load_workflow_memories
+ worker.load_workflow_memories(max_files_to_load=1)
+
+ # after second call, system message should still be based on
+ # original (not accumulating from first call)
+ second_call_system_content = worker.worker._system_message.content
+
+ # verify both calls resulted in same base system message
+ # (indicating reset happened before each load)
+ assert first_call_system_content == second_call_system_content
+
+ # verify the system message contains original content
+ assert original_content in second_call_system_content
+
+ # verify load was called twice (once per load_workflow_memories)
+ assert (
+ mock_context_utility.load_markdown_context_to_memory.call_count
+ == 2
+ )
+
+
+class TestWorkforceWorkflowMemoryMethods:
+ """Test workflow functionality for Workforce."""
+
+ def test_save_workflow_memories_success(self, mock_workforce):
+ """Test successful workflow saving for all workers."""
+ # Mock save_workflow_memories for both workers
+ mock_result_1 = {
+ "status": "success",
+ "file_path": "/path/to/data_analyst_workflow.md",
+ }
+ mock_result_2 = {
+ "status": "success",
+ "file_path": "/path/to/python_developer_workflow.md",
+ }
+
+ with (
+ patch.object(
+ mock_workforce._children[0],
+ 'save_workflow_memories',
+ return_value=mock_result_1,
+ ),
+ patch.object(
+ mock_workforce._children[1],
+ 'save_workflow_memories',
+ return_value=mock_result_2,
+ ),
+ ):
+ results = mock_workforce.save_workflow_memories()
+
+ assert len(results) == 2
+ assert all("/path/to/" in path for path in results.values())
+
+ def test_save_workflow_memories_mixed_results(self, mock_workforce):
+ """Test workflow saving with mixed success/failure results."""
+ # Mock one success, one failure
+ mock_result_success = {
+ "status": "success",
+ "file_path": "/path/to/data_analyst_workflow.md",
+ }
+ mock_result_error = {
+ "status": "error",
+ "message": "No conversation context",
+ }
+
+ with (
+ patch.object(
+ mock_workforce._children[0],
+ 'save_workflow_memories',
+ return_value=mock_result_success,
+ ),
+ patch.object(
+ mock_workforce._children[1],
+ 'save_workflow_memories',
+ return_value=mock_result_error,
+ ),
+ ):
+ results = mock_workforce.save_workflow_memories()
+
+ assert len(results) == 2
+ assert (
+ "/path/to/data_analyst_workflow.md"
+ in results[mock_workforce._children[0].node_id]
+ )
+ assert (
+ "error: No conversation context"
+ in results[mock_workforce._children[1].node_id]
+ )
+
+ def test_save_workflow_memories_exception(self, mock_workforce):
+ """Test workflow saving when exception occurs."""
+ with patch.object(
+ mock_workforce._children[0],
+ 'save_workflow_memories',
+ side_effect=Exception("Test error"),
+ ):
+ results = mock_workforce.save_workflow_memories()
+
+ assert (
+ "error: Test error"
+ in results[mock_workforce._children[0].node_id]
+ )
+
+ def test_save_workflow_memories_real_execution(self, temp_context_dir):
+ """Test save_workflow_memories with real internal execution.
+
+ This test exercises the actual save_workflow_memories() implementation,
+ only mocking the ChatAgent.summarize() method to avoid LLM API calls.
+
+ Tests the following internal behavior:
+ - Workforce iteration through child workers
+ - SingleAgentWorker validation logic
+ (_validate_workflow_save_requirements)
+ - Context utility setup and agent configuration
+ - Filename generation from worker description
+ (_generate_workflow_filename)
+ - Workflow prompt preparation (_prepare_workflow_prompt)
+ - Agent selection for summarization (_select_agent_for_summarization)
+ - Worker metadata addition to results
+ - Result processing and error handling in Workforce
+ - Shared context utility setup between Workforce and workers
+ """
+ workforce = Workforce("Test Workforce")
+ worker = MockSingleAgentWorker("data_analyst")
+ workforce._children = [worker]
+
+ # Simulate some conversation history
+ user_msg = BaseMessage.make_user_message(
+ role_name="User", content="Analyze the sales 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)
+
+ # Store initial conversation accumulator state
+ initial_accumulator = worker._conversation_accumulator
+
+ # Mock only the ChatAgent.summarize() method (which makes LLM calls)
+ mock_summary_result = {
+ "status": "success",
+ "summary": "Completed data analysis workflow",
+ "file_path": (
+ f"{temp_context_dir}/workforce_workflows/"
+ "session_test/data_analyst_workflow.md"
+ ),
+ "worker_description": "data_analyst",
+ }
+
+ with patch.object(
+ ChatAgent, 'summarize', return_value=mock_summary_result
+ ) as mock_summarize:
+ # This executes the real save_workflow_memories() logic
+ results = workforce.save_workflow_memories()
+
+ # Verify Workforce correctly processes worker results
+ assert len(results) == 1
+ assert worker.node_id in results
+ assert "data_analyst_workflow" in results[worker.node_id]
+ assert results[worker.node_id] == mock_summary_result["file_path"]
+
+ # Verify shared context utility was set up correctly
+ assert worker.worker._context_utility is not None
+
+ # Verify ChatAgent.summarize was called with correct parameters
+ # (validates filename generation, prompt preparation, agent
+ # selection)
+ mock_summarize.assert_called_once()
+ call_kwargs = mock_summarize.call_args[1]
+
+ # Verify filename generation includes worker description
+ assert 'filename' in call_kwargs
+ assert 'data_analyst_workflow' in call_kwargs['filename']
+
+ # Verify structured output format is set (WorkflowSummary)
+ assert 'response_format' in call_kwargs
+ assert call_kwargs['response_format'] is not None
+
+ # Verify summary prompt was prepared
+ assert 'summary_prompt' in call_kwargs
+ assert call_kwargs['summary_prompt'] is not None
+
+ # Verify conversation accumulator cleanup
+ # (it should be None after successful save)
+ if initial_accumulator is not None:
+ assert worker._conversation_accumulator is None
+
+ def test_load_workflow_memories_success(self, mock_workforce):
+ """Test successful workflow loading for all workers."""
+ with (
+ patch.object(
+ mock_workforce._children[0],
+ 'load_workflow_memories',
+ return_value=True,
+ ),
+ patch.object(
+ mock_workforce._children[1],
+ 'load_workflow_memories',
+ return_value=True,
+ ),
+ ):
+ results = mock_workforce.load_workflow_memories()
+
+ assert len(results) == 2
+ assert all(success for success in results.values())
+
+ def test_load_workflow_memories_mixed_results(self, mock_workforce):
+ """Test workflow loading with mixed success/failure results."""
+ with (
+ patch.object(
+ mock_workforce._children[0],
+ 'load_workflow_memories',
+ return_value=True,
+ ),
+ patch.object(
+ mock_workforce._children[1],
+ 'load_workflow_memories',
+ return_value=False,
+ ),
+ ):
+ results = mock_workforce.load_workflow_memories()
+
+ assert results[mock_workforce._children[0].node_id] is True
+ assert results[mock_workforce._children[1].node_id] is False
+
+ def test_load_workflow_memories_exception(self, mock_workforce):
+ """Test workflow loading when exception occurs."""
+ with patch.object(
+ mock_workforce._children[0],
+ 'load_workflow_memories',
+ side_effect=Exception("Test error"),
+ ):
+ results = mock_workforce.load_workflow_memories()
+
+ assert results[mock_workforce._children[0].node_id] is False
+
+ def test_workflows_skip_non_single_agent_workers(self):
+ """Test that workflow methods skip non-SingleAgentWorker instances."""
+ from camel.societies.workforce.role_playing_worker import (
+ RolePlayingWorker,
+ )
+
+ workforce = Workforce("Test Workforce")
+
+ # Add a mock RolePlayingWorker (not SingleAgentWorker)
+ mock_role_playing_worker = MagicMock(spec=RolePlayingWorker)
+ mock_role_playing_worker.node_id = "role_playing_worker_123"
+ workforce._children = [mock_role_playing_worker]
+
+ # Test save_workflows
+ save_results = workforce.save_workflow_memories()
+ assert (
+ "skipped: MagicMock not supported"
+ in save_results[mock_role_playing_worker.node_id]
+ )
+
+ # Test load_workflows
+ load_results = workforce.load_workflow_memories()
+ assert load_results[mock_role_playing_worker.node_id] is False
+
+
+class TestWorkflowIntegration:
+ """Integration tests for workflow functionality."""
+
+ def test_end_to_end_workflow_memory(self, temp_context_dir):
+ """Test complete workflow: save workflow, load in new session."""
+ # First session: create workforce and mock workflow saving
+ workforce1 = Workforce("Test Team")
+ worker1 = MockSingleAgentWorker("data_analyst")
+ workforce1._children = [worker1]
+
+ # Mock successful summarize and save
+ mock_save_result = {
+ "status": "success",
+ "summary": "Data analysis workflow completed",
+ "file_path": f"{temp_context_dir}/data_analyst_workflow_test.md",
+ }
+
+ with patch.object(
+ worker1.worker, 'summarize', return_value=mock_save_result
+ ):
+ save_results = workforce1.save_workflow_memories()
+ assert (
+ save_results[worker1.node_id] == mock_save_result["file_path"]
+ )
+
+ # Second session: create new workforce and load workflows
+ workforce2 = Workforce("Test Team")
+ worker2 = MockSingleAgentWorker("data_analyst")
+ workforce2._children = [worker2]
+
+ # Mock successful workflow loading
+ with patch.object(
+ worker2, 'load_workflow_memories', return_value=True
+ ):
+ load_results = workforce2.load_workflow_memories()
+ assert load_results[worker2.node_id] is True
+
+ def test_filename_sanitization(self):
+ """Test worker descriptions are properly sanitized for filenames."""
+ worker = MockSingleAgentWorker("Data Analyst & ML Engineer!")
+
+ # Mock datetime for consistent testing
+ with patch(
+ 'camel.societies.workforce.single_agent_worker.datetime'
+ ) as mock_datetime:
+ mock_datetime.datetime.now.return_value.strftime.return_value = (
+ "20250122_143022"
+ )
+
+ mock_result = {
+ "status": "success",
+ "summary": "Test summary",
+ "file_path": (
+ "/path/to/data_analyst_ml_engineer_workflow_"
+ "20250122_143022.md"
+ ),
+ }
+
+ with patch.object(
+ worker.worker, 'summarize', return_value=mock_result
+ ):
+ # Verify the filename generation works with special characters
+ result = worker.save_workflow_memories()
+ assert result["status"] == "success"
+
+
+class TestSharedContextUtility:
+ """Test shared context utility functionality."""
+
+ def test_get_workforce_shared_default(self):
+ """Test getting default workforce shared context."""
+ from camel.utils.context_utils import ContextUtility
+
+ # Reset any existing shared sessions
+ ContextUtility.reset_shared_sessions()
+
+ # Get default shared context
+ context1 = ContextUtility.get_workforce_shared()
+ context2 = ContextUtility.get_workforce_shared()
+
+ # Should return the same instance
+ assert context1 is context2
+ assert context1.session_id == context2.session_id
+
+ def test_get_workforce_shared_specific_session(self):
+ """Test getting workforce shared context with specific session ID."""
+ from camel.utils.context_utils import ContextUtility
+
+ # Reset any existing shared sessions
+ ContextUtility.reset_shared_sessions()
+
+ session_id = "test_session_123"
+
+ # Get shared context with specific session
+ context1 = ContextUtility.get_workforce_shared(session_id)
+ context2 = ContextUtility.get_workforce_shared(session_id)
+
+ # Should return the same instance
+ assert context1 is context2
+ assert context1.session_id == session_id
+ assert context2.session_id == session_id
+
+ def test_reset_shared_sessions(self):
+ """Test resetting shared sessions."""
+ from camel.utils.context_utils import ContextUtility
+
+ # Create some shared contexts
+ context1 = ContextUtility.get_workforce_shared()
+ context2 = ContextUtility.get_workforce_shared("test_session")
+
+ # Reset sessions
+ ContextUtility.reset_shared_sessions()
+
+ # Get new contexts - should be different instances
+ new_context1 = ContextUtility.get_workforce_shared()
+ new_context2 = ContextUtility.get_workforce_shared("test_session")
+
+ assert new_context1 is not context1
+ assert new_context2 is not context2
+
+ def test_lazy_initialization_no_folder_creation(self):
+ """Test that shared contexts don't create folders until needed."""
+ import os
+ import tempfile
+
+ from camel.utils.context_utils import ContextUtility
+
+ # Reset any existing shared sessions
+ ContextUtility.reset_shared_sessions()
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ with patch.dict(os.environ, {"CAMEL_WORKDIR": temp_dir}):
+ # Get shared context
+ context = ContextUtility.get_workforce_shared()
+
+ # Session directory should not exist yet
+ session_dir = context.get_working_directory()
+ assert not session_dir.exists()
+
+ # Creating a file should create the directory
+ context.save_markdown_file("test", "test content")
+ assert session_dir.exists()
+
+ def test_single_agent_worker_uses_shared_context(self):
+ """Test that SingleAgentWorker uses shared context."""
+ from camel.utils.context_utils import ContextUtility
+
+ # Reset any existing shared sessions
+ ContextUtility.reset_shared_sessions()
+
+ worker = MockSingleAgentWorker("test_worker")
+
+ # Get context utility - should use shared instance
+ context_util = worker._get_context_utility()
+
+ # Should be the same as calling get_workforce_shared
+ shared_context = ContextUtility.get_workforce_shared()
+ assert context_util is shared_context