forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhitl_in_app_agent.py
More file actions
159 lines (129 loc) · 5.8 KB
/
Copy pathhitl_in_app_agent.py
File metadata and controls
159 lines (129 loc) · 5.8 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
MS Agent Framework agent backing the In-App HITL (frontend-tool + popup) demo.
The agent is a support assistant that processes customer-care requests
(refunds, account changes, escalations). Any action that materially
affects a customer MUST be confirmed by the human operator via the
frontend-provided `request_user_approval` tool.
The tool is defined on the frontend via `useFrontendTool` with an async
handler that opens a modal dialog OUTSIDE the chat surface. The handler
awaits the user's decision and resolves with
``{"approved": bool, "reason": str | None}``. This agent treats that
result as authoritative: if ``approved`` is ``True``, continue;
otherwise, stop and explain the decision back to the user.
"""
from __future__ import annotations
from collections.abc import AsyncGenerator
from textwrap import dedent
from typing import Any
from ag_ui.core import BaseEvent
from agent_framework import Agent, BaseChatClient
from agent_framework_ag_ui import AgentFrameworkAgent
SYSTEM_PROMPT = dedent(
"""
You are a support operations copilot working alongside a human operator
inside an internal support console. The operator can see a list of open
support tickets on the left side of their screen and is chatting with
you on the right.
Whenever the operator asks you to take an action that affects a
customer -- for example: issuing a refund, updating a customer's plan,
cancelling a subscription, escalating a ticket, or sending an apology
credit -- you MUST first call the frontend-provided
`request_user_approval` tool to obtain the operator's explicit consent.
How to use `request_user_approval`:
- `message`: a short, plain-English summary of the exact action you
are about to take, including concrete numbers (e.g. '$50 refund to
customer #12345').
- `context`: optional extra context the operator might want to review
(the ticket ID, the policy rule you're applying, etc.). Keep it to
one or two short sentences.
The tool returns an object of the shape
`{"approved": boolean, "reason": string | null}`.
- If `approved` is `true`: confirm in one short sentence that you are
processing the action. You do not actually need to call any other
tool -- this is a demo. Just acknowledge.
- If `approved` is `false`: acknowledge the rejection in one short
sentence and, if `reason` is non-empty, reflect the operator's
reason back to them. Do NOT retry the action.
Keep all chat replies to one or two short sentences. Never make up
customer data -- always use whatever the operator told you in the
prompt.
"""
).strip()
def _tool_call_ids(message: dict[str, Any]) -> set[str]:
tool_calls = message.get("tool_calls") or message.get("toolCalls") or []
if not isinstance(tool_calls, list):
return set()
ids: set[str] = set()
for call in tool_calls:
if isinstance(call, dict) and isinstance(call.get("id"), str):
ids.add(call["id"])
return ids
def _tool_result_ids(messages: list[dict[str, Any]], start_index: int) -> set[str]:
ids: set[str] = set()
for message in messages[start_index + 1 :]:
if message.get("role") == "user":
break
if message.get("role") != "tool":
continue
call_id = message.get("tool_call_id") or message.get("toolCallId")
if isinstance(call_id, str):
ids.add(call_id)
return ids
def _last_user_message_index(messages: list[dict[str, Any]]) -> int:
for index in range(len(messages) - 1, -1, -1):
if messages[index].get("role") == "user":
return index
return -1
def _sanitize_approval_history(messages: Any) -> list[dict[str, Any]]:
"""Keep only the active approval tool pair; summarize older pairs via text."""
if not isinstance(messages, list):
return []
typed_messages = [message for message in messages if isinstance(message, dict)]
last_user_index = _last_user_message_index(typed_messages)
clean: list[dict[str, Any]] = []
for index, message in enumerate(typed_messages):
if index < last_user_index:
if message.get("role") == "tool":
continue
if message.get("role") == "assistant" and _tool_call_ids(message):
continue
clean.append(message)
continue
if message.get("role") == "assistant":
call_ids = _tool_call_ids(message)
if call_ids and not call_ids.issubset(
_tool_result_ids(typed_messages, index)
):
continue
clean.append(message)
return clean
class HitlInAppFrameworkAgent(AgentFrameworkAgent):
"""AgentFrameworkAgent that drops malformed historical approval tool calls."""
async def run( # type: ignore[override]
self,
input_data: dict[str, Any],
) -> AsyncGenerator[BaseEvent, None]:
patched_input = dict(input_data)
patched_input["messages"] = _sanitize_approval_history(
input_data.get("messages")
)
async for event in super().run(patched_input):
yield event
def create_hitl_in_app_agent(chat_client: BaseChatClient) -> HitlInAppFrameworkAgent:
"""Instantiate the In-App HITL demo agent backed by Microsoft Agent Framework."""
base_agent = Agent(
client=chat_client,
name="hitl_in_app_agent",
instructions=SYSTEM_PROMPT,
tools=[],
default_options={"allow_multiple_tool_calls": False},
)
return HitlInAppFrameworkAgent(
agent=base_agent,
name="CopilotKitMSAgentHitlInAppAgent",
description=(
"Support copilot that asks for explicit operator approval via a "
"frontend-provided tool before taking any customer-affecting action."
),
require_confirmation=False,
)