* A section that renders source code snippets from the code index.
| 15 | * A section that renders source code snippets from the code index. |
| 16 | */ |
| 17 | class SourceCodeSection extends promptrix_1.PromptSectionBase { |
| 18 | /** |
| 19 | * Creates a new 'SourceCodeSection' instance. |
| 20 | * @param index Code index to use. |
| 21 | * @param tokens Optional. Sizing strategy for this section. Defaults to `auto`. |
| 22 | * @param userPrefix Optional. Prefix to use for text output. Defaults to `user: `. |
| 23 | */ |
| 24 | constructor(index, tokens = -1, userPrefix = 'user: ') { |
| 25 | super(tokens, true, '\n', userPrefix); |
| 26 | this._index = index; |
| 27 | } |
| 28 | renderAsMessages(memory, functions, tokenizer, maxTokens) { |
| 29 | return __awaiter(this, void 0, void 0, function* () { |
| 30 | // Query the code index |
| 31 | const query = memory.get('input'); |
| 32 | const results = yield this._index.query(query, { |
| 33 | maxDocuments: 100, |
| 34 | maxChunks: 2000 |
| 35 | }); |
| 36 | // Render code & text snippets |
| 37 | let text = `Here are some snippets of code and text that might help:`; |
| 38 | let tokens = tokenizer.encode(text).length; |
| 39 | let remaining = maxTokens - tokens; |
| 40 | for (const result of results) { |
| 41 | // Create title |
| 42 | const title = `\n\npath: ${result.uri}\nsnippet:\n`; |
| 43 | const titleLength = tokenizer.encode(title).length; |
| 44 | if (remaining - titleLength < 0) { |
| 45 | break; |
| 46 | } |
| 47 | // Render sections |
| 48 | const options = this.getSectionOptions(remaining - titleLength); |
| 49 | const sections = yield result.renderSections(Math.min(remaining, options.tokens), options.sections); |
| 50 | // Add snippets to text |
| 51 | for (const section of sections) { |
| 52 | const length = section.tokenCount + titleLength; |
| 53 | if (remaining - length < 0) { |
| 54 | break; |
| 55 | } |
| 56 | text += title + section.text; |
| 57 | remaining -= length; |
| 58 | } |
| 59 | } |
| 60 | // Return as a user message |
| 61 | return { |
| 62 | output: [{ role: 'user', content: text }], |
| 63 | length: maxTokens - remaining, |
| 64 | tooLong: remaining < 0 |
| 65 | }; |
| 66 | }); |
| 67 | } |
| 68 | getSectionOptions(maxTokens) { |
| 69 | if (maxTokens < 2000) { |
| 70 | return { sections: 1, tokens: maxTokens }; |
| 71 | } |
| 72 | else if (maxTokens <= 6000) { |
| 73 | return { sections: 1, tokens: 2000 }; |
| 74 | } |
nothing calls this directly
no outgoing calls
no test coverage detected