Elffuss BraidHub

kikocis/continue public

main · 4.7 KiBRawPermalink
1import { HelperVars } from "../../autocomplete/util/HelperVars.js";
2import { NEXT_EDIT_MODELS } from "../../llm/constants.js";
3import { INSTINCT_SYSTEM_PROMPT } from "../constants.js";
4import {
5 contextSnippetsBlock,
6 currentFileContentBlock,
7 editHistoryBlock,
8} from "../templating/instinct.js";
9import {
10 NEXT_EDIT_MODEL_TEMPLATES,
11 PromptTemplateRenderer,
12} from "../templating/NextEditPromptEngine.js";
13import { ModelSpecificContext, Prompt, PromptMetadata } from "../types.js";
14import { BaseNextEditModelProvider } from "./BaseNextEditProvider.js";
15
16export class InstinctProvider extends BaseNextEditModelProvider {
17 private templateRenderer: PromptTemplateRenderer;
18
19 constructor() {
20 super(NEXT_EDIT_MODELS.INSTINCT);
21
22 const template = NEXT_EDIT_MODEL_TEMPLATES[NEXT_EDIT_MODELS.INSTINCT];
23 this.templateRenderer = new PromptTemplateRenderer(template.template);
24 }
25
26 getSystemPrompt(): string {
27 return INSTINCT_SYSTEM_PROMPT;
28 }
29
30 getWindowSize() {
31 return { topMargin: 1, bottomMargin: 5 };
32 }
33
34 shouldInjectUniqueToken(): boolean {
35 return false; // Instinct doesn't use unique tokens.
36 }
37
38 extractCompletion(message: string): string {
39 return message; // Instinct returns the completion directly.
40 }
41
42 buildPromptContext(context: ModelSpecificContext): any {
43 // Calculate the window around the cursor position (25 lines above and below).
44 const windowStart = Math.max(0, context.helper.pos.line - 25);
45 const windowEnd = Math.min(
46 context.helper.fileLines.length - 1,
47 context.helper.pos.line + 25,
48 );
49
50 // Ensure editable region boundaries are within the window.
51 const adjustedEditableStart = Math.max(
52 windowStart,
53 context.editableRegionStartLine,
54 );
55 const adjustedEditableEnd = Math.min(
56 windowEnd,
57 context.editableRegionEndLine,
58 );
59
60 return {
61 contextSnippets: context.autocompleteContext,
62 currentFileContent: context.helper.fileContents,
63 windowStart,
64 windowEnd,
65 editableRegionStartLine: adjustedEditableStart,
66 editableRegionEndLine: adjustedEditableEnd,
67 editDiffHistory: context.diffContext,
68 currentFilePath: context.helper.filepath,
69 languageShorthand: context.helper.lang.name,
70 };
71 }
72
73 async generatePrompts(context: ModelSpecificContext): Promise<Prompt[]> {
74 const promptCtx = this.buildPromptContext(context);
75
76 const templateVars = {
77 contextSnippets: contextSnippetsBlock(promptCtx.contextSnippets),
78 currentFileContent: currentFileContentBlock(
79 promptCtx.currentFileContent,
80 promptCtx.windowStart,
81 promptCtx.windowEnd,
82 promptCtx.editableRegionStartLine,
83 promptCtx.editableRegionEndLine,
84 context.helper.pos,
85 ),
86 editDiffHistory: editHistoryBlock(promptCtx.editDiffHistory),
87 currentFilePath: promptCtx.currentFilePath,
88 languageShorthand: promptCtx.languageShorthand,
89 };
90
91 const userPromptContent = this.templateRenderer.render(templateVars);
92
93 return [
94 {
95 role: "system",
96 content: this.getSystemPrompt(),
97 },
98 {
99 role: "user",
100 content: userPromptContent,
101 },
102 ];
103 }
104
105 buildPromptMetadata(context: ModelSpecificContext): PromptMetadata {
106 const promptCtx = this.buildPromptContext(context);
107
108 const templateVars = {
109 contextSnippets: contextSnippetsBlock(promptCtx.contextSnippets),
110 currentFileContent: currentFileContentBlock(
111 promptCtx.currentFileContent,
112 promptCtx.windowStart,
113 promptCtx.windowEnd,
114 promptCtx.editableRegionStartLine,
115 promptCtx.editableRegionEndLine,
116 context.helper.pos,
117 ),
118 editDiffHistory: editHistoryBlock(promptCtx.editDiffHistory),
119 currentFilePath: promptCtx.currentFilePath,
120 languageShorthand: promptCtx.languageShorthand,
121 };
122
123 const userPromptContent = this.templateRenderer.render(templateVars);
124
125 return {
126 prompt: {
127 role: "user",
128 content: userPromptContent,
129 },
130 userEdits: promptCtx.editDiffHistory.join("\n"),
131 userExcerpts: templateVars.currentFileContent,
132 };
133 }
134
135 calculateEditableRegion(
136 helper: HelperVars,
137 usingFullFileDiff: boolean,
138 ): {
139 editableRegionStartLine: number;
140 editableRegionEndLine: number;
141 } {
142 if (usingFullFileDiff) {
143 return this.calculateOptimalEditableRegion(helper, 512, "tokenizer");
144 } else {
145 const { topMargin, bottomMargin } = this.getWindowSize();
146 return {
147 editableRegionStartLine: Math.max(helper.pos.line - topMargin, 0),
148 editableRegionEndLine: Math.min(
149 helper.pos.line + bottomMargin,
150 helper.fileLines.length - 1,
151 ),
152 };
153 }
154 }
155}