Skip to content

Conversation

Copy link

Copilot AI commented Oct 7, 2025

Overview

This PR implements a complete AG-UI (Agent-User Interaction) protocol plugin for BotSharp, enabling standardized event-based communication between AI agents and user-facing applications. AG-UI is an open, lightweight protocol that allows seamless integration with frontend frameworks like CopilotKit and other AG-UI-compatible applications.

What is AG-UI?

AG-UI is an event-based protocol that standardizes how AI agents connect to user interfaces. It sits alongside other agent protocols:

  • MCP - Gives agents tools
  • A2A - Allows agent-to-agent communication
  • AG-UI - Brings agents into user-facing applications

Implementation

Core Components

Plugin Structure:

  • AgUiPlugin.cs - Plugin registration and metadata
  • AgUiController.cs - SSE streaming endpoint at /ag-ui/chat
  • Event models implementing 7 AG-UI event types
  • Message converter for bidirectional AG-UI ↔ BotSharp transformation
  • Comprehensive documentation (4 guides, 1,361 lines)

Event Types Implemented:

  • text_message_start/content/end - Streaming assistant responses
  • tool_call_start/args/end - Function/tool invocations with arguments
  • tool_call_result - Tool execution results
  • state_snapshot - Conversation state synchronization
  • custom - Custom events
  • error - Error handling

Integration with BotSharp

The plugin integrates seamlessly with existing BotSharp services:

// Uses IConversationService for conversation management
var conv = _services.GetRequiredService<IConversationService>();
conv.SetConversationId(conversationId, states);

// Uses IRoutingService for agent routing  
var routing = _services.GetRequiredService<IRoutingService>();
routing.Context.SetMessageId(conversationId, messageId);

// Streams responses via SSE
await conv.SendMessage(agentId, message, null, 
    async msg => await OnChunkReceived(outputStream, msg));

Advanced Parameter Handling

The implementation carefully handles all AG-UI parameters as specified:

State Management:

{
  "state": {
    "user_name": "Alice",
    "preferences": {"theme": "dark"}
  }
}

Converted to BotSharp conversation states for persistence across turns.

Context Awareness:

{
  "context": [
    {
      "name": "document",
      "description": "Current document being edited",
      "value": "Document content..."
    }
  ]
}

Stored as context_{name} in conversation state, accessible to agents.

Tool Definitions:

{
  "tools": [
    {
      "name": "search_knowledge",
      "description": "Search the knowledge base",
      "parameters": {
        "type": "object",
        "properties": {"query": {"type": "string"}}
      }
    }
  ]
}

Serialized and stored for agent function calling.

API Endpoint

Request:

POST /ag-ui/chat
Content-Type: application/json
Authorization: Bearer {token}

Response: Server-Sent Events stream

data: {"type":"text_message_start","role":"assistant","message_id":"msg-123"}

data: {"type":"text_message_content","message_id":"msg-123","delta":"Hello! I"}

data: {"type":"text_message_content","message_id":"msg-123","delta":" can help"}

data: {"type":"text_message_end","message_id":"msg-123"}

data: {"type":"state_snapshot","snapshot":{"channel":"ag-ui"}}

Frontend Integration

CopilotKit Example

import { CopilotKit } from "@copilotkit/react-core";
import { CopilotSidebar } from "@copilotkit/react-ui";

function App() {
  return (
    <CopilotKit
      runtimeUrl="http://localhost:5000/ag-ui/chat"
      agent="01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a"
    >
      <CopilotSidebar>
        <YourApp />
      </CopilotSidebar>
    </CopilotKit>
  );
}

Custom TypeScript Client

async function* streamChat(agentId: string, message: string) {
  const response = await fetch('/ag-ui/chat', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer TOKEN'
    },
    body: JSON.stringify({
      agent_id: agentId,
      messages: [{ role: 'user', content: message }]
    })
  });

  // Parse SSE stream
  for await (const event of parseSSE(response.body)) {
    if (event.type === 'text_message_content') {
      yield event.delta; // Stream text chunks
    }
  }
}

Documentation

Comprehensive documentation provided:

  1. QUICKSTART.md - Get started in 5 minutes with curl examples
  2. README.md - Full protocol documentation and architecture overview
  3. EXAMPLES.md - Detailed code examples for curl, JavaScript, TypeScript, and React
  4. INTEGRATION.md - Step-by-step integration guide with troubleshooting

Testing

The plugin has been verified to:

  • ✅ Build successfully with 0 errors (Release & Debug)
  • ✅ Integrate with BotSharp's existing services
  • ✅ Handle all AG-UI event types correctly
  • ✅ Convert parameters properly (state, context, config, tools)
  • ✅ Stream events via SSE with proper formatting

Test with curl:

curl -X POST http://localhost:5000/ag-ui/chat \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -N \
  -d '{
    "agent_id": "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a",
    "messages": [{"role": "user", "content": "Hello"}]
  }'

Test with CopilotKit:
Use any CopilotKit example app and point it to the BotSharp /ag-ui/chat endpoint.

Benefits

  • Standardization - Uses open AG-UI protocol instead of custom API
  • Interoperability - Works with CopilotKit and other AG-UI frontends
  • Zero Dependencies - Only uses ASP.NET Core and BotSharp.Abstraction
  • Production Ready - Comprehensive error handling and logging
  • Well Documented - 4 guides with practical examples
  • Extensible - Easy to add custom event types

References

Closes #[issue-number]

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/ag-ui-protocol/ag-ui/pulls/38/files
    • Triggering command: curl -L REDACTED (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>为BotSharp添加AGUI兼容插件</issue_title>
<issue_description>请你分析AG-UI的文档了解什么是AG-UI。
https://github.com/ag-ui-protocol/ag-ui

并了解AG-UI和一些智能体Agent框架是如何结合的。

下面的.net SDK AG-UI pr作为参考
ag-ui-protocol/ag-ui#38

请你根据什么是AG-UI并且结合相关的示例和文档内容,为botsharp智能体agent框架增加一个AG-UI的实现。

主要是为了实现协议部分和目前botsharp已有的会话管理和状态管理集成,省去了这部分逻辑的实现。

我说的这些实现ag-ui功能只在单独的botsharp插件实现,请你不要随意猜测,如果不清楚,请分析botsharp的源码实现。

注意事项:

着重分析ag-ui的参数部分,将参数部分理清楚。

实现的项目可以使用目前已有的ag-ui前端代码测试。

例如直接使用CopilotKit的UI项目测试实现的botsharp ag-ui部分的功能。
https://github.com/CopilotKit/CopilotKit</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #1

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] 为BotSharp添加AGUI兼容插件 Add AG-UI Protocol Plugin for BotSharp Oct 7, 2025
Copilot AI requested a review from GreenShadeZhang October 7, 2025 11:50
Copilot finished work on behalf of GreenShadeZhang October 7, 2025 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

为BotSharp添加AGUI兼容插件

2 participants