Skip to content

Commit

Permalink
Refactor PowerPagesChatParticipant and add CommandRegistry (#1050)
Browse files Browse the repository at this point in the history
Co-authored-by: amitjoshi <[email protected]>
  • Loading branch information
amitjoshi438 and amitjoshi authored Oct 25, 2024
1 parent 6e9e863 commit 7246c08
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/common/chat-participants/CommandRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

import * as vscode from "vscode";

export interface Command {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
execute(request: any, stream: vscode.ChatResponseStream): Promise<any>;
}

export class CommandRegistry {
private commands: { [key: string]: Command } = {};

register(commandName: string, command: Command) {
this.commands[commandName] = command;
}

get(commandName: string): Command {
return this.commands[commandName];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import { orgChangeErrorEvent, orgChangeEvent } from '../../../client/OrgChangeNo
import { isPowerPagesGitHubCopilotEnabled } from '../../copilot/utils/copilotUtil';
import { ADX_WEBPAGE, IApiRequestParams, IRelatedFiles } from '../../constants';
import { oneDSLoggerWrapper } from '../../OneDSLoggerTelemetry/oneDSLoggerWrapper';
import { CommandRegistry } from '../CommandRegistry';

// Initialize Command Registry and Register Commands
const commandRegistry = new CommandRegistry();
//Register Commands

export class PowerPagesChatParticipant {
private static instance: PowerPagesChatParticipant | null = null;
Expand Down Expand Up @@ -154,7 +159,19 @@ export class PowerPagesChatParticipant {
const location = activeFileUri ? createAndReferenceLocation(activeFileUri, startLine, endLine) : undefined;

if (request.command) {
//TODO: Handle command scenarios
const command = commandRegistry.get(request.command);

const commandRequest = {
request,
stream,
intelligenceAPIEndpointInfo,
intelligenceApiToken,
powerPagesAgentSessionId: this.powerPagesAgentSessionId,
telemetry: this.telemetry
};

return await command.execute(commandRequest, stream);

} else {
if (location) {
this.telemetry.sendTelemetryEvent(VSCODE_EXTENSION_GITHUB_POWER_PAGES_AGENT_LOCATION_REFERENCED, { sessionId: this.powerPagesAgentSessionId, orgId: this.orgID, environmentId: this.environmentID, userId: userId });
Expand Down Expand Up @@ -217,7 +234,6 @@ export class PowerPagesChatParticipant {
return createSuccessResult('', scenario.toString(), this.orgID);
}

return createSuccessResult('', '', this.orgID);
} catch (error) {
this.telemetry.sendTelemetryEvent(VSCODE_EXTENSION_GITHUB_POWER_PAGES_AGENT_ERROR, { sessionId: this.powerPagesAgentSessionId, error: error as string });
oneDSLoggerWrapper.getLogger().traceError(VSCODE_EXTENSION_GITHUB_POWER_PAGES_AGENT_ERROR, error as string, error as Error, { sessionId: this.powerPagesAgentSessionId }, {});
Expand Down

0 comments on commit 7246c08

Please sign in to comment.