diff --git a/.changeset/add-language-overloads.md b/.changeset/add-language-overloads.md new file mode 100644 index 00000000..0a1f00bd --- /dev/null +++ b/.changeset/add-language-overloads.md @@ -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 diff --git a/js/src/index.ts b/js/src/index.ts index 2ad762ba..1d337f78 100644 --- a/js/src/index.ts +++ b/js/src/index.ts @@ -1,7 +1,7 @@ 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, diff --git a/js/src/sandbox.ts b/js/src/sandbox.ts index a02b9d9e..0b320cc6 100644 --- a/js/src/sandbox.ts +++ b/js/src/sandbox.ts @@ -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. */ @@ -88,7 +102,7 @@ export interface CreateCodeContextOpts { * * @default python */ - language?: string + language?: RunCodeLanguage /** * Timeout for the request in **milliseconds**. * @@ -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 /** * Run the code for the specified language. * @@ -172,7 +163,7 @@ export class Sandbox extends BaseSandbox { * * If not defined, the default Python context is used. */ - language?: string + language?: RunCodeLanguage } ): Promise /** diff --git a/python/e2b_code_interpreter/__init__.py b/python/e2b_code_interpreter/__init__.py index 7562f51a..5202d5de 100644 --- a/python/e2b_code_interpreter/__init__.py +++ b/python/e2b_code_interpreter/__init__.py @@ -10,4 +10,5 @@ Logs, OutputHandler, OutputMessage, + RunCodeLanguage, ) diff --git a/python/e2b_code_interpreter/code_interpreter_async.py b/python/e2b_code_interpreter/code_interpreter_async.py index de938240..856c15ab 100644 --- a/python/e2b_code_interpreter/code_interpreter_async.py +++ b/python/e2b_code_interpreter/code_interpreter_async.py @@ -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 ( @@ -18,6 +18,7 @@ Execution, ExecutionError, Context, + RunCodeLanguage, Result, aextract_exception, OutputHandlerWithAsync, @@ -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: RunCodeLanguage = None, on_stdout: Optional[OutputHandlerWithAsync[OutputMessage]] = None, on_stderr: Optional[OutputHandlerWithAsync[OutputMessage]] = None, on_result: Optional[OutputHandlerWithAsync[Result]] = None, @@ -236,7 +203,7 @@ async def run_code( async def create_code_context( self, cwd: Optional[str] = None, - language: Optional[str] = None, + language: RunCodeLanguage = None, request_timeout: Optional[float] = None, ) -> Context: """ diff --git a/python/e2b_code_interpreter/code_interpreter_sync.py b/python/e2b_code_interpreter/code_interpreter_sync.py index 3adb2804..ae5de50d 100644 --- a/python/e2b_code_interpreter/code_interpreter_sync.py +++ b/python/e2b_code_interpreter/code_interpreter_sync.py @@ -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 @@ -13,6 +13,7 @@ from e2b_code_interpreter.models import ( ExecutionError, Execution, + RunCodeLanguage, Context, Result, extract_exception, @@ -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: RunCodeLanguage = None, on_stdout: Optional[OutputHandler[OutputMessage]] = None, on_stderr: Optional[OutputHandler[OutputMessage]] = None, on_result: Optional[OutputHandler[Result]] = None, @@ -232,7 +199,7 @@ def run_code( def create_code_context( self, cwd: Optional[str] = None, - language: Optional[str] = None, + language: RunCodeLanguage = None, request_timeout: Optional[float] = None, ) -> Context: """ diff --git a/python/e2b_code_interpreter/models.py b/python/e2b_code_interpreter/models.py index ef280e18..7e6429cf 100644 --- a/python/e2b_code_interpreter/models.py +++ b/python/e2b_code_interpreter/models.py @@ -6,6 +6,7 @@ from dataclasses import dataclass, field from typing import ( List, + Literal, Optional, Iterable, Dict, @@ -20,6 +21,12 @@ from .charts import Chart, _deserialize_chart +RunCodeLanguage = Union[ + Literal["python", "javascript", "typescript", "r", "java", "bash"], + str, + None, +] + T = TypeVar("T") OutputHandler = Union[Callable[[T], Any],]