From f553719ba254659803ffe15258b99bbeca83cfe7 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Thu, 4 May 2023 11:50:45 +0800 Subject: [PATCH] support user definded instructions --- instructions/commonInstructions.txt | 1 - instructions/instCode.txt | 22 ++++++++++++ ...Instructions.txt => instCommitMessage.txt} | 0 instructions/instCommon.txt | 1 + instructions/instLangPython.txt | 1 + package-lock.json | 14 ++++++++ package.json | 1 + src/commitMessageCommand.ts | 5 +-- src/extension.ts | 35 +++++++++++++++++++ src/messageHandler.ts | 28 +++++++++++++-- 10 files changed, 100 insertions(+), 8 deletions(-) delete mode 100644 instructions/commonInstructions.txt create mode 100644 instructions/instCode.txt rename instructions/{commitMessageCommandInstructions.txt => instCommitMessage.txt} (100%) create mode 100644 instructions/instCommon.txt create mode 100644 instructions/instLangPython.txt diff --git a/instructions/commonInstructions.txt b/instructions/commonInstructions.txt deleted file mode 100644 index ce7e220..0000000 --- a/instructions/commonInstructions.txt +++ /dev/null @@ -1 +0,0 @@ -You are a software developer assistant \ No newline at end of file diff --git a/instructions/instCode.txt b/instructions/instCode.txt new file mode 100644 index 0000000..b861cf3 --- /dev/null +++ b/instructions/instCode.txt @@ -0,0 +1,22 @@ +As a software developer assistant, your tasks are to: + +- Provide a clear and concise response to address the user's . +- Write code and give advice based on given code or information in the 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. \ No newline at end of file diff --git a/instructions/commitMessageCommandInstructions.txt b/instructions/instCommitMessage.txt similarity index 100% rename from instructions/commitMessageCommandInstructions.txt rename to instructions/instCommitMessage.txt diff --git a/instructions/instCommon.txt b/instructions/instCommon.txt new file mode 100644 index 0000000..1a1b2df --- /dev/null +++ b/instructions/instCommon.txt @@ -0,0 +1 @@ +You are a software developer assistant. \ No newline at end of file diff --git a/instructions/instLangPython.txt b/instructions/instLangPython.txt new file mode 100644 index 0000000..df9d0af --- /dev/null +++ b/instructions/instLangPython.txt @@ -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. \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index d607a9a..a0cbbc6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 661b071..1ba0f12 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/commitMessageCommand.ts b/src/commitMessageCommand.ts index 92942d2..d169131 100644 --- a/src/commitMessageCommand.ts +++ b/src/commitMessageCommand.ts @@ -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`; }, }; diff --git a/src/extension.ts b/src/extension.ts index a7cacb5..0034aac 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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); diff --git a/src/messageHandler.ts b/src/messageHandler.ts index 4854ed7..6b0a371 100644 --- a/src/messageHandler.ts +++ b/src/messageHandler.ts @@ -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; }