Skip to content

Commit

Permalink
further chat participant work & hide behind flag
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKless committed Feb 7, 2025
1 parent e452613 commit 61785bb
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 1 deletion.
6 changes: 6 additions & 0 deletions apps/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,12 @@
],
"default": "all",
"description": "Controls which notifications are shown for Nx Cloud."
},
"nxConsole.enableNxCopilotFeatures": {
"type": "boolean",
"default": false,
"scope": "machine",
"description": "Enable Nx-specific copilot features (Experimental)."
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions libs/vscode/configuration/src/lib/configuration-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const GLOBAL_CONFIG_KEYS = [
'showNodeVersionOnStartup',
'nxWorkspacePath',
'nxCloudNotifications',
'enableNxCopilotFeatures',
] as const;

export type GlobalConfig = {
Expand All @@ -32,6 +33,7 @@ export type GlobalConfig = {
showNodeVersionOnStartup: boolean;
nxWorkspacePath: string;
nxCloudNotifications: 'all' | 'errors' | 'none';
enableNxCopilotFeatures: boolean;
};

/**
Expand Down
1 change: 1 addition & 0 deletions libs/vscode/copilot/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export default {
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: 'test-output/jest/coverage',
passWithNoTests: true,
};
54 changes: 53 additions & 1 deletion libs/vscode/copilot/src/lib/init-copilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import {
LanguageModelChatMessage,
Uri,
chat,
commands,
Command,
} from 'vscode';
import { getNxWorkspace } from '@nx-console/vscode-nx-workspace';
import { BASE_PROMPT } from './prompt.js';
import { BASE_PROMPT, GENERATE_PROMPT } from './prompt.js';
import type { TargetConfiguration } from 'nx/src/devkit-exports.js';
import { GlobalConfigurationStore } from '@nx-console/vscode-configuration';

const OPEN_COPILOT_SETTING_COMMAND = 'nxConsole.openCopilotSettings';

export function initCopilot(context: ExtensionContext) {
const nxParticipant = chat.createChatParticipant('nx-console.nx', handler);
Expand All @@ -27,12 +33,33 @@ const handler: ChatRequestHandler = async (
stream: ChatResponseStream,
token: CancellationToken
) => {
const enableNxCopilotFeaturesSetting = GlobalConfigurationStore.instance.get(
'enableNxCopilotFeatures',
false
);

if (!enableNxCopilotFeaturesSetting) {
stream.markdown(
'The @nx copilot chat participant is experimental. To use it, please enable it in the settings.'
);

stream.button({
title: 'Enable Nx Copilot',
command: 'workbench.action.openSettings',
arguments: ['nxConsole.enableNxCopilotFeatures'],
});
return;
}
const prompt = BASE_PROMPT;

stream.progress('Retrieving workspace information...');

const projectGraph = await getPrunedProjectGraph();

// if (request.command === 'generate') {
// prompt = GENERATE_PROMPT;
// }

const messages = [LanguageModelChatMessage.User(prompt)];

messages.push(LanguageModelChatMessage.User(request.prompt));
Expand All @@ -59,6 +86,31 @@ async function getPrunedProjectGraph() {
if (node.data.metadata?.technologies) {
prunedNode.technologies = node.data.metadata.technologies;
}
if (node.data.targets) {
prunedNode.targets = Object.entries(node.data.targets)
.map(([key, target]) => {
const prunedTarget = {
executor: target.executor,
} as Partial<TargetConfiguration>;
if (target.command) {
prunedTarget.command = target.command;
}
if (target.options.commands) {
prunedTarget.command = target.options.commands;
}
if (target.configurations) {
prunedTarget.configurations = Object.keys(
target.configurations
);
}
return [key, prunedTarget] as const;
})
.reduce((acc, [key, target]) => {
acc[key] = target;
return acc;
}, {});
}

return [name, prunedNode] as const;
})
.reduce((acc, [name, node]) => {
Expand Down
6 changes: 6 additions & 0 deletions libs/vscode/copilot/src/lib/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ Remember to:
- Use code examples when applicable
- Be concise and clear
`;

export const GENERATE_PROMPT = `
You are an AI assistant specialized in Nx workspaces and monorepo development.
You have access to the project graph and schemas for running nx generators.
Use the user prompt to create a generator invocation and return a cli command to run the generator.
`;
3 changes: 3 additions & 0 deletions libs/vscode/copilot/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"files": [],
"include": [],
"references": [
{
"path": "../configuration"
},
{
"path": "../nx-workspace"
},
Expand Down
3 changes: 3 additions & 0 deletions libs/vscode/copilot/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"include": ["src/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"references": [
{
"path": "../configuration/tsconfig.lib.json"
},
{
"path": "../nx-workspace/tsconfig.lib.json"
}
Expand Down

0 comments on commit 61785bb

Please sign in to comment.