From adec4726cecd829f0438774dfcb1f7edad47e6c9 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Fri, 6 Sep 2024 13:56:37 +0800 Subject: [PATCH] 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. --- src/contributes/codecomplete/promptCreator.ts | 267 ++++++++++-------- 1 file changed, 143 insertions(+), 124 deletions(-) diff --git a/src/contributes/codecomplete/promptCreator.ts b/src/contributes/codecomplete/promptCreator.ts index 00f37fc..568a0ee 100644 --- a/src/contributes/codecomplete/promptCreator.ts +++ b/src/contributes/codecomplete/promptCreator.ts @@ -564,138 +564,157 @@ export async function createTaskDescriptionContext() { } export async function createPrompt(filePath: string, fileContent: string, line: number, column: number, posoffset: number, recentEdits: RecentEdit[]) { - const commentPrefix = await getCommentPrefix(filePath); + try { + const commentPrefix = await getCommentPrefix(filePath); - let { prefix, suffix } = await currentFileContext(filePath, fileContent, line, column); - - let tokenCount = countTokens(prefix) + countTokens(suffix); + let { prefix, suffix } = await currentFileContext(filePath, fileContent, line, column); - let taskDescriptionContextWithCommentPrefix = ""; - if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { - const taskDescriptionContext = await createTaskDescriptionContext(); - if (taskDescriptionContext) { - taskDescriptionContext.split("\n").forEach(line => { - taskDescriptionContextWithCommentPrefix += `${commentPrefix}task: ${line}\n`; - }); + let tokenCount = countTokens(prefix) + countTokens(suffix); - taskDescriptionContextWithCommentPrefix += "\n\n\n\n"; + let taskDescriptionContextWithCommentPrefix = ""; + if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { + try { + const taskDescriptionContext = await createTaskDescriptionContext(); + if (taskDescriptionContext) { + taskDescriptionContext.split("\n").forEach(line => { + taskDescriptionContextWithCommentPrefix += `${commentPrefix}task: ${line}\n`; + }); + + taskDescriptionContextWithCommentPrefix += "\n\n\n\n"; + } + + const taskDescriptionContextToken = countTokens(taskDescriptionContextWithCommentPrefix); + if (tokenCount + taskDescriptionContextToken < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { + tokenCount += taskDescriptionContextToken; + } else { + taskDescriptionContextWithCommentPrefix = ""; + } + } catch (error) { + logger.channel()?.error("Error creating task description context:", error); + } } - const taskDescriptionContextToken = countTokens(taskDescriptionContextWithCommentPrefix); - if (tokenCount + taskDescriptionContextToken < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { - tokenCount += taskDescriptionContextToken; + 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); + if (tokenCount + callBlockToken > DevChatConfig.getInstance().get("complete_context_limit", 3000)) { + break; + } + + tokenCount += callBlockToken; + callDefContext += `${commentPrefix}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); + + for (const similarContext of similarContexts) { + const blockToken = countTokens(similarContext.text); + if (tokenCount + blockToken > DevChatConfig.getInstance().get("complete_context_limit", 3000)) { + continue; + } + similarTokens += blockToken; + if (similarTokens > CONTEXT_SIMILAR_LIMITED_SIZE) { + continue; + } + + tokenCount += blockToken; + similarBlockContext += `${commentPrefix}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) { + const countSymboleToken = countTokens(symbolDefine.codeblock); + if (tokenCount + countSymboleToken > DevChatConfig.getInstance().get("complete_context_limit", 3000)) { + break; + } + + tokenCount += countSymboleToken; + symbolContext += `${commentPrefix}symbol defines:\n\n ${symbolDefine.filepath}\n\n`; + 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); + if (tokenCount + countRecentToken < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { + tokenCount += countRecentToken; + } 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); + if (tokenCount + countFileToken < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { + tokenCount += countFileToken; + neighborFileContext += `${commentPrefix}neighbor files:\n\n ${neighborFiles[0].file}\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); + + let prompt = ""; + let completeModel: string = DevChatConfig.getInstance().get("complete_model"); + if (!completeModel) { + completeModel = "nvidia/starcoder2:15b"; + } + if (completeModel.indexOf("deepseek") > -1) { + prompt = "<|fim▁begin|>" + taskDescriptionContextWithCommentPrefix + neighborFileContext + recentEditContext + symbolContext + callDefContext + similarBlockContext + gitDiffContext + `${commentPrefix}${filePath}\n\n` + prefix + "<|fim▁hole|>" + suffix + "<|fim▁end|>"; + } else if (completeModel.indexOf("starcoder") > -1) { + prompt = "" + taskDescriptionContextWithCommentPrefix + neighborFileContext + recentEditContext + symbolContext + callDefContext + similarBlockContext + gitDiffContext + `${commentPrefix}${filePath}\n\n` + prefix + "" + suffix + ""; + } else if (completeModel.indexOf("codestral") > -1) { + prompt = "[SUFFIX]" + suffix + "[PREFIX]" + taskDescriptionContextWithCommentPrefix + neighborFileContext + recentEditContext + symbolContext + callDefContext + similarBlockContext + gitDiffContext + `${commentPrefix}${filePath}\n\n` + prefix; } else { - taskDescriptionContextWithCommentPrefix = ""; + prompt = "" + taskDescriptionContextWithCommentPrefix + neighborFileContext + recentEditContext + symbolContext + callDefContext + similarBlockContext + gitDiffContext + `${commentPrefix}${filePath}\n\n` + prefix + "" + suffix + ""; } + + return prompt; + } catch (error) { + logger.channel()?.error("Error in createPrompt:", error); + return undefined; } - - // 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 = "" + gitDiffContext + "\n\n\n\n"; - // } else { - // gitDiffContext = ""; - // } - // } - let gitDiffContext = ""; - - let callDefContext = ""; - if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { - const callCodeBlocks = await createContextCallDefine(filePath, fileContent, posoffset); - for (const callCodeBlock of callCodeBlocks) { - const callBlockToken = countTokens(callCodeBlock.codeblock); - if (tokenCount + callBlockToken > DevChatConfig.getInstance().get("complete_context_limit", 3000)) { - break; - } - - tokenCount += callBlockToken; - callDefContext += `${commentPrefix}call function define:\n\n ${callCodeBlock.filepath}\n\n`; - callDefContext += `${callCodeBlock.codeblock}\n\n\n\n`; - } - } - - let similarBlockContext = ""; - if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { - let similarTokens = 0; - const similarContexts: {file: string, text: string}[] = await findSimilarCodeBlock(filePath, fileContent, line, column); - - for (const similarContext of similarContexts) { - const blockToken = countTokens(similarContext.text); - if (tokenCount + blockToken > DevChatConfig.getInstance().get("complete_context_limit", 3000)) { - continue; - } - similarTokens += blockToken; - if (similarTokens > CONTEXT_SIMILAR_LIMITED_SIZE) { - continue; - } - - tokenCount += blockToken; - similarBlockContext += `${commentPrefix}similar blocks:\n\n ${similarContext.file}\n\n`; - similarBlockContext += `${similarContext.text}\n\n\n\n`; - } - } - - let symbolContext = ""; - if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { - const symbolDefines: { filepath: string, node: Parser.SyntaxNode, codeblock: string }[] = await symbolDefinesContext(filePath, fileContent, line, column); - for (const symbolDefine of symbolDefines ) { - const countSymboleToken = countTokens(symbolDefine.codeblock); - if (tokenCount + countSymboleToken > DevChatConfig.getInstance().get("complete_context_limit", 3000)) { - break; - } - - tokenCount += countSymboleToken; - symbolContext += `${commentPrefix}symbol defines:\n\n ${symbolDefine.filepath}\n\n`; - symbolContext += `${commentPrefix}this is type of variable: ${symbolDefine.node.text}\n\n`; - symbolContext += `${symbolDefine.codeblock}\n\n\n\n`; - } - } - - let recentEditContext = ""; - if (tokenCount < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { - recentEditContext = await createRecentEditContext(recentEdits, filePath); - - const countRecentToken = countTokens(recentEditContext); - if (tokenCount + countRecentToken < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { - tokenCount += countRecentToken; - } else { - recentEditContext = ""; - } - } - - let neighborFileContext = ""; - if (tokenCount < 200) { - const neighborFiles = await findNeighborFileContext(filePath, fileContent, line, column); - if (neighborFiles.length > 0) { - const countFileToken = countTokens(neighborFiles[0].text); - if (tokenCount + countFileToken < DevChatConfig.getInstance().get("complete_context_limit", 3000)) { - tokenCount += countFileToken; - neighborFileContext += `${commentPrefix}neighbor files:\n\n ${neighborFiles[0].file}\n\n`; - neighborFileContext += `${neighborFiles[0].text}\n\n\n\n`; - } - } - } - - logger.channel()?.debug("Complete token:", tokenCount); - - let prompt = ""; - let completeModel: string = DevChatConfig.getInstance().get("complete_model"); - if (!completeModel) { - completeModel = "nvidia/starcoder2:15b"; - } - if (completeModel.indexOf("deepseek") > -1) { - prompt = "<|fim▁begin|>" + taskDescriptionContextWithCommentPrefix + neighborFileContext + recentEditContext + symbolContext + callDefContext + similarBlockContext + gitDiffContext + `${commentPrefix}${filePath}\n\n` + prefix + "<|fim▁hole|>" + suffix + "<|fim▁end|>"; - } else if (completeModel.indexOf("starcoder") > -1) { - prompt = "" + taskDescriptionContextWithCommentPrefix + neighborFileContext + recentEditContext + symbolContext + callDefContext + similarBlockContext + gitDiffContext + `${commentPrefix}${filePath}\n\n` + prefix + "" + suffix + ""; - } else if (completeModel.indexOf("codestral") > -1) { - prompt = "[SUFFIX]" + suffix + "[PREFIX]" + taskDescriptionContextWithCommentPrefix + neighborFileContext + recentEditContext + symbolContext + callDefContext + similarBlockContext + gitDiffContext + `${commentPrefix}${filePath}\n\n` + prefix; - } else { - prompt = "" + taskDescriptionContextWithCommentPrefix + neighborFileContext + recentEditContext + symbolContext + callDefContext + similarBlockContext + gitDiffContext + `${commentPrefix}${filePath}\n\n` + prefix + "" + suffix + ""; - } - - return prompt; } function findImportTypeDefine(filePath: string, fileContent: string, node: Parser.SyntaxNode) {