Skip to content

Added TypeScript support to the code interpreter #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 2, 2025
Merged
5 changes: 5 additions & 0 deletions .changeset/tiny-adults-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@e2b/code-interpreter-template': patch
---

added typescript support
4 changes: 3 additions & 1 deletion js/tests/defaultKernels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { expect } from 'vitest'
import { sandboxTest } from './setup'

sandboxTest('test js kernel', async ({ sandbox }) => {
const output = await sandbox.runCode('console.log("Hello World!")', { language: 'js' })
const output = await sandbox.runCode('console.log("Hello World!")', {
language: 'js',
})
expect(output.logs.stdout).toEqual(['Hello World!\n'])
})
2 changes: 1 addition & 1 deletion template/.ts.swcrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
"targets": "node 20"
},
"isModule": false
}
}
10 changes: 9 additions & 1 deletion template/server/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

logger = logging.Logger(__name__)

def get_kernel_for_language(language: str) -> str:
if language == "typescript":
return "javascript"

return language

def normalize_language(language: Optional[str]) -> str:
if not language:
Expand All @@ -21,13 +26,16 @@ def normalize_language(language: Optional[str]) -> str:
if language == "js":
return "javascript"

if language == "ts":
return "typescript"

return language


async def create_context(client, websockets: dict, language: str, cwd: str) -> Context:
data = {
"path": str(uuid.uuid4()),
"kernel": {"name": language},
"kernel": {"name": get_kernel_for_language(language)},
"type": "notebook",
"name": str(uuid.uuid4()),
}
Expand Down
28 changes: 27 additions & 1 deletion template/server/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import uuid
import asyncio
import subprocess

from asyncio import Queue
from envs import get_envs
Expand All @@ -27,7 +28,6 @@

logger = logging.getLogger(__name__)


class Execution:
def __init__(self, in_background: bool = False):
self.queue = Queue[
Expand Down Expand Up @@ -199,6 +199,32 @@ async def execute(
+ code
)

if self.language == "typescript":
logger.info("Compiling TypeScript: %s", code)

# call SWC to compile the typescript code
try:
compile_result = subprocess.run("swc --config-file .ts.swcrc --filename index.ts".split(), input=code.encode(), capture_output=True)

if compile_result.returncode != 0:
logger.error("Error during TypeScript compilation: %s", compile_result.stderr.decode())
yield Error(
name="TypeScriptCompilerError",
value=compile_result.stderr.decode(),
traceback="",
)
return

code = compile_result.stdout.decode()
except Exception as e:
logger.error("Error starting SWC process: %s", e)
yield Error(
name="TypeScriptCompilerError",
value=str(e),
traceback="",
)
return

logger.info(code)
request = self._get_execute_request(message_id, code, False)

Expand Down
Loading