Skip to content

Commit ea756c0

Browse files
committed
fix: predictive state examples
Fix predictive state examples by renaming the `write_document` tool to `write_document_local` and updating the metadata accordingly. This ensures that the examples correctly reflect the intended functionality of the predictive state updates, while still allowing the Pydantic AI example to leverage the AG-UI defined `write_document` tool. Restore the `confirm_changes` tool to enable the confirmation of changes for existing examples.
1 parent f31f1e5 commit ea756c0

File tree

6 files changed

+46
-21
lines changed

6 files changed

+46
-21
lines changed

typescript-sdk/.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ yarn-error.log*
4141
packages/proto/src/generated
4242

4343
# LangGraph API
44-
**/**/.langgraph_api
44+
**/**/.langgraph_api
45+
46+
# Python
47+
__pycache__/

typescript-sdk/apps/dojo/src/app/[integrationId]/feature/predictive_state_updates/page.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,28 @@ const DocumentEditor = () => {
127127
}
128128
}, [text]);
129129

130+
// TODO(steve): Remove this when all agents have been updated to use write_document tool.
131+
useCopilotAction({
132+
name: "confirm_changes",
133+
renderAndWaitForResponse: ({ args, respond, status }) => (
134+
<ConfirmChanges
135+
args={args}
136+
respond={respond}
137+
status={status}
138+
onReject={() => {
139+
editor?.commands.setContent(fromMarkdown(currentDocument));
140+
setAgentState({ document: currentDocument });
141+
}}
142+
onConfirm={() => {
143+
editor?.commands.setContent(fromMarkdown(agentState?.document || ""));
144+
setCurrentDocument(agentState?.document || "");
145+
setAgentState({ document: agentState?.document || "" });
146+
}}
147+
/>
148+
),
149+
});
150+
151+
// Action to write the document.
130152
useCopilotAction({
131153
name: "write_document",
132154
description: `Present the proposed changes to the user for review`,

typescript-sdk/integrations/crewai/python/ag_ui_crewai/examples/predictive_state_updates.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
WRITE_DOCUMENT_TOOL = {
1717
"type": "function",
1818
"function": {
19-
"name": "write_document",
19+
"name": "write_document_local",
2020
"description": " ".join("""
2121
Write a document. Use markdown formatting to format the document.
2222
It's good to format the document extensively so it's easy to read.
@@ -63,19 +63,19 @@ async def chat(self):
6363
Standard chat node.
6464
"""
6565
system_prompt = f"""
66-
You are a helpful assistant for writing documents.
67-
To write the document, you MUST use the write_document tool.
66+
You are a helpful assistant for writing documents.
67+
To write the document, you MUST use the write_document_local tool.
6868
You MUST write the full document, even when changing only a few words.
6969
When you wrote the document, DO NOT repeat it as a message.
7070
Just briefly summarize the changes you made. 2 sentences max.
7171
This is the current state of the document: ----\n {self.state.document}\n-----
7272
"""
7373

74-
# 1. Here we specify that we want to stream the tool call to write_document
74+
# 1. Here we specify that we want to stream the tool call to write_document_local
7575
# to the frontend as state.
7676
await copilotkit_predict_state({
7777
"document": {
78-
"tool_name": "write_document",
78+
"tool_name": "write_document_local",
7979
"tool_argument": "document"
8080
}
8181
})
@@ -122,7 +122,7 @@ async def chat(self):
122122
tool_call_name = tool_call["function"]["name"]
123123
tool_call_args = json.loads(tool_call["function"]["arguments"])
124124

125-
if tool_call_name == "write_document":
125+
if tool_call_name == "write_document_local":
126126
self.state.document = tool_call_args["document"]
127127

128128
# 4.1 Append the result to the messages in state

typescript-sdk/integrations/langgraph/examples/agents/predictive_state_updates/agent.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
WRITE_DOCUMENT_TOOL = {
1919
"type": "function",
2020
"function": {
21-
"name": "write_document",
21+
"name": "write_document_local",
2222
"description": " ".join("""
2323
Write a document. Use markdown formatting to format the document.
2424
It's good to format the document extensively so it's easy to read.
@@ -64,8 +64,8 @@ async def chat_node(state: AgentState, config: RunnableConfig):
6464
"""
6565

6666
system_prompt = f"""
67-
You are a helpful assistant for writing documents.
68-
To write the document, you MUST use the write_document tool.
67+
You are a helpful assistant for writing documents.
68+
To write the document, you MUST use the write_document_local tool.
6969
You MUST write the full document, even when changing only a few words.
7070
When you wrote the document, DO NOT repeat it as a message.
7171
Just briefly summarize the changes you made. 2 sentences max.
@@ -79,10 +79,10 @@ async def chat_node(state: AgentState, config: RunnableConfig):
7979
if config is None:
8080
config = RunnableConfig(recursion_limit=25)
8181

82-
# Use "predict_state" metadata to set up streaming for the write_document tool
82+
# Use "predict_state" metadata to set up streaming for the write_document_local tool
8383
config["metadata"]["predict_state"] = [{
8484
"state_key": "document",
85-
"tool": "write_document",
85+
"tool": "write_document_local",
8686
"tool_argument": "document"
8787
}]
8888

@@ -120,7 +120,7 @@ async def chat_node(state: AgentState, config: RunnableConfig):
120120
tool_call_name = tool_call.name
121121
tool_call_args = tool_call.args
122122

123-
if tool_call_name == "write_document":
123+
if tool_call_name == "write_document_local":
124124
# Add the tool response to messages
125125
tool_response = {
126126
"role": "tool",

typescript-sdk/integrations/langgraph/python/ag_ui_langgraph/examples/agents/predictive_state_updates.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
WRITE_DOCUMENT_TOOL = {
2020
"type": "function",
2121
"function": {
22-
"name": "write_document",
22+
"name": "write_document_local",
2323
"description": " ".join("""
2424
Write a document. Use markdown formatting to format the document.
2525
It's good to format the document extensively so it's easy to read.
@@ -66,7 +66,7 @@ async def chat_node(state: AgentState, config: RunnableConfig):
6666

6767
system_prompt = f"""
6868
You are a helpful assistant for writing documents.
69-
To write the document, you MUST use the write_document tool.
69+
To write the document, you MUST use the write_document_local tool.
7070
You MUST write the full document, even when changing only a few words.
7171
When you wrote the document, DO NOT repeat it as a message.
7272
Just briefly summarize the changes you made. 2 sentences max.
@@ -80,10 +80,10 @@ async def chat_node(state: AgentState, config: RunnableConfig):
8080
if config is None:
8181
config = RunnableConfig(recursion_limit=25)
8282

83-
# Use "predict_state" metadata to set up streaming for the write_document tool
83+
# Use "predict_state" metadata to set up streaming for the write_document_local tool
8484
config["metadata"]["predict_state"] = [{
8585
"state_key": "document",
86-
"tool": "write_document",
86+
"tool": "write_document_local",
8787
"tool_argument": "document"
8888
}]
8989

@@ -121,7 +121,7 @@ async def chat_node(state: AgentState, config: RunnableConfig):
121121
tool_call_name = tool_call.name
122122
tool_call_args = tool_call.args
123123

124-
if tool_call_name == "write_document":
124+
if tool_call_name == "write_document_local":
125125
# Add the tool response to messages
126126
tool_response = {
127127
"role": "tool",

typescript-sdk/integrations/server-starter-all-features/server/python/example_server/predictive_state_updates.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def make_story(name: str) -> str:
8080
async def send_tool_call_events():
8181
"""Send tool call events with predictive state and incremental story generation"""
8282
tool_call_id = str(uuid.uuid4())
83-
tool_call_name = "write_document"
83+
tool_call_name = "write_document_local"
8484

8585
# Generate a random story
8686
story = make_story(random.choice(dog_names))
@@ -93,13 +93,13 @@ async def send_tool_call_events():
9393
value=[
9494
{
9595
"state_key": "document",
96-
"tool": "write_document",
96+
"tool": "write_document_local",
9797
"tool_argument": "document"
9898
}
9999
]
100100
)
101101

102-
# First tool call: write_document
102+
# First tool call: write_document_local
103103
yield ToolCallStartEvent(
104104
type=EventType.TOOL_CALL_START,
105105
tool_call_id=tool_call_id,

0 commit comments

Comments
 (0)