Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/common/chatWidgetHistoryService.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Emitter, Event } from '../../../../base/common/event.js';
7
import { URI } from '../../../../base/common/uri.js';
8
import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';
9
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
10
import { Memento } from '../../../common/memento.js';
11
import { ModifiedFileEntryState } from './chatEditingService.js';
12
import { CHAT_PROVIDER_ID } from './chatParticipantContribTypes.js';
13
import { IChatRequestVariableEntry } from './chatVariableEntries.js';
14
import { ChatAgentLocation, ChatModeKind } from './constants.js';
15
16
export interface IChatHistoryEntry {
17
text: string;
18
state?: IChatInputState;
19
}
20
21
/** The collected input state of ChatWidget contribs + attachments */
22
export interface IChatInputState {
23
[key: string]: any;
24
chatContextAttachments?: ReadonlyArray<IChatRequestVariableEntry>;
25
chatWorkingSet?: ReadonlyArray<{ uri: URI; state: ModifiedFileEntryState }>;
26
27
/**
28
* This should be a mode id (ChatMode | string).
29
* { id: string } is the old IChatMode. This is deprecated but may still be in persisted data.
30
*/
31
chatMode?: ChatModeKind | string | { id: string };
32
}
33
34
export const IChatWidgetHistoryService = createDecorator<IChatWidgetHistoryService>('IChatWidgetHistoryService');
35
export interface IChatWidgetHistoryService {
36
_serviceBrand: undefined;
37
38
readonly onDidClearHistory: Event<void>;
39
40
clearHistory(): void;
41
getHistory(location: ChatAgentLocation): IChatHistoryEntry[];
42
saveHistory(location: ChatAgentLocation, history: IChatHistoryEntry[]): void;
43
}
44
45
interface IChatHistory {
46
history: { [providerId: string]: IChatHistoryEntry[] };
47
}
48
49
export const ChatInputHistoryMaxEntries = 40;
50
51
export class ChatWidgetHistoryService implements IChatWidgetHistoryService {
52
_serviceBrand: undefined;
53
54
private memento: Memento;
55
private viewState: IChatHistory;
56
57
private readonly _onDidClearHistory = new Emitter<void>();
58
readonly onDidClearHistory: Event<void> = this._onDidClearHistory.event;
59
60
constructor(
61
@IStorageService storageService: IStorageService
62
) {
63
this.memento = new Memento('interactive-session', storageService);
64
const loadedState = this.memento.getMemento(StorageScope.WORKSPACE, StorageTarget.MACHINE) as IChatHistory;
65
for (const provider in loadedState.history) {
66
// Migration from old format
67
loadedState.history[provider] = loadedState.history[provider].map(entry => typeof entry === 'string' ? { text: entry } : entry);
68
}
69
70
this.viewState = loadedState;
71
}
72
73
getHistory(location: ChatAgentLocation): IChatHistoryEntry[] {
74
const key = this.getKey(location);
75
return this.viewState.history?.[key] ?? [];
76
}
77
78
private getKey(location: ChatAgentLocation): string {
79
// Preserve history for panel by continuing to use the same old provider id. Use the location as a key for other chat locations.
80
return location === ChatAgentLocation.Panel ? CHAT_PROVIDER_ID : location;
81
}
82
83
saveHistory(location: ChatAgentLocation, history: IChatHistoryEntry[]): void {
84
if (!this.viewState.history) {
85
this.viewState.history = {};
86
}
87
88
const key = this.getKey(location);
89
this.viewState.history[key] = history.slice(-ChatInputHistoryMaxEntries);
90
this.memento.saveMemento();
91
}
92
93
clearHistory(): void {
94
this.viewState.history = {};
95
this.memento.saveMemento();
96
this._onDidClearHistory.fire();
97
}
98
}
99
100