using Microsoft.Agents.AI; using Microsoft.Extensions.AI; using OpenAI; using System.ClientModel; /// /// Factory for the Open-Ended Generative UI (Advanced) demo agent. /// /// This is the "advanced" variant of the Open Generative UI demo. The key /// distinguishing feature: the agent-authored, sandboxed UI can invoke /// frontend-registered sandbox functions — functions the /// app defines on the host page (see /// src/app/demos/open-gen-ui-advanced/sandbox-functions.ts) and /// makes callable from inside the iframe via /// await Websandbox.connection.remote.<name>(args). /// /// How it works end-to-end: /// - The frontend passes openGenerativeUI={{ sandboxFunctions }} /// to the CopilotKitProvider. The provider injects a JSON /// descriptor of those functions into the agent context. /// - The CopilotKit runtime picks up both the frontend-registered /// generateSandboxedUi tool (auto-registered by the provider /// when OGUI is enabled on the runtime) AND the sandbox-function /// descriptors and merges them into what the LLM sees. /// - The LLM generates HTML + JS that calls /// Websandbox.connection.remote.<name>(...) in response /// to user interactions. /// - The runtime's OpenGenerativeUIMiddleware converts the /// streaming generateSandboxedUi tool call into /// open-generative-ui activity events that the built-in /// renderer mounts inside a sandboxed iframe. /// - The renderer wires each sandboxFunctions entry as a /// localApi method on the websandbox connection so in-iframe /// code can call it. /// /// The "minimal" sibling () uses the /// same OGUI pipeline without sandbox functions. /// public class OpenGenUiAdvancedAgentFactory { private const string DefaultOpenAiEndpoint = "https://models.inference.ai.azure.com"; private const string SystemPrompt = @"You are a UI-generating assistant for the Open Generative UI (Advanced) demo. On every user turn you MUST call the `generateSandboxedUi` frontend tool exactly once. The generated UI must be INTERACTIVE and must invoke the available host-side sandbox functions described in your agent context (delivered via `copilotkit.context`) in response to user interactions. Sandbox-function calling contract (inside the generated iframe): - Call a host function with: await Websandbox.connection.remote.(args) The call returns a Promise; await it. - Each handler returns a plain object. Read the return shape from the function's description in your context and use the EXACT field names it returns (e.g. if the description says the handler returns `{ ok, value }`, read `res.value` — not `res.result`). - Descriptions, names, and JSON-schema parameter shapes for every available sandbox function are listed in your context. Read them carefully and wire at least one interactive UI element to call one. Sandbox iframe restrictions (CRITICAL): - The iframe runs with `sandbox=""allow-scripts""` ONLY. Forms are NOT allowed. You MUST NOT use `
` elements or `