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。

4.1 KB
Raw Blame
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { PluginSetupPrompt } from '@/components/new-chat/PluginSetupPrompt';
import { readBotAuthorizationCard } from '../../../shared/botAuthorization';
import {
  assistRemotePluginOauth,
  isRemoteSessionSticky,
  makerApiForSticky,
} from '@/lib/makerTransport';
import type { PluginSetupCommandInFlight } from '@/lib/makerChatStore';
import { getStickySessionDeviceId } from '@/features/device-link/stickySessionOrigin';

/** Same composable form for Host and plugin accounts, rendered in the transcript. */
export function BotAuthorizationCardView({
  data,
  sessionId,
}: {
  data?: Record<string, unknown>;
  sessionId?: string;
}) {
  const card = readBotAuthorizationCard(data);
  if (!card || card.sessionId !== sessionId) return null;
  return (
    <AuthorizationBody key={`${card.snapshot.requestId}:${card.snapshot.revision}`} card={card} />
  );
}
function AuthorizationBody({
  card,
}: {
  card: NonNullable<ReturnType<typeof readBotAuthorizationCard>>;
}) {
  const { t } = useTranslation();
  const [busy, setBusy] = useState<PluginSetupCommandInFlight | null>(null);
  const [failed, setFailed] = useState(false);
  const remote = isRemoteSessionSticky(card.sessionId);
  return (
    <div className="my-2 max-w-xl">
      <PluginSetupPrompt
        // Bot cards bind only the OAuth bridge. Do not expose private forms even
        // if a retained/newer snapshot advertises the regular setup capabilities.
        pending={
          remote
            ? { ...card.snapshot, remoteSecret: undefined, remoteConnection: undefined }
            : card.snapshot
        }
        compact
        viewerState="expanded"
        onViewerStateChange={() => {}}
        remote={remote}
        remoteDeviceId={getStickySessionDeviceId(card.sessionId) ?? undefined}
        commandInFlight={busy}
        onCommand={(requestId, action, actionId, values) => {
          const remoteOauth =
            remote &&
            card.snapshot.remoteOauth &&
            action === 'run_action' &&
            actionId &&
            card.snapshot.steps.some(
              (s) => s.action?.id === actionId && s.action.kind === 'oauth_connect',
            );
          const cancelRemoteOauth =
            action === 'cancel' &&
            remote &&
            card.snapshot.remoteOauth &&
            busy?.action === 'run_action';
          if (busy && !cancelRemoteOauth) return;
          if (remote && action !== 'cancel' && !remoteOauth) {
            setFailed(true);
            return;
          }
          setFailed(false);
          const pending = { requestId, action, actionId };
          setBusy(pending);
          const command =
            remoteOauth && actionId
              ? assistRemotePluginOauth(card.sessionId, {
                  ghostId: card.snapshot.ghost.id,
                  requestId,
                  actionId,
                  expectedRevision: card.snapshot.revision,
                })
              : action === 'submit_form' && actionId && values && !remote
                ? window.electronAPI.maker.submitPluginSetupInline({
                    requestId,
                    actionId,
                    expectedRevision: card.snapshot.revision,
                    value: values.value,
                  })
                : makerApiForSticky(card.sessionId).resolveInteraction(requestId, {
                    kind: 'plugin_setup',
                    action: action === 'cancel' ? 'cancel' : 'run_action',
                    ...(actionId ? { actionId } : {}),
                    expectedRevision: card.snapshot.revision,
                  });
          void command
            .then((result) => {
              if (result && 'accepted' in result && !result.accepted) setFailed(true);
            })
            .catch(() => setFailed(true))
            .finally(() => setBusy((current) => (current === pending ? null : current)));
        }}
      />
      {failed ? (
        <p role="alert" className="mt-2 text-13 text-[var(--error-fg)]">
          {t('newChat.pluginSetup.error.ACTION_FAILED')}
        </p>
      ) : null}
    </div>
  );
}