Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update AgUiController
  • Loading branch information
GreenShadeZhang committed Oct 7, 2025
commit eb58f63c2176f1720873739a0881cc7fc552bfbe
43 changes: 25 additions & 18 deletions src/Plugins/BotSharp.Plugin.AgUi/Controllers/AgUiController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Conversations.Models;
Expand All @@ -18,6 +11,14 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading.Tasks;

namespace BotSharp.Plugin.AgUi.Controllers;

Expand All @@ -27,11 +28,20 @@ public class AgUiController : ControllerBase
{
private readonly IServiceProvider _services;
private readonly ILogger<AgUiController> _logger;
private JsonSerializerOptions _options;

public AgUiController(ILogger<AgUiController> logger, IServiceProvider services)
{
_logger = logger;
_services = services;
_options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
AllowTrailingCommas = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
}

/// <summary>
Expand Down Expand Up @@ -62,15 +72,16 @@ public async Task Chat([FromBody] AgUiMessageInput input)
// Convert all messages to BotSharp format for conversation history
// This helps the agent understand the full context
var allMessages = AgUiMessageConverter.ConvertToBotSharpMessages(input.Messages);

// Get the current user message
var message = allMessages.LastOrDefault(m => m.Role == AgentRole.User);

if (message == null)
{
await SendErrorEvent(outputStream, "Failed to convert user message");
return;
}

message.IsStreaming = true;
// Get conversation service
var conv = _services.GetRequiredService<IConversationService>();
var routing = _services.GetRequiredService<IRoutingService>();
Expand All @@ -81,7 +92,7 @@ public async Task Chat([FromBody] AgUiMessageInput input)

// Set conversation state
var states = new List<MessageState>();

// Add AG-UI state
if (input.State != null)
{
Expand All @@ -95,7 +106,7 @@ public async Task Chat([FromBody] AgUiMessageInput input)
});
}
}

// Add AG-UI context
if (input.Context != null && input.Context.Any())
{
Expand All @@ -109,7 +120,7 @@ public async Task Chat([FromBody] AgUiMessageInput input)
});
}
}

// Add AG-UI config
if (input.Config != null)
{
Expand All @@ -125,7 +136,7 @@ public async Task Chat([FromBody] AgUiMessageInput input)

conv.SetConversationId(conversationId, states);
conv.States.SetState("channel", "ag-ui");

// Store tools information if provided
if (input.Tools != null && input.Tools.Any())
{
Expand Down Expand Up @@ -283,11 +294,7 @@ private async Task SendErrorEvent(Stream outputStream, string errorMessage)

private async Task SendEvent(Stream outputStream, AgUiEvent eventData)
{
var json = JsonSerializer.Serialize(eventData, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
});
var json = JsonSerializer.Serialize(eventData, eventData.GetType(), _options);

var buffer = Encoding.UTF8.GetBytes($"data: {json}\n\n");
await outputStream.WriteAsync(buffer, 0, buffer.Length);
Expand Down
1 change: 1 addition & 0 deletions src/WebStarter/WebStarter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<ItemGroup>
<ProjectReference Include="..\..\tests\BotSharp.Plugin.PizzaBot\BotSharp.Plugin.PizzaBot.csproj" />
<ProjectReference Include="..\BotSharp.ServiceDefaults\BotSharp.ServiceDefaults.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.AgUi\BotSharp.Plugin.AgUi.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.ExcelHandler\BotSharp.Plugin.ExcelHandler.csproj" />
</ItemGroup>

Expand Down