Heuristic:AI QL Tuui Nut JS Graceful Fallback
| Knowledge Sources | |
|---|---|
| Domains | Automation, Cross_Platform, Resilience |
| Last Updated | 2026-10-24 20:38 GMT |
Overview
Dynamically import nut-js at runtime and fall back to platform-native scripting (AppleScript/VBScript) when the native module fails to load, ensuring automation degrades gracefully.
Description
@nut-tree-fork/nut-js is a native Node.js module that may fail to load due to missing native dependencies, architecture mismatches, or permission issues. Instead of hard-failing, the NutAutomator base class attempts a dynamic import() and caches the result: true (loaded), false (permanently failed), or undefined (not yet tried). Platform-specific subclasses (MacosAutomator, WindowsAutomator) override methods to try nut-js first, then fall back to AppleScript or VBScript respectively. Each automation method (copy, paste, selectAll) independently wraps its nut-js call in try/catch and uses the platform script as fallback.
Usage
Apply this heuristic when using native Node.js modules for desktop automation that may not load on all platforms. The symptom is "Error loading nutjs. Automation not available." in the console — the app should still function, albeit slower or with reduced features.
The Insight (Rule of Thumb)
- Action: Use
await import('@nut-tree-fork/nut-js')in asetup()method; cache result astrue/null. Each automation method tries nut-js first, catches errors, and falls back to platform scripting. - Value: macOS falls back to AppleScript; Windows falls back to VBScript; Linux has no fallback (throws).
- Trade-off: Platform scripts are slower and less precise than nut-js. AppleScript copy retries 20 times with delays.
- Caching:
nutvariable:undefined= not tried, module object = loaded,null= failed permanently.
Reasoning
Native modules are fragile — they depend on the correct Node.js ABI version, platform libraries, and sometimes accessibility permissions. Hard-failing when nut-js is unavailable would make the app unusable on systems where native compilation failed. The graceful fallback ensures the core automation (text grab, paste) works on every platform, even if reduced in speed or reliability. The try-per-method approach allows partial nut-js availability (some methods may work while others don't).
Code Evidence
Dynamic import with caching from `src/main/aid/nut.ts:11-27`:
let nut: any = undefined
protected async setup() {
if (nut) return true // already loaded
if (nut === null) return false // permanently failed
try {
nut = await import('@nut-tree-fork/nut-js')
return true
} catch {
console.log('Error loading nutjs. Automation not available.')
nut = null
return false
}
}
macOS fallback to AppleScript from `src/main/aid/macos.ts:109-134`:
async copySelectedText(): Promise<void> {
try {
if (!(await this.setup())) throw new Error('nutjs not loaded')
await this.nut().keyboard.pressKey(this.commandKey(), this.nut().Key.C)
await this.nut().keyboard.releaseKey(this.commandKey(), this.nut().Key.C)
} catch {
// applescript fallback
const script = `
repeat 20 times
try
tell application "System Events" to keystroke "c" using command down
delay 0.02
set clipboardContents to the clipboard
if length of clipboardContents is not 0 then exit repeat
delay 0.1
on error
end try
end repeat
`
await this.runScript(script)
}
}
Windows fallback to VBScript from `src/main/aid/windows.ts:41-56`:
async copySelectedText() {
try {
if (!(await this.setup())) throw new Error('nutjs not loaded')
await this.nut().keyboard.pressKey(this.commandKey(), this.nut().Key.C)
await this.nut().keyboard.releaseKey(this.commandKey(), this.nut().Key.C)
} catch {
const script = `
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "^c"
WScript.Sleep 20
`
await runVbs({ vbs: script })
}
}