From bd1eb9ef9b3b89ff5825eb8bf9b2e978167c9ad0 Mon Sep 17 00:00:00 2001 From: Igor Soarez Date: Tue, 3 Nov 2026 13:05:57 +0200 Subject: show each pi agent's context use: a meter in the panel and search, text in the menu --- crates/dinggerd/src/hooks.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'crates/dinggerd/src/hooks.rs') diff --git a/crates/dinggerd/src/hooks.rs b/crates/dinggerd/src/hooks.rs index 1718ff8..82fa7bd 100644 --- a/crates/dinggerd/src/hooks.rs +++ b/crates/dinggerd/src/hooks.rs @@ -28,7 +28,9 @@ //! on an old extension once it is idle and off screen. //! //! It also carries the `draft` in the agent's input box ("" for none), for -//! marking a draft you left behind; never logged. +//! marking a draft you left behind; never logged. And `context`: how full +//! the agent's context window is, in percent, as pi's footer shows it ("" +//! when unknown, as right after compacting). //! //! Each line is answered with `{"ok":true}`. //! @@ -74,6 +76,8 @@ struct PaneReports { control: Option, /// The draft in the agent's input box, as one line ("" for none). draft: Option, + /// How full its context window is: whole percent ("" when unknown). + context: Option, /// When the last report arrived. at: Option, } @@ -172,6 +176,8 @@ struct HookParams { control: Option, #[serde(default)] draft: Option, + #[serde(default)] + context: Option, } fn handle_stream(stream: UnixStream) { @@ -239,6 +245,7 @@ fn handle_stream(stream: UnixStream) { extension: params.extension, control: params.control, draft: params.draft, + context: params.context, }; record(params.pane_id, state, reported); wake(); @@ -253,6 +260,7 @@ struct Reported { extension: Option, control: Option, draft: Option, + context: Option, } /// The longest draft kept: enough to find an agent by it. @@ -298,6 +306,24 @@ fn record(pane_id: String, state: Option, reported: Reported) { if let Some(draft) = reported.draft { pane.draft = Some(draft_line(&draft)); } + if let Some(context) = reported.context { + pane.context = Some(context_percent(&context)); + } +} + +/// A reported context use as whole percent ("12.6" is "13"); "" for +/// unknown, or anything not a percentage. +fn context_percent(value: &str) -> String { + match value.trim().parse::() { + Ok(percent) if percent.is_finite() && percent >= 0.0 => format!("{:.0}", percent.min(999.0)), + _ => String::new(), + } +} + +/// How full the pane's agent's context window is ("" when unknown), if its +/// extension reports it. +pub fn context(pane_id: &str) -> Option { + reports().get(pane_id).and_then(|pane| pane.context.clone()) } /// Start listening; reports accumulate for the watcher to consume. @@ -440,6 +466,21 @@ mod tests { assert_eq!(extension("%t4").0.as_deref(), Some("v2")); } + #[test] + fn context_use_is_kept_as_whole_percent_until_reported_again() { + let context = |text: &str| Reported { context: Some(text.into()), ..Default::default() }; + assert_eq!(super::context("%t6"), None, "an extension that does not report it"); + record("%t6".into(), None, context("12.6")); + assert_eq!(super::context("%t6").as_deref(), Some("13")); + record("%t6".into(), None, Reported::default()); + assert_eq!(super::context("%t6").as_deref(), Some("13"), "kept by a report without one"); + record("%t6".into(), None, context("")); + assert_eq!(super::context("%t6").as_deref(), Some(""), "unknown, after compacting"); + for (value, want) in [("0", "0"), ("0.4", "0"), ("97.5", "98"), ("1e9", "999"), ("-3", ""), ("NaN", ""), ("x", "")] { + assert_eq!(context_percent(value), want, "{value}"); + } + } + #[test] fn a_draft_is_kept_as_one_short_line_until_reported_again() { let draft = |text: &str| Reported { draft: Some(text.into()), ..Default::default() }; -- cgit v1.2.3