From 386549d9535f38525ed9fa355e29d11adfda1be0 Mon Sep 17 00:00:00 2001 From: GitHub Actions Bot <> Date: Thu, 9 May 2024 20:59:54 +0000 Subject: [PATCH] Update lib --- lib/graphai.d.ts | 2 +- lib/graphai.js | 5 ++++- lib/node.js | 12 ++++-------- lib/utils/utils.d.ts | 2 +- lib/utils/utils.js | 20 +++++++++++++++++++- lib/validators/relation_validator.js | 4 ++-- 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/lib/graphai.d.ts b/lib/graphai.d.ts index d3a618a0c..4eea79ea0 100644 --- a/lib/graphai.d.ts +++ b/lib/graphai.d.ts @@ -5,7 +5,7 @@ import { ComputedNode, StaticNode } from "./node"; import { TaskManager } from "./task_manager"; type GraphNodes = Record; export declare class GraphAI { - private readonly version; + readonly version: number; private readonly graphId; private readonly data; private readonly loop?; diff --git a/lib/graphai.js b/lib/graphai.js index d7baa6ce7..309ec51cf 100644 --- a/lib/graphai.js +++ b/lib/graphai.js @@ -40,7 +40,7 @@ class GraphAI { return nodes; } getValueFromResults(key, results) { - const source = (0, utils_1.parseNodeName)(key); + const source = (0, utils_1.parseNodeName)(key, this.version); return (0, utils_1.getDataFromSource)(source.nodeId ? results[source.nodeId] : undefined, source); } // for static @@ -71,6 +71,9 @@ class GraphAI { console.log("------------ no version"); } this.version = data.version ?? 0.2; + if (this.version < 0.3) { + console.log("------------ upgrade to 0.3!"); + } this.retryLimit = data.retry; // optional this.graphId = URL.createObjectURL(new Blob()).slice(-36); this.data = data; diff --git a/lib/node.js b/lib/node.js index f27476299..0f70f3627 100644 --- a/lib/node.js +++ b/lib/node.js @@ -54,21 +54,17 @@ class ComputedNode extends Node { this.isResult = data.isResult ?? false; this.priority = data.priority ?? 0; this.anyInput = data.anyInput ?? false; - this.dataSources = (data.inputs ?? []).map((input) => (0, utils_2.parseNodeName)(input)); + this.dataSources = (data.inputs ?? []).map((input) => (0, utils_2.parseNodeName)(input, graph.version)); this.pendings = new Set(this.dataSources.filter((source) => source.nodeId).map((source) => source.nodeId)); if (data.if) { - this.ifSource = (0, utils_2.parseNodeName)(data.if); + this.ifSource = (0, utils_2.parseNodeName)(data.if, graph.version); (0, utils_2.assert)(!!this.ifSource.nodeId, `Invalid data source ${data.if}`); this.pendings.add(this.ifSource.nodeId); } - const regex = /^\$\{([^{}]+)\}$/; this.dynamicParams = Object.keys(this.params).reduce((tmp, key) => { - const value = this.params[key]; - const match = typeof value === "string" ? value.match(regex) : null; - if (match) { - const dataSource = (0, utils_2.parseNodeName)(match[1]); + const dataSource = (0, utils_2.parseNodeName)(this.params[key], graph.version < 0.3 ? 0.3 : graph.version); + if (dataSource.nodeId) { tmp[key] = dataSource; - (0, utils_2.assert)(!!dataSource.nodeId, `Invalid data source ${key}:${value}`); this.pendings.add(dataSource.nodeId); } return tmp; diff --git a/lib/utils/utils.d.ts b/lib/utils/utils.d.ts index 99b9c97aa..11b2e5dc4 100644 --- a/lib/utils/utils.d.ts +++ b/lib/utils/utils.d.ts @@ -1,6 +1,6 @@ import { DataSource, ResultData } from "../type"; export declare const sleep: (milliseconds: number) => Promise; -export declare const parseNodeName: (inputNodeId: any) => DataSource; +export declare const parseNodeName: (inputNodeId: any, version: number) => DataSource; export declare function assert(condition: boolean, message: string, isWarn?: boolean): asserts condition; export declare const isObject: (x: unknown) => boolean; export declare const getDataFromSource: (result: ResultData | undefined, source: DataSource) => ResultData | undefined; diff --git a/lib/utils/utils.js b/lib/utils/utils.js index e30bca8dd..db6fd9fb9 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -5,7 +5,7 @@ const sleep = async (milliseconds) => { return await new Promise((resolve) => setTimeout(resolve, milliseconds)); }; exports.sleep = sleep; -const parseNodeName = (inputNodeId) => { +const parseNodeName_02 = (inputNodeId) => { if (typeof inputNodeId === "string") { const regex = /^"(.*)"$/; const match = inputNodeId.match(regex); @@ -20,6 +20,24 @@ const parseNodeName = (inputNodeId) => { } return { value: inputNodeId }; // non-string literal }; +const parseNodeName = (inputNodeId, version) => { + if (version === 0.2) { + return parseNodeName_02(inputNodeId); + } + if (typeof inputNodeId === "string") { + const regex = /^:(.*)$/; + const match = inputNodeId.match(regex); + if (!match) { + return { value: inputNodeId }; // string literal + } + const parts = match[1].split("."); + if (parts.length == 1) { + return { nodeId: parts[0] }; + } + return { nodeId: parts[0], propIds: parts.slice(1) }; + } + return { value: inputNodeId }; // non-string literal +}; exports.parseNodeName = parseNodeName; function assert(condition, message, isWarn = false) { if (!condition) { diff --git a/lib/validators/relation_validator.js b/lib/validators/relation_validator.js index c432adb25..b2c92a93a 100644 --- a/lib/validators/relation_validator.js +++ b/lib/validators/relation_validator.js @@ -12,7 +12,7 @@ const relationValidator = (data, staticNodeIds, computedNodeIds) => { pendings[computedNodeId] = new Set(); if ("inputs" in nodeData && nodeData && nodeData.inputs) { nodeData.inputs.forEach((inputNodeId) => { - const sourceNodeId = (0, utils_1.parseNodeName)(inputNodeId).nodeId; + const sourceNodeId = (0, utils_1.parseNodeName)(inputNodeId, data.version ?? 0.02).nodeId; if (sourceNodeId) { if (!nodeIds.has(sourceNodeId)) { throw new Error(`Inputs not match: NodeId ${computedNodeId}, Inputs: ${sourceNodeId}`); @@ -29,7 +29,7 @@ const relationValidator = (data, staticNodeIds, computedNodeIds) => { const nodeData = data.nodes[staticNodeId]; if ("value" in nodeData && nodeData.update) { const update = nodeData.update; - const updateNodeId = (0, utils_1.parseNodeName)(update).nodeId; + const updateNodeId = (0, utils_1.parseNodeName)(update, data.version ?? 0.02).nodeId; if (!updateNodeId) { throw new Error("Update it a literal"); }