Files
rea/src/application/DirectAnalysis.ts
T

84 lines
3.0 KiB
TypeScript
Raw Normal View History

2026-07-12 06:34:27 +08:00
import { parseConfig } from "../config.js";
2026-07-12 19:32:13 +08:00
import type { JsonValue } from "../domain/jsonValue.js";
2026-07-13 04:27:15 +08:00
import { EnhancedTools } from "./EnhancedTools.js";
import { createBinarySession } from "./runtime.js";
2026-07-12 10:57:34 +08:00
import { silentLogger, type Logger } from "../logger.js";
2026-07-12 19:32:13 +08:00
import { createEvidence } from "../domain/evidence.js";
2026-07-12 21:12:18 +00:00
import type { NativeToolName } from "../contracts/nativeToolContracts.js";
import type { ArtifactToolName } from "../contracts/artifactToolContracts.js";
const WORKFLOW_PROVIDER = {
id: "rea-workflow",
name: "REA composed investigation workflow",
version: "1",
} as const;
2026-07-12 06:34:27 +08:00
/**
* Open one binary, execute one tool, and always release the bridge session.
* Unlike MCP mode, every CLI invocation is intentionally isolated and does not
* retain a target or bridge resources for a subsequent command.
*/
2026-07-12 06:34:27 +08:00
export const runDirectAnalysis = async (
path: string,
tool: "binary_overview" | "procedure_pseudo_code",
arguments_: Readonly<Record<string, JsonValue>>,
2026-07-12 10:57:34 +08:00
logger: Logger = silentLogger,
2026-07-12 21:12:18 +00:00
): Promise<JsonValue> => runAnalysis(path, tool, arguments_, logger);
/** Execute one provider-native semantic operation with atomic provenance. */
export const runProviderAnalysis = async (
path: string,
tool: NativeToolName | ArtifactToolName,
arguments_: Readonly<Record<string, JsonValue>>,
logger: Logger = silentLogger,
): Promise<JsonValue> => runAnalysis(path, tool, arguments_, logger);
const runAnalysis = async (
path: string,
tool:
| NativeToolName
| ArtifactToolName
| "binary_overview"
| "procedure_pseudo_code",
arguments_: Readonly<Record<string, JsonValue>>,
logger: Logger,
2026-07-12 06:34:27 +08:00
): Promise<JsonValue> => {
const config = parseConfig(process.env);
if (!config.ok)
return { error: config.error._tag, message: config.error.message };
2026-07-12 10:57:34 +08:00
const session = createBinarySession(config.value, logger);
2026-07-12 06:34:27 +08:00
try {
const opened = await session.open(path);
if (!opened.ok)
return { error: opened.error._tag, message: opened.error.message };
2026-07-12 21:12:18 +00:00
if (tool === "binary_overview") {
const result = await new EnhancedTools(session).execute(tool, arguments_);
return result.ok
? createEvidence(opened.value, WORKFLOW_PROVIDER, {
operation: tool,
parameters: arguments_,
result: result.value,
confidence: "derived",
limitations: ["Derived by an REA composed workflow."],
})
: { error: result.error._tag, message: result.error.message };
}
2026-07-12 19:32:13 +08:00
const result = await session.execute(tool, arguments_);
2026-07-12 06:34:27 +08:00
return result.ok
2026-07-12 21:12:18 +00:00
? createEvidence(
result.value.subject ?? opened.value,
result.value.provider,
{
operation: tool,
parameters: arguments_,
result: result.value.result,
rawResult: result.value.rawResult,
limitations: result.value.limitations,
locations: result.value.locations,
},
)
2026-07-12 06:34:27 +08:00
: { error: result.error._tag, message: result.error.message };
} finally {
await session.close();
}
};