Skip to content

Commit 6cb4aa2

Browse files
authored
Merge pull request #292 from hchen2020/master
Add hook of OnDialogRecordLoaded.
2 parents 1d69fe1 + 5c852df commit 6cb4aa2

File tree

23 files changed

+117
-31
lines changed

23 files changed

+117
-31
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public virtual Task OnStateChanged(string name, string preValue, string currentV
3636
return Task.CompletedTask;
3737
}
3838

39+
public virtual Task OnDialogRecordLoaded(RoleDialogModel dialog)
40+
{
41+
return Task.CompletedTask;
42+
}
43+
3944
public virtual Task OnDialogsLoaded(List<RoleDialogModel> dialogs)
4045
{
4146
_dialogs = dialogs;

src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ public interface IConversationHook
3232
/// <returns></returns>
3333
Task OnDialogsLoaded(List<RoleDialogModel> dialogs);
3434

35+
/// <summary>
36+
/// Triggered when every dialog record is loaded
37+
/// It can be used to populate extra data point before presenting to user.
38+
/// </summary>
39+
/// <param name="dialog"></param>
40+
/// <returns></returns>
41+
Task OnDialogRecordLoaded(RoleDialogModel dialog);
42+
3543
Task OnStateLoaded(ConversationState state);
3644
Task OnStateChanged(string name, string preValue, string currentValue);
3745

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public List<RoleDialogModel> GetDialogs(string conversationId)
6767
{
6868
var db = _services.GetRequiredService<IBotSharpRepository>();
6969
var dialogs = db.GetConversationDialogs(conversationId);
70+
var hooks = _services.GetServices<IConversationHook>();
7071

7172
var results = new List<RoleDialogModel>();
7273
foreach (var dialog in dialogs)
@@ -79,16 +80,28 @@ public List<RoleDialogModel> GetDialogs(string conversationId)
7980
var function = role == AgentRole.Function ? meta.FunctionName : null;
8081
var senderId = role == AgentRole.Function ? currentAgentId : meta.SenderId;
8182
var createdAt = meta.CreateTime;
82-
83-
results.Add(new RoleDialogModel(role, content)
83+
84+
var record = new RoleDialogModel(role, content)
8485
{
8586
CurrentAgentId = currentAgentId,
8687
MessageId = messageId,
8788
CreatedAt = createdAt,
8889
SenderId = senderId,
8990
FunctionName = function
90-
});
91+
};
92+
results.Add(record);
93+
94+
foreach(var hook in hooks)
95+
{
96+
hook.OnDialogRecordLoaded(record).Wait();
97+
}
9198
}
99+
100+
foreach (var hook in hooks)
101+
{
102+
hook.OnDialogsLoaded(results).Wait();
103+
}
104+
92105
return results;
93106
}
94107

Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
Use will give you a task list with steps, which is going to be executed.
1+
User will give you a task list with steps, which is going to be executed.
22
If a specific step has been exectued, you will get something indicates the result.
33
If there is no any result provided, it means all the steps have not been executed yet.
44
You need to figure out which steps have not been completed.
55
Tell me what is the first remaining step from user steps that have not been completed based on the context.
66
Output in JSON
77
{
88
"description": "step detail with arguments",
9-
"total_remaining_steps": 0,
9+
"total_remaining_steps": -1,
1010
"should_stop": false,
1111
"stop_reason": "the reason why it should total remaining steps is zero"
1212
}

src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public async Task<IEnumerable<ChatResponseModel>> GetDialogs([FromRoute] string
8383
MessageId = message.MessageId,
8484
CreatedAt = message.CreatedAt,
8585
Text = message.Content,
86+
Data = message.Data,
8687
Sender = UserViewModel.FromUser(user)
8788
});
8889
}
@@ -95,6 +96,7 @@ public async Task<IEnumerable<ChatResponseModel>> GetDialogs([FromRoute] string
9596
MessageId = message.MessageId,
9697
CreatedAt = message.CreatedAt,
9798
Text = message.Content,
99+
Data = message.Data,
98100
Sender = new UserViewModel
99101
{
100102
FirstName = agent.Name,

src/Plugins/BotSharp.Plugin.Dashboard/DashboardPlugin.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
2525

2626
public bool AttachMenu(List<PluginMenuDef> menu)
2727
{
28-
var section = menu.First(x => x.Label == "Apps");
29-
menu.Add(new PluginMenuDef("Dashboard", link: "page/dashboard", icon: "bx bx-home-circle", weight: section.Weight - 1));
28+
menu.Add(new PluginMenuDef("Dashboard", link: "page/dashboard", icon: "bx bx-home-circle", weight: 1));
3029
return true;
3130
}
3231
}

src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public async Task InitInstance()
3636
Channel = "chrome",
3737
IgnoreDefaultArgs = new[]
3838
{
39-
"enable-automation"
39+
"--disable-infobars"
4040
},
4141
Args = new[]
4242
{
43-
"--disable-infobars"
43+
"--disable-infobars",
4444
// "--start-maximized"
4545
}
4646
});

src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.ChangeCheckbox.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ public async Task<bool> ChangeCheckbox(BrowserActionParams actionParams)
6868
try
6969
{
7070
var isChecked = await elements.IsCheckedAsync();
71-
if (actionParams.Context.UpdateValue == "checked" && !isChecked)
71+
if (actionParams.Context.UpdateValue == "check" && !isChecked)
7272
{
7373
await elements.ClickAsync();
7474
}
75-
else if (actionParams.Context.UpdateValue == "unchecked" && isChecked)
75+
else if (actionParams.Context.UpdateValue == "uncheck" && isChecked)
7676
{
7777
await elements.ClickAsync();
7878
}

src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.Screenshot.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ public async Task<string> ScreenshotAsync(string path)
1010
Path = path,
1111
});
1212

13-
return Convert.ToBase64String(bytes);
13+
return "data:image/png;base64," + Convert.ToBase64String(bytes);
1414
}
1515
}

src/Plugins/BotSharp.Plugin.WebDriver/Functions/ChangeCheckboxFn.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ public async Task<bool> Execute(RoleDialogModel message)
2222
var agent = await agentService.LoadAgent(message.CurrentAgentId);
2323
var result = await _browser.ChangeCheckbox(new BrowserActionParams(agent, args, message.MessageId));
2424

25-
message.Content = result ? "Success" : "Failed";
25+
message.Content = result ?
26+
$"{(args.UpdateValue == "check" ? "Checked" : "Unchecked")} checkbox of '{args.ElementText}' successfully" :
27+
"Failed";
2628

2729
var webDriverService = _services.GetRequiredService<WebDriverService>();
28-
var path = webDriverService.NewScreenshotFilePath(message.MessageId);
30+
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
2931

3032
message.Data = await _browser.ScreenshotAsync(path);
3133

0 commit comments

Comments
 (0)