Skip to content

Commit 8dd2924

Browse files
authored
Merge pull request #472 from tisnik/refactoring-table-based-branching
Refactoring: table based branching
2 parents 350d9cc + facf446 commit 8dd2924

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/app/endpoints/streaming_query.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,14 @@ def stream_end_event(metadata_map: dict) -> str:
134134
def stream_build_event(chunk: Any, chunk_id: int, metadata_map: dict) -> Iterator[str]:
135135
"""Build a streaming event from a chunk response.
136136
137-
This function processes chunks from the Llama Stack streaming response and formats
138-
them into Server-Sent Events (SSE) format for the client. It handles two main
139-
event types:
137+
This function processes chunks from the Llama Stack streaming response and
138+
formats them into Server-Sent Events (SSE) format for the client. It
139+
dispatches on (event_type, step_type):
140140
141-
1. step_progress: Contains text deltas from the model inference process
142-
2. step_complete: Contains information about completed tool execution steps
141+
1. turn_start, turn_awaiting_input -> start token
142+
2. turn_complete -> final output message
143+
3. step_* with step_type in {"shield_call", "inference", "tool_execution"} -> delegated handlers
144+
4. anything else -> heartbeat
143145
144146
Args:
145147
chunk: The streaming chunk from Llama Stack containing event data
@@ -154,18 +156,24 @@ def stream_build_event(chunk: Any, chunk_id: int, metadata_map: dict) -> Iterato
154156
event_type = chunk.event.payload.event_type
155157
step_type = getattr(chunk.event.payload, "step_type", None)
156158

157-
if event_type in {"turn_start", "turn_awaiting_input"}:
158-
yield from _handle_turn_start_event(chunk_id)
159-
elif event_type == "turn_complete":
160-
yield from _handle_turn_complete_event(chunk, chunk_id)
161-
elif step_type == "shield_call":
162-
yield from _handle_shield_event(chunk, chunk_id)
163-
elif step_type == "inference":
164-
yield from _handle_inference_event(chunk, chunk_id)
165-
elif step_type == "tool_execution":
166-
yield from _handle_tool_execution_event(chunk, chunk_id, metadata_map)
167-
else:
168-
yield from _handle_heartbeat_event(chunk_id)
159+
match (event_type, step_type):
160+
case (("turn_start" | "turn_awaiting_input"), _):
161+
yield from _handle_turn_start_event(chunk_id)
162+
case ("turn_complete", _):
163+
yield from _handle_turn_complete_event(chunk, chunk_id)
164+
case (_, "shield_call"):
165+
yield from _handle_shield_event(chunk, chunk_id)
166+
case (_, "inference"):
167+
yield from _handle_inference_event(chunk, chunk_id)
168+
case (_, "tool_execution"):
169+
yield from _handle_tool_execution_event(chunk, chunk_id, metadata_map)
170+
case _:
171+
logger.debug(
172+
"Unhandled event combo: event_type=%s, step_type=%s",
173+
event_type,
174+
step_type,
175+
)
176+
yield from _handle_heartbeat_event(chunk_id)
169177

170178

171179
# -----------------------------------

0 commit comments

Comments
 (0)