From 36b3192a0b5a1d889192cda69d199132c8e3ce3f Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Fri, 3 Jul 2026 14:23:32 +0200 Subject: [PATCH] Add background chat generation job support to assistants --- .../Assistants/AssistantBase.razor.cs | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/app/MindWork AI Studio/Assistants/AssistantBase.razor.cs b/app/MindWork AI Studio/Assistants/AssistantBase.razor.cs index dd48db75..11e83a02 100644 --- a/app/MindWork AI Studio/Assistants/AssistantBase.razor.cs +++ b/app/MindWork AI Studio/Assistants/AssistantBase.razor.cs @@ -2,6 +2,7 @@ using AIStudio.Chat; using AIStudio.Provider; using AIStudio.Settings; using AIStudio.Dialogs.Settings; +using AIStudio.Tools.AIJobs; using AIStudio.Tools.AssistantSessions; using AIStudio.Tools.Services; @@ -40,6 +41,12 @@ public abstract partial class AssistantBase : AssistantLowerBase wher [Inject] protected AssistantSessionService AssistantSessionService { get; init; } = null!; + + /// + /// Gets the job service used to run assistant-created chats independently from the assistant UI. + /// + [Inject] + protected AIJobService AIJobService { get; init; } = null!; protected abstract string Title { get; } @@ -453,6 +460,50 @@ public abstract partial class AssistantBase : AssistantLowerBase wher } } } + + /// + /// Starts the current assistant chat thread as a regular background-capable chat generation job. + /// + /// + /// Use this when an assistant creates a chat and hands it over to the chat page instead of + /// rendering the answer inside the assistant UI. + /// + /// The timestamp to use for the AI response block. + /// Whether the AI response block should be hidden from the user. + /// Whether the chat job should start as the current foreground job. + /// A task that completes after the chat job was registered. + protected async Task StartChatGenerationJobAsync(DateTimeOffset time, bool hideContentFromUser = false, bool isForeground = true) + { + if (this.ChatThread is null) + return; + + var aiText = new ContentText + { + InitialRemoteWait = true, + }; + + this.ResultingContentBlock = new ContentBlock + { + Time = time, + ContentType = ContentType.TEXT, + Role = ChatRole.AI, + Content = aiText, + HideFromUser = hideContentFromUser, + }; + + this.ChatThread.Blocks.Add(this.ResultingContentBlock); + this.ChatThread.SelectedProvider = this.ProviderSettings.Id; + + await this.CheckpointAssistantSession(); + await this.AIJobService.TryStartChatGenerationAsync(new ChatGenerationRequest + { + ChatThread = this.ChatThread, + AIText = aiText, + LastUserPrompt = this.LastUserPrompt, + ProviderSettings = this.ProviderSettings, + IsForeground = isForeground, + }); + } private async Task CancelStreaming() {