@cadenza.io/core 3.17.0 → 3.18.0
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/index.d.mts +99 -39
- package/dist/index.d.ts +99 -39
- package/dist/index.js +306 -72
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +306 -71
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -29,7 +29,6 @@ __export(index_exports, {
|
|
|
29
29
|
GraphRunner: () => GraphRunner,
|
|
30
30
|
SignalBroker: () => SignalBroker,
|
|
31
31
|
SignalEmitter: () => SignalEmitter,
|
|
32
|
-
SignalTask: () => SignalTask,
|
|
33
32
|
Task: () => Task,
|
|
34
33
|
default: () => index_default
|
|
35
34
|
});
|
|
@@ -1527,17 +1526,24 @@ var SignalBroker = class _SignalBroker {
|
|
|
1527
1526
|
squashId,
|
|
1528
1527
|
debounce_default(() => {
|
|
1529
1528
|
options.squash = false;
|
|
1530
|
-
this.emit(
|
|
1529
|
+
this.emit(
|
|
1530
|
+
signal,
|
|
1531
|
+
options.mergeFunction ? options.mergeFunction(
|
|
1532
|
+
this.squashedContexts.get(squashId)[0],
|
|
1533
|
+
...this.squashedContexts.get(squashId).slice(1)
|
|
1534
|
+
) : merge_default(
|
|
1535
|
+
this.squashedContexts.get(squashId)[0],
|
|
1536
|
+
...this.squashedContexts.get(squashId).slice(1)
|
|
1537
|
+
),
|
|
1538
|
+
options
|
|
1539
|
+
);
|
|
1531
1540
|
this.squashedEmitters.delete(squashId);
|
|
1532
1541
|
this.squashedContexts.delete(squashId);
|
|
1533
1542
|
}, delayMs ?? 300)
|
|
1534
1543
|
);
|
|
1535
|
-
this.squashedContexts.set(squashId, context);
|
|
1544
|
+
this.squashedContexts.set(squashId, [context]);
|
|
1536
1545
|
} else {
|
|
1537
|
-
this.squashedContexts.
|
|
1538
|
-
squashId,
|
|
1539
|
-
options.mergeFunction ? options.mergeFunction(this.squashedContexts.get(squashId), context) : merge_default(this.squashedContexts.get(squashId), context)
|
|
1540
|
-
);
|
|
1546
|
+
this.squashedContexts.get(squashId)?.push(context);
|
|
1541
1547
|
}
|
|
1542
1548
|
this.squashedEmitters.get(squashId)();
|
|
1543
1549
|
}
|
|
@@ -2115,7 +2121,7 @@ var SignalEmitter = class {
|
|
|
2115
2121
|
* @param options
|
|
2116
2122
|
*/
|
|
2117
2123
|
emit(signal, data = {}, options = {}) {
|
|
2118
|
-
Cadenza.
|
|
2124
|
+
Cadenza.signalBroker.emit(signal, data, options);
|
|
2119
2125
|
}
|
|
2120
2126
|
/**
|
|
2121
2127
|
* Emits a signal via the broker if not silent.
|
|
@@ -2127,7 +2133,7 @@ var SignalEmitter = class {
|
|
|
2127
2133
|
if (this.silent) {
|
|
2128
2134
|
return;
|
|
2129
2135
|
}
|
|
2130
|
-
Cadenza.
|
|
2136
|
+
Cadenza.signalBroker.emit(signal, data, options);
|
|
2131
2137
|
}
|
|
2132
2138
|
};
|
|
2133
2139
|
|
|
@@ -2374,7 +2380,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
2374
2380
|
},
|
|
2375
2381
|
filter: { uuid: this.routineExecId }
|
|
2376
2382
|
},
|
|
2377
|
-
{ squash: true, squashId: this.routineExecId
|
|
2383
|
+
{ squash: true, squashId: this.routineExecId }
|
|
2378
2384
|
);
|
|
2379
2385
|
}
|
|
2380
2386
|
if (this.debug && !this.task.isSubMeta && !this.context.getMetadata().__isSubMeta || this.verbose) {
|
|
@@ -2439,7 +2445,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
2439
2445
|
);
|
|
2440
2446
|
if (this.graphDone()) {
|
|
2441
2447
|
const context2 = this.context.getFullContext();
|
|
2442
|
-
if (context2.__isDeputy)
|
|
2448
|
+
if (context2.__isDeputy || context2.__isInquiry)
|
|
2443
2449
|
this.emitWithMetadata(
|
|
2444
2450
|
`meta.node.graph_completed:${this.routineExecId}`,
|
|
2445
2451
|
context2
|
|
@@ -2547,6 +2553,7 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
2547
2553
|
const result = this.task.execute(
|
|
2548
2554
|
this.context,
|
|
2549
2555
|
this.emitWithMetadata.bind(this),
|
|
2556
|
+
this.inquire.bind(this),
|
|
2550
2557
|
this.onProgress.bind(this),
|
|
2551
2558
|
{ nodeId: this.id, routineExecId: this.routineExecId }
|
|
2552
2559
|
);
|
|
@@ -2565,6 +2572,13 @@ var GraphNode = class _GraphNode extends SignalEmitter {
|
|
|
2565
2572
|
});
|
|
2566
2573
|
}
|
|
2567
2574
|
}
|
|
2575
|
+
inquire(inquiry, context, options) {
|
|
2576
|
+
return Cadenza.inquire(
|
|
2577
|
+
inquiry,
|
|
2578
|
+
{ ...context, __executionTraceId: this.executionTraceId },
|
|
2579
|
+
options
|
|
2580
|
+
);
|
|
2581
|
+
}
|
|
2568
2582
|
/**
|
|
2569
2583
|
* Emits a signal along with its associated metadata. The metadata includes
|
|
2570
2584
|
* task-specific information such as task name, version, execution ID, and
|
|
@@ -3193,8 +3207,7 @@ var GraphRoutine = class extends SignalEmitter {
|
|
|
3193
3207
|
*/
|
|
3194
3208
|
doOn(...signals) {
|
|
3195
3209
|
signals.forEach((signal) => {
|
|
3196
|
-
|
|
3197
|
-
Cadenza.broker.observe(signal, this);
|
|
3210
|
+
this.tasks.forEach((task) => task.doOn(signal));
|
|
3198
3211
|
this.observedSignals.add(signal);
|
|
3199
3212
|
});
|
|
3200
3213
|
return this;
|
|
@@ -3208,7 +3221,7 @@ var GraphRoutine = class extends SignalEmitter {
|
|
|
3208
3221
|
*/
|
|
3209
3222
|
unsubscribeAll() {
|
|
3210
3223
|
this.observedSignals.forEach(
|
|
3211
|
-
(signal) =>
|
|
3224
|
+
(signal) => this.tasks.forEach((task) => task.unsubscribe(signal))
|
|
3212
3225
|
);
|
|
3213
3226
|
this.observedSignals.clear();
|
|
3214
3227
|
return this;
|
|
@@ -3221,10 +3234,8 @@ var GraphRoutine = class extends SignalEmitter {
|
|
|
3221
3234
|
*/
|
|
3222
3235
|
unsubscribe(...signals) {
|
|
3223
3236
|
signals.forEach((signal) => {
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
this.observedSignals.delete(signal);
|
|
3227
|
-
}
|
|
3237
|
+
this.tasks.forEach((task) => task.unsubscribe(signal));
|
|
3238
|
+
this.observedSignals.delete(signal);
|
|
3228
3239
|
});
|
|
3229
3240
|
return this;
|
|
3230
3241
|
}
|
|
@@ -3320,26 +3331,22 @@ var GraphRunner = class extends SignalEmitter {
|
|
|
3320
3331
|
}
|
|
3321
3332
|
});
|
|
3322
3333
|
}
|
|
3323
|
-
this.emitMetrics(
|
|
3324
|
-
|
|
3325
|
-
|
|
3326
|
-
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
|
|
3334
|
-
previousRoutineExecution: context.__localRoutineExecId ?? context.__metadata?.__routineExecId ?? null,
|
|
3335
|
-
created: formatTimestamp(Date.now())
|
|
3336
|
-
},
|
|
3337
|
-
__metadata: {
|
|
3338
|
-
__executionTraceId: executionTraceId
|
|
3339
|
-
}
|
|
3334
|
+
this.emitMetrics("meta.runner.added_tasks", {
|
|
3335
|
+
data: {
|
|
3336
|
+
uuid: routineExecId,
|
|
3337
|
+
name: routineName,
|
|
3338
|
+
routineVersion,
|
|
3339
|
+
isMeta,
|
|
3340
|
+
executionTraceId,
|
|
3341
|
+
context: ctx.getContext(),
|
|
3342
|
+
metaContext: ctx.getMetadata(),
|
|
3343
|
+
previousRoutineExecution: context.__localRoutineExecId ?? context.__metadata?.__routineExecId ?? null,
|
|
3344
|
+
created: formatTimestamp(Date.now())
|
|
3340
3345
|
},
|
|
3341
|
-
|
|
3342
|
-
|
|
3346
|
+
__metadata: {
|
|
3347
|
+
__executionTraceId: executionTraceId
|
|
3348
|
+
}
|
|
3349
|
+
});
|
|
3343
3350
|
}
|
|
3344
3351
|
allTasks.forEach(
|
|
3345
3352
|
(task) => this.currentRun.addNode(
|
|
@@ -3513,7 +3520,8 @@ var Task = class _Task extends SignalEmitter {
|
|
|
3513
3520
|
this.signalsToEmitAfter = /* @__PURE__ */ new Set();
|
|
3514
3521
|
this.signalsToEmitOnFail = /* @__PURE__ */ new Set();
|
|
3515
3522
|
this.observedSignals = /* @__PURE__ */ new Set();
|
|
3516
|
-
this.
|
|
3523
|
+
this.handlesIntents = /* @__PURE__ */ new Set();
|
|
3524
|
+
this.inquiresIntents = /* @__PURE__ */ new Set();
|
|
3517
3525
|
this.name = name;
|
|
3518
3526
|
this.taskFunction = task;
|
|
3519
3527
|
this.description = description;
|
|
@@ -3592,7 +3600,8 @@ var Task = class _Task extends SignalEmitter {
|
|
|
3592
3600
|
observed: Array.from(this.observedSignals)
|
|
3593
3601
|
},
|
|
3594
3602
|
intents: {
|
|
3595
|
-
handles: Array.from(this.
|
|
3603
|
+
handles: Array.from(this.handlesIntents),
|
|
3604
|
+
inquires: Array.from(this.inquiresIntents)
|
|
3596
3605
|
}
|
|
3597
3606
|
},
|
|
3598
3607
|
taskInstance: this,
|
|
@@ -3884,14 +3893,16 @@ var Task = class _Task extends SignalEmitter {
|
|
|
3884
3893
|
*
|
|
3885
3894
|
* @param {GraphContext} context The execution context which provides data and functions necessary for the task.
|
|
3886
3895
|
* @param {function(string, AnyObject): void} emit A function to emit signals and communicate intermediate results or states.
|
|
3896
|
+
* @param {function(string, AnyObject, InquiryOptions): Promise<AnyObject>} inquire A function to inquire something from another task.
|
|
3887
3897
|
* @param {function(number): void} progressCallback A callback function used to report task progress as a percentage (0 to 100).
|
|
3888
3898
|
* @param {{ nodeId: string; routineExecId: string }} nodeData An object containing identifiers related to the node and execution routine.
|
|
3889
3899
|
* @return {TaskResult} The result of the executed task.
|
|
3890
3900
|
*/
|
|
3891
|
-
execute(context, emit, progressCallback, nodeData) {
|
|
3901
|
+
execute(context, emit, inquire, progressCallback, nodeData) {
|
|
3892
3902
|
return this.taskFunction(
|
|
3893
3903
|
this.isMeta ? context.getClonedFullContext() : context.getClonedContext(),
|
|
3894
3904
|
emit,
|
|
3905
|
+
inquire,
|
|
3895
3906
|
progressCallback
|
|
3896
3907
|
);
|
|
3897
3908
|
}
|
|
@@ -4101,7 +4112,7 @@ var Task = class _Task extends SignalEmitter {
|
|
|
4101
4112
|
doOn(...signals) {
|
|
4102
4113
|
signals.forEach((signal) => {
|
|
4103
4114
|
if (this.observedSignals.has(signal)) return;
|
|
4104
|
-
Cadenza.
|
|
4115
|
+
Cadenza.signalBroker.observe(signal, this);
|
|
4105
4116
|
this.observedSignals.add(signal);
|
|
4106
4117
|
if (this.register) {
|
|
4107
4118
|
this.emitWithMetadata("meta.task.observed_signal", {
|
|
@@ -4155,7 +4166,7 @@ var Task = class _Task extends SignalEmitter {
|
|
|
4155
4166
|
attachSignal(...signals) {
|
|
4156
4167
|
signals.forEach((signal) => {
|
|
4157
4168
|
this.emitsSignals.add(signal);
|
|
4158
|
-
Cadenza.
|
|
4169
|
+
Cadenza.signalBroker.registerEmittedSignal(signal);
|
|
4159
4170
|
if (this.register) {
|
|
4160
4171
|
const data = {
|
|
4161
4172
|
signals: {
|
|
@@ -4187,7 +4198,7 @@ var Task = class _Task extends SignalEmitter {
|
|
|
4187
4198
|
unsubscribe(...signals) {
|
|
4188
4199
|
signals.forEach((signal) => {
|
|
4189
4200
|
if (this.observedSignals.has(signal)) {
|
|
4190
|
-
Cadenza.
|
|
4201
|
+
Cadenza.signalBroker.unsubscribe(signal, this);
|
|
4191
4202
|
this.observedSignals.delete(signal);
|
|
4192
4203
|
if (this.register) {
|
|
4193
4204
|
signal = signal.split(":")[0];
|
|
@@ -4247,8 +4258,30 @@ var Task = class _Task extends SignalEmitter {
|
|
|
4247
4258
|
this.signalsToEmitAfter.clear();
|
|
4248
4259
|
return this;
|
|
4249
4260
|
}
|
|
4250
|
-
|
|
4251
|
-
|
|
4261
|
+
respondsTo(...inquires) {
|
|
4262
|
+
for (const intentName of inquires) {
|
|
4263
|
+
this.handlesIntents.add(intentName);
|
|
4264
|
+
Cadenza.inquiryBroker.observe(intentName, this);
|
|
4265
|
+
const intent = Cadenza.inquiryBroker.intents.get(intentName);
|
|
4266
|
+
this.inputContextSchema = intent?.input;
|
|
4267
|
+
this.outputContextSchema = intent?.output;
|
|
4268
|
+
}
|
|
4269
|
+
return this;
|
|
4270
|
+
}
|
|
4271
|
+
attachIntents(...intentNames) {
|
|
4272
|
+
for (const intent of intentNames) {
|
|
4273
|
+
this.inquiresIntents.add(intent);
|
|
4274
|
+
}
|
|
4275
|
+
return this;
|
|
4276
|
+
}
|
|
4277
|
+
detachIntents(...intentNames) {
|
|
4278
|
+
for (const intent of intentNames) {
|
|
4279
|
+
this.inquiresIntents.delete(intent);
|
|
4280
|
+
}
|
|
4281
|
+
return this;
|
|
4282
|
+
}
|
|
4283
|
+
detachAllIntents() {
|
|
4284
|
+
this.inquiresIntents.clear();
|
|
4252
4285
|
return this;
|
|
4253
4286
|
}
|
|
4254
4287
|
/**
|
|
@@ -4585,6 +4618,7 @@ var DebounceTask = class extends Task {
|
|
|
4585
4618
|
this.lastTimeout = null;
|
|
4586
4619
|
this.lastProgressCallback = null;
|
|
4587
4620
|
this.lastEmitFunction = null;
|
|
4621
|
+
this.lastInquireFunction = null;
|
|
4588
4622
|
this.debounceTime = debounceTime;
|
|
4589
4623
|
this.leading = leading;
|
|
4590
4624
|
this.trailing = trailing;
|
|
@@ -4607,6 +4641,7 @@ var DebounceTask = class extends Task {
|
|
|
4607
4641
|
result = this.taskFunction(
|
|
4608
4642
|
this.lastContext.getClonedContext(),
|
|
4609
4643
|
this.lastEmitFunction,
|
|
4644
|
+
this.lastInquireFunction,
|
|
4610
4645
|
this.lastProgressCallback
|
|
4611
4646
|
);
|
|
4612
4647
|
} catch (error) {
|
|
@@ -4622,6 +4657,11 @@ var DebounceTask = class extends Task {
|
|
|
4622
4657
|
this.lastResolve(result);
|
|
4623
4658
|
}
|
|
4624
4659
|
}
|
|
4660
|
+
this.lastContext = null;
|
|
4661
|
+
this.lastTimeout = null;
|
|
4662
|
+
this.lastProgressCallback = null;
|
|
4663
|
+
this.lastEmitFunction = null;
|
|
4664
|
+
this.lastInquireFunction = null;
|
|
4625
4665
|
}
|
|
4626
4666
|
/**
|
|
4627
4667
|
* Executes a debounced operation, ensuring controlled execution of functions
|
|
@@ -4633,10 +4673,11 @@ var DebounceTask = class extends Task {
|
|
|
4633
4673
|
* @param {GraphContext} context - The execution context for the operation.
|
|
4634
4674
|
* @param {NodeJS.Timeout} timeout - A timeout object for managing execution delays.
|
|
4635
4675
|
* @param {Function} emit - A callback function to emit signals with a specific context.
|
|
4676
|
+
* @param inquire
|
|
4636
4677
|
* @param {Function} progressCallback - A callback function to report progress during operation execution.
|
|
4637
4678
|
* @return {void} Does not return a value but sets internal timers and invokes provided callbacks.
|
|
4638
4679
|
*/
|
|
4639
|
-
debouncedTrigger(resolve, reject, context, timeout, emit, progressCallback) {
|
|
4680
|
+
debouncedTrigger(resolve, reject, context, timeout, emit, inquire, progressCallback) {
|
|
4640
4681
|
const callNow = this.leading && this.timer === null;
|
|
4641
4682
|
const isNewBurst = this.timer === null;
|
|
4642
4683
|
if (this.timer !== null) {
|
|
@@ -4649,6 +4690,7 @@ var DebounceTask = class extends Task {
|
|
|
4649
4690
|
this.lastTimeout = timeout;
|
|
4650
4691
|
this.lastProgressCallback = progressCallback;
|
|
4651
4692
|
this.lastEmitFunction = emit;
|
|
4693
|
+
this.lastInquireFunction = inquire;
|
|
4652
4694
|
if (!callNow) {
|
|
4653
4695
|
this.hasLaterCall = true;
|
|
4654
4696
|
}
|
|
@@ -4686,10 +4728,11 @@ var DebounceTask = class extends Task {
|
|
|
4686
4728
|
*
|
|
4687
4729
|
* @param {GraphContext} context - The context containing relevant graph data for the execution.
|
|
4688
4730
|
* @param {function(string, any): void} emit - A function used to emit signals with associated context.
|
|
4731
|
+
* @param inquire
|
|
4689
4732
|
* @param {function(number): void} progressCallback - A callback function to report the progress of the task as a number between 0 and 1.
|
|
4690
4733
|
* @return {Promise<TaskResult>} A promise that resolves with the task result upon completion or rejects on failure.
|
|
4691
4734
|
*/
|
|
4692
|
-
execute(context, emit, progressCallback) {
|
|
4735
|
+
execute(context, emit, inquire, progressCallback) {
|
|
4693
4736
|
return new Promise((resolve, reject) => {
|
|
4694
4737
|
const timeout = setTimeout(() => {
|
|
4695
4738
|
resolve(false);
|
|
@@ -4700,6 +4743,7 @@ var DebounceTask = class extends Task {
|
|
|
4700
4743
|
context,
|
|
4701
4744
|
timeout,
|
|
4702
4745
|
emit,
|
|
4746
|
+
inquire,
|
|
4703
4747
|
progressCallback
|
|
4704
4748
|
);
|
|
4705
4749
|
});
|
|
@@ -4739,12 +4783,19 @@ var EphemeralTask = class extends Task {
|
|
|
4739
4783
|
*
|
|
4740
4784
|
* @param {any} context - The execution context, carrying necessary parameters or states for the operation.
|
|
4741
4785
|
* @param {function(string, AnyObject): void} emit - A function to emit signals with a string identifier and associated context.
|
|
4786
|
+
* @param inquire
|
|
4742
4787
|
* @param {function(number): void} progressCallback - A callback function to report the progress of the execution as a numerical value.
|
|
4743
4788
|
* @param {{ nodeId: string, routineExecId: string }} nodeData - An object containing details about the node ID and routine execution ID.
|
|
4744
4789
|
* @return {any} The result of the execution, returned from the base implementation or processed internally.
|
|
4745
4790
|
*/
|
|
4746
|
-
execute(context, emit, progressCallback, nodeData) {
|
|
4747
|
-
const result = super.execute(
|
|
4791
|
+
execute(context, emit, inquire, progressCallback, nodeData) {
|
|
4792
|
+
const result = super.execute(
|
|
4793
|
+
context,
|
|
4794
|
+
emit,
|
|
4795
|
+
inquire,
|
|
4796
|
+
progressCallback,
|
|
4797
|
+
nodeData
|
|
4798
|
+
);
|
|
4748
4799
|
if (this.once || this.condition(result)) {
|
|
4749
4800
|
this.destroy();
|
|
4750
4801
|
}
|
|
@@ -5491,6 +5542,187 @@ var GraphStandardRun = class extends GraphRunStrategy {
|
|
|
5491
5542
|
}
|
|
5492
5543
|
};
|
|
5493
5544
|
|
|
5545
|
+
// src/engine/InquiryBroker.ts
|
|
5546
|
+
var import_uuid7 = require("uuid");
|
|
5547
|
+
var InquiryBroker = class _InquiryBroker extends SignalEmitter {
|
|
5548
|
+
constructor() {
|
|
5549
|
+
super(...arguments);
|
|
5550
|
+
this.debug = false;
|
|
5551
|
+
this.verbose = false;
|
|
5552
|
+
this.inquiryObservers = /* @__PURE__ */ new Map();
|
|
5553
|
+
this.intents = /* @__PURE__ */ new Map();
|
|
5554
|
+
}
|
|
5555
|
+
static get instance() {
|
|
5556
|
+
if (!this.instance_) {
|
|
5557
|
+
this.instance_ = new _InquiryBroker();
|
|
5558
|
+
}
|
|
5559
|
+
return this.instance_;
|
|
5560
|
+
}
|
|
5561
|
+
setDebug(value) {
|
|
5562
|
+
this.debug = value;
|
|
5563
|
+
}
|
|
5564
|
+
setVerbose(value) {
|
|
5565
|
+
this.verbose = value;
|
|
5566
|
+
}
|
|
5567
|
+
validateInquiryName(inquiryName) {
|
|
5568
|
+
if (inquiryName.length > 100) {
|
|
5569
|
+
throw new Error(
|
|
5570
|
+
`Inquiry name must be less than 100 characters: ${inquiryName}`
|
|
5571
|
+
);
|
|
5572
|
+
}
|
|
5573
|
+
if (inquiryName.includes(" ")) {
|
|
5574
|
+
throw new Error(`Inquiry name must not contain spaces: ${inquiryName}"`);
|
|
5575
|
+
}
|
|
5576
|
+
if (inquiryName.includes("\\")) {
|
|
5577
|
+
throw new Error(
|
|
5578
|
+
`Inquiry name must not contain backslashes: ${inquiryName}`
|
|
5579
|
+
);
|
|
5580
|
+
}
|
|
5581
|
+
if (inquiryName.includes(".")) {
|
|
5582
|
+
throw new Error(`Inquiry name must not contain dots: ${inquiryName}`);
|
|
5583
|
+
}
|
|
5584
|
+
}
|
|
5585
|
+
/**
|
|
5586
|
+
* Initializes with runners.
|
|
5587
|
+
* @param runner Standard runner for user signals.
|
|
5588
|
+
* @param metaRunner Meta runner for 'meta.' signals (suppresses further meta-emits).
|
|
5589
|
+
*/
|
|
5590
|
+
bootstrap(runner, metaRunner) {
|
|
5591
|
+
this.runner = runner;
|
|
5592
|
+
this.metaRunner = metaRunner;
|
|
5593
|
+
}
|
|
5594
|
+
init() {
|
|
5595
|
+
}
|
|
5596
|
+
/**
|
|
5597
|
+
* Observes an inquiry with a routine/task.
|
|
5598
|
+
* @param inquiry The inquiry (e.g., 'domain.action', 'domain.*' for wildcards).
|
|
5599
|
+
* @param task The observer.
|
|
5600
|
+
* @edge Duplicates ignored; supports wildcards for broad listening.
|
|
5601
|
+
*/
|
|
5602
|
+
observe(inquiry, task) {
|
|
5603
|
+
this.addInquiry(inquiry);
|
|
5604
|
+
this.inquiryObservers.get(inquiry).tasks.add(task);
|
|
5605
|
+
}
|
|
5606
|
+
/**
|
|
5607
|
+
* Unsubscribes a routine/task from an inquiry.
|
|
5608
|
+
* @param inquiry The inquiry.
|
|
5609
|
+
* @param task The observer.
|
|
5610
|
+
* @edge Removes all instances if duplicate; deletes if empty.
|
|
5611
|
+
*/
|
|
5612
|
+
unsubscribe(inquiry, task) {
|
|
5613
|
+
const obs = this.inquiryObservers.get(inquiry);
|
|
5614
|
+
if (obs) {
|
|
5615
|
+
obs.tasks.delete(task);
|
|
5616
|
+
if (obs.tasks.size === 0) {
|
|
5617
|
+
this.inquiryObservers.delete(inquiry);
|
|
5618
|
+
}
|
|
5619
|
+
}
|
|
5620
|
+
}
|
|
5621
|
+
addInquiry(inquiry) {
|
|
5622
|
+
if (!this.inquiryObservers.has(inquiry)) {
|
|
5623
|
+
this.validateInquiryName(inquiry);
|
|
5624
|
+
this.inquiryObservers.set(inquiry, {
|
|
5625
|
+
fn: (runner, tasks, context) => runner.run(tasks, context),
|
|
5626
|
+
tasks: /* @__PURE__ */ new Set(),
|
|
5627
|
+
registered: false
|
|
5628
|
+
});
|
|
5629
|
+
this.addIntent({
|
|
5630
|
+
name: inquiry,
|
|
5631
|
+
description: "",
|
|
5632
|
+
input: void 0,
|
|
5633
|
+
output: void 0
|
|
5634
|
+
});
|
|
5635
|
+
this.emit("meta.inquiry_broker.added", { inquiryName: inquiry });
|
|
5636
|
+
}
|
|
5637
|
+
}
|
|
5638
|
+
addIntent(intent) {
|
|
5639
|
+
if (!this.intents.has(intent.name)) {
|
|
5640
|
+
this.validateInquiryName(intent.name);
|
|
5641
|
+
this.intents.set(intent.name, intent);
|
|
5642
|
+
} else {
|
|
5643
|
+
const currentIntent = this.intents.get(intent.name);
|
|
5644
|
+
if (currentIntent.description !== intent.description) {
|
|
5645
|
+
currentIntent.description = intent.description;
|
|
5646
|
+
}
|
|
5647
|
+
if (intent.input && currentIntent.input !== intent.input) {
|
|
5648
|
+
currentIntent.input = intent.input;
|
|
5649
|
+
}
|
|
5650
|
+
if (intent.output && currentIntent.output !== intent.output) {
|
|
5651
|
+
currentIntent.output = intent.output;
|
|
5652
|
+
}
|
|
5653
|
+
if (this.inquiryObservers.has(intent.name)) {
|
|
5654
|
+
for (const task of this.inquiryObservers.get(intent.name).tasks) {
|
|
5655
|
+
task.respondsTo(intent.name);
|
|
5656
|
+
}
|
|
5657
|
+
}
|
|
5658
|
+
}
|
|
5659
|
+
}
|
|
5660
|
+
inquire(inquiry, context, options = {
|
|
5661
|
+
timeout: 0
|
|
5662
|
+
}) {
|
|
5663
|
+
const tasks = this.inquiryObservers.get(inquiry)?.tasks;
|
|
5664
|
+
if (!tasks) {
|
|
5665
|
+
return Promise.resolve({});
|
|
5666
|
+
}
|
|
5667
|
+
return new Promise((resolve) => {
|
|
5668
|
+
let joinedContext = {};
|
|
5669
|
+
const pendingTasks = Array.from(tasks).map((task) => task.name);
|
|
5670
|
+
const resolveTasks = [];
|
|
5671
|
+
let timeoutId;
|
|
5672
|
+
if (options.timeout > 0) {
|
|
5673
|
+
timeoutId = setTimeout(() => {
|
|
5674
|
+
for (const resolveTask of resolveTasks) {
|
|
5675
|
+
resolveTask.destroy();
|
|
5676
|
+
}
|
|
5677
|
+
resolve({
|
|
5678
|
+
__error: "Inquire timeout",
|
|
5679
|
+
errored: true,
|
|
5680
|
+
pendingTasks,
|
|
5681
|
+
...joinedContext
|
|
5682
|
+
});
|
|
5683
|
+
}, options.timeout);
|
|
5684
|
+
}
|
|
5685
|
+
for (const task of tasks) {
|
|
5686
|
+
const inquiryId = (0, import_uuid7.v4)();
|
|
5687
|
+
resolveTasks.push(
|
|
5688
|
+
Cadenza.createEphemeralMetaTask(
|
|
5689
|
+
`Resolve inquiry for ${inquiry}`,
|
|
5690
|
+
(ctx) => {
|
|
5691
|
+
joinedContext = { ...joinedContext, ...ctx };
|
|
5692
|
+
pendingTasks.splice(pendingTasks.indexOf(task.name), 1);
|
|
5693
|
+
if (pendingTasks.length === 0) {
|
|
5694
|
+
resolve(joinedContext);
|
|
5695
|
+
}
|
|
5696
|
+
if (timeoutId) {
|
|
5697
|
+
clearTimeout(timeoutId);
|
|
5698
|
+
timeoutId = void 0;
|
|
5699
|
+
}
|
|
5700
|
+
},
|
|
5701
|
+
"",
|
|
5702
|
+
{
|
|
5703
|
+
once: true,
|
|
5704
|
+
register: false
|
|
5705
|
+
}
|
|
5706
|
+
).doOn(`meta.node.graph_completed:${inquiryId}`)
|
|
5707
|
+
);
|
|
5708
|
+
if (task.isMeta) {
|
|
5709
|
+
this.metaRunner?.run(task, {
|
|
5710
|
+
...context,
|
|
5711
|
+
__routineExecId: inquiryId,
|
|
5712
|
+
__isInquiry: true
|
|
5713
|
+
});
|
|
5714
|
+
} else {
|
|
5715
|
+
this.runner?.run(task, {
|
|
5716
|
+
...context,
|
|
5717
|
+
__routineExecId: inquiryId,
|
|
5718
|
+
__isInquiry: true
|
|
5719
|
+
});
|
|
5720
|
+
}
|
|
5721
|
+
}
|
|
5722
|
+
});
|
|
5723
|
+
}
|
|
5724
|
+
};
|
|
5725
|
+
|
|
5494
5726
|
// src/Cadenza.ts
|
|
5495
5727
|
var Cadenza = class {
|
|
5496
5728
|
/**
|
|
@@ -5503,17 +5735,21 @@ var Cadenza = class {
|
|
|
5503
5735
|
static bootstrap() {
|
|
5504
5736
|
if (this.isBootstrapped) return;
|
|
5505
5737
|
this.isBootstrapped = true;
|
|
5506
|
-
this.
|
|
5738
|
+
this.signalBroker = SignalBroker.instance;
|
|
5739
|
+
this.inquiryBroker = InquiryBroker.instance;
|
|
5507
5740
|
this.runner = new GraphRunner();
|
|
5508
5741
|
this.metaRunner = new GraphRunner(true);
|
|
5509
|
-
this.
|
|
5742
|
+
this.signalBroker.bootstrap(this.runner, this.metaRunner);
|
|
5743
|
+
this.inquiryBroker.bootstrap(this.runner, this.metaRunner);
|
|
5510
5744
|
if (this.mode === "debug" || this.mode === "dev") {
|
|
5511
|
-
this.
|
|
5745
|
+
this.signalBroker.setDebug(true);
|
|
5746
|
+
this.inquiryBroker.setDebug(true);
|
|
5512
5747
|
this.runner.setDebug(true);
|
|
5513
5748
|
this.metaRunner.setDebug(true);
|
|
5514
5749
|
}
|
|
5515
5750
|
this.registry = GraphRegistry.instance;
|
|
5516
|
-
this.
|
|
5751
|
+
this.signalBroker.init();
|
|
5752
|
+
this.inquiryBroker.init();
|
|
5517
5753
|
}
|
|
5518
5754
|
/**
|
|
5519
5755
|
* Retrieves the available strategies for running graphs.
|
|
@@ -5540,18 +5776,18 @@ var Cadenza = class {
|
|
|
5540
5776
|
this.mode = mode;
|
|
5541
5777
|
this.bootstrap();
|
|
5542
5778
|
if (mode === "debug" || mode === "dev") {
|
|
5543
|
-
this.
|
|
5779
|
+
this.signalBroker.setDebug(true);
|
|
5544
5780
|
this.runner.setDebug(true);
|
|
5545
5781
|
}
|
|
5546
5782
|
if (mode === "verbose") {
|
|
5547
|
-
this.
|
|
5548
|
-
this.
|
|
5783
|
+
this.signalBroker.setDebug(true);
|
|
5784
|
+
this.signalBroker.setVerbose(true);
|
|
5549
5785
|
this.runner.setDebug(true);
|
|
5550
5786
|
this.runner.setVerbose(true);
|
|
5551
5787
|
}
|
|
5552
5788
|
if (mode === "production") {
|
|
5553
|
-
this.
|
|
5554
|
-
this.
|
|
5789
|
+
this.signalBroker.setDebug(false);
|
|
5790
|
+
this.signalBroker.setVerbose(false);
|
|
5555
5791
|
this.runner.setDebug(false);
|
|
5556
5792
|
this.runner.setVerbose(false);
|
|
5557
5793
|
}
|
|
@@ -5608,13 +5844,13 @@ var Cadenza = class {
|
|
|
5608
5844
|
* ```
|
|
5609
5845
|
*/
|
|
5610
5846
|
static emit(event, data = {}, options = {}) {
|
|
5611
|
-
this.
|
|
5847
|
+
this.signalBroker?.emit(event, data, options);
|
|
5612
5848
|
}
|
|
5613
5849
|
static schedule(taskName, context, delayMs, exactDateTime) {
|
|
5614
|
-
this.
|
|
5850
|
+
this.signalBroker?.schedule(taskName, context, { delayMs, exactDateTime });
|
|
5615
5851
|
}
|
|
5616
5852
|
static interval(taskName, context, intervalMs, leading = false, startDateTime) {
|
|
5617
|
-
this.
|
|
5853
|
+
this.signalBroker?.interval(
|
|
5618
5854
|
taskName,
|
|
5619
5855
|
context,
|
|
5620
5856
|
intervalMs,
|
|
@@ -5623,7 +5859,7 @@ var Cadenza = class {
|
|
|
5623
5859
|
);
|
|
5624
5860
|
}
|
|
5625
5861
|
static debounce(signalName, context, delayMs) {
|
|
5626
|
-
this.
|
|
5862
|
+
this.signalBroker?.debounce(signalName, context, { delayMs });
|
|
5627
5863
|
}
|
|
5628
5864
|
static get(taskName) {
|
|
5629
5865
|
return this.registry?.tasks.get(taskName);
|
|
@@ -5631,6 +5867,13 @@ var Cadenza = class {
|
|
|
5631
5867
|
static getRoutine(routineName) {
|
|
5632
5868
|
return this.registry?.routines.get(routineName);
|
|
5633
5869
|
}
|
|
5870
|
+
static defineIntent(intent) {
|
|
5871
|
+
this.inquiryBroker?.intents.set(intent.name, intent);
|
|
5872
|
+
return intent;
|
|
5873
|
+
}
|
|
5874
|
+
static async inquire(inquiry, context, options) {
|
|
5875
|
+
return this.inquiryBroker?.inquire(inquiry, context, options);
|
|
5876
|
+
}
|
|
5634
5877
|
/**
|
|
5635
5878
|
* Creates and registers a new task with the specified parameters and options.
|
|
5636
5879
|
* Tasks are the basic building blocks of Cadenza graphs and are responsible for executing logic.
|
|
@@ -6159,7 +6402,7 @@ var Cadenza = class {
|
|
|
6159
6402
|
return new GraphRoutine(name, tasks, description, true);
|
|
6160
6403
|
}
|
|
6161
6404
|
static reset() {
|
|
6162
|
-
this.
|
|
6405
|
+
this.signalBroker?.reset();
|
|
6163
6406
|
this.registry?.reset();
|
|
6164
6407
|
this.isBootstrapped = false;
|
|
6165
6408
|
}
|
|
@@ -6167,14 +6410,6 @@ var Cadenza = class {
|
|
|
6167
6410
|
Cadenza.isBootstrapped = false;
|
|
6168
6411
|
Cadenza.mode = "production";
|
|
6169
6412
|
|
|
6170
|
-
// src/graph/definition/SignalTask.ts
|
|
6171
|
-
var SignalTask = class extends Task {
|
|
6172
|
-
constructor(signal, description = "") {
|
|
6173
|
-
super(signal, () => true, description);
|
|
6174
|
-
this.signalsToEmitAfter.add(signal);
|
|
6175
|
-
}
|
|
6176
|
-
};
|
|
6177
|
-
|
|
6178
6413
|
// src/index.ts
|
|
6179
6414
|
var index_default = Cadenza;
|
|
6180
6415
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -6188,7 +6423,6 @@ var index_default = Cadenza;
|
|
|
6188
6423
|
GraphRunner,
|
|
6189
6424
|
SignalBroker,
|
|
6190
6425
|
SignalEmitter,
|
|
6191
|
-
SignalTask,
|
|
6192
6426
|
Task
|
|
6193
6427
|
});
|
|
6194
6428
|
/*! Bundled license information:
|