Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

6.8 KB
Raw Blame
import * as AlertDialog from '@radix-ui/react-alert-dialog';
import { useCallback } from 'react';
import { useDialogExit } from '@/hooks/useDialogExit';
import { WINDOW_DRAG_STYLE, WINDOW_NO_DRAG_STYLE } from '@/components/layout/windowDrag';
/**
 * 插件卡片(GhostToolCard)的两个宿主交互面:
 *   - GhostCardPromptPanel:data-ghost-prompt 动作的提示词输入面板(与老基座
 *     ChatImageActions 的 imgPrompt popover 同体验:textarea + 回车发送/Esc 取消),
 *     锚在被点按钮下方;
 *   - GhostCardLinkConfirm:data-ghost-link 外链确认框,域名醒目 + 完整链接全量
 *     展示——卡内文案归意识,真实去向由宿主如实亮给用户,确认才 openExternal。
 *
 * 两者都按 DESIGN.md 关闭规则点外部不关闭(免得误点丢掉已输入的提示词):遮罩只
 * 拦截点击,并 preventDefault 保住焦点,让回车 / Esc 仍有效。
 */
import { Button } from '@/components/ui/button';
import { ListComposerTextarea } from '@/components/new-chat/ListComposerTextarea';
import { useTranslation } from 'react-i18next';

import { GHOST_CARD_ACTION_PROMPT_MAX_LEN } from '@/../shared/ghost';

/** 吞掉卡片外的点击。锚定在卡片上的输入浮层保持透明;居中的确认框是模态弹窗,用统一遮罩(DESIGN §4)。 */
function BlockingScrim({ modal = false }: { modal?: boolean }) {
  return (
    <div
      className={modal ? 'modal-scrim fixed inset-0 z-40' : 'fixed inset-0 z-40'}
      data-testid="ghost-card-scrim"
      onMouseDown={(e) => e.preventDefault()}
    />
  );
}

export function GhostCardPromptPanel({
  top,
  left,
  placeholder,
  text,
  onTextChange,
  onSubmit,
  onCancel,
}: {
  top: number;
  left: number;
  placeholder: string;
  text: string;
  onTextChange: (text: string) => void;
  onSubmit: () => void;
  onCancel: () => void;
}) {
  const { t } = useTranslation();
  const canSend = !!text.trim();
  return (
    <>
      <BlockingScrim />
      <div
        className="absolute z-50 w-72 rounded-md border p-2"
        style={{
          top,
          left,
          backgroundColor: 'var(--surface-elevated)',
          borderColor: 'var(--border-default)',
          boxShadow: 'var(--shadow-menu)',
        }}
      >
        <ListComposerTextarea
          autoFocus
          rows={3}
          value={text}
          maxLength={GHOST_CARD_ACTION_PROMPT_MAX_LEN}
          onChange={(e) => onTextChange(e.target.value)}
          onKeyDown={(e) => {
            // 中文输入法组词中的 Enter 不能触发发送(同老基座)。
            if (e.nativeEvent.isComposing) return;
            if (e.key === 'Enter' && !e.shiftKey) {
              e.preventDefault();
              onSubmit();
            } else if (e.key === 'Escape') {
              onCancel();
            }
          }}
          placeholder={placeholder || t('chat.mivoAction.promptPlaceholder')}
          className="w-full resize-none rounded-md border px-2 py-1.5 text-xs outline-none placeholder:text-[var(--text-placeholder)]"
          style={{
            backgroundColor: 'var(--msg-tool-card-bg)',
            borderColor: 'var(--msg-tool-card-border)',
            color: 'var(--msg-tool-card-text)',
          }}
        />
        <div className="mt-1.5 flex items-center justify-end gap-1.5">
          <button
            type="button"
            onClick={onCancel}
            className="h-6 cursor-pointer rounded-md px-2 text-xs transition-colors"
            style={{ color: 'var(--text-secondary)' }}
          >
            {t('chat.mivoAction.promptCancel')}
          </button>
          <button
            type="button"
            onClick={onSubmit}
            disabled={!canSend}
            className={
              'h-6 rounded-md border px-2.5 text-xs font-medium transition-colors ' +
              (canSend
                ? 'cursor-pointer hover:bg-[var(--msg-table-header-bg)]'
                : 'cursor-not-allowed opacity-40')
            }
            style={{
              backgroundColor: 'var(--msg-tool-card-bg)',
              borderColor: 'var(--msg-tool-card-border)',
              color: 'var(--msg-tool-card-text)',
            }}
          >
            {t('chat.mivoAction.promptSend')}
          </button>
        </div>
      </div>
    </>
  );
}

export function GhostCardLinkConfirm({
  url,
  host,
  onConfirm: notifyConfirmed,
  onCancel: notifyCanceled,
}: {
  url: string;
  /** 醒目域名;解析失败传空串,正文仍展示全串。 */
  host: string;
  onConfirm: () => void;
  onCancel: () => void;
}) {
  const { t } = useTranslation();
  const dialog = useDialogExit();
  const onCancel = useCallback(() => dialog.close(notifyCanceled), [dialog.close, notifyCanceled]);
  const onConfirm = useCallback(() => dialog.close(notifyConfirmed), [dialog.close, notifyConfirmed]);
  return (
    <AlertDialog.Root open={dialog.open} onOpenChange={(open) => { if (!open) onCancel(); }}>
      <AlertDialog.Portal>
        <AlertDialog.Overlay
          className="modal-scrim fixed inset-0 z-40"
          style={WINDOW_DRAG_STYLE}
          data-testid="ghost-card-scrim"
          onMouseDown={(event) => event.preventDefault()}
        />
      <AlertDialog.Content
        className="modal-panel fixed inset-0 z-50 m-auto h-fit w-80 p-3.5 outline-none"
        style={WINDOW_NO_DRAG_STYLE}
        onOpenAutoFocus={dialog.onOpenAutoFocus}
        onCloseAutoFocus={dialog.onCloseAutoFocus}
      >
        <AlertDialog.Title asChild><div className="text-13 font-semibold" style={{ color: 'var(--text-primary)' }}>
          {t('chat.ghostCall.linkConfirmTitle')}
        </div></AlertDialog.Title>
        <AlertDialog.Description asChild><div className="mt-1.5 text-xs" style={{ color: 'var(--text-secondary)' }}>
          {t('chat.ghostCall.linkConfirmHint')}
        </div></AlertDialog.Description>
        {host ? (
          <div
            className="mt-1.5 break-all text-13 font-semibold"
            style={{ color: 'var(--text-primary)' }}
          >
            {host}
          </div>
        ) : null}
        <div
          className="mt-1 max-h-24 overflow-y-auto break-all font-mono text-11 leading-relaxed"
          style={{ color: 'var(--text-tertiary)' }}
        >
          {url}
        </div>
        <div className="mt-2.5 flex items-center justify-end gap-1.5">
          <AlertDialog.Cancel asChild><Button
            variant="secondary"
            tone="quiet"
            size="xs"
            compact
            type="button"
          >
            {t('chat.ghostCall.linkConfirmCancel')}
          </Button></AlertDialog.Cancel>
          <Button variant="secondary" size="xs" compact type="button" onClick={onConfirm}>
            {t('chat.ghostCall.linkConfirmOpen')}
          </Button>
        </div>
      </AlertDialog.Content>
      </AlertDialog.Portal>
    </AlertDialog.Root>
  );
}