knowledge-base-item.tsx


Overview

The knowledge-base-item.tsx file defines React components that provide user interface elements for selecting knowledge bases within a chat or agent configuration system. It primarily offers two components:

These components facilitate fetching, filtering, and displaying knowledge base options, supporting multi-selection with validation and customizable UI elements. The file also integrates internationalization (i18n) and hooks for fetching knowledge base data and building query variable options.


Exports

KnowledgeBaseItem

Description

A React functional component that renders a labeled multi-select dropdown for selecting knowledge bases. It fetches the knowledge base list, filters out those with a specific parser type (Tag), and displays the remaining bases with avatars.

Props

Name

Type

Default

Description

label

string

t('knowledgeBases')

Label for the form item.

tooltipText

string

t('knowledgeBasesTip')

Tooltip text shown on hover for the label.

name

string

'kb_ids'

The form field name.

required

boolean

true

Whether the selection is required.

onChange

() => void

Callback function when selection changes.

Usage Example

<KnowledgeBaseItem 
  label="Select Knowledge Bases"
  tooltipText="Choose relevant knowledge bases"
  name="kb_ids"
  required={true}
  onChange={() => console.log('Knowledge base selection changed')}
/>

Implementation Details


KnowledgeBaseFormField

Description

A more sophisticated form field component designed to be used within a react-hook-form context. It supports an optional showVariable flag, which when enabled, adds additional query variable options grouped alongside the knowledge bases.

Props

Name

Type

Default

Description

showVariable

boolean

false

Flag to enable display of query variable options

Usage Example

import { useForm, FormProvider } from 'react-hook-form';

const MyForm = () => {
  const methods = useForm();

  return (
    <FormProvider {...methods}>
      <form>
        <KnowledgeBaseFormField showVariable={true} />
        {/* other form fields */}
      </form>
    </FormProvider>
  );
};

Implementation Details


Internal Helper Function: buildQueryVariableOptionsByShowVariable

function buildQueryVariableOptionsByShowVariable(showVariable?: boolean)

Important Implementation Details and Algorithms


Interaction with Other Parts of the System

These components are likely part of a larger chat or AI agent configuration interface where users need to select relevant knowledge bases for queries or agent behavior.


Visual Diagram

classDiagram
    class KnowledgeBaseItem {
      +label?: string
      +tooltipText?: string
      +name?: string
      +required: boolean
      +onChange?(): void
      +render()
    }

    class KnowledgeBaseFormField {
      +showVariable: boolean
      +render()
    }

    KnowledgeBaseFormField ..> useFormContext : uses
    KnowledgeBaseFormField ..> useFetchKnowledgeList : uses
    KnowledgeBaseFormField ..> useBuildQueryVariableOptions : conditionally uses
    KnowledgeBaseFormField ..> useMemo : uses
    KnowledgeBaseItem ..> useFetchKnowledgeList : uses
    KnowledgeBaseItem ..> useTranslate : uses
    KnowledgeBaseFormField ..> useTranslation : uses

    KnowledgeBaseFormField o-- MultiSelect : renders
    KnowledgeBaseFormField o-- RAGFlowAvatar : renders avatars
    KnowledgeBaseItem o-- Select : renders
    KnowledgeBaseItem o-- AntAvatar : renders avatars

Summary

The knowledge-base-item.tsx file provides form components for selecting knowledge bases within a chat or AI agent UI. It supports fetching and filtering knowledge bases, displaying them with avatars, and optionally augmenting the selection with additional variable options. The components integrate tightly with form management and internationalization systems, offering both simple and advanced multi-select UI elements. This file is reusable and modular, facilitating consistent knowledge base selection across the application.