@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.
Files changed (184) hide show
  1. package/CHANGELOG.md +35 -1
  2. package/dist/errors/error-handler.js +3 -3
  3. package/dist/errors/errors.d.ts +1 -1
  4. package/dist/errors/errors.js +3 -3
  5. package/dist/function/function-runner.d.ts +8 -8
  6. package/dist/function/function-runner.js +88 -23
  7. package/dist/function/functions.types.d.ts +104 -33
  8. package/dist/function/functions.types.js +50 -7
  9. package/dist/handle-error.d.ts +1 -1
  10. package/dist/handle-error.js +1 -1
  11. package/dist/index.d.ts +6 -2
  12. package/dist/index.js +5 -2
  13. package/dist/middleware/auth-apikey.d.ts +1 -1
  14. package/dist/middleware/auth-apikey.js +3 -3
  15. package/dist/middleware/auth-bearer.d.ts +1 -1
  16. package/dist/middleware/auth-bearer.js +3 -3
  17. package/dist/middleware/auth-cookie.d.ts +1 -1
  18. package/dist/middleware/auth-cookie.js +5 -5
  19. package/dist/middleware/timeout.d.ts +1 -1
  20. package/dist/middleware/timeout.js +3 -2
  21. package/dist/middleware-runner.d.ts +6 -20
  22. package/dist/middleware-runner.js +19 -20
  23. package/dist/permissions.d.ts +6 -89
  24. package/dist/permissions.js +24 -81
  25. package/dist/pikku-state.d.ts +32 -104
  26. package/dist/pikku-state.js +140 -66
  27. package/dist/schema.d.ts +8 -6
  28. package/dist/schema.js +25 -13
  29. package/dist/services/user-session-service.d.ts +7 -3
  30. package/dist/services/user-session-service.js +8 -1
  31. package/dist/services/workflow-service.d.ts +38 -0
  32. package/dist/services/workflow-service.js +1 -0
  33. package/dist/types/core.types.d.ts +40 -41
  34. package/dist/types/core.types.js +4 -15
  35. package/dist/types/state.types.d.ts +135 -0
  36. package/dist/types/state.types.js +1 -0
  37. package/dist/utils.d.ts +9 -1
  38. package/dist/utils.js +48 -3
  39. package/dist/wirings/channel/channel-common.js +2 -3
  40. package/dist/wirings/channel/channel-handler.d.ts +3 -2
  41. package/dist/wirings/channel/channel-handler.js +7 -10
  42. package/dist/wirings/channel/channel-runner.d.ts +2 -2
  43. package/dist/wirings/channel/channel-runner.js +5 -5
  44. package/dist/wirings/channel/channel.types.d.ts +10 -5
  45. package/dist/wirings/channel/local/local-channel-runner.d.ts +1 -1
  46. package/dist/wirings/channel/local/local-channel-runner.js +19 -26
  47. package/dist/wirings/channel/log-channels.js +1 -1
  48. package/dist/wirings/channel/serverless/serverless-channel-runner.d.ts +1 -1
  49. package/dist/wirings/channel/serverless/serverless-channel-runner.js +43 -48
  50. package/dist/wirings/cli/channel/cli-channel-runner.js +3 -2
  51. package/dist/wirings/cli/cli-runner.d.ts +5 -5
  52. package/dist/wirings/cli/cli-runner.js +20 -38
  53. package/dist/wirings/cli/cli.types.d.ts +16 -11
  54. package/dist/wirings/forge-node/forge-node.types.d.ts +120 -0
  55. package/dist/wirings/forge-node/forge-node.types.js +38 -0
  56. package/dist/wirings/forge-node/index.d.ts +1 -0
  57. package/dist/wirings/forge-node/index.js +1 -0
  58. package/dist/wirings/http/http-runner.d.ts +8 -8
  59. package/dist/wirings/http/http-runner.js +46 -51
  60. package/dist/wirings/http/http.types.d.ts +19 -11
  61. package/dist/wirings/http/log-http-routes.js +1 -1
  62. package/dist/wirings/http/routers/path-to-regex.js +2 -2
  63. package/dist/wirings/mcp/mcp-runner.d.ts +2 -2
  64. package/dist/wirings/mcp/mcp-runner.js +33 -40
  65. package/dist/wirings/mcp/mcp.types.d.ts +6 -0
  66. package/dist/wirings/queue/queue-runner.d.ts +3 -3
  67. package/dist/wirings/queue/queue-runner.js +13 -23
  68. package/dist/wirings/queue/queue.types.d.ts +4 -11
  69. package/dist/wirings/rpc/index.d.ts +1 -1
  70. package/dist/wirings/rpc/index.js +1 -1
  71. package/dist/wirings/rpc/rpc-runner.d.ts +17 -16
  72. package/dist/wirings/rpc/rpc-runner.js +98 -61
  73. package/dist/wirings/rpc/rpc-types.d.ts +7 -0
  74. package/dist/wirings/scheduler/log-schedulers.js +1 -1
  75. package/dist/wirings/scheduler/scheduler-runner.d.ts +3 -3
  76. package/dist/wirings/scheduler/scheduler-runner.js +18 -29
  77. package/dist/wirings/scheduler/scheduler.types.d.ts +3 -8
  78. package/dist/wirings/trigger/index.d.ts +2 -0
  79. package/dist/wirings/trigger/index.js +2 -0
  80. package/dist/wirings/trigger/trigger-runner.d.ts +29 -0
  81. package/dist/wirings/trigger/trigger-runner.js +57 -0
  82. package/dist/wirings/trigger/trigger.types.d.ts +42 -0
  83. package/dist/wirings/trigger/trigger.types.js +1 -0
  84. package/dist/wirings/workflow/dsl/index.d.ts +5 -0
  85. package/dist/wirings/workflow/dsl/index.js +4 -0
  86. package/dist/wirings/workflow/dsl/workflow-dsl.types.d.ts +286 -0
  87. package/dist/wirings/workflow/dsl/workflow-dsl.types.js +5 -0
  88. package/dist/wirings/workflow/dsl/workflow-runner.d.ts +5 -0
  89. package/dist/wirings/workflow/dsl/workflow-runner.js +22 -0
  90. package/dist/wirings/workflow/graph/graph-node.d.ts +122 -0
  91. package/dist/wirings/workflow/graph/graph-node.js +58 -0
  92. package/dist/wirings/workflow/graph/graph-runner.d.ts +35 -0
  93. package/dist/wirings/workflow/graph/graph-runner.js +452 -0
  94. package/dist/wirings/workflow/graph/index.d.ts +3 -0
  95. package/dist/wirings/workflow/graph/index.js +3 -0
  96. package/dist/wirings/workflow/graph/workflow-graph.types.d.ts +95 -0
  97. package/dist/wirings/workflow/graph/workflow-graph.types.js +15 -0
  98. package/dist/wirings/workflow/index.d.ts +7 -3
  99. package/dist/wirings/workflow/index.js +6 -3
  100. package/dist/wirings/workflow/pikku-workflow-service.d.ts +108 -10
  101. package/dist/wirings/workflow/pikku-workflow-service.js +274 -165
  102. package/dist/wirings/workflow/wire-workflow.d.ts +42 -0
  103. package/dist/wirings/workflow/wire-workflow.js +53 -0
  104. package/dist/wirings/workflow/workflow-utils.d.ts +23 -0
  105. package/dist/wirings/workflow/workflow-utils.js +66 -0
  106. package/dist/wirings/workflow/workflow.types.d.ts +135 -128
  107. package/package.json +11 -2
  108. package/src/errors/error-handler.ts +3 -3
  109. package/src/errors/errors.ts +3 -3
  110. package/src/factory-functions.test.ts +9 -36
  111. package/src/function/function-runner.test.ts +58 -69
  112. package/src/function/function-runner.ts +130 -37
  113. package/src/function/functions.types.ts +193 -54
  114. package/src/handle-error.ts +1 -1
  115. package/src/index.ts +10 -5
  116. package/src/middleware/auth-apikey.test.ts +360 -0
  117. package/src/middleware/auth-apikey.ts +3 -7
  118. package/src/middleware/auth-bearer.test.ts +447 -0
  119. package/src/middleware/auth-bearer.ts +27 -33
  120. package/src/middleware/auth-cookie.test.ts +525 -0
  121. package/src/middleware/auth-cookie.ts +6 -6
  122. package/src/middleware/timeout.ts +4 -2
  123. package/src/middleware-runner.test.ts +58 -127
  124. package/src/middleware-runner.ts +29 -25
  125. package/src/permissions.test.ts +68 -130
  126. package/src/permissions.ts +41 -100
  127. package/src/pikku-state.ts +156 -201
  128. package/src/schema.ts +42 -13
  129. package/src/services/user-session-service.ts +14 -4
  130. package/src/services/workflow-service.ts +74 -0
  131. package/src/types/core.types.ts +79 -58
  132. package/src/types/state.types.ts +195 -0
  133. package/src/utils.ts +59 -4
  134. package/src/wirings/channel/channel-common.ts +4 -7
  135. package/src/wirings/channel/channel-handler.ts +17 -23
  136. package/src/wirings/channel/channel-runner.ts +7 -7
  137. package/src/wirings/channel/channel.types.ts +14 -11
  138. package/src/wirings/channel/local/local-channel-runner.test.ts +8 -4
  139. package/src/wirings/channel/local/local-channel-runner.ts +23 -38
  140. package/src/wirings/channel/log-channels.ts +1 -1
  141. package/src/wirings/channel/serverless/serverless-channel-runner.ts +53 -79
  142. package/src/wirings/cli/channel/cli-channel-runner.ts +4 -3
  143. package/src/wirings/cli/cli-runner.test.ts +26 -26
  144. package/src/wirings/cli/cli-runner.ts +34 -71
  145. package/src/wirings/cli/cli.types.ts +23 -16
  146. package/src/wirings/forge-node/forge-node.types.ts +135 -0
  147. package/src/wirings/forge-node/index.ts +1 -0
  148. package/src/wirings/http/http-runner.test.ts +10 -10
  149. package/src/wirings/http/http-runner.ts +56 -66
  150. package/src/wirings/http/http.types.ts +19 -14
  151. package/src/wirings/http/log-http-routes.ts +1 -1
  152. package/src/wirings/http/routers/path-to-regex.test.ts +30 -19
  153. package/src/wirings/http/routers/path-to-regex.ts +2 -2
  154. package/src/wirings/mcp/mcp-runner.ts +44 -69
  155. package/src/wirings/mcp/mcp.types.ts +6 -0
  156. package/src/wirings/queue/queue-runner.test.ts +641 -0
  157. package/src/wirings/queue/queue-runner.ts +16 -38
  158. package/src/wirings/queue/queue.types.ts +4 -11
  159. package/src/wirings/rpc/index.ts +1 -1
  160. package/src/wirings/rpc/rpc-runner.ts +129 -77
  161. package/src/wirings/rpc/rpc-types.ts +8 -0
  162. package/src/wirings/scheduler/log-schedulers.ts +1 -1
  163. package/src/wirings/scheduler/scheduler-runner.test.ts +627 -0
  164. package/src/wirings/scheduler/scheduler-runner.ts +29 -55
  165. package/src/wirings/scheduler/scheduler.types.ts +3 -9
  166. package/src/wirings/trigger/index.ts +2 -0
  167. package/src/wirings/trigger/trigger-runner.ts +96 -0
  168. package/src/wirings/trigger/trigger.types.ts +56 -0
  169. package/src/wirings/workflow/dsl/index.ts +30 -0
  170. package/src/wirings/workflow/dsl/workflow-dsl.types.ts +312 -0
  171. package/src/wirings/workflow/dsl/workflow-runner.ts +27 -0
  172. package/src/wirings/workflow/graph/graph-node.ts +227 -0
  173. package/src/wirings/workflow/graph/graph-runner.ts +694 -0
  174. package/src/wirings/workflow/graph/index.ts +3 -0
  175. package/src/wirings/workflow/graph/workflow-graph.types.ts +126 -0
  176. package/src/wirings/workflow/index.ts +60 -12
  177. package/src/wirings/workflow/pikku-workflow-service.ts +401 -195
  178. package/src/wirings/workflow/wire-workflow.ts +94 -0
  179. package/src/wirings/workflow/workflow-utils.ts +120 -0
  180. package/src/wirings/workflow/workflow.types.ts +167 -188
  181. package/tsconfig.tsbuildinfo +1 -1
  182. package/dist/wirings/workflow/workflow-runner.d.ts +0 -35
  183. package/dist/wirings/workflow/workflow-runner.js +0 -68
  184. package/src/wirings/workflow/workflow-runner.ts +0 -85
@@ -0,0 +1,94 @@
1
+ import { pikkuState } from '../../pikku-state.js'
2
+ import type { WorkflowWires } from './workflow.types.js'
3
+ import type { GraphNodeConfig } from './graph/workflow-graph.types.js'
4
+
5
+ /**
6
+ * Workflow definition with DSL function
7
+ */
8
+ export interface WorkflowDefinitionFunc {
9
+ wires: WorkflowWires
10
+ func: { func: (...args: any[]) => any } | ((...args: any[]) => any)
11
+ }
12
+
13
+ /**
14
+ * Workflow definition with graph
15
+ */
16
+ export interface WorkflowDefinitionGraph<
17
+ T extends Record<string, GraphNodeConfig<string>> = Record<
18
+ string,
19
+ GraphNodeConfig<string>
20
+ >,
21
+ > {
22
+ wires: WorkflowWires
23
+ graph: T
24
+ }
25
+
26
+ /**
27
+ * Union type for workflow definitions
28
+ */
29
+ export type WorkflowDefinition<
30
+ T extends Record<string, GraphNodeConfig<string>> = Record<
31
+ string,
32
+ GraphNodeConfig<string>
33
+ >,
34
+ > = WorkflowDefinitionFunc | WorkflowDefinitionGraph<T>
35
+
36
+ /**
37
+ * Type guard for DSL workflow definition
38
+ */
39
+ function isWorkflowDefinitionFunc(
40
+ def: WorkflowDefinition
41
+ ): def is WorkflowDefinitionFunc {
42
+ return 'func' in def
43
+ }
44
+
45
+ /**
46
+ * Type guard for graph workflow definition
47
+ */
48
+ function isWorkflowDefinitionGraph(
49
+ def: WorkflowDefinition
50
+ ): def is WorkflowDefinitionGraph {
51
+ return 'graph' in def
52
+ }
53
+
54
+ /**
55
+ * Wire a workflow with triggers.
56
+ * Accepts either a DSL function (func) or a graph definition (graph).
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * // DSL workflow
61
+ * wireWorkflow({
62
+ * wires: { http: { route: '/start', method: 'post' } },
63
+ * func: myWorkflowFunc,
64
+ * })
65
+ *
66
+ * // Graph workflow
67
+ * wireWorkflow({
68
+ * wires: { http: { route: '/graph-start', method: 'post' } },
69
+ * graph: myGraphWorkflow,
70
+ * })
71
+ * ```
72
+ */
73
+ export function wireWorkflow<T extends Record<string, GraphNodeConfig<string>>>(
74
+ definition: WorkflowDefinitionFunc | WorkflowDefinitionGraph<T>
75
+ ): void {
76
+ if (isWorkflowDefinitionFunc(definition)) {
77
+ // DSL workflow - store wiring for later registration by inspector
78
+ const wirings = pikkuState(null, 'workflows', 'wirings')
79
+ // The wiring will be matched to the function by the inspector
80
+ // Store the func reference and wires
81
+ wirings.set(definition.func, {
82
+ wires: definition.wires,
83
+ func: definition.func,
84
+ })
85
+ } else if (isWorkflowDefinitionGraph(definition)) {
86
+ // Graph workflow - store for registration
87
+ const graphWirings = pikkuState(null, 'workflows', 'graphWirings')
88
+ // Store the graph reference and wires
89
+ graphWirings.set(definition.graph, {
90
+ wires: definition.wires,
91
+ graph: definition.graph,
92
+ })
93
+ }
94
+ }
@@ -0,0 +1,120 @@
1
+ import { pikkuState } from '../../pikku-state.js'
2
+ import { NotFoundError } from '../../errors/errors.js'
3
+ import {
4
+ PikkuWire,
5
+ CreateWireServices,
6
+ CoreSingletonServices,
7
+ CoreServices,
8
+ CoreUserSession,
9
+ } from '../../types/core.types.js'
10
+ import { HTTPWiringMeta } from '../http/http.types.js'
11
+ import { ContextAwareRPCService } from '../rpc/rpc-runner.js'
12
+
13
+ /**
14
+ * Result of finding a workflow by HTTP wire
15
+ */
16
+ export interface WorkflowHttpMatch {
17
+ workflowName: string
18
+ startNode?: string
19
+ }
20
+
21
+ /**
22
+ * Find workflow name by matching route and method against workflow wires
23
+ */
24
+ export const findWorkflowByHttpWire = (
25
+ route: string,
26
+ method: string
27
+ ): WorkflowHttpMatch | undefined => {
28
+ const wirings = pikkuState(null, 'workflows', 'wirings')
29
+ const graphWirings = pikkuState(null, 'workflows', 'graphWirings')
30
+
31
+ // Check DSL workflow wirings
32
+ for (const [, wiring] of wirings) {
33
+ const httpWires = wiring.wires?.http
34
+ if (httpWires) {
35
+ const matchedWire = httpWires.find(
36
+ (w) => w.route === route && w.method === method
37
+ )
38
+ if (matchedWire) {
39
+ const meta = pikkuState(null, 'workflows', 'meta')
40
+ for (const [name, workflowMeta] of Object.entries(meta)) {
41
+ if (workflowMeta.source !== 'graph') {
42
+ const registrations = pikkuState(null, 'workflows', 'registrations')
43
+ if (registrations.has(name)) {
44
+ return { workflowName: name, startNode: matchedWire.startNode }
45
+ }
46
+ }
47
+ }
48
+ }
49
+ }
50
+ }
51
+
52
+ // Check graph workflow wirings
53
+ for (const [, wiring] of graphWirings) {
54
+ const httpWires = wiring.wires?.http
55
+ if (httpWires) {
56
+ const matchedWire = httpWires.find(
57
+ (w) => w.route === route && w.method === method
58
+ )
59
+ if (matchedWire) {
60
+ const meta = pikkuState(null, 'workflows', 'meta')
61
+ for (const [name, workflowMeta] of Object.entries(meta)) {
62
+ if (workflowMeta.source === 'graph') {
63
+ return { workflowName: name, startNode: matchedWire.startNode }
64
+ }
65
+ }
66
+ }
67
+ }
68
+ }
69
+
70
+ return undefined
71
+ }
72
+
73
+ /**
74
+ * Start a workflow triggered by an HTTP wire
75
+ * The workflow is responsible for handling the HTTP response
76
+ */
77
+ export const startWorkflowByHttpWire = async (
78
+ singletonServices: CoreSingletonServices,
79
+ createWireServices:
80
+ | CreateWireServices<
81
+ CoreSingletonServices,
82
+ CoreServices<CoreSingletonServices>,
83
+ CoreUserSession
84
+ >
85
+ | undefined,
86
+ matchedRoute: {
87
+ matchedPath: any
88
+ params: any
89
+ route: any
90
+ meta: HTTPWiringMeta
91
+ },
92
+ wire: PikkuWire
93
+ ): Promise<void> => {
94
+ const { meta } = matchedRoute
95
+
96
+ const match = findWorkflowByHttpWire(meta.route, meta.method)
97
+ if (!match) {
98
+ throw new NotFoundError(
99
+ `No workflow found for route ${meta.method.toUpperCase()} ${meta.route}`
100
+ )
101
+ }
102
+
103
+ const workflowService = singletonServices.workflowService
104
+ if (!workflowService) {
105
+ throw new Error('WorkflowService not available')
106
+ }
107
+
108
+ const data = await wire.http!.request!.data()
109
+
110
+ const wireServices = createWireServices?.(singletonServices, wire)
111
+ const rpcService = new ContextAwareRPCService(
112
+ { ...singletonServices, ...wireServices },
113
+ wire,
114
+ {}
115
+ )
116
+ await workflowService.startWorkflow(match.workflowName, data, rpcService, {
117
+ inline: true,
118
+ startNode: match.startNode,
119
+ })
120
+ }
@@ -1,13 +1,40 @@
1
- import {
2
- PikkuDocs,
3
- MiddlewareMetadata,
4
- SerializedError,
5
- CoreSingletonServices,
6
- CreateSessionServices,
7
- CoreConfig,
8
- } from '../../types/core.types.js'
1
+ import { SerializedError, CommonWireMeta } from '../../types/core.types.js'
9
2
  import { CorePikkuFunctionConfig } from '../../function/functions.types.js'
10
3
 
4
+ // Re-export WorkflowService from services module
5
+ export type { WorkflowService } from '../../services/workflow-service.js'
6
+
7
+ // Re-export DSL types from dsl module
8
+ export type {
9
+ WorkflowStepOptions,
10
+ WorkflowWireDoRPC,
11
+ WorkflowWireDoInline,
12
+ WorkflowWireSleep,
13
+ InputSource,
14
+ OutputBinding,
15
+ RpcStepMeta,
16
+ SimpleCondition,
17
+ Condition,
18
+ BranchCase,
19
+ BranchStepMeta,
20
+ ParallelGroupStepMeta,
21
+ FanoutStepMeta,
22
+ ReturnStepMeta,
23
+ InlineStepMeta,
24
+ SleepStepMeta,
25
+ CancelStepMeta,
26
+ SetStepMeta,
27
+ SwitchCaseMeta,
28
+ SwitchStepMeta,
29
+ FilterStepMeta,
30
+ ArrayPredicateStepMeta,
31
+ WorkflowStepMeta,
32
+ WorkflowStepWire,
33
+ PikkuWorkflowWire,
34
+ } from './dsl/workflow-dsl.types.js'
35
+
36
+ import type { WorkflowStepMeta } from './dsl/workflow-dsl.types.js'
37
+
11
38
  export interface WorkflowServiceConfig {
12
39
  retries: number
13
40
  retryDelay: number
@@ -16,6 +43,90 @@ export interface WorkflowServiceConfig {
16
43
  sleeperRPCName: string
17
44
  }
18
45
 
46
+ /**
47
+ * HTTP wire configuration for workflows
48
+ */
49
+ export interface WorkflowHTTPWire {
50
+ route: string
51
+ method: 'get' | 'post' | 'put' | 'patch' | 'delete'
52
+ startNode: string
53
+ }
54
+
55
+ /**
56
+ * Channel wire configuration for workflows
57
+ */
58
+ export interface WorkflowChannelWire {
59
+ name: string
60
+ onConnect?: string
61
+ onDisconnect?: string
62
+ onMessage?: string
63
+ }
64
+
65
+ /**
66
+ * Queue wire configuration for workflows
67
+ */
68
+ export interface WorkflowQueueWire {
69
+ name: string
70
+ startNode: string
71
+ }
72
+
73
+ /**
74
+ * CLI wire configuration for workflows
75
+ */
76
+ export interface WorkflowCliWire {
77
+ command: string
78
+ startNode: string
79
+ }
80
+
81
+ /**
82
+ * MCP wire configurations for workflows
83
+ */
84
+ export interface WorkflowMcpWires {
85
+ tool?: Array<{ name: string; startNode: string }>
86
+ prompt?: Array<{ name: string; startNode: string }>
87
+ resource?: Array<{ uri: string; startNode: string }>
88
+ }
89
+
90
+ /**
91
+ * Schedule wire configuration for workflows
92
+ */
93
+ export interface WorkflowScheduleWire {
94
+ cron?: string
95
+ interval?: string
96
+ startNode: string
97
+ }
98
+
99
+ /**
100
+ * Trigger wire configuration for workflows
101
+ */
102
+ export interface WorkflowTriggerWire {
103
+ name: string
104
+ startNode: string
105
+ }
106
+
107
+ /**
108
+ * Wire configuration for workflows
109
+ * Defines how a workflow can be triggered
110
+ */
111
+ export interface WorkflowWires {
112
+ /** API entry point - node ID for startWorkflow() calls */
113
+ api?: string
114
+ /** HTTP triggers */
115
+ http?: WorkflowHTTPWire[]
116
+ /** Channel triggers */
117
+ channel?: WorkflowChannelWire[]
118
+ /** Queue triggers */
119
+ queue?: WorkflowQueueWire[]
120
+ /** CLI triggers */
121
+ cli?: WorkflowCliWire[]
122
+ /** MCP triggers (tool, prompt, resource) */
123
+ mcp?: WorkflowMcpWires
124
+ /** Schedule triggers */
125
+ schedule?: WorkflowScheduleWire[]
126
+ /** Named trigger wires */
127
+ trigger?: WorkflowTriggerWire[]
128
+ }
129
+
19
130
  /**
20
131
  * Workflow run status
21
132
  */
@@ -47,6 +158,8 @@ export interface WorkflowRun {
47
158
  output?: any
48
159
  /** Error (if failed) */
49
160
  error?: SerializedError
161
+ /** If true, workflow executes inline without queues */
162
+ inline?: boolean
50
163
  /** Creation timestamp */
51
164
  createdAt: Date
52
165
  /** Last update timestamp */
@@ -97,8 +210,6 @@ export type CoreWorkflow<
97
210
  > = {
98
211
  /** Unique workflow name */
99
212
  name: string
100
- /** Description of the workflow */
101
- description?: string
102
213
  /** The workflow function */
103
214
  func: PikkuFunctionConfig
104
215
  /** Middleware chain for this workflow */
@@ -107,118 +218,6 @@ export type CoreWorkflow<
107
218
  permissions?: PikkuFunctionConfig['permissions']
108
219
  /** Tags for organization and filtering */
109
220
  tags?: string[]
110
- /** Documentation metadata */
111
- docs?: PikkuDocs
112
- }
113
-
114
- /**
115
- * Workflow step options
116
- */
117
- export interface WorkflowStepOptions {
118
- /** Display name for logs/UI (optional, doesn't affect execution) */
119
- description?: string
120
- /** Number of retry attempts for failed steps (only applies to local execution) */
121
- retries?: number
122
- /** Delay between retry attempts (e.g., '1s', '2s', '2min') */
123
- retryDelay?: string | number
124
- // Future: timeout, failFast, priority
125
- }
126
-
127
- /**
128
- * Type signature for workflow.do() RPC form - used by inspector
129
- */
130
- export type WorkflowInteractionDoRPC = <TOutput = any, TInput = any>(
131
- stepName: string,
132
- rpcName: string,
133
- data: TInput,
134
- options?: WorkflowStepOptions
135
- ) => Promise<TOutput>
136
-
137
- /**
138
- * Type signature for workflow.do() inline form - used by inspector
139
- */
140
- export type WorkflowInteractionDoInline = <T>(
141
- stepName: string,
142
- fn: () => Promise<T> | T,
143
- options?: WorkflowStepOptions
144
- ) => Promise<T>
145
-
146
- /**
147
- * Type signature for workflow.sleep() - used by inspector
148
- */
149
- export type WorkflowInteractionSleep = (
150
- stepName: string,
151
- duration: string
152
- ) => Promise<void>
153
-
154
- /**
155
- * Workflow step metadata (extracted by inspector)
156
- */
157
- export type WorkflowStepMeta =
158
- | {
159
- /** RPC form - generates queue worker */
160
- type: 'rpc'
161
- /** Cache key (stepName from workflow.do) */
162
- stepName: string
163
- /** RPC to invoke */
164
- rpcName: string
165
- /** Display name */
166
- description?: string
167
- /** Step options */
168
- options?: WorkflowStepOptions
169
- }
170
- | {
171
- /** Inline form - local execution */
172
- type: 'inline'
173
- /** Cache key (stepName from workflow.do) */
174
- stepName: string
175
- /** Display name */
176
- description?: string
177
- /** Step options */
178
- options?: WorkflowStepOptions
179
- }
180
- | {
181
- /** Sleep step */
182
- type: 'sleep'
183
- /** Cache key (stepName from workflow.sleep) */
184
- stepName: string
185
- /** Sleep duration */
186
- duration: string | number
187
- }
188
-
189
- /**
190
- * Workflow step interaction context for RPC functions
191
- * Provides step-level metadata including retry attempt tracking
192
- */
193
- export interface WorkflowStepInteraction {
194
- /** The workflow run ID */
195
- runId: string
196
- /** The unique step ID */
197
- stepId: string
198
- /** Current attempt number (1-indexed, increments on retry) */
199
- attemptCount: number
200
- }
201
-
202
- /**
203
- * Workflow interaction object for middleware
204
- * Provides workflow-specific capabilities to function execution
205
- */
206
- export interface PikkuWorkflowInteraction {
207
- /** The workflow name */
208
- workflowName: string
209
- /** The current run ID */
210
- runId: string
211
- /** Get the current workflow run */
212
- getRun: () => Promise<WorkflowRun>
213
-
214
- /** Execute a workflow step (overloaded - RPC or inline form) */
215
- do: WorkflowInteractionDoRPC & WorkflowInteractionDoInline
216
-
217
- /** Sleep for a duration */
218
- sleep: WorkflowInteractionSleep
219
-
220
- /** Cancel the current workflow run */
221
- cancel: (reason?: string) => Promise<void>
222
221
  }
223
222
 
224
223
  /**
@@ -234,84 +233,64 @@ export interface PikkuWorkflow {
234
233
  }
235
234
 
236
235
  /**
237
- * Workflows metadata for inspector/CLI
236
+ * Context variable definition (serialized from Zod schema or type inference)
237
+ */
238
+ export interface ContextVariable {
239
+ /** Variable type */
240
+ type: 'string' | 'number' | 'boolean' | 'object' | 'array'
241
+ /** Default value */
242
+ default?: unknown
243
+ /** Description for UI/docs */
244
+ description?: string
245
+ }
246
+
247
+ /**
248
+ * Workflow context - state variables with defaults and types
249
+ */
250
+ export type WorkflowContext = Record<string, ContextVariable>
251
+
252
+ /**
253
+ * Workflows metadata for inspector/CLI (DSL step-based format)
238
254
  */
239
255
  export type WorkflowsMeta = Record<
240
256
  string,
241
- {
242
- pikkuFuncName: string
257
+ CommonWireMeta & {
243
258
  workflowName: string
244
- description?: string
245
- session?: undefined
246
- docs?: PikkuDocs
247
- tags?: string[]
248
- middleware?: MiddlewareMetadata[]
249
259
  steps: WorkflowStepMeta[]
260
+ context?: WorkflowContext
261
+ dsl?: boolean
250
262
  }
251
263
  >
252
264
 
253
265
  /**
254
- * Interface for workflow orchestration
255
- * Handles workflow execution, replay, orchestration logic, and run-level state
266
+ * Unified workflow runtime meta (used by runtime to execute workflows)
267
+ * This is the format stored in pikkuState('workflows', 'meta')
268
+ * Both DSL and graph-based workflows are converted to this format
256
269
  */
257
- export interface WorkflowService {
258
- // Run-level state operations
259
- createRun(workflowName: string, input: any): Promise<string>
260
- getRun(id: string): Promise<WorkflowRun | null>
261
- getRunHistory(runId: string): Promise<Array<StepState & { stepName: string }>>
262
- updateRunStatus(
263
- id: string,
264
- status: WorkflowStatus,
265
- output?: any,
266
- error?: SerializedError
267
- ): Promise<void>
268
- withRunLock<T>(id: string, fn: () => Promise<T>): Promise<T>
269
- close(): Promise<void>
270
-
271
- // Orchestration operations
272
- resumeWorkflow(runId: string): Promise<void>
273
- setServices(
274
- singletonServices: CoreSingletonServices,
275
- createSessionServices: CreateSessionServices,
276
- config: CoreConfig
277
- ): void
278
- startWorkflow<I>(
279
- name: string,
280
- input: I,
281
- rpcService: any
282
- ): Promise<{ runId: string }>
283
- runWorkflowJob(runId: string, rpcService: any): Promise<void>
284
- orchestrateWorkflow(runId: string, rpcService: any): Promise<void>
285
- executeWorkflowSleep(runId: string, stepId: string): Promise<void>
286
-
287
- // Step-level state operations
288
- insertStepState(
289
- runId: string,
290
- stepName: string,
291
- rpcName: string,
292
- data: any,
293
- stepOptions?: { retries?: number; retryDelay?: string | number }
294
- ): Promise<StepState>
295
- getStepState(runId: string, stepName: string): Promise<StepState>
296
- setStepRunning(stepId: string): Promise<void>
297
- setStepScheduled(stepId: string): Promise<void>
298
- setStepResult(stepId: string, result: any): Promise<void>
299
- setStepError(stepId: string, error: Error): Promise<void>
300
- createRetryAttempt(
301
- stepId: string,
302
- status: 'pending' | 'running'
303
- ): Promise<StepState>
304
-
305
- // Step execution
306
- executeWorkflowStep(
307
- runId: string,
308
- stepName: string,
309
- rpcName: string | null,
310
- data: any,
311
- rpcService: any
312
- ): Promise<void>
270
+ export interface WorkflowRuntimeMeta {
271
+ /** Workflow name (used as key in registrations) */
272
+ name: string
273
+ /** Pikku function name (for execution) */
274
+ pikkuFuncName: string
275
+ /** Source type: 'dsl' (serializable), 'complex' (has inline steps), 'graph' */
276
+ source?: 'dsl' | 'complex' | 'graph'
277
+ /** Optional description */
278
+ description?: string
279
+ /** Tags for organization */
280
+ tags?: string[]
281
+ /** Wires - how the workflow is triggered */
282
+ wires?: WorkflowWires
283
+ /** Serialized nodes */
284
+ nodes?: Record<string, any>
285
+ /** Entry node IDs for graph workflows (computed at build time) */
286
+ entryNodeIds?: string[]
313
287
  }
314
288
 
289
+ /**
290
+ * Unified workflow runtime metadata map
291
+ */
292
+ export type WorkflowsRuntimeMeta = Record<string, WorkflowRuntimeMeta>
293
+
315
294
  /**
316
295
  * Worker input types for generated queue workers
317
296
  */