nvent 0.5.15 → 1.0.0-alpha.2
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/module.d.mts +181 -8
- package/dist/module.json +1 -1
- package/dist/module.mjs +1257 -1196
- package/dist/runtime/app/composables/useFunctionCall.d.ts +17 -0
- package/dist/runtime/app/composables/useFunctionCall.js +25 -0
- package/dist/runtime/app/composables/useNventStream.d.ts +23 -0
- package/dist/runtime/app/composables/useNventStream.js +58 -0
- package/dist/runtime/nitro/plugins/00.iii-lifecycle.d.ts +19 -0
- package/dist/runtime/nitro/plugins/00.iii-lifecycle.js +62 -0
- package/dist/runtime/nitro/plugins/01.iii-worker.d.ts +15 -0
- package/dist/runtime/nitro/plugins/01.iii-worker.js +91 -0
- package/dist/runtime/nitro/routes/stream-proxy.d.ts +17 -0
- package/dist/runtime/nitro/routes/stream-proxy.js +45 -0
- package/dist/runtime/nitro/utils/console.d.ts +32 -0
- package/dist/runtime/nitro/utils/console.js +95 -0
- package/dist/runtime/nitro/utils/defineFunction.d.ts +378 -3
- package/dist/runtime/nitro/utils/defineFunction.js +224 -4
- package/dist/runtime/nitro/utils/engine.d.ts +25 -0
- package/dist/runtime/nitro/utils/engine.js +130 -0
- package/dist/runtime/nitro/utils/nventDir.d.ts +1 -0
- package/dist/runtime/nitro/utils/nventDir.js +20 -0
- package/dist/runtime/nitro/utils/useIii.d.ts +23 -0
- package/dist/runtime/nitro/utils/useIii.js +14 -0
- package/dist/runtime/nitro/utils/workers/node.d.ts +24 -0
- package/dist/runtime/nitro/utils/workers/node.js +57 -0
- package/dist/runtime/nitro/utils/workers/python.d.ts +67 -0
- package/dist/runtime/nitro/utils/workers/python.js +220 -0
- package/dist/runtime/python/nvent.py +208 -0
- package/dist/runtime/python/worker_runtime.py +659 -0
- package/dist/runtime/virtual.d.ts +17 -0
- package/dist/types.d.mts +5 -1
- package/package.json +11 -14
- package/dist/runtime/adapters/base/index.d.ts +0 -6
- package/dist/runtime/adapters/base/index.js +0 -1
- package/dist/runtime/adapters/base/store-validator.d.ts +0 -48
- package/dist/runtime/adapters/base/store-validator.js +0 -147
- package/dist/runtime/adapters/builtin/file-queue.d.ts +0 -67
- package/dist/runtime/adapters/builtin/file-queue.js +0 -499
- package/dist/runtime/adapters/builtin/file-store.d.ts +0 -32
- package/dist/runtime/adapters/builtin/file-store.js +0 -206
- package/dist/runtime/adapters/builtin/file-stream.d.ts +0 -39
- package/dist/runtime/adapters/builtin/file-stream.js +0 -56
- package/dist/runtime/adapters/builtin/index.d.ts +0 -10
- package/dist/runtime/adapters/builtin/index.js +0 -5
- package/dist/runtime/adapters/builtin/memory-queue.d.ts +0 -52
- package/dist/runtime/adapters/builtin/memory-queue.js +0 -243
- package/dist/runtime/adapters/builtin/memory-store.d.ts +0 -69
- package/dist/runtime/adapters/builtin/memory-store.js +0 -357
- package/dist/runtime/adapters/builtin/memory-stream.d.ts +0 -21
- package/dist/runtime/adapters/builtin/memory-stream.js +0 -56
- package/dist/runtime/adapters/factory.d.ts +0 -31
- package/dist/runtime/adapters/factory.js +0 -135
- package/dist/runtime/adapters/index.d.ts +0 -8
- package/dist/runtime/adapters/index.js +0 -3
- package/dist/runtime/adapters/interfaces/index.d.ts +0 -11
- package/dist/runtime/adapters/interfaces/index.js +0 -3
- package/dist/runtime/adapters/interfaces/queue.d.ts +0 -155
- package/dist/runtime/adapters/interfaces/queue.js +0 -0
- package/dist/runtime/adapters/interfaces/store.d.ts +0 -299
- package/dist/runtime/adapters/interfaces/store.js +0 -0
- package/dist/runtime/adapters/interfaces/stream.d.ts +0 -62
- package/dist/runtime/adapters/interfaces/stream.js +0 -0
- package/dist/runtime/adapters/registry.d.ts +0 -85
- package/dist/runtime/adapters/registry.js +0 -161
- package/dist/runtime/config/index.d.ts +0 -23
- package/dist/runtime/config/index.js +0 -200
- package/dist/runtime/config/types.d.ts +0 -439
- package/dist/runtime/config/types.js +0 -0
- package/dist/runtime/events/eventBus.d.ts +0 -20
- package/dist/runtime/events/eventBus.js +0 -35
- package/dist/runtime/events/types.d.ts +0 -144
- package/dist/runtime/events/types.js +0 -0
- package/dist/runtime/events/utils/scheduleTrigger.d.ts +0 -8
- package/dist/runtime/events/utils/scheduleTrigger.js +0 -71
- package/dist/runtime/events/utils/stallDetector.d.ts +0 -83
- package/dist/runtime/events/utils/stallDetector.js +0 -318
- package/dist/runtime/events/utils/triggerRuntime.d.ts +0 -58
- package/dist/runtime/events/utils/triggerRuntime.js +0 -212
- package/dist/runtime/events/wiring/flowWiring.d.ts +0 -34
- package/dist/runtime/events/wiring/flowWiring.js +0 -1258
- package/dist/runtime/events/wiring/registry.d.ts +0 -19
- package/dist/runtime/events/wiring/registry.js +0 -43
- package/dist/runtime/events/wiring/stateWiring.d.ts +0 -37
- package/dist/runtime/events/wiring/stateWiring.js +0 -92
- package/dist/runtime/events/wiring/streamWiring.d.ts +0 -36
- package/dist/runtime/events/wiring/streamWiring.js +0 -156
- package/dist/runtime/events/wiring/triggerWiring.d.ts +0 -21
- package/dist/runtime/events/wiring/triggerWiring.js +0 -422
- package/dist/runtime/nitro/plugins/00.adapters.d.ts +0 -14
- package/dist/runtime/nitro/plugins/00.adapters.js +0 -73
- package/dist/runtime/nitro/plugins/01.ws-lifecycle.d.ts +0 -5
- package/dist/runtime/nitro/plugins/01.ws-lifecycle.js +0 -69
- package/dist/runtime/nitro/plugins/02.workers.d.ts +0 -2
- package/dist/runtime/nitro/plugins/02.workers.js +0 -92
- package/dist/runtime/nitro/plugins/03.triggers.d.ts +0 -12
- package/dist/runtime/nitro/plugins/03.triggers.js +0 -55
- package/dist/runtime/nitro/routes/webhook.await.d.ts +0 -23
- package/dist/runtime/nitro/routes/webhook.await.js +0 -112
- package/dist/runtime/nitro/routes/webhook.trigger.d.ts +0 -86
- package/dist/runtime/nitro/routes/webhook.trigger.js +0 -73
- package/dist/runtime/nitro/utils/adapters.d.ts +0 -66
- package/dist/runtime/nitro/utils/adapters.js +0 -51
- package/dist/runtime/nitro/utils/awaitPatterns/event.d.ts +0 -15
- package/dist/runtime/nitro/utils/awaitPatterns/event.js +0 -128
- package/dist/runtime/nitro/utils/awaitPatterns/index.d.ts +0 -28
- package/dist/runtime/nitro/utils/awaitPatterns/index.js +0 -55
- package/dist/runtime/nitro/utils/awaitPatterns/schedule.d.ts +0 -16
- package/dist/runtime/nitro/utils/awaitPatterns/schedule.js +0 -83
- package/dist/runtime/nitro/utils/awaitPatterns/time.d.ts +0 -15
- package/dist/runtime/nitro/utils/awaitPatterns/time.js +0 -71
- package/dist/runtime/nitro/utils/awaitPatterns/webhook.d.ts +0 -15
- package/dist/runtime/nitro/utils/awaitPatterns/webhook.js +0 -128
- package/dist/runtime/nitro/utils/defineFunctionConfig.d.ts +0 -378
- package/dist/runtime/nitro/utils/defineFunctionConfig.js +0 -3
- package/dist/runtime/nitro/utils/defineHooks.d.ts +0 -95
- package/dist/runtime/nitro/utils/defineHooks.js +0 -9
- package/dist/runtime/nitro/utils/registerAdapter.d.ts +0 -59
- package/dist/runtime/nitro/utils/registerAdapter.js +0 -13
- package/dist/runtime/nitro/utils/useAwait.d.ts +0 -83
- package/dist/runtime/nitro/utils/useAwait.js +0 -169
- package/dist/runtime/nitro/utils/useEventManager.d.ts +0 -15
- package/dist/runtime/nitro/utils/useEventManager.js +0 -26
- package/dist/runtime/nitro/utils/useFlow.d.ts +0 -66
- package/dist/runtime/nitro/utils/useFlow.js +0 -310
- package/dist/runtime/nitro/utils/useHookRegistry.d.ts +0 -40
- package/dist/runtime/nitro/utils/useHookRegistry.js +0 -25
- package/dist/runtime/nitro/utils/useNventLogger.d.ts +0 -42
- package/dist/runtime/nitro/utils/useNventLogger.js +0 -54
- package/dist/runtime/nitro/utils/useRunContext.d.ts +0 -6
- package/dist/runtime/nitro/utils/useRunContext.js +0 -102
- package/dist/runtime/nitro/utils/useStreamTopics.d.ts +0 -83
- package/dist/runtime/nitro/utils/useStreamTopics.js +0 -94
- package/dist/runtime/nitro/utils/useTrigger.d.ts +0 -150
- package/dist/runtime/nitro/utils/useTrigger.js +0 -311
- package/dist/runtime/nitro/utils/wsPeerManager.d.ts +0 -44
- package/dist/runtime/nitro/utils/wsPeerManager.js +0 -32
- package/dist/runtime/scheduler/index.d.ts +0 -33
- package/dist/runtime/scheduler/index.js +0 -42
- package/dist/runtime/scheduler/scheduler.d.ts +0 -132
- package/dist/runtime/scheduler/scheduler.js +0 -799
- package/dist/runtime/scheduler/types.d.ts +0 -122
- package/dist/runtime/scheduler/types.js +0 -0
- package/dist/runtime/worker/node/runner.d.ts +0 -95
- package/dist/runtime/worker/node/runner.js +0 -272
- package/dist/runtime/worker/python/get_config.py +0 -64
- package/dist/runtime/worker/system/awaitHandlers.d.ts +0 -27
- package/dist/runtime/worker/system/awaitHandlers.js +0 -230
- package/dist/runtime/worker/system/index.d.ts +0 -24
- package/dist/runtime/worker/system/index.js +0 -39
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
registerAwaitPattern,
|
|
3
|
-
resolveAwaitPattern,
|
|
4
|
-
registerWebhookAwait,
|
|
5
|
-
resolveWebhookAwait,
|
|
6
|
-
registerEventAwait,
|
|
7
|
-
resolveEventAwait,
|
|
8
|
-
registerScheduleAwait,
|
|
9
|
-
resolveScheduleAwait,
|
|
10
|
-
registerTimeAwait,
|
|
11
|
-
resolveTimeAwait
|
|
12
|
-
} from "./awaitPatterns/index.js";
|
|
13
|
-
import { useStoreAdapter, useStreamTopics, useNventLogger, useRuntimeConfig } from "#imports";
|
|
14
|
-
export function useAwaitDefaults() {
|
|
15
|
-
try {
|
|
16
|
-
const config = useRuntimeConfig();
|
|
17
|
-
const awaitDefaults = config?.nvent?.flow?.awaitDefaults;
|
|
18
|
-
return {
|
|
19
|
-
webhookTimeout: awaitDefaults?.webhookTimeout ?? 24 * 60 * 60 * 1e3,
|
|
20
|
-
// 24 hours
|
|
21
|
-
eventTimeout: awaitDefaults?.eventTimeout ?? 24 * 60 * 60 * 1e3,
|
|
22
|
-
// 24 hours
|
|
23
|
-
timeTimeout: awaitDefaults?.timeTimeout,
|
|
24
|
-
// undefined by default
|
|
25
|
-
scheduleTimeout: awaitDefaults?.scheduleTimeout,
|
|
26
|
-
// undefined by default
|
|
27
|
-
timeoutAction: awaitDefaults?.timeoutAction ?? "fail"
|
|
28
|
-
};
|
|
29
|
-
} catch {
|
|
30
|
-
return {
|
|
31
|
-
webhookTimeout: 24 * 60 * 60 * 1e3,
|
|
32
|
-
// 24 hours
|
|
33
|
-
eventTimeout: 24 * 60 * 60 * 1e3,
|
|
34
|
-
// 24 hours
|
|
35
|
-
timeTimeout: void 0,
|
|
36
|
-
scheduleTimeout: void 0,
|
|
37
|
-
timeoutAction: "fail"
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
export function useAwait() {
|
|
42
|
-
const logger = useNventLogger("await");
|
|
43
|
-
const store = useStoreAdapter();
|
|
44
|
-
const { StoreSubjects } = useStreamTopics();
|
|
45
|
-
return {
|
|
46
|
-
/**
|
|
47
|
-
* Register an await pattern based on config type
|
|
48
|
-
* Automatically routes to appropriate implementation
|
|
49
|
-
*/
|
|
50
|
-
register: registerAwaitPattern,
|
|
51
|
-
/**
|
|
52
|
-
* Resolve an await pattern by type
|
|
53
|
-
*/
|
|
54
|
-
resolve: resolveAwaitPattern,
|
|
55
|
-
/**
|
|
56
|
-
* Direct access to specific await pattern implementations
|
|
57
|
-
*/
|
|
58
|
-
webhook: {
|
|
59
|
-
register: registerWebhookAwait,
|
|
60
|
-
resolve: resolveWebhookAwait
|
|
61
|
-
},
|
|
62
|
-
event: {
|
|
63
|
-
register: registerEventAwait,
|
|
64
|
-
resolve: resolveEventAwait
|
|
65
|
-
},
|
|
66
|
-
schedule: {
|
|
67
|
-
register: registerScheduleAwait,
|
|
68
|
-
resolve: resolveScheduleAwait
|
|
69
|
-
},
|
|
70
|
-
time: {
|
|
71
|
-
register: registerTimeAwait,
|
|
72
|
-
resolve: resolveTimeAwait
|
|
73
|
-
},
|
|
74
|
-
/**
|
|
75
|
-
* Query methods for await state
|
|
76
|
-
*/
|
|
77
|
-
/**
|
|
78
|
-
* Get await state for a specific flow run and step
|
|
79
|
-
*/
|
|
80
|
-
async getAwaitState(flowName, runId, stepName) {
|
|
81
|
-
const indexKey = StoreSubjects.flowRunIndex(flowName);
|
|
82
|
-
if (!store.index.get) {
|
|
83
|
-
logger.warn("Store does not support indexGet");
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
const entry = await store.index.get(indexKey, runId);
|
|
87
|
-
if (!entry?.metadata) {
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
const awaitingSteps = entry.metadata.awaitingSteps || {};
|
|
91
|
-
if (stepName) {
|
|
92
|
-
const awaitKeyBefore = `${stepName}:before`;
|
|
93
|
-
const awaitKeyAfter = `${stepName}:after`;
|
|
94
|
-
const awaitState = awaitingSteps[awaitKeyBefore] || awaitingSteps[awaitKeyAfter] || awaitingSteps[stepName];
|
|
95
|
-
return awaitState || null;
|
|
96
|
-
}
|
|
97
|
-
return awaitingSteps;
|
|
98
|
-
},
|
|
99
|
-
/**
|
|
100
|
-
* Check if a step is currently awaiting
|
|
101
|
-
*/
|
|
102
|
-
async isAwaiting(flowName, runId, stepName) {
|
|
103
|
-
const awaitState = await this.getAwaitState(flowName, runId, stepName);
|
|
104
|
-
return awaitState?.status === "awaiting";
|
|
105
|
-
},
|
|
106
|
-
/**
|
|
107
|
-
* Get all active awaits across all flows
|
|
108
|
-
*/
|
|
109
|
-
async getAllActiveAwaits(flowName) {
|
|
110
|
-
const activeAwaits = [];
|
|
111
|
-
if (!store.index.read) {
|
|
112
|
-
logger.warn("Store does not support index read");
|
|
113
|
-
return activeAwaits;
|
|
114
|
-
}
|
|
115
|
-
if (flowName) {
|
|
116
|
-
const indexKey = StoreSubjects.flowRunIndex(flowName);
|
|
117
|
-
const entries = await store.index.read(indexKey, { limit: 1e3 });
|
|
118
|
-
for (const entry of entries) {
|
|
119
|
-
const awaitingSteps = entry.metadata?.awaitingSteps || {};
|
|
120
|
-
for (const [stepName, awaitState] of Object.entries(awaitingSteps)) {
|
|
121
|
-
if (awaitState.status === "awaiting") {
|
|
122
|
-
activeAwaits.push({
|
|
123
|
-
flowName,
|
|
124
|
-
runId: entry.id,
|
|
125
|
-
stepName,
|
|
126
|
-
awaitType: awaitState.awaitType,
|
|
127
|
-
position: awaitState.position,
|
|
128
|
-
registeredAt: awaitState.registeredAt
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
} else {
|
|
134
|
-
logger.warn("Scanning all flows not yet implemented - provide flowName parameter");
|
|
135
|
-
}
|
|
136
|
-
return activeAwaits;
|
|
137
|
-
},
|
|
138
|
-
/**
|
|
139
|
-
* Get await history for a specific flow run from stream
|
|
140
|
-
*/
|
|
141
|
-
async getAwaitHistory(runId, opts) {
|
|
142
|
-
const streamName = StoreSubjects.flowRun(runId);
|
|
143
|
-
const events = await store.stream.read(streamName, {
|
|
144
|
-
limit: opts?.limit || 100,
|
|
145
|
-
types: ["await.registered", "await.resolved", "await.timeout"],
|
|
146
|
-
order: "desc"
|
|
147
|
-
});
|
|
148
|
-
if (opts?.stepName) {
|
|
149
|
-
return events.filter((e) => e.stepName === opts.stepName);
|
|
150
|
-
}
|
|
151
|
-
return events;
|
|
152
|
-
},
|
|
153
|
-
/**
|
|
154
|
-
* Cancel/timeout an active await
|
|
155
|
-
*/
|
|
156
|
-
async timeoutAwait(flowName, runId, stepName, reason = "Manual timeout") {
|
|
157
|
-
const indexKey = StoreSubjects.flowRunIndex(flowName);
|
|
158
|
-
if (store.index.updateWithRetry) {
|
|
159
|
-
await store.index.updateWithRetry(indexKey, runId, {
|
|
160
|
-
[`awaitingSteps.${stepName}.status`]: "timeout",
|
|
161
|
-
[`awaitingSteps.${stepName}.timeoutReason`]: reason,
|
|
162
|
-
[`awaitingSteps.${stepName}.timeoutAt`]: (/* @__PURE__ */ new Date()).toISOString()
|
|
163
|
-
});
|
|
164
|
-
logger.info("Await timed out", { flowName, runId, stepName, reason });
|
|
165
|
-
}
|
|
166
|
-
logger.warn("await.timeout event publishing not yet implemented in useAwait");
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { getEventBus } from '../../events/eventBus.js';
|
|
2
|
-
import type { EventRecord } from '../../adapters/interfaces/store.js';
|
|
3
|
-
export interface EventManager {
|
|
4
|
-
/**
|
|
5
|
-
* Publish an event directly to the in-proc bus.
|
|
6
|
-
*/
|
|
7
|
-
publishBus(evt: Partial<EventRecord>): Promise<void>;
|
|
8
|
-
onType: (type: string, handler: (e: EventRecord) => void) => () => void;
|
|
9
|
-
subscribeRunId: ReturnType<typeof getEventBus>['subscribeRunId'];
|
|
10
|
-
}
|
|
11
|
-
declare global {
|
|
12
|
-
var __nq_event_manager: EventManager | undefined;
|
|
13
|
-
}
|
|
14
|
-
export declare function useEventManager(): EventManager;
|
|
15
|
-
export declare function setEventManager(mgr: EventManager): void;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { getEventBus } from "../../events/eventBus.js";
|
|
2
|
-
import { useNventLogger } from "#imports";
|
|
3
|
-
export function useEventManager() {
|
|
4
|
-
if (globalThis.__nq_event_manager) return globalThis.__nq_event_manager;
|
|
5
|
-
const logger = useNventLogger("event-manager");
|
|
6
|
-
const bus = getEventBus();
|
|
7
|
-
const publishBus = async (evt) => {
|
|
8
|
-
const rec = {
|
|
9
|
-
...evt,
|
|
10
|
-
runId: evt.runId || ""
|
|
11
|
-
};
|
|
12
|
-
bus.publish(rec);
|
|
13
|
-
logger.debug("Published event to bus", { type: rec.type, runId: rec.runId });
|
|
14
|
-
};
|
|
15
|
-
const subscribeRunId = (runId, handler) => {
|
|
16
|
-
return bus.subscribeRunId(runId, handler);
|
|
17
|
-
};
|
|
18
|
-
const onType = (type, handler) => {
|
|
19
|
-
return bus.onType(type, handler);
|
|
20
|
-
};
|
|
21
|
-
globalThis.__nq_event_manager = { publishBus, onType, subscribeRunId };
|
|
22
|
-
return globalThis.__nq_event_manager;
|
|
23
|
-
}
|
|
24
|
-
export function setEventManager(mgr) {
|
|
25
|
-
globalThis.__nq_event_manager = mgr;
|
|
26
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
export interface FlowStats {
|
|
2
|
-
name: string;
|
|
3
|
-
displayName?: string;
|
|
4
|
-
registeredAt: string;
|
|
5
|
-
lastRunAt?: string;
|
|
6
|
-
lastCompletedAt?: string;
|
|
7
|
-
stats: {
|
|
8
|
-
total: number;
|
|
9
|
-
success: number;
|
|
10
|
-
failure: number;
|
|
11
|
-
cancel: number;
|
|
12
|
-
running: number;
|
|
13
|
-
awaiting: number;
|
|
14
|
-
};
|
|
15
|
-
version?: number;
|
|
16
|
-
}
|
|
17
|
-
export interface StartFlowResult {
|
|
18
|
-
id: string;
|
|
19
|
-
queue: string;
|
|
20
|
-
step: string;
|
|
21
|
-
flowId: string;
|
|
22
|
-
}
|
|
23
|
-
export interface CancelFlowResult {
|
|
24
|
-
success: boolean;
|
|
25
|
-
runId: string;
|
|
26
|
-
flowName: string;
|
|
27
|
-
}
|
|
28
|
-
export interface RestartFlowResult {
|
|
29
|
-
success: boolean;
|
|
30
|
-
oldRunId: string;
|
|
31
|
-
newRunId: string;
|
|
32
|
-
flowName: string;
|
|
33
|
-
}
|
|
34
|
-
export interface RunningFlow {
|
|
35
|
-
id: string;
|
|
36
|
-
flowName: string;
|
|
37
|
-
status: string;
|
|
38
|
-
startedAt: string | undefined;
|
|
39
|
-
stepCount: number;
|
|
40
|
-
completedSteps: number;
|
|
41
|
-
}
|
|
42
|
-
export interface FlowComposable {
|
|
43
|
-
startFlow: (flowName: string, payload?: any) => Promise<StartFlowResult>;
|
|
44
|
-
emit: (trigger: string, payload?: any) => Promise<any[]>;
|
|
45
|
-
cancelFlow: (flowName: string, runId: string) => Promise<CancelFlowResult>;
|
|
46
|
-
restartFlow: (flowName: string, runId: string) => Promise<RestartFlowResult>;
|
|
47
|
-
isRunning: (flowName: string, runId?: string, options?: {
|
|
48
|
-
excludeRunIds?: string[];
|
|
49
|
-
}) => Promise<boolean>;
|
|
50
|
-
getRunningFlows: (flowName: string, options?: {
|
|
51
|
-
excludeRunIds?: string[];
|
|
52
|
-
}) => Promise<RunningFlow[]>;
|
|
53
|
-
getFlowStats: (flowName: string) => Promise<FlowStats | null>;
|
|
54
|
-
getAllFlowStats: (options?: {
|
|
55
|
-
sortBy?: 'registeredAt' | 'lastRunAt' | 'name';
|
|
56
|
-
order?: 'asc' | 'desc';
|
|
57
|
-
limit?: number;
|
|
58
|
-
offset?: number;
|
|
59
|
-
}) => Promise<FlowStats[]>;
|
|
60
|
-
hasFlowStats: (flowName: string) => Promise<boolean>;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Flow composable for managing flows and accessing flow statistics
|
|
64
|
-
* Provides methods for starting, canceling, and querying flows
|
|
65
|
-
*/
|
|
66
|
-
export declare function useFlow(): FlowComposable;
|
|
@@ -1,310 +0,0 @@
|
|
|
1
|
-
import { $useFunctionRegistry, useQueueAdapter, useEventManager, useStoreAdapter, useNventLogger, useStreamTopics } from "#imports";
|
|
2
|
-
import { randomUUID } from "node:crypto";
|
|
3
|
-
export function useFlow() {
|
|
4
|
-
const registry = $useFunctionRegistry();
|
|
5
|
-
const queueAdapter = useQueueAdapter();
|
|
6
|
-
const eventsManager = useEventManager();
|
|
7
|
-
const store = useStoreAdapter();
|
|
8
|
-
const logger = useNventLogger("use-flow");
|
|
9
|
-
const { StoreSubjects } = useStreamTopics();
|
|
10
|
-
return {
|
|
11
|
-
/**
|
|
12
|
-
* Start a flow with the given payload
|
|
13
|
-
*/
|
|
14
|
-
async startFlow(flowName, payload = {}) {
|
|
15
|
-
const flow = registry?.flows?.[flowName];
|
|
16
|
-
if (!flow || !flow.entry) throw new Error("Flow not found");
|
|
17
|
-
const queueName = typeof flow.entry.queue === "string" ? flow.entry.queue : flow.entry.queue?.name || flow.entry.queue;
|
|
18
|
-
const entryWorker = registry?.workers?.find(
|
|
19
|
-
(w) => w?.flow?.step === flow.entry.step && w?.queue?.name === queueName
|
|
20
|
-
);
|
|
21
|
-
const opts = entryWorker?.queue?.defaultJobOptions || {};
|
|
22
|
-
const flowId = randomUUID();
|
|
23
|
-
const id = await queueAdapter.enqueue(queueName, {
|
|
24
|
-
name: flow.entry.step,
|
|
25
|
-
data: { ...payload, flowId, flowName },
|
|
26
|
-
opts
|
|
27
|
-
});
|
|
28
|
-
try {
|
|
29
|
-
await eventsManager.publishBus({
|
|
30
|
-
type: "flow.start",
|
|
31
|
-
runId: flowId,
|
|
32
|
-
flowName,
|
|
33
|
-
data: {
|
|
34
|
-
input: payload
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
} catch {
|
|
38
|
-
}
|
|
39
|
-
return { id, queue: queueName, step: flow.entry.step, flowId };
|
|
40
|
-
},
|
|
41
|
-
/**
|
|
42
|
-
* Emit a trigger event for flow coordination
|
|
43
|
-
*/
|
|
44
|
-
async emit(trigger, payload = {}) {
|
|
45
|
-
const flowId = payload?.flowId;
|
|
46
|
-
const flowName = payload?.flowName || "unknown";
|
|
47
|
-
const stepName = payload?.stepName;
|
|
48
|
-
if (!flowId) {
|
|
49
|
-
logger.warn("emit called without flowId, trigger may not work", { trigger });
|
|
50
|
-
}
|
|
51
|
-
const { flowId: _, flowName: __, stepName: ___, ...actualPayload } = payload;
|
|
52
|
-
try {
|
|
53
|
-
await eventsManager.publishBus({
|
|
54
|
-
type: "emit",
|
|
55
|
-
runId: flowId || "unknown",
|
|
56
|
-
flowName,
|
|
57
|
-
stepName,
|
|
58
|
-
data: {
|
|
59
|
-
name: trigger,
|
|
60
|
-
payload: actualPayload
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
} catch (err) {
|
|
64
|
-
logger.error("Failed to emit trigger event", { trigger, error: err });
|
|
65
|
-
}
|
|
66
|
-
return [];
|
|
67
|
-
},
|
|
68
|
-
/**
|
|
69
|
-
* Cancel a running flow
|
|
70
|
-
*/
|
|
71
|
-
async cancelFlow(flowName, runId) {
|
|
72
|
-
try {
|
|
73
|
-
let previousStatus;
|
|
74
|
-
try {
|
|
75
|
-
const runIndexKey = StoreSubjects.flowRunIndex(flowName);
|
|
76
|
-
const entry = await store.index.get(runIndexKey, runId);
|
|
77
|
-
previousStatus = entry?.metadata?.status;
|
|
78
|
-
} catch {
|
|
79
|
-
}
|
|
80
|
-
await eventsManager.publishBus({
|
|
81
|
-
type: "flow.cancel",
|
|
82
|
-
runId,
|
|
83
|
-
flowName,
|
|
84
|
-
data: {
|
|
85
|
-
canceledAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
86
|
-
previousStatus
|
|
87
|
-
// Include for correct stats counter decrement
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
logger.info("Flow canceled", { flowName, runId, previousStatus });
|
|
91
|
-
return { success: true, runId, flowName };
|
|
92
|
-
} catch (err) {
|
|
93
|
-
logger.error("Failed to cancel flow", { flowName, runId, error: err });
|
|
94
|
-
throw err;
|
|
95
|
-
}
|
|
96
|
-
},
|
|
97
|
-
/**
|
|
98
|
-
* Restart a flow by canceling the current run and starting a new one with the same input
|
|
99
|
-
* @param flowName - The name of the flow
|
|
100
|
-
* @param runId - The run ID to restart
|
|
101
|
-
*/
|
|
102
|
-
async restartFlow(flowName, runId) {
|
|
103
|
-
try {
|
|
104
|
-
const streamName = StoreSubjects.flowRun(runId);
|
|
105
|
-
const events = await store.stream.read(streamName);
|
|
106
|
-
const startEvent = events.find((e) => e.type === "flow.start" || e.type === "flow.started");
|
|
107
|
-
const originalInput = startEvent?.data?.input || {};
|
|
108
|
-
logger.debug("Found original input for restart", { flowName, runId, hasInput: !!startEvent });
|
|
109
|
-
const runIndexKey = StoreSubjects.flowRunIndex(flowName);
|
|
110
|
-
const entry = await store.index.get(runIndexKey, runId);
|
|
111
|
-
const currentStatus = entry?.metadata?.status;
|
|
112
|
-
if (currentStatus === "running" || currentStatus === "awaiting") {
|
|
113
|
-
await eventsManager.publishBus({
|
|
114
|
-
type: "flow.cancel",
|
|
115
|
-
runId,
|
|
116
|
-
flowName,
|
|
117
|
-
data: {
|
|
118
|
-
canceledAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
119
|
-
previousStatus: currentStatus,
|
|
120
|
-
reason: "Restarted"
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
logger.info("Canceled flow for restart", { flowName, runId, previousStatus: currentStatus });
|
|
124
|
-
}
|
|
125
|
-
const newResult = await this.startFlow(flowName, originalInput);
|
|
126
|
-
logger.info("Flow restarted", {
|
|
127
|
-
flowName,
|
|
128
|
-
oldRunId: runId,
|
|
129
|
-
newRunId: newResult.flowId
|
|
130
|
-
});
|
|
131
|
-
return {
|
|
132
|
-
success: true,
|
|
133
|
-
oldRunId: runId,
|
|
134
|
-
newRunId: newResult.flowId,
|
|
135
|
-
flowName
|
|
136
|
-
};
|
|
137
|
-
} catch (err) {
|
|
138
|
-
logger.error("Failed to restart flow", { flowName, runId, error: err });
|
|
139
|
-
throw err;
|
|
140
|
-
}
|
|
141
|
-
},
|
|
142
|
-
/**
|
|
143
|
-
* Check if a flow is currently running (includes 'running' and 'awaiting' status)
|
|
144
|
-
* @param flowName - The name of the flow to check
|
|
145
|
-
* @param runId - Optional specific run ID to check (if provided, only checks that run)
|
|
146
|
-
* @param options - Optional configuration
|
|
147
|
-
* @param options.excludeRunIds - Exclude these run IDs from the check (useful when called from within a flow)
|
|
148
|
-
*/
|
|
149
|
-
async isRunning(flowName, runId, options) {
|
|
150
|
-
try {
|
|
151
|
-
if (!store.index.read) {
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
const runIndexKey = StoreSubjects.flowRunIndex(flowName);
|
|
155
|
-
const activeStatuses = ["running", "awaiting"];
|
|
156
|
-
if (runId) {
|
|
157
|
-
const run = await store.index.get(runIndexKey, runId);
|
|
158
|
-
return activeStatuses.includes(run?.metadata?.status);
|
|
159
|
-
}
|
|
160
|
-
const entries = await store.index.read(runIndexKey, {
|
|
161
|
-
limit: 1e3,
|
|
162
|
-
filter: { status: activeStatuses }
|
|
163
|
-
});
|
|
164
|
-
const excludeSet = new Set(options?.excludeRunIds || []);
|
|
165
|
-
const matchingRuns = entries.filter((e) => !excludeSet.has(e.id));
|
|
166
|
-
return matchingRuns.length > 0;
|
|
167
|
-
} catch (err) {
|
|
168
|
-
logger.error("Error checking flow status:", err);
|
|
169
|
-
return false;
|
|
170
|
-
}
|
|
171
|
-
},
|
|
172
|
-
/**
|
|
173
|
-
* Get all currently running flows (includes 'running' and 'awaiting' status)
|
|
174
|
-
* @param flowName - The name of the flow to check
|
|
175
|
-
* @param options - Optional configuration
|
|
176
|
-
* @param options.excludeRunIds - Exclude these run IDs from the results (useful when called from within a flow)
|
|
177
|
-
*/
|
|
178
|
-
async getRunningFlows(flowName, options) {
|
|
179
|
-
try {
|
|
180
|
-
if (!store.index.read) {
|
|
181
|
-
return [];
|
|
182
|
-
}
|
|
183
|
-
const runIndexKey = StoreSubjects.flowRunIndex(flowName);
|
|
184
|
-
const activeStatuses = ["running", "awaiting"];
|
|
185
|
-
const entries = await store.index.read(runIndexKey, {
|
|
186
|
-
limit: 1e3,
|
|
187
|
-
filter: { status: activeStatuses }
|
|
188
|
-
});
|
|
189
|
-
const excludeSet = new Set(options?.excludeRunIds || []);
|
|
190
|
-
const filteredEntries = entries.filter((e) => !excludeSet.has(e.id));
|
|
191
|
-
return filteredEntries.map((e) => ({
|
|
192
|
-
id: e.id,
|
|
193
|
-
flowName,
|
|
194
|
-
status: e.metadata?.status,
|
|
195
|
-
startedAt: e.metadata?.startedAt,
|
|
196
|
-
stepCount: e.metadata?.stepCount || 0,
|
|
197
|
-
completedSteps: e.metadata?.completedSteps || 0
|
|
198
|
-
}));
|
|
199
|
-
} catch (err) {
|
|
200
|
-
logger.error("Error getting running flows:", err);
|
|
201
|
-
return [];
|
|
202
|
-
}
|
|
203
|
-
},
|
|
204
|
-
/**
|
|
205
|
-
* Get flow statistics by name
|
|
206
|
-
*/
|
|
207
|
-
async getFlowStats(flowName) {
|
|
208
|
-
try {
|
|
209
|
-
const indexKey = StoreSubjects.flowIndex();
|
|
210
|
-
if (!store.index.get) {
|
|
211
|
-
logger.warn("Store adapter does not support indexGet");
|
|
212
|
-
return null;
|
|
213
|
-
}
|
|
214
|
-
const entry = await store.index.get(indexKey, flowName);
|
|
215
|
-
if (!entry) {
|
|
216
|
-
return null;
|
|
217
|
-
}
|
|
218
|
-
const metadata = entry.metadata;
|
|
219
|
-
return {
|
|
220
|
-
name: metadata.name || flowName,
|
|
221
|
-
displayName: metadata.displayName,
|
|
222
|
-
registeredAt: metadata.registeredAt,
|
|
223
|
-
lastRunAt: metadata.lastRunAt,
|
|
224
|
-
lastCompletedAt: metadata.lastCompletedAt,
|
|
225
|
-
stats: {
|
|
226
|
-
total: metadata.stats?.total || 0,
|
|
227
|
-
success: metadata.stats?.success || 0,
|
|
228
|
-
failure: metadata.stats?.failure || 0,
|
|
229
|
-
cancel: metadata.stats?.cancel || 0,
|
|
230
|
-
running: metadata.stats?.running || 0,
|
|
231
|
-
awaiting: metadata.stats?.awaiting || 0
|
|
232
|
-
},
|
|
233
|
-
version: metadata.version
|
|
234
|
-
};
|
|
235
|
-
} catch (err) {
|
|
236
|
-
logger.error("Error getting flow stats", {
|
|
237
|
-
flowName,
|
|
238
|
-
error: err?.message
|
|
239
|
-
});
|
|
240
|
-
return null;
|
|
241
|
-
}
|
|
242
|
-
},
|
|
243
|
-
/**
|
|
244
|
-
* Get all flows with their statistics
|
|
245
|
-
*/
|
|
246
|
-
async getAllFlowStats(options) {
|
|
247
|
-
try {
|
|
248
|
-
const indexKey = StoreSubjects.flowIndex();
|
|
249
|
-
if (!store.index.read) {
|
|
250
|
-
logger.warn("Store adapter does not support indexRead");
|
|
251
|
-
return [];
|
|
252
|
-
}
|
|
253
|
-
const entries = await store.index.read(indexKey, {
|
|
254
|
-
limit: options?.limit || 1e3,
|
|
255
|
-
offset: options?.offset || 0
|
|
256
|
-
});
|
|
257
|
-
const flows = entries.map((entry) => {
|
|
258
|
-
const metadata = entry.metadata;
|
|
259
|
-
return {
|
|
260
|
-
name: metadata.name || entry.id,
|
|
261
|
-
displayName: metadata.displayName,
|
|
262
|
-
registeredAt: metadata.registeredAt,
|
|
263
|
-
lastRunAt: metadata.lastRunAt,
|
|
264
|
-
lastCompletedAt: metadata.lastCompletedAt,
|
|
265
|
-
stats: {
|
|
266
|
-
total: metadata.stats?.total || 0,
|
|
267
|
-
success: metadata.stats?.success || 0,
|
|
268
|
-
failure: metadata.stats?.failure || 0,
|
|
269
|
-
cancel: metadata.stats?.cancel || 0,
|
|
270
|
-
running: metadata.stats?.running || 0,
|
|
271
|
-
awaiting: metadata.stats?.awaiting || 0
|
|
272
|
-
},
|
|
273
|
-
version: metadata.version
|
|
274
|
-
};
|
|
275
|
-
});
|
|
276
|
-
if (options?.sortBy) {
|
|
277
|
-
flows.sort((a, b) => {
|
|
278
|
-
let aVal;
|
|
279
|
-
let bVal;
|
|
280
|
-
if (options.sortBy === "name") {
|
|
281
|
-
aVal = a.name;
|
|
282
|
-
bVal = b.name;
|
|
283
|
-
} else if (options.sortBy === "registeredAt") {
|
|
284
|
-
aVal = new Date(a.registeredAt).getTime();
|
|
285
|
-
bVal = new Date(b.registeredAt).getTime();
|
|
286
|
-
} else if (options.sortBy === "lastRunAt") {
|
|
287
|
-
aVal = a.lastRunAt ? new Date(a.lastRunAt).getTime() : 0;
|
|
288
|
-
bVal = b.lastRunAt ? new Date(b.lastRunAt).getTime() : 0;
|
|
289
|
-
}
|
|
290
|
-
const order = options.order === "desc" ? -1 : 1;
|
|
291
|
-
return (aVal > bVal ? 1 : aVal < bVal ? -1 : 0) * order;
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
return flows;
|
|
295
|
-
} catch (err) {
|
|
296
|
-
logger.error("Error getting all flow stats", {
|
|
297
|
-
error: err?.message
|
|
298
|
-
});
|
|
299
|
-
return [];
|
|
300
|
-
}
|
|
301
|
-
},
|
|
302
|
-
/**
|
|
303
|
-
* Check if a flow has statistics in the index
|
|
304
|
-
*/
|
|
305
|
-
async hasFlowStats(flowName) {
|
|
306
|
-
const stats = await this.getFlowStats(flowName);
|
|
307
|
-
return stats !== null;
|
|
308
|
-
}
|
|
309
|
-
};
|
|
310
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Lifecycle hooks registry for await patterns
|
|
3
|
-
* v0.5 - Await Integration
|
|
4
|
-
*/
|
|
5
|
-
export interface LifecycleHooks {
|
|
6
|
-
/**
|
|
7
|
-
* Called when await pattern is registered
|
|
8
|
-
* @param hookData - Type-safe data based on await type (webhookUrl, eventName, etc.)
|
|
9
|
-
* @param stepData - Current step data
|
|
10
|
-
* @param ctx - Worker context with awaitType and awaitConfig
|
|
11
|
-
*/
|
|
12
|
-
onAwaitRegister?: (hookData: any, stepData: any, ctx: any) => Promise<void>;
|
|
13
|
-
/**
|
|
14
|
-
* Called when await pattern is resolved
|
|
15
|
-
* @param resolvedData - Data from the trigger that resolved the await
|
|
16
|
-
* @param stepData - Current step data
|
|
17
|
-
* @param ctx - Worker context with awaitType
|
|
18
|
-
*/
|
|
19
|
-
onAwaitResolve?: (resolvedData: any, stepData: any, ctx: any) => Promise<void>;
|
|
20
|
-
/**
|
|
21
|
-
* Called when await pattern times out
|
|
22
|
-
* @param stepData - Current step data
|
|
23
|
-
* @param ctx - Worker context with awaitType and timeoutAction
|
|
24
|
-
*/
|
|
25
|
-
onAwaitTimeout?: (stepData: any, ctx: any) => Promise<void>;
|
|
26
|
-
}
|
|
27
|
-
export declare function useHookRegistry(): {
|
|
28
|
-
/**
|
|
29
|
-
* Register lifecycle hooks for a specific flow step
|
|
30
|
-
*/
|
|
31
|
-
register(flowName: string, stepName: string, hooks: LifecycleHooks): void;
|
|
32
|
-
/**
|
|
33
|
-
* Load lifecycle hooks for a specific flow step
|
|
34
|
-
*/
|
|
35
|
-
load(flowName: string, stepName: string): LifecycleHooks | null;
|
|
36
|
-
/**
|
|
37
|
-
* Clear all registered hooks (useful for testing)
|
|
38
|
-
*/
|
|
39
|
-
clear(): void;
|
|
40
|
-
};
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
const hookRegistry = /* @__PURE__ */ new Map();
|
|
2
|
-
export function useHookRegistry() {
|
|
3
|
-
return {
|
|
4
|
-
/**
|
|
5
|
-
* Register lifecycle hooks for a specific flow step
|
|
6
|
-
*/
|
|
7
|
-
register(flowName, stepName, hooks) {
|
|
8
|
-
const key = `${flowName}:${stepName}`;
|
|
9
|
-
hookRegistry.set(key, hooks);
|
|
10
|
-
},
|
|
11
|
-
/**
|
|
12
|
-
* Load lifecycle hooks for a specific flow step
|
|
13
|
-
*/
|
|
14
|
-
load(flowName, stepName) {
|
|
15
|
-
const key = `${flowName}:${stepName}`;
|
|
16
|
-
return hookRegistry.get(key) || null;
|
|
17
|
-
},
|
|
18
|
-
/**
|
|
19
|
-
* Clear all registered hooks (useful for testing)
|
|
20
|
-
*/
|
|
21
|
-
clear() {
|
|
22
|
-
hookRegistry.clear();
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { type ConsolaInstance } from 'consola';
|
|
2
|
-
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
3
|
-
export interface NventLogger {
|
|
4
|
-
debug: (message: string, context?: any) => void;
|
|
5
|
-
info: (message: string, context?: any) => void;
|
|
6
|
-
warn: (message: string, context?: any) => void;
|
|
7
|
-
error: (message: string, context?: any) => void;
|
|
8
|
-
log: (level: LogLevel, message: string, context?: any) => void;
|
|
9
|
-
isEnabled: (level: LogLevel) => boolean;
|
|
10
|
-
consola: ConsolaInstance;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Creates a contextual server logger with configurable log levels
|
|
14
|
-
* Uses consola for beautiful, styled console output with colors and icons
|
|
15
|
-
*
|
|
16
|
-
* Configuration via runtime config or environment variables:
|
|
17
|
-
* - queue.debug.level: 'debug' | 'info' | 'warn' | 'error' | 'silent'
|
|
18
|
-
* - queue.debug.<scope>: boolean (enables debug logging for specific scope)
|
|
19
|
-
* - NQ_DEBUG_LEVEL: environment variable override
|
|
20
|
-
* - NQ_DEBUG_<SCOPE>: environment variable for scope (e.g., NQ_DEBUG_FLOW_WIRING=1)
|
|
21
|
-
*
|
|
22
|
-
* @param scope - Logger scope/namespace (e.g., 'flow-wiring', 'event-manager')
|
|
23
|
-
* @returns NventLogger instance with scoped logging methods
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```ts
|
|
27
|
-
* const logger = useNventLogger('flow-wiring')
|
|
28
|
-
* logger.debug('Step triggered', { stepName: 'process', runId: '123' })
|
|
29
|
-
* logger.info('Flow completed', { flowName: 'example' })
|
|
30
|
-
* logger.warn('Retry attempt', { attempt: 3 })
|
|
31
|
-
* logger.error('Processing failed', { error: err.message })
|
|
32
|
-
*
|
|
33
|
-
* // Access consola instance for advanced features
|
|
34
|
-
* logger.consola.success('All steps completed!')
|
|
35
|
-
* logger.consola.box('🚀 Flow Started')
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
export declare function useNventLogger(scope: string): NventLogger;
|
|
39
|
-
/**
|
|
40
|
-
* Type guard to check if debug logging is enabled for any scope
|
|
41
|
-
*/
|
|
42
|
-
export declare function isDebugEnabled(): boolean;
|