@ai.ntellect/core 0.6.9 → 0.6.11

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.
@@ -44,7 +44,7 @@ class GraphEngine {
44
44
  throw new Error("Cycle détecté dans le workflow");
45
45
  }
46
46
  if (options === null || options === void 0 ? void 0 : options.initialState) {
47
- // this.setState(options.initialState);
47
+ this.setState(options.initialState);
48
48
  }
49
49
  }
50
50
  /**
@@ -257,11 +257,23 @@ class GraphEngine {
257
257
  }
258
258
  break;
259
259
  }
260
- // Gestion des relations (branchements)
261
260
  const relationsNodes = currentNode.relationships || [];
262
- if (relationsNodes.length > 1) {
261
+ // Check condition for all relations
262
+ let nextNodes = [];
263
+ for (const relation of relationsNodes) {
264
+ const nextNode = this.nodes.get(relation.name);
265
+ if ((nextNode === null || nextNode === void 0 ? void 0 : nextNode.condition) && !nextNode.condition(this.currentState)) {
266
+ // Skip this relation
267
+ continue;
268
+ }
269
+ nextNodes.push({
270
+ name: relation.name,
271
+ condition: nextNode === null || nextNode === void 0 ? void 0 : nextNode.condition,
272
+ });
273
+ }
274
+ if (nextNodes.length > 1) {
263
275
  // Exécution parallèle des branches
264
- yield Promise.all(relationsNodes.map((relation) => this.execute(this.currentState, relation.name, onStream, onError)));
276
+ yield Promise.all(nextNodes.map((relation) => this.execute(this.currentState, relation.name, onStream, onError)));
265
277
  // Après exécution en parallèle, on arrête la boucle
266
278
  break;
267
279
  }
package/graph/engine.ts CHANGED
@@ -68,7 +68,7 @@ export class GraphEngine<T> {
68
68
  }
69
69
 
70
70
  if (options?.initialState) {
71
- // this.setState(options.initialState);
71
+ this.setState(options.initialState);
72
72
  }
73
73
  }
74
74
 
@@ -317,12 +317,24 @@ export class GraphEngine<T> {
317
317
  break;
318
318
  }
319
319
 
320
- // Gestion des relations (branchements)
321
320
  const relationsNodes = currentNode.relationships || [];
322
- if (relationsNodes.length > 1) {
321
+ // Check condition for all relations
322
+ let nextNodes = [];
323
+ for (const relation of relationsNodes) {
324
+ const nextNode = this.nodes.get(relation.name);
325
+ if (nextNode?.condition && !nextNode.condition(this.currentState)) {
326
+ // Skip this relation
327
+ continue;
328
+ }
329
+ nextNodes.push({
330
+ name: relation.name,
331
+ condition: nextNode?.condition,
332
+ });
333
+ }
334
+ if (nextNodes.length > 1) {
323
335
  // Exécution parallèle des branches
324
336
  await Promise.all(
325
- relationsNodes.map((relation) =>
337
+ nextNodes.map((relation) =>
326
338
  this.execute(this.currentState, relation.name, onStream, onError)
327
339
  )
328
340
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai.ntellect/core",
3
- "version": "0.6.9",
3
+ "version": "0.6.11",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -1,182 +0,0 @@
1
- import { GraphController } from "@/graph/controller";
2
- import { GraphDefinition } from "@/types";
3
- import { expect } from "chai";
4
- import { z } from "zod";
5
-
6
- describe("Controller", () => {
7
- // Define test schema
8
- const TestSchema = z.object({
9
- status: z.string(),
10
- count: z.number(),
11
- });
12
-
13
- type TestState = z.infer<typeof TestSchema>;
14
-
15
- // Sample workflow definitions
16
- const simpleWorkflow: GraphDefinition<TestState> = {
17
- name: "simple-workflow",
18
- entryNode: "start",
19
- nodes: {
20
- start: {
21
- name: "start",
22
- execute: async (state: any) => ({
23
- ...state,
24
- status: "completed",
25
- count: state.count + 1,
26
- }),
27
- relationships: [],
28
- },
29
- },
30
- schema: TestSchema,
31
- };
32
-
33
- const complexWorkflow: GraphDefinition<TestState> = {
34
- name: "complex-workflow",
35
- entryNode: "first",
36
- nodes: {
37
- first: {
38
- name: "first",
39
- execute: async (state: any) => ({
40
- ...state,
41
- status: "step1",
42
- count: state.count + 2,
43
- }),
44
- relationships: [],
45
- },
46
- },
47
- schema: TestSchema,
48
- };
49
-
50
- let controller: GraphController<TestState>;
51
-
52
- beforeEach(() => {
53
- controller = new GraphController<TestState>();
54
- });
55
-
56
- describe("Basic Execution", () => {
57
- it("should execute a single workflow successfully", async () => {
58
- const actions = [
59
- {
60
- name: "simple-workflow",
61
- parameters: [
62
- { name: "status", value: "initial" },
63
- { name: "count", value: 0 },
64
- ],
65
- },
66
- ];
67
-
68
- const result = await controller.run(actions, [simpleWorkflow]);
69
-
70
- expect(result.context).to.deep.equal({
71
- status: "completed",
72
- count: 1,
73
- });
74
- });
75
-
76
- it("should handle multiple workflows", async () => {
77
- const actions = [
78
- {
79
- name: "complex-workflow",
80
- parameters: [
81
- { name: "status", value: "initial" },
82
- { name: "count", value: 0 },
83
- ],
84
- },
85
- ];
86
-
87
- const result = await controller.run(actions, [
88
- simpleWorkflow,
89
- complexWorkflow,
90
- ]);
91
-
92
- expect(result.context).to.deep.equal({
93
- status: "step1",
94
- count: 2,
95
- });
96
- });
97
- });
98
-
99
- describe("Error Handling", () => {
100
- it("should throw error when no actions provided", async () => {
101
- try {
102
- await controller.run([], [simpleWorkflow]);
103
- expect.fail("Should have thrown an error");
104
- } catch (error) {
105
- expect((error as Error).message).to.equal("No actions provided");
106
- }
107
- });
108
-
109
- it("should throw error when workflow not found", async () => {
110
- const actions = [
111
- {
112
- name: "non-existent-workflow",
113
- parameters: [
114
- { name: "status", value: "initial" },
115
- { name: "count", value: 0 },
116
- ],
117
- },
118
- ];
119
-
120
- try {
121
- await controller.run(actions, [simpleWorkflow]);
122
- expect.fail("Should have thrown an error");
123
- } catch (error) {
124
- expect((error as Error).message).to.equal(
125
- "Graph not found: non-existent-workflow"
126
- );
127
- }
128
- });
129
- });
130
-
131
- describe("Parameter Handling", () => {
132
- it("should correctly process workflow parameters", async () => {
133
- const actions = [
134
- {
135
- name: "simple-workflow",
136
- parameters: [
137
- { name: "status", value: "custom-initial" },
138
- { name: "count", value: 10 },
139
- ],
140
- },
141
- ];
142
-
143
- const result = await controller.run(actions, [simpleWorkflow]);
144
-
145
- expect(result.context).to.deep.equal({
146
- status: "completed",
147
- count: 11,
148
- });
149
- });
150
- });
151
-
152
- describe("Multiple Actions", () => {
153
- it("should process the first action only", async () => {
154
- const actions = [
155
- {
156
- name: "simple-workflow",
157
- parameters: [
158
- { name: "status", value: "initial" },
159
- { name: "count", value: 0 },
160
- ],
161
- },
162
- {
163
- name: "complex-workflow",
164
- parameters: [
165
- { name: "status", value: "initial" },
166
- { name: "count", value: 5 },
167
- ],
168
- },
169
- ];
170
-
171
- const result = await controller.run(actions, [
172
- simpleWorkflow,
173
- complexWorkflow,
174
- ]);
175
-
176
- expect(result.context).to.deep.equal({
177
- status: "completed",
178
- count: 1,
179
- });
180
- });
181
- });
182
- });