mirror of
https://github.com/usestrix/strix.git
synced 2026-10-01 02:03:55 +00:00
feat(tui): animate the wait_for_agents indicator (#1383)
This commit is contained in:
parent
954bc0d527
commit
0ff9f8c324
7 changed files with 107 additions and 8 deletions
|
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
"github.com/usestrix/strix/tui/internal/render"
|
||||
)
|
||||
|
||||
type selectionCopiedMsg struct{ err error }
|
||||
|
|
@ -206,7 +207,7 @@ func (m *Model) toggleEventAtLine(line int) {
|
|||
|
||||
func (m Model) selectedText() string {
|
||||
fromLine, fromCol, toLine, toCol := m.selection.bounds()
|
||||
source := m.viewportContent
|
||||
source := render.StopSpinners(m.viewportContent)
|
||||
if m.selection.region == regionInput {
|
||||
source = m.inputText()
|
||||
}
|
||||
|
|
|
|||
24
strix/interface/tui/internal/app/spinner_test.go
Normal file
24
strix/interface/tui/internal/app/spinner_test.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
"github.com/usestrix/strix/tui/internal/protocol"
|
||||
)
|
||||
|
||||
func TestParkedWaitSpins(t *testing.T) {
|
||||
model := New(nil)
|
||||
model.width, model.height, model.showSplash, model.ready = 130, 30, false, true
|
||||
model.snapshot = protocol.Snapshot{
|
||||
Agents: []protocol.Agent{{ID: "one", Name: "Agent", Status: "waiting"}},
|
||||
Events: []protocol.Event{{ID: "1", AgentID: "one", Type: "tool", Data: map[string]any{"tool_name": "wait_for_agents"}}},
|
||||
}
|
||||
model.resizeViewport()
|
||||
before := ansi.Strip(model.View())
|
||||
model.sweepFrame += 2
|
||||
if after := ansi.Strip(model.View()); strings.Contains(before, "○ waiting") || before == after {
|
||||
t.Fatalf("wait line did not spin:\n%s", before)
|
||||
}
|
||||
}
|
||||
|
|
@ -28,15 +28,16 @@ type renderedBlock struct {
|
|||
version int
|
||||
width int
|
||||
expanded bool
|
||||
live bool
|
||||
wrapped string
|
||||
expandable bool
|
||||
height int
|
||||
}
|
||||
|
||||
func (m *Model) renderEvent(event protocol.Event, width int) renderedBlock {
|
||||
func (m *Model) renderEvent(event protocol.Event, width int, live bool) renderedBlock {
|
||||
expanded := m.expandedEvents[event.ID]
|
||||
if cached, ok := m.blockCache[event.ID]; ok &&
|
||||
cached.version == event.Version && cached.width == width && cached.expanded == expanded {
|
||||
cached.version == event.Version && cached.width == width && cached.expanded == expanded && cached.live == live {
|
||||
return cached
|
||||
}
|
||||
var block string
|
||||
|
|
@ -48,7 +49,10 @@ func (m *Model) renderEvent(event protocol.Event, width int) renderedBlock {
|
|||
name := render.StringValue(event.Data["tool_name"])
|
||||
block, expandable = render.CollapseTool(render.Tool(event.Data), name, expanded)
|
||||
}
|
||||
entry := renderedBlock{version: event.Version, width: width, expanded: expanded, expandable: expandable}
|
||||
if !live {
|
||||
block = render.StopSpinners(block)
|
||||
}
|
||||
entry := renderedBlock{version: event.Version, width: width, expanded: expanded, live: live, expandable: expandable}
|
||||
if block != "" {
|
||||
entry.wrapped = wrapBlock(block, width)
|
||||
entry.height = strings.Count(entry.wrapped, "\n") + 1
|
||||
|
|
@ -96,6 +100,15 @@ func (m *Model) chatContent() string {
|
|||
// to width-2 and indent every line by one cell.
|
||||
contentWidth := max(1, m.viewport.Width-2)
|
||||
render.SetImageWidth(contentWidth - 2)
|
||||
// A parked agent is waiting on its latest tool call.
|
||||
parkedOn := ""
|
||||
if m.snapshot.Agents[m.selectedAgent].Status == "waiting" {
|
||||
for _, event := range events {
|
||||
if event.AgentID == agentID && event.Type == "tool" {
|
||||
parkedOn = event.ID
|
||||
}
|
||||
}
|
||||
}
|
||||
var blocks []string
|
||||
var spans []eventSpan
|
||||
line := 0
|
||||
|
|
@ -103,7 +116,7 @@ func (m *Model) chatContent() string {
|
|||
if event.AgentID != agentID {
|
||||
continue
|
||||
}
|
||||
entry := m.renderEvent(event, contentWidth)
|
||||
entry := m.renderEvent(event, contentWidth, event.ID == parkedOn)
|
||||
if entry.wrapped == "" {
|
||||
continue
|
||||
}
|
||||
|
|
@ -437,6 +450,7 @@ type chatPaneKey struct {
|
|||
width, height int
|
||||
border lipgloss.Color
|
||||
selection selectionState
|
||||
spinnerFrame int
|
||||
}
|
||||
|
||||
// chatPane memoizes the bordered trace: slicing, scrollbar padding and border
|
||||
|
|
@ -450,12 +464,18 @@ var chatPane struct {
|
|||
}
|
||||
|
||||
func (m Model) renderChatPane(width, height int, border lipgloss.Color) string {
|
||||
key := chatPaneKey{offset: m.viewport.YOffset, width: width, height: height, border: border, selection: m.selection}
|
||||
visible := visibleContent(m.viewportContent, m.viewport.YOffset, height)
|
||||
// Only a trace with a spinner on screen changes with the tick.
|
||||
spinnerFrame := 0
|
||||
if strings.Contains(visible, render.SpinnerMarker) {
|
||||
spinnerFrame = m.sweepFrame / 2
|
||||
}
|
||||
key := chatPaneKey{offset: m.viewport.YOffset, width: width, height: height, border: border, selection: m.selection, spinnerFrame: spinnerFrame}
|
||||
if chatPane.out != "" && chatPane.key == key && chatPane.content == m.viewportContent {
|
||||
return chatPane.out
|
||||
}
|
||||
trace := withVerticalScrollbar(
|
||||
m.highlightSelection(visibleContent(m.viewportContent, m.viewport.YOffset, height), m.viewport.YOffset),
|
||||
render.AnimateSpinners(m.highlightSelection(visible, m.viewport.YOffset), spinnerFrame),
|
||||
width,
|
||||
height,
|
||||
m.viewport.TotalLineCount(),
|
||||
|
|
|
|||
|
|
@ -55,7 +55,8 @@ func renderAgentGraphTool(name string, args map[string]any, result any) string {
|
|||
b.WriteString("\n " + Dim().Render("Completing task..."))
|
||||
}
|
||||
case "wait_for_agents":
|
||||
b.WriteString(Col(Gray).Render("○ ") + Dim().Render("waiting"))
|
||||
// The chat pane animates the marker while the agent is parked here.
|
||||
b.WriteString(Col(Gray).Render(SpinnerMarker+" ") + Dim().Render("waiting"))
|
||||
if reason := StringValue(args["reason"]); reason != "" {
|
||||
b.WriteString("\n " + Dim().Render(reason))
|
||||
}
|
||||
|
|
|
|||
18
strix/interface/tui/internal/render/spinner.go
Normal file
18
strix/interface/tui/internal/render/spinner.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package render
|
||||
|
||||
import "strings"
|
||||
|
||||
// SpinnerMarker is a one-cell placeholder for a spinner. Rendered blocks are
|
||||
// cached, so the chat pane swaps in the current frame (AnimateSpinners) or a
|
||||
// still circle once the wait is over (StopSpinners).
|
||||
const SpinnerMarker = "\uE000"
|
||||
|
||||
var spinnerFrames = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
|
||||
|
||||
func StopSpinners(s string) string {
|
||||
return strings.ReplaceAll(s, SpinnerMarker, "○")
|
||||
}
|
||||
|
||||
func AnimateSpinners(s string, tick int) string {
|
||||
return strings.ReplaceAll(s, SpinnerMarker, spinnerFrames[tick%len(spinnerFrames)])
|
||||
}
|
||||
|
|
@ -69,6 +69,7 @@ class GoTuiRuntime:
|
|||
self.scan_error: BaseException | None = None
|
||||
self._last_sync_fingerprint = ""
|
||||
self._error_noted_agents: set[str] = set()
|
||||
self._output_syncs: set[asyncio.Task[None]] = set()
|
||||
self.model_verified = False
|
||||
self._setup_preflight: asyncio.Task[None] | None = None
|
||||
self.controller = TuiController(
|
||||
|
|
@ -276,6 +277,18 @@ class GoTuiRuntime:
|
|||
|
||||
def capture_event(self, agent_id: str, event: Any) -> None:
|
||||
self.live_view.ingest_sdk_event(agent_id, event)
|
||||
if getattr(getattr(event, "item", None), "type", "") == "tool_call_output_item":
|
||||
# A tool that parks its agent has already set the agent's status by
|
||||
# the time it returns; sync it now so both reach the TUI together.
|
||||
task = asyncio.get_running_loop().create_task(self._sync_and_notify())
|
||||
self._output_syncs.add(task)
|
||||
task.add_done_callback(self._output_syncs.discard)
|
||||
return
|
||||
self.controller.notify_changed()
|
||||
|
||||
async def _sync_and_notify(self) -> None:
|
||||
with contextlib.suppress(Exception):
|
||||
await self._sync_agent_state()
|
||||
self.controller.notify_changed()
|
||||
|
||||
def capture_mcp_status(self, roster: list[dict[str, Any]]) -> None:
|
||||
|
|
|
|||
|
|
@ -924,6 +924,28 @@ async def test_agent_state_sync_uses_latest_graph_snapshot_shape() -> None:
|
|||
assert child["error_message"] == "provider rejected request"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_tool_output_carries_the_status_it_parked_the_agent_in() -> None:
|
||||
runtime = GoTuiRuntime(args())
|
||||
await runtime.coordinator.register("root", "Strix", parent_id=None)
|
||||
await runtime._sync_agent_state()
|
||||
notified: list[str] = []
|
||||
runtime.controller._on_change = lambda: notified.append(
|
||||
runtime.live_view.agents["root"]["status"]
|
||||
)
|
||||
|
||||
await runtime.coordinator.park_waiting("root", wait_kind="agents")
|
||||
output = SimpleNamespace(
|
||||
type="tool_call_output_item",
|
||||
raw_item={"call_id": "call-1", "type": "function_call_output"},
|
||||
output=json.dumps({"success": True, "wait_outcome": "waiting"}),
|
||||
)
|
||||
runtime.capture_event("root", SimpleNamespace(type="run_item_stream_event", item=output))
|
||||
await asyncio.gather(*runtime._output_syncs)
|
||||
|
||||
assert notified == ["waiting"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_agent_state_sync_projects_completed_report() -> None:
|
||||
runtime = GoTuiRuntime(args())
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue