import type { WorkflowPlugin } from '../types' type MiddlewareHook = Exclude< keyof WorkflowPlugin, 'name' | 'afterWrite' | 'onError' | 'onPartialSuccess' > type ObserverHook = 'afterWrite' | 'onError' | 'onPartialSuccess' export class PluginBus { private globals: WorkflowPlugin[] = [] addGlobal(plugin: WorkflowPlugin): void { this.globals.push(plugin) } async run( hook: MiddlewareHook, perKey: WorkflowPlugin[], ctx: T, ): Promise { let current = ctx for (const plugin of [...this.globals, ...perKey]) { const fn = plugin[hook] as ((c: T) => Promise) | undefined if (fn) { current = await fn.call(plugin, current) } } return current } async runObserver( hook: ObserverHook, perKey: WorkflowPlugin[], ctx: T, ): Promise { for (const plugin of [...this.globals, ...perKey]) { const fn = plugin[hook] as ((c: T) => Promise) | undefined if (fn) { await fn.call(plugin, ctx) } } } }