From 346573247604006124898c1ad3aae74fc5d0c208 Mon Sep 17 00:00:00 2001 From: ajnart Date: Sun, 30 Aug 2026 18:39:44 +0200 Subject: [PATCH] fix(assistant): preserve widget placement follow-up --- .../chat/assistant-tool-policy.spec.ts | 74 +++++++++++++++++ .../assistant/chat/assistant-tool-policy.ts | 79 +++++++++++++++++++ .../src/app/api/assistant/chat/route.ts | 14 +++- 3 files changed, 166 insertions(+), 1 deletion(-) diff --git a/apps/nextjs/src/app/api/assistant/chat/assistant-tool-policy.spec.ts b/apps/nextjs/src/app/api/assistant/chat/assistant-tool-policy.spec.ts index 2a273a068..c76e814bc 100644 --- a/apps/nextjs/src/app/api/assistant/chat/assistant-tool-policy.spec.ts +++ b/apps/nextjs/src/app/api/assistant/chat/assistant-tool-policy.spec.ts @@ -4,6 +4,7 @@ import type { UIMessage } from "ai"; import { customWidgetAssistantInstructions, getForcedAssistantToolName, + getRequiredAssistantToolNames, withAssistantToolPolicy, } from "./assistant-tool-policy"; @@ -177,3 +178,76 @@ describe("getForcedAssistantToolName", () => { ).toBeUndefined(); }); }); + +describe("getRequiredAssistantToolNames", () => { + const successfulCreation = { + id: "widget-1", + managementPath: "/manage/custom-widgets/edit/widget-1", + }; + + test("requires a structured placement choice after persisted creation", () => { + expect( + getRequiredAssistantToolNames([ + assistantMessage({ + type: "dynamic-tool", + toolName: "customWidget_createFromPreview", + toolCallId: "create-widget-1", + input: { previewSessionId: "preview-1" }, + state: "output-available", + output: successfulCreation, + }), + ]), + ).toEqual(["configure_widget", "ask_user"]); + }); + + test("places directly when creation already has a target board", () => { + const steps = [ + { + toolResults: [ + { + toolName: "customWidget_createFromPreview", + output: { + ...successfulCreation, + nextAction: { type: "place-custom-widget", targetBoardId: "board-1" }, + }, + }, + ], + }, + ]; + + expect(getRequiredAssistantToolNames([], steps)).toEqual(["configure_widget"]); + }); + + test("handles an approved creation that executes before step zero", () => { + const responseMessages = [ + { + role: "tool", + content: [ + { + type: "tool-result", + toolCallId: "create-widget-1", + toolName: "customWidget_createFromPreview", + output: { type: "json", value: successfulCreation }, + }, + ], + }, + ]; + + expect(getRequiredAssistantToolNames([], [], responseMessages)).toEqual(["configure_widget", "ask_user"]); + }); + + test("does not force placement after failed creation", () => { + const steps = [ + { + toolResults: [ + { + toolName: "customWidget_createFromPreview", + output: { error: "Creation failed" }, + }, + ], + }, + ]; + + expect(getRequiredAssistantToolNames([], steps)).toEqual([]); + }); +}); diff --git a/apps/nextjs/src/app/api/assistant/chat/assistant-tool-policy.ts b/apps/nextjs/src/app/api/assistant/chat/assistant-tool-policy.ts index a2c2c42dc..9fa6dfcf5 100644 --- a/apps/nextjs/src/app/api/assistant/chat/assistant-tool-policy.ts +++ b/apps/nextjs/src/app/api/assistant/chat/assistant-tool-policy.ts @@ -42,3 +42,82 @@ export const getForcedAssistantToolName = (messages: UIMessage[]) => { ? nextToolByHumanTool[toolName as keyof typeof nextToolByHumanTool] : undefined; }; + +interface AssistantToolExecutionStep { + toolResults: readonly { + toolName: string; + output: unknown; + }[]; +} + +interface AssistantToolResponseMessage { + role: string; + content: unknown; +} + +const customWidgetCreateToolNames = new Set(["customWidget_create", "customWidget_createFromPreview"]); + +const getCustomWidgetCreateFollowup = (output: unknown) => { + if (typeof output !== "object" || output === null) return []; + if (!("id" in output) || typeof output.id !== "string" || output.id.length === 0) return []; + if (!("managementPath" in output) || typeof output.managementPath !== "string" || output.managementPath.length === 0) + return []; + if ("error" in output && output.error !== undefined) return []; + + const nextAction = "nextAction" in output ? output.nextAction : undefined; + const targetIsKnown = + typeof nextAction === "object" && + nextAction !== null && + "targetBoardId" in nextAction && + typeof nextAction.targetBoardId === "string" && + nextAction.targetBoardId.length > 0; + return targetIsKnown ? ["configure_widget"] : ["configure_widget", "ask_user"]; +}; + +const getCustomWidgetCreateOutputFromResponseMessages = (messages: readonly AssistantToolResponseMessage[]) => { + for (const message of messages.toReversed()) { + if (message.role !== "tool" || !Array.isArray(message.content)) continue; + for (const part of message.content.toReversed()) { + if (typeof part !== "object" || part === null) continue; + if (!("type" in part) || part.type !== "tool-result") continue; + if (!("toolName" in part) || !customWidgetCreateToolNames.has(String(part.toolName))) continue; + if (!("output" in part)) continue; + + const output = part.output; + if (typeof output === "object" && output !== null && "type" in output && output.type === "json") { + return "value" in output ? output.value : undefined; + } + return output; + } + } + return undefined; +}; + +export const getRequiredAssistantToolNames = ( + messages: UIMessage[], + completedSteps: readonly AssistantToolExecutionStep[] = [], + responseMessages: readonly AssistantToolResponseMessage[] = [], +) => { + if (completedSteps.length > 0) { + const latestCreateResult = completedSteps + .at(-1) + ?.toolResults.toReversed() + .find((result) => customWidgetCreateToolNames.has(result.toolName)); + return latestCreateResult ? getCustomWidgetCreateFollowup(latestCreateResult.output) : []; + } + + const responseCreateOutput = getCustomWidgetCreateOutputFromResponseMessages(responseMessages); + if (responseCreateOutput !== undefined) return getCustomWidgetCreateFollowup(responseCreateOutput); + + const latestMessage = messages.at(-1); + if (latestMessage?.role !== "assistant") return []; + const latestToolPart = latestMessage.parts.toReversed().find((part) => isToolUIPart(part)); + if ( + latestToolPart === undefined || + latestToolPart.state !== "output-available" || + !customWidgetCreateToolNames.has(getToolName(latestToolPart)) + ) { + return []; + } + return getCustomWidgetCreateFollowup(latestToolPart.output); +}; diff --git a/apps/nextjs/src/app/api/assistant/chat/route.ts b/apps/nextjs/src/app/api/assistant/chat/route.ts index a694f709f..97b671b54 100644 --- a/apps/nextjs/src/app/api/assistant/chat/route.ts +++ b/apps/nextjs/src/app/api/assistant/chat/route.ts @@ -78,6 +78,7 @@ import { createAssistantMcpToolGroups } from "./assistant-tool-groups"; import { customWidgetAssistantInstructions, getForcedAssistantToolName, + getRequiredAssistantToolNames, withAssistantToolPolicy, } from "./assistant-tool-policy"; @@ -719,7 +720,7 @@ export async function POST(request: Request) { instructions: baseInstructions, messages: initialModelMessages, tools: availableTools, - prepareStep: ({ messages, stepNumber, steps }) => { + prepareStep: ({ messages, responseMessages, stepNumber, steps }) => { customWidgetToolStepGate.begin(stepNumber); if (stepNumber === 0 && forcedToolName !== undefined && forcedToolName in availableTools) { return { @@ -728,6 +729,17 @@ export async function POST(request: Request) { toolChoice: { type: "tool", toolName: forcedToolName }, }; } + const requiredToolNames = getRequiredAssistantToolNames(incomingMessages, steps, responseMessages).filter( + (toolName) => toolName in availableTools, + ); + if (requiredToolNames.length > 0) { + return { + activeTools: requiredToolNames, + instructions: getStepInstructions(requiredToolNames), + messages: compactAssistantStepMessages(messages), + toolChoice: "required", + }; + } const activeTools = getActiveToolNames(steps); return { activeTools,