Skip to content

Commit 90517ff

Browse files
committed
add backend tool call
1 parent affc293 commit 90517ff

File tree

2 files changed

+70
-1
lines changed
  • typescript-sdk
    • apps/dojo/src/app/[integrationId]/feature/agentic_chat
    • integrations/server-starter-all-features/server/python/example_server

2 files changed

+70
-1
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,34 @@ const AgenticChat: React.FC<AgenticChatProps> = ({ params }) => {
2929
const Chat = () => {
3030
const [background, setBackground] = useState<string>("--copilot-kit-background-color");
3131

32+
useCopilotAction({
33+
name: "lookup_weather",
34+
description: "Lookup the weather for a given city",
35+
parameters: [
36+
{
37+
name: "city",
38+
type: "string",
39+
description: "The city to lookup the weather for",
40+
},
41+
{
42+
name: "weather",
43+
type: "string",
44+
description: "The weather for the city",
45+
},
46+
],
47+
render: ({ status, args }) => {
48+
return (
49+
<div>
50+
<div>
51+
Looked up weather for {args.city}: {args.weather}
52+
</div>
53+
<div>Status: {status}</div>
54+
</div>
55+
);
56+
},
57+
followUp: false,
58+
});
59+
3260
useCopilotAction({
3361
name: "change_background",
3462
description:

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
TextMessageEndEvent,
1818
ToolCallStartEvent,
1919
ToolCallArgsEvent,
20-
ToolCallEndEvent
20+
ToolCallEndEvent,
21+
MessagesSnapshotEvent,
22+
ToolMessage,
23+
ToolCall,
24+
AssistantMessage
2125
)
2226
from ag_ui.encoder import EventEncoder
2327

@@ -54,6 +58,9 @@ async def event_generator():
5458
elif last_message_content == "tool":
5559
async for event in send_tool_call_events():
5660
yield encoder.encode(event)
61+
elif last_message_content == "backend_tool":
62+
async for event in send_backend_tool_call_events(input_data.messages):
63+
yield encoder.encode(event)
5764
else:
5865
async for event in send_text_message_events():
5966
yield encoder.encode(event)
@@ -167,3 +174,37 @@ async def send_tool_call_events():
167174
type=EventType.TOOL_CALL_END,
168175
tool_call_id=tool_call_id
169176
)
177+
178+
async def send_backend_tool_call_events(messages):
179+
"""Send backend tool call events"""
180+
tool_call_id = str(uuid.uuid4())
181+
182+
new_message = AssistantMessage(
183+
id=str(uuid.uuid4()),
184+
role="assistant",
185+
tool_calls=[
186+
ToolCall(
187+
id=tool_call_id,
188+
type="function",
189+
function={
190+
"name": "lookup_weather",
191+
"arguments": json.dumps({"city": "San Francisco", "weather": "sunny"})
192+
}
193+
)
194+
]
195+
)
196+
197+
result_message = ToolMessage(
198+
id=str(uuid.uuid4()),
199+
role="tool",
200+
content="The weather in San Francisco is sunny.",
201+
tool_call_id=tool_call_id
202+
)
203+
204+
all_messages = list(messages) + [new_message, result_message]
205+
206+
# Send messages snapshot event
207+
yield MessagesSnapshotEvent(
208+
type=EventType.MESSAGES_SNAPSHOT,
209+
messages=all_messages
210+
)

0 commit comments

Comments
 (0)