Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/add-language-overloads.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@e2b/code-interpreter": patch
"e2b-code-interpreter": patch
---

Add autocomplete support for javascript, typescript, r, java, and bash languages in runCode/run_code and createCodeContext/create_code_context
7 changes: 6 additions & 1 deletion js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
export * from 'e2b'

export { Sandbox } from './sandbox'
export type { Context, RunCodeOpts, CreateCodeContextOpts } from './sandbox'
export type {
Context,
RunCodeLanguage,
RunCodeOpts,
CreateCodeContextOpts,
} from './sandbox'
export type {
Logs,
ExecutionError,
Expand Down
41 changes: 16 additions & 25 deletions js/src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ export type Context = {
cwd: string
}

/* eslint-disable @typescript-eslint/ban-types */
/**
* Supported language for code execution.
*/
export type RunCodeLanguage =
| 'python'
| 'javascript'
| 'typescript'
| 'r'
| 'java'
| 'bash'
| (string & {})
/* eslint-enable @typescript-eslint/ban-types */

/**
* Options for running code.
*/
Expand Down Expand Up @@ -88,7 +102,7 @@ export interface CreateCodeContextOpts {
*
* @default python
*/
language?: string
language?: RunCodeLanguage
/**
* Timeout for the request in **milliseconds**.
*
Expand Down Expand Up @@ -128,29 +142,6 @@ export class Sandbox extends BaseSandbox {
)}`
}

/**
* Run the code as Python.
*
* Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
*
* You can reference previously defined variables, imports, and functions in the code.
*
* @param code code to execute.
* @param opts options for executing the code.
*
* @returns `Execution` result object.
*/
async runCode(
code: string,
opts?: RunCodeOpts & {
/**
* Language to use for code execution.
*
* If not defined, the default Python context is used.
*/
language?: 'python'
}
): Promise<Execution>
/**
* Run the code for the specified language.
*
Expand All @@ -172,7 +163,7 @@ export class Sandbox extends BaseSandbox {
*
* If not defined, the default Python context is used.
*/
language?: string
language?: RunCodeLanguage
}
): Promise<Execution>
/**
Expand Down
1 change: 1 addition & 0 deletions python/e2b_code_interpreter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
Logs,
OutputHandler,
OutputMessage,
RunCodeLanguage,
)
41 changes: 4 additions & 37 deletions python/e2b_code_interpreter/code_interpreter_async.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import httpx

from typing import Optional, Dict, overload, Union, Literal, List
from typing import Optional, Dict, overload, Union, List
from httpx import AsyncClient

from e2b import (
Expand All @@ -18,6 +18,7 @@
Execution,
ExecutionError,
Context,
RunCodeLanguage,
Result,
aextract_exception,
OutputHandlerWithAsync,
Expand Down Expand Up @@ -68,41 +69,7 @@ def _client(self) -> AsyncClient:
async def run_code(
self,
code: str,
language: Union[Literal["python"], None] = None,
on_stdout: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
on_stderr: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
on_result: Optional[OutputHandlerWithAsync[Result]] = None,
on_error: Optional[OutputHandlerWithAsync[ExecutionError]] = None,
envs: Optional[Dict[str, str]] = None,
timeout: Optional[float] = None,
request_timeout: Optional[float] = None,
) -> Execution:
"""
Runs the code as Python.

Specify the `language` or `context` option to run the code as a different language or in a different `Context`.

You can reference previously defined variables, imports, and functions in the code.

:param code: Code to execute
:param language: Language to use for code execution. If not defined, the default Python context is used.
:param on_stdout: Callback for stdout messages
:param on_stderr: Callback for stderr messages
:param on_result: Callback for the `Result` object
:param on_error: Callback for the `ExecutionError` object
:param envs: Custom environment variables
:param timeout: Timeout for the code execution in **seconds**
:param request_timeout: Timeout for the request in **seconds**

:return: `Execution` result object
"""
...

@overload
async def run_code(
self,
code: str,
language: Optional[str] = None,
language: Optional[RunCodeLanguage] = None,
on_stdout: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
on_stderr: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
on_result: Optional[OutputHandlerWithAsync[Result]] = None,
Expand Down Expand Up @@ -236,7 +203,7 @@ async def run_code(
async def create_code_context(
self,
cwd: Optional[str] = None,
language: Optional[str] = None,
language: Optional[RunCodeLanguage] = None,
request_timeout: Optional[float] = None,
) -> Context:
"""
Expand Down
41 changes: 4 additions & 37 deletions python/e2b_code_interpreter/code_interpreter_sync.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import httpx

from typing import Optional, Dict, overload, Literal, Union, List
from typing import Optional, Dict, overload, Union, List
from httpx import Client
from e2b import Sandbox as BaseSandbox, InvalidArgumentException

Expand All @@ -13,6 +13,7 @@
from e2b_code_interpreter.models import (
ExecutionError,
Execution,
RunCodeLanguage,
Context,
Result,
extract_exception,
Expand Down Expand Up @@ -65,41 +66,7 @@ def _client(self) -> Client:
def run_code(
self,
code: str,
language: Union[Literal["python"], None] = None,
on_stdout: Optional[OutputHandler[OutputMessage]] = None,
on_stderr: Optional[OutputHandler[OutputMessage]] = None,
on_result: Optional[OutputHandler[Result]] = None,
on_error: Optional[OutputHandler[ExecutionError]] = None,
envs: Optional[Dict[str, str]] = None,
timeout: Optional[float] = None,
request_timeout: Optional[float] = None,
) -> Execution:
"""
Runs the code as Python.

Specify the `language` or `context` option to run the code as a different language or in a different `Context`.

You can reference previously defined variables, imports, and functions in the code.

:param code: Code to execute
:param language: Language to use for code execution. If not defined, the default Python context is used.
:param on_stdout: Callback for stdout messages
:param on_stderr: Callback for stderr messages
:param on_result: Callback for the `Result` object
:param on_error: Callback for the `ExecutionError` object
:param envs: Custom environment variables
:param timeout: Timeout for the code execution in **seconds**
:param request_timeout: Timeout for the request in **seconds**

:return: `Execution` result object
"""
...

@overload
def run_code(
self,
code: str,
language: Optional[str] = None,
language: Optional[RunCodeLanguage] = None,
on_stdout: Optional[OutputHandler[OutputMessage]] = None,
on_stderr: Optional[OutputHandler[OutputMessage]] = None,
on_result: Optional[OutputHandler[Result]] = None,
Expand Down Expand Up @@ -232,7 +199,7 @@ def run_code(
def create_code_context(
self,
cwd: Optional[str] = None,
language: Optional[str] = None,
language: Optional[RunCodeLanguage] = None,
request_timeout: Optional[float] = None,
) -> Context:
"""
Expand Down
6 changes: 6 additions & 0 deletions python/e2b_code_interpreter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dataclasses import dataclass, field
from typing import (
List,
Literal,
Optional,
Iterable,
Dict,
Expand All @@ -20,6 +21,11 @@

from .charts import Chart, _deserialize_chart

RunCodeLanguage = Union[
Literal["python", "javascript", "typescript", "r", "java", "bash"],
str,
]
Comment thread
cursor[bot] marked this conversation as resolved.
Comment thread
mishushakov marked this conversation as resolved.

T = TypeVar("T")
OutputHandler = Union[Callable[[T], Any],]

Expand Down
Loading