gemini-code-execute
Execute Python code in a secure sandbox with context-aware enhancements for tasks like data science, automation, and testing using built-in preferences on Gemini.
Instructions
Execute Python code using Gemini's built-in code execution sandbox (with learned user preferences)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Python code to execute in the sandbox | |
| context | No | Optional context for intelligent enhancement (e.g., "data-science", "automation", "testing") |
Implementation Reference
- src/tools/code-execution.js:43-119 (handler)The main handler function that executes Python code using Gemini's code execution capabilities, enhances prompts with intelligence system, and formats the response.async execute(args) { const code = validateNonEmptyString(args.code, 'code'); const context = args.context ? validateString(args.context, 'context') : null; log(`Executing Python code: ${code.substring(0, 100)}... with context: ${context || 'general'}`, this.name); try { const basePrompt = `Please execute this Python code and show the results:\n\n\`\`\`python\n${code}\n\`\`\`\n\nExecute the code and provide both the code output and any results.`; let enhancedPrompt = basePrompt; if (this.intelligenceSystem.initialized) { try { enhancedPrompt = await this.intelligenceSystem.enhancePrompt(basePrompt, context, this.name); log('Applied Tool Intelligence enhancement', this.name); } catch (err) { log(`Tool Intelligence enhancement failed: ${err.message}`, this.name); } } let prompt = enhancedPrompt; if (context) { prompt += `\n\nContext: ${context}`; } const candidate = await this.geminiService.generateText('CODE_EXECUTION', prompt); const responseText = candidate; // Assuming generateText returns the direct text content const executionResults = []; // Code execution results are not directly extracted from generateText if (responseText || executionResults.length > 0) { let finalResponse = `✓ Python code executed successfully:\n\n**Code:**\n\`\`\`python\n${code}\n\`\`\`\n\n`; // eslint-disable-line max-len if (responseText) { finalResponse += `**Response:**\n${responseText}\n\n`; } if (executionResults.length > 0) { finalResponse += `**Execution Results:**\n${JSON.stringify(executionResults, null, 2)}\n`; } if (this.intelligenceSystem.initialized) { try { const resultSummary = `Code executed successfully with ${executionResults.length} results and ${responseText ? 'response text' : 'no response text'}`; // eslint-disable-line max-len await this.intelligenceSystem.learnFromInteraction(basePrompt, enhancedPrompt, resultSummary, context, this.name); log('Tool Intelligence learned from interaction', this.name); } catch (err) { log(`Tool Intelligence learning failed: ${err.message}`, this.name); } } if (context && this.intelligenceSystem.initialized) { finalResponse += `\n\n---\n_Enhancement applied based on context: ${context}_`; } log('Code execution completed successfully', this.name); return { content: [ { type: 'text', text: finalResponse, }, ], }; } log('No response text or execution results generated', this.name); return { content: [ { type: 'text', text: `Code execution completed but no output was generated. The code may have run successfully without producing visible results.\n\n**Code:**\n\`\`\`python\n${code}\n\`\`\``, // eslint-disable-line max-len }, ], }; } catch (error) { log(`Error executing code: ${error.message}`, this.name); throw new Error(`Error executing code: ${error.message}`); } }
- src/tools/code-execution.js:18-30 (schema)Input schema defining the parameters for the tool: required 'code' string and optional 'context' string.type: 'object', properties: { code: { type: 'string', description: 'Python code to execute in the sandbox', }, context: { type: 'string', description: 'Optional context for intelligent enhancement (e.g., "data-science", "automation", "testing")', }, }, required: ['code'], },
- src/tools/index.js:84-84 (registration)Registration of the CodeExecutionTool instance in the central tool registry, which assigns it the name 'gemini-code-execute'.registerTool(new CodeExecutionTool(intelligenceSystem, geminiService));