|
| 1 | + |
| 2 | +script({ |
| 3 | + title: "Invoke LLM completion for code snippets", |
| 4 | +}) |
| 5 | + |
| 6 | + |
| 7 | +import * as fs from 'fs'; |
| 8 | +import * as path from 'path'; |
| 9 | + |
| 10 | + |
| 11 | +async function runCodePrompt(role, message, code) { |
| 12 | + const answer = await runPrompt( |
| 13 | + (_) => { |
| 14 | + _.def("ROLE", role); |
| 15 | + _.def("REQUEST", message); |
| 16 | + _.def("CODE", code); |
| 17 | + _.$`Your role is <ROLE>. |
| 18 | + The request is given by <REQUEST> |
| 19 | + original code snippet: |
| 20 | + <CODE>.` |
| 21 | + } |
| 22 | + ) |
| 23 | + console.log(answer.text); |
| 24 | + return answer.text; |
| 25 | +} |
| 26 | + |
| 27 | +async function invokeLLMCompletion(code, prefix) { |
| 28 | + |
| 29 | + let role = `You are a highly experienced compiler engineer with over 20 years of expertise, |
| 30 | + specializing in C and C++ programming. Your deep knowledge of best coding practices |
| 31 | + and software engineering principles enables you to produce robust, efficient, and |
| 32 | + maintainable code in any scenario.`; |
| 33 | + |
| 34 | + let userMessage = `Please complete the provided C/C++ code to ensure it is compilable and executable. |
| 35 | + Return only the fully modified code while preserving the original logic. |
| 36 | + Add any necessary stubs, infer data types, and make essential changes to enable |
| 37 | + successful compilation and execution. Avoid unnecessary code additions. |
| 38 | + Ensure the final code is robust, secure, and adheres to best practices.`; |
| 39 | + |
| 40 | + return runCodePrompt(role, userMessage, code); |
| 41 | +} |
| 42 | + |
| 43 | +async function invokeLLMAnalyzer(code, inputFilename, funcName) { |
| 44 | + // Define the llm role |
| 45 | + let role = |
| 46 | + `You are a highly experienced compiler engineer with over 20 years of expertise, |
| 47 | + specializing in C and C++ programming. Your deep knowledge of best coding practices |
| 48 | + and software engineering principles enables you to produce robust, efficient, and |
| 49 | + maintainable code in any scenario.`; |
| 50 | + |
| 51 | + // Define the message to send |
| 52 | + let userMessage = |
| 53 | + `Please analyze the provided C/C++ code and identify any potential issues, bugs, or opportunities for performance improvement. For each observation: |
| 54 | + |
| 55 | + - Clearly describe the issue or inefficiency. |
| 56 | + - Explain the reasoning behind the problem or performance bottleneck. |
| 57 | + - Suggest specific code changes or optimizations, including code examples where applicable. |
| 58 | + - Ensure recommendations follow best practices for efficiency, maintainability, and correctness. |
| 59 | + |
| 60 | + At the end of the analysis, provide a detailed report in **Markdown format** summarizing: |
| 61 | + |
| 62 | + 1. **Identified Issues and Their Impact:** |
| 63 | + - Description of each issue and its potential consequences. |
| 64 | + |
| 65 | + 2. **Suggested Fixes (with Code Examples):** |
| 66 | + - Detailed code snippets showing the recommended improvements. |
| 67 | + |
| 68 | + 3. **Performance Improvement Recommendations:** |
| 69 | + - Explanation of optimizations and their expected benefits. |
| 70 | +
|
| 71 | + 4. **Additional Insights or Best Practices:** |
| 72 | + - Suggestions to further enhance the code's quality and maintainability.`; |
| 73 | + |
| 74 | + return runCodePrompt(role, userMessage, code); |
| 75 | + } |
| 76 | + |
| 77 | +async function createGitUpdateRequest(src_directory : string, filename : string, modifiedCode : string) { |
| 78 | + // extract relative path from filename after slice_directory, extract function and source file name. |
| 79 | + // Relative path: code_slices\ast\sls\orig_sls_smt_solver.cpp_updt_params.cpp file name: orig_sls_smt.cpp |
| 80 | + const regex = /code_slices\\(.*)\\([^_]*)_(.*)\.cpp_(.*)\.cpp/; |
| 81 | + const match = filename.match(regex); |
| 82 | + if (!match) { |
| 83 | + console.log(`Filename does not match expected pattern: ${filename}`); |
| 84 | + return ""; |
| 85 | + } |
| 86 | + const [_, relative_path, prefix, fileName, funcName] = match; |
| 87 | + |
| 88 | + console.log(`Relative path: ${relative_path} file name: ${fileName}.cpp`); |
| 89 | + |
| 90 | + const srcFilePath = path.join(src_directory, relative_path, fileName + ".cpp"); |
| 91 | + const srcFileContent = await workspace.readText(srcFilePath); |
| 92 | + |
| 93 | + let role = |
| 94 | + `You are a highly experienced compiler engineer with over 20 years of expertise, |
| 95 | + specializing in C and C++ programming. Your deep knowledge of best coding practices |
| 96 | + and software engineering principles enables you to produce robust, efficient, and |
| 97 | + maintainable code in any scenario.`; |
| 98 | + |
| 99 | + const answer = await runPrompt( |
| 100 | + (_) => { |
| 101 | + _.def("ROLE", role); |
| 102 | + _.def("SOURCE", srcFileContent); |
| 103 | + _.def("REVIEW", modifiedCode); |
| 104 | + _.def("FUNCTION", funcName); |
| 105 | + _.$`Your role is <ROLE>. |
| 106 | + Please create a well-formed git patch based on the source code given in |
| 107 | + <SOURCE> |
| 108 | + A code analysis is for the method or function <FUNCTION>. |
| 109 | + The analysis is he following: |
| 110 | + <REVIEW>` |
| 111 | + } |
| 112 | + ) |
| 113 | + console.log(answer.text); |
| 114 | + return answer.text; |
| 115 | +} |
| 116 | + |
| 117 | +const input_directory = "code_slices"; |
| 118 | +const output_directory = "code_slices_analyzed"; |
| 119 | +const src_directory = "src"; |
| 120 | +const code_slice_files = await workspace.findFiles("code_slices/**/*.cpp"); |
| 121 | + |
| 122 | +let count = 0; |
| 123 | +for (const file of code_slice_files) { |
| 124 | + if (path.extname(file.filename) === '.cpp') { |
| 125 | + console.log(`Processing file: ${file.filename}`); |
| 126 | + |
| 127 | + const regex = /(.*)_(.*)\.cpp_(.*)\.cpp/; |
| 128 | + const match = file.filename.match(regex); |
| 129 | + |
| 130 | + if (!match) { |
| 131 | + console.log(`Filename does not match expected pattern: ${file.filename}`); |
| 132 | + continue; |
| 133 | + } |
| 134 | + const [_, prefix, fileName, funcName] = match; |
| 135 | + |
| 136 | + const content = file.content; |
| 137 | + const answer1 = await invokeLLMCompletion(content, fileName); |
| 138 | + const answer2 = await invokeLLMAnalyzer(answer1, fileName, funcName); |
| 139 | + const outputFilePath = path.join(output_directory, fileName + "_" + funcName + ".md"); |
| 140 | + await workspace.writeText(outputFilePath, answer2); |
| 141 | + const answer3 = await createGitUpdateRequest(src_directory, file.filename, answer2); |
| 142 | + const outputFilePath2 = path.join(output_directory, fileName + "_" + funcName + ".patch"); |
| 143 | + await workspace.writeText(outputFilePath2, answer3); |
| 144 | + ++count; |
| 145 | + if (count > 3) |
| 146 | + break; |
| 147 | + } |
| 148 | +} |
| 149 | + |
0 commit comments