Skip to content

Commit 0fa2991

Browse files
fix stateful examples
1 parent e74e722 commit 0fa2991

File tree

4 files changed

+107
-188
lines changed

4 files changed

+107
-188
lines changed

typescript-sdk/integrations/llamaindex/server-py/pyproject.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ description = "Add your description here"
99
readme = "README.md"
1010
requires-python = ">=3.9, <3.14"
1111
dependencies = [
12-
"llama-index-core>=0.12.41,<0.13",
13-
"llama-index-agent-openai>=0.4.9,<0.5",
14-
"llama-index-protocols-ag-ui>=0.1.2",
12+
"llama-index-core>=0.14.0,<0.15",
13+
"llama-index-protocols-ag-ui>=0.2.0,<0.3",
1514
"jsonpatch>=1.33",
1615
"uvicorn>=0.27.0",
1716
"fastapi>=0.100.0",
@@ -26,5 +25,8 @@ include = ["server/"]
2625
[tool.hatch.build.targets.wheel]
2726
include = ["server/"]
2827

28+
[tool.hatch.metadata]
29+
allow-direct-references = true
30+
2931
[project.scripts]
3032
dev = "server:main"

typescript-sdk/integrations/llamaindex/server-py/server/routers/agentic_generative_ui.py

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,65 +19,69 @@ async def run_task(
1919
ctx: Context, task: Task,
2020
) -> str:
2121
"""Execute any list of steps needed to complete a task. Useful for anything the user wants to do."""
22-
state = await ctx.get("state", default={})
23-
task = Task.model_validate(task)
2422

25-
state = {
26-
"steps": [
27-
{
28-
"description": step.description,
29-
"status": "pending"
30-
}
31-
for step in task.steps
32-
]
33-
}
23+
async with ctx.store.edit_state() as global_state:
24+
state = global_state.get("state", {})
25+
task = Task.model_validate(task)
3426

35-
# Send initial state snapshot
36-
ctx.write_event_to_stream(
37-
StateSnapshotWorkflowEvent(
38-
snapshot=state
39-
)
40-
)
41-
42-
# Sleep for 1 second
43-
await asyncio.sleep(1.0)
44-
45-
# Create a copy to track changes for JSON patches
46-
previous_state = copy.deepcopy(state)
27+
state = {
28+
"steps": [
29+
{
30+
"description": step.description,
31+
"status": "pending"
32+
}
33+
for step in task.steps
34+
]
35+
}
4736

48-
# Update each step and send deltas
49-
for i, step in enumerate(state["steps"]):
50-
step["status"] = "completed"
51-
52-
# Generate JSON patch from previous state to current state
53-
patch = jsonpatch.make_patch(previous_state, state)
54-
55-
# Send state delta event
37+
# Send initial state snapshot
5638
ctx.write_event_to_stream(
57-
StateDeltaWorkflowEvent(
58-
delta=patch.patch
39+
StateSnapshotWorkflowEvent(
40+
snapshot=state
5941
)
6042
)
61-
62-
# Update previous state for next iteration
63-
previous_state = copy.deepcopy(state)
64-
43+
6544
# Sleep for 1 second
6645
await asyncio.sleep(1.0)
6746

68-
# Optionally send a final snapshot to the client
69-
ctx.write_event_to_stream(
70-
StateSnapshotWorkflowEvent(
71-
snapshot=state
47+
# Create a copy to track changes for JSON patches
48+
previous_state = copy.deepcopy(state)
49+
50+
# Update each step and send deltas
51+
for i, step in enumerate(state["steps"]):
52+
step["status"] = "completed"
53+
54+
# Generate JSON patch from previous state to current state
55+
patch = jsonpatch.make_patch(previous_state, state)
56+
57+
# Send state delta event
58+
ctx.write_event_to_stream(
59+
StateDeltaWorkflowEvent(
60+
delta=patch.patch
61+
)
62+
)
63+
64+
# Update previous state for next iteration
65+
previous_state = copy.deepcopy(state)
66+
67+
# Sleep for 1 second
68+
await asyncio.sleep(1.0)
69+
70+
# Optionally send a final snapshot to the client
71+
ctx.write_event_to_stream(
72+
StateSnapshotWorkflowEvent(
73+
snapshot=state
74+
)
7275
)
73-
)
7476

75-
return "Done!"
77+
global_state["state"] = state
78+
79+
return "Task Done!"
7680

7781

7882
agentic_generative_ui_router = get_ag_ui_workflow_router(
7983
llm=OpenAI(model="gpt-4.1"),
80-
frontend_tools=[run_task],
84+
backend_tools=[run_task],
8185
initial_state={},
8286
system_prompt=(
8387
"You are a helpful assistant that can help the user with their task. "

typescript-sdk/integrations/llamaindex/server-py/server/routers/shared_state.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,20 @@ async def update_recipe(ctx: Context, recipe: Recipe) -> str:
2424
"""Useful for recording a recipe to shared state."""
2525
recipe = Recipe.model_validate(recipe)
2626

27-
state = await ctx.get("state")
28-
if state is None:
29-
state = {}
27+
async with ctx.store.edit_state() as global_state:
28+
state = global_state.get("state", {})
29+
if state is None:
30+
state = {}
3031

31-
state["recipe"] = recipe.model_dump()
32+
state["recipe"] = recipe.model_dump()
3233

33-
ctx.write_event_to_stream(
34-
StateSnapshotWorkflowEvent(
35-
snapshot=state
34+
ctx.write_event_to_stream(
35+
StateSnapshotWorkflowEvent(
36+
snapshot=state
37+
)
3638
)
37-
)
3839

39-
await ctx.set("state", state)
40+
global_state["state"] = state
4041

4142
return "Recipe updated!"
4243

0 commit comments

Comments
 (0)