-
Notifications
You must be signed in to change notification settings - Fork 82
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 2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,9 @@ import * 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-5-mini" }, | ||
| { vendor: "copilot", family: "claude-sonnet-4" }, | ||
| { vendor: "copilot", family: "claude-sonnet-4.5" }, | ||
| { vendor: "copilot", family: "gpt-4o" }, | ||
| { vendor: "copilot", family: "gpt-4-turbo" }, | ||
| { vendor: "copilot", family: "gpt-4" }, | ||
|
|
@@ -23,12 +23,21 @@ async function selectMostPreferredLm(): Promise<vscode.LanguageModelChat | undef | |
| return lmsInPreferredOrder?.at(0); | ||
| } | ||
|
|
||
| export function createPrimaryPromptToGetSingleQuickPickInput(picks: string[], relevantContext?: string): string { | ||
| export function createPrimaryPromptToGetSingleQuickPickInput(picks: string[], placeholder?: string, relevantContext?: string): string { | ||
| const subscriptionId = relevantContext ? extractSubscriptionIdFromContext(relevantContext) : undefined; | ||
| const activityChildren = extractActivityChildren(relevantContext || ''); | ||
| 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. | ||
| 3. The placeholder contains the question being asked use this to help guide your input: ${placeholder ? placeholder : "No placeholder available"} | ||
| 4. Use information from this context, if available, to make your decision: ${relevantContext ? relevantContext : "No context available"} | ||
| 5. If there are activity children you must use them to them to inform your decision: ${activityChildren ? activityChildren : "No activity children available"} | ||
| Here are some examples on how to use activity children to inform your decision: | ||
| - If the placeholder is 'Select a container app' and there is an activity child 'Use container app: myContainerApp', you should choose the pick with the label 'myContainerApp' from the picks. | ||
| - If the placeholder is 'Select a container apps environment' and there is an activity child 'Use managed environment: myEnv', you should choose the pick with the label'myEnv' from the picks. | ||
| 6. If there is an option to skipForNow, you can choose that option. | ||
| 7. If the placeholder is 'Select subscription' use the subscription id to help guide your input. Use the subscription id: ${subscriptionId} | ||
| and match to the data portion of the picks which will have the id in the form of accounts/{accountId}/tenants/{tenantId}/subscriptions/{subscriptionID}. Match the {subscriptionId} portions to inform your decision. | ||
| Respond with a JSON object of the pick you have chosen. Do not respond in a conversational tone, only JSON. `; | ||
| } | ||
|
|
||
|
|
@@ -94,3 +103,15 @@ export async function doCopilotInteraction(primaryPrompt: string): Promise<strin | |
| function extractJsonString(raw: string): string { | ||
| return raw.replace(/```json\s*|```/g, '').trim(); | ||
| } | ||
|
|
||
| function extractSubscriptionIdFromContext(context: string): string | undefined { | ||
| const regex = /https:\/\/management\.azure\.com\/subscriptions\/([0-9a-fA-F-]{36})/; | ||
| const match = context.match(regex); | ||
| return match ? match[1] : undefined; | ||
| } | ||
|
|
||
| function extractActivityChildren(context: string): string { | ||
| const activityLog = JSON.parse(context) as { children?: unknown }; | ||
|
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. Should probably try/catch this just in case the shape is corrupted. |
||
| const children = activityLog.children; | ||
| return JSON.stringify(children); | ||
| } | ||
| 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, { | ||||||||||||||
| hideStepCount: true, | ||||||||||||||
| showLoadingPrompt: wizardOptions?.showLoadingPrompt ?? true, | ||||||||||||||
| ...wizardOptions | ||||||||||||||
|
||||||||||||||
| hideStepCount: true, | |
| showLoadingPrompt: wizardOptions?.showLoadingPrompt ?? true, | |
| ...wizardOptions | |
| ...wizardOptions, | |
| hideStepCount: wizardOptions?.hideStepCount ?? true, | |
| showLoadingPrompt: wizardOptions?.showLoadingPrompt ?? true |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,18 @@ 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[args.length - 1]; | ||
|
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. Should we just do a |
||
|
|
||
| // Look for our metadata object at the end | ||
| if (metadata && typeof metadata === "object" && "__injectedContext" in metadata) { | ||
| injectedContext = metadata.__injectedContext as types.IActionContext; | ||
|
|
||
| // remove only the metadata | ||
| args.pop(); | ||
| } | ||
|
|
||
| try { | ||
| await setTelemetryProperties(context, args); | ||
| } catch (e: unknown) { | ||
|
|
@@ -52,8 +63,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); | ||
| } | ||
| ); | ||
| })); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,14 +91,18 @@ export class CopilotUserInput implements types.IAzureUserInput { | |
| public async showQuickPick<T extends types.IAzureQuickPickItem<unknown>>(items: T[] | Thenable<T[]>, options: vscodeTypes.QuickPickOptions): Promise<T | T[]> { | ||
| let primaryPrompt: string; | ||
| const resolvedItems: T[] = await Promise.resolve(items); | ||
| const jsonItems: string[] = resolvedItems.map(item => JSON.stringify(item)); | ||
|
|
||
| // Clean up items to only include label and description | ||
| const cleanedItems = this.cleanQuickPickItems(resolvedItems); | ||
| const jsonItems = cleanedItems.map(item => JSON.stringify(item)); | ||
| try { | ||
| if (options.canPickMany) { | ||
| primaryPrompt = createPrimaryPromptToGetPickManyQuickPickInput(jsonItems, this._relevantContext); | ||
| const response = await doCopilotInteraction(primaryPrompt); | ||
| const jsonResponse: T[] = JSON.parse(response) as T[]; | ||
| const picks = resolvedItems.filter(item => { | ||
| return jsonResponse.some(resp => JSON.stringify(resp) === JSON.stringify(item)); | ||
| return jsonResponse.some(resp => JSON.stringify(resp.label) === JSON.stringify(item.label) && | ||
| JSON.stringify(resp.description || '') === JSON.stringify(item.description || '')); | ||
| }); | ||
|
Comment on lines
104
to
107
|
||
|
|
||
| if (!picks || picks.length === 0) { | ||
|
|
@@ -109,11 +113,12 @@ export class CopilotUserInput implements types.IAzureUserInput { | |
|
|
||
| return picks; | ||
| } else { | ||
| primaryPrompt = createPrimaryPromptToGetSingleQuickPickInput(jsonItems, this._relevantContext); | ||
| primaryPrompt = createPrimaryPromptToGetSingleQuickPickInput(jsonItems, options.placeHolder, this._relevantContext); | ||
| const response = await doCopilotInteraction(primaryPrompt); | ||
| const jsonResponse: T = JSON.parse(response) as T; | ||
| const pick = resolvedItems.find(item => { | ||
| return JSON.stringify(item) === JSON.stringify(jsonResponse); | ||
| return JSON.stringify(item.label) === JSON.stringify(jsonResponse.label) && | ||
| JSON.stringify(item.description || '') === JSON.stringify(jsonResponse.description || ''); | ||
| }); | ||
|
|
||
| if (!pick) { | ||
|
|
@@ -128,4 +133,12 @@ export class CopilotUserInput implements types.IAzureUserInput { | |
| throw new InvalidCopilotResponseError(); | ||
| } | ||
| } | ||
|
|
||
| private cleanQuickPickItems<T extends types.IAzureQuickPickItem<unknown>>(items: T[]): { label: string, description: string, id: string | undefined }[] { | ||
| return items.map(item => ({ | ||
| label: item.label, | ||
| description: item.description || '', | ||
| id: (item.data && typeof item.data === 'object' && 'id' in item.data) ? String(item.data.id) : undefined | ||
| })); | ||
| } | ||
| } | ||
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.
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.