refactor(builder): type the AI-response/patch boundary

applyPatch's `ops` param was typed `any[]`, discarding the SitesmithPatchOp
discriminated union that SitesmithResponse.ops already carried at the call
site. Type it ops: SitesmithPatchOp[] so op.props/op.tree/op.op narrow
correctly per-variant in the switch instead of being `any`, and update the
unit tests' inline op literals to SitesmithPatchOp[] so tsc checks them too.

SerializedTreeNode/SitesmithPatchOp themselves, and buildNodeTree/
sanitizeAiTree's tree params, were already typed from a prior task -- this
closes the one remaining any at the op-handling site. Runtime validation
(protected-key guard, style-merge guard, unknown-resolvedName soft-skip) is
unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-12 15:30:56 -07:00
parent b88b242b6a
commit ebb8fe1027
2 changed files with 13 additions and 7 deletions
+8 -3
View File
@@ -1,7 +1,7 @@
import { useEditor } from '@craftjs/core';
import type { NodeTree } from '@craftjs/core';
import { usePages } from '../state/PageContext';
import { SitesmithResponse, SerializedTreeNode } from '../types/sitesmith';
import { SitesmithResponse, SerializedTreeNode, SitesmithPatchOp } from '../types/sitesmith';
import { sanitizeAiTree, flattenTreeForCraft } from './craft-tree';
// Re-exported for existing callers/tests that import sanitizeAiTree from
@@ -155,7 +155,7 @@ export function useApplyAiResponse() {
function applyPatch(
actions: any,
query: any,
ops: any[],
ops: SitesmithPatchOp[],
): { ok: boolean; message?: string } {
for (const op of ops) {
const id = findNodeIdByAiNodeId(query, op.node_id);
@@ -237,7 +237,12 @@ function applyPatch(
break;
default:
console.warn('sitesmith patch: unknown op', (op as any).op);
// Every SitesmithPatchOp variant is handled above, so `op` is typed
// `never` here at compile time — but the AI response is untrusted
// JSON (see useSitesmith.ts's `j: SendResult = await r.json()`), so
// a malformed/unrecognized `op` field can still reach this branch at
// runtime. Log the raw value rather than assuming a shape.
console.warn('sitesmith patch: unknown op', op);
}
}
return { ok: true };