save context as json
This commit is contained in:
parent
736ede2114
commit
22e334e1f6
@ -15,10 +15,15 @@ export async function handleCodeSelected(fileSelected: string, codeSelected: str
|
||||
const languageId = await getLanguageIdByFileName(fileSelected);
|
||||
|
||||
// convert fileContent to markdown code block with languageId and file path
|
||||
const markdownCodeBlock = `\`\`\`${languageId} path=${fileSelected}\n${codeSelected}\n\`\`\``;
|
||||
const data = {
|
||||
languageId: languageId,
|
||||
path: fileSelected,
|
||||
content: codeSelected
|
||||
};
|
||||
const jsonData = JSON.stringify(data);
|
||||
|
||||
// save markdownCodeBlock to temp file
|
||||
await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFile), Buffer.from(markdownCodeBlock));
|
||||
await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFile), Buffer.from(jsonData));
|
||||
|
||||
return `[context|${tempFile}]`;
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
import * as path from 'path';
|
||||
import * as vscode from 'vscode';
|
||||
import { ChatContext } from './contextManager';
|
||||
import { createTempSubdirectory, runCommandAndWriteOutput, runCommandStringAndWriteOutput } from '../util/commonUtil';
|
||||
import { createTempSubdirectory, runCommandStringAndWriteOutput } from '../util/commonUtil';
|
||||
|
||||
export const customCommandContext: ChatContext = {
|
||||
name: '<custom command>',
|
||||
|
@ -18,10 +18,15 @@ export async function handleFileSelected(fileSelected: string) {
|
||||
const languageId = await getLanguageIdByFileName(fileSelected);
|
||||
|
||||
// convert fileContent to markdown code block with languageId and file path
|
||||
const markdownCodeBlock = `\`\`\`${languageId} path=${fileSelected}\n${fileContent}\n\`\`\``;
|
||||
const data = {
|
||||
languageId: languageId,
|
||||
path: fileSelected,
|
||||
content: fileContent
|
||||
};
|
||||
const jsonData = JSON.stringify(data);
|
||||
|
||||
// save markdownCodeBlock to temp file
|
||||
await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFile), Buffer.from(markdownCodeBlock));
|
||||
await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFile), Buffer.from(jsonData));
|
||||
|
||||
return `[context|${tempFile}]`;
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
import * as path from 'path';
|
||||
import { ChatContext } from './contextManager';
|
||||
import { createTempSubdirectory, runCommandAndWriteOutput } from '../util/commonUtil';
|
||||
import { createTempSubdirectory, runCommandStringAndWriteOutput } from '../util/commonUtil';
|
||||
|
||||
export const gitDiffContext: ChatContext = {
|
||||
name: 'git diff',
|
||||
@ -8,7 +8,7 @@ export const gitDiffContext: ChatContext = {
|
||||
handler: async () => {
|
||||
const tempDir = await createTempSubdirectory('devchat/context');
|
||||
const diff_file = path.join(tempDir, 'diff_all.txt');
|
||||
const result = await runCommandAndWriteOutput('git', ['diff'], diff_file);
|
||||
const result = await runCommandStringAndWriteOutput('git diff', diff_file);
|
||||
console.log(result.exitCode);
|
||||
console.log(result.stdout);
|
||||
console.log(result.stderr);
|
||||
|
@ -1,6 +1,6 @@
|
||||
import * as path from 'path';
|
||||
import { ChatContext } from './contextManager';
|
||||
import { createTempSubdirectory, runCommandAndWriteOutput } from '../util/commonUtil';
|
||||
import { createTempSubdirectory, runCommandStringAndWriteOutput } from '../util/commonUtil';
|
||||
|
||||
export const gitDiffCachedContext: ChatContext = {
|
||||
name: 'git diff cached',
|
||||
@ -8,7 +8,7 @@ export const gitDiffCachedContext: ChatContext = {
|
||||
handler: async () => {
|
||||
const tempDir = await createTempSubdirectory('devchat/context');
|
||||
const diff_file = path.join(tempDir, 'diff_cached.txt');
|
||||
const result = await runCommandAndWriteOutput('git', ['diff', '--cached'], diff_file);
|
||||
const result = await runCommandStringAndWriteOutput('git diff --cached', diff_file);
|
||||
console.log(result.exitCode);
|
||||
console.log(result.stdout);
|
||||
console.log(result.stderr);
|
||||
|
18
src/context/contextRef.ts
Normal file
18
src/context/contextRef.ts
Normal file
@ -0,0 +1,18 @@
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import * as path from 'path';
|
||||
import { createTempSubdirectory, runCommandStringAndWriteOutput } from '../util/commonUtil';
|
||||
|
||||
export async function handleRefCommand(ref_command: string) {
|
||||
if (ref_command) {
|
||||
const tempDir = await createTempSubdirectory('devchat/context');
|
||||
const diff_file = path.join(tempDir, 'custom.txt');
|
||||
const result = await runCommandStringAndWriteOutput(ref_command, diff_file);
|
||||
console.log(result.exitCode);
|
||||
console.log(result.stdout);
|
||||
console.log(result.stderr);
|
||||
return `[context|${diff_file}]`;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
import { ChatContext } from './contextManager';
|
||||
|
||||
export const exampleContext: ChatContext = {
|
||||
name: 'exampleContext',
|
||||
description: '这是一个示例上下文',
|
||||
handler: async () => {
|
||||
return `[context|example file name]`;
|
||||
},
|
||||
};
|
@ -1,5 +1,4 @@
|
||||
import ChatContextManager from './contextManager';
|
||||
import { exampleContext } from './exampleContext';
|
||||
import { gitDiffCachedContext } from './contextGitDiffCached';
|
||||
import { gitDiffContext } from './contextGitDiff';
|
||||
import { customCommandContext } from './contextCustomCommand';
|
||||
@ -7,7 +6,6 @@ import { customCommandContext } from './contextCustomCommand';
|
||||
const chatContextManager = ChatContextManager.getInstance();
|
||||
|
||||
// 注册命令
|
||||
chatContextManager.registerContext(exampleContext);
|
||||
chatContextManager.registerContext(gitDiffCachedContext);
|
||||
chatContextManager.registerContext(gitDiffContext);
|
||||
chatContextManager.registerContext(customCommandContext);
|
||||
|
10
src/handler/addRefCommandContext.ts
Normal file
10
src/handler/addRefCommandContext.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { handleRefCommand } from '../context/contextRef';
|
||||
|
||||
// message: { command: 'addRefCommandContext', refCommand: string }
|
||||
// User input: /ref ls . then "ls ." will be passed to refCommand
|
||||
export async function addRefCommandContext(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
const contextStr = await handleRefCommand(message.refCommand);
|
||||
panel.webview.postMessage({ command: 'appendContext', context: contextStr });
|
||||
return;
|
||||
}
|
12
src/handler/contextDetail.ts
Normal file
12
src/handler/contextDetail.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import * as vscode from 'vscode';
|
||||
import * as fs from 'fs';
|
||||
import { handleRefCommand } from '../context/contextRef';
|
||||
|
||||
// message: { command: 'contextDetail', file: string }
|
||||
// read detail context information from file
|
||||
// return json string
|
||||
export async function contextDetail(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
const fileContent = fs.readFileSync(message.file, 'utf-8');
|
||||
panel.webview.postMessage({ command: 'contextDetailResponse', result: fileContent });
|
||||
return;
|
||||
}
|
@ -10,6 +10,8 @@ import { sendMessage } from './sendMessage';
|
||||
import { blockApply } from './showDiff';
|
||||
import { showDiff } from './showDiff';
|
||||
import { addConext } from './addContext';
|
||||
import { addRefCommandContext } from './addRefCommandContext';
|
||||
import { contextDetail } from './contextDetail';
|
||||
|
||||
messageHandler.registerHandler('addContext', addConext);
|
||||
messageHandler.registerHandler('code_apply', codeApply);
|
||||
@ -21,4 +23,6 @@ messageHandler.registerHandler('regCommandList', regCommandList);
|
||||
messageHandler.registerHandler('regContextList', regContextList);
|
||||
messageHandler.registerHandler('sendMessage', sendMessage);
|
||||
messageHandler.registerHandler('block_apply', blockApply);
|
||||
messageHandler.registerHandler('show_diff', showDiff);
|
||||
messageHandler.registerHandler('show_diff', showDiff);
|
||||
messageHandler.registerHandler('addRefCommandContext', addRefCommandContext);
|
||||
messageHandler.registerHandler('contextDetail', contextDetail);
|
@ -76,7 +76,13 @@ interface CommandResult {
|
||||
// 使用exec执行命令行字符串
|
||||
const childProcess = exec(commandString, { cwd: workspaceDir }, (error, stdout, stderr) => {
|
||||
// 将命令输出结果写入到文件
|
||||
fs.writeFileSync(outputFile, stdout);
|
||||
const data = {
|
||||
command: commandString,
|
||||
content: stdout
|
||||
};
|
||||
const jsonData = JSON.stringify(data);
|
||||
|
||||
fs.writeFileSync(outputFile, jsonData);
|
||||
|
||||
// 返回结果
|
||||
resolve({
|
||||
|
Loading…
x
Reference in New Issue
Block a user