Code Tool
Status: Planned
Parse, transform, and execute code.
Capabilities
- Parse code into AST
- Transform/refactor code
- Execute code snippets
- Analyze code structure
- Generate code
Interface
typescript
interface CodeTool extends Tool { name: "code"; actions: { // Parsing parse(code: string, language: string): Promise<AST>; getSymbols(code: string, language: string): Promise<Symbol[]>; getDependencies(code: string, language: string): Promise<string[]>; // Transformation format(code: string, language: string): Promise<string>; refactor(code: string, transformation: Transformation): Promise<string>; // Execution execute(code: string, language: string, options?: ExecOptions): Promise<ExecResult>; // Analysis lint(code: string, language: string): Promise<LintResult[]>; typeCheck(code: string, language: string): Promise<TypeError[]>; }; } interface ExecResult { stdout: string; stderr: string; exitCode: number; duration: number; }
Supported Languages
| Language | Parse | Execute | Format |
|---|---|---|---|
| TypeScript | ✓ | ✓ (via ts-node) | ✓ (prettier) |
| JavaScript | ✓ | ✓ (node) | ✓ (prettier) |
| Python | ✓ | ✓ | ✓ (black) |
| Bash | - | ✓ | - |
| JSON | ✓ | - | ✓ |
| Markdown | ✓ | - | ✓ |
Example Usage
typescript
// Execute Python code const result = await code.execute(` print("Hello from Python!") x = 1 + 2 print(f"Result: {x}") `, "python"); // Format TypeScript const formatted = await code.format(uglyCode, "typescript"); // Get all functions in a file const symbols = await code.getSymbols(fileContent, "typescript"); const functions = symbols.filter(s => s.kind === "function");
Execution Sandboxing
Code execution is sandboxed:
- Timeout (default 30s)
- Memory limit
- No network access (optional)
- Restricted filesystem access
Security Considerations
- Never execute untrusted code without sandboxing
- Timeout all executions
- Consider using isolated containers
Open Questions
- Which languages are essential to support?
- How to handle code that needs dependencies?
- Should we support REPL-style execution?