2026-07-12 07:44:21 +08:00
import { Cli , z } from "incur" ;
import { fileURLToPath } from "node:url" ;
import { runDoctor } from "./application/Doctor.js" ;
import { runDirectAnalysis } from "./application/DirectAnalysis.js" ;
import { runSetup } from "./application/Setup.js" ;
import { PRODUCT_IDENTITY } from "./identity.js" ;
2026-07-12 10:57:34 +08:00
import { createLogger , parseLogLevel , type Logger } from "./logger.js" ;
2026-07-12 07:44:21 +08:00
2026-07-12 08:45:35 +08:00
/**
* Build the one-shot Incur CLI without starting Hopper at import time.
* Analysis commands acquire and close their own sessions; bare `mcp` and
* `--mcp` are intercepted by the executable dispatcher before this module loads.
*/
2026-07-12 07:44:21 +08:00
export const createCli = () : ReturnType < typeof Cli.create > => {
2026-07-12 10:57:34 +08:00
const logger = createLogger (
"cli" ,
process . env . REA_LOG_LEVEL === undefined
? "silent"
: parseLogLevel ( process . env . REA_LOG_LEVEL ),
);
2026-07-12 07:44:21 +08:00
const cli = Cli . create ( PRODUCT_IDENTITY . cliBinary , {
version : "0.1.0" ,
2026-07-12 20:49:30 +08:00
description :
"Reverse engineer anything from your terminal or coding agent." ,
2026-07-12 07:44:21 +08:00
mcp : {
command : PRODUCT_IDENTITY.mcpCommand ,
instructions :
2026-07-12 20:49:30 +08:00
"Ask what software, artifact, protocol, or behavior the user wants to understand, then choose the available investigation capabilities that can produce evidence." ,
2026-07-12 07:44:21 +08:00
},
sync : {
cwd : fileURLToPath ( new URL ( ".." , import . meta . url )),
include : [ "skills/*" ],
suggestions : [
2026-07-12 20:49:30 +08:00
"understand how a software feature works" ,
"investigate an artifact or observed behavior" ,
2026-07-12 12:26:16 +08:00
"check my REA setup" ,
2026-07-12 07:44:21 +08:00
],
},
});
cli . command ( "setup" , {
2026-07-12 12:26:16 +08:00
description : "Install requirements and configure coding agents" ,
2026-07-12 07:44:21 +08:00
options : z.object ({
yes : z
. boolean ()
. default ( false )
. describe ( "Approve prerequisite installation" ),
}),
alias : { yes : "y" },
2026-07-12 10:57:34 +08:00
run : ({ options }) =>
logCliCommand ( logger , "setup" , () => runSetup ( options . yes )),
2026-07-12 07:44:21 +08:00
});
cli . command ( "doctor" , {
2026-07-12 12:26:16 +08:00
description : "Check whether REA is ready" ,
2026-07-12 07:44:21 +08:00
options : z.object ({
2026-07-12 12:26:16 +08:00
target : z.string (). optional (). describe ( "Optional app path to check" ),
2026-07-12 07:44:21 +08:00
}),
2026-07-12 10:57:34 +08:00
run : ({ options }) =>
logCliCommand ( logger , "doctor" , () => runDoctor ( options . target )),
2026-07-12 07:44:21 +08:00
});
cli . command ( "analyze" , {
2026-07-12 12:26:16 +08:00
description : "Get an overview of an app" ,
2026-07-12 07:44:21 +08:00
args : z.object ({
2026-07-12 12:26:16 +08:00
path : z.string (). describe ( "App, program, or Hopper database path" ),
2026-07-12 07:44:21 +08:00
}),
2026-07-12 10:57:34 +08:00
run : ({ args }) =>
logCliCommand ( logger , "analyze" , () =>
runDirectAnalysis ( args . path , "binary_overview" , {}, logger ),
),
2026-07-12 07:44:21 +08:00
});
cli . command ( "decompile" , {
2026-07-12 12:26:16 +08:00
description : "Read one part of an app as code" ,
2026-07-12 07:44:21 +08:00
args : z.object ({
2026-07-12 12:26:16 +08:00
path : z.string (). describe ( "App or program path" ),
2026-07-12 07:44:21 +08:00
address : z.string (). describe ( "Procedure address" ),
}),
run : ({ args }) =>
2026-07-12 10:57:34 +08:00
logCliCommand ( logger , "decompile" , () =>
runDirectAnalysis (
args . path ,
"procedure_pseudo_code" ,
{ procedure : args.address },
logger ,
),
),
2026-07-12 07:44:21 +08:00
});
return cli ;
};
2026-07-12 10:57:34 +08:00
const logCliCommand = async < Value >(
logger : Logger ,
command : string ,
execute : () => Promise < Value >,
) : Promise < Value > => {
const startedAt = performance . now ();
try {
const value = await execute ();
logger . info (
{
command ,
durationMs : Math.round (( performance . now () - startedAt ) * 100 ) / 100 ,
status : "ok" ,
},
"CLI command completed" ,
);
return value ;
} catch ( cause : unknown ) {
logger . error (
{
command ,
durationMs : Math.round (( performance . now () - startedAt ) * 100 ) / 100 ,
status : "error" ,
},
"CLI command failed" ,
);
throw cause ;
}
};