forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
126 lines (109 loc) · 4.99 KB
/
Copy pathagent.py
File metadata and controls
126 lines (109 loc) · 4.99 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from __future__ import annotations
from textwrap import dedent
from typing import Annotated
from agent_framework import ChatAgent, ChatClientProtocol, ai_function
from agent_framework_ag_ui import AgentFrameworkAgent
from pydantic import Field
STATE_SCHEMA: dict[str, object] = {
"proverbs": {
"type": "array",
"items": {"type": "string"},
"description": "Ordered list of the user's saved proverbs.",
}
}
PREDICT_STATE_CONFIG: dict[str, dict[str, str]] = {
"proverbs": {
"tool": "update_proverbs",
"tool_argument": "proverbs",
}
}
@ai_function(
name="update_proverbs",
description=(
"Replace the entire list of proverbs with the provided values. "
"Always include every proverb you want to keep."
),
)
def update_proverbs(
proverbs: Annotated[
list[str],
Field(
description=(
"The complete source of truth for the user's proverbs. "
"Maintain ordering and include the full list on each call."
)
),
],
) -> str:
"""Persist the provided set of proverbs."""
return f"Proverbs updated. Tracking {len(proverbs)} item(s)."
@ai_function(
name="get_weather",
description="Share a quick weather update for a location. Use this to render the frontend weather card.",
)
def get_weather(
location: Annotated[
str,
Field(
description="The city or region to describe. Use fully spelled out names."
),
],
) -> str:
"""Return a short natural language weather summary."""
normalized = location.strip().title() or "the requested location"
return (
f"The weather in {normalized} is mild with a light breeze. "
"Skies are mostly clear—perfect for planning something fun."
)
@ai_function(
name="go_to_moon",
description="Request a playful human-in-the-loop confirmation before launching a mission to the moon.",
approval_mode="always_require",
)
def go_to_moon() -> str:
"""Request human approval before continuing."""
return "Mission control requested. Awaiting human approval for the lunar launch."
def create_agent(chat_client: ChatClientProtocol) -> AgentFrameworkAgent:
"""Instantiate the CopilotKit demo agent backed by Microsoft Agent Framework."""
base_agent = ChatAgent(
name="proverbs_agent",
instructions=dedent(
"""
You help users brainstorm, organize, and refine proverbs while coordinating UI updates.
State sync:
- The current list of proverbs is provided in the conversation context.
- When you add, remove, or reorder proverbs, call `update_proverbs` with the full list.
Never send partial updates—always include every proverb that should exist.
- CRITICAL: When asked to "add" a proverb, you must:
1. First, identify ALL existing proverbs from the conversation history
2. Create EXACTLY ONE new proverb (never more than one unless explicitly requested)
3. Call update_proverbs with: [all existing proverbs] + [the one new proverb]
Example: Current: ["A", "B"] -> After adding: ["A", "B", "C"] (NOT ["A", "B", "C", "D", "E"])
- When asked to "remove" a proverb, remove exactly ONE item unless user specifies otherwise.
Tool usage rules:
- When user asks to go to the moon, you MUST call the `go_to_moon` tool immediately. Do NOT ask for approval
yourself—the tool's approval workflow and the client UI will handle it.
Frontend integrations:
- `get_weather` renders a weather card in the UI. Only call this tool when the user explicitly
asks for weather. Do NOT call it after unrelated tasks or approvals.
- `go_to_moon` requires explicit user approval before you proceed. Only use it when a
user asks to launch or travel to the moon. Always call the tool instead of asking manually.
Conversation tips:
- Reference the latest proverb list before suggesting changes.
- Keep responses concise and friendly unless the user requests otherwise.
- After you finish executing tools for the user's request, provide a brief, final assistant
message summarizing exactly what changed. Do NOT call additional tools or switch topics
after that summary unless the user asks. ALWAYS send this conversational summary so the message persists.
""".strip()
),
chat_client=chat_client,
tools=[update_proverbs, get_weather, go_to_moon],
)
return AgentFrameworkAgent(
agent=base_agent,
name="CopilotKitMicrosoftAgentFrameworkAgent",
description="Manages proverbs, weather snippets, and human-in-the-loop moon launches.",
state_schema=STATE_SCHEMA,
predict_state_config=PREDICT_STATE_CONFIG,
require_confirmation=False, # Allow immediate state updates with follow-up messages
)