@pikku/core 0.11.0 → 0.11.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/CHANGELOG.md +35 -1
- package/dist/errors/error-handler.js +3 -3
- package/dist/errors/errors.d.ts +1 -1
- package/dist/errors/errors.js +3 -3
- package/dist/function/function-runner.d.ts +8 -8
- package/dist/function/function-runner.js +88 -23
- package/dist/function/functions.types.d.ts +104 -33
- package/dist/function/functions.types.js +50 -7
- package/dist/handle-error.d.ts +1 -1
- package/dist/handle-error.js +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.js +5 -2
- package/dist/middleware/auth-apikey.d.ts +1 -1
- package/dist/middleware/auth-apikey.js +3 -3
- package/dist/middleware/auth-bearer.d.ts +1 -1
- package/dist/middleware/auth-bearer.js +3 -3
- package/dist/middleware/auth-cookie.d.ts +1 -1
- package/dist/middleware/auth-cookie.js +5 -5
- package/dist/middleware/timeout.d.ts +1 -1
- package/dist/middleware/timeout.js +3 -2
- package/dist/middleware-runner.d.ts +6 -20
- package/dist/middleware-runner.js +19 -20
- package/dist/permissions.d.ts +6 -89
- package/dist/permissions.js +24 -81
- package/dist/pikku-state.d.ts +32 -104
- package/dist/pikku-state.js +140 -66
- package/dist/schema.d.ts +8 -6
- package/dist/schema.js +25 -13
- package/dist/services/user-session-service.d.ts +7 -3
- package/dist/services/user-session-service.js +8 -1
- package/dist/services/workflow-service.d.ts +38 -0
- package/dist/services/workflow-service.js +1 -0
- package/dist/types/core.types.d.ts +40 -41
- package/dist/types/core.types.js +4 -15
- package/dist/types/state.types.d.ts +135 -0
- package/dist/types/state.types.js +1 -0
- package/dist/utils.d.ts +9 -1
- package/dist/utils.js +48 -3
- package/dist/wirings/channel/channel-common.js +2 -3
- package/dist/wirings/channel/channel-handler.d.ts +3 -2
- package/dist/wirings/channel/channel-handler.js +7 -10
- package/dist/wirings/channel/channel-runner.d.ts +2 -2
- package/dist/wirings/channel/channel-runner.js +5 -5
- package/dist/wirings/channel/channel.types.d.ts +10 -5
- package/dist/wirings/channel/local/local-channel-runner.d.ts +1 -1
- package/dist/wirings/channel/local/local-channel-runner.js +19 -26
- package/dist/wirings/channel/log-channels.js +1 -1
- package/dist/wirings/channel/serverless/serverless-channel-runner.d.ts +1 -1
- package/dist/wirings/channel/serverless/serverless-channel-runner.js +43 -48
- package/dist/wirings/cli/channel/cli-channel-runner.js +3 -2
- package/dist/wirings/cli/cli-runner.d.ts +5 -5
- package/dist/wirings/cli/cli-runner.js +20 -38
- package/dist/wirings/cli/cli.types.d.ts +16 -11
- package/dist/wirings/forge-node/forge-node.types.d.ts +120 -0
- package/dist/wirings/forge-node/forge-node.types.js +38 -0
- package/dist/wirings/forge-node/index.d.ts +1 -0
- package/dist/wirings/forge-node/index.js +1 -0
- package/dist/wirings/http/http-runner.d.ts +8 -8
- package/dist/wirings/http/http-runner.js +46 -51
- package/dist/wirings/http/http.types.d.ts +19 -11
- package/dist/wirings/http/log-http-routes.js +1 -1
- package/dist/wirings/http/routers/path-to-regex.js +2 -2
- package/dist/wirings/mcp/mcp-runner.d.ts +2 -2
- package/dist/wirings/mcp/mcp-runner.js +33 -40
- package/dist/wirings/mcp/mcp.types.d.ts +6 -0
- package/dist/wirings/queue/queue-runner.d.ts +3 -3
- package/dist/wirings/queue/queue-runner.js +13 -23
- package/dist/wirings/queue/queue.types.d.ts +4 -11
- package/dist/wirings/rpc/index.d.ts +1 -1
- package/dist/wirings/rpc/index.js +1 -1
- package/dist/wirings/rpc/rpc-runner.d.ts +17 -16
- package/dist/wirings/rpc/rpc-runner.js +98 -61
- package/dist/wirings/rpc/rpc-types.d.ts +7 -0
- package/dist/wirings/scheduler/log-schedulers.js +1 -1
- package/dist/wirings/scheduler/scheduler-runner.d.ts +3 -3
- package/dist/wirings/scheduler/scheduler-runner.js +18 -29
- package/dist/wirings/scheduler/scheduler.types.d.ts +3 -8
- package/dist/wirings/trigger/index.d.ts +2 -0
- package/dist/wirings/trigger/index.js +2 -0
- package/dist/wirings/trigger/trigger-runner.d.ts +29 -0
- package/dist/wirings/trigger/trigger-runner.js +57 -0
- package/dist/wirings/trigger/trigger.types.d.ts +42 -0
- package/dist/wirings/trigger/trigger.types.js +1 -0
- package/dist/wirings/workflow/dsl/index.d.ts +5 -0
- package/dist/wirings/workflow/dsl/index.js +4 -0
- package/dist/wirings/workflow/dsl/workflow-dsl.types.d.ts +286 -0
- package/dist/wirings/workflow/dsl/workflow-dsl.types.js +5 -0
- package/dist/wirings/workflow/dsl/workflow-runner.d.ts +5 -0
- package/dist/wirings/workflow/dsl/workflow-runner.js +22 -0
- package/dist/wirings/workflow/graph/graph-node.d.ts +122 -0
- package/dist/wirings/workflow/graph/graph-node.js +58 -0
- package/dist/wirings/workflow/graph/graph-runner.d.ts +35 -0
- package/dist/wirings/workflow/graph/graph-runner.js +452 -0
- package/dist/wirings/workflow/graph/index.d.ts +3 -0
- package/dist/wirings/workflow/graph/index.js +3 -0
- package/dist/wirings/workflow/graph/workflow-graph.types.d.ts +95 -0
- package/dist/wirings/workflow/graph/workflow-graph.types.js +15 -0
- package/dist/wirings/workflow/index.d.ts +7 -3
- package/dist/wirings/workflow/index.js +6 -3
- package/dist/wirings/workflow/pikku-workflow-service.d.ts +108 -10
- package/dist/wirings/workflow/pikku-workflow-service.js +274 -165
- package/dist/wirings/workflow/wire-workflow.d.ts +42 -0
- package/dist/wirings/workflow/wire-workflow.js +53 -0
- package/dist/wirings/workflow/workflow-utils.d.ts +23 -0
- package/dist/wirings/workflow/workflow-utils.js +66 -0
- package/dist/wirings/workflow/workflow.types.d.ts +135 -128
- package/package.json +11 -2
- package/src/errors/error-handler.ts +3 -3
- package/src/errors/errors.ts +3 -3
- package/src/factory-functions.test.ts +9 -36
- package/src/function/function-runner.test.ts +58 -69
- package/src/function/function-runner.ts +130 -37
- package/src/function/functions.types.ts +193 -54
- package/src/handle-error.ts +1 -1
- package/src/index.ts +10 -5
- package/src/middleware/auth-apikey.test.ts +360 -0
- package/src/middleware/auth-apikey.ts +3 -7
- package/src/middleware/auth-bearer.test.ts +447 -0
- package/src/middleware/auth-bearer.ts +27 -33
- package/src/middleware/auth-cookie.test.ts +525 -0
- package/src/middleware/auth-cookie.ts +6 -6
- package/src/middleware/timeout.ts +4 -2
- package/src/middleware-runner.test.ts +58 -127
- package/src/middleware-runner.ts +29 -25
- package/src/permissions.test.ts +68 -130
- package/src/permissions.ts +41 -100
- package/src/pikku-state.ts +156 -201
- package/src/schema.ts +42 -13
- package/src/services/user-session-service.ts +14 -4
- package/src/services/workflow-service.ts +74 -0
- package/src/types/core.types.ts +79 -58
- package/src/types/state.types.ts +195 -0
- package/src/utils.ts +59 -4
- package/src/wirings/channel/channel-common.ts +4 -7
- package/src/wirings/channel/channel-handler.ts +17 -23
- package/src/wirings/channel/channel-runner.ts +7 -7
- package/src/wirings/channel/channel.types.ts +14 -11
- package/src/wirings/channel/local/local-channel-runner.test.ts +8 -4
- package/src/wirings/channel/local/local-channel-runner.ts +23 -38
- package/src/wirings/channel/log-channels.ts +1 -1
- package/src/wirings/channel/serverless/serverless-channel-runner.ts +53 -79
- package/src/wirings/cli/channel/cli-channel-runner.ts +4 -3
- package/src/wirings/cli/cli-runner.test.ts +26 -26
- package/src/wirings/cli/cli-runner.ts +34 -71
- package/src/wirings/cli/cli.types.ts +23 -16
- package/src/wirings/forge-node/forge-node.types.ts +135 -0
- package/src/wirings/forge-node/index.ts +1 -0
- package/src/wirings/http/http-runner.test.ts +10 -10
- package/src/wirings/http/http-runner.ts +56 -66
- package/src/wirings/http/http.types.ts +19 -14
- package/src/wirings/http/log-http-routes.ts +1 -1
- package/src/wirings/http/routers/path-to-regex.test.ts +30 -19
- package/src/wirings/http/routers/path-to-regex.ts +2 -2
- package/src/wirings/mcp/mcp-runner.ts +44 -69
- package/src/wirings/mcp/mcp.types.ts +6 -0
- package/src/wirings/queue/queue-runner.test.ts +641 -0
- package/src/wirings/queue/queue-runner.ts +16 -38
- package/src/wirings/queue/queue.types.ts +4 -11
- package/src/wirings/rpc/index.ts +1 -1
- package/src/wirings/rpc/rpc-runner.ts +129 -77
- package/src/wirings/rpc/rpc-types.ts +8 -0
- package/src/wirings/scheduler/log-schedulers.ts +1 -1
- package/src/wirings/scheduler/scheduler-runner.test.ts +627 -0
- package/src/wirings/scheduler/scheduler-runner.ts +29 -55
- package/src/wirings/scheduler/scheduler.types.ts +3 -9
- package/src/wirings/trigger/index.ts +2 -0
- package/src/wirings/trigger/trigger-runner.ts +96 -0
- package/src/wirings/trigger/trigger.types.ts +56 -0
- package/src/wirings/workflow/dsl/index.ts +30 -0
- package/src/wirings/workflow/dsl/workflow-dsl.types.ts +312 -0
- package/src/wirings/workflow/dsl/workflow-runner.ts +27 -0
- package/src/wirings/workflow/graph/graph-node.ts +227 -0
- package/src/wirings/workflow/graph/graph-runner.ts +694 -0
- package/src/wirings/workflow/graph/index.ts +3 -0
- package/src/wirings/workflow/graph/workflow-graph.types.ts +126 -0
- package/src/wirings/workflow/index.ts +60 -12
- package/src/wirings/workflow/pikku-workflow-service.ts +401 -195
- package/src/wirings/workflow/wire-workflow.ts +94 -0
- package/src/wirings/workflow/workflow-utils.ts +120 -0
- package/src/wirings/workflow/workflow.types.ts +167 -188
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/wirings/workflow/workflow-runner.d.ts +0 -35
- package/dist/wirings/workflow/workflow-runner.js +0 -68
- package/src/wirings/workflow/workflow-runner.ts +0 -85
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { type CoreServices, type CoreSingletonServices, type CoreUserSession, type
|
|
1
|
+
import { type CoreServices, type CoreSingletonServices, type CoreUserSession, type CreateWireServices } from '../../types/core.types.js';
|
|
2
2
|
import type { CoreScheduledTask } from './scheduler.types.js';
|
|
3
3
|
import { CorePikkuFunctionConfig, CorePikkuFunctionSessionless } from '../../function/functions.types.js';
|
|
4
4
|
export type RunScheduledTasksParams = {
|
|
5
5
|
name: string;
|
|
6
6
|
session?: CoreUserSession;
|
|
7
7
|
singletonServices: CoreSingletonServices;
|
|
8
|
-
|
|
8
|
+
createWireServices?: CreateWireServices<CoreSingletonServices, CoreServices<CoreSingletonServices>, CoreUserSession>;
|
|
9
9
|
};
|
|
10
10
|
export declare const wireScheduler: <PikkuFunctionConfig extends CorePikkuFunctionConfig<CorePikkuFunctionSessionless<void, void>>>(scheduledTask: CoreScheduledTask<PikkuFunctionConfig>) => void;
|
|
11
|
-
export declare function runScheduledTask({ name, session, singletonServices,
|
|
11
|
+
export declare function runScheduledTask({ name, session, singletonServices, createWireServices, }: RunScheduledTasksParams): Promise<void>;
|
|
12
12
|
export declare const getScheduledTasks: () => Map<string, CoreScheduledTask>;
|
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import { PikkuWiringTypes, } from '../../types/core.types.js';
|
|
2
1
|
import { getErrorResponse, PikkuError } from '../../errors/error-handler.js';
|
|
3
|
-
import {
|
|
2
|
+
import { closeWireServices } from '../../utils.js';
|
|
4
3
|
import { pikkuState } from '../../pikku-state.js';
|
|
5
4
|
import { addFunction, runPikkuFunc } from '../../function/function-runner.js';
|
|
6
|
-
import {
|
|
7
|
-
import { PikkuUserSessionService } from '../../services/user-session-service.js';
|
|
5
|
+
import { PikkuSessionService } from '../../services/user-session-service.js';
|
|
8
6
|
export const wireScheduler = (scheduledTask) => {
|
|
9
|
-
const meta = pikkuState('scheduler', 'meta');
|
|
7
|
+
const meta = pikkuState(null, 'scheduler', 'meta');
|
|
10
8
|
const taskMeta = meta[scheduledTask.name];
|
|
11
9
|
if (!taskMeta) {
|
|
12
10
|
throw new Error('Task metadata not found');
|
|
@@ -17,9 +15,8 @@ export const wireScheduler = (scheduledTask) => {
|
|
|
17
15
|
permissions: scheduledTask.func.permissions,
|
|
18
16
|
middleware: scheduledTask.func.middleware,
|
|
19
17
|
tags: scheduledTask.func.tags,
|
|
20
|
-
docs: scheduledTask.func.docs,
|
|
21
18
|
});
|
|
22
|
-
const tasks = pikkuState('scheduler', 'tasks');
|
|
19
|
+
const tasks = pikkuState(null, 'scheduler', 'tasks');
|
|
23
20
|
if (tasks.has(scheduledTask.name)) {
|
|
24
21
|
throw new Error(`Scheduled task already exists: ${scheduledTask.name}`);
|
|
25
22
|
}
|
|
@@ -36,11 +33,11 @@ class ScheduledTaskSkippedError extends PikkuError {
|
|
|
36
33
|
this.name = 'ScheduledTaskSkippedError';
|
|
37
34
|
}
|
|
38
35
|
}
|
|
39
|
-
export async function runScheduledTask({ name, session, singletonServices,
|
|
40
|
-
let
|
|
41
|
-
const task = pikkuState('scheduler', 'tasks').get(name);
|
|
42
|
-
const meta = pikkuState('scheduler', 'meta')[name];
|
|
43
|
-
const userSession = new
|
|
36
|
+
export async function runScheduledTask({ name, session, singletonServices, createWireServices, }) {
|
|
37
|
+
let wireServices;
|
|
38
|
+
const task = pikkuState(null, 'scheduler', 'tasks').get(name);
|
|
39
|
+
const meta = pikkuState(null, 'scheduler', 'meta')[name];
|
|
40
|
+
const userSession = new PikkuSessionService();
|
|
44
41
|
if (session) {
|
|
45
42
|
userSession.set(session);
|
|
46
43
|
}
|
|
@@ -50,8 +47,8 @@ export async function runScheduledTask({ name, session, singletonServices, creat
|
|
|
50
47
|
if (!meta) {
|
|
51
48
|
throw new ScheduledTaskNotFoundError(`Scheduled task meta not found: ${name}`);
|
|
52
49
|
}
|
|
53
|
-
// Create the scheduled task
|
|
54
|
-
const
|
|
50
|
+
// Create the scheduled task wire object
|
|
51
|
+
const wire = {
|
|
55
52
|
scheduledTask: {
|
|
56
53
|
name,
|
|
57
54
|
schedule: task.schedule,
|
|
@@ -60,28 +57,20 @@ export async function runScheduledTask({ name, session, singletonServices, creat
|
|
|
60
57
|
throw new ScheduledTaskSkippedError(name, reason);
|
|
61
58
|
},
|
|
62
59
|
},
|
|
60
|
+
session: userSession,
|
|
63
61
|
};
|
|
64
62
|
try {
|
|
65
63
|
singletonServices.logger.info(`Running schedule task: ${name} | schedule: ${task.schedule}`);
|
|
66
|
-
|
|
67
|
-
sessionServices = await createSessionServices?.(singletonServices, interaction, session);
|
|
68
|
-
return rpcService.injectRPCService({
|
|
69
|
-
...singletonServices,
|
|
70
|
-
...sessionServices,
|
|
71
|
-
}, interaction);
|
|
72
|
-
};
|
|
73
|
-
const result = await runPikkuFunc(PikkuWiringTypes.scheduler, meta.name, meta.pikkuFuncName, {
|
|
64
|
+
await runPikkuFunc('scheduler', meta.name, meta.pikkuFuncName, {
|
|
74
65
|
singletonServices,
|
|
75
|
-
|
|
76
|
-
userSession,
|
|
66
|
+
createWireServices,
|
|
77
67
|
auth: false,
|
|
78
68
|
data: () => undefined,
|
|
79
69
|
inheritedMiddleware: meta.middleware,
|
|
80
70
|
wireMiddleware: task.middleware,
|
|
81
71
|
tags: task.tags,
|
|
82
|
-
|
|
72
|
+
wire,
|
|
83
73
|
});
|
|
84
|
-
return result;
|
|
85
74
|
}
|
|
86
75
|
catch (e) {
|
|
87
76
|
const errorResponse = getErrorResponse(e);
|
|
@@ -91,11 +80,11 @@ export async function runScheduledTask({ name, session, singletonServices, creat
|
|
|
91
80
|
throw e;
|
|
92
81
|
}
|
|
93
82
|
finally {
|
|
94
|
-
if (
|
|
95
|
-
await
|
|
83
|
+
if (wireServices) {
|
|
84
|
+
await closeWireServices(singletonServices.logger, wireServices);
|
|
96
85
|
}
|
|
97
86
|
}
|
|
98
87
|
}
|
|
99
88
|
export const getScheduledTasks = () => {
|
|
100
|
-
return pikkuState('scheduler', 'tasks');
|
|
89
|
+
return pikkuState(null, 'scheduler', 'tasks');
|
|
101
90
|
};
|
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CoreUserSession, CorePikkuMiddleware, CommonWireMeta } from '../../types/core.types.js';
|
|
2
2
|
import { CorePikkuFunctionConfig, CorePikkuFunctionSessionless } from '../../function/functions.types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Represents metadata for scheduled tasks, including title, schedule, and documentation.
|
|
5
5
|
*/
|
|
6
|
-
export type ScheduledTasksMeta<UserSession extends CoreUserSession = any> = Record<string, {
|
|
7
|
-
pikkuFuncName: string;
|
|
6
|
+
export type ScheduledTasksMeta<UserSession extends CoreUserSession = any> = Record<string, CommonWireMeta & {
|
|
8
7
|
name: string;
|
|
9
8
|
schedule: string;
|
|
10
9
|
session?: UserSession;
|
|
11
|
-
docs?: PikkuDocs;
|
|
12
|
-
tags?: string[];
|
|
13
|
-
middleware?: MiddlewareMetadata[];
|
|
14
10
|
}>;
|
|
15
11
|
/**
|
|
16
12
|
* Represents a core scheduled task.
|
|
@@ -19,12 +15,11 @@ export type CoreScheduledTask<PikkuFunctionConfig = CorePikkuFunctionConfig<Core
|
|
|
19
15
|
name: string;
|
|
20
16
|
schedule: string;
|
|
21
17
|
func: PikkuFunctionConfig;
|
|
22
|
-
docs?: PikkuDocs;
|
|
23
18
|
tags?: string[];
|
|
24
19
|
middleware?: PikkuMiddleware[];
|
|
25
20
|
};
|
|
26
21
|
/**
|
|
27
|
-
* Represents a scheduled task
|
|
22
|
+
* Represents a scheduled task wire object for middleware
|
|
28
23
|
* Provides information about the current scheduled task execution
|
|
29
24
|
*/
|
|
30
25
|
export interface PikkuScheduledTask {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { CoreSingletonServices } from '../../types/core.types.js';
|
|
2
|
+
import type { CoreTrigger, TriggerInstance } from './trigger.types.js';
|
|
3
|
+
import type { CorePikkuTriggerFunctionConfig } from '../../function/functions.types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Registers a trigger with the Pikku framework.
|
|
6
|
+
* The trigger will be available for setup via setupTrigger.
|
|
7
|
+
*/
|
|
8
|
+
export declare const wireTrigger: <TConfig = unknown, TOutput = unknown, TriggerFunctionConfig extends CorePikkuTriggerFunctionConfig<TConfig, TOutput> = CorePikkuTriggerFunctionConfig<TConfig, TOutput>>(trigger: CoreTrigger<TConfig, TOutput, TriggerFunctionConfig>) => void;
|
|
9
|
+
/**
|
|
10
|
+
* Parameters for setting up a trigger
|
|
11
|
+
*/
|
|
12
|
+
export type SetupTriggerParams<TOutput = unknown> = {
|
|
13
|
+
name: string;
|
|
14
|
+
singletonServices: CoreSingletonServices;
|
|
15
|
+
onTrigger: (data: TOutput) => void | Promise<void>;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Sets up a registered trigger and starts listening for events.
|
|
19
|
+
* Returns a TriggerInstance with a teardown function.
|
|
20
|
+
*/
|
|
21
|
+
export declare function setupTrigger<TOutput = unknown>({ name, singletonServices, onTrigger, }: SetupTriggerParams<TOutput>): Promise<TriggerInstance>;
|
|
22
|
+
/**
|
|
23
|
+
* Gets all registered triggers
|
|
24
|
+
*/
|
|
25
|
+
export declare const getRegisteredTriggers: () => Map<string, CoreTrigger<unknown, unknown, CorePikkuTriggerFunctionConfig<unknown, unknown>>>;
|
|
26
|
+
/**
|
|
27
|
+
* Gets trigger metadata
|
|
28
|
+
*/
|
|
29
|
+
export declare const getTriggerMeta: () => import("./trigger.types.js").TriggerMeta;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { pikkuState } from '../../pikku-state.js';
|
|
2
|
+
/**
|
|
3
|
+
* Registers a trigger with the Pikku framework.
|
|
4
|
+
* The trigger will be available for setup via setupTrigger.
|
|
5
|
+
*/
|
|
6
|
+
export const wireTrigger = (trigger) => {
|
|
7
|
+
const meta = pikkuState(null, 'trigger', 'meta');
|
|
8
|
+
const triggerMeta = meta[trigger.name];
|
|
9
|
+
if (!triggerMeta) {
|
|
10
|
+
throw new Error(`Trigger metadata not found: ${trigger.name}`);
|
|
11
|
+
}
|
|
12
|
+
const triggers = pikkuState(null, 'trigger', 'triggers');
|
|
13
|
+
if (triggers.has(trigger.name)) {
|
|
14
|
+
throw new Error(`Trigger already exists: ${trigger.name}`);
|
|
15
|
+
}
|
|
16
|
+
triggers.set(trigger.name, trigger);
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Sets up a registered trigger and starts listening for events.
|
|
20
|
+
* Returns a TriggerInstance with a teardown function.
|
|
21
|
+
*/
|
|
22
|
+
export async function setupTrigger({ name, singletonServices, onTrigger, }) {
|
|
23
|
+
const trigger = pikkuState(null, 'trigger', 'triggers').get(name);
|
|
24
|
+
const meta = pikkuState(null, 'trigger', 'meta')[name];
|
|
25
|
+
if (!trigger) {
|
|
26
|
+
throw new Error(`Trigger not found: ${name}`);
|
|
27
|
+
}
|
|
28
|
+
if (!meta) {
|
|
29
|
+
throw new Error(`Trigger metadata not found: ${name}`);
|
|
30
|
+
}
|
|
31
|
+
const wire = {
|
|
32
|
+
trigger: {
|
|
33
|
+
trigger: (data) => {
|
|
34
|
+
singletonServices.logger.info(`Trigger fired: ${name}`);
|
|
35
|
+
onTrigger(data);
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
singletonServices.logger.info(`Setting up trigger: ${name}`);
|
|
40
|
+
const teardown = await trigger.func.func(singletonServices, trigger.config, wire);
|
|
41
|
+
return {
|
|
42
|
+
name,
|
|
43
|
+
teardown,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Gets all registered triggers
|
|
48
|
+
*/
|
|
49
|
+
export const getRegisteredTriggers = () => {
|
|
50
|
+
return pikkuState(null, 'trigger', 'triggers');
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Gets trigger metadata
|
|
54
|
+
*/
|
|
55
|
+
export const getTriggerMeta = () => {
|
|
56
|
+
return pikkuState(null, 'trigger', 'meta');
|
|
57
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { CorePikkuTriggerFunctionConfig } from '../../function/functions.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The trigger interaction object passed to trigger functions via the wire.
|
|
4
|
+
* Call trigger() to fire the trigger and start a new workflow/execution.
|
|
5
|
+
*/
|
|
6
|
+
export interface PikkuTrigger<TOutput = unknown> {
|
|
7
|
+
trigger: (data: TOutput) => void;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Metadata for registered triggers stored in state.
|
|
11
|
+
*/
|
|
12
|
+
export type TriggerMeta = Record<string, {
|
|
13
|
+
pikkuFuncName: string;
|
|
14
|
+
name: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
tags?: string[];
|
|
17
|
+
}>;
|
|
18
|
+
/**
|
|
19
|
+
* Core trigger definition for registration.
|
|
20
|
+
*
|
|
21
|
+
* @template TConfig - Configuration type
|
|
22
|
+
* @template TOutput - Output type
|
|
23
|
+
*/
|
|
24
|
+
export interface CoreTrigger<TConfig = unknown, TOutput = unknown, TriggerFunctionConfig extends CorePikkuTriggerFunctionConfig<TConfig, TOutput> = CorePikkuTriggerFunctionConfig<TConfig, TOutput>> {
|
|
25
|
+
/** Unique name for this trigger */
|
|
26
|
+
name: string;
|
|
27
|
+
/** The trigger function configuration */
|
|
28
|
+
func: TriggerFunctionConfig;
|
|
29
|
+
/** Configuration to pass to the trigger function */
|
|
30
|
+
config: TConfig;
|
|
31
|
+
/** Optional description */
|
|
32
|
+
description?: string;
|
|
33
|
+
/** Optional tags for categorization */
|
|
34
|
+
tags?: string[];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Represents a trigger instance with teardown capability
|
|
38
|
+
*/
|
|
39
|
+
export interface TriggerInstance {
|
|
40
|
+
name: string;
|
|
41
|
+
teardown: () => void | Promise<void>;
|
|
42
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DSL (Domain Specific Language) workflow exports
|
|
3
|
+
*/
|
|
4
|
+
export { addWorkflow } from './workflow-runner.js';
|
|
5
|
+
export type { WorkflowStepOptions, WorkflowWireDoRPC, WorkflowWireDoInline, WorkflowWireSleep, InputSource, OutputBinding, RpcStepMeta, SimpleCondition, Condition, BranchStepMeta, ParallelGroupStepMeta, FanoutStepMeta, ReturnStepMeta, InlineStepMeta, SleepStepMeta, CancelStepMeta, SwitchCaseMeta, SwitchStepMeta, FilterStepMeta, ArrayPredicateStepMeta, WorkflowStepMeta, WorkflowStepWire, PikkuWorkflowWire, } from './workflow-dsl.types.js';
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DSL (Domain Specific Language) workflow types
|
|
3
|
+
* These types define the step-based workflow format extracted by the inspector
|
|
4
|
+
*/
|
|
5
|
+
import type { WorkflowRun } from '../workflow.types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Workflow step options
|
|
8
|
+
*/
|
|
9
|
+
export interface WorkflowStepOptions {
|
|
10
|
+
/** Display name for logs/UI (optional, doesn't affect execution) */
|
|
11
|
+
description?: string;
|
|
12
|
+
/** Number of retry attempts for failed steps (only applies to local execution) */
|
|
13
|
+
retries?: number;
|
|
14
|
+
/** Delay between retry attempts (e.g., '1s', '2s', '2min') */
|
|
15
|
+
retryDelay?: string | number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Type signature for workflow.do() RPC form - used by inspector
|
|
19
|
+
*/
|
|
20
|
+
export type WorkflowWireDoRPC = <TOutput = any, TInput = any>(stepName: string, rpcName: string, data: TInput, options?: WorkflowStepOptions) => Promise<TOutput>;
|
|
21
|
+
/**
|
|
22
|
+
* Type signature for workflow.do() inline form - used by inspector
|
|
23
|
+
*/
|
|
24
|
+
export type WorkflowWireDoInline = <T>(stepName: string, fn: () => Promise<T> | T, options?: WorkflowStepOptions) => Promise<T>;
|
|
25
|
+
/**
|
|
26
|
+
* Type signature for workflow.sleep() - used by inspector
|
|
27
|
+
*/
|
|
28
|
+
export type WorkflowWireSleep = (stepName: string, duration: string) => Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Input source for step arguments in DSL workflows
|
|
31
|
+
*/
|
|
32
|
+
export type InputSource = {
|
|
33
|
+
from: 'input';
|
|
34
|
+
path: string;
|
|
35
|
+
} | {
|
|
36
|
+
from: 'outputVar';
|
|
37
|
+
name: string;
|
|
38
|
+
path?: string;
|
|
39
|
+
} | {
|
|
40
|
+
from: 'item';
|
|
41
|
+
path: string;
|
|
42
|
+
} | {
|
|
43
|
+
from: 'literal';
|
|
44
|
+
value: unknown;
|
|
45
|
+
} | {
|
|
46
|
+
from: 'template';
|
|
47
|
+
parts: string[];
|
|
48
|
+
expressions: InputSource[];
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Output binding for return statements in DSL workflows
|
|
52
|
+
*/
|
|
53
|
+
export type OutputBinding = {
|
|
54
|
+
from: 'outputVar';
|
|
55
|
+
name: string;
|
|
56
|
+
path?: string;
|
|
57
|
+
} | {
|
|
58
|
+
from: 'stateVar';
|
|
59
|
+
name: string;
|
|
60
|
+
path?: string;
|
|
61
|
+
} | {
|
|
62
|
+
from: 'input';
|
|
63
|
+
path: string;
|
|
64
|
+
} | {
|
|
65
|
+
from: 'literal';
|
|
66
|
+
value: unknown;
|
|
67
|
+
} | {
|
|
68
|
+
from: 'expression';
|
|
69
|
+
expression: string;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* RPC step metadata (base form)
|
|
73
|
+
*/
|
|
74
|
+
export interface RpcStepMeta {
|
|
75
|
+
/** RPC form - generates queue worker */
|
|
76
|
+
type: 'rpc';
|
|
77
|
+
/** Cache key (stepName from workflow.do) */
|
|
78
|
+
stepName: string;
|
|
79
|
+
/** RPC to invoke */
|
|
80
|
+
rpcName: string;
|
|
81
|
+
/** Output variable name (if assigned) */
|
|
82
|
+
outputVar?: string;
|
|
83
|
+
/** Input source mappings, or 'passthrough' when entire data is passed */
|
|
84
|
+
inputs?: Record<string, InputSource> | 'passthrough';
|
|
85
|
+
/** Step options */
|
|
86
|
+
options?: WorkflowStepOptions;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Simple condition expression (leaf node)
|
|
90
|
+
*/
|
|
91
|
+
export interface SimpleCondition {
|
|
92
|
+
type: 'simple';
|
|
93
|
+
expression: string;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Nested condition structure supporting AND/OR operations
|
|
97
|
+
*/
|
|
98
|
+
export type Condition = SimpleCondition | {
|
|
99
|
+
type: 'and';
|
|
100
|
+
conditions: Condition[];
|
|
101
|
+
} | {
|
|
102
|
+
type: 'or';
|
|
103
|
+
conditions: Condition[];
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* A single branch in an if/else-if chain
|
|
107
|
+
*/
|
|
108
|
+
export interface BranchCase {
|
|
109
|
+
/** Condition for this branch */
|
|
110
|
+
condition: Condition;
|
|
111
|
+
/** Steps to execute when condition is true */
|
|
112
|
+
steps: WorkflowStepMeta[];
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Branch step metadata (if/else-if/else control flow)
|
|
116
|
+
*/
|
|
117
|
+
export interface BranchStepMeta {
|
|
118
|
+
type: 'branch';
|
|
119
|
+
/** Branches in order: first is "if", rest are "else if" */
|
|
120
|
+
branches: BranchCase[];
|
|
121
|
+
/** Else branch steps (when no conditions match) */
|
|
122
|
+
elseSteps?: WorkflowStepMeta[];
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Parallel group step metadata (Promise.all with multiple steps)
|
|
126
|
+
*/
|
|
127
|
+
export interface ParallelGroupStepMeta {
|
|
128
|
+
type: 'parallel';
|
|
129
|
+
/** Child steps to execute in parallel */
|
|
130
|
+
children: RpcStepMeta[];
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Fanout step metadata (parallel or sequential iteration)
|
|
134
|
+
*/
|
|
135
|
+
export interface FanoutStepMeta {
|
|
136
|
+
type: 'fanout';
|
|
137
|
+
/** Step name for this fanout */
|
|
138
|
+
stepName: string;
|
|
139
|
+
/** Source array variable name */
|
|
140
|
+
sourceVar: string;
|
|
141
|
+
/** Iterator variable name */
|
|
142
|
+
itemVar: string;
|
|
143
|
+
/** Execution mode */
|
|
144
|
+
mode: 'parallel' | 'sequential';
|
|
145
|
+
/** Child step to execute per iteration */
|
|
146
|
+
child: RpcStepMeta;
|
|
147
|
+
/** Time between iterations (sequential mode only) */
|
|
148
|
+
timeBetween?: string;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Return step metadata (workflow output)
|
|
152
|
+
*/
|
|
153
|
+
export interface ReturnStepMeta {
|
|
154
|
+
type: 'return';
|
|
155
|
+
/** Output bindings */
|
|
156
|
+
outputs: Record<string, OutputBinding>;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Inline step metadata (legacy support)
|
|
160
|
+
*/
|
|
161
|
+
export interface InlineStepMeta {
|
|
162
|
+
/** Inline form - local execution */
|
|
163
|
+
type: 'inline';
|
|
164
|
+
/** Cache key (stepName from workflow.do) */
|
|
165
|
+
stepName: string;
|
|
166
|
+
/** Display name */
|
|
167
|
+
description?: string;
|
|
168
|
+
/** Step options */
|
|
169
|
+
options?: WorkflowStepOptions;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Sleep step metadata
|
|
173
|
+
*/
|
|
174
|
+
export interface SleepStepMeta {
|
|
175
|
+
/** Sleep step */
|
|
176
|
+
type: 'sleep';
|
|
177
|
+
/** Cache key (stepName from workflow.sleep) */
|
|
178
|
+
stepName: string;
|
|
179
|
+
/** Sleep duration */
|
|
180
|
+
duration: string | number;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Cancel step metadata
|
|
184
|
+
*/
|
|
185
|
+
export interface CancelStepMeta {
|
|
186
|
+
/** Cancel step */
|
|
187
|
+
type: 'cancel';
|
|
188
|
+
/** Optional cancellation reason */
|
|
189
|
+
reason?: string;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Set step metadata (context variable assignment)
|
|
193
|
+
*/
|
|
194
|
+
export interface SetStepMeta {
|
|
195
|
+
/** Set step */
|
|
196
|
+
type: 'set';
|
|
197
|
+
/** Variable name to set (must be in context) */
|
|
198
|
+
variable: string;
|
|
199
|
+
/** Value to assign (literal or expression) */
|
|
200
|
+
value: unknown;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Switch case metadata
|
|
204
|
+
*/
|
|
205
|
+
export interface SwitchCaseMeta {
|
|
206
|
+
/** Case value (literal) or expression */
|
|
207
|
+
value?: string | number | boolean | null;
|
|
208
|
+
/** Case expression (for complex cases) */
|
|
209
|
+
expression?: string;
|
|
210
|
+
/** Steps to execute for this case */
|
|
211
|
+
steps: WorkflowStepMeta[];
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Switch step metadata (switch/case control flow)
|
|
215
|
+
*/
|
|
216
|
+
export interface SwitchStepMeta {
|
|
217
|
+
type: 'switch';
|
|
218
|
+
/** Expression being switched on */
|
|
219
|
+
expression: string;
|
|
220
|
+
/** Case branches */
|
|
221
|
+
cases: SwitchCaseMeta[];
|
|
222
|
+
/** Default case steps (optional) */
|
|
223
|
+
defaultSteps?: WorkflowStepMeta[];
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Filter step metadata (array.filter)
|
|
227
|
+
*/
|
|
228
|
+
export interface FilterStepMeta {
|
|
229
|
+
type: 'filter';
|
|
230
|
+
/** Source array variable name */
|
|
231
|
+
sourceVar: string;
|
|
232
|
+
/** Iterator variable name */
|
|
233
|
+
itemVar: string;
|
|
234
|
+
/** Filter condition */
|
|
235
|
+
condition: Condition;
|
|
236
|
+
/** Output variable name (if assigned) */
|
|
237
|
+
outputVar?: string;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Array predicate step metadata (array.some, array.every)
|
|
241
|
+
*/
|
|
242
|
+
export interface ArrayPredicateStepMeta {
|
|
243
|
+
type: 'arrayPredicate';
|
|
244
|
+
/** Predicate mode */
|
|
245
|
+
mode: 'some' | 'every';
|
|
246
|
+
/** Source array variable name */
|
|
247
|
+
sourceVar: string;
|
|
248
|
+
/** Iterator variable name */
|
|
249
|
+
itemVar: string;
|
|
250
|
+
/** Predicate condition */
|
|
251
|
+
condition: Condition;
|
|
252
|
+
/** Output variable name (if assigned) */
|
|
253
|
+
outputVar?: string;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Workflow step metadata (extracted by inspector)
|
|
257
|
+
*/
|
|
258
|
+
export type WorkflowStepMeta = RpcStepMeta | BranchStepMeta | ParallelGroupStepMeta | FanoutStepMeta | ReturnStepMeta | InlineStepMeta | SleepStepMeta | CancelStepMeta | SwitchStepMeta | FilterStepMeta | ArrayPredicateStepMeta | SetStepMeta;
|
|
259
|
+
/**
|
|
260
|
+
* Workflow step wire context for RPC functions
|
|
261
|
+
* Provides step-level metadata including retry attempt tracking
|
|
262
|
+
*/
|
|
263
|
+
export interface WorkflowStepWire {
|
|
264
|
+
/** The workflow run ID */
|
|
265
|
+
runId: string;
|
|
266
|
+
/** The unique step ID */
|
|
267
|
+
stepId: string;
|
|
268
|
+
/** Current attempt number (1-indexed, increments on retry) */
|
|
269
|
+
attemptCount: number;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Workflow wire object for DSL workflows
|
|
273
|
+
* Provides workflow-specific capabilities to function execution
|
|
274
|
+
*/
|
|
275
|
+
export interface PikkuWorkflowWire {
|
|
276
|
+
/** The workflow name */
|
|
277
|
+
workflowName: string;
|
|
278
|
+
/** The current run ID */
|
|
279
|
+
runId: string;
|
|
280
|
+
/** Get the current workflow run */
|
|
281
|
+
getRun: () => Promise<WorkflowRun>;
|
|
282
|
+
/** Execute a workflow step (overloaded - RPC or inline form) */
|
|
283
|
+
do: WorkflowWireDoRPC & WorkflowWireDoInline;
|
|
284
|
+
/** Sleep for a duration */
|
|
285
|
+
sleep: WorkflowWireSleep;
|
|
286
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { pikkuState } from '../../../pikku-state.js';
|
|
2
|
+
import { addFunction } from '../../../function/function-runner.js';
|
|
3
|
+
/**
|
|
4
|
+
* Add a workflow to the system
|
|
5
|
+
* This is called by the generated workflow wirings
|
|
6
|
+
*/
|
|
7
|
+
export const addWorkflow = (workflowName, workflowFunc) => {
|
|
8
|
+
// Get workflow metadata from inspector
|
|
9
|
+
const meta = pikkuState(null, 'workflows', 'meta');
|
|
10
|
+
const workflowMeta = meta[workflowName];
|
|
11
|
+
if (!workflowMeta) {
|
|
12
|
+
throw new Error(`Workflow metadata not found for '${workflowName}'. Make sure to run the CLI to generate metadata.`);
|
|
13
|
+
}
|
|
14
|
+
// Store workflow definition in state
|
|
15
|
+
const registrations = pikkuState(null, 'workflows', 'registrations');
|
|
16
|
+
registrations.set(workflowName, {
|
|
17
|
+
name: workflowName,
|
|
18
|
+
func: workflowFunc,
|
|
19
|
+
});
|
|
20
|
+
// Register the function with pikku
|
|
21
|
+
addFunction(workflowMeta.pikkuFuncName, workflowFunc);
|
|
22
|
+
};
|