| 1 | import { HelperVars } from "../../autocomplete/util/HelperVars.js"; |
| 2 | import { NEXT_EDIT_MODELS } from "../../llm/constants.js"; |
| 3 | import { MERCURY_SYSTEM_PROMPT, UNIQUE_TOKEN } from "../constants.js"; |
| 4 | import { |
| 5 | currentFileContentBlock, |
| 6 | editHistoryBlock, |
| 7 | recentlyViewedCodeSnippetsBlock, |
| 8 | } from "../templating/mercuryCoderNextEdit.js"; |
| 9 | import { |
| 10 | NEXT_EDIT_MODEL_TEMPLATES, |
| 11 | PromptTemplateRenderer, |
| 12 | } from "../templating/NextEditPromptEngine.js"; |
| 13 | import { ModelSpecificContext, Prompt, PromptMetadata } from "../types.js"; |
| 14 | import { BaseNextEditModelProvider } from "./BaseNextEditProvider.js"; |
| 15 | |
| 16 | export class MercuryCoderProvider extends BaseNextEditModelProvider { |
| 17 | private templateRenderer: PromptTemplateRenderer; |
| 18 | |
| 19 | constructor() { |
| 20 | super(NEXT_EDIT_MODELS.MERCURY_CODER); |
| 21 | |
| 22 | const template = NEXT_EDIT_MODEL_TEMPLATES[NEXT_EDIT_MODELS.MERCURY_CODER]; |
| 23 | this.templateRenderer = new PromptTemplateRenderer(template.template); |
| 24 | } |
| 25 | |
| 26 | getSystemPrompt(): string { |
| 27 | return MERCURY_SYSTEM_PROMPT; |
| 28 | } |
| 29 | |
| 30 | getWindowSize() { |
| 31 | return { topMargin: 0, bottomMargin: 5 }; |
| 32 | } |
| 33 | |
| 34 | shouldInjectUniqueToken(): boolean { |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | getUniqueToken(): string { |
| 39 | return UNIQUE_TOKEN; |
| 40 | } |
| 41 | |
| 42 | extractCompletion(message: string): string { |
| 43 | // Extract the code between the markdown code blocks. |
| 44 | return message.slice( |
| 45 | message.indexOf("```\n") + "```\n".length, |
| 46 | message.lastIndexOf("\n\n```"), |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | buildPromptContext(context: ModelSpecificContext): any { |
| 51 | return { |
| 52 | recentlyViewedCodeSnippets: |
| 53 | context.snippetPayload.recentlyVisitedRangesSnippets.map((snip) => ({ |
| 54 | filepath: snip.filepath, |
| 55 | content: snip.content, |
| 56 | })) ?? [], |
| 57 | currentFileContent: context.helper.fileContents, |
| 58 | editableRegionStartLine: context.editableRegionStartLine, |
| 59 | editableRegionEndLine: context.editableRegionEndLine, |
| 60 | editDiffHistory: context.diffContext, |
| 61 | currentFilePath: context.helper.filepath, |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | async generatePrompts(context: ModelSpecificContext): Promise<Prompt[]> { |
| 66 | const promptCtx = this.buildPromptContext(context); |
| 67 | |
| 68 | const templateVars = { |
| 69 | recentlyViewedCodeSnippets: recentlyViewedCodeSnippetsBlock( |
| 70 | promptCtx.recentlyViewedCodeSnippets, |
| 71 | ), |
| 72 | currentFileContent: currentFileContentBlock( |
| 73 | promptCtx.currentFileContent, |
| 74 | promptCtx.editableRegionStartLine, |
| 75 | promptCtx.editableRegionEndLine, |
| 76 | context.helper.pos, |
| 77 | ), |
| 78 | editDiffHistory: editHistoryBlock(promptCtx.editDiffHistory), |
| 79 | currentFilePath: promptCtx.currentFilePath, |
| 80 | }; |
| 81 | |
| 82 | const userPromptContent = this.templateRenderer.render(templateVars); |
| 83 | |
| 84 | return [ |
| 85 | { |
| 86 | role: "system", |
| 87 | content: this.getSystemPrompt(), |
| 88 | }, |
| 89 | { |
| 90 | role: "user", |
| 91 | content: userPromptContent, |
| 92 | }, |
| 93 | ]; |
| 94 | } |
| 95 | |
| 96 | buildPromptMetadata(context: ModelSpecificContext): PromptMetadata { |
| 97 | const promptCtx = this.buildPromptContext(context); |
| 98 | |
| 99 | const templateVars = { |
| 100 | recentlyViewedCodeSnippets: recentlyViewedCodeSnippetsBlock( |
| 101 | promptCtx.recentlyViewedCodeSnippets, |
| 102 | ), |
| 103 | currentFileContent: currentFileContentBlock( |
| 104 | promptCtx.currentFileContent, |
| 105 | promptCtx.editableRegionStartLine, |
| 106 | promptCtx.editableRegionEndLine, |
| 107 | context.helper.pos, |
| 108 | ), |
| 109 | editDiffHistory: editHistoryBlock(promptCtx.editDiffHistory), |
| 110 | currentFilePath: promptCtx.currentFilePath, |
| 111 | }; |
| 112 | |
| 113 | const userPromptContent = this.templateRenderer.render(templateVars); |
| 114 | |
| 115 | return { |
| 116 | prompt: { |
| 117 | role: "user", |
| 118 | content: userPromptContent, |
| 119 | }, |
| 120 | userEdits: promptCtx.editDiffHistory.join("\n"), |
| 121 | userExcerpts: templateVars.currentFileContent, |
| 122 | }; |
| 123 | } |
| 124 | |
| 125 | calculateEditableRegion( |
| 126 | helper: HelperVars, |
| 127 | usingFullFileDiff: boolean, |
| 128 | ): { |
| 129 | editableRegionStartLine: number; |
| 130 | editableRegionEndLine: number; |
| 131 | } { |
| 132 | if (usingFullFileDiff) { |
| 133 | return this.calculateOptimalEditableRegion(helper, 512, "tokenizer"); |
| 134 | } else { |
| 135 | const { topMargin, bottomMargin } = this.getWindowSize(); |
| 136 | return { |
| 137 | editableRegionStartLine: Math.max(helper.pos.line - topMargin, 0), |
| 138 | editableRegionEndLine: Math.min( |
| 139 | helper.pos.line + bottomMargin, |
| 140 | helper.fileLines.length - 1, |
| 141 | ), |
| 142 | }; |
| 143 | } |
| 144 | } |
| 145 | } |