Skip to content

Commit c837dd6

Browse files
author
Jicheng Lu
committed
temp save
1 parent 710a1e1 commit c837dd6

9 files changed

Lines changed: 316 additions & 34 deletions

File tree

src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
<None Remove="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\templates\instruction.simulator.liquid" />
9797
<None Remove="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\templates\instruction.simulator.liquid" />
9898
<None Remove="data\plugins\config.json" />
99+
100+
<None Remove="data\agents\01e2fc5c-2c89-4ec7-8470-7688608b496c\functions\get_weather.json" />
99101
</ItemGroup>
100102

101103
<ItemGroup>
@@ -204,6 +206,14 @@
204206
<Content Include="data\plugins\config.json">
205207
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
206208
</Content>
209+
210+
211+
<Content Include="data\agents\01e2fc5c-2c89-4ec7-8470-7688608b496c\functions\get_weather.json">
212+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
213+
</Content>
214+
<Content Include="data\agents\01e2fc5c-2c89-4ec7-8470-7688608b496c\functions\get_location.json">
215+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
216+
</Content>
207217
</ItemGroup>
208218

209219
<ItemGroup>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using BotSharp.Abstraction.Functions;
2+
using BotSharp.Abstraction.Options;
3+
4+
namespace BotSharp.Core.Functions;
5+
6+
public class GetLocationFn : IFunctionCallback
7+
{
8+
private readonly IServiceProvider _services;
9+
10+
public GetLocationFn(IServiceProvider services)
11+
{
12+
_services = services;
13+
}
14+
15+
public string Name => "get_location";
16+
public string Indication => "Finding location";
17+
18+
public async Task<bool> Execute(RoleDialogModel message)
19+
{
20+
var args = JsonSerializer.Deserialize<Location>(message.FunctionArgs, BotSharpOptions.defaultJsonOptions);
21+
22+
message.Content = $"There are a lot of fun events here in {args.City}";
23+
return true;
24+
}
25+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using BotSharp.Abstraction.Functions;
2+
using BotSharp.Abstraction.Models;
3+
using BotSharp.Abstraction.Options;
4+
using BotSharp.Abstraction.SideCar;
5+
using System.Text.Json.Serialization;
6+
7+
namespace BotSharp.Core.Functions;
8+
9+
public class GetWeatherFn : IFunctionCallback
10+
{
11+
private readonly IServiceProvider _services;
12+
13+
public GetWeatherFn(IServiceProvider services)
14+
{
15+
_services = services;
16+
}
17+
18+
public string Name => "get_weather";
19+
public string Indication => "Querying weather";
20+
21+
public async Task<bool> Execute(RoleDialogModel message)
22+
{
23+
var args = JsonSerializer.Deserialize<Location>(message.FunctionArgs, BotSharpOptions.defaultJsonOptions);
24+
25+
var sidecar = _services.GetService<IConversationSideCar>();
26+
var states = GetSideCarStates();
27+
28+
var userMessage = $"Please find the information at location {args.City}, {args.State}";
29+
var response = await sidecar.SendMessage(BuiltInAgentId.Chatbot, userMessage, states: states);
30+
message.Content = $"It is a sunny day {response.Content}.";
31+
return true;
32+
}
33+
34+
private List<MessageState> GetSideCarStates()
35+
{
36+
var sideCarStates = new List<MessageState>()
37+
{
38+
new("channel", "email")
39+
};
40+
return sideCarStates;
41+
}
42+
}
43+
44+
class Location
45+
{
46+
[JsonPropertyName("city")]
47+
public string? City { get; set; }
48+
49+
[JsonPropertyName("state")]
50+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
51+
public string? State { get; set; }
52+
53+
[JsonPropertyName("county")]
54+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
55+
public string? County { get; set; }
56+
}

src/Infrastructure/BotSharp.Core/Infrastructures/Websocket/AsyncWebsocketDataResultEnumerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public async ValueTask<bool> MoveNextAsync()
4444

4545
if (receivedResult.CloseStatus.HasValue)
4646
{
47+
Console.WriteLine($"Web socket close status: {receivedResult.CloseStatus}");
4748
Current = null;
4849
return false;
4950
}

src/Infrastructure/BotSharp.Core/Session/LlmRealtimeSession.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ public LlmRealtimeSession(
2222
_sessionOptions = sessionOptions;
2323
}
2424

25-
public async Task ConnectAsync(Uri uri, Dictionary<string, string> headers, CancellationToken cancellationToken = default)
25+
public async Task ConnectAsync(Uri uri, Dictionary<string, string>? headers = null, CancellationToken cancellationToken = default)
2626
{
2727
_webSocket?.Dispose();
2828
_webSocket = new ClientWebSocket();
2929

30-
foreach (var header in headers)
30+
if (!headers.IsNullOrEmpty())
3131
{
32-
_webSocket.Options.SetRequestHeader(header.Key, header.Value);
32+
foreach (var header in headers)
33+
{
34+
_webSocket.Options.SetRequestHeader(header.Key, header.Value);
35+
}
3336
}
3437

3538
await _webSocket.ConnectAsync(uri, cancellationToken);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "get_location",
3+
"description": "Get location information for user.",
4+
"parameters": {
5+
"type": "object",
6+
"properties": {
7+
"city": {
8+
"type": "string",
9+
"visibility_expression": "{% if states.channel == 'email' %}visible{% endif %}",
10+
"description": "The location city that user wants to know about."
11+
},
12+
"county": {
13+
"type": "string",
14+
"visibility_expression": "{% if states.channel != 'email' %}visible{% endif %}",
15+
"description": "The location county that user wants to know about."
16+
}
17+
},
18+
"required": [ "city", "county" ]
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "get_weather",
3+
"description": "Get weather information for user.",
4+
"visibility_expression": "{% if states.channel != 'email' %}visible{% endif %}",
5+
"parameters": {
6+
"type": "object",
7+
"properties": {
8+
"city": {
9+
"type": "string",
10+
"description": "The city where the user wants to get weather information."
11+
},
12+
"state": {
13+
"type": "string",
14+
"description": "The state where the user wants to get weather information."
15+
}
16+
},
17+
"required": [ "city", "state" ]
18+
}
19+
}

0 commit comments

Comments
 (0)