Skip to content

Commit 6d54054

Browse files
mishushakovclaude
andauthored
Add language autocomplete for supported languages (#247)
* Add language autocomplete for js, ts, r, java, bash Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Fix eslint ban-types error for string & {} Use NonNullable<unknown> instead of {} to satisfy @typescript-eslint/ban-types. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Simplify language type to plain string union Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Use string & {} with eslint-disable for language autocomplete Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Format sandbox.ts with prettier Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Use eslint-disable/enable block for ban-types on multiline type Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Format Python language type annotations for ruff Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a1b5f41 commit 6d54054

File tree

7 files changed

+43
-100
lines changed

7 files changed

+43
-100
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@e2b/code-interpreter": patch
3+
"e2b-code-interpreter": patch
4+
---
5+
6+
Add autocomplete support for javascript, typescript, r, java, and bash languages in runCode/run_code and createCodeContext/create_code_context

js/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
export * from 'e2b'
22

33
export { Sandbox } from './sandbox'
4-
export type { Context, RunCodeOpts, CreateCodeContextOpts } from './sandbox'
4+
export type {
5+
Context,
6+
RunCodeLanguage,
7+
RunCodeOpts,
8+
CreateCodeContextOpts,
9+
} from './sandbox'
510
export type {
611
Logs,
712
ExecutionError,

js/src/sandbox.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ export type Context = {
3333
cwd: string
3434
}
3535

36+
/* eslint-disable @typescript-eslint/ban-types */
37+
/**
38+
* Supported language for code execution.
39+
*/
40+
export type RunCodeLanguage =
41+
| 'python'
42+
| 'javascript'
43+
| 'typescript'
44+
| 'r'
45+
| 'java'
46+
| 'bash'
47+
| (string & {})
48+
/* eslint-enable @typescript-eslint/ban-types */
49+
3650
/**
3751
* Options for running code.
3852
*/
@@ -88,7 +102,7 @@ export interface CreateCodeContextOpts {
88102
*
89103
* @default python
90104
*/
91-
language?: string
105+
language?: RunCodeLanguage
92106
/**
93107
* Timeout for the request in **milliseconds**.
94108
*
@@ -128,29 +142,6 @@ export class Sandbox extends BaseSandbox {
128142
)}`
129143
}
130144

131-
/**
132-
* Run the code as Python.
133-
*
134-
* Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
135-
*
136-
* You can reference previously defined variables, imports, and functions in the code.
137-
*
138-
* @param code code to execute.
139-
* @param opts options for executing the code.
140-
*
141-
* @returns `Execution` result object.
142-
*/
143-
async runCode(
144-
code: string,
145-
opts?: RunCodeOpts & {
146-
/**
147-
* Language to use for code execution.
148-
*
149-
* If not defined, the default Python context is used.
150-
*/
151-
language?: 'python'
152-
}
153-
): Promise<Execution>
154145
/**
155146
* Run the code for the specified language.
156147
*
@@ -172,7 +163,7 @@ export class Sandbox extends BaseSandbox {
172163
*
173164
* If not defined, the default Python context is used.
174165
*/
175-
language?: string
166+
language?: RunCodeLanguage
176167
}
177168
): Promise<Execution>
178169
/**

python/e2b_code_interpreter/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
Logs,
1111
OutputHandler,
1212
OutputMessage,
13+
RunCodeLanguage,
1314
)

python/e2b_code_interpreter/code_interpreter_async.py

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import httpx
33

4-
from typing import Optional, Dict, overload, Union, Literal, List
4+
from typing import Optional, Dict, overload, Union, List
55
from httpx import AsyncClient
66

77
from e2b import (
@@ -18,6 +18,7 @@
1818
Execution,
1919
ExecutionError,
2020
Context,
21+
RunCodeLanguage,
2122
Result,
2223
aextract_exception,
2324
OutputHandlerWithAsync,
@@ -68,41 +69,7 @@ def _client(self) -> AsyncClient:
6869
async def run_code(
6970
self,
7071
code: str,
71-
language: Union[Literal["python"], None] = None,
72-
on_stdout: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
73-
on_stderr: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
74-
on_result: Optional[OutputHandlerWithAsync[Result]] = None,
75-
on_error: Optional[OutputHandlerWithAsync[ExecutionError]] = None,
76-
envs: Optional[Dict[str, str]] = None,
77-
timeout: Optional[float] = None,
78-
request_timeout: Optional[float] = None,
79-
) -> Execution:
80-
"""
81-
Runs the code as Python.
82-
83-
Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
84-
85-
You can reference previously defined variables, imports, and functions in the code.
86-
87-
:param code: Code to execute
88-
:param language: Language to use for code execution. If not defined, the default Python context is used.
89-
:param on_stdout: Callback for stdout messages
90-
:param on_stderr: Callback for stderr messages
91-
:param on_result: Callback for the `Result` object
92-
:param on_error: Callback for the `ExecutionError` object
93-
:param envs: Custom environment variables
94-
:param timeout: Timeout for the code execution in **seconds**
95-
:param request_timeout: Timeout for the request in **seconds**
96-
97-
:return: `Execution` result object
98-
"""
99-
...
100-
101-
@overload
102-
async def run_code(
103-
self,
104-
code: str,
105-
language: Optional[str] = None,
72+
language: Optional[RunCodeLanguage] = None,
10673
on_stdout: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
10774
on_stderr: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
10875
on_result: Optional[OutputHandlerWithAsync[Result]] = None,
@@ -236,7 +203,7 @@ async def run_code(
236203
async def create_code_context(
237204
self,
238205
cwd: Optional[str] = None,
239-
language: Optional[str] = None,
206+
language: Optional[RunCodeLanguage] = None,
240207
request_timeout: Optional[float] = None,
241208
) -> Context:
242209
"""

python/e2b_code_interpreter/code_interpreter_sync.py

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import httpx
33

4-
from typing import Optional, Dict, overload, Literal, Union, List
4+
from typing import Optional, Dict, overload, Union, List
55
from httpx import Client
66
from e2b import Sandbox as BaseSandbox, InvalidArgumentException
77

@@ -13,6 +13,7 @@
1313
from e2b_code_interpreter.models import (
1414
ExecutionError,
1515
Execution,
16+
RunCodeLanguage,
1617
Context,
1718
Result,
1819
extract_exception,
@@ -65,41 +66,7 @@ def _client(self) -> Client:
6566
def run_code(
6667
self,
6768
code: str,
68-
language: Union[Literal["python"], None] = None,
69-
on_stdout: Optional[OutputHandler[OutputMessage]] = None,
70-
on_stderr: Optional[OutputHandler[OutputMessage]] = None,
71-
on_result: Optional[OutputHandler[Result]] = None,
72-
on_error: Optional[OutputHandler[ExecutionError]] = None,
73-
envs: Optional[Dict[str, str]] = None,
74-
timeout: Optional[float] = None,
75-
request_timeout: Optional[float] = None,
76-
) -> Execution:
77-
"""
78-
Runs the code as Python.
79-
80-
Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
81-
82-
You can reference previously defined variables, imports, and functions in the code.
83-
84-
:param code: Code to execute
85-
:param language: Language to use for code execution. If not defined, the default Python context is used.
86-
:param on_stdout: Callback for stdout messages
87-
:param on_stderr: Callback for stderr messages
88-
:param on_result: Callback for the `Result` object
89-
:param on_error: Callback for the `ExecutionError` object
90-
:param envs: Custom environment variables
91-
:param timeout: Timeout for the code execution in **seconds**
92-
:param request_timeout: Timeout for the request in **seconds**
93-
94-
:return: `Execution` result object
95-
"""
96-
...
97-
98-
@overload
99-
def run_code(
100-
self,
101-
code: str,
102-
language: Optional[str] = None,
69+
language: Optional[RunCodeLanguage] = None,
10370
on_stdout: Optional[OutputHandler[OutputMessage]] = None,
10471
on_stderr: Optional[OutputHandler[OutputMessage]] = None,
10572
on_result: Optional[OutputHandler[Result]] = None,
@@ -232,7 +199,7 @@ def run_code(
232199
def create_code_context(
233200
self,
234201
cwd: Optional[str] = None,
235-
language: Optional[str] = None,
202+
language: Optional[RunCodeLanguage] = None,
236203
request_timeout: Optional[float] = None,
237204
) -> Context:
238205
"""

python/e2b_code_interpreter/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from dataclasses import dataclass, field
77
from typing import (
88
List,
9+
Literal,
910
Optional,
1011
Iterable,
1112
Dict,
@@ -20,6 +21,11 @@
2021

2122
from .charts import Chart, _deserialize_chart
2223

24+
RunCodeLanguage = Union[
25+
Literal["python", "javascript", "typescript", "r", "java", "bash"],
26+
str,
27+
]
28+
2329
T = TypeVar("T")
2430
OutputHandler = Union[Callable[[T], Any],]
2531

0 commit comments

Comments
 (0)