Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

5.4 KB
Raw Blame
import { motion, AnimatePresence } from 'framer-motion'
import { useState } from 'react'
import { ChevronDown } from 'lucide-react'
import type { AgentToolCall } from '../../types/agent-mode'
import { MOTION_S } from '../ui/motion'
import { askingWord } from '../status/run-status'
import { toolRegistry } from '../../api/mcp/tool-registry'

interface Props {
  toolCall: AgentToolCall
  onApprove: () => void
  onReject: () => void
  /**
   * A surface that is not the chat says who asks, as a whole sentence in
   * place of "Approve" and the words after it (Voice: "Voice wants to change
   * a file. Nothing happens until you answer.").
   */
  question?: string
}

/**
 * Inline tool-call approval strip rendered directly above the ChatInput.
 *
 * History: this used to be a centred modal with a colour-coded amber/green/red
 * palette and a dimmed backdrop. Per user feedback ("ohne Farben, kleiner,
 * sauberer, professioneller. eventuell in den chat einarbeiten") it is now a
 * compact monochrome row that sits inline in the chat instead of pulling
 * focus into a modal — readable at a glance, dismissible without breaking
 * flow.
 *
 * Keyboard shortcuts (Enter approves, Escape rejects) are NOT bound here.
 * ChatView owns that layer so the shortcuts also work while the inline
 * buttons on the tool block are off screen; a second listener here would
 * answer TWO queued approvals on one keypress, silently approving a tool the
 * user never saw. Args expand inline via the chevron button if the user wants
 * to inspect what the agent is about to run.
 *
 * What is about to happen stands in words ("change a file"), from the same
 * table as the run status. The tool's own name read like a debug line here
 * ("APPROVE file_write"); it is the tooltip of the words, and the tool block
 * in the transcript still carries it.
 */
export function ApprovalDialog({ toolCall, onApprove, onReject, question }: Props) {
  const [argsOpen, setArgsOpen] = useState(false)
  // A tool of an external MCP server says whose it is: "use Notes: search"
  // instead of the generic "use a tool" (M1).
  const shownName = toolRegistry.displayName(toolCall.toolName)
  const asking = shownName === toolCall.toolName ? askingWord(toolCall.toolName) : `use ${shownName}`

  return (
    <AnimatePresence mode="wait">
      <motion.div
        key={toolCall.id}
        initial={{ opacity: 0, y: 4 }}
        animate={{ opacity: 1, y: 0 }}
        exit={{ opacity: 0, y: 4 }}
        transition={{ duration: MOTION_S.fast }}
        className="mb-1.5 rounded-md border border-gray-200 dark:border-white/[0.08] bg-gray-50 dark:bg-white/[0.025] overflow-hidden"
      >
        <div className="flex items-center gap-2 px-2.5 py-1.5">
          {question ? (
            <span data-testid="approval-what" title={shownName} className="t-micro leading-snug text-gray-700 dark:text-gray-300 flex-1 min-w-0 break-words">
              {question}
            </span>
          ) : (
            <>
              <span className="text-[0.55rem] uppercase tracking-wider text-gray-500 dark:text-gray-500 font-medium">
                Approve
              </span>
              <span data-testid="approval-what" title={shownName} className="t-micro text-gray-700 dark:text-gray-300 font-medium truncate flex-1 min-w-0">
                {asking}
              </span>
            </>
          )}
          <button
            onClick={() => setArgsOpen((v) => !v)}
            title={argsOpen ? 'Hide arguments' : 'Show arguments'}
            className="shrink-0 p-0.5 rounded text-gray-500 hover:text-gray-700 dark:hover:text-gray-300 hover:bg-black/5 dark:hover:bg-white/5 transition-colors"
          >
            <ChevronDown
              size={11}
              className={'transition-transform ' + (argsOpen ? 'rotate-180' : '')}
            />
          </button>
          <div className="shrink-0 flex items-center gap-1 ml-1">
            <button
              onClick={onReject}
              className="px-2 py-0.5 rounded t-micro text-gray-600 dark:text-gray-400 hover:bg-black/5 dark:hover:bg-white/5 border border-gray-200 dark:border-white/10 transition-colors"
            >
              Reject
            </button>
            <button
              onClick={onApprove}
              className="px-2 py-0.5 rounded t-micro text-gray-800 dark:text-gray-100 bg-gray-100 dark:bg-white/10 hover:bg-gray-200 dark:hover:bg-white/15 border border-gray-300 dark:border-white/15 transition-colors font-medium"
            >
              Approve
            </button>
          </div>
          <span className="hidden sm:inline shrink-0 text-[0.5rem] text-gray-400 dark:text-gray-600 font-mono ml-1">
            ⏎ / Esc
          </span>
        </div>

        <AnimatePresence>
          {argsOpen && (
            <motion.div
              initial={{ height: 0, opacity: 0 }}
              animate={{ height: 'auto', opacity: 1 }}
              exit={{ height: 0, opacity: 0 }}
              transition={{ duration: MOTION_S.base }}
              className="overflow-hidden border-t border-gray-200 dark:border-white/[0.06]"
            >
              <pre className="text-[0.55rem] leading-relaxed text-gray-600 dark:text-gray-400 px-2.5 py-1.5 max-h-[180px] overflow-auto scrollbar-thin whitespace-pre-wrap break-words">
                {`${shownName}\n${JSON.stringify(toolCall.args, null, 2)}`}
              </pre>
            </motion.div>
          )}
        </AnimatePresence>
      </motion.div>
    </AnimatePresence>
  )
}