|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using Microsoft.Extensions.AI; |
| 5 | +using Microsoft.Extensions.AI.Agents; |
| 6 | + |
| 7 | +namespace Azure.AI.Agents.Persistent; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Provides extension methods for working with <see cref="Response{PersistentAgent}"/>. |
| 11 | +/// </summary> |
| 12 | +internal static class PersistentAgentResponseExtensions |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Converts a response containing persistent agent metadata into a runnable agent instance. |
| 16 | + /// </summary> |
| 17 | + /// <param name="persistentAgentResponse">The response containing the persistent agent to be converted. Cannot be <see langword="null"/>.</param> |
| 18 | + /// <param name="persistentAgentsClient">The client used to interact with persistent agents. Cannot be <see langword="null"/>.</param> |
| 19 | + /// <param name="chatOptions">The default <see cref="ChatOptions"/> to use when interacting with the agent.</param> |
| 20 | + /// <returns>A <see cref="ChatClientAgent"/> instance that can be used to perform operations on the persistent agent.</returns> |
| 21 | + public static ChatClientAgent AsRunnableAgent(this Response<PersistentAgent> persistentAgentResponse, PersistentAgentsClient persistentAgentsClient, ChatOptions? chatOptions = null) |
| 22 | + { |
| 23 | + if (persistentAgentResponse is null) |
| 24 | + { |
| 25 | + throw new ArgumentNullException(nameof(persistentAgentResponse)); |
| 26 | + } |
| 27 | + |
| 28 | + return AsRunnableAgent(persistentAgentResponse.Value, persistentAgentsClient, chatOptions); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Converts a <see cref="PersistentAgent"/> containing metadata about a persistent agent into a runnable agent instance. |
| 33 | + /// </summary> |
| 34 | + /// <param name="persistentAgentMetadata">The persistent agent metadata to be converted. Cannot be <see langword="null"/>.</param> |
| 35 | + /// <param name="persistentAgentsClient">The client used to interact with persistent agents. Cannot be <see langword="null"/>.</param> |
| 36 | + /// <param name="chatOptions">The default <see cref="ChatOptions"/> to use when interacting with the agent.</param> |
| 37 | + /// <returns>A <see cref="ChatClientAgent"/> instance that can be used to perform operations on the persistent agent.</returns> |
| 38 | + public static ChatClientAgent AsRunnableAgent(this PersistentAgent persistentAgentMetadata, PersistentAgentsClient persistentAgentsClient, ChatOptions? chatOptions = null) |
| 39 | + { |
| 40 | + if (persistentAgentMetadata is null) |
| 41 | + { |
| 42 | + throw new ArgumentNullException(nameof(persistentAgentMetadata)); |
| 43 | + } |
| 44 | + |
| 45 | + if (persistentAgentsClient is null) |
| 46 | + { |
| 47 | + throw new ArgumentNullException(nameof(persistentAgentsClient)); |
| 48 | + } |
| 49 | + |
| 50 | +#pragma warning disable CA2000 // Dispose objects before losing scope |
| 51 | + var chatClient = persistentAgentsClient.AsIChatClient(persistentAgentMetadata.Id); |
| 52 | +#pragma warning restore CA2000 // Dispose objects before losing scope |
| 53 | + |
| 54 | + return new ChatClientAgent(chatClient, options: new() |
| 55 | + { |
| 56 | + Id = persistentAgentMetadata.Id, |
| 57 | + Name = persistentAgentMetadata.Name, |
| 58 | + Description = persistentAgentMetadata.Description, |
| 59 | + Instructions = persistentAgentMetadata.Instructions, |
| 60 | + ChatOptions = chatOptions |
| 61 | + }); |
| 62 | + } |
| 63 | +} |
0 commit comments