Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

23.5 KB
Raw Blame
'use client';

import {
  EXPERTISE_RULE_DIRECTIONS,
  type ExpertiseEnforcement,
  type ExpertiseReasonKind,
} from '@lobechat/types';
import {
  ActionIcon,
  Block,
  Button,
  createStaticStyles,
  cssVar,
  cx,
  type DropdownItem,
  DropdownMenu,
  Flexbox,
  Icon,
  SkeletonText,
  Tag,
  Text,
  TextArea,
} from '@lobehub/ui';
import dayjs from 'dayjs';
import {
  ActivityIcon,
  BellIcon,
  ClipboardCheckIcon,
  FileTextIcon,
  HistoryIcon,
  type LucideIcon,
  MessageSquareTextIcon,
  MoreHorizontalIcon,
  PencilIcon,
  ScaleIcon,
  ShieldCheckIcon,
  TargetIcon,
} from 'lucide-react';
import { type ReactNode, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router';

import { useActiveWorkspaceSlug } from '@/business/client/hooks/useActiveWorkspaceSlug';
import { docsFilePath, docsPagePath } from '@/features/ResourceHome/paths';
import RightPanel from '@/features/RightPanel';
import { buildWorkspaceAwarePath } from '@/features/Workspace/workspaceAwarePath';
import type { RuleGroup, RuleItem, UpdateRuleInput } from '@/services/expertise';
import { expertiseActions } from '@/store/expertise';
import { densityVars } from '@/styles/densityVars';

import { DIRECTION_ICON } from './DirectionToggle';
import Field from './Field';
import { useRuleRevisions, useRuleSources } from './hooks';
import {
  appendException,
  mergedIntoId,
  revisionAuthorKey,
  ruleOrigin,
  sectionBody,
  useScopeLabel,
} from './labels';

const styles = createStaticStyles(({ css }) => ({
  body: css`
    overflow-y: auto;
    flex: 1;
    padding-block: 4px 24px;
    padding-inline: 28px;
  `,
  foot: css`
    flex: none;
    padding-block: ${densityVars.contentGap};
    padding-inline: 28px;
    border-block-start: 1px solid ${cssVar.colorBorderSecondary};
  `,
  head: css`
    flex: none;
    padding-block: 24px 0;
    padding-inline: 28px;
  `,
  heading: css`
    display: flex;
    align-items: center;
    justify-content: space-between;

    margin-block: 22px 6px;

    font-size: 12px;
    font-weight: 600;
    color: ${cssVar.colorTextTertiary};
    letter-spacing: 0.02em;
  `,
  kicker: css`
    font-family: ${cssVar.fontFamilyCode};
    font-size: 12px;
    color: ${cssVar.colorTextSecondary};
  `,
  link: css`
    cursor: pointer;

    display: inline-flex;
    gap: 4px;
    align-items: center;

    font-size: 12px;
    color: ${cssVar.colorTextSecondary};
    text-decoration: none;

    &:hover {
      color: ${cssVar.colorText};
    }
  `,
  muted: css`
    font-size: 12px;
    line-height: 1.6;
    color: ${cssVar.colorTextSecondary};
  `,
  properties: css`
    display: flex;
    flex-direction: column;
    gap: 2px;

    margin-block-start: 16px;
    padding-block-end: ${densityVars.cardPadding};
    border-block-end: 1px solid ${cssVar.colorBorderSecondary};
  `,
  property: css`
    width: 100%;
    min-height: 30px;
    padding-block: 5px;
    padding-inline: 8px 10px;
    border-radius: ${cssVar.borderRadius};

    font-size: 13px;
  `,
  propertyLabel: css`
    flex: none;
    width: 44px;
    font-size: 12px;
    color: ${cssVar.colorTextTertiary};
  `,
  propertyStatic: css`
    background: transparent;
  `,
  quote: css`
    margin-block: 6px 0;
    padding-block: 6px;
    padding-inline: ${densityVars.contentGap} 0;
    border-inline-start: 2px solid ${cssVar.colorBorder};

    font-size: 13px;
    line-height: 1.6;
    color: ${cssVar.colorTextSecondary};
  `,
  source: css`
    padding-block: 10px;
    border-block-end: 1px solid ${cssVar.colorBorderSecondary};

    &:last-child {
      border-block-end: none;
    }
  `,
  title: css`
    font-size: 20px;
    font-weight: 600;
    line-height: 1.35;
    text-wrap: pretty;
  `,
  titleInput: css`
    box-sizing: border-box;
    width: 100%;
    padding: 0;
    border: none;

    font-family: inherit;
    font-size: 20px;
    font-weight: 600;
    line-height: 1.35;
    color: inherit;

    background: transparent;
    outline: none;
  `,
  warning: css`
    padding-block: 6px;
    padding-inline: 8px;
    border-radius: ${cssVar.borderRadius};

    font-size: 12px;
    line-height: 1.6;
    color: ${cssVar.colorWarningText};

    background: ${cssVar.colorWarningBg};
  `,
}));

interface PropertyProps {
  children: ReactNode;
  icon: LucideIcon;
  label: string;
  menu?: DropdownItem[];
}

/**
 * One property line, in the shape the task detail uses: a label, an icon, the current value, and
 * a menu behind the click when it can be changed. Every line sits on the same plain ground; the
 * ones that open a menu only light up on hover, so the block does not read as a highlight.
 */
const Property = ({ children, icon, label, menu }: PropertyProps) => {
  const row = (
    <Block
      horizontal
      align={'center'}
      className={cx(styles.property, !menu && styles.propertyStatic)}
      clickable={Boolean(menu)}
      gap={8}
      variant={'borderless'}
    >
      <span className={styles.propertyLabel}>{label}</span>
      <Icon color={cssVar.colorTextSecondary} icon={icon} size={14} />
      <Flexbox flex={1} style={{ minWidth: 0 }}>
        {children}
      </Flexbox>
    </Block>
  );
  return menu ? <DropdownMenu items={menu}>{row}</DropdownMenu> : row;
};

interface RuleDocumentProps {
  code: string;
  group?: RuleGroup;
  groups: RuleGroup[];
  menu: DropdownItem[];
  onTitleEditing: (editing: boolean) => void;
  /** Resolves to whether the change was saved. */
  onUpdate: (patch: UpdateRuleInput) => Promise<boolean>;
  rule: RuleItem;
  titleEditing: boolean;
}

/**
 * The rule as a document: the sentence itself as the title (editable in place), its settings as
 * property lines, then the sections a lesson row carries, the evidence it grew from, and the
 * edits it has been through. The exception composer is docked at the foot so a long source list
 * never pushes it out of a short window.
 */
const RuleDocument = ({
  code,
  group,
  groups,
  menu,
  onTitleEditing,
  onUpdate,
  rule,
  titleEditing,
}: RuleDocumentProps) => {
  const { t } = useTranslation('memory');
  const scopeLabel = useScopeLabel();
  const navigate = useNavigate();
  const workspaceSlug = useActiveWorkspaceSlug();
  const {
    data: sources,
    error: sourcesError,
    isLoading: sourcesLoading,
    revalidate: retrySources,
  } = useRuleSources(rule.id);
  const { data: revisions } = useRuleRevisions(rule.id);
  const [exception, setException] = useState('');
  const [titleDraft, setTitleDraft] = useState(rule.title);
  const [busy, setBusy] = useState(false);

  useEffect(() => {
    setTitleDraft(rule.title);
  }, [rule.id, rule.title, titleEditing]);

  const archived = rule.status === 'retired';
  const authored = rule.authored;
  const all = groups.flatMap((g) => g.rules);
  const titleOf = (id: string) => all.find((r) => r.id === id)?.title ?? id;
  const mergedInto = mergedIntoId(rule);
  const origin = ruleOrigin(rule);
  // What the reviewer wrote into an agent's lessons was taught to that agent, not a rule of theirs.
  const taught = authored && group?.owner.kind === 'agent';

  const save = async (patch: UpdateRuleInput) => {
    if (busy) return false;
    setBusy(true);
    try {
      const saved = await onUpdate(patch);
      if (saved) await expertiseActions.refreshRuleRevisions(rule.id);
      return saved;
    } finally {
      setBusy(false);
    }
  };

  const commitTitle = async () => {
    const next = titleDraft.trim();
    if (!next || next === rule.title) return onTitleEditing(false);
    // Leave edit mode only once the rename is saved; on failure the draft stays to retry.
    if (await save({ title: next })) onTitleEditing(false);
  };

  const addException = () => {
    const text = exception.trim();
    if (!text) return;
    // Keep what they typed if the save did not land.
    void save({ sections: { limits: appendException(sectionBody(rule, 'limits'), text) } }).then(
      (saved) => saved && setException(''),
    );
  };

  const choose = <T extends string>(
    values: readonly T[],
    label: (value: T) => string,
    onPick: (value: T) => void,
  ): DropdownItem[] =>
    values.map((value) => ({ key: value, label: label(value), onClick: () => onPick(value) }));
  const editable = !archived && !busy;

  return (
    <Flexbox height={'100%'} width={'100%'}>
      <div className={styles.head}>
        <Flexbox horizontal align={'flex-start'} gap={8}>
          <div style={{ flex: 1, minWidth: 0 }}>
            <div className={styles.kicker}>
              {code} · {group?.domain.title}
              {rule.tags?.length ? ` · ${rule.tags.join(' / ')}` : ''}
            </div>
            {titleEditing ? (
              <input
                autoFocus
                className={styles.titleInput}
                value={titleDraft}
                onBlur={() => void commitTitle()}
                onChange={(e) => setTitleDraft(e.target.value)}
                onKeyDown={(e) => {
                  if (e.key === 'Enter') void commitTitle();
                  if (e.key === 'Escape') onTitleEditing(false);
                }}
              />
            ) : (
              <div className={styles.title}>{rule.title}</div>
            )}
          </div>
          {!archived && !titleEditing && (
            <ActionIcon
              icon={PencilIcon}
              size={'small'}
              title={t('rules.actions.edit')}
              onClick={() => onTitleEditing(true)}
            />
          )}
          <DropdownMenu items={menu}>
            <ActionIcon disabled={busy} icon={MoreHorizontalIcon} size={'small'} />
          </DropdownMenu>
        </Flexbox>

        <div className={styles.properties}>
          <Property
            icon={rule.enforcement === 'block' ? ShieldCheckIcon : BellIcon}
            label={t('rules.meta.enforcement')}
            menu={
              editable
                ? choose(
                    ['block', 'remind'] as const,
                    (value) =>
                      t(
                        value === 'block'
                          ? 'rules.compose.enforcementBlock'
                          : 'rules.compose.enforcementRemind',
                      ),
                    (enforcement: ExpertiseEnforcement) => void save({ enforcement }),
                  )
                : undefined
            }
          >
            <Text weight={500}>
              {t(
                rule.enforcement === 'block'
                  ? 'rules.enforcement.block'
                  : 'rules.enforcement.remind',
              )}
            </Text>
            <span className={styles.muted}>
              {t(
                rule.enforcement === 'block'
                  ? 'rules.enforcement.blockDesc'
                  : 'rules.enforcement.remindDesc',
              )}
            </span>
          </Property>
          <Property
            icon={DIRECTION_ICON[rule.direction ?? 'unset']}
            label={t('rules.meta.direction')}
            menu={
              editable
                ? choose(
                    EXPERTISE_RULE_DIRECTIONS,
                    (value) => t(`rules.direction.${value}`),
                    (direction) => void save({ direction }),
                  )
                : undefined
            }
          >
            <Text weight={500}>{t(`rules.direction.${rule.direction ?? 'unset'}`)}</Text>
            <span className={styles.muted}>
              {t(`rules.direction.${rule.direction ?? 'unset'}Desc`)}
            </span>
          </Property>
          <Property
            icon={ClipboardCheckIcon}
            label={t('rules.meta.method')}
            menu={
              // `compiled` is set by the compiler once a criterion is linked; the reviewer can only
              // say whether a program could check it.
              editable && rule.compilability !== 'compiled'
                ? choose(
                    ['compilable', 'not-compilable'] as const,
                    (value) => t(`rules.method.${value}`),
                    (compilability) => void save({ compilability }),
                  )
                : undefined
            }
          >
            <Text weight={500}>{t(`rules.method.${rule.compilability}`)}</Text>
          </Property>
          <Property
            icon={ScaleIcon}
            label={t('rules.meta.reason')}
            menu={
              editable
                ? choose(
                    ['mechanism', 'taste'] as const,
                    (value) => t(`rules.reason.${value}`),
                    (reasonKind: ExpertiseReasonKind) => void save({ reasonKind }),
                  )
                : undefined
            }
          >
            {/* Conversation learning never classifies its reason; saying "taste, you said it"
                for those would claim something nobody stated. */}
            <Text weight={500}>{t(`rules.reason.${rule.reasonKind ?? 'unset'}`)}</Text>
            {rule.reasonSource && (
              <span className={styles.muted}>{t(`rules.reason.${rule.reasonSource}`)}</span>
            )}
          </Property>
          {rule.enforcement === 'block' && rule.reasonKind === 'taste' && (
            <div className={styles.warning}>{t('rules.enforcement.tasteWarning')}</div>
          )}
          <Property icon={TargetIcon} label={t('rules.meta.scope')}>
            <span>{group ? scopeLabel(group.scopes) : ''}</span>
          </Property>
          <Property icon={ActivityIcon} label={t('rules.meta.runs')}>
            <span>
              {rule.hitRunCount
                ? t('rules.runs.detail', { hits: rule.hitCount, runs: rule.hitRunCount })
                : t('rules.runs.none')}
              {rule.falsePositiveCount > 0 && (
                <span className={styles.muted}>
                  {' · '}
                  {t('rules.runs.overruled', { count: rule.falsePositiveCount })}
                </span>
              )}
            </span>
          </Property>
          <Property icon={HistoryIcon} label={t('rules.meta.origin')}>
            <span>
              {taught
                ? t('rules.origin.taught')
                : t(`rules.origin.${origin.key}`, 'params' in origin ? origin.params : undefined)}
              {!authored && rule.lastHitAt
                ? t('rules.origin.lastHit', { time: dayjs(rule.lastHitAt).fromNow() })
                : ''}
            </span>
            {Boolean(rule.generalizedFromIds?.length) && (
              <span className={styles.muted}>
                {t('rules.origin.generalizedFrom', {
                  titles: rule.generalizedFromIds!.map(titleOf).join('」「'),
                })}
              </span>
            )}
            {rule.specificity === 'over-specific' && (
              <span className={styles.muted}>{t('rules.origin.overSpecific')}</span>
            )}
            {rule.specificity === 'one-off' && (
              <span className={styles.muted}>{t('rules.origin.oneOff')}</span>
            )}
            {archived && (
              <span className={styles.muted}>
                {t('rules.archived.at', {
                  time: dayjs(rule.retiredAt ?? undefined).format('YYYY-MM-DD'),
                })}
                {' · '}
                {mergedInto
                  ? t('rules.archived.mergedInto', { title: titleOf(mergedInto) })
                  : t('rules.archived.byYou')}
              </span>
            )}
          </Property>
        </div>
      </div>

      <div className={styles.body}>
        <Field
          editable={!archived}
          label={t('rules.field.why')}
          placeholder={t('rules.field.whyEmpty')}
          value={sectionBody(rule, 'why')}
          onSave={(why) => save({ sections: { why: why || null } })}
        />
        <Field
          editable={!archived}
          label={t('rules.field.how')}
          placeholder={t('rules.field.howEmpty')}
          value={sectionBody(rule, 'how')}
          onSave={(how) => save({ sections: { how: how || null } })}
        />
        <Field
          editable={!archived}
          label={t('rules.field.limits')}
          placeholder={t('rules.field.limitsEmpty')}
          value={sectionBody(rule, 'limits')}
          onSave={(next) => save({ sections: { limits: next || null } })}
        />
        <Field
          muted
          label={`${t('rules.field.check')} · ${t(`rules.method.${rule.compilability}`)}`}
          placeholder={t('rules.field.checkPending')}
        />

        <div className={styles.heading}>
          <span>{t('rules.sources.title')}</span>
        </div>
        {/* "No sources" is only said once the server has said the list is empty — a restored
            empty copy is still loading; loading and a failed request each say what they are, so
            nothing reads as lost evidence. */}
        {sourcesLoading && !sources?.length ? (
          <SkeletonText rows={2} />
        ) : sourcesError && !sources?.length ? (
          <Flexbox horizontal align={'center'} gap={8}>
            <span className={styles.muted}>{t('rules.sources.loadFailed')}</span>
            <Button size={'small'} type={'link'} onClick={() => void retrySources()}>
              {t('rules.sources.retry')}
            </Button>
          </Flexbox>
        ) : sources?.length ? (
          sources.map((source) => (
            <div className={styles.source} key={source.id}>
              <Flexbox horizontal align={'center'} gap={8} justify={'space-between'}>
                <Flexbox horizontal align={'center'} gap={6} style={{ minWidth: 0 }}>
                  <Tag size={'small'}>
                    {t(
                      source.fromAcceptance
                        ? 'rules.sources.acceptance'
                        : source.materialType
                          ? `rules.sources.material.${source.materialType as 'document' | 'file' | 'text' | 'topic'}`
                          : 'rules.sources.conversation',
                    )}
                  </Tag>
                  <span style={{ fontSize: 13, fontWeight: 500 }}>
                    {source.checkTitle ?? source.where ?? ''}
                  </span>
                </Flexbox>
                {!source.fromAcceptance && source.topicId && source.topicAgentId && (
                  <a
                    className={styles.link}
                    href={buildWorkspaceAwarePath(
                      `/agent/${source.topicAgentId}/${source.topicId}${
                        source.messageId ? `?locate=${source.messageId}` : ''
                      }`,
                      workspaceSlug,
                    )}
                    onClick={(e) => {
                      e.preventDefault();
                      navigate(e.currentTarget.getAttribute('href')!);
                    }}
                  >
                    <Icon icon={MessageSquareTextIcon} size={12} />
                    {t(source.messageId ? 'rules.sources.openMessage' : 'rules.sources.openTopic')}
                  </a>
                )}
                {/* A document or file it was read from opens where the reviewer keeps it. */}
                {source.documentId && (
                  <a
                    className={styles.link}
                    href={buildWorkspaceAwarePath(
                      source.documentFileId
                        ? docsFilePath(source.documentFileId)
                        : docsPagePath(source.documentId),
                      workspaceSlug,
                    )}
                    onClick={(e) => {
                      e.preventDefault();
                      navigate(e.currentTarget.getAttribute('href')!);
                    }}
                  >
                    <Icon icon={FileTextIcon} size={12} />
                    {t('rules.sources.openMaterial')}
                  </a>
                )}
                {source.acceptanceId && (
                  <a
                    className={styles.link}
                    href={`/acceptance/${source.acceptanceId}`}
                    rel={'noreferrer'}
                    target={'_blank'}
                  >
                    <Icon icon={ClipboardCheckIcon} size={12} />
                    {t('rules.sources.open')}
                  </a>
                )}
              </Flexbox>
              {(source.reviewerComment || source.example) && (
                <p className={styles.quote}>{source.reviewerComment || source.example}</p>
              )}
              <div className={styles.muted}>
                {dayjs(source.createdAt).format('YYYY-MM-DD')}
                {source.roundIndex
                  ? ` · ${t('rules.sources.round', { index: source.roundIndex })}`
                  : ''}
                {source.severity ? ` · ${t(`rules.severity.${source.severity}`)}` : ''}
                {source.userDecision === 'agree' ? ` · ${t('rules.sources.agreed')}` : ''}
                {source.userDecision === 'reject' ? ` · ${t('rules.sources.rejected')}` : ''}
              </div>
            </div>
          ))
        ) : (
          <p className={styles.muted}>
            {t(
              taught
                ? 'rules.sources.taughtEmpty'
                : authored
                  ? 'rules.sources.authoredEmpty'
                  : 'rules.sources.empty',
            )}
          </p>
        )}

        {Boolean(revisions?.length) && (
          <>
            <div className={styles.heading}>
              <span>{t('rules.revisions.title')}</span>
            </div>
            {revisions!.map((revision) => (
              <div key={revision.id} style={{ marginBottom: 10 }}>
                <Text fontSize={13}>
                  {revision.kind === 'generalize'
                    ? t('rules.revisions.generalize', { title: revision.feedback ?? '' })
                    : revision.feedback}
                </Text>
                {revision.prevTitle && (
                  <div className={styles.muted}>
                    {t('rules.revisions.prevTitle', { title: revision.prevTitle })}
                  </div>
                )}
                <div className={styles.muted}>
                  {t(revisionAuthorKey(revision))}
                  {' · '}
                  {dayjs(revision.createdAt).format('YYYY-MM-DD')}
                </div>
              </div>
            ))}
          </>
        )}
      </div>

      {!archived && (
        <Flexbox horizontal align={'flex-end'} className={styles.foot} gap={8}>
          <TextArea
            autoSize={{ maxRows: 4, minRows: 1 }}
            disabled={busy}
            placeholder={t('rules.exception.placeholder')}
            style={{ flex: 1 }}
            value={exception}
            onChange={(e) => setException(e.target.value)}
            onKeyDown={(e) => {
              if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') addException();
            }}
          />
          <Button
            disabled={!exception.trim()}
            loading={busy}
            type={'primary'}
            onClick={addException}
          >
            {t('rules.exception.submit')}
          </Button>
        </Flexbox>
      )}
    </Flexbox>
  );
};

interface DetailPanelProps extends Omit<RuleDocumentProps, 'rule' | 'code'> {
  code?: string;
  onClose: () => void;
  rule?: RuleItem;
}

/**
 * The same draggable right panel the sibling memory surfaces use. Its visibility follows the
 * selection: until a rule is picked there is nothing to show, and an empty column is not worth
 * the width.
 */
const DetailPanel = ({ code, onClose, rule, ...rest }: DetailPanelProps) => (
  <RightPanel
    expandFollowsSelection
    defaultWidth={440}
    expand={Boolean(rule)}
    maxWidth={760}
    minWidth={380}
    onExpandChange={(next) => !next && onClose()}
  >
    {/* Keyed by rule: every draft in the document belongs to one rule and must not carry over. */}
    {rule ? <RuleDocument code={code ?? ''} key={rule.id} rule={rule} {...rest} /> : null}
  </RightPanel>
);

export default DetailPanel;