refactor(cli): share runtime between CLI and MCP

This commit is contained in:
morluto
2026-07-12 07:44:21 +08:00
parent d9c9025ffb
commit 97541d8dad
6 changed files with 113 additions and 109 deletions
+2 -23
View File
@@ -1,10 +1,6 @@
import { fileURLToPath } from "node:url";
import { parseConfig } from "../config.js";
import { HopperApplicationLauncher } from "../hopper/BridgeLauncher.js";
import { HopperClient } from "../hopper/HopperClient.js";
import type { JsonValue } from "../hopper/protocol.js";
import { BinarySession } from "./BinarySession.js";
import { createBinarySession } from "./runtime.js";
/** Open one binary, execute one tool, and always release the target session. */
export const runDirectAnalysis = async (
@@ -15,24 +11,7 @@ export const runDirectAnalysis = async (
const config = parseConfig(process.env);
if (!config.ok)
return { error: config.error._tag, message: config.error.message };
const bridgeScriptPath = fileURLToPath(
new URL("../../bridge/hopper_bridge.py", import.meta.url),
);
const session = new BinarySession(
(target) =>
new HopperClient({
launcher: new HopperApplicationLauncher({
launcherPath: config.value.hopperLauncherPath,
targetPath: target.path,
targetKind: target.kind,
loaderArgs:
config.value.hopperLoaderArgs.length > 0
? config.value.hopperLoaderArgs
: target.loaderArgs,
bridgeScriptPath,
}),
}),
);
const session = createBinarySession(config.value);
try {
const opened = await session.open(path);
if (!opened.ok)
+28
View File
@@ -0,0 +1,28 @@
import { fileURLToPath } from "node:url";
import type { AppConfig } from "../config.js";
import { BinarySession } from "./BinarySession.js";
import { HopperApplicationLauncher } from "../hopper/BridgeLauncher.js";
import { HopperClient } from "../hopper/HopperClient.js";
/** Create the target-switching session used by both the CLI and MCP adapters. */
export const createBinarySession = (config: AppConfig): BinarySession => {
const bridgeScriptPath = fileURLToPath(
new URL("../../bridge/hopper_bridge.py", import.meta.url),
);
return new BinarySession(
(target) =>
new HopperClient({
launcher: new HopperApplicationLauncher({
launcherPath: config.hopperLauncherPath,
targetPath: target.path,
targetKind: target.kind,
loaderArgs:
config.hopperLoaderArgs.length > 0
? config.hopperLoaderArgs
: target.loaderArgs,
bridgeScriptPath,
}),
}),
);
};