UNPKG

adk-typescript

Version:

TypeScript port of Google's Agent Development Kit (ADK)

76 lines (75 loc) 2.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UnsafeLocalCodeExecutor = void 0; const baseCodeExecutor_1 = require("./baseCodeExecutor"); /** * A code executor that unsafely execute code in the current local context. */ class UnsafeLocalCodeExecutor extends baseCodeExecutor_1.BaseCodeExecutor { /** * Initializes the UnsafeLocalCodeExecutor. */ constructor(params = {}) { super(); /** * Overrides the BaseCodeExecutor attribute: this executor cannot be stateful. */ this.stateful = false; /** * Overrides the BaseCodeExecutor attribute: this executor cannot optimize_data_file. */ this.optimizeDataFile = false; if (params.stateful) { throw new Error('Cannot set `stateful=true` in UnsafeLocalCodeExecutor.'); } if (params.optimizeDataFile) { throw new Error('Cannot set `optimizeDataFile=true` in UnsafeLocalCodeExecutor.'); } if (params.errorRetryAttempts !== undefined) { this.errorRetryAttempts = params.errorRetryAttempts; } if (params.codeBlockDelimiters !== undefined) { this.codeBlockDelimiters = params.codeBlockDelimiters; } if (params.executionResultDelimiters !== undefined) { this.executionResultDelimiters = params.executionResultDelimiters; } } /** * Executes code and return the code execution result. * * @param invocationContext - The invocation context of the code execution. * @param codeExecutionInput - The code execution input. * @returns The code execution result. */ async executeCode(invocationContext, codeExecutionInput) { // Execute the code. let output = ''; let error = ''; try { // In JavaScript, we can use Function constructor to evaluate code // This is unsafe and should only be used in controlled environments const evalFunction = new Function(codeExecutionInput.code); // Capture console output by overriding console.log const originalConsoleLog = console.log; console.log = (...args) => { output += args.map(arg => typeof arg === 'object' ? JSON.stringify(arg, null, 2) : String(arg)).join(' ') + '\n'; originalConsoleLog(...args); }; // Execute the code evalFunction(); // Restore original console.log console.log = originalConsoleLog; } catch (e) { error = e instanceof Error ? e.toString() : String(e); } // Collect the final result. return { stdout: output, stderr: error, outputFiles: [], }; } } exports.UnsafeLocalCodeExecutor = UnsafeLocalCodeExecutor;