@buoy-gg/agent-core 7.0.35 → 7.0.36

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 (174) hide show
  1. package/README.md +10 -7
  2. package/lib/commonjs/blocks/receipts.js +57 -1
  3. package/lib/commonjs/blocks/receipts.js.map +1 -1
  4. package/lib/commonjs/blocks/types.js +23 -2
  5. package/lib/commonjs/blocks/types.js.map +1 -1
  6. package/lib/commonjs/blocks/uiTool.js +423 -10
  7. package/lib/commonjs/blocks/uiTool.js.map +1 -1
  8. package/lib/commonjs/catalog/catalog.g.js +121 -43
  9. package/lib/commonjs/catalog/catalog.g.js.map +1 -1
  10. package/lib/commonjs/catalog/catalog.source.json +121 -45
  11. package/lib/commonjs/catalog/catalog.types.g.js +44 -0
  12. package/lib/commonjs/catalog/catalog.types.g.js.map +1 -0
  13. package/lib/commonjs/catalog/normalizeParams.js +333 -0
  14. package/lib/commonjs/catalog/normalizeParams.js.map +1 -0
  15. package/lib/commonjs/catalog/signature.js +68 -0
  16. package/lib/commonjs/catalog/signature.js.map +1 -0
  17. package/lib/commonjs/catalog/snapshotReads.js +13 -6
  18. package/lib/commonjs/catalog/snapshotReads.js.map +1 -1
  19. package/lib/commonjs/catalog/toProviderTools.js +41 -5
  20. package/lib/commonjs/catalog/toProviderTools.js.map +1 -1
  21. package/lib/commonjs/context/buildContextPack.js +367 -19
  22. package/lib/commonjs/context/buildContextPack.js.map +1 -1
  23. package/lib/commonjs/effects/digest.js +34 -0
  24. package/lib/commonjs/effects/digest.js.map +1 -0
  25. package/lib/commonjs/effects/ledger.js +85 -1
  26. package/lib/commonjs/effects/ledger.js.map +1 -1
  27. package/lib/commonjs/engine/askGate.js +287 -0
  28. package/lib/commonjs/engine/askGate.js.map +1 -0
  29. package/lib/commonjs/engine/effectFor.js +82 -1
  30. package/lib/commonjs/engine/effectFor.js.map +1 -1
  31. package/lib/commonjs/engine/historyBudget.js +260 -0
  32. package/lib/commonjs/engine/historyBudget.js.map +1 -0
  33. package/lib/commonjs/engine/runAgentTurn.js +924 -83
  34. package/lib/commonjs/engine/runAgentTurn.js.map +1 -1
  35. package/lib/commonjs/engine/systemPrompt.js +72 -8
  36. package/lib/commonjs/engine/systemPrompt.js.map +1 -1
  37. package/lib/commonjs/engine/textToolCalls.js +276 -0
  38. package/lib/commonjs/engine/textToolCalls.js.map +1 -0
  39. package/lib/commonjs/index.js +58 -0
  40. package/lib/commonjs/index.js.map +1 -1
  41. package/lib/commonjs/policy/labels.js +19 -3
  42. package/lib/commonjs/policy/labels.js.map +1 -1
  43. package/lib/commonjs/policy/policy.js +6 -1
  44. package/lib/commonjs/policy/policy.js.map +1 -1
  45. package/lib/commonjs/policy/redact.js +42 -5
  46. package/lib/commonjs/policy/redact.js.map +1 -1
  47. package/lib/commonjs/providers/anthropic.js +331 -208
  48. package/lib/commonjs/providers/anthropic.js.map +1 -1
  49. package/lib/commonjs/providers/openai.js +229 -103
  50. package/lib/commonjs/providers/openai.js.map +1 -1
  51. package/lib/commonjs/providers/sse.js +164 -29
  52. package/lib/commonjs/providers/sse.js.map +1 -1
  53. package/lib/commonjs/providers/streamTimer.js +123 -0
  54. package/lib/commonjs/providers/streamTimer.js.map +1 -0
  55. package/lib/commonjs/providers/transport.js +107 -0
  56. package/lib/commonjs/providers/transport.js.map +1 -0
  57. package/lib/commonjs/providers/xhrStream.js +209 -0
  58. package/lib/commonjs/providers/xhrStream.js.map +1 -0
  59. package/lib/commonjs/session.js +215 -58
  60. package/lib/commonjs/session.js.map +1 -1
  61. package/lib/module/blocks/receipts.js +57 -1
  62. package/lib/module/blocks/receipts.js.map +1 -1
  63. package/lib/module/blocks/types.js +23 -2
  64. package/lib/module/blocks/types.js.map +1 -1
  65. package/lib/module/blocks/uiTool.js +421 -10
  66. package/lib/module/blocks/uiTool.js.map +1 -1
  67. package/lib/module/catalog/catalog.g.js +121 -43
  68. package/lib/module/catalog/catalog.g.js.map +1 -1
  69. package/lib/module/catalog/catalog.source.json +121 -45
  70. package/lib/module/catalog/catalog.types.g.js +40 -0
  71. package/lib/module/catalog/catalog.types.g.js.map +1 -0
  72. package/lib/module/catalog/normalizeParams.js +327 -0
  73. package/lib/module/catalog/normalizeParams.js.map +1 -0
  74. package/lib/module/catalog/signature.js +63 -0
  75. package/lib/module/catalog/signature.js.map +1 -0
  76. package/lib/module/catalog/snapshotReads.js +13 -6
  77. package/lib/module/catalog/snapshotReads.js.map +1 -1
  78. package/lib/module/catalog/toProviderTools.js +41 -5
  79. package/lib/module/catalog/toProviderTools.js.map +1 -1
  80. package/lib/module/context/buildContextPack.js +366 -19
  81. package/lib/module/context/buildContextPack.js.map +1 -1
  82. package/lib/module/effects/digest.js +29 -0
  83. package/lib/module/effects/digest.js.map +1 -0
  84. package/lib/module/effects/ledger.js +85 -1
  85. package/lib/module/effects/ledger.js.map +1 -1
  86. package/lib/module/engine/askGate.js +280 -0
  87. package/lib/module/engine/askGate.js.map +1 -0
  88. package/lib/module/engine/effectFor.js +83 -1
  89. package/lib/module/engine/effectFor.js.map +1 -1
  90. package/lib/module/engine/historyBudget.js +251 -0
  91. package/lib/module/engine/historyBudget.js.map +1 -0
  92. package/lib/module/engine/runAgentTurn.js +920 -83
  93. package/lib/module/engine/runAgentTurn.js.map +1 -1
  94. package/lib/module/engine/systemPrompt.js +71 -8
  95. package/lib/module/engine/systemPrompt.js.map +1 -1
  96. package/lib/module/engine/textToolCalls.js +270 -0
  97. package/lib/module/engine/textToolCalls.js.map +1 -0
  98. package/lib/module/index.js +5 -3
  99. package/lib/module/index.js.map +1 -1
  100. package/lib/module/policy/labels.js +19 -3
  101. package/lib/module/policy/labels.js.map +1 -1
  102. package/lib/module/policy/policy.js +6 -1
  103. package/lib/module/policy/policy.js.map +1 -1
  104. package/lib/module/policy/redact.js +42 -5
  105. package/lib/module/policy/redact.js.map +1 -1
  106. package/lib/module/providers/anthropic.js +332 -209
  107. package/lib/module/providers/anthropic.js.map +1 -1
  108. package/lib/module/providers/openai.js +230 -104
  109. package/lib/module/providers/openai.js.map +1 -1
  110. package/lib/module/providers/sse.js +160 -29
  111. package/lib/module/providers/sse.js.map +1 -1
  112. package/lib/module/providers/streamTimer.js +118 -0
  113. package/lib/module/providers/streamTimer.js.map +1 -0
  114. package/lib/module/providers/transport.js +103 -0
  115. package/lib/module/providers/transport.js.map +1 -0
  116. package/lib/module/providers/xhrStream.js +204 -0
  117. package/lib/module/providers/xhrStream.js.map +1 -0
  118. package/lib/module/session.js +199 -60
  119. package/lib/module/session.js.map +1 -1
  120. package/lib/typescript/blocks/receipts.d.ts +5 -0
  121. package/lib/typescript/blocks/receipts.d.ts.map +1 -1
  122. package/lib/typescript/blocks/types.d.ts +23 -2
  123. package/lib/typescript/blocks/types.d.ts.map +1 -1
  124. package/lib/typescript/blocks/uiTool.d.ts +11 -2
  125. package/lib/typescript/blocks/uiTool.d.ts.map +1 -1
  126. package/lib/typescript/catalog/catalog.g.d.ts.map +1 -1
  127. package/lib/typescript/catalog/catalog.types.g.d.ts +40 -0
  128. package/lib/typescript/catalog/catalog.types.g.d.ts.map +1 -0
  129. package/lib/typescript/catalog/normalizeParams.d.ts +43 -0
  130. package/lib/typescript/catalog/normalizeParams.d.ts.map +1 -0
  131. package/lib/typescript/catalog/signature.d.ts +29 -0
  132. package/lib/typescript/catalog/signature.d.ts.map +1 -0
  133. package/lib/typescript/catalog/snapshotReads.d.ts.map +1 -1
  134. package/lib/typescript/catalog/toProviderTools.d.ts +28 -15
  135. package/lib/typescript/catalog/toProviderTools.d.ts.map +1 -1
  136. package/lib/typescript/context/buildContextPack.d.ts +24 -2
  137. package/lib/typescript/context/buildContextPack.d.ts.map +1 -1
  138. package/lib/typescript/effects/digest.d.ts +10 -0
  139. package/lib/typescript/effects/digest.d.ts.map +1 -0
  140. package/lib/typescript/effects/ledger.d.ts +55 -0
  141. package/lib/typescript/effects/ledger.d.ts.map +1 -1
  142. package/lib/typescript/engine/askGate.d.ts +29 -0
  143. package/lib/typescript/engine/askGate.d.ts.map +1 -0
  144. package/lib/typescript/engine/effectFor.d.ts +6 -1
  145. package/lib/typescript/engine/effectFor.d.ts.map +1 -1
  146. package/lib/typescript/engine/historyBudget.d.ts +92 -0
  147. package/lib/typescript/engine/historyBudget.d.ts.map +1 -0
  148. package/lib/typescript/engine/runAgentTurn.d.ts +83 -1
  149. package/lib/typescript/engine/runAgentTurn.d.ts.map +1 -1
  150. package/lib/typescript/engine/systemPrompt.d.ts +41 -0
  151. package/lib/typescript/engine/systemPrompt.d.ts.map +1 -1
  152. package/lib/typescript/engine/textToolCalls.d.ts +58 -0
  153. package/lib/typescript/engine/textToolCalls.d.ts.map +1 -0
  154. package/lib/typescript/index.d.ts +5 -3
  155. package/lib/typescript/index.d.ts.map +1 -1
  156. package/lib/typescript/policy/labels.d.ts.map +1 -1
  157. package/lib/typescript/policy/policy.d.ts.map +1 -1
  158. package/lib/typescript/policy/redact.d.ts.map +1 -1
  159. package/lib/typescript/providers/anthropic.d.ts.map +1 -1
  160. package/lib/typescript/providers/openai.d.ts +0 -16
  161. package/lib/typescript/providers/openai.d.ts.map +1 -1
  162. package/lib/typescript/providers/sse.d.ts +82 -1
  163. package/lib/typescript/providers/sse.d.ts.map +1 -1
  164. package/lib/typescript/providers/streamTimer.d.ts +59 -0
  165. package/lib/typescript/providers/streamTimer.d.ts.map +1 -0
  166. package/lib/typescript/providers/transport.d.ts +40 -0
  167. package/lib/typescript/providers/transport.d.ts.map +1 -0
  168. package/lib/typescript/providers/types.d.ts +91 -3
  169. package/lib/typescript/providers/types.d.ts.map +1 -1
  170. package/lib/typescript/providers/xhrStream.d.ts +43 -0
  171. package/lib/typescript/providers/xhrStream.d.ts.map +1 -0
  172. package/lib/typescript/session.d.ts +40 -2
  173. package/lib/typescript/session.d.ts.map +1 -1
  174. package/package.json +2 -2
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.EffectLedger = void 0;
7
7
  exports.isReversible = isReversible;
8
+ var _digest = require("./digest");
8
9
  /**
9
10
  * The effect ledger — what the agent changed, and how to put it back.
10
11
  *
@@ -42,7 +43,32 @@ let seq = 0;
42
43
  class EffectLedger {
43
44
  entries = [];
44
45
  listeners = new Set();
45
- record(toolId, action, effect) {
46
+ gen = 0;
47
+
48
+ /**
49
+ * Which conversation this ledger is holding, bumped by `clear`.
50
+ *
51
+ * A turn captures it when it starts and hands it back on every `record`.
52
+ * Nothing about a dispatch can be pulled back once it is in flight, so a
53
+ * write that settles after the user has already tapped "New conversation"
54
+ * still lands in the app — but it must not land in the NEW conversation's
55
+ * undo bar, which is what happened: reset cleared the ledger, the old
56
+ * write finished a moment later, and an empty chat came up owning a change
57
+ * nobody in it had made. Recording it here would be worse than dropping it,
58
+ * because Undo would then offer to reverse something the user cannot see.
59
+ */
60
+ get generation() {
61
+ return this.gen;
62
+ }
63
+
64
+ /**
65
+ * @param generation The value of `generation` when the turn began. Omit for
66
+ * a human-initiated action, which is by definition happening now — and which
67
+ * therefore always returns an entry.
68
+ */
69
+
70
+ record(toolId, action, effect, generation) {
71
+ if (generation !== undefined && generation !== this.gen) return undefined;
46
72
  const entry = {
47
73
  id: `fx-${++seq}-${Date.now()}`,
48
74
  at: Date.now(),
@@ -62,6 +88,27 @@ class EffectLedger {
62
88
  return this.entries;
63
89
  }
64
90
 
91
+ /**
92
+ * Put back the entries of a conversation that outlived the app.
93
+ *
94
+ * The ledger is the ONLY undo surface, and it used to die with the JS
95
+ * context: after a reload the transcript came back — receipt cards, "saved"
96
+ * lines and all — while the changes bar quietly vanished and the changes
97
+ * themselves stayed applied. "New conversation" warns before losing that
98
+ * ("Clearing the conversation loses the only automatic undo for it"); a
99
+ * reload took the same thing without asking.
100
+ *
101
+ * Refuses once anything has been recorded, for the same reason `hydrate`
102
+ * refuses a live transcript: a late disk read must not shuffle old entries
103
+ * in among changes made since.
104
+ */
105
+ restore(entries) {
106
+ if (this.entries.length > 0 || entries.length === 0) return false;
107
+ this.entries = [...entries];
108
+ this.emit();
109
+ return true;
110
+ }
111
+
65
112
  /** New array identity for the snapshot after entries were mutated in place. */
66
113
  touch() {
67
114
  this.entries = [...this.entries];
@@ -88,6 +135,9 @@ class EffectLedger {
88
135
  }
89
136
  clear() {
90
137
  this.entries = [];
138
+ // Anything a turn already in flight is about to record belongs to the
139
+ // conversation being thrown away, not to the one replacing it.
140
+ this.gen += 1;
91
141
  this.emit();
92
142
  }
93
143
 
@@ -185,6 +235,40 @@ class EffectLedger {
185
235
  }
186
236
  break;
187
237
  }
238
+ case "query-write":
239
+ {
240
+ // Same concurrent-writer guard as storage, but the usual "other
241
+ // writer" here is the app itself refetching — in which case the
242
+ // server's data is already back and there is nothing to undo.
243
+ if (fx.wroteDigest !== undefined) {
244
+ try {
245
+ const current = await dispatch("query", "getQueryData", {
246
+ queryHash: fx.queryHash
247
+ });
248
+ if (current?.found === true) {
249
+ const now = current.hasData === false ? undefined : (0, _digest.digestOf)(current.data);
250
+ if (now !== fx.wroteDigest) {
251
+ return "The app has refetched or changed this query since Ask Buoy wrote it — left the newer data alone";
252
+ }
253
+ }
254
+ } catch {
255
+ /* unreadable now; proceed with the restore */
256
+ }
257
+ }
258
+ const outcome = await (fx.before === undefined ? dispatch("query", "invalidate", {
259
+ queryHash: fx.queryHash
260
+ }) : dispatch("query", "setQueryData", {
261
+ queryHash: fx.queryHash,
262
+ data: fx.before,
263
+ // The whole prior value, verbatim: the drop-a-field guard is
264
+ // for edits, and this is a restore of what the guard protects.
265
+ force: true
266
+ }));
267
+ if (outcome && typeof outcome === "object" && outcome.ok === false) {
268
+ return typeof outcome.error === "string" ? outcome.error : "The query could not be restored";
269
+ }
270
+ break;
271
+ }
188
272
  case "state-write":
189
273
  return "This change can't be undone automatically";
190
274
  case "transient":
@@ -1 +1 @@
1
- {"version":3,"names":["isReversible","effect","kind","seq","EffectLedger","entries","listeners","Set","record","toolId","action","entry","id","Date","now","at","emit","list","touch","reversible","filter","e","undoneAt","transients","isEmpty","every","undefined","subscribe","fn","add","delete","clear","noteExternalRevert","params","changed","mark","match","undoError","ruleId","revertOne","dispatch","fx","scope","instanceId","wrote","readAction","current","key","rec","value","found","before","removeAction","err","msg","Error","message","String","revertAll","outcome","reverted","failed","skippedTransients","pending","reverse","error","push","label","exports"],"sourceRoot":"../../../src","sources":["effects/ledger.ts"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAgCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,YAAYA,CAACC,MAAqB,EAAW;EAC3D,OAAOA,MAAM,CAACC,IAAI,KAAK,WAAW,IAAID,MAAM,CAACC,IAAI,KAAK,aAAa;AACrE;AAoBA,IAAIC,GAAG,GAAG,CAAC;AAEJ,MAAMC,YAAY,CAAC;EAChBC,OAAO,GAAkB,EAAE;EAC3BC,SAAS,GAAG,IAAIC,GAAG,CAAa,CAAC;EAEzCC,MAAMA,CAACC,MAAc,EAAEC,MAAc,EAAET,MAAqB,EAAe;IACzE,MAAMU,KAAkB,GAAG;MACzBC,EAAE,EAAE,MAAM,EAAET,GAAG,IAAIU,IAAI,CAACC,GAAG,CAAC,CAAC,EAAE;MAC/BC,EAAE,EAAEF,IAAI,CAACC,GAAG,CAAC,CAAC;MACdL,MAAM;MACNC,MAAM;MACNT;IACF,CAAC;IACD;IACA;IACA;IACA;IACA,IAAI,CAACI,OAAO,GAAG,CAAC,GAAG,IAAI,CAACA,OAAO,EAAEM,KAAK,CAAC;IACvC,IAAI,CAACK,IAAI,CAAC,CAAC;IACX,OAAOL,KAAK;EACd;EAEAM,IAAIA,CAAA,EAAkB;IACpB,OAAO,IAAI,CAACZ,OAAO;EACrB;;EAEA;EACQa,KAAKA,CAAA,EAAS;IACpB,IAAI,CAACb,OAAO,GAAG,CAAC,GAAG,IAAI,CAACA,OAAO,CAAC;IAChC,IAAI,CAACW,IAAI,CAAC,CAAC;EACb;;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,MAAM,CAACkE,KAAK;QAAEF;MAAM,CAAC,CAAC,CAAC,KAChEN,OAAO,CAACC,QAAQ,IAAI,CAAC;IAC5B;IACA,IAAI,CAAC5C,IAAI,CAAC,CAAC;IACX,OAAO2C,OAAO;EAChB;AACF;AAACS,OAAA,CAAAhE,YAAA,GAAAA,YAAA","ignoreList":[]}
1
+ {"version":3,"names":["_digest","require","isReversible","effect","kind","seq","EffectLedger","entries","listeners","Set","gen","generation","record","toolId","action","undefined","entry","id","Date","now","at","emit","list","restore","length","touch","reversible","filter","e","undoneAt","transients","isEmpty","every","subscribe","fn","add","delete","clear","noteExternalRevert","params","changed","mark","match","undoError","ruleId","revertOne","dispatch","fx","scope","instanceId","wrote","readAction","current","key","rec","value","found","before","removeAction","wroteDigest","queryHash","hasData","digestOf","data","outcome","force","ok","error","err","msg","Error","message","String","revertAll","reverted","failed","skippedTransients","pending","reverse","push","label","exports"],"sourceRoot":"../../../src","sources":["effects/ledger.ts"],"mappings":";;;;;;;AAsBA,IAAAA,OAAA,GAAAC,OAAA;AAtBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAsDA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,YAAYA,CAACC,MAAqB,EAAW;EAC3D,OAAOA,MAAM,CAACC,IAAI,KAAK,WAAW,IAAID,MAAM,CAACC,IAAI,KAAK,aAAa;AACrE;AAoBA,IAAIC,GAAG,GAAG,CAAC;AAEJ,MAAMC,YAAY,CAAC;EAChBC,OAAO,GAAkB,EAAE;EAC3BC,SAAS,GAAG,IAAIC,GAAG,CAAa,CAAC;EACjCC,GAAG,GAAG,CAAC;;EAEf;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,IAAIC,UAAUA,CAAA,EAAW;IACvB,OAAO,IAAI,CAACD,GAAG;EACjB;;EAEA;AACF;AACA;AACA;AACA;;E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
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ACQUISITION_CALLS = void 0;
7
+ exports.synthesizeAskBlock = synthesizeAskBlock;
8
+ exports.synthesizeShortfallBlock = synthesizeShortfallBlock;
9
+ var _runId = require("../blocks/runId");
10
+ /**
11
+ * The last guarantee of a turn: when the turn ends WAITING ON THE USER, the
12
+ * user gets something to tap.
13
+ *
14
+ * `buoy_ui` has had choice/confirm/input since the start and the prompt has
15
+ * asked for them since the start, and models still end turns waiting in prose.
16
+ * Two off a real device, a day apart:
17
+ *
18
+ * Want me to add an Eevee line to the bag?
19
+ * If you want a Blastoise in there, tell me the level
20
+ * (Lv. 5, Lv. 25 or Lv. 50) and how many, and I'll add it.
21
+ *
22
+ * The first is a question. The SECOND IS NOT — no question mark anywhere, the
23
+ * ask is an imperative, and the model had already written the options out. A
24
+ * gate that keys on "?" catches one of these, which is why this one keys on
25
+ * the ask instead: an offer to act, or a request for a value.
26
+ *
27
+ * Deliberately narrow, in this order:
28
+ *
29
+ * - It reads only the LAST sentence of the turn. A question the model asked
30
+ * and then answered itself is not waiting on anybody.
31
+ * - Options the model ALREADY enumerated win. "(Lv. 5, Lv. 25 or Lv. 50)" is
32
+ * the model doing the work and writing it in the wrong place.
33
+ * - Then a clean either-or, split into its two halves.
34
+ * - Then Yes/No, but only for an offer to ACT ("I can add it if you'd like").
35
+ * A request for a value it could not enumerate — "tell me the level" —
36
+ * produces NOTHING. Yes/No against "tell me the level" is worse than the
37
+ * sentence it replaced, and the composer is already a text field.
38
+ *
39
+ * ENGLISH ONLY, AND DELIBERATELY FAIL-SAFE. Both patterns are English, tuned
40
+ * to sentences observed in an English demo app, and there is no honest way to
41
+ * make a regex multilingual. What saves this from being an overfit that ships
42
+ * to everyone is the direction it fails in: no match returns null, the model's
43
+ * own prose question stands, and the user answers it in the composer exactly
44
+ * as they would have. A non-English app loses the extra card; it does not get
45
+ * a wrong one. Do not "fix" that by loosening the patterns — a false positive
46
+ * turns a finished answer into a spurious question, which is the one outcome
47
+ * worse than no card at all.
48
+ *
49
+ * It emits a `choice`, never a `confirm`: choice carries the question in the
50
+ * field `questionBehind` reads after a restore, renders in the neutral tone
51
+ * rather than the warning one, and keeps the "or just type your answer"
52
+ * escape. A synthesized card must never look like an approval.
53
+ *
54
+ * The block is authored AFTER the assistant message and carries no tool call,
55
+ * so nothing enters the provider history: there is no `tool_use` to answer and
56
+ * no `tool_result` to orphan. The user's tap becomes their next message
57
+ * exactly as a model-authored block's would.
58
+ */
59
+
60
+ const nextId = (0, _runId.idFactory)("ask");
61
+
62
+ /**
63
+ * An offer to DO something, waiting on yes or no. Includes the trailing
64
+ * conditional models close with — "I can add it **if you'd like**" — which is
65
+ * the same offer with the question mark filed off.
66
+ */
67
+ const OFFER = /\b(?:want me to|would you like me to|would you like|do you want me to|do you want|should i|shall i|ok(?:ay)? if i|say the word|let me know if|if you'?d (?:like|prefer)|if you want)\b/i;
68
+
69
+ /**
70
+ * A request for a VALUE the engine cannot invent: "tell me the level", "just
71
+ * say which one". Distinguished from "let me know IF you want…", which is an
72
+ * offer, by what follows the verb.
73
+ */
74
+ const VALUE_ASK = /\b(?:tell me|let me know|just say|say|give me|pick|choose)\s+(?:the|which|what|how|your|one)\b/i;
75
+ const ASK = new RegExp(`${OFFER.source}|${VALUE_ASK.source}`, "i");
76
+
77
+ /** Markdown and quoting the model wrapped the sentence in. */
78
+ const TRAILING_MARKUP = /[*_`"'’”\s]+$/;
79
+ const LEADING_MARKUP = /^[*_`>\-\s]+/;
80
+
81
+ /**
82
+ * A sentence boundary that survives "Lv. 5".
83
+ *
84
+ * A terminator only ends a sentence when what follows starts one — a capital
85
+ * or an opening quote. `.` before a digit or a lowercase word is an
86
+ * abbreviation, and splitting there tore the Blastoise sentence into five.
87
+ */
88
+ const SENTENCE_END = /[.!?]["'’”)\]]*\s+(?=[A-Z("'“])|\n+/;
89
+
90
+ /** "add it to the cart, or just show you the item" → two halves. */
91
+ const OR_SPLIT = /,?\s+or\s+/i;
92
+
93
+ /** A list the model already wrote out: "(Lv. 5, Lv. 25 or Lv. 50)". */
94
+ const PARENTHETICAL = /\(([^()]{3,140})\)/;
95
+ const LIST_SPLIT = /\s*,\s*|\s+or\s+/i;
96
+
97
+ /**
98
+ * A half that reads as a button. Clause punctuation means the split landed
99
+ * inside a list ("a, b or c") rather than between two real alternatives.
100
+ */
101
+ const CLEAN_LABEL = /^[A-Za-z0-9][^,;:—–]*$/;
102
+ /** Looser: an enumerated option was already a list item, so commas are gone. */
103
+ const CLEAN_OPTION = /^[A-Za-z0-9$][^;:—–()]*$/;
104
+ const YES_NO = [{
105
+ id: "yes",
106
+ label: "Yes, go ahead"
107
+ }, {
108
+ id: "no",
109
+ label: "No thanks"
110
+ }];
111
+
112
+ /**
113
+ * The block a turn should have ended with, or null to leave the prose alone.
114
+ *
115
+ * `text` is everything the model said this turn, rounds joined in order.
116
+ */
117
+ function synthesizeAskBlock(text) {
118
+ const sentence = lastSentence(text);
119
+ if (!sentence || sentence.length < 8 || sentence.length > 240) return null;
120
+ const ask = ASK.exec(sentence);
121
+ if (!ask) return null;
122
+
123
+ // 1. The model already wrote the options; they were just in the wrong place.
124
+ const listed = enumerated(sentence);
125
+ if (listed) return {
126
+ id: nextId(),
127
+ kind: "choice",
128
+ question: sentence,
129
+ options: listed
130
+ };
131
+
132
+ // 2. Two real alternatives, after the verb that offered them.
133
+ const after = sentence.slice(ask.index + ask[0].length).replace(/[?.!,]+$/, "").trim();
134
+ const halves = after ? after.split(OR_SPLIT) : [];
135
+ if (halves.length > 2) return null;
136
+ if (halves.length === 2) {
137
+ const alt = alternatives(halves);
138
+ return alt ? {
139
+ id: nextId(),
140
+ kind: "choice",
141
+ question: sentence,
142
+ options: alt
143
+ } : null;
144
+ }
145
+
146
+ // 3. Yes or no — but only for an offer to act. A value it could not
147
+ // enumerate stays prose; the composer is already a text field.
148
+ if (VALUE_ASK.test(sentence) || !OFFER.test(sentence)) return null;
149
+ return {
150
+ id: nextId(),
151
+ kind: "choice",
152
+ question: sentence,
153
+ options: YES_NO
154
+ };
155
+ }
156
+ function lastSentence(text) {
157
+ const trimmed = text.replace(TRAILING_MARKUP, "").trim();
158
+ if (!trimmed) return null;
159
+ const parts = trimmed.split(SENTENCE_END);
160
+ const last = parts[parts.length - 1]?.replace(LEADING_MARKUP, "").trim();
161
+ return last || null;
162
+ }
163
+ function enumerated(sentence) {
164
+ const inside = PARENTHETICAL.exec(sentence)?.[1];
165
+ // `or` is what makes it a list of choices rather than an aside
166
+ // ("(Lv25, quantity 6, $9.99 each)").
167
+ if (!inside || !/\bor\b/i.test(inside)) return null;
168
+ const labels = inside.split(LIST_SPLIT).map(p => p.trim()).filter(Boolean);
169
+ if (labels.length < 2 || labels.length > 5) return null;
170
+ for (const label of labels) {
171
+ if (label.length < 1 || label.length > 48 || !CLEAN_OPTION.test(label)) return null;
172
+ }
173
+ return labels.map((label, i) => ({
174
+ id: `o${i + 1}`,
175
+ label: capitalize(label)
176
+ }));
177
+ }
178
+ function alternatives(halves) {
179
+ const labels = halves.map(h => h.trim().replace(/[?.!]+$/, "").trim());
180
+ for (const label of labels) {
181
+ if (label.length < 2 || label.length > 48 || !CLEAN_LABEL.test(label)) return null;
182
+ }
183
+ return labels.map((label, i) => ({
184
+ id: i === 0 ? "a" : "b",
185
+ label: capitalize(label)
186
+ }));
187
+ }
188
+ function capitalize(label) {
189
+ return /^[a-z]/.test(label) ? label[0].toUpperCase() + label.slice(1) : label;
190
+ }
191
+
192
+ /* ------------------------------------------------------------------------ *
193
+ * The OTHER way a turn ends badly: it answers a smaller question than the
194
+ * one it was asked, and hands the rest back to the user.
195
+ *
196
+ * Off a real device: "Show me all the pokemon from our poke mart app." The
197
+ * app's home screen holds ten and fetches one per card as you swipe; the user
198
+ * had swiped four. The agent read the cache, found four, showed four, and
199
+ * closed with "that's everything currently in the app's cache, so if you visit
200
+ * other Pokémon in the app I can show those too" — while holding
201
+ * route-events.navigate, describeScreen and tapElement, and having called none
202
+ * of them.
203
+ *
204
+ * `synthesizeAskBlock` above should have caught that last clause — "if you
205
+ * visit … I can" is exactly its OFFER shape. It did not, because the turn had
206
+ * already shipped a `suggestions` block, so `offeredTap` was true. The chips
207
+ * were "Compare their HP", "Show me the bag", "Change Pikachu's stats". The
208
+ * user had something to tap; none of it was the thing the agent had just
209
+ * failed to do.
210
+ *
211
+ * So this gate asks a different question from that one. Not "does the user
212
+ * have something to tap" but "can the user tap THE GAP" — and it fires even
213
+ * when the turn offered chips, because that is the case it exists for.
214
+ *
215
+ * It stays quiet when the agent actually TRIED (see `attempted`). An agent
216
+ * that navigated, tapped and re-read and still came up short has earned its
217
+ * "that's all there is", and nagging it would be worse than silence.
218
+ * ------------------------------------------------------------------------ */
219
+
220
+ /**
221
+ * A limit on what the answer covers: "that's all", "only", "so far".
222
+ * Meaningless alone — half the sentences in English contain "only".
223
+ */
224
+ const LIMITED = /\b(?:that'?s (?:all|everything|it)|only|just|so far|nothing else|no others?|the rest (?:are|is)?n'?t)\b/i;
225
+
226
+ /**
227
+ * …AND a word that says the limit is about LOADING rather than about the
228
+ * world. "Only four are cached" is a shortfall; "only four are in stock" is an
229
+ * answer. Both halves have to be present.
230
+ */
231
+ const NOT_LOADED = /\b(?:cache[ds]?|cached|loaded|fetched|in memory|visited|opened|rendered|recorded|captured)\b/i;
232
+
233
+ /**
234
+ * The offer that hands the work back, which needs no second half: the user is
235
+ * being asked to go and do the thing the agent could have done itself.
236
+ */
237
+ const GO_YOURSELF = /\b(?:if|once|when|after) you (?:visit|open|go to|navigate|browse|load|tap|swipe|scroll|look at)\b/i;
238
+
239
+ /** How much of the turn to read. The concession is usually the closing line. */
240
+ const TAIL_SENTENCES = 3;
241
+
242
+ /**
243
+ * `attempted` — did this turn call anything that GOES AND GETS data?
244
+ * Keyed on the actions the prompt's ladder names, so the two move together.
245
+ */
246
+ const ACQUISITION_CALLS = exports.ACQUISITION_CALLS = new Set(["route-events.navigate", "route-events.stackGoBack", "route-events.stackNavigateToIndex", "highlight-updates.describeScreen", "highlight-updates.tapElement", "highlight-updates.waitFor", "query.refetch", "query.invalidate"]);
247
+ /**
248
+ * The block a turn should have ended with when it conceded a gap and never
249
+ * tried to close it, or null to leave the prose alone.
250
+ *
251
+ * An `actions` block, not a `choice`: nothing is being ASKED here. The turn is
252
+ * over and the answer stands; this is one button that says the gap is closable
253
+ * and closes it. So it must not end the turn or set `awaiting-user` — a
254
+ * synthesized card must never look like it is blocking on a reply.
255
+ */
256
+ function synthesizeShortfallBlock({
257
+ text,
258
+ attempted
259
+ }) {
260
+ if (attempted) return null;
261
+ const tail = lastSentences(text, TAIL_SENTENCES);
262
+ if (!tail) return null;
263
+ const conceded = GO_YOURSELF.test(tail) || LIMITED.test(tail) && NOT_LOADED.test(tail);
264
+ if (!conceded) return null;
265
+ return {
266
+ id: nextShortfallId(),
267
+ kind: "actions",
268
+ actions: [{
269
+ label: "Go get the rest",
270
+ primary: true,
271
+ // Phrased as the user would phrase it, because that is what it becomes:
272
+ // the tap is sent as their next message.
273
+ send: "Don't stop at what's already loaded. Drive the app to get the rest — check the other layers, use the sitemap to navigate to the screen that loads it, wait for it, then answer in full. If it truly isn't reachable, tell me exactly what you tried."
274
+ }]
275
+ };
276
+ }
277
+ const nextShortfallId = (0, _runId.idFactory)("gap");
278
+
279
+ /** The last `count` sentences, joined — see `lastSentence` for the split. */
280
+ function lastSentences(text, count) {
281
+ const trimmed = text.replace(TRAILING_MARKUP, "").trim();
282
+ if (!trimmed) return null;
283
+ const parts = trimmed.split(SENTENCE_END).filter(p => p.trim());
284
+ const tail = parts.slice(-count).join(" ").replace(LEADING_MARKUP, "").trim();
285
+ return tail || null;
286
+ }
287
+ //# sourceMappingURL=askGate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_runId","require","nextId","idFactory","OFFER","VALUE_ASK","ASK","RegExp","source","TRAILING_MARKUP","LEADING_MARKUP","SENTENCE_END","OR_SPLIT","PARENTHETICAL","LIST_SPLIT","CLEAN_LABEL","CLEAN_OPTION","YES_NO","id","label","synthesizeAskBlock","text","sentence","lastSentence","length","ask","exec","listed","enumerated","kind","question","options","after","slice","index","replace","trim","halves","split","alt","alternatives","test","trimmed","parts","last","inside","labels","map","p","filter","Boolean","i","capitalize","h","toUpperCase","LIMITED","NOT_LOADED","GO_YOURSELF","TAIL_SENTENCES","ACQUISITION_CALLS","exports","Set","synthesizeShortfallBlock","attempted","tail","lastSentences","conceded","nextShortfallId","actions","primary","send","count","join"],"sourceRoot":"../../../src","sources":["engine/askGate.ts"],"mappings":";;;;;;;;AAiDA,IAAAA,MAAA,GAAAC,OAAA;AAjDA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAIA,MAAMC,MAAM,GAAG,IAAAC,gBAAS,EAAC,KAAK,CAAC;;AAE/B;AACA;AACA;AACA;AACA;AACA,MAAMC,KAAK,GACT,yLAAyL;;AAE3L;AACA;AACA;AACA;AACA;AACA,MAAMC,SAAS,GACb,iGAAiG;AAEnG,MAAMC,GAAG,GAAG,IAAIC,MAAM,CAAC,GAAGH,KAAK,CAACI,MAAM,IAAIH,SAAS,CAACG,MAAM,EAAE,EAAE,GAAG,CAAC;;AAElE;AACA,MAAMC,eAAe,GAAG,eAAe;AACvC,MAAMC,cAAc,GAAG,cAAc;;AAErC;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,YAAY,GAAG,qCAAqC;;AAE1D;AACA,MAAMC,QAAQ,GAAG,aAAa;;AAE9B;AACA,MAAMC,aAAa,GAAG,oBAAoB;AAC1C,MAAMC,UAAU,GAAG,mBAAmB;;AAEtC;AACA;AACA;AACA;AACA,MAAMC,WAAW,GAAG,wBAAwB;AAC5C;AAC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S;IACfuC,OAAO,EAAE,CACP;MACEjD,KAAK,EAAE,iBAAiB;MACxBkD,OAAO,EAAE,IAAI;MACb;MACA;MACAC,IAAI,EACF;IACJ,CAAC;EAEL,CAAC;AACH;AAEA,MAAMH,eAAe,GAAG,IAAAhE,gBAAS,EAAC,KAAK,CAAC;;AAExC;AACA,SAAS8D,aAAaA,CAAC5C,IAAY,EAAEkD,KAAa,EAAiB;EACjE,MAAM7B,OAAO,GAAGrB,IAAI,CAACc,OAAO,CAAC1B,eAAe,EAAE,EAAE,CAAC,CAAC2B,IAAI,CAAC,CAAC;EACxD,IAAI,CAACM,OAAO,EAAE,OAAO,IAAI;EACzB,MAAMC,KAAK,GAAGD,OAAO,CAACJ,KAAK,CAAC3B,YAAY,CAAC,CAACsC,MAAM,CAAED,CAAC,IAAKA,CAAC,CAACZ,IAAI,CAAC,CAAC,CAAC;EACjE,MAAM4B,IAAI,GAAGrB,KAAK,CAACV,KAAK,CAAC,CAACsC,KAAK,CAAC,CAACC,IAAI,CAAC,GAAG,CAAC,CAACrC,OAAO,CAACzB,cAAc,EAAE,EAAE,CAAC,CAAC0B,IAAI,CAAC,CAAC;EAC7E,OAAO4B,IAAI,IAAI,IAAI;AACrB","ignoreList":[]}
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.effectFor = effectFor;
7
7
  var _policy = require("../policy/policy");
8
+ var _digest = require("../effects/digest");
8
9
  /**
9
10
  * Turn a completed mutation into a ledger entry.
10
11
  *
@@ -20,7 +21,31 @@ var _policy = require("../policy/policy");
20
21
  * banner that admits three of them are permanent.
21
22
  */
22
23
 
23
- function effectFor(toolId, descriptor, params, result, before) {
24
+ /**
25
+ * The largest prior value an undo will hold. The ledger is persisted with the
26
+ * transcript under a 256KB cap; a payload past this is recorded as a visible,
27
+ * honestly-permanent change instead of silently evicting the conversation.
28
+ */
29
+ const MAX_UNDO_JSON_CHARS = 64_000;
30
+
31
+ /** The `getQueryData` read the engine takes on both sides of a cache write. */
32
+
33
+ const cacheRead = v => v && typeof v === "object" ? v : undefined;
34
+
35
+ /**
36
+ * How each query change is actually put back, named in the row so the bar is
37
+ * useful rather than merely honest. A cache edit's real inverse is a refetch:
38
+ * the tool's own description says a cache edit "lasts until the next
39
+ * successful refetch".
40
+ */
41
+ const QUERY_RESTORES = {
42
+ setQueryData: "query.invalidate",
43
+ triggerError: "query.restoreError",
44
+ triggerLoading: "query.restoreLoading",
45
+ setOnline: "query.setOnline"
46
+ };
47
+ function effectFor(toolId, descriptor, params, result, before, /** The query cache read before and after a `setQueryData` — see runAgentTurn. */
48
+ cache) {
24
49
  const label = (0, _policy.describeCall)(toolId, descriptor, params);
25
50
  const rec = result ?? {};
26
51
 
@@ -77,6 +102,62 @@ function effectFor(toolId, descriptor, params, result, before) {
77
102
  };
78
103
  }
79
104
 
105
+ /**
106
+ * A cache edit is a CHANGE, not a one-shot.
107
+ *
108
+ * `setQueryData` is the headline write of the whole product — "THE way to
109
+ * change what a server-backed screen shows" — and it fell through to
110
+ * `transient` here, which the changes bar deliberately does not count. So
111
+ * the sheet said "Anything it changes shows up in a bar at the top, and you
112
+ * can undo it" and then changed the screen in front of you with no row, no
113
+ * count and no undo. Same for `triggerError` and `triggerLoading`: the app
114
+ * is left showing a failure state that nothing on screen admits to putting
115
+ * there, which is exactly how a QA tester files a bug against a state Buoy
116
+ * created.
117
+ *
118
+ * `setQueryData` is now a real `query-write` above, because the engine
119
+ * reads the query's data before the write (captureStoreState) — the same
120
+ * pre-read that makes storage reversible. The rest — triggerError,
121
+ * triggerLoading, setOnline, a setQueryData whose pre-read failed — stay
122
+ * `state-write` (visible, honestly permanent): this module only claims an
123
+ * undo it can actually perform, and the label carries the real revert so
124
+ * the row is still useful.
125
+ */
126
+ if (toolId === "query" && descriptor.action === "setQueryData") {
127
+ const prior = cacheRead(cache?.before);
128
+ const queryHash = typeof params.queryHash === "string" ? params.queryHash : prior?.queryHash;
129
+ if (prior?.found === true && typeof queryHash === "string") {
130
+ const had = prior.hasData !== false && prior.data !== undefined;
131
+ const beforeData = had ? prior.data : undefined;
132
+ if ((0, _digest.jsonLength)(beforeData) <= MAX_UNDO_JSON_CHARS) {
133
+ const after = cacheRead(cache?.after);
134
+ const wroteDigest = after?.found === true && after.hasData !== false ? (0, _digest.digestOf)(after.data) : undefined;
135
+ return {
136
+ kind: "query-write",
137
+ queryHash,
138
+ before: beforeData,
139
+ wroteDigest,
140
+ label
141
+ };
142
+ }
143
+ return {
144
+ kind: "state-write",
145
+ toolId,
146
+ action: descriptor.action,
147
+ label: `${label} — prior value too large to hold for undo; undo with query.invalidate`
148
+ };
149
+ }
150
+ }
151
+ if (toolId === "query" && descriptor.effect !== "read") {
152
+ const restores = QUERY_RESTORES[descriptor.action];
153
+ return {
154
+ kind: "state-write",
155
+ toolId,
156
+ action: descriptor.action,
157
+ label: restores ? `${label} — undo with ${restores}` : label
158
+ };
159
+ }
160
+
80
161
  // Wipes with no captured prior state are permanent. Say so rather than
81
162
  // filing them under something Undo appears to cover.
82
163
  if (descriptor.effect === "destructive") {
@@ -1 +1 @@
1
- {"version":3,"names":["_policy","require","effectFor","toolId","descriptor","params","result","before","label","describeCall","rec","action","rule","ruleId","id","wasCreate","undefined","kind","key","isMmkv","startsWith","writeAction","instanceId","had","value","wrote","removeAction","effect"],"sourceRoot":"../../../src","sources":["engine/effectFor.ts"],"mappings":";;;;;;AAgBA,IAAAA,OAAA,GAAAC,OAAA;AAhBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAKO,SAASC,SAASA,CACvBC,MAAc,EACdC,UAA4B,EAC5BC,MAA+B,EAC/BC,MAAe,EACfC,MAAoD,EACzB;EAC3B,MAAMC,KAAK,GAAG,IAAAC,oBAAY,EAACN,MAAM,EAAEC,UAAU,EAAEC,MAAM,CAAC;EACtD,MAAMK,GAAG,GAAIJ,MAAM,IAAI,CAAC,CAA6B;;EAErD;EACA;EACA;EACA;EACA,IAA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OpB,MAAM,CAACmB,KAAK,KAAK,QAAQ,GAAGnB,MAAM,CAACmB,KAAK,GAAGR,SAAS;MAClEU,YAAY,EAAEP,MAAM,GAAG,aAAa,GAAG,kBAAkB;MACzDX;IACF,CAAC;EACH;;EAEA;EACA;EACA,IAAIJ,UAAU,CAACuB,MAAM,KAAK,aAAa,EAAE;IACvC,OAAO;MAAEV,IAAI,EAAE,aAAa;MAAEd,MAAM;MAAEQ,MAAM,EAAEP,UAAU,CAACO,MAAM;MAAEH;IAAM,CAAC;EAC1E;EAEA,OAAO;IAAES,IAAI,EAAE,WAAW;IAAET;EAAM,CAAC;AACrC","ignoreList":[]}
1
+ {"version":3,"names":["_policy","require","_digest","MAX_UNDO_JSON_CHARS","cacheRead","v","undefined","QUERY_RESTORES","setQueryData","triggerError","triggerLoading","setOnline","effectFor","toolId","descriptor","params","result","before","cache","label","describeCall","rec","action","rule","ruleId","id","wasCreate","kind","key","isMmkv","startsWith","writeAction","instanceId","had","value","wrote","removeAction","prior","queryHash","found","hasData","data","beforeData","jsonLength","after","wroteDigest","digestOf","effect","restores"],"sourceRoot":"../../../src","sources":["engine/effectFor.ts"],"mappings":";;;;;;AAgBA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAAC,OAAA,GAAAD,OAAA;AAjBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAMA;AACA;AACA;AACA;AACA;AACA,MAAME,mBAAmB,GAAG,MAAM;;AAElC;;AAQA,MAAMC,SAAS,GAAIC,CAAU,IAC3BA,CAAC,IAAI,OAAOA,CAAC,KAAK,QAAQ,GAAIA,CAAC,GAAiBC,SAAS;;AAE3D;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,cAAsC,GAAG;EAC7CC,YAAY,EAAE,kBAAkB;EAChCC,YAAY,EAAE,oBAAoB;EAClCC,cAAc,EAAE,sBAAsB;EACtCC,SAAS,EAAE;AACb,CAAC;AAEM,SAASC,SAASA,CACvBC,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