graphai 0.5.4 → 0.5.6

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/graphai.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AgentFunctionInfoDictionary, AgentFilterInfo, GraphData, DataSource, ResultDataDictionary, ResultData, DefaultResultData, GraphOptions } from "./type";
1
+ import { AgentFunctionInfoDictionary, AgentFilterInfo, GraphData, DataSource, ResultDataDictionary, ResultData, DefaultResultData, GraphOptions, NestedDataSource } from "./type";
2
2
  import { TransactionLog } from "./transaction_log";
3
3
  import { ComputedNode, StaticNode } from "./node";
4
4
  import { TaskManager } from "./task_manager";
@@ -46,7 +46,8 @@ export declare class GraphAI {
46
46
  updateLog(log: TransactionLog): void;
47
47
  transactionLogs(): TransactionLog[];
48
48
  injectValue(nodeId: string, value: ResultData, injectFrom?: string): void;
49
- resultsOf(sources: Record<string, DataSource | DataSource[]>): Record<string, ResultData>;
49
+ private nestedResultOf;
50
+ resultsOf(sources: NestedDataSource): Record<string, ResultData>;
50
51
  resultOf(source: DataSource): ResultData;
51
52
  }
52
53
  export {};
package/lib/graphai.js CHANGED
@@ -252,18 +252,18 @@ class GraphAI {
252
252
  throw new Error(`injectValue with Invalid nodeId, ${nodeId}`);
253
253
  }
254
254
  }
255
+ nestedResultOf(source) {
256
+ if (Array.isArray(source)) {
257
+ return source.map((a) => {
258
+ return this.nestedResultOf(a);
259
+ });
260
+ }
261
+ return this.resultOf(source);
262
+ }
255
263
  resultsOf(sources) {
256
264
  const ret = {};
257
265
  Object.keys(sources).forEach((key) => {
258
- const source = sources[key];
259
- if (Array.isArray(source)) {
260
- ret[key] = source.map((s) => {
261
- return this.resultOf(s);
262
- });
263
- }
264
- else {
265
- ret[key] = this.resultOf(source);
266
- }
266
+ ret[key] = this.nestedResultOf(sources[key]);
267
267
  });
268
268
  return ret;
269
269
  }
package/lib/node.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { GraphAI, GraphData } from "./index";
2
- import { NodeDataParams, ResultData, DataSource, ComputedNodeData, StaticNodeData, NodeState } from "./type";
2
+ import { NodeDataParams, ResultData, DataSource, ComputedNodeData, StaticNodeData, NodeState, NestedDataSource } from "./type";
3
3
  import { TransactionLog } from "./transaction_log";
4
4
  export declare class Node {
5
5
  readonly nodeId: string;
@@ -28,7 +28,7 @@ export declare class ComputedNode extends Node {
28
28
  error?: Error;
29
29
  transactionId: undefined | number;
30
30
  readonly anyInput: boolean;
31
- dataSources: Record<string, DataSource | DataSource[]>;
31
+ dataSources: NestedDataSource;
32
32
  private inputs?;
33
33
  inputNames?: Array<string>;
34
34
  pendings: Set<string>;
@@ -55,6 +55,8 @@ export declare class ComputedNode extends Node {
55
55
  private getNamedInput;
56
56
  private getInputs;
57
57
  private getDebugInfo;
58
+ private beforeConsoleLog;
59
+ private afterConsoleLog;
58
60
  }
59
61
  export declare class StaticNode extends Node {
60
62
  value?: ResultData;
package/lib/node.js CHANGED
@@ -57,32 +57,17 @@ class ComputedNode extends Node {
57
57
  this.dataSources = (0, nodeUtils_1.namedInputs2dataSources)(data.inputs, graph.version);
58
58
  }
59
59
  }
60
- this.pendings = new Set(Object.values(this.dataSources)
61
- .flat()
62
- .filter((source) => source.nodeId)
63
- .map((source) => source.nodeId));
60
+ this.pendings = new Set((0, nodeUtils_1.flatDataSourceNodeIds)(Object.values(this.dataSources)));
61
+ (0, utils_2.assert)(["function", "string"].includes(typeof data.agent), "agent must be either string or function");
64
62
  if (typeof data.agent === "string") {
65
63
  this.agentId = data.agent;
66
64
  }
67
65
  else {
68
- (0, utils_2.assert)(typeof data.agent === "function", "agent must be either string or function");
69
66
  const agent = data.agent;
70
- if (this.inputNames) {
71
- this.agentFunction = async ({ namedInputs }) => {
72
- return agent(namedInputs);
73
- };
74
- }
75
- else {
76
- this.agentFunction = async ({ inputs }) => {
77
- return agent(...inputs);
78
- };
79
- }
67
+ this.agentFunction = this.inputNames ? async ({ namedInputs }) => agent(namedInputs) : async ({ inputs }) => agent(...inputs);
80
68
  }
81
- if (typeof data.graph === "string") {
82
- this.nestedGraph = this.addPengindNode(data.graph);
83
- }
84
- else if (data.graph) {
85
- this.nestedGraph = data.graph;
69
+ if (data.graph) {
70
+ this.nestedGraph = typeof data.graph === "string" ? this.addPengindNode(data.graph) : data.graph;
86
71
  }
87
72
  if (data.if) {
88
73
  this.ifSource = this.addPengindNode(data.if);
@@ -114,12 +99,6 @@ class ComputedNode extends Node {
114
99
  if (this.state !== type_1.NodeState.Waiting || this.pendings.size !== 0) {
115
100
  return false;
116
101
  }
117
- // Count the number of data actually available.
118
- // We care it only when this.anyInput is true.
119
- // Notice that this logic enables dynamic data-flows.
120
- if (this.anyInput && !this.checkDataAvailability(false)) {
121
- return false;
122
- }
123
102
  if ((this.ifSource && !(0, utils_2.isLogicallyTrue)(this.graph.resultOf(this.ifSource))) ||
124
103
  (this.unlessSource && (0, utils_2.isLogicallyTrue)(this.graph.resultOf(this.unlessSource)))) {
125
104
  this.state = type_1.NodeState.Skipped;
@@ -145,10 +124,7 @@ class ComputedNode extends Node {
145
124
  this.graph.onExecutionComplete(this);
146
125
  }
147
126
  }
148
- checkDataAvailability(checkAnyInput = true) {
149
- if (checkAnyInput) {
150
- (0, utils_2.assert)(this.anyInput, "checkDataAvailability should be called only for anyInput case");
151
- }
127
+ checkDataAvailability() {
152
128
  return Object.values(this.graph.resultsOf(this.dataSources))
153
129
  .flat()
154
130
  .some((result) => result !== undefined);
@@ -254,18 +230,13 @@ class ComputedNode extends Node {
254
230
  context.graphData = this.nestedGraph;
255
231
  }
256
232
  else {
257
- const graphData = this.graph.resultOf(this.nestedGraph);
258
- context.graphData = graphData; // HACK: compiler work-around
233
+ context.graphData = this.graph.resultOf(this.nestedGraph); // HACK: compiler work-around
259
234
  }
260
235
  context.agents = this.graph.agentFunctionInfoDictionary;
261
236
  }
262
- if (this.console.before) {
263
- console.log(this.console.before === true ? JSON.stringify(this.inputNames ? context.namedInputs : context.inputs, null, 2) : this.console.before);
264
- }
237
+ this.beforeConsoleLog(context);
265
238
  const result = await this.agentFilterHandler(context, agentFunction);
266
- if (this.console.after) {
267
- console.log(this.console.after === true ? (typeof result === "string" ? result : JSON.stringify(result, null, 2)) : this.console.after);
268
- }
239
+ this.afterConsoleLog(result);
269
240
  if (this.nestedGraph) {
270
241
  this.graph.taskManager.restoreAfterNesting();
271
242
  }
@@ -328,7 +299,7 @@ class ComputedNode extends Node {
328
299
  if (this.inputNames) {
329
300
  return [];
330
301
  }
331
- return (this.inputs ?? []).map((key) => previousResults[String(key)]).filter(a => !this.anyInput || a);
302
+ return (this.inputs ?? []).map((key) => previousResults[String(key)]).filter((a) => !this.anyInput || a);
332
303
  }
333
304
  getDebugInfo() {
334
305
  return {
@@ -337,8 +308,25 @@ class ComputedNode extends Node {
337
308
  retry: this.retryCount,
338
309
  verbose: this.graph.verbose,
339
310
  version: this.graph.version,
311
+ isResult: this.isResult,
340
312
  };
341
313
  }
314
+ beforeConsoleLog(context) {
315
+ if (this.console.before === true) {
316
+ console.log(JSON.stringify(this.inputNames ? context.namedInputs : context.inputs, null, 2));
317
+ }
318
+ else if (this.console.before) {
319
+ console.log(this.console.before);
320
+ }
321
+ }
322
+ afterConsoleLog(result) {
323
+ if (this.console.after === true) {
324
+ console.log(typeof result === "string" ? result : JSON.stringify(result, null, 2));
325
+ }
326
+ else if (this.console.after) {
327
+ console.log(this.console.after);
328
+ }
329
+ }
342
330
  }
343
331
  exports.ComputedNode = ComputedNode;
344
332
  class StaticNode extends Node {
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TransactionLog = void 0;
4
4
  const type_1 = require("./type");
5
5
  const utils_1 = require("./utils/utils");
6
+ const nodeUtils_1 = require("./utils/nodeUtils");
6
7
  class TransactionLog {
7
8
  constructor(nodeId) {
8
9
  this.nodeId = nodeId;
@@ -43,10 +44,7 @@ class TransactionLog {
43
44
  this.state = node.state;
44
45
  this.retryCount = node.retryCount > 0 ? node.retryCount : undefined;
45
46
  this.startTime = transactionId;
46
- this.inputs = Object.values(node.dataSources)
47
- .flat()
48
- .filter((source) => source.nodeId)
49
- .map((source) => source.nodeId);
47
+ this.inputs = (0, nodeUtils_1.flatDataSourceNodeIds)(Object.values(node.dataSources));
50
48
  this.inputsData = inputs.length > 0 ? inputs : undefined;
51
49
  graph.setLoopLog(this);
52
50
  graph.appendLog(this);
package/lib/type.d.ts CHANGED
@@ -22,6 +22,9 @@ export type DataSource = {
22
22
  value?: any;
23
23
  propIds?: string[];
24
24
  };
25
+ export type DataSources = DataSource | DataSource[] | DataSources[];
26
+ export type NestedDataSource = Record<string, DataSources>;
27
+ export type ResultDataSet = ResultData | ResultData[] | ResultDataSet[];
25
28
  export type StaticNodeData = {
26
29
  value: ResultData;
27
30
  update?: string;
@@ -74,6 +77,7 @@ export type AgentFunctionContext<ParamsType = DefaultParamsType, InputDataType =
74
77
  retry: number;
75
78
  agentId?: string;
76
79
  version?: number;
80
+ isResult?: boolean;
77
81
  };
78
82
  graphData?: GraphData;
79
83
  agents?: AgentFunctionInfoDictionary;
@@ -1,3 +1,5 @@
1
- import { DataSource } from "../type";
1
+ import { DataSource, DataSources, NestedDataSource } from "../type";
2
2
  export declare const inputs2dataSources: (inputs: string[], graphVersion: number) => Record<string, DataSource>;
3
- export declare const namedInputs2dataSources: (inputs: Record<string, any>, graphVersion: number) => Record<string, DataSource | DataSource[]>;
3
+ export declare const namedInputs2dataSources: (inputs: Record<string, any>, graphVersion: number) => NestedDataSource;
4
+ export declare const flatDataSourceNodeIds: (sources: DataSource[] | DataSources[]) => string[];
5
+ export declare const flatDataSource: (sources: DataSource[] | DataSources[]) => DataSource[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.namedInputs2dataSources = exports.inputs2dataSources = void 0;
3
+ exports.flatDataSource = exports.flatDataSourceNodeIds = exports.namedInputs2dataSources = exports.inputs2dataSources = void 0;
4
4
  const utils_1 = require("./utils");
5
5
  const inputs2dataSources = (inputs, graphVersion) => {
6
6
  return inputs.reduce((tmp, input) => {
@@ -9,16 +9,41 @@ const inputs2dataSources = (inputs, graphVersion) => {
9
9
  }, {});
10
10
  };
11
11
  exports.inputs2dataSources = inputs2dataSources;
12
+ const nestedParseNodeName = (input, graphVersion) => {
13
+ if (Array.isArray(input)) {
14
+ return input.map((inp) => nestedParseNodeName(inp, graphVersion));
15
+ }
16
+ return (0, utils_1.parseNodeName)(input, graphVersion);
17
+ };
12
18
  const namedInputs2dataSources = (inputs, graphVersion) => {
13
19
  return Object.keys(inputs).reduce((tmp, key) => {
14
20
  const input = inputs[key];
15
- if (Array.isArray(input)) {
16
- tmp[key] = input.map((inp) => (0, utils_1.parseNodeName)(inp, graphVersion));
17
- }
18
- else {
19
- tmp[key] = (0, utils_1.parseNodeName)(input, graphVersion);
20
- }
21
+ tmp[key] = nestedParseNodeName(input, graphVersion);
21
22
  return tmp;
22
23
  }, {});
23
24
  };
24
25
  exports.namedInputs2dataSources = namedInputs2dataSources;
26
+ const flatDataSourceNodeIds = (sources) => {
27
+ return (0, exports.flatDataSource)(sources)
28
+ .filter((source) => source.nodeId)
29
+ .map((source) => source.nodeId);
30
+ };
31
+ exports.flatDataSourceNodeIds = flatDataSourceNodeIds;
32
+ const flatDataSource = (sources) => {
33
+ return sources
34
+ .map((source) => {
35
+ if (Array.isArray(source)) {
36
+ return source
37
+ .map((s) => {
38
+ if (Array.isArray(s)) {
39
+ return (0, exports.flatDataSource)(s);
40
+ }
41
+ return s;
42
+ })
43
+ .flat();
44
+ }
45
+ return source;
46
+ })
47
+ .flat();
48
+ };
49
+ exports.flatDataSource = flatDataSource;
@@ -18,7 +18,7 @@ export declare const defaultAgentInfo: {
18
18
  repository: string;
19
19
  license: string;
20
20
  };
21
- export declare const agentInfoWrapper: (agent: AgentFunction<any, any, any>) => {
21
+ export declare const agentInfoWrapper: (agent: AgentFunction<any, any, any, any>) => {
22
22
  name: string;
23
23
  samples: {
24
24
  inputs: never[];
@@ -30,8 +30,8 @@ export declare const agentInfoWrapper: (agent: AgentFunction<any, any, any>) =>
30
30
  author: string;
31
31
  repository: string;
32
32
  license: string;
33
- agent: AgentFunction<any, any, any>;
34
- mock: AgentFunction<any, any, any>;
33
+ agent: AgentFunction<any, any, any, any>;
34
+ mock: AgentFunction<any, any, any, any>;
35
35
  };
36
36
  export declare const debugResultKey: (agentId: string, result: any) => string[];
37
37
  export declare const isLogicallyTrue: (value: any) => boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphai",
3
- "version": "0.5.4",
3
+ "version": "0.5.6",
4
4
  "description": "Asynchronous data flow execution engine for agentic AI apps.",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -26,7 +26,7 @@
26
26
  },
27
27
  "homepage": "https://github.com/receptron/graphai#readme",
28
28
  "devDependencies": {
29
- "typedoc": "^0.26.3"
29
+ "typedoc": "^0.26.5"
30
30
  },
31
31
  "types": "./lib/index.d.ts",
32
32
  "directories": {