forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhitl_in_app.py
More file actions
61 lines (54 loc) · 2.69 KB
/
Copy pathhitl_in_app.py
File metadata and controls
61 lines (54 loc) · 2.69 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
"""LangGraph 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}`. This agent treats that result as
authoritative: if `approved` is `True`, continue; otherwise, stop and
explain the decision back to the user.
"""
from langchain.agents import create_agent
from langchain_openai import ChatOpenAI
from copilotkit import CopilotKitMiddleware
SYSTEM_PROMPT = (
"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.\n"
"\n"
"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.\n"
"\n"
"How to use `request_user_approval`:\n"
"- `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').\n"
"- `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.\n"
"\n"
"The tool returns an object of the shape "
'`{"approved": boolean, "reason": string | null}`.\n'
"- 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.\n"
"- 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.\n"
"\n"
"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."
)
graph = create_agent(
model=ChatOpenAI(model="gpt-5.4"),
tools=[],
middleware=[CopilotKitMiddleware()],
system_prompt=SYSTEM_PROMPT,
)