kodeagent.code_runner.CodeRunner#

class kodeagent.code_runner.CodeRunner(env: Literal['host', 'docker', 'e2b'], allowed_imports: list[str], model_name: str, pip_packages: str | None = None, timeout: int = 30, env_vars_to_set: dict[str, str] | None = None, litellm_params: dict | None = None, work_dir: str | None = None, usage_tracker: UsageTracker | None = None, tool_names: set[str] | None = None)[source]#

Run Python code generated by an LLM in a given environment.

Create an environment to run Python code.

Parameters:
  • env – The code execution environment. Must be a string from CODE_ENV_NAMES.

  • allowed_imports – A list of Python modules that are allowed to be imported.

  • model_name – The LLM model name to use for security review.

  • pip_packages – Optional Python libs to be installed by pip [E2B].

  • timeout – Code execution timeout (default 30s).

  • env_vars_to_set – Optional environment variables to set in the code execution environment (E2B only).

  • litellm_params – Optional parameters for LiteLLM.

  • work_dir – Optional local workspace directory.

  • usage_tracker – Optional UsageTracker instance.

  • tool_names – Optional set of whitelisted tool names provided by the user.

__init__(env: Literal['host', 'docker', 'e2b'], allowed_imports: list[str], model_name: str, pip_packages: str | None = None, timeout: int = 30, env_vars_to_set: dict[str, str] | None = None, litellm_params: dict | None = None, work_dir: str | None = None, usage_tracker: UsageTracker | None = None, tool_names: set[str] | None = None)[source]#

Create an environment to run Python code.

Parameters:
  • env – The code execution environment. Must be a string from CODE_ENV_NAMES.

  • allowed_imports – A list of Python modules that are allowed to be imported.

  • model_name – The LLM model name to use for security review.

  • pip_packages – Optional Python libs to be installed by pip [E2B].

  • timeout – Code execution timeout (default 30s).

  • env_vars_to_set – Optional environment variables to set in the code execution environment (E2B only).

  • litellm_params – Optional parameters for LiteLLM.

  • work_dir – Optional local workspace directory.

  • usage_tracker – Optional UsageTracker instance.

  • tool_names – Optional set of whitelisted tool names provided by the user.

Methods

__init__(env, allowed_imports, model_name[, ...])

Create an environment to run Python code.

check_imports(code)

Check for disallowed imports in code, allowing submodules.

cleanup()

Clean up resources in the environment.

download_files_from_remote(remote_paths)

Download files from the remote environment to the local workspace.

run(tools_code, generated_code, task_id)

Run Python code in pre-specified environment after security review.

Attributes

local_modules_to_copy

Get the list of local modules to copy to the execution environment.