Elffuss BraidHub

kikocis/continue public

4940c71be2bf · 7.9 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 CURRENT_FILE_CONTENT_CLOSE,
8 CURRENT_FILE_CONTENT_OPEN,
9 CURSOR,
10 EDIT_DIFF_HISTORY_CLOSE,
11 EDIT_DIFF_HISTORY_OPEN,
12 RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE,
13 RECENTLY_VIEWED_CODE_SNIPPETS_OPEN,
14 USER_CURSOR_IS_HERE_TOKEN,
15} from "../constants";
16import {
17 NextEditModelName,
18 renderDefaultSystemPrompt,
19 renderDefaultUserPrompt,
20 renderPrompt,
21} from "./NextEditPromptEngine";
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 NextEditModelName,
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 NextEditModelName,
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 testHelper = {
45 modelName: "this field is not used" as NextEditModelName,
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 unsupportedHelper = {
51 modelName: "mistral/codestral" as NextEditModelName,
52 fileContents: "function test() {\n const a = 1;\n return a;\n}",
53 pos: { line: 1, character: 12 } as Position,
54 lang: { name: "typescript" },
55 } as HelperVars;
56
57 ctx = {
58 recentlyViewedCodeSnippets: [
59 { filepath: "/path/to/file1.ts", content: "const b = 2;" },
60 ],
61 currentFileContent: "function test() {\n const a = 1;\n return a;\n}",
62 editDiffHistory: "diff --git a/file.ts b/file.ts\n@@ -1,3 +1,4 @@",
63 editableRegionStartLine: 0,
64 editableRegionEndLine: 3,
65 userEdits: "Added constant a",
66 languageShorthand: "ts",
67 userExcerpts: "const a = 1;",
68 };
69 });
70
71 it("should render mercury-coder-nextedit prompt correctly", async () => {
72 const result = await renderPrompt(mercuryHelper, ctx);
73
74 expect(result).toHaveProperty("prompt");
75 expect(result.prompt.role).toBe("user");
76
77 const content = result.prompt.content;
78 expect(content).toContain(RECENTLY_VIEWED_CODE_SNIPPETS_OPEN);
79 expect(content).toContain(RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE);
80 expect(content).toContain(CURRENT_FILE_CONTENT_OPEN);
81 expect(content).toContain(CURRENT_FILE_CONTENT_CLOSE);
82 expect(content).toContain(EDIT_DIFF_HISTORY_OPEN);
83 expect(content).toContain(EDIT_DIFF_HISTORY_CLOSE);
84
85 expect(result.userEdits).toBe(ctx.editDiffHistory);
86 expect(result.userExcerpts).toContain(CURSOR);
87 });
88
89 it("should render model-1 prompt correctly", async () => {
90 const result = await renderPrompt(model1Helper, ctx);
91
92 expect(result).toHaveProperty("prompt");
93 expect(result.prompt.role).toBe("user");
94 expect(result.prompt.content).toContain("### User Edits:");
95 expect(result.prompt.content).toContain("### User Excerpts:");
96 expect(result.prompt.content).toContain("```ts");
97
98 expect(result.userEdits).toBe(ctx.userEdits);
99 expect(result.userExcerpts).toContain(USER_CURSOR_IS_HERE_TOKEN);
100 });
101
102 it("should handle 'this field is not used' model name", async () => {
103 const result = await renderPrompt(testHelper, ctx);
104
105 expect(result).toHaveProperty("prompt");
106 expect(result.prompt.role).toBe("user");
107 expect(result.prompt.content).toBe("NEXT_EDIT");
108 expect(result.userEdits).toBe("");
109 expect(result.userExcerpts).toBe(testHelper.fileContents);
110 });
111
112 it("should throw error for unsupported model name", async () => {
113 await expect(renderPrompt(unsupportedHelper, ctx)).rejects.toThrow(
114 "mistral/codestral is not yet supported for next edit.",
115 );
116 });
117 });
118
119 describe("renderDefaultSystemPrompt", () => {
120 it("should return a system prompt with expected role and content", () => {
121 const result = renderDefaultSystemPrompt();
122
123 expect(result.role).toBe("system");
124 expect(result.content).toContain("You are an expert polyglot developer");
125 expect(result.content).toContain("Do not hallucinate.");
126 });
127 });
128
129 describe("renderDefaultUserPrompt", () => {
130 it("should render a user prompt with the correct structure", () => {
131 const snippets: SnippetPayload = {
132 rootPathSnippets: [
133 {
134 type: AutocompleteSnippetType.Code,
135 filepath: "",
136 content: "root path",
137 },
138 ],
139 importDefinitionSnippets: [
140 {
141 type: AutocompleteSnippetType.Code,
142 filepath: "",
143 content: "import def",
144 },
145 ],
146 ideSnippets: [
147 {
148 type: AutocompleteSnippetType.Code,
149 filepath: "",
150 content: "ide snippets",
151 },
152 ],
153 recentlyEditedRangeSnippets: [
154 {
155 type: AutocompleteSnippetType.Code,
156 filepath: "",
157 content: "recent edits",
158 },
159 ],
160 diffSnippets: [
161 {
162 type: AutocompleteSnippetType.Diff,
163 content: "diff snippets",
164 },
165 ],
166 clipboardSnippets: [
167 {
168 type: AutocompleteSnippetType.Clipboard,
169 content: "clipboard",
170 copiedAt: new Date().toISOString(),
171 },
172 ],
173 recentlyVisitedRangesSnippets: [
174 {
175 type: AutocompleteSnippetType.Code,
176 filepath: "",
177 content: "visited ranges",
178 },
179 ],
180 recentlyOpenedFileSnippets: [
181 {
182 type: AutocompleteSnippetType.Code,
183 filepath: "",
184 content: "recently opened files",
185 },
186 ],
187 staticSnippet: [
188 {
189 type: AutocompleteSnippetType.Static,
190 filepath: "",
191 content: "static content",
192 },
193 ],
194 };
195
196 const helper: HelperVars = {
197 lang: { name: "typescript" },
198 } as HelperVars;
199
200 const result = renderDefaultUserPrompt(snippets, helper);
201
202 expect(result.role).toBe("user");
203 expect(result.content).toContain("Your junior made the following edit:");
204 expect(result.content).toContain("typescript");
205 expect(result.content).toContain("root path");
206 });
207 });
208
209 describe("insertTokens", () => {
210 it("should correctly insert cursor and editable region tokens", () => {
211 const mercuryHelper = {
212 modelName: "inception/mercury-coder-nextedit" as NextEditModelName,
213 fileContents: "function test() {\n const a = 1;\n return a;\n}",
214 pos: { line: 1, character: 12 } as Position,
215 lang: { name: "typescript" },
216 } as HelperVars;
217
218 const ctx = {
219 recentlyViewedCodeSnippets: [
220 { filepath: "/path/to/file1.ts", content: "const b = 2;" },
221 ],
222 currentFileContent: "function test() {\n const a = 1;\n return a;\n}",
223 editDiffHistory: "diff --git a/file.ts b/file.ts\n@@ -1,3 +1,4 @@",
224 editableRegionStartLine: 0,
225 editableRegionEndLine: 3,
226 userEdits: "Added constant a",
227 languageShorthand: "ts",
228 userExcerpts: "const a = 1;",
229 };
230
231 return renderPrompt(mercuryHelper, ctx).then((result) => {
232 expect(result.userExcerpts).toContain(CURSOR);
233 });
234 });
235 });
236});