Skip to content

Commit b627a9c

Browse files
authored
Add "developer: inspect chat model" action (#285707)
1 parent a30e5c8 commit b627a9c

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/vs/workbench/contrib/chat/browser/actions/chatDeveloperActions.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import { ServicesAccessor } from '../../../../../editor/browser/editorExtensions
88
import { localize2 } from '../../../../../nls.js';
99
import { Categories } from '../../../../../platform/action/common/actionCommonCategories.js';
1010
import { Action2, registerAction2 } from '../../../../../platform/actions/common/actions.js';
11+
import { IEditorService } from '../../../../services/editor/common/editorService.js';
1112
import { ChatContextKeys } from '../../common/actions/chatContextKeys.js';
1213
import { IChatService } from '../../common/chatService/chatService.js';
1314
import { IChatWidgetService } from '../chat.js';
1415

1516
export function registerChatDeveloperActions() {
1617
registerAction2(LogChatInputHistoryAction);
1718
registerAction2(LogChatIndexAction);
19+
registerAction2(InspectChatModelAction);
1820
}
1921

2022
class LogChatInputHistoryAction extends Action2 {
@@ -56,3 +58,57 @@ class LogChatIndexAction extends Action2 {
5658
chatService.logChatIndex();
5759
}
5860
}
61+
62+
class InspectChatModelAction extends Action2 {
63+
static readonly ID = 'workbench.action.chat.inspectChatModel';
64+
65+
constructor() {
66+
super({
67+
id: InspectChatModelAction.ID,
68+
title: localize2('workbench.action.chat.inspectChatModel.label', "Inspect Chat Model"),
69+
icon: Codicon.inspect,
70+
category: Categories.Developer,
71+
f1: true,
72+
precondition: ChatContextKeys.enabled
73+
});
74+
}
75+
76+
override async run(accessor: ServicesAccessor, ...args: unknown[]): Promise<void> {
77+
const chatWidgetService = accessor.get(IChatWidgetService);
78+
const editorService = accessor.get(IEditorService);
79+
const widget = chatWidgetService.lastFocusedWidget;
80+
81+
if (!widget?.viewModel) {
82+
return;
83+
}
84+
85+
const model = widget.viewModel.model;
86+
const modelData = model.toJSON();
87+
88+
// Build markdown output with latest response at the top
89+
let output = '# Chat Model Inspection\n\n';
90+
91+
// Show latest response first if it exists
92+
const requests = modelData.requests;
93+
if (requests && requests.length > 0) {
94+
const latestRequest = requests[requests.length - 1];
95+
if (latestRequest.response) {
96+
output += '## Latest Response\n\n';
97+
output += '```json\n' + JSON.stringify(latestRequest.response, null, 2) + '\n```\n\n';
98+
}
99+
}
100+
101+
// Show full model data
102+
output += '## Full Chat Model\n\n';
103+
output += '```json\n' + JSON.stringify(modelData, null, 2) + '\n```\n';
104+
105+
await editorService.openEditor({
106+
resource: undefined,
107+
contents: output,
108+
languageId: 'markdown',
109+
options: {
110+
pinned: true
111+
}
112+
});
113+
}
114+
}

src/vs/workbench/contrib/chat/browser/widget/chatWidgetService.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ export class ChatWidgetService extends Disposable implements IChatWidgetService
231231
this._widgets.push(newWidget);
232232
this._onDidAddWidget.fire(newWidget);
233233

234+
if (!this._lastFocusedWidget) {
235+
this.setLastFocusedWidget(newWidget);
236+
}
237+
234238
return combinedDisposable(
235239
newWidget.onDidFocus(() => this.setLastFocusedWidget(newWidget)),
236240
newWidget.onDidChangeViewModel(({ previousSessionResource, currentSessionResource }) => {
@@ -245,7 +249,12 @@ export class ChatWidgetService extends Disposable implements IChatWidgetService
245249
}
246250
});
247251
}),
248-
toDisposable(() => this._widgets.splice(this._widgets.indexOf(newWidget), 1))
252+
toDisposable(() => {
253+
this._widgets.splice(this._widgets.indexOf(newWidget), 1);
254+
if (this._lastFocusedWidget === newWidget) {
255+
this.setLastFocusedWidget(undefined);
256+
}
257+
})
249258
);
250259
}
251260
}

0 commit comments

Comments
 (0)