support user definded instructions

This commit is contained in:
bobo.yang 2023-05-04 11:50:45 +08:00
parent bffc69ed55
commit f553719ba2
10 changed files with 100 additions and 8 deletions

View File

@ -1 +0,0 @@
You are a software developer assistant

22
instructions/instCode.txt Normal file
View File

@ -0,0 +1,22 @@
As a software developer assistant, your tasks are to:
- Provide a clear and concise response to address the user's <request>.
- Write code and give advice based on given code or information in the <context> if provided.
- Follow language-specific best practices and coding standards.
When responding:
1. Summarize and describe the requirements or provided information in your own words.
2. The summary and description should better be written in bullet points (excluding code).
3. If modifying given code, output the changes and avoid unnecessary unchanged code.
4. Enclose code or changes within blocks using triple backticks (```), and include the programming language and the file path, if available. For example:
```
```python path=./path/to/file.py
print("Hello, World!")
```
```
If no file paths or folder structure are provided and you are unsure about the file path of the code, you may omit the file path.
5. Use separate code blocks for different files.
6. Utilize the previous messages, if provided in the end of this prompt, to create your response. Note that not all previous messages are necessarily relevant.
7. When providing a suggestion or instruction, begin by explaining the reason behind it.
8. If you need more information, ask for it.

View File

@ -0,0 +1 @@
You are a software developer assistant.

View File

@ -0,0 +1 @@
When writing Python code, include type hints where appropriate and maintain compliance with PEP-8 guidelines, such as providing docstrings for modules, classes, and functions.

14
package-lock.json generated
View File

@ -10,6 +10,7 @@
"dependencies": {
"axios": "^1.3.6",
"dotenv": "^16.0.3",
"ncp": "^2.0.0",
"node-fetch": "^3.3.1",
"nonce": "^1.0.4",
"openai": "^3.2.1",
@ -4898,6 +4899,14 @@
"integrity": "sha512-Tj+HTDSJJKaZnfiuw+iaF9skdPpTo2GtEly5JHnWV/hfv2Qj/9RKsGISQtLh2ox3l5EAGw487hnBee0sIJ6v2g==",
"dev": true
},
"node_modules/ncp": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ncp/-/ncp-2.0.0.tgz",
"integrity": "sha512-zIdGUrPRFTUELUvr3Gmc7KZ2Sw/h1PiVM0Af/oHB6zgnV1ikqSfRk+TOufi79aHYCW3NiOXmr1BP5nWbzojLaA==",
"bin": {
"ncp": "bin/ncp"
}
},
"node_modules/neo-async": {
"version": "2.6.2",
"resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.2.tgz",
@ -10133,6 +10142,11 @@
"integrity": "sha512-Tj+HTDSJJKaZnfiuw+iaF9skdPpTo2GtEly5JHnWV/hfv2Qj/9RKsGISQtLh2ox3l5EAGw487hnBee0sIJ6v2g==",
"dev": true
},
"ncp": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ncp/-/ncp-2.0.0.tgz",
"integrity": "sha512-zIdGUrPRFTUELUvr3Gmc7KZ2Sw/h1PiVM0Af/oHB6zgnV1ikqSfRk+TOufi79aHYCW3NiOXmr1BP5nWbzojLaA=="
},
"neo-async": {
"version": "2.6.2",
"resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.2.tgz",

View File

@ -96,6 +96,7 @@
"dependencies": {
"axios": "^1.3.6",
"dotenv": "^16.0.3",
"ncp": "^2.0.0",
"node-fetch": "^3.3.1",
"nonce": "^1.0.4",
"openai": "^3.2.1",

View File

@ -54,9 +54,6 @@ export const commitMessageCommand: Command = {
const diff_file = path.join(tempDir, 'diff_output.txt');
await writeDiffFile(diff_file);
const commonInstructions = vscode.Uri.joinPath(ExtensionContextHolder.context?.extensionUri as vscode.Uri, 'instructions/commonInstructions.txt');
const commitMsgInstructions = vscode.Uri.joinPath(ExtensionContextHolder.context?.extensionUri as vscode.Uri, 'instructions/commitMessageCommandInstructions.txt');
return `[instruction|${commonInstructions.path}] [instruction|${commitMsgInstructions.path}] [context|${diff_file}] Write a commit message`;
return `[context|${diff_file}] Write a commit message`;
},
};

View File

@ -1,11 +1,46 @@
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import * as ncp from 'ncp'; // 需要安装 ncp 模块,用于复制目录
const ChatPanel = require('./chatPanel').default;
const sendFileSelectMessage = require('./messageHandler').sendFileSelectMessage;
const sendCodeSelectMessage = require('./messageHandler').sendCodeSelectMessage;
import ExtensionContextHolder from './extensionContext';
function createChatDirectoryAndCopyInstructionsSync(extensionUri: vscode.Uri) {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders) {
return;
}
const workspaceRoot = workspaceFolders[0].uri.fsPath;
const chatDirPath = path.join(workspaceRoot, '.chat');
const instructionsSrcPath = path.join(extensionUri.fsPath, 'instructions');
try {
// 检查 .chat 目录是否存在,如果不存在,则创建它
if (!fs.existsSync(chatDirPath)) {
fs.mkdirSync(chatDirPath);
}
// 将 instructions 目录复制到 .chat 目录中
ncp.ncp(instructionsSrcPath, path.join(chatDirPath, 'instructions'), (err) => {
if (err) {
console.error('Error copying instructions:', err);
}
});
} catch (error) {
console.error('Error creating .chat directory and copying instructions:', error);
}
}
function activate(context: vscode.ExtensionContext) {
ExtensionContextHolder.context = context;
// 创建 .chat 目录并复制 instructions
createChatDirectoryAndCopyInstructionsSync(context.extensionUri);
let disposable = vscode.commands.registerCommand('devchat.openChatPanel', () => {
if (vscode.workspace.workspaceFolders) {
ChatPanel.createOrShow(context.extensionUri);

View File

@ -77,6 +77,28 @@ function parseMessage(message: string): { context: string[]; instruction: string
return { context: contextPaths, instruction: instructionPaths, reference: referencePaths, text };
}
function getInstructionFiles(): string[] {
const instructionFiles: string[] = [];
const workspaceDir = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
if (workspaceDir) {
const chatInstructionsPath = path.join(workspaceDir, '.chat', 'instructions');
try {
// 读取 chatInstructionsPath 目录下的所有文件和目录
const files = fs.readdirSync(chatInstructionsPath);
// 过滤出文件,忽略目录
for (const file of files) {
const filePath = path.join(chatInstructionsPath, file);
const fileStats = fs.statSync(filePath);
if (fileStats.isFile()) {
instructionFiles.push(filePath);
}
}
} catch (error) {
console.error('Error reading instruction files:', error);
}
}
return instructionFiles;
}
async function handleMessage(
message: any,
@ -96,9 +118,9 @@ async function handleMessage(
if (parsedMessage.context.length > 0) {
chatOptions.context = parsedMessage.context;
}
if (parsedMessage.instruction.length > 0) {
chatOptions.header = parsedMessage.instruction;
}
chatOptions.header = getInstructionFiles();
if (parsedMessage.reference.length > 0) {
chatOptions.reference = parsedMessage.reference;
}