@pikku/core 0.12.62 → 0.12.64

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 (198) hide show
  1. package/CHANGELOG.md +153 -0
  2. package/dist/dev/hot-reload.js +13 -55
  3. package/dist/dev/module-runner.d.ts +14 -0
  4. package/dist/dev/module-runner.js +59 -0
  5. package/dist/dev/reload-meta.d.ts +13 -0
  6. package/dist/dev/reload-meta.js +22 -0
  7. package/dist/errors/errors.d.ts +12 -0
  8. package/dist/errors/errors.js +19 -0
  9. package/dist/function/function-runner.d.ts +5 -5
  10. package/dist/function/function-runner.js +39 -111
  11. package/dist/function/functions.types.d.ts +17 -7
  12. package/dist/index.d.ts +5 -1
  13. package/dist/index.js +3 -1
  14. package/dist/middleware/auth-apikey.d.ts +1 -0
  15. package/dist/middleware/auth-bearer.d.ts +1 -0
  16. package/dist/middleware/auth-cookie.d.ts +1 -0
  17. package/dist/middleware/cors.d.ts +1 -0
  18. package/dist/middleware/index.d.ts +1 -1
  19. package/dist/middleware/index.js +1 -1
  20. package/dist/middleware/remote-auth.d.ts +1 -0
  21. package/dist/middleware/telemetry.d.ts +2 -0
  22. package/dist/middleware/timeout.d.ts +1 -0
  23. package/dist/permissions.d.ts +32 -25
  24. package/dist/permissions.js +104 -185
  25. package/dist/pikku-state.js +0 -2
  26. package/dist/scopes.d.ts +17 -0
  27. package/dist/scopes.js +59 -0
  28. package/dist/services/ai-embedding-service.d.ts +31 -0
  29. package/dist/services/ai-embedding-service.js +1 -0
  30. package/dist/services/credential-wire-service.d.ts +3 -1
  31. package/dist/services/credential-wire-service.js +10 -4
  32. package/dist/services/index.d.ts +4 -0
  33. package/dist/services/index.js +2 -0
  34. package/dist/services/meta-service.d.ts +15 -2
  35. package/dist/services/meta-service.js +51 -1
  36. package/dist/services/queue-webhook-service.d.ts +55 -0
  37. package/dist/services/queue-webhook-service.js +136 -0
  38. package/dist/services/scope-service.d.ts +67 -0
  39. package/dist/services/scope-service.js +1 -0
  40. package/dist/services/typed-secret-service.d.ts +8 -0
  41. package/dist/services/typed-secret-service.js +39 -4
  42. package/dist/services/webhook-service.d.ts +140 -0
  43. package/dist/services/webhook-service.js +44 -0
  44. package/dist/services/workflow-service.d.ts +7 -0
  45. package/dist/types/core.types.d.ts +34 -6
  46. package/dist/types/state.types.d.ts +20 -2
  47. package/dist/utils/hmac.d.ts +16 -0
  48. package/dist/utils/hmac.js +26 -0
  49. package/dist/utils/safe-fetch.d.ts +51 -0
  50. package/dist/utils/safe-fetch.js +192 -0
  51. package/dist/wirings/ai-agent/ai-agent-agui.d.ts +84 -0
  52. package/dist/wirings/ai-agent/ai-agent-agui.js +305 -0
  53. package/dist/wirings/ai-agent/ai-agent-prepare.d.ts +41 -1
  54. package/dist/wirings/ai-agent/ai-agent-prepare.js +146 -6
  55. package/dist/wirings/ai-agent/ai-agent-runner.js +6 -1
  56. package/dist/wirings/ai-agent/ai-agent-stream.js +61 -10
  57. package/dist/wirings/ai-agent/ai-agent.types.d.ts +27 -1
  58. package/dist/wirings/ai-agent/index.d.ts +1 -0
  59. package/dist/wirings/ai-agent/index.js +1 -0
  60. package/dist/wirings/ai-agent/voice-input.d.ts +1 -0
  61. package/dist/wirings/ai-agent/voice-input.js +2 -38
  62. package/dist/wirings/ai-agent/voice-output.d.ts +1 -0
  63. package/dist/wirings/channel/channel-common.js +0 -1
  64. package/dist/wirings/channel/channel-handler.js +1 -4
  65. package/dist/wirings/channel/channel-middleware-runner.d.ts +12 -0
  66. package/dist/wirings/channel/channel-middleware-runner.js +34 -19
  67. package/dist/wirings/channel/channel.types.d.ts +2 -6
  68. package/dist/wirings/cli/cli-runner.js +0 -2
  69. package/dist/wirings/cli/cli.types.d.ts +1 -2
  70. package/dist/wirings/http/http-routes.js +0 -3
  71. package/dist/wirings/http/http-runner.d.ts +9 -29
  72. package/dist/wirings/http/http-runner.js +13 -35
  73. package/dist/wirings/http/http.types.d.ts +1 -9
  74. package/dist/wirings/mcp/mcp-runner.js +0 -2
  75. package/dist/wirings/mcp/mcp.types.d.ts +5 -11
  76. package/dist/wirings/oauth2/index.d.ts +0 -3
  77. package/dist/wirings/oauth2/index.js +1 -2
  78. package/dist/wirings/rpc/addon-runner.d.ts +28 -0
  79. package/dist/wirings/rpc/addon-runner.js +173 -0
  80. package/dist/wirings/rpc/index.d.ts +5 -1
  81. package/dist/wirings/rpc/index.js +3 -1
  82. package/dist/wirings/rpc/remote-addon-auth.d.ts +26 -0
  83. package/dist/wirings/rpc/remote-addon-auth.js +43 -0
  84. package/dist/wirings/rpc/rpc-runner.d.ts +19 -0
  85. package/dist/wirings/rpc/rpc-runner.js +109 -3
  86. package/dist/wirings/rpc/rpc-types.d.ts +15 -1
  87. package/dist/wirings/rpc/wire-addon.js +9 -0
  88. package/dist/wirings/rpc/wire-remote-addon.d.ts +47 -0
  89. package/dist/wirings/rpc/wire-remote-addon.js +19 -0
  90. package/dist/wirings/scope/index.d.ts +3 -0
  91. package/dist/wirings/scope/index.js +2 -0
  92. package/dist/wirings/scope/scope.types.d.ts +39 -0
  93. package/dist/wirings/scope/scope.types.js +1 -0
  94. package/dist/wirings/scope/validate-scope-definitions.d.ts +16 -0
  95. package/dist/wirings/scope/validate-scope-definitions.js +76 -0
  96. package/dist/wirings/scope/wire-scope.d.ts +33 -0
  97. package/dist/wirings/scope/wire-scope.js +32 -0
  98. package/dist/wirings/workflow/dsl/index.d.ts +1 -1
  99. package/dist/wirings/workflow/dsl/workflow-dsl.types.d.ts +53 -1
  100. package/dist/wirings/workflow/graph/graph-node.d.ts +2 -0
  101. package/dist/wirings/workflow/graph/graph-runner.js +3 -0
  102. package/dist/wirings/workflow/graph/wire-workflow-graph.d.ts +6 -0
  103. package/dist/wirings/workflow/graph/wire-workflow-graph.js +1 -0
  104. package/dist/wirings/workflow/graph/workflow-graph.types.d.ts +5 -0
  105. package/dist/wirings/workflow/index.d.ts +2 -2
  106. package/dist/wirings/workflow/index.js +1 -1
  107. package/dist/wirings/workflow/pikku-workflow-service.d.ts +51 -1
  108. package/dist/wirings/workflow/pikku-workflow-service.js +171 -0
  109. package/dist/wirings/workflow/workflow.types.d.ts +1 -1
  110. package/package.json +5 -1
  111. package/run-tests.sh +1 -0
  112. package/src/dev/hot-reload.ts +13 -68
  113. package/src/dev/module-runner.test.ts +169 -0
  114. package/src/dev/module-runner.ts +103 -0
  115. package/src/dev/reload-meta.test.ts +31 -2
  116. package/src/dev/reload-meta.ts +26 -0
  117. package/src/errors/errors.ts +24 -0
  118. package/src/function/function-runner.test.ts +276 -142
  119. package/src/function/function-runner.ts +52 -145
  120. package/src/function/functions.types.ts +28 -38
  121. package/src/index.ts +8 -6
  122. package/src/middleware/index.ts +1 -5
  123. package/src/permissions.test.ts +153 -372
  124. package/src/permissions.ts +139 -259
  125. package/src/pikku-state.ts +0 -2
  126. package/src/scopes.test.ts +167 -0
  127. package/src/scopes.ts +69 -0
  128. package/src/services/ai-embedding-service.ts +31 -0
  129. package/src/services/credential-wire-service.ts +10 -4
  130. package/src/services/index.ts +20 -0
  131. package/src/services/meta-service.ts +68 -3
  132. package/src/services/queue-webhook-service.test.ts +408 -0
  133. package/src/services/queue-webhook-service.ts +182 -0
  134. package/src/services/scope-service.ts +75 -0
  135. package/src/services/typed-secret-service.test.ts +35 -0
  136. package/src/services/typed-secret-service.ts +39 -4
  137. package/src/services/webhook-service.ts +180 -0
  138. package/src/services/workflow-service.ts +7 -0
  139. package/src/types/core.types.ts +42 -14
  140. package/src/types/state.types.ts +23 -3
  141. package/src/utils/hmac.ts +27 -0
  142. package/src/utils/safe-fetch.test.ts +373 -0
  143. package/src/utils/safe-fetch.ts +213 -0
  144. package/src/wirings/ai-agent/ai-agent-agui.test.ts +1127 -0
  145. package/src/wirings/ai-agent/ai-agent-agui.ts +386 -0
  146. package/src/wirings/ai-agent/ai-agent-prepare.test.ts +308 -0
  147. package/src/wirings/ai-agent/ai-agent-prepare.ts +192 -8
  148. package/src/wirings/ai-agent/ai-agent-runner.test.ts +48 -1
  149. package/src/wirings/ai-agent/ai-agent-runner.ts +21 -0
  150. package/src/wirings/ai-agent/ai-agent-stream.test.ts +198 -10
  151. package/src/wirings/ai-agent/ai-agent-stream.ts +84 -10
  152. package/src/wirings/ai-agent/ai-agent.types.ts +28 -0
  153. package/src/wirings/ai-agent/index.ts +1 -0
  154. package/src/wirings/ai-agent/voice-input.ts +6 -35
  155. package/src/wirings/channel/channel-common.ts +0 -1
  156. package/src/wirings/channel/channel-handler.ts +1 -9
  157. package/src/wirings/channel/channel-middleware-runner.test.ts +28 -2
  158. package/src/wirings/channel/channel-middleware-runner.ts +39 -27
  159. package/src/wirings/channel/channel.types.ts +0 -6
  160. package/src/wirings/cli/cli-runner.ts +0 -2
  161. package/src/wirings/cli/cli.types.ts +0 -2
  162. package/src/wirings/http/http-routes.ts +0 -3
  163. package/src/wirings/http/http-runner.test.ts +1 -11
  164. package/src/wirings/http/http-runner.ts +19 -40
  165. package/src/wirings/http/http.types.ts +1 -10
  166. package/src/wirings/mcp/mcp-runner.ts +0 -2
  167. package/src/wirings/mcp/mcp.types.ts +3 -11
  168. package/src/wirings/oauth2/index.ts +0 -3
  169. package/src/wirings/rpc/addon-runner.ts +233 -0
  170. package/src/wirings/rpc/index.ts +17 -1
  171. package/src/wirings/rpc/remote-addon-auth.ts +69 -0
  172. package/src/wirings/rpc/rpc-runner.test.ts +319 -16
  173. package/src/wirings/rpc/rpc-runner.ts +155 -3
  174. package/src/wirings/rpc/rpc-types.ts +16 -5
  175. package/src/wirings/rpc/wire-addon.test.ts +6 -3
  176. package/src/wirings/rpc/wire-addon.ts +9 -0
  177. package/src/wirings/rpc/wire-remote-addon.ts +57 -0
  178. package/src/wirings/scope/index.ts +14 -0
  179. package/src/wirings/scope/scope.test.ts +135 -0
  180. package/src/wirings/scope/scope.types.ts +44 -0
  181. package/src/wirings/scope/validate-scope-definitions.ts +110 -0
  182. package/src/wirings/scope/wire-scope.ts +34 -0
  183. package/src/wirings/workflow/dsl/index.ts +4 -0
  184. package/src/wirings/workflow/dsl/workflow-dsl.types.ts +61 -0
  185. package/src/wirings/workflow/graph/graph-node.ts +2 -0
  186. package/src/wirings/workflow/graph/graph-runner.test.ts +58 -0
  187. package/src/wirings/workflow/graph/graph-runner.ts +3 -0
  188. package/src/wirings/workflow/graph/wire-workflow-graph.ts +7 -0
  189. package/src/wirings/workflow/graph/workflow-graph.types.ts +5 -0
  190. package/src/wirings/workflow/index.ts +5 -0
  191. package/src/wirings/workflow/pikku-workflow-service.test.ts +341 -0
  192. package/src/wirings/workflow/pikku-workflow-service.ts +236 -0
  193. package/src/wirings/workflow/workflow-step-session.test.ts +21 -5
  194. package/src/wirings/workflow/workflow.types.ts +4 -0
  195. package/tsconfig.tsbuildinfo +1 -1
  196. package/src/wirings/oauth2/oauth2-client.test.ts +0 -930
  197. package/src/wirings/oauth2/oauth2-client.ts +0 -331
  198. package/src/wirings/oauth2/oauth2-routes.ts +0 -234
@@ -6,10 +6,13 @@ import type {
6
6
  import type { SessionService } from '../../services/user-session-service.js'
7
7
  import type { CoreUserSession } from '../../types/core.types.js'
8
8
  import { runPikkuFunc } from '../../function/function-runner.js'
9
+ import type { AddonInstance } from './addon-runner.js'
10
+ import { addonInstanceForNamespace } from './addon-runner.js'
9
11
  import { pikkuState } from '../../pikku-state.js'
10
12
  import { PikkuError, addError } from '../../errors/error-handler.js'
11
13
  import type { PikkuRPC, ResolvedFunction } from './rpc-types.js'
12
14
  import { parseVersionedId } from '../../version.js'
15
+ import { resolveRemoteAddonToken } from './remote-addon-auth.js'
13
16
 
14
17
  export class RPCNotFoundError extends PikkuError {
15
18
  public readonly rpcName: string
@@ -23,11 +26,43 @@ addError(RPCNotFoundError, {
23
26
  mcpCode: -32601,
24
27
  message: 'RPC function not found.',
25
28
  })
29
+
30
+ /** A `wireRemoteAddon` namespace is missing a usable `serverUrl`. */
31
+ export class RemoteAddonConfigError extends PikkuError {
32
+ constructor(namespace: string, detail: string) {
33
+ super(`Remote addon '${namespace}' is misconfigured: ${detail}`)
34
+ }
35
+ }
36
+ addError(RemoteAddonConfigError, {
37
+ status: 500,
38
+ message: 'Remote addon is misconfigured.',
39
+ })
40
+
41
+ /** The hosted addon returned a non-2xx response for a remote RPC. */
42
+ export class RemoteAddonRequestError extends PikkuError {
43
+ public readonly httpStatus: number
44
+ constructor(
45
+ namespace: string,
46
+ fnName: string,
47
+ status: number,
48
+ detail: string
49
+ ) {
50
+ super(
51
+ `Remote addon '${namespace}:${fnName}' returned ${status}${detail ? `: ${detail}` : ''}`
52
+ )
53
+ this.httpStatus = status
54
+ }
55
+ }
56
+ addError(RemoteAddonRequestError, {
57
+ status: 502,
58
+ message: 'Remote addon request failed.',
59
+ })
26
60
  import type { AIAgentInput } from '../ai-agent/ai-agent.types.js'
27
61
  import type { AIStreamChannel } from '../ai-agent/ai-agent.types.js'
28
62
  import type { StreamAIAgentOptions } from '../ai-agent/ai-agent-prepare.js'
29
63
  import { runAIAgent, resumeAIAgentSync } from '../ai-agent/ai-agent-runner.js'
30
64
  import { streamAIAgent, resumeAIAgent } from '../ai-agent/ai-agent-stream.js'
65
+ import { wrapChannelWithAGUI } from '../ai-agent/ai-agent-agui.js'
31
66
 
32
67
  /**
33
68
  * Resolve a namespaced function reference to package and function names
@@ -164,6 +199,12 @@ export class ContextAwareRPCService {
164
199
  // 'namespace:func' boundary via invokeAddonFunction.
165
200
  try {
166
201
  const resolved = resolvePikkuFunction(funcName, this.packageName)
202
+ const addonInstance = resolved.packageName
203
+ ? addonInstanceForNamespace(
204
+ this.wire.addonNamespace,
205
+ resolved.packageName
206
+ )
207
+ : undefined
167
208
  return await runPikkuFunc<In, Out>(
168
209
  'rpc',
169
210
  funcName,
@@ -174,6 +215,7 @@ export class ContextAwareRPCService {
174
215
  data: () => data,
175
216
  wire: updatedWire,
176
217
  packageName: resolved.packageName,
218
+ addonInstance,
177
219
  }
178
220
  )
179
221
  } catch (e) {
@@ -213,6 +255,21 @@ export class ContextAwareRPCService {
213
255
  throw new RPCNotFoundError(namespacedFunction)
214
256
  }
215
257
 
258
+ const namespace = namespacedFunction.slice(
259
+ 0,
260
+ namespacedFunction.indexOf(':')
261
+ )
262
+
263
+ // wireRemoteAddon: the addon ships as a devDependency (types only) and its
264
+ // handlers run on the host — dispatch over HTTP, not through local meta.
265
+ if (resolved.addonConfig?.remote) {
266
+ return this.invokeRemoteAddonFunction<In, Out>(
267
+ namespace,
268
+ resolved.function,
269
+ data
270
+ )
271
+ }
272
+
216
273
  // Get the function meta from the addon package
217
274
  // Addon packages use function meta, not RPC meta
218
275
  const addonFunctionMeta = pikkuState(resolved.package, 'function', 'meta')
@@ -228,6 +285,15 @@ export class ContextAwareRPCService {
228
285
  ...(funcMeta.tags ?? []),
229
286
  ]
230
287
 
288
+ // The namespace is the consumer-facing wireAddon name; it selects the
289
+ // per-instance singleton services and secret/variable/credential overrides.
290
+ const addonInstance: AddonInstance = {
291
+ namespace,
292
+ secretOverrides: resolved.addonConfig?.secretOverrides,
293
+ variableOverrides: resolved.addonConfig?.variableOverrides,
294
+ credentialOverrides: resolved.addonConfig?.credentialOverrides,
295
+ }
296
+
231
297
  // Execute the function using runPikkuFunc with the addon package's state
232
298
  // We use the parent services (this.services) since addon packages share services
233
299
  // Pass the function's tags so tag-based middleware/permissions are applied
@@ -238,9 +304,78 @@ export class ContextAwareRPCService {
238
304
  wire,
239
305
  packageName: resolved.package,
240
306
  tags,
307
+ addonInstance,
241
308
  })
242
309
  }
243
310
 
311
+ /**
312
+ * Dispatch a `wireRemoteAddon` RPC over HTTP to the hosting service.
313
+ *
314
+ * The consumer sends the addon's own function name (not the namespaced form)
315
+ * to the host's `/remote/rpc/:rpcName` endpoint, authenticating as a client
316
+ * with the token bound in `wireRemoteAddon({ auth })`. The addon's handlers
317
+ * live on the host, so there is no local function meta to resolve.
318
+ */
319
+ private async invokeRemoteAddonFunction<In = any, Out = any>(
320
+ namespace: string,
321
+ fnName: string,
322
+ data: In
323
+ ): Promise<Out> {
324
+ const cfg = pikkuState(null, 'addons', 'packages').get(namespace)
325
+ if (!cfg?.remote) {
326
+ throw new RPCNotFoundError(`${namespace}:${fnName}`)
327
+ }
328
+
329
+ const serverUrl =
330
+ typeof cfg.serverUrl === 'function'
331
+ ? await cfg.serverUrl(this.services)
332
+ : cfg.serverUrl
333
+ if (!serverUrl) {
334
+ throw new RemoteAddonConfigError(namespace, 'serverUrl resolved empty')
335
+ }
336
+
337
+ const remoteFn = cfg.remoteName ? cfg.remoteName(fnName) : fnName
338
+ const token = await resolveRemoteAddonToken(
339
+ cfg.remoteAuth,
340
+ this.services,
341
+ this.wire,
342
+ namespace
343
+ )
344
+
345
+ const headers: Record<string, string> = {
346
+ 'content-type': 'application/json',
347
+ accept: 'application/json',
348
+ }
349
+ if (token) {
350
+ headers.authorization = `Bearer ${token}`
351
+ }
352
+ if (this.wire.traceId) {
353
+ headers['x-trace-id'] = this.wire.traceId
354
+ }
355
+
356
+ const base = serverUrl.replace(/\/+$/, '')
357
+ const res = await fetch(
358
+ `${base}/remote/rpc/${encodeURIComponent(remoteFn)}`,
359
+ {
360
+ method: 'POST',
361
+ headers,
362
+ body: JSON.stringify({ rpcName: remoteFn, data }),
363
+ }
364
+ )
365
+
366
+ if (!res.ok) {
367
+ // Best-effort body read to enrich the thrown error (mirrors postRpc).
368
+ const detail = (await res.text().catch(() => '')).slice(0, 300)
369
+ throw new RemoteAddonRequestError(namespace, remoteFn, res.status, detail)
370
+ }
371
+
372
+ if (res.status === 204) {
373
+ return undefined as Out
374
+ }
375
+ const text = await res.text()
376
+ return (text ? JSON.parse(text) : undefined) as Out
377
+ }
378
+
244
379
  public async rpcWithWire<In = any, Out = any>(
245
380
  rpcName: string,
246
381
  data: In,
@@ -257,12 +392,19 @@ export class ContextAwareRPCService {
257
392
 
258
393
  try {
259
394
  const resolved = resolvePikkuFunction(rpcName, this.packageName)
395
+ const addonInstance = resolved.packageName
396
+ ? addonInstanceForNamespace(
397
+ this.wire.addonNamespace,
398
+ resolved.packageName
399
+ )
400
+ : undefined
260
401
  return await runPikkuFunc<In, Out>('rpc', rpcName, resolved.pikkuFuncId, {
261
402
  auth: this.options.requiresAuth,
262
403
  singletonServices: this.services,
263
404
  data: () => data,
264
405
  wire: mergedWire,
265
406
  packageName: resolved.packageName,
407
+ addonInstance,
266
408
  })
267
409
  } catch (e) {
268
410
  if (e instanceof RPCNotFoundError && this.services.deploymentService) {
@@ -338,16 +480,26 @@ export class ContextAwareRPCService {
338
480
  ) => {
339
481
  const channel = this.wire.channel as unknown as AIStreamChannel
340
482
  if (!channel) throw new Error('No channel available for streaming')
483
+ let currentRunId: string | undefined
341
484
  await streamAIAgent(
342
485
  agentName,
343
486
  input,
344
- channel,
487
+ wrapChannelWithAGUI(channel, {
488
+ threadId: input.threadId,
489
+ getRunId: () => currentRunId,
490
+ }),
345
491
  {
346
492
  sessionService: this.options.sessionService,
347
493
  getCredential: this.wire.getCredential?.bind(this.wire),
348
494
  },
349
495
  undefined,
350
- options
496
+ {
497
+ ...options,
498
+ onRunCreated: (runId) => {
499
+ currentRunId = runId
500
+ options?.onRunCreated?.(runId)
501
+ },
502
+ }
351
503
  )
352
504
  },
353
505
  resume: async (
@@ -359,7 +511,7 @@ export class ContextAwareRPCService {
359
511
  if (!channel) throw new Error('No channel available for streaming')
360
512
  await resumeAIAgent(
361
513
  { runId, ...input },
362
- channel,
514
+ wrapChannelWithAGUI(channel, { runId }),
363
515
  {
364
516
  sessionService: this.options.sessionService,
365
517
  getCredential: this.wire.getCredential?.bind(this.wire),
@@ -1,9 +1,9 @@
1
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,
2
+ Invoke extends (...args: any[]) => any = (...args: any[]) => any,
3
+ Remote extends (...args: any[]) => any = (...args: any[]) => any,
4
+ startWorkflow extends (...args: any[]) => any = (...args: any[]) => any,
5
+ AgentRun extends (...args: any[]) => any = (...args: any[]) => any,
6
+ AgentStream extends (...args: any[]) => any = (...args: any[]) => any,
7
7
  > = {
8
8
  depth: number
9
9
  global: boolean
@@ -52,5 +52,16 @@ export interface ResolvedFunction {
52
52
  auth?: boolean
53
53
  tags?: string[]
54
54
  rpcEndpoint?: string
55
+ secretOverrides?: Record<string, string>
56
+ variableOverrides?: Record<string, string>
57
+ credentialOverrides?: Record<string, string>
58
+ /** Set by `wireRemoteAddon`: dispatch this namespace's RPCs over HTTP */
59
+ remote?: boolean
60
+ serverUrl?: string | ((services: any) => string | Promise<string>)
61
+ remoteAuth?:
62
+ | { credentialId: string }
63
+ | { secretId: string }
64
+ | { resolve: (services: any, wire: any) => string | Promise<string> }
65
+ remoteName?: (fn: string) => string
55
66
  }
56
67
  }
@@ -17,9 +17,9 @@ describe('wireAddon', () => {
17
17
  auth: true,
18
18
  mcp: true,
19
19
  tags: ['payments', 'billing'],
20
- secretOverrides: { apiKey: 'secretName' },
21
- variableOverrides: { region: 'eu-west-1' },
22
- credentialOverrides: { oauth: 'credentialName' },
20
+ secretOverrides: { apiKey: 'STRIPE_API_KEY' },
21
+ variableOverrides: { region: 'AWS_REGION' },
22
+ credentialOverrides: { oauth: 'stripeOAuth' },
23
23
  })
24
24
 
25
25
  assert.deepEqual(pikkuState(null, 'addons', 'packages').get('stripe'), {
@@ -27,6 +27,9 @@ describe('wireAddon', () => {
27
27
  rpcEndpoint: 'https://rpc.example.com',
28
28
  auth: true,
29
29
  tags: ['payments', 'billing'],
30
+ secretOverrides: { apiKey: 'STRIPE_API_KEY' },
31
+ variableOverrides: { region: 'AWS_REGION' },
32
+ credentialOverrides: { oauth: 'stripeOAuth' },
30
33
  })
31
34
  })
32
35
 
@@ -18,5 +18,14 @@ export const wireAddon = (config: WireAddonConfig): void => {
18
18
  rpcEndpoint: config.rpcEndpoint,
19
19
  auth: config.auth,
20
20
  tags: config.tags,
21
+ ...(config.secretOverrides
22
+ ? { secretOverrides: config.secretOverrides }
23
+ : {}),
24
+ ...(config.variableOverrides
25
+ ? { variableOverrides: config.variableOverrides }
26
+ : {}),
27
+ ...(config.credentialOverrides
28
+ ? { credentialOverrides: config.credentialOverrides }
29
+ : {}),
21
30
  })
22
31
  }
@@ -0,0 +1,57 @@
1
+ import { pikkuState } from '../../pikku-state.js'
2
+ import type { CoreServices, PikkuWire } from '../../types/core.types.js'
3
+
4
+ /**
5
+ * How the consumer supplies the token the hosted addon's auth expects.
6
+ *
7
+ * This is a CLIENT authenticating to a hosted library — NOT pikku's trusted
8
+ * machine-to-machine mesh (which uses `PIKKU_REMOTE_SECRET`). The consumer binds
9
+ * the addon's declared auth requirement to a local source:
10
+ * - `credentialId` — per-user credential, resolved via `wire.getCredential(id)`
11
+ * - `secretId` — platform key, resolved via the secrets service
12
+ * - `resolve` — custom escape hatch
13
+ * Omit entirely when the addon declares its remote surface is public.
14
+ *
15
+ * The value is sent `Authorization: Bearer <token>` by default; a non-default
16
+ * header is declared once by the addon (its meta), never chosen here.
17
+ */
18
+ export type RemoteAddonAuth =
19
+ | { credentialId: string }
20
+ | { secretId: string }
21
+ | { resolve: (services: CoreServices, wire: PikkuWire) => string | Promise<string> }
22
+
23
+ export type WireRemoteAddonConfig = {
24
+ /** Consumer-facing namespace, e.g. `registry` → `rpc('registry:getOpenApi')` */
25
+ name: string
26
+ /**
27
+ * The addon package. Installed as a **devDependency** (types only — its
28
+ * handlers run on the host); `pikku verify` enforces this.
29
+ */
30
+ package: string
31
+ /** Base URL of the host serving the addon's remote surface. */
32
+ serverUrl: string | ((services: CoreServices) => string | Promise<string>)
33
+ /** Bind the addon's declared auth to a local source. Omit if the surface is public. */
34
+ auth?: RemoteAddonAuth
35
+ /** Map a consumer-facing fn name → the remote fn name, when they differ (rare). */
36
+ remoteName?: (fn: string) => string
37
+ tags?: string[]
38
+ }
39
+
40
+ /**
41
+ * Consume a hosted addon's `remote: true` RPCs transparently over HTTP.
42
+ *
43
+ * Unlike `wireAddon` (which bundles the addon's functions in-process, a
44
+ * production dependency), `wireRemoteAddon` dispatches `rpc('name:fn')` to the
45
+ * host at `serverUrl`, fully typed — the addon ships as a devDependency (types
46
+ * only). See {@link RemoteAddonAuth} for how auth is bound.
47
+ */
48
+ export const wireRemoteAddon = (config: WireRemoteAddonConfig): void => {
49
+ pikkuState(null, 'addons', 'packages').set(config.name, {
50
+ package: config.package,
51
+ tags: config.tags,
52
+ remote: true,
53
+ serverUrl: config.serverUrl,
54
+ ...(config.auth ? { remoteAuth: config.auth } : {}),
55
+ ...(config.remoteName ? { remoteName: config.remoteName } : {}),
56
+ })
57
+ }
@@ -0,0 +1,14 @@
1
+ export { wireScope } from './wire-scope.js'
2
+ export {
3
+ flattenScopeDefinitions,
4
+ validateAndBuildScopeDefinitionsMeta,
5
+ } from './validate-scope-definitions.js'
6
+ export type {
7
+ CoreScopes,
8
+ CoreScopeNode,
9
+ FlatScope,
10
+ ScopeDefinitionMeta,
11
+ ScopeDefinitionsMeta,
12
+ ScopeDefinitions,
13
+ ScopeNodeMeta,
14
+ } from './scope.types.js'
@@ -0,0 +1,135 @@
1
+ import { describe, test } from 'node:test'
2
+ import * as assert from 'node:assert'
3
+ import {
4
+ flattenScopeDefinitions,
5
+ validateAndBuildScopeDefinitionsMeta,
6
+ } from './validate-scope-definitions.js'
7
+ import type { ScopeDefinitions } from './scope.types.js'
8
+
9
+ describe('flattenScopeDefinitions', () => {
10
+ test('a flat scope yields itself', () => {
11
+ const definitions: ScopeDefinitions = [
12
+ { name: 'admin', description: 'Administration' },
13
+ ]
14
+
15
+ assert.deepEqual(flattenScopeDefinitions(definitions), [
16
+ { id: 'admin', description: 'Administration' },
17
+ ])
18
+ })
19
+
20
+ test('nested scopes yield every intermediate level', () => {
21
+ const definitions: ScopeDefinitions = [
22
+ {
23
+ name: 'admin',
24
+ scopes: {
25
+ invoices: {
26
+ description: 'Invoice management',
27
+ scopes: { create: { description: 'Create invoices' } },
28
+ },
29
+ },
30
+ },
31
+ ]
32
+
33
+ assert.deepEqual(flattenScopeDefinitions(definitions), [
34
+ { id: 'admin', description: undefined },
35
+ { id: 'admin:invoices', description: 'Invoice management' },
36
+ { id: 'admin:invoices:create', description: 'Create invoices' },
37
+ ])
38
+ })
39
+
40
+ test('siblings are flattened depth-first', () => {
41
+ const definitions: ScopeDefinitions = [
42
+ {
43
+ name: 'billing',
44
+ scopes: { read: {}, write: {} },
45
+ },
46
+ ]
47
+
48
+ assert.deepEqual(
49
+ flattenScopeDefinitions(definitions).map((s) => s.id),
50
+ ['billing', 'billing:read', 'billing:write']
51
+ )
52
+ })
53
+
54
+ test('several definitions are flattened together', () => {
55
+ const definitions: ScopeDefinitions = [
56
+ { name: 'admin', scopes: { users: {} } },
57
+ { name: 'billing' },
58
+ ]
59
+
60
+ assert.deepEqual(
61
+ flattenScopeDefinitions(definitions).map((s) => s.id),
62
+ ['admin', 'admin:users', 'billing']
63
+ )
64
+ })
65
+ })
66
+
67
+ describe('validateAndBuildScopeDefinitionsMeta', () => {
68
+ test('keys definitions by name', () => {
69
+ const meta = validateAndBuildScopeDefinitionsMeta([
70
+ { name: 'admin', description: 'Administration' },
71
+ ])
72
+
73
+ assert.equal(meta.admin!.name, 'admin')
74
+ assert.equal(meta.admin!.description, 'Administration')
75
+ })
76
+
77
+ test('an identical duplicate is tolerated', () => {
78
+ const meta = validateAndBuildScopeDefinitionsMeta([
79
+ { name: 'admin', scopes: { users: {} } },
80
+ { name: 'admin', scopes: { users: {} } },
81
+ ])
82
+
83
+ assert.equal(Object.keys(meta).length, 1)
84
+ })
85
+
86
+ test('a conflicting duplicate throws and names both files', () => {
87
+ assert.throws(
88
+ () =>
89
+ validateAndBuildScopeDefinitionsMeta([
90
+ { name: 'admin', scopes: { users: {} }, sourceFile: 'a.ts' },
91
+ { name: 'admin', scopes: { invoices: {} }, sourceFile: 'b.ts' },
92
+ ]),
93
+ (err: Error) => {
94
+ assert.match(err.message, /admin/)
95
+ assert.match(err.message, /a\.ts/)
96
+ assert.match(err.message, /b\.ts/)
97
+ return true
98
+ }
99
+ )
100
+ })
101
+
102
+ test('rejects a name containing the separator', () => {
103
+ assert.throws(
104
+ () => validateAndBuildScopeDefinitionsMeta([{ name: 'admin:users' }]),
105
+ /separator/i
106
+ )
107
+ })
108
+
109
+ test('rejects a nested key containing the separator', () => {
110
+ assert.throws(
111
+ () =>
112
+ validateAndBuildScopeDefinitionsMeta([
113
+ { name: 'admin', scopes: { 'a:b': {} } },
114
+ ]),
115
+ /separator/i
116
+ )
117
+ })
118
+
119
+ test('rejects a wildcard as a declared name', () => {
120
+ assert.throws(
121
+ () => validateAndBuildScopeDefinitionsMeta([{ name: '*' }]),
122
+ /wildcard/i
123
+ )
124
+ })
125
+
126
+ test('rejects a wildcard nested inside a scope', () => {
127
+ assert.throws(
128
+ () =>
129
+ validateAndBuildScopeDefinitionsMeta([
130
+ { name: 'admin', scopes: { '*': {} } },
131
+ ]),
132
+ /wildcard/i
133
+ )
134
+ })
135
+ })
@@ -0,0 +1,44 @@
1
+ /**
2
+ * A node within a scope tree. Every node is itself a grantable scope — given
3
+ * `admin` containing `invoices` containing `create`, all three of `admin`,
4
+ * `admin:invoices` and `admin:invoices:create` are valid scope ids.
5
+ */
6
+ export type CoreScopeNode = {
7
+ /** Short human-readable name, e.g. "Administration". */
8
+ displayName?: string
9
+ /** Longer-form description, surfaced in the console when granting. */
10
+ description?: string
11
+ /** Nested scopes, keyed by their segment. */
12
+ scopes?: Record<string, CoreScopeNode>
13
+ }
14
+
15
+ /**
16
+ * Scope trees to declare, keyed by their root segment. A root is just a node —
17
+ * it is named by its key, exactly like every node beneath it. A key must not
18
+ * contain `:` or be `*`.
19
+ */
20
+ export type CoreScopes = Record<string, CoreScopeNode>
21
+
22
+ export type ScopeNodeMeta = {
23
+ displayName?: string
24
+ description?: string
25
+ scopes?: Record<string, ScopeNodeMeta>
26
+ }
27
+
28
+ export type ScopeDefinitionMeta = {
29
+ name: string
30
+ displayName?: string
31
+ description?: string
32
+ scopes?: Record<string, ScopeNodeMeta>
33
+ sourceFile?: string
34
+ }
35
+
36
+ export type ScopeDefinitions = ScopeDefinitionMeta[]
37
+ export type ScopeDefinitionsMeta = Record<string, ScopeDefinitionMeta>
38
+
39
+ /** A single scope, flattened out of the declared tree. */
40
+ export type FlatScope = {
41
+ /** Colon-delimited id, e.g. `admin:invoices:create`. */
42
+ id: string
43
+ description?: string
44
+ }
@@ -0,0 +1,110 @@
1
+ import type {
2
+ FlatScope,
3
+ ScopeDefinitions,
4
+ ScopeDefinitionsMeta,
5
+ ScopeNodeMeta,
6
+ } from './scope.types.js'
7
+
8
+ const SEPARATOR = ':'
9
+ const WILDCARD = '*'
10
+
11
+ const assertSegment = (segment: string, scopeName: string): void => {
12
+ if (segment.includes(SEPARATOR)) {
13
+ throw new Error(
14
+ `Scope segment '${segment}' in '${scopeName}' contains the '${SEPARATOR}' separator. ` +
15
+ `Nest scopes with the 'scopes' property instead of embedding '${SEPARATOR}' in a name.`
16
+ )
17
+ }
18
+ if (segment === WILDCARD) {
19
+ throw new Error(
20
+ `Scope segment '${segment}' in '${scopeName}' is the wildcard. ` +
21
+ `'${WILDCARD}' is reserved for granting a scope and its descendants, and cannot be declared.`
22
+ )
23
+ }
24
+ if (segment.length === 0) {
25
+ throw new Error(`Scope '${scopeName}' contains an empty segment.`)
26
+ }
27
+ }
28
+
29
+ const assertNodesValid = (
30
+ nodes: Record<string, ScopeNodeMeta> | undefined,
31
+ scopeName: string
32
+ ): void => {
33
+ for (const [segment, node] of Object.entries(nodes ?? {})) {
34
+ assertSegment(segment, scopeName)
35
+ assertNodesValid(node.scopes, scopeName)
36
+ }
37
+ }
38
+
39
+ const flattenNodes = (
40
+ nodes: Record<string, ScopeNodeMeta> | undefined,
41
+ prefix: string,
42
+ out: FlatScope[]
43
+ ): void => {
44
+ for (const [segment, node] of Object.entries(nodes ?? {})) {
45
+ const id = `${prefix}${SEPARATOR}${segment}`
46
+ out.push({ id, description: node.description })
47
+ flattenNodes(node.scopes, id, out)
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Flattens declared scope trees into the full list of grantable scope ids,
53
+ * depth-first. Every node is emitted, including intermediate ones.
54
+ *
55
+ * Used by codegen to build the `ScopeId` union, and by a ScopeService to sync
56
+ * the declared set into its store.
57
+ */
58
+ export const flattenScopeDefinitions = (
59
+ definitions: ScopeDefinitions
60
+ ): FlatScope[] => {
61
+ const out: FlatScope[] = []
62
+ for (const def of definitions) {
63
+ out.push({ id: def.name, description: def.description })
64
+ flattenNodes(def.scopes, def.name, out)
65
+ }
66
+ return out
67
+ }
68
+
69
+ /**
70
+ * Validates declared scopes and keys them by name.
71
+ *
72
+ * Definitions sharing a name must be identical; a conflicting redeclaration is
73
+ * a hard error naming both source files.
74
+ */
75
+ export function validateAndBuildScopeDefinitionsMeta(
76
+ definitions: ScopeDefinitions
77
+ ): ScopeDefinitionsMeta {
78
+ const meta: ScopeDefinitionsMeta = {}
79
+
80
+ for (const def of definitions) {
81
+ assertSegment(def.name, def.name)
82
+ assertNodesValid(def.scopes, def.name)
83
+
84
+ const existing = meta[def.name]
85
+ if (existing) {
86
+ const sameShape =
87
+ JSON.stringify(existing.scopes ?? {}) ===
88
+ JSON.stringify(def.scopes ?? {})
89
+ if (!sameShape) {
90
+ throw new Error(
91
+ `Scope '${def.name}' is declared with different nested scopes.\n` +
92
+ ` First declaration: ${existing.sourceFile ?? 'unknown'}\n` +
93
+ ` Second declaration: ${def.sourceFile ?? 'unknown'}\n` +
94
+ `Scopes sharing a name must declare the same tree.`
95
+ )
96
+ }
97
+ continue
98
+ }
99
+
100
+ meta[def.name] = {
101
+ name: def.name,
102
+ displayName: def.displayName,
103
+ description: def.description,
104
+ scopes: def.scopes,
105
+ sourceFile: def.sourceFile,
106
+ }
107
+ }
108
+
109
+ return meta
110
+ }