| 1 | import { BranchAndDir, ContextItem, ContextProviderExtras } from "../../"; |
| 2 | import { getUriDescription } from "../../util/uri"; |
| 3 | import { INSTRUCTIONS_BASE_ITEM } from "../providers/utils"; |
| 4 | |
| 5 | import { RetrievalPipelineOptions } from "./pipelines/BaseRetrievalPipeline"; |
| 6 | import NoRerankerRetrievalPipeline from "./pipelines/NoRerankerRetrievalPipeline"; |
| 7 | import RerankerRetrievalPipeline from "./pipelines/RerankerRetrievalPipeline"; |
| 8 | |
| 9 | const DEFAULT_N_FINAL = 25; |
| 10 | |
| 11 | export async function retrieveContextItemsFromEmbeddings( |
| 12 | extras: ContextProviderExtras, |
| 13 | options: any | undefined, |
| 14 | filterDirectory: string | undefined, |
| 15 | ): Promise<ContextItem[]> { |
| 16 | // Currently you can use codebase without an embeddings provider and it will just skip the embeddings inputs |
| 17 | // if (!extras.embeddingsProvider) { |
| 18 | // void extras.ide.showToast( |
| 19 | // "warning", |
| 20 | // "Set up an embeddings model to use this feature. Visit the docs to learn more: " + |
| 21 | // "https://docs.continue.dev/customize/model-roles/embeddings", |
| 22 | // ); |
| 23 | // return []; |
| 24 | // } |
| 25 | const includeEmbeddings = !!extras.config.selectedModelByRole.embed; |
| 26 | |
| 27 | // Get tags to retrieve for |
| 28 | const workspaceDirs = await extras.ide.getWorkspaceDirs(); |
| 29 | |
| 30 | if (workspaceDirs.length === 0) { |
| 31 | throw new Error("No workspace directories found"); |
| 32 | } |
| 33 | |
| 34 | // Fill half of the context length, up to a max of 100 snippets |
| 35 | const contextLength = extras.llm.contextLength; |
| 36 | const tokensPerSnippet = 512; |
| 37 | const nFinal = |
| 38 | options?.nFinal ?? |
| 39 | Math.min(DEFAULT_N_FINAL, contextLength / tokensPerSnippet / 2); |
| 40 | const useReranking = options?.useReranking ?? !!extras.reranker; |
| 41 | const nRetrieve = useReranking ? options?.nRetrieve || 2 * nFinal : nFinal; |
| 42 | |
| 43 | const branches = (await Promise.race([ |
| 44 | Promise.all(workspaceDirs.map((dir) => extras.ide.getBranch(dir))), |
| 45 | new Promise((resolve) => { |
| 46 | setTimeout(() => { |
| 47 | resolve(["NONE"]); |
| 48 | }, 500); |
| 49 | }), |
| 50 | ])) as string[]; |
| 51 | |
| 52 | const tags: BranchAndDir[] = workspaceDirs.map((directory, i) => ({ |
| 53 | directory, |
| 54 | branch: branches[i], |
| 55 | })); |
| 56 | |
| 57 | const pipelineType = useReranking |
| 58 | ? RerankerRetrievalPipeline |
| 59 | : NoRerankerRetrievalPipeline; |
| 60 | |
| 61 | const pipelineOptions: RetrievalPipelineOptions = { |
| 62 | nFinal, |
| 63 | nRetrieve, |
| 64 | tags, |
| 65 | filterDirectory, |
| 66 | ide: extras.ide, |
| 67 | input: extras.fullInput, |
| 68 | llm: extras.llm, |
| 69 | config: extras.config, |
| 70 | }; |
| 71 | |
| 72 | const pipeline = new pipelineType(pipelineOptions); |
| 73 | const results = await pipeline.run({ |
| 74 | tags, |
| 75 | filterDirectory, |
| 76 | query: extras.fullInput, |
| 77 | includeEmbeddings, |
| 78 | }); |
| 79 | |
| 80 | if (results.length === 0) { |
| 81 | if (extras.config.disableIndexing) { |
| 82 | void extras.ide.showToast( |
| 83 | "warning", |
| 84 | "No results found (Indexing disabled).", |
| 85 | ); |
| 86 | return []; |
| 87 | } |
| 88 | if (extras.isInAgentMode) { |
| 89 | return [ |
| 90 | { |
| 91 | ...INSTRUCTIONS_BASE_ITEM, |
| 92 | content: "No results were found. Try using other tools.", |
| 93 | }, |
| 94 | ]; |
| 95 | } |
| 96 | return []; |
| 97 | } |
| 98 | |
| 99 | return [ |
| 100 | { |
| 101 | ...INSTRUCTIONS_BASE_ITEM, |
| 102 | content: |
| 103 | "Use the above code to answer the following question. You should not reference any files outside of what is shown, unless they are commonly known files, like a .gitignore or package.json. Reference the filenames whenever possible. If there isn't enough information to answer the question, suggest where the user might look to learn more.", |
| 104 | }, |
| 105 | ...results |
| 106 | .sort((a, b) => a.filepath.localeCompare(b.filepath)) |
| 107 | .map((r) => { |
| 108 | const { relativePathOrBasename, last2Parts, baseName } = |
| 109 | getUriDescription(r.filepath, workspaceDirs); |
| 110 | |
| 111 | if (baseName === "package.json") { |
| 112 | console.warn("Retrieval pipeline: package.json detected"); |
| 113 | } |
| 114 | |
| 115 | return { |
| 116 | // Don't show line numbers for tool calls where startLine = -1 |
| 117 | name: |
| 118 | r.startLine === -1 |
| 119 | ? baseName |
| 120 | : `${baseName} (${r.startLine + 1}-${r.endLine + 1})`, |
| 121 | description: last2Parts, |
| 122 | content: `\`\`\`${relativePathOrBasename}\n${r.content}\n\`\`\``, |
| 123 | uri: { |
| 124 | type: "file" as const, |
| 125 | value: r.filepath, |
| 126 | }, |
| 127 | }; |
| 128 | }), |
| 129 | ]; |
| 130 | } |