A desktop coding agent with free access to frontier models.
TypeScript
feat(desktop): read a picked skill folder in the main process
Samrat Talukdarbed8ca035363
diff --git a/apps/app/src/app/lib/desktop-types.ts b/apps/app/src/app/lib/desktop-types.tsindex dc0e1a6b..1872a906 100644--- a/apps/app/src/app/lib/desktop-types.ts+++ b/apps/app/src/app/lib/desktop-types.ts@@ -31,6 +31,7 @@ export type {ExecResult,LocalSkillCard,LocalSkillContent,+ SkillFolderRead,NukeManifestPreview,NukeOptions,NukeReceipt,diff --git a/apps/app/src/app/lib/desktop.ts b/apps/app/src/app/lib/desktop.tsindex fb18b832..dfdb3bdc 100644--- a/apps/app/src/app/lib/desktop.ts+++ b/apps/app/src/app/lib/desktop.ts@@ -20,6 +20,7 @@ export type {ExecResult,LocalSkillCard,LocalSkillContent,+ SkillFolderRead,NukeManifestPreview,NukeOptions,NukeReceipt,@@ -759,6 +760,7 @@ const {engineInstall,desktopNotificationShow,importSkill,+ readSkillFolder,installSkillTemplate,listLocalSkills,readLocalSkill,@@ -818,6 +820,7 @@ export {engineInstall,desktopNotificationShow,importSkill,+ readSkillFolder,installSkillTemplate,listLocalSkills,readLocalSkill,diff --git a/apps/desktop/electron/main.mjs b/apps/desktop/electron/main.mjsindex 1b049190..c8db7cf4 100644--- a/apps/desktop/electron/main.mjs+++ b/apps/desktop/electron/main.mjs@@ -79,6 +79,7 @@ import {} from "./brand-icon-windows.mjs";import { resetMacDockIcon } from "./brand-icon-darwin.mjs";import { createOpenLogsFolderHandler } from "./logs-folder.mjs";+import { readSkillFolder } from "./skill-folder.mjs";import { createDesktopVaultKeyProvider } from "./secure-vault-key.mjs";import { applyLinuxPasswordStore, recordLinuxPasswordStore } from "./linux-password-store.mjs";import { createDesktopOmniRushAccountStore, legacyKeychainAllowed } from "./omnirush-account.mjs";@@ -2194,6 +2195,11 @@ const desktopCommandHandlers = {await cp(sourceDir, destination, { recursive: true });return execResult(true, `Imported skill to ${destination}`);},+ "readSkillFolder": async (event, ...args) => {+ // Upload source for "Add skill > Upload folder": walked here (not in the+ // renderer) so links are seen and refused; the local server re-validates.+ return readSkillFolder(String(args[0] ?? "").trim());+ },"installSkillTemplate": async (event, ...args) => {const projectDir = String(args[0] ?? "").trim();const name = validateSkillName(args[1]);diff --git a/apps/desktop/electron/skill-folder.mjs b/apps/desktop/electron/skill-folder.mjsnew file mode 100644index 00000000..6140fcc6--- /dev/null+++ b/apps/desktop/electron/skill-folder.mjs@@ -0,0 +1,113 @@+// Reads a skill folder the user picked (or dropped) for upload.+//+// The renderer cannot see links in a folder it reads through <input+// webkitdirectory> or drag and drop (Chromium follows them), so on the desktop+// the main process walks the folder itself with lstat: a link anywhere in the+// skill refuses the read with the offending paths, OS/VCS litter and dependency+// trees are skipped without descending into them, and the same count and size+// caps as the local server apply, so a mistaken pick (a home folder) stops+// early instead of buffering gigabytes. The local server re-validates+// everything it is sent.+import { lstat, open, readdir } from "node:fs/promises";+import path from "node:path";++export const SKILL_FOLDER_LIMITS = Object.freeze({+ maxFiles: 200,+ maxRawEntries: 2000,+ maxFileBytes: 10 * 1024 * 1024,+ maxTotalBytes: 25 * 1024 * 1024,+ maxDepth: 16,+});++const JUNK_NAMES = new Set([".ds_store", "thumbs.db", "desktop.ini", ".localized"]);+const JUNK_DIRS = new Set(["__macosx", ".git", "node_modules", "__pycache__", ".svn", ".hg", ".venv", ".idea", ".vscode"]);++function formatBytes(bytes) {+ if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;+ return `${Math.round((bytes / (1024 * 1024)) * 10) / 10} MB`;+}++function isJunkFile(name) {+ const lower = name.toLowerCase();+ return JUNK_NAMES.has(lower) || lower.startsWith("._") || lower.endsWith(".pyc");+}++async function readCapped(file, size) {+ const handle = await open(file, "r");+ try {+ const buffer = Buffer.alloc(size);+ const { bytesRead } = await handle.read(buffer, 0, size, 0);+ return buffer.subarray(0, bytesRead);+ } finally {+ await handle.close();+ }+}++/**+ * @param {string} root absolute folder path+ * @param {{ limits?: typeof SKILL_FOLDER_LIMITS }} [options]+ * @returns {Promise<{ root: string; name: string; files: Array<{ path: string; contentBase64: string; executable: boolean }>; skipped: string[] }>}+ */+export async function readSkillFolder(root, options = {}) {+ const limits = options.limits ?? SKILL_FOLDER_LIMITS;+ const source = String(root ?? "").trim();+ if (!source || !path.isAbsolute(source)) throw new Error("Choose a skill folder");+ const top = await lstat(source);+ if (top.isSymbolicLink()) throw new Error("The chosen folder is a link; choose the folder it points to");+ if (!top.isDirectory()) throw new Error("The chosen item is not a folder");++ /** @type {Array<{ path: string; full: string; size: number; executable: boolean }>} */+ const found = [];+ const skipped = [];+ const links = [];+ let entries = 0;+ let total = 0;++ const walk = async (dir, depth) => {+ const items = await readdir(dir, { withFileTypes: true });+ items.sort((a, b) => a.name.localeCompare(b.name));+ for (const item of items) {+ entries += 1;+ if (entries > limits.maxRawEntries) throw new Error(`The folder has more than ${limits.maxRawEntries} entries; a skill folder holds at most ${limits.maxFiles} files`);+ const full = path.join(dir, item.name);+ const rel = path.relative(source, full).split(path.sep).join("/");+ const info = await lstat(full);+ if (info.isSymbolicLink()) {+ links.push(rel);+ continue;+ }+ if (info.isDirectory()) {+ if (JUNK_DIRS.has(item.name.toLowerCase())) {+ skipped.push(`${rel}/`);+ continue;+ }+ if (depth >= limits.maxDepth) throw new Error(`The folder is nested too deep at ${rel}`);+ await walk(full, depth + 1);+ continue;+ }+ if (!info.isFile()) {+ skipped.push(rel);+ continue;+ }+ if (isJunkFile(item.name)) {+ skipped.push(rel);+ continue;+ }+ if (info.size > limits.maxFileBytes) throw new Error(`${rel} is larger than ${formatBytes(limits.maxFileBytes)}`);+ total += info.size;+ if (total > limits.maxTotalBytes) throw new Error(`The folder is larger than ${formatBytes(limits.maxTotalBytes)}`);+ found.push({ path: rel, full, size: info.size, executable: process.platform !== "win32" && (info.mode & 0o111) !== 0 });+ if (found.length > limits.maxFiles) throw new Error(`The folder has more than ${limits.maxFiles} files`);+ }+ };+ await walk(source, 1);+ if (links.length) {+ throw new Error(`Skills cannot contain links (symlinks or shortcuts). Replace them with the real files: ${links.slice(0, 10).join(", ")}${links.length > 10 ? `, and ${links.length - 10} more` : ""}`);+ }+ const files = [];+ for (const file of found) {+ const bytes = await readCapped(file.full, Math.min(file.size, limits.maxFileBytes));+ files.push({ path: file.path, contentBase64: bytes.toString("base64"), executable: file.executable });+ }+ return { root: source, name: path.basename(source), files, skipped };+}diff --git a/apps/desktop/electron/skill-folder.test.mjs b/apps/desktop/electron/skill-folder.test.mjsnew file mode 100644index 00000000..2d683f2e--- /dev/null+++ b/apps/desktop/electron/skill-folder.test.mjs@@ -0,0 +1,59 @@+import assert from "node:assert/strict";+import { chmod, mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises";+import os from "node:os";+import path from "node:path";+import test from "node:test";++import { readSkillFolder, SKILL_FOLDER_LIMITS } from "./skill-folder.mjs";++async function fixture() {+ const root = await mkdtemp(path.join(os.tmpdir(), "omnirush-skill-folder-"));+ const skill = path.join(root, "greeter");+ await mkdir(path.join(skill, "scripts"), { recursive: true });+ await mkdir(path.join(skill, "node_modules", "dep"), { recursive: true });+ await writeFile(path.join(skill, "SKILL.md"), "---\nname: greeter\ndescription: Greets\n---\n");+ await writeFile(path.join(skill, "scripts", "greet.sh"), "#!/bin/sh\necho hi\n");+ await chmod(path.join(skill, "scripts", "greet.sh"), 0o755);+ await writeFile(path.join(skill, ".DS_Store"), "junk");+ await writeFile(path.join(skill, "node_modules", "dep", "index.js"), "x");+ return { root, skill };+}++test("reads a skill folder with relative paths and exec bits, skipping junk", async () => {+ const { root, skill } = await fixture();+ try {+ const result = await readSkillFolder(skill);+ assert.equal(result.name, "greeter");+ assert.deepEqual(result.files.map((f) => f.path).sort(), ["SKILL.md", "scripts/greet.sh"]);+ const script = result.files.find((f) => f.path === "scripts/greet.sh");+ assert.equal(Buffer.from(script.contentBase64, "base64").toString(), "#!/bin/sh\necho hi\n");+ if (process.platform !== "win32") assert.equal(script.executable, true);+ assert.equal(result.files.find((f) => f.path === "SKILL.md").executable, false);+ assert.deepEqual(result.skipped.sort(), [".DS_Store", "node_modules/"]);+ } finally {+ await rm(root, { recursive: true, force: true });+ }+});++test("refuses a folder holding links", { skip: process.platform === "win32" }, async () => {+ const { root, skill } = await fixture();+ try {+ await symlink("/etc/hostname", path.join(skill, "scripts", "leak"));+ await assert.rejects(readSkillFolder(skill), /cannot contain links.*scripts\/leak/);+ await assert.rejects(readSkillFolder(path.join(root, "missing")));+ await assert.rejects(readSkillFolder("relative/path"), /Choose a skill folder/);+ } finally {+ await rm(root, { recursive: true, force: true });+ }+});++test("stops at the size and count caps", async () => {+ const { root, skill } = await fixture();+ try {+ await assert.rejects(readSkillFolder(skill, { limits: { ...SKILL_FOLDER_LIMITS, maxFiles: 1 } }), /more than 1 files/);+ await assert.rejects(readSkillFolder(skill, { limits: { ...SKILL_FOLDER_LIMITS, maxTotalBytes: 20 } }), /larger than/);+ await assert.rejects(readSkillFolder(skill, { limits: { ...SKILL_FOLDER_LIMITS, maxFileBytes: 5 } }), /larger than/);+ } finally {+ await rm(root, { recursive: true, force: true });+ }+});diff --git a/apps/desktop/package.json b/apps/desktop/package.jsonindex 21cc4b25..1a9976bc 100644--- a/apps/desktop/package.json+++ b/apps/desktop/package.json@@ -27,7 +27,7 @@"prepare:sidecar": "node ./scripts/prepare-sidecar.mjs","pretest": "pnpm --filter @omnirush/headless-threads build","test:omnirush-account": "node --test electron/omnirush-account.test.mjs",- "test": "node --test scripts/prepare-server-constants.test.mjs scripts/verify-update-manifest.test.mjs electron/after-pack-paths.test.mjs electron/agent-context-diagnostics-fetch.test.mjs electron/automation-runner.test.mjs electron/binary-transfer.test.mjs electron/blank-slate-profile.test.mjs electron/blank-slate-profile-isolation.test.mjs electron/brand-app-name.test.mjs electron/brand-icon-darwin.test.mjs electron/brand-icon-windows.test.mjs electron/browser-login-sync.test.mjs electron/browser-panel.test.mjs electron/connect-link.test.mjs electron/desktop-distribution.test.mjs electron/dev-profile.test.mjs electron/electron-builder-config.test.mjs electron/linux-desktop-integration.test.mjs electron/linux-password-store.test.mjs electron/logs-folder.test.mjs electron/media-permissions.test.mjs electron/no-bare-external-fetch.test.mjs electron/nuke.test.mjs electron/omnirush-account.test.mjs electron/omnirush-ui-mcp.test.mjs electron/open-external.test.mjs electron/process-resilience.test.mjs electron/runtime.test.mjs electron/runtime-ca.test.mjs electron/runtime-chain-repair.test.mjs electron/sentry.test.mjs electron/server-dependency-mirror.test.mjs electron/secure-vault-key.test.mjs electron/stdio-errors.test.mjs electron/ui-control-server.test.mjs electron/updater.test.mjs electron/remote-workspace.test.mjs electron/workspace-archive.test.mjs electron/workspace-store.test.mjs",+ "test": "node --test scripts/prepare-server-constants.test.mjs scripts/verify-update-manifest.test.mjs electron/after-pack-paths.test.mjs electron/agent-context-diagnostics-fetch.test.mjs electron/automation-runner.test.mjs electron/binary-transfer.test.mjs electron/blank-slate-profile.test.mjs electron/blank-slate-profile-isolation.test.mjs electron/brand-app-name.test.mjs electron/brand-icon-darwin.test.mjs electron/brand-icon-windows.test.mjs electron/browser-login-sync.test.mjs electron/browser-panel.test.mjs electron/connect-link.test.mjs electron/desktop-distribution.test.mjs electron/dev-profile.test.mjs electron/electron-builder-config.test.mjs electron/linux-desktop-integration.test.mjs electron/linux-password-store.test.mjs electron/logs-folder.test.mjs electron/media-permissions.test.mjs electron/no-bare-external-fetch.test.mjs electron/nuke.test.mjs electron/omnirush-account.test.mjs electron/omnirush-ui-mcp.test.mjs electron/open-external.test.mjs electron/process-resilience.test.mjs electron/runtime.test.mjs electron/runtime-ca.test.mjs electron/runtime-chain-repair.test.mjs electron/sentry.test.mjs electron/server-dependency-mirror.test.mjs electron/skill-folder.test.mjs electron/secure-vault-key.test.mjs electron/stdio-errors.test.mjs electron/ui-control-server.test.mjs electron/updater.test.mjs electron/remote-workspace.test.mjs electron/workspace-archive.test.mjs electron/workspace-store.test.mjs","test:core": "node --test scripts/prepare-server-constants.test.mjs electron/workspace-store.test.mjs electron/workspace-archive.test.mjs electron/connect-link.test.mjs electron/open-external.test.mjs electron/secure-vault-key.test.mjs electron/process-resilience.test.mjs electron/stdio-errors.test.mjs electron/runtime-ca.test.mjs electron/runtime-chain-repair.test.mjs electron/automation-runner.test.mjs","pretest:core": "pnpm --filter @omnirush/headless-threads build"},diff --git a/packages/types/src/desktop-ipc.ts b/packages/types/src/desktop-ipc.tsindex baddcf00..dd349a73 100644--- a/packages/types/src/desktop-ipc.ts+++ b/packages/types/src/desktop-ipc.ts@@ -308,6 +308,14 @@ export type LocalSkillCard = {trigger?: string;};+export type SkillFolderRead = {+ root: string;+ /** The folder's basename. */+ name: string;+ files: Array<{ path: string; contentBase64: string; executable: boolean }>;+ skipped: string[];+};+export type LocalSkillContent = {path: string;content: string;@@ -616,6 +624,8 @@ export type DesktopCommandMap = {args: [projectDir: string, sourceDir: string, options?: { overwrite?: boolean }];result: ExecResult;};+ /** Reads a picked/dropped skill folder for upload (links refused, junk skipped, capped). */+ readSkillFolder: { args: [sourceDir: string]; result: SkillFolderRead };installSkillTemplate: {args: [projectDir: string, name: string, content: string, options?: { overwrite?: boolean }];result: ExecResult;