Skip to content

Commit 717ec7a

Browse files
author
Jicheng Lu
committed
add action step result
1 parent b0dfa76 commit 717ec7a

6 files changed

Lines changed: 49 additions & 11 deletions

File tree

src/Infrastructure/BotSharp.Abstraction/Rules/Models/RuleActionContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ public class RuleActionContext
66
{
77
public string Text { get; set; } = string.Empty;
88
public Dictionary<string, object?> Parameters { get; set; } = [];
9+
public IEnumerable<RuleActionStepResult> PrevStepResults { get; set; } = [];
910
public JsonSerializerOptions? JsonOptions { get; set; }
1011
}

src/Infrastructure/BotSharp.Abstraction/Rules/Models/RuleActionResult.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ public class RuleActionResult
1111
public bool Success { get; set; }
1212

1313
/// <summary>
14-
/// The conversation ID if a new conversation was created
14+
/// Response content from the action
1515
/// </summary>
16-
public string? ConversationId { get; set; }
16+
public string? Response { get; set; }
1717

1818
/// <summary>
19-
/// Response content from the action
19+
/// Result data
2020
/// </summary>
21-
public string? Response { get; set; }
21+
public Dictionary<string, object> Data { get; set; } = [];
2222

2323
/// <summary>
2424
/// Error message if the action failed
@@ -44,3 +44,7 @@ public static RuleActionResult Failed(string errorMessage)
4444
}
4545
}
4646

47+
public class RuleActionStepResult : RuleActionResult
48+
{
49+
public AgentRuleAction RuleAction { get; set; }
50+
}

src/Infrastructure/BotSharp.Core.Rules/Actions/ChatRuleAction.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ await convService.SendMessage(agent.Id,
6060
return new RuleActionResult
6161
{
6262
Success = true,
63-
ConversationId = conv.Id
63+
Data = new()
64+
{
65+
["agent_id"] = agent.Id,
66+
["conversation_id"] = conv.Id
67+
}
6468
};
6569
}
6670
catch (Exception ex)

src/Infrastructure/BotSharp.Core.Rules/Actions/FunctionCallRuleAction.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ public async Task<RuleActionResult> ExecuteAsync(
3838
return new RuleActionResult
3939
{
4040
Success = true,
41-
Response = funcArg?.RichContent?.Message?.Text ?? funcArg?.Content
41+
Response = funcArg?.RichContent?.Message?.Text ?? funcArg?.Content,
42+
Data = new()
43+
{
44+
["function_name"] = funcName!,
45+
["function_argument"] = funcArg!,
46+
["function_call_result"] = funcArg?.RichContent?.Message?.Text ?? funcArg?.Content ?? string.Empty
47+
}
4248
};
4349
}
4450
}

src/Infrastructure/BotSharp.Core.Rules/Actions/HttpRuleAction.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ public async Task<RuleActionResult> ExecuteAsync(
7979
return new RuleActionResult
8080
{
8181
Success = true,
82-
Response = responseContent
82+
Response = responseContent,
83+
Data = new()
84+
{
85+
["http_response"] = responseContent
86+
}
8387
};
8488
}
8589
else

src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,29 @@ public async Task<IEnumerable<string>> Triggered(IRuleTrigger trigger, string te
5858
continue;
5959
}
6060

61+
var stepResults = new List<RuleActionStepResult>();
6162
foreach (var ruleAction in ruleActions)
6263
{
63-
var actionResult = await ExecuteActionAsync(agent, ruleAction, trigger, text, states, options);
64-
if (actionResult?.Success == true && !string.IsNullOrEmpty(actionResult.ConversationId))
64+
var actionResult = await ExecuteActionAsync(agent, ruleAction, trigger, text, states, options, stepResults);
65+
if (actionResult == null)
6566
{
66-
newConversationIds.Add(actionResult.ConversationId);
67+
continue;
68+
}
69+
70+
stepResults.Add(new()
71+
{
72+
RuleAction = ruleAction,
73+
Success = actionResult.Success,
74+
Response = actionResult.Response,
75+
ErrorMessage = actionResult.ErrorMessage,
76+
Data = actionResult.Data
77+
});
78+
79+
if (actionResult?.Success == true
80+
&& actionResult.Data.TryGetValue("conversation_id", out var convId)
81+
&& convId != null)
82+
{
83+
newConversationIds.Add(convId.ToString()!);
6784
}
6885
}
6986
}
@@ -137,7 +154,8 @@ private async Task<RuleActionResult> ExecuteActionAsync(
137154
IRuleTrigger trigger,
138155
string text,
139156
IEnumerable<MessageState>? states,
140-
RuleTriggerOptions? triggerOptions)
157+
RuleTriggerOptions? triggerOptions,
158+
IEnumerable<RuleActionStepResult> prevStepResults)
141159
{
142160
try
143161
{
@@ -158,6 +176,7 @@ private async Task<RuleActionResult> ExecuteActionAsync(
158176
{
159177
Text = text,
160178
Parameters = BuildContextParameters(ruleAction.Config, states),
179+
PrevStepResults = prevStepResults,
161180
JsonOptions = triggerOptions?.JsonOptions
162181
};
163182

0 commit comments

Comments
 (0)