Class Judge0JavaScriptExecutionTool

java.lang.Object
dev.langchain4j.code.judge0.Judge0JavaScriptExecutionTool

public class Judge0JavaScriptExecutionTool extends Object
A tool that executes JS code using the Judge0 service, hosted by Rapid API.
  • Field Details

    • JAVASCRIPT

      public static final int JAVASCRIPT
      The language ID for JavaScript in Judge0's API
      See Also:
  • Constructor Details

    • Judge0JavaScriptExecutionTool

      public Judge0JavaScriptExecutionTool(String apiKey)
      Constructs a new instance with the provided Rapid API key. The code fixing feature is enabled by default. Default timeout is 10 seconds.
      Parameters:
      apiKey - The Rapid API key. You can subscribe to the free plan (Basic) here: https://rapidapi.com/judge0-official/api/judge0-ce/pricing
      Throws:
      IllegalArgumentException - if the API key is null, empty, or blank
    • Judge0JavaScriptExecutionTool

      public Judge0JavaScriptExecutionTool(String apiKey, Duration timeout)
      Constructs a new instance with the provided Rapid API key and a timeout. The code fixing feature is enabled by default.
      Parameters:
      apiKey - The Rapid API key
      timeout - Timeout for calling Judge0
      Throws:
      IllegalArgumentException - if the API key is null, empty, or blank
      NullPointerException - if timeout is null
    • Judge0JavaScriptExecutionTool

      public Judge0JavaScriptExecutionTool(String apiKey, boolean fixCodeIfNeeded)
      Constructs a new instance with the provided Rapid API key and code fixing flag. Uses default timeout of 10 seconds.
      Parameters:
      apiKey - The Rapid API key
      fixCodeIfNeeded - Whether to automatically fix code if needed
      Throws:
      IllegalArgumentException - if the API key is null, empty, or blank
    • Judge0JavaScriptExecutionTool

      public Judge0JavaScriptExecutionTool(String apiKey, boolean fixCodeIfNeeded, Duration timeout)
      Constructs a new instance with the provided Rapid API key, a flag to control whether to fix the code, and a timeout.
      Parameters:
      apiKey - Rapid API key. You can subscribe to the free plan (Basic) here: https://rapidapi.com/judge0-official/api/judge0-ce/pricing
      fixCodeIfNeeded - Judge0 can return result of an execution if it was printed to the console. If provided JS code does not print result to the console, attempt will be made to fix it.
      timeout - Timeout for calling Judge0.
      Throws:
      IllegalArgumentException - if the API key is null, empty, or blank
      NullPointerException - if timeout is null
  • Method Details

    • executeJavaScriptCode

      public String executeJavaScriptCode(String javaScriptCode)
      Executes the provided JavaScript code using the Judge0 service. If code fixing is enabled (default), the method will attempt to add console.log statements if the code doesn't already output results to the console.
      Parameters:
      javaScriptCode - JavaScript code to execute
      Returns:
      The result of the JavaScript code execution
      Throws:
      IllegalArgumentException - if javaScriptCode is null, empty or blank
      RuntimeException - if code execution fails
    • isCodeFixingEnabled

      public boolean isCodeFixingEnabled()
      Returns whether code fixing is enabled for this tool.
      Returns:
      true if code fixing is enabled, false otherwise
    • builder

      public static Judge0JavaScriptExecutionTool.Builder builder()
      Creates a builder for configuring a Judge0JavaScriptExecutionTool.
      Returns:
      a new builder