Elffuss BraidHub

kikocis/continue public

87f8476c7cf1 · 7.4 KiBRawPermalink
1import { beforeEach, describe, expect, it } from "vitest";
2import { Position } from "../..";
3import { SnippetPayload } from "../../autocomplete/snippets";
4import { AutocompleteSnippetType } from "../../autocomplete/snippets/types";
5import { HelperVars } from "../../autocomplete/util/HelperVars";
6import {
7 MERCURY_CURRENT_FILE_CONTENT_CLOSE,
8 MERCURY_CURRENT_FILE_CONTENT_OPEN,
9 MERCURY_CURSOR,
10 MERCURY_EDIT_DIFF_HISTORY_CLOSE,
11 MERCURY_EDIT_DIFF_HISTORY_OPEN,
12 MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE,
13 MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_OPEN,
14 MODEL_1_USER_CURSOR_IS_HERE_TOKEN,
15} from "../constants";
16import {
17 renderDefaultSystemPrompt,
18 renderDefaultUserPrompt,
19 renderPrompt,
20} from "./NextEditPromptEngine";
21import { NEXT_EDIT_MODELS } from "../../llm/constants";
22
23describe("NextEditPromptEngine", () => {
24 describe("renderPrompt", () => {
25 let mercuryHelper: HelperVars;
26 let model1Helper: HelperVars;
27 let testHelper: HelperVars;
28 let unsupportedHelper: HelperVars;
29 let ctx: any;
30
31 beforeEach(() => {
32 mercuryHelper = {
33 modelName: "inception/mercury-coder-nextedit" as NEXT_EDIT_MODELS,
34 fileContents: "function test() {\n const a = 1;\n return a;\n}",
35 pos: { line: 1, character: 12 } as Position,
36 lang: { name: "typescript" },
37 } as HelperVars;
38 model1Helper = {
39 modelName: "continuedev/model-1" as NEXT_EDIT_MODELS,
40 fileContents: "function test() {\n const a = 1;\n return a;\n}",
41 pos: { line: 1, character: 12 } as Position,
42 lang: { name: "typescript" },
43 } as HelperVars;
44 unsupportedHelper = {
45 modelName: "mistral/codestral" as NEXT_EDIT_MODELS,
46 fileContents: "function test() {\n const a = 1;\n return a;\n}",
47 pos: { line: 1, character: 12 } as Position,
48 lang: { name: "typescript" },
49 } as HelperVars;
50
51 ctx = {
52 recentlyViewedCodeSnippets: [
53 { filepath: "/path/to/file1.ts", content: "const b = 2;" },
54 ],
55 currentFileContent: "function test() {\n const a = 1;\n return a;\n}",
56 editDiffHistory: "diff --git a/file.ts b/file.ts\n@@ -1,3 +1,4 @@",
57 editableRegionStartLine: 0,
58 editableRegionEndLine: 3,
59 userEdits: "Added constant a",
60 languageShorthand: "ts",
61 userExcerpts: "const a = 1;",
62 };
63 });
64
65 it("should render mercury-coder-nextedit prompt correctly", async () => {
66 const result = await renderPrompt(mercuryHelper, ctx);
67
68 expect(result).toHaveProperty("prompt");
69 expect(result.prompt.role).toBe("user");
70
71 const content = result.prompt.content;
72 expect(content).toContain(MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_OPEN);
73 expect(content).toContain(MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE);
74 expect(content).toContain(MERCURY_CURRENT_FILE_CONTENT_OPEN);
75 expect(content).toContain(MERCURY_CURRENT_FILE_CONTENT_CLOSE);
76 expect(content).toContain(MERCURY_EDIT_DIFF_HISTORY_OPEN);
77 expect(content).toContain(MERCURY_EDIT_DIFF_HISTORY_CLOSE);
78
79 expect(result.userEdits).toBe(ctx.editDiffHistory);
80 expect(result.userExcerpts).toContain(MERCURY_CURSOR);
81 });
82
83 it("should render model-1 prompt correctly", async () => {
84 const result = await renderPrompt(model1Helper, ctx);
85
86 expect(result).toHaveProperty("prompt");
87 expect(result.prompt.role).toBe("user");
88 expect(result.prompt.content).toContain("### User Edits:");
89 expect(result.prompt.content).toContain("### User Excerpts:");
90 expect(result.prompt.content).toContain("```ts");
91
92 expect(result.userEdits).toBe(ctx.userEdits);
93 expect(result.userExcerpts).toContain(MODEL_1_USER_CURSOR_IS_HERE_TOKEN);
94 });
95
96 it("should throw error for unsupported model name", async () => {
97 await expect(renderPrompt(unsupportedHelper, ctx)).rejects.toThrow(
98 "mistral/codestral is not yet supported for next edit.",
99 );
100 });
101 });
102
103 describe("renderDefaultSystemPrompt", () => {
104 it("should return a system prompt with expected role and content", () => {
105 const result = renderDefaultSystemPrompt();
106
107 expect(result.role).toBe("system");
108 expect(result.content).toContain("You are an expert polyglot developer");
109 expect(result.content).toContain("Do not hallucinate.");
110 });
111 });
112
113 describe("renderDefaultUserPrompt", () => {
114 it("should render a user prompt with the correct structure", () => {
115 const snippets: SnippetPayload = {
116 rootPathSnippets: [
117 {
118 type: AutocompleteSnippetType.Code,
119 filepath: "",
120 content: "root path",
121 },
122 ],
123 importDefinitionSnippets: [
124 {
125 type: AutocompleteSnippetType.Code,
126 filepath: "",
127 content: "import def",
128 },
129 ],
130 ideSnippets: [
131 {
132 type: AutocompleteSnippetType.Code,
133 filepath: "",
134 content: "ide snippets",
135 },
136 ],
137 recentlyEditedRangeSnippets: [
138 {
139 type: AutocompleteSnippetType.Code,
140 filepath: "",
141 content: "recent edits",
142 },
143 ],
144 diffSnippets: [
145 {
146 type: AutocompleteSnippetType.Diff,
147 content: "diff snippets",
148 },
149 ],
150 clipboardSnippets: [
151 {
152 type: AutocompleteSnippetType.Clipboard,
153 content: "clipboard",
154 copiedAt: new Date().toISOString(),
155 },
156 ],
157 recentlyVisitedRangesSnippets: [
158 {
159 type: AutocompleteSnippetType.Code,
160 filepath: "",
161 content: "visited ranges",
162 },
163 ],
164 recentlyOpenedFileSnippets: [
165 {
166 type: AutocompleteSnippetType.Code,
167 filepath: "",
168 content: "recently opened files",
169 },
170 ],
171 staticSnippet: [
172 {
173 type: AutocompleteSnippetType.Static,
174 filepath: "",
175 content: "static content",
176 },
177 ],
178 };
179
180 const helper: HelperVars = {
181 lang: { name: "typescript" },
182 } as HelperVars;
183
184 const result = renderDefaultUserPrompt(snippets, helper);
185
186 expect(result.role).toBe("user");
187 expect(result.content).toContain("Your junior made the following edit:");
188 expect(result.content).toContain("typescript");
189 expect(result.content).toContain("root path");
190 });
191 });
192
193 describe("insertTokens", () => {
194 it("should correctly insert cursor and editable region tokens", () => {
195 const mercuryHelper = {
196 modelName: "inception/mercury-coder-nextedit" as NEXT_EDIT_MODELS,
197 fileContents: "function test() {\n const a = 1;\n return a;\n}",
198 pos: { line: 1, character: 12 } as Position,
199 lang: { name: "typescript" },
200 } as HelperVars;
201
202 const ctx = {
203 recentlyViewedCodeSnippets: [
204 { filepath: "/path/to/file1.ts", content: "const b = 2;" },
205 ],
206 currentFileContent: "function test() {\n const a = 1;\n return a;\n}",
207 editDiffHistory: "diff --git a/file.ts b/file.ts\n@@ -1,3 +1,4 @@",
208 editableRegionStartLine: 0,
209 editableRegionEndLine: 3,
210 userEdits: "Added constant a",
211 languageShorthand: "ts",
212 userExcerpts: "const a = 1;",
213 };
214
215 return renderPrompt(mercuryHelper, ctx).then((result) => {
216 expect(result.userExcerpts).toContain(MERCURY_CURSOR);
217 });
218 });
219 });
220});