CodeInterpreterClient
Source: src/services/code-interpreter.ts
Signature
Section titled “Signature”CodeInterpreterClient(transport: Leap0Transport)Overview
Section titled “Overview”Talks to the sandbox-hosted code interpreter service.
Throws
Section titled “Throws”Leap0Error: If API calls, stream decoding, or response validation fail.
Methods
Section titled “Methods”health
Section titled “health”async health(sandbox: SandboxRef, options: RequestOptions = {}): Promise<boolean>Returns whether the code interpreter service is healthy.
Parameters
Section titled “Parameters”sandbox: Sandbox ID or sandbox-like object.options: Optional request settings such as timeout and query params.
Returns
Section titled “Returns”- Whether the code interpreter reports a healthy status.
createContext
Section titled “createContext”async createContext(sandbox: SandboxRef, language: CodeLanguage = "python" as CodeLanguage, cwd?: string, options: RequestOptions = {}): Promise<CodeContext>Creates a reusable interpreter context.
Parameters
Section titled “Parameters”sandbox: Sandbox ID or sandbox-like object.language: Interpreter language for the context.cwd: Optional working directory.options: Optional request settings such as timeout and query params.
Returns
Section titled “Returns”- The created interpreter context.
listContexts
Section titled “listContexts”async listContexts(sandbox: SandboxRef, options: RequestOptions = {}): Promise<CodeContext[]>Lists active interpreter contexts.
Parameters
Section titled “Parameters”sandbox: Sandbox ID or sandbox-like object.options: Optional request settings such as timeout and query params.
Returns
Section titled “Returns”- The active interpreter contexts.
getContext
Section titled “getContext”async getContext(sandbox: SandboxRef, contextId: string, options: RequestOptions = {}): Promise<CodeContext>Fetches a single interpreter context by ID.
Parameters
Section titled “Parameters”sandbox: Sandbox ID or sandbox-like object.contextId: Interpreter context ID.options: Optional request settings such as timeout and query params.
Returns
Section titled “Returns”- The requested interpreter context.
deleteContext
Section titled “deleteContext”async deleteContext(sandbox: SandboxRef, contextId: string, options: RequestOptions = {}): Promise<void>Deletes an interpreter context by ID.
Parameters
Section titled “Parameters”sandbox: Sandbox ID or sandbox-like object.contextId: Interpreter context ID.options: Optional request settings such as timeout and query params.
execute
Section titled “execute”async execute(sandbox: SandboxRef, params: { code: string; language?: CodeLanguage; contextId?: string; envVars?: Record<string, string>; timeoutMs?: number; }, options: RequestOptions = {}): Promise<CodeExecutionResult>Executes code and waits for the final result.
Parameters
Section titled “Parameters”sandbox: Sandbox ID or sandbox-like object.params: Execution parameters.params.code: Source code to execute.params.language: Optional interpreter language.params.contextId: Optional existing context ID to reuse.params.envVars: Optional environment variables for execution.params.timeoutMs: Optional execution timeout in milliseconds.options: Optional request settings such as timeout and query params.
Returns
Section titled “Returns”- The completed execution result.
Examples
Section titled “Examples”const result = await sandbox.codeInterpreter.execute({ language: "python", code: "print('hello')",});executeStream
Section titled “executeStream”async executeStream(sandbox: SandboxRef, params: { code: string; language?: CodeLanguage; contextId?: string; timeoutMs?: number; }, options: RequestOptions = {}): AsyncIterable<StreamEvent>Streams code execution events until the interpreter exits.
Parameters
Section titled “Parameters”sandbox: Sandbox ID or sandbox-like object.params: Stream execution parameters.params.code: Source code to execute.params.language: Optional interpreter language.params.contextId: Optional existing context ID to reuse.params.timeoutMs: Optional execution timeout in milliseconds.options: Optional request settings such as timeout and query params.
Throws
Section titled “Throws”Leap0Error: If the stream returns malformed events or reports an execution error.
Examples
Section titled “Examples”for await (const event of sandbox.codeInterpreter.executeStream({ language: "python", code: "print('hello')",})) { console.log(event);}