graphai 0.6.2 → 0.6.4

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/bundle.esm.js CHANGED
@@ -247,7 +247,7 @@ class ComputedNode extends Node {
247
247
  super(nodeId, graph);
248
248
  this.retryCount = 0;
249
249
  this.dataSources = []; // no longer needed. This is for transaction log.
250
- this.isNamedInputs = false;
250
+ this.isSkip = false;
251
251
  this.isStaticNode = false;
252
252
  this.isComputedNode = true;
253
253
  this.graphId = graphId;
@@ -261,8 +261,7 @@ class ComputedNode extends Node {
261
261
  this.priority = data.priority ?? 0;
262
262
  this.anyInput = data.anyInput ?? false;
263
263
  this.inputs = data.inputs;
264
- // this.isNamedInputs = isObject(data.inputs) && !Array.isArray(data.inputs);
265
- this.dataSources = data.inputs ? inputs2dataSources(data.inputs).flat(10) : [];
264
+ this.dataSources = [...(data.inputs ? inputs2dataSources(data.inputs).flat(10) : []), ...(data.params ? inputs2dataSources(data.params).flat(10) : [])];
266
265
  if (data.inputs && Array.isArray(data.inputs)) {
267
266
  throw new Error(`array inputs have been deprecated. nodeId: ${nodeId}: see https://github.com/receptron/graphai/blob/main/docs/NamedInputs.md`);
268
267
  }
@@ -273,7 +272,6 @@ class ComputedNode extends Node {
273
272
  }
274
273
  else {
275
274
  const agent = data.agent;
276
- // this.agentFunction = this.isNamedInputs ? async ({ namedInputs, params }) => agent(namedInputs, params) : async ({ inputs }) => agent(...inputs);
277
275
  this.agentFunction = async ({ namedInputs, params }) => agent(namedInputs, params);
278
276
  }
279
277
  if (data.graph) {
@@ -288,15 +286,10 @@ class ComputedNode extends Node {
288
286
  if (data.unless) {
289
287
  this.unlessSource = this.addPendingNode(data.unless);
290
288
  }
291
- this.dynamicParams = Object.keys(this.params).reduce((tmp, key) => {
292
- const dataSource = parseNodeName(this.params[key]);
293
- if (dataSource.nodeId) {
294
- assert(!this.anyInput, "Dynamic params are not supported with anyInput");
295
- tmp[key] = dataSource;
296
- this.pendings.add(dataSource.nodeId);
297
- }
298
- return tmp;
299
- }, {});
289
+ if (data.defaultValue) {
290
+ this.defaultValue = data.defaultValue;
291
+ }
292
+ this.isSkip = false;
300
293
  this.log.initForComputedNode(this, graph);
301
294
  }
302
295
  getAgentId() {
@@ -312,8 +305,9 @@ class ComputedNode extends Node {
312
305
  if (this.state !== NodeState.Waiting || this.pendings.size !== 0) {
313
306
  return false;
314
307
  }
315
- if ((this.ifSource && !isLogicallyTrue(this.graph.resultOf(this.ifSource))) ||
316
- (this.unlessSource && isLogicallyTrue(this.graph.resultOf(this.unlessSource)))) {
308
+ this.isSkip = !!((this.ifSource && !isLogicallyTrue(this.graph.resultOf(this.ifSource))) ||
309
+ (this.unlessSource && isLogicallyTrue(this.graph.resultOf(this.unlessSource))));
310
+ if (this.isSkip && this.defaultValue === undefined) {
317
311
  this.state = NodeState.Skipped;
318
312
  this.log.onSkipped(this, this.graph);
319
313
  return false;
@@ -407,6 +401,10 @@ class ComputedNode extends Node {
407
401
  // then it removes itself from the "running node" list of the graph.
408
402
  // Notice that setting the result of this node may make other nodes ready to run.
409
403
  async execute() {
404
+ if (this.isSkip) {
405
+ this.afterExecute(this.defaultValue, []);
406
+ return;
407
+ }
410
408
  const previousResults = this.graph.resultsOf(this.inputs, this.anyInput);
411
409
  const transactionId = Date.now();
412
410
  this.prepareExecute(transactionId, Object.values(previousResults));
@@ -423,18 +421,10 @@ class ComputedNode extends Node {
423
421
  // if this is a nested agent or not.
424
422
  if (this.nestedGraph) {
425
423
  this.graph.taskManager.prepareForNesting();
426
- context.taskManager = this.graph.taskManager;
427
424
  context.onLogCallback = this.graph.onLogCallback;
428
- if ("nodes" in this.nestedGraph) {
429
- context.graphData = this.nestedGraph;
430
- }
431
- else {
432
- context.graphData = this.graph.resultOf(this.nestedGraph); // HACK: compiler work-around
433
- }
434
- context.agents = this.graph.agentFunctionInfoDictionary;
435
425
  context.forNestedGraph = {
436
- graphData: context.graphData,
437
- agents: context.agents,
426
+ graphData: "nodes" in this.nestedGraph ? this.nestedGraph : this.graph.resultOf(this.nestedGraph), // HACK: compiler work-around
427
+ agents: this.graph.agentFunctionInfoDictionary,
438
428
  graphOptions: {
439
429
  agentFilters: this.graph.agentFilters,
440
430
  taskManager: this.graph.taskManager,
@@ -442,6 +432,7 @@ class ComputedNode extends Node {
442
432
  config: this.graph.config,
443
433
  graphLoader: this.graph.graphLoader,
444
434
  },
435
+ onLogCallback: this.graph.onLogCallback,
445
436
  };
446
437
  }
447
438
  this.beforeConsoleLog(context);
@@ -456,16 +447,20 @@ class ComputedNode extends Node {
456
447
  console.log(`-- transactionId mismatch with ${this.nodeId} (probably timeout)`);
457
448
  return;
458
449
  }
459
- this.state = NodeState.Completed;
460
- this.result = this.getResult(result);
461
- this.log.onComplete(this, this.graph, localLog);
462
- this.onSetResult();
463
- this.graph.onExecutionComplete(this);
450
+ // after process
451
+ this.afterExecute(result, localLog);
464
452
  }
465
453
  catch (error) {
466
454
  this.errorProcess(error, transactionId, previousResults);
467
455
  }
468
456
  }
457
+ afterExecute(result, localLog) {
458
+ this.state = NodeState.Completed;
459
+ this.result = this.getResult(result);
460
+ this.log.onComplete(this, this.graph, localLog);
461
+ this.onSetResult();
462
+ this.graph.onExecutionComplete(this);
463
+ }
469
464
  // This private method (called only by execute()) prepares the ComputedNode object
470
465
  // for execution, and create a new transaction to record it.
471
466
  prepareExecute(transactionId, inputs) {
@@ -495,25 +490,9 @@ class ComputedNode extends Node {
495
490
  this.retry(NodeState.Failed, Error("Unknown"));
496
491
  }
497
492
  }
498
- getParams() {
499
- return Object.keys(this.dynamicParams).reduce((tmp, key) => {
500
- const result = this.graph.resultOf(this.dynamicParams[key]);
501
- tmp[key] = result;
502
- return tmp;
503
- }, { ...this.params });
504
- }
505
- /*
506
- private getInputs(previousResults: Record<string, ResultData | undefined>) {
507
- if (Array.isArray(this.inputs)) {
508
- return (this.inputs ?? []).map((key) => previousResults[String(key)]).filter((a) => !this.anyInput || a);
509
- }
510
- return [];
511
- }
512
- */
513
493
  getContext(previousResults, localLog) {
514
494
  const context = {
515
- params: this.getParams(),
516
- // inputs: this.getInputs(previousResults),
495
+ params: this.graph.resultsOf(this.params),
517
496
  namedInputs: previousResults,
518
497
  inputSchema: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(this.agentId)?.inputs,
519
498
  debugInfo: this.getDebugInfo(),
@@ -548,7 +527,6 @@ class ComputedNode extends Node {
548
527
  }
549
528
  beforeConsoleLog(context) {
550
529
  if (this.console.before === true) {
551
- // console.log(JSON.stringify(this.isNamedInputs ? context.namedInputs : context.inputs, null, 2));
552
530
  console.log(JSON.stringify(context.namedInputs, null, 2));
553
531
  }
554
532
  else if (this.console.before) {
@@ -593,7 +571,7 @@ const propArrayFunction = (result, propId) => {
593
571
  return result.length === 0;
594
572
  }
595
573
  // array join
596
- const matchJoin = propId.match(/^join\(([,-]?)\)$/);
574
+ const matchJoin = propId.match(/^join\(([,-\s]?)\)$/);
597
575
  if (matchJoin && Array.isArray(matchJoin)) {
598
576
  return result.join(matchJoin[1] ?? "");
599
577
  }
@@ -631,6 +609,16 @@ const propStringFunction = (result, propId) => {
631
609
  return ret;
632
610
  }
633
611
  }
612
+ if (propId === "trim()") {
613
+ return result.trim();
614
+ }
615
+ if (propId === "toLowerCase()") {
616
+ return result.toLowerCase();
617
+ }
618
+ if (propId === "toUpperCase()") {
619
+ return result.toUpperCase();
620
+ }
621
+ // split()
634
622
  }
635
623
  return undefined;
636
624
  };
@@ -784,6 +772,7 @@ const computedNodeAttributeKeys = [
784
772
  "priority",
785
773
  "if",
786
774
  "unless",
775
+ "defaultValue",
787
776
  "filterParams",
788
777
  "console",
789
778
  "passThrough",
@@ -839,9 +828,9 @@ const nodeValidator = (nodeData) => {
839
828
  if (nodeData.agent && nodeData.value) {
840
829
  throw new ValidationError("Cannot set both agent and value");
841
830
  }
842
- if (!("agent" in nodeData) && !("value" in nodeData)) {
843
- throw new ValidationError("Either agent or value is required");
844
- }
831
+ // if (!("agent" in nodeData) && !("value" in nodeData)) {
832
+ // throw new ValidationError("Either agent or value is required");
833
+ // }
845
834
  return true;
846
835
  };
847
836
 
@@ -960,7 +949,7 @@ const validateGraphData = (data, agentIds) => {
960
949
  const graphAgentIds = new Set();
961
950
  Object.keys(data.nodes).forEach((nodeId) => {
962
951
  const node = data.nodes[nodeId];
963
- const isStaticNode = "value" in node;
952
+ const isStaticNode = !("agent" in node);
964
953
  nodeValidator(node);
965
954
  const agentId = isStaticNode ? "" : node.agent;
966
955
  isStaticNode && staticNodeValidator(node) && staticNodeIds.push(nodeId);
@@ -1047,15 +1036,13 @@ class GraphAI {
1047
1036
  createNodes(data) {
1048
1037
  const nodes = Object.keys(data.nodes).reduce((_nodes, nodeId) => {
1049
1038
  const nodeData = data.nodes[nodeId];
1050
- if ("value" in nodeData) {
1051
- _nodes[nodeId] = new StaticNode(nodeId, nodeData, this);
1052
- }
1053
- else if ("agent" in nodeData) {
1039
+ if ("agent" in nodeData) {
1054
1040
  _nodes[nodeId] = new ComputedNode(this.graphId, nodeId, nodeData, this);
1055
1041
  }
1056
1042
  else {
1057
- throw new Error("Unknown node type (neither value nor agent): " + nodeId);
1043
+ _nodes[nodeId] = new StaticNode(nodeId, nodeData, this);
1058
1044
  }
1045
+ // throw new Error("Unknown node type (neither value nor agent): " + nodeId);
1059
1046
  return _nodes;
1060
1047
  }, {});
1061
1048
  // Generate the waitlist for each node.
@@ -1224,6 +1211,11 @@ class GraphAI {
1224
1211
  }
1225
1212
  // Public API
1226
1213
  async run(all = false) {
1214
+ if (Object.values(this.nodes)
1215
+ .filter((node) => node.isStaticNode)
1216
+ .some((node) => node.result === undefined && node.update === undefined)) {
1217
+ throw new Error("Static node must have value. Set value or injectValue or set update");
1218
+ }
1227
1219
  if (this.isRunning()) {
1228
1220
  throw new Error("This GraphUI instance is already running");
1229
1221
  }