| 13 | import { INSTRUCTIONS_BASE_ITEM } from "./utils"; |
| 14 | |
| 15 | class DocsContextProvider extends BaseContextProvider { |
| 16 | static nRetrieve = 30; |
| 17 | static nFinal = 15; |
| 18 | static description: ContextProviderDescription = { |
| 19 | title: "docs", |
| 20 | displayTitle: "Docs", |
| 21 | description: "Type to search docs", |
| 22 | type: "submenu", |
| 23 | // Todo: consider a different renderInline so that when multiple docs are referenced in one message, |
| 24 | // Or the doc has an odd name unrelated to the content |
| 25 | // The name of the doc itself doesn't skew the embedding results |
| 26 | renderInlineAs: "", |
| 27 | }; |
| 28 | |
| 29 | constructor(options: any) { |
| 30 | super(options); |
| 31 | const docsService = DocsService.getSingleton(); |
| 32 | docsService?.setGithubToken(this.options?.githubToken); |
| 33 | } |
| 34 | |
| 35 | private async _rerankChunks( |
| 36 | chunks: Chunk[], |
| 37 | reranker: NonNullable<ContextProviderExtras["reranker"]>, |
| 38 | fullInput: ContextProviderExtras["fullInput"], |
| 39 | ide: IDE, |
| 40 | ) { |
| 41 | let chunksCopy = [...chunks]; |
| 42 | |
| 43 | try { |
| 44 | const scores = await reranker.rerank(fullInput, chunksCopy); |
| 45 | |
| 46 | if (Array.isArray(scores)) { |
| 47 | // reranker model has returned a valid array |
| 48 | chunksCopy.sort( |
| 49 | (a, b) => |
| 50 | scores[chunksCopy.indexOf(b)] - scores[chunksCopy.indexOf(a)], |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | chunksCopy = chunksCopy.splice( |
| 55 | 0, |
| 56 | this.options?.nFinal ?? DocsContextProvider.nFinal, |
| 57 | ); |
| 58 | } catch (e) { |
| 59 | void ide.showToast("warning", `Failed to rerank retrieval results\n${e}`); |
| 60 | |
| 61 | chunksCopy = chunksCopy.splice( |
| 62 | 0, |
| 63 | this.options?.nFinal ?? DocsContextProvider.nFinal, |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | return chunksCopy; |
| 68 | } |
| 69 | |
| 70 | private _sortAlphabetically( |
| 71 | submenuItems: ContextSubmenuItem[], |
| 72 | ): ContextSubmenuItem[] { |
nothing calls this directly
no outgoing calls
no test coverage detected