code.md

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

LanguageParseExecuteFormat
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

  1. Which languages are essential to support?
  2. How to handle code that needs dependencies?
  3. Should we support REPL-style execution?