-
Notifications
You must be signed in to change notification settings - Fork 76
utils: Changes to enable re running commands with copilot #2136
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
Changes from all commits
ec2baf5
61c7344
62ca7a6
c06071a
68d59d9
57fb835
049bb6c
27018b6
4ec5036
a29d225
08dec57
c204f10
fb58cdb
f1a2d6a
b15a1a8
569916a
27164e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,34 +3,35 @@ | |
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import * as vscode from "vscode"; | ||
| import type { CopilotClient, CopilotSession } from "@github/copilot-sdk"; | ||
| import type * as vscode from "vscode"; | ||
| import { InvalidCopilotResponseError } from "../errors"; | ||
|
|
||
| const languageModelPreference: { vendor: "copilot", family: string }[] = [ | ||
| // not yet seen/available | ||
| // { vendor: "copilot", family: "gpt-4-turbo-preview" }, | ||
| // seen/available | ||
| { vendor: "copilot", family: "gpt-4o" }, | ||
| { vendor: "copilot", family: "gpt-4-turbo" }, | ||
| { vendor: "copilot", family: "gpt-4" }, | ||
| { vendor: "copilot", family: "gpt-3.5-turbo" }, | ||
| ]; | ||
|
|
||
| async function selectMostPreferredLm(): Promise<vscode.LanguageModelChat | undefined> { | ||
| const lms = await vscode.lm.selectChatModels({ vendor: "copilot" }); | ||
| const lmsInPreferredOrder = (lms || []) | ||
| .filter((lm) => languageModelPreference.some((pref) => pref.family === lm.family && pref.vendor === lm.vendor)) | ||
| .sort((a, b) => languageModelPreference.findIndex((pref) => pref.family === a.family && pref.vendor === a.vendor) - languageModelPreference.findIndex((pref) => pref.family === b.family && pref.vendor === b.vendor)); | ||
| return lmsInPreferredOrder?.at(0); | ||
| let client: CopilotClient | undefined; | ||
| let session: CopilotSession | undefined; | ||
|
|
||
| async function loadCopilotSdk(): Promise<typeof import("@github/copilot-sdk")> { | ||
| return await import("@github/copilot-sdk"); | ||
| } | ||
|
|
||
| export function createPrimaryPromptToGetSingleQuickPickInput(picks: string[], relevantContext?: string): string { | ||
| return `The User is asking you to choose an item based on the following information: | ||
| 1. Choose from this list of picks ${picks.join(", ")}. | ||
| 2. You must choose one item from the list | ||
| 3. Use information from this context, if available, to make your decision: ${relevantContext ? relevantContext : "No context available"} | ||
| 4. If there is an option to skipForNow, you can choose that option. | ||
| Respond with a JSON object of the pick you have chosen. Do not respond in a conversational tone, only JSON. `; | ||
| function getCopilotCliPath(): string { | ||
| return require.resolve(`@github/copilot-${process.platform}-${process.arch}`); | ||
| } | ||
|
|
||
| export function createPrimaryPromptToGetSingleQuickPickInput(picks: string[], placeholder?: string): string { | ||
| return ` | ||
| Task: choose one pick. | ||
|
|
||
| Input: | ||
| picks: ${JSON.stringify(picks)} | ||
| placeholder: ${placeholder ?? null} | ||
|
|
||
| Rules: | ||
| - If the picks represent container image tags, always prefer the newest available image tag. | ||
|
|
||
| Output: | ||
| Return ONLY the JSON object with fields "label" and "description". | ||
| `; | ||
| } | ||
|
|
||
| export function createPrimaryPromptToGetPickManyQuickPickInput(picks: string[], relevantContext?: string): string { | ||
|
|
@@ -63,35 +64,68 @@ export function createPrimaryPromptForWorkspaceFolderPick(folders: readonly vsco | |
| Respond with the workspace folder you have chosen. Do not respond in a conversational tone. `; | ||
| } | ||
|
|
||
| export async function doCopilotInteraction(primaryPrompt: string): Promise<string> { | ||
| let messages: vscode.LanguageModelChatMessage[] = []; | ||
| const lm: vscode.LanguageModelChat | undefined = await selectMostPreferredLm(); | ||
|
|
||
| messages = [ | ||
| new vscode.LanguageModelChatMessage(vscode.LanguageModelChatMessageRole.User, primaryPrompt) | ||
| ]; | ||
|
|
||
| if (lm !== undefined) { | ||
| const chatRequestOptions = { justification: `Access to Copilot for the @azure agent.` }; | ||
| const request = await lm.sendRequest(messages, chatRequestOptions); | ||
| const fragments: string[] = []; | ||
| try { | ||
| // Consume the stream and collect fragments | ||
| for await (const fragment of request.text) { | ||
| fragments.push(fragment); | ||
| } | ||
|
|
||
| // Combine fragments into a single string | ||
| const responseText = fragments.join(""); | ||
| const cleanedResponse = extractJsonString(responseText); | ||
| return cleanedResponse; | ||
| } catch { | ||
| throw new InvalidCopilotResponseError(); | ||
| } | ||
| export async function doGithubCopilotInteraction(primaryPrompt: string, relevantContext?: string): Promise<string> { | ||
| const session = await getCopilotSession(relevantContext); | ||
| const response = await session.sendAndWait({ | ||
| prompt: primaryPrompt, | ||
| mode: "immediate" | ||
| }); | ||
|
|
||
| if (!response || !response.data) { | ||
| throw new InvalidCopilotResponseError(); | ||
| } | ||
|
|
||
| return response?.data.content; | ||
| } | ||
|
Comment on lines
+67
to
+79
|
||
|
|
||
| export async function getCopilotSession(relevantContext?: string): Promise<CopilotSession> { | ||
| if (session) { | ||
| return session; | ||
| } | ||
|
|
||
| const { CopilotClient } = await loadCopilotSdk(); | ||
| client = new CopilotClient({ cliPath: getCopilotCliPath() }); | ||
| session = await client.createSession({ | ||
| onPermissionRequest: () => ({ kind: "approved" }) | ||
| }); | ||
| const activityChildren = extractActivityChildren(relevantContext || ''); | ||
| const subscriptionId = relevantContext ? extractSubscriptionIdFromContext(relevantContext) : undefined; | ||
|
Comment on lines
+81
to
+92
|
||
|
|
||
| await session.sendAndWait({ | ||
| mode: "immediate", | ||
| prompt: `Picker. Choose one. Never explain your reasoning. Never use markdown. Output: {"label":"X","description":"Y"} | ||
|
|
||
| Rules (priority) | ||
| 1. Match activityChildren "Use X" → pick X | ||
| 2. Match subscriptionId → pick.data /subscriptions/{id} | ||
| 3. Else skipForNow | ||
|
|
||
| Context: ${JSON.stringify({ activityChildren, subscriptionId, relevantContext })}` | ||
| }); | ||
|
|
||
| return session; | ||
| } | ||
|
|
||
| export async function disposeCopilotSession(): Promise<void> { | ||
| if (client) { | ||
| await client.stop(); | ||
| client = undefined; | ||
| session = undefined; | ||
| } | ||
| return ""; | ||
| } | ||
|
|
||
| function extractJsonString(raw: string): string { | ||
| return raw.replace(/```json\s*|```/g, '').trim(); | ||
| function extractSubscriptionIdFromContext(context: string): string | undefined { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is going to be too strict for any other cloud (government, China, etc.) It's a little looser matching, but I don't think we need to be too strict since we kind of know what we're parsing. |
||
| const regex = /https:\/\/management\.[^/]+\/subscriptions\/([0-9a-fA-F-]{36})/; | ||
| const match = context.match(regex); | ||
| return match ? match[1] : undefined; | ||
| } | ||
|
|
||
| function extractActivityChildren(context: string): string | undefined { | ||
| try { | ||
| const activityLog = JSON.parse(context) as { children?: unknown }; | ||
| const children = activityLog.children; | ||
| return JSON.stringify(children); | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
| import * as vscode from "vscode"; | ||
| import * as types from '../index'; | ||
|
|
||
| export function executeCommandWithAddedContext<T = unknown>(commandId: string, additionalContext: Partial<types.IActionContext>, ...args: unknown[]): Thenable<T> { | ||
| const metadata = { __injectedContext: additionalContext }; | ||
| return vscode.commands.executeCommand(commandId, ...args, metadata); | ||
| } | ||
|
Comment on lines
+8
to
+11
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import * as types from '../../index'; | ||
| import { AzureWizard } from '../wizard/AzureWizard'; | ||
|
|
||
| export async function runGenericPromptStep(context: types.PickExperienceContext, wizardOptions?: types.IWizardOptions<types.AzureResourceQuickPickWizardContext>): Promise<void> { | ||
| const wizard = new AzureWizard(context, { | ||
| ...wizardOptions, | ||
| hideStepCount: wizardOptions?.hideStepCount ?? true, | ||
| showLoadingPrompt: wizardOptions?.showLoadingPrompt ?? true | ||
| }); | ||
|
|
||
| await wizard.prompt(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,20 @@ export function registerCommand(commandId: string, callback: (context: types.IAc | |
| return await callWithTelemetryAndErrorHandling( | ||
| telemetryId || commandId, | ||
| async (context: types.IActionContext) => { | ||
| let injectedContext: Partial<types.IActionContext> | undefined; | ||
| if (args.length > 0) { | ||
| const metadata = args.find( | ||
| (arg) => arg !== null && typeof arg === "object" && Object.prototype.hasOwnProperty.call(arg, "__injectedContext") | ||
| ); | ||
|
|
||
| if (metadata) { | ||
| const candidate = (metadata as Record<string, unknown>).__injectedContext; | ||
| if (candidate && typeof candidate === "object") { | ||
| injectedContext = candidate as Partial<types.IActionContext>; | ||
| } | ||
| args.splice(args.indexOf(metadata), 1); // remove only the metadata | ||
|
Comment on lines
+47
to
+56
|
||
| } | ||
|
|
||
| try { | ||
| await setTelemetryProperties(context, args); | ||
| } catch (e: unknown) { | ||
|
|
@@ -52,8 +65,8 @@ export function registerCommand(commandId: string, callback: (context: types.IAc | |
| context.telemetry.properties.telemetryError = error.message; | ||
| } | ||
| } | ||
|
|
||
| return callback(context, ...args); | ||
| const finalContext = injectedContext ? { ...context, ...injectedContext } : context; | ||
| return callback(finalContext, ...args); | ||
| } | ||
| ); | ||
| })); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
relevantContextis ignored ifsessionalready exists due to the logic on line 68. Might want to redesign this so thatsessionisn't a global variable.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is by design we only want to pass in the relevant context when we create the session. In the subsequent calls it will be using that context and the prompt is changed through
createPrimaryPromptToGetSingleQuickPickInputso that the proper placeholder and picks get sent each time. If there is no session found then we need to send the relevantContext when we create the session.I designed it this way as it is much faster to not have create a new session on each quick pick call and send in all the relevant context everytime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of storing the Copilot session as a module-level global, it might be best to move the session ownership into the CopilotUserInput class itself.
Each CopilotUserInput instance could lazily create its own CopilotSession on the first interaction and reuse it for subsequent wizard steps within the same command so we keep the performance benefit of not recreating sessions for each step. But since each command already gets its own CopilotUserInput via context.ui, the session is naturally scoped to that command's lifecycle. This removes the need for callers to remember to call disposeCopilotSession(), and makes concurrent commands safe since they'd each have their own session. Also, the doGithubCopilotInteraction function would take a session as a parameter instead of managing the global itself.