Skip to content

Commit 5403794

Browse files
committed
fix(agents): improve focus restoration and dynamic description truncation (anomalyco#4825)
- Rename initialAgent prop to focusedAgent for clarity - Add dynamic description truncation based on terminal width - Fix focus restoration when navigating between dialogs
1 parent 1ef332b commit 5403794

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

‎packages/opencode/src/cli/cmd/tui/component/dialog-agent-details.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function DialogAgentDetails(props: { agentName: string }) {
2525
useKeyboard((evt) => {
2626
if (evt.name === "escape" || evt.name === "return") {
2727
evt.preventDefault()
28-
dialog.replace(() => <DialogAgent initialAgent={props.agentName} />)
28+
dialog.replace(() => <DialogAgent focusedAgent={props.agentName} />)
2929
return
3030
}
3131
if (evt.name === "up" || (evt.ctrl && evt.name === "p")) {

‎packages/opencode/src/cli/cmd/tui/component/dialog-agent.tsx‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@ import { createMemo, createSignal } from "solid-js"
22
import { useLocal } from "@tui/context/local"
33
import { DialogSelect } from "@tui/ui/dialog-select"
44
import { useDialog } from "@tui/ui/dialog"
5+
import { useTerminalDimensions } from "@opentui/solid"
56
import { Keybind } from "@/util/keybind"
7+
import { Locale } from "@/util/locale"
68
import { DialogAgentDetails } from "./dialog-agent-details"
79

8-
export function DialogAgent(props: { initialAgent?: string }) {
10+
export function DialogAgent(props: { focusedAgent?: string }) {
911
const local = useLocal()
1012
const dialog = useDialog()
13+
const dimensions = useTerminalDimensions()
1114

12-
const [selectedAgentName, setSelectedAgentName] = createSignal(props.initialAgent ?? local.agent.current().name)
15+
const [selectedAgentName, setSelectedAgentName] = createSignal(props.focusedAgent ?? local.agent.current().name)
1316

14-
const selectedAgentHasPrompt = createMemo(() => {
17+
const selectedAgentHasDescription = createMemo(() => {
1518
const agent = local.agent.list().find((a) => a.name === selectedAgentName())
1619
return agent && !agent.builtIn && !!agent.description
1720
})
1821

22+
// Dialog width is min(60, terminalWidth - 2), minus ~10 for padding
23+
const availableWidth = createMemo(() => Math.min(60, dimensions().width - 2) - 10)
24+
1925
const options = createMemo(() =>
2026
local.agent.list().map((item) => {
2127
let description: string | undefined
@@ -28,12 +34,18 @@ export function DialogAgent(props: { initialAgent?: string }) {
2834
description = item.description
2935
}
3036

31-
description = description ? description.length > 35 ? description.slice(0, 32) + "..." : description : undefined
37+
// Truncate description based on available space after title
38+
const maxDescLen = availableWidth() - item.name.length - 1
39+
if (description && maxDescLen > 3) {
40+
description = Locale.truncate(description, maxDescLen)
41+
} else if (description && maxDescLen <= 3) {
42+
description = undefined
43+
}
3244

3345
return {
3446
value: item.name,
3547
title: item.name,
36-
description
48+
description,
3749
}
3850
}),
3951
)
@@ -42,7 +54,7 @@ export function DialogAgent(props: { initialAgent?: string }) {
4254
<DialogSelect
4355
title="Select agent"
4456
current={local.agent.current().name}
45-
defaultSelected={props.initialAgent}
57+
defaultSelected={props.focusedAgent}
4658
options={options()}
4759
onMove={(option) => setSelectedAgentName(option.value)}
4860
onSelect={(option) => {
@@ -54,7 +66,7 @@ export function DialogAgent(props: { initialAgent?: string }) {
5466
keybind: Keybind.parse("ctrl+e")[0],
5567
title: "show details",
5668
get disabled() {
57-
return !selectedAgentHasPrompt()
69+
return !selectedAgentHasDescription()
5870
},
5971
onTrigger: (option) => {
6072
dialog.replace(() => <DialogAgentDetails agentName={option.value} />)

0 commit comments

Comments
 (0)