Files
rea/src/ghidra/protocol.ts
T
morluto b8a9da0c0f refactor(mcp): make analysis states unrepresentable (#468)
* refactor(config): parse capability settings into exact states

* refactor(runtime): encode replay and process outcomes exactly

* refactor: make analysis states unrepresentable

* refactor(mcp): register canonical schemas with SDK

* fix: preserve capability and catalog invariants

* fix(build): remove stale compiled modules

* fix(replay): update real verifier policy dependency

* fix(package): verify the complete MCP catalog

* fix(test): supply complete replay reload policy

* refactor(contracts): centralize output schema lookup

* refactor(mcp): delegate protocol boundaries to SDK

* fix(browser): approve real source capture

* fix(mcp): preserve catalog guidance
2026-08-10 02:28:11 +08:00

62 lines
1.8 KiB
TypeScript

import { z } from "zod";
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
import { err, ok, type Result } from "../domain/result.js";
const responseSchema = z.discriminatedUnion("ok", [
z.strictObject({
id: z.number().int().positive(),
ok: z.literal(true),
result: jsonValueSchema,
}),
z.strictObject({
id: z.number().int().positive(),
ok: z.literal(false),
error: z
.object({
code: z.string().min(1).max(128),
message: z.string().min(1).max(4_096),
})
.strict(),
}),
]);
/** Validated response emitted by REA's packaged Ghidra script. */
export type GhidraBridgeResponse = z.infer<typeof responseSchema>;
/** Expected operation failure returned by the authenticated Java bridge. */
export class GhidraRemoteError extends Error {
constructor(
readonly code: string,
message: string,
) {
super(message);
this.name = "GhidraRemoteError";
}
}
/** Parse one complete Java-bridge response without accepting extra fields. */
export const parseGhidraResponseLine = (
line: string,
): Result<GhidraBridgeResponse, Error> => {
try {
const parsed: unknown = JSON.parse(line);
const response = responseSchema.safeParse(parsed);
return response.success
? ok(response.data)
: err(new Error("Ghidra bridge response has an invalid shape"));
} catch (cause: unknown) {
return err(
new Error("Ghidra bridge response is not valid JSON", { cause }),
);
}
};
/** Project one validated response into its result or remote failure. */
export const ghidraResponseResult = (
response: GhidraBridgeResponse,
): Result<JsonValue, GhidraRemoteError> =>
response.ok
? ok(response.result)
: err(new GhidraRemoteError(response.error.code, response.error.message));