Changesets · Branches · Tags · Compare
| Changeset | 80a7ebf02fa6a9e63ba449b90ecd920ea9a048d8659898aca320df2515e405bb |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-08-18 11:57 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-08-18 11:57 -07:00 |
| Parents | f5ca24cbed08, 79db7a83c7e6 |
2 files changed
modified core/indexing/chunk/code.test.ts +58 −1
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | import { ChunkWithoutID } from "../.."; |
| 2 | - | |
| 2 | +import { cleanupAsyncEncoders, countTokensAsync } from "../../llm/countTokens"; | |
| 3 | 3 | import { codeChunker } from "./code"; |
| 4 | 4 | |
| 5 | 5 | async function genToArr<T>(generator: AsyncGenerator<T>): Promise<T[]> { |
| @@ -71,3 +71,60 @@ | ||
| 71 | 71 | expect(chunks).toContain('def method20():\n return "Hello, 20!"'); |
| 72 | 72 | }); |
| 73 | 73 | }); |
| 74 | + | |
| 75 | +async function collectContents( | |
| 76 | + filepath: string, | |
| 77 | + contents: string, | |
| 78 | + maxChunkSize: number, | |
| 79 | +): Promise<string[]> { | |
| 80 | + const out: string[] = []; | |
| 81 | + for await (const c of codeChunker(filepath, contents, maxChunkSize)) { | |
| 82 | + out.push(c.content); | |
| 83 | + } | |
| 84 | + return out; | |
| 85 | +} | |
| 86 | + | |
| 87 | +describe("codeChunker function definition", () => { | |
| 88 | + afterAll(async () => await cleanupAsyncEncoders()); | |
| 89 | + | |
| 90 | + test("class methods never exceed maxChunkSize", async () => { | |
| 91 | + const body = Array.from( | |
| 92 | + { length: 200 }, | |
| 93 | + (_, i) => `console.log(${i});`, | |
| 94 | + ).join("\n"); | |
| 95 | + const contents = ` | |
| 96 | +class C { | |
| 97 | + a() { | |
| 98 | + ${body} | |
| 99 | + } | |
| 100 | + b() { | |
| 101 | + ${body} | |
| 102 | + } | |
| 103 | +} | |
| 104 | +`; | |
| 105 | + const max = 50; | |
| 106 | + const chunks = await collectContents("filename1.ts", contents, max); | |
| 107 | + const counts = await Promise.all(chunks.map((s) => countTokensAsync(s))); | |
| 108 | + counts.forEach((n) => expect(n).toBeLessThanOrEqual(max)); | |
| 109 | + }); | |
| 110 | + | |
| 111 | + test("top-level functions never exceed maxChunkSize", async () => { | |
| 112 | + const body = Array.from( | |
| 113 | + { length: 150 }, | |
| 114 | + (_, i) => `const x${i} = ${i};`, | |
| 115 | + ).join("\n"); | |
| 116 | + const contents = ` | |
| 117 | +function f() { | |
| 118 | + ${body} | |
| 119 | +} | |
| 120 | + | |
| 121 | +function g() { | |
| 122 | + ${body} | |
| 123 | +} | |
| 124 | +`; | |
| 125 | + const max = 60; | |
| 126 | + const chunks = await collectContents("filename2.ts", contents, max); | |
| 127 | + const counts = await Promise.all(chunks.map((s) => countTokensAsync(s))); | |
| 128 | + counts.forEach((n) => expect(n).toBeLessThanOrEqual(max)); | |
| 129 | + }); | |
| 130 | +}); |
modified core/indexing/chunk/code.ts +36 −15
| @@ -128,25 +128,46 @@ | ||
| 128 | 128 | maxChunkSize: number, |
| 129 | 129 | ): Promise<string> { |
| 130 | 130 | const bodyNode = node.children[node.children.length - 1]; |
| 131 | - const funcText = | |
| 132 | - code.slice(node.startIndex, bodyNode.startIndex) + | |
| 133 | - collapsedReplacement(bodyNode); | |
| 134 | - | |
| 135 | - if ( | |
| 131 | + const collapsedBody = collapsedReplacement(bodyNode); | |
| 132 | + const signature = code.slice(node.startIndex, bodyNode.startIndex); | |
| 133 | + const funcText = signature + collapsedBody; | |
| 134 | + | |
| 135 | + const isInClass = | |
| 136 | 136 | node.parent && |
| 137 | 137 | ["block", "declaration_list"].includes(node.parent.type) && |
| 138 | 138 | node.parent.parent && |
| 139 | - ["class_definition", "impl_item"].includes(node.parent.parent.type) | |
| 140 | - ) { | |
| 141 | - // If inside a class, include the class header | |
| 142 | - const classNode = node.parent.parent; | |
| 143 | - const classBlock = node.parent; | |
| 144 | - return `${code.slice( | |
| 145 | - classNode.startIndex, | |
| 146 | - classBlock.startIndex, | |
| 147 | - )}...\n\n${" ".repeat(node.startPosition.column)}${funcText}`; | |
| 139 | + ["class_definition", "impl_item"].includes(node.parent.parent.type); | |
| 140 | + | |
| 141 | + if (isInClass) { | |
| 142 | + const classNode = node.parent!.parent!; | |
| 143 | + const classBlock = node.parent!; | |
| 144 | + const classHeader = code.slice(classNode.startIndex, classBlock.startIndex); | |
| 145 | + const indent = " ".repeat(node.startPosition.column); | |
| 146 | + const combined = `${classHeader}...\n\n${indent}${funcText}`; | |
| 147 | + | |
| 148 | + if ((await countTokensAsync(combined)) <= maxChunkSize) { | |
| 149 | + return combined; | |
| 150 | + } | |
| 151 | + if ((await countTokensAsync(funcText)) <= maxChunkSize) { | |
| 152 | + return funcText; | |
| 153 | + } | |
| 154 | + const firstLine = signature.split("\n")[0] ?? ""; | |
| 155 | + const minimal = `${firstLine} ${collapsedBody}`; | |
| 156 | + if ((await countTokensAsync(minimal)) <= maxChunkSize) { | |
| 157 | + return minimal; | |
| 158 | + } | |
| 159 | + return collapsedBody; | |
| 160 | + } | |
| 161 | + | |
| 162 | + if ((await countTokensAsync(funcText)) <= maxChunkSize) { | |
| 163 | + return funcText; | |
| 164 | + } | |
| 165 | + const firstLine = signature.split("\n")[0] ?? ""; | |
| 166 | + const minimal = `${firstLine} ${collapsedBody}`; | |
| 167 | + if ((await countTokensAsync(minimal)) <= maxChunkSize) { | |
| 168 | + return minimal; | |
| 148 | 169 | } |
| 149 | - return funcText; | |
| 170 | + return collapsedBody; | |
| 150 | 171 | } |
| 151 | 172 | |
| 152 | 173 | const collapsedNodeConstructors: { |