fix: harden list mode and batch generation flow
This commit is contained in:
1 parent
e94eab883e
commit
af96824011
6 files changed
+128
-30
No files matched your search
@@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
maxCloudAgentBatchRows = 500
|
maxCloudAgentBatchRows = 500
|
||||||
maxCloudAgentBatchReferences = 6
|
maxCloudAgentBatchReferences = 10
|
||||||
maxCloudAgentBatchPromptRunes = 20000
|
maxCloudAgentBatchPromptRunes = 20000
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -96,7 +96,7 @@ func batchTableNodeFromDocument(doc map[string]any, nodeID string) (map[string]a
|
|||||||
}
|
}
|
||||||
columns := creationMaps(table["referenceColumns"])
|
columns := creationMaps(table["referenceColumns"])
|
||||||
if len(columns) < 1 || len(columns) > maxCloudAgentBatchReferences {
|
if len(columns) < 1 || len(columns) > maxCloudAgentBatchReferences {
|
||||||
return nil, nil, nil, nil, BadAuthRequest("批量创作表必须包含 1 到 6 个参考图列")
|
return nil, nil, nil, nil, BadAuthRequest("批量创作表必须包含 1 到 10 个参考图列")
|
||||||
}
|
}
|
||||||
columnIDs := map[string]bool{}
|
columnIDs := map[string]bool{}
|
||||||
for _, column := range columns {
|
for _, column := range columns {
|
||||||
@@ -299,7 +299,7 @@ func prepareCloudAgentBatchTableEdit(repo *repository.Repository, userID, canvas
|
|||||||
fields = []string{"并发数"}
|
fields = []string{"并发数"}
|
||||||
case "add_reference_column":
|
case "add_reference_column":
|
||||||
if len(columns) >= maxCloudAgentBatchReferences {
|
if len(columns) >= maxCloudAgentBatchReferences {
|
||||||
return nil, BadAuthRequest("批量创作表最多支持6组参考图")
|
return nil, BadAuthRequest("批量创作表最多支持10组参考图")
|
||||||
}
|
}
|
||||||
if args.RowID != "" || len(args.Patch) != 0 || args.Operation != "" || args.Concurrency != 0 {
|
if args.RowID != "" || len(args.Patch) != 0 || args.Operation != "" || args.Concurrency != 0 {
|
||||||
return nil, BadAuthRequest("新增参考图列不接受其他修改参数")
|
return nil, BadAuthRequest("新增参考图列不接受其他修改参数")
|
||||||
|
|||||||
@@ -6,10 +6,12 @@ import { ImageSettingsPanel } from "@/components/image-settings-panel";
|
|||||||
import { ModelPicker } from "@/components/model-picker";
|
import { ModelPicker } from "@/components/model-picker";
|
||||||
import { canvasThemes } from "@/lib/canvas-theme";
|
import { canvasThemes } from "@/lib/canvas-theme";
|
||||||
import { defaultImageParamsForModel } from "@/lib/model-selection";
|
import { defaultImageParamsForModel } from "@/lib/model-selection";
|
||||||
|
import { CanvasCameraControlPopover } from "./canvas-camera-control-popover";
|
||||||
|
import type { CameraControlOptions } from "@/lib/canvas/camera-prompt-library";
|
||||||
import { useActiveTheme } from "@/stores/canvas/use-canvas-theme-store";
|
import { useActiveTheme } from "@/stores/canvas/use-canvas-theme-store";
|
||||||
import type { AiConfig } from "@/stores/use-config-store";
|
import type { AiConfig } from "@/stores/use-config-store";
|
||||||
|
|
||||||
export type BatchGenerationSettings = Pick<AiConfig, "model" | "imageModel" | "quality" | "size" | "transparentBackground" | "count">;
|
export type BatchGenerationSettings = Pick<AiConfig, "model" | "imageModel" | "quality" | "size" | "transparentBackground" | "count"> & { cameraControl?: CameraControlOptions };
|
||||||
|
|
||||||
type BatchGenerationSettingsDialogProps = {
|
type BatchGenerationSettingsDialogProps = {
|
||||||
open: boolean;
|
open: boolean;
|
||||||
@@ -23,9 +25,13 @@ type BatchGenerationSettingsDialogProps = {
|
|||||||
export function BatchGenerationSettingsDialog({ open, config, rowCount, concurrency, onClose, onConfirm }: BatchGenerationSettingsDialogProps) {
|
export function BatchGenerationSettingsDialog({ open, config, rowCount, concurrency, onClose, onConfirm }: BatchGenerationSettingsDialogProps) {
|
||||||
const theme = canvasThemes[useActiveTheme()];
|
const theme = canvasThemes[useActiveTheme()];
|
||||||
const [generationConfig, setGenerationConfig] = useState<AiConfig>(config);
|
const [generationConfig, setGenerationConfig] = useState<AiConfig>(config);
|
||||||
|
const [cameraControl, setCameraControl] = useState<CameraControlOptions | undefined>(undefined);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (open) setGenerationConfig(config);
|
if (open) {
|
||||||
|
setGenerationConfig(config);
|
||||||
|
setCameraControl(undefined);
|
||||||
|
}
|
||||||
}, [config, open]);
|
}, [config, open]);
|
||||||
|
|
||||||
const handleConfigChange = (key: "quality" | "size" | "transparentBackground" | "count", value: string) => {
|
const handleConfigChange = (key: "quality" | "size" | "transparentBackground" | "count", value: string) => {
|
||||||
@@ -78,6 +84,14 @@ export function BatchGenerationSettingsDialog({ open, config, rowCount, concurre
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center justify-between border-t pt-3" style={{ borderColor: theme.node.stroke }}>
|
||||||
|
<div>
|
||||||
|
<div className="text-sm font-medium">摄像机控制</div>
|
||||||
|
<div className="mt-1 text-xs opacity-60">为本批次统一应用镜头、焦段和光圈设置</div>
|
||||||
|
</div>
|
||||||
|
<CanvasCameraControlPopover cameraControl={cameraControl} onCameraControlChange={setCameraControl} theme={theme} compact />
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="mt-2 flex justify-end gap-2">
|
<div className="mt-2 flex justify-end gap-2">
|
||||||
<Button icon={<X className="size-4" />} onClick={onClose}>取消</Button>
|
<Button icon={<X className="size-4" />} onClick={onClose}>取消</Button>
|
||||||
<Button
|
<Button
|
||||||
@@ -90,6 +104,7 @@ export function BatchGenerationSettingsDialog({ open, config, rowCount, concurre
|
|||||||
size: generationConfig.size,
|
size: generationConfig.size,
|
||||||
transparentBackground: generationConfig.transparentBackground,
|
transparentBackground: generationConfig.transparentBackground,
|
||||||
count: generationConfig.count,
|
count: generationConfig.count,
|
||||||
|
cameraControl,
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
开始生成 {rowCount} 个任务
|
开始生成 {rowCount} 个任务
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { message } from "antd";
|
|||||||
import { CanvasNodeType, type CanvasConnection, type CanvasNodeData, type CanvasBatchTableData, type CanvasBatchRow } from "@/types/canvas";
|
import { CanvasNodeType, type CanvasConnection, type CanvasNodeData, type CanvasBatchTableData, type CanvasBatchRow } from "@/types/canvas";
|
||||||
import { runBackendGenerationTask } from "@/services/api/generation-task";
|
import { runBackendGenerationTask } from "@/services/api/generation-task";
|
||||||
import { buildGenerationConfig } from "@/lib/canvas/canvas-project-generation";
|
import { buildGenerationConfig } from "@/lib/canvas/canvas-project-generation";
|
||||||
|
import { modelRequestOptions, type ModelRequirements } from "@/lib/model-selection";
|
||||||
import type { AiConfig } from "@/stores/use-config-store";
|
import type { AiConfig } from "@/stores/use-config-store";
|
||||||
|
|
||||||
const LIST_MODE_SYSTEM_PROMPT = `你是一个电商内容分析助手。用户会给你多张产品图片和一个任务描述。
|
const LIST_MODE_SYSTEM_PROMPT = `你是一个电商内容分析助手。用户会给你多张产品图片和一个任务描述。
|
||||||
@@ -29,30 +30,79 @@ type ListGenerationResult = {
|
|||||||
rows: Record<string, string>[];
|
rows: Record<string, string>[];
|
||||||
};
|
};
|
||||||
|
|
||||||
function parseListModeJson(text: string): ListGenerationResult | null {
|
export function parseListModeJson(text: string): ListGenerationResult | null {
|
||||||
// Try to extract JSON from the response
|
const candidates = [
|
||||||
const jsonMatch = text.match(/\{[\s\S]*\}/);
|
text.trim(),
|
||||||
if (!jsonMatch) return null;
|
...Array.from(text.matchAll(/```(?:json)?\s*([\s\S]*?)\s*```/gi), (match) => match[1].trim()),
|
||||||
try {
|
...balancedJsonObjects(text),
|
||||||
const parsed = JSON.parse(jsonMatch[0]);
|
];
|
||||||
if (Array.isArray(parsed.columns) && Array.isArray(parsed.rows) && parsed.columns.length > 0) {
|
for (const candidate of candidates) {
|
||||||
return parsed as ListGenerationResult;
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Try removing markdown code blocks
|
|
||||||
const cleaned = jsonMatch[0].replace(/^```json\s*/, "").replace(/\s*```$/, "");
|
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(cleaned);
|
const parsed = JSON.parse(candidate) as { columns?: unknown; rows?: unknown };
|
||||||
if (Array.isArray(parsed.columns) && Array.isArray(parsed.rows)) {
|
const columns = normalizeListColumns(parsed.columns);
|
||||||
return parsed as ListGenerationResult;
|
if (!columns.length || !Array.isArray(parsed.rows)) continue;
|
||||||
}
|
const rows = parsed.rows
|
||||||
|
.filter((row): row is Record<string, unknown> => Boolean(row) && typeof row === "object" && !Array.isArray(row))
|
||||||
|
.map((row) => Object.fromEntries(Object.entries(row).map(([key, value]) => [key, listCellText(value)])));
|
||||||
|
return { columns, rows };
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
// Try the next candidate. Models often wrap valid JSON in prose or a code block.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function balancedJsonObjects(text: string) {
|
||||||
|
const results: string[] = [];
|
||||||
|
let start = -1;
|
||||||
|
let depth = 0;
|
||||||
|
let inString = false;
|
||||||
|
let escaped = false;
|
||||||
|
for (let index = 0; index < text.length; index += 1) {
|
||||||
|
const char = text[index];
|
||||||
|
if (inString) {
|
||||||
|
if (escaped) escaped = false;
|
||||||
|
else if (char === "\\") escaped = true;
|
||||||
|
else if (char === '"') inString = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (char === '"') {
|
||||||
|
inString = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (char === "{") {
|
||||||
|
if (depth === 0) start = index;
|
||||||
|
depth += 1;
|
||||||
|
} else if (char === "}" && depth > 0) {
|
||||||
|
depth -= 1;
|
||||||
|
if (depth === 0 && start >= 0) {
|
||||||
|
results.push(text.slice(start, index + 1));
|
||||||
|
start = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeListColumns(value: unknown) {
|
||||||
|
if (!Array.isArray(value)) return [];
|
||||||
|
const used = new Set<string>();
|
||||||
|
return value.flatMap((item) => {
|
||||||
|
const label = typeof item === "string" ? item.trim() : "";
|
||||||
|
if (!label || used.has(label)) return [];
|
||||||
|
used.add(label);
|
||||||
|
return [label];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function listCellText(value: unknown): string {
|
||||||
|
if (typeof value === "string") return value.trim();
|
||||||
|
if (value === null || value === undefined) return "";
|
||||||
|
if (Array.isArray(value)) return value.map(listCellText).filter(Boolean).join("、");
|
||||||
|
if (typeof value === "object") return JSON.stringify(value);
|
||||||
|
return String(value);
|
||||||
|
}
|
||||||
|
|
||||||
type HandleListGenerateOptions = {
|
type HandleListGenerateOptions = {
|
||||||
sourceNodeId: string;
|
sourceNodeId: string;
|
||||||
prompt: string;
|
prompt: string;
|
||||||
@@ -85,7 +135,7 @@ export async function handleListGenerate({
|
|||||||
const connectedImageIds = connections
|
const connectedImageIds = connections
|
||||||
.filter((c) => c.toNodeId === sourceNodeId && c.fromNodeId !== sourceNodeId)
|
.filter((c) => c.toNodeId === sourceNodeId && c.fromNodeId !== sourceNodeId)
|
||||||
.map((c) => c.fromNodeId);
|
.map((c) => c.fromNodeId);
|
||||||
const imageNodes = nodes.filter((n) => connectedImageIds.includes(n.id) && n.type === CanvasNodeType.Image && n.metadata?.content);
|
const imageNodes = nodes.filter((n) => connectedImageIds.includes(n.id) && n.type === CanvasNodeType.Image && Boolean(n.metadata?.content || n.metadata?.storageKey));
|
||||||
|
|
||||||
if (imageNodes.length === 0) {
|
if (imageNodes.length === 0) {
|
||||||
message.warning("请先连接至少一张图片到当前文本节点");
|
message.warning("请先连接至少一张图片到当前文本节点");
|
||||||
@@ -95,13 +145,14 @@ export async function handleListGenerate({
|
|||||||
setRunningNodeId(sourceNodeId);
|
setRunningNodeId(sourceNodeId);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Force text model: strip node.metadata.model so buildGenerationConfig uses config.textModel
|
// Ask model selection for a text model that can receive all connected images.
|
||||||
const sourceNodeForConfig = { ...sourceNode, metadata: { ...(sourceNode.metadata || {}), model: undefined } };
|
const sourceNodeForConfig = { ...sourceNode, metadata: { ...(sourceNode.metadata || {}), model: undefined } };
|
||||||
let listConfig = buildGenerationConfig(config, sourceNodeForConfig, "text");
|
const requirements: ModelRequirements = {
|
||||||
// Explicitly set model to the configured text model
|
capability: "text",
|
||||||
if (config.textModel) {
|
input: { textCount: 1, imageCount: imageNodes.length, videoCount: 0, audioCount: 0, characterCount: 0 },
|
||||||
listConfig = { ...listConfig, model: config.textModel };
|
options: modelRequestOptions(config, "text"),
|
||||||
}
|
};
|
||||||
|
const listConfig = buildGenerationConfig(config, sourceNodeForConfig, "text", requirements);
|
||||||
|
|
||||||
const referenceImages = imageNodes.map((node) => ({
|
const referenceImages = imageNodes.map((node) => ({
|
||||||
id: node.id,
|
id: node.id,
|
||||||
|
|||||||
@@ -163,6 +163,7 @@ export function useCanvasBatchTable({ nodesRef, connectionsRef, setNodes, setCon
|
|||||||
batchRowId: row.id,
|
batchRowId: row.id,
|
||||||
batchOperation: table.operation,
|
batchOperation: table.operation,
|
||||||
batchInputNodeIds: row.inputNodeIds,
|
batchInputNodeIds: row.inputNodeIds,
|
||||||
|
cameraControl: settings.cameraControl,
|
||||||
};
|
};
|
||||||
const output = existingIndex >= 0
|
const output = existingIndex >= 0
|
||||||
? { ...nextNodes[existingIndex], metadata }
|
? { ...nextNodes[existingIndex], metadata }
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ describe("batch creation table", () => {
|
|||||||
|
|
||||||
expect(withoutText).not.toContain("repeat(0,");
|
expect(withoutText).not.toContain("repeat(0,");
|
||||||
expect(withoutText).toContain("68px");
|
expect(withoutText).toContain("68px");
|
||||||
expect(withoutText).toContain("repeat(3, 88px)");
|
expect(withoutText).toContain("repeat(3, 72px)");
|
||||||
expect(withText).toContain("repeat(1, minmax(168px, 0.75fr))");
|
expect(withText).toContain("repeat(1, minmax(168px, 0.75fr))");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import { describe, expect, test } from "bun:test";
|
||||||
|
|
||||||
|
import { parseListModeJson } from "@/pages/canvas/list-mode-generator";
|
||||||
|
|
||||||
|
describe("list mode response parsing", () => {
|
||||||
|
test("parses plain JSON", () => {
|
||||||
|
expect(parseListModeJson('{"columns":["卖点"],"rows":[{"卖点":"轻便"}]}')).toEqual({
|
||||||
|
columns: ["卖点"],
|
||||||
|
rows: [{ 卖点: "轻便" }],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("parses markdown JSON with surrounding explanation", () => {
|
||||||
|
expect(parseListModeJson('结果如下:\n```json\n{"columns":["标题"],"rows":[{"标题":"新品"}]}\n```\n')).toEqual({
|
||||||
|
columns: ["标题"],
|
||||||
|
rows: [{ 标题: "新品" }],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("normalizes duplicate columns and non-string cells", () => {
|
||||||
|
expect(parseListModeJson('{"columns":["卖点","卖点","细节"],"rows":[{"卖点":["轻","薄"],"细节":123}]}')).toEqual({
|
||||||
|
columns: ["卖点", "细节"],
|
||||||
|
rows: [{ 卖点: "轻、薄", 细节: "123" }],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("rejects malformed responses", () => {
|
||||||
|
expect(parseListModeJson("模型暂时无法分析")).toBeNull();
|
||||||
|
expect(parseListModeJson('{"columns":[],"rows":[]}')).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user