(gateway: GatewayClient, id: string, input: Partial<CreateGraphInput>)
| 84 | // 网关的 getGraph 只返回 {id, nodes},名称与启用状态在规则列表里;两次请求同时发 |
| 85 | const [graph, graphList] = await Promise.all([ |
| 86 | gateway.callApi<Graph>('getGraph', { id }, 10000), |
| 87 | gateway.callApi<Array<{ |
| 88 | id: string; |
| 89 | enable?: boolean; |
| 90 | userData?: Graph['cfg']['userData']; |
| 91 | }>>('getGraphList', {}, 10000).catch(() => undefined), |
| 92 | ]); |
| 93 | if (!graph.cfg) { |
| 94 | const info = Array.isArray(graphList) ? graphList.find((item) => item.id === id) : undefined; |
| 95 | graph.cfg = { |
| 96 | id, |
| 97 | enable: info?.enable ?? false, |
| 98 | uiType: 'graph', |
| 99 | userData: { |
| 100 | name: info?.userData?.name || id, |
| 101 | lastUpdateTime: info?.userData?.lastUpdateTime || 0, |
| 102 | transform: info?.userData?.transform || { x: 0, y: 0, scale: 1, rotate: 0 }, |
| 103 | }, |
| 104 | }; |
| 105 | } |
| 106 | return { success: true, data: graph }; |
| 107 | } catch (error) { |
| 108 | return { success: false, error: `获取规则详情失败: ${error}` }; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | export async function createGraph(gateway: GatewayClient, input: CreateGraphInput): Promise<ToolResponse<{ graphId: string }>> { |
| 113 | let graphId = input.graphId || nextGraphId(); |
| 114 | let variableScope = `R${graphId}`; |
| 115 | const variables = input.variables || []; |
| 116 | const createdVariables: string[] = []; |
| 117 | let shellCreatedHere = false; |
| 118 | let resumedExisting = false; |
| 119 | try { |
| 120 | const [existingGraphs, scopeResult] = await Promise.all([ |
| 121 | gateway.callApi<Array<{ id: string }>>('getGraphList', {}, 10000), |
| 122 | gateway.callApi<{ scopes?: string[] } | string[]>('getVarScopeList', {}, 10000), |
| 123 | ]); |
| 124 | const existingIds = new Set((Array.isArray(existingGraphs) ? existingGraphs : []).map((graph) => graph.id)); |
| 125 | const existingScopes = new Set(Array.isArray(scopeResult) ? scopeResult : scopeResult?.scopes || []); |
| 126 | while (!input.graphId && (existingIds.has(graphId) || existingScopes.has(`R${graphId}`))) graphId = nextGraphId(); |
| 127 | variableScope = `R${graphId}`; |
| 128 | const definitionError = validateVariableDefinitions(variables); |
| 129 | if (definitionError) return { success: false, error: definitionError }; |
| 130 | |
| 131 | const processedNodes = input.nodes.map((node) => ({ |
| 132 | ...node, |
| 133 | cfg: { |
| 134 | ...node.cfg, |
| 135 | name: (node.cfg as Record<string, unknown>)?.name || node.type, |
| 136 | version: (node.cfg as Record<string, unknown>)?.version ?? 1, |
| 137 | }, |
| 138 | props: replaceRuleScope(node.props || {}, variableScope) as Record<string, unknown>, |
| 139 | })); |
| 140 | |
| 141 | processedNodes.splice(0, processedNodes.length, ...arrangeNodes(processedNodes).nodes as typeof processedNodes); |
no test coverage detected