// tools/detect-agent.ts — Detect which AI agent is being used
// Uses skill-generator engine: detectAgentPlatformFromFiles(), getAgentConfig()
// SPEC-022: Detects by scanning actual config files on disk (not just conventions)
import { t, ti } from '../i18n/index.js';
import { knowledgeStore } from '../storage/index.js';
import { detectAgentPlatformFromFiles, detectAgentPlatform, getAgentConfig, } from '../engine/skill-generator.js';
export async function handleDetectAgent(args) {
    const { projectId } = args;
    const knowledge = await knowledgeStore.getKnowledge(projectId);
    if (!knowledge) {
        return {
            content: [
                {
                    type: 'text',
                    text: ti('errors.projectNotFound', { projectId }),
                },
            ],
            isError: true,
        };
    }
    // SPEC-022: Detect agent platform from actual config files on disk (primary method)
    let detection = await detectAgentPlatformFromFiles(knowledge.projectPath);
    // Fall back to convention-based detection if file scan found nothing
    if (detection.confidence === 0) {
        detection = await detectAgentPlatform(knowledge.conventions);
    }
    // Get agent config for the detected platform
    const agentConfig = await getAgentConfig(detection.platform);
    const result = {
        platform: detection.platform,
        confidence: detection.confidence,
        detectedFrom: detection.detectedFrom,
        capabilities: agentConfig.capabilities,
        message: detection.confidence > 0.5
            ? `Detected ${detection.platform} with ${Math.round(detection.confidence * 100)}% confidence.`
            : `Best guess: ${detection.platform} (low confidence: ${Math.round(detection.confidence * 100)}%). Consider configuring agent rules explicitly.`,
    };
    // Format output
    const lines = [
        `## ${t('tools.detect_agent.description')}`,
        '',
        `**Detected platform:** ${result.platform}`,
        `**Confidence:** ${Math.round(result.confidence * 100)}%`,
        '',
    ];
    if (result.detectedFrom.length > 0) {
        lines.push('### Detection Evidence');
        lines.push('');
        for (const evidence of result.detectedFrom) {
            lines.push(`- ${evidence}`);
        }
        lines.push('');
    }
    lines.push('### Platform Configuration');
    lines.push('');
    lines.push(`| Setting | Value |`);
    lines.push(`|---------|-------|`);
    lines.push(`| **Rules file** | \`${agentConfig.rulesFile}\` |`);
    if (agentConfig.skillsDir) {
        lines.push(`| **Skills directory** | \`${agentConfig.skillsDir}\` |`);
    }
    lines.push(`| **Format** | ${agentConfig.format} |`);
    lines.push(`| **MCP support** | ${agentConfig.mcpSupport ? 'Yes' : 'No'} |`);
    lines.push('');
    if (result.capabilities.length > 0) {
        lines.push('### Capabilities');
        lines.push('');
        for (const cap of result.capabilities) {
            lines.push(`- ${cap}`);
        }
        lines.push('');
    }
    lines.push(`**${result.message}**`);
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
    };
}
//# sourceMappingURL=detect-agent.js.map