-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexecutor.ts
More file actions
1260 lines (1065 loc) · 40.1 KB
/
Copy pathexecutor.ts
File metadata and controls
1260 lines (1065 loc) · 40.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Workflow Executor - Core execution engine for AITuberFlow.
*
* Supports two execution modes:
* 1. Linear: Entry point → DAG order → completion
* 2. Event-driven: Source nodes emit events → downstream nodes react
*
* Ported from Python apps/server/engine/executor.py (1173 lines).
*/
import { db } from "../db/database";
import { globalSettings } from "../db/schema";
import { vtsClient } from "../integrations/vtube-studio";
import type { Event } from "./event-bus";
import { EventBus, EventFilter } from "./event-bus";
import { EventQueue } from "./event-queue";
import { SOURCE_NODE_TYPES, loadPlugin } from "./plugin-loader";
import { TaskRegistry } from "./task-registry";
// ─── Types ───────────────────────────────────────────────────────
interface NodeData {
id: string;
type: string;
config?: Record<string, unknown>;
eventFilters?: EventFilterDef[];
event_filters?: EventFilterDef[];
position?: { x: number; y: number };
}
interface ConnectionData {
id: string;
from: { nodeId: string; port: string };
to: { nodeId: string; port: string };
}
interface WorkflowData {
id?: string;
name?: string;
nodes: NodeData[];
connections: ConnectionData[];
character: Record<string, unknown>;
}
interface EventFilterDef {
event: string;
condition?: string;
}
type LogCallback = (nodeId: string | null, message: string, level: string) => Promise<void>;
type EventCallback = (event: Event) => Promise<void>;
type StatusCallback = (
nodeId: string,
status: string,
data?: Record<string, unknown> | null,
) => Promise<void>;
// ─── NodeContext (executor-internal) ─────────────────────────────
export class NodeContext {
workflowId: string;
nodeId: string;
character: Record<string, unknown>;
private eventBus: EventBus | null;
private logCallback: LogCallback | null;
private taskRegistry: TaskRegistry | null;
private taskIds = new Set<string>();
private localControllers = new Set<AbortController>();
constructor(opts: {
workflowId: string;
nodeId: string;
character: Record<string, unknown>;
eventBus?: EventBus | null;
logCallback?: LogCallback | null;
taskRegistry?: TaskRegistry | null;
}) {
this.workflowId = opts.workflowId;
this.nodeId = opts.nodeId;
this.character = opts.character;
this.eventBus = opts.eventBus ?? null;
this.logCallback = opts.logCallback ?? null;
this.taskRegistry = opts.taskRegistry ?? null;
}
async emitEvent(event: Event | Record<string, any>): Promise<void> {
if (!this.eventBus) return;
let evt: Event;
if ("type" in event && typeof event.type === "string") {
const { type, source_node_id, sourceNodeId, timestamp, ...payload } = event as any;
evt = {
type,
payload: (event as Event).payload ?? payload,
sourceNodeId: this.nodeId,
timestamp: timestamp ?? new Date().toISOString(),
};
} else {
evt = {
type: "unknown",
payload: event as Record<string, any>,
sourceNodeId: this.nodeId,
timestamp: new Date().toISOString(),
};
}
evt.sourceNodeId = this.nodeId;
await this.eventBus.emit(evt);
}
async log(message: string, level = "info"): Promise<void> {
if (this.logCallback) {
await this.logCallback(this.nodeId, message, level);
}
}
createTask(fn: (signal: AbortSignal) => Promise<void>): AbortController {
const taskId = `${this.nodeId}_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
if (this.taskRegistry) {
this.taskIds.add(taskId);
return this.taskRegistry.register(taskId, async (signal) => {
try {
await fn(signal);
} finally {
this.taskIds.delete(taskId);
}
});
}
const controller = new AbortController();
this.localControllers.add(controller);
fn(controller.signal)
.catch((err) => {
if (err?.name !== "AbortError") {
console.error(`Background task ${taskId} error:`, err);
}
})
.finally(() => {
this.localControllers.delete(controller);
});
return controller;
}
async updateCharacter(updates: Record<string, any>): Promise<void> {
// Guard against prototype pollution: never copy __proto__/constructor/prototype
// keys from user-controllable updates into the character state.
for (const [key, value] of Object.entries(updates)) {
if (key === "__proto__" || key === "constructor" || key === "prototype") continue;
this.character[key] = value;
}
}
getCharacterName(): string {
return (this.character.name as string) ?? "AI Assistant";
}
getCharacterPersonality(): string {
return (this.character.personality as string) ?? "";
}
getEmotion(): Record<string, any> {
return (
(this.character.emotion as Record<string, any>) ?? {
current: "neutral",
intensity: 0.5,
}
);
}
async cancelBackgroundTasks(): Promise<void> {
for (const taskId of this.taskIds) {
this.taskRegistry?.cancel(taskId);
}
this.taskIds.clear();
for (const controller of this.localControllers) {
controller.abort();
}
this.localControllers.clear();
}
}
// ─── NodeRuntime ─────────────────────────────────────────────────
interface NodeRuntime {
nodeId: string;
nodeType: string;
config: Record<string, any>;
instance: any | null;
context: NodeContext;
}
// ─── Error ───────────────────────────────────────────────────────
export class WorkflowCycleError extends Error {
cycleNodes: string[];
constructor(cycleNodes: string[]) {
super(`Cycle detected in workflow: ${cycleNodes.join(", ")}`);
this.name = "WorkflowCycleError";
this.cycleNodes = cycleNodes;
}
}
// ─── Global Settings ─────────────────────────────────────────────
/**
* Maps node types to their config fields and corresponding global setting keys.
* When a node's config field is empty, the global setting value is used instead.
*/
const GLOBAL_SETTINGS_MAP: Record<string, Record<string, string>> = {
"openai-llm": { apiKey: "openai.apiKey", model: "openai.model" },
"anthropic-llm": { apiKey: "anthropic.apiKey", model: "anthropic.model" },
"google-llm": { apiKey: "google.apiKey", model: "google.model" },
"ollama-llm": { host: "ollama.host", model: "ollama.model" },
"voicevox-tts": { host: "voicevox.host" },
"coeiroink-tts": { host: "coeiroink.host" },
"sbv2-tts": { host: "sbv2.host" },
};
function loadGlobalSettings(): Record<string, string> {
const rows = db.select().from(globalSettings).all();
const result: Record<string, string> = {};
for (const row of rows) {
result[row.key] = row.value;
}
return result;
}
function mergeGlobalSettings(
nodeType: string,
config: Record<string, unknown>,
settings: Record<string, string>,
): Record<string, unknown> {
const mapping = GLOBAL_SETTINGS_MAP[nodeType];
if (!mapping) return config;
const merged = { ...config };
for (const [configField, settingsKey] of Object.entries(mapping)) {
const currentValue = merged[configField];
if (
(currentValue === undefined || currentValue === null || currentValue === "") &&
settings[settingsKey]
) {
merged[configField] = settings[settingsKey];
}
}
return merged;
}
// ─── Executor ────────────────────────────────────────────────────
export class WorkflowExecutor {
private runningWorkflows = new Map<string, Record<string, unknown>>();
private eventBuses = new Map<string, EventBus>();
private logCallbacks = new Map<string, LogCallback>();
private eventCallbacks = new Map<string, EventCallback>();
private statusCallbacks = new Map<string, StatusCallback>();
private nodeInstances = new Map<string, Map<string, NodeRuntime>>();
private eventQueues = new Map<string, EventQueue>();
private sourceNodes = new Map<string, Map<string, unknown>>();
private queueProcessors = new Map<string, AbortController>();
private taskRegistries = new Map<string, TaskRegistry>();
private vtsWorkflows = new Set<string>();
private workflowLocks = new Map<string, Promise<void>>();
// ─── Callbacks ────────────────────
setLogCallback(workflowId: string, callback: LogCallback): void {
this.logCallbacks.set(workflowId, callback);
}
setEventCallback(workflowId: string, callback: EventCallback): void {
this.eventCallbacks.set(workflowId, callback);
}
setStatusCallback(workflowId: string, callback: StatusCallback): void {
this.statusCallbacks.set(workflowId, callback);
}
clearCallbacks(workflowId: string): void {
this.logCallbacks.delete(workflowId);
this.eventCallbacks.delete(workflowId);
this.statusCallbacks.delete(workflowId);
}
// ─── Status ───────────────────────
getRunningWorkflowIds(): string[] {
return [...this.runningWorkflows.keys()];
}
getStatus(workflowId: string): Record<string, unknown> {
const status = this.runningWorkflows.get(workflowId);
if (!status) return { status: "idle" };
const result = { ...status };
const queue = this.eventQueues.get(workflowId);
if (queue) {
result.queue_size = queue.qsize;
result.queue_processing = queue.isProcessing;
result.queue_dropped = queue.droppedCount;
}
return result;
}
// ─── VTube Studio ─────────────────
private async setupVtsIfNeeded(workflowId: string, workflowData: WorkflowData): Promise<void> {
const nodes = workflowData.nodes;
const avatarConfig = nodes.find((n) => n.type === "avatar-configuration")?.config;
if (!avatarConfig) return;
if (avatarConfig.renderer !== "vtube-studio") return;
const port = (avatarConfig.vtube_port as number) ?? 8001;
const mouthParam = (avatarConfig.vtube_mouth_param as string) ?? "MouthOpen";
let expressionMap: Record<string, string> | undefined;
const rawMap = avatarConfig.vtube_expression_map;
if (rawMap) {
try {
expressionMap = typeof rawMap === "string" ? JSON.parse(rawMap) : rawMap;
} catch {
console.warn("Failed to parse VTS expression map");
}
}
vtsClient.configure(port, mouthParam, expressionMap);
console.log(`Workflow ${workflowId} uses VTube Studio mode, connecting...`);
const success = await vtsClient.connect();
if (success) {
this.vtsWorkflows.add(workflowId);
console.log(`VTube Studio connected for workflow ${workflowId}`);
} else {
console.warn(`Failed to connect to VTube Studio for workflow ${workflowId}`);
}
}
private async disconnectVtsIfNeeded(workflowId: string): Promise<void> {
if (this.vtsWorkflows.has(workflowId)) {
this.vtsWorkflows.delete(workflowId);
if (this.vtsWorkflows.size === 0) {
await vtsClient.disconnect();
console.log("VTube Studio disconnected (no active VTS workflows)");
}
}
}
// ─── Node Context Creation ────────
private createNodeContext(
workflowId: string,
nodeId: string,
character: Record<string, any>,
): NodeContext {
return new NodeContext({
workflowId,
nodeId,
character,
eventBus: this.eventBuses.get(workflowId),
logCallback: (nid, msg, lvl) => this.log(workflowId, nid, msg, lvl),
taskRegistry: this.taskRegistries.get(workflowId),
});
}
// ─── Node Lifecycle ───────────────
private async initializeNodes(
workflowId: string,
nodes: NodeData[],
character: Record<string, any>,
settings: Record<string, string>,
): Promise<void> {
const runtimes = new Map<string, NodeRuntime>();
this.nodeInstances.set(workflowId, runtimes);
for (const node of nodes) {
const context = this.createNodeContext(workflowId, node.id, character);
const instance = await loadPlugin(node.type);
const mergedConfig = mergeGlobalSettings(node.type, node.config ?? {}, settings);
const runtime: NodeRuntime = {
nodeId: node.id,
nodeType: node.type,
config: mergedConfig,
instance,
context,
};
runtimes.set(node.id, runtime);
const pluginInstance = instance as Record<string, any> | null;
if (pluginInstance?.setup) {
try {
await pluginInstance.setup(mergedConfig, context);
} catch (err) {
await this.log(workflowId, node.id, `Node setup error: ${err}`, "error");
}
}
}
}
private getNodeRuntime(workflowId: string, nodeId: string): NodeRuntime | undefined {
return this.nodeInstances.get(workflowId)?.get(nodeId);
}
private async executeNodeRuntime(
runtime: NodeRuntime,
inputs: Record<string, any>,
): Promise<Record<string, any>> {
if (runtime.instance) {
return await runtime.instance.execute(inputs, runtime.context);
}
return await this.executeBuiltinNode(runtime.nodeType, runtime.config, inputs, runtime.context);
}
private async teardownNodes(workflowId: string): Promise<void> {
const runtimes = this.nodeInstances.get(workflowId);
if (!runtimes) return;
this.nodeInstances.delete(workflowId);
for (const runtime of runtimes.values()) {
if (runtime.instance?.teardown) {
try {
await runtime.instance.teardown();
} catch (err) {
console.error(`Error tearing down node ${runtime.nodeId}:`, err);
}
}
}
}
// ─── Event Filter Check ───────────
private nodeAcceptsEvent(node: NodeData, event: Event): boolean {
const filters = node.eventFilters ?? node.event_filters;
if (!filters || filters.length === 0) return true;
for (const filterDef of filters) {
if (!filterDef.event) continue;
const ef = new EventFilter(filterDef.event, filterDef.condition);
if (ef.matches(event)) return true;
}
return false;
}
// ─── Workflow Lock ──────────────
private async withWorkflowLock<T>(workflowId: string, fn: () => Promise<T>): Promise<T> {
// Wait for any existing operation on this workflow to complete
while (this.workflowLocks.has(workflowId)) {
await this.workflowLocks.get(workflowId);
}
let resolve: () => void;
const lock = new Promise<void>((r) => {
resolve = r;
});
this.workflowLocks.set(workflowId, lock);
try {
return await fn();
} finally {
this.workflowLocks.delete(workflowId);
resolve!();
}
}
// ─── Workflow Start/Stop ──────────
async startWorkflow(
workflowId: string,
workflowData: WorkflowData,
startNodeId?: string | null,
): Promise<void> {
await this.withWorkflowLock(workflowId, async () => {
if (this.runningWorkflows.has(workflowId)) {
console.log(`Workflow ${workflowId} is already running, restarting...`);
// Preserve callbacks before stop clears them so event forwarding
// continues after restart without requiring the WS client to re-join
const savedEventCallback = this.eventCallbacks.get(workflowId);
const savedLogCallback = this.logCallbacks.get(workflowId);
const savedStatusCallback = this.statusCallbacks.get(workflowId);
await this.stopWorkflowInternal(workflowId);
if (savedEventCallback) this.eventCallbacks.set(workflowId, savedEventCallback);
if (savedLogCallback) this.logCallbacks.set(workflowId, savedLogCallback);
if (savedStatusCallback) this.statusCallbacks.set(workflowId, savedStatusCallback);
}
// Create event bus
const eventBus = new EventBus();
await eventBus.start();
this.eventBuses.set(workflowId, eventBus);
// Create task registry
this.taskRegistries.set(workflowId, new TaskRegistry());
// Create event queue
this.eventQueues.set(workflowId, new EventQueue(100));
// Check VTube Studio
await this.setupVtsIfNeeded(workflowId, workflowData);
// Subscribe to events and forward
const eventCallback = this.eventCallbacks.get(workflowId);
if (eventCallback) {
const forwardEvent = async (event: Event) => {
if (
event.type.startsWith("audio.") ||
event.type.startsWith("avatar.") ||
event.type === "subtitle"
) {
await eventCallback(event);
}
// VTube Studio forwarding
if (this.vtsWorkflows.has(workflowId) && vtsClient.isConnected) {
if (event.type === "avatar.mouth") {
const value = (event.payload.value as number) ?? 0;
await vtsClient.setMouthOpen(value);
} else if (event.type === "avatar.expression") {
const expression = event.payload.expression as string;
if (expression) {
await vtsClient.triggerExpression(expression);
}
}
}
};
eventBus.subscribe("audio.*", forwardEvent);
eventBus.subscribe("avatar.*", forwardEvent);
eventBus.subscribe("subtitle", forwardEvent);
}
// Filter subgraph if start node specified
let data = workflowData;
if (startNodeId) {
data = this.filterSubgraph(workflowData, startNodeId);
console.log(`Filtered workflow to subgraph starting from node: ${startNodeId}`);
}
// Track running state
this.runningWorkflows.set(workflowId, {
status: "running",
started_at: new Date(),
workflow_data: data,
});
// Start execution in background (non-blocking). Errors here reach the
// top-level promise; record them on the workflow status so the HTTP
// /status endpoint and WebSocket clients can observe the failure.
this.executeWorkflow(workflowId, data).catch((err) => {
console.error("Workflow execution error:", err);
const status = this.runningWorkflows.get(workflowId);
if (status) {
status.status = "error";
status.error = err instanceof Error ? err.message : String(err);
}
});
console.log(`Started workflow: ${workflowId}`);
});
}
async stopWorkflow(workflowId: string): Promise<void> {
await this.withWorkflowLock(workflowId, async () => {
await this.stopWorkflowInternal(workflowId);
});
}
private async stopWorkflowInternal(workflowId: string): Promise<void> {
if (!this.runningWorkflows.has(workflowId)) return;
// Stop queue processor
const qpController = this.queueProcessors.get(workflowId);
if (qpController) {
qpController.abort();
this.queueProcessors.delete(workflowId);
}
// Teardown nodes
await this.teardownNodes(workflowId);
// Clean up source nodes
this.sourceNodes.delete(workflowId);
// Stop event bus
const eventBus = this.eventBuses.get(workflowId);
if (eventBus) {
await eventBus.stop();
this.eventBuses.delete(workflowId);
}
// Clean up event queue
this.eventQueues.delete(workflowId);
// Cancel and clean up background tasks
const registry = this.taskRegistries.get(workflowId);
if (registry) {
registry.cancelAll();
this.taskRegistries.delete(workflowId);
}
// Disconnect VTS
await this.disconnectVtsIfNeeded(workflowId);
// Clean up callbacks
this.clearCallbacks(workflowId);
// Update status
this.runningWorkflows.delete(workflowId);
console.log(`Stopped workflow: ${workflowId}`);
}
// ─── Main Execution ───────────────
private async executeWorkflow(workflowId: string, workflowData: WorkflowData): Promise<void> {
try {
const { nodes, connections, character } = workflowData;
if (!nodes?.length) {
console.warn(`No nodes in workflow ${workflowId}`);
return;
}
const settings = loadGlobalSettings();
await this.initializeNodes(workflowId, nodes, character, settings);
const sourceNodes = nodes.filter((n) => SOURCE_NODE_TYPES.has(n.type));
const regularNodes = nodes.filter((n) => !SOURCE_NODE_TYPES.has(n.type));
const adjacency = this.buildAdjacency(nodes, connections);
const hasStartNode = nodes.some((n) => n.type === "start");
const hasSourceNodes = sourceNodes.length > 0;
if (hasSourceNodes) {
await this.log(
workflowId,
null,
`Event-driven workflow: ${sourceNodes.length} source node(s), ${regularNodes.length} regular node(s)`,
"info",
);
await this.runEventDriven(
workflowId,
nodes,
connections,
character,
sourceNodes,
adjacency,
);
} else if (hasStartNode) {
await this.log(workflowId, null, `Linear workflow (${nodes.length} nodes)`, "info");
await this.runLinear(workflowId, nodes, connections, character);
} else {
await this.log(workflowId, null, `Workflow started (${nodes.length} nodes)`, "info");
await this.runLinear(workflowId, nodes, connections, character);
}
} catch (err) {
console.error("Workflow execution error:", err);
const status = this.runningWorkflows.get(workflowId);
if (status) {
status.status = "error";
status.error = String(err);
}
}
}
// ─── Event-Driven Mode ────────────
private async runEventDriven(
workflowId: string,
nodes: NodeData[],
connections: ConnectionData[],
character: Record<string, any>,
sourceNodes: NodeData[],
adjacency: Map<string, string[]>,
): Promise<void> {
const sources = new Map<string, any>();
this.sourceNodes.set(workflowId, sources);
// Start source nodes
for (const node of sourceNodes) {
const runtime = this.getNodeRuntime(workflowId, node.id);
if (!runtime?.instance) {
await this.log(workflowId, node.id, `Failed to load source node: ${node.type}`, "error");
await this.updateNodeStatus(workflowId, node.id, "error", {
error: "Plugin not found",
});
continue;
}
sources.set(node.id, {
instance: runtime.instance,
node,
context: runtime.context,
});
await this.updateNodeStatus(workflowId, node.id, "listening");
await this.log(workflowId, node.id, `Source node started: ${node.type}`, "info");
}
// Subscribe to source events
const eventBus = this.eventBuses.get(workflowId);
if (eventBus) {
const onSourceEvent = async (event: Event) => {
const queue = this.eventQueues.get(workflowId);
if (queue) {
const added = queue.put({
event,
source_node_id: event.sourceNodeId,
});
if (!added) {
await this.log(workflowId, null, "Event queue full, dropping event", "warning");
}
}
};
eventBus.subscribe("message.*", onSourceEvent);
eventBus.subscribe("timer.*", onSourceEvent);
}
// Start queue processor
const controller = new AbortController();
this.queueProcessors.set(workflowId, controller);
this.processEventQueue(
workflowId,
nodes,
connections,
character,
adjacency,
controller.signal,
).catch(() => {
// Cancelled or errored - handled internally
});
await this.log(workflowId, null, "Listening for events...", "info");
}
private async processEventQueue(
workflowId: string,
nodes: NodeData[],
connections: ConnectionData[],
character: Record<string, any>,
adjacency: Map<string, string[]>,
signal: AbortSignal,
): Promise<void> {
const queue = this.eventQueues.get(workflowId);
if (!queue) return;
while (this.runningWorkflows.has(workflowId) && !signal.aborted) {
try {
const eventData = await queue.get(1000);
if (!eventData || signal.aborted) continue;
queue.processing = true;
const event: Event = (eventData as any).event;
const sourceNodeId: string = (eventData as any).source_node_id;
await this.log(workflowId, sourceNodeId, `Processing event: ${event.type}`, "info");
const downstreamIds = this.getDownstreamNodes(sourceNodeId, adjacency);
if (downstreamIds.length === 0) {
await this.log(workflowId, sourceNodeId, "No downstream nodes connected", "warning");
queue.processing = false;
continue;
}
// Node outputs tracking
const nodeOutputs = new Map<string, Record<string, any>>();
nodeOutputs.set(sourceNodeId, event.payload);
// Inbound connection counts
const inboundCounts = new Map<string, number>();
for (const conn of connections) {
const toId = conn.to.nodeId;
inboundCounts.set(toId, (inboundCounts.get(toId) ?? 0) + 1);
}
// Execute downstream in order
const executionOrder = this.getExecutionOrderFrom(
sourceNodeId,
nodes,
connections,
adjacency,
);
for (const node of executionOrder) {
if (!this.runningWorkflows.has(workflowId) || signal.aborted) break;
if (SOURCE_NODE_TYPES.has(node.type)) continue;
if (!this.nodeAcceptsEvent(node, event)) continue;
const inputs = this.getNodeInputs(node.id, connections, nodeOutputs);
if (Object.keys(inputs).length === 0 && (inboundCounts.get(node.id) ?? 0) > 0) continue;
const runtime = this.getNodeRuntime(workflowId, node.id);
if (!runtime) {
await this.log(workflowId, node.id, `Node runtime missing: ${node.type}`, "warning");
continue;
}
await this.updateNodeStatus(workflowId, node.id, "running");
try {
const outputs = await this.executeNodeRuntime(runtime, inputs);
nodeOutputs.set(node.id, outputs ?? {});
await this.updateNodeStatus(workflowId, node.id, "completed", { outputs });
} catch (err) {
await this.updateNodeStatus(workflowId, node.id, "error", { error: String(err) });
await this.log(workflowId, node.id, `Node error: ${err}`, "error");
}
}
queue.processing = false;
} catch (err) {
if ((err as Error)?.name === "AbortError") break;
if (err instanceof WorkflowCycleError) {
const errorMsg = `ワークフローに循環参照が検出されました / Cycle detected: ${err.cycleNodes.join(", ")}`;
await this.log(workflowId, null, errorMsg, "error");
console.error(`Cycle detected in workflow ${workflowId}:`, err.cycleNodes);
queue.processing = false;
const status = this.runningWorkflows.get(workflowId);
if (status) {
status.status = "error";
status.error = errorMsg;
}
// Cleanup
this.queueProcessors.delete(workflowId);
const registry = this.taskRegistries.get(workflowId);
if (registry) registry.cancelAll();
await this.teardownNodes(workflowId);
this.sourceNodes.delete(workflowId);
const eventBus = this.eventBuses.get(workflowId);
if (eventBus) {
await eventBus.stop();
this.eventBuses.delete(workflowId);
}
this.eventQueues.delete(workflowId);
await this.disconnectVtsIfNeeded(workflowId);
break;
}
console.error("Queue processor error:", err);
queue.processing = false;
}
}
}
// ─── Linear Mode ──────────────────
private async runLinear(
workflowId: string,
nodes: NodeData[],
connections: ConnectionData[],
character: Record<string, any>,
): Promise<void> {
let executionOrder: NodeData[];
try {
executionOrder = this.getExecutionOrder(nodes, connections);
} catch (err) {
if (err instanceof WorkflowCycleError) {
const msg = `ワークフローに循環参照が検出されました / Cycle detected: ${err.cycleNodes.join(", ")}`;
await this.log(workflowId, null, msg, "error");
}
throw err;
}
if (executionOrder.length === 0) {
await this.log(workflowId, null, "No executable nodes found", "warning");
return;
}
const nodeOutputs = new Map<string, Record<string, any>>();
for (const node of executionOrder) {
if (!this.runningWorkflows.has(workflowId)) break;
const inputs = this.getNodeInputs(node.id, connections, nodeOutputs);
const runtime = this.getNodeRuntime(workflowId, node.id);
if (!runtime) {
await this.log(workflowId, node.id, `Node runtime missing: ${node.type}`, "warning");
continue;
}
await this.log(workflowId, node.id, `Executing node: ${node.type}`, "info");
await this.updateNodeStatus(workflowId, node.id, "running");
try {
const outputs = await this.executeNodeRuntime(runtime, inputs);
nodeOutputs.set(node.id, outputs ?? {});
await this.updateNodeStatus(workflowId, node.id, "completed", {
outputs,
});
await this.log(workflowId, node.id, `Node completed: ${node.type}`, "info");
} catch (err) {
await this.updateNodeStatus(workflowId, node.id, "error", {
error: String(err),
});
await this.log(workflowId, node.id, `Node error: ${err}`, "error");
throw err;
}
}
await this.log(workflowId, null, "Workflow execution completed", "info");
const status = this.runningWorkflows.get(workflowId);
if (status) {
status.status = "completed";
}
await this.cleanupCompletedWorkflow(workflowId);
}
// ─── Cleanup ──────────────────────
private async cleanupCompletedWorkflow(workflowId: string): Promise<void> {
// Wait for background tasks
const registry = this.taskRegistries.get(workflowId);
if (registry) {
registry.cancelAll();
let awaitTimedOut = false;
await Promise.race([
registry.awaitAll(),
new Promise<void>((resolve) => {
setTimeout(() => {
awaitTimedOut = true;
resolve();
}, 30_000);
}),
]);
if (awaitTimedOut) {
console.warn(`Timed out waiting for background tasks to finish for workflow ${workflowId}`);
}
this.taskRegistries.delete(workflowId);
}
// Stop queue processor
const qpController = this.queueProcessors.get(workflowId);
if (qpController) {
qpController.abort();
this.queueProcessors.delete(workflowId);
}
// Teardown nodes
await this.teardownNodes(workflowId);
// Clean up source nodes
this.sourceNodes.delete(workflowId);
// Stop event bus
const eventBus = this.eventBuses.get(workflowId);
if (eventBus) {
await eventBus.stop();
this.eventBuses.delete(workflowId);
}
// Clean up event queue
this.eventQueues.delete(workflowId);
// Completed workflows should not stay in memory.
this.runningWorkflows.delete(workflowId);
}
// ─── Graph Algorithms ─────────────
private filterSubgraph(workflowData: WorkflowData, startNodeId: string): WorkflowData {
const { nodes, connections } = workflowData;
const adjacency = new Map<string, string[]>();
for (const node of nodes) adjacency.set(node.id, []);
for (const conn of connections) {
const fromId = conn.from.nodeId;
const toId = conn.to.nodeId;
if (adjacency.has(fromId)) {
adjacency.get(fromId)?.push(toId);
}
}
// BFS to find reachable nodes
const reachable = new Set<string>([startNodeId]);
const queue = [startNodeId];
while (queue.length > 0) {
const current = queue.shift();
if (current === undefined) break;
for (const neighbor of adjacency.get(current) ?? []) {
if (!reachable.has(neighbor)) {
reachable.add(neighbor);
queue.push(neighbor);
}
}
}