MCPcopy Create free account
hub / github.com/Stevenic/codepilot / SourceCodeSection

Class SourceCodeSection

src/SourceCodeSection.ts:7–74  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5 * A section that renders source code snippets from the code index.
6 */
7export class SourceCodeSection extends PromptSectionBase {
8 private readonly _index: CodeIndex;
9
10 /**
11 * Creates a new 'SourceCodeSection' instance.
12 * @param index Code index to use.
13 * @param tokens Optional. Sizing strategy for this section. Defaults to `auto`.
14 * @param userPrefix Optional. Prefix to use for text output. Defaults to `user: `.
15 */
16 public constructor(index: CodeIndex, tokens: number = -1, userPrefix: string = 'user: ') {
17 super(tokens, true, '\n', userPrefix);
18 this._index = index;
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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected