using AIStudio.Chat; using AIStudio.Provider; using AIStudio.Tools.AIJobs; namespace AIStudio.Tools.ToolCallingSystem.Harness; /// /// Everything one run of the tool calling loop needs besides its provider adapter. /// public sealed class ToolCallingLoopContext { /// /// The chat the loop runs for. Tool results may raise its required provider confidence. /// public required ChatThread ChatThread { get; init; } /// /// The tools the model may call in this run. /// public required IReadOnlyList<(ToolDefinition Definition, IToolImplementation Implementation)> RunnableTools { get; init; } public required ToolExecutor ToolExecutor { get; init; } /// /// The provider running the conversation, needed to judge what a tool may return to it. /// public required IProvider Provider { get; init; } /// /// The assistant message being built, or null when there is none to update. /// /// /// The loop writes the tool traces and the live status into this instance, which is already /// part of the chat thread. That is how the UI learns about a running tool without the loop /// having to yield anything. /// public ContentText? CurrentAssistantContent { get; init; } public required string ProviderInstanceName { get; init; } public required LLMProviders ProviderType { get; init; } public required string ModelId { get; init; } /// /// Records one tool invocation for the UI. /// /// /// Tells the UI right away, so a finished call shows up while the next one is still running. /// Waiting for the round to end would leave the user watching a list that lags behind what the /// model is doing. /// public async Task AddToolInvocationAsync(ToolInvocationTrace trace) { if (this.CurrentAssistantContent is null) return; this.CurrentAssistantContent.ToolInvocations.Add(trace); await this.AnnounceAsync(this.CurrentAssistantContent); } /// /// Hands the conversation the adapter has accumulated to the assistant message. /// /// /// Called after every recording, not once per round: a round which reads five web pages is the /// one during which the request grows the most, and a number which only moves between rounds /// would stand still through exactly that. /// /// The adapter of this run, which knows what it has recorded. public async Task PublishPendingToolConversationAsync(IToolCallingProviderAdapter adapter) { if (this.CurrentAssistantContent is null) return; this.CurrentAssistantContent.PendingToolConversation = [..adapter.RecordedRequestTexts]; await this.AnnounceAsync(this.CurrentAssistantContent); } /// /// Tells the UI that the named tools are running. /// public async Task ShowToolRuntimeStatusAsync(IEnumerable toolNames) { if (this.CurrentAssistantContent is null) return; this.CurrentAssistantContent.ToolRuntimeStatus = new ToolRuntimeStatus { IsRunning = true, ToolNames = [.. toolNames], }; await this.AnnounceAsync(this.CurrentAssistantContent); } /// /// Clears the running-tool status. /// /// /// Must happen on every path leaving a round, including the failing ones: a status left /// behind tells the user a tool is still running when nothing is. /// public async Task ResetToolRuntimeStatusAsync() { if (this.CurrentAssistantContent is null) return; this.CurrentAssistantContent.ToolRuntimeStatus = new(); await this.AnnounceAsync(this.CurrentAssistantContent); } /// /// Says that something about the running answer has changed. /// /// /// Two receivers, because the screen is built from two of them. The content's own event /// renders the message block, which is what shows a running tool and the calls it has made. /// The job service renders the chat around it, and that is what recounts the tokens -- which /// nothing else would ask for during a tool run: the chat hears about progress one streamed /// chunk at a time, and a tool run produces none until it is over.

/// One method rather than two calls at each of the four places above, because the second of /// them is the one which is easy to forget. ///
/// The assistant message which changed. private async Task AnnounceAsync(ContentText content) { await content.StreamingEvent(); // // Asked for here rather than taken as a dependency: the same loop runs for the assistants, // where there is no job to tell and nothing which counts tokens. // var jobService = Program.SERVICE_PROVIDER.GetService(); if (jobService is not null) await jobService.NotifyChatActivityAsync(this.ChatThread.ChatId); } }