Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/browser/chatEditing/chatEditingTextModelContentProviders.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 { Schemas } from '../../../../../base/common/network.js';
7
import { URI } from '../../../../../base/common/uri.js';
8
import { ITextModel } from '../../../../../editor/common/model.js';
9
import { IModelService } from '../../../../../editor/common/services/model.js';
10
import { ITextModelContentProvider } from '../../../../../editor/common/services/resolverService.js';
11
import { IChatEditingService } from '../../common/chatEditingService.js';
12
13
type ChatEditingTextModelContentQueryData = { kind: 'doc'; documentId: string; chatSessionId: string };
14
15
export class ChatEditingTextModelContentProvider implements ITextModelContentProvider {
16
public static readonly scheme = Schemas.chatEditingModel;
17
18
public static getFileURI(chatSessionId: string, documentId: string, path: string): URI {
19
return URI.from({
20
scheme: ChatEditingTextModelContentProvider.scheme,
21
path,
22
query: JSON.stringify({ kind: 'doc', documentId, chatSessionId } satisfies ChatEditingTextModelContentQueryData),
23
});
24
}
25
26
constructor(
27
private readonly _chatEditingService: IChatEditingService,
28
@IModelService private readonly _modelService: IModelService,
29
) { }
30
31
async provideTextContent(resource: URI): Promise<ITextModel | null> {
32
const existing = this._modelService.getModel(resource);
33
if (existing && !existing.isDisposed()) {
34
return existing;
35
}
36
37
const data: ChatEditingTextModelContentQueryData = JSON.parse(resource.query);
38
39
const session = this._chatEditingService.getEditingSession(data.chatSessionId);
40
41
const entry = session?.entries.get().find(candidate => candidate.entryId === data.documentId);
42
if (!entry) {
43
return null;
44
}
45
46
return this._modelService.getModel(entry.originalURI);
47
}
48
}
49
50
type ChatEditingSnapshotTextModelContentQueryData = { sessionId: string; requestId: string | undefined; undoStop: string | undefined };
51
52
export class ChatEditingSnapshotTextModelContentProvider implements ITextModelContentProvider {
53
public static getSnapshotFileURI(chatSessionId: string, requestId: string | undefined, undoStop: string | undefined, path: string): URI {
54
return URI.from({
55
scheme: Schemas.chatEditingSnapshotScheme,
56
path,
57
query: JSON.stringify({ sessionId: chatSessionId, requestId: requestId ?? '', undoStop: undoStop ?? '' } satisfies ChatEditingSnapshotTextModelContentQueryData),
58
});
59
}
60
61
constructor(
62
private readonly _chatEditingService: IChatEditingService,
63
@IModelService private readonly _modelService: IModelService,
64
) { }
65
66
async provideTextContent(resource: URI): Promise<ITextModel | null> {
67
const existing = this._modelService.getModel(resource);
68
if (existing && !existing.isDisposed()) {
69
return existing;
70
}
71
72
const data: ChatEditingSnapshotTextModelContentQueryData = JSON.parse(resource.query);
73
74
const session = this._chatEditingService.getEditingSession(data.sessionId);
75
if (!session || !data.requestId) {
76
return null;
77
}
78
79
return session.getSnapshotModel(data.requestId, data.undoStop || undefined, resource);
80
}
81
}
82
83