68 lines
2.0 KiB
TypeScript
68 lines
2.0 KiB
TypeScript
import type { ContextPayload } from "@/lib/ai/context-memory/types";
|
|
|
|
type CanvasAgentExecutionMode = "edit_image" | "generate_image" | "generate_video";
|
|
|
|
const CANVAS_AGENT_SLOT_GENERATION_REGEX =
|
|
/(生成|生图|出图|画一张|来一张|做一张|做图|做视频|视频生成|生成视频|填充|放到|放进|在这里|这个空框|这个槽位|槽位|slot)/i;
|
|
|
|
function isCanvasSource(contextPayload?: ContextPayload | null) {
|
|
return (
|
|
contextPayload?.source === "canvas" || contextPayload?.source === "realtime-canvas"
|
|
);
|
|
}
|
|
|
|
function hasSelectedCanvasAgentSlot(contextPayload?: ContextPayload | null) {
|
|
const context = contextPayload?.canvasAgentContext;
|
|
if (!context) return false;
|
|
const selectedNodeIds = new Set(
|
|
(context.selectedNodeIds || contextPayload?.selectedNodeIds || [])
|
|
.map((id) => String(id || "").trim())
|
|
.filter(Boolean)
|
|
);
|
|
|
|
return (context.slots || []).some((slot) => {
|
|
const slotId = String(slot.nodeId || "").trim();
|
|
return Boolean(slotId && (slot.isSelected || selectedNodeIds.has(slotId)));
|
|
});
|
|
}
|
|
|
|
export function shouldBypassCanvasAgentMissingTargetClarification(params: {
|
|
text: string;
|
|
contextPayload?: ContextPayload | null;
|
|
executionMode?: string | null;
|
|
}) {
|
|
const executionMode = String(params.executionMode || "") as CanvasAgentExecutionMode;
|
|
if (executionMode !== "generate_image" && executionMode !== "generate_video") {
|
|
return {
|
|
shouldBypass: false,
|
|
reason: undefined,
|
|
};
|
|
}
|
|
|
|
if (!isCanvasSource(params.contextPayload)) {
|
|
return {
|
|
shouldBypass: false,
|
|
reason: undefined,
|
|
};
|
|
}
|
|
|
|
if (!hasSelectedCanvasAgentSlot(params.contextPayload)) {
|
|
return {
|
|
shouldBypass: false,
|
|
reason: undefined,
|
|
};
|
|
}
|
|
|
|
if (!CANVAS_AGENT_SLOT_GENERATION_REGEX.test(String(params.text || ""))) {
|
|
return {
|
|
shouldBypass: false,
|
|
reason: undefined,
|
|
};
|
|
}
|
|
|
|
return {
|
|
shouldBypass: true,
|
|
reason: "selected_canvas_agent_slot_generation_intent",
|
|
};
|
|
}
|