Skip to content

Commit

Permalink
Update lib
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub Actions Bot committed May 9, 2024
1 parent 096c5f9 commit 386549d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/graphai.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ComputedNode, StaticNode } from "./node";
import { TaskManager } from "./task_manager";
type GraphNodes = Record<string, ComputedNode | StaticNode>;
export declare class GraphAI {
private readonly version;
readonly version: number;
private readonly graphId;
private readonly data;
private readonly loop?;
Expand Down
5 changes: 4 additions & 1 deletion lib/graphai.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 4 additions & 8 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataSource, ResultData } from "../type";
export declare const sleep: (milliseconds: number) => Promise<unknown>;
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;
Expand Down
20 changes: 19 additions & 1 deletion lib/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions lib/validators/relation_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand All @@ -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");
}
Expand Down

0 comments on commit 386549d

Please sign in to comment.