use-create-agent.ts


Overview

The use-create-agent.ts file defines a custom React hook named useCreateAgentOrPipeline. This hook encapsulates the logic and state management necessary for creating an "Agent" or a "Pipeline" entity within the application. Primarily, it provides a clean interface to initiate the creation process, manage loading and modal visibility states, and handle form submissions related to agent or pipeline creation.

The hook leverages other hooks and utilities such as useSetAgent for agent API interactions and useSetModalState for modal visibility control, ensuring separation of concerns and reusability.


Detailed Explanation

useCreateAgentOrPipeline Hook

Purpose

To provide UI components with the necessary state and functions to create agents or pipelines, control modal visibility, and track loading states during these operations.

Returns

An object containing:

Internal Functions

createAgent(name: string): Promise<{code: number; [key: string]: any}>
handleCreateAgentOrPipeline(data: FormSchemaType): Promise<void>

Important Implementation Details


Interaction with Other Parts of the System

This hook is typically used in UI components responsible for rendering the creation modal and handling user input for creating agents or pipelines. It abstracts the asynchronous creation logic and UI state management, allowing components to remain focused on presentation.


Example Usage in a React Component

import React from 'react';
import { useCreateAgentOrPipeline } from './use-create-agent';
import { FlowType } from '../constant';

function CreateAgentModal() {
  const {
    loading,
    creatingVisible,
    showCreatingModal,
    hideCreatingModal,
    handleCreateAgentOrPipeline,
  } = useCreateAgentOrPipeline();

  const onSubmit = async (formData) => {
    await handleCreateAgentOrPipeline(formData);
  };

  return (
    <>
      <button onClick={showCreatingModal}>Create Agent</button>
      {creatingVisible && (
        <Modal onClose={hideCreatingModal}>
          <AgentForm 
            loading={loading}
            onSubmit={onSubmit}
            defaultType={FlowType.Agent}
          />
        </Modal>
      )}
    </>
  );
}

Mermaid Diagram

flowchart TD
    A[useCreateAgentOrPipeline Hook]
    A --> B[useSetAgent Hook]
    A --> C[useSetModalState Hook]
    A --> D[createAgent(name)]
    A --> E[handleCreateAgentOrPipeline(data)]

    B --> F[setAgent({title, dsl})]
    C --> G[showModal()]
    C --> H[hideModal()]

    E --> |if data.type == FlowType.Agent| D
    D --> |returns promise with code| E
    E --> |on success (code===0)| H

Summary

The use-create-agent.ts file is a utility hook that centralizes the creation logic for agents and pipelines. It cleanly encapsulates API calls, loading states, and modal visibility, making it easier for UI components to manage creation workflows with minimal boilerplate. While currently only supporting agent creation, the structure allows extension for pipeline creation in the future. It interacts with other hooks and interface definitions to maintain strong typing and modularity within the application.