import { sec0, AgentManager } from "sec0-sdk/instrumentation";
import { createContextualEvaluatorManager } from "sec0-sdk/evaluator";
const evaluator = createContextualEvaluatorManager({
evaluatorSource: "local",
evaluatorMode: "sync",
local: {
denyThreshold: 0.85,
escalateThreshold: 0.45,
},
});
class RefundMiddleware {
@sec0.middleware()
async enforce(
input: { orderId: string; amount: number; reason: string },
manager: AgentManager,
) {
const evaluation = await evaluator.evaluate({
action: {
kind: "refund",
summary: "Issue a customer refund",
sideEffect: true,
disclosure: false,
crossesBoundary: true,
tool: {
server: "payments",
name: "refundCharge",
version: "1.0",
},
target: {
boundary: "payments",
},
},
actor: {
id: manager.nodeId,
role: "refund-agent",
},
purpose: {
summary: "Resolve a refund request",
justification: input.reason,
},
authority: {
grantedScopes: ["payments.refund"],
allowedBoundaries: ["payments"],
approvals: [],
delegations: [],
},
runtimeContext: {
integrationSurface: "sec0",
executionLayer: "middleware",
runId: manager.runId,
workflowState: {
orderId: input.orderId,
amount: input.amount,
},
unresolvedPrerequisites: [],
},
sourceUse: {
sources: [],
},
constraints: {
hard: [],
soft: [],
requiredPrerequisites: [],
requiredApprovals: [],
forbiddenBoundaries: [],
},
metadata: {},
});
if (evaluation?.output.decision === "deny") {
throw new Error(evaluation.output.summary);
}
if (evaluation?.output.decision === "escalate") {
return {
status: "needs_review",
summary: evaluation.output.summary,
};
}
return await manager.invoke("PaymentsGateway.refund", input);
}
}