refactor(chat): execution graph optimize (#873)

Co-authored-by: Haze <hazeone@users.noreply.github.com>
This commit is contained in:
Haze
2026-04-19 19:36:33 +08:00
committed by GitHub
Unverified
parent 2f03aa1fad
commit 1b2dccee6e
24 changed files with 1444 additions and 536 deletions

View File

@@ -148,6 +148,38 @@ const childTranscriptMessages = [
},
];
const inFlightPrompt = 'Open browser, search for tech news, and take a screenshot';
const seededInFlightHistory = [
{
role: 'user',
content: [{ type: 'text', text: inFlightPrompt }],
timestamp: Date.now(),
},
];
const longRunPrompt = 'Inspect the workspace and summarize the result';
const longRunProcessSegments = Array.from({ length: 9 }, (_, index) => `Checked source ${index + 1}.`);
const longRunSummary = 'Here is the summary.';
const longRunReplyText = `${longRunProcessSegments.join(' ')} ${longRunSummary}`;
const longRunHistory = [
{
role: 'user',
content: [{ type: 'text', text: longRunPrompt }],
timestamp: Date.now(),
},
...longRunProcessSegments.map((segment, index) => ({
role: 'assistant',
id: `long-run-step-${index + 1}`,
content: [{ type: 'text', text: segment }],
timestamp: Date.now(),
})),
{
role: 'assistant',
id: 'long-run-final',
content: [{ type: 'text', text: longRunReplyText }],
timestamp: Date.now(),
},
];
test.describe('ClawX chat execution graph', () => {
test('renders internal yield status and linked subagent branch from mocked IPC', async ({ launchElectronApp }) => {
const app = await launchElectronApp({ skipSetup: true });
@@ -222,6 +254,12 @@ test.describe('ClawX chat execution graph', () => {
}
await expect(page.getByTestId('main-layout')).toBeVisible();
await expect(page.getByTestId('chat-execution-graph')).toBeVisible({ timeout: 30_000 });
// Completed runs auto-collapse into a single-line summary button. Expand
// it first so the underlying step details are rendered.
const graph = page.getByTestId('chat-execution-graph');
if ((await graph.getAttribute('data-collapsed')) === 'true') {
await graph.click();
}
await expect(
page.locator('[data-testid="chat-execution-graph"] [data-testid="chat-execution-step"]').getByText('sessions_yield', { exact: true }),
).toBeVisible();
@@ -229,6 +267,9 @@ test.describe('ClawX chat execution graph', () => {
await expect(
page.locator('[data-testid="chat-execution-graph"] [data-testid="chat-execution-step"]').getByText('exec', { exact: true }),
).toBeVisible();
const execRow = page.locator('[data-testid="chat-execution-step"]').filter({ hasText: 'exec' }).first();
await execRow.click();
await expect(execRow.locator('pre')).toBeVisible();
await expect(page.locator('[data-testid="chat-execution-graph"]').getByText('I asked coder to break down the core blocks of ~/Velaria uncommitted changes; will give you the conclusion when it returns.')).toBeVisible();
await expect(page.getByText('CHECKLIST.md')).toHaveCount(0);
} finally {
@@ -252,7 +293,7 @@ test.describe('ClawX chat execution graph', () => {
[stableStringify(['chat.history', { sessionKey: PROJECT_MANAGER_SESSION_KEY, limit: 200 }])]: {
success: true,
result: {
messages: [],
messages: seededInFlightHistory,
},
},
},
@@ -281,9 +322,16 @@ test.describe('ClawX chat execution graph', () => {
await app.evaluate(async ({ app: _app }) => {
const { ipcMain } = process.mainModule!.require('electron') as typeof import('electron');
const sendPayloads: Array<{ message?: string; sessionKey?: string }> = [];
(globalThis as typeof globalThis & { __chatExecutionHistory?: unknown[] }).__chatExecutionHistory = [
{
role: 'user',
content: [{ type: 'text', text: 'Open browser, search for tech news, and take a screenshot' }],
timestamp: Date.now(),
},
];
ipcMain.removeHandler('gateway:rpc');
ipcMain.handle('gateway:rpc', async (_event: unknown, method: string, payload: unknown) => {
void payload;
if (method === 'sessions.list') {
return {
success: true,
@@ -295,22 +343,16 @@ test.describe('ClawX chat execution graph', () => {
if (method === 'chat.history') {
return {
success: true,
result: { messages: [] },
};
}
if (method === 'chat.send') {
if (payload && typeof payload === 'object') {
const p = payload as { message?: string; sessionKey?: string };
sendPayloads.push({ message: p.message, sessionKey: p.sessionKey });
}
return {
success: true,
result: { runId: 'mock-run' },
result: {
messages: (
(globalThis as typeof globalThis & { __chatExecutionHistory?: unknown[] }).__chatExecutionHistory
?? seededInFlightHistory
),
},
};
}
return { success: true, result: {} };
});
(globalThis as typeof globalThis & { __clawxSendPayloads?: Array<{ message?: string; sessionKey?: string }> }).__clawxSendPayloads = sendPayloads;
});
const page = await getStableWindow(app);
@@ -323,18 +365,24 @@ test.describe('ClawX chat execution graph', () => {
}
await expect(page.getByTestId('main-layout')).toBeVisible();
await page.getByTestId('chat-composer-input').fill('Open browser, search for tech news, and take a screenshot');
await page.getByTestId('chat-composer-send').click();
await expect(page.getByText(inFlightPrompt)).toHaveCount(1);
await expect(page.getByText('Open browser, search for tech news, and take a screenshot')).toHaveCount(1);
await expect.poll(async () => {
return await app.evaluate(() => {
const sendPayloads = (globalThis as typeof globalThis & {
__clawxSendPayloads?: Array<{ message?: string; sessionKey?: string }>;
}).__clawxSendPayloads || [];
return sendPayloads.length;
await app.evaluate(async ({ BrowserWindow }) => {
const win = BrowserWindow.getAllWindows()[0];
win?.webContents.send('gateway:notification', {
method: 'agent',
params: {
runId: 'mock-run',
sessionKey: 'agent:main:main',
state: 'started',
},
});
}).toBe(1);
});
await expect(page.locator('[data-testid="chat-execution-graph"]')).toHaveAttribute('data-collapsed', 'false');
await expect(page.locator('[data-testid="chat-execution-step-thinking-trailing"]')).toBeVisible();
await expect(page.locator('[data-testid="chat-execution-step-thinking-trailing"] [aria-hidden="true"]')).toHaveCount(1);
await expect(page.locator('[data-testid^="chat-message-"]')).toHaveCount(1);
await app.evaluate(async ({ BrowserWindow }) => {
const win = BrowserWindow.getAllWindows()[0];
@@ -359,15 +407,143 @@ test.describe('ClawX chat execution graph', () => {
});
});
await expect(page.getByText('Open browser, search for tech news, and take a screenshot')).toHaveCount(1);
await expect(page.getByText(/^thinking 1 2 3$/)).toHaveCount(1);
await expect(page.getByText(/^thinking 1 2$/)).toHaveCount(0);
await expect(page.getByText(/^thinking 1$/)).toHaveCount(0);
await expect(page.getByText(/^1 2 3$/)).toHaveCount(1);
await expect(page.getByText(/^1 2$/)).toHaveCount(0);
await expect(page.getByText(/^1$/)).toHaveCount(0);
await expect(page.getByText(inFlightPrompt)).toHaveCount(1);
// Intermediate process output should be rendered in the execution graph
// only, not as a streaming assistant chat bubble.
await expect(page.locator('[data-testid^="chat-message-"]')).toHaveCount(1);
await expect(page.locator('[data-testid="chat-execution-graph"]')).toHaveAttribute('data-collapsed', 'false');
await expect(page.locator('[data-testid="chat-execution-step-thinking-trailing"]')).toBeVisible();
await expect(page.locator('[data-testid="chat-execution-step-thinking-trailing"] [aria-hidden="true"]')).toHaveCount(1);
await expect(page.locator('[data-testid="chat-execution-graph"] [data-testid="chat-execution-step"]').getByText('Thinking', { exact: true })).toHaveCount(3);
const firstChatBubble = page.locator('[data-testid^="chat-message-"] > div').first();
await expect(firstChatBubble.getByText(/^1 2 3$/)).toHaveCount(0);
await app.evaluate(async ({ BrowserWindow }) => {
(globalThis as typeof globalThis & { __chatExecutionHistory?: unknown[] }).__chatExecutionHistory = [
{
role: 'user',
content: [{ type: 'text', text: 'Open browser, search for tech news, and take a screenshot' }],
timestamp: Date.now(),
},
{
role: 'assistant',
content: [{
type: 'toolCall',
id: 'browser-start-call',
name: 'browser',
arguments: { action: 'start' },
}],
timestamp: Date.now(),
},
{
role: 'assistant',
content: [{
type: 'toolCall',
id: 'browser-open-call',
name: 'browser',
arguments: { action: 'open', targetUrl: 'https://x.com/home' },
}],
timestamp: Date.now(),
},
{
role: 'assistant',
id: 'final-response',
content: [{ type: 'text', text: 'Done.' }],
timestamp: Date.now(),
},
];
const win = BrowserWindow.getAllWindows()[0];
win?.webContents.send('gateway:notification', {
method: 'agent',
params: {
runId: 'mock-run',
sessionKey: 'agent:main:main',
state: 'final',
message: {
role: 'assistant',
id: 'final-response',
content: [{ type: 'text', text: 'Done.' }],
timestamp: Date.now(),
},
},
});
});
await expect(page.getByText('Done.')).toBeVisible();
await expect(page.locator('[data-testid="chat-execution-graph"]')).toHaveAttribute('data-collapsed', 'true');
} finally {
await closeElectronApp(app);
}
});
test('preserves long execution history counts and strips the full folded reply prefix', async ({ launchElectronApp }) => {
const app = await launchElectronApp({ skipSetup: true });
try {
await installIpcMocks(app, {
gatewayStatus: { state: 'running', port: 18789, pid: 12345 },
gatewayRpc: {
[stableStringify(['sessions.list', {}])]: {
success: true,
result: {
sessions: [{ key: PROJECT_MANAGER_SESSION_KEY, displayName: 'main' }],
},
},
[stableStringify(['chat.history', { sessionKey: PROJECT_MANAGER_SESSION_KEY, limit: 200 }])]: {
success: true,
result: {
messages: longRunHistory,
},
},
[stableStringify(['chat.history', { sessionKey: PROJECT_MANAGER_SESSION_KEY, limit: 1000 }])]: {
success: true,
result: {
messages: longRunHistory,
},
},
},
hostApi: {
[stableStringify(['/api/gateway/status', 'GET'])]: {
ok: true,
data: {
status: 200,
ok: true,
json: { state: 'running', port: 18789, pid: 12345 },
},
},
[stableStringify(['/api/agents', 'GET'])]: {
ok: true,
data: {
status: 200,
ok: true,
json: {
success: true,
agents: [{ id: 'main', name: 'main' }],
},
},
},
},
});
const page = await getStableWindow(app);
try {
await page.reload();
} catch (error) {
if (!String(error).includes('ERR_FILE_NOT_FOUND')) {
throw error;
}
}
await expect(page.getByTestId('main-layout')).toBeVisible();
await expect(page.getByTestId('chat-execution-graph')).toBeVisible({ timeout: 30_000 });
await expect(page.getByTestId('chat-execution-graph')).toHaveAttribute('data-collapsed', 'true');
await expect(page.getByTestId('chat-execution-graph')).toContainText('0 tool calls');
await expect(page.getByTestId('chat-execution-graph')).toContainText('9 process messages');
await expect(page.getByText(longRunSummary, { exact: true })).toBeVisible();
await expect(page.getByText(longRunReplyText, { exact: true })).toHaveCount(0);
} finally {
await closeElectronApp(app);
}
});
});

View File

@@ -0,0 +1,140 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { extractText } from '@/pages/Chat/message-utils';
const { gatewayRpcMock, hostApiFetchMock, agentsState } = vi.hoisted(() => ({
gatewayRpcMock: vi.fn(),
hostApiFetchMock: vi.fn(),
agentsState: {
agents: [] as Array<Record<string, unknown>>,
},
}));
vi.mock('@/stores/gateway', () => ({
useGatewayStore: {
getState: () => ({
status: { state: 'running', port: 18789 },
rpc: gatewayRpcMock,
}),
},
}));
vi.mock('@/stores/agents', () => ({
useAgentsStore: {
getState: () => agentsState,
},
}));
vi.mock('@/lib/host-api', () => ({
hostApiFetch: (...args: unknown[]) => hostApiFetchMock(...args),
}));
describe('chat event dedupe', () => {
beforeEach(() => {
vi.resetModules();
window.localStorage.clear();
gatewayRpcMock.mockReset();
hostApiFetchMock.mockReset();
agentsState.agents = [];
});
it('keeps processing delta events without seq for the same run', async () => {
const { useChatStore } = await import('@/stores/chat');
useChatStore.setState({
currentSessionKey: 'agent:main:main',
currentAgentId: 'main',
sessions: [{ key: 'agent:main:main' }],
messages: [],
sessionLabels: {},
sessionLastActivity: {},
sending: false,
activeRunId: null,
streamingText: '',
streamingMessage: null,
streamingTools: [],
pendingFinal: true,
lastUserMessageAt: null,
pendingToolImages: [],
error: null,
loading: false,
thinkingLevel: null,
});
useChatStore.getState().handleChatEvent({
state: 'delta',
runId: 'run-no-seq',
sessionKey: 'agent:main:main',
message: {
role: 'assistant',
id: 'reply-stream',
content: [{ type: 'text', text: 'Checked X.' }],
},
});
useChatStore.getState().handleChatEvent({
state: 'delta',
runId: 'run-no-seq',
sessionKey: 'agent:main:main',
message: {
role: 'assistant',
id: 'reply-stream',
content: [
{ type: 'text', text: 'Checked X.' },
{ type: 'text', text: 'Checked X. Here is the summary.' },
],
},
});
expect(extractText(useChatStore.getState().streamingMessage)).toBe('Checked X. Here is the summary.');
});
it('still dedupes repeated delta events when seq matches', async () => {
const { useChatStore } = await import('@/stores/chat');
useChatStore.setState({
currentSessionKey: 'agent:main:main',
currentAgentId: 'main',
sessions: [{ key: 'agent:main:main' }],
messages: [],
sessionLabels: {},
sessionLastActivity: {},
sending: false,
activeRunId: null,
streamingText: '',
streamingMessage: null,
streamingTools: [],
pendingFinal: false,
lastUserMessageAt: null,
pendingToolImages: [],
error: null,
loading: false,
thinkingLevel: null,
});
useChatStore.getState().handleChatEvent({
state: 'delta',
runId: 'run-with-seq',
sessionKey: 'agent:main:main',
seq: 3,
message: {
role: 'assistant',
id: 'reply-stream',
content: [{ type: 'text', text: 'first version' }],
},
});
useChatStore.getState().handleChatEvent({
state: 'delta',
runId: 'run-with-seq',
sessionKey: 'agent:main:main',
seq: 3,
message: {
role: 'assistant',
id: 'reply-stream',
content: [{ type: 'text', text: 'duplicate version should be ignored' }],
},
});
expect(extractText(useChatStore.getState().streamingMessage)).toBe('first version');
});
});

View File

@@ -23,7 +23,6 @@ describe('ChatMessage attachment dedupe', () => {
render(
<ChatMessage
message={message}
showThinking={false}
suppressProcessAttachments
/>,
);

View File

@@ -0,0 +1,180 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { render, screen, waitFor } from '@testing-library/react';
const hostApiFetchMock = vi.fn();
const { gatewayState, agentsState } = vi.hoisted(() => ({
gatewayState: {
status: { state: 'running', port: 18789 },
},
agentsState: {
agents: [{ id: 'main', name: 'main' }] as Array<Record<string, unknown>>,
fetchAgents: vi.fn(),
},
}));
vi.mock('@/stores/gateway', () => ({
useGatewayStore: (selector: (state: typeof gatewayState) => unknown) => selector(gatewayState),
}));
vi.mock('@/stores/agents', () => ({
useAgentsStore: (selector: (state: typeof agentsState) => unknown) => selector(agentsState),
}));
vi.mock('@/lib/host-api', () => ({
hostApiFetch: (...args: unknown[]) => hostApiFetchMock(...args),
}));
vi.mock('react-i18next', () => ({
useTranslation: () => ({
t: (key: string, params?: Record<string, unknown>) => {
if (key === 'executionGraph.collapsedSummary') {
return `collapsed ${String(params?.toolCount ?? '')} ${String(params?.processCount ?? '')}`.trim();
}
if (key === 'executionGraph.agentRun') {
return `Main execution`;
}
if (key === 'executionGraph.title') {
return 'Execution Graph';
}
if (key === 'executionGraph.collapseAction') {
return 'Collapse';
}
if (key === 'executionGraph.thinkingLabel') {
return 'Thinking';
}
if (key.startsWith('taskPanel.stepStatus.')) {
return key.split('.').at(-1) ?? key;
}
return key;
},
}),
}));
vi.mock('@/hooks/use-stick-to-bottom-instant', () => ({
useStickToBottomInstant: () => ({
contentRef: { current: null },
scrollRef: { current: null },
}),
}));
vi.mock('@/hooks/use-min-loading', () => ({
useMinLoading: () => false,
}));
vi.mock('@/pages/Chat/ChatToolbar', () => ({
ChatToolbar: () => null,
}));
vi.mock('@/pages/Chat/ChatInput', () => ({
ChatInput: () => null,
}));
describe('Chat execution graph lifecycle', () => {
beforeEach(async () => {
vi.resetModules();
hostApiFetchMock.mockReset();
hostApiFetchMock.mockResolvedValue({ success: true, messages: [] });
agentsState.fetchAgents.mockReset();
const { useChatStore } = await import('@/stores/chat');
useChatStore.setState({
messages: [
{
role: 'user',
content: 'Check semiconductor chatter',
},
{
role: 'assistant',
id: 'tool-turn',
content: [
{ type: 'text', text: 'Checked X.' },
{ type: 'tool_use', id: 'browser-search', name: 'browser', input: { action: 'search', query: 'semiconductor' } },
],
},
],
loading: false,
error: null,
sending: true,
activeRunId: 'run-live',
streamingText: '',
streamingMessage: {
role: 'assistant',
id: 'final-stream',
content: [
{ type: 'text', text: 'Checked X.' },
{ type: 'text', text: 'Checked X. Here is the summary.' },
],
},
streamingTools: [
{
toolCallId: 'browser-search',
name: 'browser',
status: 'completed',
updatedAt: Date.now(),
},
],
pendingFinal: true,
lastUserMessageAt: Date.now(),
pendingToolImages: [],
sessions: [{ key: 'agent:main:main' }],
currentSessionKey: 'agent:main:main',
currentAgentId: 'main',
sessionLabels: {},
sessionLastActivity: {},
thinkingLevel: null,
});
});
it('collapses execution once the reply starts streaming and keeps only the reply suffix in the bubble', async () => {
const { Chat } = await import('@/pages/Chat/index');
render(<Chat />);
await waitFor(() => {
expect(screen.getByTestId('chat-execution-graph')).toHaveAttribute('data-collapsed', 'true');
});
expect(screen.getByText('Here is the summary.')).toBeInTheDocument();
expect(screen.queryByText('Checked X. Here is the summary.')).not.toBeInTheDocument();
});
it('renders the execution graph immediately for an active run before any stream content arrives', async () => {
const { useChatStore } = await import('@/stores/chat');
useChatStore.setState({
messages: [
{
role: 'user',
content: 'Check semiconductor chatter',
},
],
loading: false,
error: null,
sending: true,
activeRunId: 'run-starting',
streamingText: '',
streamingMessage: null,
streamingTools: [],
pendingFinal: false,
lastUserMessageAt: Date.now(),
pendingToolImages: [],
sessions: [{ key: 'agent:main:main' }],
currentSessionKey: 'agent:main:main',
currentAgentId: 'main',
sessionLabels: {},
sessionLastActivity: {},
thinkingLevel: null,
});
const { Chat } = await import('@/pages/Chat/index');
render(<Chat />);
await waitFor(() => {
expect(screen.getByTestId('chat-execution-graph')).toHaveAttribute('data-collapsed', 'false');
});
expect(screen.getByTestId('chat-execution-step-thinking-trailing')).toBeInTheDocument();
expect(screen.getAllByText('Thinking').length).toBeGreaterThan(0);
});
});

View File

@@ -63,7 +63,6 @@ describe('useChatStore startup history retry', () => {
error: null,
loading: false,
thinkingLevel: null,
showThinking: true,
});
gatewayRpcMock
@@ -115,7 +114,6 @@ describe('useChatStore startup history retry', () => {
error: null,
loading: false,
thinkingLevel: null,
showThinking: true,
});
gatewayRpcMock
@@ -162,7 +160,6 @@ describe('useChatStore startup history retry', () => {
error: null,
loading: false,
thinkingLevel: null,
showThinking: true,
});
let resolveFirstAttempt: ((value: { messages: Array<{ role: string; content: string; timestamp: number }> }) => void) | null = null;
@@ -242,7 +239,6 @@ describe('useChatStore startup history retry', () => {
error: null,
loading: false,
thinkingLevel: null,
showThinking: true,
});
gatewayRpcMock.mockImplementationOnce(async () => {

View File

@@ -104,7 +104,6 @@ describe('chat target routing', () => {
error: null,
loading: false,
thinkingLevel: null,
showThinking: true,
});
await useChatStore.getState().sendMessage('Hello direct agent', undefined, 'research');
@@ -148,7 +147,6 @@ describe('chat target routing', () => {
error: null,
loading: false,
thinkingLevel: null,
showThinking: true,
});
await useChatStore.getState().sendMessage(

View File

@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest';
import { deriveTaskSteps, parseSubagentCompletionInfo } from '@/pages/Chat/task-visualization';
import { stripProcessMessagePrefix } from '@/pages/Chat/message-utils';
import type { RawMessage, ToolStatus } from '@/stores/chat';
describe('deriveTaskSteps', () => {
@@ -23,14 +24,11 @@ describe('deriveTaskSteps', () => {
],
},
streamingTools,
sending: true,
pendingFinal: false,
showThinking: true,
});
expect(steps).toEqual([
expect.objectContaining({
id: 'stream-thinking',
id: 'stream-thinking-0',
label: 'Thinking',
status: 'running',
kind: 'thinking',
@@ -69,9 +67,6 @@ describe('deriveTaskSteps', () => {
summary: 'Scanning files',
},
],
sending: true,
pendingFinal: false,
showThinking: false,
});
expect(steps).toEqual([
@@ -111,9 +106,6 @@ describe('deriveTaskSteps', () => {
summary: 'Permission denied',
},
],
sending: true,
pendingFinal: false,
showThinking: false,
});
expect(steps).toEqual([
@@ -127,7 +119,7 @@ describe('deriveTaskSteps', () => {
]);
});
it('keeps the newest running step when the execution graph exceeds the max length', () => {
it('keeps all steps when the execution graph exceeds the previous max length', () => {
const messages: RawMessage[] = Array.from({ length: 9 }, (_, index) => ({
role: 'assistant',
id: `assistant-${index}`,
@@ -153,12 +145,14 @@ describe('deriveTaskSteps', () => {
summary: 'Scanning current workspace',
},
],
sending: true,
pendingFinal: false,
showThinking: false,
});
expect(steps).toHaveLength(8);
expect(steps).toHaveLength(10);
expect(steps[0]).toEqual(expect.objectContaining({
id: 'tool-0',
label: 'read_0',
status: 'completed',
}));
expect(steps.at(-1)).toEqual(expect.objectContaining({
id: 'tool-live',
label: 'grep_live',
@@ -182,14 +176,11 @@ describe('deriveTaskSteps', () => {
messages,
streamingMessage: null,
streamingTools: [],
sending: false,
pendingFinal: false,
showThinking: true,
});
expect(steps).toEqual([
expect.objectContaining({
id: 'history-thinking-assistant-1',
id: 'history-thinking-assistant-1-0',
label: 'Thinking',
status: 'completed',
kind: 'thinking',
@@ -203,31 +194,106 @@ describe('deriveTaskSteps', () => {
]);
});
it('collapses cumulative streaming thinking details into the newest version', () => {
it('splits cumulative streaming thinking into separate execution steps', () => {
const steps = deriveTaskSteps({
messages: [],
streamingMessage: {
role: 'assistant',
content: [
{ type: 'thinking', thinking: 'thinking 1' },
{ type: 'thinking', thinking: 'thinking 1 2' },
{ type: 'thinking', thinking: 'thinking 1 2 3' },
{ type: 'thinking', thinking: 'Reviewing X.' },
{ type: 'thinking', thinking: 'Reviewing X. Comparing Y.' },
{ type: 'thinking', thinking: 'Reviewing X. Comparing Y. Drafting answer.' },
],
},
streamingTools: [],
sending: true,
pendingFinal: false,
showThinking: true,
});
expect(steps).toEqual([
expect.objectContaining({
id: 'stream-thinking',
detail: 'thinking 1 2 3',
id: 'stream-thinking-0',
detail: 'Reviewing X.',
status: 'completed',
}),
expect.objectContaining({
id: 'stream-thinking-1',
detail: 'Comparing Y.',
status: 'completed',
}),
expect.objectContaining({
id: 'stream-thinking-2',
detail: 'Drafting answer.',
status: 'running',
}),
]);
});
it('keeps earlier reply segments in the graph when the last streaming segment is rendered separately', () => {
const steps = deriveTaskSteps({
messages: [],
streamingMessage: {
role: 'assistant',
content: [
{ type: 'text', text: 'Checked X.' },
{ type: 'text', text: 'Checked X. Checked Snowball.' },
{ type: 'text', text: 'Checked X. Checked Snowball. Here is the summary.' },
],
},
streamingTools: [],
omitLastStreamingMessageSegment: true,
});
expect(steps).toEqual([
expect.objectContaining({
id: 'stream-message-0',
detail: 'Checked X.',
status: 'completed',
}),
expect.objectContaining({
id: 'stream-message-1',
detail: 'Checked Snowball.',
status: 'completed',
}),
]);
});
it('folds earlier reply segments into the graph but leaves the final answer for the chat bubble', () => {
const steps = deriveTaskSteps({
messages: [
{
role: 'assistant',
id: 'assistant-reply',
content: [
{ type: 'text', text: 'Checked X.' },
{ type: 'text', text: 'Checked X. Checked Snowball.' },
{ type: 'text', text: 'Checked X. Checked Snowball. Here is the summary.' },
],
},
],
streamingMessage: null,
streamingTools: [],
});
expect(steps).toEqual([
expect.objectContaining({
id: 'history-message-assistant-reply-0',
detail: 'Checked X.',
status: 'completed',
}),
expect.objectContaining({
id: 'history-message-assistant-reply-1',
detail: 'Checked Snowball.',
status: 'completed',
}),
]);
});
it('strips folded process narration from the final reply text', () => {
expect(stripProcessMessagePrefix(
'Checked X. Checked Snowball. Here is the summary.',
['Checked X.', 'Checked Snowball.'],
)).toBe('Here is the summary.');
});
it('builds a branch for spawned subagents', () => {
const messages: RawMessage[] = [
{
@@ -254,9 +320,6 @@ describe('deriveTaskSteps', () => {
messages,
streamingMessage: null,
streamingTools: [],
sending: false,
pendingFinal: false,
showThinking: true,
});
expect(steps).toEqual([