llm-hooks.tsx

Overview

The llm-hooks.tsx file provides a collection of React custom hooks and utility functions designed to interact with Large Language Model (LLM) related data in the application. It primarily manages fetching, manipulating, and presenting LLM model lists, factories, and user-specific LLM configurations through asynchronous queries and mutations using React Query (@tanstack/react-query). The hooks facilitate seamless integration with the backend services (via userService) to enable CRUD operations, selection options preparation for UI components, and state synchronization.

This file acts as a bridge between the UI components and the backend API endpoints related to LLMs, enabling components to easily consume and manipulate LLM data through simple hooks encapsulating complex logic and side effects. It also includes utility functions to format LLM options with icons for UI select components.


Detailed Explanation of Exports

1. useFetchLlmList(modelType?: LlmModelType): IThirdAiModelCollection


2. useSelectLlmOptions()


3. useSelectLlmOptionsByModelType()


4. useComposeLlmOptionsByModelTypes(modelTypes: LlmModelType[])


5. useFetchLlmFactoryList(): ResponseGetType<IFactory[]>


6. useFetchMyLlmList(): ResponseGetType<Record<string, IMyLlmValue>>


7. useFetchMyLlmListDetailed(): ResponseGetType<Record<string, any>>


8. useSelectLlmList()


9. Mutation Hooks for CRUD Operations on LLM and Factory

All mutation hooks use React Query's useMutation and userService API calls. They handle success messaging and query invalidations to refresh cached data.

a) useSaveApiKey()

b) useSaveTenantInfo()

c) useAddLlm()

d) useDeleteLlm()

e) useDeleteFactory()


Important Implementation Details


Interaction with Other System Components


Visual Diagram: Component Diagram of Hook Functions and Data Flow

flowchart TD
    subgraph Fetchers
        direction TB
        useFetchLlmList --> userService.llm_list
        useFetchLlmFactoryList --> userService.factories_list
        useFetchMyLlmList --> userService.my_llm
        useFetchMyLlmListDetailed --> userService.my_llm
    end

    subgraph Selectors
        useSelectLlmOptions --> useFetchLlmList
        useSelectLlmOptionsByModelType --> useFetchLlmList
        useComposeLlmOptionsByModelTypes --> useSelectLlmOptionsByModelType
        useSelectLlmList --> useFetchMyLlmList
        useSelectLlmList --> useFetchLlmFactoryList
    end

    subgraph Mutations
        useSaveApiKey --> userService.set_api_key
        useSaveTenantInfo --> userService.set_tenant_info
        useAddLlm --> userService.add_llm
        useDeleteLlm --> userService.delete_llm
        useDeleteFactory --> userService.deleteFactory
    end

    userService.llm_list -->|returns| useFetchLlmList
    userService.factories_list -->|returns| useFetchLlmFactoryList
    userService.my_llm -->|returns| useFetchMyLlmList
    userService.my_llm -->|returns| useFetchMyLlmListDetailed

    useFetchLlmList --> useSelectLlmOptions
    useFetchLlmList --> useSelectLlmOptionsByModelType
    useSelectLlmOptionsByModelType --> useComposeLlmOptionsByModelTypes
    useFetchMyLlmList --> useSelectLlmList
    useFetchLlmFactoryList --> useSelectLlmList

    Mutations -.->|invalidate queries| useFetchMyLlmList
    Mutations -.->|invalidate queries| useFetchMyLlmListDetailed
    Mutations -.->|invalidate queries| useFetchLlmFactoryList

Summary

This modular design promotes maintainability, testability, and scalability of the LLM management features within the system.