abyss-ai
Version:
Autonomous AI coding agent - enhanced OpenCode with autonomous capabilities
117 lines (95 loc) • 3.42 kB
text/typescript
import { ReasoningMode, type ProcessingContext, type ProcessingResult } from "../types/agent"
import { Log } from "../../util/log"
// Base Reasoning Processor Interface
export interface IReasoningProcessor {
readonly mode: ReasoningMode
process(code: string, context: ProcessingContext): Promise<ProcessingResult>
calculateConfidence(result: any): number
}
// Base Implementation
export abstract class BaseReasoningProcessor implements IReasoningProcessor {
abstract readonly mode: ReasoningMode
protected log = Log.create({ service: `reasoning-processor` })
abstract process(code: string, context: ProcessingContext): Promise<ProcessingResult>
abstract calculateConfidence(result: any): number
protected measureExecutionTime<T>(fn: () => Promise<T>): Promise<{ result: T; duration: number }> {
const start = Date.now()
return fn().then(result => ({
result,
duration: Date.now() - start
}))
}
protected assessComplexity(code: string): number {
// Basic complexity assessment
const lines = code.split('\n').length
const cyclomaticComplexity = this.calculateCyclomaticComplexity(code)
const nestingDepth = this.calculateNestingDepth(code)
return Math.min(1, (lines / 1000 + cyclomaticComplexity / 10 + nestingDepth / 5) / 3)
}
private calculateCyclomaticComplexity(code: string): number {
// Count decision points: if, while, for, case, catch, etc.
const patterns = [
/\bif\s*\(/g,
/\bwhile\s*\(/g,
/\bfor\s*\(/g,
/\bcase\s+/g,
/\bcatch\s*\(/g,
/\belse\s+if\b/g,
/\?\s*.*\s*:/g, // ternary
]
return patterns.reduce((complexity, pattern) => {
const matches = code.match(pattern)
return complexity + (matches ? matches.length : 0)
}, 1) // Start with 1 for the main path
}
private calculateNestingDepth(code: string): number {
let maxDepth = 0
let currentDepth = 0
for (const char of code) {
if (char === '{') {
currentDepth++
maxDepth = Math.max(maxDepth, currentDepth)
} else if (char === '}') {
currentDepth--
}
}
return maxDepth
}
protected analyzeCodeStructure(code: string) {
return {
lineCount: code.split('\n').length,
characterCount: code.length,
functions: this.extractFunctions(code),
classes: this.extractClasses(code),
imports: this.extractImports(code),
complexity: this.assessComplexity(code)
}
}
private extractFunctions(code: string): string[] {
const functionRegex = /(?:function\s+(\w+)|(\w+)\s*[=:]\s*(?:function|\([^)]*\)\s*=>))/g
const matches = []
let match
while ((match = functionRegex.exec(code)) !== null) {
matches.push(match[1] || match[2])
}
return matches.filter(Boolean)
}
private extractClasses(code: string): string[] {
const classRegex = /class\s+(\w+)/g
const matches = []
let match
while ((match = classRegex.exec(code)) !== null) {
matches.push(match[1])
}
return matches
}
private extractImports(code: string): string[] {
const importRegex = /(?:import\s+.*?\s+from\s+['"]([^'"]+)['"]|require\s*\(\s*['"]([^'"]+)['"]\s*\))/g
const matches = []
let match
while ((match = importRegex.exec(code)) !== null) {
matches.push(match[1] || match[2])
}
return matches
}
}