Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions packages/cli/src/ui/AppContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ import {
type WaitingToolCall,
} from '@qwen-code/qwen-code-core';
import { buildResumedHistoryItems } from './utils/resumeHistoryUtils.js';
import { getStickyTodos } from './utils/todoSnapshot.js';
import {
getStickyTodos,
getStickyTodoMaxVisibleItems,
getStickyTodosLayoutKey,
getStickyTodosRenderKey,
} from './utils/todoSnapshot.js';
import type { TodoItem } from './components/TodoDisplay.js';
import { validateAuthMethod } from '../config/auth.js';
import { loadHierarchicalGeminiMemory } from '../config/config.js';
import process from 'node:process';
Expand Down Expand Up @@ -160,6 +166,20 @@ function isToolExecuting(pendingHistoryItems: HistoryItemWithoutId[]) {
});
}

function useStableStickyTodos(todos: TodoItem[] | null): TodoItem[] | null {
const renderKey = getStickyTodosRenderKey(todos);
const stableTodosRef = useRef<{
renderKey: string;
todos: TodoItem[] | null;
} | null>(null);

if (stableTodosRef.current?.renderKey !== renderKey) {
stableTodosRef.current = { renderKey, todos };
}

return stableTodosRef.current.todos;
}

// Exported for tests. Given a newest-first list of messages, return a list
// with duplicates removed, keeping the first (newest) occurrence of each.
export function dedupeNewestFirst(messages: readonly string[]): string[] {
Expand Down Expand Up @@ -1230,10 +1250,11 @@ export const AppContainer = (props: AppContainerProps) => {
() => [...pendingSlashCommandHistoryItems, ...pendingGeminiHistoryItems],
[pendingSlashCommandHistoryItems, pendingGeminiHistoryItems],
);
const stickyTodos = useMemo(
const rawStickyTodos = useMemo(
() => getStickyTodos(historyManager.history, pendingHistoryItems),
[historyManager.history, pendingHistoryItems],
);
const stickyTodos = useStableStickyTodos(rawStickyTodos);

// Terminal tab progress bar (OSC 9;4) for iTerm2/Ghostty
useTerminalProgress(streamingState, isToolExecuting(pendingHistoryItems));
Expand Down Expand Up @@ -1573,24 +1594,39 @@ export const AppContainer = (props: AppContainerProps) => {
!dialogsVisible &&
!isFeedbackDialogOpen &&
streamingState !== StreamingState.WaitingForConfirmation;
const stickyTodoWidth = Math.min(mainAreaWidth, 64);
const stickyTodoMaxVisibleItems =
getStickyTodoMaxVisibleItems(terminalHeight);
const stickyTodosLayoutKey = shouldShowStickyTodos
? getStickyTodosLayoutKey(
stickyTodos,
stickyTodoWidth,
stickyTodoMaxVisibleItems,
)
: 'hidden';
const [controlsHeight, setControlsHeight] = useState(0);

useLayoutEffect(() => {
if (!mainControlsRef.current) {
setControlsHeight(0);
setControlsHeight((previousHeight) =>
previousHeight === 0 ? previousHeight : 0,
);
return;
}

const fullFooterMeasurement = measureElement(mainControlsRef.current);
setControlsHeight(fullFooterMeasurement.height);
setControlsHeight((previousHeight) =>
previousHeight === fullFooterMeasurement.height
? previousHeight
: fullFooterMeasurement.height,
);
}, [
buffer,
terminalWidth,
terminalHeight,
btwItem,
dialogsVisible,
shouldShowStickyTodos,
stickyTodos,
stickyTodosLayoutKey,
]);

// agentViewState is declared earlier (before handleFinalSubmit) so it
Expand Down
19 changes: 19 additions & 0 deletions packages/cli/src/ui/components/StickyTodoList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,23 @@ describe('StickyTodoList', () => {
output.indexOf('Summarize results'),
);
});

it('limits visible items and shows an overflow summary', () => {
const todos: TodoItem[] = Array.from({ length: 7 }, (_, index) => ({
id: `todo-${index + 1}`,
content: `Task ${index + 1}`,
status: 'pending',
}));

const { lastFrame } = render(
<StickyTodoList todos={todos} width={60} maxVisibleItems={5} />,
);
const output = lastFrame() ?? '';

expect(output).toContain('Task 1');
expect(output).toContain('Task 5');
expect(output).not.toContain('Task 6');
expect(output).not.toContain('Task 7');
expect(output).toContain('... and 2 more');
});
});
33 changes: 28 additions & 5 deletions packages/cli/src/ui/components/StickyTodoList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
*/

import type React from 'react';
import { useMemo } from 'react';
import { memo, useMemo } from 'react';
import { Box, Text } from 'ink';
import { t } from '../../i18n/index.js';
import { Colors } from '../colors.js';
import { theme } from '../semantic-colors.js';
import { getOrderedStickyTodos } from '../utils/todoSnapshot.js';
import {
getOrderedStickyTodos,
STICKY_TODO_MAX_VISIBLE_ITEMS,
getStickyTodosRenderKey,
} from '../utils/todoSnapshot.js';
import type { TodoItem } from './TodoDisplay.js';

interface StickyTodoListProps {
todos: TodoItem[];
width: number;
maxVisibleItems?: number;
}

const STATUS_ICONS = {
Expand All @@ -24,9 +29,10 @@ const STATUS_ICONS = {
completed: '●',
} as const;

export const StickyTodoList: React.FC<StickyTodoListProps> = ({
const StickyTodoListComponent: React.FC<StickyTodoListProps> = ({
todos,
width,
maxVisibleItems = STICKY_TODO_MAX_VISIBLE_ITEMS,
}) => {
const orderedTodos = useMemo(() => getOrderedStickyTodos(todos), [todos]);
const todoNumberById = useMemo(
Expand All @@ -35,6 +41,9 @@ export const StickyTodoList: React.FC<StickyTodoListProps> = ({
[todos],
);

const visibleTodos = orderedTodos.slice(0, maxVisibleItems);
const overflowCount = orderedTodos.length - maxVisibleItems;

if (todos.length === 0) {
return null;
}
Expand All @@ -53,7 +62,7 @@ export const StickyTodoList: React.FC<StickyTodoListProps> = ({
<Text color={theme.text.secondary} bold>
{t('Current tasks')}
</Text>
{orderedTodos.map((todo, index) => {
{visibleTodos.map((todo, index) => {
const todoNumber = todoNumberById.get(todo.id) ?? `${index + 1}.`;
const itemColor =
todo.status === 'in_progress'
Expand All @@ -72,14 +81,28 @@ export const StickyTodoList: React.FC<StickyTodoListProps> = ({
<Text
color={itemColor}
strikethrough={todo.status === 'completed'}
wrap="wrap"
wrap="truncate-end"
>
{todo.content}
</Text>
</Box>
</Box>
);
})}
{overflowCount > 0 && (
<Text color={theme.text.secondary}>
{t('... and {{count}} more', { count: String(overflowCount) })}
</Text>
)}
</Box>
);
};

export const StickyTodoList = memo(
StickyTodoListComponent,
(previousProps, nextProps) =>
previousProps.width === nextProps.width &&
previousProps.maxVisibleItems === nextProps.maxVisibleItems &&
getStickyTodosRenderKey(previousProps.todos) ===
getStickyTodosRenderKey(nextProps.todos),
);
16 changes: 16 additions & 0 deletions packages/cli/src/ui/layouts/DefaultAppLayout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ describe('DefaultAppLayout', () => {
expect(output).toContain('Composer');
});

it('renders sticky todo list while responding', () => {
mockedUseAgentViewState.mockReturnValue({
activeView: 'main',
agents: new Map(),
});

const { lastFrame } = renderLayout({
...baseUIState,
streamingState: StreamingState.Responding,
});

const output = lastFrame() ?? '';
expect(output).toContain('StickyTodoList');
expect(output).toContain('Composer');
});

it('does not render sticky todo list when feedback dialog is open', () => {
mockedUseAgentViewState.mockReturnValue({
activeView: 'main',
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/ui/layouts/DefaultAppLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useUIActions } from '../contexts/UIActionsContext.js';
import { useAgentViewState } from '../contexts/AgentViewContext.js';
import { useTerminalSize } from '../hooks/useTerminalSize.js';
import { StreamingState } from '../types.js';
import { getStickyTodoMaxVisibleItems } from '../utils/todoSnapshot.js';

export const DefaultAppLayout: React.FC = () => {
const uiState = useUIState();
Expand All @@ -30,6 +31,9 @@ export const DefaultAppLayout: React.FC = () => {
const hasAgents = agents.size > 0;
const isAgentTab = activeView !== 'main' && agents.has(activeView);
const stickyTodoWidth = Math.min(uiState.mainAreaWidth, 64);
const stickyTodoMaxVisibleItems = getStickyTodoMaxVisibleItems(
uiState.terminalHeight,
);
const shouldShowStickyTodos =
uiState.stickyTodos !== null &&
!uiState.dialogsVisible &&
Expand Down Expand Up @@ -81,6 +85,7 @@ export const DefaultAppLayout: React.FC = () => {
<StickyTodoList
todos={uiState.stickyTodos!}
width={stickyTodoWidth}
maxVisibleItems={stickyTodoMaxVisibleItems}
/>
)}
{uiState.btwItem && (
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/src/ui/layouts/ScreenReaderAppLayout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ describe('ScreenReaderAppLayout', () => {
expect(output).toContain('Composer');
});

it('renders sticky todo list while responding', () => {
const { lastFrame } = renderLayout({
...baseUIState,
streamingState: StreamingState.Responding,
});

const output = lastFrame() ?? '';
expect(output).toContain('StickyTodoList');
expect(output).toContain('Composer');
});

it('does not render sticky todo list when feedback dialog is open', () => {
const { lastFrame } = renderLayout({
...baseUIState,
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/ui/layouts/ScreenReaderAppLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ import { StickyTodoList } from '../components/StickyTodoList.js';
import { BtwMessage } from '../components/messages/BtwMessage.js';
import { useUIState } from '../contexts/UIStateContext.js';
import { StreamingState } from '../types.js';
import { getStickyTodoMaxVisibleItems } from '../utils/todoSnapshot.js';

export const ScreenReaderAppLayout: React.FC = () => {
const uiState = useUIState();
const stickyTodoWidth = Math.min(uiState.mainAreaWidth, 64);
const stickyTodoMaxVisibleItems = getStickyTodoMaxVisibleItems(
uiState.terminalHeight,
);
const shouldShowStickyTodos =
uiState.stickyTodos !== null &&
!uiState.dialogsVisible &&
Expand Down Expand Up @@ -47,6 +51,7 @@ export const ScreenReaderAppLayout: React.FC = () => {
<StickyTodoList
todos={uiState.stickyTodos!}
width={stickyTodoWidth}
maxVisibleItems={stickyTodoMaxVisibleItems}
/>
)}
{uiState.btwItem && (
Expand Down
Loading
Loading