@pikku/core 0.12.4 → 0.12.5

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 (229) hide show
  1. package/CHANGELOG.md +11 -1
  2. package/dist/dev/hot-reload.d.ts +10 -0
  3. package/dist/dev/hot-reload.js +158 -0
  4. package/dist/middleware-runner.d.ts +1 -0
  5. package/dist/middleware-runner.js +5 -0
  6. package/dist/permissions.d.ts +1 -0
  7. package/dist/permissions.js +5 -0
  8. package/dist/services/in-memory-workflow-service.js +7 -11
  9. package/dist/wirings/ai-agent/ai-agent-prepare.js +16 -1
  10. package/dist/wirings/channel/channel-middleware-runner.d.ts +1 -0
  11. package/dist/wirings/channel/channel-middleware-runner.js +5 -0
  12. package/dist/wirings/workflow/pikku-workflow-service.js +7 -8
  13. package/package.json +3 -2
  14. package/src/crypto-utils.test.ts +214 -0
  15. package/src/crypto-utils.ts +213 -0
  16. package/src/dev/hot-reload.test.ts +484 -0
  17. package/src/dev/hot-reload.ts +212 -0
  18. package/src/errors/error-handler.ts +62 -0
  19. package/src/errors/error.test.ts +195 -0
  20. package/src/errors/errors.ts +374 -0
  21. package/src/errors/index.ts +2 -0
  22. package/src/factory-functions.test.ts +109 -0
  23. package/src/function/function-runner.test.ts +536 -0
  24. package/src/function/function-runner.ts +365 -0
  25. package/src/function/functions.types.ts +304 -0
  26. package/src/function/index.ts +5 -0
  27. package/src/handle-error.test.ts +424 -0
  28. package/src/handle-error.ts +69 -0
  29. package/src/index.ts +118 -0
  30. package/src/internal.ts +6 -0
  31. package/src/middleware/auth-apikey.test.ts +363 -0
  32. package/src/middleware/auth-apikey.ts +47 -0
  33. package/src/middleware/auth-bearer.test.ts +450 -0
  34. package/src/middleware/auth-bearer.ts +72 -0
  35. package/src/middleware/auth-cookie.test.ts +528 -0
  36. package/src/middleware/auth-cookie.ts +80 -0
  37. package/src/middleware/cors.test.ts +424 -0
  38. package/src/middleware/cors.ts +104 -0
  39. package/src/middleware/index.ts +5 -0
  40. package/src/middleware/remote-auth.test.ts +488 -0
  41. package/src/middleware/remote-auth.ts +68 -0
  42. package/src/middleware/timeout.ts +15 -0
  43. package/src/middleware-runner.test.ts +418 -0
  44. package/src/middleware-runner.ts +240 -0
  45. package/src/permissions.test.ts +434 -0
  46. package/src/permissions.ts +327 -0
  47. package/src/pikku-request.ts +23 -0
  48. package/src/pikku-response.ts +5 -0
  49. package/src/pikku-state.test.ts +224 -0
  50. package/src/pikku-state.ts +216 -0
  51. package/src/run-tests-script.test.ts +49 -0
  52. package/src/schema.test.ts +249 -0
  53. package/src/schema.ts +151 -0
  54. package/src/services/ai-agent-runner-service.ts +41 -0
  55. package/src/services/ai-run-state-service.ts +20 -0
  56. package/src/services/ai-storage-service.ts +39 -0
  57. package/src/services/content-service.ts +76 -0
  58. package/src/services/deployment-service.ts +22 -0
  59. package/src/services/gateway-service.ts +20 -0
  60. package/src/services/gopass-secrets.ts +98 -0
  61. package/src/services/in-memory-ai-run-state-service.ts +73 -0
  62. package/src/services/in-memory-trigger-service.ts +64 -0
  63. package/src/services/in-memory-workflow-service.test.ts +351 -0
  64. package/src/services/in-memory-workflow-service.ts +502 -0
  65. package/src/services/index.ts +56 -0
  66. package/src/services/jwt-service.ts +30 -0
  67. package/src/services/local-content.ts +120 -0
  68. package/src/services/local-gateway-service.ts +62 -0
  69. package/src/services/local-secrets.test.ts +80 -0
  70. package/src/services/local-secrets.ts +94 -0
  71. package/src/services/local-variables.test.ts +61 -0
  72. package/src/services/local-variables.ts +39 -0
  73. package/src/services/logger-console.test.ts +118 -0
  74. package/src/services/logger-console.ts +98 -0
  75. package/src/services/logger.ts +57 -0
  76. package/src/services/scheduler-service.ts +86 -0
  77. package/src/services/schema-service.ts +33 -0
  78. package/src/services/scoped-secret-service.test.ts +74 -0
  79. package/src/services/scoped-secret-service.ts +41 -0
  80. package/src/services/secret-service.ts +38 -0
  81. package/src/services/trigger-service.ts +17 -0
  82. package/src/services/typed-secret-service.test.ts +93 -0
  83. package/src/services/typed-secret-service.ts +70 -0
  84. package/src/services/typed-variables-service.test.ts +73 -0
  85. package/src/services/typed-variables-service.ts +81 -0
  86. package/src/services/user-session-service.test.ts +113 -0
  87. package/src/services/user-session-service.ts +86 -0
  88. package/src/services/variables-service.ts +11 -0
  89. package/src/services/workflow-service.ts +95 -0
  90. package/src/testing/index.ts +2 -0
  91. package/src/testing/service-tests.ts +873 -0
  92. package/src/time-utils.test.ts +56 -0
  93. package/src/time-utils.ts +107 -0
  94. package/src/types/core.types.ts +478 -0
  95. package/src/types/state.types.ts +188 -0
  96. package/src/utils/hash.test.ts +68 -0
  97. package/src/utils/hash.ts +26 -0
  98. package/src/utils.test.ts +350 -0
  99. package/src/utils.ts +129 -0
  100. package/src/version.test.ts +80 -0
  101. package/src/version.ts +25 -0
  102. package/src/wirings/ai-agent/agent-dynamic-workflow.ts +469 -0
  103. package/src/wirings/ai-agent/ai-agent-helpers.test.ts +152 -0
  104. package/src/wirings/ai-agent/ai-agent-helpers.ts +76 -0
  105. package/src/wirings/ai-agent/ai-agent-memory.ts +310 -0
  106. package/src/wirings/ai-agent/ai-agent-model-config.test.ts +115 -0
  107. package/src/wirings/ai-agent/ai-agent-model-config.ts +43 -0
  108. package/src/wirings/ai-agent/ai-agent-prepare.ts +659 -0
  109. package/src/wirings/ai-agent/ai-agent-registry.test.ts +37 -0
  110. package/src/wirings/ai-agent/ai-agent-registry.ts +82 -0
  111. package/src/wirings/ai-agent/ai-agent-runner.test.ts +319 -0
  112. package/src/wirings/ai-agent/ai-agent-runner.ts +773 -0
  113. package/src/wirings/ai-agent/ai-agent-stream.test.ts +859 -0
  114. package/src/wirings/ai-agent/ai-agent-stream.ts +1153 -0
  115. package/src/wirings/ai-agent/ai-agent.types.ts +382 -0
  116. package/src/wirings/ai-agent/index.ts +37 -0
  117. package/src/wirings/channel/channel-common.ts +90 -0
  118. package/src/wirings/channel/channel-handler.ts +250 -0
  119. package/src/wirings/channel/channel-middleware-runner.ts +126 -0
  120. package/src/wirings/channel/channel-runner.ts +166 -0
  121. package/src/wirings/channel/channel-store.ts +29 -0
  122. package/src/wirings/channel/channel.types.ts +172 -0
  123. package/src/wirings/channel/define-channel-routes.ts +25 -0
  124. package/src/wirings/channel/eventhub-service.ts +34 -0
  125. package/src/wirings/channel/eventhub-store.ts +13 -0
  126. package/src/wirings/channel/index.ts +24 -0
  127. package/src/wirings/channel/local/index.ts +3 -0
  128. package/src/wirings/channel/local/local-channel-handler.ts +81 -0
  129. package/src/wirings/channel/local/local-channel-runner.test.ts +215 -0
  130. package/src/wirings/channel/local/local-channel-runner.ts +210 -0
  131. package/src/wirings/channel/local/local-eventhub-service.test.ts +95 -0
  132. package/src/wirings/channel/local/local-eventhub-service.ts +101 -0
  133. package/src/wirings/channel/log-channels.ts +20 -0
  134. package/src/wirings/channel/pikku-abstract-channel-handler.test.ts +54 -0
  135. package/src/wirings/channel/pikku-abstract-channel-handler.ts +45 -0
  136. package/src/wirings/channel/serverless/index.ts +5 -0
  137. package/src/wirings/channel/serverless/serverless-channel-runner.ts +289 -0
  138. package/src/wirings/cli/channel/cli-channel-runner.ts +136 -0
  139. package/src/wirings/cli/channel/index.ts +1 -0
  140. package/src/wirings/cli/cli-runner.test.ts +403 -0
  141. package/src/wirings/cli/cli-runner.ts +529 -0
  142. package/src/wirings/cli/cli.types.ts +358 -0
  143. package/src/wirings/cli/command-parser.test.ts +445 -0
  144. package/src/wirings/cli/command-parser.ts +504 -0
  145. package/src/wirings/cli/define-cli-commands.ts +24 -0
  146. package/src/wirings/cli/index.ts +19 -0
  147. package/src/wirings/gateway/gateway-runner.test.ts +474 -0
  148. package/src/wirings/gateway/gateway-runner.ts +411 -0
  149. package/src/wirings/gateway/gateway.types.ts +149 -0
  150. package/src/wirings/gateway/index.ts +13 -0
  151. package/src/wirings/http/http-routes.test.ts +322 -0
  152. package/src/wirings/http/http-routes.ts +197 -0
  153. package/src/wirings/http/http-runner.test.ts +144 -0
  154. package/src/wirings/http/http-runner.ts +588 -0
  155. package/src/wirings/http/http.types.ts +369 -0
  156. package/src/wirings/http/index.ts +28 -0
  157. package/src/wirings/http/log-http-routes.ts +22 -0
  158. package/src/wirings/http/pikku-fetch-http-request.test.ts +237 -0
  159. package/src/wirings/http/pikku-fetch-http-request.ts +170 -0
  160. package/src/wirings/http/pikku-fetch-http-response.test.ts +82 -0
  161. package/src/wirings/http/pikku-fetch-http-response.ts +147 -0
  162. package/src/wirings/http/routers/http-router.ts +13 -0
  163. package/src/wirings/http/routers/path-to-regex.test.ts +319 -0
  164. package/src/wirings/http/routers/path-to-regex.ts +126 -0
  165. package/src/wirings/http/web-request.test.ts +236 -0
  166. package/src/wirings/http/web-request.ts +104 -0
  167. package/src/wirings/mcp/index.ts +27 -0
  168. package/src/wirings/mcp/mcp-endpoint-registry.test.ts +389 -0
  169. package/src/wirings/mcp/mcp-endpoint-registry.ts +159 -0
  170. package/src/wirings/mcp/mcp-runner.ts +323 -0
  171. package/src/wirings/mcp/mcp.types.ts +216 -0
  172. package/src/wirings/node/index.ts +2 -0
  173. package/src/wirings/node/node.types.ts +23 -0
  174. package/src/wirings/oauth2/index.ts +3 -0
  175. package/src/wirings/oauth2/oauth2-client.test.ts +929 -0
  176. package/src/wirings/oauth2/oauth2-client.ts +335 -0
  177. package/src/wirings/oauth2/oauth2.types.ts +69 -0
  178. package/src/wirings/oauth2/wire-oauth2-credential.ts +23 -0
  179. package/src/wirings/queue/index.ts +31 -0
  180. package/src/wirings/queue/queue-runner.test.ts +710 -0
  181. package/src/wirings/queue/queue-runner.ts +192 -0
  182. package/src/wirings/queue/queue.types.ts +189 -0
  183. package/src/wirings/queue/register-queue-helper.ts +60 -0
  184. package/src/wirings/queue/validate-worker-config.test.ts +108 -0
  185. package/src/wirings/queue/validate-worker-config.ts +116 -0
  186. package/src/wirings/rpc/index.ts +4 -0
  187. package/src/wirings/rpc/rpc-runner.ts +460 -0
  188. package/src/wirings/rpc/rpc-types.ts +56 -0
  189. package/src/wirings/rpc/wire-addon.ts +21 -0
  190. package/src/wirings/scheduler/index.ts +11 -0
  191. package/src/wirings/scheduler/log-schedulers.ts +20 -0
  192. package/src/wirings/scheduler/scheduler-runner.test.ts +660 -0
  193. package/src/wirings/scheduler/scheduler-runner.ts +133 -0
  194. package/src/wirings/scheduler/scheduler.types.ts +53 -0
  195. package/src/wirings/secret/index.ts +9 -0
  196. package/src/wirings/secret/secret.types.ts +32 -0
  197. package/src/wirings/secret/validate-secret-definitions.test.ts +140 -0
  198. package/src/wirings/secret/validate-secret-definitions.ts +82 -0
  199. package/src/wirings/trigger/index.ts +10 -0
  200. package/src/wirings/trigger/pikku-trigger-service.ts +112 -0
  201. package/src/wirings/trigger/trigger-runner.test.ts +79 -0
  202. package/src/wirings/trigger/trigger-runner.ts +135 -0
  203. package/src/wirings/trigger/trigger.types.ts +178 -0
  204. package/src/wirings/variable/index.ts +8 -0
  205. package/src/wirings/variable/validate-variable-definitions.test.ts +91 -0
  206. package/src/wirings/variable/validate-variable-definitions.ts +69 -0
  207. package/src/wirings/variable/variable.types.ts +22 -0
  208. package/src/wirings/workflow/dsl/index.ts +31 -0
  209. package/src/wirings/workflow/dsl/workflow-dsl.types.ts +320 -0
  210. package/src/wirings/workflow/dsl/workflow-runner.ts +28 -0
  211. package/src/wirings/workflow/graph/graph-node.ts +222 -0
  212. package/src/wirings/workflow/graph/graph-runner.test.ts +487 -0
  213. package/src/wirings/workflow/graph/graph-runner.ts +816 -0
  214. package/src/wirings/workflow/graph/graph-validation.test.ts +170 -0
  215. package/src/wirings/workflow/graph/graph-validation.ts +237 -0
  216. package/src/wirings/workflow/graph/index.ts +19 -0
  217. package/src/wirings/workflow/graph/template.test.ts +49 -0
  218. package/src/wirings/workflow/graph/template.ts +42 -0
  219. package/src/wirings/workflow/graph/wire-workflow-graph.ts +29 -0
  220. package/src/wirings/workflow/graph/workflow-graph.types.ts +104 -0
  221. package/src/wirings/workflow/index.ts +82 -0
  222. package/src/wirings/workflow/pikku-workflow-service.test.ts +200 -0
  223. package/src/wirings/workflow/pikku-workflow-service.ts +1179 -0
  224. package/src/wirings/workflow/workflow-helpers.test.ts +129 -0
  225. package/src/wirings/workflow/workflow-helpers.ts +79 -0
  226. package/src/wirings/workflow/workflow.types.ts +274 -0
  227. package/tsconfig.json +14 -0
  228. package/tsconfig.tsbuildinfo +1 -0
  229. package/lcov.info +0 -18891
@@ -0,0 +1,320 @@
1
+ /**
2
+ * DSL (Domain Specific Language) workflow types
3
+ * These types define the step-based workflow format extracted by the inspector
4
+ */
5
+
6
+ import type { WorkflowRun } from '../workflow.types.js'
7
+
8
+ /**
9
+ * Workflow step options
10
+ */
11
+ export interface WorkflowStepOptions {
12
+ /** Display name for logs/UI (optional, doesn't affect execution) */
13
+ description?: string
14
+ /** Number of retry attempts for failed steps (only applies to local execution) */
15
+ retries?: number
16
+ /** Delay between retry attempts (e.g., '1s', '2s', '2min') */
17
+ retryDelay?: string | number
18
+ // Future: timeout, failFast, priority
19
+ }
20
+
21
+ /**
22
+ * Type signature for workflow.do() RPC form - used by inspector
23
+ */
24
+ export type WorkflowWireDoRPC = <TOutput = any, TInput = any>(
25
+ stepName: string,
26
+ rpcName: string,
27
+ data: TInput,
28
+ options?: WorkflowStepOptions
29
+ ) => Promise<TOutput>
30
+
31
+ /**
32
+ * Type signature for workflow.do() inline form - used by inspector
33
+ */
34
+ export type WorkflowWireDoInline = <T>(
35
+ stepName: string,
36
+ fn: () => Promise<T> | T,
37
+ options?: WorkflowStepOptions
38
+ ) => Promise<T>
39
+
40
+ /**
41
+ * Type signature for workflow.sleep() - used by inspector
42
+ */
43
+ export type WorkflowWireSleep = (
44
+ stepName: string,
45
+ duration: string
46
+ ) => Promise<void>
47
+
48
+ /**
49
+ * Type signature for workflow.suspend() - used by inspector
50
+ */
51
+ export type WorkflowWireSuspend = (reason: string) => Promise<void>
52
+
53
+ /**
54
+ * Input source for step arguments in DSL workflows
55
+ */
56
+ export type InputSource =
57
+ | { from: 'input'; path: string }
58
+ | { from: 'outputVar'; name: string; path?: string }
59
+ | { from: 'item'; path: string }
60
+ | { from: 'literal'; value: unknown }
61
+ | { from: 'template'; parts: string[]; expressions: InputSource[] }
62
+
63
+ /**
64
+ * Output binding for return statements in DSL workflows
65
+ */
66
+ export type OutputBinding =
67
+ | { from: 'outputVar'; name: string; path?: string }
68
+ | { from: 'stateVar'; name: string; path?: string }
69
+ | { from: 'input'; path: string }
70
+ | { from: 'literal'; value: unknown }
71
+ | { from: 'expression'; expression: string }
72
+
73
+ /**
74
+ * RPC step metadata (base form)
75
+ */
76
+ export interface RpcStepMeta {
77
+ /** RPC form - generates queue worker */
78
+ type: 'rpc'
79
+ /** Cache key (stepName from workflow.do) */
80
+ stepName: string
81
+ /** RPC to invoke */
82
+ rpcName: string
83
+ /** Output variable name (if assigned) */
84
+ outputVar?: string
85
+ /** Input source mappings, or 'passthrough' when entire data is passed */
86
+ inputs?: Record<string, InputSource> | 'passthrough'
87
+ /** Step options */
88
+ options?: WorkflowStepOptions
89
+ }
90
+
91
+ /**
92
+ * Simple condition expression (leaf node)
93
+ */
94
+ export interface SimpleCondition {
95
+ type: 'simple'
96
+ expression: string
97
+ }
98
+
99
+ /**
100
+ * Nested condition structure supporting AND/OR operations
101
+ */
102
+ export type Condition =
103
+ | SimpleCondition
104
+ | { type: 'and'; conditions: Condition[] }
105
+ | { type: 'or'; conditions: Condition[] }
106
+
107
+ /**
108
+ * A single branch in an if/else-if chain
109
+ */
110
+ export interface BranchCase {
111
+ /** Condition for this branch */
112
+ condition: Condition
113
+ /** Steps to execute when condition is true */
114
+ steps: WorkflowStepMeta[]
115
+ }
116
+
117
+ /**
118
+ * Branch step metadata (if/else-if/else control flow)
119
+ */
120
+ export interface BranchStepMeta {
121
+ type: 'branch'
122
+ /** Branches in order: first is "if", rest are "else if" */
123
+ branches: BranchCase[]
124
+ /** Else branch steps (when no conditions match) */
125
+ elseSteps?: WorkflowStepMeta[]
126
+ }
127
+
128
+ /**
129
+ * Parallel group step metadata (Promise.all with multiple steps)
130
+ */
131
+ export interface ParallelGroupStepMeta {
132
+ type: 'parallel'
133
+ /** Child steps to execute in parallel */
134
+ children: RpcStepMeta[]
135
+ }
136
+
137
+ /**
138
+ * Fanout step metadata (parallel or sequential iteration)
139
+ */
140
+ export interface FanoutStepMeta {
141
+ type: 'fanout'
142
+ /** Step name for this fanout */
143
+ stepName: string
144
+ /** Source array variable name */
145
+ sourceVar: string
146
+ /** Iterator variable name */
147
+ itemVar: string
148
+ /** Execution mode */
149
+ mode: 'parallel' | 'sequential'
150
+ /** Child step to execute per iteration */
151
+ child: RpcStepMeta
152
+ /** Time between iterations (sequential mode only) */
153
+ timeBetween?: string
154
+ }
155
+
156
+ /**
157
+ * Return step metadata (workflow output)
158
+ */
159
+ export interface ReturnStepMeta {
160
+ type: 'return'
161
+ /** Output bindings */
162
+ outputs: Record<string, OutputBinding>
163
+ }
164
+
165
+ /**
166
+ * Inline step metadata (legacy support)
167
+ */
168
+ export interface InlineStepMeta {
169
+ /** Inline form - local execution */
170
+ type: 'inline'
171
+ /** Cache key (stepName from workflow.do) */
172
+ stepName: string
173
+ /** Display name */
174
+ description?: string
175
+ /** Step options */
176
+ options?: WorkflowStepOptions
177
+ }
178
+
179
+ /**
180
+ * Sleep step metadata
181
+ */
182
+ export interface SleepStepMeta {
183
+ /** Sleep step */
184
+ type: 'sleep'
185
+ /** Cache key (stepName from workflow.sleep) */
186
+ stepName: string
187
+ /** Sleep duration */
188
+ duration: string | number
189
+ }
190
+
191
+ /**
192
+ * Cancel step metadata
193
+ */
194
+ export interface CancelStepMeta {
195
+ /** Cancel step */
196
+ type: 'cancel'
197
+ /** Optional cancellation reason */
198
+ reason?: string
199
+ }
200
+
201
+ /**
202
+ * Set step metadata (context variable assignment)
203
+ */
204
+ export interface SetStepMeta {
205
+ /** Set step */
206
+ type: 'set'
207
+ /** Variable name to set (must be in context) */
208
+ variable: string
209
+ /** Value to assign (literal or expression) */
210
+ value: unknown
211
+ }
212
+
213
+ /**
214
+ * Switch case metadata
215
+ */
216
+ export interface SwitchCaseMeta {
217
+ /** Case value (literal) or expression */
218
+ value?: string | number | boolean | null
219
+ /** Case expression (for complex cases) */
220
+ expression?: string
221
+ /** Steps to execute for this case */
222
+ steps: WorkflowStepMeta[]
223
+ }
224
+
225
+ /**
226
+ * Switch step metadata (switch/case control flow)
227
+ */
228
+ export interface SwitchStepMeta {
229
+ type: 'switch'
230
+ /** Expression being switched on */
231
+ expression: string
232
+ /** Case branches */
233
+ cases: SwitchCaseMeta[]
234
+ /** Default case steps (optional) */
235
+ defaultSteps?: WorkflowStepMeta[]
236
+ }
237
+
238
+ /**
239
+ * Filter step metadata (array.filter)
240
+ */
241
+ export interface FilterStepMeta {
242
+ type: 'filter'
243
+ /** Source array variable name */
244
+ sourceVar: string
245
+ /** Iterator variable name */
246
+ itemVar: string
247
+ /** Filter condition */
248
+ condition: Condition
249
+ /** Output variable name (if assigned) */
250
+ outputVar?: string
251
+ }
252
+
253
+ /**
254
+ * Array predicate step metadata (array.some, array.every)
255
+ */
256
+ export interface ArrayPredicateStepMeta {
257
+ type: 'arrayPredicate'
258
+ /** Predicate mode */
259
+ mode: 'some' | 'every'
260
+ /** Source array variable name */
261
+ sourceVar: string
262
+ /** Iterator variable name */
263
+ itemVar: string
264
+ /** Predicate condition */
265
+ condition: Condition
266
+ /** Output variable name (if assigned) */
267
+ outputVar?: string
268
+ }
269
+
270
+ /**
271
+ * Workflow step metadata (extracted by inspector)
272
+ */
273
+ export type WorkflowStepMeta =
274
+ | RpcStepMeta
275
+ | BranchStepMeta
276
+ | ParallelGroupStepMeta
277
+ | FanoutStepMeta
278
+ | ReturnStepMeta
279
+ | InlineStepMeta
280
+ | SleepStepMeta
281
+ | CancelStepMeta
282
+ | SwitchStepMeta
283
+ | FilterStepMeta
284
+ | ArrayPredicateStepMeta
285
+ | SetStepMeta
286
+
287
+ /**
288
+ * Workflow step wire context for RPC functions
289
+ * Provides step-level metadata including retry attempt tracking
290
+ */
291
+ export interface WorkflowStepWire {
292
+ /** The workflow run ID */
293
+ runId: string
294
+ /** The unique step ID */
295
+ stepId: string
296
+ /** Current attempt number (1-indexed, increments on retry) */
297
+ attemptCount: number
298
+ }
299
+
300
+ /**
301
+ * Workflow wire object for DSL workflows
302
+ * Provides workflow-specific capabilities to function execution
303
+ */
304
+ export interface PikkuWorkflowWire {
305
+ /** The workflow name */
306
+ name: string
307
+ /** The current run ID */
308
+ runId: string
309
+ /** Get the current workflow run */
310
+ getRun: () => Promise<WorkflowRun>
311
+
312
+ /** Execute a workflow step (overloaded - RPC or inline form) */
313
+ do: WorkflowWireDoRPC & WorkflowWireDoInline
314
+
315
+ /** Sleep for a duration */
316
+ sleep: WorkflowWireSleep
317
+
318
+ /** Suspend workflow until explicitly resumed */
319
+ suspend: WorkflowWireSuspend
320
+ }
@@ -0,0 +1,28 @@
1
+ import { pikkuState } from '../../../pikku-state.js'
2
+ import { addFunction } from '../../../function/function-runner.js'
3
+ import { PikkuMissingMetaError } from '../../../errors/errors.js'
4
+
5
+ /**
6
+ * Add a workflow to the system
7
+ * This is called by the generated workflow wirings
8
+ */
9
+ export const addWorkflow = (workflowName: string, workflowFunc: any) => {
10
+ // Get workflow metadata from inspector
11
+ const meta = pikkuState(null, 'workflows', 'meta')
12
+ const workflowMeta = meta[workflowName]
13
+ if (!workflowMeta) {
14
+ throw new PikkuMissingMetaError(
15
+ `Missing generated metadata for workflow '${workflowName}'`
16
+ )
17
+ }
18
+
19
+ // Store workflow definition in state
20
+ const registrations = pikkuState(null, 'workflows', 'registrations')
21
+ registrations.set(workflowName, {
22
+ name: workflowName,
23
+ func: workflowFunc,
24
+ })
25
+
26
+ // Register the function with pikku
27
+ addFunction(workflowMeta.pikkuFuncId, workflowFunc)
28
+ }
@@ -0,0 +1,222 @@
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 } 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
+ */
76
+ /**
77
+ * Type to compute output types for all nodes based on their func mapping
78
+ */
79
+ type ComputeNodeOutputs<
80
+ FuncMap extends Record<string, string>,
81
+ RPCMap extends Record<string, RPCHandler>,
82
+ > = {
83
+ [K in keyof FuncMap]: FuncMap[K] extends keyof RPCMap
84
+ ? RPCMap[FuncMap[K]]['output']
85
+ : unknown
86
+ }
87
+
88
+ /**
89
+ * Type to compute input types for all nodes based on their func mapping
90
+ */
91
+ type ComputeNodeInputs<
92
+ FuncMap extends Record<string, string>,
93
+ RPCMap extends Record<string, RPCHandler>,
94
+ > = {
95
+ [K in keyof FuncMap]: FuncMap[K] extends keyof RPCMap
96
+ ? RPCMap[FuncMap[K]]['input']
97
+ : unknown
98
+ }
99
+
100
+ /**
101
+ * Typed ref value - carries the type of the referenced field as a phantom type.
102
+ * At runtime this is just RefValue, but TypeScript tracks the type T.
103
+ */
104
+ export type TypedRef<T> = RefValue & { __phantomType?: T }
105
+
106
+ /**
107
+ * Map input type fields to allow TypedRef of matching type as an alternative
108
+ */
109
+ type InputWithRefs<T> = {
110
+ [K in keyof T]: T[K] | TypedRef<T[K]>
111
+ }
112
+
113
+ export function createGraph<RPCMap extends Record<string, RPCHandler>>() {
114
+ return <const FuncMap extends Record<string, keyof RPCMap & string>>(
115
+ funcMap: FuncMap,
116
+ nodesOrBuilder?:
117
+ | GraphNodeConfigMap<FuncMap, RPCMap>
118
+ | ((nodes: FuncMap) => GraphNodeConfigMap<FuncMap, RPCMap>)
119
+ ): Record<
120
+ Extract<keyof FuncMap, string>,
121
+ GraphNodeConfig<Extract<keyof FuncMap, string>>
122
+ > => {
123
+ type NodeIds = Extract<keyof FuncMap, string>
124
+
125
+ // If no nodes provided, return just the funcMap converted to graph nodes
126
+ if (!nodesOrBuilder) {
127
+ const result: Record<string, GraphNodeConfig<string>> = {}
128
+ for (const [nodeId, rpcName] of Object.entries(funcMap)) {
129
+ result[nodeId] = {
130
+ func: rpcName as string,
131
+ }
132
+ }
133
+ return result as Record<NodeIds, GraphNodeConfig<NodeIds>>
134
+ }
135
+
136
+ const nodes =
137
+ typeof nodesOrBuilder === 'function'
138
+ ? nodesOrBuilder(funcMap)
139
+ : nodesOrBuilder
140
+
141
+ const result: Record<string, GraphNodeConfig<string>> = {}
142
+
143
+ for (const [nodeId, def] of Object.entries(nodes) as [string, any][]) {
144
+ result[nodeId] = {
145
+ func: funcMap[nodeId] as string,
146
+ input: def?.input as any,
147
+ next: def?.next,
148
+ onError: def?.onError,
149
+ }
150
+ }
151
+
152
+ return result as Record<NodeIds, GraphNodeConfig<NodeIds>>
153
+ }
154
+ }
155
+
156
+ /**
157
+ * Type helper for node configuration map
158
+ */
159
+ type GraphNodeConfigMap<
160
+ FuncMap extends Record<string, string>,
161
+ RPCMap extends Record<string, RPCHandler>,
162
+ > = {
163
+ [K in Extract<keyof FuncMap, string>]?: {
164
+ next?: NextConfig<Extract<keyof FuncMap, string>>
165
+ input?: (
166
+ ref: <
167
+ N extends Extract<keyof FuncMap, string>,
168
+ P extends keyof ComputeNodeOutputs<FuncMap, RPCMap>[N] & string,
169
+ >(
170
+ nodeId: N,
171
+ path: P
172
+ ) => TypedRef<ComputeNodeOutputs<FuncMap, RPCMap>[N][P]>
173
+ ) => InputWithRefs<ComputeNodeInputs<FuncMap, RPCMap>[K]>
174
+ onError?: Extract<keyof FuncMap, string> | Extract<keyof FuncMap, string>[]
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Untyped graph for use without RPC map.
180
+ * Pass node IDs as type parameter for type-safe next/ref, or omit for untyped usage.
181
+ *
182
+ * @example
183
+ * ```typescript
184
+ * // Typed usage - next and ref are type-checked
185
+ * graph<'entry' | 'sendWelcome'>({
186
+ * entry: { func: 'createUserProfile', next: 'sendWelcome' },
187
+ * sendWelcome: { func: 'sendEmail', input: (ref) => ({ to: ref('entry', 'email') }) },
188
+ * })
189
+ *
190
+ * // Untyped usage - no type checking on next/ref
191
+ * graph({
192
+ * entry: { func: 'createUserProfile', next: 'sendWelcome' },
193
+ * sendWelcome: { func: 'sendEmail' },
194
+ * })
195
+ * ```
196
+ */
197
+ export function graph<NodeIds extends string = string>(
198
+ nodes: Record<
199
+ NodeIds,
200
+ {
201
+ func: string
202
+ next?: NextConfig<NodeIds>
203
+ input?: (
204
+ ref: (nodeId: NodeIds, path: string) => RefValue
205
+ ) => Record<string, unknown>
206
+ onError?: NodeIds | NodeIds[]
207
+ }
208
+ >
209
+ ): Record<NodeIds, GraphNodeConfig<NodeIds>> {
210
+ const result: Record<string, GraphNodeConfig<string>> = {}
211
+
212
+ for (const [nodeId, def] of Object.entries(nodes) as [string, any][]) {
213
+ result[nodeId] = {
214
+ func: def.func,
215
+ input: def.input as any,
216
+ next: def.next,
217
+ onError: def.onError,
218
+ }
219
+ }
220
+
221
+ return result as Record<NodeIds, GraphNodeConfig<NodeIds>>
222
+ }