forked from microsoft/agent-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDurableAIAgentProxy.cs
More file actions
96 lines (79 loc) · 3.79 KB
/
DurableAIAgentProxy.cs
File metadata and controls
96 lines (79 loc) · 3.79 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) Microsoft. All rights reserved.
using System.Text.Json;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.DurableTask;
internal class DurableAIAgentProxy(string name, IDurableAgentClient agentClient) : AIAgent
{
private readonly IDurableAgentClient _agentClient = agentClient;
public override string? Name { get; } = name;
public override JsonElement SerializeSession(AgentSession session, JsonSerializerOptions? jsonSerializerOptions = null)
{
if (session is null)
{
throw new ArgumentNullException(nameof(session));
}
if (session is not DurableAgentSession durableSession)
{
throw new InvalidOperationException("The provided session is not compatible with the agent. Only sessions created by the agent can be serialized.");
}
return durableSession.Serialize(jsonSerializerOptions);
}
public override ValueTask<AgentSession> DeserializeSessionAsync(
JsonElement serializedSession,
JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
{
return ValueTask.FromResult<AgentSession>(DurableAgentSession.Deserialize(serializedSession, jsonSerializerOptions));
}
public override ValueTask<AgentSession> CreateSessionAsync(CancellationToken cancellationToken = default)
{
return ValueTask.FromResult<AgentSession>(new DurableAgentSession(AgentSessionId.WithRandomKey(this.Name!)));
}
protected override async Task<AgentResponse> RunCoreAsync(
IEnumerable<ChatMessage> messages,
AgentSession? session = null,
AgentRunOptions? options = null,
CancellationToken cancellationToken = default)
{
session ??= await this.CreateSessionAsync(cancellationToken).ConfigureAwait(false);
if (session is not DurableAgentSession durableSession)
{
throw new ArgumentException(
"The provided session is not valid for a durable agent. " +
"Create a new session using CreateSessionAsync or provide a session previously created by this agent.",
paramName: nameof(session));
}
IList<string>? enableToolNames = null;
bool enableToolCalls = true;
ChatResponseFormat? responseFormat = null;
bool isFireAndForget = false;
if (options is DurableAgentRunOptions durableOptions)
{
enableToolCalls = durableOptions.EnableToolCalls;
enableToolNames = durableOptions.EnableToolNames;
responseFormat = durableOptions.ResponseFormat;
isFireAndForget = durableOptions.IsFireAndForget;
}
else if (options is ChatClientAgentRunOptions chatClientOptions)
{
// Honor the response format from the chat client options if specified
responseFormat = chatClientOptions.ChatOptions?.ResponseFormat;
}
RunRequest request = new([.. messages], responseFormat, enableToolCalls, enableToolNames);
AgentSessionId sessionId = durableSession.SessionId;
AgentRunHandle agentRunHandle = await this._agentClient.RunAgentAsync(sessionId, request, cancellationToken);
if (isFireAndForget)
{
// If the request is fire and forget, return an empty response.
return new AgentResponse();
}
return await agentRunHandle.ReadAgentResponseAsync(cancellationToken);
}
protected override IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingAsync(
IEnumerable<ChatMessage> messages,
AgentSession? session = null,
AgentRunOptions? options = null,
CancellationToken cancellationToken = default)
{
throw new NotSupportedException("Streaming is not supported for durable agents.");
}
}