(gateway: GatewayClient, input: CreateGraphInput)
| 36 | && node.cfg?.name === 'onLoad' |
| 37 | && Object.keys(node.props || {}).length === 0 |
| 38 | && Object.keys(node.inputs || {}).length === 0 |
| 39 | && JSON.stringify(node.outputs) === JSON.stringify({ output: [] }); |
| 40 | } |
| 41 | |
| 42 | function validateVariableDefinitions(variables: NonNullable<CreateGraphInput['variables']>): string | undefined { |
| 43 | const ids = new Set<string>(); |
| 44 | for (const variable of variables) { |
| 45 | if (!/^[a-zA-Z0-9]+$/.test(variable.id)) return `变量 ID ${variable.id} 必须是纯字母数字`; |
| 46 | if (ids.has(variable.id)) return `变量 ID ${variable.id} 重复`; |
| 47 | if (typeof variable.value !== variable.type) return `变量 ${variable.id} 的初始值类型必须是 ${variable.type}`; |
| 48 | if (variable.name !== undefined && !variable.name.trim()) return `变量 ${variable.id} 的显示名称不能为空`; |
| 49 | ids.add(variable.id); |
| 50 | } |
| 51 | return undefined; |
| 52 | } |
| 53 | |
| 54 | async function deleteGraphConfirmed(gateway: GatewayClient, id: string): Promise<void> { |
| 55 | try { |
| 56 | await gateway.callApi('deleteGraph', { id }, 10000); |
| 57 | } catch (deleteError) { |
| 58 | const graphList = await gateway.callApi<Array<{ id: string }>>('getGraphList', {}, 10000); |
| 59 | if (!Array.isArray(graphList) || graphList.some((graph) => graph.id === id)) throw deleteError; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | export async function getGraphs(gateway: GatewayClient): Promise<ToolResponse<GraphSummary[]>> { |
| 64 | try { |
| 65 | const graphs = await gateway.callApi<Array<{ id: string; enable?: boolean; userData?: { name?: string; lastUpdateTime?: number }; createTime?: number }>>('getGraphList', {}, 10000); |
| 66 | const graphList = Array.isArray(graphs) ? graphs : []; |
| 67 | return { |
| 68 | success: true, |
| 69 | data: graphList.map((graph) => ({ |
| 70 | id: graph.id, |
| 71 | name: graph.userData?.name || graph.id, |
| 72 | enable: graph.enable ?? false, |
| 73 | createTime: graph.createTime, |
| 74 | updateTime: graph.userData?.lastUpdateTime, |
| 75 | })), |
| 76 | }; |
| 77 | } catch (error) { |
| 78 | return { success: false, error: `获取规则列表失败: ${error}` }; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | export async function getGraph(gateway: GatewayClient, id: string): Promise<ToolResponse<Graph>> { |
| 83 | try { |
| 84 | // 网关的 getGraph 只返回 {id, nodes},名称与启用状态在规则列表里;两次请求同时发 |
| 85 | const [graph, graphList] = await Promise.all([ |
| 86 | gateway.callApi<Graph>('getGraph', { id }, 10000), |
| 87 | gateway.callApi<Array<{ |
no test coverage detected