Path: blob/main/src/vs/workbench/contrib/chat/browser/agentSessions/agentSessionsQuickAccess.ts
5263 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { IKeyMods, IQuickPickDidAcceptEvent, IQuickPickSeparator } from '../../../../../platform/quickinput/common/quickInput.js';6import { PickerQuickAccessProvider, IPickerQuickAccessItem, TriggerAction } from '../../../../../platform/quickinput/browser/pickerQuickAccess.js';7import { localize } from '../../../../../nls.js';8import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';9import { IMatch, matchesFuzzy } from '../../../../../base/common/filters.js';10import { ThemeIcon } from '../../../../../base/common/themables.js';11import { IAgentSessionsService } from './agentSessionsService.js';12import { AgentSessionsSorter, groupAgentSessionsByDate } from './agentSessionsViewer.js';13import { IAgentSession } from './agentSessionsModel.js';14import { openSession } from './agentSessionsOpener.js';15import { ICommandService } from '../../../../../platform/commands/common/commands.js';16import { AGENT_SESSION_DELETE_ACTION_ID, AGENT_SESSION_RENAME_ACTION_ID } from './agentSessions.js';17import { archiveButton, deleteButton, getSessionButtons, getSessionDescription, renameButton, unarchiveButton } from './agentSessionsPicker.js';18import { AgentSessionsFilter } from './agentSessionsFilter.js';1920export const AGENT_SESSIONS_QUICK_ACCESS_PREFIX = 'agent ';2122export class AgentSessionsQuickAccessProvider extends PickerQuickAccessProvider<IPickerQuickAccessItem> {2324private readonly sorter = new AgentSessionsSorter();25private readonly filter: AgentSessionsFilter;2627constructor(28@IAgentSessionsService private readonly agentSessionsService: IAgentSessionsService,29@IInstantiationService private readonly instantiationService: IInstantiationService,30@ICommandService private readonly commandService: ICommandService,31) {32super(AGENT_SESSIONS_QUICK_ACCESS_PREFIX, {33canAcceptInBackground: true,34noResultsPick: {35label: localize('noAgentSessionResults', "No matching agent sessions")36}37});3839this.filter = this._register(this.instantiationService.createInstance(AgentSessionsFilter, {}));40}4142protected async _getPicks(filter: string): Promise<(IQuickPickSeparator | IPickerQuickAccessItem)[]> {43const picks: Array<IPickerQuickAccessItem | IQuickPickSeparator> = [];4445const sessions = this.agentSessionsService.model.sessions46.filter(session => !this.filter.exclude(session))47.sort(this.sorter.compare.bind(this.sorter));48const groupedSessions = groupAgentSessionsByDate(sessions);4950for (const group of groupedSessions.values()) {51if (group.sessions.length > 0) {52picks.push({ type: 'separator', label: group.label });5354for (const session of group.sessions) {55const highlights = matchesFuzzy(filter, session.label, true);56if (highlights) {57picks.push(this.toPickItem(session, highlights));58}59}60}61}6263return picks;64}6566private toPickItem(session: IAgentSession, highlights: IMatch[]): IPickerQuickAccessItem {67const description = getSessionDescription(session);68const buttons = getSessionButtons(session);6970return {71label: session.label,72description,73highlights: { label: highlights },74iconClass: ThemeIcon.asClassName(session.icon),75buttons,76trigger: async (buttonIndex) => {77const button = buttons[buttonIndex];78switch (button) {79case renameButton:80await this.commandService.executeCommand(AGENT_SESSION_RENAME_ACTION_ID, session);81return TriggerAction.REFRESH_PICKER;82case deleteButton:83await this.commandService.executeCommand(AGENT_SESSION_DELETE_ACTION_ID, session);84return TriggerAction.REFRESH_PICKER;85case archiveButton:86case unarchiveButton: {87const newArchivedState = !session.isArchived();88session.setArchived(newArchivedState);89return TriggerAction.REFRESH_PICKER;90}91default:92return TriggerAction.NO_ACTION;93}94},95accept: (keyMods: IKeyMods, event: IQuickPickDidAcceptEvent) => {96this.instantiationService.invokeFunction(openSession, session, {97sideBySide: event.inBackground,98editorOptions: {99preserveFocus: event.inBackground,100pinned: event.inBackground101}102});103}104};105}106}107108109