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): unify streaming response handling and callback integration (#3744)

Lei Zhang2cf8e987bc06
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 1bc6921c0..0e092b64c 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -4561,6 +4561,9 @@ class ChatAgent(BaseAgent):
"external_tool_requests": None,
"streaming": False,
"partial": False,
+ "stream_accumulate_mode": "accumulate"
+ if self.stream_accumulate
+ else "delta",
},
)
self._emit_request_usage(
@@ -4790,6 +4793,9 @@ class ChatAgent(BaseAgent):
"external_tool_requests": None,
"streaming": False,
"partial": False,
+ "stream_accumulate_mode": "accumulate"
+ if self.stream_accumulate
+ else "delta",
},
)
yield final_response
@@ -5565,6 +5571,9 @@ class ChatAgent(BaseAgent):
"external_tool_requests": None,
"streaming": False,
"partial": False,
+ "stream_accumulate_mode": "accumulate"
+ if self.stream_accumulate
+ else "delta",
},
)
await self._aemit_request_usage(
@@ -5896,6 +5905,9 @@ class ChatAgent(BaseAgent):
"external_tool_requests": None,
"streaming": False,
"partial": False,
+ "stream_accumulate_mode": "accumulate"
+ if self.stream_accumulate
+ else "delta",
},
)
yield final_response
@@ -6048,6 +6060,9 @@ class ChatAgent(BaseAgent):
"external_tool_requests": None,
"streaming": True,
"partial": True,
+ "stream_accumulate_mode": "accumulate"
+ if self.stream_accumulate
+ else "delta",
},
)
diff --git a/camel/societies/workforce/role_playing_worker.py b/camel/societies/workforce/role_playing_worker.py
index 54c26054a..00454078c 100644
--- a/camel/societies/workforce/role_playing_worker.py
+++ b/camel/societies/workforce/role_playing_worker.py
@@ -13,7 +13,7 @@
# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
from __future__ import annotations
-from typing import Dict, List, Optional
+from typing import TYPE_CHECKING, Awaitable, Callable, Dict, List, Optional
from colorama import Fore
@@ -31,6 +31,9 @@ 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
+if TYPE_CHECKING:
+ from camel.responses import ChatAgentResponse
+
class RolePlayingWorker(Worker):
r"""A worker node that contains a role playing.
@@ -98,7 +101,12 @@ class RolePlayingWorker(Worker):
self.user_agent_kwargs = user_agent_kwargs
async def _process_task(
- self, task: Task, dependencies: List[Task]
+ self,
+ task: Task,
+ dependencies: List[Task],
+ stream_callback: Optional[
+ Callable[["ChatAgentResponse"], Optional[Awaitable[None]]]
+ ] = None,
) -> TaskState:
r"""Processes a task leveraging its dependencies through role-playing.
diff --git a/camel/societies/workforce/single_agent_worker.py b/camel/societies/workforce/single_agent_worker.py
index 3026e832d..babed1e71 100644
--- a/camel/societies/workforce/single_agent_worker.py
+++ b/camel/societies/workforce/single_agent_worker.py
@@ -17,7 +17,15 @@ import asyncio
import datetime
import time
from collections import deque
-from typing import Any, Dict, List, Optional
+from typing import (
+ TYPE_CHECKING,
+ Any,
+ Awaitable,
+ Callable,
+ Dict,
+ List,
+ Optional,
+)
from colorama import Fore
@@ -34,8 +42,12 @@ from camel.societies.workforce.workflow_memory_manager import (
WorkflowMemoryManager,
)
from camel.tasks.task import Task, TaskState, is_task_result_insufficient
+from camel.utils import consume_response_content_async
from camel.utils.context_utils import ContextUtility
+if TYPE_CHECKING:
+ from camel.responses import ChatAgentResponse
+
logger = get_logger(__name__)
@@ -335,7 +347,12 @@ class SingleAgentWorker(Worker):
return self._workflow_manager
async def _process_task(
- self, task: Task, dependencies: List[Task]
+ self,
+ task: Task,
+ dependencies: List[Task],
+ stream_callback: Optional[
+ Callable[["ChatAgentResponse"], Optional[Awaitable[None]]]
+ ] = None,
) -> TaskState:
r"""Processes a task with its dependencies using an efficient agent
management system.
@@ -349,7 +366,9 @@ class SingleAgentWorker(Worker):
task (Task): The task to process, which includes necessary details
like content and type.
dependencies (List[Task]): Tasks that the given task depends on.
-
+ stream_callback (Callable[[ChatAgentResponse], None], optional): A
+ callback function that receives each chunk (ChatAgentResponse)
+ during streaming decomposition.
Returns:
TaskState: `TaskState.DONE` if processed successfully, otherwise
`TaskState.FAILED`.
@@ -392,18 +411,12 @@ class SingleAgentWorker(Worker):
response = await worker_agent.astep(enhanced_prompt)
# Handle streaming response
- if isinstance(response, AsyncStreamingChatAgentResponse):
- content = ""
- async for chunk in response:
- if chunk.msg:
- content = chunk.msg.content
- response_content = content
- else:
- # Regular ChatAgentResponse
- response_content = (
- response.msg.content if response.msg else ""
- )
-
+ (
+ response,
+ response_content,
+ ) = await consume_response_content_async(
+ response, stream_callback
+ )
task_result = (
self.structured_handler.parse_structured_response(
response_text=response_content,
@@ -424,6 +437,10 @@ class SingleAgentWorker(Worker):
if isinstance(response, AsyncStreamingChatAgentResponse):
task_result = None
async for chunk in response:
+ if stream_callback:
+ maybe = stream_callback(chunk)
+ if asyncio.iscoroutine(maybe):
+ await maybe
if chunk.msg and chunk.msg.parsed:
task_result = chunk.msg.parsed
response_content = chunk.msg.content
diff --git a/camel/societies/workforce/worker.py b/camel/societies/workforce/worker.py
index 2ea1c6d13..9fd9a39b4 100644
--- a/camel/societies/workforce/worker.py
+++ b/camel/societies/workforce/worker.py
@@ -16,7 +16,7 @@ from __future__ import annotations
import asyncio
import logging
from abc import ABC, abstractmethod
-from typing import List, Optional, Set
+from typing import TYPE_CHECKING, Awaitable, Callable, List, Optional, Set
from colorama import Fore
@@ -25,6 +25,9 @@ from camel.societies.workforce.task_channel import TaskChannel
from camel.societies.workforce.utils import check_if_running
from camel.tasks.task import Task, TaskState
+if TYPE_CHECKING:
+ from camel.responses import ChatAgentResponse
+
logger = logging.getLogger(__name__)
@@ -46,13 +49,24 @@ class Worker(BaseNode, ABC):
super().__init__(description, node_id=node_id)
self._active_task_ids: Set[str] = set()
self._running_tasks: Set[asyncio.Task] = set()
+ self._stream_callback: Optional[
+ Callable[
+ ["ChatAgentResponse", str, str],
+ Optional[Awaitable[None]],
+ ]
+ ] = None
def __repr__(self):
return f"Worker node {self.node_id} ({self.description})"
@abstractmethod
async def _process_task(
- self, task: Task, dependencies: List[Task]
+ self,
+ task: Task,
+ dependencies: List[Task],
+ stream_callback: Optional[
+ Callable[["ChatAgentResponse"], Optional[Awaitable[None]]]
+ ] = None,
) -> TaskState:
r"""Processes a task based on its dependencies.
@@ -80,6 +94,18 @@ class Worker(BaseNode, ABC):
def set_channel(self, channel: TaskChannel):
self._channel = channel
+ def set_stream_callback(
+ self,
+ stream_callback: Optional[
+ Callable[
+ ["ChatAgentResponse", str, str],
+ Optional[Awaitable[None]],
+ ]
+ ],
+ ) -> None:
+ r"""Set streaming callback for worker token/chunk updates."""
+ self._stream_callback = stream_callback
+
async def _process_single_task(self, task: Task) -> None:
r"""Process a single task and handle its completion/failure."""
try:
@@ -89,8 +115,24 @@ class Worker(BaseNode, ABC):
f"{Fore.RESET}"
)
+ async def _on_chunk(chunk: "ChatAgentResponse") -> None:
+ if self._stream_callback is None:
+ return
+ try:
+ maybe = self._stream_callback(chunk, self.node_id, task.id)
+ if asyncio.iscoroutine(maybe):
+ await maybe
+ except Exception:
+ logger.warning(
+ "stream_callback failed while processing "
+ "worker stream chunk",
+ exc_info=True,
+ )
+
# Process the task
- task_state = await self._process_task(task, task.dependencies)
+ task_state = await self._process_task(
+ task, task.dependencies, stream_callback=_on_chunk
+ )
# Update the result and status of the task
task.set_state(task_state)
diff --git a/camel/societies/workforce/workforce.py b/camel/societies/workforce/workforce.py
index 59b373f4f..bea689dcc 100644
--- a/camel/societies/workforce/workforce.py
+++ b/camel/societies/workforce/workforce.py
@@ -24,6 +24,7 @@ from enum import Enum
from typing import (
TYPE_CHECKING,
Any,
+ Awaitable,
Callable,
Coroutine,
Deque,
@@ -91,7 +92,12 @@ from camel.toolkits import (
SearchToolkit,
ThinkingToolkit,
)
-from camel.utils import dependencies_required, safe_extract_parsed
+from camel.utils import (
+ consume_response_content,
+ consume_response_content_async,
+ dependencies_required,
+ safe_extract_parsed,
+)
from .events import (
AllTasksCompletedEvent,
@@ -243,6 +249,10 @@ class Workforce(BaseNode):
is added automatically. If at least one provided callback
implements :class:`WorkforceMetrics`, no default logger is added.
(default: :obj:`None`)
+ stream_callback (Optional[Callable], optional): Real-time callback for
+ worker streaming output chunks. Called as
+ ``(worker_id, task_id, text, mode)``.
+ (default: :obj:`None`)
mode (WorkforceMode, optional): The execution mode for task
processing. AUTO_DECOMPOSE mode uses intelligent recovery
strategies (decompose, replan, etc.) when tasks fail.
@@ -323,6 +333,9 @@ class Workforce(BaseNode):
task_timeout_seconds: Optional[float] = None,
mode: WorkforceMode = WorkforceMode.AUTO_DECOMPOSE,
callbacks: Optional[List[WorkforceCallback]] = None,
+ stream_callback: Optional[
+ Callable[[str, str, str, str], Optional[Awaitable[None]]]
+ ] = None,
failure_handling_config: Optional[
Union[FailureHandlingConfig, Dict[str, Any]]
] = None,
@@ -380,6 +393,9 @@ class Workforce(BaseNode):
self.snapshot_interval: float = 30.0
# Shared memory UUID tracking to prevent re-sharing duplicates
self._shared_memory_uuids: Set[str] = set()
+ # Optional user callback for worker streaming text chunks.
+ self._user_stream_callback = stream_callback
+ self._stream_progress: Dict[Tuple[str, str], str] = {}
self._initialize_callbacks(callbacks)
# Set up coordinator agent with default system message
@@ -535,6 +551,7 @@ class Workforce(BaseNode):
# Shared context utility for workflow management (created lazily)
self._shared_context_utility: Optional["ContextUtility"] = None
+ self._sync_child_stream_callbacks()
# ------------------------------------------------------------------
# Helper for propagating pause control to externally supplied agents
@@ -580,6 +597,69 @@ class Workforce(BaseNode):
for child in self._children:
self._notify_worker_created(child)
+ def _sync_child_stream_callbacks(self) -> None:
+ r"""Propagate stream callback settings to all child nodes."""
+ for child in self._children:
+ if isinstance(child, Worker):
+ child.set_stream_callback(self._on_worker_stream_chunk)
+ elif isinstance(child, Workforce):
+ child.set_stream_callback(self._user_stream_callback)
+
+ def set_stream_callback(
+ self,
+ stream_callback: Optional[
+ Callable[[str, str, str, str], Optional[Awaitable[None]]]
+ ],
+ ) -> Workforce:
+ r"""Set callback for real-time worker streaming output chunks.
+
+ Callback arguments:
+ worker_id (str): ID of the worker emitting this chunk.
+ task_id (str): ID of the task currently processed.
+ text (str): Incremental text content for this chunk.
+ mode (str): Chunk mode reported by model ("delta"/"accumulate").
+ """
+ self._user_stream_callback = stream_callback
+ self._stream_progress.clear()
+ self._sync_child_stream_callbacks()
+ return self
+
+ async def _on_worker_stream_chunk(
+ self, chunk: "ChatAgentResponse", worker_id: str, task_id: str
+ ) -> None:
+ r"""Normalize worker stream chunks and dispatch to user callback."""
+ if self._user_stream_callback is None:
+ return
+
+ if chunk.msg is None or chunk.msg.content is None:
+ return
+
+ mode = "accumulate"
+ if chunk.info:
+ mode = chunk.info.get("stream_accumulate_mode", mode)
+ if "mode" in chunk.info:
+ mode = chunk.info["mode"]
+
+ content = chunk.msg.content
+ key = (worker_id, task_id)
+
+ if mode == "accumulate":
+ previous = self._stream_progress.get(key, "")
+ if content.startswith(previous):
+ text = content[len(previous) :]
+ else:
+ text = content
+ self._stream_progress[key] = content
+ else:
+ text = content
+
+ if not text:
+ return
+
+ maybe = self._user_stream_callback(worker_id, task_id, text, mode)
+ if asyncio.iscoroutine(maybe):
+ await maybe
+
def _notify_worker_created(
self,
worker_node: BaseNode,
@@ -1190,6 +1270,24 @@ class Workforce(BaseNode):
return shared_memory
+ def _on_stream_callback(self, chunk: ChatAgentResponse) -> None:
+ # TODO (transitional): This is a local, best-effort stream speed
+ # logger used only until streaming telemetry is moved into
+ # WorkforceCallback. In a follow-up PR, replace this direct logging
+ # with callback-driven events/metrics and remove this helper path.
+ now = time.monotonic()
+ if not hasattr(self, "_stream_start_ts"):
+ self._stream_start_ts = now
+ self._stream_chars = 0
+ content = chunk.msg.content or ""
+ self._stream_chars += len(content)
+ elapsed = now - self._stream_start_ts
+ if elapsed > 0:
+ speed = self._stream_chars / elapsed # chars per second
+ logger.info(
+ f"Stream Speed: {speed:.1f} chars/s (elapsed {elapsed:.2f}s)"
+ )
+
def _share_memory_with_agents(
self, shared_memory: Dict[str, List]
) -> None:
@@ -1368,6 +1466,11 @@ class Workforce(BaseNode):
if task_id in self._assignees:
del self._assignees[task_id]
+ # Clean streaming progress for completed/failed task chunks.
+ stale_keys = [k for k in self._stream_progress if k[1] == task_id]
+ for key in stale_keys:
+ del self._stream_progress[key]
+
def _decompose_task(
self,
task: Task,
@@ -1605,9 +1708,12 @@ class Workforce(BaseNode):
self.task_agent.reset()
response = self.task_agent.step(enhanced_prompt)
+ response, response_content = consume_response_content(
+ response, self._on_stream_callback
+ )
result = self.structured_handler.parse_structured_response(
- response.msg.content if response.msg else "",
+ response_content,
schema=result_schema,
fallback_values=fallback_values,
)
@@ -1624,6 +1730,9 @@ class Workforce(BaseNode):
response = self.task_agent.step(
analysis_prompt, response_format=result_schema
)
+ response, _ = consume_response_content(
+ response, self._on_stream_callback
+ )
parsed = safe_extract_parsed(response, result_schema)
if parsed is not None:
return parsed
@@ -1773,7 +1882,9 @@ class Workforce(BaseNode):
logger.info(
f"Task {task.id} will be decomposed due to {reason}"
)
- subtasks_result = self._decompose_task(task)
+ subtasks_result = self._decompose_task(
+ task, self._on_stream_callback
+ )
# Handle both streaming and non-streaming results
if isinstance(subtasks_result, Generator):
@@ -2439,7 +2550,7 @@ class Workforce(BaseNode):
# The agent tend to be overconfident on the whole task, so we
# decompose the task into subtasks first
- subtasks_result = self._decompose_task(task)
+ subtasks_result = self._decompose_task(task, self._on_stream_callback)
# Handle both streaming and non-streaming results
if isinstance(subtasks_result, Generator):
@@ -2890,6 +3001,7 @@ class Workforce(BaseNode):
enable_workflow_memory=enable_workflow_memory,
)
self._children.append(worker_node)
+ self._sync_child_stream_callbacks()
# If we have a channel set up, set it for the new worker
if hasattr(self, '_channel') and self._channel is not None:
@@ -2967,6 +3079,7 @@ class Workforce(BaseNode):
use_structured_output_handler=self.use_structured_output_handler,
)
self._children.append(worker_node)
+ self._sync_child_stream_callbacks()
# If we have a channel set up, set it for the new worker
if hasattr(self, '_channel') and self._channel is not None:
@@ -3003,6 +3116,7 @@ class Workforce(BaseNode):
# control of worker agents only.
workforce._pause_event = self._pause_event
self._children.append(workforce)
+ self._sync_child_stream_callbacks()
# If we have a channel set up, set it for the new workforce
if hasattr(self, '_channel') and self._channel is not None:
@@ -3640,8 +3754,11 @@ class Workforce(BaseNode):
# Get response without structured format
response = self.coordinator_agent.step(enhanced_prompt)
+ response, response_content = consume_response_content(
+ response, self._on_stream_callback
+ )
- if response.msg is None or response.msg.content is None:
+ if response.msg is None or response_content is None:
logger.error(
"Coordinator agent returned empty response for "
"task assignment"
@@ -3650,7 +3767,7 @@ class Workforce(BaseNode):
# Parse with structured handler
result = self.structured_handler.parse_structured_response(
- response.msg.content,
+ response_content,
schema=TaskAssignResult,
fallback_values={"assignments": []},
)
@@ -3666,8 +3783,11 @@ class Workforce(BaseNode):
response = self.coordinator_agent.step(
prompt, response_format=TaskAssignResult
)
+ response, response_content = consume_response_content(
+ response, self._on_stream_callback
+ )
- if response.msg is None or response.msg.content is None:
+ if response.msg is None or response_content is None:
logger.error(
"Coordinator agent returned empty response for "
"task assignment"
@@ -3675,13 +3795,13 @@ class Workforce(BaseNode):
return TaskAssignResult(assignments=[])
try:
- result_dict = json.loads(response.msg.content, parse_int=str)
+ result_dict = json.loads(response_content, parse_int=str)
return TaskAssignResult(**result_dict)
except json.JSONDecodeError as e:
logger.error(
f"JSON parsing error in task assignment: Invalid response "
f"format - {e}. Response content: "
- f"{response.msg.content}"
+ f"{response_content}"
)
return TaskAssignResult(assignments=[])
@@ -4024,8 +4144,11 @@ class Workforce(BaseNode):
)
response = self.coordinator_agent.step(enhanced_prompt)
+ response, response_content = await consume_response_content_async(
+ response, self._on_stream_callback
+ )
- if response.msg is None or response.msg.content is None:
+ if response.msg is None or response_content is None:
logger.error(
"Coordinator agent returned empty response for "
"worker creation"
@@ -4038,7 +4161,7 @@ class Workforce(BaseNode):
)
else:
result = self.structured_handler.parse_structured_response(
- response.msg.content,
+ response_content,
schema=WorkerConf,
fallback_values={
"description": f"Worker for task: {task.content}",
@@ -4062,7 +4185,10 @@ class Workforce(BaseNode):
response = self.coordinator_agent.step(
prompt, response_format=WorkerConf
)
- if response.msg is None or response.msg.content is None:
+ response, response_content = await consume_response_content_async(
+ response, self._on_stream_callback
+ )
+ if response.msg is None or response_content is None:
logger.error(
"Coordinator agent returned empty response for "
"worker creation"
@@ -4076,13 +4202,13 @@ class Workforce(BaseNode):
)
else:
try:
- result_dict = json.loads(response.msg.content)
+ result_dict = json.loads(response_content)
new_node_conf = WorkerConf(**result_dict)
except json.JSONDecodeError as e:
logger.error(
f"JSON parsing error in worker creation: Invalid "
f"response format - {e}. Response content: "
- f"{response.msg.content}"
+ f"{response_content}"
)
raise RuntimeError(
f"Failed to create worker for task {task.id}: "
@@ -4118,6 +4244,7 @@ class Workforce(BaseNode):
)
self._children.append(new_node)
+ self._sync_child_stream_callbacks()
self._notify_worker_created(
new_node,
diff --git a/camel/tasks/task.py b/camel/tasks/task.py
index 63f91c202..61d1d6268 100644
--- a/camel/tasks/task.py
+++ b/camel/tasks/task.py
@@ -14,7 +14,6 @@
import re
from enum import Enum
-from types import GeneratorType
from typing import (
TYPE_CHECKING,
Any,
@@ -447,18 +446,10 @@ class Task(BaseModel):
)
response = agent.step(msg)
- # Auto-detect streaming based on response type
- from camel.agents.chat_agent import StreamingChatAgentResponse
+ # Auto-detect streaming based on response type (centralized helper)
+ from camel.utils import is_streaming_response
- is_streaming = isinstance(
- response, StreamingChatAgentResponse
- ) or isinstance(response, GeneratorType)
- if (
- not is_streaming
- and hasattr(response, "__iter__")
- and not hasattr(response, "msg")
- ):
- is_streaming = True
+ is_streaming = is_streaming_response(response)
if is_streaming:
return self._decompose_streaming(
@@ -491,7 +482,19 @@ class Task(BaseModel):
# Process streaming response
for chunk in response:
- accumulated_content = chunk.msg.content
+ # Determine whether this chunk is accumulated or delta
+ stream_accumulate_mode = None
+ if getattr(chunk, "info", None):
+ stream_accumulate_mode = chunk.info.get(
+ "stream_accumulate_mode"
+ )
+
+ if stream_accumulate_mode == "delta":
+ accumulated_content += chunk.msg.content
+ else:
+ # Default (accumulate mode): chunk already contains full text
+ accumulated_content = chunk.msg.content
+
if stream_callback:
try:
stream_callback(chunk)
diff --git a/camel/utils/__init__.py b/camel/utils/__init__.py
index f285f2dc3..336e13665 100644
--- a/camel/utils/__init__.py
+++ b/camel/utils/__init__.py
@@ -57,6 +57,11 @@ from .langfuse import (
)
from .mcp import MCPServer
from .response_format import get_pydantic_model, model_from_json_schema
+from .stream_utils import (
+ consume_response_content,
+ consume_response_content_async,
+ is_streaming_response,
+)
from .token_counting import (
AnthropicTokenCounter,
BaseTokenCounter,
@@ -118,4 +123,7 @@ __all__ = [
"get_langfuse_status",
"get_current_agent_id",
"set_current_agent_id",
+ "is_streaming_response",
+ "consume_response_content",
+ "consume_response_content_async",
]
diff --git a/camel/utils/stream_utils.py b/camel/utils/stream_utils.py
new file mode 100644
index 000000000..06dbf48a2
--- /dev/null
+++ b/camel/utils/stream_utils.py
@@ -0,0 +1,236 @@
+# ========= Copyright 2023-2026 @ 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-2026 @ CAMEL-AI.org. All Rights Reserved. =========
+"""Utilities to detect and normalize streaming chat responses."""
+
+from __future__ import annotations
+
+import asyncio
+from types import GeneratorType
+from typing import TYPE_CHECKING, Any, Awaitable, Callable, Optional, Tuple
+
+from camel.logger import get_logger
+
+if TYPE_CHECKING:
+ from camel.responses import ChatAgentResponse
+
+logger = get_logger(__name__)
+
+
+def _is_cumulative(contents: list[str]) -> bool:
+ """Return True if each string is a prefix of the next one.
+
+ Streaming heuristic:
+ - Gather non-empty ``chunk.msg.content`` pieces.
+ - If each later piece strictly grows in length and starts with the
+ previous, treat as cumulative (covers ``stream_accumulate=True``) and
+ use the last piece.
+ - Otherwise concatenate pieces in order (covers delta streaming).
+ """
+
+ return all(
+ len(contents[i + 1]) > len(contents[i])
+ and contents[i + 1].startswith(contents[i])
+ for i in range(len(contents) - 1)
+ )
+
+
+def _finalize_stream(
+ contents: list[str], final_response: Optional[ChatAgentResponse]
+) -> Tuple[ChatAgentResponse, str]:
+ """Normalize collected chunks into ``(final_response, content)``."""
+
+ from camel.responses import (
+ ChatAgentResponse, # local import to avoid cycles
+ )
+
+ if final_response is None:
+ final_response = ChatAgentResponse(msgs=[], terminated=False, info={})
+
+ if not contents:
+ return final_response, ""
+
+ if len(contents) == 1:
+ return final_response, contents[0]
+
+ if _is_cumulative(contents):
+ return final_response, contents[-1]
+
+ return final_response, "".join(contents)
+
+
+def _collect_sync_stream(
+ response: Any,
+ stream_callback: Optional[Callable[[ChatAgentResponse], None]] = None,
+) -> Tuple[Optional[ChatAgentResponse], list[str]]:
+ """Collect chunks from a sync streaming response."""
+
+ final_response: Optional[ChatAgentResponse] = None
+ contents: list[str] = []
+
+ for chunk in response:
+ final_response = chunk
+ if stream_callback:
+ try:
+ stream_callback(chunk)
+ except Exception:
+ logger.warning(
+ "stream_callback failed while processing a stream chunk",
+ exc_info=True,
+ )
+ if getattr(chunk, "msg", None) and chunk.msg.content is not None:
+ contents.append(chunk.msg.content)
+
+ return final_response, contents
+
+
+async def _collect_async_stream(
+ response: Any,
+ stream_callback: Optional[
+ Callable[[ChatAgentResponse], Optional[Awaitable[None]]]
+ ] = None,
+) -> Tuple[Optional[ChatAgentResponse], list[str]]:
+ """Collect chunks from an async streaming response."""
+
+ final_response: Optional[ChatAgentResponse] = None
+ contents: list[str] = []
+
+ from camel.agents.chat_agent import AsyncStreamingChatAgentResponse
+
+ async def _handle_chunk(chunk: Any) -> None:
+ nonlocal final_response
+ final_response = chunk
+ if stream_callback:
+ try:
+ maybe = stream_callback(chunk)
+ if asyncio.iscoroutine(maybe):
+ await maybe
+ except Exception:
+ logger.warning(
+ "stream_callback failed while processing a stream chunk",
+ exc_info=True,
+ )
+ if getattr(chunk, "msg", None) and chunk.msg.content is not None:
+ contents.append(chunk.msg.content)
+
+ if isinstance(response, AsyncStreamingChatAgentResponse):
+ async for chunk in response:
+ await _handle_chunk(chunk)
+ else:
+ for chunk in response:
+ await _handle_chunk(chunk)
+
+ return final_response, contents
+
+
+def is_streaming_response(response: Any) -> bool:
+ """Return True when the object represents a streaming sync response.
+
+ Args:
+ response: Any object that might represent a chat response. Typically
+ a ``StreamingChatAgentResponse`` instance, a generator of
+ response chunks, or any iterable that does not expose a
+ ``msg`` attribute.
+
+ Returns:
+ bool: ``True`` if the object is detected as streaming according
+ to the heuristics above, otherwise ``False``.
+ """
+ from camel.agents.chat_agent import (
+ AsyncStreamingChatAgentResponse,
+ StreamingChatAgentResponse,
+ )
+
+ non_streaming_iterables = (str, bytes, bytearray, dict)
+ is_streaming = (
+ isinstance(response, StreamingChatAgentResponse)
+ or isinstance(response, AsyncStreamingChatAgentResponse)
+ or isinstance(response, GeneratorType)
+ )
+ if (
+ not is_streaming
+ and hasattr(response, "__iter__")
+ and not hasattr(response, "msg")
+ and not isinstance(response, non_streaming_iterables)
+ ):
+ is_streaming = True
+
+ return is_streaming
+
+
+def consume_response_content(
+ response: Any,
+ stream_callback: Optional[Callable[[ChatAgentResponse], None]] = None,
+) -> Tuple[Any, str]:
+ """Consume a sync response (streaming or not) into final_response, content.
+
+ Args:
+ response: A complete ``ChatAgentResponse`` or a streaming
+ iterable of partial responses/chunks.
+ stream_callback: Optional callable executed for each chunk as it
+ is produced. The callback receives the current chunk and is
+ intended for side effects such as logging or UI updates.
+
+ Returns:
+ Tuple[Any, str]: The last chunk observed (or an empty
+ ``ChatAgentResponse`` placeholder) and the assembled message
+ content string derived from the streamed pieces.
+ """
+ from camel.agents.chat_agent import AsyncStreamingChatAgentResponse
+
+ if isinstance(response, AsyncStreamingChatAgentResponse):
+ try:
+ asyncio.get_running_loop()
+ except RuntimeError:
+ # No running loop; safely consume async stream synchronously.
+ return asyncio.run(
+ consume_response_content_async(response, stream_callback)
+ )
+ raise RuntimeError(
+ "consume_response_content received an "
+ "AsyncStreamingChatAgentResponse while an event loop is running; "
+ "use await consume_response_content_async instead."
+ )
+
+ if not is_streaming_response(response):
+ content = (
+ response.msg.content if getattr(response, "msg", None) else ""
+ )
+ return response, content
+
+ final_response, contents = _collect_sync_stream(response, stream_callback)
+ return _finalize_stream(contents, final_response)
+
+
+async def consume_response_content_async(
+ response: Any,
+ stream_callback: Optional[
+ Callable[[ChatAgentResponse], Optional[Awaitable[None]]]
+ ] = None,
+) -> Tuple[Any, str]:
+ """Async-friendly variant of :func:`consume_response_content`.
+
+ This version handles ``AsyncStreamingChatAgentResponse`` objects using
+ ``async for`` while retaining the same return contract.
+ """
+
+ if not is_streaming_response(response):
+ content = (
+ response.msg.content if getattr(response, "msg", None) else ""
+ )
+ return response, content
+
+ final_response, contents = await _collect_async_stream(
+ response, stream_callback
+ )
+ return _finalize_stream(contents, final_response)
diff --git a/examples/workforce/workforce_callbacks_example.py b/examples/workforce/workforce_callbacks_example.py
index 4ace9e042..031b168f9 100644
--- a/examples/workforce/workforce_callbacks_example.py
+++ b/examples/workforce/workforce_callbacks_example.py
@@ -20,11 +20,14 @@ Includes:
`Workforce`.
"""
-import asyncio
+import json
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 FailureHandlingConfig
from camel.societies.workforce.events import (
AllTasksCompletedEvent,
LogEvent,
@@ -41,14 +44,44 @@ from camel.societies.workforce.events import (
from camel.societies.workforce.workforce import Workforce
from camel.societies.workforce.workforce_callback import WorkforceCallback
from camel.societies.workforce.workforce_logger import WorkforceLogger
+from camel.tasks import Task
from camel.types import ModelPlatformType, ModelType
logger = get_logger(__name__)
+def _extract_structured_content(raw_text: str) -> str:
+ r"""Extract ``content`` from structured-output JSON when possible."""
+ text = raw_text.strip()
+ if not text:
+ return ""
+
+ candidates = [text]
+ if text.startswith("```"):
+ lines = text.splitlines()
+ if len(lines) >= 3 and lines[-1].strip() == "```":
+ body = "\n".join(lines[1:-1]).strip()
+ if body.startswith("json"):
+ body = body[4:].lstrip()
+ candidates.insert(0, body)
+
+ for candidate in candidates:
+ try:
+ data = json.loads(candidate)
+ except json.JSONDecodeError:
+ continue
+ if isinstance(data, dict) and isinstance(data.get("content"), str):
+ return data["content"]
+
+ return text
+
+
class PrintCallback(WorkforceCallback):
r"""Simple callback printing events to logs to observe ordering."""
+ def __init__(self, stream_buffers: dict[tuple[str, str], list[str]]):
+ self._stream_buffers = stream_buffers
+
def log_message(self, event: LogEvent) -> None:
print(
f"[PrintCallback] {event.message} level={event.level}, "
@@ -90,6 +123,14 @@ class PrintCallback(WorkforceCallback):
f"[PrintCallback] task_completed: task={event.task_id}, "
f"worker={event.worker_id}, took={event.processing_time_seconds}s"
)
+ key = (event.worker_id, event.task_id)
+ chunks = self._stream_buffers.pop(key, [])
+ if chunks:
+ merged_text = "".join(chunks).strip()
+ if merged_text:
+ content = _extract_structured_content(merged_text)
+ print(f"\n[StreamMerged][{event.worker_id}][{event.task_id}]")
+ print(content)
def log_task_failed(self, event: TaskFailedEvent) -> None:
logger.warning(
@@ -113,48 +154,88 @@ class PrintCallback(WorkforceCallback):
print("[PrintCallback] all_tasks_completed")
-def build_teacher_agent() -> ChatAgent:
+def main() -> None:
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
+ model_config_dict=ChatGPTConfig(stream=True).as_dict(),
+ )
+
+ search_agent = ChatAgent(
+ system_message=BaseMessage.make_assistant_message(
+ role_name="Research Specialist",
+ content="You are a research specialist who excels at finding and "
+ "gathering information from the web.",
+ ),
+ model=model,
)
- return ChatAgent(system_message="You are a teacher", model=model)
+ analyst_agent = ChatAgent(
+ system_message=BaseMessage.make_assistant_message(
+ role_name="Business Analyst",
+ content="You are an expert business analyst. Your job is "
+ "to analyze research findings, identify key insights, "
+ "opportunities, and challenges.",
+ ),
+ model=model,
+ )
-def build_student_agent() -> ChatAgent:
- model = ModelFactory.create(
- model_platform=ModelPlatformType.OPENAI,
- model_type=ModelType.GPT_4O_MINI,
+ writer_agent = ChatAgent(
+ system_message=BaseMessage.make_assistant_message(
+ role_name="Report Writer",
+ content="You are a professional report writer. You take "
+ "analytical insights and synthesize them into a clear, "
+ "concise, and well-structured final report.",
+ ),
+ model=model,
)
- return ChatAgent(system_message="You are a student", model=model)
+ # Buffer stream chunks per worker/task and print once when task completes.
+ stream_buffers: dict[tuple[str, str], list[str]] = {}
-async def run_demo() -> None:
- logger_cb = WorkforceLogger('demo-logger')
- print_cb = PrintCallback()
- callbacks = [logger_cb, print_cb]
+ def on_worker_stream(
+ worker_id: str, task_id: str, text: str, _mode: str
+ ) -> None:
+ if not text:
+ return
+ key = (worker_id, task_id)
+ stream_buffers.setdefault(key, []).append(text)
workforce = Workforce(
"Workforce Callbacks Demo",
- callbacks=callbacks,
+ callbacks=[
+ WorkforceLogger('demo-logger'),
+ PrintCallback(stream_buffers),
+ ],
+ stream_callback=on_worker_stream,
use_structured_output_handler=True,
+ failure_handling_config=FailureHandlingConfig(enabled_strategies=[]),
+ default_model=model,
+ )
+
+ workforce.add_single_agent_worker(
+ "A researcher who can search online for information.",
+ worker=search_agent,
+ ).add_single_agent_worker(
+ "An analyst who can process research findings.", worker=analyst_agent
+ ).add_single_agent_worker(
+ "A writer who can create a final report from the analysis.",
+ worker=writer_agent,
)
- teacher = build_teacher_agent()
- student = build_student_agent()
- workforce.add_single_agent_worker("Teacher Worker", teacher)
- workforce.add_single_agent_worker("Student Worker", student)
- workforce.add_main_task(
- "The teacher set an exam question and had the students answer it."
+ # Use a simpler task to ensure fast and deterministic execution
+ human_task = Task(
+ content=(
+ "Create a simple report about electric scooters. "
+ "The report should have three sections: "
+ "1. Market overview "
+ "2. Target customers "
+ "3. Summary"
+ ),
+ id='0',
)
- # Start Workforce and wait for completion (timeout to avoid hanging)
- wf_task = asyncio.create_task(workforce.start())
- try:
- await asyncio.wait_for(wf_task, timeout=30.0)
- except asyncio.TimeoutError:
- logger.warning("Workforce run timed out; stopping...")
- workforce.stop()
+ workforce.process_task(human_task)
# Read KPIs and a simple "tree"
print(f"KPIs: {workforce.get_workforce_kpis()}")
@@ -162,4 +243,4 @@ async def run_demo() -> None:
if __name__ == "__main__":
- asyncio.run(run_demo())
+ main()
diff --git a/test/utils/test_stream_utils.py b/test/utils/test_stream_utils.py
new file mode 100644
index 000000000..0e25d191d
--- /dev/null
+++ b/test/utils/test_stream_utils.py
@@ -0,0 +1,257 @@
+# ========= Copyright 2023-2026 @ 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-2026 @ CAMEL-AI.org. All Rights Reserved. =========
+
+from __future__ import annotations
+
+import asyncio
+
+import pytest
+
+from camel.agents.chat_agent import (
+ AsyncStreamingChatAgentResponse,
+ StreamingChatAgentResponse,
+)
+from camel.messages import BaseMessage
+from camel.responses import ChatAgentResponse
+from camel.utils.stream_utils import (
+ consume_response_content,
+ consume_response_content_async,
+ is_streaming_response,
+)
+
+
+def _make_chunk(content: str | None) -> ChatAgentResponse:
+ if content is None:
+ return ChatAgentResponse(msgs=[], terminated=False, info={})
+ return ChatAgentResponse(
+ msgs=[BaseMessage.make_assistant_message("assistant", content)],
+ terminated=False,
+ info={},
+ )
+
+
+def _sync_stream(chunks: list[ChatAgentResponse]):
+ for chunk in chunks:
+ yield chunk
+
+
+async def _async_stream(chunks: list[ChatAgentResponse]):
+ for chunk in chunks:
+ yield chunk
+
+
+class _IterableNoMsg:
+ def __iter__(self):
+ yield _make_chunk("x")
+
+
+def test_is_streaming_response_for_standard_types():
+ assert is_streaming_response(_sync_stream([_make_chunk("x")]))
+ assert not is_streaming_response(_make_chunk("x"))
+ assert not is_streaming_response("plain string")
+ assert not is_streaming_response({"k": "v"})
+
+
+def test_is_streaming_response_for_stream_wrappers_and_iterable():
+ sync_wrapped = StreamingChatAgentResponse(_sync_stream([_make_chunk("x")]))
+ async_wrapped = AsyncStreamingChatAgentResponse(
+ _async_stream([_make_chunk("x")])
+ )
+
+ assert is_streaming_response(sync_wrapped)
+ assert is_streaming_response(async_wrapped)
+ assert is_streaming_response(_IterableNoMsg())
+
+
+def test_consume_response_content_non_streaming_response():
+ response = _make_chunk("final answer")
+
+ final_response, content = consume_response_content(response)
+
+ assert final_response is response
+ assert content == "final answer"
+
+
+def test_consume_response_content_single_chunk_stream():
+ final_response, content = consume_response_content(
+ _sync_stream([_make_chunk("hello")])
+ )
+
+ assert content == "hello"
+ assert final_response.msg.content == "hello"
+
+
+def test_consume_response_content_cumulative_stream_uses_last_piece():
+ chunks = [_make_chunk("H"), _make_chunk("He"), _make_chunk("Hello")]
+
+ final_response, content = consume_response_content(_sync_stream(chunks))
+
+ assert final_response.msg.content == "Hello"
+ assert content == "Hello"
+
+
+def test_consume_response_content_delta_stream_concatenates():
+ chunks = [_make_chunk("He"), _make_chunk("llo"), _make_chunk("!")]
+
+ final_response, content = consume_response_content(_sync_stream(chunks))
+
+ assert final_response.msg.content == "!"
+ assert content == "Hello!"
+
+
+def test_consume_response_content_delta_stream_with_repeated_prefixes():
+ chunks = [
+ _make_chunk("Hello!"),
+ _make_chunk("Hello!"),
+ _make_chunk("Hello!"),
+ ]
+
+ final_response, content = consume_response_content(_sync_stream(chunks))
+
+ assert final_response.msg.content == "Hello!"
+ assert content == "Hello!Hello!Hello!"
+
+
+def test_consume_response_content_empty_stream_returns_placeholder():
+ final_response, content = consume_response_content(_sync_stream([]))
+
+ assert isinstance(final_response, ChatAgentResponse)
+ assert final_response.msgs == []
+ assert final_response.terminated is False
+ assert final_response.info == {}
+ assert content == ""
+
+
+def test_consume_response_content_callback_runs_for_every_chunk():
+ chunks = [_make_chunk("a"), _make_chunk(None), _make_chunk("b")]
+ seen = []
+
+ def callback(chunk: ChatAgentResponse) -> None:
+ seen.append(chunk)
+
+ final_response, content = consume_response_content(
+ _sync_stream(chunks), callback
+ )
+
+ assert len(seen) == 3
+ assert final_response is chunks[-1]
+ assert content == "ab"
+
+
+def test_consume_response_content_callback_exception_is_swallowed(caplog):
+ def bad_callback(_: ChatAgentResponse) -> None:
+ raise RuntimeError("callback failed")
+
+ final_response, content = consume_response_content(
+ _sync_stream([_make_chunk("x")]), bad_callback
+ )
+
+ assert final_response.msg.content == "x"
+ assert content == "x"
+ assert (
+ "stream_callback failed while processing a stream chunk" in caplog.text
+ )
+
+
+def test_consume_response_content_sync_consumes_async_stream_without_loop():
+ async_wrapped = AsyncStreamingChatAgentResponse(
+ _async_stream([_make_chunk("H"), _make_chunk("Hi")])
+ )
+
+ final_response, content = consume_response_content(async_wrapped)
+
+ assert final_response.msg.content == "Hi"
+ assert content == "Hi"
+
+
+async def test_consume_response_content_raises_for_async_stream_in_loop():
+ async_wrapped = AsyncStreamingChatAgentResponse(
+ _async_stream([_make_chunk("x")])
+ )
+
+ with pytest.raises(
+ RuntimeError, match="use await consume_response_content_async"
+ ):
+ consume_response_content(async_wrapped)
+
+
+async def test_consume_response_content_async_non_streaming_response():
+ response = _make_chunk("non streaming")
+
+ final_response, content = await consume_response_content_async(response)
+
+ assert final_response is response
+ assert content == "non streaming"
+
+
+async def test_consume_response_content_async_with_sync_iterable_stream():
+ chunks = [_make_chunk("foo"), _make_chunk("bar")]
+
+ final_response, content = await consume_response_content_async(chunks)
+
+ assert final_response is chunks[-1]
+ assert content == "foobar"
+
+
+async def test_consume_response_content_async_with_async_stream_wrapper():
+ async_wrapped = AsyncStreamingChatAgentResponse(
+ _async_stream([_make_chunk("a"), _make_chunk("ab")])
+ )
+
+ final_response, content = await consume_response_content_async(
+ async_wrapped
+ )
+
+ assert final_response.msg.content == "ab"
+ assert content == "ab"
+
+
+async def test_consume_response_content_async_callback_runs_for_every_chunk():
+ chunks = [_make_chunk("a"), _make_chunk(None), _make_chunk("b")]
+ seen = []
+
+ async def callback(chunk: ChatAgentResponse) -> None:
+ await asyncio.sleep(0)
+ seen.append(chunk)
+
+ final_response, content = await consume_response_content_async(
+ chunks, callback
+ )
+
+ assert len(seen) == 3
+ assert final_response is chunks[-1]
+ assert content == "ab"
+
+
+async def test_consume_response_content_async_callback_exception_is_swallowed(
+ caplog,
+):
+ async def bad_callback(_: ChatAgentResponse) -> None:
+ raise RuntimeError("callback failed")
+
+ final_response, content = await consume_response_content_async(
+ [_make_chunk("x")], bad_callback
+ )
+
+ assert final_response.msg.content == "x"
+ assert content == "x"
+ assert (
+ "stream_callback failed while processing a stream chunk" in caplog.text
+ )
diff --git a/test/workforce/test_workflow_memory.py b/test/workforce/test_workflow_memory.py
index fda784c20..c2dfbf98d 100644
--- a/test/workforce/test_workflow_memory.py
+++ b/test/workforce/test_workflow_memory.py
@@ -14,7 +14,7 @@
import os
import tempfile
-from typing import List
+from typing import TYPE_CHECKING, Awaitable, Callable, List, Optional
from unittest.mock import MagicMock, patch
import pytest
@@ -25,6 +25,9 @@ from camel.societies.workforce import Workforce
from camel.societies.workforce.single_agent_worker import SingleAgentWorker
from camel.tasks.task import Task, TaskState
+if TYPE_CHECKING:
+ from camel.responses import ChatAgentResponse
+
@pytest.fixture(autouse=True)
def mock_model_backend():
@@ -70,7 +73,12 @@ class MockSingleAgentWorker(SingleAgentWorker):
super().__init__(description, agent)
async def _process_task(
- self, task: Task, dependencies: List[Task]
+ self,
+ task: Task,
+ dependencies: List[Task],
+ stream_callback: Optional[
+ Callable[["ChatAgentResponse"], Optional[Awaitable[None]]]
+ ] = None,
) -> TaskState:
"""Mock task processing with simulated conversation."""
# Simulate some conversation history
diff --git a/test/workforce/test_workforce_pipeline.py b/test/workforce/test_workforce_pipeline.py
index 4c0410a12..70f275875 100644
--- a/test/workforce/test_workforce_pipeline.py
+++ b/test/workforce/test_workforce_pipeline.py
@@ -13,7 +13,7 @@
# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
import os
-from typing import List
+from typing import TYPE_CHECKING, Awaitable, Callable, List, Optional
import pytest
@@ -26,6 +26,9 @@ from camel.societies.workforce.workforce import WorkforceMode
from camel.tasks.task import Task, TaskState
from camel.types import ModelPlatformType, ModelType
+if TYPE_CHECKING:
+ from camel.responses import ChatAgentResponse
+
class SuccessfulWorker(SingleAgentWorker):
"""A worker that always succeeds for testing purposes."""
@@ -40,7 +43,12 @@ class SuccessfulWorker(SingleAgentWorker):
self.result_suffix = result_suffix
async def _process_task(
- self, task: Task, dependencies: List[Task]
+ self,
+ task: Task,
+ dependencies: List[Task],
+ stream_callback: Optional[
+ Callable[["ChatAgentResponse"], Optional[Awaitable[None]]]
+ ] = None,
) -> TaskState:
"""Always return successful task with dependency info."""
# Include dependency results in the output
@@ -67,7 +75,12 @@ class FailingWorker(SingleAgentWorker):
super().__init__(description, agent)
async def _process_task(
- self, task: Task, dependencies: List[Task]
+ self,
+ task: Task,
+ dependencies: List[Task],
+ stream_callback: Optional[
+ Callable[["ChatAgentResponse"], Optional[Awaitable[None]]]
+ ] = None,
) -> TaskState:
"""Always return failed task."""
task.state = TaskState.FAILED
diff --git a/test/workforce/test_workforce_single_agent.py b/test/workforce/test_workforce_single_agent.py
index 3986a6fe8..f09ed3f8b 100644
--- a/test/workforce/test_workforce_single_agent.py
+++ b/test/workforce/test_workforce_single_agent.py
@@ -12,13 +12,14 @@
# limitations under the License.
# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
import time
-from typing import List
+from typing import Awaitable, Callable, List, Optional
from unittest.mock import patch
import pytest
from camel.agents.chat_agent import ChatAgent
from camel.messages.base import BaseMessage
+from camel.responses import ChatAgentResponse
from camel.societies.workforce import Workforce
from camel.societies.workforce.single_agent_worker import SingleAgentWorker
from camel.tasks.task import Task, TaskState
@@ -36,7 +37,12 @@ class AlwaysFailingWorker(SingleAgentWorker):
super().__init__(description, agent)
async def _process_task(
- self, task: Task, dependencies: List[Task]
+ self,
+ task: Task,
+ dependencies: List[Task],
+ stream_callback: Optional[
+ Callable[["ChatAgentResponse"], Optional[Awaitable[None]]]
+ ] = None,
) -> TaskState:
"""Always return failed task."""
task.state = TaskState.FAILED
@@ -44,6 +50,48 @@ class AlwaysFailingWorker(SingleAgentWorker):
return TaskState.FAILED
+class StreamingProbeWorker(SingleAgentWorker):
+ """A worker that emits one artificial streaming chunk."""
+
+ def __init__(self, description: str):
+ sys_msg = BaseMessage.make_assistant_message(
+ role_name="Streaming Probe",
+ content="Emit one stream chunk.",
+ )
+ agent = ChatAgent(sys_msg)
+ super().__init__(description, agent)
+
+ async def _process_task(
+ self,
+ task: Task,
+ dependencies: List[Task],
+ stream_callback: Optional[
+ Callable[["ChatAgentResponse"], Optional[Awaitable[None]]]
+ ] = None,
+ ) -> TaskState:
+ chunk = ChatAgentResponse(
+ msgs=[
+ BaseMessage.make_assistant_message(
+ role_name="Assistant", content="partial"
+ )
+ ],
+ terminated=False,
+ info={"stream_accumulate_mode": "delta"},
+ )
+ if stream_callback:
+ maybe = stream_callback(chunk)
+ if maybe is not None:
+ await maybe
+ task.state = TaskState.DONE
+ task.result = "ok"
+ return TaskState.DONE
+
+
+class _DummyChannel:
+ async def return_task(self, task_id: str) -> None:
+ self.last_task_id = task_id
+
+
@pytest.mark.asyncio
async def test_graceful_shutdown_immediate_timeout():
"""Test that 0 timeout causes immediate shutdown."""
@@ -135,3 +183,80 @@ async def test_get_dep_tasks_info(mock_process_task, mock_decompose):
# Verify the mocks were called
mock_decompose.assert_called_once_with(agent)
mock_process_task.assert_called_once_with(human_task, mock_subtasks)
+
+
+async def test_worker_forwards_stream_chunks_to_registered_callback():
+ worker = StreamingProbeWorker("probe")
+ worker.set_channel(_DummyChannel())
+ received = []
+
+ async def stream_handler(chunk, worker_id, task_id):
+ received.append((worker_id, task_id, chunk.msg.content))
+
+ worker.set_stream_callback(stream_handler)
+ task = Task(content="test", id="task-stream")
+
+ await worker._process_single_task(task)
+
+ assert len(received) == 1
+ worker_id, task_id, content = received[0]
+ assert worker_id
+ assert task_id == "task-stream"
+ assert content == "partial"
+
+
+async def test_worker_stream_callback_exception_does_not_fail_task():
+ worker = StreamingProbeWorker("probe")
+ worker.set_channel(_DummyChannel())
+
+ async def failing_stream_handler(chunk, worker_id, task_id):
+ raise RuntimeError("stream push failed")
+
+ worker.set_stream_callback(failing_stream_handler)
+ task = Task(content="test", id="task-stream-fail-safe")
+
+ await worker._process_single_task(task)
+
+ assert task.state == TaskState.DONE
+ assert task.result == "ok"
+
+
+async def test_workforce_stream_callback_accumulate_mode_emits_delta():
+ chunks = []
+
+ def on_stream(worker_id: str, task_id: str, text: str, mode: str):
+ chunks.append((worker_id, task_id, text, mode))
+
+ workforce = Workforce("stream test", stream_callback=on_stream)
+
+ first_chunk = ChatAgentResponse(
+ msgs=[
+ BaseMessage.make_assistant_message(
+ role_name="Assistant",
+ content="Hello",
+ )
+ ],
+ terminated=False,
+ info={"stream_accumulate_mode": "accumulate"},
+ )
+ second_chunk = ChatAgentResponse(
+ msgs=[
+ BaseMessage.make_assistant_message(
+ role_name="Assistant",
+ content="Hello world",
+ )
+ ],
+ terminated=False,
+ info={"stream_accumulate_mode": "accumulate"},
+ )
+
+ await workforce._on_worker_stream_chunk(first_chunk, "worker-1", "task-1")
+ await workforce._on_worker_stream_chunk(second_chunk, "worker-1", "task-1")
+
+ assert chunks == [
+ ("worker-1", "task-1", "Hello", "accumulate"),
+ ("worker-1", "task-1", " world", "accumulate"),
+ ]