use-chat-logic.ts


Overview

use-chat-logic.ts defines a custom React hook, useAwaitCompentData, that encapsulates the logic for managing and processing chat messages that require user inputs before proceeding. This hook primarily supports chat components or interfaces where the chat assistant sends a message containing form-like inputs, waits for user responses, and then processes those responses to continue the conversation.

The hook manages the extraction and transformation of input data from chat messages, provides handlers to process user submissions, and determines whether the chat is currently waiting for user input based on the latest messages.

This file is part of a chat system that integrates with UI components rendering chat messages, forms, or input interfaces, and it interacts with data structures representing chat messages and queries.


Detailed Explanation

Types and Imports


Hook: useAwaitCompentData

const useAwaitCompentData = (props: IAwaitCompentData) => { ... }

This hook returns utility functions and state related to handling assistant messages that require user input.

Parameters

Returned Object


Usage Example

import { useAwaitCompentData } from './use-chat-logic';
import { Message, IMessage } from '@/interfaces/database/chat';

const ChatComponent = ({ messages, sendFormMessage, canvasId }) => {
  const { getInputs, buildInputList, handleOk, isWaitting } = useAwaitCompentData({
    derivedMessages: messages,
    sendFormMessage,
    canvasId,
  });

  // Find the latest assistant message with inputs
  const lastAssistantMessage = messages.findLast(
    (msg) => msg.role === MessageType.Assistant && msg.data?.inputs
  );

  if (!lastAssistantMessage) {
    return <div>No inputs required</div>;
  }

  const inputs = buildInputList(lastAssistantMessage);

  const onSubmit = handleOk(lastAssistantMessage);

  return (
    <Form inputs={inputs} onSubmit={onSubmit} disabled={!isWaitting} />
  );
};

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram - Flowchart of Main Functions and Their Relationships

flowchart TD
  A[useAwaitCompentData(props)] --> B[getInputs(message)]
  A --> C[buildInputList(message)]
  A --> D[handleOk(message)]
  A --> E[isWaitting]

  D --> F[buildBeginQueryWithObject(inputs, values)]
  D --> G[sendFormMessage({inputs, id})]

  B --> H[Extract inputs from message.data.inputs]
  C --> I[Convert inputs object to array with keys]
  E --> J[Check last message role and data presence]

  style A fill:#f9f,stroke:#333,stroke-width:2px
  style B fill:#bbf,stroke:#333
  style C fill:#bbf,stroke:#333
  style D fill:#bbf,stroke:#333
  style E fill:#bbf,stroke:#333
  style F fill:#afa,stroke:#333
  style G fill:#afa,stroke:#333
  style H fill:#eee,stroke:#333,stroke-dasharray: 4 2
  style I fill:#eee,stroke:#333,stroke-dasharray: 4 2
  style J fill:#eee,stroke:#333,stroke-dasharray: 4 2

Summary

The use-chat-logic.ts file provides a reusable hook to process chat messages that require user input before continuing the conversation. By encapsulating input extraction, transformation, submission handling, and waiting state determination, it abstracts complex chat form logic, enhancing maintainability and UI integration. This hook is a key part of the interactive chat flow handling user-driven data submission in the system.