Skip to content

improve events of the chat prompt attachments collection #246785

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class PromptInstructionsAttachmentsCollectionWidget extends Disposable {
this.render = this.render.bind(this);

// when a new attachment model is added, create a new child widget for it
this.model.onAdd((attachment) => {
this._register(this.model.onAdd((attachment) => {
const widget = this.initService.createInstance(
InstructionsAttachmentWidget,
attachment,
Expand All @@ -114,7 +114,7 @@ export class PromptInstructionsAttachmentsCollectionWidget extends Disposable {

// fire the event to notify about the change in the number of attachments
this._onAttachmentsCountChange.fire();
});
}));
}

/**
Expand Down
16 changes: 13 additions & 3 deletions src/vs/workbench/contrib/chat/browser/chatAttachmentModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,19 @@ export class ChatAttachmentModel extends Disposable {

this.promptInstructions = this._register(
this.initService.createInstance(ChatPromptAttachmentsCollection),
).onUpdate(() => {
this._onDidChange.fire({ added: [], deleted: [], updated: [] });
});
);

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>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,37 @@ export const toChatVariable = (
* See {@linkcode ChatPromptAttachmentModel} for individual attachment.
*/
export class ChatPromptAttachmentsCollection extends Disposable {
/**
* Event that fires then this model is updated.
*
* See {@linkcode onUpdate}.
*/
protected _onUpdate = this._register(new Emitter<void>());
/**
* Subscribe to the `onUpdate` event.
*/
public onUpdate = this._onUpdate.event;

/**
* Event that fires when a new prompt instruction attachment is added.
* See {@linkcode onAdd}.
*/
protected _onAdd = this._register(new Emitter<ChatPromptAttachmentModel>());
/**
* The `onAdd` event fires when a new prompt instruction attachment is added.
*/
public onAdd = this._onAdd.event;

/**
* Event that fires when a new prompt instruction attachment is removed.
* See {@linkcode onRemove}.
*/
protected _onRemove = this._register(new Emitter<ChatPromptAttachmentModel>());
/**
* The `onRemove` event fires when a new prompt instruction attachment is removed.
*/
public onRemove = this._onRemove.event;

/**
* List of all prompt instruction attachments.
*/
Expand Down Expand Up @@ -154,38 +185,6 @@ export class ChatPromptAttachmentsCollection extends Disposable {
);
}

/**
* Event that fires then this model is updated.
*
* See {@linkcode onUpdate}.
*/
protected _onUpdate = this._register(new Emitter<void>());
/**
* Subscribe to the `onUpdate` event.
* @param callback Function to invoke on update.
*/
public onUpdate(callback: () => unknown): this {
this._register(this._onUpdate.event(callback));

return this;
}

/**
* Event that fires when a new prompt instruction attachment is added.
* See {@linkcode onAdd}.
*/
protected _onAdd = this._register(new Emitter<ChatPromptAttachmentModel>());
/**
* The `onAdd` event fires when a new prompt instruction attachment is added.
*
* @param callback Function to invoke on add.
*/
public onAdd(callback: (attachment: ChatPromptAttachmentModel) => unknown): this {
this._register(this._onAdd.event(callback));

return this;
}

constructor(
@IInstantiationService private readonly initService: IInstantiationService,
@IConfigurationService private readonly configService: IConfigurationService,
Expand Down Expand Up @@ -214,6 +213,7 @@ export class ChatPromptAttachmentsCollection extends Disposable {
// alternative results in an infinite loop of calling this callback
this.attachments.deleteAndLeak(uri.path);
this._onUpdate.fire();
this._onRemove.fire(instruction);
});

this.attachments.set(uri.path, instruction);
Expand Down
Loading