@pikku/core 0.10.2 → 0.11.1

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 (138) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/errors/errors.d.ts +1 -1
  3. package/dist/errors/errors.js +3 -3
  4. package/dist/function/function-runner.d.ts +6 -7
  5. package/dist/function/function-runner.js +35 -16
  6. package/dist/function/functions.types.d.ts +23 -30
  7. package/dist/function/functions.types.js +9 -6
  8. package/dist/handle-error.d.ts +1 -1
  9. package/dist/handle-error.js +1 -1
  10. package/dist/index.d.ts +2 -0
  11. package/dist/index.js +2 -0
  12. package/dist/middleware/auth-apikey.d.ts +1 -0
  13. package/dist/middleware/auth-apikey.js +3 -3
  14. package/dist/middleware/auth-bearer.d.ts +1 -0
  15. package/dist/middleware/auth-bearer.js +3 -3
  16. package/dist/middleware/auth-cookie.d.ts +1 -0
  17. package/dist/middleware/auth-cookie.js +5 -5
  18. package/dist/middleware/timeout.d.ts +1 -0
  19. package/dist/middleware/timeout.js +3 -2
  20. package/dist/middleware-runner.d.ts +3 -6
  21. package/dist/middleware-runner.js +11 -11
  22. package/dist/permissions.d.ts +6 -6
  23. package/dist/permissions.js +15 -15
  24. package/dist/pikku-state.d.ts +7 -2
  25. package/dist/pikku-state.js +4 -0
  26. package/dist/services/index.d.ts +1 -0
  27. package/dist/services/index.js +1 -0
  28. package/dist/services/scheduler-service.d.ts +63 -0
  29. package/dist/services/scheduler-service.js +6 -0
  30. package/dist/services/user-session-service.d.ts +7 -3
  31. package/dist/services/user-session-service.js +8 -1
  32. package/dist/time-utils.d.ts +14 -0
  33. package/dist/time-utils.js +62 -0
  34. package/dist/types/core.types.d.ts +48 -35
  35. package/dist/types/core.types.js +3 -13
  36. package/dist/utils.d.ts +1 -1
  37. package/dist/utils.js +3 -3
  38. package/dist/wirings/channel/channel-common.js +2 -3
  39. package/dist/wirings/channel/channel-handler.d.ts +3 -2
  40. package/dist/wirings/channel/channel-handler.js +6 -9
  41. package/dist/wirings/channel/channel-runner.d.ts +2 -2
  42. package/dist/wirings/channel/channel.types.d.ts +10 -5
  43. package/dist/wirings/channel/local/local-channel-runner.d.ts +1 -1
  44. package/dist/wirings/channel/local/local-channel-runner.js +19 -26
  45. package/dist/wirings/channel/serverless/serverless-channel-runner.d.ts +1 -1
  46. package/dist/wirings/channel/serverless/serverless-channel-runner.js +41 -46
  47. package/dist/wirings/cli/channel/cli-channel-runner.js +1 -1
  48. package/dist/wirings/cli/cli-runner.d.ts +5 -5
  49. package/dist/wirings/cli/cli-runner.js +11 -29
  50. package/dist/wirings/cli/cli.types.d.ts +15 -11
  51. package/dist/wirings/http/http-runner.d.ts +6 -6
  52. package/dist/wirings/http/http-runner.js +25 -39
  53. package/dist/wirings/http/http.types.d.ts +5 -10
  54. package/dist/wirings/mcp/mcp-runner.d.ts +2 -2
  55. package/dist/wirings/mcp/mcp-runner.js +12 -19
  56. package/dist/wirings/mcp/mcp.types.d.ts +6 -0
  57. package/dist/wirings/queue/queue-runner.d.ts +3 -3
  58. package/dist/wirings/queue/queue-runner.js +7 -14
  59. package/dist/wirings/queue/queue.types.d.ts +4 -13
  60. package/dist/wirings/rpc/index.d.ts +1 -1
  61. package/dist/wirings/rpc/index.js +1 -1
  62. package/dist/wirings/rpc/rpc-runner.d.ts +13 -7
  63. package/dist/wirings/rpc/rpc-runner.js +50 -26
  64. package/dist/wirings/rpc/rpc-types.d.ts +1 -0
  65. package/dist/wirings/scheduler/scheduler-runner.d.ts +3 -3
  66. package/dist/wirings/scheduler/scheduler-runner.js +13 -24
  67. package/dist/wirings/scheduler/scheduler.types.d.ts +3 -8
  68. package/dist/wirings/workflow/index.d.ts +15 -0
  69. package/dist/wirings/workflow/index.js +14 -0
  70. package/dist/wirings/workflow/pikku-workflow-service.d.ts +160 -0
  71. package/dist/wirings/workflow/pikku-workflow-service.js +430 -0
  72. package/dist/wirings/workflow/workflow-runner.d.ts +34 -0
  73. package/dist/wirings/workflow/workflow-runner.js +65 -0
  74. package/dist/wirings/workflow/workflow.types.d.ts +332 -0
  75. package/dist/wirings/workflow/workflow.types.js +1 -0
  76. package/package.json +3 -4
  77. package/src/errors/errors.ts +3 -3
  78. package/src/factory-functions.test.ts +9 -36
  79. package/src/function/function-runner.test.ts +57 -68
  80. package/src/function/function-runner.ts +51 -28
  81. package/src/function/function-runner.ts.bak +188 -0
  82. package/src/function/functions.types.ts +69 -51
  83. package/src/handle-error.ts +1 -1
  84. package/src/index.ts +2 -0
  85. package/src/middleware/auth-apikey.test.ts +360 -0
  86. package/src/middleware/auth-apikey.ts +3 -7
  87. package/src/middleware/auth-bearer.test.ts +447 -0
  88. package/src/middleware/auth-bearer.ts +27 -33
  89. package/src/middleware/auth-cookie.test.ts +525 -0
  90. package/src/middleware/auth-cookie.ts +6 -6
  91. package/src/middleware/timeout.ts +4 -2
  92. package/src/middleware-runner.test.ts +58 -127
  93. package/src/middleware-runner.ts +13 -15
  94. package/src/permissions.test.ts +41 -45
  95. package/src/permissions.ts +21 -20
  96. package/src/pikku-state.ts +14 -2
  97. package/src/services/index.ts +1 -0
  98. package/src/services/scheduler-service.ts +76 -0
  99. package/src/services/user-session-service.ts +14 -4
  100. package/src/time-utils.ts +75 -0
  101. package/src/types/core.types.ts +92 -51
  102. package/src/utils.ts +4 -4
  103. package/src/wirings/channel/channel-common.ts +4 -7
  104. package/src/wirings/channel/channel-handler.ts +16 -22
  105. package/src/wirings/channel/channel-runner.ts +2 -2
  106. package/src/wirings/channel/channel.types.ts +14 -11
  107. package/src/wirings/channel/local/local-channel-runner.test.ts +5 -3
  108. package/src/wirings/channel/local/local-channel-runner.ts +23 -38
  109. package/src/wirings/channel/serverless/serverless-channel-runner.ts +51 -77
  110. package/src/wirings/cli/channel/cli-channel-runner.ts +1 -1
  111. package/src/wirings/cli/cli-runner.test.ts +7 -7
  112. package/src/wirings/cli/cli-runner.ts +25 -63
  113. package/src/wirings/cli/cli.types.ts +22 -16
  114. package/src/wirings/http/http-runner.test.ts +8 -8
  115. package/src/wirings/http/http-runner.ts +29 -56
  116. package/src/wirings/http/http.types.ts +5 -14
  117. package/src/wirings/mcp/mcp-runner.ts +23 -48
  118. package/src/wirings/mcp/mcp.types.ts +6 -0
  119. package/src/wirings/queue/queue-runner.test.ts +641 -0
  120. package/src/wirings/queue/queue-runner.ts +10 -24
  121. package/src/wirings/queue/queue.types.ts +5 -15
  122. package/src/wirings/rpc/index.ts +1 -1
  123. package/src/wirings/rpc/rpc-runner.ts +71 -32
  124. package/src/wirings/rpc/rpc-types.ts +1 -0
  125. package/src/wirings/scheduler/scheduler-runner.test.ts +627 -0
  126. package/src/wirings/scheduler/scheduler-runner.ts +24 -50
  127. package/src/wirings/scheduler/scheduler.types.ts +3 -9
  128. package/src/wirings/workflow/index.ts +43 -0
  129. package/src/wirings/workflow/pikku-workflow-service.ts +746 -0
  130. package/src/wirings/workflow/workflow-runner.ts +72 -0
  131. package/src/wirings/workflow/workflow.types.ts +422 -0
  132. package/tsconfig.tsbuildinfo +1 -1
  133. package/dist/services/file-channel-store.d.ts +0 -19
  134. package/dist/services/file-channel-store.js +0 -69
  135. package/dist/services/file-eventhub-store.d.ts +0 -17
  136. package/dist/services/file-eventhub-store.js +0 -71
  137. package/src/services/file-channel-store.ts +0 -86
  138. package/src/services/file-eventhub-store.ts +0 -90
@@ -1,4 +1,4 @@
1
- import type { CoreServices } from '../../types/core.types.js'
1
+ import type { CoreServices, PikkuWire } from '../../types/core.types.js'
2
2
  import type { CoreQueueWorker, QueueJob, PikkuQueue } from './queue.types.js'
3
3
  import type {
4
4
  CorePikkuFunctionConfig,
@@ -7,10 +7,7 @@ import type {
7
7
  import { getErrorResponse, PikkuError } from '../../errors/error-handler.js'
8
8
  import { pikkuState } from '../../pikku-state.js'
9
9
  import { addFunction, runPikkuFunc } from '../../function/function-runner.js'
10
- import {
11
- CreateSessionServices,
12
- PikkuWiringTypes,
13
- } from '../../types/core.types.js'
10
+ import { CreateWireServices } from '../../types/core.types.js'
14
11
 
15
12
  /**
16
13
  * Error class for queue processor not found
@@ -70,7 +67,6 @@ export const wireQueueWorker = <
70
67
  permissions: queueWorker.func.permissions,
71
68
  middleware: queueWorker.func.middleware as any,
72
69
  tags: queueWorker.func.tags,
73
- docs: queueWorker.func.docs as any,
74
70
  })
75
71
 
76
72
  // Store processor definition in state - runtime adapters will pick this up
@@ -104,12 +100,12 @@ export async function removeQueueWorker(name: string): Promise<void> {
104
100
  */
105
101
  export async function runQueueJob({
106
102
  singletonServices,
107
- createSessionServices,
103
+ createWireServices,
108
104
  job,
109
105
  updateProgress,
110
106
  }: {
111
107
  singletonServices: CoreServices
112
- createSessionServices?: CreateSessionServices
108
+ createWireServices?: CreateWireServices
113
109
  job: QueueJob
114
110
  updateProgress?: (progress: number | string | object) => Promise<void>
115
111
  }): Promise<void> {
@@ -128,7 +124,7 @@ export async function runQueueJob({
128
124
  throw new Error(`Queue worker registration not found for: ${job.queueName}`)
129
125
  }
130
126
 
131
- // Create the queue interaction object
127
+ // Create the queue wire object
132
128
  const queue: PikkuQueue = {
133
129
  queueName: job.queueName,
134
130
  jobId: job.id,
@@ -149,34 +145,24 @@ export async function runQueueJob({
149
145
  try {
150
146
  logger.info(`Processing job ${job.id} in queue ${job.queueName}`)
151
147
 
152
- // Use provided singleton services
153
- const getAllServices = async () => {
154
- const sessionServices = await createSessionServices?.(
155
- singletonServices,
156
- { queue },
157
- undefined
158
- )
159
-
160
- return {
161
- ...singletonServices,
162
- ...sessionServices,
163
- }
148
+ const wire: PikkuWire = {
149
+ queue,
164
150
  }
165
151
 
166
152
  // Execute the pikku function with the job data
167
153
  const result = await runPikkuFunc(
168
- PikkuWiringTypes.queue,
154
+ 'queue',
169
155
  job.queueName,
170
156
  processorMeta.pikkuFuncName,
171
157
  {
172
158
  singletonServices,
173
- getAllServices,
159
+ createWireServices,
174
160
  auth: false,
175
161
  data: () => job.data,
176
162
  inheritedMiddleware: processorMeta.middleware,
177
163
  wireMiddleware: queueWorker.middleware,
178
164
  tags: queueWorker.tags,
179
- interaction: { queue },
165
+ wire,
180
166
  }
181
167
  )
182
168
 
@@ -1,4 +1,4 @@
1
- import { PikkuDocs, MiddlewareMetadata } from '../../types/core.types.js'
1
+ import { CommonWireMeta } from '../../types/core.types.js'
2
2
  import { CorePikkuFunctionConfig } from '../../function/functions.types.js'
3
3
  import { QueueConfigMapping } from './validate-worker-config.js'
4
4
 
@@ -140,25 +140,16 @@ export interface QueueWorkers {
140
140
 
141
141
  /** Scan state and register all compatible processors */
142
142
  registerQueues(): Promise<Record<string, ConfigValidationResult[]>>
143
-
144
- /** Close all queues and connections */
145
- close(): Promise<void>
146
143
  }
147
144
 
148
145
  /**
149
146
  * Queue processor metadata
150
147
  */
151
- export type queueWorkersMeta = Record<
148
+ export type QueueWorkersMeta = Record<
152
149
  string,
153
- {
154
- pikkuFuncName: string
155
- schemaName?: string
150
+ CommonWireMeta & {
156
151
  queueName: string
157
- session?: undefined
158
- docs?: PikkuDocs
159
- tags?: string[]
160
152
  config?: PikkuWorkerConfig
161
- middleware?: MiddlewareMetadata[] // Pre-resolved middleware chain (tag + explicit)
162
153
  }
163
154
  >
164
155
 
@@ -175,14 +166,13 @@ export type CoreQueueWorker<
175
166
  queueName: string
176
167
  func: PikkuFunctionConfig
177
168
  config?: PikkuWorkerConfig
178
- docs?: PikkuDocs
179
- session?: undefined
169
+ errors?: string[]
180
170
  tags?: string[]
181
171
  middleware?: PikkuFunctionConfig['middleware']
182
172
  }
183
173
 
184
174
  /**
185
- * Represents a queue interaction object for middleware
175
+ * Represents a queue wire object for middleware
186
176
  * Provides information and actions for the current queue job execution
187
177
  */
188
178
  export interface PikkuQueue {
@@ -1,2 +1,2 @@
1
- export { initialize, PikkuRPCService } from './rpc-runner.js'
1
+ export { initialize, PikkuRPCService, rpcService } from './rpc-runner.js'
2
2
  export type { PikkuRPC, RPCMeta } from './rpc-types.js'
@@ -1,8 +1,4 @@
1
- import {
2
- CoreServices,
3
- PikkuInteraction,
4
- PikkuWiringTypes,
5
- } from '../../types/core.types.js'
1
+ import { CoreServices, PikkuWire } from '../../types/core.types.js'
6
2
  import { runPikkuFunc } from '../../function/function-runner.js'
7
3
  import { pikkuState } from '../../pikku-state.js'
8
4
  import { ForbiddenError } from '../../errors/errors.js'
@@ -26,7 +22,7 @@ const getPikkuFunctionName = (rpcName: string): string => {
26
22
  export class ContextAwareRPCService {
27
23
  constructor(
28
24
  private services: CoreServices,
29
- private interaction: PikkuInteraction,
25
+ private wire: PikkuWire,
30
26
  private options: {
31
27
  coerceDataFromSchema?: boolean
32
28
  requiresAuth?: boolean
@@ -48,32 +44,77 @@ export class ContextAwareRPCService {
48
44
  funcName: string,
49
45
  data: In
50
46
  ): Promise<Out> {
51
- const rpcDepth = this.services.rpc?.depth || 0
52
- const pikkuFuncName = getPikkuFunctionName(funcName)
47
+ const rpcDepth = this.wire.rpc?.depth || 0
48
+ const updatedWire: PikkuWire = {
49
+ ...this.wire,
50
+ rpc: this.wire.rpc
51
+ ? {
52
+ ...this.wire.rpc,
53
+ depth: rpcDepth + 1,
54
+ global: false,
55
+ }
56
+ : undefined,
57
+ }
58
+ return runPikkuFunc<In, Out>(
59
+ 'rpc',
60
+ funcName,
61
+ getPikkuFunctionName(funcName),
62
+ {
63
+ auth: this.options.requiresAuth,
64
+ // TODO: this is a hack since services have already been created
65
+ // but is valid since we don't want to keep creating new wire services
66
+ singletonServices: this.services,
67
+ data: () => data,
68
+ coerceDataFromSchema: this.options.coerceDataFromSchema,
69
+ wire: updatedWire,
70
+ }
71
+ )
72
+ }
73
+
74
+ public async rpcWithWire<In = any, Out = any>(
75
+ rpcName: string,
76
+ data: In,
77
+ wire: PikkuWire
78
+ ): Promise<Out> {
79
+ const rpcDepth = this.wire.rpc?.depth || 0
80
+ const mergedWire: PikkuWire = {
81
+ ...this.wire,
82
+ ...wire,
83
+ rpc: this.wire.rpc
84
+ ? {
85
+ ...this.wire.rpc,
86
+ depth: rpcDepth + 1,
87
+ global: false,
88
+ }
89
+ : undefined,
90
+ }
53
91
  return runPikkuFunc<In, Out>(
54
- PikkuWiringTypes.rpc,
55
- pikkuFuncName,
56
- pikkuFuncName,
92
+ 'rpc',
93
+ rpcName,
94
+ getPikkuFunctionName(rpcName),
57
95
  {
58
96
  auth: this.options.requiresAuth,
59
97
  singletonServices: this.services,
60
- getAllServices: () => {
61
- this.services.rpc = this.services.rpc
62
- ? ({
63
- ...this.services.rpc,
64
- depth: rpcDepth + 1,
65
- global: false,
66
- } as any)
67
- : undefined
68
- return this.services
69
- },
70
98
  data: () => data,
71
- userSession: this.services.userSession,
72
99
  coerceDataFromSchema: this.options.coerceDataFromSchema,
73
- interaction: this.interaction,
100
+ wire: mergedWire,
74
101
  }
75
102
  )
76
103
  }
104
+
105
+ public async startWorkflow<In = any>(
106
+ workflowName: string,
107
+ input: In
108
+ ): Promise<{ runId: string }> {
109
+ if (!this.services.workflowService) {
110
+ throw new Error('WorkflowService service not available')
111
+ }
112
+ return this.services.workflowService.startWorkflow(
113
+ workflowName,
114
+ input,
115
+ this
116
+ )
117
+ }
77
118
  }
78
119
 
79
120
  // RPC Service class for the global interface
@@ -89,26 +130,24 @@ export class PikkuRPCService<
89
130
  }
90
131
 
91
132
  // Convenience function for initializing
92
- injectRPCService(
133
+ getContextRPCService(
93
134
  services: Services,
94
- interaction: PikkuInteraction,
135
+ wire: PikkuWire,
95
136
  requiresAuth?: boolean | undefined,
96
137
  depth: number = 0
97
- ): Services & { rpc: TypedRPC } {
98
- const serviceCopy = {
99
- ...services,
100
- }
101
- const serviceRPC = new ContextAwareRPCService(serviceCopy, interaction, {
138
+ ): TypedRPC {
139
+ const serviceRPC = new ContextAwareRPCService(services, wire, {
102
140
  coerceDataFromSchema: this.config?.coerceDataFromSchema,
103
141
  requiresAuth,
104
142
  })
105
- serviceCopy.rpc = {
143
+ return {
106
144
  depth,
107
145
  global: false,
108
146
  invoke: serviceRPC.rpc.bind(serviceRPC),
109
147
  invokeExposed: serviceRPC.rpc.bind(serviceRPC),
148
+ startWorkflow: serviceRPC.startWorkflow.bind(serviceRPC),
149
+ rpcWithWire: serviceRPC.rpcWithWire.bind(serviceRPC),
110
150
  } as any
111
- return serviceCopy as Services & { rpc: TypedRPC }
112
151
  }
113
152
  }
114
153
 
@@ -7,6 +7,7 @@ export type PikkuRPC<invoke extends Function = any> = {
7
7
  export type RPCMeta = {
8
8
  pikkuFuncName: string
9
9
  expose: boolean
10
+ internal?: boolean
10
11
  }
11
12
 
12
13
  /**