From e42d59452113ede10009b1d43eaf2a9b7f0f6e3d Mon Sep 17 00:00:00 2001 From: phantom5099 <1011668688@qq.com> Date: Sun, 22 Nov 2026 20:47:08 +0800 Subject: [PATCH] fix(desktop): refine approval and tool event rendering --- packages/desktop/src/agent/AgentWorkspace.tsx | 28 ++- packages/desktop/src/agent/ApprovalPanel.tsx | 41 ++++- packages/desktop/src/lib/frame-reducer.ts | 2 +- packages/desktop/src/shared/MessageItem.tsx | 35 +--- packages/desktop/src/shared/ToolCallCard.tsx | 36 ++-- packages/desktop/src/shared/ToolCallGroup.tsx | 21 +-- packages/desktop/src/shared/ToolSummary.tsx | 33 ++-- packages/desktop/src/stores/agent.store.ts | 14 +- packages/desktop/src/styles/index.css | 164 +++++++++++++++++- 9 files changed, 278 insertions(+), 96 deletions(-) diff --git a/packages/desktop/src/agent/AgentWorkspace.tsx b/packages/desktop/src/agent/AgentWorkspace.tsx index a806e6c2..8a441a15 100644 --- a/packages/desktop/src/agent/AgentWorkspace.tsx +++ b/packages/desktop/src/agent/AgentWorkspace.tsx @@ -855,6 +855,13 @@ interface AgentWorkspaceProps { export default function AgentWorkspace({ sendMessage, sendQueuedInput, abort }: AgentWorkspaceProps) { const currentThreadId = useAgentStore((s) => s.currentThreadId); + const hasPendingApproval = useAgentStore((s) => + currentThreadId + ? s.threads[currentThreadId]?.turns.some((turn) => + turn.items.some((item) => item.type === 'tool_call' && item.status === 'pending') + ) ?? false + : false + ); const isCompressing = useAgentStore((s) => s.isCompressing); const workspace = useWorkspaceStore(); const [planPanelOpen, setPlanPanelOpen] = useState(false); @@ -878,7 +885,6 @@ export default function AgentWorkspace({ sendMessage, sendQueuedInput, abort }:
- {isCompressing && ( @@ -887,14 +893,18 @@ export default function AgentWorkspace({ sendMessage, sendQueuedInput, abort }: 正在压缩上下文...
)} -
- setPlanPanelOpen(true)} - /> -
+ {hasPendingApproval && currentThreadId ? ( + + ) : ( +
+ setPlanPanelOpen(true)} + /> +
+ )}
{planPanelOpen && ( void; } -export default function ApprovalPanel({ threadId }: ApprovalPanelProps) { +export default function ApprovalPanel({ threadId, inline = false, onStop }: ApprovalPanelProps) { const [collapsed, setCollapsed] = useState(false); const { approveTool, rejectTool } = useAgentApproval(); const { sendMessage } = useAgentCore(); @@ -83,6 +86,42 @@ export default function ApprovalPanel({ threadId }: ApprovalPanelProps) { if (pendingItems.length === 0) return null; + if (inline) { + return ( +
+
+ 需要确认的操作 +
+ {pendingItems.length} 项 + {onStop && ( + + )} +
+
+
+ {pendingItems.map((item) => ( + + ))} +
+
+ ); + } + if (collapsed) { return (
diff --git a/packages/desktop/src/lib/frame-reducer.ts b/packages/desktop/src/lib/frame-reducer.ts index 25ecc49f..7d53f9a4 100644 --- a/packages/desktop/src/lib/frame-reducer.ts +++ b/packages/desktop/src/lib/frame-reducer.ts @@ -148,7 +148,7 @@ export function reduceFrame(frame: Frame, state: StreamState, fx: StreamEffects) if (e.name === 'submit_plan' && e.outcome.status !== 'ok') state.planTitle = null; if (e.name === 'ask_user') fx.markAskAnswered?.(e.id); if (e.outcome.status === 'denied') { - fx.applyItem({ id: e.id, type: 'tool_call', name: e.name, args: {}, status: 'rejected' }); + fx.applyItem({ id: e.id, type: 'tool_call', name: e.name, args: (e as any).args ?? {}, status: 'rejected' }); } const { output, exitCode } = toResultItem(e.outcome); fx.applyItem({ id: fx.newId(), type: 'tool_result', callId: e.id, name: e.name, output, exitCode }); diff --git a/packages/desktop/src/shared/MessageItem.tsx b/packages/desktop/src/shared/MessageItem.tsx index ad95137d..a22e22d0 100644 --- a/packages/desktop/src/shared/MessageItem.tsx +++ b/packages/desktop/src/shared/MessageItem.tsx @@ -235,40 +235,7 @@ const MessageItem = memo(function MessageItem({ } if (item.type === 'tool_call') { - if (item.status === 'pending') { - const a = item.args as Record; - const path = - typeof a.path === 'string' ? a.path : typeof a.file_path === 'string' ? a.file_path : ''; - const cmd = typeof a.command === 'string' ? a.command : ''; - const label = path || cmd || item.name; - return ( -
- {label} - 等待审批 -
- ); - } - - if (toolResult) { - return ; - } - - const isRejected = item.status === 'rejected'; - return ( -
- - {item.name} - - {item.status === 'running' && ( - - ⟳ 执行中 - - )} - {isRejected && ✗ 已拒绝} -
- ); + return ; } if (item.type === 'tool_result') { diff --git a/packages/desktop/src/shared/ToolCallCard.tsx b/packages/desktop/src/shared/ToolCallCard.tsx index a89e92cb..d79d5047 100644 --- a/packages/desktop/src/shared/ToolCallCard.tsx +++ b/packages/desktop/src/shared/ToolCallCard.tsx @@ -1,5 +1,5 @@ import { useState } from 'react'; -import CodeBlock from './CodeBlock'; +import { Terminal } from 'lucide-react'; interface ToolCallItem { id: string; @@ -14,44 +14,46 @@ interface ToolCallCardProps { threadId: string; onApprove: (threadId: string, callId: string) => void; onReject: (threadId: string, callId: string) => void; + compact?: boolean; } -export default function ToolCallCard({ item, threadId, onApprove, onReject }: ToolCallCardProps) { - const [argsOpen, setArgsOpen] = useState(false); +export default function ToolCallCard({ item, threadId, onApprove, onReject, compact = false }: ToolCallCardProps) { + const [argsOpen, setArgsOpen] = useState(true); const argsJson = JSON.stringify(item.args, null, 2); const isRejected = item.status === 'rejected'; const hasArgs = argsJson !== '{}'; return (
-
+
{hasArgs ? ( ) : ( {item.name} )} -
+ {hasArgs && ( + + {JSON.stringify(item.args)} + + )} +
{item.status === 'pending' && ( <>
{hasArgs && argsOpen && ( -
- +
+
+            {argsJson}
+          
)}
diff --git a/packages/desktop/src/shared/ToolCallGroup.tsx b/packages/desktop/src/shared/ToolCallGroup.tsx index 6b02cc6f..b38b5867 100644 --- a/packages/desktop/src/shared/ToolCallGroup.tsx +++ b/packages/desktop/src/shared/ToolCallGroup.tsx @@ -8,25 +8,16 @@ export interface ToolCallEntry { toolResult?: Item & { type: 'tool_result' }; } -function pendingLabel(item: ToolCallEntry['item']): string { - const args = item.args as Record; - const path = typeof args.path === 'string' ? args.path : typeof args.file_path === 'string' ? args.file_path : ''; - const command = typeof args.command === 'string' ? args.command : ''; - return path || command || item.name; -} - function statusLabel(entries: ToolCallEntry[]): string { if (entries.some(({ item }) => item.status === 'running')) return '执行中'; if (entries.some(({ item }) => item.status === 'pending')) return '等待审批'; - if (entries.some(({ item }) => item.status === 'rejected')) return '有操作被拒绝'; return '已完成'; } export default function ToolCallGroup({ entries }: { entries: ToolCallEntry[] }) { const [open, setOpen] = useState(false); + if (entries.length === 1) return ; const status = statusLabel(entries); - const hasFailure = entries.some(({ toolResult }) => toolResult?.exitCode !== undefined && toolResult.exitCode !== 0); - return (
))} diff --git a/packages/desktop/src/shared/ToolSummary.tsx b/packages/desktop/src/shared/ToolSummary.tsx index af9384c9..3b71e8c8 100644 --- a/packages/desktop/src/shared/ToolSummary.tsx +++ b/packages/desktop/src/shared/ToolSummary.tsx @@ -34,7 +34,7 @@ function formatToolArgs(args: object): string { } } -function toolIcon(name: string) { +export function toolIcon(name: string) { if (name === 'read_file') return FileText; if (name === 'write_file' || name === 'edit_file') return FilePenLine; if (name === 'list_dir') return Folder; @@ -126,22 +126,31 @@ export default function ToolSummary({ toolCall, toolResult }: ToolSummaryProps) const effectiveResult = computedResult ?? toolResult; const { isError, isRejected } = buildToolSummaryTitle(toolCall, effectiveResult); const argsSummary = formatToolArgs(toolCall.args); + const argsFull = JSON.stringify(toolCall.args, null, 2); const ToolIcon = toolIcon(toolCall.name); + const statusLabel = isRejected + ? '已拒绝' + : toolCall.status === 'pending' + ? '等待审批' + : toolCall.status === 'running' + ? '执行中' + : '已执行'; const titleColor = isRejected - ? 'text-[var(--text-muted)] line-through' + ? 'text-[var(--accent-danger)]' : isError ? 'text-[var(--accent-danger)]' : 'text-[var(--syntax-function)]'; - const hasContent = !!(effectiveResult?.diff || effectiveResult?.output); + const hasContent = !isRejected && !!(effectiveResult?.diff || effectiveResult?.output); + const displayResult = isRejected ? undefined : effectiveResult; return (
- {open && hasContent && ( -
- {isFileTool && effectiveResult?.diff ? ( - - ) : effectiveResult?.output ? ( - + {open && ( +
+
{argsFull}
+ {isFileTool && displayResult?.diff ? ( + + ) : displayResult?.output ? ( + ) : null}
)} diff --git a/packages/desktop/src/stores/agent.store.ts b/packages/desktop/src/stores/agent.store.ts index 1177f63c..771fac30 100644 --- a/packages/desktop/src/stores/agent.store.ts +++ b/packages/desktop/src/stores/agent.store.ts @@ -439,7 +439,11 @@ export const useAgentStore = create()( if (existingItem.status === 'pending' && chunk.status === 'running') { return; } - turn.items[existing] = chunk; + turn.items[existing] = { + ...chunk, + args: Object.keys(chunk.args ?? {}).length > 0 ? chunk.args : + existingItem.type === 'tool_call' ? existingItem.args : chunk.args, + }; } else { turn.items.push(chunk); } @@ -453,7 +457,9 @@ export const useAgentStore = create()( ); if (callIdx >= 0) { const callItem = turn.items[callIdx] as any; - callItem.status = 'approved'; + if (callItem.status !== 'rejected') { + callItem.status = 'approved'; + } targetChunk = buildToolDiff(chunk, callItem) as any; turn.items.push(targetChunk); return; @@ -465,7 +471,9 @@ export const useAgentStore = create()( ); if (otherCallIdx >= 0) { const callItem = t.items[otherCallIdx] as any; - callItem.status = 'approved'; + if (callItem.status !== 'rejected') { + callItem.status = 'approved'; + } targetChunk = buildToolDiff(chunk, callItem) as any; t.items.push(targetChunk); return; diff --git a/packages/desktop/src/styles/index.css b/packages/desktop/src/styles/index.css index 7edd1e0b..a1ca89d5 100644 --- a/packages/desktop/src/styles/index.css +++ b/packages/desktop/src/styles/index.css @@ -924,6 +924,69 @@ select { background: var(--accent-danger-bg); } +.cc-rejected-event { + color: var(--accent-danger); + background: color-mix(in srgb, var(--accent-danger-bg) 72%, var(--bg-panel)); + border-color: color-mix(in srgb, var(--accent-danger) 40%, var(--border-card)); +} + +.cc-inline-approval .cc-tool-call-card { + background: transparent; + border-color: var(--border-card); +} + +.cc-approval-args-preview { + min-width: 0; + flex: 1 1 auto; + display: block; + overflow: hidden; + color: var(--text-muted); + font-family: var(--font-mono, ui-monospace, SFMono-Regular, Consolas, monospace); + font-size: 11px; + white-space: nowrap; + text-overflow: clip; + -webkit-mask-image: linear-gradient(to right, #000 0%, #000 calc(100% - 24px), transparent 100%); + mask-image: linear-gradient(to right, #000 0%, #000 calc(100% - 24px), transparent 100%); +} + +.cc-approval-header { + display: grid; + grid-template-columns: max-content minmax(0, 1fr) max-content; + align-items: center; + column-gap: 8px; + min-width: 0; +} + +.cc-approval-tool { + display: inline-flex; + align-items: center; + gap: 6px; + min-width: max-content; + padding: 0; + text-align: left; +} + +.cc-approval-tool-name { + white-space: nowrap; +} + +.cc-tool-call-card .cc-approval-actions { + flex: 0 0 auto; + white-space: nowrap; + min-width: max-content; +} + +.cc-tool-call-card .cc-approval-actions button { + flex: 0 0 auto; + white-space: nowrap; +} + +.cc-approval-args-body { + max-height: 50vh; + overflow: auto; + border-top: 1px solid var(--border-card); +} + .cc-chat-surface .mt-2.rounded-lg.bg-\[var\(--bg-card\)\] { border-radius: 12px; border-color: var(--border-card); @@ -995,7 +1058,73 @@ select { } .cc-tool-summary .cc-tool-args { - max-width: min(52vw, 440px); + flex: 1 1 auto; + max-width: none; + text-align: left; + margin-left: 0; +} + +.cc-tool-status { + flex: 0 0 64px; + width: 64px; + margin-left: 8px; + color: var(--text-muted); + font-size: 11px; + text-align: right; + white-space: nowrap; +} + +.cc-tool-summary-row { + display: flex; + width: 100%; +} + +.cc-tool-summary-row .cc-tool-args { + flex: 1 1 auto; + min-width: 0; + margin-left: 2px; + text-align: left; +} + +.cc-tool-disclosure { + flex: 0 0 16px; + width: 16px; + margin-left: 8px; +} + +.cc-tool-summary-row > * { + min-width: 0; +} + +.cc-tool-summary-row .cc-tool-args { + flex: 1 1 auto; + min-width: 0; + overflow: hidden; + text-overflow: clip; + white-space: nowrap; + -webkit-mask-image: linear-gradient(to right, #000 0%, #000 calc(100% - 24px), transparent 100%); + mask-image: linear-gradient(to right, #000 0%, #000 calc(100% - 24px), transparent 100%); +} + +.cc-tool-expanded-details { + max-height: 50vh; + overflow: auto; + margin-left: 22px; + padding-left: 10px; + border-left: 1px solid var(--border-card); +} + +.cc-tool-expanded-args { + margin: 0 0 8px; + padding: 8px 10px; + overflow-x: auto; + color: var(--text-secondary); + background: var(--bg-code); + border: 1px solid var(--border-card); + border-radius: 6px; + font: 12px/1.5 var(--font-mono, ui-monospace, SFMono-Regular, Consolas, monospace); + white-space: pre-wrap; + text-align: left; } .cc-tool-group-count, @@ -1025,11 +1154,42 @@ select { font-size: 12px; } +.cc-tool-group-rejected .cc-tool-group-pending { + color: var(--accent-danger); +} + +.cc-tool-group-rejected .cc-tool-group-muted { + color: color-mix(in srgb, var(--accent-danger) 72%, var(--text-muted)); + font-size: 11px; +} + +.cc-tool-group-rejected .cc-tool-group-pending { + gap: 8px; +} + +.cc-rejected-args { + flex: 1 1 auto; + min-width: 0; + -webkit-mask-image: linear-gradient(to right, #000 0%, #000 calc(100% - 24px), transparent 100%); + mask-image: linear-gradient(to right, #000 0%, #000 calc(100% - 24px), transparent 100%); +} + +.cc-tool-args { + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + color: color-mix(in srgb, var(--text-muted) 70%, transparent); + font-size: 11px; +} + .cc-tool-group-dot { width: 5px; height: 5px; border-radius: 999px; - background: var(--accent-primary); + text-overflow: clip; + -webkit-mask-image: linear-gradient(to right, #000 0%, #000 calc(100% - 24px), transparent 100%); + mask-image: linear-gradient(to right, #000 0%, #000 calc(100% - 24px), transparent 100%); } .cc-tool-summary > button,