// Copyright (c) Microsoft. All rights reserved. using System; using System.Text; namespace Microsoft.Agents.AI.Tools.Shell; /// /// The outcome of a single shell command invocation. /// /// Captured standard output, possibly truncated. /// Captured standard error, possibly truncated. /// The exit status reported by the shell or subprocess. -1 if the process never exited cleanly. /// How long the command took to execute end-to-end. /// when stdout or stderr was truncated. /// when the command was killed because it exceeded the configured timeout. public sealed record ShellResult( string Stdout, string Stderr, int ExitCode, TimeSpan Duration, bool Truncated = false, bool TimedOut = false) { /// /// Format the result as a single text block suitable for return to a language model. /// /// A multi-line string combining stdout, stderr, status flags, and the exit code. public string FormatForModel() { var sb = new StringBuilder(); if (!string.IsNullOrEmpty(this.Stdout)) { _ = sb.Append(this.Stdout); if (this.Truncated) { _ = sb.AppendLine().Append("[stdout truncated]"); } _ = sb.AppendLine(); } if (!string.IsNullOrEmpty(this.Stderr)) { _ = sb.Append("stderr: ").Append(this.Stderr).AppendLine(); } if (this.TimedOut) { _ = sb.AppendLine("[command timed out]"); } _ = sb.Append("exit_code: ").Append(this.ExitCode); return sb.ToString(); } }