Skip to content

Commit 231babc

Browse files
authored
fix(discord): explore public data inline with whole-tree depth controls (#334)
Keep the original reply and show two data levels initially. Expand and Collapse change depth everywhere; paging stays at the selected depth. Hide restores the original card without rerunning or refetching data. Share bounded inline pane framing and retain snapshot-token, actual-reply, revision, ownership and uncertain-acknowledgement protections.
1 parent a5216f5 commit 231babc

7 files changed

Lines changed: 391 additions & 183 deletions

File tree

‎doc/CHANGELOG.md‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## Unreleased
44

5+
- **Explore data** expands inside the original Discord reply and becomes
6+
**Hide data**. The public JSON tree initially shows two levels; **Expand**
7+
and **Collapse** change one level everywhere. Large views are paged without
8+
rerunning or refetching data. Hiding restores the original answer, media/files
9+
and elapsed caption. Data, tool and error panes share one bounded restoration
10+
base; stale or foreign controls cannot update another reply.
511
- **Show details** now expands the frozen public tool-call summary inside the
612
original Discord reply and becomes **Hide details**. Hiding restores the
713
original answer, files/media and elapsed caption. Inspection never reruns
@@ -11,10 +17,9 @@
1117
read-only inline navigation and no extra response. Collapsing restores the
1218
original card; files, controls and one original elapsed caption are preserved.
1319
Updates require the actual tracked reply, frozen snapshot and current revision.
14-
**Explore data** remains a separate public viewer.
1520
- Rich Discord command errors offer **Error details**. Supported commands offer
16-
**Explore data** from explicitly reviewed public projections, with nested
17-
field/item selection, Back and paging. Views are public and read-only; no
21+
**Explore data** from explicitly reviewed public projections. Error details
22+
retain field/item selection, Back and paging. Views are public and read-only; no
1823
command is rerun. Snapshots are bounded, frozen at completion and expire with
1924
command revisions/cache eviction. Unreviewed result data and private upstream
2025
bodies are never automatically public.

‎doc/PUBLIC_RESULTS.md‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,30 @@ Current limits: eight data records and eight diagnostic records; six nested
4545
levels below the root, 32 fields/items per container, 1,200 UTF-16 characters per
4646
string. Data has a 192-value/8,000-character budget; diagnostics reserve a
4747
separate 64-value/4,000-character budget. Character budgets apply to retained
48-
keys/strings, not serialized JSON byte counts. Views show eight fields/items per
49-
page and announce that large snapshots may be shortened.
48+
keys/strings, not serialized JSON byte counts. Large snapshots may be shortened
49+
when retained; opening a view never refetches missing data.
5050

51-
Navigation is public and read-only. Components refer to bounded snapshot node
52-
IDs, not arbitrary paths or command arguments. Each view is revision-token
53-
bound and restricted to bot-authored messages in the command's reply
54-
channel/thread. **Explore data** opens a separate viewer; its field, Back and
55-
paging controls update that viewer without replacing the answer.
51+
Navigation is public and read-only. Controls select bounded snapshot depth,
52+
page or diagnostic node IDs, never arbitrary paths or command arguments. Each
53+
inline view requires the actual tracked bot reply, channel/thread, frozen
54+
snapshot token and current revision.
55+
56+
**Explore data** expands inside the original reply and becomes **Hide data**.
57+
It initially shows two levels (or all available levels for shallower data).
58+
**Expand** shows one more level everywhere; **Collapse** shows one fewer level
59+
and both reset to the first page. Collapsed objects/arrays show type/count
60+
summaries. Scalars retain their JSON types. The JSON preview is paged in
61+
surrogate-safe chunks of at most 1,500 UTF-16 characters, preferably at line
62+
boundaries; a large JSON document can continue across pages. Previous/Next
63+
preserve depth. Depth and paging buttons are disabled at their bounds. There
64+
is no one-field dropdown or separately posted viewer. Hiding restores the exact
65+
original card; reopening starts at the two-level overview.
5666

5767
**Error details** expands the tracked original reply in place. Its navigation
5868
also stays inline, and **Hide error details** restores the exact original card.
5969
Only JSON-only outbound public presentation is retained for restoration, never
6070
upload streams or raw result data. Restoration is capped at the ordinary
61-
30-reply limit; overflow notices do not offer an inline error control.
71+
30-reply limit; overflow notices do not offer unrestorable inline controls.
6272
Attachments are neither uploaded again nor cleared, and one original elapsed
6373
caption is preserved. Long error text may be
6474
shortened only while details are open; collapsing restores it in full.
@@ -71,10 +81,12 @@ The summary retains only the existing bounded status text (at most 1,800 UTF-16
7181
characters), never raw tool arguments/results, private reasoning or provider
7282
responses. Late status writes do not alter an already-published summary.
7383

74-
Tool and error panes share the same JSON-only restoration base and 30-reply
75-
limit; opening either replaces the other pane. Long answers may be shortened
76-
only while the tool pane is open; Hide restores their original presentation.
77-
Neither opening nor hiding executes a command, tool or upstream request.
84+
Data, tool and error panes share the same JSON-only restoration base and
85+
30-reply limit; opening one replaces the other pane. Long answers may be
86+
shortened only while a pane is open; Hide restores their original presentation.
87+
No opening, hiding, depth or paging action executes a command, tool or upstream
88+
request. Data navigation is stateless: its tokenized controls encode only a
89+
bounded depth and page, not mutable paths or new copies of result data.
7890

7991
Inline updates additionally require the actual tracked reply ID, matching
8092
snapshot token and current revision. Updates serialize with reply replacement,

‎src/yetibot/core/adapters/discord.clj‎

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@
390390
:data (components/interaction-payload text)))
391391

392392
(defn- inline-details! [conn event entry expected
393-
{:keys [action key token node page]}]
393+
{:keys [action key token node page depth]}]
394394
(binding [*delivery-deadline* (+ (monotonic-time) 2500000000)]
395395
(with-command-lock (:lock entry)
396396
(let [state (:inspection entry)
@@ -412,6 +412,12 @@
412412
(when (seq (:tool-details reply))
413413
(:inspection-base reply))
414414
"error-hide" (:inspection-base reply)
415+
"data-hide" (when (:data? @state)
416+
(:inspection-base reply))
417+
"data" (inspection/inline-data-payload
418+
state key (:inspection-base reply) nil 0)
419+
"tree" (inspection/inline-data-payload
420+
state key (:inspection-base reply) depth page)
415421
(inspection/inline-error-payload
416422
state key (:inspection-base reply)
417423
(or node 0) (or page 0)
@@ -457,7 +463,7 @@
457463

458464
(defmethod handle-event :interaction-create
459465
[_ event conn yetibot-user]
460-
(when-let [{:keys [action key confirmation token kind node page] :as control}
466+
(when-let [{:keys [action key confirmation token kind] :as control}
461467
(or (components/parse-button (get-in event [:data :custom-id]))
462468
(inspection/parse-control (get-in event [:data :custom-id])))]
463469
;; Claim an interaction once before dispatch. Never retain its token.
@@ -484,44 +490,34 @@
484490
(identical? (:inspection entry)
485491
(get-in @conn [:commands key :inspection]))
486492
(or (not (contains? #{"view" "error-hide" "details"
487-
"details-hide"} action))
493+
"details-hide" "data" "data-hide"
494+
"tree"} action))
488495
(= token (:token @(:inspection entry))))
489-
(if (and (= action "view") (= kind "d"))
490-
(some #(= (:channel-id event) (:target %))
491-
@(:replies entry))
492-
(and
493-
(or (not confirm?)
494-
(and (= confirmation (:token pending))
495-
(identical? expected @(:revision entry))))
496-
(some #(and (= (:channel-id event) (:target %))
497-
(= (if confirm? (:reply-id pending)
498-
(get-in event [:message :id]))
499-
(:id %)))
500-
@(:replies entry)))))]
496+
(not (and (= action "view") (= kind "d")))
497+
(or (not confirm?)
498+
(and (= confirmation (:token pending))
499+
(identical? expected @(:revision entry))))
500+
(some #(and (= (:channel-id event) (:target %))
501+
(= (if confirm? (:reply-id pending)
502+
(get-in event [:message :id]))
503+
(:id %)))
504+
@(:replies entry)))]
501505
(cond
502506
(not known?)
503507
(interaction-reply! conn event
504508
"This command's buttons have expired. Please send the command again.")
505509

506-
(or (contains? #{"error" "error-hide" "details" "details-hide"} action)
510+
(or (contains? #{"error" "error-hide" "details" "details-hide"
511+
"data" "data-hide" "tree"} action)
507512
(and (= action "view") (= kind "e")))
508513
(when-not (inline-details! conn event entry expected control)
509514
(interaction-reply! conn event
510-
(if (contains? #{"details" "details-hide"} action)
515+
(cond
516+
(contains? #{"details" "details-hide"} action)
511517
"This tool view has expired or is unavailable."
512-
"This error view has expired or is unavailable.")))
513-
514-
(contains? #{"view" "data"} action)
515-
(if-let [data (inspection/payload
516-
(:inspection entry) key
517-
(or kind "d")
518-
(or node 0) (or page 0)
519-
(get-in event [:data :values]))]
520-
(messaging/create-interaction-response!
521-
(:rest @conn) (:id event) (:token event)
522-
(if (= action "view") 7 4) :data data)
523-
(interaction-reply! conn event
524-
"This data view has expired or is unavailable."))
518+
(contains? #{"data" "data-hide" "tree"} action)
519+
"This data view has expired or is unavailable."
520+
:else "This error view has expired or is unavailable.")))
525521

526522
(= action "help")
527523
(let [body (second (command/extract-command (:content source)))
@@ -622,9 +618,11 @@
622618
(not= false (:inspection-restorable? part)))
623619
@(:details-state part))
624620
:details-token (some-> (:inspection part) deref :token))
621+
(:inspection part)
622+
(assoc :data-token (:token @(:inspection part)))
625623
(some-> (:inspection part) deref :sealed?)
626624
(merge (select-keys @(:inspection part) [:data? :error?]))
627-
(false? (:inspection-restorable? part)) (assoc :error? false)
625+
(false? (:inspection-restorable? part)) (assoc :error? false :data? false)
628626
(:timing part) (assoc :elapsed (elapsed-label (:timing part))))))
629627

630628
(defn- create-message! [conn target {:keys [image file filename] :as part}]
@@ -687,6 +685,7 @@
687685
(:inspection part)
688686
(:sealed? @(:inspection part))
689687
(or (:error? @(:inspection part))
688+
(:data? @(:inspection part))
690689
(some-> (:details-state part) deref seq)))
691690
(assoc :inspection-token (:token @(:inspection part))
692691
:tool-details (some-> (:details-state part) deref)

‎src/yetibot/core/adapters/discord_components.clj‎

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -112,28 +112,31 @@
112112
(defn parse-button [id]
113113
(when (string? id)
114114
(when-let [[_ action channel command token]
115-
(re-matches #"yb:(again|help|details|details-hide|confirm|data|error):([^:]+):([^:]+)(?::([a-f0-9-]+))?" id)]
116-
(let [details? (contains? #{"details" "details-hide"} action)]
117-
(when (if details?
118-
(or (and (= action "details") (nil? token))
115+
(re-matches #"yb:(again|help|details|details-hide|confirm|data|data-hide|error):([^:]+):([^:]+)(?::([a-f0-9-]+))?" id)]
116+
(let [snapshot? (contains? #{"details" "details-hide" "data" "data-hide"}
117+
action)]
118+
(when (if snapshot?
119+
(or (and (contains? #{"details" "data"} action) (nil? token))
119120
(and token (re-matches #"[a-f0-9]{16}" token)))
120121
(= (= action "confirm") (some? token)))
121122
(cond-> {:action action :key [channel command]
122-
:confirmation (when-not details? token)}
123-
(and details? token) (assoc :token token)))))))
123+
:confirmation (when-not snapshot? token)}
124+
(and snapshot? token) (assoc :token token)))))))
124125

125126
(defn payload
126127
"Build a V2 card. Details is a bounded caller-sanitized snapshot, never raw data."
127128
[{:keys [content image file filename command-key details details-token elapsed
128-
data? error? allowed-mentions]}]
129+
data? data-token error? allowed-mentions]}]
129130
(let [media (or (when image (str "attachment://" filename)) (media-url content))
130131
buttons (when command-key
131132
(mapv (fn [[action label]]
132133
{:type 2 :style 2 :label label
133134
:custom_id (str (button-id action command-key)
134-
(when (and (= action "details")
135-
details-token)
136-
(str ":" details-token)))})
135+
(when-let [token (case action
136+
"details" details-token
137+
"data" data-token
138+
nil)]
139+
(str ":" token)))})
137140
(cond-> [["again" "Run again"] ["help" "Help"]]
138141
(seq details) (conj ["details" "Show details"])
139142
error? (conj ["error" "Error details"])
@@ -149,27 +152,26 @@
149152
:components [{:type 17 :accent_color accent-color :components children}]
150153
:allowed-mentions (or allowed-mentions {:parse [] :replied_user false})}))
151154

152-
(defn inline-details-payload
153-
"Expand only frozen, caller-sanitized tool progress inside a public card.
154-
No streams or attachment edits; Hide uses the untouched original base."
155-
[base key token details]
156-
(when (and (string? details) (seq details)
157-
(= 1 (count (:components base)))
155+
(defn inline-pane-payload
156+
"Insert a bounded public pane before the original footer/controls. Retain
157+
media/files without attachment edits; Hide uses the untouched JSON-only base."
158+
[base pane show-id hide-id hide-label]
159+
(when (and (= 1 (count (:components base)))
158160
(= 17 (get-in base [:components 0 :type])))
159161
(let [children (get-in base [:components 0 :components])
162+
maps #(filter map? (tree-seq coll? seq %))
160163
text-length #(reduce + 0 (keep (fn [c]
161-
(when (= 10 (:type c))
162-
(count (:content c)))) %))
163-
pane {:type 10 :content (str "**Tool details**\n" (excerpt details 1800))}
164-
shorten? (> (+ (text-length children) (count (:content pane)))
164+
(when (= 10 (:type c))
165+
(count (:content c)))) (maps %)))
166+
shorten? (> (+ (text-length children) (text-length pane))
165167
max-text-length)
166168
children (mapv (fn [c]
167169
(if (and shorten? (= 10 (:type c))
168170
(> (count (:content c)) 1400))
169171
(update c :content
170172
#(str (answer-preview % 650)
171-
"\n-# Answer shortened while details are open."
172-
" Hide details to restore it."))
173+
"\n-# Answer shortened while this view is open. "
174+
hide-label " to restore it."))
173175
c)) children)
174176
start (or (first (keep-indexed
175177
(fn [i c]
@@ -179,19 +181,27 @@
179181
(:content c)))) i))
180182
children))
181183
(count children))
182-
children (into (conj (subvec children 0 start)
183-
{:type 14 :divider true :spacing 1} pane)
184+
children (into (into (conj (subvec children 0 start)
185+
{:type 14 :divider true :spacing 1}) pane)
184186
(subvec children start))
185-
show-id (str (button-id "details" key) ":" token)
186187
children (walk/postwalk
187188
(fn [c]
188189
(if (and (map? c) (= show-id (:custom_id c)))
189-
(assoc c :label "Hide details"
190-
:custom_id (str (button-id "details-hide" key)
191-
":" token))
192-
c)) children)]
193-
(when (<= (text-length children) max-text-length)
194-
(assoc-in base [:components 0 :components] children)))))
190+
(assoc c :label hide-label :custom_id hide-id) c))
191+
children)
192+
result (assoc-in base [:components 0 :components] children)]
193+
(when (and (<= (text-length children) max-text-length)
194+
(<= (count (filter :type (maps result))) 40))
195+
result))))
196+
197+
(defn inline-details-payload
198+
"Expand only frozen, caller-sanitized tool progress inside a public card."
199+
[base key token details]
200+
(when (and (string? details) (seq details))
201+
(inline-pane-payload
202+
base [{:type 10 :content (str "**Tool details**\n" (excerpt details 1800))}]
203+
(str (button-id "details" key) ":" token)
204+
(str (button-id "details-hide" key) ":" token) "Hide details")))
195205

196206
(defn message-text
197207
"Read visible text/media from legacy or V2 messages, excluding button labels."

0 commit comments

Comments
 (0)