Path: blob/main/src/vs/workbench/contrib/chat/browser/actions/chatDeveloperActions.ts
5245 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Codicon } from '../../../../../base/common/codicons.js';6import { isUriComponents, URI } from '../../../../../base/common/uri.js';7import { ServicesAccessor } from '../../../../../editor/browser/editorExtensions.js';8import { localize2 } from '../../../../../nls.js';9import { Categories } from '../../../../../platform/action/common/actionCommonCategories.js';10import { Action2, registerAction2 } from '../../../../../platform/actions/common/actions.js';11import { IEditorService } from '../../../../services/editor/common/editorService.js';12import { ChatContextKeys } from '../../common/actions/chatContextKeys.js';13import { IChatService } from '../../common/chatService/chatService.js';14import { IChatWidgetService } from '../chat.js';1516function uriReplacer(_key: string, value: unknown): unknown {17if (URI.isUri(value)) {18return value.toString();19}2021if (isUriComponents(value)) {22// This shouldn't be necessary but it seems that some URIs in ChatModels aren't properly revived23return URI.from(value).toString();24}2526return value;27}2829export function registerChatDeveloperActions() {30registerAction2(LogChatInputHistoryAction);31registerAction2(LogChatIndexAction);32registerAction2(InspectChatModelAction);33}3435class LogChatInputHistoryAction extends Action2 {36static readonly ID = 'workbench.action.chat.logInputHistory';3738constructor() {39super({40id: LogChatInputHistoryAction.ID,41title: localize2('workbench.action.chat.logInputHistory.label', "Log Chat Input History"),42icon: Codicon.attach,43category: Categories.Developer,44f1: true,45precondition: ChatContextKeys.enabled46});47}4849override async run(accessor: ServicesAccessor, ...args: unknown[]): Promise<void> {50const chatWidgetService = accessor.get(IChatWidgetService);51chatWidgetService.lastFocusedWidget?.logInputHistory();52}53}5455class LogChatIndexAction extends Action2 {56static readonly ID = 'workbench.action.chat.logChatIndex';5758constructor() {59super({60id: LogChatIndexAction.ID,61title: localize2('workbench.action.chat.logChatIndex.label', "Log Chat Index"),62icon: Codicon.attach,63category: Categories.Developer,64f1: true,65precondition: ChatContextKeys.enabled66});67}6869override async run(accessor: ServicesAccessor, ...args: unknown[]): Promise<void> {70const chatService = accessor.get(IChatService);71chatService.logChatIndex();72}73}7475class InspectChatModelAction extends Action2 {76static readonly ID = 'workbench.action.chat.inspectChatModel';7778constructor() {79super({80id: InspectChatModelAction.ID,81title: localize2('workbench.action.chat.inspectChatModel.label', "Inspect Chat Model"),82icon: Codicon.inspect,83category: Categories.Developer,84f1: true,85precondition: ChatContextKeys.enabled86});87}8889override async run(accessor: ServicesAccessor, ...args: unknown[]): Promise<void> {90const chatWidgetService = accessor.get(IChatWidgetService);91const editorService = accessor.get(IEditorService);92const widget = chatWidgetService.lastFocusedWidget;9394if (!widget?.viewModel) {95return;96}9798const model = widget.viewModel.model;99const modelData = model.toJSON();100101// Build markdown output with latest response at the top102let output = '# Chat Model Inspection\n\n';103104// Show latest response first if it exists105const requests = modelData.requests;106if (requests && requests.length > 0) {107const latestRequest = requests[requests.length - 1];108if (latestRequest.response) {109output += '## Latest Response\n\n';110output += '```json\n' + JSON.stringify(latestRequest.response, uriReplacer, 2) + '\n```\n\n';111}112}113114// Show full model data115output += '## Full Chat Model\n\n';116output += '```json\n' + JSON.stringify(modelData, uriReplacer, 2) + '\n```\n';117118await editorService.openEditor({119resource: undefined,120contents: output,121languageId: 'markdown',122options: {123pinned: true124}125});126}127}128129130