feat: Add logging in code completion

- Added try-catch blocks to handle errors in creating task description context.
- Added logging for errors in creating call definition context, similar block context, symbol context, recent edit context, and neighbor file context.
- Wrapped the main function logic in a try-catch block to log errors in createPrompt.
This commit is contained in:
bobo.yang 2024-09-06 13:56:37 +08:00
parent 46e17b99c5
commit adec4726ce

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[]) {
try {
const commentPrefix = await getCommentPrefix(filePath);
let { prefix, suffix } = await currentFileContext(filePath, fileContent, line, column);
@ -572,6 +573,7 @@ export async function createPrompt(filePath: string, fileContent: string, line:
let taskDescriptionContextWithCommentPrefix = "";
if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) {
try {
const taskDescriptionContext = await createTaskDescriptionContext();
if (taskDescriptionContext) {
taskDescriptionContext.split("\n").forEach(line => {
@ -587,22 +589,16 @@ export async function createPrompt(filePath: string, fileContent: string, line:
} else {
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 callDefContext = "";
if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) {
try {
const callCodeBlocks = await createContextCallDefine(filePath, fileContent, posoffset);
for (const callCodeBlock of callCodeBlocks) {
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 += `${callCodeBlock.codeblock}\n\n\n\n`;
}
} catch (error) {
logger.channel()?.error("Error creating call definition context:", error);
}
}
let similarBlockContext = "";
if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) {
try {
let similarTokens = 0;
const similarContexts: {file: string, text: string}[] = await findSimilarCodeBlock(filePath, fileContent, line, column);
@ -635,12 +635,16 @@ export async function createPrompt(filePath: string, fileContent: string, line:
similarBlockContext += `${commentPrefix}<filename>similar blocks:\n\n ${similarContext.file}\n\n`;
similarBlockContext += `${similarContext.text}\n\n\n\n`;
}
} catch (error) {
logger.channel()?.error("Error creating similar block context:", error);
}
}
let symbolContext = "";
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);
for (const symbolDefine of symbolDefines ) {
for (const symbolDefine of symbolDefines) {
const countSymboleToken = countTokens(symbolDefine.codeblock);
if (tokenCount + countSymboleToken > DevChatConfig.getInstance().get("complete_context_limit", 3000)) {
break;
@ -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 += `${symbolDefine.codeblock}\n\n\n\n`;
}
} catch (error) {
logger.channel()?.error("Error creating symbol context:", error);
}
}
let recentEditContext = "";
if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) {
try {
recentEditContext = await createRecentEditContext(recentEdits, filePath);
const countRecentToken = countTokens(recentEditContext);
@ -663,10 +671,14 @@ export async function createPrompt(filePath: string, fileContent: string, line:
} else {
recentEditContext = "";
}
} catch (error) {
logger.channel()?.error("Error creating recent edit context:", error);
}
}
let neighborFileContext = "";
if (tokenCount < 200) {
try {
const neighborFiles = await findNeighborFileContext(filePath, fileContent, line, column);
if (neighborFiles.length > 0) {
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`;
}
}
} catch (error) {
logger.channel()?.error("Error creating neighbor file context:", error);
}
}
logger.channel()?.debug("Complete token:", tokenCount);
@ -696,6 +711,10 @@ export async function createPrompt(filePath: string, fileContent: string, line:
}
return prompt;
} catch (error) {
logger.channel()?.error("Error in createPrompt:", error);
return undefined;
}
}
function findImportTypeDefine(filePath: string, fileContent: string, node: Parser.SyntaxNode) {