Skip to content
Merged
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
28 changes: 19 additions & 9 deletions packages/desktop/src/agent/AgentWorkspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -878,7 +885,6 @@ export default function AgentWorkspace({ sendMessage, sendQueuedInput, abort }:
<div className="cc-agent-workspace-shell flex-1 flex flex-row overflow-hidden bg-[var(--bg-panel)] min-w-0">
<div className="cc-chat-surface flex-1 flex flex-col overflow-hidden min-w-0">
<MessageStream key={currentThreadId} threadId={currentThreadId} />
<ApprovalPanel threadId={currentThreadId} />
<AskUserPanel threadId={currentThreadId} />
<TodoPanel threadId={currentThreadId} />
{isCompressing && (
Expand All @@ -887,14 +893,18 @@ export default function AgentWorkspace({ sendMessage, sendQueuedInput, abort }:
<span>正在压缩上下文...</span>
</div>
)}
<div className="shrink-0">
<InputBox
sendMessage={sendMessage}
sendQueuedInput={sendQueuedInput}
abort={abort}
onOpenPlanPanel={() => setPlanPanelOpen(true)}
/>
</div>
{hasPendingApproval && currentThreadId ? (
<ApprovalPanel threadId={currentThreadId} inline onStop={abort} />
) : (
<div className="shrink-0">
<InputBox
sendMessage={sendMessage}
sendQueuedInput={sendQueuedInput}
abort={abort}
onOpenPlanPanel={() => setPlanPanelOpen(true)}
/>
</div>
)}
</div>
{planPanelOpen && (
<PlanPanel
Expand Down
41 changes: 40 additions & 1 deletion packages/desktop/src/agent/ApprovalPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import ToolCallCard from '../shared/ToolCallCard';
import PlanDecisionModal from '../shared/PlanDecisionModal';
import { useWorkspaceStore } from '../stores/workspace.store';
import { textPart } from '@shared/parts';
import { Square } from 'lucide-react';

interface ApprovalPanelProps {
threadId: string;
inline?: boolean;
onStop?: () => 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();
Expand Down Expand Up @@ -83,6 +86,42 @@ export default function ApprovalPanel({ threadId }: ApprovalPanelProps) {

if (pendingItems.length === 0) return null;

if (inline) {
return (
<div className="cc-inline-approval shrink-0 rounded-2xl border border-[var(--border-card)] bg-[var(--bg-card)] overflow-hidden">
<div className="flex items-center justify-between px-4 py-2.5 border-b border-[var(--border-card)]">
<span className="text-[13px] font-medium text-[var(--text-primary)]">需要确认的操作</span>
<div className="flex items-center gap-2">
<span className="text-[12px] text-[var(--text-muted)]">{pendingItems.length} 项</span>
{onStop && (
<button
type="button"
onClick={onStop}
aria-label="停止生成"
title="停止生成"
className="flex h-7 w-7 items-center justify-center rounded-full bg-[var(--border-hover)] text-[var(--text-primary)] hover:bg-[var(--border-strong)]"
>
<Square size={12} fill="currentColor" />
</button>
)}
</div>
</div>
<div className="max-h-44 overflow-y-auto px-2 py-1">
{pendingItems.map((item) => (
<ToolCallCard
key={item.id}
item={item}
threadId={threadId}
onApprove={approveTool}
onReject={rejectTool}
compact
/>
))}
</div>
</div>
);
}

if (collapsed) {
return (
<div className="fixed bottom-24 right-5 z-40">
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/src/lib/frame-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
35 changes: 1 addition & 34 deletions packages/desktop/src/shared/MessageItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,40 +235,7 @@ const MessageItem = memo(function MessageItem({
}

if (item.type === 'tool_call') {
if (item.status === 'pending') {
const a = item.args as Record<string, unknown>;
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 (
<div className="cc-event-row cc-tool-event mb-1 flex items-center gap-1.5 text-[13px] text-[var(--text-tertiary)] pl-8">
<span className="font-mono text-[var(--text-primary)]">{label}</span>
<span className="text-[var(--text-muted)]">等待审批</span>
</div>
);
}

if (toolResult) {
return <ToolSummary toolCall={item} toolResult={toolResult} />;
}

const isRejected = item.status === 'rejected';
return (
<div className="cc-event-row cc-tool-event mb-1 flex items-center gap-1.5 text-[13px] pl-8">
<span
className={`font-mono ${isRejected ? 'text-[var(--text-muted)] line-through' : 'text-[var(--text-primary)]'}`}
>
{item.name}
</span>
{item.status === 'running' && (
<span className="text-[var(--accent-primary)] flex items-center gap-1">
<span className="inline-block animate-spin">⟳</span> 执行中
</span>
)}
{isRejected && <span className="text-[var(--text-muted)]">✗ 已拒绝</span>}
</div>
);
return <ToolSummary toolCall={item} toolResult={toolResult} />;
}

if (item.type === 'tool_result') {
Expand Down
36 changes: 20 additions & 16 deletions packages/desktop/src/shared/ToolCallCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from 'react';
import CodeBlock from './CodeBlock';
import { Terminal } from 'lucide-react';

interface ToolCallItem {
id: string;
Expand All @@ -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 (
<div
className={`cc-tool-call-card my-2 rounded border ${isRejected ? 'border-[var(--border-strong)] opacity-50' : 'border-[var(--border-card)]'} bg-[var(--bg-card)] overflow-hidden`}
className={`cc-tool-call-card ${compact ? 'my-1' : 'my-2'} rounded border ${isRejected ? 'border-[var(--border-strong)] opacity-50' : 'border-[var(--border-card)]'} bg-[var(--bg-card)] overflow-hidden`}
>
<div className="flex items-center gap-2 px-3 py-2 bg-[var(--bg-code-header)]">
<div className="cc-approval-header px-3 py-2 bg-[var(--bg-code-header)]">
{hasArgs ? (
<button
type="button"
onClick={() => setArgsOpen((v) => !v)}
className="flex items-center gap-2 min-w-0 hover:opacity-80 transition-opacity"
className="cc-approval-tool hover:opacity-80 transition-opacity"
>
<Terminal size={14} strokeWidth={1.7} className="shrink-0 text-[var(--text-muted)]" />
<span
className={`transition-transform text-[10px] text-[var(--text-muted)] ${argsOpen ? 'rotate-90' : ''}`}
>
▶
</span>
<span
className={`font-mono text-sm font-medium ${isRejected ? 'line-through text-[var(--text-muted)]' : 'text-[var(--syntax-function)]'}`}
className={`font-mono shrink-0 text-sm font-medium ${isRejected ? 'line-through text-[var(--text-muted)]' : 'text-[var(--syntax-function)]'}`}
>
{item.name}
</span>
</button>
) : (
<span
className={`font-mono text-sm font-medium ${isRejected ? 'line-through text-[var(--text-muted)]' : 'text-[var(--syntax-function)]'}`}
className={`cc-approval-tool-name font-mono text-sm font-medium ${isRejected ? 'line-through text-[var(--text-muted)]' : 'text-[var(--syntax-function)]'}`}
>
{item.name}
</span>
)}
<div className="ml-auto flex items-center gap-1">
{hasArgs && (
<span className="cc-approval-args-preview" title={argsJson}>
{JSON.stringify(item.args)}
</span>
)}
<div className="cc-approval-actions flex items-center gap-1">
{item.status === 'pending' && (
<>
<button
Expand Down Expand Up @@ -84,8 +86,10 @@ export default function ToolCallCard({ item, threadId, onApprove, onReject }: To
</div>
</div>
{hasArgs && argsOpen && (
<div className="px-3 pb-2 pt-1">
<CodeBlock code={argsJson} language="json" />
<div className="cc-approval-args-body px-3 pb-2 pt-1">
<pre className="m-0 whitespace-pre-wrap break-all text-left font-mono text-[12px] leading-relaxed text-[var(--text-secondary)]">
{argsJson}
</pre>
</div>
)}
</div>
Expand Down
21 changes: 2 additions & 19 deletions packages/desktop/src/shared/ToolCallGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,16 @@ export interface ToolCallEntry {
toolResult?: Item & { type: 'tool_result' };
}

function pendingLabel(item: ToolCallEntry['item']): string {
const args = item.args as Record<string, unknown>;
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 <ToolSummary toolCall={entries[0]!.item} toolResult={entries[0]!.toolResult} />;
const status = statusLabel(entries);
const hasFailure = entries.some(({ toolResult }) => toolResult?.exitCode !== undefined && toolResult.exitCode !== 0);

return (
<div className="cc-tool-group">
<button
Expand All @@ -38,7 +29,6 @@ export default function ToolCallGroup({ entries }: { entries: ToolCallEntry[] })
<Wrench size={14} strokeWidth={1.7} />
<span>{status}</span>
<span className="cc-tool-group-count">{entries.length} 项操作</span>
{hasFailure && <span className="cc-tool-group-error">有失败</span>}
<span className="ml-auto shrink-0">
{open ? <ChevronDown size={14} /> : <ChevronRight size={14} />}
</span>
Expand All @@ -50,14 +40,7 @@ export default function ToolCallGroup({ entries }: { entries: ToolCallEntry[] })
{toolResult ? (
<ToolSummary toolCall={item} toolResult={toolResult} />
) : (
<div className="cc-tool-group-pending">
<span className="cc-tool-group-dot" />
<span className="font-mono">{item.name}</span>
<span className="cc-tool-args">{pendingLabel(item) === item.name ? '' : pendingLabel(item)}</span>
<span className="cc-tool-group-muted">
{item.status === 'pending' ? '等待审批' : item.status === 'running' ? '执行中' : '已拒绝'}
</span>
</div>
<ToolSummary toolCall={item} />
)}
</div>
))}
Expand Down
33 changes: 22 additions & 11 deletions packages/desktop/src/shared/ToolSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 (
<div className="cc-tool-summary pb-1.5">
<button
type="button"
onClick={() => setOpen((v) => !v)}
className="flex items-center gap-2 px-2.5 py-1 rounded-md bg-[var(--border-card)] hover:bg-[var(--border-hover)] transition-colors text-[13px]"
className="cc-tool-summary-row flex items-center gap-2 px-2.5 py-1 rounded-md bg-[var(--border-card)] hover:bg-[var(--border-hover)] transition-colors text-[13px]"
>
<ToolIcon size={14} strokeWidth={1.7} className={`shrink-0 ${titleColor}`} />
<span className={`font-mono shrink-0 ${titleColor}`}>{toolCall.name}</span>
Expand All @@ -154,20 +163,22 @@ export default function ToolSummary({ toolCall, toolResult }: ToolSummaryProps)
{effectiveResult.deletions ? ` -${effectiveResult.deletions}` : ''}
</span>
)}
<span className="ml-auto shrink-0">
<span className={`cc-tool-status ${isRejected ? 'text-[var(--accent-danger)]' : ''}`}>{statusLabel}</span>
<span className="cc-tool-disclosure shrink-0">
{open ? (
<ChevronDown className={`w-3.5 h-3.5 ${titleColor}`} />
) : (
<ChevronRight className={`w-3.5 h-3.5 ${titleColor}`} />
)}
</span>
</button>
{open && hasContent && (
<div className="pt-1.5">
{isFileTool && effectiveResult?.diff ? (
<DiffBlock diff={effectiveResult.diff} />
) : effectiveResult?.output ? (
<CodeBlock code={effectiveResult.output.slice(0, 4000)} />
{open && (
<div className="cc-tool-expanded-details pt-1.5">
<pre className="cc-tool-expanded-args">{argsFull}</pre>
{isFileTool && displayResult?.diff ? (
<DiffBlock diff={displayResult.diff} />
) : displayResult?.output ? (
<CodeBlock code={displayResult.output.slice(0, 4000)} />
) : null}
</div>
)}
Expand Down
14 changes: 11 additions & 3 deletions packages/desktop/src/stores/agent.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,11 @@ export const useAgentStore = create<AgentState & AgentActions>()(
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);
}
Expand All @@ -453,7 +457,9 @@ export const useAgentStore = create<AgentState & AgentActions>()(
);
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;
Expand All @@ -465,7 +471,9 @@ export const useAgentStore = create<AgentState & AgentActions>()(
);
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;
Expand Down
Loading
Loading