Skip to content

Commit ce278ff

Browse files
authored
Merge pull request #396 from hchen2020/master
PostbackFunctionName
2 parents 054b898 + 3c968bd commit ce278ff

File tree

5 files changed

+70
-31
lines changed

5 files changed

+70
-31
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public class RoleDialogModel : ITrackableMessage
3535
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3636
public string? FunctionName { get; set; }
3737

38+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
39+
public string? PostbackFunctionName { get; set; }
40+
3841
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3942
public string? FunctionArgs { get; set; }
4043

@@ -95,6 +98,7 @@ public static RoleDialogModel From(RoleDialogModel source,
9598
MessageId = source.MessageId,
9699
FunctionArgs = source.FunctionArgs,
97100
FunctionName = source.FunctionName,
101+
PostbackFunctionName = source.PostbackFunctionName,
98102
RichContent = source.RichContent,
99103
StopCompletion = source.StopCompletion,
100104
Instruction = source.Instruction,

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,13 @@ public async Task<bool> SendMessage(string agentId,
7272
}
7373

7474
// Persist to storage
75-
_storage.Append(_conversationId, message);
75+
if (!message.StopCompletion)
76+
{
77+
_storage.Append(_conversationId, message);
7678

77-
// Add to thread
78-
dialogs.Add(RoleDialogModel.From(message));
79+
// Add to thread
80+
dialogs.Add(RoleDialogModel.From(message));
81+
}
7982

8083
if (!stopCompletion)
8184
{
@@ -147,6 +150,12 @@ response.RichContent is RichContent<IRichMessage> template &&
147150
Message = new TextMessage(response.Content)
148151
};
149152

153+
// Patch return function name
154+
if (response.PostbackFunctionName != null)
155+
{
156+
response.FunctionName = response.PostbackFunctionName;
157+
}
158+
150159
var hooks = _services.GetServices<IConversationHook>().ToList();
151160

152161
if (response.Instruction != null)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public void Append(string conversationId, RoleDialogModel dialog)
2828
var dialogElements = new List<DialogElement>();
2929

3030
// Prevent duplicate record to be inserted
31-
var dialogs = db.GetConversationDialogs(conversationId);
31+
/*var dialogs = db.GetConversationDialogs(conversationId);
3232
if (dialogs.Any(x => x.MetaData.MessageId == dialog.MessageId && x.Content == dialog.Content))
3333
{
3434
return;
35-
}
35+
}*/
3636

3737
if (dialog.Role == AgentRole.Function)
3838
{

src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeFunction.cs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace BotSharp.Core.Routing;
55
public partial class RoutingService
66
{
77
private List<FunctionCallingResponse> _functionCallStack = new List<FunctionCallingResponse>();
8+
public List<FunctionCallingResponse> FunctionCallStack => _functionCallStack;
9+
810
public async Task<bool> InvokeFunction(string name, RoleDialogModel message)
911
{
1012
var function = _services.GetServices<IFunctionCallback>().FirstOrDefault(x => x.Name == name);
@@ -16,10 +18,9 @@ public async Task<bool> InvokeFunction(string name, RoleDialogModel message)
1618
return false;
1719
}
1820

19-
var originalFunctionName = message.FunctionName;
20-
message.FunctionName = name;
21-
message.Role = AgentRole.Function;
22-
message.FunctionArgs = message.FunctionArgs;
21+
// Clone message
22+
var clonedMessage = RoleDialogModel.From(message);
23+
clonedMessage.FunctionName = name;
2324

2425
var hooks = _services.GetServices<IConversationHook>()
2526
.OrderBy(x => x.Priority)
@@ -28,21 +29,36 @@ public async Task<bool> InvokeFunction(string name, RoleDialogModel message)
2829
// Before executing functions
2930
foreach (var hook in hooks)
3031
{
31-
await hook.OnFunctionExecuting(message);
32+
await hook.OnFunctionExecuting(clonedMessage);
3233
}
3334

3435
bool result = false;
3536

3637
try
3738
{
38-
result = await function.Execute(message);
39+
result = await function.Execute(clonedMessage);
40+
3941
_functionCallStack.Add(new FunctionCallingResponse
4042
{
4143
Role = AgentRole.Function,
42-
FunctionName = message.FunctionName,
43-
Args = JsonDocument.Parse(message.FunctionArgs ?? "{}"),
44-
Content = message.Content
44+
FunctionName = clonedMessage.FunctionName,
45+
Args = JsonDocument.Parse(clonedMessage.FunctionArgs ?? "{}"),
46+
Content = clonedMessage.Content
4547
});
48+
49+
// After functions have been executed
50+
foreach (var hook in hooks)
51+
{
52+
await hook.OnFunctionExecuted(clonedMessage);
53+
}
54+
55+
// Set result to original message
56+
message.PostbackFunctionName = clonedMessage.PostbackFunctionName;
57+
message.CurrentAgentId = clonedMessage.CurrentAgentId;
58+
message.Content = clonedMessage.Content;
59+
message.StopCompletion = clonedMessage.StopCompletion;
60+
message.RichContent = clonedMessage.RichContent;
61+
message.Data = clonedMessage.Data;
4662
}
4763
catch (JsonException ex)
4864
{
@@ -63,25 +79,12 @@ public async Task<bool> InvokeFunction(string name, RoleDialogModel message)
6379
message.Content = JsonSerializer.Serialize(message.Data);
6480
}
6581

66-
// After functions have been executed
67-
foreach (var hook in hooks)
68-
{
69-
await hook.OnFunctionExecuted(message);
70-
}
71-
72-
// restore original function name
73-
if (!message.StopCompletion &&
74-
message.FunctionName != originalFunctionName)
75-
{
76-
message.FunctionName = originalFunctionName;
77-
}
78-
7982
// Save to Storage as well
80-
if (!message.StopCompletion && message.FunctionName != "route_to_agent")
83+
/*if (!message.StopCompletion && message.FunctionName != "route_to_agent")
8184
{
8285
var storage = _services.GetRequiredService<IConversationStorage>();
8386
storage.Append(Context.ConversationId, message);
84-
}
87+
}*/
8588

8689
return result;
8790
}

src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversati
9898
if (!_convSettings.ShowVerboseLog) return;
9999
}
100100

101-
public override async Task OnFunctionExecuted(RoleDialogModel message)
101+
public override async Task OnFunctionExecuting(RoleDialogModel message)
102102
{
103103
if (message.FunctionName == "route_to_agent")
104104
{
@@ -109,7 +109,30 @@ public override async Task OnFunctionExecuted(RoleDialogModel message)
109109
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
110110
message.FunctionArgs = message.FunctionArgs ?? "{}";
111111
var args = JsonSerializer.Serialize(JsonDocument.Parse(message.FunctionArgs), _options.JsonSerializerOptions);
112-
var log = $"*{message.FunctionName}*\r\n```json\r\n{args}\r\n```\r\n=> {message.Content?.Trim()}";
112+
var log = $"{message.FunctionName} <u>executing</u>\r\n```json\r\n{args}\r\n```";
113+
114+
var input = new ContentLogInputModel(conversationId, message)
115+
{
116+
Name = agent?.Name,
117+
AgentId = agent?.Id,
118+
Source = ContentLogSource.FunctionCall,
119+
Log = log
120+
};
121+
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(input));
122+
}
123+
124+
public override async Task OnFunctionExecuted(RoleDialogModel message)
125+
{
126+
if (message.FunctionName == "route_to_agent")
127+
{
128+
return;
129+
}
130+
131+
var conversationId = _state.GetConversationId();
132+
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
133+
message.FunctionArgs = message.FunctionArgs ?? "{}";
134+
// var args = JsonSerializer.Serialize(JsonDocument.Parse(message.FunctionArgs), _options.JsonSerializerOptions);
135+
var log = $"{message.FunctionName} =>\r\n*{message.Content?.Trim()}*";
113136

114137
var input = new ContentLogInputModel(conversationId, message)
115138
{

0 commit comments

Comments
 (0)