@pikku/core 0.9.10 → 0.9.12-next.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 (100) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/function/function-runner.d.ts +13 -8
  3. package/dist/function/function-runner.js +49 -37
  4. package/dist/index.d.ts +3 -1
  5. package/dist/index.js +3 -1
  6. package/dist/middleware-runner.d.ts +61 -40
  7. package/dist/middleware-runner.js +118 -66
  8. package/dist/permissions.js +1 -0
  9. package/dist/pikku-state.d.ts +26 -1
  10. package/dist/pikku-state.js +10 -1
  11. package/dist/services/content-service.d.ts +2 -3
  12. package/dist/services/index.d.ts +0 -1
  13. package/dist/services/index.js +0 -1
  14. package/dist/services/local-content.d.ts +2 -4
  15. package/dist/services/local-content.js +2 -2
  16. package/dist/types/core.types.d.ts +51 -2
  17. package/dist/types/core.types.js +21 -0
  18. package/dist/wirings/channel/channel-handler.d.ts +2 -2
  19. package/dist/wirings/channel/channel-handler.js +12 -9
  20. package/dist/wirings/channel/channel-runner.d.ts +0 -2
  21. package/dist/wirings/channel/channel-runner.js +2 -3
  22. package/dist/wirings/channel/channel.types.d.ts +5 -3
  23. package/dist/wirings/channel/local/local-channel-runner.js +12 -12
  24. package/dist/wirings/channel/serverless/serverless-channel-runner.js +7 -8
  25. package/dist/wirings/cli/channel/cli-channel-runner.d.ts +12 -0
  26. package/dist/wirings/cli/channel/cli-channel-runner.js +80 -0
  27. package/dist/wirings/cli/channel/index.d.ts +1 -0
  28. package/dist/wirings/cli/channel/index.js +1 -0
  29. package/dist/wirings/cli/cli-runner.d.ts +32 -0
  30. package/dist/wirings/cli/cli-runner.js +328 -0
  31. package/dist/wirings/cli/cli.types.d.ts +177 -0
  32. package/dist/wirings/cli/cli.types.js +1 -0
  33. package/dist/wirings/cli/command-parser.d.ts +19 -0
  34. package/dist/wirings/cli/command-parser.js +373 -0
  35. package/dist/wirings/cli/index.d.ts +5 -0
  36. package/dist/wirings/cli/index.js +5 -0
  37. package/dist/wirings/http/http-runner.d.ts +29 -10
  38. package/dist/wirings/http/http-runner.js +103 -131
  39. package/dist/wirings/http/http.types.d.ts +10 -10
  40. package/dist/wirings/http/index.d.ts +1 -1
  41. package/dist/wirings/http/index.js +1 -1
  42. package/dist/wirings/http/pikku-fetch-http-response.js +3 -1
  43. package/dist/wirings/http/routers/http-router.d.ts +0 -2
  44. package/dist/wirings/http/routers/path-to-regex.d.ts +1 -1
  45. package/dist/wirings/http/routers/path-to-regex.js +5 -34
  46. package/dist/wirings/mcp/mcp-runner.d.ts +4 -5
  47. package/dist/wirings/mcp/mcp-runner.js +30 -34
  48. package/dist/wirings/mcp/mcp.types.d.ts +11 -8
  49. package/dist/wirings/queue/queue-runner.d.ts +2 -2
  50. package/dist/wirings/queue/queue-runner.js +25 -27
  51. package/dist/wirings/queue/queue.types.d.ts +6 -5
  52. package/dist/wirings/rpc/index.d.ts +1 -1
  53. package/dist/wirings/rpc/index.js +1 -1
  54. package/dist/wirings/rpc/rpc-runner.d.ts +6 -18
  55. package/dist/wirings/rpc/rpc-runner.js +13 -8
  56. package/dist/wirings/scheduler/scheduler-runner.d.ts +2 -2
  57. package/dist/wirings/scheduler/scheduler-runner.js +37 -35
  58. package/dist/wirings/scheduler/scheduler.types.d.ts +4 -3
  59. package/package.json +4 -3
  60. package/src/function/function-runner.test.ts +73 -23
  61. package/src/function/function-runner.ts +74 -55
  62. package/src/index.ts +8 -1
  63. package/src/middleware-runner.test.ts +24 -16
  64. package/src/middleware-runner.ts +136 -83
  65. package/src/permissions.ts +1 -0
  66. package/src/pikku-state.ts +47 -2
  67. package/src/services/content-service.ts +5 -4
  68. package/src/services/index.ts +0 -1
  69. package/src/services/local-content.ts +11 -6
  70. package/src/types/core.types.ts +74 -3
  71. package/src/wirings/channel/channel-handler.ts +9 -13
  72. package/src/wirings/channel/channel-runner.ts +2 -6
  73. package/src/wirings/channel/channel.types.ts +5 -1
  74. package/src/wirings/channel/local/local-channel-runner.ts +25 -15
  75. package/src/wirings/channel/serverless/serverless-channel-runner.ts +15 -17
  76. package/src/wirings/cli/channel/cli-channel-runner.ts +118 -0
  77. package/src/wirings/cli/channel/index.ts +1 -0
  78. package/src/wirings/cli/cli-runner.test.ts +382 -0
  79. package/src/wirings/cli/cli-runner.ts +503 -0
  80. package/src/wirings/cli/cli.types.ts +320 -0
  81. package/src/wirings/cli/command-parser.test.ts +440 -0
  82. package/src/wirings/cli/command-parser.ts +470 -0
  83. package/src/wirings/cli/index.ts +12 -0
  84. package/src/wirings/http/http-runner.test.ts +8 -7
  85. package/src/wirings/http/http-runner.ts +126 -159
  86. package/src/wirings/http/http.types.ts +56 -11
  87. package/src/wirings/http/index.ts +1 -1
  88. package/src/wirings/http/pikku-fetch-http-response.ts +3 -1
  89. package/src/wirings/http/routers/http-router.ts +0 -2
  90. package/src/wirings/http/routers/path-to-regex.test.ts +4 -5
  91. package/src/wirings/http/routers/path-to-regex.ts +6 -43
  92. package/src/wirings/mcp/mcp-runner.ts +56 -55
  93. package/src/wirings/mcp/mcp.types.ts +23 -8
  94. package/src/wirings/queue/queue-runner.ts +44 -47
  95. package/src/wirings/queue/queue.types.ts +10 -6
  96. package/src/wirings/rpc/index.ts +1 -1
  97. package/src/wirings/rpc/rpc-runner.ts +27 -9
  98. package/src/wirings/scheduler/scheduler-runner.ts +57 -56
  99. package/src/wirings/scheduler/scheduler.types.ts +9 -2
  100. package/tsconfig.tsbuildinfo +1 -1
@@ -1,5 +1,5 @@
1
- import { CorePikkuFunctionSessionless } from '../../function/functions.types.js';
2
- import { CorePikkuMiddleware } from '../../types/core.types.js';
1
+ import { CorePikkuFunctionConfig, CorePikkuFunctionSessionless } from '../../function/functions.types.js';
2
+ import { CorePikkuMiddleware, MiddlewareMetadata } from '../../types/core.types.js';
3
3
  export type PikkuMCP<Tools extends string = any> = {
4
4
  uri?: string;
5
5
  sendResourceUpdated: (uri: string) => void;
@@ -10,23 +10,25 @@ export type PikkuMCP<Tools extends string = any> = {
10
10
  /**
11
11
  * Represents metadata for MCP resources, including name, description, and documentation.
12
12
  */
13
- export type MCPResourceMeta = Record<string, Omit<CoreMCPResource, 'func'> & {
13
+ export type MCPResourceMeta = Record<string, Omit<CoreMCPResource, 'func' | 'middleware'> & {
14
14
  pikkuFuncName: string;
15
15
  inputSchema: string | null;
16
16
  outputSchema: string | null;
17
+ middleware?: MiddlewareMetadata[];
17
18
  }>;
18
19
  /**
19
20
  * Represents metadata for MCP tools, including name, description, and documentation.
20
21
  */
21
- export type MCPToolMeta = Record<string, Omit<CoreMCPTool, 'func'> & {
22
+ export type MCPToolMeta = Record<string, Omit<CoreMCPTool, 'func' | 'middleware'> & {
22
23
  pikkuFuncName: string;
23
24
  inputSchema: string | null;
24
25
  outputSchema: string | null;
26
+ middleware?: MiddlewareMetadata[];
25
27
  }>;
26
28
  /**
27
29
  * Represents metadata for MCP prompts, including name, description, and arguments.
28
30
  */
29
- export type MCPPromptMeta = Record<string, Omit<CoreMCPPrompt, 'func'> & {
31
+ export type MCPPromptMeta = Record<string, Omit<CoreMCPPrompt, 'func' | 'middleware'> & {
30
32
  pikkuFuncName: string;
31
33
  inputSchema: string | null;
32
34
  outputSchema: string | null;
@@ -35,11 +37,12 @@ export type MCPPromptMeta = Record<string, Omit<CoreMCPPrompt, 'func'> & {
35
37
  description: string;
36
38
  required: boolean;
37
39
  }>;
40
+ middleware?: MiddlewareMetadata[];
38
41
  }>;
39
42
  /**
40
43
  * Represents an MCP resource with specific properties.
41
44
  */
42
- export type CoreMCPResource<PikkuFunction = CorePikkuFunctionSessionless<any, any>, PikkuMiddleware = CorePikkuMiddleware<any>> = {
45
+ export type CoreMCPResource<PikkuFunction = CorePikkuFunctionConfig<CorePikkuFunctionSessionless<any, any>>, PikkuMiddleware = CorePikkuMiddleware<any>> = {
43
46
  uri: string;
44
47
  title: string;
45
48
  description?: string;
@@ -53,7 +56,7 @@ export type CoreMCPResource<PikkuFunction = CorePikkuFunctionSessionless<any, an
53
56
  /**
54
57
  * Represents an MCP tool with specific properties.
55
58
  */
56
- export type CoreMCPTool<PikkuFunction = CorePikkuFunctionSessionless<any, any>, PikkuMiddleware = CorePikkuMiddleware<any>> = {
59
+ export type CoreMCPTool<PikkuFunction = CorePikkuFunctionConfig<CorePikkuFunctionSessionless<any, any>>, PikkuMiddleware = CorePikkuMiddleware<any>> = {
57
60
  name: string;
58
61
  title?: string;
59
62
  description: string;
@@ -66,7 +69,7 @@ export type CoreMCPTool<PikkuFunction = CorePikkuFunctionSessionless<any, any>,
66
69
  /**
67
70
  * Represents an MCP prompt with specific properties.
68
71
  */
69
- export type CoreMCPPrompt<PikkuFunction = CorePikkuFunctionSessionless<any, MCPPromptResponse>, PikkuMiddleware = CorePikkuMiddleware<any>> = {
72
+ export type CoreMCPPrompt<PikkuFunction = CorePikkuFunctionConfig<CorePikkuFunctionSessionless<any, MCPPromptResponse>>, PikkuMiddleware = CorePikkuMiddleware<any>> = {
70
73
  name: string;
71
74
  description?: string;
72
75
  func: PikkuFunction;
@@ -1,6 +1,6 @@
1
1
  import type { CoreServices } from '../../types/core.types.js';
2
2
  import type { CoreQueueWorker, QueueJob } from './queue.types.js';
3
- import type { CorePikkuFunctionSessionless } from '../../function/functions.types.js';
3
+ import type { CorePikkuFunctionConfig, CorePikkuFunctionSessionless } from '../../function/functions.types.js';
4
4
  import { PikkuError } from '../../errors/error-handler.js';
5
5
  import { CreateSessionServices } from '../../types/core.types.js';
6
6
  /**
@@ -18,7 +18,7 @@ export declare class QueueJobDiscardedError extends PikkuError {
18
18
  /**
19
19
  * Add a queue processor to the system
20
20
  */
21
- export declare const wireQueueWorker: <InputData = any, OutputData = any, PikkuFunction extends CorePikkuFunctionSessionless<InputData, OutputData> = CorePikkuFunctionSessionless<InputData, OutputData>>(queueWorker: CoreQueueWorker<PikkuFunction>) => void;
21
+ export declare const wireQueueWorker: <InputData = any, OutputData = any, PikkuFunctionConfig extends CorePikkuFunctionConfig<CorePikkuFunctionSessionless<InputData, OutputData>> = CorePikkuFunctionConfig<CorePikkuFunctionSessionless<InputData, OutputData>>>(queueWorker: CoreQueueWorker<PikkuFunctionConfig>) => void;
22
22
  /**
23
23
  * Get all registered queue processors
24
24
  */
@@ -2,7 +2,6 @@ import { getErrorResponse, PikkuError } from '../../errors/error-handler.js';
2
2
  import { pikkuState } from '../../pikku-state.js';
3
3
  import { addFunction, runPikkuFunc } from '../../function/function-runner.js';
4
4
  import { PikkuWiringTypes, } from '../../types/core.types.js';
5
- import { combineMiddleware, runMiddleware } from '../../middleware-runner.js';
6
5
  /**
7
6
  * Error class for queue processor not found
8
7
  */
@@ -40,7 +39,14 @@ export const wireQueueWorker = (queueWorker) => {
40
39
  throw new Error(`Queue processor metadata not found for '${queueWorker.queueName}'. Make sure to run the CLI to generate metadata.`);
41
40
  }
42
41
  // Register the function with pikku
43
- addFunction(processorMeta.pikkuFuncName, queueWorker.func);
42
+ addFunction(processorMeta.pikkuFuncName, {
43
+ func: queueWorker.func.func,
44
+ auth: queueWorker.func.auth,
45
+ permissions: queueWorker.func.permissions,
46
+ middleware: queueWorker.func.middleware,
47
+ tags: queueWorker.func.tags,
48
+ docs: queueWorker.func.docs,
49
+ });
44
50
  // Store processor definition in state - runtime adapters will pick this up
45
51
  const registrations = pikkuState('queue', 'registrations');
46
52
  registrations.set(queueWorker.queueName, queueWorker);
@@ -96,33 +102,25 @@ export async function runQueueJob({ singletonServices, createSessionServices, jo
96
102
  };
97
103
  try {
98
104
  logger.info(`Processing job ${job.id} in queue ${job.queueName}`);
99
- let result;
100
- // Main job execution logic wrapped for middleware handling
101
- const runMain = async () => {
102
- // Use provided singleton services
103
- const getAllServices = () => ({
105
+ // Use provided singleton services
106
+ const getAllServices = async () => {
107
+ const sessionServices = await createSessionServices?.(singletonServices, { queue }, undefined);
108
+ return {
104
109
  ...singletonServices,
105
- ...(createSessionServices
106
- ? createSessionServices(singletonServices, { queue }, undefined)
107
- : {}),
108
- });
109
- // Execute the pikku function with the job data
110
- result = await runPikkuFunc(PikkuWiringTypes.queue, job.queueName, processorMeta.pikkuFuncName, {
111
- getAllServices,
112
- data: job.data,
113
- tags: queueWorker.tags,
114
- });
115
- logger.debug(`Successfully processed job ${job.id} in queue ${job.queueName}`);
110
+ ...sessionServices,
111
+ };
116
112
  };
117
- // Get function config for middleware and tags
118
- const funcConfig = pikkuState('function', 'functions').get(processorMeta.pikkuFuncName);
119
- // Get middleware for tags and combine with queue-specific middleware
120
- await runMiddleware(singletonServices, { queue }, combineMiddleware(PikkuWiringTypes.queue, `${job.queueName}:${job.id}`, {
121
- wiringMiddleware: queueWorker.middleware,
122
- wiringTags: queueWorker.tags,
123
- funcMiddleware: funcConfig?.middleware,
124
- funcTags: funcConfig?.tags,
125
- }), runMain);
113
+ // Execute the pikku function with the job data
114
+ const result = await runPikkuFunc(PikkuWiringTypes.queue, job.queueName, processorMeta.pikkuFuncName, {
115
+ singletonServices,
116
+ getAllServices,
117
+ data: () => job.data,
118
+ inheritedMiddleware: processorMeta.middleware,
119
+ wireMiddleware: queueWorker.middleware,
120
+ tags: queueWorker.tags,
121
+ interaction: { queue },
122
+ });
123
+ logger.debug(`Successfully processed job ${job.id} in queue ${job.queueName}`);
126
124
  return result;
127
125
  }
128
126
  catch (error) {
@@ -1,5 +1,5 @@
1
- import { PikkuDocs, CorePikkuMiddleware } from '../../types/core.types.js';
2
- import { CorePikkuFunctionSessionless } from '../../function/functions.types.js';
1
+ import { PikkuDocs, MiddlewareMetadata } from '../../types/core.types.js';
2
+ import { CorePikkuFunctionConfig } from '../../function/functions.types.js';
3
3
  import { QueueConfigMapping } from './validate-worker-config.js';
4
4
  /**
5
5
  * Configuration for queue workers - how jobs are processed
@@ -140,18 +140,19 @@ export type queueWorkersMeta = Record<string, {
140
140
  docs?: PikkuDocs;
141
141
  tags?: string[];
142
142
  config?: PikkuWorkerConfig;
143
+ middleware?: MiddlewareMetadata[];
143
144
  }>;
144
145
  /**
145
146
  * Core queue processor definition
146
147
  */
147
- export type CoreQueueWorker<PikkuFunction = CorePikkuFunctionSessionless<any, any>, PikkuMiddleware = CorePikkuMiddleware<any>> = {
148
+ export type CoreQueueWorker<PikkuFunctionConfig extends CorePikkuFunctionConfig<any, any, any> = CorePikkuFunctionConfig<any, any, any>> = {
148
149
  queueName: string;
149
- func: PikkuFunction;
150
+ func: PikkuFunctionConfig;
150
151
  config?: PikkuWorkerConfig;
151
152
  docs?: PikkuDocs;
152
153
  session?: undefined;
153
154
  tags?: string[];
154
- middleware?: PikkuMiddleware[];
155
+ middleware?: PikkuFunctionConfig['middleware'];
155
156
  };
156
157
  /**
157
158
  * Represents a queue interaction object for middleware
@@ -1,2 +1,2 @@
1
- export { initialize } from './rpc-runner.js';
1
+ export { initialize, PikkuRPCService } from './rpc-runner.js';
2
2
  export type { PikkuRPC, RPCMeta } from './rpc-types.js';
@@ -1 +1 @@
1
- export { initialize } from './rpc-runner.js';
1
+ export { initialize, PikkuRPCService } from './rpc-runner.js';
@@ -1,27 +1,15 @@
1
- import { CoreServices } from '../../types/core.types.js';
1
+ import { CoreServices, PikkuInteraction } from '../../types/core.types.js';
2
+ import { PikkuRPC } from './rpc-types.js';
2
3
  type RPCServiceConfig = {
3
4
  coerceDataFromSchema: boolean;
4
5
  };
5
- export declare class PikkuRPCService {
6
+ export declare class PikkuRPCService<Services extends CoreServices, TypedRPC = PikkuRPC> {
6
7
  private config?;
7
8
  initialize(config: RPCServiceConfig): void;
8
- injectRPCService(coreServices: CoreServices, depth?: number): {
9
- schema?: import("../../index.js").SchemaService;
10
- config: {
11
- logLevel?: import("../../index.js").LogLevel;
12
- secrets?: {};
13
- };
14
- logger: import("../../index.js").Logger;
15
- variables: import("../../services/variables-service.js").VariablesService;
16
- http?: import("../http/http.types.js").PikkuHTTP<unknown> | undefined;
17
- mcp?: import("../mcp/mcp.types.js").PikkuMCP | undefined;
18
- rpc?: import("./rpc-types.js").PikkuRPC | undefined;
19
- channel?: import("../channel/channel.types.js").PikkuChannel<unknown, unknown> | undefined;
20
- scheduledTask?: import("../scheduler/scheduler.types.js").PikkuScheduledTask | undefined;
21
- queue?: import("../queue/queue.types.js").PikkuQueue | undefined;
22
- userSession?: import("../../index.js").UserSessionService<import("../../types/core.types.js").CoreUserSession> | undefined;
9
+ injectRPCService(services: Services, interaction: PikkuInteraction, requiresAuth?: boolean | undefined, depth?: number): Services & {
10
+ rpc: TypedRPC;
23
11
  };
24
12
  }
25
- export declare const rpcService: PikkuRPCService;
13
+ export declare const rpcService: PikkuRPCService<CoreServices, PikkuRPC>;
26
14
  export declare const initialize: (config: RPCServiceConfig) => void;
27
15
  export {};
@@ -1,4 +1,4 @@
1
- import { PikkuWiringTypes } from '../../types/core.types.js';
1
+ import { PikkuWiringTypes, } from '../../types/core.types.js';
2
2
  import { runPikkuFunc } from '../../function/function-runner.js';
3
3
  import { pikkuState } from '../../pikku-state.js';
4
4
  import { ForbiddenError } from '../../errors/errors.js';
@@ -13,9 +13,11 @@ const getPikkuFunctionName = (rpcName) => {
13
13
  // Context-aware RPC client for use within services
14
14
  class ContextAwareRPCService {
15
15
  services;
16
+ interaction;
16
17
  options;
17
- constructor(services, options) {
18
+ constructor(services, interaction, options) {
18
19
  this.services = services;
20
+ this.interaction = interaction;
19
21
  this.options = options;
20
22
  }
21
23
  async rpcExposed(funcName, data) {
@@ -29,10 +31,11 @@ class ContextAwareRPCService {
29
31
  return await this.rpc(funcName, data);
30
32
  }
31
33
  async rpc(funcName, data) {
32
- const session = await this.services.userSession?.get();
33
34
  const rpcDepth = this.services.rpc?.depth || 0;
34
35
  const pikkuFuncName = getPikkuFunctionName(funcName);
35
36
  return runPikkuFunc(PikkuWiringTypes.rpc, pikkuFuncName, pikkuFuncName, {
37
+ auth: this.options.requiresAuth,
38
+ singletonServices: this.services,
36
39
  getAllServices: () => {
37
40
  this.services.rpc = this.services.rpc
38
41
  ? {
@@ -43,9 +46,10 @@ class ContextAwareRPCService {
43
46
  : undefined;
44
47
  return this.services;
45
48
  },
46
- data,
47
- session,
49
+ data: () => data,
50
+ userSession: this.services.userSession,
48
51
  coerceDataFromSchema: this.options.coerceDataFromSchema,
52
+ interaction: this.interaction,
49
53
  });
50
54
  }
51
55
  }
@@ -57,12 +61,13 @@ export class PikkuRPCService {
57
61
  this.config = config;
58
62
  }
59
63
  // Convenience function for initializing
60
- injectRPCService(coreServices, depth = 0) {
64
+ injectRPCService(services, interaction, requiresAuth, depth = 0) {
61
65
  const serviceCopy = {
62
- ...coreServices,
66
+ ...services,
63
67
  };
64
- const serviceRPC = new ContextAwareRPCService(serviceCopy, {
68
+ const serviceRPC = new ContextAwareRPCService(serviceCopy, interaction, {
65
69
  coerceDataFromSchema: this.config?.coerceDataFromSchema,
70
+ requiresAuth,
66
71
  });
67
72
  serviceCopy.rpc = {
68
73
  depth,
@@ -1,12 +1,12 @@
1
1
  import { type CoreServices, type CoreSingletonServices, type CoreUserSession, type CreateSessionServices } from '../../types/core.types.js';
2
2
  import type { CoreScheduledTask } from './scheduler.types.js';
3
- import type { CorePikkuFunctionSessionless } from '../../function/functions.types.js';
3
+ import { CorePikkuFunctionConfig, CorePikkuFunctionSessionless } from '../../function/functions.types.js';
4
4
  export type RunScheduledTasksParams = {
5
5
  name: string;
6
6
  session?: CoreUserSession;
7
7
  singletonServices: CoreSingletonServices;
8
8
  createSessionServices?: CreateSessionServices<CoreSingletonServices, CoreServices<CoreSingletonServices>, CoreUserSession>;
9
9
  };
10
- export declare const wireScheduler: <PikkuFunction extends CorePikkuFunctionSessionless<void, void>>(scheduledTask: CoreScheduledTask<PikkuFunction>) => void;
10
+ export declare const wireScheduler: <PikkuFunctionConfig extends CorePikkuFunctionConfig<CorePikkuFunctionSessionless<void, void>>>(scheduledTask: CoreScheduledTask<PikkuFunctionConfig>) => void;
11
11
  export declare function runScheduledTask({ name, session, singletonServices, createSessionServices, }: RunScheduledTasksParams): Promise<void>;
12
12
  export declare const getScheduledTasks: () => Map<string, CoreScheduledTask>;
@@ -4,14 +4,21 @@ import { closeSessionServices } from '../../utils.js';
4
4
  import { pikkuState } from '../../pikku-state.js';
5
5
  import { addFunction, runPikkuFunc } from '../../function/function-runner.js';
6
6
  import { rpcService } from '../rpc/rpc-runner.js';
7
- import { combineMiddleware, runMiddleware } from '../../middleware-runner.js';
7
+ import { PikkuUserSessionService } from '../../services/user-session-service.js';
8
8
  export const wireScheduler = (scheduledTask) => {
9
9
  const meta = pikkuState('scheduler', 'meta');
10
10
  const taskMeta = meta[scheduledTask.name];
11
11
  if (!taskMeta) {
12
12
  throw new Error('Task metadata not found');
13
13
  }
14
- addFunction(taskMeta.pikkuFuncName, scheduledTask.func);
14
+ addFunction(taskMeta.pikkuFuncName, {
15
+ func: scheduledTask.func.func,
16
+ auth: scheduledTask.func.auth,
17
+ permissions: scheduledTask.func.permissions,
18
+ middleware: scheduledTask.func.middleware,
19
+ tags: scheduledTask.func.tags,
20
+ docs: scheduledTask.func.docs,
21
+ });
15
22
  const tasks = pikkuState('scheduler', 'tasks');
16
23
  if (tasks.has(scheduledTask.name)) {
17
24
  throw new Error(`Scheduled task already exists: ${scheduledTask.name}`);
@@ -33,6 +40,10 @@ export async function runScheduledTask({ name, session, singletonServices, creat
33
40
  let sessionServices;
34
41
  const task = pikkuState('scheduler', 'tasks').get(name);
35
42
  const meta = pikkuState('scheduler', 'meta')[name];
43
+ const userSession = new PikkuUserSessionService();
44
+ if (session) {
45
+ userSession.set(session);
46
+ }
36
47
  if (!task) {
37
48
  throw new ScheduledTaskNotFoundError(`Scheduled task not found: ${name}`);
38
49
  }
@@ -40,44 +51,35 @@ export async function runScheduledTask({ name, session, singletonServices, creat
40
51
  throw new ScheduledTaskNotFoundError(`Scheduled task meta not found: ${name}`);
41
52
  }
42
53
  // Create the scheduled task interaction object
43
- const scheduledTask = {
44
- name,
45
- schedule: task.schedule,
46
- executionTime: new Date(),
47
- skip: (reason) => {
48
- throw new ScheduledTaskSkippedError(name, reason);
54
+ const interaction = {
55
+ scheduledTask: {
56
+ name,
57
+ schedule: task.schedule,
58
+ executionTime: new Date(),
59
+ skip: (reason) => {
60
+ throw new ScheduledTaskSkippedError(name, reason);
61
+ },
49
62
  },
50
63
  };
51
64
  try {
52
65
  singletonServices.logger.info(`Running schedule task: ${name} | schedule: ${task.schedule}`);
53
- let result;
54
- // Main scheduled task execution logic wrapped for middleware handling
55
- const runMain = async () => {
56
- const getAllServices = async () => {
57
- if (createSessionServices) {
58
- const services = await createSessionServices(singletonServices, { scheduledTask }, session);
59
- sessionServices = services;
60
- return rpcService.injectRPCService({
61
- ...singletonServices,
62
- ...services,
63
- });
64
- }
65
- return singletonServices;
66
- };
67
- result = await runPikkuFunc(PikkuWiringTypes.scheduler, meta.name, meta.pikkuFuncName, {
68
- getAllServices,
69
- session,
70
- data: undefined,
71
- tags: task.tags,
72
- });
66
+ const getAllServices = async () => {
67
+ sessionServices = await createSessionServices?.(singletonServices, interaction, session);
68
+ return rpcService.injectRPCService({
69
+ ...singletonServices,
70
+ ...sessionServices,
71
+ }, interaction);
73
72
  };
74
- const funcConfig = pikkuState('function', 'functions').get(meta.pikkuFuncName);
75
- await runMiddleware(singletonServices, { scheduledTask }, combineMiddleware(PikkuWiringTypes.scheduler, meta.name, {
76
- wiringMiddleware: task.middleware,
77
- wiringTags: task.tags,
78
- funcMiddleware: funcConfig?.middleware,
79
- funcTags: funcConfig?.tags,
80
- }), runMain);
73
+ const result = await runPikkuFunc(PikkuWiringTypes.scheduler, meta.name, meta.pikkuFuncName, {
74
+ singletonServices,
75
+ getAllServices,
76
+ userSession,
77
+ data: () => undefined,
78
+ inheritedMiddleware: meta.middleware,
79
+ wireMiddleware: task.middleware,
80
+ tags: task.tags,
81
+ interaction,
82
+ });
81
83
  return result;
82
84
  }
83
85
  catch (e) {
@@ -1,5 +1,5 @@
1
- import { PikkuDocs, CoreUserSession, CorePikkuMiddleware } from '../../types/core.types.js';
2
- import { CorePikkuFunctionSessionless } from '../../function/functions.types.js';
1
+ import { PikkuDocs, CoreUserSession, CorePikkuMiddleware, MiddlewareMetadata } from '../../types/core.types.js';
2
+ import { CorePikkuFunctionConfig, CorePikkuFunctionSessionless } from '../../function/functions.types.js';
3
3
  /**
4
4
  * Represents metadata for scheduled tasks, including title, schedule, and documentation.
5
5
  */
@@ -10,11 +10,12 @@ export type ScheduledTasksMeta<UserSession extends CoreUserSession = any> = Reco
10
10
  session?: UserSession;
11
11
  docs?: PikkuDocs;
12
12
  tags?: string[];
13
+ middleware?: MiddlewareMetadata[];
13
14
  }>;
14
15
  /**
15
16
  * Represents a core scheduled task.
16
17
  */
17
- export type CoreScheduledTask<PikkuFunction = CorePikkuFunctionSessionless<void, void>, PikkuMiddleware = CorePikkuMiddleware<any>> = {
18
+ export type CoreScheduledTask<PikkuFunction = CorePikkuFunctionConfig<CorePikkuFunctionSessionless<void, void>>, PikkuMiddleware = CorePikkuMiddleware<any>> = {
18
19
  name: string;
19
20
  schedule: string;
20
21
  func: PikkuFunction;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pikku/core",
3
- "version": "0.9.10",
3
+ "version": "0.9.12-next.0",
4
4
  "author": "yasser.fadl@gmail.com",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",
@@ -8,8 +8,7 @@
8
8
  "type": "module",
9
9
  "scripts": {
10
10
  "tsc": "tsc",
11
- "build:esm": "tsc -b",
12
- "build": "yarn build:esm",
11
+ "build": "tsc -b",
13
12
  "ncu": "npx npm-check-updates",
14
13
  "release": "npm run build && npm test",
15
14
  "test": "bash run-tests.sh",
@@ -28,8 +27,10 @@
28
27
  "./scheduler": "./dist/wirings/scheduler/index.js",
29
28
  "./rpc": "./dist/wirings/rpc/index.js",
30
29
  "./mcp": "./dist/wirings/mcp/index.js",
30
+ "./cli/channel": "./dist/wirings/cli/channel/index.js",
31
31
  "./errors": "./dist/errors/index.js",
32
32
  "./services": "./dist/services/index.js",
33
+ "./services/local-content": "./dist/services/local-content.js",
33
34
  "./schema": "./dist/schema.js",
34
35
  "./types": "./dist/types/index.d.ts"
35
36
  },