All files / src/llm-orchestration/action-handlers execute-code.handler.ts

89.09% Statements 49/55
75% Branches 24/32
83.33% Functions 5/6
88.67% Lines 47/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 1837x           7x 7x 7x   7x 7x 7x 7x 7x     7x 16x 16x 16x   16x                                                             2x           2x                       2x                               2x               10x 10x 10x 2x 2x   2x                 8x 8x 8x     8x     8x   8x     8x     8x               8x 1x 1x 1x 1x 7x 2x 2x 2x 2x   5x                         8x 8x   2x           8x                              
import { Injectable, Logger } from '@nestjs/common';
import { ActionHandler } from './action-handler.interface';
import {
  ActionExecutionResult,
  ToolMetadata,
} from '../llm-orchestration.interfaces';
import { ExecuteCodeArgsDto } from './dto/execute-code.args.dto';
import { plainToClass } from 'class-transformer';
import { validate } from 'class-validator';
 
import { generateToolCall, generateToolCallJson } from '../../utils';
import { execWithProcessGroupKill } from '../../utils/spawn-with-kill';
import * as path from 'path';
import * as fs from 'fs/promises';
import * as crypto from 'crypto';
 
@Injectable()
export class ExecuteCodeHandler implements ActionHandler {
  readonly toolName = 'execute_code';
  private readonly logger = new Logger(ExecuteCodeHandler.name);
  private readonly projectRoot: string =
    process.env.REPOBURG_PROJECT_PATH || process.cwd();
  private readonly tempDir: string = path.join(
    this.projectRoot,
    '.repoburg',
    'temp',
  );
 
  getMetadata(): ToolMetadata {
    return {
      name: this.toolName,
      description: this.getDefinition(true),
      arguments: [
        {
          name: 'code',
          type: 'string',
          description: 'The TypeScript code to execute.',
          required: true,
        },
      ],
    };
  }
 
  /**
   * Generates a tool call example in the specified format.
   * @param toolCall - The tool call object to format
   * @param useJson - If true, uses JSON format; otherwise uses XML-style format
   * @returns Formatted tool call string
   */
  private generateExample(
    toolCall: Record<string, any>,
    useJson: boolean = false,
  ): string {
    return useJson
      ? generateToolCallJson(toolCall)
      : generateToolCall(toolCall);
  }
 
  getDefinition(useJsonFormat: boolean = false): string {
    const example = this.generateExample(
      {
        tool_name: this.toolName,
        code: `
import * as fs from 'fs';
const files = fs.readdirSync('.');
console.log(files.join('\\n'));
`,
      },
      useJsonFormat,
    );
 
    const definition = `
-------------
### ${this.toolName}
  Executes the provided TypeScript code in a temporary environment.
  Useful for testing logic, verifying algorithms, or inspecting the system programmatically without permanently creating files.
  The code is executed using 'npx tsx', so you can use ES modules and standard Node.js APIs.
 
  CRITICAL: The code MUST be TypeScript. NEVER generate Python, Bash, or any other language.
 
  #### Parameters
  - "code": (string) The TypeScript code to execute.
 
  #### Example
:${example}
-------------
`;
    return `\n${definition.trim()}\n`;
  }
 
  async execute(
    args: { [key: string]: any },
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    _context: any,
  ): Promise<ActionExecutionResult> {
    const validatedArgs = plainToClass(ExecuteCodeArgsDto, args);
    const errors = await validate(validatedArgs);
    if (errors.length > 0) {
      const errorMessages = errors
        .map((err) => Object.values(err.constraints || {}).join(', '))
        .join('; ');
      return {
        status: 'FAILURE',
        summary: `Invalid arguments for ${this.toolName}.`,
        error_message: errorMessages,
        persisted_args: args,
        execution_log: { output: '', error_message: errorMessages },
      };
    }
 
    const { code } = validatedArgs;
    const randomId = crypto.randomBytes(8).toString('hex');
    const tempFilePath = path.join(this.tempDir, `exec-${randomId}.ts`);
 
    let unifiedOutput: string;
    let executionStatus: 'SUCCESS' | 'FAILURE' = 'SUCCESS';
    let errorDetails: string | undefined;
 
    try {
      // Ensure temp directory exists
      await fs.mkdir(this.tempDir, { recursive: true });
 
      // Write code to temp file
      await fs.writeFile(tempFilePath, code, 'utf-8');
 
      // Execute the code
      const result = await execWithProcessGroupKill(
        `npx tsx "${tempFilePath}"`,
        {
          cwd: this.projectRoot,
          timeout: 30000, // 30 seconds timeout
        },
      );
 
      if (result.killed) {
        executionStatus = 'FAILURE';
        unifiedOutput = `EXECUTION TIMED OUT after 30000ms\n\nSTDOUT:\n${result.stdout || 'N/A'}\n\nSTDERR:\n${result.stderr || 'N/A'}`;
        errorDetails = 'Code execution timed out after 30000ms.';
        this.logger.warn(`Code execution timed out`);
      } else if (result.exitCode !== 0) {
        executionStatus = 'FAILURE';
        unifiedOutput = `EXECUTION FAILED with exit code ${result.exitCode}\n\nSTDOUT:\n${result.stdout || 'N/A'}\n\nSTDERR:\n${result.stderr || 'N/A'}`;
        errorDetails = `Code execution failed with exit code ${result.exitCode}.`;
        this.logger.error(`Error executing code: ${unifiedOutput}`);
      } else {
        unifiedOutput =
          result.stdout || result.stderr || '(Script produced no output)';
      }
    } catch (error) {
      const execError = error as any;
      executionStatus = 'FAILURE';
      unifiedOutput = `EXECUTION FAILED: ${execError.message}\n\nSTDOUT:\n${
        execError.stdout || 'N/A'
      }\n\nSTDERR:\n${execError.stderr || 'N/A'}`;
      errorDetails = execError.message;
      this.logger.error(`Error executing code: ${unifiedOutput}`);
    } finally {
      // Clean up temp file
      try {
        await fs.unlink(tempFilePath);
      } catch (cleanupErr) {
        this.logger.warn(
          `Failed to delete temp file ${tempFilePath}: ${cleanupErr}`,
        );
      }
    }
 
    return {
      status: executionStatus,
      summary:
        executionStatus === 'SUCCESS'
          ? `Code executed successfully.`
          : `Code execution failed.`,
      error_message: errorDetails,
      persisted_args: { content: code },
      execution_log: {
        output: unifiedOutput,
        error_message: errorDetails || '',
      },
    };
  }
}