use-knowledge-request.ts


Overview

The use-knowledge-request.ts file provides a collection of React hooks designed to interact with a knowledge base backend service. These hooks encapsulate various CRUD (Create, Read, Update, Delete) operations and data retrieval patterns related to knowledge bases, including fetching knowledge lists, creating or deleting knowledge entries, updating configurations, running retrieval tests, and fetching metadata or knowledge graphs.

The file integrates with the backend through a kbService API layer, leverages the powerful react-query library for asynchronous data fetching and caching, and uses React state and effects to manage UI interactions such as pagination, filtering, and search debouncing.

This file serves as a core utility within the application for managing knowledge base data, enabling components to easily consume and mutate knowledge-related data with automatic UI updates and caching.


Exports and Their Descriptions

Enumerations

KnowledgeApiAction

export const enum KnowledgeApiAction {
  TestRetrieval = 'testRetrieval',
  FetchKnowledgeListByPage = 'fetchKnowledgeListByPage',
  CreateKnowledge = 'createKnowledge',
  DeleteKnowledge = 'deleteKnowledge',
  SaveKnowledge = 'saveKnowledge',
  FetchKnowledgeDetail = 'fetchKnowledgeDetail',
  FetchKnowledgeGraph = 'fetchKnowledgeGraph',
  FetchMetadata = 'fetchMetadata',
}

Hooks


useKnowledgeBaseId

export const useKnowledgeBaseId = (): string;

useTestRetrieval

export const useTestRetrieval = () => {
  // returns:
  // - data: INextTestingResult
  // - loading: boolean
  // - setValues: React.Dispatch<ITestRetrievalRequestBody>
  // - refetch: () => void
  // - onPaginationChange: (page: number, pageSize: number) => void
  // - page: number
  // - pageSize: number
  // - handleFilterSubmit: () => void
  // - filterValue: any
}

useFetchNextKnowledgeListByPage

export const useFetchNextKnowledgeListByPage = () => {
  // returns:
  // - kbs: IKnowledge[]
  // - total: number
  // - searchString: string
  // - handleInputChange: React.ChangeEventHandler<HTMLInputElement>
  // - pagination: { current: number; pageSize: number; total: number }
  // - setPagination: React.Dispatch<...>
  // - loading: boolean
  // - filterValue: any
  // - handleFilterSubmit: () => void
}

useCreateKnowledge

export const useCreateKnowledge = () => {
  // returns:
  // - data: any
  // - loading: boolean
  // - createKnowledge: (params: { id?: string; name: string }) => Promise<any>
}

useDeleteKnowledge

export const useDeleteKnowledge = () => {
  // returns:
  // - data: any
  // - loading: boolean
  // - deleteKnowledge: (id: string) => Promise<any>
}

useUpdateKnowledge

export const useUpdateKnowledge = (shouldFetchList = false) => {
  // returns:
  // - data: any
  // - loading: boolean
  // - saveKnowledgeConfiguration: (params: Record<string, any>) => Promise<any>
}

useFetchKnowledgeBaseConfiguration

export const useFetchKnowledgeBaseConfiguration = (refreshCount?: number) => {
  // returns:
  // - data: IKnowledge
  // - loading: boolean
}

useFetchKnowledgeGraph

export function useFetchKnowledgeGraph() {
  // returns:
  // - data: IKnowledgeGraph
  // - loading: boolean
}

useFetchKnowledgeMetadata

export function useFetchKnowledgeMetadata(kbIds: string[] = []) {
  // returns:
  // - data: Record<string, Record<string, string[]>>
  // - loading: boolean
}

Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class useKnowledgeBaseId {
        +() : string
    }

    class useTestRetrieval {
        - knowledgeBaseId: string
        - values: ITestRetrievalRequestBody | undefined
        - page: number
        - pageSize: number
        +() : {
            data: INextTestingResult,
            loading: boolean,
            setValues: React.Dispatch,
            refetch: () => void,
            onPaginationChange: (number, number) => void,
            page: number,
            pageSize: number,
            handleFilterSubmit: () => void,
            filterValue: any
          }
    }

    class useFetchNextKnowledgeListByPage {
        - searchString: string
        - pagination: { current: number; pageSize: number }
        +() : {
            kbs: IKnowledge[],
            total: number,
            searchString: string,
            handleInputChange: React.ChangeEventHandler,
            pagination: object,
            setPagination: React.Dispatch,
            loading: boolean,
            filterValue: any,
            handleFilterSubmit: () => void
          }
    }

    class useCreateKnowledge {
        +() : {
            data: any,
            loading: boolean,
            createKnowledge: (params) => Promise<any>
          }
    }

    class useDeleteKnowledge {
        +() : {
            data: any,
            loading: boolean,
            deleteKnowledge: (id: string) => Promise<any>
          }
    }

    class useUpdateKnowledge {
        + (shouldFetchList?: boolean) : {
            data: any,
            loading: boolean,
            saveKnowledgeConfiguration: (params) => Promise<any>
          }
    }

    class useFetchKnowledgeBaseConfiguration {
        + (refreshCount?: number) : {
            data: IKnowledge,
            loading: boolean
          }
    }

    class useFetchKnowledgeGraph {
        +() : {
            data: IKnowledgeGraph,
            loading: boolean
          }
    }

    class useFetchKnowledgeMetadata {
        + (kbIds: string[]) : {
            data: Record<string, Record<string, string[]>>,
            loading: boolean
          }
    }

    useTestRetrieval ..> useKnowledgeBaseId : uses
    useFetchKnowledgeGraph ..> useKnowledgeBaseId : uses

Summary

The use-knowledge-request.ts file is a centralized module of React hooks that abstract and standardize interactions with knowledge base APIs. It promotes consistent data handling, caching, and UI synchronization while providing convenient interfaces for components to perform knowledge base operations such as listing, creating, updating, deleting, and testing retrieval functionalities.

This modular approach improves maintainability, testability, and scalability of the knowledge base features in the application.