|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE.md in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { callWithTelemetryAndErrorHandling, IActionContext } from "@microsoft/vscode-azext-utils"; |
| 7 | +import { CopilotTool } from "@microsoft/vscode-inproc-mcp"; |
| 8 | +import { UnspecifiedOutputSchema } from "@microsoft/vscode-inproc-mcp/mcp"; |
| 9 | +import { l10n } from "vscode"; |
| 10 | +import type { z } from "zod"; |
| 11 | +import { ext } from "../../../../extensionVariables"; |
| 12 | +import { ActivitySelectedCache } from "../../../askAgentAboutActivityLog/ActivitySelectedCache"; |
| 13 | +import { GetAzureActivityLogContext } from "./GetAzureActivityLogContext"; |
| 14 | +import { convertActivityTreeToSimpleObjectArray, ConvertedActivityItem, ExcludedActivityItem } from "./convertActivityTree"; |
| 15 | +import { logActivityTelemetry, logSelectedActivityTelemetry } from "./logTelemetry"; |
| 16 | + |
| 17 | +const getAzureActivityLogToolName = 'get_azure_activity_log'; |
| 18 | + |
| 19 | +export const getAzureActivityLogTool: CopilotTool<z.ZodVoid, typeof UnspecifiedOutputSchema> = { |
| 20 | + name: getAzureActivityLogToolName, |
| 21 | + description: 'Get the Azure activity log from VS Code.', |
| 22 | + annotations: { |
| 23 | + readOnlyHint: true, |
| 24 | + idempotentHint: true, |
| 25 | + }, |
| 26 | + execute: async (_, extras) => { |
| 27 | + return await callWithTelemetryAndErrorHandling(`mcpTool/${getAzureActivityLogToolName}/execute`, async (context: IActionContext) => { |
| 28 | + context.telemetry.properties.isCopilotEvent = 'true'; |
| 29 | + context.telemetry.properties.sessionId = extras?.sessionId; |
| 30 | + context.telemetry.properties.requestId = extras?.requestId.toString(); |
| 31 | + return await getAzureActivityLog(context); |
| 32 | + }) ?? { |
| 33 | + message: l10n.t('Failed to get the Azure activity log.'), |
| 34 | + activityItems: [], |
| 35 | + }; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +async function getAzureActivityLog(actionContext: IActionContext): Promise<{ message: string; activityItems: string[] }> { |
| 40 | + const context: GetAzureActivityLogContext = Object.assign(actionContext, { activitySelectedCache: ActivitySelectedCache.getInstance() }); |
| 41 | + |
| 42 | + const convertedActivityItems: ConvertedActivityItem[] = await convertActivityTreeToSimpleObjectArray(context); |
| 43 | + logActivityTelemetry(context, convertedActivityItems); |
| 44 | + |
| 45 | + let selectedActivityItems: ConvertedActivityItem[] = convertedActivityItems.filter(item => !(item as ExcludedActivityItem)._exclude); |
| 46 | + logSelectedActivityTelemetry(context, selectedActivityItems); |
| 47 | + |
| 48 | + if (!context.activitySelectedCache.selectionCount) { |
| 49 | + // If no items were selected (i.e. invoked mcp without using a VS Code command), default to providing the entire activity tree. |
| 50 | + selectedActivityItems = convertedActivityItems; |
| 51 | + } else if (selectedActivityItems.length !== context.activitySelectedCache.selectionCount) { |
| 52 | + // If we weren't able to verify all of the selected items, fallback to providing the entire activity tree |
| 53 | + selectedActivityItems = convertedActivityItems; |
| 54 | + |
| 55 | + const warning: string = l10n.t('Failed to provide some of the selected item(s) to Copilot. Falling back to providing the entire activity log tree.'); |
| 56 | + void context.ui.showWarningMessage(warning); |
| 57 | + ext.outputChannel.warn(warning); |
| 58 | + } |
| 59 | + |
| 60 | + context.activitySelectedCache.reset(); |
| 61 | + |
| 62 | + if (selectedActivityItems.length === 0) { |
| 63 | + return { |
| 64 | + message: |
| 65 | + 'No activity log items found.', |
| 66 | + activityItems: [], |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + return { |
| 71 | + message: |
| 72 | + 'Explain the data from the following activity items. ' + |
| 73 | + 'Prefer explaining the data more conversationally rather than responding with the raw json data. ' + |
| 74 | + 'The activities provided are in chronological order.', |
| 75 | + activityItems: selectedActivityItems.map(item => JSON.stringify(item)), |
| 76 | + }; |
| 77 | +} |
0 commit comments