forked from longtao.wu/droboticflow
283 lines
9.8 KiB
JavaScript
283 lines
9.8 KiB
JavaScript
import { artifactExtension } from "./artifact-download.js";
|
|
import { isPoseRenderArtifact } from "./pose-artifacts.js";
|
|
import { summarizeQualityReportArtifact } from "./report-summary.js";
|
|
|
|
export const ARTIFACT_GROUP_ORDER = ["final", "intermediate", "input"];
|
|
|
|
export const ARTIFACT_GROUP_LABELS = {
|
|
final: "最终结果",
|
|
intermediate: "中间产物",
|
|
input: "输入数据",
|
|
};
|
|
|
|
const artifactPriority = (item) => {
|
|
if (isPoseRenderArtifact(item)) return -1;
|
|
if (summarizeQualityReportArtifact(item)) return 0;
|
|
const extension = artifactExtension(item);
|
|
if (extension === "html" || extension === "htm" || extension === "pdf") return 1;
|
|
if (item?.type === "json" || extension === "json") return 2;
|
|
return 3;
|
|
};
|
|
|
|
export const sortArtifactsForDisplay = (items) =>
|
|
[...items].sort((left, right) => {
|
|
const priority = artifactPriority(left) - artifactPriority(right);
|
|
if (priority !== 0) return priority;
|
|
return String(left?.name || "").localeCompare(String(right?.name || ""), "zh-CN");
|
|
});
|
|
|
|
const emptyGroups = () => ({
|
|
final: [],
|
|
intermediate: [],
|
|
input: [],
|
|
});
|
|
|
|
const appendNodeArtifact = (groups, kind, node, artifact) => {
|
|
let group = groups[kind].find((item) => item.nodeId === node.nodeId);
|
|
if (!group) {
|
|
group = {
|
|
nodeId: node.nodeId,
|
|
nodeLabel: node.nodeLabel || node.nodeId || "未知节点",
|
|
artifacts: [],
|
|
};
|
|
groups[kind].push(group);
|
|
}
|
|
group.artifacts.push(artifact);
|
|
};
|
|
|
|
const artifactIdentityKeys = (artifact) => {
|
|
if (!artifact || typeof artifact !== "object") return [];
|
|
const source = artifact.data?.source;
|
|
const values = [
|
|
["id", artifact.id],
|
|
["path", artifact.path],
|
|
["source", source && typeof source === "object" ? source.path : ""],
|
|
];
|
|
return values
|
|
.map(([kind, value]) => [kind, typeof value === "string" ? value.trim() : ""])
|
|
.filter(([, value]) => value)
|
|
.map(([kind, value]) => `${kind}:${value}`);
|
|
};
|
|
|
|
const artifactInputIdentityKeys = (artifact) => {
|
|
const keys = artifactIdentityKeys(artifact);
|
|
const nodeId = typeof artifact?.nodeId === "string" ? artifact.nodeId.trim() : "";
|
|
const portId = typeof artifact?.portId === "string" ? artifact.portId.trim() : "";
|
|
const portName = typeof artifact?.portName === "string" ? artifact.portName.trim() : "";
|
|
const name = typeof artifact?.name === "string" ? artifact.name.trim() : "";
|
|
const binding = portId || portName || name;
|
|
if (binding) keys.push(`input:${nodeId}:${binding}`);
|
|
return keys;
|
|
};
|
|
|
|
const inputArtifactData = (artifact) => {
|
|
const data = artifact?.data && typeof artifact.data === "object" ? artifact.data : {};
|
|
const reference = artifact?.payload?.mode === "ref" ? artifact.payload.ref : undefined;
|
|
const explicitContentURL = String(data.contentURL || data.contentUrl || "").trim();
|
|
return {
|
|
...data,
|
|
inputReference: true,
|
|
contentAvailable: explicitContentURL ? data.contentAvailable !== false : false,
|
|
...(reference?.kind === "directory" && !data.datasetType ? { datasetType: "directory" } : {}),
|
|
};
|
|
};
|
|
|
|
const normalizeInputArtifact = (artifact) => ({
|
|
...artifact,
|
|
data: inputArtifactData(artifact),
|
|
});
|
|
|
|
const inputArtifactFromPayload = ({ runId, workflowInputId, nodeId, targetPortId, payload }) => {
|
|
const reference = payload?.mode === "ref" ? payload.ref : undefined;
|
|
const inline = payload?.mode === "inline" ? payload.inline : undefined;
|
|
const inputName = String(workflowInputId || targetPortId || "workflow-input").trim() || "workflow-input";
|
|
const bindingId = String(targetPortId || inputName).trim() || inputName;
|
|
return normalizeInputArtifact({
|
|
id: `${runId || "run"}-input-${inputName}-${bindingId}`,
|
|
runId: runId || "",
|
|
nodeId: nodeId || "workflow-inputs",
|
|
portId: bindingId,
|
|
portName: bindingId,
|
|
name: inputName,
|
|
type: payload?.type || "file",
|
|
size: reference?.sizeBytes ? `${reference.sizeBytes} B` : "",
|
|
sizeBytes: reference?.sizeBytes,
|
|
path: "",
|
|
metadata: {
|
|
...(reference?.mimeType ? { mimeType: reference.mimeType } : {}),
|
|
...(reference?.sha256 ? { sha256: reference.sha256 } : {}),
|
|
},
|
|
data: {
|
|
...(inline !== undefined ? { preview: { kind: "json", value: inline } } : {}),
|
|
...(reference?.kind === "directory" ? { datasetType: "directory" } : {}),
|
|
},
|
|
payload,
|
|
transferMode: payload?.mode,
|
|
presentation: payload?.mode === "ref" ? "reference" : "value",
|
|
});
|
|
};
|
|
|
|
export function runInputArtifacts(run) {
|
|
if (!run || typeof run !== "object") return [];
|
|
const items = [];
|
|
const boundInputIDs = new Set();
|
|
for (const binding of Array.isArray(run.resolvedInputBindings) ? run.resolvedInputBindings : []) {
|
|
if (!binding?.payload) continue;
|
|
const workflowInputId = String(binding.workflowInputId || binding.targetPortId || "").trim();
|
|
if (workflowInputId) boundInputIDs.add(workflowInputId);
|
|
items.push(
|
|
inputArtifactFromPayload({
|
|
runId: run.id,
|
|
workflowInputId,
|
|
nodeId: binding.nodeId,
|
|
targetPortId: binding.targetPortId,
|
|
payload: binding.payload,
|
|
}),
|
|
);
|
|
}
|
|
for (const [workflowInputId, payload] of Object.entries(run.inputs || {})) {
|
|
if (!payload || boundInputIDs.has(workflowInputId)) continue;
|
|
items.push(
|
|
inputArtifactFromPayload({
|
|
runId: run.id,
|
|
workflowInputId,
|
|
nodeId: "workflow-inputs",
|
|
targetPortId: workflowInputId,
|
|
payload,
|
|
}),
|
|
);
|
|
}
|
|
return items;
|
|
}
|
|
|
|
const buildArtifactNodeIndex = (nodes) => {
|
|
const nodeIDsByKey = new Map();
|
|
for (const node of nodes) {
|
|
for (const artifact of Array.isArray(node?.outputArtifacts) ? node.outputArtifacts : []) {
|
|
for (const key of artifactIdentityKeys(artifact)) {
|
|
const nodeIDs = nodeIDsByKey.get(key) || new Set();
|
|
nodeIDs.add(node.nodeId);
|
|
nodeIDsByKey.set(key, nodeIDs);
|
|
}
|
|
}
|
|
}
|
|
return nodeIDsByKey;
|
|
};
|
|
|
|
const resolveArtifactNode = (artifact, nodeById, nodeIDsByArtifactKey) => {
|
|
const explicitNode = nodeById.get(artifact?.nodeId);
|
|
if (explicitNode) return explicitNode;
|
|
const candidates = new Set();
|
|
for (const key of artifactIdentityKeys(artifact)) {
|
|
for (const nodeId of nodeIDsByArtifactKey.get(key) || []) {
|
|
candidates.add(nodeId);
|
|
}
|
|
}
|
|
if (candidates.size !== 1) return null;
|
|
return nodeById.get([...candidates][0]) || null;
|
|
};
|
|
|
|
export function groupArtifactsByLineage(artifacts = [], lineage, explicitInputArtifacts = []) {
|
|
const items = Array.isArray(artifacts) ? artifacts : [];
|
|
const fallbackInputs = Array.isArray(explicitInputArtifacts) ? explicitInputArtifacts : [];
|
|
const nodes = Array.isArray(lineage?.nodes) ? lineage.nodes : [];
|
|
if (nodes.length === 0) {
|
|
const groups = emptyGroups();
|
|
for (const artifact of fallbackInputs) {
|
|
appendNodeArtifact(
|
|
groups,
|
|
"input",
|
|
{ nodeId: artifact?.nodeId || "workflow-inputs", nodeLabel: "工作流输入" },
|
|
normalizeInputArtifact(artifact),
|
|
);
|
|
}
|
|
groups.input = groups.input.map((group) => ({
|
|
...group,
|
|
artifacts: sortArtifactsForDisplay(group.artifacts),
|
|
}));
|
|
return {
|
|
hasLineage: false,
|
|
groups: {
|
|
...groups,
|
|
final: [
|
|
{
|
|
nodeId: "legacy",
|
|
nodeLabel: "运行产物",
|
|
artifacts: sortArtifactsForDisplay(items),
|
|
},
|
|
],
|
|
},
|
|
};
|
|
}
|
|
|
|
const outgoing = new Set();
|
|
for (const edge of Array.isArray(lineage?.edges) ? lineage.edges : []) {
|
|
if (edge?.sourceNodeId) outgoing.add(edge.sourceNodeId);
|
|
}
|
|
const nodeById = new Map(nodes.map((node) => [node.nodeId, node]));
|
|
const kindByNodeId = new Map(
|
|
nodes.map((node) => [node.nodeId, outgoing.has(node.nodeId) ? "intermediate" : "final"]),
|
|
);
|
|
const nodeIDsByArtifactKey = buildArtifactNodeIndex(nodes);
|
|
const producedArtifactKeys = new Set();
|
|
for (const node of nodes) {
|
|
for (const artifact of Array.isArray(node?.outputArtifacts) ? node.outputArtifacts : []) {
|
|
for (const key of artifactIdentityKeys(artifact)) producedArtifactKeys.add(key);
|
|
}
|
|
}
|
|
const groups = emptyGroups();
|
|
const seenInputKeys = new Set();
|
|
const appendInput = (artifact, node) => {
|
|
const normalized = normalizeInputArtifact(artifact);
|
|
const keys = artifactInputIdentityKeys(normalized);
|
|
if (keys.some((key) => seenInputKeys.has(key))) return;
|
|
for (const key of keys) seenInputKeys.add(key);
|
|
appendNodeArtifact(groups, "input", node, normalized);
|
|
};
|
|
for (const artifact of fallbackInputs) {
|
|
appendInput(
|
|
artifact,
|
|
nodeById.get(artifact?.nodeId) || {
|
|
nodeId: artifact?.nodeId || "workflow-inputs",
|
|
nodeLabel: "工作流输入",
|
|
},
|
|
);
|
|
}
|
|
for (const node of nodes) {
|
|
for (const artifact of Array.isArray(node?.inputArtifacts) ? node.inputArtifacts : []) {
|
|
const keys = artifactIdentityKeys(artifact);
|
|
if (keys.some((key) => producedArtifactKeys.has(key))) continue;
|
|
appendInput(artifact, node);
|
|
}
|
|
}
|
|
for (const artifact of items) {
|
|
const node = resolveArtifactNode(artifact, nodeById, nodeIDsByArtifactKey) || {
|
|
nodeId: artifact?.nodeId || "legacy",
|
|
nodeLabel: artifact?.nodeId || "运行产物",
|
|
};
|
|
const kind = kindByNodeId.get(node.nodeId) || "final";
|
|
appendNodeArtifact(groups, kind, node, artifact);
|
|
}
|
|
for (const kind of ARTIFACT_GROUP_ORDER) {
|
|
groups[kind] = groups[kind]
|
|
.map((group) => ({ ...group, artifacts: sortArtifactsForDisplay(group.artifacts) }))
|
|
.sort((left, right) => left.nodeLabel.localeCompare(right.nodeLabel, "zh-CN"));
|
|
}
|
|
return { hasLineage: true, groups };
|
|
}
|
|
|
|
export function preferredArtifact(grouped, kinds = ARTIFACT_GROUP_ORDER) {
|
|
for (const kind of kinds) {
|
|
for (const node of grouped?.groups?.[kind] || []) {
|
|
if (node.artifacts.length > 0) {
|
|
return node.artifacts[0];
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function artifactGroupCount(groups, kind) {
|
|
return (groups?.[kind] || []).reduce((total, node) => total + node.artifacts.length, 0);
|
|
}
|