@ai.ntellect/core 0.8.1 → 0.8.3
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/README.md +190 -118
- package/dist/graph/controller.d.ts +4 -5
- package/dist/graph/controller.d.ts.map +1 -1
- package/dist/graph/controller.js +10 -10
- package/dist/graph/controller.js.map +1 -1
- package/dist/graph/event-manager.d.ts.map +1 -1
- package/dist/graph/event-manager.js +4 -4
- package/dist/graph/event-manager.js.map +1 -1
- package/dist/graph/index.d.ts +5 -16
- package/dist/graph/index.d.ts.map +1 -1
- package/dist/graph/index.js +32 -52
- package/dist/graph/index.js.map +1 -1
- package/dist/graph/node.d.ts +1 -10
- package/dist/graph/node.d.ts.map +1 -1
- package/dist/graph/node.js +7 -36
- package/dist/graph/node.js.map +1 -1
- package/dist/graph/observer.d.ts +4 -0
- package/dist/graph/observer.d.ts.map +1 -1
- package/dist/graph/observer.js +27 -1
- package/dist/graph/observer.js.map +1 -1
- package/dist/modules/agent/agent.d.ts.map +1 -1
- package/dist/modules/agent/agent.js +3 -6
- package/dist/modules/agent/agent.js.map +1 -1
- package/dist/modules/agent/generic-assistant.d.ts.map +1 -1
- package/dist/modules/agent/generic-assistant.js +3 -3
- package/dist/modules/agent/generic-assistant.js.map +1 -1
- package/dist/modules/agent/generic-executor.d.ts +1 -0
- package/dist/modules/agent/generic-executor.d.ts.map +1 -1
- package/dist/modules/agent/generic-executor.js +24 -35
- package/dist/modules/agent/generic-executor.js.map +1 -1
- package/dist/modules/agent/llm-factory.d.ts.map +1 -1
- package/dist/types/index.d.ts +90 -25
- package/dist/types/index.d.ts.map +1 -1
- package/graph/controller.ts +10 -11
- package/graph/event-manager.ts +2 -14
- package/graph/index.ts +37 -67
- package/graph/node.ts +5 -39
- package/graph/observer.ts +35 -5
- package/modules/agent/agent.ts +7 -8
- package/modules/agent/generic-assistant.ts +7 -5
- package/modules/agent/generic-executor.ts +33 -40
- package/package.json +1 -1
- package/test/graph/controller.test.ts +62 -17
- package/test/graph/index.test.ts +128 -133
- package/test/graph/node.test.ts +38 -233
- package/test/graph/observer.test.ts +18 -23
- package/types/index.ts +91 -28
package/test/graph/node.test.ts
CHANGED
@@ -5,7 +5,8 @@ import { BehaviorSubject, Subject } from "rxjs";
|
|
5
5
|
import { z } from "zod";
|
6
6
|
import { GraphEventManager } from "../../graph/event-manager";
|
7
7
|
import { GraphLogger } from "../../graph/logger";
|
8
|
-
import { GraphNode
|
8
|
+
import { GraphNode } from "../../graph/node";
|
9
|
+
import { IEventEmitter } from "../../interfaces";
|
9
10
|
import { GraphContext } from "../../types";
|
10
11
|
|
11
12
|
use(chaiAsPromised);
|
@@ -66,7 +67,7 @@ describe("GraphNode", () => {
|
|
66
67
|
stateSubject
|
67
68
|
);
|
68
69
|
|
69
|
-
await node.executeNode("test", { counter: 0, message: "Hello" },
|
70
|
+
await node.executeNode("test", { counter: 0, message: "Hello" }, undefined);
|
70
71
|
|
71
72
|
// Vérifier les événements émis
|
72
73
|
expect(events).to.have.lengthOf(3); // nodeStarted, nodeStateChanged, nodeCompleted
|
@@ -94,12 +95,12 @@ describe("GraphNode", () => {
|
|
94
95
|
);
|
95
96
|
|
96
97
|
// Test avec condition vraie
|
97
|
-
await node.executeNode("test", { counter: 0, message: "Hello" },
|
98
|
+
await node.executeNode("test", { counter: 0, message: "Hello" }, undefined);
|
98
99
|
expect(events.some((e) => e.type === "nodeStateChanged")).to.be.true;
|
99
100
|
|
100
101
|
// Test avec condition fausse
|
101
102
|
events = [];
|
102
|
-
await node.executeNode("test", { counter: 5, message: "Hello" },
|
103
|
+
await node.executeNode("test", { counter: 5, message: "Hello" }, undefined);
|
103
104
|
expect(events.some((e) => e.type === "nodeStateChanged")).to.be.false;
|
104
105
|
});
|
105
106
|
|
@@ -121,12 +122,7 @@ describe("GraphNode", () => {
|
|
121
122
|
);
|
122
123
|
|
123
124
|
try {
|
124
|
-
await node.executeNode(
|
125
|
-
"test",
|
126
|
-
{ counter: 0, message: "Hello" },
|
127
|
-
null,
|
128
|
-
false
|
129
|
-
);
|
125
|
+
await node.executeNode("test", { counter: 0, message: "Hello" }, false);
|
130
126
|
expect.fail("Test error");
|
131
127
|
} catch (error: any) {
|
132
128
|
expect(error.message).to.equal("Test error");
|
@@ -152,7 +148,7 @@ describe("GraphNode", () => {
|
|
152
148
|
eventSubject,
|
153
149
|
stateSubject
|
154
150
|
);
|
155
|
-
await node.executeNode("test", { counter: 0, message: "Hello" },
|
151
|
+
await node.executeNode("test", { counter: 0, message: "Hello" }, undefined);
|
156
152
|
|
157
153
|
// Compter les occurrences de chaque type d'événement
|
158
154
|
const eventCounts = events.reduce((acc, event) => {
|
@@ -192,7 +188,7 @@ describe("GraphNode", () => {
|
|
192
188
|
eventSubject,
|
193
189
|
stateSubject
|
194
190
|
);
|
195
|
-
await node.executeNode("test", { counter: 0, message: "Hello" },
|
191
|
+
await node.executeNode("test", { counter: 0, message: "Hello" }, undefined);
|
196
192
|
|
197
193
|
const stateChanges = events.filter((e) => e.type === "nodeStateChanged");
|
198
194
|
expect(stateChanges).to.have.lengthOf(1); // Seulement pour message
|
@@ -203,9 +199,9 @@ describe("GraphNode", () => {
|
|
203
199
|
const nodes = new Map();
|
204
200
|
nodes.set("test", {
|
205
201
|
name: "test",
|
206
|
-
execute: async (context: TestContext
|
207
|
-
context.counter =
|
208
|
-
context.message =
|
202
|
+
execute: async (context: TestContext) => {
|
203
|
+
context.counter = 42;
|
204
|
+
context.message = "Custom";
|
209
205
|
},
|
210
206
|
});
|
211
207
|
|
@@ -217,56 +213,21 @@ describe("GraphNode", () => {
|
|
217
213
|
stateSubject
|
218
214
|
);
|
219
215
|
|
220
|
-
await node.executeNode(
|
221
|
-
"test",
|
222
|
-
{ counter: 0, message: "Hello" },
|
223
|
-
{ value: 5, message: "Custom" },
|
224
|
-
false
|
225
|
-
);
|
216
|
+
await node.executeNode("test", { counter: 0, message: "Hello" }, false);
|
226
217
|
|
227
218
|
const stateChanges = events.filter((e) => e.type === "nodeStateChanged");
|
228
219
|
expect(stateChanges).to.have.lengthOf(2);
|
229
|
-
expect(stateChanges[0].payload.newValue).to.equal(
|
220
|
+
expect(stateChanges[0].payload.newValue).to.equal(42);
|
230
221
|
expect(stateChanges[1].payload.newValue).to.equal("Custom");
|
231
222
|
});
|
232
223
|
|
233
|
-
it("should use default values when no parameters provided", async () => {
|
234
|
-
const nodes = new Map();
|
235
|
-
nodes.set("test", {
|
236
|
-
name: "test",
|
237
|
-
execute: async (
|
238
|
-
context: TestContext,
|
239
|
-
_inputs: any,
|
240
|
-
params?: NodeParams
|
241
|
-
) => {
|
242
|
-
context.counter = params?.increment || 1;
|
243
|
-
context.message = params?.message || "Default";
|
244
|
-
},
|
245
|
-
});
|
246
|
-
|
247
|
-
node = new GraphNode(
|
248
|
-
nodes,
|
249
|
-
logger,
|
250
|
-
eventManager,
|
251
|
-
eventSubject,
|
252
|
-
stateSubject
|
253
|
-
);
|
254
|
-
|
255
|
-
await node.executeNode("test", { counter: 0, message: "Hello" }, null);
|
256
|
-
|
257
|
-
const stateChanges = events.filter((e) => e.type === "nodeStateChanged");
|
258
|
-
expect(stateChanges).to.have.lengthOf(2);
|
259
|
-
expect(stateChanges[0].payload.newValue).to.equal(1); // counter (default)
|
260
|
-
expect(stateChanges[1].payload.newValue).to.equal("Default"); // message (default)
|
261
|
-
});
|
262
|
-
|
263
224
|
it("should properly handle node inputs", async () => {
|
264
225
|
const nodes = new Map();
|
265
226
|
nodes.set("test", {
|
266
227
|
name: "test",
|
267
|
-
execute: async (context: TestContext
|
268
|
-
context.counter =
|
269
|
-
context.message =
|
228
|
+
execute: async (context: TestContext) => {
|
229
|
+
context.counter = 42;
|
230
|
+
context.message = "Test Input";
|
270
231
|
},
|
271
232
|
});
|
272
233
|
|
@@ -278,21 +239,12 @@ describe("GraphNode", () => {
|
|
278
239
|
stateSubject
|
279
240
|
);
|
280
241
|
|
281
|
-
|
282
|
-
value: 42,
|
283
|
-
message: "Test Input",
|
284
|
-
};
|
285
|
-
|
286
|
-
await node.executeNode(
|
287
|
-
"test",
|
288
|
-
{ counter: 0, message: "Hello" },
|
289
|
-
testInputs
|
290
|
-
);
|
242
|
+
await node.executeNode("test", { counter: 0, message: "Hello" }, false);
|
291
243
|
|
292
244
|
const stateChanges = events.filter((e) => e.type === "nodeStateChanged");
|
293
245
|
expect(stateChanges).to.have.lengthOf(2);
|
294
|
-
expect(stateChanges[0].payload.newValue).to.equal(42);
|
295
|
-
expect(stateChanges[1].payload.newValue).to.equal("Test Input");
|
246
|
+
expect(stateChanges[0].payload.newValue).to.equal(42);
|
247
|
+
expect(stateChanges[1].payload.newValue).to.equal("Test Input");
|
296
248
|
});
|
297
249
|
|
298
250
|
it("should not emit duplicate state changes", async () => {
|
@@ -315,7 +267,7 @@ describe("GraphNode", () => {
|
|
315
267
|
stateSubject
|
316
268
|
);
|
317
269
|
|
318
|
-
await node.executeNode("test", { counter: 0, message: "Hello" },
|
270
|
+
await node.executeNode("test", { counter: 0, message: "Hello" }, undefined);
|
319
271
|
|
320
272
|
// Vérifier qu'il n'y a pas de doublons dans les événements
|
321
273
|
const stateChanges = events.filter((e) => e.type === "nodeStateChanged");
|
@@ -330,159 +282,14 @@ describe("GraphNode", () => {
|
|
330
282
|
expect(stateChanges).to.have.lengthOf(2); // Un pour counter, un pour message
|
331
283
|
});
|
332
284
|
|
333
|
-
it("should
|
334
|
-
const paramSchema = z.object({
|
335
|
-
increment: z.number().min(1),
|
336
|
-
message: z.string().min(1),
|
337
|
-
});
|
338
|
-
|
339
|
-
const nodes = new Map();
|
340
|
-
nodes.set("test", {
|
341
|
-
name: "test",
|
342
|
-
params: paramSchema,
|
343
|
-
execute: async (context: TestContext, params?: NodeParams) => {
|
344
|
-
context.counter += params?.increment || 0;
|
345
|
-
context.message = params?.message || "";
|
346
|
-
},
|
347
|
-
});
|
348
|
-
|
349
|
-
node = new GraphNode(
|
350
|
-
nodes,
|
351
|
-
logger,
|
352
|
-
eventManager,
|
353
|
-
eventSubject,
|
354
|
-
stateSubject
|
355
|
-
);
|
356
|
-
|
357
|
-
// Test avec des paramètres valides
|
358
|
-
await node.executeNode(
|
359
|
-
"test",
|
360
|
-
{ counter: 0, message: "Hello" },
|
361
|
-
{ increment: 5, message: "Valid" }
|
362
|
-
);
|
363
|
-
|
364
|
-
// Test avec des paramètres invalides
|
365
|
-
await expect(
|
366
|
-
node.executeNode(
|
367
|
-
"test",
|
368
|
-
{ counter: 0, message: "Hello" },
|
369
|
-
{ increment: 0, message: "" }
|
370
|
-
)
|
371
|
-
).to.be.rejected; // Enlever le .with() car le message d'erreur vient directement de Zod
|
372
|
-
});
|
373
|
-
|
374
|
-
it("should work without params schema", async () => {
|
375
|
-
const nodes = new Map();
|
376
|
-
nodes.set("test", {
|
377
|
-
name: "test",
|
378
|
-
execute: async (context: TestContext) => {
|
379
|
-
context.counter++;
|
380
|
-
},
|
381
|
-
});
|
382
|
-
|
383
|
-
node = new GraphNode(
|
384
|
-
nodes,
|
385
|
-
logger,
|
386
|
-
eventManager,
|
387
|
-
eventSubject,
|
388
|
-
stateSubject
|
389
|
-
);
|
390
|
-
|
391
|
-
// Devrait fonctionner sans erreur même sans schema de params
|
392
|
-
await node.executeNode("test", { counter: 0, message: "Hello" }, null);
|
393
|
-
});
|
394
|
-
|
395
|
-
it("should not require params when node has no params schema", async () => {
|
285
|
+
it("should handle node execution without params", async () => {
|
396
286
|
const nodes = new Map();
|
397
287
|
nodes.set("test", {
|
398
288
|
name: "test",
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
});
|
404
|
-
|
405
|
-
node = new GraphNode(
|
406
|
-
nodes,
|
407
|
-
logger,
|
408
|
-
eventManager,
|
409
|
-
eventSubject,
|
410
|
-
stateSubject
|
411
|
-
);
|
412
|
-
|
413
|
-
await node.executeNode("test", { counter: 0, message: "Hello" }, null);
|
414
|
-
|
415
|
-
const stateChanges = events.filter((e) => e.type === "nodeStateChanged");
|
416
|
-
expect(stateChanges).to.have.lengthOf(1);
|
417
|
-
expect(stateChanges[0].payload.newValue).to.equal(1);
|
418
|
-
});
|
419
|
-
|
420
|
-
it("should require params only when node has params schema", async () => {
|
421
|
-
const nodes = new Map();
|
422
|
-
nodes.set("test", {
|
423
|
-
name: "test",
|
424
|
-
params: z.object({
|
425
|
-
// Avec un schéma de params
|
426
|
-
value: z.number(),
|
427
|
-
}),
|
428
|
-
execute: async (context: TestContext, params?: NodeParams) => {
|
429
|
-
context.counter = params?.value || 0;
|
430
|
-
},
|
431
|
-
});
|
432
|
-
|
433
|
-
node = new GraphNode(
|
434
|
-
nodes,
|
435
|
-
logger,
|
436
|
-
eventManager,
|
437
|
-
eventSubject,
|
438
|
-
stateSubject
|
439
|
-
);
|
440
|
-
|
441
|
-
// Devrait échouer sans params
|
442
|
-
await expect(
|
443
|
-
node.executeNode("test", { counter: 0, message: "Hello" }, null)
|
444
|
-
).to.be.rejectedWith("Params required for node");
|
445
|
-
});
|
446
|
-
|
447
|
-
it("should execute node without params when no schema is defined (real world scenario)", async () => {
|
448
|
-
const nodes = new Map();
|
449
|
-
nodes.set("incrementCounter", {
|
450
|
-
name: "incrementCounter",
|
451
|
-
execute: async (context: TestContext) => {
|
452
|
-
context.counter++;
|
453
|
-
},
|
454
|
-
});
|
455
|
-
|
456
|
-
node = new GraphNode(
|
457
|
-
nodes,
|
458
|
-
logger,
|
459
|
-
eventManager,
|
460
|
-
eventSubject,
|
461
|
-
stateSubject
|
462
|
-
);
|
463
|
-
|
464
|
-
// Simuler l'appel comme dans examples/t2.ts
|
465
|
-
await node.executeNode(
|
466
|
-
"incrementCounter",
|
467
|
-
{ message: "Hello", counter: 0 },
|
468
|
-
{ test: "test" } // Passer des params même si non requis
|
469
|
-
);
|
470
|
-
|
471
|
-
const stateChanges = events.filter((e) => e.type === "nodeStateChanged");
|
472
|
-
expect(stateChanges).to.have.lengthOf(1);
|
473
|
-
expect(stateChanges[0].payload.newValue).to.equal(1);
|
474
|
-
});
|
475
|
-
|
476
|
-
it("should handle optional params schema", async () => {
|
477
|
-
const nodes = new Map();
|
478
|
-
nodes.set("test", {
|
479
|
-
name: "test",
|
480
|
-
params: z
|
481
|
-
.object({
|
482
|
-
test: z.string(),
|
483
|
-
})
|
484
|
-
.optional(),
|
485
|
-
execute: async (context: TestContext, params?: NodeParams) => {
|
289
|
+
execute: async (
|
290
|
+
context: TestContext,
|
291
|
+
tools?: { eventEmitter: IEventEmitter }
|
292
|
+
) => {
|
486
293
|
context.counter++;
|
487
294
|
},
|
488
295
|
});
|
@@ -495,13 +302,8 @@ describe("GraphNode", () => {
|
|
495
302
|
stateSubject
|
496
303
|
);
|
497
304
|
|
498
|
-
|
499
|
-
await node.executeNode(
|
500
|
-
"test",
|
501
|
-
{ counter: 0, message: "Hello" },
|
502
|
-
{ test: "test" }
|
503
|
-
);
|
504
|
-
await node.executeNode("test", { counter: 1, message: "Hello" }, null);
|
305
|
+
await node.executeNode("test", { counter: 0, message: "Hello" }, undefined);
|
306
|
+
await node.executeNode("test", { counter: 1, message: "Hello" }, undefined);
|
505
307
|
|
506
308
|
const stateChanges = events.filter((e) => e.type === "nodeStateChanged");
|
507
309
|
expect(stateChanges).to.have.lengthOf(2);
|
@@ -534,7 +336,7 @@ describe("GraphNode", () => {
|
|
534
336
|
const execution = node.executeNode(
|
535
337
|
"waitForEventsNode",
|
536
338
|
{ counter: 0, message: "Hello" },
|
537
|
-
|
339
|
+
undefined
|
538
340
|
);
|
539
341
|
|
540
342
|
// Simuler les événements après un court délai
|
@@ -572,7 +374,11 @@ describe("GraphNode", () => {
|
|
572
374
|
stateSubject
|
573
375
|
);
|
574
376
|
await expect(
|
575
|
-
node.executeNode(
|
377
|
+
node.executeNode(
|
378
|
+
"timeoutNode",
|
379
|
+
{ counter: 0, message: "Hello" },
|
380
|
+
undefined
|
381
|
+
)
|
576
382
|
).to.be.rejectedWith("Timeout waiting for events");
|
577
383
|
});
|
578
384
|
|
@@ -597,11 +403,10 @@ describe("GraphNode", () => {
|
|
597
403
|
eventSubject,
|
598
404
|
stateSubject
|
599
405
|
);
|
600
|
-
const execution = node.executeNode(
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
);
|
406
|
+
const execution = node.executeNode("partialEventsNode", {
|
407
|
+
counter: 0,
|
408
|
+
message: "Hello",
|
409
|
+
});
|
605
410
|
|
606
411
|
setTimeout(() => {
|
607
412
|
eventEmitter.emit("event1", { data: "test1" });
|
@@ -648,7 +453,7 @@ describe("GraphNode", () => {
|
|
648
453
|
node.executeNode(
|
649
454
|
"correlatedEventsNode",
|
650
455
|
{ counter: 0, message: "Hello" },
|
651
|
-
|
456
|
+
undefined
|
652
457
|
);
|
653
458
|
|
654
459
|
setTimeout(() => {
|
@@ -33,7 +33,7 @@ describe("GraphObserver", () => {
|
|
33
33
|
let observer: GraphObserver<typeof TestSchema>;
|
34
34
|
|
35
35
|
beforeEach(() => {
|
36
|
-
graph = new GraphFlow(
|
36
|
+
graph = new GraphFlow({
|
37
37
|
name: "TestGraph",
|
38
38
|
nodes: [],
|
39
39
|
context: { value: 0 },
|
@@ -50,7 +50,8 @@ describe("GraphObserver", () => {
|
|
50
50
|
graph,
|
51
51
|
eventSubject,
|
52
52
|
stateSubject,
|
53
|
-
destroySubject
|
53
|
+
destroySubject,
|
54
|
+
graph["eventManager"]
|
54
55
|
);
|
55
56
|
});
|
56
57
|
|
@@ -319,33 +320,27 @@ describe("GraphObserver", () => {
|
|
319
320
|
* - Events are captured in correct order
|
320
321
|
*/
|
321
322
|
it("should wait for correlated events", async () => {
|
322
|
-
|
323
|
-
|
323
|
+
const promise = observer.waitForCorrelatedEvents(
|
324
|
+
["eventA", "eventB"],
|
325
|
+
1000,
|
326
|
+
(events) => events.every((e) => e.payload?.status === "success")
|
327
|
+
);
|
328
|
+
|
329
|
+
// Émettre les événements immédiatement
|
330
|
+
eventSubject.next({
|
324
331
|
type: "eventA",
|
325
|
-
payload: {
|
332
|
+
payload: { status: "success" },
|
326
333
|
timestamp: Date.now(),
|
327
|
-
}
|
334
|
+
});
|
328
335
|
|
329
|
-
|
336
|
+
eventSubject.next({
|
330
337
|
type: "eventB",
|
331
|
-
payload: {
|
338
|
+
payload: { status: "success" },
|
332
339
|
timestamp: Date.now(),
|
333
|
-
}
|
334
|
-
|
335
|
-
// Emit events after a short delay
|
336
|
-
setTimeout(() => {
|
337
|
-
eventSubject.next(eventA);
|
338
|
-
eventSubject.next(eventB);
|
339
|
-
}, 100);
|
340
|
-
|
341
|
-
const events = await observer.waitForCorrelatedEvents(
|
342
|
-
["eventA", "eventB"],
|
343
|
-
2000,
|
344
|
-
(events) =>
|
345
|
-
events.every((e) => e.payload.eventPayload?.status === "success")
|
346
|
-
);
|
340
|
+
});
|
347
341
|
|
348
|
-
|
342
|
+
const events = await promise;
|
343
|
+
expect(events).to.have.length(2);
|
349
344
|
expect(events[0].type).to.equal("eventA");
|
350
345
|
expect(events[1].type).to.equal("eventB");
|
351
346
|
});
|
package/types/index.ts
CHANGED
@@ -77,13 +77,45 @@ export type GraphContext<T extends ZodSchema> = {
|
|
77
77
|
};
|
78
78
|
|
79
79
|
/**
|
80
|
-
* Configuration for event handling in
|
80
|
+
* Configuration for event handling strategies in nodes
|
81
|
+
* @typedef {Object} EventStrategy
|
82
|
+
* @property {"single" | "all" | "correlate"} type - The type of event handling strategy
|
83
|
+
* - single: Waits for any single event from the specified events
|
84
|
+
* - all: Waits for all specified events to occur
|
85
|
+
* - correlate: Uses a correlation function to match related events
|
86
|
+
* @property {(events: any[]) => boolean} [correlation] - Optional correlation function for "correlate" strategy
|
81
87
|
*/
|
82
88
|
export type EventStrategy = {
|
83
89
|
type: "single" | "all" | "correlate";
|
84
90
|
correlation?: (events: any[]) => boolean;
|
85
91
|
};
|
86
92
|
|
93
|
+
/**
|
94
|
+
* Configuration for event handling in nodes
|
95
|
+
* @typedef {Object} EventConfig
|
96
|
+
* @property {string[]} events - Array of event names to wait for
|
97
|
+
* @property {number} [timeout] - Optional timeout in milliseconds
|
98
|
+
* @property {EventStrategy} strategy - Strategy for handling multiple events
|
99
|
+
* @property {(events: any[]) => Promise<void>} [onSuccess] - Optional callback when events are successfully received
|
100
|
+
* @property {() => Promise<void>} [onTimeout] - Optional callback when event waiting times out
|
101
|
+
* @example
|
102
|
+
* ```typescript
|
103
|
+
* const eventConfig: EventConfig = {
|
104
|
+
* events: ["payment.received", "order.validated"],
|
105
|
+
* timeout: 5000,
|
106
|
+
* strategy: {
|
107
|
+
* type: "correlate",
|
108
|
+
* correlation: (events) => events.every(e => e.transactionId === events[0].transactionId)
|
109
|
+
* },
|
110
|
+
* onSuccess: async (events) => {
|
111
|
+
* console.log("Correlated events received:", events);
|
112
|
+
* },
|
113
|
+
* onTimeout: async () => {
|
114
|
+
* console.log("Event waiting timed out");
|
115
|
+
* }
|
116
|
+
* };
|
117
|
+
* ```
|
118
|
+
*/
|
87
119
|
export type EventConfig = {
|
88
120
|
events: string[];
|
89
121
|
timeout?: number;
|
@@ -92,12 +124,62 @@ export type EventConfig = {
|
|
92
124
|
onTimeout?: () => Promise<void>;
|
93
125
|
};
|
94
126
|
|
127
|
+
/**
|
128
|
+
* Represents an event in the graph system
|
129
|
+
* @template T - Schema type for context validation
|
130
|
+
* @property {string} type - The type/name of the event
|
131
|
+
* @property {any} [payload] - Optional payload data
|
132
|
+
* @property {number} timestamp - Unix timestamp of when the event occurred
|
133
|
+
* @example
|
134
|
+
* ```typescript
|
135
|
+
* const event: GraphEvent<MySchema> = {
|
136
|
+
* type: "payment.received",
|
137
|
+
* payload: {
|
138
|
+
* transactionId: "tx123",
|
139
|
+
* amount: 100,
|
140
|
+
* currency: "USD"
|
141
|
+
* },
|
142
|
+
* timestamp: Date.now()
|
143
|
+
* };
|
144
|
+
* ```
|
145
|
+
*/
|
146
|
+
export type GraphEvent<T extends ZodSchema> = {
|
147
|
+
type: string;
|
148
|
+
payload?: any;
|
149
|
+
timestamp: number;
|
150
|
+
};
|
151
|
+
|
152
|
+
/**
|
153
|
+
* Configuration for waiting on multiple events
|
154
|
+
* @template T - Schema type for context validation
|
155
|
+
* @property {string[]} events - Array of event names to wait for
|
156
|
+
* @property {number} [timeout] - Optional timeout in milliseconds
|
157
|
+
* @property {"all" | "any" | "race"} strategy - Strategy for handling multiple events
|
158
|
+
* @property {(context: GraphContext<T>) => Promise<void>} [onSuccess] - Optional success callback
|
159
|
+
* @example
|
160
|
+
* ```typescript
|
161
|
+
* const config: WaitForEvents<MySchema> = {
|
162
|
+
* events: ["event1", "event2"],
|
163
|
+
* timeout: 5000,
|
164
|
+
* strategy: "all",
|
165
|
+
* onSuccess: async (context) => {
|
166
|
+
* console.log("All events received");
|
167
|
+
* }
|
168
|
+
* };
|
169
|
+
* ```
|
170
|
+
*/
|
171
|
+
export type WaitForEvents<T extends ZodSchema> = {
|
172
|
+
events: string[];
|
173
|
+
timeout?: number;
|
174
|
+
strategy: "all" | "any" | "race";
|
175
|
+
onSuccess?: <T extends ZodSchema>(context: GraphContext<T>) => Promise<void>;
|
176
|
+
};
|
177
|
+
|
95
178
|
/**
|
96
179
|
* Interface representing a node in the graph
|
97
180
|
* @interface
|
98
181
|
* @template T - Schema type
|
99
|
-
* @template
|
100
|
-
* @template O - Output schema type
|
182
|
+
* @template P - Parameters type
|
101
183
|
*/
|
102
184
|
export interface GraphNodeConfig<T extends ZodSchema, P = any> {
|
103
185
|
/** Name of the node */
|
@@ -109,13 +191,11 @@ export interface GraphNodeConfig<T extends ZodSchema, P = any> {
|
|
109
191
|
/** Execute function for the node */
|
110
192
|
execute: (
|
111
193
|
context: GraphContext<T>,
|
112
|
-
params?: P,
|
113
194
|
tools?: { eventEmitter: IEventEmitter }
|
114
195
|
) => Promise<void>;
|
115
|
-
/** Optional condition for node
|
196
|
+
/** Optional condition for node execution */
|
116
197
|
condition?: (context: GraphContext<T>, params?: P) => boolean;
|
117
|
-
|
118
|
-
/** Array of next node names or objects with conditions for the next node */
|
198
|
+
/** Array of next node names or objects with conditions */
|
119
199
|
next?:
|
120
200
|
| Array<
|
121
201
|
| string
|
@@ -129,13 +209,9 @@ export interface GraphNodeConfig<T extends ZodSchema, P = any> {
|
|
129
209
|
when?: EventConfig;
|
130
210
|
/** Retry configuration */
|
131
211
|
retry?: {
|
132
|
-
/** Maximum number of retry attempts */
|
133
212
|
maxAttempts: number;
|
134
|
-
/** Delay between retries in milliseconds */
|
135
213
|
delay: number;
|
136
|
-
/** Error handler function */
|
137
214
|
onRetryFailed?: (error: Error, context: GraphContext<T>) => Promise<void>;
|
138
|
-
/** Continue execution on failed retry */
|
139
215
|
continueOnFailed?: boolean;
|
140
216
|
};
|
141
217
|
/** Error handler function */
|
@@ -151,12 +227,12 @@ export interface GraphNodeConfig<T extends ZodSchema, P = any> {
|
|
151
227
|
export type GraphConfig<T extends ZodSchema> = {
|
152
228
|
/** Name of the graph */
|
153
229
|
name: string;
|
154
|
-
/** Array of nodes in the graph */
|
155
|
-
nodes: GraphNodeConfig<T, any>[];
|
156
|
-
/** Initial context */
|
157
|
-
context: SchemaType<T>;
|
158
230
|
/** Schema for validation */
|
159
231
|
schema: T;
|
232
|
+
/** Initial context */
|
233
|
+
context: SchemaType<T>;
|
234
|
+
/** Array of nodes in the graph */
|
235
|
+
nodes: GraphNodeConfig<T, any>[];
|
160
236
|
/** Global error handler */
|
161
237
|
onError?: (error: Error, context: GraphContext<T>) => void;
|
162
238
|
/** Entry node name */
|
@@ -205,19 +281,6 @@ export type MeilisearchSettings = {
|
|
205
281
|
sortableAttributes?: string[];
|
206
282
|
};
|
207
283
|
|
208
|
-
export type GraphEvent<T extends ZodSchema> = {
|
209
|
-
type: string;
|
210
|
-
payload?: any;
|
211
|
-
timestamp: number;
|
212
|
-
};
|
213
|
-
|
214
|
-
export type WaitForEvents<T extends ZodSchema> = {
|
215
|
-
events: string[];
|
216
|
-
timeout?: number;
|
217
|
-
strategy: "all" | "any" | "race";
|
218
|
-
onSuccess?: <T extends ZodSchema>(context: GraphContext<T>) => Promise<void>;
|
219
|
-
};
|
220
|
-
|
221
284
|
/**
|
222
285
|
* Configuration interface for NLP Engine
|
223
286
|
* @interface NLPConfig
|