Skip to content

Class: Code

Overview

The Code module provides secure code execution capabilities in isolated environments. It supports multiple programming languages including Python, JavaScript, and more.

Requirements

  • Requires agb-code-space-1 image for code execution features

Code execution module for running code in the session (Python, JavaScript, Java, R).

Hierarchy

  • BaseService

    Code

Table of contents

Properties

Methods

Properties

Methods

run

run(code, language, timeoutS?, options?): Promise<EnhancedCodeExecutionResult>

Execute code in the specified language with a timeout.

This is the primary public method for code execution. For real-time streaming output, set streamBeta=true and provide callback functions.

Parameters

NameTypeDefault valueDescription
codestringundefinedSource code to execute
languagestringundefinedLanguage identifier. Case-insensitive. Supported values: 'python', 'javascript', 'java', 'r'. Aliases: 'py' -> 'python', 'js'/'node' -> 'javascript'.
timeoutSnumber60Timeout in seconds (default 60)
options?ObjectundefinedOptional streaming parameters
options.onError?(err: unknown) => voidundefinedCallback invoked when an error occurs during streaming
options.onStderr?(chunk: string) => voidundefinedCallback invoked with each stderr chunk during streaming
options.onStdout?(chunk: string) => voidundefinedCallback invoked with each stdout chunk during streaming
options.streamBeta?booleanundefinedIf true, use WebSocket streaming for real-time output

Returns

Promise<EnhancedCodeExecutionResult>

Promise resolving to EnhancedCodeExecutionResult with results and logs

Example

typescript
// Simple execution
const result = await session.code.run("print('Hello')", "python");
console.log(result.results);

// Streaming execution for long-running code
const result = await session.code.run(
  "import time\nfor i in range(5):\n    print(f'Progress: {i+1}/5')\n    time.sleep(1)",
  "python",
  60,
  { streamBeta: true, onStdout: (chunk) => console.log(chunk) }
);

Best Practices

  1. Validate code syntax before execution
  2. Set appropriate execution timeouts
  3. Handle execution errors and exceptions
  4. Use proper resource limits to prevent resource exhaustion
  5. Clean up temporary files after code execution