@pikku/core 0.12.0 → 0.12.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 (262) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/errors/errors.d.ts +6 -0
  3. package/dist/errors/errors.js +10 -0
  4. package/dist/errors/index.d.ts +1 -0
  5. package/dist/errors/index.js +1 -0
  6. package/dist/function/function-runner.d.ts +3 -3
  7. package/dist/function/function-runner.js +66 -44
  8. package/dist/function/functions.types.d.ts +1 -0
  9. package/dist/handle-error.d.ts +2 -2
  10. package/dist/handle-error.js +2 -2
  11. package/dist/index.d.ts +5 -3
  12. package/dist/index.js +2 -1
  13. package/dist/internal.d.ts +3 -0
  14. package/dist/internal.js +2 -0
  15. package/dist/middleware/auth-bearer.d.ts +1 -1
  16. package/dist/middleware/auth-bearer.js +1 -1
  17. package/dist/middleware/auth-cookie.d.ts +2 -2
  18. package/dist/middleware/auth-cookie.js +1 -1
  19. package/dist/middleware/cors.d.ts +2 -2
  20. package/dist/middleware/cors.js +45 -37
  21. package/dist/middleware-runner.d.ts +1 -1
  22. package/dist/permissions.d.ts +2 -2
  23. package/dist/pikku-state.d.ts +9 -8
  24. package/dist/pikku-state.js +30 -17
  25. package/dist/schema.d.ts +4 -4
  26. package/dist/schema.js +9 -5
  27. package/dist/services/ai-agent-runner-service.d.ts +22 -3
  28. package/dist/services/ai-run-state-service.d.ts +1 -1
  29. package/dist/services/ai-storage-service.d.ts +1 -1
  30. package/dist/services/content-service.d.ts +6 -0
  31. package/dist/services/gopass-secrets.d.ts +1 -1
  32. package/dist/services/gopass-secrets.js +3 -0
  33. package/dist/services/in-memory-ai-run-state-service.d.ts +15 -0
  34. package/dist/services/in-memory-ai-run-state-service.js +44 -0
  35. package/dist/services/in-memory-trigger-service.d.ts +0 -1
  36. package/dist/services/in-memory-trigger-service.js +6 -8
  37. package/dist/services/index.d.ts +3 -2
  38. package/dist/services/index.js +1 -0
  39. package/dist/services/local-content.d.ts +2 -1
  40. package/dist/services/local-content.js +6 -1
  41. package/dist/services/local-secrets.d.ts +2 -2
  42. package/dist/services/local-variables.d.ts +1 -1
  43. package/dist/services/logger-console.d.ts +2 -1
  44. package/dist/services/scheduler-service.d.ts +0 -12
  45. package/dist/services/scheduler-service.js +0 -6
  46. package/dist/services/scoped-secret-service.d.ts +1 -1
  47. package/dist/services/trigger-service.d.ts +0 -7
  48. package/dist/services/user-session-service.d.ts +3 -3
  49. package/dist/services/workflow-service.d.ts +2 -3
  50. package/dist/types/core.types.d.ts +24 -18
  51. package/dist/types/state.types.d.ts +17 -15
  52. package/dist/utils.d.ts +5 -5
  53. package/dist/utils.js +12 -9
  54. package/dist/wirings/ai-agent/ai-agent-assistant-ui.d.ts +5 -0
  55. package/dist/wirings/ai-agent/ai-agent-assistant-ui.js +146 -0
  56. package/dist/wirings/ai-agent/ai-agent-helpers.d.ts +28 -0
  57. package/dist/wirings/ai-agent/ai-agent-helpers.js +40 -0
  58. package/dist/wirings/ai-agent/ai-agent-prepare.d.ts +4 -5
  59. package/dist/wirings/ai-agent/ai-agent-prepare.js +66 -30
  60. package/dist/wirings/ai-agent/ai-agent-registry.d.ts +1 -1
  61. package/dist/wirings/ai-agent/ai-agent-runner.js +107 -8
  62. package/dist/wirings/ai-agent/ai-agent-stream.d.ts +2 -1
  63. package/dist/wirings/ai-agent/ai-agent-stream.js +246 -87
  64. package/dist/wirings/ai-agent/ai-agent.types.d.ts +61 -2
  65. package/dist/wirings/ai-agent/index.d.ts +3 -1
  66. package/dist/wirings/ai-agent/index.js +2 -0
  67. package/dist/wirings/channel/channel-common.d.ts +2 -2
  68. package/dist/wirings/channel/channel-handler.d.ts +3 -3
  69. package/dist/wirings/channel/channel-handler.js +4 -4
  70. package/dist/wirings/channel/channel-runner.d.ts +5 -5
  71. package/dist/wirings/channel/channel-runner.js +3 -2
  72. package/dist/wirings/channel/channel-store.d.ts +1 -1
  73. package/dist/wirings/channel/channel.types.d.ts +4 -6
  74. package/dist/wirings/channel/local/local-channel-runner.d.ts +7 -2
  75. package/dist/wirings/channel/local/local-channel-runner.js +4 -3
  76. package/dist/wirings/channel/local/local-eventhub-service.d.ts +2 -2
  77. package/dist/wirings/channel/log-channels.d.ts +1 -1
  78. package/dist/wirings/channel/pikku-abstract-channel-handler.d.ts +1 -1
  79. package/dist/wirings/channel/serverless/serverless-channel-runner.d.ts +4 -4
  80. package/dist/wirings/channel/serverless/serverless-channel-runner.js +25 -19
  81. package/dist/wirings/cli/channel/cli-channel-runner.d.ts +1 -1
  82. package/dist/wirings/cli/cli-runner.d.ts +2 -2
  83. package/dist/wirings/cli/cli.types.d.ts +3 -3
  84. package/dist/wirings/cli/command-parser.d.ts +1 -1
  85. package/dist/wirings/http/http-runner.d.ts +9 -9
  86. package/dist/wirings/http/http-runner.js +33 -14
  87. package/dist/wirings/http/http.types.d.ts +2 -6
  88. package/dist/wirings/http/log-http-routes.d.ts +1 -1
  89. package/dist/wirings/http/pikku-fetch-http-request.d.ts +1 -1
  90. package/dist/wirings/http/pikku-fetch-http-response.d.ts +2 -2
  91. package/dist/wirings/http/pikku-fetch-http-response.js +6 -1
  92. package/dist/wirings/http/routers/http-router.d.ts +1 -1
  93. package/dist/wirings/http/routers/path-to-regex.d.ts +2 -2
  94. package/dist/wirings/mcp/mcp-endpoint-registry.d.ts +1 -1
  95. package/dist/wirings/mcp/mcp-runner.d.ts +0 -3
  96. package/dist/wirings/mcp/mcp-runner.js +4 -2
  97. package/dist/wirings/mcp/mcp.types.d.ts +2 -2
  98. package/dist/wirings/oauth2/oauth2-client.d.ts +3 -3
  99. package/dist/wirings/oauth2/wire-oauth2-credential.d.ts +1 -1
  100. package/dist/wirings/queue/index.d.ts +1 -1
  101. package/dist/wirings/queue/index.js +1 -1
  102. package/dist/wirings/queue/queue-runner.d.ts +1 -12
  103. package/dist/wirings/queue/queue-runner.js +4 -12
  104. package/dist/wirings/queue/queue.types.d.ts +3 -9
  105. package/dist/wirings/queue/register-queue-helper.d.ts +2 -2
  106. package/dist/wirings/queue/register-queue-helper.js +1 -1
  107. package/dist/wirings/queue/validate-worker-config.d.ts +1 -1
  108. package/dist/wirings/rpc/index.d.ts +2 -0
  109. package/dist/wirings/rpc/index.js +1 -0
  110. package/dist/wirings/rpc/rpc-runner.d.ts +36 -14
  111. package/dist/wirings/rpc/rpc-runner.js +56 -28
  112. package/dist/wirings/rpc/rpc-types.d.ts +14 -2
  113. package/dist/wirings/rpc/wire-addon.d.ts +10 -0
  114. package/dist/wirings/rpc/wire-addon.js +9 -0
  115. package/dist/wirings/scheduler/index.d.ts +1 -1
  116. package/dist/wirings/scheduler/index.js +1 -1
  117. package/dist/wirings/scheduler/log-schedulers.d.ts +1 -1
  118. package/dist/wirings/scheduler/scheduler-runner.d.ts +3 -10
  119. package/dist/wirings/scheduler/scheduler-runner.js +4 -25
  120. package/dist/wirings/scheduler/scheduler.types.d.ts +2 -2
  121. package/dist/wirings/trigger/pikku-trigger-service.d.ts +2 -6
  122. package/dist/wirings/trigger/pikku-trigger-service.js +11 -16
  123. package/dist/wirings/trigger/trigger-runner.d.ts +1 -5
  124. package/dist/wirings/trigger/trigger-runner.js +4 -3
  125. package/dist/wirings/workflow/pikku-workflow-service.d.ts +2 -6
  126. package/dist/wirings/workflow/pikku-workflow-service.js +20 -29
  127. package/dist/wirings/workflow/workflow.types.d.ts +2 -2
  128. package/package.json +2 -1
  129. package/src/crypto-utils.test.ts +116 -0
  130. package/src/errors/error.test.ts +143 -2
  131. package/src/errors/errors.ts +10 -0
  132. package/src/errors/index.ts +1 -0
  133. package/src/function/function-runner.test.ts +2 -2
  134. package/src/function/function-runner.ts +72 -49
  135. package/src/function/functions.types.ts +1 -0
  136. package/src/handle-error.test.ts +361 -0
  137. package/src/handle-error.ts +4 -4
  138. package/src/index.ts +3 -10
  139. package/src/internal.ts +6 -0
  140. package/src/middleware/auth-apikey.test.ts +1 -1
  141. package/src/middleware/auth-bearer.test.ts +1 -1
  142. package/src/middleware/auth-bearer.ts +2 -5
  143. package/src/middleware/auth-cookie.test.ts +1 -1
  144. package/src/middleware/auth-cookie.ts +3 -5
  145. package/src/middleware/cors.test.ts +424 -0
  146. package/src/middleware/cors.ts +14 -6
  147. package/src/middleware/remote-auth.test.ts +488 -0
  148. package/src/middleware-runner.test.ts +1 -1
  149. package/src/middleware-runner.ts +1 -1
  150. package/src/permissions.test.ts +2 -2
  151. package/src/permissions.ts +2 -2
  152. package/src/pikku-state.test.ts +224 -0
  153. package/src/pikku-state.ts +40 -28
  154. package/src/schema.test.ts +198 -6
  155. package/src/schema.ts +11 -7
  156. package/src/services/ai-agent-runner-service.ts +15 -3
  157. package/src/services/ai-run-state-service.ts +1 -1
  158. package/src/services/ai-storage-service.ts +1 -1
  159. package/src/services/content-service.ts +7 -0
  160. package/src/services/gopass-secrets.ts +4 -1
  161. package/src/services/in-memory-ai-run-state-service.ts +73 -0
  162. package/src/services/in-memory-trigger-service.ts +6 -10
  163. package/src/services/in-memory-workflow-service.test.ts +351 -0
  164. package/src/services/index.ts +2 -1
  165. package/src/services/local-content.ts +9 -3
  166. package/src/services/local-secrets.test.ts +80 -0
  167. package/src/services/local-secrets.ts +2 -2
  168. package/src/services/local-variables.test.ts +61 -0
  169. package/src/services/local-variables.ts +1 -1
  170. package/src/services/logger-console.test.ts +118 -0
  171. package/src/services/logger-console.ts +2 -1
  172. package/src/services/scheduler-service.ts +0 -18
  173. package/src/services/scoped-secret-service.test.ts +74 -0
  174. package/src/services/scoped-secret-service.ts +1 -1
  175. package/src/services/trigger-service.ts +0 -21
  176. package/src/services/typed-secret-service.test.ts +93 -0
  177. package/src/services/typed-variables-service.test.ts +73 -0
  178. package/src/services/user-session-service.test.ts +113 -0
  179. package/src/services/user-session-service.ts +3 -3
  180. package/src/services/workflow-service.ts +2 -12
  181. package/src/time-utils.test.ts +1 -1
  182. package/src/types/core.types.ts +25 -19
  183. package/src/types/state.types.ts +24 -15
  184. package/src/utils.test.ts +350 -0
  185. package/src/utils.ts +14 -13
  186. package/src/wirings/ai-agent/ai-agent-assistant-ui.test.ts +363 -0
  187. package/src/wirings/ai-agent/ai-agent-assistant-ui.ts +214 -0
  188. package/src/wirings/ai-agent/ai-agent-helpers.test.ts +152 -0
  189. package/src/wirings/ai-agent/ai-agent-helpers.ts +76 -0
  190. package/src/wirings/ai-agent/ai-agent-model-config.test.ts +115 -0
  191. package/src/wirings/ai-agent/ai-agent-prepare.ts +79 -45
  192. package/src/wirings/ai-agent/ai-agent-registry.ts +1 -1
  193. package/src/wirings/ai-agent/ai-agent-runner.test.ts +245 -3
  194. package/src/wirings/ai-agent/ai-agent-runner.ts +121 -7
  195. package/src/wirings/ai-agent/ai-agent-stream.test.ts +430 -24
  196. package/src/wirings/ai-agent/ai-agent-stream.ts +377 -102
  197. package/src/wirings/ai-agent/ai-agent.types.ts +57 -2
  198. package/src/wirings/ai-agent/index.ts +11 -0
  199. package/src/wirings/channel/channel-common.ts +2 -2
  200. package/src/wirings/channel/channel-handler.ts +13 -9
  201. package/src/wirings/channel/channel-runner.ts +6 -6
  202. package/src/wirings/channel/channel-store.ts +1 -1
  203. package/src/wirings/channel/channel.types.ts +4 -8
  204. package/src/wirings/channel/local/local-channel-runner.test.ts +19 -12
  205. package/src/wirings/channel/local/local-channel-runner.ts +18 -13
  206. package/src/wirings/channel/local/local-eventhub-service.test.ts +2 -2
  207. package/src/wirings/channel/local/local-eventhub-service.ts +2 -2
  208. package/src/wirings/channel/log-channels.ts +1 -1
  209. package/src/wirings/channel/pikku-abstract-channel-handler.ts +1 -1
  210. package/src/wirings/channel/serverless/serverless-channel-runner.ts +37 -32
  211. package/src/wirings/cli/channel/cli-channel-runner.ts +1 -1
  212. package/src/wirings/cli/cli-runner.test.ts +1 -1
  213. package/src/wirings/cli/cli-runner.ts +11 -4
  214. package/src/wirings/cli/cli.types.ts +3 -3
  215. package/src/wirings/cli/command-parser.test.ts +1 -1
  216. package/src/wirings/cli/command-parser.ts +1 -1
  217. package/src/wirings/http/http-routes.test.ts +1 -1
  218. package/src/wirings/http/http-runner.test.ts +9 -17
  219. package/src/wirings/http/http-runner.ts +43 -27
  220. package/src/wirings/http/http.types.ts +1 -14
  221. package/src/wirings/http/log-http-routes.ts +1 -1
  222. package/src/wirings/http/pikku-fetch-http-request.ts +1 -1
  223. package/src/wirings/http/pikku-fetch-http-response.ts +12 -5
  224. package/src/wirings/http/routers/http-router.ts +1 -1
  225. package/src/wirings/http/routers/path-to-regex.test.ts +1 -1
  226. package/src/wirings/http/routers/path-to-regex.ts +4 -3
  227. package/src/wirings/mcp/mcp-endpoint-registry.ts +5 -1
  228. package/src/wirings/mcp/mcp-runner.ts +9 -15
  229. package/src/wirings/mcp/mcp.types.ts +2 -2
  230. package/src/wirings/oauth2/oauth2-client.ts +3 -3
  231. package/src/wirings/oauth2/wire-oauth2-credential.ts +1 -1
  232. package/src/wirings/queue/index.ts +0 -1
  233. package/src/wirings/queue/queue-runner.test.ts +52 -25
  234. package/src/wirings/queue/queue-runner.ts +8 -30
  235. package/src/wirings/queue/queue.types.ts +3 -13
  236. package/src/wirings/queue/register-queue-helper.ts +3 -5
  237. package/src/wirings/queue/validate-worker-config.test.ts +108 -0
  238. package/src/wirings/queue/validate-worker-config.ts +4 -1
  239. package/src/wirings/rpc/index.ts +2 -0
  240. package/src/wirings/rpc/rpc-runner.ts +92 -49
  241. package/src/wirings/rpc/rpc-types.ts +17 -2
  242. package/src/wirings/rpc/wire-addon.ts +20 -0
  243. package/src/wirings/scheduler/index.ts +0 -1
  244. package/src/wirings/scheduler/log-schedulers.ts +1 -1
  245. package/src/wirings/scheduler/scheduler-runner.test.ts +49 -60
  246. package/src/wirings/scheduler/scheduler-runner.ts +9 -56
  247. package/src/wirings/scheduler/scheduler.types.ts +2 -2
  248. package/src/wirings/secret/validate-secret-definitions.test.ts +140 -0
  249. package/src/wirings/trigger/pikku-trigger-service.ts +17 -39
  250. package/src/wirings/trigger/trigger-runner.test.ts +79 -0
  251. package/src/wirings/trigger/trigger-runner.ts +8 -11
  252. package/src/wirings/variable/validate-variable-definitions.test.ts +91 -0
  253. package/src/wirings/workflow/graph/graph-runner.test.ts +14 -22
  254. package/src/wirings/workflow/graph/template.test.ts +49 -0
  255. package/src/wirings/workflow/pikku-workflow-service.test.ts +15 -27
  256. package/src/wirings/workflow/pikku-workflow-service.ts +29 -45
  257. package/src/wirings/workflow/workflow-helpers.test.ts +129 -0
  258. package/src/wirings/workflow/workflow.types.ts +2 -2
  259. package/tsconfig.tsbuildinfo +1 -1
  260. package/dist/wirings/workflow/workflow-utils.d.ts +0 -1
  261. package/dist/wirings/workflow/workflow-utils.js +0 -1
  262. package/src/wirings/workflow/workflow-utils.ts +0 -0
@@ -1,8 +1,11 @@
1
1
  import { NotFoundError, PikkuMissingMetaError } from '../../errors/errors.js'
2
2
  import { addFunction, runPikkuFunc } from '../../function/function-runner.js'
3
3
  import { pikkuState } from '../../pikku-state.js'
4
- import { CorePikkuMiddleware, CoreUserSession } from '../../types/core.types.js'
5
- import {
4
+ import type {
5
+ CorePikkuMiddleware,
6
+ CoreUserSession,
7
+ } from '../../types/core.types.js'
8
+ import type {
6
9
  CoreCLI,
7
10
  CLICommandMeta,
8
11
  CLIOption,
@@ -19,7 +22,7 @@ import type {
19
22
  CreateConfig,
20
23
  CreateSingletonServices,
21
24
  } from '../../types/core.types.js'
22
- import { PikkuChannel } from '../channel/channel.types.js'
25
+ import type { PikkuChannel } from '../channel/channel.types.js'
23
26
  import {
24
27
  PikkuSessionService,
25
28
  createMiddlewareSessionWireProps,
@@ -357,7 +360,11 @@ export async function runCLICommand({
357
360
  // Skip if result is undefined (void functions handle their own output)
358
361
  if (renderer && result !== undefined) {
359
362
  await Promise.resolve(
360
- renderer(singletonServices, result, userSession.get())
363
+ renderer(
364
+ singletonServices,
365
+ result,
366
+ userSession.get() as CoreUserSession | undefined
367
+ )
361
368
  )
362
369
  }
363
370
 
@@ -1,4 +1,4 @@
1
- import {
1
+ import type {
2
2
  CorePikkuMiddleware,
3
3
  CoreSingletonServices,
4
4
  CoreUserSession,
@@ -6,13 +6,13 @@ import {
6
6
  MiddlewareMetadata,
7
7
  PermissionMetadata,
8
8
  } from '../../types/core.types.js'
9
- import {
9
+ import type {
10
10
  CorePikkuFunctionConfig,
11
11
  CorePikkuPermission,
12
12
  CorePikkuFunction,
13
13
  CorePikkuFunctionSessionless,
14
14
  } from '../../function/functions.types.js'
15
- import { PikkuChannel } from '../channel/channel.types.js'
15
+ import type { PikkuChannel } from '../channel/channel.types.js'
16
16
 
17
17
  /**
18
18
  * CLI option definition
@@ -1,7 +1,7 @@
1
1
  import { test, describe } from 'node:test'
2
2
  import * as assert from 'assert'
3
3
  import { parseCLIArguments, generateCommandHelp } from './command-parser.js'
4
- import { CLIMeta } from './cli.types.js'
4
+ import type { CLIMeta } from './cli.types.js'
5
5
 
6
6
  const testMeta: CLIMeta = {
7
7
  programs: {
@@ -1,4 +1,4 @@
1
- import {
1
+ import type {
2
2
  CLIMeta,
3
3
  CLIProgramMeta,
4
4
  CLICommandMeta,
@@ -4,7 +4,7 @@ import { wireHTTPRoutes, defineHTTPRoutes } from './http-routes.js'
4
4
  import { pikkuState, resetPikkuState } from '../../pikku-state.js'
5
5
  import { addFunction } from '../../function/function-runner.js'
6
6
  import { httpRouter } from './routers/http-router.js'
7
- import { CorePikkuMiddleware } from '../../types/core.types.js'
7
+ import type { CorePikkuMiddleware } from '../../types/core.types.js'
8
8
 
9
9
  // Mock function for testing
10
10
  const mockFunc = async () => ({ success: true })
@@ -1,7 +1,7 @@
1
1
  import { test, describe, beforeEach, afterEach } from 'node:test'
2
2
  import * as assert from 'assert'
3
3
  import { NotFoundError } from '../../errors/errors.js'
4
- import { JSONValue, CorePikkuMiddleware } from '../../types/core.types.js'
4
+ import type { JSONValue, CorePikkuMiddleware } from '../../types/core.types.js'
5
5
  import { fetch, wireHTTP } from './http-runner.js'
6
6
  import { pikkuState, resetPikkuState } from '../../pikku-state.js'
7
7
  import {
@@ -42,8 +42,6 @@ const setHTTPFunctionMap = (func: any) => {
42
42
  }
43
43
 
44
44
  describe('fetch', () => {
45
- let singletonServices: any
46
- let createWireServices: any
47
45
  let request: any
48
46
  let response: any
49
47
 
@@ -51,7 +49,7 @@ describe('fetch', () => {
51
49
  resetPikkuState()
52
50
  httpRouter.reset()
53
51
 
54
- singletonServices = {
52
+ const singletonServices = {
55
53
  logger: {
56
54
  info: () => {},
57
55
  warn: () => {},
@@ -59,7 +57,11 @@ describe('fetch', () => {
59
57
  },
60
58
  }
61
59
 
62
- createWireServices = async () => ({})
60
+ const createWireServices = async () => ({})
61
+
62
+ pikkuState(null, 'package', 'singletonServices', singletonServices as any)
63
+ pikkuState(null, 'package', 'factories', { createWireServices } as any)
64
+
63
65
  request = new PikkuMockRequest('/test', 'get')
64
66
  response = new PikkuMockResponse()
65
67
 
@@ -76,8 +78,6 @@ describe('fetch', () => {
76
78
  await assert.rejects(
77
79
  async () =>
78
80
  fetch(request, {
79
- singletonServices,
80
- createWireServices,
81
81
  bubbleErrors: true,
82
82
  }),
83
83
  NotFoundError
@@ -97,10 +97,7 @@ describe('fetch', () => {
97
97
  // Initialize router after adding route (for tests)
98
98
  httpRouter.initialize()
99
99
 
100
- const result = await fetch(request, {
101
- singletonServices,
102
- createWireServices,
103
- })
100
+ const result = await fetch(request)
104
101
 
105
102
  assert.deepStrictEqual(await result.json(), { success: true })
106
103
  })
@@ -120,10 +117,7 @@ describe('fetch', () => {
120
117
  },
121
118
  })
122
119
 
123
- await fetch(request, {
124
- singletonServices,
125
- createWireServices,
126
- })
120
+ await fetch(request)
127
121
 
128
122
  assert.strictEqual(await permissions.test(), true)
129
123
  })
@@ -142,8 +136,6 @@ describe('fetch', () => {
142
136
  await assert.rejects(
143
137
  async () =>
144
138
  fetch(request, {
145
- singletonServices,
146
- createWireServices,
147
139
  bubbleErrors: true,
148
140
  }),
149
141
  error
@@ -1,20 +1,19 @@
1
- import {
1
+ import type {
2
2
  CoreHTTPFunctionWiring,
3
3
  RunHTTPWiringOptions,
4
- RunHTTPWiringParams,
5
4
  PikkuHTTP,
6
5
  PikkuHTTPRequest,
7
6
  PikkuHTTPResponse,
8
7
  HTTPWiringMeta,
9
8
  HTTPMethod,
10
9
  } from './http.types.js'
11
- import {
10
+ import type {
12
11
  CorePikkuFunction,
13
12
  CorePikkuFunctionSessionless,
14
13
  CorePikkuPermission,
15
14
  CorePermissionGroup,
16
15
  } from '../../function/functions.types.js'
17
- import {
16
+ import type {
18
17
  CoreUserSession,
19
18
  CorePikkuMiddleware,
20
19
  CorePikkuMiddlewareGroup,
@@ -29,17 +28,19 @@ import {
29
28
  isSerializable,
30
29
  } from '../../utils.js'
31
30
  import {
32
- PikkuSessionService,
33
- createMiddlewareSessionWireProps,
34
- } from '../../services/user-session-service.js'
31
+ getSingletonServices,
32
+ getCreateWireServices,
33
+ } from '../../pikku-state.js'
34
+ import { PikkuSessionService } from '../../services/user-session-service.js'
35
35
  import { handleHTTPError } from '../../handle-error.js'
36
36
  import { pikkuState } from '../../pikku-state.js'
37
37
  import { PikkuFetchHTTPResponse } from './pikku-fetch-http-response.js'
38
38
  import { PikkuFetchHTTPRequest } from './pikku-fetch-http-request.js'
39
- import { PikkuChannel } from '../channel/channel.types.js'
39
+ import type { PikkuChannel } from '../channel/channel.types.js'
40
40
  import { addFunction, runPikkuFunc } from '../../function/function-runner.js'
41
41
  import { httpRouter } from './routers/http-router.js'
42
42
  import { validateSchema } from '../../schema.js'
43
+ import { runMiddleware } from '../../middleware-runner.js'
43
44
 
44
45
  /**
45
46
  * Extract headers from a PikkuHTTPRequest based on the schema keys
@@ -369,7 +370,10 @@ const executeRoute = async (
369
370
  const wire: PikkuWire = {
370
371
  http,
371
372
  channel,
372
- ...createMiddlewareSessionWireProps(userSession),
373
+ session: userSession.get() as CoreUserSession | undefined,
374
+ setSession: (s: any) => userSession.setInitial(s),
375
+ getSession: () => userSession.get(),
376
+ hasSessionChanged: () => userSession.sessionChanged,
373
377
  }
374
378
 
375
379
  result = await runPikkuFunc(
@@ -417,12 +421,12 @@ const executeRoute = async (
417
421
  * @template In Expected input data type.
418
422
  * @template Out Expected output data type.
419
423
  * @param {Request} request - The native Fetch API Request object.
420
- * @param {RunHTTPWiringOptions & RunHTTPWiringParams} params - Additional options including services and session management.
424
+ * @param {RunHTTPWiringOptions} params - Additional options including services and session management.
421
425
  * @returns {Promise<Response>} A promise that resolves to a Fetch API Response object.
422
426
  */
423
427
  export const fetch = async <In, Out>(
424
428
  request: Request,
425
- params: RunHTTPWiringOptions & RunHTTPWiringParams
429
+ params: RunHTTPWiringOptions = {}
426
430
  ): Promise<Response> => {
427
431
  const pikkuResponse = new PikkuFetchHTTPResponse()
428
432
  await fetchData<In, Out>(request, pikkuResponse, params)
@@ -438,12 +442,12 @@ export const fetch = async <In, Out>(
438
442
  * @template In Expected input data type.
439
443
  * @template Out Expected output data type.
440
444
  * @param {Request | PikkuHTTPRequest} request - The request object.
441
- * @param {RunHTTPWiringOptions & RunHTTPWiringParams} params - Execution options including services and session configuration.
445
+ * @param {RunHTTPWiringOptions} params - Execution options including services and session configuration.
442
446
  * @returns {Promise<PikkuFetchHTTPResponse>} A promise that resolves to a PikkuFetchHTTPResponse object.
443
447
  */
444
448
  export const pikkuFetch = async <In, Out>(
445
449
  request: Request | PikkuHTTPRequest,
446
- params: RunHTTPWiringOptions & RunHTTPWiringParams
450
+ params: RunHTTPWiringOptions = {}
447
451
  ): Promise<PikkuFetchHTTPResponse> => {
448
452
  const pikkuResponse = new PikkuFetchHTTPResponse()
449
453
  await fetchData<In, Out>(request, pikkuResponse, params)
@@ -465,43 +469,56 @@ export const pikkuFetch = async <In, Out>(
465
469
  * @template Out Expected output data type.
466
470
  * @param {Request | PikkuHTTPRequest} request - The incoming HTTP request.
467
471
  * @param {PikkuHTTPResponse} response - The response object to be populated.
468
- * @param {RunHTTPWiringOptions & RunHTTPWiringParams} options - Options such as singleton services, session handling, and error configuration.
472
+ * @param {RunHTTPWiringOptions} options - Options such as singleton services, session handling, and error configuration.
469
473
  * @returns {Promise<Out | void>} The output from the route handler or void if an error occurred.
470
474
  */
471
475
  export const fetchData = async <In, Out>(
472
476
  request: Request | PikkuHTTPRequest,
473
477
  response: PikkuHTTPResponse,
474
478
  {
475
- singletonServices,
476
- createWireServices,
477
479
  skipUserSession = false,
478
480
  respondWith404 = true,
479
481
  logWarningsForStatusCodes = [],
480
482
  coerceDataFromSchema = true,
481
483
  bubbleErrors = false,
482
484
  generateRequestId,
483
- }: RunHTTPWiringOptions & RunHTTPWiringParams
485
+ }: RunHTTPWiringOptions = {}
484
486
  ): Promise<Out | void> => {
485
- const requestId =
486
- (request as any).getHeader?.('x-request-id') ||
487
- generateRequestId?.() ||
488
- createWeakUID()
487
+ const singletonServices = getSingletonServices()
488
+ const createWireServices = getCreateWireServices()
489
489
  let wireServices: WireServices<typeof singletonServices> | undefined
490
490
  let result: Out
491
491
 
492
492
  // Combine the request and response into one wire object
493
- const http = createHTTPWire(
494
- request instanceof Request ? new PikkuFetchHTTPRequest(request) : request,
495
- response
496
- )
493
+ const pikkuRequest =
494
+ request instanceof Request ? new PikkuFetchHTTPRequest(request) : request
495
+ let requestId: string | null = null
496
+ try {
497
+ requestId = pikkuRequest.header('x-request-id')
498
+ } catch {}
499
+ requestId = requestId || generateRequestId?.() || createWeakUID()
500
+ const http = createHTTPWire(pikkuRequest, response)
497
501
  const apiType = http!.request!.method()
498
502
  const apiRoute = http!.request!.path()
499
503
 
500
504
  // Locate the matching route based on the HTTP method and path
501
505
  const matchedRoute = getMatchingRoute(apiType, apiRoute)
502
506
  try {
503
- // If no route matches, log the occurrence and throw a NotFoundError
504
507
  if (!matchedRoute) {
508
+ if (apiType.toLowerCase() === 'options') {
509
+ const httpGroups = pikkuState(null, 'middleware', 'httpGroup')
510
+ const globalMiddleware = httpGroups['*']
511
+ const wire: PikkuWire = { http: http! }
512
+ if (globalMiddleware) {
513
+ await runMiddleware(
514
+ singletonServices,
515
+ wire,
516
+ globalMiddleware as CorePikkuMiddleware[]
517
+ )
518
+ }
519
+ response.status(204).json(undefined as any)
520
+ return
521
+ }
505
522
  singletonServices.logger.info({
506
523
  message: 'Route not found',
507
524
  apiRoute,
@@ -525,7 +542,6 @@ export const fetchData = async <In, Out>(
525
542
 
526
543
  return result
527
544
  } catch (e: any) {
528
- // Handle errors and, depending on configuration, bubble them up or respond with an error
529
545
  handleHTTPError(
530
546
  e,
531
547
  http,
@@ -1,11 +1,7 @@
1
1
  import type { SerializeOptions } from 'cookie'
2
2
  import type { StandardSchemaV1 } from '@standard-schema/spec'
3
- import { PikkuError } from '../../errors/error-handler.js'
3
+ import type { PikkuError } from '../../errors/error-handler.js'
4
4
  import type {
5
- CoreServices,
6
- CoreSingletonServices,
7
- CoreUserSession,
8
- CreateWireServices,
9
5
  CorePikkuMiddleware,
10
6
  CommonWireMeta,
11
7
  } from '../../types/core.types.js'
@@ -43,15 +39,6 @@ export type RunHTTPWiringOptions = Partial<{
43
39
  generateRequestId: () => string
44
40
  }>
45
41
 
46
- export type RunHTTPWiringParams = {
47
- singletonServices: CoreSingletonServices
48
- createWireServices?: CreateWireServices<
49
- CoreSingletonServices,
50
- CoreServices<CoreSingletonServices>,
51
- CoreUserSession
52
- >
53
- }
54
-
55
42
  /**
56
43
  * Represents the HTTP methods supported for API HTTP wirings.
57
44
  */
@@ -1,5 +1,5 @@
1
1
  import { pikkuState } from '../../pikku-state.js'
2
- import { Logger } from '../../services/index.js'
2
+ import type { Logger } from '../../services/index.js'
3
3
 
4
4
  /**
5
5
  * Logs all the loaded routes.
@@ -1,6 +1,6 @@
1
1
  import { parse as parseQuery } from 'picoquery'
2
2
  import { parse as parseCookie } from 'cookie'
3
- import { HTTPMethod, PikkuHTTPRequest, PikkuQuery } from './http.types.js'
3
+ import type { HTTPMethod, PikkuHTTPRequest, PikkuQuery } from './http.types.js'
4
4
  import { UnprocessableContentError } from '../../errors/errors.js'
5
5
 
6
6
  /**
@@ -1,8 +1,6 @@
1
- import { PikkuHTTPResponse } from './http.types.js'
2
- import {
3
- SerializeOptions as CookieSerializeOptions,
4
- serialize as serializeCookie,
5
- } from 'cookie'
1
+ import type { PikkuHTTPResponse } from './http.types.js'
2
+ import type { SerializeOptions as CookieSerializeOptions } from 'cookie'
3
+ import { serialize as serializeCookie } from 'cookie'
6
4
 
7
5
  export class PikkuFetchHTTPResponse implements PikkuHTTPResponse {
8
6
  #statusCode: number = 200
@@ -91,6 +89,15 @@ export class PikkuFetchHTTPResponse implements PikkuHTTPResponse {
91
89
  // }
92
90
 
93
91
  public redirect(location: string, status: number = 302): this {
92
+ if (
93
+ !location.startsWith('/') &&
94
+ !location.startsWith('https://') &&
95
+ !location.startsWith('http://')
96
+ ) {
97
+ throw new Error(
98
+ 'Invalid redirect location: must be a relative path or absolute URL'
99
+ )
100
+ }
94
101
  this.#statusCode = status
95
102
  this.header('Location', location)
96
103
  return this
@@ -1,4 +1,4 @@
1
- import { HTTPMethod } from '../http.types.js'
1
+ import type { HTTPMethod } from '../http.types.js'
2
2
  import { PathToRegexRouter } from './path-to-regex.js'
3
3
 
4
4
  export type MatchResult = {
@@ -2,7 +2,7 @@ import { test, describe, beforeEach } from 'node:test'
2
2
  import * as assert from 'node:assert/strict'
3
3
  import { PathToRegexRouter } from './path-to-regex.js'
4
4
  import { resetPikkuState, pikkuState } from '../../../pikku-state.js'
5
- import { HTTPMethod } from '../http.types.js'
5
+ import type { HTTPMethod } from '../http.types.js'
6
6
 
7
7
  describe('PathToRegexRouter', () => {
8
8
  let router: PathToRegexRouter
@@ -1,6 +1,7 @@
1
- import { match, MatchFunction } from 'path-to-regexp'
2
- import { MatchResult, Router } from './http-router.js'
3
- import { HTTPMethod } from '../http.types.js'
1
+ import type { MatchFunction } from 'path-to-regexp'
2
+ import { match } from 'path-to-regexp'
3
+ import type { MatchResult, Router } from './http-router.js'
4
+ import type { HTTPMethod } from '../http.types.js'
4
5
  import { pikkuState } from '../../../pikku-state.js'
5
6
 
6
7
  interface CompiledRoute {
@@ -1,4 +1,8 @@
1
- import { MCPResourceMeta, MCPToolMeta, MCPPromptMeta } from '@pikku/core/mcp'
1
+ import type {
2
+ MCPResourceMeta,
3
+ MCPToolMeta,
4
+ MCPPromptMeta,
5
+ } from '@pikku/core/mcp'
2
6
 
3
7
  export interface MCPToolEndpoint {
4
8
  name: string
@@ -1,10 +1,4 @@
1
- import {
2
- PikkuWire,
3
- type CoreServices,
4
- type CoreSingletonServices,
5
- type CoreUserSession,
6
- type CreateWireServices,
7
- } from '../../types/core.types.js'
1
+ import type { PikkuWire } from '../../types/core.types.js'
8
2
  import type {
9
3
  CoreMCPResource,
10
4
  CoreMCPPrompt,
@@ -19,7 +13,11 @@ import type {
19
13
  } from '../../function/functions.types.js'
20
14
  import { getErrorResponse } from '../../errors/error-handler.js'
21
15
  import { closeWireServices } from '../../utils.js'
22
- import { pikkuState } from '../../pikku-state.js'
16
+ import {
17
+ pikkuState,
18
+ getSingletonServices,
19
+ getCreateWireServices,
20
+ } from '../../pikku-state.js'
23
21
  import { addFunction, runPikkuFunc } from '../../function/function-runner.js'
24
22
  import {
25
23
  BadRequestError,
@@ -40,13 +38,7 @@ export class MCPError extends Error {
40
38
  }
41
39
 
42
40
  export type RunMCPEndpointParams<Tools extends string = any> = {
43
- singletonServices: CoreSingletonServices
44
41
  mcp?: PikkuMCP<Tools>
45
- createWireServices?: CreateWireServices<
46
- CoreSingletonServices,
47
- CoreServices<CoreSingletonServices>,
48
- CoreUserSession
49
- >
50
42
  }
51
43
 
52
44
  export type JsonRpcError = {
@@ -197,8 +189,10 @@ async function runMCPPikkuFunc(
197
189
  name: string,
198
190
  mcp: CoreMCPResource | CoreMCPPrompt | undefined,
199
191
  pikkuFuncId: string | undefined,
200
- { singletonServices, createWireServices, mcp: mcpWire }: RunMCPEndpointParams
192
+ { mcp: mcpWire }: RunMCPEndpointParams
201
193
  ): Promise<JsonRpcResponse> {
194
+ const singletonServices = getSingletonServices()
195
+ const createWireServices = getCreateWireServices()
202
196
  let wireServices: any
203
197
 
204
198
  try {
@@ -1,10 +1,10 @@
1
- import {
1
+ import type {
2
2
  CorePermissionGroup,
3
3
  CorePikkuFunctionConfig,
4
4
  CorePikkuFunctionSessionless,
5
5
  CorePikkuPermission,
6
6
  } from '../../function/functions.types.js'
7
- import {
7
+ import type {
8
8
  CorePikkuMiddleware,
9
9
  MiddlewareMetadata,
10
10
  PermissionMetadata,
@@ -1,6 +1,6 @@
1
- import { OAuth2Token, OAuth2AppCredential } from './oauth2.types.js'
2
- import { OAuth2CredentialConfig } from '../secret/secret.types.js'
3
- import { SecretService } from '../../services/secret-service.js'
1
+ import type { OAuth2Token, OAuth2AppCredential } from './oauth2.types.js'
2
+ import type { OAuth2CredentialConfig } from '../secret/secret.types.js'
3
+ import type { SecretService } from '../../services/secret-service.js'
4
4
 
5
5
  /**
6
6
  * OAuth2 client that acts as a service.
@@ -1,4 +1,4 @@
1
- import { CoreOAuth2Credential } from './oauth2.types.js'
1
+ import type { CoreOAuth2Credential } from './oauth2.types.js'
2
2
 
3
3
  /**
4
4
  * No-op function for declaring OAuth2 credentials.
@@ -16,7 +16,6 @@ export type {
16
16
  export {
17
17
  wireQueueWorker,
18
18
  runQueueJob,
19
- createQueueJobRunner,
20
19
  getQueueWorkers,
21
20
  removeQueueWorker,
22
21
  QueueJobDiscardedError,