Package dev.langchain4j.code.judge0
Class Judge0JavaScriptExecutionTool
java.lang.Object
dev.langchain4j.code.judge0.Judge0JavaScriptExecutionTool
A tool that executes JS code using the Judge0 service, hosted by Rapid API.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Builder for creating a configured Judge0JavaScriptExecutionTool. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
The language ID for JavaScript in Judge0's API -
Constructor Summary
ConstructorsConstructorDescriptionJudge0JavaScriptExecutionTool
(String apiKey) Constructs a new instance with the provided Rapid API key.Judge0JavaScriptExecutionTool
(String apiKey, boolean fixCodeIfNeeded) Constructs a new instance with the provided Rapid API key and code fixing flag.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.Judge0JavaScriptExecutionTool
(String apiKey, Duration timeout) Constructs a new instance with the provided Rapid API key and a timeout. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()
Creates a builder for configuring a Judge0JavaScriptExecutionTool.executeJavaScriptCode
(String javaScriptCode) Executes the provided JavaScript code using the Judge0 service.boolean
Returns whether code fixing is enabled for this tool.
-
Field Details
-
JAVASCRIPT
public static final int JAVASCRIPTThe language ID for JavaScript in Judge0's API- See Also:
-
-
Constructor Details
-
Judge0JavaScriptExecutionTool
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
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 keytimeout
- Timeout for calling Judge0- Throws:
IllegalArgumentException
- if the API key is null, empty, or blankNullPointerException
- if timeout is null
-
Judge0JavaScriptExecutionTool
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 keyfixCodeIfNeeded
- Whether to automatically fix code if needed- Throws:
IllegalArgumentException
- if the API key is null, empty, or blank
-
Judge0JavaScriptExecutionTool
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/pricingfixCodeIfNeeded
- 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 blankNullPointerException
- if timeout is null
-
-
Method Details
-
executeJavaScriptCode
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 blankRuntimeException
- 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
Creates a builder for configuring a Judge0JavaScriptExecutionTool.- Returns:
- a new builder
-