shared-hooks.ts


Overview

The shared-hooks.ts file provides a set of custom React hooks designed to handle shared chat interactions within a web application. These hooks manage key functionalities such as parsing URL search parameters related to shared chat sessions, controlling user input and message sending logic, managing asynchronous message sending with server-sent events (SSE), and handling errors during chat completion requests.

Primarily, this file enables a chat interface to:

This file interacts closely with other hooks from the project such as chat-hooks, logic-hooks, and local hooks for message input handling, integrating with backend APIs to support real-time chat functionality.


Detailed Explanations

Utility Function: isCompletionError

const isCompletionError = (res: any) => 
  res && (res?.response.status !== 200 || res?.data?.code !== 0);

Hook: useSendButtonDisabled

export const useSendButtonDisabled = (value: string) => {
  return trim(value) === '';
};
const isDisabled = useSendButtonDisabled(userInput);
<button disabled={isDisabled}>Send</button>

Hook: useGetSharedChatSearchParams

export const useGetSharedChatSearchParams = () => { ... };
const { from, sharedId, locale, data, visibleAvatar } = useGetSharedChatSearchParams();
console.log(sharedId); // e.g., "abc123"

Hook: useSendSharedMessage

export const useSendSharedMessage = () => { ... };

Internal Implementation Details:

Usage Example:

const {
  handlePressEnter,
  handleInputChange,
  value,
  sendLoading,
  derivedMessages,
  hasError,
  stopOutputMessage,
} = useSendSharedMessage();

return (
  <ChatInput
    value={value}
    onChange={handleInputChange}
    onEnter={(docIds) => handlePressEnter(docIds)}
    disabled={sendLoading}
  />
);

Important Implementation Details and Algorithms

  1. Dynamic API Endpoint:
    The hook switches API endpoints based on the from parameter (Agent or other), allowing reuse of the hook for different chatbot sources.

  2. Session Management:
    On mount, an empty question is sent to initialize or fetch session metadata, ensuring the session ID is available for subsequent messages.

  3. Optimistic Update and Error Recovery:
    When sending a message, the UI optimistically adds the question to the message list. If the API call fails, it restores the input value and removes the latest optimistic message.

  4. Streaming Responses (SSE):
    The hook listens for streamed answers via server-sent events, incrementally updating the chat UI as the answer arrives.

  5. Message ID Handling:
    Uses UUIDs for new messages to maintain unique identification across optimistic UI updates and backend synchronization.


Interaction with Other Parts of the System

This file acts as a bridge between chat UI components and backend chat APIs, encapsulating logic for message sending workflows and shared chat session management.


Mermaid Diagram

flowchart TD
    A[useSendSharedMessage] --> B[useGetSharedChatSearchParams]
    A --> C[useCreateNextSharedConversation]
    A --> D[useHandleMessageInputChange]
    A --> E[useSendMessageWithSse]
    A --> F[useSelectDerivedMessages]
    E --> G[send message to API via SSE]
    F --> H[manage derivedMessages state]
    D --> I[handle input value and changes]
    C --> J[create new conversation if needed]
    A --> K[handlePressEnter]
    K --> L[sendMessage]
    L --> G
    A --> M[fetchSessionId]
    M --> G
    E --> N[answer updates]
    N --> H

Summary

The shared-hooks.ts file is a core utility module that provides React hooks to support shared chat functionality, including URL parameter parsing, message input management, asynchronous message sending with streaming, and error handling. It integrates multiple internal hooks and external libraries to facilitate real-time chat experience with robust state and session management. This file is essential for any UI components that render or manage shared chat conversations in the system.