Skip to content

Refactor: Extract JS/TS wrapper scripts from inline strings in scripting_engine.rs #429

@louis030195

Description

@louis030195

Problem

scripting_engine.rs contains large JavaScript/TypeScript wrapper scripts embedded as Rust strings using format!() blocks (lines ~970-1030, ~1690-1770, etc.). This makes the code:

  • Hard to maintain (no syntax highlighting, no linting)
  • Error-prone (escaping issues with {{ and }})
  • Difficult to read and review

Proposed Solution

Extract wrapper scripts to external template files using include_str!:

// Include at compile time
const JS_WRAPPER: &str = include_str!("templates/wrapper.js");

// Use string replacement for dynamic parts
let script = JS_WRAPPER
    .replace("{{USER_SCRIPT}}", &script)
    .replace("{{MODULE_SETUP}}", &module_resolution_setup);

File structure

crates/terminator-mcp-agent/src/
├── scripting_engine.rs
└── templates/
    ├── js_wrapper.js
    ├── ts_wrapper.ts
    └── py_wrapper.py

Benefits

  • IDE support: Syntax highlighting, linting, formatting for JS/TS/Python
  • Easier maintenance: Edit JS as JS, not escaped Rust strings
  • Better reviews: Cleaner diffs when modifying wrapper logic
  • No runtime cost: include_str! embeds at compile time

Alternatives Considered

  1. indoc! macro: Cleaner inline formatting but still strings
  2. Template engine (tera/handlebars): Overkill, adds runtime dep
  3. Build-time codegen: More complex than needed

Affected Code

  • execute_javascript_with_nodejs (~line 970)
  • execute_typescript_with_nodejs (~line 1690)
  • execute_python_with_bindings (~line 2100)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions