@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,27 @@
1
+ import { pikkuState } from '../../../pikku-state.js'
2
+ import { addFunction } from '../../../function/function-runner.js'
3
+
4
+ /**
5
+ * Add a workflow to the system
6
+ * This is called by the generated workflow wirings
7
+ */
8
+ export const addWorkflow = (workflowName: string, workflowFunc: any) => {
9
+ // Get workflow metadata from inspector
10
+ const meta = pikkuState(null, 'workflows', 'meta')
11
+ const workflowMeta = meta[workflowName]
12
+ if (!workflowMeta) {
13
+ throw new Error(
14
+ `Workflow metadata not found for '${workflowName}'. Make sure to run the CLI to generate metadata.`
15
+ )
16
+ }
17
+
18
+ // Store workflow definition in state
19
+ const registrations = pikkuState(null, 'workflows', 'registrations')
20
+ registrations.set(workflowName, {
21
+ name: workflowName,
22
+ func: workflowFunc,
23
+ })
24
+
25
+ // Register the function with pikku
26
+ addFunction(workflowMeta.pikkuFuncName, workflowFunc)
27
+ }
@@ -0,0 +1,227 @@
1
+ import type {
2
+ GraphNodeConfig,
3
+ NextConfig,
4
+ RefValue,
5
+ } from './workflow-graph.types.js'
6
+
7
+ /**
8
+ * Standard RPC handler interface (matches generated FlattenedRPCMap entries)
9
+ */
10
+ export interface RPCHandler<I = any, O = any> {
11
+ input: I
12
+ output: O
13
+ }
14
+
15
+ /**
16
+ * Compute output types for all nodes based on their func (RPC name)
17
+ */
18
+ export type NodeOutputMap<
19
+ Nodes extends Record<string, { func: string }>,
20
+ RPCMap extends Record<string, RPCHandler>,
21
+ > = {
22
+ [K in keyof Nodes]: Nodes[K]['func'] extends keyof RPCMap
23
+ ? RPCMap[Nodes[K]['func']]['output']
24
+ : unknown
25
+ }
26
+
27
+ /**
28
+ * Node definition with RPC name reference
29
+ */
30
+ export interface GraphNodeDef<
31
+ RPCMap extends Record<string, RPCHandler>,
32
+ NodeIds extends string = string,
33
+ NodeOutputs extends Record<string, any> = Record<string, any>,
34
+ > {
35
+ /** RPC function name (must be a key in RPCMap) */
36
+ func: keyof RPCMap & string
37
+ /** Input mapping - reference outputs from other nodes with autocomplete */
38
+ input?: (
39
+ ref: <N extends keyof NodeOutputs & string>(
40
+ nodeId: N,
41
+ path: keyof NodeOutputs[N] & string
42
+ ) => RefValue
43
+ ) => Record<string, unknown>
44
+ /** Next node(s) to execute */
45
+ next?: NextConfig<NodeIds>
46
+ /** Error handling - node(s) to execute on error */
47
+ onError?: NodeIds | NodeIds[]
48
+ }
49
+
50
+ /**
51
+ * Helper to create a type-safe graph definition with RPC autocomplete.
52
+ * Pass node IDs as first type parameter for type-safe next/ref/output key completion.
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * // Import the typed graph from generated types
57
+ * import { pikkuWorkflowGraph, wireWorkflow } from './.pikku/workflow/pikku-workflow-types.gen.js'
58
+ *
59
+ * export const myGraph = pikkuWorkflowGraph({
60
+ * name: 'myWorkflow',
61
+ * nodes: {
62
+ * entry: 'createUserProfile', // autocompletes RPC names
63
+ * sendWelcome: 'sendEmail',
64
+ * },
65
+ * config: {
66
+ * entry: { next: 'sendWelcome' },
67
+ * sendWelcome: {
68
+ * input: (ref) => ({
69
+ * to: ref('entry', 'email'), // 'entry' and 'email' both autocomplete
70
+ * }),
71
+ * },
72
+ * },
73
+ * })
74
+ *
75
+ * wireWorkflow({
76
+ * wires: { http: { route: '/start', method: 'post' } },
77
+ * graph: myGraph,
78
+ * })
79
+ * ```
80
+ */
81
+ /**
82
+ * Type to compute output types for all nodes based on their func mapping
83
+ */
84
+ type ComputeNodeOutputs<
85
+ FuncMap extends Record<string, string>,
86
+ RPCMap extends Record<string, RPCHandler>,
87
+ > = {
88
+ [K in keyof FuncMap]: FuncMap[K] extends keyof RPCMap
89
+ ? RPCMap[FuncMap[K]]['output']
90
+ : unknown
91
+ }
92
+
93
+ /**
94
+ * Type to compute input types for all nodes based on their func mapping
95
+ */
96
+ type ComputeNodeInputs<
97
+ FuncMap extends Record<string, string>,
98
+ RPCMap extends Record<string, RPCHandler>,
99
+ > = {
100
+ [K in keyof FuncMap]: FuncMap[K] extends keyof RPCMap
101
+ ? RPCMap[FuncMap[K]]['input']
102
+ : unknown
103
+ }
104
+
105
+ /**
106
+ * Typed ref value - carries the type of the referenced field as a phantom type.
107
+ * At runtime this is just RefValue, but TypeScript tracks the type T.
108
+ */
109
+ export type TypedRef<T> = RefValue & { __phantomType?: T }
110
+
111
+ /**
112
+ * Map input type fields to allow TypedRef of matching type as an alternative
113
+ */
114
+ type InputWithRefs<T> = {
115
+ [K in keyof T]: T[K] | TypedRef<T[K]>
116
+ }
117
+
118
+ export function createGraph<RPCMap extends Record<string, RPCHandler>>() {
119
+ return <const FuncMap extends Record<string, keyof RPCMap & string>>(
120
+ funcMap: FuncMap,
121
+ nodesOrBuilder?:
122
+ | GraphNodeConfigMap<FuncMap, RPCMap>
123
+ | ((nodes: FuncMap) => GraphNodeConfigMap<FuncMap, RPCMap>)
124
+ ): Record<
125
+ Extract<keyof FuncMap, string>,
126
+ GraphNodeConfig<Extract<keyof FuncMap, string>>
127
+ > => {
128
+ type NodeIds = Extract<keyof FuncMap, string>
129
+
130
+ // If no nodes provided, return just the funcMap converted to graph nodes
131
+ if (!nodesOrBuilder) {
132
+ const result: Record<string, GraphNodeConfig<string>> = {}
133
+ for (const [nodeId, rpcName] of Object.entries(funcMap)) {
134
+ result[nodeId] = {
135
+ func: rpcName as string,
136
+ }
137
+ }
138
+ return result as Record<NodeIds, GraphNodeConfig<NodeIds>>
139
+ }
140
+
141
+ const nodes =
142
+ typeof nodesOrBuilder === 'function'
143
+ ? nodesOrBuilder(funcMap)
144
+ : nodesOrBuilder
145
+
146
+ const result: Record<string, GraphNodeConfig<string>> = {}
147
+
148
+ for (const [nodeId, def] of Object.entries(nodes) as [string, any][]) {
149
+ result[nodeId] = {
150
+ func: funcMap[nodeId] as string,
151
+ input: def?.input as any,
152
+ next: def?.next,
153
+ onError: def?.onError,
154
+ }
155
+ }
156
+
157
+ return result as Record<NodeIds, GraphNodeConfig<NodeIds>>
158
+ }
159
+ }
160
+
161
+ /**
162
+ * Type helper for node configuration map
163
+ */
164
+ type GraphNodeConfigMap<
165
+ FuncMap extends Record<string, string>,
166
+ RPCMap extends Record<string, RPCHandler>,
167
+ > = {
168
+ [K in Extract<keyof FuncMap, string>]?: {
169
+ next?: NextConfig<Extract<keyof FuncMap, string>>
170
+ input?: (
171
+ ref: <
172
+ N extends Extract<keyof FuncMap, string>,
173
+ P extends keyof ComputeNodeOutputs<FuncMap, RPCMap>[N] & string,
174
+ >(
175
+ nodeId: N,
176
+ path: P
177
+ ) => TypedRef<ComputeNodeOutputs<FuncMap, RPCMap>[N][P]>
178
+ ) => InputWithRefs<ComputeNodeInputs<FuncMap, RPCMap>[K]>
179
+ onError?: Extract<keyof FuncMap, string> | Extract<keyof FuncMap, string>[]
180
+ }
181
+ }
182
+
183
+ /**
184
+ * Untyped graph for use without RPC map.
185
+ * Pass node IDs as type parameter for type-safe next/ref, or omit for untyped usage.
186
+ *
187
+ * @example
188
+ * ```typescript
189
+ * // Typed usage - next and ref are type-checked
190
+ * graph<'entry' | 'sendWelcome'>({
191
+ * entry: { func: 'createUserProfile', next: 'sendWelcome' },
192
+ * sendWelcome: { func: 'sendEmail', input: (ref) => ({ to: ref('entry', 'email') }) },
193
+ * })
194
+ *
195
+ * // Untyped usage - no type checking on next/ref
196
+ * graph({
197
+ * entry: { func: 'createUserProfile', next: 'sendWelcome' },
198
+ * sendWelcome: { func: 'sendEmail' },
199
+ * })
200
+ * ```
201
+ */
202
+ export function graph<NodeIds extends string = string>(
203
+ nodes: Record<
204
+ NodeIds,
205
+ {
206
+ func: string
207
+ next?: NextConfig<NodeIds>
208
+ input?: (
209
+ ref: (nodeId: NodeIds, path: string) => RefValue
210
+ ) => Record<string, unknown>
211
+ onError?: NodeIds | NodeIds[]
212
+ }
213
+ >
214
+ ): Record<NodeIds, GraphNodeConfig<NodeIds>> {
215
+ const result: Record<string, GraphNodeConfig<string>> = {}
216
+
217
+ for (const [nodeId, def] of Object.entries(nodes) as [string, any][]) {
218
+ result[nodeId] = {
219
+ func: def.func,
220
+ input: def.input as any,
221
+ next: def.next,
222
+ onError: def.onError,
223
+ }
224
+ }
225
+
226
+ return result as Record<NodeIds, GraphNodeConfig<NodeIds>>
227
+ }