-
Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathchatAttachmentModel.ts
201 lines (168 loc) · 6.5 KB
/
chatAttachmentModel.ts
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { URI } from '../../../../base/common/uri.js';
import { Emitter } from '../../../../base/common/event.js';
import { basename } from '../../../../base/common/resources.js';
import { IRange } from '../../../../editor/common/core/range.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { IChatRequestVariableEntry } from '../common/chatModel.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { ChatPromptAttachmentsCollection } from './chatAttachmentModel/chatPromptAttachmentsCollection.js';
import { IFileService } from '../../../../platform/files/common/files.js';
import { IDialogService } from '../../../../platform/dialogs/common/dialogs.js';
import { ISharedWebContentExtractorService } from '../../../../platform/webContentExtractor/common/webContentExtractor.js';
import { Schemas } from '../../../../base/common/network.js';
import { resolveImageEditorAttachContext } from './chatAttachmentResolve.js';
import { CancellationToken } from '../../../../base/common/cancellation.js';
import { equals } from '../../../../base/common/objects.js';
export interface IChatAttachmentChangeEvent {
readonly deleted: readonly string[];
readonly added: readonly IChatRequestVariableEntry[];
readonly updated: readonly IChatRequestVariableEntry[];
}
export class ChatAttachmentModel extends Disposable {
/**
* Collection on prompt instruction attachments.
*/
public readonly promptInstructions: ChatPromptAttachmentsCollection;
constructor(
@IInstantiationService private readonly initService: IInstantiationService,
@IFileService private readonly fileService: IFileService,
@IDialogService private readonly dialogService: IDialogService,
@ISharedWebContentExtractorService private readonly webContentExtractorService: ISharedWebContentExtractorService,
) {
super();
this.promptInstructions = this._register(
this.initService.createInstance(ChatPromptAttachmentsCollection),
);
this._register(
this.promptInstructions.onAdd(() => {
this._onDidChange.fire({ added: [], deleted: [], updated: [] });
}),
);
this._register(
this.promptInstructions.onRemove(() => {
this._onDidChange.fire({ added: [], deleted: [], updated: [] });
}),
);
}
private _attachments = new Map<string, IChatRequestVariableEntry>();
get attachments(): ReadonlyArray<IChatRequestVariableEntry> {
return Array.from(this._attachments.values());
}
private _onDidChange = this._register(new Emitter<IChatAttachmentChangeEvent>());
readonly onDidChange = this._onDidChange.event;
get size(): number {
return this._attachments.size;
}
get fileAttachments(): URI[] {
return this.attachments.filter(file => file.kind === 'file' && URI.isUri(file.value))
.map(file => file.value as URI);
}
getAttachmentIDs() {
return new Set(this._attachments.keys());
}
clear(): void {
const deleted = Array.from(this._attachments.keys());
this._attachments.clear();
this._onDidChange.fire({ deleted, added: [], updated: [] });
}
delete(...variableEntryIds: string[]) {
const deleted: string[] = [];
for (const variableEntryId of variableEntryIds) {
if (this._attachments.delete(variableEntryId)) {
deleted.push(variableEntryId);
}
}
if (deleted.length > 0) {
this._onDidChange.fire({ deleted, added: [], updated: [] });
}
}
async addFile(uri: URI, range?: IRange) {
if (/\.(png|jpe?g|gif|bmp|webp)$/i.test(uri.path)) {
const context = await this.asImageVariableEntry(uri);
if (context) {
this.addContext(context);
}
return;
}
this.addContext(this.asVariableEntry(uri, range));
}
addFolder(uri: URI) {
this.addContext({
kind: 'directory',
value: uri,
id: uri.toString(),
name: basename(uri),
});
}
asVariableEntry(uri: URI, range?: IRange): IChatRequestVariableEntry {
return {
kind: 'file',
value: range ? { uri, range } : uri,
id: uri.toString() + (range?.toString() ?? ''),
name: basename(uri),
};
}
// Gets an image variable for a given URI, which may be a file or a web URL
async asImageVariableEntry(uri: URI): Promise<IChatRequestVariableEntry | undefined> {
if (uri.scheme === Schemas.file && await this.fileService.canHandleResource(uri)) {
return await resolveImageEditorAttachContext(this.fileService, this.dialogService, uri);
} else if (uri.scheme === Schemas.http || uri.scheme === Schemas.https) {
const extractedImages = await this.webContentExtractorService.readImage(uri, CancellationToken.None);
if (extractedImages) {
return await resolveImageEditorAttachContext(this.fileService, this.dialogService, uri, extractedImages);
}
}
return undefined;
}
addContext(...attachments: IChatRequestVariableEntry[]) {
const added: IChatRequestVariableEntry[] = [];
for (const attachment of attachments) {
if (!this._attachments.has(attachment.id)) {
this._attachments.set(attachment.id, attachment);
added.push(attachment);
}
}
if (added.length > 0) {
this._onDidChange.fire({ deleted: [], added, updated: [] });
}
}
clearAndSetContext(...attachments: IChatRequestVariableEntry[]) {
const deleted = Array.from(this._attachments.keys());
this._attachments.clear();
const added: IChatRequestVariableEntry[] = [];
for (const attachment of attachments) {
this._attachments.set(attachment.id, attachment);
added.push(attachment);
}
if (deleted.length > 0 || added.length > 0) {
this._onDidChange.fire({ deleted, added, updated: [] });
}
}
updateContent(toDelete: Iterable<string>, upsert: Iterable<IChatRequestVariableEntry>) {
const deleted: string[] = [];
const added: IChatRequestVariableEntry[] = [];
const updated: IChatRequestVariableEntry[] = [];
for (const id of toDelete) {
if (this._attachments.delete(id)) {
deleted.push(id);
}
}
for (const item of upsert) {
const oldItem = this._attachments.get(item.id);
if (!oldItem) {
this._attachments.set(item.id, item);
added.push(item);
} else if (!equals(oldItem, item)) {
this._attachments.set(item.id, item);
updated.push(item);
}
}
if (deleted.length > 0 || added.length > 0 || updated.length > 0) {
this._onDidChange.fire({ deleted, added, updated });
}
}
}