Skip to content

Commit 0497c08

Browse files
Factor ChatOptions.Instructions into PersistentAgentsChatClient (Azure#51180)
* Factor ChatOptions.Instructions into PersistentAgentsChatClient * Small fix --------- Co-authored-by: Dmytro Struk <[email protected]>
1 parent ad228bf commit 0497c08

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

eng/Packages.Data.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@
207207
</ItemGroup>
208208

209209
<ItemGroup Condition="$(MSBuildProjectName.Contains('Azure.AI.Agents.Persistent'))">
210-
<PackageReference Update="Microsoft.Extensions.AI.Abstractions" Version="9.6.0"/>
210+
<PackageReference Update="Microsoft.Extensions.AI.Abstractions" Version="9.7.0"/>
211211
</ItemGroup>
212212

213213
<ItemGroup Condition="$(MSBuildProjectName.Contains('Azure.Projects'))">

sdk/ai/Azure.AI.Agents.Persistent/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "net",
44
"TagPrefix": "net/ai/Azure.AI.Agents.Persistent",
5-
"Tag": "net/ai/Azure.AI.Agents.Persistent_ee70790a0c"
5+
"Tag": "net/ai/Azure.AI.Agents.Persistent_566ea3f825"
66
}

sdk/ai/Azure.AI.Agents.Persistent/src/Custom/PersistentAgentsChatClient.cs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ internal partial class PersistentAgentsChatClient : IChatClient
3535
/// <summary>The thread ID to use if none is supplied in <see cref="ChatOptions.ConversationId"/>.</summary>
3636
private readonly string? _defaultThreadId;
3737

38-
/// <summary>List of tools associated with the agent.</summary>
39-
private IReadOnlyList<ToolDefinition>? _agentTools;
38+
/// <summary>Lazily-retrieved agent instance. Used for its properties.</summary>
39+
private PersistentAgent? _agent;
4040

4141
/// <summary>Initializes a new instance of the <see cref="PersistentAgentsChatClient"/> class for the specified <see cref="PersistentAgentsClient"/>.</summary>
4242
public PersistentAgentsChatClient(PersistentAgentsClient client, string agentId, string? defaultThreadId = null)
@@ -233,6 +233,13 @@ public void Dispose() { }
233233
options?.RawRepresentationFactory?.Invoke(this) as ThreadAndRunOptions ??
234234
new();
235235

236+
// Load details about the agent if not already loaded.
237+
if (_agent is null)
238+
{
239+
PersistentAgent agent = await _client!.Administration.GetAgentAsync(_agentId, cancellationToken).ConfigureAwait(false);
240+
Interlocked.CompareExchange(ref _agent, agent, null);
241+
}
242+
236243
// Populate the run options from the ChatOptions, if provided.
237244
if (options is not null)
238245
{
@@ -253,13 +260,7 @@ public void Dispose() { }
253260
// along with our tools.
254261
if (runOptions.OverrideTools is null || !runOptions.OverrideTools.Any())
255262
{
256-
if (_agentTools is null)
257-
{
258-
PersistentAgent agent = await _client!.Administration.GetAgentAsync(_agentId, cancellationToken).ConfigureAwait(false);
259-
_agentTools = agent.Tools;
260-
}
261-
262-
toolDefinitions.AddRange(_agentTools);
263+
toolDefinitions.AddRange(_agent.Tools);
263264
}
264265

265266
// The caller can provide tools in the supplied ThreadAndRunOptions.
@@ -337,6 +338,18 @@ public void Dispose() { }
337338

338339
runOptions.ThreadOptions ??= new();
339340

341+
bool treatInstructionsAsOverride = false;
342+
if (runOptions.OverrideInstructions is not null)
343+
{
344+
treatInstructionsAsOverride = true;
345+
(instructions ??= new()).Append(runOptions.OverrideInstructions);
346+
}
347+
348+
if (options?.Instructions is not null)
349+
{
350+
(instructions ??= new()).Append(options.Instructions);
351+
}
352+
340353
foreach (ChatMessage chatMessage in messages)
341354
{
342355
List<MessageInputContentBlock> messageContents = [];
@@ -392,6 +405,14 @@ public void Dispose() { }
392405

393406
if (instructions is not null)
394407
{
408+
// If runOptions.OverrideInstructions was set by the caller, then all instructions are treated
409+
// as an override. Otherwise, we want all of the instructions to augment the agent's instructions,
410+
// so insert the agent's at the beginning.
411+
if (!treatInstructionsAsOverride && !string.IsNullOrEmpty(_agent.Instructions))
412+
{
413+
instructions.Insert(0, _agent.Instructions);
414+
}
415+
395416
runOptions.OverrideInstructions = instructions.ToString();
396417
}
397418

sdk/ai/Azure.AI.Agents.Persistent/tests/PersistentAgentsChatClientTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public void Cleanup()
339339
Pageable<PersistentAgent> agents = client.Administration.GetAgents();
340340
foreach (PersistentAgent agent in agents)
341341
{
342-
if (agent.Name.StartsWith(AGENT_NAME))
342+
if (agent.Name is not null && agent.Name.StartsWith(AGENT_NAME))
343343
client.Administration.DeleteAgent(agent.Id);
344344
}
345345

0 commit comments

Comments
 (0)