-
Notifications
You must be signed in to change notification settings - Fork 857
Added Shell tool abstractions #7336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dmytrostruk
wants to merge
5
commits into
dotnet:main
Choose a base branch
from
dmytrostruk:shell-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/ShellCallContent.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.Shared.DiagnosticIds; | ||
|
|
||
| namespace Microsoft.Extensions.AI; | ||
|
|
||
| /// <summary> | ||
| /// Represents a shell tool call invocation by a hosted service. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This content type is produced by <see cref="IChatClient"/> implementations that have native shell tool support. | ||
| /// It is informational only and represents the call itself, not the result. | ||
| /// </remarks> | ||
| [Experimental(DiagnosticIds.Experiments.AIShell, UrlFormat = DiagnosticIds.UrlFormat)] | ||
| public sealed class ShellCallContent : ToolCallContent | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ShellCallContent"/> class. | ||
| /// </summary> | ||
| /// <param name="callId">The tool call ID.</param> | ||
| public ShellCallContent(string callId) | ||
| : base(callId) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the list of commands to execute. | ||
| /// </summary> | ||
| public IList<string>? Commands { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the timeout for the shell command execution. | ||
| /// </summary> | ||
| public TimeSpan? Timeout { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the maximum output length in characters. | ||
| /// </summary> | ||
| public int? MaxOutputLength { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the status of the shell call. | ||
| /// </summary> | ||
| public string? Status { get; set; } | ||
| } | ||
34 changes: 34 additions & 0 deletions
34
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/ShellCommandOutput.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.Shared.DiagnosticIds; | ||
|
|
||
| namespace Microsoft.Extensions.AI; | ||
|
|
||
| /// <summary> | ||
| /// Represents the output of a single shell command execution. | ||
| /// </summary> | ||
| [Experimental(DiagnosticIds.Experiments.AIShell, UrlFormat = DiagnosticIds.UrlFormat)] | ||
| public class ShellCommandOutput : AIContent | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the standard output of the command. | ||
| /// </summary> | ||
| public string? Stdout { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the standard error output of the command. | ||
| /// </summary> | ||
| public string? Stderr { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the exit code of the command, or <see langword="null"/> if the command timed out. | ||
| /// </summary> | ||
| public int? ExitCode { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a value indicating whether the command execution timed out. | ||
| /// </summary> | ||
| public bool TimedOut { get; set; } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/ShellResultContent.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.Shared.DiagnosticIds; | ||
|
|
||
| namespace Microsoft.Extensions.AI; | ||
|
|
||
| /// <summary> | ||
| /// Represents the result of a shell tool invocation by a hosted service. | ||
| /// </summary> | ||
| [Experimental(DiagnosticIds.Experiments.AIShell, UrlFormat = DiagnosticIds.UrlFormat)] | ||
| public sealed class ShellResultContent : ToolResultContent | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ShellResultContent"/> class. | ||
| /// </summary> | ||
| /// <param name="callId">The tool call ID.</param> | ||
| public ShellResultContent(string callId) | ||
| : base(callId) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the output of the shell command execution. | ||
| /// </summary> | ||
| public IList<ShellCommandOutput>? Output { get; set; } | ||
dmytrostruk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// <summary> | ||
| /// Gets or sets the maximum output length in characters. | ||
| /// </summary> | ||
| public int? MaxOutputLength { get; set; } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/Libraries/Microsoft.Extensions.AI.Abstractions/Tools/HostedShellTool.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.Shared.DiagnosticIds; | ||
|
|
||
| namespace Microsoft.Extensions.AI; | ||
|
|
||
| /// <summary>Represents a hosted tool that can be specified to an AI service to enable it to execute shell commands.</summary> | ||
| /// <remarks> | ||
| /// This tool does not itself implement shell command execution. It is a marker that can be used to inform a service | ||
| /// that the service is allowed to execute shell commands if the service is capable of doing so. | ||
| /// </remarks> | ||
| [Experimental(DiagnosticIds.Experiments.AIShell, UrlFormat = DiagnosticIds.UrlFormat)] | ||
| public class HostedShellTool : AITool | ||
dmytrostruk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| /// <summary>Any additional properties associated with the tool.</summary> | ||
| private IReadOnlyDictionary<string, object?>? _additionalProperties; | ||
|
|
||
| /// <summary>Initializes a new instance of the <see cref="HostedShellTool"/> class.</summary> | ||
| public HostedShellTool() | ||
| { | ||
| } | ||
|
|
||
| /// <summary>Initializes a new instance of the <see cref="HostedShellTool"/> class.</summary> | ||
| /// <param name="additionalProperties">Any additional properties associated with the tool.</param> | ||
| public HostedShellTool(IReadOnlyDictionary<string, object?>? additionalProperties) | ||
| { | ||
| _additionalProperties = additionalProperties; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override string Name => "shell"; | ||
|
|
||
| /// <inheritdoc /> | ||
| public override IReadOnlyDictionary<string, object?> AdditionalProperties => _additionalProperties ?? base.AdditionalProperties; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/ShellCallContentTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text.Json; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Extensions.AI; | ||
|
|
||
| public class ShellCallContentTests | ||
| { | ||
| [Fact] | ||
| public void Constructor_PropsDefault() | ||
| { | ||
| ShellCallContent content = new("call123"); | ||
| Assert.Equal("call123", content.CallId); | ||
| Assert.Null(content.Commands); | ||
| Assert.Null(content.Timeout); | ||
| Assert.Null(content.MaxOutputLength); | ||
| Assert.Null(content.Status); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Properties_Roundtrip() | ||
| { | ||
| ShellCallContent content = new("call123"); | ||
|
|
||
| Assert.Null(content.Commands); | ||
| IList<string> commands = ["ls -la", "pwd"]; | ||
| content.Commands = commands; | ||
| Assert.Same(commands, content.Commands); | ||
|
|
||
| Assert.Null(content.Timeout); | ||
| content.Timeout = TimeSpan.FromMinutes(2); | ||
| Assert.Equal(TimeSpan.FromMinutes(2), content.Timeout); | ||
|
|
||
| Assert.Null(content.MaxOutputLength); | ||
| content.MaxOutputLength = 4096; | ||
| Assert.Equal(4096, content.MaxOutputLength); | ||
|
|
||
| Assert.Null(content.Status); | ||
| content.Status = "completed"; | ||
| Assert.Equal("completed", content.Status); | ||
|
|
||
| Assert.Null(content.RawRepresentation); | ||
| object raw = new(); | ||
| content.RawRepresentation = raw; | ||
| Assert.Same(raw, content.RawRepresentation); | ||
|
|
||
| Assert.Null(content.AdditionalProperties); | ||
| AdditionalPropertiesDictionary props = new() { { "key", "value" } }; | ||
| content.AdditionalProperties = props; | ||
| Assert.Same(props, content.AdditionalProperties); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Serialization_Roundtrips() | ||
| { | ||
| ShellCallContent content = new("call123") | ||
| { | ||
| Commands = ["ls -la", "pwd"], | ||
| Timeout = TimeSpan.FromSeconds(60), | ||
| MaxOutputLength = 4096, | ||
| Status = "completed", | ||
| }; | ||
|
|
||
| var json = JsonSerializer.Serialize(content, AIJsonUtilities.DefaultOptions); | ||
| var deserializedSut = JsonSerializer.Deserialize<ShellCallContent>(json, AIJsonUtilities.DefaultOptions); | ||
|
|
||
| Assert.NotNull(deserializedSut); | ||
| Assert.Equal("call123", deserializedSut.CallId); | ||
| Assert.NotNull(deserializedSut.Commands); | ||
| Assert.Equal(2, deserializedSut.Commands.Count); | ||
| Assert.Equal("ls -la", deserializedSut.Commands[0]); | ||
| Assert.Equal("pwd", deserializedSut.Commands[1]); | ||
| Assert.Equal(TimeSpan.FromSeconds(60), deserializedSut.Timeout); | ||
| Assert.Equal(4096, deserializedSut.MaxOutputLength); | ||
| Assert.Equal("completed", deserializedSut.Status); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Serialization_PolymorphicAsAIContent_Roundtrips() | ||
| { | ||
| AIContent content = new ShellCallContent("call123") | ||
| { | ||
| Commands = ["echo hello"], | ||
| }; | ||
|
|
||
| var json = JsonSerializer.Serialize(content, AIJsonUtilities.DefaultOptions); | ||
| Assert.Contains("\"$type\"", json); | ||
| Assert.Contains("\"shellCall\"", json); | ||
|
|
||
| var deserializedSut = JsonSerializer.Deserialize<AIContent>(json, AIJsonUtilities.DefaultOptions); | ||
| var shellCall = Assert.IsType<ShellCallContent>(deserializedSut); | ||
| Assert.Equal("call123", shellCall.CallId); | ||
| Assert.NotNull(shellCall.Commands); | ||
| Assert.Single(shellCall.Commands); | ||
| Assert.Equal("echo hello", shellCall.Commands[0]); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.