Files
rea/src/server/runDerivedOperation.ts
T
morluto 735c10059c refactor(domain): split error taxonomy into thematic modules (#636)
Move each error family out of domain/errors.ts into its own module
(base, core, provider selection/adapter, browser observation,
artifact, evidence, unknown registry, hopper, configuration) and
relocate AnalysisErrorProjection to analysisErrorProjection.
Migrate all 162 importers to the owning modules and delete
errors.ts. No re-export shims; behavior is unchanged.
2026-10-06 05:57:38 +08:00

60 lines
1.7 KiB
TypeScript

import type { ServerContext } from "@modelcontextprotocol/server";
import { setImmediate } from "node:timers/promises";
import {
AnalysisCancelledError,
AnalysisInputError,
} from "../domain/analysisErrorCore.js";
import { err, ok, type Result } from "../domain/result.js";
import { mcpProgressReporter, type McpProgressContext } from "./mcpProgress.js";
export interface DerivedOperationContext extends McpProgressContext {
readonly mcpReq: McpProgressContext["mcpReq"] &
Pick<ServerContext["mcpReq"], "signal">;
}
/** Run synchronous derived work with transport yields and a publication guard. */
export const runDerivedOperation = async <Value>(
context: DerivedOperationContext,
operation: string,
compute: () => Value,
): Promise<Result<Value, AnalysisCancelledError | AnalysisInputError>> => {
const progress = mcpProgressReporter(context);
await progress.report({
phase: "prepare",
completed: 0,
total: 2,
message: `Preparing ${operation}`,
});
await setImmediate();
if (context.mcpReq.signal.aborted)
return err(new AnalysisCancelledError(operation));
let value: Value;
try {
value = compute();
} catch (cause: unknown) {
return err(
cause instanceof AnalysisCancelledError
? cause
: new AnalysisInputError(operation, { cause }),
);
}
await progress.report({
phase: "compute",
completed: 1,
total: 2,
message: `Computed ${operation}`,
});
await setImmediate();
if (context.mcpReq.signal.aborted)
return err(new AnalysisCancelledError(operation));
await progress.report({
phase: "complete",
completed: 2,
total: 2,
message: `Completed ${operation}`,
terminal: true,
});
return ok(value);
};