@pikku/core 0.12.2 → 0.12.4

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 (282) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/crypto-utils.d.ts +10 -0
  3. package/dist/crypto-utils.js +62 -0
  4. package/dist/function/function-runner.js +4 -4
  5. package/dist/function/functions.types.d.ts +26 -1
  6. package/dist/function/functions.types.js +16 -0
  7. package/dist/handle-error.d.ts +1 -1
  8. package/dist/handle-error.js +7 -3
  9. package/dist/index.d.ts +14 -2
  10. package/dist/index.js +1 -1
  11. package/dist/pikku-state.js +3 -1
  12. package/dist/schema.js +1 -1
  13. package/dist/services/in-memory-workflow-service.d.ts +22 -3
  14. package/dist/services/in-memory-workflow-service.js +77 -1
  15. package/dist/services/workflow-service.d.ts +8 -2
  16. package/dist/testing/index.d.ts +2 -0
  17. package/dist/testing/index.js +1 -0
  18. package/dist/testing/service-tests.d.ts +31 -0
  19. package/dist/testing/service-tests.js +598 -0
  20. package/dist/types/core.types.d.ts +9 -1
  21. package/dist/types/state.types.d.ts +4 -2
  22. package/dist/utils/hash.d.ts +2 -0
  23. package/dist/utils/hash.js +23 -0
  24. package/dist/wirings/ai-agent/agent-dynamic-workflow.d.ts +6 -0
  25. package/dist/wirings/ai-agent/agent-dynamic-workflow.js +348 -0
  26. package/dist/wirings/ai-agent/ai-agent-memory.d.ts +1 -1
  27. package/dist/wirings/ai-agent/ai-agent-memory.js +6 -5
  28. package/dist/wirings/ai-agent/ai-agent-prepare.d.ts +10 -7
  29. package/dist/wirings/ai-agent/ai-agent-prepare.js +127 -47
  30. package/dist/wirings/ai-agent/ai-agent-runner.d.ts +4 -0
  31. package/dist/wirings/ai-agent/ai-agent-runner.js +381 -27
  32. package/dist/wirings/ai-agent/ai-agent-stream.d.ts +7 -2
  33. package/dist/wirings/ai-agent/ai-agent-stream.js +109 -46
  34. package/dist/wirings/ai-agent/ai-agent.types.d.ts +22 -1
  35. package/dist/wirings/ai-agent/index.d.ts +1 -2
  36. package/dist/wirings/ai-agent/index.js +1 -2
  37. package/dist/wirings/channel/channel-handler.js +5 -1
  38. package/dist/wirings/channel/channel.types.d.ts +1 -7
  39. package/dist/wirings/channel/local/local-channel-handler.d.ts +1 -1
  40. package/dist/wirings/channel/local/local-channel-handler.js +5 -3
  41. package/dist/wirings/channel/local/local-channel-runner.js +14 -4
  42. package/dist/wirings/channel/serverless/serverless-channel-runner.js +4 -1
  43. package/dist/wirings/cli/cli-runner.js +10 -2
  44. package/dist/wirings/cli/cli.types.d.ts +1 -0
  45. package/dist/wirings/http/http-routes.js +0 -1
  46. package/dist/wirings/http/http-runner.d.ts +1 -1
  47. package/dist/wirings/http/http-runner.js +25 -3
  48. package/dist/wirings/http/http.types.d.ts +3 -11
  49. package/dist/wirings/http/index.d.ts +2 -1
  50. package/dist/wirings/http/index.js +1 -0
  51. package/dist/wirings/http/pikku-fetch-http-request.d.ts +1 -0
  52. package/dist/wirings/http/pikku-fetch-http-request.js +3 -0
  53. package/dist/wirings/http/pikku-fetch-http-response.d.ts +1 -0
  54. package/dist/wirings/http/pikku-fetch-http-response.js +4 -4
  55. package/dist/wirings/http/web-request.d.ts +12 -0
  56. package/dist/wirings/http/web-request.js +85 -0
  57. package/dist/wirings/mcp/mcp-endpoint-registry.js +1 -1
  58. package/dist/wirings/mcp/mcp-runner.js +13 -1
  59. package/dist/wirings/rpc/rpc-runner.d.ts +30 -6
  60. package/dist/wirings/rpc/rpc-runner.js +36 -14
  61. package/dist/wirings/rpc/rpc-types.d.ts +6 -1
  62. package/dist/wirings/rpc/wire-addon.d.ts +1 -0
  63. package/dist/wirings/rpc/wire-addon.js +1 -1
  64. package/dist/wirings/scheduler/scheduler-runner.js +1 -0
  65. package/dist/wirings/workflow/graph/graph-validation.d.ts +3 -0
  66. package/dist/wirings/workflow/graph/graph-validation.js +175 -0
  67. package/dist/wirings/workflow/graph/index.d.ts +1 -0
  68. package/dist/wirings/workflow/graph/index.js +1 -0
  69. package/dist/wirings/workflow/index.d.ts +1 -1
  70. package/dist/wirings/workflow/pikku-workflow-service.d.ts +8 -2
  71. package/dist/wirings/workflow/pikku-workflow-service.js +1 -2
  72. package/dist/wirings/workflow/workflow.types.d.ts +10 -1
  73. package/lcov.info +18891 -0
  74. package/package.json +4 -2
  75. package/dist/wirings/ai-agent/ai-agent-assistant-ui.d.ts +0 -5
  76. package/dist/wirings/ai-agent/ai-agent-assistant-ui.js +0 -168
  77. package/src/crypto-utils.test.ts +0 -116
  78. package/src/crypto-utils.ts +0 -99
  79. package/src/errors/error-handler.ts +0 -62
  80. package/src/errors/error.test.ts +0 -195
  81. package/src/errors/errors.ts +0 -374
  82. package/src/errors/index.ts +0 -2
  83. package/src/factory-functions.test.ts +0 -109
  84. package/src/function/function-runner.test.ts +0 -536
  85. package/src/function/function-runner.ts +0 -365
  86. package/src/function/functions.types.ts +0 -267
  87. package/src/function/index.ts +0 -5
  88. package/src/handle-error.test.ts +0 -361
  89. package/src/handle-error.ts +0 -65
  90. package/src/index.ts +0 -104
  91. package/src/internal.ts +0 -6
  92. package/src/middleware/auth-apikey.test.ts +0 -363
  93. package/src/middleware/auth-apikey.ts +0 -47
  94. package/src/middleware/auth-bearer.test.ts +0 -450
  95. package/src/middleware/auth-bearer.ts +0 -72
  96. package/src/middleware/auth-cookie.test.ts +0 -528
  97. package/src/middleware/auth-cookie.ts +0 -80
  98. package/src/middleware/cors.test.ts +0 -424
  99. package/src/middleware/cors.ts +0 -104
  100. package/src/middleware/index.ts +0 -5
  101. package/src/middleware/remote-auth.test.ts +0 -488
  102. package/src/middleware/remote-auth.ts +0 -68
  103. package/src/middleware/timeout.ts +0 -15
  104. package/src/middleware-runner.test.ts +0 -418
  105. package/src/middleware-runner.ts +0 -234
  106. package/src/permissions.test.ts +0 -434
  107. package/src/permissions.ts +0 -319
  108. package/src/pikku-request.ts +0 -23
  109. package/src/pikku-response.ts +0 -5
  110. package/src/pikku-state.test.ts +0 -224
  111. package/src/pikku-state.ts +0 -214
  112. package/src/run-tests-script.test.ts +0 -49
  113. package/src/schema.test.ts +0 -249
  114. package/src/schema.ts +0 -151
  115. package/src/services/ai-agent-runner-service.ts +0 -41
  116. package/src/services/ai-run-state-service.ts +0 -20
  117. package/src/services/ai-storage-service.ts +0 -39
  118. package/src/services/content-service.ts +0 -76
  119. package/src/services/deployment-service.ts +0 -22
  120. package/src/services/gateway-service.ts +0 -20
  121. package/src/services/gopass-secrets.ts +0 -98
  122. package/src/services/in-memory-ai-run-state-service.ts +0 -73
  123. package/src/services/in-memory-trigger-service.ts +0 -64
  124. package/src/services/in-memory-workflow-service.test.ts +0 -351
  125. package/src/services/in-memory-workflow-service.ts +0 -395
  126. package/src/services/index.ts +0 -56
  127. package/src/services/jwt-service.ts +0 -30
  128. package/src/services/local-content.ts +0 -120
  129. package/src/services/local-gateway-service.ts +0 -62
  130. package/src/services/local-secrets.test.ts +0 -80
  131. package/src/services/local-secrets.ts +0 -94
  132. package/src/services/local-variables.test.ts +0 -61
  133. package/src/services/local-variables.ts +0 -39
  134. package/src/services/logger-console.test.ts +0 -118
  135. package/src/services/logger-console.ts +0 -98
  136. package/src/services/logger.ts +0 -57
  137. package/src/services/scheduler-service.ts +0 -86
  138. package/src/services/schema-service.ts +0 -33
  139. package/src/services/scoped-secret-service.test.ts +0 -74
  140. package/src/services/scoped-secret-service.ts +0 -41
  141. package/src/services/secret-service.ts +0 -38
  142. package/src/services/trigger-service.ts +0 -17
  143. package/src/services/typed-secret-service.test.ts +0 -93
  144. package/src/services/typed-secret-service.ts +0 -70
  145. package/src/services/typed-variables-service.test.ts +0 -73
  146. package/src/services/typed-variables-service.ts +0 -81
  147. package/src/services/user-session-service.test.ts +0 -113
  148. package/src/services/user-session-service.ts +0 -86
  149. package/src/services/variables-service.ts +0 -11
  150. package/src/services/workflow-service.ts +0 -84
  151. package/src/time-utils.test.ts +0 -56
  152. package/src/time-utils.ts +0 -107
  153. package/src/types/core.types.ts +0 -470
  154. package/src/types/state.types.ts +0 -186
  155. package/src/utils.test.ts +0 -350
  156. package/src/utils.ts +0 -129
  157. package/src/version.test.ts +0 -80
  158. package/src/version.ts +0 -25
  159. package/src/wirings/ai-agent/ai-agent-assistant-ui.test.ts +0 -363
  160. package/src/wirings/ai-agent/ai-agent-assistant-ui.ts +0 -238
  161. package/src/wirings/ai-agent/ai-agent-helpers.test.ts +0 -152
  162. package/src/wirings/ai-agent/ai-agent-helpers.ts +0 -76
  163. package/src/wirings/ai-agent/ai-agent-memory.ts +0 -309
  164. package/src/wirings/ai-agent/ai-agent-model-config.test.ts +0 -115
  165. package/src/wirings/ai-agent/ai-agent-model-config.ts +0 -43
  166. package/src/wirings/ai-agent/ai-agent-prepare.ts +0 -522
  167. package/src/wirings/ai-agent/ai-agent-registry.test.ts +0 -37
  168. package/src/wirings/ai-agent/ai-agent-registry.ts +0 -82
  169. package/src/wirings/ai-agent/ai-agent-runner.test.ts +0 -317
  170. package/src/wirings/ai-agent/ai-agent-runner.ts +0 -251
  171. package/src/wirings/ai-agent/ai-agent-stream.test.ts +0 -652
  172. package/src/wirings/ai-agent/ai-agent-stream.ts +0 -1060
  173. package/src/wirings/ai-agent/ai-agent.types.ts +0 -360
  174. package/src/wirings/ai-agent/index.ts +0 -41
  175. package/src/wirings/channel/channel-common.ts +0 -90
  176. package/src/wirings/channel/channel-handler.ts +0 -241
  177. package/src/wirings/channel/channel-middleware-runner.ts +0 -120
  178. package/src/wirings/channel/channel-runner.ts +0 -166
  179. package/src/wirings/channel/channel-store.ts +0 -29
  180. package/src/wirings/channel/channel.types.ts +0 -178
  181. package/src/wirings/channel/define-channel-routes.ts +0 -25
  182. package/src/wirings/channel/eventhub-service.ts +0 -34
  183. package/src/wirings/channel/eventhub-store.ts +0 -13
  184. package/src/wirings/channel/index.ts +0 -24
  185. package/src/wirings/channel/local/index.ts +0 -3
  186. package/src/wirings/channel/local/local-channel-handler.ts +0 -79
  187. package/src/wirings/channel/local/local-channel-runner.test.ts +0 -215
  188. package/src/wirings/channel/local/local-channel-runner.ts +0 -200
  189. package/src/wirings/channel/local/local-eventhub-service.test.ts +0 -95
  190. package/src/wirings/channel/local/local-eventhub-service.ts +0 -101
  191. package/src/wirings/channel/log-channels.ts +0 -20
  192. package/src/wirings/channel/pikku-abstract-channel-handler.test.ts +0 -54
  193. package/src/wirings/channel/pikku-abstract-channel-handler.ts +0 -45
  194. package/src/wirings/channel/serverless/index.ts +0 -5
  195. package/src/wirings/channel/serverless/serverless-channel-runner.ts +0 -286
  196. package/src/wirings/cli/channel/cli-channel-runner.ts +0 -136
  197. package/src/wirings/cli/channel/index.ts +0 -1
  198. package/src/wirings/cli/cli-runner.test.ts +0 -403
  199. package/src/wirings/cli/cli-runner.ts +0 -519
  200. package/src/wirings/cli/cli.types.ts +0 -357
  201. package/src/wirings/cli/command-parser.test.ts +0 -445
  202. package/src/wirings/cli/command-parser.ts +0 -504
  203. package/src/wirings/cli/define-cli-commands.ts +0 -24
  204. package/src/wirings/cli/index.ts +0 -19
  205. package/src/wirings/gateway/gateway-runner.test.ts +0 -474
  206. package/src/wirings/gateway/gateway-runner.ts +0 -411
  207. package/src/wirings/gateway/gateway.types.ts +0 -149
  208. package/src/wirings/gateway/index.ts +0 -13
  209. package/src/wirings/http/http-routes.test.ts +0 -322
  210. package/src/wirings/http/http-routes.ts +0 -198
  211. package/src/wirings/http/http-runner.test.ts +0 -144
  212. package/src/wirings/http/http-runner.ts +0 -563
  213. package/src/wirings/http/http.types.ts +0 -378
  214. package/src/wirings/http/index.ts +0 -25
  215. package/src/wirings/http/log-http-routes.ts +0 -22
  216. package/src/wirings/http/pikku-fetch-http-request.test.ts +0 -237
  217. package/src/wirings/http/pikku-fetch-http-request.ts +0 -166
  218. package/src/wirings/http/pikku-fetch-http-response.test.ts +0 -82
  219. package/src/wirings/http/pikku-fetch-http-response.ts +0 -147
  220. package/src/wirings/http/routers/http-router.ts +0 -13
  221. package/src/wirings/http/routers/path-to-regex.test.ts +0 -319
  222. package/src/wirings/http/routers/path-to-regex.ts +0 -126
  223. package/src/wirings/mcp/index.ts +0 -27
  224. package/src/wirings/mcp/mcp-endpoint-registry.test.ts +0 -389
  225. package/src/wirings/mcp/mcp-endpoint-registry.ts +0 -159
  226. package/src/wirings/mcp/mcp-runner.ts +0 -305
  227. package/src/wirings/mcp/mcp.types.ts +0 -216
  228. package/src/wirings/node/index.ts +0 -2
  229. package/src/wirings/node/node.types.ts +0 -23
  230. package/src/wirings/oauth2/index.ts +0 -3
  231. package/src/wirings/oauth2/oauth2-client.test.ts +0 -929
  232. package/src/wirings/oauth2/oauth2-client.ts +0 -335
  233. package/src/wirings/oauth2/oauth2.types.ts +0 -69
  234. package/src/wirings/oauth2/wire-oauth2-credential.ts +0 -23
  235. package/src/wirings/queue/index.ts +0 -31
  236. package/src/wirings/queue/queue-runner.test.ts +0 -710
  237. package/src/wirings/queue/queue-runner.ts +0 -192
  238. package/src/wirings/queue/queue.types.ts +0 -189
  239. package/src/wirings/queue/register-queue-helper.ts +0 -60
  240. package/src/wirings/queue/validate-worker-config.test.ts +0 -108
  241. package/src/wirings/queue/validate-worker-config.ts +0 -116
  242. package/src/wirings/rpc/index.ts +0 -4
  243. package/src/wirings/rpc/rpc-runner.ts +0 -439
  244. package/src/wirings/rpc/rpc-types.ts +0 -51
  245. package/src/wirings/rpc/wire-addon.ts +0 -20
  246. package/src/wirings/scheduler/index.ts +0 -11
  247. package/src/wirings/scheduler/log-schedulers.ts +0 -20
  248. package/src/wirings/scheduler/scheduler-runner.test.ts +0 -660
  249. package/src/wirings/scheduler/scheduler-runner.ts +0 -132
  250. package/src/wirings/scheduler/scheduler.types.ts +0 -53
  251. package/src/wirings/secret/index.ts +0 -9
  252. package/src/wirings/secret/secret.types.ts +0 -32
  253. package/src/wirings/secret/validate-secret-definitions.test.ts +0 -140
  254. package/src/wirings/secret/validate-secret-definitions.ts +0 -82
  255. package/src/wirings/trigger/index.ts +0 -10
  256. package/src/wirings/trigger/pikku-trigger-service.ts +0 -112
  257. package/src/wirings/trigger/trigger-runner.test.ts +0 -79
  258. package/src/wirings/trigger/trigger-runner.ts +0 -135
  259. package/src/wirings/trigger/trigger.types.ts +0 -178
  260. package/src/wirings/variable/index.ts +0 -8
  261. package/src/wirings/variable/validate-variable-definitions.test.ts +0 -91
  262. package/src/wirings/variable/validate-variable-definitions.ts +0 -69
  263. package/src/wirings/variable/variable.types.ts +0 -22
  264. package/src/wirings/workflow/dsl/index.ts +0 -31
  265. package/src/wirings/workflow/dsl/workflow-dsl.types.ts +0 -320
  266. package/src/wirings/workflow/dsl/workflow-runner.ts +0 -28
  267. package/src/wirings/workflow/graph/graph-node.ts +0 -222
  268. package/src/wirings/workflow/graph/graph-runner.test.ts +0 -487
  269. package/src/wirings/workflow/graph/graph-runner.ts +0 -816
  270. package/src/wirings/workflow/graph/index.ts +0 -14
  271. package/src/wirings/workflow/graph/template.test.ts +0 -49
  272. package/src/wirings/workflow/graph/template.ts +0 -42
  273. package/src/wirings/workflow/graph/wire-workflow-graph.ts +0 -29
  274. package/src/wirings/workflow/graph/workflow-graph.types.ts +0 -104
  275. package/src/wirings/workflow/index.ts +0 -81
  276. package/src/wirings/workflow/pikku-workflow-service.test.ts +0 -200
  277. package/src/wirings/workflow/pikku-workflow-service.ts +0 -1165
  278. package/src/wirings/workflow/workflow-helpers.test.ts +0 -129
  279. package/src/wirings/workflow/workflow-helpers.ts +0 -79
  280. package/src/wirings/workflow/workflow.types.ts +0 -266
  281. package/tsconfig.json +0 -14
  282. package/tsconfig.tsbuildinfo +0 -1
@@ -1,439 +0,0 @@
1
- import type { CoreServices, PikkuWire } from '../../types/core.types.js'
2
- import type { SessionService } from '../../services/user-session-service.js'
3
- import type { CoreUserSession } from '../../types/core.types.js'
4
- import { runPikkuFunc } from '../../function/function-runner.js'
5
- import { pikkuState } from '../../pikku-state.js'
6
- import { ForbiddenError } from '../../errors/errors.js'
7
- import { PikkuError } from '../../errors/error-handler.js'
8
- import type { PikkuRPC, ResolvedFunction } from './rpc-types.js'
9
- import { parseVersionedId } from '../../version.js'
10
- import { encryptJSON } from '../../crypto-utils.js'
11
-
12
- export class RPCNotFoundError extends PikkuError {
13
- public readonly rpcName: string
14
- constructor(rpcName: string) {
15
- super(`RPC function not found: ${rpcName}`)
16
- this.rpcName = rpcName
17
- }
18
- }
19
- import type { AIAgentInput } from '../ai-agent/ai-agent.types.js'
20
- import type { AIStreamChannel } from '../ai-agent/ai-agent.types.js'
21
- import type { StreamAIAgentOptions } from '../ai-agent/ai-agent-prepare.js'
22
- import { runAIAgent } from '../ai-agent/ai-agent-runner.js'
23
- import { streamAIAgent, resumeAIAgent } from '../ai-agent/ai-agent-stream.js'
24
- import { approveAIAgent } from '../ai-agent/ai-agent-registry.js'
25
-
26
- /**
27
- * Resolve a namespaced function reference to package and function names
28
- * Uses pikkuState to look up the namespace -> package mapping
29
- */
30
- const resolveNamespace = (
31
- namespacedFunction: string
32
- ): ResolvedFunction | null => {
33
- const colonIndex = namespacedFunction.indexOf(':')
34
- if (colonIndex === -1) {
35
- return null
36
- }
37
-
38
- const namespace = namespacedFunction.substring(0, colonIndex)
39
- const functionName = namespacedFunction.substring(colonIndex + 1)
40
-
41
- const addons = pikkuState(null, 'rpc', 'addons')
42
- const pkgConfig = addons.get(namespace)
43
- if (!pkgConfig) {
44
- return null
45
- }
46
-
47
- return {
48
- namespace,
49
- package: pkgConfig.package,
50
- function: functionName,
51
- }
52
- }
53
-
54
- const getPikkuFunctionName = (rpcName: string): string => {
55
- const rpc = pikkuState(null, 'rpc', 'meta')
56
- let rpcMeta = rpc[rpcName]
57
- if (!rpcMeta) {
58
- const { baseName, version } = parseVersionedId(rpcName)
59
- if (version !== null) {
60
- rpcMeta = rpc[baseName]
61
- }
62
- }
63
- if (!rpcMeta) {
64
- throw new RPCNotFoundError(rpcName)
65
- }
66
- return rpcMeta
67
- }
68
-
69
- // Context-aware RPC client for use within services
70
- export class ContextAwareRPCService {
71
- constructor(
72
- private services: CoreServices,
73
- private wire: PikkuWire,
74
- private options: {
75
- requiresAuth?: boolean
76
- sessionService?: SessionService<CoreUserSession>
77
- }
78
- ) {}
79
-
80
- public async rpcExposed(funcName: string, data: any): Promise<any> {
81
- let functionMeta: any
82
- if (funcName.includes(':')) {
83
- const resolved = resolveNamespace(funcName)
84
- if (resolved) {
85
- functionMeta = pikkuState(resolved.package, 'function', 'meta')[
86
- resolved.function
87
- ]
88
- }
89
- } else {
90
- functionMeta = pikkuState(null, 'function', 'meta')[funcName]
91
- }
92
- if (!functionMeta) {
93
- throw new Error(`Function not found: ${funcName}`)
94
- }
95
- if (!functionMeta.expose) {
96
- throw new ForbiddenError()
97
- }
98
- return await this.rpc(funcName, data)
99
- }
100
-
101
- public async rpc<In = any, Out = any>(
102
- funcName: string,
103
- data: In
104
- ): Promise<Out> {
105
- const rpcDepth = this.wire.rpc?.depth || 0
106
- const updatedWire: PikkuWire = {
107
- ...this.wire,
108
- rpc: this.wire.rpc
109
- ? {
110
- ...this.wire.rpc,
111
- depth: rpcDepth + 1,
112
- global: false,
113
- }
114
- : undefined,
115
- }
116
-
117
- // Check if it's a namespaced function call (e.g., 'stripe:createCharge')
118
- if (funcName.includes(':')) {
119
- return this.invokeAddonFunction<In, Out>(funcName, data, updatedWire)
120
- }
121
-
122
- // Main package function
123
- return runPikkuFunc<In, Out>(
124
- 'rpc',
125
- funcName,
126
- getPikkuFunctionName(funcName),
127
- {
128
- auth: this.options.requiresAuth,
129
- singletonServices: this.services,
130
- data: () => data,
131
- wire: updatedWire,
132
- }
133
- )
134
- }
135
-
136
- /**
137
- * Invoke a function from an addon package
138
- * Addon packages register their functions in pikkuState under their package name.
139
- * The function is executed using the parent services (shared singleton services).
140
- * @private
141
- */
142
- private async invokeAddonFunction<In = any, Out = any>(
143
- namespacedFunction: string,
144
- data: In,
145
- wire: PikkuWire
146
- ): Promise<Out> {
147
- // Resolve namespace to package name
148
- const resolved = resolveNamespace(namespacedFunction)
149
- if (!resolved) {
150
- throw new Error(
151
- `Unknown namespace in function reference: ${namespacedFunction}. ` +
152
- `Make sure the package is registered in addons config.`
153
- )
154
- }
155
-
156
- // Get the function meta from the addon package
157
- // Addon packages use function meta, not RPC meta
158
- const addonFunctionMeta = pikkuState(resolved.package, 'function', 'meta')
159
- const funcMeta = addonFunctionMeta[resolved.function]
160
- if (!funcMeta) {
161
- throw new Error(
162
- `Function '${resolved.function}' not found in package '${resolved.package}'. ` +
163
- `Available functions: ${Object.keys(addonFunctionMeta).join(', ') || '(none)'}`
164
- )
165
- }
166
- const funcName = funcMeta.pikkuFuncId || resolved.function
167
-
168
- const addonConfig = pikkuState(null, 'rpc', 'addons').get(
169
- resolved.namespace
170
- )
171
- const auth = addonConfig?.auth ?? this.options.requiresAuth
172
- const tags = [...(addonConfig?.tags ?? []), ...(funcMeta.tags ?? [])]
173
-
174
- // Execute the function using runPikkuFunc with the addon package's state
175
- // We use the parent services (this.services) since addon packages share services
176
- // Pass the function's tags so tag-based middleware/permissions are applied
177
- return runPikkuFunc<In, Out>('rpc', namespacedFunction, funcName, {
178
- auth,
179
- singletonServices: this.services,
180
- data: () => data,
181
- wire,
182
- packageName: resolved.package,
183
- tags,
184
- })
185
- }
186
-
187
- public async rpcWithWire<In = any, Out = any>(
188
- rpcName: string,
189
- data: In,
190
- wire: PikkuWire
191
- ): Promise<Out> {
192
- const rpcDepth = this.wire.rpc?.depth || 0
193
- const mergedWire: PikkuWire = {
194
- ...this.wire,
195
- ...wire,
196
- rpc: this.wire.rpc
197
- ? {
198
- ...this.wire.rpc,
199
- depth: rpcDepth + 1,
200
- global: false,
201
- }
202
- : undefined,
203
- }
204
- return runPikkuFunc<In, Out>(
205
- 'rpc',
206
- rpcName,
207
- getPikkuFunctionName(rpcName),
208
- {
209
- auth: this.options.requiresAuth,
210
- singletonServices: this.services,
211
- data: () => data,
212
- wire: mergedWire,
213
- }
214
- )
215
- }
216
-
217
- public async startWorkflow<In = any>(
218
- workflowName: string,
219
- input: In,
220
- options?: {
221
- startNode?: string
222
- wire?: { type: string; id?: string; parentRunId?: string }
223
- }
224
- ): Promise<{ runId: string }> {
225
- if (!this.services.workflowService) {
226
- throw new Error('WorkflowService service not available')
227
- }
228
- const parentRunId = this.wire.workflowStep?.runId ?? this.wire.graph?.runId
229
- const wire = options?.wire ?? {
230
- type: this.wire.wireType ?? 'unknown',
231
- id: this.wire.wireId,
232
- ...(parentRunId ? { parentRunId } : {}),
233
- }
234
- return this.services.workflowService.startWorkflow(
235
- workflowName,
236
- input,
237
- wire,
238
- this,
239
- options
240
- )
241
- }
242
-
243
- public get agent() {
244
- return {
245
- run: async (agentName: string, input: AIAgentInput) => {
246
- const result = await runAIAgent(agentName, input, {
247
- sessionService: this.options.sessionService,
248
- })
249
- return {
250
- runId: result.runId,
251
- result: result.object ?? result.text,
252
- usage: result.usage,
253
- }
254
- },
255
- stream: async (
256
- agentName: string,
257
- input:
258
- | { message: string; threadId: string; resourceId: string }
259
- | Record<string, unknown>,
260
- options?: StreamAIAgentOptions
261
- ) => {
262
- const channel = this.wire.channel as unknown as AIStreamChannel
263
- if (!channel) throw new Error('No channel available for streaming')
264
- await streamAIAgent(
265
- agentName,
266
- input,
267
- channel,
268
- { sessionService: this.options.sessionService },
269
- undefined,
270
- options
271
- )
272
- },
273
- resume: async (
274
- runId: string,
275
- input: { toolCallId: string; approved: boolean },
276
- options?: StreamAIAgentOptions
277
- ) => {
278
- const channel = this.wire.channel as unknown as AIStreamChannel
279
- if (!channel) throw new Error('No channel available for streaming')
280
- await resumeAIAgent(
281
- { runId, ...input },
282
- channel,
283
- { sessionService: this.options.sessionService },
284
- options
285
- )
286
- },
287
- approve: async (
288
- runId: string,
289
- approvals: { toolCallId: string; approved: boolean }[],
290
- expectedAgentName?: string
291
- ) => {
292
- if (!this.services.aiRunState)
293
- throw new Error('AIRunStateService not available')
294
- return approveAIAgent(
295
- this.services.aiRunState,
296
- runId,
297
- approvals,
298
- expectedAgentName
299
- )
300
- },
301
- }
302
- }
303
-
304
- public async remote<In = any, Out = any>(
305
- funcName: string,
306
- data: In
307
- ): Promise<Out> {
308
- let endpoint: string | undefined
309
- const headers: Record<string, string> = {
310
- 'Content-Type': 'application/json',
311
- }
312
-
313
- const colonIndex = funcName.indexOf(':')
314
- if (colonIndex !== -1) {
315
- const namespace = funcName.substring(0, colonIndex)
316
- const addons = pikkuState(null, 'rpc', 'addons')
317
- const pkgConfig = addons.get(namespace)
318
- endpoint = pkgConfig?.rpcEndpoint
319
- }
320
-
321
- if (!endpoint && this.services.deploymentService) {
322
- const deployments =
323
- await this.services.deploymentService.findFunction(funcName)
324
- if (deployments.length > 0) {
325
- endpoint = deployments[0].endpoint
326
- }
327
- }
328
-
329
- if (!endpoint) {
330
- throw new Error(
331
- `No endpoint configured for remote RPC: ${funcName}. ` +
332
- `Configure rpcEndpoint in addons config or set up a DeploymentService.`
333
- )
334
- }
335
-
336
- let secret: string | undefined
337
- if (this.services.secrets) {
338
- try {
339
- secret = await this.services.secrets.getSecret('PIKKU_REMOTE_SECRET')
340
- } catch {}
341
- }
342
- if (secret) {
343
- if (!this.services.jwt) {
344
- throw new Error(
345
- 'PIKKU_REMOTE_SECRET is set but JWT service is not available'
346
- )
347
- }
348
- const session =
349
- this.wire.getSession && typeof this.wire.getSession === 'function'
350
- ? await this.wire.getSession()
351
- : (this.wire as any).session
352
- const sessionEnc = session
353
- ? await encryptJSON(secret, { session })
354
- : undefined
355
- const token = await this.services.jwt.encode(
356
- { value: 5, unit: 'minute' },
357
- {
358
- aud: 'pikku-remote',
359
- fn: funcName,
360
- iat: Date.now(),
361
- session: sessionEnc,
362
- }
363
- )
364
- headers.Authorization = `Bearer ${token}`
365
- }
366
-
367
- const url = `${endpoint}/rpc/${encodeURIComponent(funcName)}`
368
- const timeoutMs = 30_000
369
- const response = await fetch(url, {
370
- method: 'POST',
371
- headers,
372
- body: JSON.stringify({ data }),
373
- signal: AbortSignal.timeout(timeoutMs),
374
- })
375
-
376
- if (!response.ok) {
377
- let errorBody: string
378
- try {
379
- errorBody = JSON.stringify(await response.json())
380
- } catch {
381
- errorBody = await response.text()
382
- }
383
- throw new Error(
384
- `Remote RPC call failed: ${response.status} ${response.statusText}. ${errorBody}`
385
- )
386
- }
387
-
388
- try {
389
- return (await response.json()) as Out
390
- } catch {
391
- const text = await response.text()
392
- throw new Error(
393
- `Remote RPC call returned non-JSON response: ${response.status} ${response.statusText}. ${text}`
394
- )
395
- }
396
- }
397
- }
398
-
399
- // RPC Service class for the global interface
400
- export class PikkuRPCService<
401
- Services extends CoreServices,
402
- TypedRPC = PikkuRPC,
403
- > {
404
- // Convenience function for initializing
405
- getContextRPCService(
406
- services: Services,
407
- wire: PikkuWire,
408
- requiresAuthOrOptions?:
409
- | boolean
410
- | {
411
- requiresAuth?: boolean
412
- sessionService?: SessionService<CoreUserSession>
413
- }
414
- | undefined,
415
- depth: number = 0
416
- ): TypedRPC {
417
- const options =
418
- typeof requiresAuthOrOptions === 'object' &&
419
- requiresAuthOrOptions !== null
420
- ? requiresAuthOrOptions
421
- : { requiresAuth: requiresAuthOrOptions }
422
- const serviceRPC = new ContextAwareRPCService(services, wire, options)
423
- return {
424
- depth,
425
- global: false,
426
- invoke: serviceRPC.rpc.bind(serviceRPC),
427
- remote: serviceRPC.remote.bind(serviceRPC),
428
- exposed: serviceRPC.rpcExposed.bind(serviceRPC),
429
- startWorkflow: serviceRPC.startWorkflow.bind(serviceRPC),
430
- get agent() {
431
- return serviceRPC.agent
432
- },
433
- rpcWithWire: serviceRPC.rpcWithWire.bind(serviceRPC),
434
- } as any
435
- }
436
- }
437
-
438
- // Create a singleton instance
439
- export const rpcService = new PikkuRPCService()
@@ -1,51 +0,0 @@
1
- export type PikkuRPC<
2
- Invoke extends Function = any,
3
- Remote extends Function = any,
4
- startWorkflow extends Function = any,
5
- AgentRun extends Function = any,
6
- AgentStream extends Function = any,
7
- > = {
8
- depth: number
9
- global: boolean
10
- invoke: Invoke
11
- remote: Remote
12
- exposed: (name: string, data: any) => Promise<any>
13
- startWorkflow: startWorkflow
14
- agent: {
15
- run: AgentRun
16
- stream: AgentStream
17
- resume: (
18
- runId: string,
19
- input: { toolCallId: string; approved: boolean },
20
- options?: any
21
- ) => Promise<void>
22
- approve: (
23
- runId: string,
24
- approvals: { toolCallId: string; approved: boolean }[],
25
- expectedAgentName?: string
26
- ) => Promise<any>
27
- }
28
- }
29
-
30
- export type RPCMeta = {
31
- pikkuFuncId: string
32
- expose: boolean
33
- remote?: boolean
34
- }
35
-
36
- /**
37
- * Type for RPC handlers
38
- */
39
- export interface RPCHandler<Input, Output> {
40
- input: Input
41
- output: Output
42
- }
43
-
44
- /**
45
- * Resolved function reference from namespace
46
- */
47
- export interface ResolvedFunction {
48
- namespace: string
49
- package: string
50
- function: string
51
- }
@@ -1,20 +0,0 @@
1
- import { pikkuState } from '../../pikku-state.js'
2
-
3
- export type WireAddonConfig = {
4
- name: string
5
- package: string
6
- rpcEndpoint?: string
7
- auth?: boolean
8
- tags?: string[]
9
- secretOverrides?: Record<string, string>
10
- variableOverrides?: Record<string, string>
11
- }
12
-
13
- export const wireAddon = (config: WireAddonConfig): void => {
14
- pikkuState(null, 'rpc', 'addons').set(config.name, {
15
- package: config.package,
16
- rpcEndpoint: config.rpcEndpoint,
17
- auth: config.auth,
18
- tags: config.tags,
19
- })
20
- }
@@ -1,11 +0,0 @@
1
- export {
2
- runScheduledTask,
3
- wireScheduler,
4
- getScheduledTasks,
5
- } from './scheduler-runner.js'
6
- export { logSchedulers } from './log-schedulers.js'
7
- export type {
8
- CoreScheduledTask,
9
- ScheduledTasksMeta,
10
- PikkuScheduledTask,
11
- } from './scheduler.types.js'
@@ -1,20 +0,0 @@
1
- import { pikkuState } from '../../pikku-state.js'
2
- import type { Logger } from '../../services/index.js'
3
-
4
- /**
5
- * Logs all the loaded scheduled tasks.
6
- * @param logger - A logger for logging information.
7
- */
8
- export const logSchedulers = (logger: Logger) => {
9
- const scheduledTasks = pikkuState(null, 'scheduler', 'tasks')
10
- if (scheduledTasks.size === 0) {
11
- logger.info('No scheduled tasks added')
12
- return
13
- }
14
-
15
- let scheduledTasksMessage = 'Scheduled tasks:'
16
- scheduledTasks.forEach(({ schedule }, name) => {
17
- scheduledTasksMessage += `\n\t- ${name} -> ${schedule}`
18
- })
19
- logger.info(scheduledTasksMessage)
20
- }