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.

2.9 KB
Raw Blame
'use client';

import type { BuiltinInspectorProps } from '@lobechat/types';
import { Text } from '@lobehub/ui';
import { cssVar, cx } from '@lobehub/ui';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';

import { highlightTextStyles, inspectorTextStyles, shinyTextStyles } from '@/styles';

import type { SearchKnowledgeBaseArgs, SearchKnowledgeBaseState } from '../../..';

export const SearchKnowledgeBaseInspector = memo<
  BuiltinInspectorProps<SearchKnowledgeBaseArgs, SearchKnowledgeBaseState>
>(({ args, partialArgs, isArgumentsStreaming, isLoading, pluginState }) => {
  const { t } = useTranslation('plugin');

  const query = args?.query || partialArgs?.query || '';
  // Use fileResults length for display (aggregated by file)
  // The read path drops the hit lists and pins the size as `resultCount`; the
  // array is only here for a payload stored before that.
  const resultCount = pluginState?.resultCount ?? pluginState?.fileResults?.length ?? 0;
  const hasSettled = !!pluginState?.fileResults || typeof pluginState?.resultCount === 'number';
  const hasResults = resultCount > 0;
  // No library was in scope, so nothing was searched — not the same as 0 hits.
  const isUnscoped = pluginState?.scope === 'none';

  // During argument streaming
  if (isArgumentsStreaming) {
    if (!query)
      return (
        <div className={inspectorTextStyles.root}>
          <span className={shinyTextStyles.shinyText}>
            {t('builtins.lobe-knowledge-base.apiName.searchKnowledgeBase')}
          </span>
        </div>
      );

    return (
      <div className={inspectorTextStyles.root}>
        <span className={shinyTextStyles.shinyText}>
          {t('builtins.lobe-knowledge-base.apiName.searchKnowledgeBase')}:{' '}
        </span>
        <span className={highlightTextStyles.gold}>{query}</span>
      </div>
    );
  }

  return (
    <div className={inspectorTextStyles.root}>
      <span style={{ marginInlineStart: 2 }}>
        <span className={cx(isLoading && shinyTextStyles.shinyText)}>
          {t('builtins.lobe-knowledge-base.apiName.searchKnowledgeBase')}:{' '}
        </span>
        {query && <span className={highlightTextStyles.gold}>{query}</span>}
        {!isLoading &&
          hasSettled &&
          (hasResults ? (
            <span style={{ marginInlineStart: 4 }}>({resultCount})</span>
          ) : (
            <Text
              as={'span'}
              color={cssVar.colorTextDescription}
              fontSize={12}
              style={{ marginInlineStart: 4 }}
            >
              (
              {t(
                isUnscoped
                  ? 'builtins.lobe-knowledge-base.inspector.noScope'
                  : 'builtins.lobe-knowledge-base.inspector.noResults',
              )}
              )
            </Text>
          ))}
      </span>
    </div>
  );
});

SearchKnowledgeBaseInspector.displayName = 'SearchKnowledgeBaseInspector';