import { fileURLToPath as __fileURLToPath } from 'url';
import { dirname as __pathDirname } from 'path';
const __filename = __fileURLToPath(import.meta.url);
const __dirname = __pathDirname(__filename);
import { logger } from "../../core/monitoring/logger.js";
import { exec } from "child_process";
import { promisify } from "util";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
const execAsync = promisify(exec);
class ClaudeCodeSubagentClient {
  tempDir;
  activeSubagents = /* @__PURE__ */ new Map();
  mockMode;
  constructor(mockMode = true) {
    this.mockMode = mockMode;
    this.tempDir = path.join(os.tmpdir(), "stackmemory-rlm");
    if (!fs.existsSync(this.tempDir)) {
      fs.mkdirSync(this.tempDir, { recursive: true });
    }
    logger.info("Claude Code Subagent Client initialized", {
      tempDir: this.tempDir,
      mockMode: this.mockMode
    });
  }
  /**
   * Execute a subagent task using Claude Code's Task tool
   * This will spawn a new Claude instance with specific instructions
   */
  async executeSubagent(request) {
    const startTime = Date.now();
    const subagentId = `${request.type}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
    logger.info(`Spawning ${request.type} subagent`, {
      subagentId,
      task: request.task.slice(0, 100),
      mockMode: this.mockMode
    });
    if (this.mockMode) {
      return this.getMockResponse(request, startTime, subagentId);
    }
    try {
      const prompt = this.buildSubagentPrompt(request);
      const contextFile = path.join(this.tempDir, `${subagentId}-context.json`);
      await fs.promises.writeFile(
        contextFile,
        JSON.stringify(request.context, null, 2)
      );
      const resultFile = path.join(this.tempDir, `${subagentId}-result.json`);
      const taskCommand = this.buildTaskCommand(request, prompt, contextFile, resultFile);
      const result = await this.executeTaskTool(taskCommand, request.timeout);
      let subagentResult = {};
      if (fs.existsSync(resultFile)) {
        const resultContent = await fs.promises.readFile(resultFile, "utf-8");
        try {
          subagentResult = JSON.parse(resultContent);
        } catch (e) {
          subagentResult = { rawOutput: resultContent };
        }
      }
      this.cleanup(subagentId);
      return {
        success: true,
        result: subagentResult,
        output: result.stdout,
        duration: Date.now() - startTime,
        subagentType: request.type,
        tokens: this.estimateTokens(prompt + JSON.stringify(subagentResult))
      };
    } catch (error) {
      logger.error(`Subagent execution failed: ${request.type}`, { error, subagentId });
      return {
        success: false,
        result: null,
        error: error.message,
        duration: Date.now() - startTime,
        subagentType: request.type
      };
    }
  }
  /**
   * Execute multiple subagents in parallel
   */
  async executeParallel(requests) {
    logger.info(`Executing ${requests.length} subagents in parallel`);
    const promises = requests.map((request) => this.executeSubagent(request));
    const results = await Promise.allSettled(promises);
    return results.map((result, index) => {
      if (result.status === "fulfilled") {
        return result.value;
      } else {
        return {
          success: false,
          result: null,
          error: result.reason?.message || "Unknown error",
          duration: 0,
          subagentType: requests[index].type
        };
      }
    });
  }
  /**
   * Build subagent prompt based on type
   */
  buildSubagentPrompt(request) {
    const prompts = {
      planning: `You are a Planning Subagent. Your role is to decompose complex tasks into manageable subtasks.
        
        Task: ${request.task}
        
        Instructions:
        1. Analyze the task and identify all components
        2. Create a dependency graph of subtasks
        3. Assign appropriate agent types to each subtask
        4. Consider parallel execution opportunities
        5. Include comprehensive testing at each stage
        
        Context is available in the provided file.
        
        Output a JSON structure with the task decomposition.`,
      code: `You are a Code Generation Subagent. Your role is to implement high-quality, production-ready code.
        
        Task: ${request.task}
        
        Instructions:
        1. Write clean, maintainable code
        2. Follow project conventions (check context)
        3. Include comprehensive error handling
        4. Add clear comments for complex logic
        5. Ensure code is testable
        
        Context and requirements are in the provided file.
        
        Output the implementation code.`,
      testing: `You are a Testing Subagent specializing in comprehensive test generation.
        
        Task: ${request.task}
        
        Instructions:
        1. Generate unit tests for all functions/methods
        2. Create integration tests for API endpoints
        3. Add E2E tests for critical user flows
        4. Include edge cases and error scenarios
        5. Ensure high code coverage (aim for 100%)
        6. Validate that all tests pass
        
        Context and code to test are in the provided file.
        
        Output a complete test suite.`,
      linting: `You are a Linting Subagent ensuring code quality and standards.
        
        Task: ${request.task}
        
        Instructions:
        1. Check for syntax errors and type issues
        2. Verify code formatting and style
        3. Identify security vulnerabilities
        4. Find performance anti-patterns
        5. Detect unused imports and dead code
        6. Provide specific fixes for each issue
        
        Code to analyze is in the context file.
        
        Output a JSON report with issues and fixes.`,
      review: `You are a Code Review Subagent performing thorough multi-stage reviews.
        
        Task: ${request.task}
        
        Instructions:
        1. Evaluate architecture and design patterns
        2. Assess code quality and maintainability
        3. Check performance implications
        4. Review security considerations
        5. Verify test coverage adequacy
        6. Suggest specific improvements with examples
        7. Rate quality on a 0-1 scale
        
        Code and context are in the provided file.
        
        Output a detailed review with quality score and improvements.`,
      improve: `You are an Improvement Subagent enhancing code based on reviews.
        
        Task: ${request.task}
        
        Instructions:
        1. Implement all suggested improvements
        2. Refactor for better architecture
        3. Optimize performance bottlenecks
        4. Enhance error handling
        5. Improve code clarity and documentation
        6. Add missing test cases
        7. Ensure backward compatibility
        
        Review feedback and code are in the context file.
        
        Output the improved code.`,
      context: `You are a Context Retrieval Subagent finding relevant information.
        
        Task: ${request.task}
        
        Instructions:
        1. Search project codebase for relevant code
        2. Find similar implementations
        3. Locate relevant documentation
        4. Identify dependencies and patterns
        5. Retrieve best practices
        
        Search parameters are in the context file.
        
        Output relevant context snippets.`,
      publish: `You are a Publishing Subagent handling releases and deployments.
        
        Task: ${request.task}
        
        Instructions:
        1. Prepare package for publishing
        2. Update version numbers
        3. Generate changelog
        4. Create GitHub release
        5. Publish to NPM if applicable
        6. Update documentation
        
        Release details are in the context file.
        
        Output the release plan and commands.`
    };
    return request.systemPrompt || prompts[request.type] || prompts.planning;
  }
  /**
   * Build Task tool command
   * This creates a command that Claude Code's Task tool can execute
   */
  buildTaskCommand(request, prompt, contextFile, resultFile) {
    const scriptContent = `
#!/bin/bash
# Subagent execution script for ${request.type}

# Read context
CONTEXT=$(cat "${contextFile}")

# Execute task based on type
case "${request.type}" in
  "testing")
    # For testing subagent, actually run tests
    echo "Generating and running tests..."
    # The subagent will generate test files and run them
    ;;
  "linting")
    # For linting subagent, run actual linters
    echo "Running linters..."
    npm run lint || true
    ;;
  "code")
    # For code generation, create implementation files
    echo "Generating implementation..."
    ;;
  *)
    # Default behavior
    echo "Executing ${request.type} task..."
    ;;
esac

# Write result
echo '{"status": "completed", "type": "${request.type}"}' > "${resultFile}"
`;
    const scriptFile = path.join(this.tempDir, `${request.type}-script.sh`);
    fs.writeFileSync(scriptFile, scriptContent);
    fs.chmodSync(scriptFile, "755");
    return scriptFile;
  }
  /**
   * Execute via Task tool (simulated for now)
   * In production, this would use Claude Code's actual Task tool API
   */
  async executeTaskTool(command, timeout) {
    try {
      const result = await execAsync(command, {
        timeout: timeout || 3e5,
        // 5 minutes default
        maxBuffer: 10 * 1024 * 1024
        // 10MB buffer
      });
      return result;
    } catch (error) {
      if (error.killed || error.signal === "SIGTERM") {
        throw new Error(`Subagent timeout after ${timeout}ms`);
      }
      throw error;
    }
  }
  /**
   * Get mock response for testing
   */
  async getMockResponse(request, startTime, subagentId) {
    await new Promise((resolve) => setTimeout(resolve, Math.random() * 20 + 10));
    const mockResponses = {
      planning: {
        tasks: [
          { id: "task-1", name: "Analyze requirements", type: "analysis" },
          { id: "task-2", name: "Design solution", type: "design" },
          { id: "task-3", name: "Implement solution", type: "implementation" }
        ],
        dependencies: [],
        estimated_time: 300
      },
      code: {
        implementation: `function greetUser(name: string): string {
  if (!name || typeof name !== 'string') {
    throw new Error('Invalid name parameter');
  }
  return \`Hello, \${name}!\`;
}`,
        files_modified: ["src/greet.ts"],
        lines_added: 6,
        lines_removed: 0
      },
      testing: {
        tests: [
          {
            name: "greetUser should return greeting",
            code: `test('greetUser should return greeting', () => {
  expect(greetUser('Alice')).toBe('Hello, Alice!');
});`,
            type: "unit"
          }
        ],
        coverage: { lines: 100, branches: 100, functions: 100 }
      },
      linting: {
        issues: [],
        fixes: [],
        passed: true
      },
      review: {
        quality: 0.85,
        issues: [
          "Consider adding JSDoc comments",
          "Could add more edge case tests"
        ],
        suggestions: [
          "Add documentation for the function",
          "Consider adding internationalization support",
          "Add performance tests for large inputs"
        ],
        improvements: []
      },
      improve: {
        improved_code: `/**
 * Greets a user with their name
 * @param name - The name of the user to greet
 * @returns A greeting message
 * @throws {Error} If name is invalid
 */
function greetUser(name: string): string {
  if (!name || typeof name !== 'string') {
    throw new Error('Invalid name parameter: name must be a non-empty string');
  }
  return \`Hello, \${name}!\`;
}`,
        changes_made: [
          "Added JSDoc documentation",
          "Improved error message"
        ]
      },
      context: {
        relevant_files: ["src/greet.ts", "test/greet.test.ts"],
        patterns: ["greeting functions", "input validation"],
        dependencies: []
      },
      publish: {
        version: "1.0.0",
        changelog: "Initial release",
        published: false,
        reason: "Mock mode - no actual publishing"
      }
    };
    const result = mockResponses[request.type] || {};
    return {
      success: true,
      result,
      output: `Mock ${request.type} subagent completed successfully`,
      duration: Date.now() - startTime,
      subagentType: request.type,
      tokens: this.estimateTokens(JSON.stringify(result))
    };
  }
  /**
   * Estimate token usage
   */
  estimateTokens(text) {
    return Math.ceil(text.length / 4);
  }
  /**
   * Cleanup temporary files
   */
  cleanup(subagentId) {
    const patterns = [
      `${subagentId}-context.json`,
      `${subagentId}-result.json`,
      `${subagentId}-script.sh`
    ];
    for (const pattern of patterns) {
      const filePath = path.join(this.tempDir, pattern);
      if (fs.existsSync(filePath)) {
        try {
          fs.unlinkSync(filePath);
        } catch (e) {
        }
      }
    }
  }
  /**
   * Create a mock Task tool response for development
   * This simulates what Claude Code's Task tool would return
   */
  async mockTaskToolExecution(request) {
    const startTime = Date.now();
    await new Promise((resolve) => setTimeout(resolve, 1e3 + Math.random() * 2e3));
    const mockResponses = {
      planning: {
        tasks: [
          { id: "1", type: "analyze", description: "Analyze requirements" },
          { id: "2", type: "implement", description: "Implement solution" },
          { id: "3", type: "test", description: "Test implementation" },
          { id: "4", type: "review", description: "Review and improve" }
        ],
        dependencies: { "2": ["1"], "3": ["2"], "4": ["3"] }
      },
      code: {
        implementation: `
export class Solution {
  constructor(private config: any) {}
  
  async execute(input: string): Promise<string> {
    // Implementation generated by Code subagent
    return this.process(input);
  }
  
  private process(input: string): string {
    return \`Processed: \${input}\`;
  }
}`,
        files: ["src/solution.ts"]
      },
      testing: {
        tests: `
describe('Solution', () => {
  it('should process input correctly', () => {
    const solution = new Solution({});
    const result = solution.execute('test');
    expect(result).toBe('Processed: test');
  });
  
  it('should handle edge cases', () => {
    // Edge case tests
  });
});`,
        coverage: { lines: 95, branches: 88, functions: 100 }
      },
      review: {
        quality: 0.82,
        issues: [
          { severity: "high", message: "Missing error handling" },
          { severity: "medium", message: "Could improve type safety" }
        ],
        suggestions: [
          "Add try-catch blocks",
          "Use stricter TypeScript types",
          "Add input validation"
        ]
      }
    };
    return {
      success: true,
      result: mockResponses[request.type] || { status: "completed" },
      output: `Mock ${request.type} subagent completed successfully`,
      duration: Date.now() - startTime,
      subagentType: request.type,
      tokens: Math.floor(Math.random() * 5e3) + 1e3
    };
  }
  /**
   * Get active subagent statistics
   */
  getStats() {
    return {
      activeSubagents: this.activeSubagents.size,
      tempDir: this.tempDir
    };
  }
  /**
   * Cleanup all resources
   */
  async cleanupAll() {
    for (const [id, controller] of this.activeSubagents) {
      controller.abort();
    }
    this.activeSubagents.clear();
    if (fs.existsSync(this.tempDir)) {
      const files = await fs.promises.readdir(this.tempDir);
      for (const file of files) {
        await fs.promises.unlink(path.join(this.tempDir, file));
      }
    }
    logger.info("Claude Code Subagent Client cleaned up");
  }
}
export {
  ClaudeCodeSubagentClient
};
//# sourceMappingURL=subagent-client.js.map
