Skip to content

The VS Code of video editing, built for agents.

TypeScript

Implement CLI uninstallation and enhance menu integration

konstantin-paulus7ddd530eb25b
diff --git a/apps/desktop/src/cli-install.ts b/apps/desktop/src/cli-install.ts
index 8e7d800d..79307ddc 100644
--- a/apps/desktop/src/cli-install.ts
+++ b/apps/desktop/src/cli-install.ts
@@ -4,10 +4,18 @@
import { app } from "electron";
import { execFile } from "node:child_process";
-import { existsSync } from "node:fs";
+import { existsSync, lstatSync } from "node:fs";
import { join } from "node:path";
import type { CliInstallResult } from "./main-channels";
+// Outcome of taking the symlink back out. "absent" means there was none to
+// remove; "cancelled" that the admin prompt was dismissed and it stays.
+export type CliUninstallResult =
+ | { status: "removed" }
+ | { status: "absent" }
+ | { status: "cancelled" }
+ | { status: "error"; error: string };
+
export const CLI_LINK_PATH = "/usr/local/bin/dapi";
// The dev workflow links the workspace build into Homebrew's bin instead
@@ -18,6 +26,15 @@ export function isCliInstalled(): boolean {
return existsSync(CLI_LINK_PATH) || existsSync(DEV_LINK_PATH);
}
+/**
+ * Whether the link this app creates is there — dangling or not, since a
+ * link left behind by a deleted bundle is exactly what removal is for.
+ * `existsSync` follows links and would miss that case.
+ */
+export function isCliLinked(): boolean {
+ return lstatSync(CLI_LINK_PATH, { throwIfNoEntry: false })?.isSymbolicLink() ?? false;
+}
+
// Linking into /usr/local/bin needs elevation; osascript shows the standard
// macOS admin prompt so the app itself never asks for credentials.
function linkCli(): Promise<void> {
@@ -29,6 +46,14 @@ function linkCli(): Promise<void> {
});
}
+// The same elevation for the removal, with the same prompt.
+function unlinkCli(): Promise<void> {
+ const script = `do shell script "rm -f '${CLI_LINK_PATH}'" with administrator privileges`;
+ return new Promise((resolve, reject) => {
+ execFile("osascript", ["-e", script], (err) => (err ? reject(err) : resolve()));
+ });
+}
+
export async function installCli(): Promise<CliInstallResult> {
if (!app.isPackaged) {
return {
@@ -45,3 +70,25 @@ export async function installCli(): Promise<CliInstallResult> {
return { status: "error", error: message };
}
}
+
+/**
+ * Removes the `dapi` link from PATH. Only the link at {@link CLI_LINK_PATH}
+ * is touched, and only when it is a symlink: a real binary somebody put
+ * there is not ours to delete, and the dev link in Homebrew's bin belongs
+ * to `npm run symlink:remove`.
+ */
+export async function uninstallCli(): Promise<CliUninstallResult> {
+ const stat = lstatSync(CLI_LINK_PATH, { throwIfNoEntry: false });
+ if (!stat) return { status: "absent" };
+ if (!stat.isSymbolicLink()) {
+ return { status: "error", error: `${CLI_LINK_PATH} is not a link this app created, so it was left alone.` };
+ }
+ try {
+ await unlinkCli();
+ return { status: "removed" };
+ } catch (e) {
+ const message = (e as Error).message ?? "";
+ if (message.includes("-128")) return { status: "cancelled" }; // user cancelled the admin prompt
+ return { status: "error", error: message };
+ }
+}
diff --git a/apps/desktop/src/menu.ts b/apps/desktop/src/menu.ts
index 088c0718..d8aaaa3b 100644
--- a/apps/desktop/src/menu.ts
+++ b/apps/desktop/src/menu.ts
@@ -5,10 +5,11 @@
import { app, dialog, Menu } from "electron";
import type { MenuItemConstructorOptions } from "electron";
-import { CLI_LINK_PATH } from "./cli-install";
-import { registeredAgents, unregisterMcp } from "./mcp-install";
-import { ensureSetup } from "./setup";
+import { CLI_LINK_PATH, isCliLinked } from "./cli-install";
+import { registeredAgents } from "./mcp-install";
+import { disconnectSetup, ensureSetup, setupConnected } from "./setup";
+import type { CliUninstallResult } from "./cli-install";
import type { CliSetupResult } from "./main-channels";
/** The line the dialog gets about the CLI, or null when there is nothing to say. */
@@ -54,15 +55,30 @@ async function connectAgentsFromMenu() {
message: "Your agents are connected.",
detail: detail.join("\n\n"),
});
- refreshAppMenu();
+}
+
+/** The line the disconnect dialog gets about the CLI, or null when there was nothing to remove. */
+function cliRemovalDetail(result: CliUninstallResult): string | null {
+ switch (result.status) {
+ case "removed":
+ return `The dapi command line tool was removed from ${CLI_LINK_PATH}.`;
+ case "cancelled":
+ return `The dapi command line tool is still at ${CLI_LINK_PATH} — the admin prompt was dismissed.`;
+ case "error":
+ return `The dapi command line tool could not be removed: ${result.error}`;
+ case "absent":
+ return null;
+ }
}
// The inverse of the item above: takes the app's MCP entry back out of every
-// agent config that has one. Reversible from the same menu, so a plain
-// confirm is enough; the item is only shown while there is something to undo.
+// agent config that has one, and the dapi link off PATH. Reversible from the
+// same menu, so a plain confirm is enough; the item is only shown while
+// there is something to undo.
async function disconnectAgentsFromMenu() {
const agents = registeredAgents();
- if (agents.length === 0) {
+ const linked = isCliLinked();
+ if (agents.length === 0 && !linked) {
refreshAppMenu();
return;
}
@@ -71,33 +87,40 @@ async function disconnectAgentsFromMenu() {
type: "question",
message: "Disconnect your agents?",
detail: [
- `Diffusion Studio will be removed from the MCP configuration of ${agents.join(", ")}. Restart the agent to pick it up.`,
- "The dapi command line tool stays installed. Sending a prompt to an agent from the dashboard connects it again.",
- ].join("\n\n"),
+ agents.length > 0 ? `Diffusion Studio will be removed from the MCP configuration of ${agents.join(", ")}. Restart the agent to pick it up.` : null,
+ linked ? `The dapi command line tool will be removed from ${CLI_LINK_PATH}, which needs your admin password.` : null,
+ "Sending a prompt to an agent from the dashboard connects it again.",
+ ].filter((line): line is string => line !== null).join("\n\n"),
buttons: ["Disconnect", "Cancel"],
defaultId: 0,
cancelId: 1,
});
if (response !== 0) return;
- const result = unregisterMcp();
- refreshAppMenu();
+ const { mcp, cli } = await disconnectSetup();
- if (result.agents.length === 0) {
+ const failed = [
+ ...mcp.failures,
+ cli.status === "error" ? cli.error : null,
+ ].filter((line): line is string => line !== null);
+ const undone = mcp.agents.length > 0 || cli.status === "removed";
+
+ if (!undone) {
await dialog.showMessageBox({
- type: "error",
- message: "Could not disconnect your agents.",
- detail: result.failures.join("\n"),
+ type: cli.status === "cancelled" ? "info" : "error",
+ message: "Your agents were not disconnected.",
+ detail: [cliRemovalDetail(cli), ...mcp.failures].filter((line): line is string => line !== null).join("\n\n"),
});
return;
}
await dialog.showMessageBox({
- type: result.failures.length > 0 ? "warning" : "info",
+ type: failed.length > 0 || cli.status === "cancelled" ? "warning" : "info",
message: "Your agents are disconnected.",
detail: [
- `Disconnected: ${result.agents.join(", ")}. Restart the agent to pick it up.`,
- result.failures.length > 0 ? `Could not update:\n${result.failures.join("\n")}` : null,
+ mcp.agents.length > 0 ? `Disconnected: ${mcp.agents.join(", ")}. Restart the agent to pick it up.` : null,
+ cliRemovalDetail(cli),
+ mcp.failures.length > 0 ? `Could not update:\n${mcp.failures.join("\n")}` : null,
].filter((line): line is string => line !== null).join("\n\n"),
});
}
@@ -105,14 +128,14 @@ async function disconnectAgentsFromMenu() {
const DISCONNECT_ITEM_ID = "disconnect-agents";
/**
- * Re-reads the agent configs and shows or hides "Disconnect Agents…" to
- * match. Called after anything that writes those configs — the two menu
+ * Re-reads what setup left on this machine and shows or hides "Disconnect
+ * Agents…" to match. Called after anything that changes it — the two menu
* items, and setup runs the renderer asks for — so the menu never offers to
* undo a connection that is not there.
*/
export function refreshAppMenu() {
const item = Menu.getApplicationMenu()?.getMenuItemById(DISCONNECT_ITEM_ID);
- if (item) item.visible = registeredAgents().length > 0;
+ if (item) item.visible = setupConnected();
}
export function setupAppMenu() {
@@ -131,7 +154,7 @@ export function setupAppMenu() {
{
id: DISCONNECT_ITEM_ID,
label: "Disconnect Agents…",
- visible: registeredAgents().length > 0,
+ visible: setupConnected(),
click: disconnectAgentsFromMenu,
},
{ type: "separator" },
diff --git a/apps/desktop/src/setup.ts b/apps/desktop/src/setup.ts
index 27ad2b60..60b3fdcd 100644
--- a/apps/desktop/src/setup.ts
+++ b/apps/desktop/src/setup.ts
@@ -6,11 +6,13 @@ import { app } from "electron";
import { existsSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";
-import { installCli, isCliInstalled } from "./cli-install";
-import { healMcpRegistrations, mcpRegistered, registerMcp } from "./mcp-install";
+import { installCli, isCliInstalled, isCliLinked, uninstallCli } from "./cli-install";
+import { healMcpRegistrations, mcpRegistered, registerMcp, registeredAgents, unregisterMcp } from "./mcp-install";
import { pruneLegacySkills } from "./skills-cleanup";
import type { CliMode, CliSetupResult, SetupResult, SetupStatus } from "./main-channels";
+import type { CliUninstallResult } from "./cli-install";
+import type { McpUnregisterResult } from "./mcp-install";
import { refreshAppMenu } from "./menu";
// Linking the CLI needs an admin password, so a refusal has to be
@@ -71,6 +73,34 @@ export async function ensureSetup(mode: CliMode = "auto"): Promise<SetupResult>
return { mcp, cli, skills };
}
+/** What `disconnectSetup` did to each half of what `ensureSetup` put in place. */
+export type DisconnectResult = { mcp: McpUnregisterResult; cli: CliUninstallResult };
+
+/**
+ * Whether there is anything for `disconnectSetup` to undo: an agent config
+ * pointing at the app, or the `dapi` link on PATH.
+ */
+export function setupConnected(): boolean {
+ return registeredAgents().length > 0 || isCliLinked();
+}
+
+/**
+ * The inverse of `ensureSetup`: takes the app out of every agent config and
+ * the `dapi` link off PATH. The configs go first because they never prompt;
+ * the link comes second because it does, and a dismissed prompt should not
+ * leave the agents half-disconnected. A removed link is remembered as a
+ * refusal, so the next handoff does not immediately ask to put it back —
+ * only "Connect Agents…" does.
+ */
+export async function disconnectSetup(): Promise<DisconnectResult> {
+ const mcp = unregisterMcp();
+ const cli = await uninstallCli();
+ if (cli.status === "removed") rememberDecline(true);
+
+ refreshAppMenu();
+ return { mcp, cli };
+}
+
/**
* The launch pass. It repairs entries this app already owns.
*/