@pikku/core 0.11.0 → 0.11.2

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 (184) hide show
  1. package/CHANGELOG.md +35 -1
  2. package/dist/errors/error-handler.js +3 -3
  3. package/dist/errors/errors.d.ts +1 -1
  4. package/dist/errors/errors.js +3 -3
  5. package/dist/function/function-runner.d.ts +8 -8
  6. package/dist/function/function-runner.js +88 -23
  7. package/dist/function/functions.types.d.ts +104 -33
  8. package/dist/function/functions.types.js +50 -7
  9. package/dist/handle-error.d.ts +1 -1
  10. package/dist/handle-error.js +1 -1
  11. package/dist/index.d.ts +6 -2
  12. package/dist/index.js +5 -2
  13. package/dist/middleware/auth-apikey.d.ts +1 -1
  14. package/dist/middleware/auth-apikey.js +3 -3
  15. package/dist/middleware/auth-bearer.d.ts +1 -1
  16. package/dist/middleware/auth-bearer.js +3 -3
  17. package/dist/middleware/auth-cookie.d.ts +1 -1
  18. package/dist/middleware/auth-cookie.js +5 -5
  19. package/dist/middleware/timeout.d.ts +1 -1
  20. package/dist/middleware/timeout.js +3 -2
  21. package/dist/middleware-runner.d.ts +6 -20
  22. package/dist/middleware-runner.js +19 -20
  23. package/dist/permissions.d.ts +6 -89
  24. package/dist/permissions.js +24 -81
  25. package/dist/pikku-state.d.ts +32 -104
  26. package/dist/pikku-state.js +140 -66
  27. package/dist/schema.d.ts +8 -6
  28. package/dist/schema.js +25 -13
  29. package/dist/services/user-session-service.d.ts +7 -3
  30. package/dist/services/user-session-service.js +8 -1
  31. package/dist/services/workflow-service.d.ts +38 -0
  32. package/dist/services/workflow-service.js +1 -0
  33. package/dist/types/core.types.d.ts +40 -41
  34. package/dist/types/core.types.js +4 -15
  35. package/dist/types/state.types.d.ts +135 -0
  36. package/dist/types/state.types.js +1 -0
  37. package/dist/utils.d.ts +9 -1
  38. package/dist/utils.js +48 -3
  39. package/dist/wirings/channel/channel-common.js +2 -3
  40. package/dist/wirings/channel/channel-handler.d.ts +3 -2
  41. package/dist/wirings/channel/channel-handler.js +7 -10
  42. package/dist/wirings/channel/channel-runner.d.ts +2 -2
  43. package/dist/wirings/channel/channel-runner.js +5 -5
  44. package/dist/wirings/channel/channel.types.d.ts +10 -5
  45. package/dist/wirings/channel/local/local-channel-runner.d.ts +1 -1
  46. package/dist/wirings/channel/local/local-channel-runner.js +19 -26
  47. package/dist/wirings/channel/log-channels.js +1 -1
  48. package/dist/wirings/channel/serverless/serverless-channel-runner.d.ts +1 -1
  49. package/dist/wirings/channel/serverless/serverless-channel-runner.js +43 -48
  50. package/dist/wirings/cli/channel/cli-channel-runner.js +3 -2
  51. package/dist/wirings/cli/cli-runner.d.ts +5 -5
  52. package/dist/wirings/cli/cli-runner.js +20 -38
  53. package/dist/wirings/cli/cli.types.d.ts +16 -11
  54. package/dist/wirings/forge-node/forge-node.types.d.ts +120 -0
  55. package/dist/wirings/forge-node/forge-node.types.js +38 -0
  56. package/dist/wirings/forge-node/index.d.ts +1 -0
  57. package/dist/wirings/forge-node/index.js +1 -0
  58. package/dist/wirings/http/http-runner.d.ts +8 -8
  59. package/dist/wirings/http/http-runner.js +46 -51
  60. package/dist/wirings/http/http.types.d.ts +19 -11
  61. package/dist/wirings/http/log-http-routes.js +1 -1
  62. package/dist/wirings/http/routers/path-to-regex.js +2 -2
  63. package/dist/wirings/mcp/mcp-runner.d.ts +2 -2
  64. package/dist/wirings/mcp/mcp-runner.js +33 -40
  65. package/dist/wirings/mcp/mcp.types.d.ts +6 -0
  66. package/dist/wirings/queue/queue-runner.d.ts +3 -3
  67. package/dist/wirings/queue/queue-runner.js +13 -23
  68. package/dist/wirings/queue/queue.types.d.ts +4 -11
  69. package/dist/wirings/rpc/index.d.ts +1 -1
  70. package/dist/wirings/rpc/index.js +1 -1
  71. package/dist/wirings/rpc/rpc-runner.d.ts +17 -16
  72. package/dist/wirings/rpc/rpc-runner.js +98 -61
  73. package/dist/wirings/rpc/rpc-types.d.ts +7 -0
  74. package/dist/wirings/scheduler/log-schedulers.js +1 -1
  75. package/dist/wirings/scheduler/scheduler-runner.d.ts +3 -3
  76. package/dist/wirings/scheduler/scheduler-runner.js +18 -29
  77. package/dist/wirings/scheduler/scheduler.types.d.ts +3 -8
  78. package/dist/wirings/trigger/index.d.ts +2 -0
  79. package/dist/wirings/trigger/index.js +2 -0
  80. package/dist/wirings/trigger/trigger-runner.d.ts +29 -0
  81. package/dist/wirings/trigger/trigger-runner.js +57 -0
  82. package/dist/wirings/trigger/trigger.types.d.ts +42 -0
  83. package/dist/wirings/trigger/trigger.types.js +1 -0
  84. package/dist/wirings/workflow/dsl/index.d.ts +5 -0
  85. package/dist/wirings/workflow/dsl/index.js +4 -0
  86. package/dist/wirings/workflow/dsl/workflow-dsl.types.d.ts +286 -0
  87. package/dist/wirings/workflow/dsl/workflow-dsl.types.js +5 -0
  88. package/dist/wirings/workflow/dsl/workflow-runner.d.ts +5 -0
  89. package/dist/wirings/workflow/dsl/workflow-runner.js +22 -0
  90. package/dist/wirings/workflow/graph/graph-node.d.ts +122 -0
  91. package/dist/wirings/workflow/graph/graph-node.js +58 -0
  92. package/dist/wirings/workflow/graph/graph-runner.d.ts +35 -0
  93. package/dist/wirings/workflow/graph/graph-runner.js +452 -0
  94. package/dist/wirings/workflow/graph/index.d.ts +3 -0
  95. package/dist/wirings/workflow/graph/index.js +3 -0
  96. package/dist/wirings/workflow/graph/workflow-graph.types.d.ts +95 -0
  97. package/dist/wirings/workflow/graph/workflow-graph.types.js +15 -0
  98. package/dist/wirings/workflow/index.d.ts +7 -3
  99. package/dist/wirings/workflow/index.js +6 -3
  100. package/dist/wirings/workflow/pikku-workflow-service.d.ts +108 -10
  101. package/dist/wirings/workflow/pikku-workflow-service.js +274 -165
  102. package/dist/wirings/workflow/wire-workflow.d.ts +42 -0
  103. package/dist/wirings/workflow/wire-workflow.js +53 -0
  104. package/dist/wirings/workflow/workflow-utils.d.ts +23 -0
  105. package/dist/wirings/workflow/workflow-utils.js +66 -0
  106. package/dist/wirings/workflow/workflow.types.d.ts +135 -128
  107. package/package.json +11 -2
  108. package/src/errors/error-handler.ts +3 -3
  109. package/src/errors/errors.ts +3 -3
  110. package/src/factory-functions.test.ts +9 -36
  111. package/src/function/function-runner.test.ts +58 -69
  112. package/src/function/function-runner.ts +130 -37
  113. package/src/function/functions.types.ts +193 -54
  114. package/src/handle-error.ts +1 -1
  115. package/src/index.ts +10 -5
  116. package/src/middleware/auth-apikey.test.ts +360 -0
  117. package/src/middleware/auth-apikey.ts +3 -7
  118. package/src/middleware/auth-bearer.test.ts +447 -0
  119. package/src/middleware/auth-bearer.ts +27 -33
  120. package/src/middleware/auth-cookie.test.ts +525 -0
  121. package/src/middleware/auth-cookie.ts +6 -6
  122. package/src/middleware/timeout.ts +4 -2
  123. package/src/middleware-runner.test.ts +58 -127
  124. package/src/middleware-runner.ts +29 -25
  125. package/src/permissions.test.ts +68 -130
  126. package/src/permissions.ts +41 -100
  127. package/src/pikku-state.ts +156 -201
  128. package/src/schema.ts +42 -13
  129. package/src/services/user-session-service.ts +14 -4
  130. package/src/services/workflow-service.ts +74 -0
  131. package/src/types/core.types.ts +79 -58
  132. package/src/types/state.types.ts +195 -0
  133. package/src/utils.ts +59 -4
  134. package/src/wirings/channel/channel-common.ts +4 -7
  135. package/src/wirings/channel/channel-handler.ts +17 -23
  136. package/src/wirings/channel/channel-runner.ts +7 -7
  137. package/src/wirings/channel/channel.types.ts +14 -11
  138. package/src/wirings/channel/local/local-channel-runner.test.ts +8 -4
  139. package/src/wirings/channel/local/local-channel-runner.ts +23 -38
  140. package/src/wirings/channel/log-channels.ts +1 -1
  141. package/src/wirings/channel/serverless/serverless-channel-runner.ts +53 -79
  142. package/src/wirings/cli/channel/cli-channel-runner.ts +4 -3
  143. package/src/wirings/cli/cli-runner.test.ts +26 -26
  144. package/src/wirings/cli/cli-runner.ts +34 -71
  145. package/src/wirings/cli/cli.types.ts +23 -16
  146. package/src/wirings/forge-node/forge-node.types.ts +135 -0
  147. package/src/wirings/forge-node/index.ts +1 -0
  148. package/src/wirings/http/http-runner.test.ts +10 -10
  149. package/src/wirings/http/http-runner.ts +56 -66
  150. package/src/wirings/http/http.types.ts +19 -14
  151. package/src/wirings/http/log-http-routes.ts +1 -1
  152. package/src/wirings/http/routers/path-to-regex.test.ts +30 -19
  153. package/src/wirings/http/routers/path-to-regex.ts +2 -2
  154. package/src/wirings/mcp/mcp-runner.ts +44 -69
  155. package/src/wirings/mcp/mcp.types.ts +6 -0
  156. package/src/wirings/queue/queue-runner.test.ts +641 -0
  157. package/src/wirings/queue/queue-runner.ts +16 -38
  158. package/src/wirings/queue/queue.types.ts +4 -11
  159. package/src/wirings/rpc/index.ts +1 -1
  160. package/src/wirings/rpc/rpc-runner.ts +129 -77
  161. package/src/wirings/rpc/rpc-types.ts +8 -0
  162. package/src/wirings/scheduler/log-schedulers.ts +1 -1
  163. package/src/wirings/scheduler/scheduler-runner.test.ts +627 -0
  164. package/src/wirings/scheduler/scheduler-runner.ts +29 -55
  165. package/src/wirings/scheduler/scheduler.types.ts +3 -9
  166. package/src/wirings/trigger/index.ts +2 -0
  167. package/src/wirings/trigger/trigger-runner.ts +96 -0
  168. package/src/wirings/trigger/trigger.types.ts +56 -0
  169. package/src/wirings/workflow/dsl/index.ts +30 -0
  170. package/src/wirings/workflow/dsl/workflow-dsl.types.ts +312 -0
  171. package/src/wirings/workflow/dsl/workflow-runner.ts +27 -0
  172. package/src/wirings/workflow/graph/graph-node.ts +227 -0
  173. package/src/wirings/workflow/graph/graph-runner.ts +694 -0
  174. package/src/wirings/workflow/graph/index.ts +3 -0
  175. package/src/wirings/workflow/graph/workflow-graph.types.ts +126 -0
  176. package/src/wirings/workflow/index.ts +60 -12
  177. package/src/wirings/workflow/pikku-workflow-service.ts +401 -195
  178. package/src/wirings/workflow/wire-workflow.ts +94 -0
  179. package/src/wirings/workflow/workflow-utils.ts +120 -0
  180. package/src/wirings/workflow/workflow.types.ts +167 -188
  181. package/tsconfig.tsbuildinfo +1 -1
  182. package/dist/wirings/workflow/workflow-runner.d.ts +0 -35
  183. package/dist/wirings/workflow/workflow-runner.js +0 -68
  184. package/src/wirings/workflow/workflow-runner.ts +0 -85
@@ -1,106 +1,34 @@
1
- import { ChannelsMeta, CoreChannel } from './wirings/channel/channel.types.js';
2
- import { CoreHTTPFunctionWiring, HTTPMethod, HTTPWiringsMeta } from './wirings/http/http.types.js';
3
- import { FunctionsMeta, CorePikkuMiddleware, CorePikkuMiddlewareGroup, FunctionServicesMeta } from './types/core.types.js';
4
- import { CoreScheduledTask, ScheduledTasksMeta } from './wirings/scheduler/scheduler.types.js';
5
- import { ErrorDetails, PikkuError } from './errors/error-handler.js';
6
- import { CorePermissionGroup, CorePikkuFunctionConfig, CorePikkuPermission } from './function/functions.types.js';
7
- import { QueueWorkersMeta, CoreQueueWorker } from './wirings/queue/queue.types.js';
8
- import { CoreMCPResource, CoreMCPTool, CoreMCPPrompt, MCPResourceMeta, MCPToolMeta, MCPPromptMeta } from './wirings/mcp/mcp.types.js';
9
- import { CLIMeta, CLIProgramState } from './wirings/cli/cli.types.js';
10
- import { CoreWorkflow, WorkflowsMeta } from './wirings/workflow/workflow.types.js';
11
- interface PikkuState {
12
- function: {
13
- meta: FunctionsMeta;
14
- functions: Map<string, CorePikkuFunctionConfig<any, any>>;
15
- };
16
- rpc: {
17
- meta: Record<string, string>;
18
- files: Map<string, {
19
- exportedName: string;
20
- path: string;
21
- }>;
22
- };
23
- http: {
24
- middleware: Map<string, CorePikkuMiddleware<any, any>[]>;
25
- permissions: Map<string, CorePermissionGroup | CorePikkuPermission[]>;
26
- routes: Map<HTTPMethod, Map<string, CoreHTTPFunctionWiring<any, any, any>>>;
27
- meta: HTTPWiringsMeta;
28
- };
29
- channel: {
30
- channels: Map<string, CoreChannel<any, any>>;
31
- meta: ChannelsMeta;
32
- };
33
- scheduler: {
34
- tasks: Map<string, CoreScheduledTask>;
35
- meta: ScheduledTasksMeta;
36
- };
37
- queue: {
38
- registrations: Map<string, CoreQueueWorker>;
39
- meta: QueueWorkersMeta;
40
- };
41
- workflows: {
42
- registrations: Map<string, CoreWorkflow>;
43
- meta: WorkflowsMeta;
44
- };
45
- mcp: {
46
- resources: Map<string, CoreMCPResource>;
47
- resourcesMeta: MCPResourceMeta;
48
- tools: Map<string, CoreMCPTool>;
49
- toolsMeta: MCPToolMeta;
50
- prompts: Map<string, CoreMCPPrompt>;
51
- promptsMeta: MCPPromptMeta;
52
- };
53
- cli: {
54
- meta: CLIMeta | Record<string, any>;
55
- programs: Record<string, CLIProgramState>;
56
- };
57
- middleware: {
58
- tagGroup: Record<string, CorePikkuMiddlewareGroup>;
59
- httpGroup: Record<string, CorePikkuMiddlewareGroup>;
60
- tagGroupMeta: Record<string, {
61
- exportName: string | null;
62
- sourceFile: string;
63
- position: number;
64
- services: FunctionServicesMeta;
65
- middlewareCount: number;
66
- isFactory: boolean;
67
- }>;
68
- httpGroupMeta: Record<string, {
69
- exportName: string | null;
70
- sourceFile: string;
71
- position: number;
72
- services: FunctionServicesMeta;
73
- middlewareCount: number;
74
- isFactory: boolean;
75
- }>;
76
- };
77
- permissions: {
78
- tagGroup: Record<string, CorePermissionGroup | CorePikkuPermission[]>;
79
- httpGroup: Record<string, CorePermissionGroup | CorePikkuPermission[]>;
80
- tagGroupMeta: Record<string, {
81
- exportName: string | null;
82
- sourceFile: string;
83
- position: number;
84
- services: FunctionServicesMeta;
85
- permissionCount: number;
86
- isFactory: boolean;
87
- }>;
88
- httpGroupMeta: Record<string, {
89
- exportName: string | null;
90
- sourceFile: string;
91
- position: number;
92
- services: FunctionServicesMeta;
93
- permissionCount: number;
94
- isFactory: boolean;
95
- }>;
96
- };
97
- misc: {
98
- errors: Map<PikkuError, ErrorDetails>;
99
- schemas: Map<string, any>;
100
- middleware: Record<string, CorePikkuMiddleware[]>;
101
- permissions: Record<string, CorePermissionGroup | CorePikkuPermission[]>;
102
- };
1
+ import { PikkuPackageState } from './index.js';
2
+ declare global {
3
+ var pikkuState: Map<string, PikkuPackageState> | undefined;
103
4
  }
5
+ /**
6
+ * Get or set package-scoped pikku state
7
+ *
8
+ * @param packageName - Package name (null for main package, '@scope/package' for external packages)
9
+ * @param type - State category (function, rpc, http, etc.)
10
+ * @param content - Content key within the category
11
+ * @param value - Optional value to set
12
+ * @returns The current value of the state
13
+ *
14
+ * @example
15
+ * // Main package
16
+ * pikkuState(null, 'function', 'functions').get(funcName)
17
+ *
18
+ * // External package
19
+ * pikkuState('@acme/stripe-functions', 'rpc', 'meta')
20
+ */
21
+ export declare const pikkuState: <Type extends keyof PikkuPackageState, Content extends keyof PikkuPackageState[Type]>(packageName: string | null, type: Type, content: Content, value?: PikkuPackageState[Type][Content]) => PikkuPackageState[Type][Content];
22
+ /**
23
+ * Initialize state for a new package
24
+ */
25
+ export declare const initializePikkuState: (packageName: string) => void;
104
26
  export declare const resetPikkuState: () => void;
105
- export declare const pikkuState: <Type extends keyof PikkuState, Content extends keyof PikkuState[Type]>(type: Type, content: Content, value?: PikkuState[Type][Content]) => PikkuState[Type][Content];
106
- export {};
27
+ /**
28
+ * Register service factories for an external package.
29
+ * These factories are used to create services when the package's functions are invoked.
30
+ *
31
+ * @param packageName - The package name (e.g., '@pikku/templates-function-external')
32
+ * @param factories - The service factory functions
33
+ */
34
+ export declare const addPackageServiceFactories: (packageName: string, factories: NonNullable<PikkuPackageState["package"]["factories"]>) => void;
@@ -1,72 +1,146 @@
1
- export const resetPikkuState = () => {
2
- globalThis.pikkuState = {
3
- function: {
4
- meta: {},
5
- functions: new Map(),
6
- },
7
- rpc: {
8
- meta: {},
9
- files: new Map(),
10
- },
11
- http: {
12
- permissions: new Map(),
13
- routes: new Map(),
14
- meta: {},
15
- },
16
- channel: {
17
- channels: new Map(),
18
- meta: {},
19
- },
20
- scheduler: {
21
- tasks: new Map(),
22
- meta: [],
23
- },
24
- queue: {
25
- registrations: new Map(),
26
- meta: {},
27
- },
28
- workflows: {
29
- registrations: new Map(),
30
- meta: {},
31
- },
32
- mcp: {
33
- resources: new Map(),
34
- resourcesMeta: {},
35
- tools: new Map(),
36
- toolsMeta: {},
37
- prompts: new Map(),
38
- promptsMeta: {},
39
- },
40
- cli: {
41
- meta: { programs: {}, renderers: {} },
42
- programs: {},
43
- },
44
- middleware: {
45
- tagGroup: {},
46
- httpGroup: {},
47
- tagGroupMeta: {},
48
- httpGroupMeta: {},
49
- },
50
- permissions: {
51
- tagGroup: {},
52
- httpGroup: {},
53
- tagGroupMeta: {},
54
- httpGroupMeta: {},
55
- },
56
- misc: {
57
- errors: globalThis.pikkuState?.misc?.errors || new Map(),
58
- schemas: new Map(),
59
- middleware: {},
60
- permissions: {},
1
+ /**
2
+ * Get or set package-scoped pikku state
3
+ *
4
+ * @param packageName - Package name (null for main package, '@scope/package' for external packages)
5
+ * @param type - State category (function, rpc, http, etc.)
6
+ * @param content - Content key within the category
7
+ * @param value - Optional value to set
8
+ * @returns The current value of the state
9
+ *
10
+ * @example
11
+ * // Main package
12
+ * pikkuState(null, 'function', 'functions').get(funcName)
13
+ *
14
+ * // External package
15
+ * pikkuState('@acme/stripe-functions', 'rpc', 'meta')
16
+ */
17
+ export const pikkuState = (packageName, type, content, value) => {
18
+ const resolvedPackageName = packageName ?? '__main__';
19
+ // Initialize package state if it doesn't exist
20
+ if (!globalThis.pikkuState ||
21
+ !globalThis.pikkuState.has(resolvedPackageName)) {
22
+ initializePikkuState(resolvedPackageName);
23
+ }
24
+ const packageState = globalThis.pikkuState.get(resolvedPackageName);
25
+ if (value !== undefined) {
26
+ packageState[type][content] = value;
27
+ }
28
+ return packageState[type][content];
29
+ };
30
+ const createEmptyPackageState = () => ({
31
+ function: {
32
+ meta: {},
33
+ functions: new Map(),
34
+ },
35
+ rpc: {
36
+ meta: {},
37
+ files: new Map(),
38
+ externalPackages: new Map(),
39
+ },
40
+ http: {
41
+ middleware: new Map(),
42
+ permissions: new Map(),
43
+ routes: new Map(),
44
+ meta: {
45
+ post: {},
46
+ get: {},
47
+ delete: {},
48
+ patch: {},
49
+ head: {},
50
+ put: {},
51
+ options: {},
61
52
  },
62
- };
53
+ },
54
+ channel: {
55
+ channels: new Map(),
56
+ meta: {},
57
+ },
58
+ scheduler: {
59
+ tasks: new Map(),
60
+ meta: [],
61
+ },
62
+ queue: {
63
+ registrations: new Map(),
64
+ meta: {},
65
+ },
66
+ workflows: {
67
+ registrations: new Map(),
68
+ graphRegistrations: new Map(),
69
+ wirings: new Map(),
70
+ graphWirings: new Map(),
71
+ meta: {},
72
+ },
73
+ trigger: {
74
+ triggers: new Map(),
75
+ meta: {},
76
+ },
77
+ mcp: {
78
+ resources: new Map(),
79
+ resourcesMeta: {},
80
+ tools: new Map(),
81
+ toolsMeta: {},
82
+ prompts: new Map(),
83
+ promptsMeta: {},
84
+ },
85
+ cli: {
86
+ meta: { programs: {}, renderers: {} },
87
+ programs: {},
88
+ },
89
+ middleware: {
90
+ tagGroup: {},
91
+ httpGroup: {},
92
+ tagGroupMeta: {},
93
+ httpGroupMeta: {},
94
+ },
95
+ permissions: {
96
+ tagGroup: {},
97
+ httpGroup: {},
98
+ tagGroupMeta: {},
99
+ httpGroupMeta: {},
100
+ },
101
+ misc: {
102
+ errors: new Map(),
103
+ schemas: new Map(),
104
+ middleware: {},
105
+ permissions: {},
106
+ },
107
+ package: {
108
+ factories: null,
109
+ singletonServices: null,
110
+ },
111
+ });
112
+ /**
113
+ * Initialize state for a new package
114
+ */
115
+ export const initializePikkuState = (packageName) => {
116
+ if (!globalThis.pikkuState) {
117
+ globalThis.pikkuState = new Map();
118
+ }
119
+ if (!globalThis.pikkuState.has(packageName)) {
120
+ globalThis.pikkuState.set(packageName, createEmptyPackageState());
121
+ }
122
+ };
123
+ export const resetPikkuState = () => {
124
+ // Preserve the errors map before resetting
125
+ const existingErrors = globalThis.pikkuState?.get('__main__')?.misc.errors;
126
+ globalThis.pikkuState = new Map();
127
+ initializePikkuState('__main__');
128
+ // Restore the errors map if it existed
129
+ if (existingErrors) {
130
+ const mainState = globalThis.pikkuState.get('__main__');
131
+ mainState.misc.errors = existingErrors;
132
+ }
63
133
  };
64
134
  if (!globalThis.pikkuState) {
65
135
  resetPikkuState();
66
136
  }
67
- export const pikkuState = (type, content, value) => {
68
- if (value) {
69
- globalThis.pikkuState[type][content] = value;
70
- }
71
- return globalThis.pikkuState[type][content];
137
+ /**
138
+ * Register service factories for an external package.
139
+ * These factories are used to create services when the package's functions are invoked.
140
+ *
141
+ * @param packageName - The package name (e.g., '@pikku/templates-function-external')
142
+ * @param factories - The service factory functions
143
+ */
144
+ export const addPackageServiceFactories = (packageName, factories) => {
145
+ pikkuState(packageName, 'package', 'factories', factories);
72
146
  };
package/dist/schema.d.ts CHANGED
@@ -1,23 +1,25 @@
1
1
  import { Logger } from './services/logger.js';
2
2
  import { SchemaService } from './services/schema-service.js';
3
3
  /**
4
- * Adds a schema to the global schemas map.
4
+ * Adds a schema to the schemas map for a specific package.
5
5
  * @param name - The name of the schema.
6
6
  * @param value - The schema value.
7
+ * @param packageName - The package name (null for main package, '@scope/package' for external packages).
7
8
  * @ignore
8
9
  */
9
- export declare const addSchema: (name: string, value: any) => void;
10
+ export declare const addSchema: (name: string, value: any, packageName?: string | null) => void;
10
11
  /**
11
- * Retrieves a schema from the global schemas map.
12
+ * Retrieves a schema from the schemas map for a specific package.
12
13
  * @param name - The name of the schema.
14
+ * @param packageName - The package name (null for main package, '@scope/package' for external packages).
13
15
  * @returns The schema value or undefined if not found.
14
16
  * @ignore
15
17
  */
16
- export declare const getSchema: (name: string) => Record<string, unknown> | undefined;
18
+ export declare const getSchema: (name: string, packageName?: string | null) => Record<string, unknown> | undefined;
17
19
  /**
18
20
  * Loads a schema and compiles it into a validator.
19
21
  * @param logger - A logger for logging information.
20
22
  */
21
23
  export declare const compileAllSchemas: (logger: Logger, schemaService?: SchemaService) => void;
22
- export declare const coerceTopLevelDataFromSchema: (schemaName: string, data: any) => void;
23
- export declare const validateSchema: (logger: Logger, schemaService: SchemaService | undefined, schemaName: string | undefined | null, data: any) => Promise<void>;
24
+ export declare const coerceTopLevelDataFromSchema: (schemaName: string, data: any, packageName?: string | null) => void;
25
+ export declare const validateSchema: (logger: Logger, schemaService: SchemaService | undefined, schemaName: string | undefined | null, data: any, packageName?: string | null) => Promise<void>;
package/dist/schema.js CHANGED
@@ -1,22 +1,24 @@
1
1
  import { UnprocessableContentError } from './errors/errors.js';
2
2
  import { pikkuState } from './pikku-state.js';
3
3
  /**
4
- * Adds a schema to the global schemas map.
4
+ * Adds a schema to the schemas map for a specific package.
5
5
  * @param name - The name of the schema.
6
6
  * @param value - The schema value.
7
+ * @param packageName - The package name (null for main package, '@scope/package' for external packages).
7
8
  * @ignore
8
9
  */
9
- export const addSchema = (name, value) => {
10
- pikkuState('misc', 'schemas').set(name, value);
10
+ export const addSchema = (name, value, packageName = null) => {
11
+ pikkuState(packageName, 'misc', 'schemas').set(name, value);
11
12
  };
12
13
  /**
13
- * Retrieves a schema from the global schemas map.
14
+ * Retrieves a schema from the schemas map for a specific package.
14
15
  * @param name - The name of the schema.
16
+ * @param packageName - The package name (null for main package, '@scope/package' for external packages).
15
17
  * @returns The schema value or undefined if not found.
16
18
  * @ignore
17
19
  */
18
- export const getSchema = (name) => {
19
- return pikkuState('misc', 'schemas').get(name);
20
+ export const getSchema = (name, packageName = null) => {
21
+ return pikkuState(packageName, 'misc', 'schemas').get(name);
20
22
  };
21
23
  /**
22
24
  * Loads a schema and compiles it into a validator.
@@ -26,19 +28,20 @@ export const compileAllSchemas = (logger, schemaService) => {
26
28
  if (!schemaService) {
27
29
  throw new Error('SchemaService needs to be defined to load schemas');
28
30
  }
29
- const schemas = pikkuState('misc', 'schemas');
31
+ const schemas = pikkuState(null, 'misc', 'schemas');
30
32
  for (const [name, schema] of schemas) {
31
33
  schemaService.compileSchema(name, schema);
32
34
  }
33
35
  validateAllSchemasLoaded(logger, schemaService);
34
36
  };
35
37
  const validateAllSchemasLoaded = (logger, schemaService) => {
36
- const routesMeta = pikkuState('http', 'meta');
38
+ const routesMeta = pikkuState(null, 'http', 'meta');
37
39
  const validators = schemaService.getSchemaNames();
38
40
  const missingSchemas = [];
39
41
  for (const routePaths of Object.values(routesMeta)) {
40
42
  for (const meta of Object.values(routePaths)) {
41
- const inputs = pikkuState('function', 'meta')[meta.pikkuFuncName]?.inputs;
43
+ const inputs = pikkuState(null, 'function', 'meta')[meta.pikkuFuncName]
44
+ ?.inputs;
42
45
  const input = inputs?.[0];
43
46
  if (!input || validators.has(input)) {
44
47
  continue;
@@ -55,8 +58,8 @@ const validateAllSchemasLoaded = (logger, schemaService) => {
55
58
  logger.info('All schemas loaded');
56
59
  }
57
60
  };
58
- export const coerceTopLevelDataFromSchema = (schemaName, data) => {
59
- const schema = pikkuState('misc', 'schemas').get(schemaName);
61
+ export const coerceTopLevelDataFromSchema = (schemaName, data, packageName = null) => {
62
+ const schema = pikkuState(packageName, 'misc', 'schemas').get(schemaName);
60
63
  for (const key in schema.properties) {
61
64
  const property = schema.properties[key];
62
65
  if (typeof property === 'boolean') {
@@ -74,7 +77,7 @@ export const coerceTopLevelDataFromSchema = (schemaName, data) => {
74
77
  }
75
78
  }
76
79
  };
77
- export const validateSchema = async (logger, schemaService, schemaName, data) => {
80
+ export const validateSchema = async (logger, schemaService, schemaName, data, packageName = null) => {
78
81
  if (schemaService) {
79
82
  if (!schemaName) {
80
83
  if (data && (data.length > 0 || Object.keys(data).length > 0)) {
@@ -85,7 +88,16 @@ export const validateSchema = async (logger, schemaService, schemaName, data) =>
85
88
  return;
86
89
  }
87
90
  }
88
- const schema = pikkuState('misc', 'schemas').get(schemaName);
91
+ logger.debug(`[validateSchema] Validating schema '${schemaName}' for package '${packageName ?? 'main'}' with data:`, JSON.stringify(data));
92
+ const schemas = pikkuState(packageName, 'misc', 'schemas');
93
+ const schema = schemas.get(schemaName);
94
+ if (schema === undefined) {
95
+ const availableSchemas = Array.from(schemas.keys());
96
+ logger.error(`Schema '${schemaName}' not found for package '${packageName ?? 'main'}'. Available schemas: ${availableSchemas.join(', ') || '(none)'}`);
97
+ }
98
+ else {
99
+ logger.debug(`[validateSchema] Found schema '${schemaName}':`, JSON.stringify(schema));
100
+ }
89
101
  await schemaService.compileSchema(schemaName, schema);
90
102
  await schemaService.validateSchema(schemaName, data);
91
103
  }
@@ -1,19 +1,23 @@
1
1
  import { ChannelStore } from '../wirings/channel/channel-store.js';
2
2
  import { CoreUserSession } from '../types/core.types.js';
3
- export interface UserSessionService<UserSession extends CoreUserSession> {
3
+ export interface SessionService<UserSession extends CoreUserSession> {
4
4
  sessionChanged: boolean;
5
+ initial: UserSession | undefined;
5
6
  setInitial(session: UserSession): void;
7
+ freezeInitial(): UserSession | undefined;
6
8
  set(session: UserSession): Promise<void> | void;
7
9
  clear(): Promise<void> | void;
8
- get(): Promise<UserSession | undefined> | UserSession | undefined;
10
+ get(): Promise<UserSession> | UserSession | undefined;
9
11
  }
10
- export declare class PikkuUserSessionService<UserSession extends CoreUserSession> implements UserSessionService<UserSession> {
12
+ export declare class PikkuSessionService<UserSession extends CoreUserSession> implements SessionService<UserSession> {
11
13
  private channelStore?;
12
14
  private channelId?;
13
15
  sessionChanged: boolean;
16
+ initial: UserSession | undefined;
14
17
  private session;
15
18
  constructor(channelStore?: ChannelStore<unknown, unknown, UserSession> | undefined, channelId?: string | undefined);
16
19
  setInitial(session: UserSession): void;
20
+ freezeInitial(): UserSession | undefined;
17
21
  set(session: UserSession): void | Promise<void> | undefined;
18
22
  clear(): void | Promise<void> | undefined;
19
23
  get(): Promise<UserSession> | UserSession | undefined;
@@ -1,7 +1,8 @@
1
- export class PikkuUserSessionService {
1
+ export class PikkuSessionService {
2
2
  channelStore;
3
3
  channelId;
4
4
  sessionChanged = false;
5
+ initial;
5
6
  session;
6
7
  constructor(channelStore, channelId) {
7
8
  this.channelStore = channelStore;
@@ -13,6 +14,12 @@ export class PikkuUserSessionService {
13
14
  setInitial(session) {
14
15
  this.session = session;
15
16
  }
17
+ freezeInitial() {
18
+ if (this.initial === undefined) {
19
+ this.initial = this.session;
20
+ }
21
+ return this.initial;
22
+ }
16
23
  set(session) {
17
24
  this.sessionChanged = true;
18
25
  this.session = session;
@@ -0,0 +1,38 @@
1
+ import { SerializedError, CoreSingletonServices, CreateWireServices, CoreConfig } from '../types/core.types.js';
2
+ import { WorkflowRun, StepState, WorkflowStatus } from '../wirings/workflow/workflow.types.js';
3
+ /**
4
+ * Interface for workflow orchestration
5
+ * Handles workflow execution, replay, orchestration logic, and run-level state
6
+ */
7
+ export interface WorkflowService {
8
+ createRun(workflowName: string, input: any, inline?: boolean): Promise<string>;
9
+ getRun(id: string): Promise<WorkflowRun | null>;
10
+ getRunHistory(runId: string): Promise<Array<StepState & {
11
+ stepName: string;
12
+ }>>;
13
+ updateRunStatus(id: string, status: WorkflowStatus, output?: any, error?: SerializedError): Promise<void>;
14
+ withRunLock<T>(id: string, fn: () => Promise<T>): Promise<T>;
15
+ close(): Promise<void>;
16
+ resumeWorkflow(runId: string): Promise<void>;
17
+ setServices(singletonServices: CoreSingletonServices, createWireServices: CreateWireServices, config: CoreConfig): void;
18
+ startWorkflow<I>(name: string, input: I, rpcService: any, options?: {
19
+ inline?: boolean;
20
+ startNode?: string;
21
+ }): Promise<{
22
+ runId: string;
23
+ }>;
24
+ runWorkflowJob(runId: string, rpcService: any): Promise<void>;
25
+ orchestrateWorkflow(runId: string, rpcService: any): Promise<void>;
26
+ executeWorkflowSleepCompleted(runId: string, stepId: string): Promise<void>;
27
+ insertStepState(runId: string, stepName: string, rpcName: string, data: any, stepOptions?: {
28
+ retries?: number;
29
+ retryDelay?: string | number;
30
+ }): Promise<StepState>;
31
+ getStepState(runId: string, stepName: string): Promise<StepState>;
32
+ setStepRunning(stepId: string): Promise<void>;
33
+ setStepScheduled(stepId: string): Promise<void>;
34
+ setStepResult(stepId: string, result: any): Promise<void>;
35
+ setStepError(stepId: string, error: Error): Promise<void>;
36
+ createRetryAttempt(stepId: string, status: 'pending' | 'running'): Promise<StepState>;
37
+ executeWorkflowStep(runId: string, stepName: string, rpcName: string | null, data: any, rpcService: any): Promise<void>;
38
+ }
@@ -0,0 +1 @@
1
+ export {};