UNPKG

ken-you-code

Version:

Connect your codebase to Kimi: Ultra-fast AI code analysis with Kimi-K2 model via MCP

193 lines (178 loc) 8.16 kB
import OpenAI from 'openai'; import { config } from '../config/index.js'; import { FileOperations } from './fileOperations.js'; export class SpecializedTaskHandler { openai; constructor() { this.openai = new OpenAI({ baseURL: 'https://openrouter.ai/api/v1', apiKey: config.openRouterApiKey, }); } getTaskParameters(taskType) { const taskParams = { debug: { temperature: 0.0, max_tokens: 4000, top_p: 0.8, frequency_penalty: 0.2, presence_penalty: 0.0, }, analyze: { temperature: 0.1, max_tokens: 8000, top_p: 0.9, frequency_penalty: 0.0, presence_penalty: 0.1, }, review: { temperature: 0.2, max_tokens: 6000, top_p: 0.9, frequency_penalty: 0.1, presence_penalty: 0.15, }, design: { temperature: 0.7, max_tokens: 5000, top_p: 0.98, frequency_penalty: 0.0, presence_penalty: 0.3, }, second_opinion: { temperature: 0.3, max_tokens: 5000, top_p: 0.9, frequency_penalty: 0.1, presence_penalty: 0.2, }, reality_check: { temperature: 0.2, max_tokens: 4000, top_p: 0.85, frequency_penalty: 0.15, presence_penalty: 0.1, }, synthesis: { temperature: 0.3, max_tokens: 6000, top_p: 0.9, frequency_penalty: 0.0, presence_penalty: 0.25, }, }; return taskParams[taskType]; } buildSystemPrompt(taskType) { const tasks = { debug: `Claude Code needs your debugging expertise as a second opinion. I've done initial analysis but need your specialized knowledge to: • Find issues I missed or overlooked • Suggest alternative debugging strategies I haven't considered • Dive deeper into complex technical problems beyond my initial assessment • Identify non-obvious root causes or edge cases Don't repeat basic debugging steps. Focus on advanced techniques, deeper analysis, and what an expert debugger would catch that others miss.`, analyze: `Claude Code needs your architectural expertise for comprehensive code analysis. I've done surface-level review but need your deep technical insight: • Advanced code quality issues I might not have spotted • Complex architectural patterns and their implications • Performance bottlenecks requiring specialized knowledge • Modern patterns and practices I should consider • Technical debt and maintainability concerns at scale • Optimization opportunities and refactoring strategies Go beyond basic analysis. Provide the kind of insights that come from years of architectural experience.`, review: `Claude Code needs your expert code review as validation and enhancement of my initial assessment. I'll share my findings - your job is to: • Identify security vulnerabilities I missed • Spot subtle bugs or logic errors I overlooked • Suggest improvements to my proposed solutions • Challenge my assumptions with alternative approaches • Provide security-focused insights beyond general code review • Catch style inconsistencies and testing gaps I missed Act as a senior reviewer who catches what others miss and provides expert-level security guidance.`, design: `Claude Code needs your UX/UI expertise for technical design decisions. I'm strong on implementation but need your design knowledge for: • User experience patterns and best practices • Accessibility requirements and implementation strategies • Modern UI/UX approaches I should consider • Design system patterns and component architecture • Balancing technical constraints with user needs Provide design expertise that helps me implement user-focused, accessible solutions.`, second_opinion: `Claude Code has reached a conclusion but needs you to challenge it. I'll share my analysis and proposed solution. Your job: Play devil's advocate. What could go wrong? What am I not considering? Where might I be wrong? Challenge my assumptions and logic. Don't just agree - find the flaws, edge cases, and alternative perspectives I need to consider.`, reality_check: `Claude Code has a theoretical solution but needs a reality check on practical implementation. I'll share my proposed approach. Your job: Assess real-world feasibility: • What implementation challenges will I face? • What dependencies or constraints am I missing? • How would this perform in production? • What could break this in real scenarios? Give me the practical perspective I need before implementing.`, synthesis: `Claude Code has multiple potential solutions and needs help synthesizing them. I'll provide several approaches I've considered. Your expertise: Combine the best parts of different approaches into a unified solution, or help me choose the optimal path by analyzing trade-offs. Focus on practical synthesis that leverages strengths while mitigating weaknesses.`, }; return tasks[taskType]; } // OPTIMIZED: Parallel file loading instead of sequential async loadFileContexts(filePaths) { const filePromises = filePaths.map(async (filePath) => { try { const fileResult = await FileOperations.readFileSecurely(filePath); return { path: filePath, content: fileResult.content, }; } catch (error) { return { path: filePath, content: `ERROR: ${error instanceof Error ? error.message : 'Unknown error'}`, }; } }); return Promise.all(filePromises); } buildUserPrompt(request, fileContexts) { // Claude principle: Minimal, direct prompts let prompt = request.context; if (fileContexts.length > 0) { prompt += '\n\nFiles:\n'; for (const file of fileContexts) { prompt += `${file.path}:\n${file.content}\n\n`; } } return prompt; } async executeTask(request) { const model = request.model || config.defaultModel; const filePaths = request.files || []; try { // OPTIMIZED: Load files in parallel const fileContexts = await this.loadFileContexts(filePaths); // OPTIMIZED: Much shorter system prompts const systemPrompt = this.buildSystemPrompt(request.task_type); const userPrompt = this.buildUserPrompt(request, fileContexts); // Get task-specific optimized parameters const taskParams = this.getTaskParameters(request.task_type); const completion = await this.openai.chat.completions.create({ model, messages: [ { role: 'system', content: systemPrompt }, { role: 'user', content: userPrompt }, ], ...taskParams, }); const response = completion.choices[0]?.message?.content || 'No response generated'; const usage = completion.usage; return { result: response, model, tokenUsage: { prompt: usage?.prompt_tokens || 0, completion: usage?.completion_tokens || 0, total: usage?.total_tokens || 0, }, }; } catch (error) { throw new Error(`Task execution failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } }