82 lines
2.6 KiB
TypeScript
82 lines
2.6 KiB
TypeScript
import type {
|
|
PublicRectificationMethod,
|
|
PublicRectificationTool,
|
|
RectificationActivityStatus,
|
|
} from "./rectification-agentic/v9/public-receipt";
|
|
|
|
export type CompletedActivityReceiptView = Readonly<{
|
|
steps: readonly PublicRectificationTool[];
|
|
methods: readonly PublicRectificationMethod[];
|
|
failedTool?: PublicRectificationTool;
|
|
}>;
|
|
|
|
type ToolTerminalStatus = "completed" | "failed";
|
|
|
|
export type RectificationActivityReceiptState = Readonly<{
|
|
completedSteps: readonly PublicRectificationTool[];
|
|
methodsByTool: Readonly<Partial<Record<PublicRectificationTool, readonly PublicRectificationMethod[]>>>;
|
|
terminalStatus: Readonly<Partial<Record<PublicRectificationTool, ToolTerminalStatus>>>;
|
|
failureOrder: readonly PublicRectificationTool[];
|
|
}>;
|
|
|
|
type ReceiptActivityEvent = Readonly<{
|
|
tool: PublicRectificationTool;
|
|
status: RectificationActivityStatus;
|
|
methods?: readonly PublicRectificationMethod[];
|
|
}>;
|
|
|
|
export function createRectificationActivityReceiptState(): RectificationActivityReceiptState {
|
|
return {
|
|
completedSteps: [],
|
|
methodsByTool: {},
|
|
terminalStatus: {},
|
|
failureOrder: [],
|
|
};
|
|
}
|
|
|
|
export function reduceRectificationActivityReceipt(
|
|
state: RectificationActivityReceiptState,
|
|
event: ReceiptActivityEvent,
|
|
): RectificationActivityReceiptState {
|
|
if (event.status === "started") return state;
|
|
|
|
const terminalStatus = { ...state.terminalStatus, [event.tool]: event.status };
|
|
if (event.status === "completed") {
|
|
return {
|
|
completedSteps: state.completedSteps.includes(event.tool)
|
|
? state.completedSteps
|
|
: [...state.completedSteps, event.tool],
|
|
methodsByTool: {
|
|
...state.methodsByTool,
|
|
[event.tool]: [...new Set(event.methods ?? [])],
|
|
},
|
|
terminalStatus,
|
|
failureOrder: state.failureOrder.filter((tool) => tool !== event.tool),
|
|
};
|
|
}
|
|
|
|
return {
|
|
...state,
|
|
completedSteps: state.completedSteps.filter((tool) => tool !== event.tool),
|
|
methodsByTool: { ...state.methodsByTool, [event.tool]: [] },
|
|
terminalStatus,
|
|
failureOrder: [
|
|
...state.failureOrder.filter((tool) => tool !== event.tool),
|
|
event.tool,
|
|
],
|
|
};
|
|
}
|
|
|
|
export function receiptFromRectificationActivityState(
|
|
state: RectificationActivityReceiptState,
|
|
): CompletedActivityReceiptView {
|
|
const failedTool = [...state.failureOrder]
|
|
.reverse()
|
|
.find((tool) => state.terminalStatus[tool] === "failed");
|
|
return {
|
|
steps: [...state.completedSteps],
|
|
methods: [...new Set(state.completedSteps.flatMap((tool) => state.methodsByTool[tool] ?? []))],
|
|
...(failedTool ? { failedTool } : {}),
|
|
};
|
|
}
|