14
14
namespace Microsoft . SemanticKernel . Agents . OpenAI ;
15
15
16
16
/// <summary>
17
- /// Represents a <see cref="Agent"/> specialization based on Open AI Assistant / GPT .
17
+ /// Represents a <see cref="Agent"/> specialization based on OpenAI Response API .
18
18
/// </summary>
19
19
[ ExcludeFromCodeCoverage ]
20
20
public sealed class OpenAIResponseAgent : Agent
@@ -45,11 +45,7 @@ public override async IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> In
45
45
{
46
46
Verify . NotNull ( messages ) ;
47
47
48
- var agentThread = await this . EnsureThreadExistsWithMessagesAsync (
49
- messages ,
50
- thread ,
51
- ( ) => new OpenAIResponseAgentThread ( this . Client , this . StoreEnabled ) ,
52
- cancellationToken ) . ConfigureAwait ( false ) ;
48
+ var agentThread = await this . EnsureThreadExistsWithMessagesAsync ( messages , thread , cancellationToken ) . ConfigureAwait ( false ) ;
53
49
54
50
// Invoke responses with the updated chat history.
55
51
var chatHistory = new ChatHistory ( ) ;
@@ -97,10 +93,20 @@ protected override Task<AgentChannel> RestoreChannelAsync(string channelState, C
97
93
}
98
94
99
95
#region private
96
+ private async Task < AgentThread > EnsureThreadExistsWithMessagesAsync ( ICollection < ChatMessageContent > messages , AgentThread ? thread , CancellationToken cancellationToken )
97
+ {
98
+ if ( this . StoreEnabled )
99
+ {
100
+ return await this . EnsureThreadExistsWithMessagesAsync ( messages , thread , ( ) => new OpenAIResponseAgentThread ( this . Client ) , cancellationToken ) . ConfigureAwait ( false ) ;
101
+ }
102
+
103
+ return await this . EnsureThreadExistsWithMessagesAsync ( messages , thread , ( ) => new ChatHistoryAgentThread ( ) , cancellationToken ) . ConfigureAwait ( false ) ;
104
+ }
105
+
100
106
private async IAsyncEnumerable < ChatMessageContent > InternalInvokeAsync (
101
107
string ? agentName ,
102
108
ChatHistory history ,
103
- OpenAIResponseAgentThread agentThread ,
109
+ AgentThread agentThread ,
104
110
AgentInvokeOptions ? options ,
105
111
[ EnumeratorCancellation ] CancellationToken cancellationToken )
106
112
{
@@ -110,28 +116,27 @@ private async IAsyncEnumerable<ChatMessageContent> InternalInvokeAsync(
110
116
if ( ! this . StoreEnabled )
111
117
{
112
118
// Use the thread chat history
113
- overrideHistory = [ .. agentThread . ChatHistory , .. history ] ;
119
+ overrideHistory = [ .. this . GetChatHistory ( agentThread ) , .. history ] ;
114
120
}
115
121
116
122
var inputItems = overrideHistory . Select ( c => c . ToResponseItem ( ) ) ;
117
123
var creationOptions = new ResponseCreationOptions ( )
118
124
{
119
125
EndUserId = this . GetDisplayName ( ) ,
120
126
Instructions = $ "{ this . Instructions } \n { options ? . AdditionalInstructions } ",
121
- StoredOutputEnabled = agentThread . StoreEnabled ,
127
+ StoredOutputEnabled = this . StoreEnabled ,
122
128
} ;
123
- if ( agentThread . StoreEnabled && agentThread . ResponseId != null )
129
+ if ( this . StoreEnabled && agentThread . Id != null )
124
130
{
125
- creationOptions . PreviousResponseId = agentThread . ResponseId ;
131
+ creationOptions . PreviousResponseId = agentThread . Id ;
126
132
}
127
133
128
134
var clientResult = await this . Client . CreateResponseAsync ( inputItems , creationOptions , cancellationToken ) . ConfigureAwait ( false ) ;
129
135
var response = clientResult . Value ;
130
136
131
137
if ( this . StoreEnabled )
132
138
{
133
- // Update the response id
134
- agentThread . ResponseId = response . Id ;
139
+ this . UpdateResponseId ( agentThread , response . Id ) ;
135
140
}
136
141
137
142
var messages = response . OutputItems . Select ( o => o . ToChatMessageContent ( ) ) ;
@@ -143,5 +148,26 @@ private async IAsyncEnumerable<ChatMessageContent> InternalInvokeAsync(
143
148
yield return message ;
144
149
}
145
150
}
151
+
152
+ private ChatHistory GetChatHistory ( AgentThread agentThread )
153
+ {
154
+ if ( agentThread is ChatHistoryAgentThread chatHistoryAgentThread )
155
+ {
156
+ return chatHistoryAgentThread . ChatHistory ;
157
+ }
158
+
159
+ throw new InvalidOperationException ( "The agent thread is not a ChatHistoryAgentThread." ) ;
160
+ }
161
+
162
+ private void UpdateResponseId ( AgentThread agentThread , string id )
163
+ {
164
+ if ( agentThread is OpenAIResponseAgentThread openAIResponseAgentThread )
165
+ {
166
+ openAIResponseAgentThread . ResponseId = id ;
167
+ return ;
168
+ }
169
+
170
+ throw new InvalidOperationException ( "The agent thread is not an OpenAIResponseAgentThread." ) ;
171
+ }
146
172
#endregion
147
173
}
0 commit comments