2026-07-07 09:27:28 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
2026-09-08 12:17:03 +02:00
|
|
|
import { execFile } from "node:child_process";
|
2026-09-03 15:53:35 +02:00
|
|
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
2026-09-12 09:18:46 +02:00
|
|
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
|
|
|
import { MCP_URL } from "@diffusionstudio/dapi";
|
2026-09-03 15:53:35 +02:00
|
|
|
import { version } from "../../../package.json";
|
2026-09-03 10:04:02 +02:00
|
|
|
|
2026-09-03 15:53:35 +02:00
|
|
|
import type { ToolInput, ToolName, ToolOutput } from "@diffusionstudio/dapi";
|
2026-07-07 09:27:28 +02:00
|
|
|
|
2026-09-08 12:17:03 +02:00
|
|
|
export const APP_NAME = "Diffusion Studio";
|
|
|
|
|
|
2026-09-13 16:08:07 +02:00
|
|
|
// Renders and AI generation outlive the 60s default.
|
2026-09-08 12:17:03 +02:00
|
|
|
const TIMEOUTS: Record<string, number> = {
|
2026-09-09 11:37:43 +02:00
|
|
|
export: 3_600_000,
|
|
|
|
|
capture: 600_000,
|
|
|
|
|
media_transcribe: 600_000,
|
|
|
|
|
media_listen: 600_000,
|
2026-09-08 12:17:03 +02:00
|
|
|
};
|
|
|
|
|
|
2026-09-03 10:04:02 +02:00
|
|
|
/**
|
2026-09-12 09:18:46 +02:00
|
|
|
* Calls one tool in the running app over an MCP session on its HTTP
|
|
|
|
|
* endpoint — the same URL agents register. Typed by the catalog: the input is
|
|
|
|
|
* what the tool's schema accepts, the output its structured content. One
|
|
|
|
|
* session per call; a command makes one or two, and the process exits when
|
|
|
|
|
* it settles.
|
2026-09-03 10:04:02 +02:00
|
|
|
*/
|
2026-09-09 11:37:43 +02:00
|
|
|
export async function call<N extends ToolName>(name: N, input: ToolInput<N>): Promise<ToolOutput<N>> {
|
2026-09-12 09:18:46 +02:00
|
|
|
const client = await connect();
|
2026-09-09 11:37:43 +02:00
|
|
|
try {
|
|
|
|
|
const result = await client.callTool({ name, arguments: input as Record<string, unknown> }, undefined, {
|
|
|
|
|
timeout: TIMEOUTS[name] ?? 60_000,
|
|
|
|
|
});
|
2026-09-03 15:53:35 +02:00
|
|
|
if (result.isError) {
|
|
|
|
|
const text = (result.content as Array<{ type: string; text?: string }>)
|
|
|
|
|
.filter((block) => block.type === "text")
|
|
|
|
|
.map((block) => block.text)
|
|
|
|
|
.join("\n");
|
|
|
|
|
throw new Error(text || `${name} failed`);
|
|
|
|
|
}
|
|
|
|
|
return result.structuredContent as ToolOutput<N>;
|
2026-09-09 11:37:43 +02:00
|
|
|
} finally {
|
|
|
|
|
await client.close().catch(() => {});
|
|
|
|
|
}
|
2026-07-07 09:27:28 +02:00
|
|
|
}
|
|
|
|
|
|
2026-09-03 15:53:35 +02:00
|
|
|
/** Liveness: a round-trip through the app's MCP server. */
|
2026-09-09 11:37:43 +02:00
|
|
|
export async function ping(): Promise<void> {
|
2026-09-12 09:18:46 +02:00
|
|
|
const client = await connect();
|
2026-07-09 10:54:07 +02:00
|
|
|
try {
|
2026-09-09 11:37:43 +02:00
|
|
|
await client.ping();
|
2026-07-09 10:54:07 +02:00
|
|
|
} finally {
|
2026-09-03 15:53:35 +02:00
|
|
|
await client.close().catch(() => {});
|
2026-07-09 10:54:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-12 09:18:46 +02:00
|
|
|
// Connecting is where "the app is not running" shows up: the `initialize`
|
|
|
|
|
// request's fetch is refused, see `isAppDown`.
|
|
|
|
|
async function connect(): Promise<Client> {
|
|
|
|
|
const client = new Client({ name: "dapi", version });
|
|
|
|
|
await client.connect(new StreamableHTTPClientTransport(new URL(MCP_URL)));
|
|
|
|
|
return client;
|
2026-09-03 15:53:35 +02:00
|
|
|
}
|
|
|
|
|
|
2026-09-12 09:18:46 +02:00
|
|
|
/**
|
|
|
|
|
* Nothing is listening on the app's port. Node's fetch reports that as a
|
|
|
|
|
* `fetch failed` TypeError whose cause carries the errno, so the chain of
|
|
|
|
|
* causes is searched.
|
|
|
|
|
*/
|
2026-09-08 12:17:03 +02:00
|
|
|
export function isAppDown(e: unknown): boolean {
|
2026-09-12 09:18:46 +02:00
|
|
|
for (let error = e, depth = 0; error && depth < 5; error = (error as { cause?: unknown }).cause, depth++) {
|
|
|
|
|
const code = (error as NodeJS.ErrnoException).code;
|
|
|
|
|
if (code === "ECONNREFUSED" || code === "ECONNRESET") return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2026-09-08 12:17:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Launches the app, or surfaces the running instance: `open -a` on a running
|
|
|
|
|
* app only activates it, so this is safe to always run. macOS only; elsewhere
|
2026-09-12 09:18:46 +02:00
|
|
|
* it resolves false and the caller falls through to the connection.
|
2026-09-13 21:27:24 +02:00
|
|
|
*
|
|
|
|
|
* The bundled `dapi` runs as Electron with ELECTRON_RUN_AS_NODE=1, and `open`
|
|
|
|
|
* hands its environment to the app it launches — left in, the app boots as
|
|
|
|
|
* plain Node and never answers.
|
2026-09-08 12:17:03 +02:00
|
|
|
*/
|
|
|
|
|
export function launchApp(background: boolean): Promise<boolean> {
|
|
|
|
|
if (process.platform !== "darwin") return Promise.resolve(false);
|
|
|
|
|
const args = background ? ["-g", "-a", APP_NAME, "--args", "--hidden"] : ["-a", APP_NAME];
|
2026-09-13 21:27:24 +02:00
|
|
|
const { ELECTRON_RUN_AS_NODE: _, ...env } = process.env;
|
|
|
|
|
return new Promise((res) => execFile("open", args, { env }, (err) => res(!err)));
|
2026-09-08 12:17:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-09 11:37:43 +02:00
|
|
|
* Bridges the cold-start gap after launching the app: retries while the app
|
2026-09-12 09:18:46 +02:00
|
|
|
* looks down, until it answers a ping.
|
2026-09-08 12:17:03 +02:00
|
|
|
*/
|
2026-09-03 15:53:35 +02:00
|
|
|
export async function waitForApp(timeoutMs = 30000): Promise<void> {
|
2026-07-07 09:27:28 +02:00
|
|
|
const start = Date.now();
|
|
|
|
|
let lastError: unknown = null;
|
|
|
|
|
while (Date.now() - start < timeoutMs) {
|
|
|
|
|
try {
|
2026-09-09 11:37:43 +02:00
|
|
|
return await ping();
|
2026-07-07 09:27:28 +02:00
|
|
|
} catch (e) {
|
2026-09-08 12:17:03 +02:00
|
|
|
if (!isAppDown(e)) throw e;
|
2026-07-07 09:27:28 +02:00
|
|
|
lastError = e;
|
|
|
|
|
await new Promise((r) => setTimeout(r, 200));
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-09 11:37:43 +02:00
|
|
|
const detail = lastError instanceof Error ? ` (${lastError.message})` : "";
|
|
|
|
|
throw new Error(`${APP_NAME} did not answer within ${Math.round(timeoutMs / 1000)}s of launching${detail}`);
|
2026-07-07 09:27:28 +02:00
|
|
|
}
|