forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterrupt_agent.py
More file actions
63 lines (54 loc) · 2.72 KB
/
Copy pathinterrupt_agent.py
File metadata and controls
63 lines (54 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
AG2 scheduling agent -- interrupt-adapted.
This agent powers two demos (gen-ui-interrupt, interrupt-headless) that in the
LangGraph showcase rely on the native `interrupt()` primitive with
checkpoint/resume. AG2 does NOT have that primitive, so we adapt using the
same "Strategy B" pattern as the MS Agent Framework port: the backend agent's
system prompt tells the LLM to call `schedule_meeting`, but no local
implementation is registered -- the tool is provided entirely by the frontend
via `useFrontendTool` with an async handler that returns a Promise resolving
only once the user picks a time slot (or cancels).
See `src/agents/agent.py` for the shared ConversableAgent used by most other
AG2 demos.
"""
# @region[backend-interrupt-tool]
from __future__ import annotations
from autogen import ConversableAgent, LLMConfig
from autogen.ag_ui import AGUIStream
from fastapi import FastAPI
# @region[backend-tool-call]
SYSTEM_PROMPT = (
"You are a scheduling assistant. Whenever the user asks you to book a call "
"or schedule a meeting, you MUST call the `schedule_meeting` tool. Pass a "
"short `topic` describing the purpose of the meeting and, if known, an "
"`attendee` describing who the meeting is with.\n\n"
"The `schedule_meeting` tool is implemented on the client: it surfaces a "
"time-picker UI to the user and returns the user's selection. After the "
"tool returns, briefly confirm whether the meeting was scheduled and at "
"what time, or note that the user cancelled. Do NOT ask for approval "
"yourself -- always call the tool and let the picker handle the decision.\n\n"
"Keep responses short and friendly. After you finish executing tools, "
"always send a brief final assistant message summarizing what happened so "
"the message persists."
)
interrupt_agent = ConversableAgent(
name="scheduling_agent",
system_message=SYSTEM_PROMPT,
llm_config=LLMConfig({"model": "gpt-4o-mini", "stream": True}),
human_input_mode="NEVER",
max_consecutive_auto_reply=5,
# No backend tools. `schedule_meeting` is registered on the frontend
# via `useFrontendTool` and dispatched through the CopilotKit runtime.
# When the agent calls `schedule_meeting`, the request is routed to
# the frontend handler, which returns a Promise that only resolves
# once the user picks a slot -- equivalent to `interrupt()` in the
# LangGraph reference.
functions=[],
)
# @endregion[backend-tool-call]
# @endregion[backend-interrupt-tool]
# AG-UI stream wrapper
interrupt_stream = AGUIStream(interrupt_agent)
# FastAPI sub-app so agent_server.py can mount at /interrupt-adapted
interrupt_app = FastAPI(title="AG2 Interrupt Agent")
interrupt_app.mount("/", interrupt_stream.build_asgi())