Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

feat(desktop): 支持运行中思考预览展开 (#1201)

yan60fbc28677a7
diff --git a/apps/desktop/src/renderer/__tests__/workGroupBlockLivePreview.test.tsx b/apps/desktop/src/renderer/__tests__/workGroupBlockLivePreview.test.tsx
index 2b5536eb8b..ad922fd133 100644
--- a/apps/desktop/src/renderer/__tests__/workGroupBlockLivePreview.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/workGroupBlockLivePreview.test.tsx
@@ -186,6 +186,34 @@ describe('WorkGroupBlock — running latest-five preview', () => {
expect(document.querySelectorAll('[data-live-work-activity="thinking"]')).toHaveLength(1);
});
+ it('continues updating an expanded live reasoning row as deltas arrive', () => {
+ const { rerender } = render(
+ createElement(WorkGroupBlock, {
+ blockId: 'work:t1',
+ isStreaming: true,
+ childItems: [thinking(mkThinking('th1', 'initial\nreasoning'))],
+ }),
+ );
+
+ const thinkingButton = screen.getByText('initial reasoning').closest('button');
+ if (!thinkingButton) throw new Error('Missing live thinking row');
+ fireEvent.click(thinkingButton);
+ expect(thinkingButton.getAttribute('aria-expanded')).toBe('true');
+
+ rerender(
+ createElement(WorkGroupBlock, {
+ blockId: 'work:t1',
+ isStreaming: true,
+ childItems: [thinking(mkThinking('th1', 'initial reasoning\nwith more detail'))],
+ }),
+ );
+
+ const updatedThinkingButton = screen
+ .getByText('initial reasoning with more detail')
+ .closest('button');
+ expect(updatedThinkingButton?.getAttribute('aria-expanded')).toBe('true');
+ });
+
it('shows complete commandActions as separate rows and summarizes pure exploration', () => {
const command = mkTool('exec-1', 'cat src/a.ts && rg TODO src');
command.toolInput = {
@@ -224,7 +252,7 @@ describe('WorkGroupBlock — running latest-five preview', () => {
it('offers no toggle while running when the preview already shows everything', () => {
const childItems = [
tools('seg-1', [mkTool('t1', 'git status')]),
- thinking(mkThinking('th1', 'checking the current state')),
+ thinking(mkThinking('th1', 'checking the\ncurrent state')),
];
const { rerender } = render(
createElement(WorkGroupBlock, {
@@ -238,6 +266,19 @@ describe('WorkGroupBlock — running latest-five preview', () => {
expect(document.querySelector('[data-live-work-preview="true"]')).toBeTruthy();
expect(screen.getByTestId('direct-tool').getAttribute('data-show-raw')).toBe('true');
+ // 即使运行中的工作组没有更多内容可供外层展开,思考预览行本身也应可点击查看全文。
+ const thinkingPreview = screen.getByText('checking the current state').closest('button');
+ if (!thinkingPreview) throw new Error('Missing expandable live thinking row');
+ thinkingPreview.focus();
+ expect(document.activeElement).toBe(thinkingPreview);
+ expect(thinkingPreview.className).toContain(
+ 'focus-visible:ring-[var(--focus-ring)]',
+ );
+ expect(thinkingPreview.getAttribute('aria-expanded')).toBe('false');
+ fireEvent.click(thinkingPreview);
+ expect(thinkingPreview.getAttribute('aria-expanded')).toBe('true');
+ expect(thinkingPreview.textContent).toContain('checking the\ncurrent state');
+
// ≤5 条活动且没有 preview 之外的子项:展开不会露出更多内容,组头不再
// 提供折叠交互 — 无箭头、禁用、点击后预览保持原样。
const runningButton = screen.getByText('chat.workGroup.working').closest('button');
@@ -266,6 +307,68 @@ describe('WorkGroupBlock — running latest-five preview', () => {
expect(screen.getByText('checking the current state')).toBeTruthy();
});
+ it('shares thinking expansion state between the live preview and full work-group view', () => {
+ render(
+ createElement(WorkGroupBlock, {
+ blockId: 'work:t1',
+ isStreaming: true,
+ childItems: [
+ thinking(mkThinking('th1', 'first line\nsecond line')),
+ rendered('progress', 'continuing the analysis'),
+ ],
+ }),
+ );
+
+ const livePreview = document.querySelector('[data-live-work-preview="true"]');
+ const liveThinkingButton = livePreview?.querySelector<HTMLButtonElement>(
+ '[data-live-work-activity="thinking"]',
+ );
+ if (!liveThinkingButton) throw new Error('Missing live thinking row');
+ fireEvent.click(liveThinkingButton);
+ expect(liveThinkingButton.getAttribute('aria-expanded')).toBe('true');
+
+ clickGroup('chat.workGroup.working');
+ const fullThinkingButton = Array.from(
+ document.querySelectorAll<HTMLButtonElement>(
+ '[data-live-work-activity="thinking"]',
+ ),
+ ).find((button) => !button.closest('[data-live-work-preview="true"]'));
+ expect(fullThinkingButton?.getAttribute('aria-expanded')).toBe('true');
+
+ if (!fullThinkingButton) throw new Error('Missing full thinking row');
+ fireEvent.click(fullThinkingButton);
+ expect(fullThinkingButton.getAttribute('aria-expanded')).toBe('false');
+
+ clickGroup('chat.workGroup.working');
+ const restoredPreviewButton = document
+ .querySelector('[data-live-work-preview="true"]')
+ ?.querySelector<HTMLButtonElement>('[data-live-work-activity="thinking"]');
+ expect(restoredPreviewButton?.getAttribute('aria-expanded')).toBe('false');
+ });
+
+ it('keeps an inherited expanded state toggleable for single-line long thinking', () => {
+ expandMemory.setExpanded('work:t1', true);
+ expandMemory.setExpanded('thinking:th1', true);
+ render(
+ createElement(WorkGroupBlock, {
+ blockId: 'work:t1',
+ durationMs: 2_000,
+ childItems: [
+ thinking(mkThinking('th1', 'a single-line reasoning block that is long enough to overflow')),
+ rendered('progress', 'continuing the analysis'),
+ ],
+ }),
+ );
+
+ const fullThinkingButton = Array.from(
+ document.querySelectorAll<HTMLButtonElement>(
+ '[data-live-work-activity="thinking"]',
+ ),
+ ).find((button) => !button.closest('[data-live-work-preview="true"]'));
+ expect(fullThinkingButton?.getAttribute('aria-expanded')).toBe('true');
+ expect(fullThinkingButton?.hasAttribute('disabled')).toBe(false);
+ });
+
it('keeps the toggle while running when expansion can reveal more than the preview', () => {
render(
createElement(WorkGroupBlock, {
diff --git a/apps/desktop/src/renderer/components/chat/WorkGroupBlock.tsx b/apps/desktop/src/renderer/components/chat/WorkGroupBlock.tsx
index 901da2533b..761b5ec5cc 100644
--- a/apps/desktop/src/renderer/components/chat/WorkGroupBlock.tsx
+++ b/apps/desktop/src/renderer/components/chat/WorkGroupBlock.tsx
@@ -80,9 +80,10 @@ function thinkingActivityForMessage(
message: ChatMessage,
): ProjectedThinkingActivity | null {
if (message.thinkingRedacted) return null;
- const content = message.content.replace(/\s+/g, ' ').trim();
+ const rawContent = message.content.trim();
+ const content = rawContent.replace(/\s+/g, ' ');
return content
- ? { kind: 'thinking', key: message.clientId, content }
+ ? { kind: 'thinking', key: message.clientId, rawContent, content }
: null;
}
@@ -128,21 +129,72 @@ function ThinkingActivityRow({
}: {
activity: ProjectedThinkingActivity;
}) {
+ const rawContent = activity.rawContent;
+ const hasExplicitLineBreak = /[\r\n]/.test(rawContent);
+ const textRef = useRef<HTMLSpanElement>(null);
+ const [canExpand, setCanExpand] = useState(hasExplicitLineBreak);
+ const { expanded, setExpanded } = useExpandedBlockMemory(`thinking:${activity.key}`);
+
+ useLayoutEffect(() => {
+ if (expanded) {
+ setCanExpand(true);
+ return;
+ }
+ const textElement = textRef.current;
+ if (!textElement) return;
+ const updateOverflow = () => {
+ setCanExpand(
+ hasExplicitLineBreak || textElement.scrollWidth > textElement.clientWidth + 1,
+ );
+ };
+ updateOverflow();
+ if (typeof ResizeObserver === 'undefined') return;
+ const observer = new ResizeObserver(updateOverflow);
+ observer.observe(textElement);
+ return () => observer.disconnect();
+ }, [activity.content, expanded, hasExplicitLineBreak]);
+
return (
- <div
+ <button
+ type="button"
data-live-work-activity="thinking"
- className="flex min-w-0 items-center gap-[6px] px-2 py-[3px]"
+ data-work-thinking-expandable={canExpand ? 'true' : 'false'}
+ disabled={!canExpand}
+ aria-expanded={canExpand ? expanded : undefined}
+ onClick={() => setExpanded((value) => !value)}
+ className={cn(
+ 'flex w-full min-w-0 items-start gap-[6px] px-2 py-[3px] text-left focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--focus-ring)]',
+ canExpand && 'cursor-pointer hover:opacity-80 transition-opacity',
+ )}
>
- <span className="inline-flex h-[18px] w-4 shrink-0 items-center justify-center text-[var(--msg-tool-card-chevron)]">
+ <span
+ aria-hidden="true"
+ className="inline-flex h-[18px] w-4 shrink-0 items-center justify-center text-[var(--msg-tool-card-chevron)]"
+ >
<Sparkles size={13} />
</span>
<span
- className="min-w-0 truncate text-[14px] italic text-[var(--thinking-body-text)]"
- title={activity.content}
+ ref={textRef}
+ className={cn(
+ 'min-w-0 flex-1 text-[14px] italic text-[var(--thinking-body-text)]',
+ expanded ? 'whitespace-pre-wrap break-words' : 'truncate',
+ )}
+ title={expanded ? undefined : activity.content}
>
- <ThinkingText content={activity.content} />
+ <ThinkingText content={expanded ? rawContent : activity.content} />
</span>
- </div>
+ {canExpand && (
+ <span aria-hidden="true" className="inline-flex h-[18px] shrink-0 items-center text-[var(--msg-tool-card-chevron)]">
+ <ChevronRight
+ size={13}
+ className={cn(
+ 'transition-transform duration-[var(--motion-fast,150ms)]',
+ expanded && 'rotate-90',
+ )}
+ />
+ </span>
+ )}
+ </button>
);
}
@@ -157,7 +209,11 @@ function ExpandedThinkingRow({ message }: { message: ChatMessage }) {
const { expanded, setExpanded } = useExpandedBlockMemory(`thinking:${message.clientId}`);
useLayoutEffect(() => {
- if (!activity || expanded) return;
+ if (!activity) return;
+ if (expanded) {
+ setCanExpand(true);
+ return;
+ }
const textElement = textRef.current;
if (!textElement) return;
const updateOverflow = () => {
@@ -196,11 +252,14 @@ function ExpandedThinkingRow({ message }: { message: ChatMessage }) {
disabled={!canExpand}
aria-expanded={canExpand ? expanded : undefined}
className={cn(
- 'flex w-full min-w-0 items-start gap-[6px] px-2 py-[3px] text-left',
+ 'flex w-full min-w-0 items-start gap-[6px] px-2 py-[3px] text-left focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--focus-ring)]',
canExpand && 'cursor-pointer hover:opacity-80 transition-opacity',
)}
>
- <span className="inline-flex h-[18px] w-4 shrink-0 items-center justify-center text-[var(--msg-tool-card-chevron)]">
+ <span
+ aria-hidden="true"
+ className="inline-flex h-[18px] w-4 shrink-0 items-center justify-center text-[var(--msg-tool-card-chevron)]"
+ >
<Sparkles size={13} />
</span>
<span
@@ -214,7 +273,7 @@ function ExpandedThinkingRow({ message }: { message: ChatMessage }) {
<ThinkingText content={expanded ? rawContent : activity.content} />
</span>
{canExpand && (
- <span className="inline-flex h-[18px] shrink-0 items-center text-[var(--msg-tool-card-chevron)]">
+ <span aria-hidden="true" className="inline-flex h-[18px] shrink-0 items-center text-[var(--msg-tool-card-chevron)]">
<ChevronRight
size={13}
className={cn(
diff --git a/packages/maker-shared/src/workActivityProjection.ts b/packages/maker-shared/src/workActivityProjection.ts
index fb336c3ff1..a8387b0352 100644
--- a/packages/maker-shared/src/workActivityProjection.ts
+++ b/packages/maker-shared/src/workActivityProjection.ts
@@ -48,6 +48,8 @@ export interface ProjectedToolActivity<
export interface ProjectedThinkingActivity {
kind: 'thinking';
key: string;
+ /** Original trimmed content, retained for an expanded live-preview row. */
+ rawContent: string;
content: string;
}
@@ -207,8 +209,11 @@ function projectThinkingMessage<TMessage extends WorkActivityMessageLike>(
message: TMessage,
): ProjectedThinkingActivity | null {
if (message.thinkingRedacted) return null;
- const content = message.content.replace(/\s+/g, ' ').trim();
- return content ? { kind: 'thinking', key: message.clientId, content } : null;
+ const rawContent = message.content.trim();
+ const content = rawContent.replace(/\s+/g, ' ');
+ return content
+ ? { kind: 'thinking', key: message.clientId, rawContent, content }
+ : null;
}
/** Reverse hot path used by the running latest-N preview. */