@lushly-dev/afd-server 2.4.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (279) hide show
  1. package/README.md +239 -56
  2. package/dist/admission.d.ts +137 -0
  3. package/dist/admission.d.ts.map +1 -0
  4. package/dist/admission.js +144 -0
  5. package/dist/admission.js.map +1 -0
  6. package/dist/app-call-registry.d.ts +31 -0
  7. package/dist/app-call-registry.d.ts.map +1 -0
  8. package/dist/app-call-registry.js +64 -0
  9. package/dist/app-call-registry.js.map +1 -0
  10. package/dist/approval-admission.d.ts +139 -0
  11. package/dist/approval-admission.d.ts.map +1 -0
  12. package/dist/approval-admission.js +252 -0
  13. package/dist/approval-admission.js.map +1 -0
  14. package/dist/approval-crypto.d.ts +37 -17
  15. package/dist/approval-crypto.d.ts.map +1 -1
  16. package/dist/approval-crypto.js +84 -75
  17. package/dist/approval-crypto.js.map +1 -1
  18. package/dist/approval-process-state.d.ts +21 -0
  19. package/dist/approval-process-state.d.ts.map +1 -0
  20. package/dist/approval-process-state.js +29 -0
  21. package/dist/approval-process-state.js.map +1 -0
  22. package/dist/approval-relay.d.ts +192 -9
  23. package/dist/approval-relay.d.ts.map +1 -1
  24. package/dist/approval-relay.js +45 -9
  25. package/dist/approval-relay.js.map +1 -1
  26. package/dist/approval-up-front.d.ts +40 -0
  27. package/dist/approval-up-front.d.ts.map +1 -0
  28. package/dist/approval-up-front.js +409 -0
  29. package/dist/approval-up-front.js.map +1 -0
  30. package/dist/approval.d.ts +65 -12
  31. package/dist/approval.d.ts.map +1 -1
  32. package/dist/approval.js +311 -47
  33. package/dist/approval.js.map +1 -1
  34. package/dist/approver.d.ts +84 -0
  35. package/dist/approver.d.ts.map +1 -0
  36. package/dist/approver.js +107 -0
  37. package/dist/approver.js.map +1 -0
  38. package/dist/auth-middleware-check.d.ts +23 -0
  39. package/dist/auth-middleware-check.d.ts.map +1 -0
  40. package/dist/auth-middleware-check.js +48 -0
  41. package/dist/auth-middleware-check.js.map +1 -0
  42. package/dist/bootstrap/afd-context.d.ts.map +1 -1
  43. package/dist/bootstrap/afd-context.js +27 -3
  44. package/dist/bootstrap/afd-context.js.map +1 -1
  45. package/dist/bootstrap/afd-docs.d.ts.map +1 -1
  46. package/dist/bootstrap/afd-docs.js +4 -0
  47. package/dist/bootstrap/afd-docs.js.map +1 -1
  48. package/dist/bootstrap/afd-help.d.ts +4 -1
  49. package/dist/bootstrap/afd-help.d.ts.map +1 -1
  50. package/dist/bootstrap/afd-help.js +19 -4
  51. package/dist/bootstrap/afd-help.js.map +1 -1
  52. package/dist/bootstrap/described-command.d.ts +3 -1
  53. package/dist/bootstrap/described-command.d.ts.map +1 -1
  54. package/dist/bootstrap/described-command.js.map +1 -1
  55. package/dist/command-allow.d.ts +4 -4
  56. package/dist/command-allow.d.ts.map +1 -1
  57. package/dist/command-allow.js +10 -15
  58. package/dist/command-allow.js.map +1 -1
  59. package/dist/command-routing.d.ts +56 -8
  60. package/dist/command-routing.d.ts.map +1 -1
  61. package/dist/command-routing.js +82 -16
  62. package/dist/command-routing.js.map +1 -1
  63. package/dist/command-timeout.d.ts +17 -0
  64. package/dist/command-timeout.d.ts.map +1 -0
  65. package/dist/command-timeout.js +99 -0
  66. package/dist/command-timeout.js.map +1 -0
  67. package/dist/confirmation-gate.d.ts +7 -7
  68. package/dist/confirmation-gate.d.ts.map +1 -1
  69. package/dist/confirmation-gate.js +26 -12
  70. package/dist/confirmation-gate.js.map +1 -1
  71. package/dist/confirmations.d.ts +28 -3
  72. package/dist/confirmations.d.ts.map +1 -1
  73. package/dist/confirmations.js +6 -7
  74. package/dist/confirmations.js.map +1 -1
  75. package/dist/context-fields.d.ts +16 -1
  76. package/dist/context-fields.d.ts.map +1 -1
  77. package/dist/context-fields.js +30 -13
  78. package/dist/context-fields.js.map +1 -1
  79. package/dist/context-scope.d.ts +3 -3
  80. package/dist/context-scope.d.ts.map +1 -1
  81. package/dist/context-scope.js +7 -8
  82. package/dist/context-scope.js.map +1 -1
  83. package/dist/define-core.d.ts +63 -1
  84. package/dist/define-core.d.ts.map +1 -1
  85. package/dist/define-core.js +27 -4
  86. package/dist/define-core.js.map +1 -1
  87. package/dist/define.d.ts +4 -3
  88. package/dist/define.d.ts.map +1 -1
  89. package/dist/define.js +1 -1
  90. package/dist/define.js.map +1 -1
  91. package/dist/direct-registry.d.ts +54 -8
  92. package/dist/direct-registry.d.ts.map +1 -1
  93. package/dist/direct-registry.js +64 -8
  94. package/dist/direct-registry.js.map +1 -1
  95. package/dist/direct-tool-surface.d.ts +1 -1
  96. package/dist/direct-tool-surface.d.ts.map +1 -1
  97. package/dist/direct-tool-surface.js +28 -6
  98. package/dist/direct-tool-surface.js.map +1 -1
  99. package/dist/direct.d.ts +6 -2
  100. package/dist/direct.d.ts.map +1 -1
  101. package/dist/direct.js +4 -1
  102. package/dist/direct.js.map +1 -1
  103. package/dist/elicitation-state.d.ts +5 -1
  104. package/dist/elicitation-state.d.ts.map +1 -1
  105. package/dist/elicitation-state.js +4 -1
  106. package/dist/elicitation-state.js.map +1 -1
  107. package/dist/execution.d.ts +52 -6
  108. package/dist/execution.d.ts.map +1 -1
  109. package/dist/execution.js +183 -19
  110. package/dist/execution.js.map +1 -1
  111. package/dist/host-state.d.ts +81 -0
  112. package/dist/host-state.d.ts.map +1 -0
  113. package/dist/host-state.js +62 -0
  114. package/dist/host-state.js.map +1 -0
  115. package/dist/http-handler.d.ts +48 -11
  116. package/dist/http-handler.d.ts.map +1 -1
  117. package/dist/http-handler.js +207 -58
  118. package/dist/http-handler.js.map +1 -1
  119. package/dist/http-security.d.ts +14 -1
  120. package/dist/http-security.d.ts.map +1 -1
  121. package/dist/http-security.js +79 -17
  122. package/dist/http-security.js.map +1 -1
  123. package/dist/http-sessions.d.ts +45 -5
  124. package/dist/http-sessions.d.ts.map +1 -1
  125. package/dist/http-sessions.js +52 -10
  126. package/dist/http-sessions.js.map +1 -1
  127. package/dist/http-sse.d.ts +9 -0
  128. package/dist/http-sse.d.ts.map +1 -1
  129. package/dist/http-sse.js +12 -3
  130. package/dist/http-sse.js.map +1 -1
  131. package/dist/human-relay.d.ts +195 -0
  132. package/dist/human-relay.d.ts.map +1 -0
  133. package/dist/human-relay.js +670 -0
  134. package/dist/human-relay.js.map +1 -0
  135. package/dist/idempotency-step.d.ts +231 -0
  136. package/dist/idempotency-step.d.ts.map +1 -0
  137. package/dist/idempotency-step.js +389 -0
  138. package/dist/idempotency-step.js.map +1 -0
  139. package/dist/idempotency-store.d.ts +107 -0
  140. package/dist/idempotency-store.d.ts.map +1 -0
  141. package/dist/idempotency-store.js +100 -0
  142. package/dist/idempotency-store.js.map +1 -0
  143. package/dist/index.d.ts +18 -8
  144. package/dist/index.d.ts.map +1 -1
  145. package/dist/index.js +15 -4
  146. package/dist/index.js.map +1 -1
  147. package/dist/instance-state.d.ts +53 -0
  148. package/dist/instance-state.d.ts.map +1 -0
  149. package/dist/instance-state.js +94 -0
  150. package/dist/instance-state.js.map +1 -0
  151. package/dist/journal-off-tape.d.ts +52 -0
  152. package/dist/journal-off-tape.d.ts.map +1 -0
  153. package/dist/journal-off-tape.js +150 -0
  154. package/dist/journal-off-tape.js.map +1 -0
  155. package/dist/journal-replay.d.ts.map +1 -1
  156. package/dist/journal-replay.js +48 -3
  157. package/dist/journal-replay.js.map +1 -1
  158. package/dist/journal-view.d.ts +50 -0
  159. package/dist/journal-view.d.ts.map +1 -0
  160. package/dist/journal-view.js +214 -0
  161. package/dist/journal-view.js.map +1 -0
  162. package/dist/journal.d.ts +34 -6
  163. package/dist/journal.d.ts.map +1 -1
  164. package/dist/journal.js +183 -47
  165. package/dist/journal.js.map +1 -1
  166. package/dist/json-rpc.d.ts +3 -2
  167. package/dist/json-rpc.d.ts.map +1 -1
  168. package/dist/json-rpc.js +5 -3
  169. package/dist/json-rpc.js.map +1 -1
  170. package/dist/lazy-tools.d.ts +27 -2
  171. package/dist/lazy-tools.d.ts.map +1 -1
  172. package/dist/lazy-tools.js +22 -4
  173. package/dist/lazy-tools.js.map +1 -1
  174. package/dist/mcp-auth-bearer.d.ts +2 -0
  175. package/dist/mcp-auth-bearer.d.ts.map +1 -1
  176. package/dist/mcp-auth-bearer.js +1 -1
  177. package/dist/mcp-auth-bearer.js.map +1 -1
  178. package/dist/mcp-auth-dispatch.d.ts +38 -0
  179. package/dist/mcp-auth-dispatch.d.ts.map +1 -0
  180. package/dist/mcp-auth-dispatch.js +113 -0
  181. package/dist/mcp-auth-dispatch.js.map +1 -0
  182. package/dist/mcp-auth-jwt.d.ts +3 -2
  183. package/dist/mcp-auth-jwt.d.ts.map +1 -1
  184. package/dist/mcp-auth-jwt.js.map +1 -1
  185. package/dist/mcp-auth-resource.d.ts +6 -2
  186. package/dist/mcp-auth-resource.d.ts.map +1 -1
  187. package/dist/mcp-auth-resource.js +6 -5
  188. package/dist/mcp-auth-resource.js.map +1 -1
  189. package/dist/mcp-auth-scopes.d.ts +84 -0
  190. package/dist/mcp-auth-scopes.d.ts.map +1 -0
  191. package/dist/mcp-auth-scopes.js +125 -0
  192. package/dist/mcp-auth-scopes.js.map +1 -0
  193. package/dist/mcp-auth-view.d.ts +50 -0
  194. package/dist/mcp-auth-view.d.ts.map +1 -0
  195. package/dist/mcp-auth-view.js +179 -0
  196. package/dist/mcp-auth-view.js.map +1 -0
  197. package/dist/mcp-auth.d.ts +94 -3
  198. package/dist/mcp-auth.d.ts.map +1 -1
  199. package/dist/mcp-auth.js +241 -23
  200. package/dist/mcp-auth.js.map +1 -1
  201. package/dist/mcp-modern.d.ts +80 -6
  202. package/dist/mcp-modern.d.ts.map +1 -1
  203. package/dist/mcp-modern.js +378 -91
  204. package/dist/mcp-modern.js.map +1 -1
  205. package/dist/meta-argument.d.ts +9 -8
  206. package/dist/meta-argument.d.ts.map +1 -1
  207. package/dist/meta-argument.js +28 -10
  208. package/dist/meta-argument.js.map +1 -1
  209. package/dist/meta-tools.js +2 -2
  210. package/dist/meta-tools.js.map +1 -1
  211. package/dist/middleware.d.ts +16 -1
  212. package/dist/middleware.d.ts.map +1 -1
  213. package/dist/middleware.js +57 -10
  214. package/dist/middleware.js.map +1 -1
  215. package/dist/output-budget.d.ts +77 -0
  216. package/dist/output-budget.d.ts.map +1 -0
  217. package/dist/output-budget.js +202 -0
  218. package/dist/output-budget.js.map +1 -0
  219. package/dist/principal-binding.js +1 -1
  220. package/dist/principal-binding.js.map +1 -1
  221. package/dist/relay-evidence.d.ts +74 -0
  222. package/dist/relay-evidence.d.ts.map +1 -0
  223. package/dist/relay-evidence.js +143 -0
  224. package/dist/relay-evidence.js.map +1 -0
  225. package/dist/resources.d.ts +35 -10
  226. package/dist/resources.d.ts.map +1 -1
  227. package/dist/resources.js +59 -18
  228. package/dist/resources.js.map +1 -1
  229. package/dist/schema.d.ts +4 -3
  230. package/dist/schema.d.ts.map +1 -1
  231. package/dist/schema.js +3 -3
  232. package/dist/schema.js.map +1 -1
  233. package/dist/scope-view.d.ts +65 -0
  234. package/dist/scope-view.d.ts.map +1 -0
  235. package/dist/scope-view.js +45 -0
  236. package/dist/scope-view.js.map +1 -0
  237. package/dist/server-types.d.ts +182 -27
  238. package/dist/server-types.d.ts.map +1 -1
  239. package/dist/server-types.js.map +1 -1
  240. package/dist/server.d.ts +26 -0
  241. package/dist/server.d.ts.map +1 -1
  242. package/dist/server.js +403 -61
  243. package/dist/server.js.map +1 -1
  244. package/dist/skills.d.ts.map +1 -1
  245. package/dist/skills.js +99 -3
  246. package/dist/skills.js.map +1 -1
  247. package/dist/task-dispatch.d.ts +35 -0
  248. package/dist/task-dispatch.d.ts.map +1 -0
  249. package/dist/task-dispatch.js +25 -0
  250. package/dist/task-dispatch.js.map +1 -0
  251. package/dist/tasks-runtime.d.ts +115 -0
  252. package/dist/tasks-runtime.d.ts.map +1 -0
  253. package/dist/tasks-runtime.js +760 -0
  254. package/dist/tasks-runtime.js.map +1 -0
  255. package/dist/tasks.d.ts +237 -0
  256. package/dist/tasks.d.ts.map +1 -0
  257. package/dist/tasks.js +321 -0
  258. package/dist/tasks.js.map +1 -0
  259. package/dist/tool-router.d.ts +68 -3
  260. package/dist/tool-router.d.ts.map +1 -1
  261. package/dist/tool-router.js +293 -70
  262. package/dist/tool-router.js.map +1 -1
  263. package/dist/tools.d.ts +32 -0
  264. package/dist/tools.d.ts.map +1 -1
  265. package/dist/tools.js +162 -55
  266. package/dist/tools.js.map +1 -1
  267. package/dist/up-front-steps.d.ts +35 -0
  268. package/dist/up-front-steps.d.ts.map +1 -0
  269. package/dist/up-front-steps.js +159 -0
  270. package/dist/up-front-steps.js.map +1 -0
  271. package/dist/validation.d.ts +38 -3
  272. package/dist/validation.d.ts.map +1 -1
  273. package/dist/validation.js +118 -23
  274. package/dist/validation.js.map +1 -1
  275. package/dist/verified-credentials.d.ts +13 -5
  276. package/dist/verified-credentials.d.ts.map +1 -1
  277. package/dist/verified-credentials.js +43 -10
  278. package/dist/verified-credentials.js.map +1 -1
  279. package/package.json +15 -11
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @fileoverview What an approval middleware keeps or needs per process, marked on the middleware
3
+ * itself under a registered symbol, so a server built from one installed copy of this package
4
+ * reads it from a middleware made by another copy, and `composeMiddleware` carries its parts'.
5
+ */
6
+ const MARK = Symbol.for('dev.afd/approval-process-state');
7
+ /** Mark `middleware` with what it keeps per process: non-writable, non-enumerable, permanent. */
8
+ export function markApprovalProcessState(middleware, state) {
9
+ Object.defineProperty(middleware, MARK, { value: state });
10
+ }
11
+ /** The per-process state marked on `middleware` (its own property), if any. */
12
+ export function approvalProcessState(middleware) {
13
+ return typeof middleware === 'function' && Object.hasOwn(middleware, MARK)
14
+ ? middleware[MARK]
15
+ : undefined;
16
+ }
17
+ /** Mark `composed` with what any of `parts` keeps per process; nothing when none is marked. */
18
+ export function carryApprovalProcessState(composed, parts) {
19
+ const states = parts.map(approvalProcessState);
20
+ const any = (field) => states.some((state) => state?.[field]);
21
+ if (states.some(Boolean)) {
22
+ markApprovalProcessState(composed, {
23
+ relay: any('relay'),
24
+ perProcessNonces: any('perProcessNonces'),
25
+ perProcessRelayNonces: any('perProcessRelayNonces'),
26
+ });
27
+ }
28
+ }
29
+ //# sourceMappingURL=approval-process-state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"approval-process-state.js","sourceRoot":"","sources":["../src/approval-process-state.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAYH,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,CAAC,gCAAgC,CAAC,CAAC;AAE1D,iGAAiG;AACjG,MAAM,UAAU,wBAAwB,CAAC,UAAkB,EAAE,KAA2B;IACvF,MAAM,CAAC,cAAc,CAAC,UAAU,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;AAC3D,CAAC;AAED,+EAA+E;AAC/E,MAAM,UAAU,oBAAoB,CAAC,UAAmB;IACvD,OAAO,OAAO,UAAU,KAAK,UAAU,IAAI,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,IAAI,CAAC;QACzE,CAAC,CAAE,UAA8D,CAAC,IAAI,CAAC;QACvE,CAAC,CAAC,SAAS,CAAC;AACd,CAAC;AAED,+FAA+F;AAC/F,MAAM,UAAU,yBAAyB,CAAC,QAAgB,EAAE,KAAyB;IACpF,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAC;IAC/C,MAAM,GAAG,GAAG,CAAC,KAAiC,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAC1F,IAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;QAC1B,wBAAwB,CAAC,QAAQ,EAAE;YAClC,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC;YACnB,gBAAgB,EAAE,GAAG,CAAC,kBAAkB,CAAC;YACzC,qBAAqB,EAAE,GAAG,CAAC,uBAAuB,CAAC;SACnD,CAAC,CAAC;IACJ,CAAC;AACF,CAAC"}
@@ -3,16 +3,20 @@
3
3
  * an MCP client, through a form elicitation, instead of returning a token to the agent.
4
4
  *
5
5
  * The MCP lane binds an {@link ApprovalRelay} to the context of each `tools/call` that can carry a
6
- * form elicitation, under a private symbol (as the confirmation channel is), so neither
6
+ * form elicitation, in its host state (`host-state.ts`, as the confirmation channel is), so neither
7
7
  * `createContext` nor tool arguments can forge one. The tool router names the one command the
8
8
  * call routes to; with `relay: 'mcp-elicitation'`, the approval middleware of that command reads
9
9
  * the human's answer from the relay or records a hold on it, and the lane then answers the call
10
10
  * with `input_required` instead of the held result.
11
11
  *
12
12
  * `afd-batch` and `afd-pipe` replace the relay with one that never asks, because asking mid-run
13
- * would run the earlier steps again on the retry.
13
+ * would run the earlier steps again on the retry. Instead (#459), the router binds an
14
+ * {@link UpFrontApproval} for the whole call: before any step runs, the approval middleware works
15
+ * out every gated step's input and asks once for all of them, or refuses the call when a gated
16
+ * pipeline step's input comes from an earlier step.
14
17
  */
15
- import type { CommandContext } from '@lushly-dev/afd-core';
18
+ import type { BatchRequest, Clock, CommandContext, CommandError, CommandResult, PipelineRequest } from '@lushly-dev/afd-core';
19
+ import type { ZodCommandDefinition } from './schema.js';
16
20
  /** The `inputRequests` key of the approval elicitation. */
17
21
  export declare const APPROVAL_INPUT_KEY = "approval";
18
22
  /** The approval elicitation form: one required boolean. */
@@ -27,8 +31,22 @@ export declare const APPROVAL_REQUESTED_SCHEMA: {
27
31
  };
28
32
  readonly required: readonly ['approve'];
29
33
  };
30
- /** A call held for the human's approval, as recorded by the middleware. */
31
- export interface ApprovalHold {
34
+ /**
35
+ * The step of an `afd-batch` or `afd-pipe` an up-front approval was asked for, kept with the
36
+ * question in the signed `requestState` (#1414). The request that brings the answer runs the step
37
+ * on this input instead of validating what was sent again, so a schema whose defaults differ from
38
+ * one validation to the next (a random id, the time) still runs exactly what the human approved.
39
+ */
40
+ export interface ApprovedStep {
41
+ /** The step's position in the request, 0-based. */
42
+ index: number;
43
+ /** The canonical JSON of the step's validated input, as the human was shown it. */
44
+ input: string;
45
+ /** A digest of the whole request as sent: the input serves that request and no other. */
46
+ call: string;
47
+ }
48
+ /** One approval asked of the human: a command, its validated input and the actor. */
49
+ export interface ApprovalQuestion {
32
50
  /** Keyed digest of the command, its validated input and the actor, under the signing key. */
33
51
  binding: string;
34
52
  /** The same digest under every verification key: earlier answers to any of them are dropped. */
@@ -39,21 +57,57 @@ export interface ApprovalHold {
39
57
  expiresAt: number;
40
58
  /** MAC of the binding, nonce and expiry under the approval secret. */
41
59
  mac: string;
60
+ /** For an up-front approval, the step asked about (left out when its input is too long to keep). */
61
+ step?: ApprovedStep;
62
+ }
63
+ /** A call held for the human's approval, as recorded by the middleware. */
64
+ export interface ApprovalHold extends ApprovalQuestion {
42
65
  /** The question shown to the human. */
43
66
  message: string;
44
67
  }
45
- /** The human's answer to an earlier approval elicitation for one binding. */
46
- export type ApprovalAnswer = {
47
- kind: 'accept';
68
+ /**
69
+ * A whole `afd-batch` or `afd-pipe` held for the human's approval: one elicitation, one
70
+ * question per gated step, each answered by the same form.
71
+ */
72
+ export interface StepsHold {
73
+ questions: readonly ApprovalQuestion[];
74
+ /** The question shown to the human, listing every gated step. */
75
+ message: string;
76
+ }
77
+ /** An accepted approval as the request carries it: its binding, nonce, expiry and MAC. */
78
+ export interface AcceptedApproval {
48
79
  binding: string;
49
80
  nonce: string;
50
81
  expiresAt: number;
51
82
  mac: string;
52
- } | {
83
+ /** The step the approval was asked for, when the question kept it. */
84
+ step?: ApprovedStep;
85
+ }
86
+ /**
87
+ * The evidence an approval step admitted a held call on, from which it builds the call's approval
88
+ * summary (`spec/approvals.md`, "The approval summary"): how it was admitted, the
89
+ * evidence's single-use key, and the times the evidence carries.
90
+ */
91
+ export interface ApprovalAdmission {
92
+ via: 'token' | 'elicitation' | 'relay' | 'direct';
93
+ /** The key the step spends in its nonce store, or would spend when reuse is allowed. */
94
+ key: string;
95
+ issuedAt?: number;
96
+ expiresAt?: number;
97
+ }
98
+ /** The human's answer to an earlier approval elicitation for one binding. */
99
+ export type ApprovalAnswer = ({
100
+ kind: 'accept';
101
+ } & AcceptedApproval) | {
53
102
  kind: 'decline';
54
103
  } | {
55
104
  kind: 'missing';
56
105
  };
106
+ /** Every approval answer a request carries: those accepted, and the bindings declined. */
107
+ export interface ApprovalAnswers {
108
+ accepted: readonly AcceptedApproval[];
109
+ declined: readonly string[];
110
+ }
57
111
  /** What one MCP `tools/call` offers the approval middleware. */
58
112
  export interface ApprovalRelay {
59
113
  /** Name the command this tool call routes to; only that command can claim the relay. */
@@ -70,6 +124,116 @@ export interface ApprovalRelay {
70
124
  answer(bindings: readonly string[]): Promise<ApprovalAnswer>;
71
125
  /** Record that the call needs the human's approval; the lane asks once the call returns. */
72
126
  hold(hold: ApprovalHold): void;
127
+ /** Every approval answer this request carries, for an up-front approval of several steps. */
128
+ answers(): Promise<ApprovalAnswers>;
129
+ /** Record that a batch or pipeline needs the human's approval of `hold`'s steps. */
130
+ holdSteps(hold: StepsHold): void;
131
+ /**
132
+ * The up-front approval of an `afd-batch` or `afd-pipe` request, which runs `commands` (#459).
133
+ * Only the MCP lane offers one, so a host that never serves MCP does not load its code.
134
+ */
135
+ upFront?(tool: UpFrontApproval['tool'], request: BatchRequest | PipelineRequest, commands: readonly ZodCommandDefinition[]): UpFrontApproval;
136
+ }
137
+ /** What the up-front gate needs from the approval middleware. */
138
+ export interface UpFrontDeps {
139
+ clock: Clock;
140
+ /** Whether `command`'s policy holds this input. */
141
+ requiresApproval(command: string, input: unknown, context: CommandContext): Promise<boolean>;
142
+ /** Whether `command`'s policy could hold some input ('required' or a function). */
143
+ mayRequireApproval(command: string, context: CommandContext): boolean;
144
+ /**
145
+ * The bindings of an up-front approval, one per key, the signing key's first: a keyed digest of
146
+ * the call (its {@link callTags} under that key), the command, its input and the actor.
147
+ */
148
+ bindings(tags: readonly string[], command: string, input: string, actor: string): Promise<string[]>;
149
+ /** The call's tag under each key: a keyed digest of the whole request as sent. */
150
+ callTags(call: string): Promise<string[]>;
151
+ /** Whether an accepted approval's MAC verifies for `bindings` and it has not expired. */
152
+ genuine(answer: AcceptedApproval, bindings: readonly string[], now: number): Promise<boolean>;
153
+ /** Spend a single-use nonce; `true` when it was fresh (or reuse is allowed). */
154
+ spend(nonce: string, expiresAt: number, now: number): Promise<boolean>;
155
+ /** A fresh question (nonce, expiry and MAC) for `bindings`. */
156
+ question(bindings: readonly string[], now: number): Promise<ApprovalQuestion>;
157
+ summarize(command: string, input: unknown, context: CommandContext): string;
158
+ }
159
+ /** One step of an `afd-batch` or `afd-pipe`, as the up-front approval sees it. */
160
+ export interface UpFrontStep {
161
+ /** The step's position in the request, 0-based as in its results. */
162
+ index: number;
163
+ command: string;
164
+ /**
165
+ * Whether the step's input comes from an earlier step (`$prev`, `$first` or `$steps`), so its
166
+ * input is not known until the pipeline runs.
167
+ */
168
+ dynamic: boolean;
169
+ /** The validated input of a step that is not dynamic; absent when it does not validate. */
170
+ input?: unknown;
171
+ /** Whether {@link input} holds the validated input. */
172
+ valid: boolean;
173
+ /**
174
+ * The input as sent (after `$input` references are resolved), as canonical JSON: how the engine
175
+ * finds the step it is about to run. Absent when {@link valid} is false.
176
+ */
177
+ sent?: string;
178
+ /**
179
+ * Whether {@link input} is the one an earlier round of the same request kept with its question
180
+ * (#1414), rather than one validated afresh by this request.
181
+ */
182
+ kept?: boolean;
183
+ }
184
+ /**
185
+ * The up-front approval of one `afd-batch` or `afd-pipe` call that can carry a form elicitation
186
+ * (#459). The approval middleware decides it once, before any step's handler runs: every step
187
+ * runs, each gated step on its own single-use approval, or none does.
188
+ */
189
+ export interface UpFrontApproval {
190
+ tool: 'afd-batch' | 'afd-pipe';
191
+ /**
192
+ * The whole request as sent (its steps or commands, inputs, conditions and options), as
193
+ * canonical JSON: every approval is bound to it, so it covers this call and no other.
194
+ */
195
+ call: string;
196
+ /** Whether a step runs only under a condition (`when`), so one may run while another is skipped. */
197
+ conditional: boolean;
198
+ /**
199
+ * The call's steps, worked out once, when the engine or the middleware first asks for them. A
200
+ * step the human already approved in an earlier round of this request has the input approved
201
+ * then; any other is validated now.
202
+ */
203
+ steps(): Promise<readonly UpFrontStep[]>;
204
+ /**
205
+ * The input validated up front for a step that runs `command` on `input` as sent, handed out
206
+ * once per step (#1414). The server runs the step on it instead of on a second validation's
207
+ * result, so the input that runs is the one the human approved even when the schema's defaults
208
+ * differ from one validation to the next (a random id, the time). `undefined` for a command
209
+ * with no approval policy and for a step that is not known up front or did not validate: the
210
+ * step runs on its own validation, as usual.
211
+ */
212
+ validated(command: string, input: unknown): Promise<{
213
+ input: unknown;
214
+ } | undefined>;
215
+ /**
216
+ * The server's definition of `command` (its approval policy and prompt), so the middleware
217
+ * decides every step from the command that step runs.
218
+ */
219
+ command(command: string): Pick<ZodCommandDefinition, 'name' | 'approval' | 'confirmPrompt'> | undefined;
220
+ /**
221
+ * Run one step through the up-front gate, with the approval middleware's own keys, nonce
222
+ * store and policies (`deps`): nothing runs until the whole call is approved.
223
+ */
224
+ run(deps: UpFrontDeps, command: string, input: unknown, context: CommandContext,
225
+ /**
226
+ * Lets the step through, admitting it on the canonical input it was decided on; `admission`
227
+ * when an approval the human gave admits it.
228
+ */
229
+ next: (canonical: string | undefined, admission?: ApprovalAdmission) => Promise<CommandResult>): Promise<CommandResult>;
230
+ /** The call's relay, which reads the human's answers and records the hold. */
231
+ relay: ApprovalRelay;
232
+ /**
233
+ * Set by the middleware when nothing may run (a gated step whose input is not known up front,
234
+ * a decline, or a pending question): the router answers with this instead of the run.
235
+ */
236
+ refusal?: CommandError;
73
237
  }
74
238
  /** Return a copy of `context` bound to `relay`. */
75
239
  export declare function bindApprovalRelay(context: CommandContext, relay: ApprovalRelay): CommandContext;
@@ -80,6 +244,25 @@ export declare function bindApprovalRelay(context: CommandContext, relay: Approv
80
244
  export declare function withoutApprovalRelay(context: CommandContext): CommandContext;
81
245
  /** The relay bound to `context`, if any. */
82
246
  export declare function approvalRelay(context: CommandContext | undefined): ApprovalRelay | undefined;
247
+ /**
248
+ * Return a copy of `context` for the steps of an `afd-batch` or `afd-pipe` whose call can carry a
249
+ * form elicitation: its relay never asks for one step, and `upFront` asks once for them all.
250
+ */
251
+ export declare function bindUpFrontApproval(context: CommandContext, upFront: UpFrontApproval): CommandContext;
252
+ /** The up-front approval bound to `context`, if any. */
253
+ export declare function upFrontApproval(context: CommandContext | undefined): UpFrontApproval | undefined;
254
+ /**
255
+ * Remove the up-front approval from a step's own context before its handler runs, so a command
256
+ * the handler runs never takes a step's approval. Each step runs with a context of its own. The
257
+ * approval middleware does this (not the engine, which keeps the gate out of `/direct` hosts), so
258
+ * only a handler behind an `mcp-elicitation` approval middleware is covered. The approval is read
259
+ * twice: by that middleware, and by the engine's up-front step hook, which takes the step's
260
+ * approved input (#1414). Without the middleware nothing is gated, so a nested command could at
261
+ * most take a later step's approved input as its own, never an approval. A retry middleware placed
262
+ * before it retries the step on the ordinary single-command path, which holds it again.
263
+ */
264
+ export declare function releaseUpFrontApproval(context: CommandContext): void;
265
+ export declare function shownInput(input: string): string;
83
266
  /** The question of a relayed approval: the summary, then what exactly would run. */
84
267
  export declare function relayMessage(summary: string, command: string, input: string, note?: string): string;
85
268
  //# sourceMappingURL=approval-relay.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"approval-relay.d.ts","sourceRoot":"","sources":["../src/approval-relay.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAE3D,2DAA2D;AAC3D,eAAO,MAAM,kBAAkB,aAAa,CAAC;AAE7C,2DAA2D;AAC3D,eAAO,MAAM,yBAAyB;aACrC,IAAI,EAAE,QAAQ;aACd,UAAU;iBACT,OAAO;qBACN,IAAI,EAAE,SAAS;qBACf,KAAK,EAAE,SAAS;qBAChB,WAAW,EAAE,0BAA0B;;;aAGzC,QAAQ,YAAG,SAAS;CACX,CAA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AAW,EACjB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,OAAO,EACd,OAAO,EAAE,cAAc;IACvB;;;OAGG;IACH,IAAI,EAAE,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,EAAE,SAAS,CAAC,EAAE,iBAAiB,KAAK,OAAO,CAAC,aAAa,CAAC,GAC5F,OAAO,CAAC,aAAa,CAAC,CAAC;IAC1B,8EAA8E;IAC9E,KAAK,EAAE,aAAa,CAAC;IACrB;;;OAGG;IACH,OAAO,CAAC,EAAE,YAAY,CAAC;CACvB;AAYD,mDAAmD;AACnD,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,aAAa,GAAG,cAAc,CAE/F;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,cAAc,GAAG,cAAc,CAG5E;AAED,4CAA4C;AAC5C,wBAAgB,aAAa,CAAC,OAAO,EAAE,cAAc,GAAG,SAAS,GAAG,aAAa,GAAG,SAAS,CAE5F;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAClC,OAAO,EAAE,cAAc,EACvB,OAAO,EAAE,eAAe,GACtB,cAAc,CAEhB;AAED,wDAAwD;AACxD,wBAAgB,eAAe,CAAC,OAAO,EAAE,cAAc,GAAG,SAAS,GAAG,eAAe,GAAG,SAAS,CAEhG;AAED;;;;;;;;;GASG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI,CAEpE;AAKD,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED,oFAAoF;AACpF,wBAAgB,YAAY,CAC3B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,EACb,IAAI,CAAC,EAAE,MAAM,GACX,MAAM,CAQR"}
@@ -3,15 +3,19 @@
3
3
  * an MCP client, through a form elicitation, instead of returning a token to the agent.
4
4
  *
5
5
  * The MCP lane binds an {@link ApprovalRelay} to the context of each `tools/call` that can carry a
6
- * form elicitation, under a private symbol (as the confirmation channel is), so neither
6
+ * form elicitation, in its host state (`host-state.ts`, as the confirmation channel is), so neither
7
7
  * `createContext` nor tool arguments can forge one. The tool router names the one command the
8
8
  * call routes to; with `relay: 'mcp-elicitation'`, the approval middleware of that command reads
9
9
  * the human's answer from the relay or records a hold on it, and the lane then answers the call
10
10
  * with `input_required` instead of the held result.
11
11
  *
12
12
  * `afd-batch` and `afd-pipe` replace the relay with one that never asks, because asking mid-run
13
- * would run the earlier steps again on the retry.
13
+ * would run the earlier steps again on the retry. Instead (#459), the router binds an
14
+ * {@link UpFrontApproval} for the whole call: before any step runs, the approval middleware works
15
+ * out every gated step's input and asks once for all of them, or refuses the call when a gated
16
+ * pipeline step's input comes from an earlier step.
14
17
  */
18
+ import { hostState, patchHostStateInPlace, withHostState } from './host-state.js';
15
19
  /** The `inputRequests` key of the approval elicitation. */
16
20
  export const APPROVAL_INPUT_KEY = 'approval';
17
21
  /** The approval elicitation form: one required boolean. */
@@ -26,37 +30,69 @@ export const APPROVAL_REQUESTED_SCHEMA = {
26
30
  },
27
31
  required: ['approve'],
28
32
  };
29
- const RELAY = Symbol('afd.approvalRelay');
30
33
  /** A relay that never asks: the steps of `afd-batch` and `afd-pipe` get it. */
31
34
  const NEVER_ASKS = {
32
35
  route() { },
33
36
  claim: () => false,
34
37
  answer: async () => ({ kind: 'missing' }),
35
38
  hold() { },
39
+ answers: async () => ({ accepted: [], declined: [] }),
40
+ holdSteps() { },
36
41
  };
37
42
  /** Return a copy of `context` bound to `relay`. */
38
43
  export function bindApprovalRelay(context, relay) {
39
- const bound = { ...context, [RELAY]: relay };
40
- return bound;
44
+ return withHostState(context, { approvalRelay: relay });
41
45
  }
42
46
  /**
43
47
  * Return a copy of `context` whose relay never asks (for `afd-batch` and `afd-pipe` steps), so the
44
48
  * middleware can still tell the agent to run the command on its own.
45
49
  */
46
50
  export function withoutApprovalRelay(context) {
47
- if (!context[RELAY])
51
+ if (!hostState(context).approvalRelay)
48
52
  return context;
49
53
  return bindApprovalRelay(context, NEVER_ASKS);
50
54
  }
51
55
  /** The relay bound to `context`, if any. */
52
56
  export function approvalRelay(context) {
53
- return context?.[RELAY];
57
+ return hostState(context).approvalRelay;
58
+ }
59
+ /**
60
+ * Return a copy of `context` for the steps of an `afd-batch` or `afd-pipe` whose call can carry a
61
+ * form elicitation: its relay never asks for one step, and `upFront` asks once for them all.
62
+ */
63
+ export function bindUpFrontApproval(context, upFront) {
64
+ return withHostState(context, { approvalRelay: NEVER_ASKS, upFrontApproval: upFront });
65
+ }
66
+ /** The up-front approval bound to `context`, if any. */
67
+ export function upFrontApproval(context) {
68
+ return hostState(context).upFrontApproval;
69
+ }
70
+ /**
71
+ * Remove the up-front approval from a step's own context before its handler runs, so a command
72
+ * the handler runs never takes a step's approval. Each step runs with a context of its own. The
73
+ * approval middleware does this (not the engine, which keeps the gate out of `/direct` hosts), so
74
+ * only a handler behind an `mcp-elicitation` approval middleware is covered. The approval is read
75
+ * twice: by that middleware, and by the engine's up-front step hook, which takes the step's
76
+ * approved input (#1414). Without the middleware nothing is gated, so a nested command could at
77
+ * most take a later step's approved input as its own, never an approval. A retry middleware placed
78
+ * before it retries the step on the ordinary single-command path, which holds it again.
79
+ */
80
+ export function releaseUpFrontApproval(context) {
81
+ patchHostStateInPlace(context, { upFrontApproval: undefined });
54
82
  }
55
83
  /** Longest input shown in an approval elicitation, in characters. */
56
84
  const MAX_SHOWN_INPUT = 1000;
85
+ export function shownInput(input) {
86
+ return input.length > MAX_SHOWN_INPUT ? `${input.slice(0, MAX_SHOWN_INPUT)}…` : input;
87
+ }
57
88
  /** The question of a relayed approval: the summary, then what exactly would run. */
58
89
  export function relayMessage(summary, command, input, note) {
59
- const shown = input.length > MAX_SHOWN_INPUT ? `${input.slice(0, MAX_SHOWN_INPUT)}…` : input;
60
- return [...(note ? [note] : []), summary, '', `Command: ${command}`, `Input: ${shown}`].join('\n');
90
+ return [
91
+ ...(note ? [note] : []),
92
+ summary,
93
+ '',
94
+ `Command: ${command}`,
95
+ `Input: ${shownInput(input)}`,
96
+ ].join('\n');
61
97
  }
62
98
  //# sourceMappingURL=approval-relay.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"approval-relay.js","sourceRoot":"","sources":["../src/approval-relay.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAIH,2DAA2D;AAC3D,MAAM,CAAC,MAAM,kBAAkB,GAAG,UAAU,CAAC;AAE7C,2DAA2D;AAC3D,MAAM,CAAC,MAAM,yBAAyB,GAAG;IACxC,IAAI,EAAE,QAAQ;IACd,UAAU,EAAE;QACX,OAAO,EAAE;YACR,IAAI,EAAE,SAAS;YACf,KAAK,EAAE,SAAS;YAChB,WAAW,EAAE,0BAA0B;SACvC;KACD;IACD,QAAQ,EAAE,CAAC,SAAS,CAAC;CACZ,CAAC;AA0CX,MAAM,KAAK,GAAG,MAAM,CAAC,mBAAmB,CAAC,CAAC;AAI1C,+EAA+E;AAC/E,MAAM,UAAU,GAAkB;IACjC,KAAK,KAAI,CAAC;IACV,KAAK,EAAE,GAAG,EAAE,CAAC,KAAK;IAClB,MAAM,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;IACzC,IAAI,KAAI,CAAC;CACT,CAAC;AAEF,mDAAmD;AACnD,MAAM,UAAU,iBAAiB,CAAC,OAAuB,EAAE,KAAoB;IAC9E,MAAM,KAAK,GAAiB,EAAE,GAAG,OAAO,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,CAAC;IAC3D,OAAO,KAAK,CAAC;AACd,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAuB;IAC3D,IAAI,CAAE,OAAwB,CAAC,KAAK,CAAC;QAAE,OAAO,OAAO,CAAC;IACtD,OAAO,iBAAiB,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;AAC/C,CAAC;AAED,4CAA4C;A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UAAU,UAAU,CAAC,KAAa;IACvC,OAAO,KAAK,CAAC,MAAM,GAAG,eAAe,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC;AACvF,CAAC;AAED,oFAAoF;AACpF,MAAM,UAAU,YAAY,CAC3B,OAAe,EACf,OAAe,EACf,KAAa,EACb,IAAa;IAEb,OAAO;QACN,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACvB,OAAO;QACP,EAAE;QACF,YAAY,OAAO,EAAE;QACrB,UAAU,UAAU,CAAC,KAAK,CAAC,EAAE;KAC7B,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACd,CAAC"}
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @fileoverview One up-front approval for an `afd-batch` or `afd-pipe` (#459, option A).
3
+ *
4
+ * Asking mid-run would run the earlier steps again on the retry, so the approval middleware
5
+ * decides the whole call before any step's handler runs, the first time a step reaches it:
6
+ *
7
+ * - a gated pipeline step whose input comes from an earlier step (`$prev`, `$first`, `$steps`)
8
+ * cannot be shown to the human up front, so nothing runs and the agent is told to split the
9
+ * pipeline there;
10
+ * - otherwise, when the request carries the human's acceptance of every gated step (each bound to
11
+ * its command, canonical validated input and actor, with a MAC, an expiry and a nonce), each
12
+ * nonce is spent and the call runs: each gated step then takes one approval matching its own
13
+ * binding, once. The engine runs every step on the input validated here, never on a second
14
+ * validation of it (#1414), so the input that runs is the one the human was shown;
15
+ * - a decline refuses the call; anything else asks once, listing every gated step.
16
+ *
17
+ * While the plan is not to run, every step answers without calling `next`, and the router answers
18
+ * the call with the refusal (or the lane with the elicitation), so no handler runs.
19
+ *
20
+ * Only the MCP lane loads this module (through its relay's `upFront`): a host that runs commands
21
+ * in process, with or without the approval middleware or the tool surface, never does.
22
+ */
23
+ import type { BatchRequest, PipelineRequest } from '@lushly-dev/afd-core';
24
+ import { type ApprovalRelay, type UpFrontApproval } from './approval-relay.js';
25
+ import type { ZodCommandDefinition } from './schema.js';
26
+ /** The most gated steps one question lists; a person cannot weigh more at once. */
27
+ export declare const MAX_UP_FRONT_APPROVALS = 20;
28
+ /**
29
+ * The most characters (UTF-16 code units) of approved input one question keeps in the
30
+ * `requestState`, over all its steps. A step past it is validated again by the request that brings the answer, as every step
31
+ * was before #1414.
32
+ */
33
+ export declare const MAX_KEPT_INPUT = 32768;
34
+ /**
35
+ * The up-front approval of one `afd-batch` or `afd-pipe` request that can carry a form
36
+ * elicitation, asking and reading answers through `relay`. Its steps are worked out only when a
37
+ * step whose command has an approval policy is about to run, or an approval middleware asks.
38
+ */
39
+ export declare function createUpFrontApproval(relay: ApprovalRelay, tool: UpFrontApproval['tool'], request: BatchRequest | PipelineRequest, commands: readonly ZodCommandDefinition[]): UpFrontApproval;
40
+ //# sourceMappingURL=approval-up-front.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"approval-up-front.d.ts","sourceRoot":"","sources":["../src/approval-up-front.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,KAAK,EACX,YAAY,EAIZ,eAAe,EACf,MAAM,sBAAsB,CAAC;AAU9B,OAAO,EAGN,KAAK,aAAa,EAGlB,KAAK,eAAe,EAGpB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAcxD,mFAAmF;AACnF,eAAO,MAAM,sBAAsB,KAAK,CAAC;AAEzC;;;;GAIG;AACH,eAAO,MAAM,cAAc,QAAS,CAAC;AAQrC;;;;GAIG;AACH,wBAAgB,qBAAqB,CACpC,KAAK,EAAE,aAAa,EACpB,IAAI,EAAE,eAAe,CAAC,MAAM,CAAC,EAC7B,OAAO,EAAE,YAAY,GAAG,eAAe,EACvC,QAAQ,EAAE,SAAS,oBAAoB,EAAE,GACvC,eAAe,CAiEjB"}