@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,135 @@
1
+ import type { PikkuWire } from '../../types/core.types.js'
2
+ import type {
3
+ CoreTrigger,
4
+ CoreTriggerSource,
5
+ TriggerInstance,
6
+ } from './trigger.types.js'
7
+ import {
8
+ pikkuState,
9
+ getSingletonServices,
10
+ getCreateWireServices,
11
+ } from '../../pikku-state.js'
12
+ import { addFunction, runPikkuFunc } from '../../function/function-runner.js'
13
+ import { PikkuMissingMetaError } from '../../errors/errors.js'
14
+
15
+ /**
16
+ * Registers a trigger with the Pikku framework.
17
+ * Declares a trigger name and its target pikku function.
18
+ * Runs everywhere. Inspector extracts at build time.
19
+ */
20
+ export const wireTrigger = (trigger: CoreTrigger) => {
21
+ const meta = pikkuState(null, 'trigger', 'meta')
22
+ const triggerMeta = meta[trigger.name]
23
+ if (!triggerMeta) {
24
+ throw new PikkuMissingMetaError(
25
+ `Missing generated metadata for trigger '${trigger.name}'`
26
+ )
27
+ }
28
+
29
+ addFunction(triggerMeta.pikkuFuncId, trigger.func as any)
30
+
31
+ const triggers = pikkuState(null, 'trigger', 'triggers')
32
+ triggers.set(trigger.name, trigger as any)
33
+ }
34
+
35
+ /**
36
+ * Registers a trigger source with the Pikku framework.
37
+ * Provides the actual subscription function and input data.
38
+ * Only imported in the trigger worker process.
39
+ */
40
+ export const wireTriggerSource = <TInput = unknown, TOutput = unknown>(
41
+ source: CoreTriggerSource<TInput, TOutput>
42
+ ) => {
43
+ const sourceMeta = pikkuState(null, 'trigger', 'sourceMeta')
44
+ const triggerSourceMeta = sourceMeta[source.name]
45
+ if (!triggerSourceMeta) {
46
+ throw new PikkuMissingMetaError(
47
+ `Missing generated metadata for trigger source '${source.name}'`
48
+ )
49
+ }
50
+
51
+ const triggerSources = pikkuState(null, 'trigger', 'triggerSources')
52
+ if (triggerSources.has(source.name)) {
53
+ throw new Error(`Trigger source already exists: ${source.name}`)
54
+ }
55
+ triggerSources.set(source.name, source as any)
56
+
57
+ addFunction(
58
+ triggerSourceMeta.pikkuFuncId,
59
+ source.func as any,
60
+ triggerSourceMeta.packageName
61
+ )
62
+ }
63
+
64
+ /**
65
+ * Parameters for setting up a trigger
66
+ */
67
+ export type SetupTriggerParams<TInput = unknown, TOutput = unknown> = {
68
+ name: string
69
+ input?: TInput
70
+ onTrigger: (data: TOutput) => void | Promise<void>
71
+ }
72
+
73
+ /**
74
+ * Sets up a registered trigger and starts listening for events.
75
+ * Returns a TriggerInstance with a teardown function.
76
+ *
77
+ * @param name - The trigger name (must be registered via wireTrigger)
78
+ * @param input - The input to pass to the trigger function
79
+ * @param onTrigger - Callback invoked when the trigger fires
80
+ */
81
+ export async function setupTrigger<TInput = unknown, TOutput = unknown>({
82
+ name,
83
+ input,
84
+ onTrigger,
85
+ }: SetupTriggerParams<TInput, TOutput>): Promise<TriggerInstance> {
86
+ const singletonServices = getSingletonServices()
87
+ const createWireServices = getCreateWireServices()
88
+ const source = pikkuState(null, 'trigger', 'triggerSources').get(name)
89
+ const sourceMeta = pikkuState(null, 'trigger', 'sourceMeta')[name]
90
+
91
+ if (!source) {
92
+ throw new Error(`Trigger source not found: ${name}`)
93
+ }
94
+ if (!sourceMeta) {
95
+ throw new PikkuMissingMetaError(
96
+ `Missing generated metadata for trigger source '${name}'`
97
+ )
98
+ }
99
+
100
+ const wire: PikkuWire = {
101
+ trigger: {
102
+ invoke: (data: unknown) => {
103
+ singletonServices.logger.info(`Trigger fired: ${name}`)
104
+ onTrigger(data as TOutput)
105
+ },
106
+ },
107
+ }
108
+
109
+ singletonServices.logger.info(`Setting up trigger: ${name}`)
110
+
111
+ const teardown = await runPikkuFunc('trigger', name, sourceMeta.pikkuFuncId, {
112
+ singletonServices,
113
+ createWireServices,
114
+ auth: false,
115
+ data: () => input as any,
116
+ wire,
117
+ packageName: sourceMeta.packageName || null,
118
+ })
119
+
120
+ return { name, teardown }
121
+ }
122
+
123
+ /**
124
+ * Gets all registered triggers
125
+ */
126
+ export const getRegisteredTriggers = () => {
127
+ return pikkuState(null, 'trigger', 'triggers')
128
+ }
129
+
130
+ /**
131
+ * Gets trigger metadata
132
+ */
133
+ export const getTriggerMeta = () => {
134
+ return pikkuState(null, 'trigger', 'meta')
135
+ }
@@ -0,0 +1,178 @@
1
+ import type {
2
+ CommonWireMeta,
3
+ CoreSingletonServices,
4
+ } from '../../types/core.types.js'
5
+ import type { StandardSchemaV1 } from '@standard-schema/spec'
6
+ import type { CoreNodeConfig } from '../node/node.types.js'
7
+
8
+ /**
9
+ * The trigger interaction object passed to trigger functions via the wire.
10
+ * Call trigger() to fire the trigger and start a new workflow/execution.
11
+ */
12
+ export interface PikkuTrigger<TOutput = unknown> {
13
+ invoke: (data: TOutput) => void
14
+ }
15
+
16
+ /**
17
+ * Metadata for registered triggers stored in state.
18
+ */
19
+ export type TriggerMeta = Record<string, CommonWireMeta & { name: string }>
20
+
21
+ export type TriggerSourceMeta = Record<
22
+ string,
23
+ { name: string; pikkuFuncId: string; packageName?: string }
24
+ >
25
+
26
+ /**
27
+ * A trigger function that sets up a subscription and returns a teardown function.
28
+ * The trigger is fired via wire.trigger.invoke(data).
29
+ *
30
+ * @template TInput - Input type (configuration passed when wired)
31
+ * @template TOutput - Output type produced when trigger fires
32
+ * @template Services - Services available to the trigger
33
+ */
34
+ export type CorePikkuTriggerFunction<
35
+ TInput = unknown,
36
+ TOutput = unknown,
37
+ Services extends CoreSingletonServices = CoreSingletonServices,
38
+ > = (
39
+ services: Services,
40
+ input: TInput,
41
+ wire: { trigger: { invoke: (data: TOutput) => void } }
42
+ ) => Promise<() => void | Promise<void>>
43
+
44
+ /**
45
+ * Configuration object for creating a trigger function with metadata
46
+ */
47
+ export type CorePikkuTriggerFunctionConfig<
48
+ TInput = unknown,
49
+ TOutput = unknown,
50
+ Services extends CoreSingletonServices = CoreSingletonServices,
51
+ InputSchema extends StandardSchemaV1 | undefined = undefined,
52
+ OutputSchema extends StandardSchemaV1 | undefined = undefined,
53
+ > = {
54
+ /** Optional human-readable title for the trigger */
55
+ title?: string
56
+ /** Optional description of what the trigger does */
57
+ description?: string
58
+ /** Optional tags for categorization */
59
+ tags?: string[]
60
+ /** The trigger function */
61
+ func: CorePikkuTriggerFunction<TInput, TOutput, Services>
62
+ /** Optional Zod schema for input validation */
63
+ input?: InputSchema
64
+ /** Optional Zod schema for output validation */
65
+ output?: OutputSchema
66
+ /** Optional node configuration */
67
+ node?: CoreNodeConfig
68
+ }
69
+
70
+ /**
71
+ * Factory function for creating trigger functions
72
+ * Supports both direct function and configuration object syntax
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * // Direct function syntax
77
+ * export const redisSubscribeTrigger = pikkuTriggerFunc<
78
+ * { channel: string },
79
+ * { message: string }
80
+ * >(async ({ redis }, { channel }, { trigger }) => {
81
+ * const subscriber = redis.duplicate()
82
+ * await subscriber.subscribe(channel, (msg) => {
83
+ * trigger.invoke({ message: msg })
84
+ * })
85
+ * return () => subscriber.unsubscribe()
86
+ * })
87
+ *
88
+ * // Configuration object syntax with metadata
89
+ * export const redisSubscribeTrigger = pikkuTriggerFunc({
90
+ * title: 'Redis Subscribe Trigger',
91
+ * description: 'Listens to Redis pub/sub channel',
92
+ * input: z.object({ channel: z.string() }),
93
+ * output: z.object({ message: z.string() }),
94
+ * func: async ({ redis }, { channel }, { trigger }) => {
95
+ * const subscriber = redis.duplicate()
96
+ * await subscriber.subscribe(channel, (msg) => {
97
+ * trigger.invoke({ message: msg })
98
+ * })
99
+ * return () => subscriber.unsubscribe()
100
+ * }
101
+ * })
102
+ * ```
103
+ */
104
+ export const pikkuTriggerFunc = <
105
+ TInput = unknown,
106
+ TOutput = unknown,
107
+ Services extends CoreSingletonServices = CoreSingletonServices,
108
+ InputSchema extends StandardSchemaV1 | undefined = undefined,
109
+ OutputSchema extends StandardSchemaV1 | undefined = undefined,
110
+ >(
111
+ triggerOrConfig:
112
+ | CorePikkuTriggerFunction<TInput, TOutput, Services>
113
+ | CorePikkuTriggerFunctionConfig<
114
+ TInput,
115
+ TOutput,
116
+ Services,
117
+ InputSchema,
118
+ OutputSchema
119
+ >
120
+ ): CorePikkuTriggerFunctionConfig<
121
+ TInput,
122
+ TOutput,
123
+ Services,
124
+ InputSchema,
125
+ OutputSchema
126
+ > => {
127
+ if (typeof triggerOrConfig === 'function') {
128
+ return { func: triggerOrConfig }
129
+ }
130
+ return triggerOrConfig
131
+ }
132
+
133
+ /**
134
+ * Core trigger definition for registration.
135
+ *
136
+ * @template TInput - Input type (configuration passed when wired)
137
+ * @template TOutput - Output type
138
+ */
139
+ export interface CoreTrigger<PikkuFunctionConfig = any> {
140
+ /** Unique name for this trigger */
141
+ name: string
142
+ /** The target RPC function to invoke when the trigger fires */
143
+ func: PikkuFunctionConfig
144
+ /** Optional description */
145
+ description?: string
146
+ /** Optional tags for categorization */
147
+ tags?: string[]
148
+ /** Whether this trigger is used by a graph workflow */
149
+ graph?: true
150
+ }
151
+
152
+ /**
153
+ * Represents a trigger instance with teardown capability
154
+ */
155
+ export interface TriggerInstance {
156
+ name: string
157
+ teardown: () => void | Promise<void>
158
+ }
159
+
160
+ /**
161
+ * A trigger source that provides the subscription function.
162
+ * Only imported in the trigger worker process.
163
+ *
164
+ * @template TInput - Input type passed to the trigger function
165
+ * @template TOutput - Output type produced when trigger fires
166
+ */
167
+ export type CoreTriggerSource<TInput = unknown, TOutput = unknown> = {
168
+ /** Must match a wireTrigger name */
169
+ name: string
170
+ /** The trigger function config that sets up the subscription */
171
+ func: CorePikkuTriggerFunctionConfig<
172
+ TInput,
173
+ TOutput,
174
+ CoreSingletonServices,
175
+ StandardSchemaV1 | undefined,
176
+ StandardSchemaV1 | undefined
177
+ >
178
+ } & (unknown extends TInput ? { input?: TInput } : { input: TInput })
@@ -0,0 +1,8 @@
1
+ export { wireVariable } from './variable.types.js'
2
+ export type {
3
+ CoreVariable,
4
+ VariableDefinitionMeta,
5
+ VariableDefinitionsMeta,
6
+ VariableDefinitions,
7
+ } from './variable.types.js'
8
+ export { validateAndBuildVariableDefinitionsMeta } from './validate-variable-definitions.js'
@@ -0,0 +1,91 @@
1
+ import { describe, test } from 'node:test'
2
+ import assert from 'node:assert'
3
+ import { validateAndBuildVariableDefinitionsMeta } from './validate-variable-definitions.js'
4
+
5
+ describe('validateAndBuildVariableDefinitionsMeta', () => {
6
+ test('should build meta from single definition', () => {
7
+ const definitions = [
8
+ {
9
+ name: 'dbUrl',
10
+ displayName: 'Database URL',
11
+ description: 'Connection string',
12
+ variableId: 'DB_URL',
13
+ sourceFile: 'a.ts',
14
+ },
15
+ ]
16
+ const result = validateAndBuildVariableDefinitionsMeta(
17
+ definitions as any,
18
+ new Map()
19
+ )
20
+ assert.ok(result['dbUrl'])
21
+ assert.strictEqual(result['dbUrl'].variableId, 'DB_URL')
22
+ })
23
+
24
+ test('should allow duplicate variableId with same schema', () => {
25
+ const schemaLookup = new Map([
26
+ ['schema1', { variableName: 'Schema', sourceFile: 'types.ts' }],
27
+ ])
28
+ const definitions = [
29
+ {
30
+ name: 'var1',
31
+ displayName: 'Var 1',
32
+ description: 'd',
33
+ variableId: 'SHARED',
34
+ schema: 'schema1',
35
+ sourceFile: 'a.ts',
36
+ },
37
+ {
38
+ name: 'var2',
39
+ displayName: 'Var 2',
40
+ description: 'd',
41
+ variableId: 'SHARED',
42
+ schema: 'schema1',
43
+ sourceFile: 'b.ts',
44
+ },
45
+ ]
46
+ const result = validateAndBuildVariableDefinitionsMeta(
47
+ definitions as any,
48
+ schemaLookup
49
+ )
50
+ assert.ok(result['var1'])
51
+ assert.ok(result['var2'])
52
+ })
53
+
54
+ test('should throw when duplicate variableId has different schemas', () => {
55
+ const schemaLookup = new Map([
56
+ ['schema1', { variableName: 'SchemaA', sourceFile: 'types.ts' }],
57
+ ['schema2', { variableName: 'SchemaB', sourceFile: 'types2.ts' }],
58
+ ])
59
+ const definitions = [
60
+ {
61
+ name: 'var1',
62
+ displayName: 'Var 1',
63
+ description: 'd',
64
+ variableId: 'SHARED',
65
+ schema: 'schema1',
66
+ sourceFile: 'a.ts',
67
+ },
68
+ {
69
+ name: 'var2',
70
+ displayName: 'Var 2',
71
+ description: 'd',
72
+ variableId: 'SHARED',
73
+ schema: 'schema2',
74
+ sourceFile: 'b.ts',
75
+ },
76
+ ]
77
+ assert.throws(
78
+ () =>
79
+ validateAndBuildVariableDefinitionsMeta(
80
+ definitions as any,
81
+ schemaLookup
82
+ ),
83
+ (err: any) => err.message.includes('different schemas')
84
+ )
85
+ })
86
+
87
+ test('should handle empty definitions', () => {
88
+ const result = validateAndBuildVariableDefinitionsMeta([], new Map())
89
+ assert.deepStrictEqual(result, {})
90
+ })
91
+ })
@@ -0,0 +1,69 @@
1
+ import type {
2
+ VariableDefinitions,
3
+ VariableDefinitionsMeta,
4
+ } from './variable.types.js'
5
+
6
+ export interface SchemaRefLike {
7
+ variableName: string
8
+ sourceFile: string
9
+ }
10
+
11
+ export function validateAndBuildVariableDefinitionsMeta(
12
+ definitions: VariableDefinitions,
13
+ schemaLookup: Map<string, SchemaRefLike>
14
+ ): VariableDefinitionsMeta {
15
+ const meta: VariableDefinitionsMeta = {}
16
+ const variableIdToDefinition: Map<string, VariableDefinitions[0]> = new Map()
17
+
18
+ for (const def of definitions) {
19
+ const existingDef = variableIdToDefinition.get(def.variableId)
20
+
21
+ if (existingDef) {
22
+ if (def.schema && existingDef.schema) {
23
+ const defSchemaRef = schemaLookup.get(def.schema as string)
24
+ const existingSchemaRef = schemaLookup.get(existingDef.schema as string)
25
+
26
+ if (defSchemaRef && existingSchemaRef) {
27
+ if (
28
+ defSchemaRef.variableName !== existingSchemaRef.variableName ||
29
+ defSchemaRef.sourceFile !== existingSchemaRef.sourceFile
30
+ ) {
31
+ throw new Error(
32
+ `Variable '${def.variableId}' is defined with different schemas.\n` +
33
+ ` First definition: ${existingDef.sourceFile} (schema: ${existingSchemaRef.variableName})\n` +
34
+ ` Second definition: ${def.sourceFile} (schema: ${defSchemaRef.variableName})\n` +
35
+ `Variables sharing a variableId must use the same schema.`
36
+ )
37
+ }
38
+ }
39
+ }
40
+
41
+ if (!meta[def.name]) {
42
+ meta[def.name] = {
43
+ name: def.name,
44
+ displayName: def.displayName,
45
+ description: def.description,
46
+ variableId: def.variableId,
47
+ schema: def.schema,
48
+ sourceFile: def.sourceFile,
49
+ }
50
+ }
51
+ continue
52
+ }
53
+
54
+ variableIdToDefinition.set(def.variableId, def)
55
+
56
+ if (!meta[def.name]) {
57
+ meta[def.name] = {
58
+ name: def.name,
59
+ displayName: def.displayName,
60
+ description: def.description,
61
+ variableId: def.variableId,
62
+ schema: def.schema,
63
+ sourceFile: def.sourceFile,
64
+ }
65
+ }
66
+ }
67
+
68
+ return meta
69
+ }
@@ -0,0 +1,22 @@
1
+ export type CoreVariable<T = unknown> = {
2
+ name: string
3
+ displayName: string
4
+ description?: string
5
+ variableId: string
6
+ schema: T
7
+ }
8
+
9
+ export type VariableDefinitionMeta = {
10
+ name: string
11
+ displayName: string
12
+ description?: string
13
+ variableId: string
14
+ schema?: Record<string, unknown> | string
15
+ sourceFile?: string
16
+ }
17
+
18
+ export type VariableDefinitionsMeta = Record<string, VariableDefinitionMeta>
19
+
20
+ export type VariableDefinitions = VariableDefinitionMeta[]
21
+
22
+ export const wireVariable = <T>(_config: CoreVariable<T>): void => {}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * DSL (Domain Specific Language) workflow exports
3
+ */
4
+ export { addWorkflow } from './workflow-runner.js'
5
+
6
+ export type {
7
+ WorkflowStepOptions,
8
+ WorkflowWireDoRPC,
9
+ WorkflowWireDoInline,
10
+ WorkflowWireSleep,
11
+ WorkflowWireSuspend,
12
+ InputSource,
13
+ OutputBinding,
14
+ RpcStepMeta,
15
+ SimpleCondition,
16
+ Condition,
17
+ BranchStepMeta,
18
+ ParallelGroupStepMeta,
19
+ FanoutStepMeta,
20
+ ReturnStepMeta,
21
+ InlineStepMeta,
22
+ SleepStepMeta,
23
+ CancelStepMeta,
24
+ SwitchCaseMeta,
25
+ SwitchStepMeta,
26
+ FilterStepMeta,
27
+ ArrayPredicateStepMeta,
28
+ WorkflowStepMeta,
29
+ WorkflowStepWire,
30
+ PikkuWorkflowWire,
31
+ } from './workflow-dsl.types.js'