Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/eslint-suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@
},
"services/code-index/processors/__tests__/parser.spec.ts": {
"@typescript-eslint/no-explicit-any": {
"count": 22
"count": 21
}
},
"services/code-index/processors/__tests__/scanner.spec.ts": {
Expand Down
66 changes: 61 additions & 5 deletions src/services/code-index/processors/__tests__/parser.spec.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

171 changes: 88 additions & 83 deletions src/services/code-index/processors/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { sanitizeErrorMessage } from "../shared/validation-helpers"
export class CodeParser implements ICodeParser {
private loadedParsers: LanguageParser = {}
private pendingLoads: Map<string, Promise<LanguageParser>> = new Map()
private loggedParserFailures = new Set<string>()
// Markdown files are now supported using the custom markdown parser
// which extracts headers and sections for semantic indexing

Expand Down Expand Up @@ -113,13 +114,8 @@ export class CodeParser implements ICodeParser {
try {
await pendingLoad
} catch (error) {
console.error(`Error in pending parser load for ${filePath}:`, error)
TelemetryService.instance.captureEvent(TelemetryEventName.CODE_INDEX_ERROR, {
error: sanitizeErrorMessage(error instanceof Error ? error.message : String(error)),
stack: error instanceof Error ? sanitizeErrorMessage(error.stack || "") : undefined,
location: "parseContent:loadParser",
})
return []
this.recordParserLoadFailure(ext, filePath, error)
return this._performFallbackChunking(filePath, content, fileHash, seenSegmentHashes)
}
} else {
const loadPromise = loadRequiredLanguageParsers([filePath])
Expand All @@ -130,13 +126,8 @@ export class CodeParser implements ICodeParser {
this.loadedParsers = { ...this.loadedParsers, ...newParsers }
}
} catch (error) {
console.error(`Error loading language parser for ${filePath}:`, error)
TelemetryService.instance.captureEvent(TelemetryEventName.CODE_INDEX_ERROR, {
error: sanitizeErrorMessage(error instanceof Error ? error.message : String(error)),
stack: error instanceof Error ? sanitizeErrorMessage(error.stack || "") : undefined,
location: "parseContent:loadParser",
})
return []
this.recordParserLoadFailure(ext, filePath, error)
return this._performFallbackChunking(filePath, content, fileHash, seenSegmentHashes)
} finally {
this.pendingLoads.delete(ext)
}
Expand All @@ -150,83 +141,97 @@ export class CodeParser implements ICodeParser {
}

const tree = language.parser.parse(content)

// We don't need to get the query string from languageQueries since it's already loaded
// in the language object
const captures = tree ? language.query.captures(tree.rootNode) : []

// Check if captures are empty
if (captures.length === 0) {
if (content.length >= MIN_BLOCK_CHARS) {
// Perform fallback chunking if content is large enough
const blocks = this._performFallbackChunking(filePath, content, fileHash, seenSegmentHashes)
return blocks
} else {
// Return empty if content is too small for fallback
return []
try {
// We don't need to get the query string from languageQueries since it's already loaded
// in the language object
const captures = tree ? language.query.captures(tree.rootNode) : []

// Check if captures are empty
if (captures.length === 0) {
if (content.length >= MIN_BLOCK_CHARS) {
// Perform fallback chunking if content is large enough
const blocks = this._performFallbackChunking(filePath, content, fileHash, seenSegmentHashes)
return blocks
} else {
// Return empty if content is too small for fallback
return []
}
}
}

const results: CodeBlock[] = []

// Process captures if not empty
const queue: Node[] = Array.from(captures).map((capture) => capture.node)

while (queue.length > 0) {
const currentNode = queue.shift()!
// const lineSpan = currentNode.endPosition.row - currentNode.startPosition.row + 1 // Removed as per lint error

// Check if the node meets the minimum character requirement
if (currentNode.text.length >= MIN_BLOCK_CHARS) {
// If it also exceeds the maximum character limit, try to break it down
if (currentNode.text.length > MAX_BLOCK_CHARS * MAX_CHARS_TOLERANCE_FACTOR) {
if (currentNode.children.filter((child) => child !== null).length > 0) {
// If it has children, process them instead
queue.push(...currentNode.children.filter((child) => child !== null))
const results: CodeBlock[] = []

// Process captures if not empty
const queue: Node[] = Array.from(captures).map((capture) => capture.node)

while (queue.length > 0) {
const currentNode = queue.shift()!
// const lineSpan = currentNode.endPosition.row - currentNode.startPosition.row + 1 // Removed as per lint error

// Check if the node meets the minimum character requirement
if (currentNode.text.length >= MIN_BLOCK_CHARS) {
// If it also exceeds the maximum character limit, try to break it down
if (currentNode.text.length > MAX_BLOCK_CHARS * MAX_CHARS_TOLERANCE_FACTOR) {
if (currentNode.children.filter((child) => child !== null).length > 0) {
// If it has children, process them instead
queue.push(...currentNode.children.filter((child) => child !== null))
} else {
// If it's a leaf node, chunk it
const chunkedBlocks = this._chunkLeafNodeByLines(
currentNode,
filePath,
fileHash,
seenSegmentHashes,
)
results.push(...chunkedBlocks)
}
} else {
// If it's a leaf node, chunk it
const chunkedBlocks = this._chunkLeafNodeByLines(
currentNode,
filePath,
fileHash,
seenSegmentHashes,
)
results.push(...chunkedBlocks)
}
} else {
// Node meets min chars and is within max chars, create a block
const identifier =
currentNode.childForFieldName("name")?.text ||
currentNode.children.find((c) => c?.type === "identifier")?.text ||
null
const type = currentNode.type
const start_line = currentNode.startPosition.row + 1
const end_line = currentNode.endPosition.row + 1
const content = currentNode.text
const contentPreview = content.slice(0, 100)
const segmentHash = createHash("sha256")
.update(`${filePath}-${start_line}-${end_line}-${content.length}-${contentPreview}`)
.digest("hex")

if (!seenSegmentHashes.has(segmentHash)) {
seenSegmentHashes.add(segmentHash)
results.push({
file_path: filePath,
identifier,
type,
start_line,
end_line,
content,
segmentHash,
fileHash,
})
// Node meets min chars and is within max chars, create a block
const identifier =
currentNode.childForFieldName("name")?.text ||
currentNode.children.find((c) => c?.type === "identifier")?.text ||
null
const type = currentNode.type
const start_line = currentNode.startPosition.row + 1
const end_line = currentNode.endPosition.row + 1
const content = currentNode.text
const contentPreview = content.slice(0, 100)
const segmentHash = createHash("sha256")
.update(`${filePath}-${start_line}-${end_line}-${content.length}-${contentPreview}`)
.digest("hex")

if (!seenSegmentHashes.has(segmentHash)) {
seenSegmentHashes.add(segmentHash)
results.push({
file_path: filePath,
identifier,
type,
start_line,
end_line,
content,
segmentHash,
fileHash,
})
}
}
}
// Nodes smaller than minBlockChars are ignored
}
// Nodes smaller than minBlockChars are ignored

return results
} finally {
tree?.delete()
}
}

return results
private recordParserLoadFailure(ext: string, filePath: string, error: unknown): void {
if (this.loggedParserFailures.has(ext)) return
this.loggedParserFailures.add(ext)
console.error(`Error loading language parser for ${filePath}:`, error)
TelemetryService.instance.captureEvent(TelemetryEventName.CODE_INDEX_ERROR, {
error: sanitizeErrorMessage(error instanceof Error ? error.message : String(error)),
stack: error instanceof Error ? sanitizeErrorMessage(error.stack || "") : undefined,
location: "parseContent:loadParser",
})
}

/**
Expand Down
72 changes: 72 additions & 0 deletions src/services/tree-sitter/__tests__/languageParser.spec.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading