Merge pull request #416 from znygugeyx-ctrl/feat/subagent-fork-render

feat: 参考 claude code 官方实现,改进 sub agent 以及 fork agent 的渲染方式
This commit is contained in:
znygugeyx-ctrl
2026-05-06 09:57:52 +08:00
committed by GitHub
parent c4e9efb7a8
commit 5c107e5f8c
11 changed files with 488 additions and 27 deletions

View File

@@ -43,8 +43,12 @@ export async function call(
// Omitting subagent_type triggers implicit fork.
const input = {
prompt: directive,
fork: true, // 触发 AgentTool 的 fork 路径:继承父会话上下文 + system prompt + 模型
run_in_background: true, // fork always runs async
description: `Fork: ${directive.slice(0, 30)}${directive.length > 30 ? '...' : ''}`,
// description 只显示在底部 selector / BackgroundTasksDialog保持简短标签
// 即可;用户输入的 prompt 会作为第一条用户消息呈现在主视图里,这里不要
// 重复显示。
description: 'forked from main',
};
// Call AgentTool with proper parameters:

View File

@@ -26,6 +26,7 @@ import { useSetPromptOverlayDialog } from '../../context/promptOverlayContext.js
import { formatImageRef, formatPastedTextRef, getPastedTextRefNumLines, parseReferences } from '../../history.js';
import type { VerificationStatus } from '../../hooks/useApiKeyVerification.js';
import { type HistoryMode, useArrowKeyHistory } from '../../hooks/useArrowKeyHistory.js';
import { useBackgroundAgentTasks } from '../../hooks/useBackgroundAgentTasks.js';
import { useDoublePress } from '../../hooks/useDoublePress.js';
import { useHistorySearch } from '../../hooks/useHistorySearch.js';
import type { IDESelection } from '../../hooks/useIdeSelection.js';
@@ -415,6 +416,16 @@ function PromptInput({
// First ↓ selects the pill, second ↓ moves to row 0. Prevents double-select
// of pill + row when both bg tasks (pill) and forked agents (rows) are visible.
const coordinatorTaskIndex = useAppState(s => s.coordinatorTaskIndex);
const selectedBgAgentIndex = useAppState(s => s.selectedBgAgentIndex);
const setSelectedBgAgentIndex = useCallback(
(v: number | ((prev: number) => number)) =>
setAppState(prev => {
const next = typeof v === 'function' ? v(prev.selectedBgAgentIndex) : v;
if (next === prev.selectedBgAgentIndex) return prev;
return { ...prev, selectedBgAgentIndex: next };
}),
[setAppState],
);
const setCoordinatorTaskIndex = useCallback(
(v: number | ((prev: number) => number)) =>
setAppState(prev => {
@@ -501,10 +512,13 @@ function PromptInput({
(runningTaskCount > 0 || (process.env.USER_TYPE === 'ant' && coordinatorTaskCount > 0)) &&
!shouldHideTasksFooter(tasks, showSpinnerTree);
const teamsFooterVisible = cachedTeams.length > 0;
const bgAgentList = useBackgroundAgentTasks();
const bgAgentFooterVisible = bgAgentList.length > 0;
const footerItems = useMemo(
() =>
[
bgAgentFooterVisible && 'bg_agent',
tasksFooterVisible && 'tasks',
tmuxFooterVisible && 'tmux',
bagelFooterVisible && 'bagel',
@@ -513,6 +527,7 @@ function PromptInput({
companionFooterVisible && 'companion',
].filter(Boolean) as FooterItem[],
[
bgAgentFooterVisible,
tasksFooterVisible,
tmuxFooterVisible,
bagelFooterVisible,
@@ -540,6 +555,7 @@ function PromptInput({
const _bagelSelected = footerItemSelected === 'bagel';
const teamsSelected = footerItemSelected === 'teams';
const bridgeSelected = footerItemSelected === 'bridge';
const bgAgentSelected = footerItemSelected === 'bg_agent';
function selectFooterItem(item: FooterItem | null): void {
setAppState(prev => (prev.footerSelection === item ? prev : { ...prev, footerSelection: item }));
@@ -547,6 +563,9 @@ function PromptInput({
setTeammateFooterIndex(0);
setCoordinatorTaskIndex(minCoordinatorIndex);
}
if (item === 'bg_agent') {
setSelectedBgAgentIndex(-1);
}
}
// delta: +1 = down/right, -1 = up/left. Returns true if nav happened
@@ -1808,6 +1827,15 @@ function PromptInput({
useKeybindings(
{
'footer:up': () => {
// ↑ in bg_agent pill: move selection up (-1 = main). At -1, leave pill.
if (bgAgentSelected) {
if (selectedBgAgentIndex > -1) {
setSelectedBgAgentIndex(prev => prev - 1);
} else {
selectFooterItem(null);
}
return;
}
// ↑ scrolls within the coordinator task list before leaving the pill
if (
tasksSelected &&
@@ -1821,6 +1849,13 @@ function PromptInput({
navigateFooter(-1, true);
},
'footer:down': () => {
// ↓ in bg_agent pill: move selection down through agents. Clamp at last.
if (bgAgentSelected) {
if (selectedBgAgentIndex < bgAgentList.length - 1) {
setSelectedBgAgentIndex(prev => prev + 1);
}
return;
}
// ↓ scrolls within the coordinator task list, never leaves the pill
if (tasksSelected && process.env.USER_TYPE === 'ant' && coordinatorTaskCount > 0) {
if (coordinatorTaskIndex < coordinatorTaskCount - 1) {
@@ -1906,6 +1941,15 @@ function PromptInput({
setShowBridgeDialog(true);
selectFooterItem(null);
break;
case 'bg_agent':
if (selectedBgAgentIndex === -1) {
exitTeammateView(setAppState);
} else {
const picked = bgAgentList[selectedBgAgentIndex];
if (picked) enterTeammateView(picked.agentId, setAppState);
}
// Keep the pill focused so ↑/↓ continue to work after Enter.
break;
}
},
'footer:clearSelection': () => {

View File

@@ -30,6 +30,7 @@ type Props = {
inProgressToolCallCount?: number;
lookups: ReturnType<typeof buildMessageLookups>;
isTranscriptMode?: boolean;
defaultCollapsed?: boolean;
};
export function AssistantToolUseMessage({
@@ -45,6 +46,7 @@ export function AssistantToolUseMessage({
inProgressToolCallCount,
lookups,
isTranscriptMode,
defaultCollapsed,
}: Props): React.ReactNode {
const terminalSize = useTerminalSize();
const [theme] = useTheme();
@@ -167,6 +169,7 @@ export function AssistantToolUseMessage({
</Box>
{!isResolved &&
!isQueued &&
!defaultCollapsed &&
(isClassifierChecking ? (
<MessageResponse height={1}>
<Text dimColor>

View File

@@ -3,28 +3,31 @@
*/
import type { TextBlockParam } from '@anthropic-ai/sdk/resources/index.mjs';
import * as React from 'react';
import { Box, Text } from '@anthropic/ink';
import { FORK_BOILERPLATE_TAG, FORK_DIRECTIVE_PREFIX } from '../../constants/xml.js';
import { extractTag } from '../../utils/messages.js';
import { UserPromptMessage } from './UserPromptMessage.js';
type Props = {
addMargin: boolean;
param: TextBlockParam;
isTranscriptMode?: boolean;
timestamp?: string;
};
export function UserForkBoilerplateMessage({ param, addMargin }: Props): React.ReactNode {
const text = param.text;
const extracted = extractTag(text, 'fork-boilerplate');
if (!extracted) {
return null;
}
const firstLine = extracted.trim().split('\n')[0] ?? '';
const preview = firstLine.length > 80 ? firstLine.slice(0, 77) + '...' : firstLine;
export function UserForkBoilerplateMessage({ param, addMargin, isTranscriptMode, timestamp }: Props): React.ReactNode {
if (!extractTag(param.text, FORK_BOILERPLATE_TAG)) return null;
const closeTag = `</${FORK_BOILERPLATE_TAG}>`;
const afterTag = param.text.slice(param.text.indexOf(closeTag) + closeTag.length).trimStart();
const userPrompt = afterTag.startsWith(FORK_DIRECTIVE_PREFIX)
? afterTag.slice(FORK_DIRECTIVE_PREFIX.length)
: afterTag;
return (
<Box flexDirection="row" marginTop={addMargin ? 1 : 0}>
<Text dimColor>[fork] </Text>
<Text>{preview}</Text>
</Box>
<UserPromptMessage
addMargin={addMargin}
param={{ type: 'text', text: userPrompt }}
isTranscriptMode={isTranscriptMode}
timestamp={timestamp}
/>
);
}

View File

@@ -4,6 +4,7 @@ import * as React from 'react';
import { NO_CONTENT_MESSAGE } from '../../constants/messages.js';
import {
COMMAND_MESSAGE_TAG,
FORK_BOILERPLATE_TAG,
LOCAL_COMMAND_CAVEAT_TAG,
TASK_NOTIFICATION_TAG,
TEAMMATE_MESSAGE_TAG,
@@ -124,16 +125,21 @@ export function UserTextMessage({
}
// Fork child's first message: collapse the rules/format boilerplate, show
// only the directive. FORK_BOILERPLATE_TAG is inlined so the import doesn't
// ship in external builds where feature('FORK_SUBAGENT') is false.
if (feature('FORK_SUBAGENT')) {
if (param.text.includes('<fork-boilerplate>')) {
/* eslint-disable @typescript-eslint/no-require-imports */
const { UserForkBoilerplateMessage } =
require('./UserForkBoilerplateMessage.js') as typeof import('./UserForkBoilerplateMessage.js');
/* eslint-enable @typescript-eslint/no-require-imports */
return <UserForkBoilerplateMessage addMargin={addMargin} param={param} />;
}
// only the user prompt. Independent of FORK_SUBAGENT flag — the fork agent
// transcript always needs to render the prompt as a normal user bubble.
if (param.text.includes(`<${FORK_BOILERPLATE_TAG}>`)) {
/* eslint-disable @typescript-eslint/no-require-imports */
const { UserForkBoilerplateMessage } =
require('./UserForkBoilerplateMessage.js') as typeof import('./UserForkBoilerplateMessage.js');
/* eslint-enable @typescript-eslint/no-require-imports */
return (
<UserForkBoilerplateMessage
addMargin={addMargin}
param={param}
isTranscriptMode={isTranscriptMode}
timestamp={timestamp}
/>
);
}
// Cross-session UDS message (from another Claude session's SendMessage).

View File

@@ -0,0 +1,63 @@
import { Box, Text } from '@anthropic/ink';
import { useBackgroundAgentTasks } from '../../hooks/useBackgroundAgentTasks.js';
import { useElapsedTime } from '../../hooks/useElapsedTime.js';
import { useAppState } from '../../state/AppState.js';
import type { LocalAgentTaskState } from '../../tasks/LocalAgentTask/LocalAgentTask.js';
import { formatTokens } from '../../utils/format.js';
function AgentRow({ task, selected }: { task: LocalAgentTaskState; selected: boolean }) {
const elapsed = useElapsedTime(task.startTime, task.status === 'running');
const tokens = task.progress?.tokenCount ?? 0;
const isRunning = task.status === 'running';
return (
<Box flexDirection="row" width="100%" justifyContent="space-between">
<Box flexDirection="row" flexShrink={1}>
<Text color={isRunning ? 'success' : undefined}>{selected ? '● ' : '○ '}</Text>
<Text bold={selected} wrap="truncate-end">
{task.agentType} <Text dimColor>{task.description}</Text>
</Text>
</Box>
<Box flexShrink={0}>
<Text dimColor>
{elapsed} · {formatTokens(tokens)} tokens
</Text>
</Box>
</Box>
);
}
function getHint(pillFocused: boolean, viewedTask: LocalAgentTaskState | null): string {
if (pillFocused) return '↑/↓ to select · Enter to view';
if (!viewedTask) return 'shift+↓ to manage background agents';
return viewedTask.status === 'running' ? 'shift+↓ to manage · x to stop' : 'shift+↓ to manage · x to clear';
}
export function BackgroundAgentSelector(): React.ReactNode {
const tasks = useBackgroundAgentTasks();
const viewingId = useAppState(s => s.viewingAgentTaskId);
const footerSelection = useAppState(s => s.footerSelection);
const selectedBgIndex = useAppState(s => s.selectedBgAgentIndex);
if (tasks.length === 0) return null;
const pillFocused = footerSelection === 'bg_agent';
const highlightedId = pillFocused
? selectedBgIndex === -1
? null
: (tasks[selectedBgIndex]?.agentId ?? null)
: (viewingId ?? null);
const mainHighlighted = pillFocused ? selectedBgIndex === -1 : viewingId === undefined;
const viewedTask = viewingId ? (tasks.find(t => t.agentId === viewingId) ?? null) : null;
return (
<Box flexDirection="column" width="100%">
<Box flexDirection="row" width="100%" justifyContent="space-between">
<Text bold={mainHighlighted}>{mainHighlighted ? '● ' : '○ '}main</Text>
<Text dimColor>{getHint(pillFocused, viewedTask)}</Text>
</Box>
{tasks.map(task => (
<AgentRow key={task.agentId} task={task} selected={task.agentId === highlightedId} />
))}
</Box>
);
}

View File

@@ -0,0 +1,19 @@
import { useMemo } from 'react'
import { useAppState } from '../state/AppState.js'
import {
isLocalAgentTask,
type LocalAgentTaskState,
} from '../tasks/LocalAgentTask/LocalAgentTask.js'
export function useBackgroundAgentTasks(): LocalAgentTaskState[] {
const tasks = useAppState(s => s.tasks)
return useMemo(() => {
const now = Date.now()
return Object.values(tasks)
.filter(isLocalAgentTask)
.filter(t => t.agentType !== 'main-session')
.filter(t => t.isBackgrounded !== false)
.filter(t => t.evictAfter === undefined || t.evictAfter > now)
.sort((a, b) => a.startTime - b.startTime)
}, [tasks])
}

View File

@@ -3485,6 +3485,7 @@ async function run(): Promise<CommanderCommand> {
: 'none',
showTeammateMessagePreview: isAgentSwarmsEnabled() ? false : undefined,
selectedIPAgentIndex: -1,
selectedBgAgentIndex: -1,
coordinatorTaskIndex: -1,
viewSelectionMode: 'none',
footerSelection: null,

View File

@@ -244,7 +244,14 @@ import {
formatCommandInputTags,
} from '../utils/messages.js';
import { generateSessionTitle } from '../utils/sessionTitle.js';
import { BASH_INPUT_TAG, COMMAND_MESSAGE_TAG, COMMAND_NAME_TAG, LOCAL_COMMAND_STDOUT_TAG } from '../constants/xml.js';
import {
BASH_INPUT_TAG,
COMMAND_MESSAGE_TAG,
COMMAND_NAME_TAG,
FORK_BOILERPLATE_TAG,
LOCAL_COMMAND_STDOUT_TAG,
} from '../constants/xml.js';
import { FORK_SUBAGENT_TYPE } from '@claude-code-best/builtin-tools/tools/AgentTool/forkSubagent.js';
import { escapeXml } from '../utils/xml.js';
import type { ThinkingConfig } from '../utils/thinking.js';
import { gracefulShutdownSync } from '../utils/gracefulShutdown.js';
@@ -336,6 +343,7 @@ import {
import { isBgSession, updateSessionName, updateSessionActivity } from '../utils/concurrentSessions.js';
import { isInProcessTeammateTask, type InProcessTeammateTaskState } from '../tasks/InProcessTeammateTask/types.js';
import { restoreRemoteAgentTasks } from '../tasks/RemoteAgentTask/RemoteAgentTask.js';
import { BackgroundAgentSelector } from '../components/tasks/BackgroundAgentSelector.js';
import { useInboxPoller } from '../hooks/useInboxPoller.js';
// Dead code elimination: conditional import for loop mode
/* eslint-disable @typescript-eslint/no-require-imports */
@@ -800,6 +808,21 @@ export type Props = {
export type Screen = 'prompt' | 'transcript';
// Boilerplate carrier lives in a mixed user message ([tool_result..., text])
// that AgentTool/forkSubagent.buildForkedMessages emits as the fork child's
// first user turn. The text block wraps <FORK_BOILERPLATE_TAG>...</..> + the
// user prompt; tool_result siblings keep the parent's tool calls closed.
const FORK_BOILERPLATE_OPEN_TAG = `<${FORK_BOILERPLATE_TAG}>`;
function isForkBoilerplateTextBlock(block: { type: string; text?: string }): boolean {
return block.type === 'text' && typeof block.text === 'string' && block.text.includes(FORK_BOILERPLATE_OPEN_TAG);
}
function isForkBoilerplateMessage(message: MessageType): boolean {
if (message.type !== 'user' || !Array.isArray(message.message?.content)) return false;
return message.message.content.some(isForkBoilerplateTextBlock);
}
export function REPL({
commands: initialCommands,
debug,
@@ -5548,8 +5571,72 @@ export function REPL({
const usesSyncMessages = showStreamingText || !isLoading;
// When viewing an agent, never fall through to leader — empty until
// bootstrap/stream fills. Closes the see-leader-type-agent footgun.
const rawAgentMessages = viewedAgentTask?.messages;
// Fork sidechain encodes the user prompt inside a mixed user message alongside
// tool_result blocks; surface the prompt as a standalone bubble and strip the
// boilerplate text from its original carrier while preserving tool_results.
const displayedAgentMessages = useMemo(() => {
if (!viewedAgentTask) return undefined;
const agentMessages = rawAgentMessages ?? [];
if (
!isLocalAgentTask(viewedAgentTask) ||
viewedAgentTask.agentType !== FORK_SUBAGENT_TYPE ||
!viewedAgentTask.prompt
) {
return agentMessages;
}
// Single pass: locate boilerplate carrier, check whether the prompt text is
// already present elsewhere, and find the fallback insertion point (after
// the last parent assistant tool_use).
const trimmedPrompt = viewedAgentTask.prompt.trim();
let boilerplateIndex = -1;
let lastAssistantToolUseIndex = -1;
let promptAlreadyRendered = false;
for (let i = 0; i < agentMessages.length; i++) {
const m = agentMessages[i]!;
if (m.type === 'user' && Array.isArray(m.message?.content)) {
const hasBoilerplate = m.message.content.some(isForkBoilerplateTextBlock);
if (hasBoilerplate) {
boilerplateIndex = i;
} else if (!promptAlreadyRendered) {
const firstText = m.message.content.find(b => b.type === 'text' && typeof b.text === 'string') as
| { type: 'text'; text: string }
| undefined;
if (firstText && firstText.text.trim() === trimmedPrompt) promptAlreadyRendered = true;
}
continue;
}
if (m.type === 'assistant' && Array.isArray(m.message?.content)) {
if (m.message.content.some(b => b.type === 'tool_use')) lastAssistantToolUseIndex = i;
}
}
const stripped =
boilerplateIndex === -1
? agentMessages
: agentMessages.map((m, i) => {
if (i !== boilerplateIndex) return m;
if (!Array.isArray(m.message?.content)) return m;
return {
...m,
message: {
...m.message,
content: m.message.content.filter(b => !isForkBoilerplateTextBlock(b)),
},
};
});
if (promptAlreadyRendered) return stripped;
const insertAt = boilerplateIndex !== -1 ? boilerplateIndex + 1 : lastAssistantToolUseIndex + 1;
const synthetic = createUserMessage({
content: viewedAgentTask.prompt,
timestamp: new Date(viewedAgentTask.startTime).toISOString(),
});
return [...stripped.slice(0, insertAt), synthetic, ...stripped.slice(insertAt)];
}, [viewedAgentTask, rawAgentMessages]);
const displayedMessages = viewedAgentTask
? (viewedAgentTask.messages ?? [])
? (displayedAgentMessages ?? [])
: usesSyncMessages
? messages
: deferredMessages;
@@ -6286,6 +6373,7 @@ export function REPL({
voiceInterimRange={voice.interimRange}
/>
<SessionBackgroundHint onBackgroundSession={handleBackgroundSession} isLoading={isLoading} />
<BackgroundAgentSelector />
</>
)}
{cursor && (

View File

@@ -85,6 +85,7 @@ export type FooterItem =
| 'teams'
| 'bridge'
| 'companion'
| 'bg_agent'
export type AppState = DeepImmutable<{
settings: SettingsJson
@@ -97,6 +98,9 @@ export type AppState = DeepImmutable<{
// Optional - only present when ENABLE_AGENT_SWARMS is true (for dead code elimination)
showTeammateMessagePreview?: boolean
selectedIPAgentIndex: number
// Selection index for the bottom BackgroundAgentSelector.
// -1 = main, 0..N-1 = index into useBackgroundAgentTasks().
selectedBgAgentIndex: number
// CoordinatorTaskPanel selection: -1 = pill, 0 = main, 1..N = agent rows.
// AppState (not local) so the panel can read it directly without prop-drilling
// through PromptInput → PromptInputFooter.
@@ -477,6 +481,7 @@ export function getDefaultAppState(): AppState {
isBriefOnly: false,
showTeammateMessagePreview: false,
selectedIPAgentIndex: -1,
selectedBgAgentIndex: -1,
coordinatorTaskIndex: -1,
viewSelectionMode: 'none',
footerSelection: null,