-
Notifications
You must be signed in to change notification settings - Fork 39.7k
Expand file tree
/
Copy pathchatBackgroundTaskService.ts
More file actions
136 lines (118 loc) · 4.83 KB
/
chatBackgroundTaskService.ts
File metadata and controls
136 lines (118 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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);
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();
}
}