(memory: PromptMemory, functions: PromptFunctions, tokenizer: Tokenizer, maxTokens: number)
| 19 | } |
| 20 | |
| 21 | public async renderAsMessages(memory: PromptMemory, functions: PromptFunctions, tokenizer: Tokenizer, maxTokens: number): Promise<RenderedPromptSection<Message<string>[]>> { |
| 22 | // Query the code index |
| 23 | const query = memory.get('input') as string; |
| 24 | const results = await this._index.query(query, { |
| 25 | maxDocuments: 100, |
| 26 | maxChunks: 2000 |
| 27 | }); |
| 28 | |
| 29 | // Render code & text snippets |
| 30 | let text = `Here are some snippets of code and text that might help:`; |
| 31 | let tokens = tokenizer.encode(text).length; |
| 32 | let remaining = maxTokens - tokens; |
| 33 | for (const result of results) { |
| 34 | // Create title |
| 35 | const title = `\n\npath: ${result.uri}\nsnippet:\n`; |
| 36 | const titleLength = tokenizer.encode(title).length; |
| 37 | if (remaining - titleLength < 0) { |
| 38 | break; |
| 39 | } |
| 40 | |
| 41 | // Render sections |
| 42 | const options = this.getSectionOptions(remaining - titleLength); |
| 43 | const sections = await result.renderSections(Math.min(remaining, options.tokens), options.sections); |
| 44 | |
| 45 | // Add snippets to text |
| 46 | for (const section of sections) { |
| 47 | const length = section.tokenCount + titleLength; |
| 48 | if (remaining - length < 0) { |
| 49 | break; |
| 50 | } |
| 51 | |
| 52 | text += title + section.text; |
| 53 | remaining -= length; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Return as a user message |
| 58 | return { |
| 59 | output: [{ role: 'user', content: text }], |
| 60 | length: maxTokens - remaining, |
| 61 | tooLong: remaining < 0 |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | private getSectionOptions(maxTokens: number): { sections: number; tokens: number; } { |
| 66 | if (maxTokens < 2000) { |
nothing calls this directly
no test coverage detected