@pikku/core 0.10.1 → 0.11.0

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 (81) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/dist/function/function-runner.js +2 -2
  3. package/dist/function/functions.types.d.ts +1 -0
  4. package/dist/index.d.ts +2 -0
  5. package/dist/index.js +2 -0
  6. package/dist/middleware/auth-apikey.d.ts +1 -0
  7. package/dist/middleware/auth-bearer.d.ts +1 -0
  8. package/dist/middleware/auth-cookie.d.ts +1 -0
  9. package/dist/middleware/timeout.d.ts +1 -0
  10. package/dist/middleware-runner.js +1 -0
  11. package/dist/permissions.js +1 -0
  12. package/dist/pikku-state.d.ts +7 -2
  13. package/dist/pikku-state.js +4 -0
  14. package/dist/services/index.d.ts +1 -0
  15. package/dist/services/index.js +1 -0
  16. package/dist/services/scheduler-service.d.ts +63 -0
  17. package/dist/services/scheduler-service.js +6 -0
  18. package/dist/time-utils.d.ts +14 -0
  19. package/dist/time-utils.js +62 -0
  20. package/dist/types/core.types.d.ts +24 -2
  21. package/dist/types/core.types.js +1 -0
  22. package/dist/wirings/channel/channel-common.d.ts +28 -0
  23. package/dist/wirings/channel/channel-common.js +42 -0
  24. package/dist/wirings/channel/channel-handler.js +37 -11
  25. package/dist/wirings/channel/channel-runner.js +9 -4
  26. package/dist/wirings/channel/channel.types.d.ts +8 -2
  27. package/dist/wirings/channel/local/local-channel-handler.js +3 -0
  28. package/dist/wirings/channel/local/local-channel-runner.js +47 -14
  29. package/dist/wirings/channel/serverless/serverless-channel-runner.js +90 -41
  30. package/dist/wirings/cli/channel/cli-channel-runner.js +10 -1
  31. package/dist/wirings/cli/cli-runner.d.ts +1 -1
  32. package/dist/wirings/cli/cli-runner.js +1 -1
  33. package/dist/wirings/http/http-runner.js +0 -1
  34. package/dist/wirings/mcp/mcp-runner.js +3 -2
  35. package/dist/wirings/queue/queue-runner.js +6 -3
  36. package/dist/wirings/queue/queue.types.d.ts +1 -3
  37. package/dist/wirings/rpc/index.d.ts +1 -1
  38. package/dist/wirings/rpc/index.js +1 -1
  39. package/dist/wirings/rpc/rpc-runner.d.ts +15 -0
  40. package/dist/wirings/rpc/rpc-runner.js +38 -3
  41. package/dist/wirings/rpc/rpc-types.d.ts +1 -0
  42. package/dist/wirings/workflow/index.d.ts +6 -0
  43. package/dist/wirings/workflow/index.js +6 -0
  44. package/dist/wirings/workflow/pikku-workflow-service.d.ts +152 -0
  45. package/dist/wirings/workflow/pikku-workflow-service.js +448 -0
  46. package/dist/wirings/workflow/workflow-runner.d.ts +35 -0
  47. package/dist/wirings/workflow/workflow-runner.js +68 -0
  48. package/dist/wirings/workflow/workflow.types.d.ts +247 -0
  49. package/dist/wirings/workflow/workflow.types.js +1 -0
  50. package/package.json +2 -3
  51. package/src/function/function-runner.ts +2 -2
  52. package/src/function/functions.types.ts +1 -0
  53. package/src/index.ts +2 -0
  54. package/src/middleware-runner.ts +1 -0
  55. package/src/permissions.ts +1 -0
  56. package/src/pikku-state.ts +14 -2
  57. package/src/services/index.ts +1 -0
  58. package/src/services/scheduler-service.ts +76 -0
  59. package/src/time-utils.ts +75 -0
  60. package/src/types/core.types.ts +30 -1
  61. package/src/wirings/channel/channel-common.ts +80 -0
  62. package/src/wirings/channel/channel-handler.ts +48 -18
  63. package/src/wirings/channel/channel-runner.ts +15 -4
  64. package/src/wirings/channel/channel.types.ts +12 -2
  65. package/src/wirings/channel/local/local-channel-handler.ts +3 -0
  66. package/src/wirings/channel/local/local-channel-runner.ts +48 -36
  67. package/src/wirings/channel/serverless/serverless-channel-runner.ts +134 -66
  68. package/src/wirings/cli/channel/cli-channel-runner.ts +13 -1
  69. package/src/wirings/cli/cli-runner.ts +2 -2
  70. package/src/wirings/http/http-runner.ts +0 -2
  71. package/src/wirings/mcp/mcp-runner.ts +5 -2
  72. package/src/wirings/queue/queue-runner.ts +14 -6
  73. package/src/wirings/queue/queue.types.ts +1 -4
  74. package/src/wirings/rpc/index.ts +1 -1
  75. package/src/wirings/rpc/rpc-runner.ts +57 -4
  76. package/src/wirings/rpc/rpc-types.ts +1 -0
  77. package/src/wirings/workflow/index.ts +22 -0
  78. package/src/wirings/workflow/pikku-workflow-service.ts +756 -0
  79. package/src/wirings/workflow/workflow-runner.ts +85 -0
  80. package/src/wirings/workflow/workflow.types.ts +332 -0
  81. package/tsconfig.tsbuildinfo +1 -1
@@ -434,7 +434,7 @@ export async function executeCLI({
434
434
  args?: string[]
435
435
  createConfig: CreateConfig<any, any>
436
436
  createSingletonServices: CreateSingletonServices<any, any>
437
- createSessionServices?: CreateSessionServices<any, any>
437
+ createSessionServices?: CreateSessionServices<any, any, any>
438
438
  }): Promise<void> {
439
439
  if (!args) {
440
440
  throw new Error(
@@ -449,7 +449,7 @@ export async function executeCLI({
449
449
  | undefined
450
450
  if (!allCLIMeta) {
451
451
  throw new Error(
452
- '[PKU342] CLI metadata not found. No CLI wirings were registered. See https://pikku.dev/errors/pku342 for more information.'
452
+ '[PKU342] CLI metadata not found. No CLI wirings were registered. See https://pikku.dev/docs/pikku-cli/errors/pku342 for more information.'
453
453
  )
454
454
  }
455
455
  const programMeta = allCLIMeta.programs[programName]
@@ -327,8 +327,6 @@ const executeRoute = async (
327
327
  const interaction: PikkuInteraction = { http, channel }
328
328
 
329
329
  const getAllServices = async (session?: CoreUserSession) => {
330
- let channel: PikkuChannel<unknown, unknown> | undefined
331
-
332
330
  // Create session-specific services for handling the request
333
331
  sessionServices = await createSessionServices(
334
332
  { ...singletonServices, userSession, channel },
@@ -62,7 +62,7 @@ export const wireMCPResource = <
62
62
  if (!mcpResourceMeta) {
63
63
  throw new Error(`MCP resource metadata not found for '${mcpResource.uri}'`)
64
64
  }
65
- addFunction(mcpResourceMeta.pikkuFuncName, mcpResource)
65
+ addFunction(mcpResourceMeta.pikkuFuncName, mcpResource.func as any)
66
66
  const resources = pikkuState('mcp', 'resources')
67
67
  if (resources.has(mcpResource.uri)) {
68
68
  throw new Error(`MCP resource already exists: ${mcpResource.uri}`)
@@ -160,7 +160,9 @@ export async function runMCPResource(
160
160
  uri,
161
161
  endpoint,
162
162
  pikkuFuncName,
163
- { ...params, mcp: { uri } } as RunMCPEndpointParams<keyof CoreMCPResource>
163
+ { ...params, mcp: { ...params.mcp, uri } } as RunMCPEndpointParams<
164
+ keyof CoreMCPResource
165
+ >
164
166
  )
165
167
  }
166
168
 
@@ -250,6 +252,7 @@ async function runMCPPikkuFunc(
250
252
  {
251
253
  ...singletonServices,
252
254
  ...sessionServices,
255
+ mcp: mcpInteraction,
253
256
  },
254
257
  interaction
255
258
  )
@@ -1,4 +1,4 @@
1
- import type { CoreServices } from '../../types/core.types.js'
1
+ import type { CoreServices, PikkuInteraction } from '../../types/core.types.js'
2
2
  import type { CoreQueueWorker, QueueJob, PikkuQueue } from './queue.types.js'
3
3
  import type {
4
4
  CorePikkuFunctionConfig,
@@ -11,6 +11,7 @@ import {
11
11
  CreateSessionServices,
12
12
  PikkuWiringTypes,
13
13
  } from '../../types/core.types.js'
14
+ import { rpcService } from '../rpc/rpc-runner.js'
14
15
 
15
16
  /**
16
17
  * Error class for queue processor not found
@@ -149,6 +150,7 @@ export async function runQueueJob({
149
150
  try {
150
151
  logger.info(`Processing job ${job.id} in queue ${job.queueName}`)
151
152
 
153
+ const interaction: PikkuInteraction = { queue }
152
154
  // Use provided singleton services
153
155
  const getAllServices = async () => {
154
156
  const sessionServices = await createSessionServices?.(
@@ -157,10 +159,16 @@ export async function runQueueJob({
157
159
  undefined
158
160
  )
159
161
 
160
- return {
161
- ...singletonServices,
162
- ...sessionServices,
163
- }
162
+ const services = rpcService.injectRPCService(
163
+ {
164
+ ...singletonServices,
165
+ ...sessionServices,
166
+ },
167
+ interaction,
168
+ false
169
+ )
170
+
171
+ return services
164
172
  }
165
173
 
166
174
  // Execute the pikku function with the job data
@@ -176,7 +184,7 @@ export async function runQueueJob({
176
184
  inheritedMiddleware: processorMeta.middleware,
177
185
  wireMiddleware: queueWorker.middleware,
178
186
  tags: queueWorker.tags,
179
- interaction: { queue },
187
+ interaction,
180
188
  }
181
189
  )
182
190
 
@@ -140,15 +140,12 @@ 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
150
  {
154
151
  pikkuFuncName: string
@@ -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'
@@ -23,7 +23,7 @@ const getPikkuFunctionName = (rpcName: string): string => {
23
23
  }
24
24
 
25
25
  // Context-aware RPC client for use within services
26
- class ContextAwareRPCService {
26
+ export class ContextAwareRPCService {
27
27
  constructor(
28
28
  private services: CoreServices,
29
29
  private interaction: PikkuInteraction,
@@ -49,11 +49,10 @@ class ContextAwareRPCService {
49
49
  data: In
50
50
  ): Promise<Out> {
51
51
  const rpcDepth = this.services.rpc?.depth || 0
52
- const pikkuFuncName = getPikkuFunctionName(funcName)
53
52
  return runPikkuFunc<In, Out>(
54
53
  PikkuWiringTypes.rpc,
55
- pikkuFuncName,
56
- pikkuFuncName,
54
+ funcName,
55
+ getPikkuFunctionName(funcName),
57
56
  {
58
57
  auth: this.options.requiresAuth,
59
58
  singletonServices: this.services,
@@ -74,6 +73,58 @@ class ContextAwareRPCService {
74
73
  }
75
74
  )
76
75
  }
76
+
77
+ public async rpcWithInteraction<In = any, Out = any>(
78
+ funcName: string,
79
+ data: In,
80
+ interaction: PikkuInteraction
81
+ ): Promise<Out> {
82
+ const rpcDepth = this.services.rpc?.depth || 0
83
+ const mergedInteraction = {
84
+ ...this.interaction,
85
+ ...interaction,
86
+ }
87
+ return runPikkuFunc<In, Out>(
88
+ PikkuWiringTypes.rpc,
89
+ funcName,
90
+ getPikkuFunctionName(funcName),
91
+ {
92
+ auth: this.options.requiresAuth,
93
+ singletonServices: this.services,
94
+ getAllServices: () => {
95
+ this.services.rpc = this.services.rpc
96
+ ? ({
97
+ ...this.services.rpc,
98
+ depth: rpcDepth + 1,
99
+ global: false,
100
+ } as any)
101
+ : undefined
102
+ return {
103
+ ...this.services,
104
+ ...mergedInteraction,
105
+ }
106
+ },
107
+ data: () => data,
108
+ userSession: this.services.userSession,
109
+ coerceDataFromSchema: this.options.coerceDataFromSchema,
110
+ interaction: mergedInteraction,
111
+ }
112
+ )
113
+ }
114
+
115
+ public async startWorkflow<In = any>(
116
+ workflowName: string,
117
+ input: In
118
+ ): Promise<{ runId: string }> {
119
+ if (!this.services.workflowService) {
120
+ throw new Error('WorkflowService service not available')
121
+ }
122
+ return this.services.workflowService.startWorkflow(
123
+ workflowName,
124
+ input,
125
+ this
126
+ )
127
+ }
77
128
  }
78
129
 
79
130
  // RPC Service class for the global interface
@@ -107,6 +158,8 @@ export class PikkuRPCService<
107
158
  global: false,
108
159
  invoke: serviceRPC.rpc.bind(serviceRPC),
109
160
  invokeExposed: serviceRPC.rpc.bind(serviceRPC),
161
+ startWorkflow: serviceRPC.startWorkflow.bind(serviceRPC),
162
+ rpcWithInteraction: serviceRPC.rpcWithInteraction.bind(serviceRPC),
110
163
  } as any
111
164
  return serviceCopy as Services & { rpc: TypedRPC }
112
165
  }
@@ -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
  /**
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Workflow module exports
3
+ */
4
+
5
+ // Types
6
+ export type {
7
+ CoreWorkflow,
8
+ WorkflowStepOptions,
9
+ WorkflowStepMeta,
10
+ PikkuWorkflowInteraction,
11
+ PikkuWorkflow,
12
+ WorkflowsMeta,
13
+ WorkflowRun,
14
+ StepState,
15
+ WorkflowStatus,
16
+ StepStatus,
17
+ } from './workflow.types.js'
18
+
19
+ export { PikkuWorkflowService } from './pikku-workflow-service.js'
20
+
21
+ // Functions
22
+ export { wireWorkflow } from './workflow-runner.js'