graphai 0.5.5 → 0.5.7

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;
@@ -27,8 +27,9 @@ export declare class ComputedNode extends Node {
27
27
  readonly priority: number;
28
28
  error?: Error;
29
29
  transactionId: undefined | number;
30
+ private readonly passThrough?;
30
31
  readonly anyInput: boolean;
31
- dataSources: Record<string, DataSource | DataSource[]>;
32
+ dataSources: NestedDataSource;
32
33
  private inputs?;
33
34
  inputNames?: Array<string>;
34
35
  pendings: Set<string>;
@@ -55,6 +56,8 @@ export declare class ComputedNode extends Node {
55
56
  private getNamedInput;
56
57
  private getInputs;
57
58
  private getDebugInfo;
59
+ private beforeConsoleLog;
60
+ private afterConsoleLog;
58
61
  }
59
62
  export declare class StaticNode extends Node {
60
63
  value?: ResultData;
package/lib/node.js CHANGED
@@ -42,6 +42,7 @@ class ComputedNode extends Node {
42
42
  this.params = data.params ?? {};
43
43
  this.console = data.console ?? {};
44
44
  this.filterParams = data.filterParams ?? {};
45
+ this.passThrough = data.passThrough;
45
46
  this.retryLimit = data.retry ?? graph.retryLimit ?? 0;
46
47
  this.timeout = data.timeout;
47
48
  this.isResult = data.isResult ?? false;
@@ -57,32 +58,17 @@ class ComputedNode extends Node {
57
58
  this.dataSources = (0, nodeUtils_1.namedInputs2dataSources)(data.inputs, graph.version);
58
59
  }
59
60
  }
60
- this.pendings = new Set(Object.values(this.dataSources)
61
- .flat()
62
- .filter((source) => source.nodeId)
63
- .map((source) => source.nodeId));
61
+ this.pendings = new Set((0, nodeUtils_1.flatDataSourceNodeIds)(Object.values(this.dataSources)));
62
+ (0, utils_2.assert)(["function", "string"].includes(typeof data.agent), "agent must be either string or function");
64
63
  if (typeof data.agent === "string") {
65
64
  this.agentId = data.agent;
66
65
  }
67
66
  else {
68
- (0, utils_2.assert)(typeof data.agent === "function", "agent must be either string or function");
69
67
  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
- }
80
- }
81
- if (typeof data.graph === "string") {
82
- this.nestedGraph = this.addPengindNode(data.graph);
68
+ this.agentFunction = this.inputNames ? async ({ namedInputs }) => agent(namedInputs) : async ({ inputs }) => agent(...inputs);
83
69
  }
84
- else if (data.graph) {
85
- this.nestedGraph = data.graph;
70
+ if (data.graph) {
71
+ this.nestedGraph = typeof data.graph === "string" ? this.addPengindNode(data.graph) : data.graph;
86
72
  }
87
73
  if (data.if) {
88
74
  this.ifSource = this.addPengindNode(data.if);
@@ -114,12 +100,6 @@ class ComputedNode extends Node {
114
100
  if (this.state !== type_1.NodeState.Waiting || this.pendings.size !== 0) {
115
101
  return false;
116
102
  }
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
103
  if ((this.ifSource && !(0, utils_2.isLogicallyTrue)(this.graph.resultOf(this.ifSource))) ||
124
104
  (this.unlessSource && (0, utils_2.isLogicallyTrue)(this.graph.resultOf(this.unlessSource)))) {
125
105
  this.state = type_1.NodeState.Skipped;
@@ -145,10 +125,7 @@ class ComputedNode extends Node {
145
125
  this.graph.onExecutionComplete(this);
146
126
  }
147
127
  }
148
- checkDataAvailability(checkAnyInput = true) {
149
- if (checkAnyInput) {
150
- (0, utils_2.assert)(this.anyInput, "checkDataAvailability should be called only for anyInput case");
151
- }
128
+ checkDataAvailability() {
152
129
  return Object.values(this.graph.resultsOf(this.dataSources))
153
130
  .flat()
154
131
  .some((result) => result !== undefined);
@@ -254,18 +231,13 @@ class ComputedNode extends Node {
254
231
  context.graphData = this.nestedGraph;
255
232
  }
256
233
  else {
257
- const graphData = this.graph.resultOf(this.nestedGraph);
258
- context.graphData = graphData; // HACK: compiler work-around
234
+ context.graphData = this.graph.resultOf(this.nestedGraph); // HACK: compiler work-around
259
235
  }
260
236
  context.agents = this.graph.agentFunctionInfoDictionary;
261
237
  }
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
- }
238
+ this.beforeConsoleLog(context);
265
239
  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
- }
240
+ this.afterConsoleLog(result);
269
241
  if (this.nestedGraph) {
270
242
  this.graph.taskManager.restoreAfterNesting();
271
243
  }
@@ -276,7 +248,17 @@ class ComputedNode extends Node {
276
248
  return;
277
249
  }
278
250
  this.state = type_1.NodeState.Completed;
279
- this.result = result;
251
+ this.result = (() => {
252
+ if (result && this.passThrough) {
253
+ if ((0, utils_2.isObject)(result) && !Array.isArray(result)) {
254
+ return { ...result, ...this.passThrough };
255
+ }
256
+ else if (Array.isArray(result)) {
257
+ return result.map((r) => ((0, utils_2.isObject)(r) && !Array.isArray(r) ? { ...r, ...this.passThrough } : r));
258
+ }
259
+ }
260
+ return result;
261
+ })();
280
262
  this.log.onComplete(this, this.graph, localLog);
281
263
  this.onSetResult();
282
264
  this.graph.onExecutionComplete(this);
@@ -337,8 +319,25 @@ class ComputedNode extends Node {
337
319
  retry: this.retryCount,
338
320
  verbose: this.graph.verbose,
339
321
  version: this.graph.version,
322
+ isResult: this.isResult,
340
323
  };
341
324
  }
325
+ beforeConsoleLog(context) {
326
+ if (this.console.before === true) {
327
+ console.log(JSON.stringify(this.inputNames ? context.namedInputs : context.inputs, null, 2));
328
+ }
329
+ else if (this.console.before) {
330
+ console.log(this.console.before);
331
+ }
332
+ }
333
+ afterConsoleLog(result) {
334
+ if (this.console.after === true) {
335
+ console.log(typeof result === "string" ? result : JSON.stringify(result, null, 2));
336
+ }
337
+ else if (this.console.after) {
338
+ console.log(this.console.after);
339
+ }
340
+ }
342
341
  }
343
342
  exports.ComputedNode = ComputedNode;
344
343
  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
@@ -17,11 +17,15 @@ export type ResultData<ResultType = DefaultResultData> = ResultType | undefined;
17
17
  export type ResultDataDictionary<ResultType = DefaultResultData> = Record<string, ResultData<ResultType>>;
18
18
  export type DefaultParamsType = Record<string, any>;
19
19
  export type NodeDataParams<ParamsType = DefaultParamsType> = ParamsType;
20
+ export type PassThrough = Record<string, any>;
20
21
  export type DataSource = {
21
22
  nodeId?: string;
22
23
  value?: any;
23
24
  propIds?: string[];
24
25
  };
26
+ export type DataSources = DataSource | DataSource[] | DataSources[];
27
+ export type NestedDataSource = Record<string, DataSources>;
28
+ export type ResultDataSet = ResultData | ResultData[] | ResultDataSet[];
25
29
  export type StaticNodeData = {
26
30
  value: ResultData;
27
31
  update?: string;
@@ -42,6 +46,7 @@ export type ComputedNodeData = {
42
46
  graph?: GraphData | string;
43
47
  isResult?: boolean;
44
48
  priority?: number;
49
+ passThrough?: PassThrough;
45
50
  console?: Record<string, string | boolean>;
46
51
  };
47
52
  export type NodeData = StaticNodeData | ComputedNodeData;
@@ -74,6 +79,7 @@ export type AgentFunctionContext<ParamsType = DefaultParamsType, InputDataType =
74
79
  retry: number;
75
80
  agentId?: string;
76
81
  version?: number;
82
+ isResult?: boolean;
77
83
  };
78
84
  graphData?: GraphData;
79
85
  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;
@@ -16,6 +16,7 @@ exports.computedNodeAttributeKeys = [
16
16
  "unless",
17
17
  "filterParams",
18
18
  "console",
19
+ "passThrough",
19
20
  ];
20
21
  exports.staticNodeAttributeKeys = ["value", "update", "isResult"];
21
22
  class ValidationError extends Error {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphai",
3
- "version": "0.5.5",
3
+ "version": "0.5.7",
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": {