diff --git a/lib/canvas/agent-pending-requests.ts b/lib/canvas/agent-pending-requests.ts index cf2fe9c47..c00f83a30 100644 --- a/lib/canvas/agent-pending-requests.ts +++ b/lib/canvas/agent-pending-requests.ts @@ -23,6 +23,16 @@ function normalizeNodeId(value: unknown) { return String(value || "").trim(); } +function shouldConsumePendingRequest(params: { + pendingNodeId?: string; + detailNodeId?: string; + hasDetail: boolean; +}) { + const pendingNodeId = normalizeNodeId(params.pendingNodeId); + const detailNodeId = normalizeNodeId(params.detailNodeId); + return !params.hasDetail || !pendingNodeId || pendingNodeId === detailNodeId; +} + export function storeCanvasAgentSlotFillRequest( targetWindow: CanvasAgentPendingRequestWindow, request: PendingCanvasAgentSlotFillRequest @@ -42,9 +52,13 @@ export function consumeCanvasAgentSlotFillRequest( ) { const pending = getPendingCanvasAgentSlotFillRequest(targetWindow); if (!pending) return null; - const pendingNodeId = normalizeNodeId(pending.nodeId); - const detailNodeId = normalizeNodeId(detail?.nodeId); - if (!pendingNodeId || pendingNodeId === detailNodeId) { + if ( + shouldConsumePendingRequest({ + pendingNodeId: pending.nodeId, + detailNodeId: detail?.nodeId, + hasDetail: Boolean(detail), + }) + ) { delete targetWindow.__canvasAgentPendingSlotFillRequest; } return pending; @@ -69,9 +83,13 @@ export function consumeCanvasAgentAnnotationEditRequest( ) { const pending = getPendingCanvasAgentAnnotationEditRequest(targetWindow); if (!pending) return null; - const pendingNodeId = normalizeNodeId(pending.nodeId); - const detailNodeId = normalizeNodeId(detail?.nodeId); - if (!pendingNodeId || pendingNodeId === detailNodeId) { + if ( + shouldConsumePendingRequest({ + pendingNodeId: pending.nodeId, + detailNodeId: detail?.nodeId, + hasDetail: Boolean(detail), + }) + ) { delete targetWindow.__canvasAgentPendingAnnotationEditRequest; } return pending; diff --git a/tests/routes/canvas-chat-helpers.test.ts b/tests/routes/canvas-chat-helpers.test.ts index c74c5f753..64b01834a 100644 --- a/tests/routes/canvas-chat-helpers.test.ts +++ b/tests/routes/canvas-chat-helpers.test.ts @@ -3706,6 +3706,29 @@ test.describe("canvas chat pure helpers", () => { expect(getPendingCanvasAgentAnnotationEditRequest(targetWindow)).toBeNull(); }); + test("consumes pending canvas agent UI requests when no event detail is provided", () => { + const targetWindow = {} as CanvasAgentPendingRequestWindow; + const slotRequest = { + nodeId: "slot-1", + label: "主图槽", + }; + const annotationRequest = { + nodeId: "image-annotated", + title: "白底主图", + annotationCount: 2, + }; + + storeCanvasAgentSlotFillRequest(targetWindow, slotRequest); + storeCanvasAgentAnnotationEditRequest(targetWindow, annotationRequest); + + expect(consumeCanvasAgentSlotFillRequest(targetWindow)).toEqual(slotRequest); + expect(consumeCanvasAgentAnnotationEditRequest(targetWindow)).toEqual( + annotationRequest + ); + expect(getPendingCanvasAgentSlotFillRequest(targetWindow)).toBeNull(); + expect(getPendingCanvasAgentAnnotationEditRequest(targetWindow)).toBeNull(); + }); + test("keeps pending annotation edit targets eligible for the next canvas context", () => { const nodes = [ {