Skip to content

Commit 6fc844e

Browse files
committed
2 parents 27d3027 + 9beea89 commit 6fc844e

File tree

20 files changed

+68
-428
lines changed

20 files changed

+68
-428
lines changed

Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@
111111
<PackageVersion Include="MSTest.TestFramework" Version="3.1.1" />
112112
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
113113
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
114-
<PackageVersion Include="ModelContextProtocol" Version="0.1.0-preview.2" />
114+
<PackageVersion Include="ModelContextProtocol" Version="0.1.0-preview.5" />
115+
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.1.0-preview.5" />
115116
</ItemGroup>
116117
<ItemGroup>
117118
<PackageVersion Include="BotSharp.Core" Version="$(BotSharpVersion)" />

src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ public class ElementLocatingArgs
3636
/// </summary>
3737
public bool Highlight { get; set; }
3838
public string HighlightColor { get; set; } = "red";
39+
[JsonPropertyName("is_read_content")]
40+
public bool IsReadContent { get;set; }
3941
}

src/Infrastructure/BotSharp.Abstraction/MCP/Models/McpServerConfigModel.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ public class McpServerConfigModel
2424
/// </summary>
2525
public string? Location { get; set; }
2626

27-
/// <summary>
28-
/// Arguments (if any) to pass to the executable.
29-
/// </summary>
30-
public string[]? Arguments { get; set; }
31-
3227
/// <summary>
3328
/// Additional transport-specific configuration.
3429
/// </summary>

src/Infrastructure/BotSharp.Core.MCP/BotSharpMCPExtensions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using BotSharp.Core.MCP.Functions;
2-
using BotSharp.Core.MCP.Settings;
32
using BotSharp.Core.MCP.Hooks;
3+
using BotSharp.Core.MCP.Managers;
4+
using BotSharp.Core.MCP.Services;
5+
using BotSharp.Core.MCP.Settings;
46
using Microsoft.Extensions.Configuration;
5-
using ModelContextProtocol.Configuration;
7+
using ModelContextProtocol;
68
using ModelContextProtocol.Client;
7-
using BotSharp.Core.MCP.Managers;
89

910
namespace BotSharp.Core.MCP;
1011

@@ -18,6 +19,7 @@ public static class BotSharpMcpExtensions
1819
/// <returns></returns>
1920
public static IServiceCollection AddBotSharpMCP(this IServiceCollection services, IConfiguration config)
2021
{
22+
services.AddScoped<IMcpService, McpService>();
2123
var settings = config.GetSection("MCP").Get<McpSettings>();
2224
services.AddScoped(provider => { return settings; });
2325

src/Infrastructure/BotSharp.Core.MCP/McpPlugin.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Infrastructure/BotSharp.Core.MCP/Services/McpService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public IEnumerable<McpServerConfigModel> GetServerConfigs()
2626
Name = x.Name,
2727
TransportType = x.TransportType,
2828
TransportOptions = x.TransportOptions,
29-
Arguments = x.Arguments,
3029
Location = x.Location
3130
});
3231
}

src/Infrastructure/BotSharp.Core.MCP/Settings/MCPSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using ModelContextProtocol.Client;
2-
using ModelContextProtocol.Configuration;
2+
using ModelContextProtocol;
33

44
namespace BotSharp.Core.MCP.Settings;
55

src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Utilities;
2+
using BotSharp.Core.Infrastructures;
23

34
namespace BotSharp.Core.Realtime.Hooks;
45

@@ -29,6 +30,10 @@ public async Task OnFunctionExecuted(RoleDialogModel message)
2930
{
3031
return;
3132
}
33+
34+
// Clear cache to force to rebuild the agent instruction
35+
Utilities.ClearCache();
36+
3237
var routing = _services.GetRequiredService<IRoutingService>();
3338

3439
message.Role = AgentRole.Function;

src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public async Task Listen(WebSocket userWebSocket,
4949
{
5050
await _completer.AppenAudioBuffer(_conn.Data);
5151
}
52+
else if (_conn.Event == "user_dtmf_receiving")
53+
{
54+
}
5255
else if (_conn.Event == "user_dtmf_received")
5356
{
5457
await HandleUserDtmfReceived();

src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,28 @@ private async Task ReceiveMessage(RealtimeHubConnection conn,
144144
Action onUserInterrupted)
145145
{
146146
var buffer = new byte[1024 * 32];
147-
WebSocketReceiveResult result;
147+
// Model response timeout
148+
var timeout = 30;
149+
WebSocketReceiveResult? result = default;
148150

149151
do
150152
{
151153
Array.Clear(buffer, 0, buffer.Length);
152-
result = await _webSocket.ReceiveAsync(
153-
new ArraySegment<byte>(buffer), CancellationToken.None);
154+
155+
var taskWorker = _webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
156+
var taskTimer = Task.Delay(1000 * timeout);
157+
var completedTask = await Task.WhenAny(taskWorker, taskTimer);
158+
159+
if (completedTask == taskWorker)
160+
{
161+
result = taskWorker.Result;
162+
}
163+
else
164+
{
165+
_logger.LogWarning($"Timeout {timeout} seconds waiting for Model response.");
166+
await TriggerModelInference("Response user immediately");
167+
continue;
168+
}
154169

155170
// Convert received data to text/audio (Twilio sends Base64-encoded audio)
156171
string receivedText = Encoding.UTF8.GetString(buffer, 0, result.Count);
@@ -164,6 +179,11 @@ private async Task ReceiveMessage(RealtimeHubConnection conn,
164179
if (response.Type == "error")
165180
{
166181
_logger.LogError($"{response.Type}: {receivedText}");
182+
var error = JsonSerializer.Deserialize<ServerEventErrorResponse>(receivedText);
183+
if (error?.Body.Type == "server_error")
184+
{
185+
break;
186+
}
167187
}
168188
else if (response.Type == "session.created")
169189
{
@@ -182,7 +202,6 @@ private async Task ReceiveMessage(RealtimeHubConnection conn,
182202
{
183203
_logger.LogInformation($"{response.Type}: {receivedText}");
184204
var data = JsonSerializer.Deserialize<ResponseAudioTranscript>(receivedText);
185-
await Task.Delay(1000);
186205
onModelAudioTranscriptDone(data.Transcript);
187206
}
188207
else if (response.Type == "response.audio.delta")

src/Plugins/BotSharp.Plugin.Twilio/TwilioStreamMiddleware.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BotSharp.Abstraction.Realtime;
2-
using BotSharp.Abstraction.Realtime.Models;
32
using BotSharp.Plugin.Twilio.Interfaces;
43
using BotSharp.Plugin.Twilio.Models.Stream;
54
using Microsoft.AspNetCore.Http;
@@ -100,6 +99,7 @@ await hub.Listen(webSocket, (receivedText) =>
10099
}
101100
else
102101
{
102+
conn.Event = "user_dtmf_receiving";
103103
conn.KeypadInputBuffer += dtmfResponse.Body.Digit;
104104
}
105105
break;
@@ -115,19 +115,29 @@ await hub.Listen(webSocket, (receivedText) =>
115115
streamSid = response.StreamSid,
116116
media = new { payload = message }
117117
};
118+
118119
conn.OnModelAudioResponseDone = () =>
119120
new
120121
{
121122
@event = "mark",
122123
streamSid = response.StreamSid,
123124
mark = new { name = "responsePart" }
124125
};
126+
125127
conn.OnModelUserInterrupted = () =>
126128
new
127129
{
128130
@event = "clear",
129131
streamSid = response.StreamSid
130132
};
133+
134+
/*if (response.Event == "dtmf")
135+
{
136+
// Send a Stop command to Twilio
137+
string stopPlaybackCommand = "{ \"action\": \"stop_playback\" }";
138+
var stopBytes = Encoding.UTF8.GetBytes(stopPlaybackCommand);
139+
webSocket.SendAsync(new ArraySegment<byte>(stopBytes), WebSocketMessageType.Text, true, CancellationToken.None);
140+
}*/
131141
});
132142
}
133143
}

src/Plugins/BotSharp.Plugin.WebDriver/UtilFunctions/UtilWebLocateElementFn.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ public async Task<bool> Execute(RoleDialogModel message)
3232
};
3333
var result = await browser.LocateElement(msg, locatorArgs);
3434

35-
message.Content = $"Locating element {(result.IsSuccess ? "success" : "failed")}";
35+
message.Content = $"Locating element {(result.IsSuccess ? "success" : "failed")}. ";
36+
if (locatorArgs.IsReadContent && result.IsSuccess && !string.IsNullOrWhiteSpace(result.Body))
37+
{
38+
message.Content += $"Content is: \n{result.Body}";
39+
}
3640

3741
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
3842

src/Plugins/BotSharp.Plugin.WebDriver/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/util-web-locate_element.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
"selector": {
88
"type": "string",
99
"description": "element selector in XPath, use syntax of Playwright in .NET"
10+
},
11+
"is_read_content": {
12+
"type": "boolean",
13+
"description": "read the content"
1014
}
1115
},
1216
"required": [ "selector" ]

tests/BotSharp.PizzaBot.MCPServer/BotSharp.PizzaBot.MCPServer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageReference Include="Microsoft.Extensions.Hosting" />
1111
<PackageReference Include="Microsoft.Extensions.Logging" />
1212
<PackageReference Include="ModelContextProtocol" />
13+
<PackageReference Include="ModelContextProtocol.AspNetCore" />
1314
<PackageReference Include="Serilog.Extensions.Logging" />
1415
<PackageReference Include="Serilog.Sinks.File" />
1516
</ItemGroup>

tests/BotSharp.PizzaBot.MCPServer/McpEndpointRouteBuilderExtensions.cs

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)