-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathAddConversationMessageTemplate.tt
More file actions
67 lines (63 loc) · 2.84 KB
/
AddConversationMessageTemplate.tt
File metadata and controls
67 lines (63 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.Extensions" #>
<#@ import namespace="Microsoft.Agents.ObjectModel" #>
<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #>
<#@ include file="Snippets/EvaluateRecordExpressionTemplate.tt" once="true" #>
<#@ include file="Snippets/EvaluateStringExpressionTemplate.tt" once="true" #>
<#@ include file="Snippets/FormatMessageTemplate.tt" once="true" #>
/// <summary>
/// Adds a new message to the specified agent conversation
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session, ResponseAgentProvider agentProvider) : ActionExecutor(id: "<#= this.Id #>", session)
{
// <inheritdoc />
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{<#
EvaluateStringExpression(this.Model.ConversationId, "conversationId", isNullable: true); #>
if (string.IsNullOrWhiteSpace(conversationId))
{
throw new DeclarativeActionException($"Conversation identifier must be defined: {this.Id}");
}
ChatMessage newMessage = new(ChatRole.<#= FormatEnum(this.Model.Role, RoleMap) #>, await this.GetContentAsync(context).ConfigureAwait(false)) { AdditionalProperties = this.GetMetadata() };
newMessage = await agentProvider.CreateMessageAsync(conversationId, newMessage, cancellationToken).ConfigureAwait(false);<#
AssignVariable(this.Message, "newMessage");
#>
return default;
}
private async ValueTask<IList<AIContent>> GetContentAsync(IWorkflowContext context)
{
List<AIContent> content = [];
<#
int index = 0;
foreach (AddConversationMessageContent content in this.Model.Content)
{
++index;
EvaluateMessageTemplate(content.Value, $"contentValue{index}");
AgentMessageContentType contentType = content.Type.Value;
if (contentType == AgentMessageContentType.ImageUrl)
{#>
content.Add(UriContent(contentValue<#= index #>, "image/*"));<#
}
else if (contentType == AgentMessageContentType.ImageFile)
{#>
content.Add(new HostedFileContent(contentValue<#= index #>));<#
}
else
{#>
content.Add(new TextContent(contentValue<#= index #>));<#
}
}#>
return content;
}
private AdditionalPropertiesDictionary? GetMetadata()
{<#
EvaluateRecordExpression<object>(this.Model.Metadata, "metadata"); #>
if (metadata is null)
{
return null;
}
return new AdditionalPropertiesDictionary(metadata);
}
}