Skip to content

Commit 8ba84e9

Browse files
improve how state snapshotting works in AG-UI (#19934)
improve how state snapshotting works
1 parent 2965fc8 commit 8ba84e9

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

llama-index-integrations/protocols/llama-index-protocols-ag-ui/llama_index/protocols/ag_ui/agent.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from llama_index.core.workflow.events import Event, StartEvent, StopEvent
1313
from llama_index.protocols.ag_ui.events import (
1414
MessagesSnapshotWorkflowEvent,
15+
StateSnapshotWorkflowEvent,
1516
TextMessageChunkWorkflowEvent,
1617
ToolCallChunkWorkflowEvent,
1718
)
@@ -134,6 +135,7 @@ async def chat(
134135

135136
# Save state to context for tools to use
136137
await ctx.store.set("state", state)
138+
ctx.write_event_to_stream(StateSnapshotWorkflowEvent(snapshot=state))
137139

138140
if state:
139141
for msg in chat_history[::-1]:
@@ -250,6 +252,13 @@ async def handle_tool_call(
250252
kwargs[tool.ctx_param_name] = ctx
251253

252254
tool_output = await tool.acall(**kwargs)
255+
256+
# Update the state snapshot
257+
current_state = await ctx.store.get("state", default={})
258+
ctx.write_event_to_stream(
259+
StateSnapshotWorkflowEvent(snapshot=current_state)
260+
)
261+
253262
return ToolCallResultEvent(
254263
tool_call_id=ev.tool_call_id,
255264
tool_name=ev.tool_name,

llama-index-integrations/protocols/llama-index-protocols-ag-ui/llama_index/protocols/ag_ui/router.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ async def stream_response():
7575
# Finish the run
7676
_ = await handler
7777

78-
# Update the state
79-
state = await handler.ctx.store.get("state", default={})
80-
yield workflow_event_to_sse(StateSnapshotWorkflowEvent(snapshot=state))
81-
8278
yield workflow_event_to_sse(
8379
RunFinishedWorkflowEvent(
8480
timestamp=timestamp(),

llama-index-integrations/protocols/llama-index-protocols-ag-ui/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ dev = [
2828

2929
[project]
3030
name = "llama-index-protocols-ag-ui"
31-
version = "0.2.1"
31+
version = "0.2.2"
3232
description = "llama-index protocols AG-UI integration"
3333
authors = [
3434
{name = "Logan Markewich", email = "[email protected]"},

0 commit comments

Comments
 (0)