class LittleGhost::CodeMode::JavascriptEngine
Runs model-written JavaScript in an isolated V8 context.
This optional engine uses MiniRacer. Requiring LittleGhost does not load MiniRacer; applications opt in by requiring little_ghost/code_mode/javascript_engine.
Constants
- DEFAULT_LIMITS
-
Resource and concurrency limits applied when the application does not override them.
Public Instance Methods
# File lib/little_ghost/code_mode/javascript_engine.rb, line 53 def instructions(catalog:, prompts: nil) javascript_catalog = Javascript::Catalog.new(catalog) renderer = prompts || ->(key, **locals) { FrameworkPrompts.new.render(key, locals:) } renderer.call("code_mode/javascript/instructions", declarations: javascript_catalog.declarations) end
Builds JavaScript usage instructions and TypeScript declarations for catalog.
Source
# File lib/little_ghost/code_mode/javascript_engine.rb, line 49 def language = :javascript
Returns the :javascript language identifier.
# File lib/little_ghost/code_mode/javascript_engine.rb, line 61 def open_session(broker:, sandbox_factory:, limits: {}, framework_prompt_scope: {}) configured_limits = normalize_limits(limits, defaults: DEFAULT_LIMITS) root = Dir.mktmpdir("little-ghost-javascript-") runtime_paths = javascript_runtime_paths workspace = Workspace.new( root:, paths: runtime_paths, teardown: lambda do |workspace:, **| FileUtils.remove_entry_secure(workspace.root) if File.exist?(workspace.root) end ).open sandbox = sandbox_factory.call( workspace:, required_runtime_paths: runtime_paths.keys.to_h { |name| [name, :read_only] } ) sandbox.open client = Javascript::Client.new( session_factory: lambda { sandbox.start_program( host_command, cwd: ".", environment: child_environment, output_bytes: configured_limits.fetch(:output_bytes), memory_bytes: configured_limits[:memory_bytes], cpu_seconds: configured_limits[:cpu_seconds], file_bytes: configured_limits[:file_bytes], allow_subprocesses: allow_subprocesses_for(sandbox) ) }, framework_prompt_scope: ) Javascript::Session.new( broker:, client:, sandbox:, workspace:, max_concurrency: configured_limits.fetch(:max_concurrency), wall_seconds: configured_limits.fetch(:wall_seconds), framework_prompt_scope: ) rescue sandbox&.close workspace&.close FileUtils.remove_entry_secure(root) if root && File.exist?(root) raise end
Opens a JavaScript Session with engine defaults merged with limits. Unsupported limit keys raise ArgumentError.