"use strict";
/**
 * Paste-Your-Agent Parser (W4.3 of F-NEW-PRODUCT-AGENT-VISIBILITY-CLUSTER-2026-05-12).
 *
 * Single-text entry point for the landing-page funnel surface: customer
 * pastes an agent definition (Claude Code agent.md / OpenAI Agents SDK
 * snippet / LangChain Tool registration / MCP config), and we produce an
 * AgentManifest the OWASP scanner + auto-pack generator can consume.
 *
 * Designed to run BROWSER-SIDE (zero data exfiltration). This module has
 * no fs/node-builtin imports so it bundles cleanly for a browser context.
 * The user's pasted text never leaves the page.
 *
 * Format detection:
 *   - Frontmatter `---` + `tools:` field -> claude-code
 *   - `new Agent({ name, tools: [...] })` -> openai-agents
 *   - `new (Dynamic)?Tool({ name })` -> langchain
 *   - JSON with `mcpServers` -> mcp-config
 *
 * @connexum/ai-governance  F-NEW-PRODUCT-PASTE-YOUR-AGENT
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseAgentDefinition = parseAgentDefinition;
function parseAgentDefinition(text, hint) {
    const trimmed = text.trim();
    if (trimmed.length === 0) {
        return { format: 'unknown', manifest: null, detail: 'empty input' };
    }
    // Hint short-circuit: caller knows the format.
    if (hint && hint !== 'unknown') {
        const m = parseAs(text, hint);
        if (m)
            return { format: hint, manifest: m };
    }
    // Auto-detect in priority order. Stop on first confident match.
    if (trimmed.startsWith('---')) {
        const m = parseClaudeCode(text);
        if (m)
            return { format: 'claude-code', manifest: m };
    }
    if (trimmed.startsWith('{')) {
        const m = parseMcpConfig(text);
        if (m)
            return { format: 'mcp-config', manifest: m };
    }
    if (/new\s+Agent\s*\(/.test(text)) {
        const m = parseOpenaiAgents(text);
        if (m)
            return { format: 'openai-agents', manifest: m };
    }
    if (/new\s+(?:Dynamic)?Tool\s*\(/.test(text)) {
        const m = parseLangChain(text);
        if (m)
            return { format: 'langchain', manifest: m };
    }
    return {
        format: 'unknown',
        manifest: null,
        detail: 'no agent-definition format detected. Supported: Claude Code agent.md frontmatter, ' +
            'OpenAI Agents SDK (new Agent({...})), LangChain (new Tool({...})), MCP config JSON.',
    };
}
function parseAs(text, format) {
    switch (format) {
        case 'claude-code': return parseClaudeCode(text);
        case 'openai-agents': return parseOpenaiAgents(text);
        case 'langchain': return parseLangChain(text);
        case 'mcp-config': return parseMcpConfig(text);
        default: return null;
    }
}
function parseClaudeCode(text) {
    const m = text.match(/^---\s*\n([\s\S]*?)\n---\s*(?:\n([\s\S]*))?$/);
    if (!m)
        return null;
    const frontmatter = m[1];
    const body = m[2] ?? '';
    const name = matchKey(frontmatter, 'name') ?? 'pasted-agent';
    const toolsLine = matchKey(frontmatter, 'tools');
    const tools = toolsLine
        ? toolsLine.split(',').map((t) => t.trim()).filter((t) => t.length > 0)
        : [];
    return {
        agentId: `claude-code:${name}`,
        name,
        tools,
        systemPrompt: body.slice(0, 2000),
        llmProvider: 'anthropic/claude-code',
        hooks: { preToolUse: false, postToolUse: false, postResponse: false },
    };
}
function parseOpenaiAgents(text) {
    const m = text.match(/new\s+Agent\s*\(\s*\{([\s\S]*?)\}\s*\)/);
    if (!m)
        return null;
    const block = m[1];
    const nameMatch = block.match(/name\s*:\s*['"`]([^'"`]+)['"`]/);
    const toolsMatch = block.match(/tools\s*:\s*\[([\s\S]*?)\]/);
    const name = nameMatch ? nameMatch[1] : 'pasted-agent';
    const tools = [];
    if (toolsMatch) {
        const idRe = /([A-Za-z_][A-Za-z0-9_]*)/g;
        let tm;
        while ((tm = idRe.exec(toolsMatch[1])) !== null)
            tools.push(tm[1]);
    }
    return {
        agentId: `openai-agents:${name}`,
        name,
        tools,
        llmProvider: 'openai/agents-sdk',
        hooks: { preToolUse: false, postToolUse: false, postResponse: false },
    };
}
function parseLangChain(text) {
    const re = /new\s+(?:Dynamic)?Tool\s*\(\s*\{[\s\S]*?name\s*:\s*['"`]([^'"`]+)['"`]/g;
    const tools = [];
    let m;
    while ((m = re.exec(text)) !== null)
        tools.push(m[1]);
    if (tools.length === 0)
        return null;
    return {
        agentId: 'langchain:pasted-agent',
        name: 'pasted LangChain agent',
        tools,
        llmProvider: 'langchain',
        hooks: { preToolUse: false, postToolUse: false, postResponse: false },
    };
}
function parseMcpConfig(text) {
    try {
        const parsed = JSON.parse(text);
        if (!parsed.mcpServers)
            return null;
        const servers = Object.entries(parsed.mcpServers).map(([n, def]) => def.url ?? `stdio:${def.command ?? n}`);
        return {
            agentId: 'mcp-config:pasted-agent',
            name: 'pasted MCP-using agent',
            tools: Object.keys(parsed.mcpServers).map((n) => `mcp__${n}`),
            mcpServers: servers,
            hooks: { preToolUse: false, postToolUse: false, postResponse: false },
        };
    }
    catch {
        return null;
    }
}
function matchKey(frontmatter, key) {
    const re = new RegExp(`^${key}\\s*:\\s*(.+)$`, 'm');
    const m = frontmatter.match(re);
    return m ? m[1].trim() : undefined;
}
//# sourceMappingURL=paste-your-agent.js.map