Validate completed reports before cancellation projection, retain valid Evidence and captured command diagnostics, and convert raw cleanup partials at their application owner. Record partial Evidence consistently across MCP handlers. Keep page inspection separate from unfinished bundle analysis. Refs #4261
135 lines
4.4 KiB
TypeScript
135 lines
4.4 KiB
TypeScript
import type { ExecutionOptions } from "../AnalysisProvider.js";
|
|
import type { ElectronObservationPort } from "./ElectronObservationPort.js";
|
|
import type { Evidence } from "../../domain/evidence.js";
|
|
import type {
|
|
InspectElectronPageInput,
|
|
ListElectronTargetsInput,
|
|
} from "../../domain/javascript/electronObservation.js";
|
|
import {
|
|
AnalysisCancelledError,
|
|
AnalysisCapabilityUnavailableError,
|
|
AnalysisOutputError,
|
|
} from "../../domain/analysisErrorCore.js";
|
|
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
|
|
import { err, ok, type Result } from "../../domain/result.js";
|
|
import { createElectronEvidence } from "./ElectronEvidence.js";
|
|
import {
|
|
electronPageInspectionSchema,
|
|
electronTargetListSchema,
|
|
} from "../../domain/javascript/electronObservation.js";
|
|
import { completeProviderEvidence } from "../ProviderOperationExecution.js";
|
|
|
|
/** List local file pages exposed by the explicitly selected endpoint. */
|
|
export const listElectronTargets = async (
|
|
provider: ElectronObservationPort | undefined,
|
|
input: ListElectronTargetsInput,
|
|
options: ExecutionOptions = {},
|
|
): Promise<Result<Evidence, AnalysisError>> => {
|
|
const ready = requireProvider(provider, "list_electron_targets");
|
|
if (!ready.ok) return ready;
|
|
if (options.signal?.aborted)
|
|
return err(new AnalysisCancelledError("list_electron_targets"));
|
|
const result = await ready.value.listTargets(input, options);
|
|
if (!result.ok) return result;
|
|
const report = electronTargetListSchema.safeParse(result.value);
|
|
if (!report.success)
|
|
return err(
|
|
new AnalysisOutputError(
|
|
"list_electron_targets",
|
|
"Electron provider returned a malformed target list.",
|
|
{ cause: report.error },
|
|
),
|
|
);
|
|
return completeProviderEvidence(
|
|
ok(
|
|
createElectronEvidence(
|
|
"list_electron_targets",
|
|
input,
|
|
report.data,
|
|
ready.value.identity(),
|
|
),
|
|
),
|
|
"list_electron_targets",
|
|
options,
|
|
);
|
|
};
|
|
|
|
/** Inspect one local file page from the explicitly selected endpoint. */
|
|
export const inspectElectronPage = async (
|
|
provider: ElectronObservationPort | undefined,
|
|
input: InspectElectronPageInput,
|
|
options: ExecutionOptions = {},
|
|
): Promise<Result<Evidence, AnalysisError>> => {
|
|
const ready = requireProvider(provider, "inspect_electron_page");
|
|
if (!ready.ok) return ready;
|
|
if (options.signal?.aborted)
|
|
return err(new AnalysisCancelledError("inspect_electron_page"));
|
|
const result = await ready.value.inspectPage(input, options);
|
|
if (!result.ok) {
|
|
if (
|
|
result.error.partialObservation !== undefined &&
|
|
!("evidence_id" in result.error.partialObservation) &&
|
|
"target" in result.error.partialObservation &&
|
|
"dom" in result.error.partialObservation &&
|
|
"capture_window" in result.error.partialObservation
|
|
) {
|
|
const report = electronPageInspectionSchema.safeParse(
|
|
result.error.partialObservation,
|
|
);
|
|
if (!report.success) {
|
|
if (result.error instanceof AnalysisCancelledError)
|
|
return err(
|
|
new AnalysisOutputError(
|
|
"inspect_electron_page",
|
|
"Electron provider retained a malformed completed page inspection.",
|
|
{ cause: report.error },
|
|
),
|
|
);
|
|
return result;
|
|
}
|
|
result.error.partialObservation = createElectronEvidence(
|
|
"inspect_electron_page",
|
|
input,
|
|
report.data,
|
|
ready.value.identity(),
|
|
);
|
|
}
|
|
return result;
|
|
}
|
|
const report = electronPageInspectionSchema.safeParse(result.value);
|
|
if (!report.success)
|
|
return err(
|
|
new AnalysisOutputError(
|
|
"inspect_electron_page",
|
|
"Electron provider returned a malformed page inspection.",
|
|
{ cause: report.error },
|
|
),
|
|
);
|
|
return completeProviderEvidence(
|
|
ok(
|
|
createElectronEvidence(
|
|
"inspect_electron_page",
|
|
input,
|
|
report.data,
|
|
ready.value.identity(),
|
|
),
|
|
),
|
|
"inspect_electron_page",
|
|
options,
|
|
);
|
|
};
|
|
|
|
const requireProvider = (
|
|
provider: ElectronObservationPort | undefined,
|
|
operation: "list_electron_targets" | "inspect_electron_page",
|
|
): Result<ElectronObservationPort, AnalysisError> =>
|
|
provider === undefined
|
|
? err(
|
|
new AnalysisCapabilityUnavailableError(
|
|
"rea-cdp-electron",
|
|
operation,
|
|
"Electron observation provider is not configured",
|
|
),
|
|
)
|
|
: ok(provider);
|