// Copyright (c) Microsoft. All rights reserved. using System.Collections.ObjectModel; using Harness.Shared.Console.Commands; using Harness.Shared.Console.Observers; using Harness.Shared.Console.ToolFormatters; using Microsoft.Agents.AI; namespace Harness.Shared.Console; /// /// Configuration options for . /// public class HarnessConsoleOptions { /// /// Gets or sets the list of console observers that participate in the agent response /// streaming lifecycle. Use the factory methods on this class to create common observer sets. /// When (the default), a default set of observers is used. /// Set to an empty list to disable all observers. /// public IReadOnlyList? Observers { get; set; } /// /// Gets or sets the list of command handlers to check before sending user input to the agent. /// Use to create the default set. /// When (the default), a default set of handlers is used. /// Set to an empty list to disable all command handlers. /// public IReadOnlyList? CommandHandlers { get; set; } /// /// The default mode-to-color mapping used when no custom are provided. /// public static readonly IReadOnlyDictionary DefaultModeColors = new ReadOnlyDictionary( new Dictionary(StringComparer.OrdinalIgnoreCase) { ["plan"] = ConsoleColor.Cyan, ["execute"] = ConsoleColor.Green, }); /// /// Gets or sets a mapping of agent mode names to console colors. /// When a mode is not found in this dictionary, the default color () is used. /// public Dictionary ModeColors { get; set; } = new(DefaultModeColors, StringComparer.OrdinalIgnoreCase); /// /// Gets or sets an optional factory for creating the . /// When (the default), is used. /// public Func>? SessionFactory { get; set; } /// /// Creates the default set of observers without planning support. /// Includes tool call display, tool approval, error display, reasoning display, /// usage display, and text output. /// /// Optional maximum context window size in tokens for usage display. /// Optional maximum output tokens for usage display. /// Optional tool call formatters. When , /// each observer uses the default formatters from . /// A list of observers for a standard (non-planning) console session. public static List BuildDefaultObservers( int? maxContextWindowTokens = null, int? maxOutputTokens = null, IReadOnlyList? toolFormatters = null) { return [ new ToolCallDisplayObserver(toolFormatters), new ToolApprovalObserver(toolFormatters), new ErrorDisplayObserver(), new ReasoningDisplayObserver(), new UsageDisplayObserver(maxContextWindowTokens, maxOutputTokens), new TextOutputObserver(), ]; } /// /// Creates the default set of observers with planning support. /// Includes a instead of . /// /// The agent, used to resolve . /// The mode name that represents the planning mode. /// The mode name to switch to when the user approves a plan. /// Optional mode-to-color mapping for display. /// Defaults to when . /// Optional maximum context window size in tokens for usage display. /// Optional maximum output tokens for usage display. /// Optional tool call formatters. When , /// each observer uses the default formatters from . /// A list of observers for a planning-enabled console session. public static List BuildObserversWithPlanning( AIAgent agent, string planModeName, string executionModeName, IReadOnlyDictionary? modeColors = null, int? maxContextWindowTokens = null, int? maxOutputTokens = null, IReadOnlyList? toolFormatters = null) { var modeProvider = agent.GetService() ?? throw new InvalidOperationException("Planning requires an AgentModeProvider service on the agent."); return [ new ToolCallDisplayObserver(toolFormatters), new ToolApprovalObserver(toolFormatters), new ErrorDisplayObserver(), new ReasoningDisplayObserver(), new UsageDisplayObserver(maxContextWindowTokens, maxOutputTokens), new PlanningOutputObserver(modeProvider, planModeName, executionModeName, modeColors ?? DefaultModeColors), ]; } /// /// Creates the default set of command handlers. /// Includes exit, todo, and mode command handlers. /// /// The agent, used to resolve and . /// Optional mode-to-color mapping for the mode command display. /// Defaults to when . /// A list of command handlers for a standard console session. public static List BuildDefaultCommandHandlers( AIAgent agent, IReadOnlyDictionary? modeColors = null) { var todoProvider = agent.GetService(); var modeProvider = agent.GetService(); return [ new ExitCommandHandler(), new TodoCommandHandler(todoProvider), new ModeCommandHandler(modeProvider, modeColors ?? DefaultModeColors), new SessionCommandHandler(agent), ]; } }