Elffuss BraidHub

kikocis/continue public

main · 3.3 KiBRawPermalink
1import { Chunk, ContinueConfig, IDE, ILLM } from "../..";
2import { getModelByRole } from "../../config/util";
3import generateRepoMap from "../../util/generateRepoMap";
4import { resolveRelativePathInDir } from "../../util/ideUtils";
5import { renderChatMessage } from "../../util/messageContent";
6
7const SUPPORTED_MODEL_TITLE_FAMILIES = [
8 "claude-3",
9 "llama3.1",
10 "llama3.2",
11 "gemini-1.5",
12 "gpt-4",
13];
14
15function isSupportedModel(
16 config: ContinueConfig,
17 modelTitle?: string,
18): boolean {
19 if (config.experimental?.modelRoles?.repoMapFileSelection) {
20 return true;
21 }
22
23 if (!modelTitle) {
24 return false;
25 }
26
27 const lowercaseModelTitle = modelTitle.toLowerCase();
28
29 return SUPPORTED_MODEL_TITLE_FAMILIES.some((title) =>
30 lowercaseModelTitle.includes(title),
31 );
32}
33
34export async function requestFilesFromRepoMap(
35 defaultLlm: ILLM,
36 config: ContinueConfig,
37 ide: IDE,
38 input: string,
39 filterDirUri?: string,
40): Promise<Chunk[]> {
41 const llm = getModelByRole(config, "repoMapFileSelection") ?? defaultLlm;
42
43 // Only supported for Claude models right now
44 if (!isSupportedModel(config, llm.title)) {
45 return [];
46 }
47
48 try {
49 const repoMap = await generateRepoMap(llm, ide, {
50 dirUris: filterDirUri ? [filterDirUri] : undefined,
51 includeSignatures: false,
52 outputRelativeUriPaths: true,
53 });
54
55 const prompt = `${repoMap}
56
57Given the above repo map, your task is to decide which files are most likely to be relevant in answering a question. Before giving your answer, you should write your reasoning about which files/folders are most important. This thinking should start with a <reasoning> tag, followed by a paragraph explaining your reasoning, and then a closing </reasoning> tag on the last line.
58
59After this, your response should begin with a <results> tag, followed by a list of each file, one per line, and then a closing </results> tag on the last line. You should select between 5 and 10 files. The names that you list should be the exact relative path that you saw in the repo map, not just the basename of the file.
60
61This is the question that you should select relevant files for: "${input}"`;
62
63 const response = await llm.chat(
64 [
65 { role: "user", content: prompt },
66 { role: "assistant", content: "<reasoning>" },
67 ],
68 new AbortController().signal,
69 );
70 const content = renderChatMessage(response);
71
72 if (!content.includes("\n")) {
73 return [];
74 }
75
76 const filepaths = content
77 .split("<results>")[1]
78 ?.split("</results>")[0]
79 ?.split("\n")
80 .filter(Boolean)
81 .map((filepath) => filepath.trim());
82
83 const chunks = await Promise.all(
84 filepaths.map(async (filepath) => {
85 const uri = await resolveRelativePathInDir(filepath, ide);
86 if (!uri) {
87 return undefined;
88 }
89 const content = await ide.readFile(uri);
90 const lineCount = content.split("\n").length;
91 const chunk: Chunk = {
92 digest: uri,
93 content,
94 filepath: uri,
95 endLine: lineCount - 1,
96 startLine: 0,
97 index: 0,
98 };
99 return chunk;
100 }),
101 );
102
103 return chunks.filter((c) => c !== undefined) as Chunk[];
104 } catch (e) {
105 console.debug("Error requesting files from repo map", e);
106 return [];
107 }
108}