Files
hose-use/src/domains/platform/workflow/execution-context.ts
T
gitea 4e03c9bcd7 refactor: 将应用数据与工作流统一归入应用平台子模块
建立 applications 的应用模型和查询出口,保留基础及详情投影差异。
下沉 DataLink,将工作流模型、API、编排及执行上下文归回 workflow。
同步 CLI、SDK、生成脚本、测试和活跃文档引用,公开命令与 SDK 名称保持兼容。

验证:943 项测试、类型和版本检查通过;三份生成规范字节一致,README 审计通过。
2026-10-30 16:56:09 +08:00

46 lines
2.1 KiB
TypeScript

import type { WorkflowTaskExecution } from "./types.js";
export interface ExecutionContext {
node: WorkflowTaskExecution;
key: string;
scope: string[];
loopPath: string;
}
/** Index real executions by parent task path, loop context and repeated occurrence. */
export function indexExecutionContexts(tree: WorkflowTaskExecution[]): ExecutionContext[] {
const result: ExecutionContext[] = [];
const entries = new Map<string, { node: WorkflowTaskExecution; parentId?: string }>();
const occurrences = new Map<string, number>();
function collect(node: WorkflowTaskExecution, parentId?: string) {
if (!node || !node.id || entries.has(node.id)) return;
entries.set(node.id, { node, parentId: String(node.parentId || parentId || "") || undefined });
for (const key of ["children", "subTasks", "items", "tasks"]) {
const children = node[key];
if (Array.isArray(children)) for (const child of children) collect(child, node.id);
}
}
for (const node of tree) collect(node);
const scopes = new Map<string, { parts: string[]; loopPath: string }>();
function scopeOf(id: string, visited = new Set<string>()): { parts: string[]; loopPath: string } {
const cached = scopes.get(id);
if (cached) return cached;
const entry = entries.get(id);
if (!entry || visited.has(id)) return { parts: [JSON.stringify(["unresolved-parent", id])], loopPath: "" };
visited.add(id);
const parent = entry.parentId ? scopeOf(entry.parentId, visited) : { parts: [], loopPath: "" };
const loopPath = String(entry.node.currentLoopPath ?? parent.loopPath);
const scope = { parts: [...parent.parts, JSON.stringify([entry.node.name || entry.node.type || "unnamed", loopPath])], loopPath };
scopes.set(id, scope);
return scope;
}
for (const { node } of entries.values()) {
const { parts, loopPath } = scopeOf(node.id);
const base = JSON.stringify(parts);
const occurrence = occurrences.get(base) ?? 0;
occurrences.set(base, occurrence + 1);
result.push({ node, key: `${base}#${occurrence}`, scope: parts.slice(0, -1), loopPath });
}
return result;
}