340 lines
16 KiB
JavaScript
340 lines
16 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.registerShellHandlers = registerShellHandlers;
|
|
const electron_1 = require("electron");
|
|
const child_process_1 = require("child_process");
|
|
const fs = __importStar(require("fs"));
|
|
const path = __importStar(require("path"));
|
|
const os = __importStar(require("os"));
|
|
const file_utils_1 = require("./file_utils");
|
|
function registerShellHandlers() {
|
|
electron_1.ipcMain.handle('native:open-folder', async (event, folderPath) => {
|
|
const cleanPath = (0, file_utils_1.expandPath)(folderPath);
|
|
const error = await electron_1.shell.openPath(cleanPath);
|
|
return { success: !error, error };
|
|
});
|
|
electron_1.ipcMain.handle('native:run-cmd', async (event, { cmd, timeout = 30000 }) => {
|
|
const cleanCmd = (0, file_utils_1.expandPath)(cmd);
|
|
return new Promise((resolve) => {
|
|
(0, child_process_1.exec)(cleanCmd, { timeout }, (error, stdout, stderr) => {
|
|
resolve({ success: !error, stdout: stdout.trim(), stderr: stderr.trim(), error: error ? error.message : null });
|
|
});
|
|
});
|
|
});
|
|
electron_1.ipcMain.handle('native:run-cmd-sync', async (event, { cmd }) => {
|
|
const cleanCmd = (0, file_utils_1.expandPath)(cmd);
|
|
try {
|
|
const stdout = (0, child_process_1.execSync)(cleanCmd).toString();
|
|
return { success: true, stdout: stdout.trim() };
|
|
}
|
|
catch (error) {
|
|
return { success: false, error: error.message, stderr: error.stderr?.toString() };
|
|
}
|
|
});
|
|
electron_1.ipcMain.handle('native:run-osascript', async (event, script) => {
|
|
if (os.platform() !== 'darwin')
|
|
return { success: false, error: 'AppleScript is only available on macOS' };
|
|
// HARDENED: Write script to a temp .scpt file rather than passing inline via -e.
|
|
// The old -e approach (`osascript -e "..."`) has two fatal flaws:
|
|
// 1. It breaks on multi-line scripts.
|
|
// 2. It breaks on paths containing spaces or special characters (quotes, parens, etc.)
|
|
// Writing to a file sidesteps both — no shell escaping needed at all.
|
|
// The .scpt file is deleted immediately after execution (success or failure).
|
|
// Worst case on crash: a stale .scpt in /tmp, cleared on next OS reboot.
|
|
//
|
|
// LEGACY (removed): const cmd = `osascript -e "${script.replace(/"/g, '\\"')}"`;
|
|
const tmp_script_path = path.join(os.tmpdir(), `ae_osa_${Date.now()}.scpt`);
|
|
return new Promise((resolve) => {
|
|
try {
|
|
fs.writeFileSync(tmp_script_path, script.trim());
|
|
}
|
|
catch (e) {
|
|
resolve({ success: false, error: `Failed to write AppleScript temp file: ${e.message}` });
|
|
return;
|
|
}
|
|
(0, child_process_1.exec)(`osascript "${tmp_script_path}"`, (error, stdout, stderr) => {
|
|
try {
|
|
fs.unlinkSync(tmp_script_path);
|
|
}
|
|
catch { }
|
|
resolve({ success: !error, stdout: stdout.trim(), stderr: stderr.trim(), error: error ? error.message : null });
|
|
});
|
|
});
|
|
});
|
|
electron_1.ipcMain.handle('native:kill-processes', async (event, { process_name_li = [] }) => {
|
|
console.log(`Native: Killing processes -> `, process_name_li);
|
|
const results = [];
|
|
for (const name of process_name_li) {
|
|
const cmd = os.platform() === 'win32'
|
|
? `taskkill /F /IM ${name} /T`
|
|
: `pkill -f ${name}`;
|
|
try {
|
|
(0, child_process_1.execSync)(cmd);
|
|
results.push({ name, success: true });
|
|
}
|
|
catch (e) {
|
|
results.push({ name, success: false, error: e.message });
|
|
}
|
|
}
|
|
return { success: true, results };
|
|
});
|
|
electron_1.ipcMain.handle('native:open-local-file-v2', async (event, filePath) => {
|
|
const cleanPath = (0, file_utils_1.expandPath)(filePath);
|
|
const error = await electron_1.shell.openPath(cleanPath);
|
|
return { success: !error, error };
|
|
});
|
|
electron_1.ipcMain.handle('native:launch-presentation', async (event, { path: rawPath, app: appType = 'default' }) => {
|
|
const cleanedPath = (0, file_utils_1.expandPath)(rawPath);
|
|
console.log(`Native: Launching Presentation -> ${cleanedPath} (App: ${appType})`);
|
|
if (os.platform() === 'linux') {
|
|
const cmd = `libreoffice --impress "${cleanedPath}"`;
|
|
return new Promise((resolve) => {
|
|
(0, child_process_1.exec)(cmd, (err, stdout, stderr) => {
|
|
if (err)
|
|
resolve({ success: false, error: err.message });
|
|
else
|
|
resolve({ success: true, stdout, stderr });
|
|
});
|
|
});
|
|
}
|
|
if (os.platform() === 'darwin') {
|
|
let script = '';
|
|
if (appType === 'keynote') {
|
|
script = `
|
|
tell application "Keynote"
|
|
activate
|
|
open (POSIX file "${cleanedPath}")
|
|
delay 1
|
|
start (front document)
|
|
end tell
|
|
`.trim();
|
|
}
|
|
else if (appType === 'powerpoint') {
|
|
script = `
|
|
tell application "Microsoft PowerPoint"
|
|
activate
|
|
open (POSIX file "${cleanedPath}")
|
|
delay 1
|
|
run slide show of active presentation
|
|
end tell
|
|
`.trim();
|
|
}
|
|
if (script) {
|
|
const tmp_script_path = path.join(os.tmpdir(), `ae_launch_${Date.now()}.scpt`);
|
|
return new Promise((resolve) => {
|
|
try {
|
|
fs.writeFileSync(tmp_script_path, script);
|
|
}
|
|
catch (e) {
|
|
resolve({ success: false, error: `Failed to write AppleScript temp file: ${e.message}` });
|
|
return;
|
|
}
|
|
(0, child_process_1.exec)(`osascript "${tmp_script_path}"`, (err) => {
|
|
try {
|
|
fs.unlinkSync(tmp_script_path);
|
|
}
|
|
catch { }
|
|
if (err)
|
|
resolve({ success: false, error: err.message });
|
|
else
|
|
resolve({ success: true });
|
|
});
|
|
});
|
|
}
|
|
}
|
|
const error = await electron_1.shell.openPath(cleanedPath);
|
|
return { success: !error, error };
|
|
});
|
|
electron_1.ipcMain.handle('native:control-presentation', async (event, { app, action }) => {
|
|
if (os.platform() !== 'darwin')
|
|
return { success: false, error: 'Presentation control is only available on macOS' };
|
|
let script = '';
|
|
if (app === 'powerpoint') {
|
|
switch (action) {
|
|
case 'next':
|
|
script = 'tell application "Microsoft PowerPoint" to next slide of slide show view of active presentation';
|
|
break;
|
|
case 'prev':
|
|
script = 'tell application "Microsoft PowerPoint" to previous slide of slide show view of active presentation';
|
|
break;
|
|
case 'start':
|
|
script = 'tell application "Microsoft PowerPoint" to run slide show of active presentation';
|
|
break;
|
|
case 'stop':
|
|
script = 'tell application "Microsoft PowerPoint" to stop slide show of active presentation';
|
|
break;
|
|
}
|
|
}
|
|
else if (app === 'keynote') {
|
|
switch (action) {
|
|
case 'next':
|
|
script = 'tell application "Keynote" to show next';
|
|
break;
|
|
case 'prev':
|
|
script = 'tell application "Keynote" to show previous';
|
|
break;
|
|
case 'start':
|
|
script = 'tell application "Keynote" to start (front document)';
|
|
break;
|
|
case 'stop':
|
|
script = 'tell application "Keynote" to stop';
|
|
break;
|
|
}
|
|
}
|
|
if (!script)
|
|
return { success: false, error: `Unsupported app or action: ${app}/${action}` };
|
|
return new Promise((resolve) => {
|
|
(0, child_process_1.exec)(`osascript -e "${script.replace(/"/g, '\\"')}"`, (error, stdout, stderr) => {
|
|
resolve({ success: !error, stdout: stdout.trim(), stderr: stderr.trim(), error: error ? error.message : null });
|
|
});
|
|
});
|
|
});
|
|
electron_1.ipcMain.handle('native:list-tools', async () => {
|
|
return [
|
|
// --- Config & Info ---
|
|
{
|
|
name: 'get_device_config',
|
|
description: 'Returns hydrated device config injected at startup from seed.json + API.',
|
|
params: {}
|
|
},
|
|
{
|
|
name: 'get_device_info',
|
|
description: 'Returns OS metadata: platform, hostname, IPs, CPU count, free RAM, home/tmp paths.',
|
|
params: {}
|
|
},
|
|
// --- File Cache ---
|
|
{
|
|
name: 'check_cache',
|
|
description: 'Checks if a file exists in the hashed cache. verify_hash:true re-hashes to confirm integrity.',
|
|
params: { cache_root: 'string', hash: 'string', hash_prefix_length: 'number (optional, default 2)', verify_hash: 'boolean (optional)' }
|
|
},
|
|
{
|
|
name: 'download_to_cache',
|
|
description: 'Streams a file from the API into the hashed cache with SHA-256 integrity check. Cleans stale .tmp files older than 5 min.',
|
|
params: { url: 'string', cache_root: 'string', hash: 'string', api_key: 'string', account_id: 'string', hash_prefix_length: 'number (optional, default 2)' }
|
|
},
|
|
{
|
|
name: 'copy_from_cache_to_temp',
|
|
description: 'Preferred primitive. Copies a cached file to temp with its original filename. Returns { success, path }. Caller decides what to do next.',
|
|
params: { cache_root: 'string', hash: 'string', temp_root: 'string', filename: 'string', hash_prefix_length: 'number (optional, default 2)' }
|
|
},
|
|
{
|
|
name: 'launch_from_cache',
|
|
description: 'Combines copy_from_cache_to_temp + execute. Runs native_template after copying — AppleScript string with {{path}} placeholder, or "shell:<cmd>" prefix. Returns error if native_template is null.',
|
|
params: { cache_root: 'string', hash: 'string', temp_root: 'string', filename: 'string', hash_prefix_length: 'number (optional)', native_template: 'string | null' }
|
|
},
|
|
// --- Shell & OS ---
|
|
{
|
|
name: 'open_folder',
|
|
description: 'Opens a directory in the OS file explorer (Finder on macOS).',
|
|
params: { path: 'string' }
|
|
},
|
|
{
|
|
name: 'run_cmd',
|
|
description: 'Async shell command execution with timeout.',
|
|
params: { cmd: 'string', timeout: 'number (optional, default 30000ms)' }
|
|
},
|
|
{
|
|
name: 'run_cmd_sync',
|
|
description: 'Synchronous shell command execution.',
|
|
params: { cmd: 'string' }
|
|
},
|
|
{
|
|
name: 'run_osascript',
|
|
description: 'Hardened AppleScript executor — writes to temp .scpt file, handles multi-line scripts and paths with special characters. macOS only.',
|
|
params: { script: 'string' }
|
|
},
|
|
{
|
|
name: 'kill_processes',
|
|
description: 'Terminates processes by name. macOS/Linux: pkill -f. Windows: taskkill /F.',
|
|
params: { process_name_li: 'string[]' }
|
|
},
|
|
{
|
|
name: 'open_local_file_v2',
|
|
description: 'Opens a file with its default OS application via shell.openPath.',
|
|
params: { path: 'string' }
|
|
},
|
|
{
|
|
name: 'open_external',
|
|
description: 'Opens a URL in Chrome, Firefox, or the system default browser.',
|
|
params: { url: 'string', app: 'chrome | firefox | default (optional)' }
|
|
},
|
|
// --- Presentations ---
|
|
{
|
|
name: 'launch_presentation',
|
|
description: 'Platform-aware launcher for PowerPoint, Keynote, LibreOffice. Resolves [home]/[tmp] placeholders. Hardened AppleScript (2026-05-11). Prefer copy_from_cache_to_temp + run_osascript for new flows.',
|
|
params: { path: 'string', app: 'default | powerpoint | keynote (optional)', os_platform: 'string (optional)' }
|
|
},
|
|
{
|
|
name: 'control_presentation',
|
|
description: 'Slide navigation for active PowerPoint or Keynote via AppleScript. macOS only.',
|
|
params: { app: 'powerpoint | keynote', action: 'next | prev | start | stop' }
|
|
},
|
|
// --- System Management ---
|
|
{
|
|
name: 'set_wallpaper',
|
|
description: 'Sets desktop wallpaper. Downloads from url (cached to ~/Library/Caches/OSIT/wallpaper/) or applies local path. url_external targets projector/second display separately. macOS only in production.',
|
|
params: { path: 'string (optional)', url: 'string (optional)', url_external: 'string (optional)', display: 'all | primary | external (optional, default all)', api_key: 'string (optional)', account_id: 'string (optional)' }
|
|
},
|
|
{
|
|
name: 'set_display_layout',
|
|
description: 'Mirror or extend displays via bundled displayplacer. macOS only. Auto-detects displays when no configStr given; configStr is an optional manual override.',
|
|
params: { mode: 'mirror | extend', configStr: 'string (optional)' }
|
|
},
|
|
{
|
|
name: 'window_control',
|
|
description: 'Electron window management.',
|
|
params: { action: 'maximize | unmaximize | minimize | restore | close | fullscreen | kiosk | devtools | reload', value: 'boolean (optional, used by fullscreen/kiosk/devtools)' }
|
|
},
|
|
{
|
|
name: 'power_control',
|
|
description: 'Shutdown, reboot, or sleep the host machine. macOS + Linux. May require sudo for shutdown/reboot.',
|
|
params: { action: 'shutdown | reboot | sleep' }
|
|
},
|
|
{
|
|
name: 'manage_recording',
|
|
description: 'Screen recording via bundled aperture binary. macOS only.',
|
|
params: { action: 'start | stop | status', options: '{ fps?, audioDeviceId?, output? } (optional)' }
|
|
},
|
|
{
|
|
name: 'update_app',
|
|
description: 'STUB: Downloads update package but does not install. Not functional.',
|
|
params: { source: 'url | file', url: 'string (optional)', path: 'string (optional)' }
|
|
}
|
|
];
|
|
});
|
|
}
|
|
//# sourceMappingURL=shell_handlers.js.map
|