code_helper
Explain, optimize, or simplify code snippets to improve understanding, performance, or readability using local AI processing.
Instructions
Explain, optimize, or simplify code snippets.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"action": "<action>",
"code": "<code>"
},
"name": "code_helper"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | Action: explain (code explanation), optimize (performance suggestions), simplify (reduce complexity) | |
| code | Yes | Code snippet to process | |
| language | No | Programming language (optional, auto-detected) | |
| level | No | For explain: detail level (default: intermediate) | |
| focus | No | For optimize: focus area (default: all) | |
| preserve | No | For simplify: features to preserve (optional) |
Implementation Reference
- src/tools/code-assistance.ts:254-308 (handler)One of the methods (explainCode) in CodeAssistanceTools that uses 'code_helper' as a tool identifier to the LLM.
async explainCode( code: string, options?: { language?: string; depth?: 'brief' | 'detailed' | 'comprehensive'; focusOn?: string; } ): Promise<ExplainCodeResult> { const depth = options?.depth ?? 'detailed'; const language = options?.language ?? 'code'; const prompt = `You are an expert code explainer. Explain the following ${language} code in plain English. Depth level: ${depth} ${options?.focusOn ? `Focus specifically on: ${options.focusOn}` : ''} Provide your response as JSON with these fields: { "explanation": "Clear explanation of what the code does", "keyPoints": ["Key point 1", "Key point 2"], "complexity": "Brief complexity assessment", "potentialIssues": ["Any potential issues or edge cases"] }`; try { const responseText = await this.llmWrapper.callToolLlm( 'code_helper', [ { role: 'system', content: prompt }, { role: 'user', content: `\`\`\`${language}\n${code}\n\`\`\`` }, ], { type: 'explain_code', language } ); const parsed = this.parseJsonResponse(responseText, { explanation: responseText, keyPoints: [], }); return { success: true, explanation: parsed.explanation || responseText, keyPoints: parsed.keyPoints || [], complexity: parsed.complexity, potentialIssues: parsed.potentialIssues, }; } catch (error) { return { success: false, explanation: '', keyPoints: [], error: error instanceof Error ? error.message : 'Unknown error', }; } } - src/tools/code-assistance.ts:317-378 (handler)Another method in CodeAssistanceTools (suggestOptimizations) that uses 'code_helper'.
async suggestOptimizations( code: string, options?: { language?: string; focusAreas?: Array<'performance' | 'memory' | 'readability' | 'algorithm'>; } ): Promise<SuggestOptimizationsResult> { const language = options?.language ?? 'code'; const focusAreas = options?.focusAreas ?? ['performance', 'memory', 'readability', 'algorithm']; const prompt = `You are an expert code optimizer. Analyze the following ${language} code and suggest optimizations. Focus on: ${focusAreas.join(', ')} Constraints: - Preserve semantics (same outputs for all inputs). Do NOT propose behavior-changing edits. - Call out edge cases (e.g., empty arrays/strings, null/undefined, error handling) and keep them correct. - If an optimization depends on assumptions, state them explicitly. Provide your response as JSON: { "suggestions": [ { "type": "performance|memory|readability|algorithm", "description": "What to optimize", "impact": "high|medium|low", "before": "Original code snippet (optional)", "after": "Optimized code snippet (optional)" } ], "summary": "Overall assessment" }`; try { const responseText = await this.llmWrapper.callToolLlm( 'code_helper', [ { role: 'system', content: prompt }, { role: 'user', content: `\`\`\`${language}\n${code}\n\`\`\`` }, ], { type: 'optimize_code', language } ); const parsed = this.parseJsonResponse(responseText, { suggestions: [], summary: responseText, }); return { success: true, suggestions: parsed.suggestions || [], summary: parsed.summary || '', }; } catch (error) { return { success: false, suggestions: [], summary: '', error: error instanceof Error ? error.message : 'Unknown error', }; } } - src/tools/code-assistance.ts:660-715 (handler)Another method in CodeAssistanceTools (simplifyCode) that uses 'code_helper'.
async simplifyCode( code: string, options?: { language?: string; preserveComments?: boolean; } ): Promise<SimplifyCodeResult> { const language = options?.language ?? 'code'; const prompt = `You are an expert at simplifying code. Rewrite this ${language} code to be cleaner, more readable, and easier to maintain while preserving its functionality. ${options?.preserveComments ? 'Preserve existing comments.' : ''} Provide your response as JSON: { "originalComplexity": "Brief assessment of original complexity", "simplifiedCode": "The simplified code", "simplifiedComplexity": "Brief assessment of new complexity", "changes": ["Description of each simplification made"] }`; try { const responseText = await this.llmWrapper.callToolLlm( 'code_helper', [ { role: 'system', content: prompt }, { role: 'user', content: `\`\`\`${language}\n${code}\n\`\`\`` }, ], { type: 'simplify_code', language } ); const parsed = this.parseJsonResponse(responseText, { originalComplexity: '', simplifiedCode: code, simplifiedComplexity: '', changes: [], }); return { success: true, originalComplexity: parsed.originalComplexity || '', simplifiedCode: parsed.simplifiedCode || code, simplifiedComplexity: parsed.simplifiedComplexity || '', changes: parsed.changes || [], }; } catch (error) { return { success: false, originalComplexity: '', simplifiedCode: '', simplifiedComplexity: '', changes: [], error: error instanceof Error ? error.message : 'Unknown error', }; } }