fix: stabilize custom widget authoring
This commit is contained in:
1 parent
d80e883479
commit
a1c2559541
11 files changed
+796
-27
No files matched your search
@@ -57,6 +57,11 @@ import { assistantExecutionPolicy } from "./assistant-execution-policy";
|
||||
import { getAssistantStreamErrorMessage } from "./assistant-stream-error";
|
||||
import { getSafeAssistantToolError } from "./assistant-tool-error";
|
||||
import { repairCustomWidgetToolInput } from "./assistant-tool-input-repair";
|
||||
import {
|
||||
createCustomWidgetDynamicContextController,
|
||||
getCustomWidgetAuthoringContext,
|
||||
prunePreloadedCustomWidgetModelMessages,
|
||||
} from "./custom-widget-authoring-context";
|
||||
import {
|
||||
customWidgetAssistantInstructions,
|
||||
getForcedAssistantToolName,
|
||||
@@ -343,8 +348,9 @@ const getProcedureTypeMap = () => {
|
||||
);
|
||||
};
|
||||
|
||||
const waitForDemoChunkAsync = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
const createDemoAssistantResponse = (request: z.infer<typeof requestSchema>) => {
|
||||
const delay = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));
|
||||
const text =
|
||||
"This is the Homarr demo assistant. It is running in preview/mock mode, so it responds with a canned answer instead of calling a real language model. In a real deployment, an administrator configures a provider and a model for full conversations.";
|
||||
const responseId = crypto.randomUUID();
|
||||
@@ -358,7 +364,7 @@ const createDemoAssistantResponse = (request: z.infer<typeof requestSchema>) =>
|
||||
writer.write({ type: "text-start", id: partId });
|
||||
for (const chunk of chunkTexts) {
|
||||
writer.write({ type: "text-delta", id: partId, delta: chunk });
|
||||
await delay(18);
|
||||
await waitForDemoChunkAsync(18);
|
||||
}
|
||||
writer.write({ type: "text-end", id: partId });
|
||||
writer.write({ type: "finish", finishReason: "stop" });
|
||||
@@ -401,6 +407,7 @@ export async function POST(request: Request) {
|
||||
{ status: 413 },
|
||||
);
|
||||
}
|
||||
const incomingMessages = parsed.data.messages as UIMessage[];
|
||||
|
||||
const configuration = await db.query.assistantConfigurations.findFirst({
|
||||
where: eq(assistantConfigurations.id, "default"),
|
||||
@@ -469,9 +476,12 @@ export async function POST(request: Request) {
|
||||
) {
|
||||
return Response.json({ error: "The selected model does not support image input." }, { status: 400 });
|
||||
}
|
||||
const canAuthorCustomWidgets = session.user.permissions.includes("admin");
|
||||
const customWidgetAuthoringContext = getCustomWidgetAuthoringContext(incomingMessages, canAuthorCustomWidgets);
|
||||
const caller = mcpRouter.createCaller(context);
|
||||
const procedureTypes = getProcedureTypeMap();
|
||||
const mcpTools = extractMcpTools();
|
||||
const omittedCustomWidgetTools = new Set<string>(customWidgetAuthoringContext.omittedToolNames);
|
||||
const mcpTools = extractMcpTools().filter((mcpTool) => !omittedCustomWidgetTools.has(mcpTool.name));
|
||||
|
||||
const homarrTools = Object.fromEntries(
|
||||
mcpTools.map((mcpTool) => {
|
||||
@@ -535,7 +545,7 @@ export async function POST(request: Request) {
|
||||
}),
|
||||
) satisfies ToolSet;
|
||||
const availableTools = { ...homarrTools, ...frontendTools };
|
||||
const forcedToolName = getForcedAssistantToolName(parsed.data.messages as UIMessage[]);
|
||||
const forcedToolName = getForcedAssistantToolName(incomingMessages);
|
||||
const openRouterServerToolsEnabled =
|
||||
configuration.webSearchEnabled && assistantProviderCanUseOpenRouterServerTools(configuration.provider);
|
||||
|
||||
@@ -571,27 +581,39 @@ export async function POST(request: Request) {
|
||||
let hasReportedCost = false;
|
||||
let upstreamCost = 0;
|
||||
let hasUpstreamCost = false;
|
||||
const initialModelMessages = await convertToModelMessages(prepareMessagesForModel(incomingMessages));
|
||||
const modelMessages =
|
||||
customWidgetAuthoringContext.systemContext.length > 0
|
||||
? prunePreloadedCustomWidgetModelMessages(initialModelMessages)
|
||||
: initialModelMessages;
|
||||
const baseInstructions = `${assistantInstructions}${customWidgetAssistantInstructions}${openRouterServerToolsEnabled ? webSearchInstructions : ""}${mentionContext}${customWidgetAuthoringContext.systemContext}`;
|
||||
const prepareDynamicCustomWidgetContext = createCustomWidgetDynamicContextController({
|
||||
isAdmin: canAuthorCustomWidgets,
|
||||
baseInstructions,
|
||||
availableToolNames: Object.keys(availableTools),
|
||||
});
|
||||
const result = streamText({
|
||||
model: provider(modelId),
|
||||
instructions: `${assistantInstructions}${customWidgetAssistantInstructions}${openRouterServerToolsEnabled ? webSearchInstructions : ""}${mentionContext}`,
|
||||
messages: await convertToModelMessages(prepareMessagesForModel(parsed.data.messages as UIMessage[])),
|
||||
instructions: baseInstructions,
|
||||
messages: modelMessages,
|
||||
tools: availableTools,
|
||||
prepareStep: ({ stepNumber, steps, responseMessages }) => {
|
||||
prepareStep: ({ instructions, messages, stepNumber, steps, responseMessages }) => {
|
||||
const dynamicStepOverrides = prepareDynamicCustomWidgetContext({ instructions, messages, steps });
|
||||
const stepOverrides = dynamicStepOverrides ?? {};
|
||||
if (stepNumber === 0 && forcedToolName !== undefined && forcedToolName in availableTools) {
|
||||
return {
|
||||
...stepOverrides,
|
||||
activeTools: [forcedToolName],
|
||||
toolChoice: { type: "tool", toolName: forcedToolName },
|
||||
};
|
||||
}
|
||||
const requiredToolNames = getRequiredAssistantToolNames(
|
||||
parsed.data.messages as UIMessage[],
|
||||
steps,
|
||||
responseMessages,
|
||||
).filter((toolName) => toolName in availableTools);
|
||||
const requiredToolNames = getRequiredAssistantToolNames(incomingMessages, steps, responseMessages).filter(
|
||||
(toolName) => toolName in availableTools,
|
||||
);
|
||||
if (requiredToolNames.length > 0) {
|
||||
return { activeTools: requiredToolNames, toolChoice: "required" };
|
||||
return { ...stepOverrides, activeTools: requiredToolNames, toolChoice: "required" };
|
||||
}
|
||||
return undefined;
|
||||
return dynamicStepOverrides;
|
||||
},
|
||||
stopWhen: stepCountIs(assistantExecutionPolicy.maxSteps),
|
||||
abortSignal: request.signal,
|
||||
|
||||
Reference in new issue
Block a user