Skip to content

Add an icon to clear the copilot chat #2236

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 3 commits into from
Mar 18, 2025
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
12 changes: 12 additions & 0 deletions vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@
"command": "qsharp-vscode.workspacesAdd",
"when": "view == quantum-workspaces",
"group": "navigation"
},
{
"command": "qsharp-vscode.copilotClear",
"when": "view == quantum-copilot",
"group": "navigation"
}
],
"view/item/context": [
Expand Down Expand Up @@ -386,6 +391,13 @@
"title": "Remove workspace connection",
"icon": "$(remove)"
},
{
"command": "qsharp-vscode.copilotClear",
"category": "Q#",
"title": "Clear Quantum Copilot chat",
"enablement": "!qdkCopilotIsBusy",
"icon": "$(clear-all)"
},
{
"command": "qsharp-vscode.workspacePythonCode",
"category": "Q#",
Expand Down
10 changes: 9 additions & 1 deletion vscode/src/copilot/copilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { OpenAIChatService } from "./openAiChatService";
import { getRandomGuid } from "../utils";
import { EventType, sendTelemetryEvent, UserFlowStatus } from "../telemetry";
import { knownToolNameOrDefault } from "./azqTools";
import { commands } from "vscode";

export class Copilot {
private service: IChatService;
Expand Down Expand Up @@ -59,7 +60,14 @@ export class Copilot {
}
this.messages.push({ role: "user", content: userMessage });

await this.completeChat();
// Set the context so the 'clear' command is disabled when processing a response.
await commands.executeCommand("setContext", `qdkCopilotIsBusy`, true);
try {
await this.completeChat();
} finally {
// Reset the busy context when the chat is done.
await commands.executeCommand("setContext", `qdkCopilotIsBusy`, false);
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions vscode/src/copilot/webviewViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { log } from "qsharp-lang";
import {
commands,
CancellationToken,
ExtensionContext,
Uri,
Expand All @@ -13,6 +14,7 @@ import {
} from "vscode";
import { Copilot, CopilotUpdateHandler } from "./copilot";
import { CopilotCommand } from "./shared";
import { qsharpExtensionId } from "../common";

export function registerCopilotPanel(context: ExtensionContext): void {
const provider = new CopilotWebviewViewProvider(context.extensionUri);
Expand All @@ -25,6 +27,11 @@ export function registerCopilotPanel(context: ExtensionContext): void {
},
),
);
context.subscriptions.push(
commands.registerCommand(`${qsharpExtensionId}.copilotClear`, async () => {
provider.clearChat();
}),
);
}

class CopilotWebviewViewProvider implements WebviewViewProvider {
Expand Down Expand Up @@ -99,6 +106,10 @@ class CopilotWebviewViewProvider implements WebviewViewProvider {
}
}

clearChat() {
this.copilot?.restartChat([]);
}

handleMessageFromWebview(message: CopilotCommand) {
if (!this.copilot) {
return;
Expand Down