fix(assistant): preserve widget placement follow-up
This commit is contained in:
1 parent
05ae366fe1
commit
3465732476
3 files changed
+166
-1
No files matched your search
@@ -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([]);
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
};
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user