-
Notifications
You must be signed in to change notification settings - Fork 39.2k
Improve UX of chat background tasks, implement for terminal command finished task #310331
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { Disposable, DisposableMap } from '../../../../base/common/lifecycle.js'; | ||
| import { Emitter } from '../../../../base/common/event.js'; | ||
| import { IObservable, ISettableObservable, observableFromEventOpts, observableValue } from '../../../../base/common/observable.js'; | ||
| import { generateUuid } from '../../../../base/common/uuid.js'; | ||
| import { URI } from '../../../../base/common/uri.js'; | ||
| import { ILogService } from '../../../../platform/log/common/log.js'; | ||
| import { BackgroundTaskSource, BackgroundTaskStatus, IChatBackgroundTask, IChatBackgroundTaskHandle, IChatBackgroundTaskService } from '../common/chatBackgroundTask.js'; | ||
| import { IChatService } from '../common/chatService/chatService.js'; | ||
|
|
||
| class ChatBackgroundTask extends Disposable implements IChatBackgroundTaskHandle { | ||
| private readonly _status: ISettableObservable<BackgroundTaskStatus>; | ||
| private readonly _statusMessage: ISettableObservable<string | undefined>; | ||
| private readonly _result: ISettableObservable<unknown | undefined>; | ||
|
|
||
| get status(): IObservable<BackgroundTaskStatus> { return this._status; } | ||
| get statusMessage(): IObservable<string | undefined> { return this._statusMessage; } | ||
| get result(): IObservable<unknown | undefined> { return this._result; } | ||
|
|
||
| constructor( | ||
| readonly taskId: string, | ||
| readonly name: string, | ||
| readonly source: BackgroundTaskSource, | ||
| private readonly _cancel: () => void, | ||
| ) { | ||
| super(); | ||
| this._status = observableValue('bgTaskStatus', BackgroundTaskStatus.Working); | ||
| this._statusMessage = observableValue('bgTaskMessage', undefined); | ||
| this._result = observableValue('bgTaskResult', undefined); | ||
| } | ||
|
|
||
| cancel(): void { | ||
| if (this._status.get() !== BackgroundTaskStatus.Working) { | ||
| return; | ||
| } | ||
| this._status.set(BackgroundTaskStatus.Cancelled, undefined); | ||
| this._cancel(); | ||
| } | ||
|
|
||
| complete(result: unknown): void { | ||
| if (this._status.get() !== BackgroundTaskStatus.Working) { | ||
| return; | ||
| } | ||
| this._result.set(result, undefined); | ||
| this._status.set(BackgroundTaskStatus.Completed, undefined); | ||
| } | ||
|
|
||
| fail(message?: string): void { | ||
| if (this._status.get() !== BackgroundTaskStatus.Working) { | ||
| return; | ||
| } | ||
| this._statusMessage.set(message, undefined); | ||
| this._status.set(BackgroundTaskStatus.Failed, undefined); | ||
| } | ||
|
|
||
| updateStatusMessage(message: string): void { | ||
| this._statusMessage.set(message, undefined); | ||
| } | ||
| } | ||
|
|
||
| export class ChatBackgroundTaskServiceImpl extends Disposable implements IChatBackgroundTaskService { | ||
| declare readonly _serviceBrand: undefined; | ||
|
|
||
| private readonly _tasksBySession = this._register(new DisposableMap<string, DisposableMap<string, ChatBackgroundTask>>()); | ||
| private readonly _onDidChangeTasks = this._register(new Emitter<void>()); | ||
|
|
||
| constructor( | ||
| @ILogService private readonly _logService: ILogService, | ||
| @IChatService private readonly _chatService: IChatService, | ||
| ) { | ||
| super(); | ||
|
|
||
| this._register(this._chatService.onDidDisposeSession(e => { | ||
| for (const sessionResource of e.sessionResources) { | ||
| this._tasksBySession.deleteAndDispose(sessionResource.toString()); | ||
| } | ||
| this._onDidChangeTasks.fire(); | ||
| })); | ||
| } | ||
|
|
||
| createTask(sessionResource: URI, options: { | ||
| name: string; | ||
| source: BackgroundTaskSource; | ||
| onCancel?: () => void; | ||
| }): IChatBackgroundTaskHandle { | ||
| const taskId = generateUuid(); | ||
| const task = new ChatBackgroundTask(taskId, options.name, options.source, () => { | ||
| options.onCancel?.(); | ||
| }); | ||
|
|
||
| this._trackTask(sessionResource, task); | ||
| this._logService.debug(`[ChatBackgroundTaskService] Created task ${taskId} for session ${sessionResource.toString()}`); | ||
| return task; | ||
| } | ||
|
|
||
| getTasksForSession(sessionResource: URI): IObservable<readonly IChatBackgroundTask[]> { | ||
| const key = sessionResource.toString(); | ||
| return observableFromEventOpts({ equalsFn: () => false }, this._onDidChangeTasks.event, () => { | ||
| const map = this._tasksBySession.get(key); | ||
| return map ? [...map.values()] : []; | ||
| }); | ||
| } | ||
|
|
||
| getTask(taskId: string): IChatBackgroundTask | undefined { | ||
| for (const map of this._tasksBySession.values()) { | ||
| const task = map.get(taskId); | ||
|
Comment on lines
+103
to
+110
|
||
| if (task) { | ||
| return task; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| evictTask(taskId: string): void { | ||
| for (const [, map] of this._tasksBySession) { | ||
| if (map.has(taskId)) { | ||
| map.deleteAndDispose(taskId); | ||
| this._onDidChangeTasks.fire(); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private _trackTask(sessionResource: URI, task: ChatBackgroundTask): void { | ||
| const key = sessionResource.toString(); | ||
| if (!this._tasksBySession.has(key)) { | ||
| this._tasksBySession.set(key, new DisposableMap()); | ||
| } | ||
| this._tasksBySession.get(key)!.set(task.taskId, task); | ||
| this._onDidChangeTasks.fire(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,6 +83,7 @@ import { IChatTipService } from '../chatTipService.js'; | |
| import { ChatTipContentPart } from './chatContentParts/chatTipContentPart.js'; | ||
| import { ChatContentMarkdownRenderer } from './chatContentMarkdownRenderer.js'; | ||
| import { IAgentSessionsService } from '../agentSessions/agentSessionsService.js'; | ||
| import { BackgroundTaskStatus, IChatBackgroundTaskService } from '../../common/chatBackgroundTask.js'; | ||
| import { IChatDebugService } from '../../common/chatDebugService.js'; | ||
|
|
||
| const $ = dom.$; | ||
|
|
@@ -413,6 +414,7 @@ export class ChatWidget extends Disposable implements IChatWidget { | |
| @IChatAttachmentResolveService private readonly chatAttachmentResolveService: IChatAttachmentResolveService, | ||
| @IChatTipService private readonly chatTipService: IChatTipService, | ||
| @IChatDebugService private readonly chatDebugService: IChatDebugService, | ||
| @IChatBackgroundTaskService private readonly chatBackgroundTaskService: IChatBackgroundTaskService, | ||
| ) { | ||
| super(); | ||
|
|
||
|
|
@@ -2004,6 +2006,10 @@ export class ChatWidget extends Disposable implements IChatWidget { | |
|
|
||
| this.listWidget.setViewModel(this.viewModel); | ||
|
|
||
| // Re-render attached context now that the session resource is available | ||
| // so the background task autorun is set up. | ||
| this.input.renderAttachedContext(); | ||
|
|
||
| if (this._lockedAgent) { | ||
| let placeholder = this.chatSessionsService.getChatSessionContribution(this._lockedAgent.id)?.inputPlaceholder; | ||
| if (!placeholder) { | ||
|
|
@@ -2465,6 +2471,16 @@ export class ChatWidget extends Disposable implements IChatWidget { | |
| this.updateChatViewVisibility(); | ||
| this.input.acceptInput(options?.storeToHistory ?? isUserQuery); | ||
|
|
||
| // Evict completed background tasks so their context is included in this request | ||
| if (submittedSessionResource) { | ||
| const tasks = this.chatBackgroundTaskService.getTasksForSession(submittedSessionResource).get(); | ||
| for (const task of tasks) { | ||
| if (task.status.get() === BackgroundTaskStatus.Completed || task.status.get() === BackgroundTaskStatus.Failed || task.status.get() === BackgroundTaskStatus.Cancelled) { | ||
| this.chatBackgroundTaskService.evictTask(task.taskId); | ||
|
Comment on lines
+2474
to
+2479
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| const sent = ChatSendResult.isQueued(result) ? await result.deferred : result; | ||
| if (!ChatSendResult.isSent(sent)) { | ||
| return; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ChatBackgroundTask.cancel()sets status to Cancelled, butfail()/complete()can still overwrite it later. This causes cancel to end up as Failed when the terminal disposal handler runs (and can also flip Completed→Failed). Makecomplete()/fail()no-op unless the task is still in Working state (similar tocancel()), or otherwise enforce a one-way state transition.