graphai 0.4.8 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/node.d.ts +1 -0
- package/lib/node.js +36 -10
- package/lib/type.d.ts +4 -1
- package/lib/validators/relation_validator.js +27 -10
- package/package.json +1 -1
package/lib/node.d.ts
CHANGED
package/lib/node.js
CHANGED
|
@@ -40,23 +40,41 @@ class ComputedNode extends Node {
|
|
|
40
40
|
this.params = data.params ?? {};
|
|
41
41
|
this.console = data.console ?? {};
|
|
42
42
|
this.filterParams = data.filterParams ?? {};
|
|
43
|
+
this.retryLimit = data.retry ?? graph.retryLimit ?? 0;
|
|
44
|
+
this.timeout = data.timeout;
|
|
45
|
+
this.isResult = data.isResult ?? false;
|
|
46
|
+
this.priority = data.priority ?? 0;
|
|
47
|
+
this.anyInput = data.anyInput ?? false;
|
|
48
|
+
if (!data.inputs) {
|
|
49
|
+
this.dataSources = [];
|
|
50
|
+
}
|
|
51
|
+
else if (Array.isArray(data.inputs)) {
|
|
52
|
+
this.dataSources = (data.inputs ?? []).map((input) => (0, utils_2.parseNodeName)(input, graph.version));
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
const inputs = data.inputs;
|
|
56
|
+
const keys = Object.keys(inputs);
|
|
57
|
+
this.inputNames = keys;
|
|
58
|
+
this.dataSources = keys.map((key) => (0, utils_2.parseNodeName)(inputs[key], graph.version));
|
|
59
|
+
}
|
|
60
|
+
this.pendings = new Set(this.dataSources.filter((source) => source.nodeId).map((source) => source.nodeId));
|
|
43
61
|
if (typeof data.agent === "string") {
|
|
44
62
|
this.agentId = data.agent;
|
|
45
63
|
}
|
|
46
64
|
else {
|
|
47
65
|
(0, utils_2.assert)(typeof data.agent === "function", "agent must be either string or function");
|
|
48
66
|
const agent = data.agent;
|
|
49
|
-
this.
|
|
50
|
-
|
|
51
|
-
|
|
67
|
+
if (this.inputNames) {
|
|
68
|
+
this.agentFunction = async ({ namedInputs }) => {
|
|
69
|
+
return agent(namedInputs);
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
this.agentFunction = async ({ inputs }) => {
|
|
74
|
+
return agent(...inputs);
|
|
75
|
+
};
|
|
76
|
+
}
|
|
52
77
|
}
|
|
53
|
-
this.retryLimit = data.retry ?? graph.retryLimit ?? 0;
|
|
54
|
-
this.timeout = data.timeout;
|
|
55
|
-
this.isResult = data.isResult ?? false;
|
|
56
|
-
this.priority = data.priority ?? 0;
|
|
57
|
-
this.anyInput = data.anyInput ?? false;
|
|
58
|
-
this.dataSources = (data.inputs ?? []).map((input) => (0, utils_2.parseNodeName)(input, graph.version));
|
|
59
|
-
this.pendings = new Set(this.dataSources.filter((source) => source.nodeId).map((source) => source.nodeId));
|
|
60
78
|
if (typeof data.graph === "string") {
|
|
61
79
|
const source = (0, utils_2.parseNodeName)(data.graph, graph.version);
|
|
62
80
|
(0, utils_2.assert)(!!source.nodeId, `Invalid data source ${data.graph}`);
|
|
@@ -232,6 +250,7 @@ class ComputedNode extends Node {
|
|
|
232
250
|
const context = {
|
|
233
251
|
params: params,
|
|
234
252
|
inputs: previousResults,
|
|
253
|
+
namedInputs: {},
|
|
235
254
|
debugInfo: {
|
|
236
255
|
nodeId: this.nodeId,
|
|
237
256
|
agentId: this.agentId,
|
|
@@ -242,6 +261,13 @@ class ComputedNode extends Node {
|
|
|
242
261
|
agentFilters: this.graph.agentFilters,
|
|
243
262
|
log: localLog,
|
|
244
263
|
};
|
|
264
|
+
if (this.inputNames) {
|
|
265
|
+
context.namedInputs = this.inputNames.reduce((tmp, name, index) => {
|
|
266
|
+
tmp[name] = previousResults[index];
|
|
267
|
+
return tmp;
|
|
268
|
+
}, {});
|
|
269
|
+
context.inputs = [];
|
|
270
|
+
}
|
|
245
271
|
// NOTE: We use the existence of graph object in the agent-specific params to determine
|
|
246
272
|
// if this is a nested agent or not.
|
|
247
273
|
if (this.nestedGraph) {
|
package/lib/type.d.ts
CHANGED
|
@@ -30,7 +30,7 @@ export type AgentAnonymousFunction = (...params: any[]) => unknown;
|
|
|
30
30
|
export type AgentFilterParams = Record<string, any>;
|
|
31
31
|
export type ComputedNodeData = {
|
|
32
32
|
agent: string | AgentAnonymousFunction;
|
|
33
|
-
inputs?: Array<any>;
|
|
33
|
+
inputs?: Array<any> | Record<string, any>;
|
|
34
34
|
anyInput?: boolean;
|
|
35
35
|
params?: NodeDataParams;
|
|
36
36
|
filterParams?: AgentFilterParams;
|
|
@@ -64,6 +64,7 @@ export type GraphOptions = {
|
|
|
64
64
|
export type AgentFunctionContext<ParamsType = DefaultParamsType, InputDataType = DefaultInputData> = {
|
|
65
65
|
params: NodeDataParams<ParamsType>;
|
|
66
66
|
inputs: Array<InputDataType>;
|
|
67
|
+
namedInputs: Record<string, any>;
|
|
67
68
|
debugInfo: {
|
|
68
69
|
verbose: boolean;
|
|
69
70
|
nodeId: string;
|
|
@@ -90,6 +91,8 @@ export type AgentFunctionInfo = {
|
|
|
90
91
|
name: string;
|
|
91
92
|
agent: AgentFunction<any, any, any>;
|
|
92
93
|
mock: AgentFunction<any, any, any>;
|
|
94
|
+
inputs?: any;
|
|
95
|
+
output?: any;
|
|
93
96
|
samples: {
|
|
94
97
|
inputs: any;
|
|
95
98
|
params: DefaultParamsType;
|
|
@@ -12,17 +12,34 @@ const relationValidator = (data, staticNodeIds, computedNodeIds) => {
|
|
|
12
12
|
const nodeData = data.nodes[computedNodeId];
|
|
13
13
|
pendings[computedNodeId] = new Set();
|
|
14
14
|
if ("inputs" in nodeData && nodeData && nodeData.inputs) {
|
|
15
|
-
nodeData.inputs
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (
|
|
19
|
-
|
|
15
|
+
if (Array.isArray(nodeData.inputs)) {
|
|
16
|
+
nodeData.inputs.forEach((inputNodeId) => {
|
|
17
|
+
const sourceNodeId = (0, utils_1.parseNodeName)(inputNodeId, data.version ?? 0.02).nodeId;
|
|
18
|
+
if (sourceNodeId) {
|
|
19
|
+
if (!nodeIds.has(sourceNodeId)) {
|
|
20
|
+
throw new common_1.ValidationError(`Inputs not match: NodeId ${computedNodeId}, Inputs: ${sourceNodeId}`);
|
|
21
|
+
}
|
|
22
|
+
waitlist[sourceNodeId] === undefined && (waitlist[sourceNodeId] = new Set());
|
|
23
|
+
pendings[computedNodeId].add(sourceNodeId);
|
|
24
|
+
waitlist[sourceNodeId].add(computedNodeId);
|
|
20
25
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
const keys = Object.keys(nodeData.inputs);
|
|
30
|
+
keys.forEach((key) => {
|
|
31
|
+
const inputNodeId = nodeData.inputs[key];
|
|
32
|
+
const sourceNodeId = (0, utils_1.parseNodeName)(inputNodeId, data.version ?? 0.3).nodeId;
|
|
33
|
+
if (sourceNodeId) {
|
|
34
|
+
if (!nodeIds.has(sourceNodeId)) {
|
|
35
|
+
throw new common_1.ValidationError(`Inputs not match: NodeId ${computedNodeId}, Inputs: ${sourceNodeId}`);
|
|
36
|
+
}
|
|
37
|
+
waitlist[sourceNodeId] === undefined && (waitlist[sourceNodeId] = new Set());
|
|
38
|
+
pendings[computedNodeId].add(sourceNodeId);
|
|
39
|
+
waitlist[sourceNodeId].add(computedNodeId);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
26
43
|
}
|
|
27
44
|
});
|
|
28
45
|
// TODO. validate update
|