From cb8d535d9bf383542a3bddf11cf9cf40847ee08b Mon Sep 17 00:00:00 2001
From: Huu Le <39040748+leehuwuj@users.noreply.github.com>
Date: Wed, 25 Sep 2024 08:58:47 +0700
Subject: [PATCH] fix: don't write the StopEvent from sub task to the stream
 (#319)

---
 .changeset/eighty-berries-tie.md                            | 5 +++++
 templates/types/multiagent/fastapi/app/agents/planner.py    | 4 +++-
 templates/types/multiagent/fastapi/app/examples/workflow.py | 4 +++-
 3 files changed, 11 insertions(+), 2 deletions(-)
 create mode 100644 .changeset/eighty-berries-tie.md

diff --git a/.changeset/eighty-berries-tie.md b/.changeset/eighty-berries-tie.md
new file mode 100644
index 00000000..40756ad9
--- /dev/null
+++ b/.changeset/eighty-berries-tie.md
@@ -0,0 +1,5 @@
+---
+"create-llama": patch
+---
+
+Fix only produces one agent event
diff --git a/templates/types/multiagent/fastapi/app/agents/planner.py b/templates/types/multiagent/fastapi/app/agents/planner.py
index c81944e1..07306f2b 100644
--- a/templates/types/multiagent/fastapi/app/agents/planner.py
+++ b/templates/types/multiagent/fastapi/app/agents/planner.py
@@ -129,7 +129,9 @@ class StructuredPlannerAgent(Workflow):
         )
         # bubble all events while running the executor to the planner
         async for event in handler.stream_events():
-            ctx.write_event_to_stream(event)
+            # Don't write the StopEvent from sub task to the stream
+            if type(event) is not StopEvent:
+                ctx.write_event_to_stream(event)
         result: AgentRunResult = await handler
         if self._verbose:
             print("=== Done executing sub task ===\n")
diff --git a/templates/types/multiagent/fastapi/app/examples/workflow.py b/templates/types/multiagent/fastapi/app/examples/workflow.py
index ba626443..c92f96ab 100644
--- a/templates/types/multiagent/fastapi/app/examples/workflow.py
+++ b/templates/types/multiagent/fastapi/app/examples/workflow.py
@@ -133,5 +133,7 @@ Review:
         handler = agent.run(input=input, streaming=streaming)
         # bubble all events while running the executor to the planner
         async for event in handler.stream_events():
-            ctx.write_event_to_stream(event)
+            # Don't write the StopEvent from sub task to the stream
+            if type(event) is not StopEvent:
+                ctx.write_event_to_stream(event)
         return await handler
-- 
GitLab