Skip to content

Commit 21cd1eb

Browse files
committed
update
1 parent e6f6105 commit 21cd1eb

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

camel/societies/workforce/workforce.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,9 @@ def _cleanup_task_tracking(self, task_id: str) -> None:
12681268
del self._assignees[task_id]
12691269

12701270
def _decompose_task(
1271-
self, task: Task
1271+
self,
1272+
task: Task,
1273+
stream_callback: Optional[Callable[[str], None]] = None,
12721274
) -> Union[List[Task], Generator[List[Task], None, None]]:
12731275
r"""Decompose the task into subtasks. This method will also set the
12741276
relationship between the task and its subtasks.
@@ -1293,7 +1295,9 @@ def _decompose_task(
12931295
)
12941296
)
12951297
self.task_agent.reset()
1296-
result = task.decompose(self.task_agent, decompose_prompt)
1298+
result = task.decompose(
1299+
self.task_agent, decompose_prompt, stream_callback=stream_callback
1300+
)
12971301

12981302
# Handle both streaming and non-streaming results
12991303
if isinstance(result, Generator):

camel/tasks/task.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
import re
1616
from enum import Enum
17+
from types import GeneratorType
1718
from typing import (
1819
TYPE_CHECKING,
1920
Any,
2021
Callable,
2122
Dict,
2223
Generator,
24+
Iterable,
2325
List,
2426
Literal,
2527
Optional,
@@ -31,7 +33,6 @@
3133

3234
if TYPE_CHECKING:
3335
from camel.agents import ChatAgent
34-
from camel.agents.chat_agent import StreamingChatAgentResponse
3536
import uuid
3637

3738
from camel.logger import get_logger
@@ -409,6 +410,7 @@ def decompose(
409410
agent: "ChatAgent",
410411
prompt: Optional[str] = None,
411412
task_parser: Callable[[str, str], List["Task"]] = parse_response,
413+
stream_callback: Optional[Callable[[str], None]] = None,
412414
) -> Union[List["Task"], Generator[List["Task"], None, None]]:
413415
r"""Decompose a task to a list of sub-tasks. Automatically detects
414416
streaming or non-streaming based on agent configuration.
@@ -441,15 +443,27 @@ def decompose(
441443
# Auto-detect streaming based on response type
442444
from camel.agents.chat_agent import StreamingChatAgentResponse
443445

444-
if isinstance(response, StreamingChatAgentResponse):
445-
return self._decompose_streaming(response, task_parser)
446-
else:
447-
return self._decompose_non_streaming(response, task_parser)
446+
is_streaming = isinstance(
447+
response, StreamingChatAgentResponse
448+
) or isinstance(response, GeneratorType)
449+
if (
450+
not is_streaming
451+
and hasattr(response, "__iter__")
452+
and not hasattr(response, "msg")
453+
):
454+
is_streaming = True
455+
456+
if is_streaming:
457+
return self._decompose_streaming(
458+
response, task_parser, stream_callback=stream_callback
459+
)
460+
return self._decompose_non_streaming(response, task_parser)
448461

449462
def _decompose_streaming(
450463
self,
451-
response: "StreamingChatAgentResponse",
464+
response: Iterable,
452465
task_parser: Callable[[str, str], List["Task"]],
466+
stream_callback: Optional[Callable[[str], None]] = None,
453467
) -> Generator[List["Task"], None, None]:
454468
r"""Handle streaming response for task decomposition.
455469
@@ -466,6 +480,14 @@ def _decompose_streaming(
466480
# Process streaming response
467481
for chunk in response:
468482
accumulated_content = chunk.msg.content
483+
if stream_callback:
484+
try:
485+
stream_callback(accumulated_content)
486+
except Exception:
487+
logger.warning(
488+
"stream_callback failed during decomposition",
489+
exc_info=True,
490+
)
469491

470492
# Try to parse partial tasks from accumulated content
471493
try:

0 commit comments

Comments
 (0)