Skip to content

Commit

Permalink
Warm up slash command cache (#206290)
Browse files Browse the repository at this point in the history
  • Loading branch information
roblourens authored Feb 26, 2024
1 parent 927ae34 commit 9bd6ee4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/vs/workbench/contrib/chat/common/chatAgents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ export const IChatAgentService = createDecorator<IChatAgentService>('chatAgentSe

export interface IChatAgentService {
_serviceBrand: undefined;
readonly onDidChangeAgents: Event<void>;
/**
* undefined when an agent was removed
*/
readonly onDidChangeAgents: Event<IChatAgent | undefined>;
registerAgent(agent: IChatAgent): IDisposable;
invokeAgent(id: string, request: IChatAgentRequest, progress: (part: IChatProgress) => void, history: IChatAgentHistoryEntry[], token: CancellationToken): Promise<IChatAgentResult>;
getFollowups(id: string, request: IChatAgentRequest, result: IChatAgentResult, token: CancellationToken): Promise<IChatFollowup[]>;
Expand All @@ -127,8 +130,8 @@ export class ChatAgentService extends Disposable implements IChatAgentService {

private readonly _agents = new Map<string, { agent: IChatAgent }>();

private readonly _onDidChangeAgents = this._register(new Emitter<void>());
readonly onDidChangeAgents: Event<void> = this._onDidChangeAgents.event;
private readonly _onDidChangeAgents = this._register(new Emitter<IChatAgent | undefined>());
readonly onDidChangeAgents: Event<IChatAgent | undefined> = this._onDidChangeAgents.event;

override dispose(): void {
super.dispose();
Expand All @@ -140,11 +143,11 @@ export class ChatAgentService extends Disposable implements IChatAgentService {
throw new Error(`Already registered an agent with id ${agent.id}`);
}
this._agents.set(agent.id, { agent });
this._onDidChangeAgents.fire();
this._onDidChangeAgents.fire(agent);

return toDisposable(() => {
if (this._agents.delete(agent.id)) {
this._onDidChangeAgents.fire();
this._onDidChangeAgents.fire(undefined);
}
});
}
Expand All @@ -155,7 +158,7 @@ export class ChatAgentService extends Disposable implements IChatAgentService {
throw new Error(`No agent with id ${id} registered`);
}
data.agent.metadata = { ...data.agent.metadata, ...updateMetadata };
this._onDidChangeAgents.fire();
this._onDidChangeAgents.fire(data.agent);
}

getDefaultAgent(): IChatAgent | undefined {
Expand Down
14 changes: 13 additions & 1 deletion src/vs/workbench/contrib/chat/common/chatServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Progress } from 'vs/platform/progress/common/progress';
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IChatAgentRequest, IChatAgentResult, IChatAgentService } from 'vs/workbench/contrib/chat/common/chatAgents';
import { IChatAgent, IChatAgentRequest, IChatAgentResult, IChatAgentService } from 'vs/workbench/contrib/chat/common/chatAgents';
import { CONTEXT_PROVIDER_EXISTS } from 'vs/workbench/contrib/chat/common/chatContextKeys';
import { ChatModel, ChatModelInitState, ChatRequestModel, ChatWelcomeMessageModel, IChatModel, IChatRequestVariableData, ISerializableChatData, ISerializableChatsData, getHistoryEntriesFromModel, updateRanges } from 'vs/workbench/contrib/chat/common/chatModel';
import { ChatRequestAgentPart, ChatRequestAgentSubcommandPart, ChatRequestSlashCommandPart, IParsedChatRequest, getPromptText } from 'vs/workbench/contrib/chat/common/chatParserTypes';
Expand Down Expand Up @@ -193,6 +193,12 @@ export class ChatService extends Disposable implements IChatService {
}

this._register(storageService.onWillSaveState(() => this.saveState()));

this._register(Event.debounce(this.chatAgentService.onDidChangeAgents, () => { }, 500)(() => {
for (const model of this._sessionModels.values()) {
this.warmSlashCommandCache(model);
}
}));
}

private saveState(): void {
Expand Down Expand Up @@ -358,9 +364,15 @@ export class ChatService extends Disposable implements IChatService {
this.initializeSession(model, CancellationToken.None);
}

private warmSlashCommandCache(model: IChatModel, agent?: IChatAgent) {
const agents = agent ? [agent] : this.chatAgentService.getAgents();
agents.forEach(agent => agent.provideSlashCommands(model, [], CancellationToken.None));
}

private async initializeSession(model: ChatModel, token: CancellationToken): Promise<void> {
try {
this.trace('initializeSession', `Initialize session ${model.sessionId}`);
this.warmSlashCommandCache(model);
model.startInitialize();
await this.extensionService.activateByEvent(`onInteractiveSession:${model.providerId}`);

Expand Down

0 comments on commit 9bd6ee4

Please sign in to comment.