Files
copilotkit__copilotkit/showcase/integrations/ms-agent-dotnet/agent/HitlInAppAgent.cs
Alem Tuzlak f57151990f fix(showcase/ms-agent-dotnet): wire system prompts to Instructions + demo repairs
Root cause across nearly every ChatClientAgent: system prompts were passed as
`description:` (agent metadata) instead of `instructions:` (the actual system
message). ChatClientAgent(instructions, name, description, …) therefore ran
with null instructions, so models ignored tool guidance and BYOC JSON demos
emitted prose.

Fixes reported staging failures:
- shared-state-read-write pills: instructions now reach the model + stronger set_notes guidance
- declarative-hashbrown / declarative-json-render: instructions + ChatResponseFormat.Json (LGP parity)
- declarative-gen-ui: catalog-specific design prompt (no DashboardCard), stronger outer agent
- shared-state-streaming: feature was demo-only in the manifest → shell "Backend fixture unavailable"; added to features

Verified: unit suite 79/79 in Docker SDK 9.
2026-07-29 13:00:08 +02:00

81 lines
3.7 KiB
C#

using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;
using System.ClientModel;
// In-App HITL (frontend-tool + popup modal) agent.
//
// The agent is a support-ops copilot. Any action that materially affects
// a customer MUST be confirmed by the operator via the frontend-provided
// `request_user_approval` tool (registered via `useFrontendTool` on the
// page). The tool handler opens a modal OUTSIDE the chat surface and
// returns `{ approved: boolean, reason?: string }` back to the agent.
//
// This agent owns NO server-side tools — the approval tool lives on the
// frontend. The system prompt tells the model to invoke it whenever a
// customer-affecting action is requested.
//
// Reference parity with:
// showcase/integrations/langgraph-python/src/agents/hitl_in_app.py
public sealed class HitlInAppAgentFactory
{
private const string SystemPrompt =
"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.";
private readonly OpenAIClient _openAiClient;
private readonly ILogger _logger;
public HitlInAppAgentFactory(IConfiguration configuration, ILoggerFactory loggerFactory)
{
ArgumentNullException.ThrowIfNull(configuration);
ArgumentNullException.ThrowIfNull(loggerFactory);
_logger = loggerFactory.CreateLogger<HitlInAppAgentFactory>();
var apiKey = ApiKeyResolver.ResolveApiKey(configuration);
var endpoint = ApiKeyResolver.ResolveEndpoint(configuration);
_openAiClient = new(
new ApiKeyCredential(apiKey),
AimockHeaderPolicy.CreateOpenAIClientOptions(endpoint));
}
public AIAgent CreateHitlInAppAgent()
{
var chatClient = _openAiClient.GetChatClient("gpt-4o-mini").AsIChatClient();
return new ChatClientAgent(
chatClient,
name: "HitlInAppAgent",
instructions: SystemPrompt,
tools: []);
}
}