use-agent-request.ts


Overview

The use-agent-request.ts file provides a collection of React custom hooks for managing agent-related data and operations within the application. It primarily facilitates interaction with the backend agent service APIs, handling fetching, updating, deleting, and other agent lifecycle operations.

These hooks leverage the React Query library (@tanstack/react-query) for data fetching and caching, providing reactive state updates, caching, background refetching, and mutation capabilities.

This file abstracts the complexity of API communication and state management, exposing easy-to-use hooks for components to interact with agent data such as agent lists, templates, settings, canvas files, logs, versioning, and more.


Detailed Explanation of Exports

Constants and Types

enum AgentApiAction

Defines string constants representing the various agent API actions supported. These keys are used as query or mutation keys in React Query to uniquely identify requests and cache entries.

Example values:


EmptyDsl

A constant representing the default empty DSL (Domain Specific Language) structure for a new agent flow. It contains:

This object is useful for initializing a new agent's DSL when creating or resetting agents.


Hooks for Fetching Data

useFetchAgentTemplates(): IFlowTemplate[]

Fetches a list of agent flow templates.

Usage Example:

const templates = useFetchAgentTemplates();
console.log(templates); // array of flow templates

useFetchAgentListByPage()

Fetches paginated list of agent canvases filtered by a search string.

Usage Example:

const {
  data: agents,
  loading,
  searchString,
  handleInputChange,
  pagination,
  setPagination,
} = useFetchAgentListByPage();

useFetchAgent(): { data: IFlow; loading: boolean; refetch: () => void }

Fetch detailed data for a single agent by ID or shared ID.


useFetchAgentAvatar(): { data: IFlow; loading: boolean; refetch: () => void }

Fetches avatar information for an agent identified by shared ID.


useFetchAgentLog(searchParams: IAgentLogsRequest)

Fetches logs for a given agent ID with filtering options.


useFetchExternalAgentInputs()

Fetches inputs for an external agent identified by shared ID.


useFetchInputForm(componentId?: string)

Fetches input form schema for a specific component within an agent.


useFetchVersionList()

Fetches a list of versions for the current agent.


useFetchVersion(version_id?: string): { data?: IFlow; loading: boolean }

Fetches a specific version of an agent by version ID.


useFetchPrompt()

Fetches prompt strings related to agents.


Hooks for Mutations (Create, Update, Delete)

useUpdateAgentSetting()

Updates settings for an agent canvas.


useDeleteAgent()

Deletes one or more agent canvases by their IDs.


useResetAgent()

Resets the current agent's canvas.


useSetAgent(showMessage = true)

Creates or updates an agent canvas.


useUploadCanvasFile()

Uploads one or more files to the current agent's canvas.


useUploadCanvasFileWithProgress(identifier?: Nullable<string>)

Uploads files to an agent canvas with progress callbacks.


useTestDbConnect()

Tests database connection parameters.


useDebugSingle()

Debugs a single agent request with provided input data.


useSetAgentSetting()

Updates settings of the current agent canvas.


Hooks for Tracing and Logs

useFetchMessageTrace(isStopFetchTrace: boolean, canvasId?: string)

Fetches trace data for a specific message in an agent canvas.


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
  A[useFetchAgentTemplates]
  B[useFetchAgentListByPage]
  C[useFetchAgent]
  D[useUpdateAgentSetting]
  E[useDeleteAgent]
  F[useResetAgent]
  G[useSetAgent]
  H[useUploadCanvasFile]
  I[useUploadCanvasFileWithProgress]
  J[useFetchMessageTrace]
  K[useTestDbConnect]
  L[useDebugSingle]
  M[useFetchInputForm]
  N[useFetchVersionList]
  O[useFetchVersion]
  P[useFetchAgentAvatar]
  Q[useFetchAgentLog]
  R[useFetchExternalAgentInputs]
  S[useSetAgentSetting]
  T[useFetchPrompt]

  A --> agentService.listTemplates
  B --> agentService.listCanvasTeam
  C --> agentService.fetchCanvas
  D --> agentService.settingCanvas
  E --> agentService.removeCanvas
  F --> agentService.resetCanvas
  G --> agentService.setCanvas
  H --> agentService.uploadCanvasFile
  I --> agentService.uploadCanvasFile
  J --> fetchTrace
  K --> agentService.testDbConnect
  L --> agentService.debugSingle
  M --> agentService.inputForm
  N --> agentService.fetchVersionList
  O --> agentService.fetchVersion
  P --> agentService.fetchAgentAvatar
  Q --> fetchAgentLogsByCanvasId
  R --> agentService.fetchExternalAgentInputs
  S --> agentService.settingCanvas
  T --> agentService.fetchPrompt

  style A fill:#f9f,stroke:#333,stroke-width:1px
  style B fill:#ccf,stroke:#333,stroke-width:1px
  style C fill:#cfc,stroke:#333,stroke-width:1px
  style D fill:#fcf,stroke:#333,stroke-width:1px
  style E fill:#fcc,stroke:#333,stroke-width:1px
  style F fill:#cff,stroke:#333,stroke-width:1px
  style G fill:#ffc,stroke:#333,stroke-width:1px
  style H fill:#fc9,stroke:#333,stroke-width:1px
  style I fill:#9cf,stroke:#333,stroke-width:1px
  style J fill:#99f,stroke:#333,stroke-width:1px
  style K fill:#f99,stroke:#333,stroke-width:1px
  style L fill:#f6c,stroke:#333,stroke-width:1px
  style M fill:#6f9,stroke:#333,stroke-width:1px
  style N fill:#6cf,stroke:#333,stroke-width:1px
  style O fill:#f96,stroke:#333,stroke-width:1px
  style P fill:#9f6,stroke:#333,stroke-width:1px
  style Q fill:#f69,stroke:#333,stroke-width:1px
  style R fill:#69f,stroke:#333,stroke-width:1px
  style S fill:#fc6,stroke:#333,stroke-width:1px
  style T fill:#6ff,stroke:#333,stroke-width:1px

Summary

The use-agent-request.ts file is a vital layer in the application that abstracts agent API interactions into reusable React hooks. It manages agent data fetching, creation, updates, deletions, file uploads, tracing, debugging, and versioning — all integrated with React Query for effective state management and caching.

These hooks enable components to easily work with agent-related data through a clean and consistent interface, promoting maintainability and scalability of the agent management features in the application.