Skip to content

Commit 2324c95

Browse files
authored
fix: add debug mode in vscode (#2400)
1 parent b631a9d commit 2324c95

File tree

5 files changed

+83
-75
lines changed

5 files changed

+83
-75
lines changed

apps/vscode/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -796,11 +796,11 @@
796796
"default": "all",
797797
"description": "Controls which notifications are shown for Nx Cloud."
798798
},
799-
"nxConsole.enableNxCopilotFeatures": {
799+
"nxConsole.debugMode": {
800800
"type": "boolean",
801801
"default": false,
802802
"scope": "machine",
803-
"description": "Enable Nx-specific copilot features (Experimental)."
803+
"description": "Enable debug mode (internal)"
804804
}
805805
}
806806
},

libs/vscode/configuration/src/lib/configuration-keys.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const GLOBAL_CONFIG_KEYS = [
1414
'showNodeVersionOnStartup',
1515
'nxWorkspacePath',
1616
'nxCloudNotifications',
17-
'enableNxCopilotFeatures',
17+
'debugMode',
1818
] as const;
1919

2020
export type GlobalConfig = {
@@ -33,7 +33,7 @@ export type GlobalConfig = {
3333
showNodeVersionOnStartup: boolean;
3434
nxWorkspacePath: string;
3535
nxCloudNotifications: 'all' | 'errors' | 'none';
36-
enableNxCopilotFeatures: boolean;
36+
debugMode: boolean;
3737
};
3838

3939
/**

libs/vscode/copilot/src/lib/init-copilot.ts

+67-70
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
import { openGenerateUIPrefilled } from '@nx-console/vscode-generate-ui-webview';
88
import { EXECUTE_ARBITRARY_COMMAND } from '@nx-console/vscode-nx-commands-view';
99
import { getGenerators, getNxWorkspace } from '@nx-console/vscode-nx-workspace';
10-
import { renderPrompt } from '@vscode/prompt-tsx';
10+
import { sendChatParticipantRequest } from '@vscode/chat-extension-utils';
11+
import { PromptElementAndProps } from '@vscode/chat-extension-utils/dist/toolsPrompt';
1112
import { readFile } from 'fs/promises';
1213
import type { TargetConfiguration } from 'nx/src/devkit-exports.js';
1314
import {
@@ -19,8 +20,7 @@ import {
1920
ChatResponseStream,
2021
commands,
2122
ExtensionContext,
22-
LanguageModelChatMessage,
23-
lm,
23+
LanguageModelToolResult,
2424
MarkdownString,
2525
Uri,
2626
} from 'vscode';
@@ -51,20 +51,12 @@ const handler: ChatRequestHandler = async (
5151
token: CancellationToken
5252
) => {
5353
const enableNxCopilotFeaturesSetting = GlobalConfigurationStore.instance.get(
54-
'enableNxCopilotFeatures',
54+
'debugMode',
5555
false
5656
);
5757

5858
if (!enableNxCopilotFeaturesSetting) {
59-
stream.markdown(
60-
'The @nx copilot chat participant is experimental. To use it, please enable it in the settings.'
61-
);
62-
63-
stream.button({
64-
title: 'Enable Nx Copilot',
65-
command: 'workbench.action.openSettings',
66-
arguments: ['nxConsole.enableNxCopilotFeatures'],
67-
});
59+
stream.markdown('@nx is coming soon. Stay tuned!');
6860
return;
6961
}
7062
const workspacePath = getNxWorkspacePath();
@@ -75,7 +67,6 @@ const handler: ChatRequestHandler = async (
7567

7668
const pmExec = (await getPackageManagerCommand(workspacePath)).exec;
7769

78-
let messages: LanguageModelChatMessage[];
7970
const baseProps: NxCopilotPromptProps = {
8071
userQuery: request.prompt,
8172
projectGraph: projectGraph,
@@ -84,91 +75,97 @@ const handler: ChatRequestHandler = async (
8475
packageManagerExecCommand: pmExec,
8576
};
8677

78+
let promptElementAndProps: PromptElementAndProps<
79+
NxCopilotPrompt | GeneratePrompt
80+
>;
81+
8782
if (request.command === 'generate') {
8883
stream.progress('Retrieving generator schemas...');
8984

90-
const prompt = await renderPrompt(
91-
GeneratePrompt,
92-
{
85+
promptElementAndProps = {
86+
promptElement: GeneratePrompt,
87+
props: {
9388
...baseProps,
9489
generatorSchemas: await getGeneratorSchemas(),
9590
},
96-
{ modelMaxPromptTokens: request.model.maxInputTokens },
97-
request.model
98-
);
99-
messages = prompt.messages;
91+
};
10092
} else {
101-
try {
102-
const prompt = await renderPrompt(
103-
NxCopilotPrompt,
104-
baseProps,
105-
{ modelMaxPromptTokens: request.model.maxInputTokens },
106-
request.model
107-
);
108-
messages = prompt.messages;
109-
} catch (error) {
110-
console.error('Error rendering prompt:', error);
111-
stream.markdown(
112-
'An error occurred while rendering the prompt. Please try again later.'
113-
);
114-
return;
115-
}
93+
promptElementAndProps = {
94+
promptElement: NxCopilotPrompt,
95+
props: baseProps,
96+
};
11697
}
11798

118-
const chatResponse = await request.model.sendRequest(messages, {}, token);
99+
const chatParticipantRequest = sendChatParticipantRequest(
100+
request,
101+
context,
102+
{
103+
prompt: promptElementAndProps,
104+
responseStreamOptions: {
105+
stream,
106+
},
107+
tools: [],
108+
},
109+
token
110+
);
119111

120112
const startMarker = new RegExp(`"""\\s*${pmExec}\\s+nx\\s*`);
121113
const endMarker = `"""`;
122114

123115
let pendingText = '';
124116
let codeBuffer: string | null = null;
125117

126-
for await (const fragment of chatResponse.text) {
127-
if (codeBuffer !== null) {
128-
codeBuffer += fragment;
118+
for await (const fragment of chatParticipantRequest.stream) {
119+
if (fragment instanceof LanguageModelToolResult) {
120+
stream.markdown(JSON.stringify(fragment));
121+
continue;
129122
} else {
130-
pendingText += fragment;
131-
}
132-
133-
// Process when we're not in a code block: look for a start marker.
134-
while (codeBuffer === null) {
135-
const match = pendingText.match(startMarker);
136-
const startIndex = match ? match.index : -1;
137-
if (startIndex === -1) {
138-
break;
139-
}
140-
if (startIndex > 0) {
141-
stream.markdown(pendingText.slice(0, startIndex));
123+
if (codeBuffer !== null) {
124+
codeBuffer += fragment.value;
125+
} else {
126+
pendingText += fragment.value;
142127
}
143-
// Switch to code mode.
144-
codeBuffer = '';
145-
pendingText = pendingText.slice(startIndex + match[0].length);
146-
codeBuffer += pendingText;
147-
pendingText = '';
148-
}
149128

150-
// If we are in a code block, look for the end marker.
151-
while (codeBuffer !== null) {
152-
const endIndex = codeBuffer.indexOf(endMarker);
153-
if (endIndex === -1) {
154-
break;
129+
// Process when we're not in a code block: look for a start marker.
130+
while (codeBuffer === null) {
131+
const match = pendingText.match(startMarker);
132+
const startIndex = match ? match.index : -1;
133+
if (startIndex === -1) {
134+
break;
135+
}
136+
if (startIndex > 0) {
137+
stream.markdown(pendingText.slice(0, startIndex));
138+
}
139+
// Switch to code mode.
140+
codeBuffer = '';
141+
pendingText = pendingText.slice(startIndex + match[0].length);
142+
codeBuffer += pendingText;
143+
pendingText = '';
155144
}
156-
const codeSnippet = codeBuffer.slice(0, endIndex);
157145

158-
renderCommandSnippet(codeSnippet, stream, pmExec);
159-
codeBuffer = codeBuffer.slice(endIndex + endMarker.length);
146+
// If we are in a code block, look for the end marker.
147+
while (codeBuffer !== null) {
148+
const endIndex = codeBuffer.indexOf(endMarker);
149+
if (endIndex === -1) {
150+
break;
151+
}
152+
const codeSnippet = codeBuffer.slice(0, endIndex);
153+
154+
renderCommandSnippet(codeSnippet, stream, pmExec);
155+
codeBuffer = codeBuffer.slice(endIndex + endMarker.length);
160156

161-
// switch back to normal mode.
162-
pendingText += codeBuffer;
163-
codeBuffer = null;
157+
// switch back to normal mode.
158+
pendingText += codeBuffer;
159+
codeBuffer = null;
160+
}
164161
}
165162
}
166163

167164
if (codeBuffer === null && pendingText) {
168165
stream.markdown(pendingText);
169166
}
170167

171-
return;
168+
return await chatParticipantRequest.result;
172169
};
173170

174171
async function renderCommandSnippet(

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@monodon/typescript-nx-imports-plugin": "0.2.0",
2424
"@open-wc/lit-helpers": "^0.6.0",
2525
"@vscode-elements/elements": "1.9.2-pre.1",
26+
"@vscode/chat-extension-utils": "^0.0.0-alpha.5",
2627
"@vscode/codicons": "^0.0.36",
2728
"@vscode/webview-ui-toolkit": "^1.2.0",
2829
"@yarnpkg/fslib": "^3.0.0-rc.48",

yarn.lock

+11-1
Original file line numberDiff line numberDiff line change
@@ -5157,6 +5157,15 @@ __metadata:
51575157
languageName: node
51585158
linkType: hard
51595159

5160+
"@vscode/chat-extension-utils@npm:^0.0.0-alpha.5":
5161+
version: 0.0.0-alpha.5
5162+
resolution: "@vscode/chat-extension-utils@npm:0.0.0-alpha.5"
5163+
dependencies:
5164+
"@vscode/prompt-tsx": ^0.3.0-alpha.14
5165+
checksum: 20a182c54398d8985af47209dcb7efc6fef2eb47f25bfb13800d3c45546fe28042439386175264ff2b14a704d0601bc0a0a59fd146d2cdb8c331236be7982fc9
5166+
languageName: node
5167+
linkType: hard
5168+
51605169
"@vscode/codicons@npm:^0.0.36":
51615170
version: 0.0.36
51625171
resolution: "@vscode/codicons@npm:0.0.36"
@@ -5171,7 +5180,7 @@ __metadata:
51715180
languageName: node
51725181
linkType: hard
51735182

5174-
"@vscode/prompt-tsx@npm:^0.3.0-alpha.18":
5183+
"@vscode/prompt-tsx@npm:^0.3.0-alpha.14, @vscode/prompt-tsx@npm:^0.3.0-alpha.18":
51755184
version: 0.3.0-alpha.18
51765185
resolution: "@vscode/prompt-tsx@npm:0.3.0-alpha.18"
51775186
checksum: fd273d88c4b7dc0f6ffab3b1f0079b2e691a225bd38082d90eca046b9e500679e6f9b43411ca6db72203db150f5ab4cb36861730387455619c1cf309bac94283
@@ -13942,6 +13951,7 @@ __metadata:
1394213951
"@typescript-eslint/parser": 7.16.1
1394313952
"@typescript-eslint/utils": 7.16.1
1394413953
"@vscode-elements/elements": 1.9.2-pre.1
13954+
"@vscode/chat-extension-utils": ^0.0.0-alpha.5
1394513955
"@vscode/codicons": ^0.0.36
1394613956
"@vscode/prompt-tsx": ^0.3.0-alpha.18
1394713957
"@vscode/test-electron": ^2.1.5

0 commit comments

Comments
 (0)