@ai.ntellect/core 0.7.10 → 0.7.12
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/dist/graph/event-manager.d.ts +2 -2
- package/dist/graph/event-manager.d.ts.map +1 -1
- package/dist/graph/event-manager.js.map +1 -1
- package/dist/graph/index.d.ts +15 -9
- package/dist/graph/index.d.ts.map +1 -1
- package/dist/graph/index.js +11 -3
- package/dist/graph/index.js.map +1 -1
- package/dist/graph/node.d.ts +2 -2
- package/dist/graph/node.d.ts.map +1 -1
- package/dist/graph/node.js +2 -1
- package/dist/graph/node.js.map +1 -1
- package/dist/graph/visualizer.d.ts +34 -0
- package/dist/graph/visualizer.d.ts.map +1 -0
- package/dist/graph/visualizer.js +227 -0
- package/dist/graph/visualizer.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/types/index.d.ts +11 -8
- package/dist/types/index.d.ts.map +1 -1
- package/graph/event-manager.ts +2 -2
- package/graph/index.ts +25 -11
- package/graph/node.ts +10 -8
- package/graph/visualizer.ts +243 -0
- package/index.ts +1 -0
- package/package.json +1 -1
- package/test/graph/controller.test.ts +2 -2
- package/test/graph/index.test.ts +27 -27
- package/test/graph/observer.test.ts +9 -9
- package/types/index.ts +15 -8
package/dist/types/index.d.ts
CHANGED
@@ -68,7 +68,7 @@ export type GraphContext<T extends ZodSchema> = {
|
|
68
68
|
* @template I - Input schema type
|
69
69
|
* @template O - Output schema type
|
70
70
|
*/
|
71
|
-
export
|
71
|
+
export type GraphNodeConfig<T extends ZodSchema, P = any> = {
|
72
72
|
/** Name of the node */
|
73
73
|
name: string;
|
74
74
|
/** Description of the node */
|
@@ -79,10 +79,13 @@ export interface Node<T extends ZodSchema, P = any> {
|
|
79
79
|
execute: (context: GraphContext<T>, params?: P, tools?: {
|
80
80
|
eventEmitter: IEventEmitter;
|
81
81
|
}) => Promise<void>;
|
82
|
-
/** Optional condition for node execution */
|
82
|
+
/** Optional condition for node start execution */
|
83
83
|
condition?: (context: GraphContext<T>, params?: P) => boolean;
|
84
|
-
/** Array of next node names */
|
85
|
-
next?: string
|
84
|
+
/** Array of next node names or objects with conditions for the next node */
|
85
|
+
next?: Array<string | {
|
86
|
+
node: string;
|
87
|
+
condition: (context: GraphContext<T>) => boolean;
|
88
|
+
}> | string | ((context: GraphContext<T>) => string[]);
|
86
89
|
/** Array of event names that trigger this node */
|
87
90
|
events?: string[];
|
88
91
|
/** Wait for a single event before continuing */
|
@@ -108,17 +111,17 @@ export interface Node<T extends ZodSchema, P = any> {
|
|
108
111
|
};
|
109
112
|
/** Error handler function */
|
110
113
|
onError?: (error: Error) => void;
|
111
|
-
}
|
114
|
+
};
|
112
115
|
/**
|
113
116
|
* Interface for graph definition
|
114
117
|
* @interface
|
115
118
|
* @template T - Schema type
|
116
119
|
*/
|
117
|
-
export
|
120
|
+
export type GraphConfig<T extends ZodSchema> = {
|
118
121
|
/** Name of the graph */
|
119
122
|
name: string;
|
120
123
|
/** Array of nodes in the graph */
|
121
|
-
nodes:
|
124
|
+
nodes: GraphNodeConfig<T, any>[];
|
122
125
|
/** Initial context */
|
123
126
|
context: SchemaType<T>;
|
124
127
|
/** Schema for validation */
|
@@ -131,7 +134,7 @@ export interface GraphDefinition<T extends ZodSchema> {
|
|
131
134
|
eventEmitter?: IEventEmitter | EventEmitter;
|
132
135
|
/** Array of events */
|
133
136
|
events?: string[];
|
134
|
-
}
|
137
|
+
};
|
135
138
|
/**
|
136
139
|
* Configuration type for Meilisearch
|
137
140
|
* @typedef {Object} MeilisearchConfig
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC;AAChC,OAAO,EAAE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B,CAAC;AAEF,MAAM,WAAW,UAAU,CAAC,CAAC,SAAS,SAAS;IAC7C,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,KAAK,GAAG,KAAK,GAAG,MAAM,CAAC;IACjC,SAAS,CAAC,EAAE,CAAC,CAAC,SAAS,SAAS,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9E"}
|
package/graph/event-manager.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import { Observable, Subject, filter } from "rxjs";
|
2
2
|
import { ZodSchema } from "zod";
|
3
3
|
import { IEventEmitter } from "../interfaces";
|
4
|
-
import { GraphContext, GraphEvent,
|
4
|
+
import { GraphContext, GraphEvent, GraphNodeConfig } from "../types";
|
5
5
|
import { GraphNode } from "./node";
|
6
6
|
|
7
7
|
/**
|
@@ -31,7 +31,7 @@ export class GraphEventManager<T extends ZodSchema> {
|
|
31
31
|
*/
|
32
32
|
constructor(
|
33
33
|
private eventEmitter: IEventEmitter,
|
34
|
-
private nodes: Map<string,
|
34
|
+
private nodes: Map<string, GraphNodeConfig<T, any>>,
|
35
35
|
name: string,
|
36
36
|
context: GraphContext<T>,
|
37
37
|
graphEvents?: string[],
|
package/graph/index.ts
CHANGED
@@ -2,11 +2,17 @@ import { EventEmitter } from "events";
|
|
2
2
|
import { BehaviorSubject, Subject } from "rxjs";
|
3
3
|
import { ZodSchema } from "zod";
|
4
4
|
import { GraphObservable, IEventEmitter } from "../interfaces";
|
5
|
-
import {
|
5
|
+
import {
|
6
|
+
GraphConfig,
|
7
|
+
GraphContext,
|
8
|
+
GraphEvent,
|
9
|
+
GraphNodeConfig,
|
10
|
+
} from "../types";
|
6
11
|
import { GraphEventManager } from "./event-manager";
|
7
12
|
import { GraphLogger } from "./logger";
|
8
13
|
import { GraphNode, NodeParams } from "./node";
|
9
14
|
import { GraphObserver } from "./observer";
|
15
|
+
import { GraphVisualizer } from "./visualizer";
|
10
16
|
|
11
17
|
/**
|
12
18
|
* @module GraphFlow
|
@@ -29,7 +35,7 @@ export class GraphFlow<T extends ZodSchema> {
|
|
29
35
|
private graphEvents?: string[];
|
30
36
|
private entryNode?: string;
|
31
37
|
private verbose: boolean = false;
|
32
|
-
public nodes: Map<string,
|
38
|
+
public nodes: Map<string, GraphNodeConfig<T, any>>;
|
33
39
|
|
34
40
|
private eventSubject: Subject<GraphEvent<T>> = new Subject();
|
35
41
|
private stateSubject: BehaviorSubject<GraphContext<T>>;
|
@@ -43,16 +49,16 @@ export class GraphFlow<T extends ZodSchema> {
|
|
43
49
|
/**
|
44
50
|
* Creates a new instance of GraphFlow
|
45
51
|
* @param {string} name - The name of the graph flow
|
46
|
-
* @param {
|
52
|
+
* @param {GraphConfig<T>} config - Configuration object containing nodes, schema, context, and error handlers
|
47
53
|
* @param {Object} options - Optional options for the graph flow
|
48
54
|
*/
|
49
55
|
constructor(
|
50
56
|
public name: string,
|
51
|
-
config:
|
57
|
+
config: GraphConfig<T>,
|
52
58
|
options: { verbose?: boolean } = {}
|
53
59
|
) {
|
54
60
|
this.nodes = new Map(
|
55
|
-
config.nodes.map((node:
|
61
|
+
config.nodes.map((node: GraphNodeConfig<T, any>) => [node.name, node])
|
56
62
|
);
|
57
63
|
this.validator = config.schema;
|
58
64
|
this.context = config.schema.parse(config.context) as GraphContext<T>;
|
@@ -75,7 +81,7 @@ export class GraphFlow<T extends ZodSchema> {
|
|
75
81
|
config.onError
|
76
82
|
);
|
77
83
|
this.nodeExecutor = new GraphNode(
|
78
|
-
this.nodes as Map<string,
|
84
|
+
this.nodes as Map<string, GraphNodeConfig<T, any>>,
|
79
85
|
this.logger,
|
80
86
|
this.eventManager,
|
81
87
|
this.eventSubject,
|
@@ -288,9 +294,9 @@ export class GraphFlow<T extends ZodSchema> {
|
|
288
294
|
|
289
295
|
/**
|
290
296
|
* Updates the graph definition with new configuration
|
291
|
-
* @param {
|
297
|
+
* @param {GraphConfig<T>} definition - New graph definition
|
292
298
|
*/
|
293
|
-
public load(definition:
|
299
|
+
public load(definition: GraphConfig<T>): void {
|
294
300
|
// Clear all existing nodes
|
295
301
|
this.nodes.clear();
|
296
302
|
// Wipe out old node-based event listeners
|
@@ -358,10 +364,10 @@ export class GraphFlow<T extends ZodSchema> {
|
|
358
364
|
|
359
365
|
/**
|
360
366
|
* Adds a new node to the graph
|
361
|
-
* @param {
|
367
|
+
* @param {GraphNodeConfig<T, any>} node - Node to add
|
362
368
|
* @throws {Error} If node with same name already exists
|
363
369
|
*/
|
364
|
-
public addNode(node:
|
370
|
+
public addNode(node: GraphNodeConfig<T, any>): void {
|
365
371
|
this.nodes.set(node.name, node);
|
366
372
|
this.eventManager.setupEventListeners();
|
367
373
|
}
|
@@ -379,7 +385,7 @@ export class GraphFlow<T extends ZodSchema> {
|
|
379
385
|
* Returns all nodes in the graph
|
380
386
|
* @returns {Node<T>[]} Array of all nodes
|
381
387
|
*/
|
382
|
-
public getNodes():
|
388
|
+
public getNodes(): GraphNodeConfig<T, any>[] {
|
383
389
|
return Array.from(this.nodes.values());
|
384
390
|
}
|
385
391
|
|
@@ -392,4 +398,12 @@ export class GraphFlow<T extends ZodSchema> {
|
|
392
398
|
this.eventSubject.complete();
|
393
399
|
this.stateSubject.complete();
|
394
400
|
}
|
401
|
+
|
402
|
+
/**
|
403
|
+
* Creates a visualizer instance for the current graph
|
404
|
+
* @returns {GraphVisualizer<T>} A visualizer instance
|
405
|
+
*/
|
406
|
+
public createVisualizer(): GraphVisualizer<T> {
|
407
|
+
return new GraphVisualizer(this.nodes);
|
408
|
+
}
|
395
409
|
}
|
package/graph/node.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import { IEventEmitter } from "interfaces";
|
2
2
|
import { BehaviorSubject, Subject } from "rxjs";
|
3
3
|
import { ZodSchema } from "zod";
|
4
|
-
import { GraphContext, GraphEvent,
|
4
|
+
import { GraphContext, GraphEvent, GraphNodeConfig } from "../types";
|
5
5
|
import { GraphEventManager } from "./event-manager";
|
6
6
|
|
7
7
|
/**
|
@@ -29,7 +29,7 @@ export class GraphNode<T extends ZodSchema> {
|
|
29
29
|
* @param stateSubject - Subject for managing graph state
|
30
30
|
*/
|
31
31
|
constructor(
|
32
|
-
private nodes: Map<string,
|
32
|
+
private nodes: Map<string, GraphNodeConfig<T, any>>,
|
33
33
|
private logger: GraphLogger,
|
34
34
|
private eventManager: GraphEventManager<T>,
|
35
35
|
private eventSubject: Subject<GraphEvent<T>>,
|
@@ -144,7 +144,9 @@ export class GraphNode<T extends ZodSchema> {
|
|
144
144
|
if (!triggeredByEvent && node.next) {
|
145
145
|
const nextNodes =
|
146
146
|
typeof node.next === "function" ? node.next(contextProxy) : node.next;
|
147
|
-
for (const
|
147
|
+
for (const nextNode of nextNodes) {
|
148
|
+
const nextNodeName =
|
149
|
+
typeof nextNode === "string" ? nextNode : nextNode.node;
|
148
150
|
await this.executeNode(nextNodeName, context, undefined, false);
|
149
151
|
}
|
150
152
|
}
|
@@ -167,7 +169,7 @@ export class GraphNode<T extends ZodSchema> {
|
|
167
169
|
* @private
|
168
170
|
*/
|
169
171
|
private async validateParams(
|
170
|
-
node:
|
172
|
+
node: GraphNodeConfig<T, any>,
|
171
173
|
params: any,
|
172
174
|
nodeName: string
|
173
175
|
): Promise<void> {
|
@@ -194,7 +196,7 @@ export class GraphNode<T extends ZodSchema> {
|
|
194
196
|
* @private
|
195
197
|
*/
|
196
198
|
private async handleEvents(
|
197
|
-
node:
|
199
|
+
node: GraphNodeConfig<T, any>,
|
198
200
|
nodeName: string,
|
199
201
|
context: GraphContext<T>
|
200
202
|
): Promise<void> {
|
@@ -218,7 +220,7 @@ export class GraphNode<T extends ZodSchema> {
|
|
218
220
|
* @private
|
219
221
|
*/
|
220
222
|
private async executeWithRetry(
|
221
|
-
node:
|
223
|
+
node: GraphNodeConfig<T, any>,
|
222
224
|
contextProxy: GraphContext<T>,
|
223
225
|
nodeName: string,
|
224
226
|
params?: NodeParams
|
@@ -266,7 +268,7 @@ export class GraphNode<T extends ZodSchema> {
|
|
266
268
|
* @private
|
267
269
|
*/
|
268
270
|
private async handleCorrelatedEvents(
|
269
|
-
node:
|
271
|
+
node: GraphNodeConfig<T, any>,
|
270
272
|
nodeName: string
|
271
273
|
): Promise<void> {
|
272
274
|
if (node.correlateEvents) {
|
@@ -312,7 +314,7 @@ export class GraphNode<T extends ZodSchema> {
|
|
312
314
|
* @private
|
313
315
|
*/
|
314
316
|
private async handleWaitForEvents(
|
315
|
-
node:
|
317
|
+
node: GraphNodeConfig<T, any>,
|
316
318
|
nodeName: string
|
317
319
|
): Promise<void> {
|
318
320
|
if (node.waitForEvents) {
|
@@ -0,0 +1,243 @@
|
|
1
|
+
import { z, ZodSchema } from "zod";
|
2
|
+
import { GraphNodeConfig } from "../types";
|
3
|
+
import { GraphFlow } from "./index";
|
4
|
+
|
5
|
+
interface NodeVisualData {
|
6
|
+
id: string;
|
7
|
+
label: string;
|
8
|
+
type?: string;
|
9
|
+
events?: string[];
|
10
|
+
contextChanges?: string[];
|
11
|
+
}
|
12
|
+
|
13
|
+
interface EdgeVisualData {
|
14
|
+
from: string;
|
15
|
+
to: string;
|
16
|
+
condition?: string;
|
17
|
+
}
|
18
|
+
|
19
|
+
interface NextNode {
|
20
|
+
node: string;
|
21
|
+
condition: (context: any) => boolean;
|
22
|
+
}
|
23
|
+
|
24
|
+
export class GraphVisualizer<T extends ZodSchema> {
|
25
|
+
private nodes: NodeVisualData[] = [];
|
26
|
+
private edges: EdgeVisualData[] = [];
|
27
|
+
|
28
|
+
constructor(private graphNodes: Map<string, GraphNodeConfig<T, any>>) {
|
29
|
+
this.processNodes();
|
30
|
+
}
|
31
|
+
|
32
|
+
private processNodes(): void {
|
33
|
+
this.graphNodes.forEach((node, nodeName) => {
|
34
|
+
// Extraire les modifications de contexte depuis la fonction execute
|
35
|
+
const contextChanges: string[] = [];
|
36
|
+
const executeStr = node.execute.toString();
|
37
|
+
const contextAssignments = executeStr.match(/context\.\w+\s*=/g) || [];
|
38
|
+
contextAssignments.forEach((assignment) => {
|
39
|
+
const variable = assignment
|
40
|
+
.replace("context.", "")
|
41
|
+
.replace("=", "")
|
42
|
+
.trim();
|
43
|
+
contextChanges.push(variable);
|
44
|
+
});
|
45
|
+
|
46
|
+
this.nodes.push({
|
47
|
+
id: nodeName,
|
48
|
+
label: nodeName,
|
49
|
+
events: node.events,
|
50
|
+
contextChanges,
|
51
|
+
});
|
52
|
+
|
53
|
+
if (node.next) {
|
54
|
+
if (Array.isArray(node.next)) {
|
55
|
+
node.next.forEach((nextNode: string | NextNode) => {
|
56
|
+
if (typeof nextNode === "string") {
|
57
|
+
this.edges.push({ from: nodeName, to: nextNode });
|
58
|
+
} else {
|
59
|
+
const conditionStr = nextNode.condition
|
60
|
+
.toString()
|
61
|
+
.replace(/.*=> /, "")
|
62
|
+
.replace(/ctx\./g, "")
|
63
|
+
.replace(/[{}]/g, "")
|
64
|
+
.replace(/\|\|/g, " or ")
|
65
|
+
.replace(/&&/g, " and ")
|
66
|
+
.replace(/!/g, "not ");
|
67
|
+
|
68
|
+
this.edges.push({
|
69
|
+
from: nodeName,
|
70
|
+
to: nextNode.node,
|
71
|
+
condition: conditionStr,
|
72
|
+
});
|
73
|
+
}
|
74
|
+
});
|
75
|
+
} else if (typeof node.next === "string") {
|
76
|
+
this.edges.push({ from: nodeName, to: node.next });
|
77
|
+
}
|
78
|
+
}
|
79
|
+
});
|
80
|
+
}
|
81
|
+
|
82
|
+
/**
|
83
|
+
* Generates a Mermaid flowchart representation of the graph
|
84
|
+
*/
|
85
|
+
public toMermaid(): string {
|
86
|
+
let mmd = "flowchart TD\n";
|
87
|
+
|
88
|
+
// Add nodes with context changes
|
89
|
+
this.nodes.forEach((node) => {
|
90
|
+
let nodeLabel = node.label;
|
91
|
+
if (node.contextChanges?.length) {
|
92
|
+
nodeLabel += `\nSet: ${node.contextChanges.join(", ")}`;
|
93
|
+
}
|
94
|
+
if (node.events?.length) {
|
95
|
+
// Créer des nœuds d'événements externes
|
96
|
+
node.events.forEach((event) => {
|
97
|
+
mmd += ` ${event}((${event}))\n`;
|
98
|
+
mmd += ` ${event} -.->|event| ${node.id}\n`;
|
99
|
+
});
|
100
|
+
}
|
101
|
+
mmd += ` ${node.id}["${nodeLabel}"]\n`;
|
102
|
+
});
|
103
|
+
|
104
|
+
// Add edges with escaped conditions
|
105
|
+
this.edges.forEach((edge) => {
|
106
|
+
const escapedCondition = edge.condition?.replace(/[|]/g, "\\|");
|
107
|
+
const arrow = escapedCondition ? `-->|${escapedCondition}|` : "-->";
|
108
|
+
mmd += ` ${edge.from} ${arrow} ${edge.to}\n`;
|
109
|
+
});
|
110
|
+
|
111
|
+
return mmd;
|
112
|
+
}
|
113
|
+
|
114
|
+
/**
|
115
|
+
* Returns the nodes and edges data
|
116
|
+
*/
|
117
|
+
public getVisualizationData() {
|
118
|
+
return {
|
119
|
+
nodes: this.nodes,
|
120
|
+
edges: this.edges,
|
121
|
+
};
|
122
|
+
}
|
123
|
+
}
|
124
|
+
|
125
|
+
// Example usage
|
126
|
+
const graph = new GraphFlow("CryptoTradeAssistant", {
|
127
|
+
name: "CryptoTradeAssistant",
|
128
|
+
entryNode: "Initialize",
|
129
|
+
nodes: [
|
130
|
+
{
|
131
|
+
name: "Initialize",
|
132
|
+
execute: async (context) => {
|
133
|
+
context.walletBalance = 1000;
|
134
|
+
context.targetPrice = 2000;
|
135
|
+
context.gasFees = 50;
|
136
|
+
context.isWalletConnected = false;
|
137
|
+
console.log("Initializing crypto assistant");
|
138
|
+
},
|
139
|
+
next: ["ConnectWallet"],
|
140
|
+
},
|
141
|
+
{
|
142
|
+
name: "ConnectWallet",
|
143
|
+
execute: async (context) => {
|
144
|
+
context.isWalletConnected = true;
|
145
|
+
console.log("Connecting wallet");
|
146
|
+
},
|
147
|
+
next: ["CheckMarketConditions"],
|
148
|
+
events: ["walletConnected"],
|
149
|
+
},
|
150
|
+
{
|
151
|
+
name: "CheckMarketConditions",
|
152
|
+
execute: async (context) => {
|
153
|
+
context.currentPrice = 1950;
|
154
|
+
context.marketVolatility = "low";
|
155
|
+
context.hasLiquidity = true;
|
156
|
+
console.log("Checking market conditions");
|
157
|
+
},
|
158
|
+
next: [
|
159
|
+
{
|
160
|
+
node: "PrepareTransaction",
|
161
|
+
condition: (ctx) =>
|
162
|
+
ctx.isWalletConnected &&
|
163
|
+
ctx.currentPrice < ctx.targetPrice &&
|
164
|
+
ctx.hasLiquidity,
|
165
|
+
},
|
166
|
+
{
|
167
|
+
node: "WaitForBetterConditions",
|
168
|
+
condition: (ctx) =>
|
169
|
+
!ctx.hasLiquidity || ctx.currentPrice >= ctx.targetPrice,
|
170
|
+
},
|
171
|
+
],
|
172
|
+
events: ["priceUpdate", "liquidityUpdate"],
|
173
|
+
},
|
174
|
+
{
|
175
|
+
name: "WaitForBetterConditions",
|
176
|
+
execute: async () => console.log("Waiting for better conditions"),
|
177
|
+
next: ["CheckMarketConditions"],
|
178
|
+
events: ["marketUpdate"],
|
179
|
+
},
|
180
|
+
{
|
181
|
+
name: "PrepareTransaction",
|
182
|
+
execute: async (context) => {
|
183
|
+
context.txHash = "0x123...";
|
184
|
+
context.estimatedGas = 45;
|
185
|
+
console.log("Preparing transaction");
|
186
|
+
},
|
187
|
+
next: [
|
188
|
+
{
|
189
|
+
node: "ExecuteTransaction",
|
190
|
+
condition: (ctx) => ctx.estimatedGas < ctx.gasFees,
|
191
|
+
},
|
192
|
+
{
|
193
|
+
node: "WaitForBetterConditions",
|
194
|
+
condition: (ctx) => ctx.estimatedGas >= ctx.gasFees,
|
195
|
+
},
|
196
|
+
],
|
197
|
+
},
|
198
|
+
{
|
199
|
+
name: "ExecuteTransaction",
|
200
|
+
execute: async (context) => {
|
201
|
+
context.txStatus = "pending";
|
202
|
+
console.log("Executing transaction");
|
203
|
+
},
|
204
|
+
next: ["MonitorTransaction"],
|
205
|
+
events: ["txSubmitted"],
|
206
|
+
},
|
207
|
+
{
|
208
|
+
name: "MonitorTransaction",
|
209
|
+
execute: async (context) => {
|
210
|
+
context.txStatus = "confirmed";
|
211
|
+
context.walletBalance = context.walletBalance - context.currentPrice;
|
212
|
+
console.log("Monitoring transaction");
|
213
|
+
},
|
214
|
+
next: ["NotifyUser"],
|
215
|
+
events: ["txConfirmed", "txFailed"],
|
216
|
+
},
|
217
|
+
{
|
218
|
+
name: "NotifyUser",
|
219
|
+
execute: async (context) => {
|
220
|
+
context.notificationSent = true;
|
221
|
+
console.log("Notifying user");
|
222
|
+
},
|
223
|
+
next: ["CheckMarketConditions"],
|
224
|
+
},
|
225
|
+
],
|
226
|
+
context: {},
|
227
|
+
schema: z.object({
|
228
|
+
walletBalance: z.number().optional(),
|
229
|
+
targetPrice: z.number().optional(),
|
230
|
+
currentPrice: z.number().optional(),
|
231
|
+
gasFees: z.number().optional(),
|
232
|
+
estimatedGas: z.number().optional(),
|
233
|
+
isWalletConnected: z.boolean().optional(),
|
234
|
+
marketVolatility: z.string().optional(),
|
235
|
+
hasLiquidity: z.boolean().optional(),
|
236
|
+
txHash: z.string().optional(),
|
237
|
+
txStatus: z.string().optional(),
|
238
|
+
notificationSent: z.boolean().optional(),
|
239
|
+
}),
|
240
|
+
});
|
241
|
+
|
242
|
+
const visualizer = graph.createVisualizer();
|
243
|
+
console.log(visualizer.toMermaid());
|
package/index.ts
CHANGED
@@ -16,6 +16,7 @@ export * from "./graph/event-manager";
|
|
16
16
|
export * from "./graph/index";
|
17
17
|
export * from "./graph/node";
|
18
18
|
export * from "./graph/observer";
|
19
|
+
export * from "./graph/visualizer";
|
19
20
|
|
20
21
|
export * from "./modules/memory";
|
21
22
|
export * from "./modules/memory/adapters/in-memory";
|
package/package.json
CHANGED
@@ -2,7 +2,7 @@ import { expect } from "chai";
|
|
2
2
|
import { z } from "zod";
|
3
3
|
import { GraphController } from "../../graph/controller";
|
4
4
|
import { GraphFlow } from "../../graph/index";
|
5
|
-
import {
|
5
|
+
import { GraphNodeConfig } from "../../types";
|
6
6
|
|
7
7
|
describe("GraphController", () => {
|
8
8
|
const TestSchema = z.object({
|
@@ -11,7 +11,7 @@ describe("GraphController", () => {
|
|
11
11
|
});
|
12
12
|
|
13
13
|
const createTestGraph = (name: string): GraphFlow<typeof TestSchema> => {
|
14
|
-
const nodes:
|
14
|
+
const nodes: GraphNodeConfig<typeof TestSchema>[] = [
|
15
15
|
{
|
16
16
|
name: "start",
|
17
17
|
execute: async (context, params) => {
|