Merge pull request #591 from devchat-ai/add_log_in_codecompletion

feat: Add logging in code completion
This commit is contained in:
boob.yang 2024-09-06 14:15:41 +08:00 committed by GitHub
commit b15956a660
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -564,6 +564,7 @@ export async function createTaskDescriptionContext() {
} }
export async function createPrompt(filePath: string, fileContent: string, line: number, column: number, posoffset: number, recentEdits: RecentEdit[]) { export async function createPrompt(filePath: string, fileContent: string, line: number, column: number, posoffset: number, recentEdits: RecentEdit[]) {
try {
const commentPrefix = await getCommentPrefix(filePath); const commentPrefix = await getCommentPrefix(filePath);
let { prefix, suffix } = await currentFileContext(filePath, fileContent, line, column); let { prefix, suffix } = await currentFileContext(filePath, fileContent, line, column);
@ -572,6 +573,7 @@ export async function createPrompt(filePath: string, fileContent: string, line:
let taskDescriptionContextWithCommentPrefix = ""; let taskDescriptionContextWithCommentPrefix = "";
if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) {
try {
const taskDescriptionContext = await createTaskDescriptionContext(); const taskDescriptionContext = await createTaskDescriptionContext();
if (taskDescriptionContext) { if (taskDescriptionContext) {
taskDescriptionContext.split("\n").forEach(line => { taskDescriptionContext.split("\n").forEach(line => {
@ -587,22 +589,16 @@ export async function createPrompt(filePath: string, fileContent: string, line:
} else { } else {
taskDescriptionContextWithCommentPrefix = ""; taskDescriptionContextWithCommentPrefix = "";
} }
} catch (error) {
logger.channel()?.error("Error creating task description context:", error);
}
} }
// let gitDiffContext = GitDiffWatcher.getInstance().getGitDiffResult();
// if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000) && gitDiffContext.length > 0) {
// const gitDiffContextToken = countTokens(gitDiffContext);
// if (tokenCount + gitDiffContextToken < DevChatConfig.getInstance().get("complete_context_limit", 3000)) {
// tokenCount += gitDiffContextToken;
// gitDiffContext = "<git_diff_start>" + gitDiffContext + "<git_diff_end>\n\n\n\n";
// } else {
// gitDiffContext = "";
// }
// }
let gitDiffContext = ""; let gitDiffContext = "";
let callDefContext = ""; let callDefContext = "";
if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) {
try {
const callCodeBlocks = await createContextCallDefine(filePath, fileContent, posoffset); const callCodeBlocks = await createContextCallDefine(filePath, fileContent, posoffset);
for (const callCodeBlock of callCodeBlocks) { for (const callCodeBlock of callCodeBlocks) {
const callBlockToken = countTokens(callCodeBlock.codeblock); const callBlockToken = countTokens(callCodeBlock.codeblock);
@ -614,10 +610,14 @@ export async function createPrompt(filePath: string, fileContent: string, line:
callDefContext += `${commentPrefix}<filename>call function define:\n\n ${callCodeBlock.filepath}\n\n`; callDefContext += `${commentPrefix}<filename>call function define:\n\n ${callCodeBlock.filepath}\n\n`;
callDefContext += `${callCodeBlock.codeblock}\n\n\n\n`; callDefContext += `${callCodeBlock.codeblock}\n\n\n\n`;
} }
} catch (error) {
logger.channel()?.error("Error creating call definition context:", error);
}
} }
let similarBlockContext = ""; let similarBlockContext = "";
if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) {
try {
let similarTokens = 0; let similarTokens = 0;
const similarContexts: {file: string, text: string}[] = await findSimilarCodeBlock(filePath, fileContent, line, column); const similarContexts: {file: string, text: string}[] = await findSimilarCodeBlock(filePath, fileContent, line, column);
@ -635,10 +635,14 @@ export async function createPrompt(filePath: string, fileContent: string, line:
similarBlockContext += `${commentPrefix}<filename>similar blocks:\n\n ${similarContext.file}\n\n`; similarBlockContext += `${commentPrefix}<filename>similar blocks:\n\n ${similarContext.file}\n\n`;
similarBlockContext += `${similarContext.text}\n\n\n\n`; similarBlockContext += `${similarContext.text}\n\n\n\n`;
} }
} catch (error) {
logger.channel()?.error("Error creating similar block context:", error);
}
} }
let symbolContext = ""; let symbolContext = "";
if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) {
try {
const symbolDefines: { filepath: string, node: Parser.SyntaxNode, codeblock: string }[] = await symbolDefinesContext(filePath, fileContent, line, column); const symbolDefines: { filepath: string, node: Parser.SyntaxNode, codeblock: string }[] = await symbolDefinesContext(filePath, fileContent, line, column);
for (const symbolDefine of symbolDefines) { for (const symbolDefine of symbolDefines) {
const countSymboleToken = countTokens(symbolDefine.codeblock); const countSymboleToken = countTokens(symbolDefine.codeblock);
@ -651,10 +655,14 @@ export async function createPrompt(filePath: string, fileContent: string, line:
symbolContext += `${commentPrefix}this is type of variable: ${symbolDefine.node.text}\n\n`; symbolContext += `${commentPrefix}this is type of variable: ${symbolDefine.node.text}\n\n`;
symbolContext += `${symbolDefine.codeblock}\n\n\n\n`; symbolContext += `${symbolDefine.codeblock}\n\n\n\n`;
} }
} catch (error) {
logger.channel()?.error("Error creating symbol context:", error);
}
} }
let recentEditContext = ""; let recentEditContext = "";
if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) {
try {
recentEditContext = await createRecentEditContext(recentEdits, filePath); recentEditContext = await createRecentEditContext(recentEdits, filePath);
const countRecentToken = countTokens(recentEditContext); const countRecentToken = countTokens(recentEditContext);
@ -663,10 +671,14 @@ export async function createPrompt(filePath: string, fileContent: string, line:
} else { } else {
recentEditContext = ""; recentEditContext = "";
} }
} catch (error) {
logger.channel()?.error("Error creating recent edit context:", error);
}
} }
let neighborFileContext = ""; let neighborFileContext = "";
if (tokenCount < 200) { if (tokenCount < 200) {
try {
const neighborFiles = await findNeighborFileContext(filePath, fileContent, line, column); const neighborFiles = await findNeighborFileContext(filePath, fileContent, line, column);
if (neighborFiles.length > 0) { if (neighborFiles.length > 0) {
const countFileToken = countTokens(neighborFiles[0].text); const countFileToken = countTokens(neighborFiles[0].text);
@ -676,6 +688,9 @@ export async function createPrompt(filePath: string, fileContent: string, line:
neighborFileContext += `${neighborFiles[0].text}\n\n\n\n`; neighborFileContext += `${neighborFiles[0].text}\n\n\n\n`;
} }
} }
} catch (error) {
logger.channel()?.error("Error creating neighbor file context:", error);
}
} }
logger.channel()?.debug("Complete token:", tokenCount); logger.channel()?.debug("Complete token:", tokenCount);
@ -696,6 +711,10 @@ export async function createPrompt(filePath: string, fileContent: string, line:
} }
return prompt; return prompt;
} catch (error) {
logger.channel()?.error("Error in createPrompt:", error);
return undefined;
}
} }
function findImportTypeDefine(filePath: string, fileContent: string, node: Parser.SyntaxNode) { function findImportTypeDefine(filePath: string, fileContent: string, node: Parser.SyntaxNode) {