refactor: replace callReducer with callReducerStrict for improved error handling in mcp-review routes

This commit is contained in:
Aadesh Kheria 2026-04-20 12:24:33 -07:00
parent 1ccef9cbab
commit 4965534c3d
7 changed files with 28 additions and 12 deletions

View File

@ -1,4 +1,4 @@
import { callReducer } from "@/lib/ai/mcp-logger";
import { callReducerStrict } from "@/lib/ai/mcp-logger";
import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler";
import { adaptSchema, yupBoolean, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields";
import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env";
@ -34,7 +34,7 @@ export const POST = createSmartRouteHandler({
}
const token = getEnvVariable("STACK_MCP_LOG_TOKEN");
await callReducer("add_manual_qa", [
await callReducerStrict("add_manual_qa", [
token,
body.question,
body.answer,

View File

@ -1,4 +1,4 @@
import { callReducer } from "@/lib/ai/mcp-logger";
import { callReducerStrict } from "@/lib/ai/mcp-logger";
import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler";
import { adaptSchema, yupBoolean, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields";
import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env";
@ -31,7 +31,7 @@ export const POST = createSmartRouteHandler({
}
const token = getEnvVariable("STACK_MCP_LOG_TOKEN");
await callReducer("delete_qa_entry", [
await callReducerStrict("delete_qa_entry", [
token,
body.correlationId,
]);

View File

@ -1,4 +1,4 @@
import { callReducer } from "@/lib/ai/mcp-logger";
import { callReducerStrict } from "@/lib/ai/mcp-logger";
import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler";
import { adaptSchema, yupBoolean, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields";
import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env";
@ -32,7 +32,7 @@ export const POST = createSmartRouteHandler({
}
const token = getEnvVariable("STACK_MCP_LOG_TOKEN");
await callReducer("mark_human_reviewed", [
await callReducerStrict("mark_human_reviewed", [
token,
body.correlationId,
user.display_name ?? user.primary_email ?? user.id,

View File

@ -1,4 +1,4 @@
import { callReducer } from "@/lib/ai/mcp-logger";
import { callReducerStrict } from "@/lib/ai/mcp-logger";
import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler";
import { adaptSchema, yupBoolean, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields";
import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env";
@ -32,7 +32,7 @@ export const POST = createSmartRouteHandler({
}
const token = getEnvVariable("STACK_MCP_LOG_TOKEN");
await callReducer("unmark_human_reviewed", [
await callReducerStrict("unmark_human_reviewed", [
token,
body.correlationId,
]);

View File

@ -1,4 +1,4 @@
import { callReducer } from "@/lib/ai/mcp-logger";
import { callReducerStrict } from "@/lib/ai/mcp-logger";
import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler";
import { adaptSchema, yupBoolean, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields";
import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env";
@ -35,7 +35,7 @@ export const POST = createSmartRouteHandler({
}
const token = getEnvVariable("STACK_MCP_LOG_TOKEN");
await callReducer("update_human_correction", [
await callReducerStrict("update_human_correction", [
token,
body.correlationId,
body.correctedQuestion,

View File

@ -1,4 +1,4 @@
import { callReducer } from "@/lib/ai/mcp-logger";
import { callReducerStrict } from "@/lib/ai/mcp-logger";
import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler";
import { adaptSchema, yupBoolean, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields";
import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env";
@ -35,7 +35,7 @@ export const POST = createSmartRouteHandler({
}
const token = getEnvVariable("STACK_MCP_LOG_TOKEN");
await callReducer("add_operator", [
await callReducerStrict("add_operator", [
token,
[`0x${body.identity}`],
user.id,

View File

@ -79,6 +79,22 @@ export async function callReducer(reducer: string, args: unknown[]): Promise<voi
await rawCallReducer(token, reducer, args);
}
/**
* Like {@link callReducer} but throws when SpacetimeDB isn't configured, rather
* than no-opping. Use for endpoints where the client treats a 200 as proof the
* mutation actually ran (reviewer enrollment, human QA edits, deletions).
* Fire-and-forget logging paths should keep using the best-effort variant.
*/
export async function callReducerStrict(reducer: string, args: unknown[]): Promise<void> {
const token = await getServiceToken();
if (!token) {
throw new StackAssertionError(
`SpacetimeDB is not configured (STACK_SPACETIMEDB_URL is empty). Reducer ${reducer} cannot run.`
);
}
await rawCallReducer(token, reducer, args);
}
/**
* Wraps a nullable value in the SpacetimeDB tagged-variant encoding expected
* by HTTP reducer calls for `Option<T>` arguments. Use for every reducer arg