refactor: unify permission requests into agent event stream (#31)
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import type { ReactElement } from 'react';
|
||||
import type { Message } from '@/lib/types/http';
|
||||
import { ChatMessage } from '@/components/task/chat/messages/chat-message';
|
||||
import { PermissionRequestMessage } from '@/components/task/chat/messages/permission-request-message';
|
||||
import { StatusMessage } from '@/components/task/chat/messages/status-message';
|
||||
import { ToolCallMessage } from '@/components/task/chat/messages/tool-call-message';
|
||||
import { ThinkingMessage } from '@/components/task/chat/messages/thinking-message';
|
||||
@@ -11,6 +12,7 @@ import { TodoMessage } from '@/components/task/chat/messages/todo-message';
|
||||
type AdapterContext = {
|
||||
isTaskDescription: boolean;
|
||||
taskId?: string;
|
||||
permissionsByToolCallId?: Map<string, Message>;
|
||||
};
|
||||
|
||||
type MessageAdapter = {
|
||||
@@ -29,12 +31,21 @@ const adapters: MessageAdapter[] = [
|
||||
},
|
||||
{
|
||||
matches: (comment) => comment.type === 'tool_call',
|
||||
render: (comment, ctx) => <ToolCallMessage comment={comment} taskId={ctx.taskId} />,
|
||||
render: (comment, ctx) => {
|
||||
const toolCallId = (comment.metadata as { tool_call_id?: string } | undefined)?.tool_call_id;
|
||||
const permissionMessage = toolCallId ? ctx.permissionsByToolCallId?.get(toolCallId) : undefined;
|
||||
return <ToolCallMessage comment={comment} permissionMessage={permissionMessage} />;
|
||||
},
|
||||
},
|
||||
{
|
||||
matches: (comment) => comment.type === 'error' || comment.type === 'status' || comment.type === 'progress',
|
||||
render: (comment) => <StatusMessage comment={comment} />,
|
||||
},
|
||||
{
|
||||
// Standalone permission requests (no matching tool call)
|
||||
matches: (comment) => comment.type === 'permission_request',
|
||||
render: (comment) => <PermissionRequestMessage comment={comment} />,
|
||||
},
|
||||
{
|
||||
matches: () => true,
|
||||
render: (comment, ctx) => {
|
||||
@@ -73,10 +84,11 @@ type MessageRendererProps = {
|
||||
comment: Message;
|
||||
isTaskDescription: boolean;
|
||||
taskId?: string;
|
||||
permissionsByToolCallId?: Map<string, Message>;
|
||||
};
|
||||
|
||||
export function MessageRenderer({ comment, isTaskDescription, taskId }: MessageRendererProps) {
|
||||
const ctx = { isTaskDescription, taskId };
|
||||
export function MessageRenderer({ comment, isTaskDescription, taskId, permissionsByToolCallId }: MessageRendererProps) {
|
||||
const ctx = { isTaskDescription, taskId, permissionsByToolCallId };
|
||||
const adapter = adapters.find((entry) => entry.matches(comment, ctx)) ?? adapters[adapters.length - 1];
|
||||
return adapter.render(comment, ctx);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
'use client';
|
||||
|
||||
import { useCallback, useState } from 'react';
|
||||
import {
|
||||
IconAlertTriangle,
|
||||
IconCheck,
|
||||
IconX,
|
||||
IconLoader2,
|
||||
} from '@tabler/icons-react';
|
||||
import { Button } from '@kandev/ui/button';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { getWebSocketClient } from '@/lib/ws/connection';
|
||||
import type { Message } from '@/lib/types/http';
|
||||
|
||||
type PermissionOption = {
|
||||
option_id: string;
|
||||
name: string;
|
||||
kind: string;
|
||||
};
|
||||
|
||||
type PermissionRequestMetadata = {
|
||||
pending_id: string;
|
||||
tool_call_id?: string;
|
||||
options: PermissionOption[];
|
||||
action_type: string;
|
||||
action_details?: {
|
||||
command?: string;
|
||||
path?: string;
|
||||
cwd?: string;
|
||||
};
|
||||
status?: 'pending' | 'approved' | 'rejected';
|
||||
};
|
||||
|
||||
type PermissionRequestMessageProps = {
|
||||
comment: Message;
|
||||
};
|
||||
|
||||
export function PermissionRequestMessage({ comment }: PermissionRequestMessageProps) {
|
||||
const [isResponding, setIsResponding] = useState(false);
|
||||
const metadata = comment.metadata as PermissionRequestMetadata | undefined;
|
||||
|
||||
const isResolved = metadata?.status === 'approved' || metadata?.status === 'rejected';
|
||||
const isPending = !isResolved;
|
||||
|
||||
const handleRespond = useCallback(
|
||||
async (optionId: string, cancelled: boolean = false) => {
|
||||
if (!metadata) return;
|
||||
|
||||
const client = getWebSocketClient();
|
||||
if (!client) {
|
||||
console.error('WebSocket client not available');
|
||||
return;
|
||||
}
|
||||
|
||||
setIsResponding(true);
|
||||
try {
|
||||
await client.request('permission.respond', {
|
||||
session_id: comment.session_id,
|
||||
pending_id: metadata.pending_id,
|
||||
option_id: cancelled ? undefined : optionId,
|
||||
cancelled,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to respond to permission request:', error);
|
||||
} finally {
|
||||
setIsResponding(false);
|
||||
}
|
||||
},
|
||||
[comment.session_id, metadata]
|
||||
);
|
||||
|
||||
const handleApprove = useCallback(() => {
|
||||
const allowOption = metadata?.options.find(
|
||||
(opt) => opt.kind === 'allow_once' || opt.kind === 'allow_always'
|
||||
);
|
||||
if (allowOption) {
|
||||
handleRespond(allowOption.option_id);
|
||||
}
|
||||
}, [metadata, handleRespond]);
|
||||
|
||||
const handleReject = useCallback(() => {
|
||||
const rejectOption = metadata?.options.find(
|
||||
(opt) => opt.kind === 'reject_once' || opt.kind === 'reject_always'
|
||||
);
|
||||
if (rejectOption) {
|
||||
handleRespond(rejectOption.option_id);
|
||||
} else {
|
||||
handleRespond('', true);
|
||||
}
|
||||
}, [metadata, handleRespond]);
|
||||
|
||||
const getStatusBadge = () => {
|
||||
if (metadata?.status === 'approved') {
|
||||
return (
|
||||
<span className="inline-flex items-center gap-1 text-xs text-green-600 dark:text-green-400">
|
||||
<IconCheck className="h-3 w-3" /> Approved
|
||||
</span>
|
||||
);
|
||||
}
|
||||
if (metadata?.status === 'rejected') {
|
||||
return (
|
||||
<span className="inline-flex items-center gap-1 text-xs text-red-600 dark:text-red-400">
|
||||
<IconX className="h-3 w-3" /> Rejected
|
||||
</span>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<span className="inline-flex items-center gap-1 text-xs text-amber-600 dark:text-amber-400">
|
||||
<IconLoader2 className="h-3 w-3 animate-spin" /> Pending Approval
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
'w-full rounded-md border overflow-hidden',
|
||||
isPending ? 'border-amber-500/50 bg-amber-500/5' : 'border-border/50 bg-muted/20'
|
||||
)}
|
||||
>
|
||||
{/* Header */}
|
||||
<div className="flex items-center gap-2 px-3 py-2 text-sm">
|
||||
<IconAlertTriangle
|
||||
className={cn(
|
||||
'h-4 w-4 flex-shrink-0',
|
||||
isPending ? 'text-amber-600 dark:text-amber-400' : 'text-muted-foreground'
|
||||
)}
|
||||
/>
|
||||
<span className="flex-1 font-medium text-foreground truncate">
|
||||
{comment.content || 'Permission Required'}
|
||||
</span>
|
||||
{getStatusBadge()}
|
||||
</div>
|
||||
|
||||
{/* Approval buttons - only show when pending */}
|
||||
{isPending && (
|
||||
<div className="flex items-center gap-2 px-3 py-2 border-t border-amber-500/30 bg-amber-500/10">
|
||||
<span className="text-xs text-amber-600 dark:text-amber-400 flex-1">
|
||||
Approve this action?
|
||||
</span>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={handleReject}
|
||||
disabled={isResponding}
|
||||
className="h-7 px-2 text-red-600 hover:text-red-700 hover:bg-red-500/10"
|
||||
>
|
||||
<IconX className="h-4 w-4 mr-1" />
|
||||
Deny
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={handleApprove}
|
||||
disabled={isResponding}
|
||||
className="h-7 px-2 text-green-600 hover:text-green-700 hover:bg-green-500/10"
|
||||
>
|
||||
{isResponding ? (
|
||||
<IconLoader2 className="h-4 w-4 mr-1 animate-spin" />
|
||||
) : (
|
||||
<IconCheck className="h-4 w-4 mr-1" />
|
||||
)}
|
||||
Approve
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -16,10 +16,28 @@ import {
|
||||
} from '@tabler/icons-react';
|
||||
import { Button } from '@kandev/ui/button';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { getWebSocketClient } from '@/lib/ws/connection';
|
||||
import type { Message } from '@/lib/types/http';
|
||||
import type { ToolCallMetadata } from '@/components/task/chat/types';
|
||||
import { useAppStore } from '@/components/state-provider';
|
||||
import { getWebSocketClient } from '@/lib/ws/connection';
|
||||
|
||||
type PermissionOption = {
|
||||
option_id: string;
|
||||
name: string;
|
||||
kind: string;
|
||||
};
|
||||
|
||||
type PermissionRequestMetadata = {
|
||||
pending_id: string;
|
||||
tool_call_id: string;
|
||||
options: PermissionOption[];
|
||||
action_type: string;
|
||||
action_details: {
|
||||
command?: string;
|
||||
path?: string;
|
||||
cwd?: string;
|
||||
};
|
||||
status?: 'pending' | 'approved' | 'rejected';
|
||||
};
|
||||
|
||||
function getToolIcon(toolName: string | undefined, className: string) {
|
||||
const name = toolName?.toLowerCase() ?? '';
|
||||
@@ -41,7 +59,15 @@ function getToolIcon(toolName: string | undefined, className: string) {
|
||||
return <IconCode className={className} />;
|
||||
}
|
||||
|
||||
function getStatusIcon(status?: string) {
|
||||
function getStatusIcon(status?: string, permissionStatus?: string) {
|
||||
// If there's a permission status, show that instead
|
||||
if (permissionStatus === 'approved') {
|
||||
return <IconCheck className="h-3.5 w-3.5 text-green-500" />;
|
||||
}
|
||||
if (permissionStatus === 'rejected') {
|
||||
return <IconX className="h-3.5 w-3.5 text-red-500" />;
|
||||
}
|
||||
|
||||
switch (status) {
|
||||
case 'complete':
|
||||
return <IconCheck className="h-3.5 w-3.5 text-green-500" />;
|
||||
@@ -56,74 +82,64 @@ function getStatusIcon(status?: string) {
|
||||
|
||||
type ToolCallMessageProps = {
|
||||
comment: Message;
|
||||
taskId?: string;
|
||||
permissionMessage?: Message;
|
||||
};
|
||||
|
||||
export function ToolCallMessage({ comment, taskId }: ToolCallMessageProps) {
|
||||
export function ToolCallMessage({ comment, permissionMessage }: ToolCallMessageProps) {
|
||||
const [isExpanded, setIsExpanded] = useState(false);
|
||||
const [isResponding, setIsResponding] = useState(false);
|
||||
const metadata = comment.metadata as ToolCallMetadata | undefined;
|
||||
const pendingPermissions = useAppStore((state) => state.permissions.pending);
|
||||
const removePendingPermission = useAppStore((state) => state.removePendingPermission);
|
||||
const activeSessionId = useAppStore((state) => state.tasks.activeSessionId);
|
||||
const permissionMetadata = permissionMessage?.metadata as PermissionRequestMetadata | undefined;
|
||||
|
||||
const toolCallId = metadata?.tool_call_id;
|
||||
const toolName = metadata?.tool_name ?? '';
|
||||
const title = metadata?.title ?? comment.content ?? 'Tool call';
|
||||
const status = metadata?.status;
|
||||
const args = metadata?.args;
|
||||
const result = metadata?.result;
|
||||
|
||||
// Find pending permission for this tool call (only if we have a valid tool_call_id)
|
||||
const pendingPermission = pendingPermissions.find(
|
||||
(p) =>
|
||||
toolCallId &&
|
||||
p.tool_call_id === toolCallId &&
|
||||
(!activeSessionId || p.session_id === activeSessionId) &&
|
||||
(!taskId || p.task_id === taskId)
|
||||
);
|
||||
const isPendingApproval = !!pendingPermission;
|
||||
// Permission state
|
||||
const hasPermission = !!permissionMessage;
|
||||
const permissionStatus = permissionMetadata?.status;
|
||||
const isPermissionPending = hasPermission && permissionStatus !== 'approved' && permissionStatus !== 'rejected';
|
||||
|
||||
const handleRespond = useCallback(
|
||||
async (optionId: string, cancelled: boolean = false) => {
|
||||
if (!pendingPermission) return;
|
||||
setIsResponding(true);
|
||||
if (!permissionMetadata || !permissionMessage) return;
|
||||
|
||||
const client = getWebSocketClient();
|
||||
if (!client) {
|
||||
console.error('WebSocket client not available');
|
||||
setIsResponding(false);
|
||||
return;
|
||||
}
|
||||
|
||||
setIsResponding(true);
|
||||
try {
|
||||
await client.request('permission.respond', {
|
||||
session_id: pendingPermission.session_id,
|
||||
pending_id: pendingPermission.pending_id,
|
||||
session_id: permissionMessage.session_id,
|
||||
pending_id: permissionMetadata.pending_id,
|
||||
option_id: cancelled ? undefined : optionId,
|
||||
cancelled,
|
||||
});
|
||||
removePendingPermission(pendingPermission.pending_id);
|
||||
} catch (error) {
|
||||
console.error('Failed to respond to permission request:', error);
|
||||
} finally {
|
||||
setIsResponding(false);
|
||||
}
|
||||
},
|
||||
[pendingPermission, removePendingPermission]
|
||||
[permissionMessage, permissionMetadata]
|
||||
);
|
||||
|
||||
const handleApprove = useCallback(() => {
|
||||
const allowOption = pendingPermission?.options.find(
|
||||
const allowOption = permissionMetadata?.options.find(
|
||||
(opt) => opt.kind === 'allow_once' || opt.kind === 'allow_always'
|
||||
);
|
||||
if (allowOption) {
|
||||
handleRespond(allowOption.option_id);
|
||||
}
|
||||
}, [pendingPermission, handleRespond]);
|
||||
}, [permissionMetadata, handleRespond]);
|
||||
|
||||
const handleReject = useCallback(() => {
|
||||
const rejectOption = pendingPermission?.options.find(
|
||||
const rejectOption = permissionMetadata?.options.find(
|
||||
(opt) => opt.kind === 'reject_once' || opt.kind === 'reject_always'
|
||||
);
|
||||
if (rejectOption) {
|
||||
@@ -131,9 +147,12 @@ export function ToolCallMessage({ comment, taskId }: ToolCallMessageProps) {
|
||||
} else {
|
||||
handleRespond('', true);
|
||||
}
|
||||
}, [pendingPermission, handleRespond]);
|
||||
}, [permissionMetadata, handleRespond]);
|
||||
|
||||
const toolIcon = getToolIcon(toolName, 'h-4 w-4 text-amber-600 dark:text-amber-400 flex-shrink-0');
|
||||
const toolIcon = getToolIcon(toolName, cn(
|
||||
'h-4 w-4 flex-shrink-0',
|
||||
isPermissionPending ? 'text-amber-600 dark:text-amber-400' : 'text-amber-600 dark:text-amber-400'
|
||||
));
|
||||
const hasDetails = args && Object.keys(args).length > 0;
|
||||
|
||||
let filePath: string | undefined;
|
||||
@@ -151,9 +170,7 @@ export function ToolCallMessage({ comment, taskId }: ToolCallMessageProps) {
|
||||
return (
|
||||
<div className={cn(
|
||||
'w-full rounded-md border overflow-hidden',
|
||||
isPendingApproval
|
||||
? 'border-amber-500/50 bg-amber-500/5'
|
||||
: 'border-border/50 bg-muted/20'
|
||||
isPermissionPending ? 'border-amber-500/50 bg-amber-500/5' : 'border-border/50 bg-muted/20'
|
||||
)}>
|
||||
<button
|
||||
type="button"
|
||||
@@ -173,7 +190,7 @@ export function ToolCallMessage({ comment, taskId }: ToolCallMessageProps) {
|
||||
{filePath}
|
||||
</span>
|
||||
)}
|
||||
{!isPendingApproval && getStatusIcon(status)}
|
||||
{getStatusIcon(status, permissionStatus)}
|
||||
{hasDetails && (
|
||||
isExpanded
|
||||
? <IconChevronDown className="h-4 w-4 text-muted-foreground/50" />
|
||||
@@ -181,39 +198,6 @@ export function ToolCallMessage({ comment, taskId }: ToolCallMessageProps) {
|
||||
)}
|
||||
</button>
|
||||
|
||||
{/* Inline approval buttons when permission is pending */}
|
||||
{isPendingApproval && (
|
||||
<div className="flex items-center gap-2 px-3 py-2 border-t border-amber-500/30 bg-amber-500/10">
|
||||
<span className="text-xs text-amber-600 dark:text-amber-400 flex-1">
|
||||
Approve this action?
|
||||
</span>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={handleReject}
|
||||
disabled={isResponding}
|
||||
className="h-7 px-2 text-red-600 hover:text-red-700 hover:bg-red-500/10"
|
||||
>
|
||||
<IconX className="h-4 w-4 mr-1" />
|
||||
Deny
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={handleApprove}
|
||||
disabled={isResponding}
|
||||
className="h-7 px-2 text-green-600 hover:text-green-700 hover:bg-green-500/10"
|
||||
>
|
||||
{isResponding ? (
|
||||
<IconLoader2 className="h-4 w-4 mr-1 animate-spin" />
|
||||
) : (
|
||||
<IconCheck className="h-4 w-4 mr-1" />
|
||||
)}
|
||||
Approve
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isExpanded && hasDetails && (
|
||||
<div className="border-t border-border/30 bg-background/50 p-3 space-y-2">
|
||||
{args && Object.entries(args).map(([key, value]) => {
|
||||
@@ -243,6 +227,39 @@ export function ToolCallMessage({ comment, taskId }: ToolCallMessageProps) {
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Inline permission approval UI */}
|
||||
{isPermissionPending && (
|
||||
<div className="flex items-center gap-2 px-3 py-2 border-t border-amber-500/30 bg-amber-500/10">
|
||||
<span className="text-xs text-amber-600 dark:text-amber-400 flex-1">
|
||||
Approve this action?
|
||||
</span>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={handleReject}
|
||||
disabled={isResponding}
|
||||
className="h-7 px-2 text-red-600 hover:text-red-700 hover:bg-red-500/10"
|
||||
>
|
||||
<IconX className="h-4 w-4 mr-1" />
|
||||
Deny
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={handleApprove}
|
||||
disabled={isResponding}
|
||||
className="h-7 px-2 text-green-600 hover:text-green-700 hover:bg-green-500/10"
|
||||
>
|
||||
{isResponding ? (
|
||||
<IconLoader2 className="h-4 w-4 mr-1 animate-spin" />
|
||||
) : (
|
||||
<IconCheck className="h-4 w-4 mr-1" />
|
||||
)}
|
||||
Approve
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,187 +0,0 @@
|
||||
'use client';
|
||||
|
||||
import { useCallback, useMemo, useState } from 'react';
|
||||
import { IconTerminal2, IconAlertTriangle, IconLoader2 } from '@tabler/icons-react';
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from '@kandev/ui/alert-dialog';
|
||||
import { useAppStore } from '@/components/state-provider';
|
||||
import { getWebSocketClient } from '@/lib/ws/connection';
|
||||
import type { Message } from '@/lib/types/http';
|
||||
|
||||
const EMPTY_MESSAGES: Message[] = [];
|
||||
|
||||
type CommandApprovalDialogProps = {
|
||||
taskId: string;
|
||||
/** Only show permissions that don't have a matching tool call (standalone permissions like workspace indexing) */
|
||||
standaloneOnly?: boolean;
|
||||
};
|
||||
|
||||
export function CommandApprovalDialog({ taskId, standaloneOnly = false }: CommandApprovalDialogProps) {
|
||||
const [isResponding, setIsResponding] = useState(false);
|
||||
const pendingPermissions = useAppStore((state) => state.permissions.pending);
|
||||
const removePendingPermission = useAppStore((state) => state.removePendingPermission);
|
||||
const activeSessionId = useAppStore((state) => state.tasks.activeSessionId);
|
||||
const messages = useAppStore((state) =>
|
||||
activeSessionId ? state.messages.bySession[activeSessionId] ?? EMPTY_MESSAGES : EMPTY_MESSAGES
|
||||
);
|
||||
|
||||
// Get tool_call_ids from all tool call messages
|
||||
const toolCallIds = useMemo(() => {
|
||||
const ids = new Set<string>();
|
||||
for (const msg of messages) {
|
||||
const tcid = msg.metadata?.tool_call_id;
|
||||
if (msg.type === 'tool_call' && typeof tcid === 'string' && tcid) {
|
||||
ids.add(tcid);
|
||||
}
|
||||
}
|
||||
return ids;
|
||||
}, [messages]);
|
||||
|
||||
// Get the first pending permission for this task
|
||||
// If standaloneOnly, filter out permissions that have a matching tool call
|
||||
const permission = useMemo(() => {
|
||||
return pendingPermissions.find((p) => {
|
||||
if (activeSessionId) {
|
||||
if (p.session_id !== activeSessionId) return false;
|
||||
} else if (p.task_id !== taskId) {
|
||||
return false;
|
||||
}
|
||||
if (standaloneOnly && p.tool_call_id && toolCallIds.has(p.tool_call_id)) {
|
||||
return false; // This permission will be handled by inline UI
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}, [pendingPermissions, activeSessionId, taskId, standaloneOnly, toolCallIds]);
|
||||
|
||||
const handleRespond = useCallback(
|
||||
async (optionId: string, cancelled: boolean = false) => {
|
||||
if (!permission) return;
|
||||
setIsResponding(true);
|
||||
|
||||
const client = getWebSocketClient();
|
||||
if (!client) {
|
||||
console.error('WebSocket client not available');
|
||||
setIsResponding(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await client.request('permission.respond', {
|
||||
session_id: permission.session_id,
|
||||
pending_id: permission.pending_id,
|
||||
option_id: cancelled ? undefined : optionId,
|
||||
cancelled,
|
||||
});
|
||||
removePendingPermission(permission.pending_id);
|
||||
} catch (error) {
|
||||
console.error('Failed to respond to permission request:', error);
|
||||
} finally {
|
||||
setIsResponding(false);
|
||||
}
|
||||
},
|
||||
[permission, removePendingPermission]
|
||||
);
|
||||
|
||||
const handleApprove = useCallback(() => {
|
||||
// Find the "allow" option (allow_once or allow_always)
|
||||
const allowOption = permission?.options.find(
|
||||
(opt) => opt.kind === 'allow_once' || opt.kind === 'allow_always'
|
||||
);
|
||||
if (allowOption) {
|
||||
handleRespond(allowOption.option_id);
|
||||
}
|
||||
}, [permission, handleRespond]);
|
||||
|
||||
const handleReject = useCallback(() => {
|
||||
// Find the "reject" option (reject_once or reject_always)
|
||||
const rejectOption = permission?.options.find(
|
||||
(opt) => opt.kind === 'reject_once' || opt.kind === 'reject_always'
|
||||
);
|
||||
if (rejectOption) {
|
||||
handleRespond(rejectOption.option_id);
|
||||
} else {
|
||||
// If no reject option, cancel the request
|
||||
handleRespond('', true);
|
||||
}
|
||||
}, [permission, handleRespond]);
|
||||
|
||||
if (!permission) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Extract command details from action_details
|
||||
const actionDetails = permission.action_details || {};
|
||||
const command = actionDetails.command as string | undefined;
|
||||
const cwd = actionDetails.cwd as string | undefined;
|
||||
const reasoning = actionDetails.reasoning as string | undefined;
|
||||
|
||||
return (
|
||||
<AlertDialog open={!!permission}>
|
||||
<AlertDialogContent className="max-w-lg">
|
||||
<AlertDialogHeader>
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex h-10 w-10 items-center justify-center rounded-full bg-amber-500/10">
|
||||
<IconAlertTriangle className="h-5 w-5 text-amber-500" />
|
||||
</div>
|
||||
<AlertDialogTitle className="text-base">
|
||||
{permission.title || 'Command Execution Approval'}
|
||||
</AlertDialogTitle>
|
||||
</div>
|
||||
<AlertDialogDescription className="text-left">
|
||||
The agent is requesting permission to execute a command.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
|
||||
<div className="space-y-3">
|
||||
{command && (
|
||||
<div className="rounded-md border bg-muted/50 p-3">
|
||||
<div className="mb-1 flex items-center gap-1.5 text-xs font-medium text-muted-foreground">
|
||||
<IconTerminal2 className="h-3.5 w-3.5" />
|
||||
Command
|
||||
</div>
|
||||
<code className="block whitespace-pre-wrap break-all font-mono text-sm">
|
||||
{command}
|
||||
</code>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{cwd && (
|
||||
<div className="text-xs text-muted-foreground">
|
||||
<span className="font-medium">Working directory:</span> {cwd}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{reasoning && (
|
||||
<div className="text-sm text-muted-foreground">
|
||||
<span className="font-medium">Reason:</span> {reasoning}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel onClick={handleReject} disabled={isResponding}>
|
||||
Reject
|
||||
</AlertDialogCancel>
|
||||
<AlertDialogAction onClick={handleApprove} variant="default" disabled={isResponding}>
|
||||
{isResponding ? (
|
||||
<>
|
||||
<IconLoader2 className="h-4 w-4 mr-1 animate-spin" />
|
||||
Approving...
|
||||
</>
|
||||
) : (
|
||||
'Approve'
|
||||
)}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { FormEvent, useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { FormEvent, useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { flushSync } from 'react-dom';
|
||||
import { useVirtualizer } from '@tanstack/react-virtual';
|
||||
import { IconBrain, IconListCheck, IconLoader2, IconPaperclip } from '@tabler/icons-react';
|
||||
@@ -68,18 +68,62 @@ export function TaskChatPanel({
|
||||
const showLoadingState = (messagesLoading || isInitialLoading) && !isWorking;
|
||||
const { loadMore, hasMore, isLoading: isLoadingMore } = useLazyLoadMessages(resolvedSessionId);
|
||||
|
||||
// Build a set of tool_call_ids that exist in the messages
|
||||
const toolCallIds = useMemo(() => {
|
||||
const set = new Set<string>();
|
||||
for (const message of messages) {
|
||||
if (message.type === 'tool_call') {
|
||||
const toolCallId = (message.metadata as { tool_call_id?: string } | undefined)?.tool_call_id;
|
||||
if (toolCallId) {
|
||||
set.add(toolCallId);
|
||||
}
|
||||
}
|
||||
}
|
||||
return set;
|
||||
}, [messages]);
|
||||
|
||||
// Build a lookup map of tool_call_id → permission_request message
|
||||
const permissionsByToolCallId = useMemo(() => {
|
||||
const map = new Map<string, Message>();
|
||||
for (const message of messages) {
|
||||
if (message.type === 'permission_request') {
|
||||
const toolCallId = (message.metadata as { tool_call_id?: string } | undefined)?.tool_call_id;
|
||||
if (toolCallId) {
|
||||
map.set(toolCallId, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}, [messages]);
|
||||
|
||||
// Filter to only show chat-relevant types.
|
||||
const visibleMessages = messages.filter((message) =>
|
||||
!message.type ||
|
||||
message.type === 'message' ||
|
||||
message.type === 'content' ||
|
||||
message.type === 'tool_call' ||
|
||||
message.type === 'progress' ||
|
||||
message.type === 'status' ||
|
||||
message.type === 'error' ||
|
||||
message.type === 'thinking' ||
|
||||
message.type === 'todo'
|
||||
);
|
||||
// Permission requests are only hidden if they have a matching tool_call in the messages
|
||||
// (so they can be displayed inline). Standalone permissions are shown as separate messages.
|
||||
const visibleMessages = messages.filter((message) => {
|
||||
// Standard visible types
|
||||
if (
|
||||
!message.type ||
|
||||
message.type === 'message' ||
|
||||
message.type === 'content' ||
|
||||
message.type === 'tool_call' ||
|
||||
message.type === 'progress' ||
|
||||
message.type === 'status' ||
|
||||
message.type === 'error' ||
|
||||
message.type === 'thinking' ||
|
||||
message.type === 'todo'
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Permission requests: show if no matching tool_call exists (standalone permission)
|
||||
if (message.type === 'permission_request') {
|
||||
const toolCallId = (message.metadata as { tool_call_id?: string } | undefined)?.tool_call_id;
|
||||
// Show as standalone if no tool_call_id or no matching tool_call message exists
|
||||
return !toolCallId || !toolCallIds.has(toolCallId);
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
// Create a synthetic "user" message for the task description
|
||||
const taskDescriptionMessage: Message | null =
|
||||
@@ -248,6 +292,7 @@ export function TaskChatPanel({
|
||||
comment={message}
|
||||
isTaskDescription={message.id === 'task-description'}
|
||||
taskId={taskId ?? undefined}
|
||||
permissionsByToolCallId={permissionsByToolCallId}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user