Skip to content

Agent Zero AI framework

steps/groups tracking and bugfix; timeout for msgs

3clyp5036925876968e
diff --git a/webui/components/messages/process-group/process-group-store.js b/webui/components/messages/process-group/process-group-store.js
index 56a18a683..977695716 100644
--- a/webui/components/messages/process-group/process-group-store.js
+++ b/webui/components/messages/process-group/process-group-store.js
@@ -40,23 +40,15 @@ const DISPLAY_CODES = {
};
const model = {
- // Track which process groups are expanded (by group ID)
- expandedGroups: {},
-
- // Track which individual steps are expanded within a group
- expandedSteps: {},
-
// Default collapsed state for new process groups
defaultCollapsed: true,
init() {
try {
- // Load persisted state
+ // Load persisted default collapsed state only
const stored = localStorage.getItem("processGroupState");
if (stored) {
const parsed = JSON.parse(stored);
- this.expandedGroups = parsed.expandedGroups || {};
- this.expandedSteps = parsed.expandedSteps || {};
this.defaultCollapsed = parsed.defaultCollapsed ?? true;
}
} catch (e) {
@@ -66,9 +58,9 @@ const model = {
_persist() {
try {
+ // Only persist the default collapsed preference
+ // DOM is the source of truth for actual state
localStorage.setItem("processGroupState", JSON.stringify({
- expandedGroups: this.expandedGroups,
- expandedSteps: this.expandedSteps,
defaultCollapsed: this.defaultCollapsed
}));
} catch (e) {
@@ -78,43 +70,54 @@ const model = {
// Check if a process group is expanded
isGroupExpanded(groupId) {
- if (groupId in this.expandedGroups) {
- return this.expandedGroups[groupId];
+ const groupElement = document.getElementById(groupId);
+ if (groupElement) {
+ return groupElement.classList.contains("expanded");
}
return !this.defaultCollapsed;
},
// Toggle process group expansion
toggleGroup(groupId) {
- const current = this.isGroupExpanded(groupId);
- this.expandedGroups[groupId] = !current;
- this._persist();
+ const groupElement = document.getElementById(groupId);
+ if (!groupElement) return;
+
+ const currentState = groupElement.classList.contains("expanded");
+ groupElement.classList.toggle("expanded", !currentState);
},
// Expand a specific group
expandGroup(groupId) {
- this.expandedGroups[groupId] = true;
- this._persist();
+ const groupElement = document.getElementById(groupId);
+ if (groupElement) {
+ groupElement.classList.add("expanded");
+ }
},
// Collapse a specific group
collapseGroup(groupId) {
- this.expandedGroups[groupId] = false;
- this._persist();
+ const groupElement = document.getElementById(groupId);
+ if (groupElement) {
+ groupElement.classList.remove("expanded");
+ }
},
// Check if a step within a group is expanded
isStepExpanded(groupId, stepId) {
- const key = `${groupId}:${stepId}`;
- return this.expandedSteps[key] || false;
+ const stepElement = document.getElementById(`process-step-${stepId}`);
+ if (stepElement) {
+ return stepElement.classList.contains("step-expanded");
+ }
+ return false;
},
// Toggle step expansion
toggleStep(groupId, stepId) {
- const key = `${groupId}:${stepId}`;
- const currentState = this.expandedSteps[key] || false;
- this.expandedSteps[key] = !currentState;
- this._persist();
+ const stepElement = document.getElementById(`process-step-${stepId}`);
+ if (!stepElement) return;
+
+ const currentState = stepElement.classList.contains("step-expanded");
+ stepElement.classList.toggle("step-expanded", !currentState);
},
// Status code (3-4 letter) for backend log types
@@ -163,19 +166,6 @@ const model = {
// Clear state for a specific context (when chat is reset)
clearContext(contextPrefix) {
- // Clear groups matching the context
- for (const key of Object.keys(this.expandedGroups)) {
- if (key.startsWith(contextPrefix)) {
- delete this.expandedGroups[key];
- }
- }
- // Clear steps matching the context
- for (const key of Object.keys(this.expandedSteps)) {
- if (key.startsWith(contextPrefix)) {
- delete this.expandedSteps[key];
- }
- }
- this._persist();
},
// Get current detail mode from preferences
@@ -183,11 +173,10 @@ const model = {
return preferencesStore.detailMode || "current";
},
- expandGroup(groupId, isActiveAndGenerating = false) {
+ shouldExpandGroup(groupId, isActiveAndGenerating = false) {
const mode = this._getDetailMode();
if (mode === "collapsed") {
- // Only expand if generating, not for completed groups
- return isActiveAndGenerating;
+ return false;
}
if (mode === "current" || mode === "expanded") return true;
return !this.defaultCollapsed;
@@ -207,17 +196,20 @@ const model = {
const showUtils = preferencesStore.showUtils || false;
const allGroups = document.querySelectorAll(".process-group");
- // Find the last VISIBLE step using targeted selector
- const stepSelector = showUtils ? ".process-step" : ".process-step:not(.message-util)";
+ // Find the last visible step in an active (not completed) group only
+ const stepSelector = showUtils
+ ? ".process-group:not(.process-group-completed) .process-step"
+ : ".process-group:not(.process-group-completed) .process-step:not(.message-util)";
const visibleSteps = document.querySelectorAll(stepSelector);
- const lastStep = visibleSteps.length > 0 ? visibleSteps[visibleSteps.length - 1] : null;
+ const lastActiveStep = visibleSteps.length > 0 ? visibleSteps[visibleSteps.length - 1] : null;
// Get all steps for applying expansion
const allSteps = document.querySelectorAll(".process-step");
// Apply to groups
allGroups.forEach(group => {
- group.classList.toggle("expanded", mode !== "collapsed");
+ const shouldExpandGroup = mode !== "collapsed";
+ group.classList.toggle("expanded", shouldExpandGroup);
});
// Apply to steps
@@ -226,10 +218,15 @@ const model = {
if (mode === "expanded") {
shouldExpand = true;
} else if (mode === "current") {
- // Expand the last step and any parent steps containing it (for nested subordinate steps)
- shouldExpand = step === lastStep || step.contains(lastStep);
+ // Only expand the last step in an active group
+ shouldExpand = step === lastActiveStep || step.contains(lastActiveStep);
}
step.classList.toggle("step-expanded", shouldExpand);
+
+ // Clear user-pinned flag when mode changes
+ if (!shouldExpand) {
+ step.removeAttribute("data-user-pinned");
+ }
});
// Apply to error groups
diff --git a/webui/js/messages.js b/webui/js/messages.js
index d473324c2..67ee59949 100644
--- a/webui/js/messages.js
+++ b/webui/js/messages.js
@@ -9,6 +9,17 @@ import { store as stepDetailStore } from "/components/modals/process-step-detail
import { store as preferencesStore } from "/components/sidebar/bottom/preferences/preferences-store.js";
import { formatDuration } from "./time-utils.js";
+// ============================================
+// Timing Constants
+// ============================================
+// Delay before collapsing previous steps when a new step is added
+const STEP_COLLAPSE_DELAY_MS = 2000;
+// Delay before collapsing the last step when processing completes
+const FINAL_STEP_COLLAPSE_DELAY_MS = 2000;
+
+// Track active collapse timeouts for steps (key: step DOM element, value: timeout ID)
+const stepCollapseTimeouts = new Map();
+
const chatHistory = document.getElementById("chat-history");
let messageGroup = null;
@@ -1295,8 +1306,8 @@ function createProcessGroup(id) {
group.classList.add("process-group");
group.setAttribute("data-group-id", groupId);
- // Check initial expansion state from store
- const initiallyExpanded = processGroupStore.expandGroup(groupId, true); // true = is active
+ // Determine initial expansion state from current detail mode
+ const initiallyExpanded = processGroupStore.shouldExpandGroup(groupId, true); // true = is active
if (initiallyExpanded) {
group.classList.add('expanded');
}
@@ -1318,9 +1329,8 @@ function createProcessGroup(id) {
// Add click handler for expansion
header.addEventListener("click", (e) => {
+ // Toggle group (store directly modifies DOM - single source of truth)
processGroupStore.toggleGroup(groupId);
- const newState = processGroupStore.isGroupExpanded(groupId);
- group.classList.toggle("expanded", newState);
});
group.appendChild(header);
@@ -1364,6 +1374,70 @@ function getNestedContainer(parentStep) {
return innerWrapper || nestedContainer; // Fallback to container if wrapper missing
}
+/**
+ * Schedule a step to collapse after a delay
+ * Automatically handles cancellation on click and reset on hover
+ */
+function scheduleStepCollapse(stepElement, delayMs) {
+ // Cancel any existing timeout for this step
+ cancelStepCollapse(stepElement);
+
+ // Schedule the collapse
+ const timeoutId = setTimeout(() => {
+ stepElement.classList.remove("step-expanded");
+ stepCollapseTimeouts.delete(stepElement);
+ // Clear user-pinned flag when auto-collapsing
+ stepElement.removeAttribute("data-user-pinned");
+ }, delayMs);
+
+ // Store the timeout ID
+ stepCollapseTimeouts.set(stepElement, timeoutId);
+}
+
+/**
+ * Cancel a scheduled collapse for a step
+ */
+function cancelStepCollapse(stepElement) {
+ const timeoutId = stepCollapseTimeouts.get(stepElement);
+ if (timeoutId) {
+ clearTimeout(timeoutId);
+ stepCollapseTimeouts.delete(stepElement);
+ }
+}
+
+/**
+ * Add interaction handlers to prevent fighting with user
+ * - Hover: cancels the collapse timeout (keeps step open while reading)
+ * - Leave: starts a new timeout ONLY if user hasn't clicked (no explicit interaction)
+ * - Click anywhere on step: permanently cancels auto-collapse (user wants it open)
+ */
+function addStepCollapseInteractionHandlers(stepElement) {
+ // On hover, cancel the timeout to keep it open while user is reading
+ stepElement.addEventListener("mouseenter", () => {
+ if (stepElement.classList.contains("step-expanded")) {
+ cancelStepCollapse(stepElement);
+ }
+ });
+
+ // On leave, start a new timeout ONLY if user hasn't explicitly clicked
+ stepElement.addEventListener("mouseleave", () => {
+ // Don't restart timeout if user has explicitly interacted (clicked)
+ if (stepElement.classList.contains("step-expanded") &&
+ !stepElement.hasAttribute("data-user-pinned")) {
+ scheduleStepCollapse(stepElement, STEP_COLLAPSE_DELAY_MS);
+ }
+ });
+
+ // On click anywhere on step, permanently cancel auto-collapse
+ stepElement.addEventListener("click", () => {
+ if (stepElement.classList.contains("step-expanded")) {
+ cancelStepCollapse(stepElement);
+ // Mark as user-pinned so mouseleave won't restart timeout
+ stepElement.setAttribute("data-user-pinned", "true");
+ }
+ });
+}
+
/**
* Add a step to a process group
*/
@@ -1431,19 +1505,28 @@ function addProcessStep(group, id, type, heading, content, kvps, timestamp = nul
// Get step info from heading (single source of truth: backend)
const title = getStepTitle(heading, kvps, type);
- // Check if step should be expanded
- const isActiveStep = !isGroupCompleted && group.id === activeProcessGroupId;
- const isStepExpanded = processGroupStore.expandStep(groupId, id, isActiveStep);
-
- // In "current" mode, collapse all other steps
+ // Determine if this new step should be expanded
const detailMode = preferencesStore.detailMode;
- if (detailMode === "current" && isStepExpanded) {
- document.querySelectorAll(".process-step.step-expanded").forEach(s => {
- s.classList.remove("step-expanded");
+ let shouldExpand = false;
+
+ if (detailMode === "expanded") {
+ shouldExpand = true;
+ } else if (detailMode === "current" && !isGroupCompleted) {
+ // In "current" mode: expand new step, delay-collapse all previous steps
+ shouldExpand = true;
+
+ // Schedule collapse for ALL previously expanded steps
+ const allExpandedSteps = stepsContainer.querySelectorAll(".process-step.step-expanded");
+ allExpandedSteps.forEach(expandedStep => {
+ // Don't schedule collapse for the newly added step (the current one)
+ if (expandedStep.id !== `process-step-${id}`) {
+ scheduleStepCollapse(expandedStep, STEP_COLLAPSE_DELAY_MS);
+ }
});
}
+ // In "collapsed" mode: shouldExpand stays false
- if (isStepExpanded) {
+ if (shouldExpand) {
step.classList.add("step-expanded");
}
@@ -1467,14 +1550,16 @@ function addProcessStep(group, id, type, heading, content, kvps, timestamp = nul
// Add click handler for step expansion
stepHeader.addEventListener("click", (e) => {
e.stopPropagation();
+
+ // Cancel any scheduled auto-collapse (user is manually toggling)
+ cancelStepCollapse(step);
+
+ // Toggle step (store directly modifies DOM - single source of truth)
processGroupStore.toggleStep(groupId, id);
- const newState = processGroupStore.isStepExpanded(groupId, id);
- // Explicitly add or remove the class based on state
- if (newState) {
- step.classList.add("step-expanded");
- } else {
- step.classList.remove("step-expanded");
- }
+
+ // Clear user-pinned flag when manually toggling
+ // (allows auto-collapse to work again on next expansion)
+ step.removeAttribute("data-user-pinned");
});
step.appendChild(stepHeader);
@@ -1532,6 +1617,9 @@ function addProcessStep(group, id, type, heading, content, kvps, timestamp = nul
appendTarget.appendChild(step);
+ // Add interaction handlers to prevent fighting with user during auto-collapse
+ addStepCollapseInteractionHandlers(step);
+
// Scroll terminal to bottom on initial render (including page refresh)
const initialTerminal = step.querySelector(".terminal-output");
if (initialTerminal) {
@@ -2159,6 +2247,15 @@ function markProcessGroupComplete(group, responseTitle) {
// Add completed class to group
group.classList.add("process-group-completed");
+ // Collapse all expanded steps when processing is done (in "current" mode) with delay
+ const detailMode = preferencesStore.detailMode;
+ if (detailMode === "current") {
+ // Schedule collapse for all expanded steps (deterministic)
+ const allExpandedSteps = group.querySelectorAll(".process-step.step-expanded");
+ allExpandedSteps.forEach(expandedStep => {
+ scheduleStepCollapse(expandedStep, FINAL_STEP_COLLAPSE_DELAY_MS);
+ });
+ }
// Calculate final duration from backend data (sum of all step durations)
const steps = group.querySelectorAll(".process-step");
@@ -2190,6 +2287,10 @@ export function resetProcessGroups() {
activeStepTitleEl.classList.remove("shiny-text");
}
activeStepTitleEl = null;
+
+ // Clear all pending collapse timeouts
+ stepCollapseTimeouts.forEach(timeoutId => clearTimeout(timeoutId));
+ stepCollapseTimeouts.clear();
}
/**